From 4ff6158a6cd29e87222387870e6f01aca41b8183 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 6 Apr 2024 17:35:22 +0530 Subject: meson: init --- .gitignore | 2 - Makefile | 13 ---- flake.nix | 2 +- gpg.c | 130 --------------------------------- gpg.h | 6 -- include/gpg.h | 6 ++ include/libnpass.h | 7 ++ include/util.h | 18 +++++ libnpass/gpg.c | 130 +++++++++++++++++++++++++++++++++ libnpass/libnpass.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++ libnpass/meson.build | 11 +++ libnpass/util.c | 66 +++++++++++++++++ meson.build | 16 ++++ npass/meson.build | 8 ++ npass/npass.c | 133 +++++++++++++++++++++++++++++++++ pass.c | 133 --------------------------------- pass_util.c | 202 --------------------------------------------------- pass_util.h | 7 -- util.c | 66 ----------------- util.h | 18 ----- 20 files changed, 598 insertions(+), 578 deletions(-) delete mode 100644 .gitignore delete mode 100644 Makefile delete mode 100644 gpg.c delete mode 100644 gpg.h create mode 100644 include/gpg.h create mode 100644 include/libnpass.h create mode 100644 include/util.h create mode 100644 libnpass/gpg.c create mode 100644 libnpass/libnpass.c create mode 100644 libnpass/meson.build create mode 100644 libnpass/util.c create mode 100644 meson.build create mode 100644 npass/meson.build create mode 100644 npass/npass.c delete mode 100644 pass.c delete mode 100644 pass_util.c delete mode 100644 pass_util.h delete mode 100644 util.c delete mode 100644 util.h diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 5fe4433..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.o -pass diff --git a/Makefile b/Makefile deleted file mode 100644 index c636e6d..0000000 --- a/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -OBJECTS = pass.o pass_util.o util.o gpg.o -CC = gcc -CFLAGS = -g -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Wpedantic -fsanitize=address - -pass: $(OBJECTS) - $(CC) $(CFLAGS) -o pass $(OBJECTS) -l gpgme - -pass.o: pass_util.h util.h -pass_util.o: util.h gpg.h - -.PHONY: clean -clean: - rm -f pass $(OBJECTS) diff --git a/flake.nix b/flake.nix index eb7c190..f025d16 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,7 @@ default = pkgs.mkShell { name = "dev"; - buildInputs = with pkgs; [ gpgme ]; + buildInputs = with pkgs; [ gpgme meson ninja ]; shellHook = '' [ -z "$XDG_DATA_HOME" ] && export XDG_DATA_HOME="$HOME/.local/share" diff --git a/gpg.c b/gpg.c deleted file mode 100644 index 574a492..0000000 --- a/gpg.c +++ /dev/null @@ -1,130 +0,0 @@ -#include -#include -#include - -#include - -#include "gpg.h" -#include "util.h" - -#define fail_if_err(err) \ - if (err) { \ - fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, \ - gpgme_strsource(err), gpgme_strerror(err)); \ - gpg_cleanup(); \ - return 1; \ - } - -static gpgme_ctx_t ctx = NULL; -static gpgme_key_t key = NULL; - -int gpg_init(void); -void gpg_cleanup(void); - -int gpg_init(void) -{ - gpgme_error_t err; - const char *local = setlocale(LC_ALL, ""); - - gpgme_check_version(NULL); - gpgme_set_locale(NULL, LC_CTYPE, local); -#ifdef LC_MESSAGES - gpgme_set_locale(NULL, LC_MESSAGES, local); -#endif - - err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); - fail_if_err(err); - err = gpgme_new(&ctx); - fail_if_err(err); - - return 0; -} - -void gpg_cleanup(void) -{ - if (ctx) - gpgme_key_release(key); - if (key) - gpgme_release(ctx); -} - -int gpg_key_validate(const char *fpr) -{ - int r; - gpgme_error_t err; - - r = gpg_init(); - if (r) - return r; - - err = gpgme_get_key(ctx, fpr, &key, 1); - fail_if_err(err); - - gpg_cleanup(); - return 0; -} - -int gpg_decrypt(FILE *pass_out, const char *pass_path) -{ - int r; - char buf[BUFSIZ]; - gpgme_data_t in, out; - gpgme_error_t err; - - r = gpg_init(); - if (r) - return r; - - err = gpgme_data_new_from_file(&in, pass_path, 1); - fail_if_err(err); - err = gpgme_data_new(&out); - fail_if_err(err); - err = gpgme_op_decrypt(ctx, in, out); - fail_if_err(err); - - r = gpgme_data_seek(out, 0, SEEK_SET); - if (r) - fail_if_err (gpgme_err_code_from_errno(errno)); - - while ((r = gpgme_data_read(out, buf, sizeof(buf)))) - fwrite(buf, r, 1, pass_out); - if (r < 0) - fail_if_err(gpgme_err_code_from_errno(errno)); - - gpg_cleanup(); - return 0; -} - -int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n) -{ - int r; - char buf[BUFSIZ]; - gpgme_data_t in, out; - gpgme_error_t err; - - r = gpg_init(); - if (r) - return r; - - err = gpgme_get_key(ctx, fpr, &key, 1); - fail_if_err(err); - - err = gpgme_data_new_from_mem(&in, pass, n, 0); - fail_if_err(err); - err = gpgme_data_new(&out); - fail_if_err(err); - err = gpgme_op_encrypt(ctx, &key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); - fail_if_err(err); - - r = gpgme_data_seek(out, 0, SEEK_SET); - if (r) - fail_if_err (gpgme_err_code_from_errno(errno)); - - while ((r = gpgme_data_read(out, buf, sizeof(buf)))) - fwrite(buf, r, 1, stream); - gpg_cleanup(); - if (r < 0) - fail_if_err(gpgme_err_code_from_errno(errno)); - - return 0; -} diff --git a/gpg.h b/gpg.h deleted file mode 100644 index f5053bf..0000000 --- a/gpg.h +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int gpg_key_validate(const char *fpr); -int gpg_decrypt(FILE *pass_out, const char *pass_path); -int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n); diff --git a/include/gpg.h b/include/gpg.h new file mode 100644 index 0000000..f5053bf --- /dev/null +++ b/include/gpg.h @@ -0,0 +1,6 @@ +#include +#include + +int gpg_key_validate(const char *fpr); +int gpg_decrypt(FILE *pass_out, const char *pass_path); +int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n); diff --git a/include/libnpass.h b/include/libnpass.h new file mode 100644 index 0000000..d4b7fb4 --- /dev/null +++ b/include/libnpass.h @@ -0,0 +1,7 @@ +#include + +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/include/util.h b/include/util.h new file mode 100644 index 0000000..f36965a --- /dev/null +++ b/include/util.h @@ -0,0 +1,18 @@ +#ifndef UTIL_H + +#include +#include + +#define err_die(r, ...) {\ + fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fputc('\n', stderr); \ + return r; \ +} + +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 +#endif diff --git a/libnpass/gpg.c b/libnpass/gpg.c new file mode 100644 index 0000000..574a492 --- /dev/null +++ b/libnpass/gpg.c @@ -0,0 +1,130 @@ +#include +#include +#include + +#include + +#include "gpg.h" +#include "util.h" + +#define fail_if_err(err) \ + if (err) { \ + fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, \ + gpgme_strsource(err), gpgme_strerror(err)); \ + gpg_cleanup(); \ + return 1; \ + } + +static gpgme_ctx_t ctx = NULL; +static gpgme_key_t key = NULL; + +int gpg_init(void); +void gpg_cleanup(void); + +int gpg_init(void) +{ + gpgme_error_t err; + const char *local = setlocale(LC_ALL, ""); + + gpgme_check_version(NULL); + gpgme_set_locale(NULL, LC_CTYPE, local); +#ifdef LC_MESSAGES + gpgme_set_locale(NULL, LC_MESSAGES, local); +#endif + + err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); + fail_if_err(err); + err = gpgme_new(&ctx); + fail_if_err(err); + + return 0; +} + +void gpg_cleanup(void) +{ + if (ctx) + gpgme_key_release(key); + if (key) + gpgme_release(ctx); +} + +int gpg_key_validate(const char *fpr) +{ + int r; + gpgme_error_t err; + + r = gpg_init(); + if (r) + return r; + + err = gpgme_get_key(ctx, fpr, &key, 1); + fail_if_err(err); + + gpg_cleanup(); + return 0; +} + +int gpg_decrypt(FILE *pass_out, const char *pass_path) +{ + int r; + char buf[BUFSIZ]; + gpgme_data_t in, out; + gpgme_error_t err; + + r = gpg_init(); + if (r) + return r; + + err = gpgme_data_new_from_file(&in, pass_path, 1); + fail_if_err(err); + err = gpgme_data_new(&out); + fail_if_err(err); + err = gpgme_op_decrypt(ctx, in, out); + fail_if_err(err); + + r = gpgme_data_seek(out, 0, SEEK_SET); + if (r) + fail_if_err (gpgme_err_code_from_errno(errno)); + + while ((r = gpgme_data_read(out, buf, sizeof(buf)))) + fwrite(buf, r, 1, pass_out); + if (r < 0) + fail_if_err(gpgme_err_code_from_errno(errno)); + + gpg_cleanup(); + return 0; +} + +int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n) +{ + int r; + char buf[BUFSIZ]; + gpgme_data_t in, out; + gpgme_error_t err; + + r = gpg_init(); + if (r) + return r; + + err = gpgme_get_key(ctx, fpr, &key, 1); + fail_if_err(err); + + err = gpgme_data_new_from_mem(&in, pass, n, 0); + fail_if_err(err); + err = gpgme_data_new(&out); + fail_if_err(err); + err = gpgme_op_encrypt(ctx, &key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); + fail_if_err(err); + + r = gpgme_data_seek(out, 0, SEEK_SET); + if (r) + fail_if_err (gpgme_err_code_from_errno(errno)); + + while ((r = gpgme_data_read(out, buf, sizeof(buf)))) + fwrite(buf, r, 1, stream); + gpg_cleanup(); + if (r < 0) + fail_if_err(gpgme_err_code_from_errno(errno)); + + return 0; +} diff --git a/libnpass/libnpass.c b/libnpass/libnpass.c new file mode 100644 index 0000000..72e51df --- /dev/null +++ b/libnpass/libnpass.c @@ -0,0 +1,202 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libnpass.h" +#include "util.h" +#include "gpg.h" + +#define DEF_PASS_DIR "pass" +#define FPR_MAX 256 + +static char pass_dir[PATH_MAX] = {0}; + +int set_pass_dir(void); + +int set_pass_dir(void) { + const char *env; + + env = getenv("PASSWORD_STORE_DIR"); + if (env) { + strncpy(pass_dir, env, sizeof(pass_dir) - 1); + return 0; + } + + env = getenv("XDG_DATA_HOME"); + if (env) { + snprintf(pass_dir, sizeof(pass_dir) - 1, "%s/%s", env, + DEF_PASS_DIR); + return 0; + } + + env = getenv("HOME"); + if (env) { + snprintf(pass_dir, sizeof(pass_dir) - 1, "%s/%s/%s", env, + ".local/share", DEF_PASS_DIR); + return 0; + } + + return 1; +} + +int pass_init(const char *fpr) +{ + int r; + char gpg_id_path[PATH_MAX]; + FILE *gpg_id; + + r = set_pass_dir(); + if (r) + err_die(1, "PASSWORD_STORE_DIR not set"); + + r = gpg_key_validate(fpr); + if (r) + err_die(1, "key not usable, try gpg --full-generate-key"); + + + r = r_mkdir(pass_dir, S_IRWXU); + if (r) + 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(1, "path exceeded PATH_MAX"); + + gpg_id = fopen(gpg_id_path, "w"); + if (!gpg_id) + 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(1, "write failed"); + + return 0; +} + +int pass_cat(FILE *out, const char *path) +{ + int r; + char pass_path[PATH_MAX]; + + r = set_pass_dir(); + if (r) + err_die(1, "PASSWORD_STORE_DIR not set"); + + r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); + if (r >= (int) sizeof(pass_path)) + err_die(1, "path exceeded PATH_MAX"); + + r = gpg_decrypt(out, pass_path); + return r; +} + +ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream) +{ + ssize_t r; + struct termios new, old; + + r = tcgetattr(fileno(stream), &old); + if (r) + return -1; + + new = old; + new.c_lflag &= ~ECHO; + r = tcsetattr(fileno(stream), TCSAFLUSH, &new); + if (r) + return -1; + + r = getline (lineptr, n, stream); + if (r > 0 && (*lineptr)[r - 1] == '\n') + (*lineptr)[r - 1] = '\0'; + + (void) tcsetattr (fileno (stream), TCSAFLUSH, &old); + + return r; +} + +int pass_add(const char *path, const char *pass, size_t n) +{ + int r; + char *rc; + FILE *gpg_id, *out_stream; + char gpg_id_path[PATH_MAX], fpr[FPR_MAX], pass_path[PATH_MAX]; + + r = set_pass_dir(); + if (r) + err_die(1, "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(1, "path exceeded PATH_MAX"); + + gpg_id = fopen(gpg_id_path, "r"); + if (!gpg_id) + err_die(1, "%s %s", gpg_id_path, strerror(errno)); + + rc = fgets(fpr, sizeof(fpr), gpg_id); + if (!rc) + err_die(1, "failed to read %s", gpg_id_path); + fclose(gpg_id); + util_strtrim(fpr); + + r = gpg_key_validate(fpr); + 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(pass_path)) + err_die(1, "path exceeded PATH_MAX"); + + rc = strdup(pass_path); + if (!rc) + err_die(1, "%s", strerror(errno)); + (void) r_mkdir(dirname(rc), S_IRWXU); + free(rc); + + r = access(pass_path, F_OK); + if (!(errno & ENOENT)) + err_die(1, "an entry already exists for %s", path); + + out_stream = fopen(pass_path, "w"); + if (!out_stream) + err_die(1, "%s", strerror(errno)); + + r = gpg_encrypt(out_stream, fpr, pass, 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/libnpass/meson.build b/libnpass/meson.build new file mode 100644 index 0000000..1ce4621 --- /dev/null +++ b/libnpass/meson.build @@ -0,0 +1,11 @@ +lib_npass = static_library( + 'libnpass', + [ + 'util.c', + 'gpg.c', + 'libnpass.c' + ], + + dependencies: gpgme_dep, + include_directories: npass_inc, +) diff --git a/libnpass/util.c b/libnpass/util.c new file mode 100644 index 0000000..8e3a108 --- /dev/null +++ b/libnpass/util.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "util.h" + +int r_mkdir(const char *path, mode_t mode) +{ + int r; + size_t len; + char *p; + char tmp[NAME_MAX + 1]; + + strncpy(tmp, path, sizeof(tmp) - 1); + len = strlen(tmp); + if(tmp[len - 1] == '/') + tmp[len - 1] = '\0'; + + for (p = tmp + 1; *p; ++p) { + if (*p == '/') { + *p = '\0'; + + r = mkdir(tmp, mode); + if (r && !(errno & EEXIST)) + return r; + + *p = '/'; + } + } + + 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; + + for (rend = s; *s; ++s) + if (!isspace(*s)) + rend = s; + + rend[1] = '\0'; +} diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..a0c5988 --- /dev/null +++ b/meson.build @@ -0,0 +1,16 @@ +project( + 'npass', + 'c', + version : '0.1', + license: 'GPL-3.0-only', + default_options : [ + 'warning_level=3', + ] +) + +gpgme_dep = dependency('gpgme', version: '>= 1.0') + +npass_inc = include_directories('include') + +subdir('libnpass') +subdir('npass') diff --git a/npass/meson.build b/npass/meson.build new file mode 100644 index 0000000..2371298 --- /dev/null +++ b/npass/meson.build @@ -0,0 +1,8 @@ +executable( + 'pass', + 'npass.c', + + include_directories: npass_inc, + link_with: lib_npass, + install: true, +) diff --git a/npass/npass.c b/npass/npass.c new file mode 100644 index 0000000..8aacc14 --- /dev/null +++ b/npass/npass.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include + +#include "libnpass.h" +#include "util.h" + +void print_usage(void); +int cat(const char *path); +int add(const char *path); + +void print_usage(void) +{ + printf("Usage: pass COMMAND\n\n" + + "Commands:\n" + " init key-id/fingerprint\n" + " Initialize new password storage\n" + " ls [ pass-path ]\n" + " List passwords\n" + " rm pass-name\n" + " Remove password\n" + " add pass-name\n" + " Add new password\n" + " gen pass-name\n" + " Generate new password\n" + " cat pass-name\n" + " Show encrypted password\n" + " help\n" + " Show this help\n"); +} + +int cat(const char *path) +{ + int r; + + r = pass_cat(stdout, path); + if (!r && isatty(STDOUT_FILENO)) + putchar('\n'); + + return r; +} + +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); + 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 (p1) + free(p1); + err_die(1, "%d:%s:", errno, strerror(errno)); + } + + if (in != stdin) + fclose(in); + + 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; + + if (!--argc) { + print_usage(); + exit(EXIT_FAILURE); + } + ++argv; + + + if (!strcmp("help", *argv)) { + print_usage(); + } else if (!strcmp("init", *argv)) { + if (argc != 2) + err_die(1, "invalid usage, try pass help"); + + r = pass_init(argv[1]); + } else if (!strcmp("cat", *argv)) { + if (argc != 2) + err_die(1, "invalid usage, try pass help"); + + r = cat(argv[1]); + } else if (!strcmp("add", *argv)) { + if (argc != 2) + err_die(1, "invalid usage, try pass help"); + + r = add(argv[1]); + } else if (!strcmp("rm", *argv)) { + if (argc != 2) + err_die(1, "invalid usage, try pass help"); + + r = pass_rm(argv[1]); + } else { + r = cat(*argv); + } + + if (r) + err_die(r, "Command failed"); + + return 0; +} diff --git a/pass.c b/pass.c deleted file mode 100644 index 91a4817..0000000 --- a/pass.c +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include -#include -#include -#include - -#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) -{ - printf("Usage: pass COMMAND\n\n" - - "Commands:\n" - " init key-id/fingerprint\n" - " Initialize new password storage\n" - " ls [ pass-path ]\n" - " List passwords\n" - " rm pass-name\n" - " Remove password\n" - " add pass-name\n" - " Add new password\n" - " gen pass-name\n" - " Generate new password\n" - " cat pass-name\n" - " Show encrypted password\n" - " help\n" - " Show this help\n"); -} - -int cat(const char *path) -{ - int r; - - r = pass_cat(stdout, path); - if (!r && isatty(STDOUT_FILENO)) - putchar('\n'); - - return r; -} - -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); - 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 (p1) - free(p1); - err_die(1, "%d:%s:", errno, strerror(errno)); - } - - if (in != stdin) - fclose(in); - - 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; - - if (!--argc) { - print_usage(); - exit(EXIT_FAILURE); - } - ++argv; - - - if (!strcmp("help", *argv)) { - print_usage(); - } else if (!strcmp("init", *argv)) { - if (argc != 2) - err_die(1, "invalid usage, try pass help"); - - r = pass_init(argv[1]); - } else if (!strcmp("cat", *argv)) { - if (argc != 2) - err_die(1, "invalid usage, try pass help"); - - r = cat(argv[1]); - } else if (!strcmp("add", *argv)) { - if (argc != 2) - err_die(1, "invalid usage, try pass help"); - - r = add(argv[1]); - } else if (!strcmp("rm", *argv)) { - if (argc != 2) - err_die(1, "invalid usage, try pass help"); - - r = pass_rm(argv[1]); - } else { - r = cat(*argv); - } - - if (r) - err_die(r, "Command failed"); - - return 0; -} diff --git a/pass_util.c b/pass_util.c deleted file mode 100644 index de8308a..0000000 --- a/pass_util.c +++ /dev/null @@ -1,202 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "pass_util.h" -#include "util.h" -#include "gpg.h" - -#define DEF_PASS_DIR "pass" -#define FPR_MAX 256 - -static char pass_dir[PATH_MAX] = {0}; - -int set_pass_dir(void); - -int set_pass_dir(void) { - const char *env; - - env = getenv("PASSWORD_STORE_DIR"); - if (env) { - strncpy(pass_dir, env, sizeof(pass_dir) - 1); - return 0; - } - - env = getenv("XDG_DATA_HOME"); - if (env) { - snprintf(pass_dir, sizeof(pass_dir) - 1, "%s/%s", env, - DEF_PASS_DIR); - return 0; - } - - env = getenv("HOME"); - if (env) { - snprintf(pass_dir, sizeof(pass_dir) - 1, "%s/%s/%s", env, - ".local/share", DEF_PASS_DIR); - return 0; - } - - return 1; -} - -int pass_init(const char *fpr) -{ - int r; - char gpg_id_path[PATH_MAX]; - FILE *gpg_id; - - r = set_pass_dir(); - if (r) - err_die(1, "PASSWORD_STORE_DIR not set"); - - r = gpg_key_validate(fpr); - if (r) - err_die(1, "key not usable, try gpg --full-generate-key"); - - - r = r_mkdir(pass_dir, S_IRWXU); - if (r) - 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(1, "path exceeded PATH_MAX"); - - gpg_id = fopen(gpg_id_path, "w"); - if (!gpg_id) - 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(1, "write failed"); - - return 0; -} - -int pass_cat(FILE *out, const char *path) -{ - int r; - char pass_path[PATH_MAX]; - - r = set_pass_dir(); - if (r) - err_die(1, "PASSWORD_STORE_DIR not set"); - - r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); - if (r >= (int) sizeof(pass_path)) - err_die(1, "path exceeded PATH_MAX"); - - r = gpg_decrypt(out, pass_path); - return r; -} - -ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream) -{ - ssize_t r; - struct termios new, old; - - r = tcgetattr(fileno(stream), &old); - if (r) - return -1; - - new = old; - new.c_lflag &= ~ECHO; - r = tcsetattr(fileno(stream), TCSAFLUSH, &new); - if (r) - return -1; - - r = getline (lineptr, n, stream); - if (r > 0 && (*lineptr)[r - 1] == '\n') - (*lineptr)[r - 1] = '\0'; - - (void) tcsetattr (fileno (stream), TCSAFLUSH, &old); - - return r; -} - -int pass_add(const char *path, const char *pass, size_t n) -{ - int r; - char *rc; - FILE *gpg_id, *out_stream; - char gpg_id_path[PATH_MAX], fpr[FPR_MAX], pass_path[PATH_MAX]; - - r = set_pass_dir(); - if (r) - err_die(1, "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(1, "path exceeded PATH_MAX"); - - gpg_id = fopen(gpg_id_path, "r"); - if (!gpg_id) - err_die(1, "%s %s", gpg_id_path, strerror(errno)); - - rc = fgets(fpr, sizeof(fpr), gpg_id); - if (!rc) - err_die(1, "failed to read %s", gpg_id_path); - fclose(gpg_id); - util_strtrim(fpr); - - r = gpg_key_validate(fpr); - 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(pass_path)) - err_die(1, "path exceeded PATH_MAX"); - - rc = strdup(pass_path); - if (!rc) - err_die(1, "%s", strerror(errno)); - (void) r_mkdir(dirname(rc), S_IRWXU); - free(rc); - - r = access(pass_path, F_OK); - if (!(errno & ENOENT)) - err_die(1, "an entry already exists for %s", path); - - out_stream = fopen(pass_path, "w"); - if (!out_stream) - err_die(1, "%s", strerror(errno)); - - r = gpg_encrypt(out_stream, fpr, pass, 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 deleted file mode 100644 index d4b7fb4..0000000 --- a/pass_util.h +++ /dev/null @@ -1,7 +0,0 @@ -#include - -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 deleted file mode 100644 index 8e3a108..0000000 --- a/util.c +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "util.h" - -int r_mkdir(const char *path, mode_t mode) -{ - int r; - size_t len; - char *p; - char tmp[NAME_MAX + 1]; - - strncpy(tmp, path, sizeof(tmp) - 1); - len = strlen(tmp); - if(tmp[len - 1] == '/') - tmp[len - 1] = '\0'; - - for (p = tmp + 1; *p; ++p) { - if (*p == '/') { - *p = '\0'; - - r = mkdir(tmp, mode); - if (r && !(errno & EEXIST)) - return r; - - *p = '/'; - } - } - - 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; - - for (rend = s; *s; ++s) - if (!isspace(*s)) - rend = s; - - rend[1] = '\0'; -} diff --git a/util.h b/util.h deleted file mode 100644 index f36965a..0000000 --- a/util.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef UTIL_H - -#include -#include - -#define err_die(r, ...) {\ - fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); \ - fputc('\n', stderr); \ - return r; \ -} - -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 -#endif -- cgit v1.2.3