diff options
Diffstat (limited to 'pass_util.c')
-rw-r--r-- | pass_util.c | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/pass_util.c b/pass_util.c index bcc4aaa..47e72f2 100644 --- a/pass_util.c +++ b/pass_util.c @@ -11,7 +11,8 @@ #define DEF_PASS_DIR "pass" -char pass_dir[PATH_MAX]; +static char pass_dir[PATH_MAX] = {0}; +static char pass_out[PASS_MAX] = {0}; int set_pass_dir(void); @@ -49,30 +50,66 @@ int pass_init(const char *fpr) r = set_pass_dir(); if (r) - err_die("PASSWORD_STORE_DIR not set"); + err_die(1, "PASSWORD_STORE_DIR not set"); r = gpg_key_validate(fpr); if (r) - err_die("key not usable, try gpg --full-generate-key"); + err_die(1, "key not usable, try gpg --full-generate-key"); r = r_mkdir(pass_dir, S_IRWXU); if (r) - err_die("%s %s", pass_dir, strerror(errno)); + err_die(1, "%s %s", pass_dir, strerror(errno)); r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); if (r > (int) sizeof(gpg_id_path)) - err_die("path exceeded PATH_MAX"); + err_die(1, "path exceeded PATH_MAX"); gpg_id = fopen(gpg_id_path, "w"); if (!gpg_id) - err_die("%s %s", gpg_id_path, strerror(errno)); + err_die(1, "%s %s", gpg_id_path, strerror(errno)); r = fwrite(fpr, strlen(fpr), 1,gpg_id); + fclose(gpg_id); if (!r) - err_die("write failed"); + err_die(1, "write failed"); + + return 0; +} + +const char *pass_cat(const char *path) +{ + int r; + char *rc; + char gpg_id_path[PATH_MAX], fpr[FPR_MAX], pass_path[PATH_MAX]; + FILE *gpg_id; + + r = set_pass_dir(); + if (r) + err_die(NULL, "PASSWORD_STORE_DIR not set"); + + r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); + if (r > (int) sizeof(gpg_id_path)) + err_die(NULL, "path exceeded PATH_MAX"); + + gpg_id = fopen(gpg_id_path, "r"); + if (!gpg_id) + err_die(NULL, "%s %s", gpg_id_path, strerror(errno)); + + rc = fgets(fpr, sizeof(fpr), gpg_id); + if (!rc) + err_die(NULL, "failed to read %s", gpg_id_path); fclose(gpg_id); - return 0; + r = gpg_key_validate(fpr); + if (r) + err_die(NULL, "key not usable, try gpg --list-keys"); + + r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); + if (r > (int) sizeof(gpg_id_path)) + err_die(NULL, "path exceeded PATH_MAX"); + + r = gpg_decrypt(fpr, pass_path, pass_out, sizeof(pass_out)); + return r ? NULL : pass_out; } |