aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libnpass/libnpass.c36
-rw-r--r--src/npass/npass.c51
2 files changed, 86 insertions, 1 deletions
diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c
index 9158c62..25b707f 100644
--- a/src/libnpass/libnpass.c
+++ b/src/libnpass/libnpass.c
@@ -18,6 +18,17 @@
#define FPR_MAX 256
static char pass_dir[PATH_MAX] = {0};
+const char *pass_gen_set[] = {
+ "0123456789",
+
+ "0123456789"
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
+
+ "0123456789"
+ "abcdefghijklmnopqrstuvwxyz"
+ "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
+};
int set_pass_dir(void);
@@ -123,6 +134,31 @@ ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream)
return r;
}
+int pass_gen(pass_gen_t gen, char *pass, size_t n)
+{
+ int r;
+ FILE *rand;
+ size_t pass_gen_len;
+
+
+ if (n <= 0)
+ err_ret(1, "small password");
+
+ rand = fopen("/dev/urandom", "r");
+ if (!rand)
+ err_ret(1, "%s", strerror(errno));
+
+ r = fread(pass, sizeof(pass[0]), n, rand);
+ if (r != (int) n)
+ err_ret(1, "fread failed");
+
+ pass_gen_len = strlen(pass_gen_set[gen]);
+ for (size_t i = 0; i < n; i++)
+ pass[i] = pass_gen_set[gen][pass[i] % (pass_gen_len - 1)];
+
+ return 0;
+}
+
int pass_add(const char *path, const char *pass, size_t n)
{
int r;
diff --git a/src/npass/npass.c b/src/npass/npass.c
index d286d80..4eba07f 100644
--- a/src/npass/npass.c
+++ b/src/npass/npass.c
@@ -12,6 +12,7 @@
void print_usage(void);
int cat(const char *path);
int add(const char *path);
+int gen(int argc, char *argv[]);
void print_usage(void)
{
@@ -26,7 +27,7 @@ void print_usage(void)
" Remove password\n"
" add pass-name\n"
" Add new password\n"
- " gen pass-name\n"
+ " gen [-d|-a|-p|-l] pass-name\n"
" Generate new password\n"
" cat pass-name\n"
" Show encrypted password\n"
@@ -91,6 +92,49 @@ int add(const char *path)
return r;
}
+int gen(int argc, char *argv[]) {
+ char *pass;
+ int opt, r = 0;
+ size_t len = 30;
+ pass_gen_t gen = PASS_GEN_PRINT;
+
+ while ((opt = getopt(argc, argv, "dapl:")) != -1) {
+ switch (opt) {
+ case 'd':
+ gen = PASS_GEN_DIGIT;
+ break;
+ case 'a':
+ gen = PASS_GEN_ALPHA;
+ break;
+ case 'p':
+ gen = PASS_GEN_PRINT;
+ break;
+ case 'l':
+ len = atoi(optarg);
+ break;
+ }
+ }
+
+ if (optind != argc - 1)
+ invalid_usage_err_ret();
+
+ pass = malloc(sizeof(char) * len);
+ if (!pass)
+ err_ret(1, "%s", strerror(errno));
+
+ r = pass_gen(gen, pass, len);
+ if (r)
+ goto out_free_pass;
+
+ r = pass_add(argv[optind], pass, len);
+ if (!r)
+ printf("%s\n", pass);
+
+out_free_pass:
+ free(pass);
+ return r;
+}
+
int main(int argc, char *argv[])
{
int r = 0;
@@ -124,6 +168,11 @@ int main(int argc, char *argv[])
invalid_usage_err_ret();
r = pass_rm(argv[1]);
+ } else if (!strcmp("gen", *argv)) {
+ if (argc < 2)
+ invalid_usage_err_ret();
+
+ r = gen(argc, argv);
} else if (argc == 1){
r = cat(*argv);
} else {