pass

A stripped down version of the standard unix password manager "pass"
git clone git://nocebo.space/pass
Log | Files | Refs | LICENSE

commit d73960aadcda4fdf4f787cdf5f0b435873e57bc9
parent ad97f92d3dc0fb15decca22c6a79f09c9bc6d743
Author: Naveen Narayanan zerous <zerous@nocebo.space>
Date:   Wed, 20 Dec 2017 19:44:05 +0300

Implement printpass

Diffstat:
pass.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 111 insertions(+), 6 deletions(-)

diff --git a/pass.c b/pass.c @@ -34,14 +34,14 @@ find (char *item) void delete (char *item) -{ +{ (void)item; } void list(void) { - return; + } @@ -54,7 +54,113 @@ usage(void) void printpass(char *item) { - (void)item; + char file[PATH_MAX], uid[128], buf[512]; + const char* home; + int fin; + FILE* fp; + /*mode_t mode = S_IRWXU;*/ + gpgme_data_t in, out; + gpgme_error_t gpgerr; + gpgme_ctx_t ctx; + /*gpgme_key_t key;*/ + /*gpgme_key_t keys[2];*/ + gpgme_protocol_t proto; + gpgme_decrypt_result_t res; + + /* + * Algo: + * Check if file exists + * If it does + * get the uid + * create gpg objects + * open file + * decrypt + * print data + */ + + (void)uid; + + initgpgme(); + proto = GPGME_PROTOCOL_OpenPGP; + + if (!(home = getenv("HOME"))) + errx(1, "$HOME not set, cannot determine password-store location"); + snprintf(file, sizeof(file), "%s/.password-store/%s.gpg", home, item); + + /*printf("file: %s\n", file);*/ + + /* Check if file exists */ + fin = open(file, O_RDONLY); + if (fin == -1) { + if (debug) + fatal("%s is not in password store.", file); + else + errx(1, "%s is not in password store.", item); + } + + + + if (gpgme_err_code(gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) + errx(1, "gpme_new"); + + gpgerr = gpgme_set_protocol(ctx, proto); + if (gpgme_err_code(gpgerr) == GPG_ERR_INV_VALUE) + errx(1, "gpgme_set_protocol"); + + gpgerr = gpgme_data_new_from_file(&in, file, 1); + if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR) + err(1, "gpgme_data_new_from_fd: %s\n", gpgme_strerror(gpgerr)); + + gpgerr = gpgme_data_new(&out); + if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR) + err(1, "gpgme_data_new: %s\n", gpgme_strerror(gpgerr)); + + /* set ascii armor */ + /*gpgme_set_armor(ctx, 1);*/ + + /* decrypt */ + gpgerr = gpgme_op_decrypt(ctx, in, out); + if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR) + err(1, " gpgme_op_decrypt: %s\n", gpgme_strerror(gpgerr)); + + (void)res; + /*res = gpgme_op_decrypt_result(ctx);*/ + /*printf("res: %s\n, %d\n, %s\n",*/ + /*res->unsupported_algorithm,*/ + /*res->wrong_key_usage,*/ + /*res->file_name);*/ + + /* print contents of data object */ + /*int ret = gpgme_data_seek(out, 0, SEEK_SET); */ + /*printf("here\n");*/ + /*if (ret) {*/ + /*printf("here\n");*/ + + /*printf("gpgme_data_get_encoding: %s", gpgme_data_get_encoding(out));*/ + + + int a, ret; + ret = gpgme_data_seek(out, 0, SEEK_SET); + if (ret) + err(1, "gpgme_data_seek"); + if ((a = gpgme_data_read(out, buf, 100)) > 0) { + /*printf("a : %d", a);*/ + buf[a] = '\0'; + } + printf("%s\n", buf); + /*if ((gpgme_data_read(out, buf, 2)) > 0) {*/ + /*fwrite(buf, ret, 1, stdout);*/ + /*printf("%s", buf);*/ + /*}*/ + /*if (ret < 0)*/ + /*err(1, "can not find key: %s\n", gpgme_strerror(gpgerr));*/ + /*}*/ + + /*gpgme_key_release(key);*/ + gpgme_data_release(in); + gpgme_data_release(out); + gpgme_release(ctx); + close(fin); } void @@ -169,7 +275,6 @@ insert(char *item) fin = fopen(file, "r"); memcpy(t, file, strlen(file) + 1); - printf("t: %s\n", t); snprintf(file, sizeof(file), "%s.gpg", file); fout = fopen(file, "w"); gpgerr = gpgme_data_new_from_stream(&in, fin); @@ -261,7 +366,7 @@ initpass(char *pgpid) int main(int argc, char** argv) { - debug = 1; + debug = 0; if (debug) loginit("pass"); if (argc > 1) @@ -279,7 +384,7 @@ main(int argc, char** argv) else if (!strcmp("find", *argv)) find(*(argv + 1)); else - printpass(*(argv + 1)); + printpass(*argv); } else { list();