diff options
-rw-r--r-- | pass.c | 5 | ||||
-rw-r--r-- | pass_util.c | 30 | ||||
-rw-r--r-- | pass_util.h | 1 | ||||
-rw-r--r-- | util.c | 21 | ||||
-rw-r--r-- | util.h | 1 |
5 files changed, 56 insertions, 2 deletions
@@ -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 <errno.h> #include <string.h> #include <sys/stat.h> -#include <fcntl.h> #include <linux/limits.h> #include <libgen.h> #include <termios.h> @@ -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); @@ -3,6 +3,8 @@ #include <errno.h> #include <linux/limits.h> #include <ctype.h> +#include <unistd.h> +#include <libgen.h> #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; @@ -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 |