diff options
Diffstat (limited to 'pass.c')
-rw-r--r-- | pass.c | 65 |
1 files changed, 64 insertions, 1 deletions
@@ -1,12 +1,14 @@ #include <stdio.h> #include <string.h> #include <stdlib.h> +#include <errno.h> #include "pass_util.h" #include "util.h" void print_usage(void); int cat(const char *path); +int add(const char *path); void print_usage(void) { @@ -40,6 +42,59 @@ int cat(const char *path) return (s == NULL); } +int add(const char *path) +{ + char *p1 = NULL, *p2 = NULL; + FILE *in; + size_t n; + int r; + + in = fopen("/dev/tty", "r"); + if (!in) + in = stdin; + + fputs("Password: ", stdout); + r = pass_getpass(&p1, &n, in); + if (r < 0) { + if (in != stdin) + fclose(in); + if (p1) + free(p1); + err_die(1, "%d:%s:", errno, strerror(errno)); + } + + fputs("\nRetype password: ", stdout); + r = pass_getpass(&p2, &n, in); + putc('\n', stdout); + if (r < 0) { + if (in != stdin) + fclose(in); + if (p2) + free(p1); + if (p2) + free(p2); + err_die(1, "%d:%s:", errno, strerror(errno)); + } + + if (in != stdin) + fclose(in); + + if (n > PASS_MAX - 1) /* TODO: get rid of the limit */ + err_die(1, "password must not exceed %d characters", PASS_MAX); + + if (strcmp(p1, p2)) { + free(p1); + free(p2); + err_die(1, "Sorry, passwords do not match"); + } + + free(p1); + r = pass_add(path, p2, n); + + free(p2); + return r; +} + int main(int argc, char *argv[]) { int r = 0; @@ -63,7 +118,15 @@ int main(int argc, char *argv[]) err_die(1, "invalid usage, try pass help"); r = cat(argv[1]); + } else if (!strcmp("add", *argv)) { + if (!argv[1]) + err_die(1, "invalid usage, try pass help"); + + r = add(argv[1]); } - return r; + if (r) + err_die(r, "Command '%s' failed", *argv); + + return 0; } |