aboutsummaryrefslogtreecommitdiff
path: root/pass_util.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2023-12-30 11:14:24 +0530
committersinanmohd <sinan@sinanmohd.com>2023-12-30 11:14:24 +0530
commit705ed7afd5c707419e02fca1b97f2a6516453506 (patch)
tree90443e523fb9390b579053d2c76f34a66daab249 /pass_util.c
parent438ad16d03f38e0e444f6ad575078ee949679a86 (diff)
pass/cat: init
Diffstat (limited to 'pass_util.c')
-rw-r--r--pass_util.c53
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;
}