From 84f1e50cb1a959f3a835b8ba265159cc6e75635a Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sun, 31 Dec 2023 00:36:01 +0530 Subject: pass/rm: init --- pass.c | 5 +++++ pass_util.c | 30 ++++++++++++++++++++++++++++-- pass_util.h | 1 + util.c | 21 +++++++++++++++++++++ util.h | 1 + 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/pass.c b/pass.c index a58276d..513c9aa 100644 --- a/pass.c +++ b/pass.c @@ -117,6 +117,11 @@ int main(int argc, char *argv[]) err_die(1, "invalid usage, try pass help"); r = add(argv[1]); + } else if (!strcmp("rm", *argv)) { + if (!argv[1]) + err_die(1, "invalid usage, try pass help"); + + r = pass_rm(argv[1]); } else { r = cat(*argv); } diff --git a/pass_util.c b/pass_util.c index 3ce3784..de8308a 100644 --- a/pass_util.c +++ b/pass_util.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -151,8 +150,9 @@ int pass_add(const char *path, const char *pass, size_t n) if (r) err_die(1, "invalid key , try gpg --list-keys"); + // TODO: guard against .*\.gpg\.gpg[/$] r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); - if (r > (int) sizeof(gpg_id_path)) + if (r > (int) sizeof(pass_path)) err_die(1, "path exceeded PATH_MAX"); rc = strdup(pass_path); @@ -174,3 +174,29 @@ int pass_add(const char *path, const char *pass, size_t n) fclose(out_stream); return r; } + +int pass_rm(const char *path) +{ + + int r = 0; + char gpg_path[PATH_MAX], abs_path[PATH_MAX]; + + r = set_pass_dir(); + if (r) + err_die(1, "PASSWORD_STORE_DIR not set"); + + r = snprintf(gpg_path, sizeof(gpg_path), "%s.gpg", path); + if (r > (int) sizeof(gpg_path)) + err_die(1, "path exceeded PATH_MAX"); + + r = snprintf(abs_path, sizeof(gpg_path), "%s/%s", pass_dir, gpg_path); + if (r > (int) sizeof(abs_path)) + err_die(1, "path exceeded PATH_MAX"); + + // TODO: guard against .*\.gpg\.gpg[/$] + r = unlink(abs_path); + if (r) + err_die(1, "%s %s", abs_path, strerror(errno)); + + return r_rmdir(pass_dir, dirname(gpg_path)); +} diff --git a/pass_util.h b/pass_util.h index eaeaa2b..d4b7fb4 100644 --- a/pass_util.h +++ b/pass_util.h @@ -3,4 +3,5 @@ int pass_init(const char *fpr); int pass_cat(FILE *out, const char *path); int pass_add(const char *path, const char *pass, size_t n); +int pass_rm(const char *path); ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream); diff --git a/util.c b/util.c index 14dfeaa..8e3a108 100644 --- a/util.c +++ b/util.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "util.h" @@ -33,6 +35,25 @@ int r_mkdir(const char *path, mode_t mode) return mkdir(path, mode); } +int r_rmdir(const char *prefix_path, char *rm_path) +{ + int r; + char abs_path[PATH_MAX]; + + if (!strcmp(rm_path, ".")) + return 0; + + r = snprintf(abs_path, sizeof(abs_path), "%s/%s", prefix_path, rm_path); + if (r > (int) sizeof(abs_path)) + err_die(1, "path exceeded PATH_MAX"); + + r = rmdir(abs_path); + if (r && errno != EEXIST && errno != ENOTEMPTY) + err_die(1, "%s", strerror(errno)); + + return r_rmdir(prefix_path, dirname(rm_path)); +} + void util_strtrim(char *s) { char *rend; diff --git a/util.h b/util.h index d3b4168..f36965a 100644 --- a/util.h +++ b/util.h @@ -11,6 +11,7 @@ } int r_mkdir(const char *path, mode_t mode); +int r_rmdir(const char *prefix_path, char *rm_path); void util_strtrim(char *s); #define UTIL_H -- cgit v1.2.3