aboutsummaryrefslogtreecommitdiff
path: root/src/npass/npass.c
blob: f02c4beeb33cbd618975153a8a6f9f8f7e69e925 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "libnpass/libnpass.h"
#include "util.h"

#define invalid_usage_err_ret() err_ret(1, "invalid usage, try pass help")

#define DEF_DEPTTH 16;
#define BLUE	   "\e[0;34m"
#define NCOL	   "\e[0m"

typedef enum {
	DEPTH_ITS_OVER = 0,
	DEPTH_NOT_OVER = 1,
} depth_state_t;

static void print_usage(void);
static int cat(const char *path);
static int add(const char *path);
static int gen(int argc, char *argv[]);
static int ls(const char *path, size_t depth);

static void print_usage(void)
{
	printf("Usage: pass [COMMAND | pass_path]\n\n"

	       "Commands:\n"
	       "  help\n"
	       "	Show this help\n"
	       "  add  pass_name\n"
	       "	Add new password\n"
	       "  cat  pass_name\n"
	       "	Show encrypted password\n"
	       "  ls   [pass_path]\n"
	       "	List passwords\n"
	       "  rm   pass_name\n"
	       "	Remove password\n"
	       "  init key_id | fingerprint\n"
	       "	Initialize new password storage\n"
	       "  gen  [-a | -d | -g] [-l length] pass_name\n"
	       "	Generate new password\n");
}

static int cat(const char *path)
{
	int r;

	r = pass_cat(stdout, path);
	if (!r && isatty(STDOUT_FILENO))
		putchar('\n');

	return r;
}

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;

	len = readstore_all(path, &stor);
	if (len <= 0)
		return 1;

	if (!depth) {
		depth_state = malloc(sizeof(depth_state_t) * depth_state_len);
		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));
		depth_state = p;
	}

	qsort(stor, len, sizeof(stor[0]), pass_store_cmp);
	for (i = 0; i < len; i++) {
		if (i == len - 1) {
			prefix = "└──";
			depth_state[depth] = DEPTH_ITS_OVER;
		} else {
			prefix = "├──";
			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");
			ls(new_path, depth + 1);
		} else {
			printf("%s %s\n", prefix, stor[i].name);
		}
	}

	if (!depth)
		free(depth_state);
	free(stor);

	return 0;
}

static int add(const char *path)
{
	char *p1 = NULL, *p2 = NULL;
	FILE *in;
	size_t n;
	int r;

	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));
	}

	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));
	}

	if (in != stdin)
		fclose(in);

	if (strcmp(p1, p2)) {
		free(p1);
		free(p2);
		err_ret(1, "Sorry, passwords do not match");
	}

	free(p1);
	r = pass_add(path, p2, n);

	free(p2);
	return r;
}

static int gen(int argc, char *argv[])
{
	char *pass;
	int opt, r = 0;
	size_t len = PASS_DEF_LEN;
	pass_gen_t gen = PASS_GEN_GRAPH;

	while ((opt = getopt(argc, argv, "dagl:")) != -1) {
		switch (opt) {
		case 'd':
			gen = PASS_GEN_DIGIT;
			break;
		case 'a':
			gen = PASS_GEN_ALNUM;
			break;
		case 'g':
			gen = PASS_GEN_GRAPH;
			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%s", pass, isatty(STDOUT_FILENO) ? "\n" : "");

out_free_pass:
	free(pass);
	return r;
}

int main(int argc, char *argv[])
{
	int r = 0;
	pass_store_t store_type;

	++argv;
	if (!--argc) {
		r = ls(NULL, 0);
	} else if (!strcmp("help", *argv)) {
		print_usage();
	} else if (!strcmp("init", *argv)) {
		if (argc != 2)
			invalid_usage_err_ret();

		r = pass_init(argv[1]);
	} else if (!strcmp("ls", *argv)) {
		if (argc > 2)
			invalid_usage_err_ret();

		r = ls(argv[1], 0);
	} else if (!strcmp("cat", *argv)) {
		if (argc != 2)
			invalid_usage_err_ret();

		r = cat(argv[1]);
	} else if (!strcmp("add", *argv)) {
		if (argc != 2)
			invalid_usage_err_ret();

		r = add(argv[1]);
	} else if (!strcmp("rm", *argv)) {
		if (argc != 2)
			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) {
		store_type = pass_store_type(*argv);

		switch (store_type) {
		case PASS_STORE_DIR:
			r = ls(*argv, 0);
			break;
		case PASS_STORE_ENC:
			r = cat(*argv);
			break;
		default:
			return 1;
			break;
		}
	} else {
		invalid_usage_err_ret();
	}

	return r;
}