commit d9c3288a67719934f75745b79dc99efc69010f7e
parent b4db9c0f618366510ec7022becc5d7fbcafcb079
Author: Naveen Narayanan zerous <zerous@nocebo.space>
Date: Fri, 29 Dec 2017 12:53:30 +0300
detangle gpg and file operations
- move gpg encrypt calls to encrypt()
- move gpg decrypt calls to decrypt()
Diffstat:
pass.c | | | 167 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
1 file changed, 91 insertions(+), 76 deletions(-)
diff --git a/pass.c b/pass.c
@@ -63,29 +63,16 @@ usage(void)
}
void
-printpass(char *item)
+decrypt(char *buf)
{
- char buf[512];
- const char* home;
- int fin, a, ret;
-
gpgme_data_t in, out;
gpgme_error_t gpgerr;
gpgme_ctx_t ctx;
gpgme_protocol_t proto;
+ int a, ret;
initgpgme();
proto = GPGME_PROTOCOL_OpenPGP;
-
- if (!(home = getenv("HOME")))
- fatalx("$HOME not set, cannot determine password-store location");
- snprintf(file, sizeof(file), "%s/.password-store/%s.gpg", home, item);
-
- /* Check if file exists */
- fin = open(file, O_RDONLY);
- if (fin == -1) {
- fatal("%s is not in password store.", file);
- }
gpgerr = gpgme_new(&ctx);
if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR) {
@@ -115,17 +102,38 @@ printpass(char *item)
if ((a = gpgme_data_read(out, buf, 100)) > 0) {
buf[a] = '\0';
}
- printf("%s\n", buf);
gpgme_data_release(in);
gpgme_data_release(out);
gpgme_release(ctx);
+}
+
+
+void
+printpass(char *item)
+{
+ char buf[512];
+ const char* home;
+ int fin;
+
+ if (!(home = getenv("HOME")))
+ fatalx("$HOME not set, cannot determine password-store location");
+ snprintf(file, sizeof(file), "%s/.password-store/%s.gpg", home, item);
+
+ /* Check if file exists */
+ fin = open(file, O_RDONLY);
+ if (fin == -1) {
+ fatal("%s is not in password store.", file);
+ }
+
+ decrypt(buf);
+ printf("%s\n", buf);
close(fin);
}
void
-getuserid(char *u)
+getuserid(char *u, int usize)
{
char file[PATH_MAX];
const char* home;
@@ -139,7 +147,7 @@ getuserid(char *u)
fp = fopen(file, "r");
if (!fp)
fatal("fopen: %s", file);
- while ((i = fgetc(fp)) != EOF)
+ while ((i = fgetc(fp)) != EOF && usize--)
*u++ = i;
*u = '\0';
}
@@ -167,7 +175,7 @@ mkdirp(const char *tp)
}
void
-insert(char *item)
+encrypt()
{
gpgme_data_t in, out;
gpgme_error_t gpgerr;
@@ -175,14 +183,74 @@ insert(char *item)
gpgme_key_t key;
gpgme_key_t keys[2];
gpgme_protocol_t proto;
+ char uid[128], t[PATH_MAX];
+ FILE *fin, *fout;
+
+ proto = GPGME_PROTOCOL_OpenPGP;
+ key = NULL;
+ initgpgme();
+ getuserid(uid, 128);
+
+ gpgerr = gpgme_new(&ctx);
+ if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
+ fatalgpg(gpgerr, "gpme_new");
+
+ gpgerr = gpgme_set_protocol(ctx, proto);
+ if (gpgme_err_code(gpgerr) == GPG_ERR_INV_VALUE)
+ fatalgpg(gpgerr, "gpgme_set_protocol");
+
+ gpgme_set_armor(ctx, 1);
+
+ if (gpgme_op_keylist_start(ctx, uid, 0) != GPG_ERR_INV_VALUE)
+ while (!(gpgerr = gpgme_op_keylist_next(ctx, &key))) {
+ if (key->can_encrypt) {
+ break;
+ }
+ }
+ if (gpgme_err_code(gpgerr) == GPG_ERR_EOF)
+ fatalgpg(gpgerr, "can not find key");
+
+ keys[0] = key;
+ keys[1] = NULL;
+
+ fin = fopen(file, "r");
+ memcpy(t, file, strlen(file) + 1);
+ snprintf(file, sizeof(file), "%s.gpg", t);
+ fout = fopen(file, "w");
+
+ gpgerr = gpgme_data_new_from_stream(&in, fin);
+ if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
+ fatalgpg(gpgerr, "gpgme_data_new_from_stream");
+
+ gpgerr = gpgme_data_new_from_stream(&out, fout);
+ gpgme_data_set_encoding(out, GPGME_DATA_ENCODING_ARMOR);
+ if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
+ fatalgpg(gpgerr, "gpgme_data_new_from_stream");
+
+ gpgerr = gpgme_op_encrypt(ctx, keys,
+ GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
+ if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
+ fatalgpg(gpgerr, "gpgme_op_encrypt");
+ if (remove(t))
+ fprintf(stderr, "remove failed\n");
+
+ gpgme_key_release(key);
+ gpgme_data_release(in);
+ gpgme_data_release(out);
+ gpgme_release(ctx);
+}
+
+
+void
+insert(char *item)
+{
char *filename, t[PATH_MAX];
const char *home;
- FILE *fp, *fin, *fout;
- char uid[128], pass[128];
+ FILE *fp;
+ char pass[128];
int c, fd;
- key = NULL;
c = 'y';
fd = fileno(stdin);
@@ -214,7 +282,6 @@ insert(char *item)
if (!(fp = fopen(file, "w+b")))
fatal("fopen: %s", file);
if (isatty(fd)) {
- printf("2\n");
readpassphrase("Enter password: ", pass, 128, RPP_ECHO_OFF);
memcpy(t, pass, strlen(pass) + 1);
readpassphrase("Retype password: ", pass, 128, RPP_ECHO_OFF);
@@ -235,60 +302,8 @@ insert(char *item)
fputc(c, fp);
}
fclose(fp);
- getuserid(uid);
-
- proto = GPGME_PROTOCOL_OpenPGP;
- initgpgme();
- gpgerr = gpgme_new(&ctx);
- if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
- fatalgpg(gpgerr, "gpme_new");
-
- gpgerr = gpgme_set_protocol(ctx, proto);
- if (gpgme_err_code(gpgerr) == GPG_ERR_INV_VALUE)
- fatalgpg(gpgerr, "gpgme_set_protocol");
- gpgme_set_armor(ctx, 1);
-
- if (gpgme_op_keylist_start(ctx, uid, 0) != GPG_ERR_INV_VALUE)
- while (!(gpgerr = gpgme_op_keylist_next(ctx, &key))) {
- if (key->can_encrypt) {
- break;
- }
- }
- if (gpgme_err_code(gpgerr) == GPG_ERR_EOF)
- fatalgpg(gpgerr, "can not find key");
-
- keys[0] = key;
- keys[1] = NULL;
-
- fin = fopen(file, "r");
- memcpy(t, file, strlen(file) + 1);
- snprintf(file, sizeof(file), "%s.gpg", t);
- fout = fopen(file, "w");
-
- gpgerr = gpgme_data_new_from_stream(&in, fin);
- if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
- fatalgpg(gpgerr, "gpgme_data_new_from_stream");
-
- gpgerr = gpgme_data_new_from_stream(&out, fout);
- gpgme_data_set_encoding(out, GPGME_DATA_ENCODING_ARMOR);
- if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
- fatalgpg(gpgerr, "gpgme_data_new_from_stream");
-
- gpgerr = gpgme_op_encrypt(ctx, keys,
- GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
- if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
- fatalgpg(gpgerr, "gpgme_op_encrypt");
-
- gpgme_key_release(key);
- gpgme_data_release(in);
- gpgme_data_release(out);
- gpgme_release(ctx);
-
- fclose(fin);
- fclose(fout);
- if (remove(t))
- fprintf(stderr, "remove failed\n");
+ encrypt();
}