diff options
author | sinanmohd <sinan@sinanmohd.com> | 2023-12-30 19:00:32 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2023-12-30 19:00:46 +0530 |
commit | 7960f6b6d80d64001282b7b0b43c0195645cc35c (patch) | |
tree | 99db4218323383fbea99faccfed2b67fcd9735f3 | |
parent | 1a56879bb29e307bd6b00e250e67597235fa0adf (diff) |
pass: support unlimited password length
-rw-r--r-- | gpg.c | 15 | ||||
-rw-r--r-- | gpg.h | 2 | ||||
-rw-r--r-- | pass.c | 18 | ||||
-rw-r--r-- | pass_util.c | 11 | ||||
-rw-r--r-- | pass_util.h | 4 |
5 files changed, 20 insertions, 30 deletions
@@ -64,9 +64,10 @@ int gpg_key_validate(const char *fpr) return 0; } -int gpg_decrypt(const char *path, char *pass_out, size_t n) +int gpg_decrypt(FILE *pass_out, const char *pass_path) { int r; + char buf[BUFSIZ]; gpgme_data_t in, out; gpgme_error_t err; @@ -74,7 +75,7 @@ int gpg_decrypt(const char *path, char *pass_out, size_t n) if (r) return r; - err = gpgme_data_new_from_file(&in, path, 1); + err = gpgme_data_new_from_file(&in, pass_path, 1); fail_if_err(err); err = gpgme_data_new(&out); fail_if_err(err); @@ -84,13 +85,13 @@ int gpg_decrypt(const char *path, char *pass_out, size_t n) r = gpgme_data_seek(out, 0, SEEK_SET); if (r) fail_if_err (gpgme_err_code_from_errno(errno)); - r = gpgme_data_read(out, pass_out, n); - gpg_cleanup(); + + 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)); - // if (r) // TODO: upstream: did not return 0 despite eob - // err_die(r, "did not reach end of object"); + gpg_cleanup(); return 0; } @@ -119,7 +120,7 @@ int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n) if (r) fail_if_err (gpgme_err_code_from_errno(errno)); - while ((r = gpgme_data_read(out, buf, BUFSIZ))) + while ((r = gpgme_data_read(out, buf, sizeof(buf)))) fwrite(buf, r, 1, stream); gpg_cleanup(); if (r < 0) @@ -2,5 +2,5 @@ #include <sys/types.h> int gpg_key_validate(const char *fpr); -int gpg_decrypt(const char *path, char *pass_out, size_t n); +int gpg_decrypt(FILE *pass_out, const char *pass_path); int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n); @@ -34,18 +34,13 @@ void print_usage(void) int cat(const char *path) { - const char *s = NULL; - - s = pass_cat(path); - if (!s) - return -1; + int r; - if(isatty(STDOUT_FILENO)) - puts(s); - else - fputs(s, stdout); + r = pass_cat(stdout, path); + if (!r && isatty(STDOUT_FILENO)) + putchar('\n'); - return 0; + return r; } int add(const char *path) @@ -81,9 +76,6 @@ int add(const char *path) if (in != stdin) fclose(in); - if (n > PASS_MAX - 1) /* TODO: get rid of the limit */ - err_die(1, "password must not exceed %d characters", PASS_MAX); - if (strcmp(p1, p2)) { free(p1); free(p2); diff --git a/pass_util.c b/pass_util.c index 7147681..d9d6fc3 100644 --- a/pass_util.c +++ b/pass_util.c @@ -17,7 +17,6 @@ #define FPR_MAX 256 static char pass_dir[PATH_MAX] = {0}; -static char pass_out[PASS_MAX] = {0}; int set_pass_dir(void); @@ -82,21 +81,21 @@ int pass_init(const char *fpr) return 0; } -const char *pass_cat(const char *path) +int pass_cat(FILE *out, const char *path) { int r; char pass_path[PATH_MAX]; r = set_pass_dir(); if (r) - err_die(NULL, "PASSWORD_STORE_DIR not set"); + 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(NULL, "path exceeded PATH_MAX"); + err_die(1, "path exceeded PATH_MAX"); - r = gpg_decrypt(pass_path, pass_out, sizeof(pass_out)); - return r ? NULL : pass_out; + r = gpg_decrypt(out, pass_path); + return r; } ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream) diff --git a/pass_util.h b/pass_util.h index aa49c8a..eaeaa2b 100644 --- a/pass_util.h +++ b/pass_util.h @@ -1,8 +1,6 @@ #include <stdio.h> -#define PASS_MAX 4096 - int pass_init(const char *fpr); -const char *pass_cat(const char *path); +int pass_cat(FILE *out, const char *path); int pass_add(const char *path, const char *pass, size_t n); ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream); |