commit db4303793040120df2b53f6906ffb28f399f31c2
parent 41a16892758dc58d8410773bd0eedad6bf9ebe72
Author: Naveen Narayanan zerous <zerous@nocebo.space>
Date: Fri, 29 Dec 2017 14:01:48 +0300
style fix
Diffstat:
pass.c | | | 75 | ++++++++++++++------------------------------------------------------------- |
1 file changed, 14 insertions(+), 61 deletions(-)
diff --git a/pass.c b/pass.c
@@ -43,10 +43,9 @@ delete(char *item)
exit(0);
else if (r == 'y' && !remove(file)) {
printf("removed '%s'\n", file);
-
if ((l = strrchr(file, '/')))
*l = '\0';
- rmdir(file); /* remove folder if empty */
+ rmdir(file);
}
}
@@ -75,34 +74,25 @@ decrypt(char *buf)
proto = GPGME_PROTOCOL_OpenPGP;
gpgerr = gpgme_new(&ctx);
- if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR) {
+ if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
fatalgpg(gpgerr, "Error: gpgme_new: %s");
- }
-
gpgerr = gpgme_set_protocol(ctx, proto);
if (gpgme_err_code(gpgerr) == GPG_ERR_INV_VALUE)
fatalgpg(gpgerr, "Error: gpgme_set_protocol");
-
gpgerr = gpgme_data_new_from_file(&in, file, 1);
if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
fatalgpg(gpgerr, "Error: gpgme_data_new_from_file");
-
gpgerr = gpgme_data_new(&out);
if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
fatalgpg(gpgerr, "Error: gpgme_data_new");
-
- /* decrypt */
gpgerr = gpgme_op_decrypt(ctx, in, out);
if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
fatalgpg(gpgerr, "Error: gpgme_op_decrypt");
-
ret = gpgme_data_seek(out, 0, SEEK_SET);
if (ret)
fatalx("gpgme_data_seek");
- if ((a = gpgme_data_read(out, buf, 100)) > 0) {
+ if ((a = gpgme_data_read(out, buf, 100)) > 0)
buf[a] = '\0';
- }
-
gpgme_data_release(in);
gpgme_data_release(out);
gpgme_release(ctx);
@@ -119,16 +109,12 @@ printpass(char *item)
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);
- }
-
+ if (fin == -1)
+ fatal("%s is not in password store.", file);
decrypt(buf);
printf("%s\n", buf);
-
close(fin);
}
@@ -143,7 +129,6 @@ getuserid(char *u, int usize)
if (!(home = getenv("HOME")))
fatalx("$HOME not set, cannot determine password-store location");
snprintf(file, sizeof(file), "%s/.password-store/.gpg-id", home);
-
fp = fopen(file, "r");
if (!fp)
fatal("fopen: %s", file);
@@ -188,60 +173,47 @@ encrypt()
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) {
+ 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);
+ 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)
{
@@ -257,7 +229,6 @@ insert(char *item)
if (!(home = getenv("HOME")))
fatalx("$HOME not set, cannot determine password-store location");
snprintf(file, sizeof(file), "%s/.password-store", home);
-
filename = basename(item);
mkdirp(item);
memcpy(t, file, strlen(file) + 1);
@@ -271,12 +242,10 @@ insert(char *item)
logdbgx("Don't overwrite");
exit(1);
}
- } else {
+ } else
/* Assuming user knows what he/she is doing */
printf("Overwriting %s\n", filename);
- }
}
-
if (c != 'Y' && c != 'y')
exit(1);
if (!(fp = fopen(file, "w+b")))
@@ -293,20 +262,18 @@ insert(char *item)
fatal("fputc: %s", file);
i++;
}
- } else {
- fatalx("Passwords don't match.");
- }
+ } else
+ fatalx("Passwords don't match.");
+
} else {
int c;
while ((c = getchar()) != EOF && c != '\n')
fputc(c, fp);
}
fclose(fp);
-
encrypt();
}
-
int
isinit(void) /* check if .password-store exists */
{
@@ -317,9 +284,7 @@ isinit(void) /* check if .password-store exists */
if (!(home = getenv("HOME")))
fatalx("$HOME not set, cannot determine password-store location");
-
snprintf(file, sizeof(file), "%s/.password-store", home);
-
if ((fp = open(file, O_RDONLY)) != -1 &&
(!fstat(fp, &sb)) &&
(S_ISDIR(sb.st_mode))) {
@@ -340,28 +305,20 @@ initpass(char *pgpid)
if (!pgpid) {
usage();
}
-
-
if (!isinit())
{
if (!(home = getenv("HOME")))
fatalx("$HOME not set, cannot determine password-store location");
-
snprintf(file, sizeof(file), "%s/.password-store", home);
-
if (mkdir(file, mode))
fatal("mkdir");
-
snprintf(file, sizeof(file), "%s/.password-store/.gpg-id", home);
if (!(gpg = fopen(file, "a")))
fatal("fopen");
-
if (fputs(pgpid, gpg) == EOF)
fatal("fputs");
-
printf("Password store initialized for %s\n", pgpid);
fclose(gpg);
-
} else {
printf("Password store initialized for %s\n", pgpid);
}
@@ -370,18 +327,16 @@ initpass(char *pgpid)
int
main(int argc, char** argv)
{
- debug = 0;
-
char **s;
int i;
+ debug = 0;
+
for (s = argv, i = 0; i < argc; i++) {
if (!strcmp("-d", *s++))
debug = 1;
}
-
loginit("pass");
-
if (argc > 1)
{
argv++;
@@ -394,9 +349,7 @@ main(int argc, char** argv)
delete(*(argv + 1));
else
printpass(*argv);
-
} else
usage();
-
return 0;
}