aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libnpass/libnpass.c24
-rw-r--r--src/npass/npass.c139
2 files changed, 92 insertions, 71 deletions
diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c
index f62f098..779a42a 100644
--- a/src/libnpass/libnpass.c
+++ b/src/libnpass/libnpass.c
@@ -250,6 +250,8 @@ int readstore_all(const char *path, struct store **stor)
err_ret(-errno, "%s", strerror(errno));
}
}
+ if (len == 0)
+ free(*stor);
return (ret < 0) ? ret : len;
}
@@ -309,10 +311,11 @@ int pass_cat(FILE *out, const char *path)
return gpg_decrypt(out, pass_path);
}
-ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream)
+ssize_t pass_getpass(char **lineptr, FILE *stream)
{
struct termios new, old;
- int ret;
+ size_t len;
+ ssize_t ret;
ret = tcgetattr(fileno(stream), &old);
if (ret < 0)
@@ -324,17 +327,22 @@ ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream)
if (ret < 0)
err_ret(-errno, "%s", strerror(errno));
- ret = getline(lineptr, n, stream);
- if (ret > 0 && (*lineptr)[ret - 1] == '\n')
- (*lineptr)[ret - 1] = '\0';
- else if (ret < 0)
+ len = 0;
+ *lineptr = NULL;
+ ret = getline(lineptr, &len, stream);
+ if (ret < 0)
err_ret(-errno, "%s", strerror(errno));
+ else if (ret > 0 && (*lineptr)[ret - 1] == '\n')
+ (*lineptr)[--ret] = '\0';
+ len = ret;
ret = tcsetattr(fileno(stream), TCSAFLUSH, &old);
- if (ret < 0)
+ if (ret < 0) {
+ free(*lineptr);
err_ret(-errno, "%s", strerror(errno));
+ }
- return 0;
+ return len;
}
int pass_gen(pass_gen_t gen, char *pass, int len)
diff --git a/src/npass/npass.c b/src/npass/npass.c
index f02c4be..cf6338f 100644
--- a/src/npass/npass.c
+++ b/src/npass/npass.c
@@ -7,7 +7,8 @@
#include "libnpass/libnpass.h"
#include "util.h"
-#define invalid_usage_err_ret() err_ret(1, "invalid usage, try pass help")
+#define invalid_usage_err_ret(err) \
+ err_ret(err, "%s", "Invalid usage, try $pass help")
#define DEF_DEPTTH 16;
#define BLUE "\e[0;34m"
@@ -47,43 +48,56 @@ static void print_usage(void)
static int cat(const char *path)
{
- int r;
+ int ret;
- r = pass_cat(stdout, path);
- if (!r && isatty(STDOUT_FILENO))
+ ret = pass_cat(stdout, path);
+ if (ret >= 0 && isatty(STDOUT_FILENO))
putchar('\n');
- return r;
+ return ret;
}
static int ls(const char *path, size_t depth)
{
- void *p;
- char *prefix;
- int i, j, len, r;
- struct store *stor;
- char new_path[PATH_MAX];
static depth_state_t *depth_state;
- static size_t depth_state_len = DEF_DEPTTH;
+ static size_t depth_state_len;
- len = readstore_all(path, &stor);
- if (len <= 0)
- return 1;
+ char new_path[PATH_MAX];
+ struct store *stor;
+ int slen, ret;
+ char *prefix;
+ void *p;
+
+ slen = readstore_all(path, &stor);
+ if (slen <= 0)
+ return -EPERM;
+ depth_state_len = DEF_DEPTTH;
if (!depth) {
depth_state = malloc(sizeof(depth_state_t) * depth_state_len);
+ if (!depth_state) {
+ free(stor);
+ err_ret(-errno, "%s", strerror(errno));
+ }
+
puts((path) ? path : "Password Store");
} else if (depth == depth_state_len - 1) {
depth_state_len *= 2;
- p = realloc(stor, sizeof(depth_state[0]) * len);
- if (!p)
- err_ret(1, "%s", strerror(errno));
+ p = realloc(stor, sizeof(*depth_state) * depth_state_len);
+ if (!p) {
+ free(stor);
+ err_ret(-errno, "%s", strerror(errno));
+ }
depth_state = p;
}
- qsort(stor, len, sizeof(stor[0]), pass_store_cmp);
- for (i = 0; i < len; i++) {
- if (i == len - 1) {
+ qsort(stor, slen, sizeof(*stor), pass_store_cmp);
+ for (int i = 0; i < slen; i++) {
+ for (int j = 0; (size_t)j < depth; j++)
+ printf("%s ",
+ (depth_state[j]) == DEPTH_ITS_OVER ? " " : "│");
+
+ if (i == slen - 1) {
prefix = "└──";
depth_state[depth] = DEPTH_ITS_OVER;
} else {
@@ -91,74 +105,73 @@ static int ls(const char *path, size_t depth)
depth_state[depth] = DEPTH_NOT_OVER;
}
- for (j = 0; (size_t)j < depth; j++)
- printf("%s ",
- (depth_state[j]) == DEPTH_ITS_OVER ? " " : "│");
-
if (stor[i].type == PASS_STORE_DIR) {
printf("%s %s%s%s\n", prefix, BLUE, stor[i].name, NCOL);
- r = snprintf(new_path, sizeof(new_path), "%s/%s",
- (path) ? path : "", stor[i].name);
- if (r < 0 || (size_t)r >= sizeof(new_path))
- err_ret(1, "%s", "failed to build path");
+ ret = snprintf(new_path, sizeof(new_path), "%s/%s",
+ (path) ? path : "", stor[i].name);
+ if (ret < 0 || (size_t)ret >= sizeof(new_path)) {
+ ret = -ENAMETOOLONG;
+ err_print("%s", "Path exceeded PATH_MAX");
+ goto out_free_depth_state;
+ }
+
ls(new_path, depth + 1);
} else {
printf("%s %s\n", prefix, stor[i].name);
}
}
+out_free_depth_state:
if (!depth)
free(depth_state);
free(stor);
- return 0;
+ return (ret < 0) ? ret : 0;
}
static int add(const char *path)
{
char *p1 = NULL, *p2 = NULL;
FILE *in;
- size_t n;
- int r;
+ ssize_t ret;
in = fopen("/dev/tty", "r");
if (!in)
in = stdin;
fputs("Password: ", stdout);
- r = pass_getpass(&p1, &n, in);
- if (r < 0) {
- if (in != stdin)
- fclose(in);
- err_ret(1, "%d:%s:", errno, strerror(errno));
+ ret = pass_getpass(&p1, in);
+ if (ret < 0) {
+ err_print("%s", strerror(errno));
+ goto out_close_in;
}
fputs("\nRetype password: ", stdout);
- r = pass_getpass(&p2, &n, in);
- putc('\n', stdout);
- if (r < 0) {
- if (in != stdin)
- fclose(in);
- if (p1)
- free(p1);
- err_ret(1, "%d:%s:", errno, strerror(errno));
+ ret = pass_getpass(&p2, in);
+ putchar('\n');
+ if (ret < 0) {
+ err_print("%s", strerror(errno));
+ goto out_free_p1;
}
- if (in != stdin)
- fclose(in);
-
if (strcmp(p1, p2)) {
- free(p1);
- free(p2);
- err_ret(1, "Sorry, passwords do not match");
+ err_print("%s", "Passwords do not match");
+ ret = -EINVAL;
+ goto out_free_p2;
}
- free(p1);
- r = pass_add(path, p2, n);
+ ret = pass_add(path, p2, ret);
+out_free_p2:
free(p2);
- return r;
+out_free_p1:
+ free(p1);
+out_close_in:
+ if (in != stdin)
+ fclose(in);
+
+ return ret;
}
static int gen(int argc, char *argv[])
@@ -186,11 +199,11 @@ static int gen(int argc, char *argv[])
}
if (optind != argc - 1)
- invalid_usage_err_ret();
+ invalid_usage_err_ret(-EINVAL);
pass = malloc(sizeof(char) * len);
if (!pass)
- err_ret(1, "%s", strerror(errno));
+ err_ret(-errno, "%s", strerror(errno));
r = pass_gen(gen, pass, len);
if (r)
@@ -217,32 +230,32 @@ int main(int argc, char *argv[])
print_usage();
} else if (!strcmp("init", *argv)) {
if (argc != 2)
- invalid_usage_err_ret();
+ invalid_usage_err_ret(EXIT_FAILURE);
r = pass_init(argv[1]);
} else if (!strcmp("ls", *argv)) {
if (argc > 2)
- invalid_usage_err_ret();
+ invalid_usage_err_ret(EXIT_FAILURE);
r = ls(argv[1], 0);
} else if (!strcmp("cat", *argv)) {
if (argc != 2)
- invalid_usage_err_ret();
+ invalid_usage_err_ret(EXIT_FAILURE);
r = cat(argv[1]);
} else if (!strcmp("add", *argv)) {
if (argc != 2)
- invalid_usage_err_ret();
+ invalid_usage_err_ret(EXIT_FAILURE);
r = add(argv[1]);
} else if (!strcmp("rm", *argv)) {
if (argc != 2)
- invalid_usage_err_ret();
+ invalid_usage_err_ret(EXIT_FAILURE);
r = pass_rm(argv[1]);
} else if (!strcmp("gen", *argv)) {
if (argc < 2)
- invalid_usage_err_ret();
+ invalid_usage_err_ret(EXIT_FAILURE);
r = gen(argc, argv);
} else if (argc == 1) {
@@ -260,8 +273,8 @@ int main(int argc, char *argv[])
break;
}
} else {
- invalid_usage_err_ret();
+ invalid_usage_err_ret(EXIT_FAILURE);
}
- return r;
+ return (r < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
}