aboutsummaryrefslogtreecommitdiff
path: root/src/libnpass
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnpass')
-rw-r--r--src/libnpass/gpg.c36
-rw-r--r--src/libnpass/libnpass.c42
-rw-r--r--src/libnpass/util.c4
3 files changed, 41 insertions, 41 deletions
diff --git a/src/libnpass/gpg.c b/src/libnpass/gpg.c
index 0b90b33..9e3bb7a 100644
--- a/src/libnpass/gpg.c
+++ b/src/libnpass/gpg.c
@@ -7,12 +7,12 @@
#include "libnpass/gpg.h"
#include "libnpass/util.h"
-#define fail_if_err(err) \
+#include "util.h"
+
+#define gpg_err_ret(err) \
if (err) { \
- fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, \
- gpgme_strsource(err), gpgme_strerror(err)); \
gpg_cleanup(); \
- return 1; \
+ err_ret(1, "%s: %s", gpgme_strsource(err), gpgme_strerror(err)); \
}
static gpgme_ctx_t ctx = NULL;
@@ -33,9 +33,9 @@ int gpg_init(void)
#endif
err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
- fail_if_err(err);
+ gpg_err_ret(err);
err = gpgme_new(&ctx);
- fail_if_err(err);
+ gpg_err_ret(err);
return 0;
}
@@ -58,7 +58,7 @@ int gpg_key_validate(const char *fpr)
return r;
err = gpgme_get_key(ctx, fpr, &key, 1);
- fail_if_err(err);
+ gpg_err_ret(err);
gpg_cleanup();
return 0;
@@ -76,20 +76,20 @@ int gpg_decrypt(FILE *pass_out, const char *pass_path)
return r;
err = gpgme_data_new_from_file(&in, pass_path, 1);
- fail_if_err(err);
+ gpg_err_ret(err);
err = gpgme_data_new(&out);
- fail_if_err(err);
+ gpg_err_ret(err);
err = gpgme_op_decrypt(ctx, in, out);
- fail_if_err(err);
+ gpg_err_ret(err);
r = gpgme_data_seek(out, 0, SEEK_SET);
if (r)
- fail_if_err (gpgme_err_code_from_errno(errno));
+ gpg_err_ret(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_err_ret(gpgme_err_code_from_errno(errno));
gpg_cleanup();
return 0;
@@ -107,24 +107,24 @@ int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n)
return r;
err = gpgme_get_key(ctx, fpr, &key, 1);
- fail_if_err(err);
+ gpg_err_ret(err);
err = gpgme_data_new_from_mem(&in, pass, n, 0);
- fail_if_err(err);
+ gpg_err_ret(err);
err = gpgme_data_new(&out);
- fail_if_err(err);
+ gpg_err_ret(err);
err = gpgme_op_encrypt(ctx, &key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
- fail_if_err(err);
+ gpg_err_ret(err);
r = gpgme_data_seek(out, 0, SEEK_SET);
if (r)
- fail_if_err (gpgme_err_code_from_errno(errno));
+ gpg_err_ret(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));
+ gpg_err_ret(gpgme_err_code_from_errno(errno));
return 0;
}
diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c
index 14f4737..9158c62 100644
--- a/src/libnpass/libnpass.c
+++ b/src/libnpass/libnpass.c
@@ -55,29 +55,29 @@ int pass_init(const char *fpr)
r = set_pass_dir();
if (r)
- err_die(1, "PASSWORD_STORE_DIR not set");
+ err_ret(1, "PASSWORD_STORE_DIR not set");
r = gpg_key_validate(fpr);
if (r)
- err_die(1, "key not usable, try gpg --full-generate-key");
+ err_ret(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));
+ err_ret(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");
+ err_ret(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));
+ err_ret(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");
+ err_ret(1, "write failed");
return 0;
}
@@ -89,11 +89,11 @@ int pass_cat(FILE *out, const char *path)
r = set_pass_dir();
if (r)
- err_die(1, "PASSWORD_STORE_DIR not set");
+ err_ret(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");
+ err_ret(1, "path exceeded PATH_MAX");
r = gpg_decrypt(out, pass_path);
return r;
@@ -132,44 +132,44 @@ int pass_add(const char *path, const char *pass, size_t n)
r = set_pass_dir();
if (r)
- err_die(1, "PASSWORD_STORE_DIR not set");
+ err_ret(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");
+ err_ret(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));
+ err_ret(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);
+ err_ret(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");
+ err_ret(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");
+ err_ret(1, "path exceeded PATH_MAX");
rc = strdup(pass_path);
if (!rc)
- err_die(1, "%s", strerror(errno));
+ err_ret(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);
+ err_ret(1, "an entry already exists for %s", path);
out_stream = fopen(pass_path, "w");
if (!out_stream)
- err_die(1, "%s", strerror(errno));
+ err_ret(1, "%s", strerror(errno));
r = gpg_encrypt(out_stream, fpr, pass, n);
@@ -185,20 +185,20 @@ int pass_rm(const char *path)
r = set_pass_dir();
if (r)
- err_die(1, "PASSWORD_STORE_DIR not set");
+ err_ret(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");
+ err_ret(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");
+ err_ret(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));
+ err_ret(1, "%s %s", abs_path, strerror(errno));
return r_rmdir(pass_dir, dirname(gpg_path));
}
diff --git a/src/libnpass/util.c b/src/libnpass/util.c
index 9164354..11cf264 100644
--- a/src/libnpass/util.c
+++ b/src/libnpass/util.c
@@ -46,11 +46,11 @@ int r_rmdir(const char *prefix_path, char *rm_path)
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");
+ err_ret(1, "path exceeded PATH_MAX");
r = rmdir(abs_path);
if (r && errno != EEXIST && errno != ENOTEMPTY)
- err_die(1, "%s", strerror(errno));
+ err_ret(1, "%s", strerror(errno));
return r_rmdir(prefix_path, dirname(rm_path));
}