aboutsummaryrefslogtreecommitdiff
path: root/src/libnpass/libnpass.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-04-22 17:32:22 +0530
committersinanmohd <sinan@sinanmohd.com>2024-04-22 17:32:22 +0530
commit5c1279fe17f23ec1f407809567b8a752d3be7f09 (patch)
treeae7b32b30f8296ce944a0cf7513d9b68517d77ae /src/libnpass/libnpass.c
parent9598ccb3c4984ab14e41de84243194e7484ad9f9 (diff)
libnpass: refactor set_pass_dir to pass_store_path_set
Diffstat (limited to 'src/libnpass/libnpass.c')
-rw-r--r--src/libnpass/libnpass.c78
1 files changed, 34 insertions, 44 deletions
diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c
index edeb4af..63bf96f 100644
--- a/src/libnpass/libnpass.c
+++ b/src/libnpass/libnpass.c
@@ -18,7 +18,7 @@
#define DEF_STOR_SIZE 64
#define FPR_MAX NAME_MAX
-static char pass_dir[PATH_MAX] = {0};
+static char store_path[PATH_MAX] = {0};
static const char *pass_gen_set[] = {
"0123456789",
@@ -35,17 +35,21 @@ static const char *pass_gen_set[] = {
static int openstore(const char *spath, DIR **dirp);
static int readstore(DIR *dirp, struct store *s);
static int is_storeobj(struct dirent *dir);
-static int set_pass_dir(void);
-static int set_pass_dir(void)
+int pass_store_path_set(char **store)
{
struct stat statbuf;
const char *env;
- int r;
+ int ret;
+
+ if (store != NULL)
+ *store = store_path;
+ if (*store_path)
+ return 0;
env = getenv("PASSWORD_STORE_DIR");
if (env) {
- strncpy(pass_dir, env, sizeof(pass_dir) - 1);
+ strncpy(store_path, env, sizeof(store_path) - 1);
return 0;
}
@@ -54,30 +58,30 @@ static int set_pass_dir(void)
*/
env = getenv("HOME");
if (env) {
- r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s", env,
- ".password-store");
- if (r < 0 || (size_t)r > sizeof(pass_dir))
+ ret = snprintf(store_path, sizeof(store_path), "%s/%s", env,
+ ".password-store");
+ if (ret < 0 || (size_t)ret > sizeof(store_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
- r = stat(pass_dir, &statbuf);
- if (!r)
+ ret = stat(store_path, &statbuf);
+ if (!ret)
return 0;
}
env = getenv("XDG_DATA_HOME");
if (env) {
- r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s", env,
- DEF_PASS_DIR);
- if (r < 0 || (size_t)r > sizeof(pass_dir))
+ ret = snprintf(store_path, sizeof(store_path), "%s/%s", env,
+ DEF_PASS_DIR);
+ if (ret < 0 || (size_t)ret > sizeof(store_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
return 0;
}
env = getenv("HOME");
if (env) {
- r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s/%s", env,
- ".local/share", DEF_PASS_DIR);
- if (r < 0 || (size_t)r > sizeof(pass_dir))
+ ret = snprintf(store_path, sizeof(store_path), "%s/%s/%s", env,
+ ".local/share", DEF_PASS_DIR);
+ if (ret < 0 || (size_t)ret > sizeof(store_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
return 0;
}
@@ -127,11 +131,7 @@ int pass_store_type(const char *spath)
struct stat sbuf;
int r;
- r = set_pass_dir();
- if (r < 0)
- return_err(-EINVAL, "PASSWORD_STORE_DIR must be set");
-
- r = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir,
+ r = snprintf(abs_path, sizeof(abs_path), "%s/%s", store_path,
(spath) ? spath : "");
if (r < 0 || (size_t)r >= sizeof(abs_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
@@ -139,7 +139,8 @@ int pass_store_type(const char *spath)
if (!r && (sbuf.st_mode & S_IFMT) == S_IFDIR)
return PASS_STORE_DIR;
- r = snprintf(abs_path, sizeof(abs_path), "%s/%s.gpg", pass_dir, spath);
+ r = snprintf(abs_path, sizeof(abs_path), "%s/%s.gpg", store_path,
+ spath);
if (r < 0 || (size_t)r >= sizeof(abs_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
r = stat(abs_path, &sbuf);
@@ -164,14 +165,14 @@ static int openstore(const char *spath, DIR **dirp)
return (ret < 0) ? ret : -EINVAL;
if (spath) {
- ret = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir,
+ ret = snprintf(abs_path, sizeof(abs_path), "%s/%s", store_path,
spath);
if (ret < 0 || (size_t)ret >= sizeof(abs_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
path = abs_path;
} else {
- path = pass_dir;
+ path = store_path;
}
*dirp = opendir(path);
@@ -260,19 +261,15 @@ int pass_init(const char *fpr)
FILE *gpg_id;
int r, fpr_len;
- r = set_pass_dir();
- if (r < 0)
- return_err(r, "PASSWORD_STORE_DIR must be set");
-
r = gpg_key_validate(fpr);
if (r < 0)
return_err(r, "Try `gpg --full-generate-key`");
- r = r_mkdir(pass_dir, S_IRWXU);
+ r = r_mkdir(store_path, S_IRWXU);
if (r < 0)
- return_err(r, "%s: %s", strerror(errno), pass_dir);
+ return_err(r, "%s: %s", strerror(errno), store_path);
- r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir,
+ r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", store_path,
".gpg-id");
if (r < 0 || (size_t)r >= sizeof(gpg_id_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
@@ -301,7 +298,7 @@ int pass_cat(FILE *out, const char *path)
if (ret != PASS_STORE_ENC)
return_err(-EINVAL, "Not a password: %s", path);
- ret = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir,
+ ret = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", store_path,
path);
if (ret < 0 || (size_t)ret >= sizeof(pass_path))
return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX");
@@ -374,11 +371,7 @@ int pass_add(const char *path, const char *pass, size_t n)
char *p;
int ret;
- ret = set_pass_dir();
- if (ret < 0)
- return_err(ret, "PASSWORD_STORE_DIR must be set");
-
- ret = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir,
+ ret = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", store_path,
".gpg-id");
if (ret < 0 || (size_t)ret >= sizeof(gpg_id_path))
return_err(-EINVAL, "Path exceeded PATH_MAX");
@@ -398,7 +391,7 @@ int pass_add(const char *path, const char *pass, size_t n)
return_err(ret, "Invalid key: Try `gpg --list-keys`");
/* TODO: guard against .*\.gpg\.gpg[/$] */
- ret = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir,
+ ret = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", store_path,
path);
if (ret < 0 || (size_t)ret >= sizeof(pass_path))
return_err(-errno, "Path exceeded PATH_MAX");
@@ -427,15 +420,12 @@ int pass_rm(const char *path)
char gpg_path[PATH_MAX], abs_path[PATH_MAX];
int ret = 0;
- ret = set_pass_dir();
- if (ret < 0)
- return_err(ret, "PASSWORD_STORE_DIR must be set");
-
ret = snprintf(gpg_path, sizeof(gpg_path), "%s.gpg", path);
if (ret < 0 || (size_t)ret >= sizeof(gpg_path))
return_err(-EINVAL, "Path exceeded PATH_MAX");
- ret = snprintf(abs_path, sizeof(gpg_path), "%s/%s", pass_dir, gpg_path);
+ ret = snprintf(abs_path, sizeof(gpg_path), "%s/%s", store_path,
+ gpg_path);
if (ret < 0 || (size_t)ret >= sizeof(abs_path))
return_err(-EINVAL, "Path exceeded PATH_MAX");
@@ -444,5 +434,5 @@ int pass_rm(const char *path)
if (ret < 0)
return_err(-errno, "%s: %s", strerror(errno), abs_path);
- return r_rmdir_empty(pass_dir, dirname(gpg_path));
+ return r_rmdir_empty(store_path, dirname(gpg_path));
}