util  Check-in [a3ccd193d5]

Overview
Comment:add kpw source
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a3ccd193d5e01d37bba34a8e88b4a0d3975665a214ddce0a3e67ec9e63b90189
User & Date: lexi on 2019-08-15 05:45:12
Other Links: manifest | tags
Context
2019-08-15
08:34
updates check-in: c2ffe127f3 user: lexi tags: trunk
05:45
add kpw source check-in: a3ccd193d5 user: lexi tags: trunk
2019-08-13
02:43
fixes for script compat check-in: 2592d3e6e0 user: lexi tags: trunk
Changes

Modified clib/compose.c from [d981619adf] to [5d94fe752b].

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
..
55
56
57
58
59
60
61

62
63
64
65
66
67
68

#include <string.h>

typedef struct pstr { size_t len; union {
	const char* ptr;
	char* mutptr;
}; } pstr;
#define _p(str) { sizeof str - 1, str }

typedef struct safestr {
	union {
		const char* ptr;
		char* mutptr;
	}; // im sorry okay
#	ifndef k_static
................................................................................
	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) {
			if (lst[i].ptr == NULL) continue;
			lst[i].len = strlen(lst[i].ptr);
		}
		len += lst[i].len;
	}

})

char* pstrcoll(pstr* lst, size_t ct, char* ptr) fn({
	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) continue;
		strncpy(ptr,lst[i].ptr,lst[i].len);
		ptr += lst[i].len;







|







 







>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
..
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <string.h>

typedef struct pstr { size_t len; union {
	const char* ptr;
	char* mutptr;
}; } pstr;
#define _p(str) { sizeof (str) - 1, (str) }

typedef struct safestr {
	union {
		const char* ptr;
		char* mutptr;
	}; // im sorry okay
#	ifndef k_static
................................................................................
	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) {
			if (lst[i].ptr == NULL) continue;
			lst[i].len = strlen(lst[i].ptr);
		}
		len += lst[i].len;
	}
	return len;
})

char* pstrcoll(pstr* lst, size_t ct, char* ptr) fn({
	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) continue;
		strncpy(ptr,lst[i].ptr,lst[i].len);
		ptr += lst[i].len;

Added kpw.d/db.md version [f52a921e08].





































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# kpw db format

kpw uses a simple binary database format. it consists of a number of values of constant size, followed by a series of encrypted records.

 1. public key
 2. password salt
 3. encrypt(password, private key)
 4. encrypt(private key, password salt) [for pw verification]
 5. record *

each record takes the form of

 1. account name length (1 byte)
 2. account name
 3. password length (4 bytes)
 4. password

records are added simply by encrypting them with the public key and appending them to the end of the file. thus, adding a new password does not require the decryption password.

Added kpw.d/errtab version [203a9ab500].

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ok			completed successfully				debug	0
fail		unknown error
mem			out of memory
user		kpw started as wrong user
option		unrecognized option passed
syntax		invalid invocation syntax
entropy		could not acquire entropy
copy		could not copy password
insane		your environment is not sane
db_create	database could not be created
cancel		user canceled operation				notice
pw			invalid password
pw_match	passwords do not match
usage		usage displayed to user				debug	64
lib				unspecified library error		fatal	128
lib_sodium_init	could not initialize libsodium	fatal

Added kpw.d/errtab.awk version [ec5262a4e4].











































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
BEGIN {
	if (emit == "enum") {
		print "typedef enum bad {"
	} else if (emit == "msg") {
		print "#define kpw_emit_error_switch \\"
		code = 0
	}
}

function name(n) {
	if (NR > 2) {
		return "bad_" n
	} else {
		return n
	}
}

emit == "enum" {
	print "\t" name($1) ","
}

emit == "msg" {
	if (NF >= 4) {
		retval = $4
		code = retval + 1
	} else {
		retval = code++
	}

	if (NF >= 3) {
		level = "a_" $3
		if (level == "a_fatal") {
			level = level " | " retval
		}
	} else {
		level = "a_fatal | " retval
	}

	print "\t" \
		  "case " name($1)":" \
		  "level=" level ";" \
		  "msg=\""$2"\";" \
		  "rv=" retval";" \
		  "break; \\"
}

END {
	if (emit == "enum") {
		print "} bad;"
	} else if (emit == "msg") {
		print ""
	}
}

Added kpw.d/kpw.c version [f865f78c63].































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* [ʞ] kpw.c - password manager
 *  ↳ derivative of mkpw.c
 *  ~ lexi hale <lexi@hale.su>
 *  © AGPLv3
 *  $ cc -O4 kpw.c -okpw [-D_CLIPBOARD]
 *    - D_CLIPBOARD enables kpw to automatically
 *      copy passwords to the clipboard. it does
 *      this by attempting to execute a sequence
 *      of binaries, and then writing the password
 *      to STDIN of the binary that succeeds.
 *  ? generates passwords
 *  → kpw is unlikely to be portable to non-POSIX
 *    systems, but should run fine on Linux as well
 *    as BSDs with getrandom() support.
 *  ! for getrandom() to work with the version of
 *    libc on my android phone, the getrandom() call
 *    had to be converted to use the syscall()
 *    interface. this is unlikely to cause problems,
 *    but should be kept in mind.
 */

#include <unistd.h>
#include <sys/random.h>
#include <sys/syscall.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sodium.h>
#include <termios.h>


#define sz(a) ( sizeof (a) / sizeof (a) [0] )
#define say(x) (write(2, (x), sizeof (x)))

#ifdef _CLIPBOARD
#	include <sys/types.h>
#	include <pwd.h>
#	include <stdlib.h>
#else
#   define copy(str,len)
#endif
 
enum /* constants */ {
	null = 0, true = 1, false = 0
};

#include "err.inc"

enum /* db format constants */ {
	db_pubkey_len = crypto_box_PUBLICKEYBYTES,
	db_privkey_len = crypto_box_SECRETKEYBYTES,
	kpw_db_pw_max = 64,
};

typedef _Bool bool;
typedef unsigned long long iaia_word_type;
typedef bad iaia_error_type;
enum /* iaia errors */ {
	iaia_e_ok = ok,
	iaia_e_base = fail,
	iaia_e_domain = fail,
	iaia_e_overflow = fail,
};
#define _IAIA_FN_ATOI katoi
#define _IAIA_FN_ITOA kitoa
#define _IAIA_EXTERNAL_TYPES
#include "clib/iaia.c"

#define k_static
#include "clib/compose.c"
#undef k_static

typedef enum {
	tbl_ok = ok, tbl_error = fail
} tbl_error_type;
typedef unsigned char tbl_word_type;
#include "clib/tbl.c"

#include "opt.inc"

#define str_ucase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define str_lcase "abcdefghijklmnopqrstuvwxyz"
#define str_num "0123456789" 
const char* reftbl = str_num str_ucase str_lcase;
const char* reftbl_lcase = str_num str_lcase;

#ifdef _CLIPBOARD
char* const*
cbd_cmds[] = {
	/* NOTE: these commands must be specified in order of
	 * most- to least-specific. more than one utility may
	 * be present on a given system, so we need to make sure
	 * the right one is called. */
	(char* const[]){"termux-clipboard-set", null},
	(char* const[]){"xsel", "-bi", null},
	/* TODO: allow command to be specified by env var */
	null
};

bad
copy(const char* str, size_t len) {
	char* const clipboard_env = getenv("mkpw_clipboard_setter");
	char* const clipboard_env_arg = getenv("mkpw_clipboard_setter_arg");
	// FIXME: allow multiple args
	int fds[2];
	if (pipe(fds) != 0) return 63;
	if (!fork()) {
		close(fds[1]);
		dup2(fds[0], 0);
		if (clipboard_env != null) {
			execvp(clipboard_env, (char* const[]){
					clipboard_env, clipboard_env_arg, null});
			return bad_copy;
		} else for(char* const** cmd = cbd_cmds; *cmd != null; ++cmd) {
			execvp((*cmd)[0], *cmd);
		}
		return bad_copy;
	} else {
		close(fds[0]);
		write(fds[1], str, len);
		write(fds[1], "\n", 1);
		close(fds[1]);
		return ok;
	}
}
#endif

enum genmode { upper, mix, lower, stupid };

bad
mkpw(enum genmode mode, char* buf, size_t const len) {
	const unsigned char chars = (sizeof str_num - 1) +
		((mode == upper) ? (sizeof str_ucase - 1) :
		((mode == lower) ? (sizeof str_lcase - 1) :
			((sizeof str_ucase - 1) + (sizeof str_lcase - 1))));
	const char* tbl = (mode == upper) ? reftbl :
			((mode == lower) ? reftbl_lcase : reftbl);

	unsigned char noise[len];
	unsigned char* cur = noise;
	/* getrandom(noise, len, 0); // android doesnt like it */
	if(syscall(SYS_getrandom, noise, len, 0) == -1) 
		return bad_entropy;

	for(char* ptr = buf; ptr < buf + len; ++ptr, ++cur) {
		*ptr = tbl[*cur % chars]; /* such a waste of entropy… :( */
	}

	if(mode == stupid) {
		/* appease systems with stupid password reqs */
		buf[0] = '!';
		buf[1] = 'a' + (noise[1] % 26);
		buf[2] = 'A' + (noise[1] % 26);
	}

	buf[len] = '\n';

	return ok;
}

struct entry {
	pstr account;
	pstr pw;
};

enum term_clear {term_clear_line,
                 term_clear_screen};

enum alert {a_notice, a_warn, a_debug, a_fatal = 1 << 8};
pstr alert_msg[] = {
	_p("(notice)"), _p("(warn)"),
	_p("(debug)"),  _p("(fatal)")
};

bool _g_alert_quiet = false,
     _g_debug_msgs  = false;
void alert(uint16_t kind, const char* msg) {
	if (!((kind >= a_fatal) ||
	      (_g_debug_msgs && kind == a_debug) ||
	      (!_g_alert_quiet && kind != a_debug))) return;

	uint8_t idx;
	if (kind & a_fatal) idx = a_debug + 1;
		else idx = kind;
	

	if (isatty(2)) {
		char msgcode[] = "\x1b[1;30m";
		char* color = msgcode+5;
		*color = '0' + (4 - idx);
		write(2,msgcode, sz(msgcode));
	}

	write(2,alert_msg[idx].ptr,alert_msg[idx].len);
	if (isatty(2)) write(2,"\x1b[m ",4);
		else write(2," ",1);
	write(2,msg,strlen(msg));
	write(2,"\n",1);

	if (kind & a_fatal) exit(kind & (~a_fatal));
}

void term_clear(enum term_clear behavior) {
	switch(behavior) {
		case term_clear_line:   write(1,"\r\x1b[2K",5); break;
		case term_clear_screen: write(1,"\r\x1b[3J",5); break;
	}
}
void term_bell() {
	write(1,"\a",1);
}

typedef char password[kpw_db_pw_max + 1];
bad pwread(char* dest, size_t* out_len, const char* prompt, const size_t plen) {
	if (isatty(0)) {
		struct termios initial; {
			/* in order to take PW input, we need to shut
			 * off echo and canonical mode. now we're in
			 * charge of reading each keypress. */
			tcgetattr(1, &initial);
			struct termios nt = initial;
			nt.c_lflag &= (~ECHO & ~ICANON);
			tcsetattr(1, TCSANOW, &nt);
		}

		*dest = 0;
		char* p = dest;

		do {
			term_clear(term_clear_line);
			write(1, "\x1b[1m", 4);
			write(1, prompt, plen);
			write(1, "\x1b[21m", 5);

			for(size_t i = 0; i < p - dest; ++i)
				write(1, "*", 1);

			char c;
			if (read(0, &c, 1) == 1) {
				switch (c) {
					case '\n': case '\r':
						/* accept pw */
						if (p > dest) goto end_read_loop;
							else break;
					case '\x1b': /* escape */
						term_clear(term_clear_line);
						return bad_cancel;
					case '\b': case '\x7f':
						if (p > dest) *p--=0;
							else term_bell();
						break;
					default:
						if (p - dest != 64) *p++=c;
							else term_bell();
				}
			} else {
				/* either EOF or an error - either way,
				 * we're finished here */
				break;
			}
		} while(1);
		end_read_loop: term_clear(term_clear_line);
		*p = 0;
		if (out_len!=NULL) *out_len = p - dest;

		/* return the terminal to normal */
		tcsetattr(1, TCSANOW, &initial);
	} else {
		alert(a_warn, "reading pw from standard input");
		ssize_t ct = read(0, dest, kpw_db_pw_max);
		dest[ct] = 0;
	}
}

#include <stdio.h>
int
dbopen(int flags) {
	const char* dbpath = getenv("kpw_db");
	int db;
	if (dbpath == NULL) {
		const char* cfg = getenv("XDG_CONFIG_HOME");
		if (cfg == NULL) {
			const char* home = getenv("HOME");
			if (home == NULL) exit(bad_insane);

			size_t homelen = strlen(home);
			pstr path[] = { {homelen, home}, _p("/.config/kpw.db") };
			char buf[homelen + path[1].len + 1];
			bzero(buf, sz(buf));
			impose(path, sz(path), NULL, buf);

			db = open(buf, flags, 0600);
		} else {
			size_t cfglen = strlen(cfg);
			pstr path[] = { {cfglen, cfg}, _p("/kpw.db") };
			char buf[cfglen + path[1].len + 1];
			bzero(buf, sz(buf));
			impose(path, sz(path), NULL, buf);

			db = open(buf, flags, 0600);
		}
	} else {
		printf("path is %s", dbpath);
		db = open(dbpath, flags, 0600);
	}

	return db;
}

int
kpw(int argc, const char** argv) {
	if (argc == 0) return -1;
	if (argc == 1) { 
		size_t namelen = strlen(argv[0]);
		say("\x1b[1musage:\x1b[m ");
		write(2, argv[0], namelen);
		write(2, kpw_optstr, sz(kpw_optstr));
		write(2, kpw_usage,  sz(kpw_usage));
		return bad_usage;
	}

	enum genmode
		mode = lower;
	enum {usage,getpw,addpw,delpw,genpw,
	      chpw,keyin,logout,createdb,rekeydb}
		op = getpw;

	const char* params[3];
	uint8_t param = 0;

	bool print = false,
		 clobber = false;
#	ifdef _CLIPBOARD
		bool copy_pw = true;
#	endif
	for (const char** arg = argv + 1; *arg != null; ++arg) {
		if ((*arg)[0] == '-') {
			if ((*arg)[1] == '-') { /* long opt */
				unsigned char a;
				if (tblget(sz(argtbl), argtbl, *arg + 2, &a) == ok) switch (a) {
					kpw_emit_long_option_switch
				} else {
					return bad_option;
				}
			} else { /* short opt */
				for(const char* ptr = (*arg) + 1; *ptr != 0; ++ptr) {
					switch(*ptr) {
						kpw_emit_short_option_switch
						default: return bad_option;
					}
				}
			}
		} else {
			if (param > sz(params)) return bad_syntax;
			params[param++] = *arg;
		}
	}

	if (sodium_init() < 0) 
		return bad_lib_sodium_init;

	switch(op) {
		case getpw:{ /* kpw <acct> */
			break;
		}

		case addpw:{ /* kpw -a <acct> [<pw>] */
			break;
		}

		case delpw:{ /* kpw -d <acct> */
			break;
		}

		case genpw:{ /* kpw -g[lmu] <acct> [<len>] */
			break;
		}

		case createdb: { /* kpw -C [<db>] */
			alert(a_debug, "creating new database");
			if (clobber)
				alert(a_warn, "will clobber any existing database");
			/* before we open our new file, we need to generate
			 * our keypairs.  the private key will be encrypted
			 * with a blake2 hash of a user-supplied passphrase 
			 * and stored in the database after the unencrypted
			 * public key.
			 */
			uint8_t pub [crypto_box_PUBLICKEYBYTES],
			        priv[crypto_box_SECRETKEYBYTES];
			uint8_t priv_enc[sz(priv)];

			alert(a_debug, "generating keypair");
			crypto_box_keypair(pub, priv);

			/* kpw   works  very   differently  compared   to
			 * most  password  managers. it  uses  public-key
			 * encryption so that new  passwords can be saved
			 * to the  db without bothering the  user for the
			 * password.  now we  have  our  keypair, we  can
			 * prompt  the user for a secret  passphrase with
			 * which to encrypt the private key with.
			 */


			alert(a_notice, "database keypair generated, encrypting");
			password dbpw;
			size_t pwlen;
#			define _str(s) (s),sizeof(s)
			bad e = pwread(dbpw, &pwlen, _str("database passphrase: "));
			if (e != ok) return e;
			if (isatty(0)) {
				password dbpw_conf;
				e = pwread(dbpw_conf, NULL, _str("confirm: "));
#				undef _str
				if (e != ok) return e;

				if(strcmp(dbpw,dbpw_conf) != 0)
					return bad_pw_match;
			}

			uint8_t salt[crypto_pwhash_SALTBYTES],
			         key[db_privkey_len];
			uint8_t salt_enc[crypto_box_SEALBYTES + sz(salt)];

			if (syscall(SYS_getrandom, salt, sz(salt), 0) == -1)
				return bad_entropy;

			if(crypto_pwhash(key, sz(key), dbpw, pwlen, salt,
					crypto_pwhash_OPSLIMIT_INTERACTIVE,
					crypto_pwhash_MEMLIMIT_INTERACTIVE,
					crypto_pwhash_ALG_DEFAULT) != 0) {
				return bad_mem;
			}

			for (size_t i = 0; i < sz(key); ++i) {
				priv_enc[i] = priv[i] ^ key[i];
			}

			crypto_box_seal(salt_enc, salt, sz(salt), priv);

			/* we have everything we need. now we create the
			 * file, failing  if it already exists so as not
			 * to clobber anyone's passwords.
			 */
			int db;
			const int flags = O_CREAT | (clobber ? O_TRUNC : O_EXCL);
			if(param == 0)
				db = dbopen(flags);
			else if (param == 1)
				db = open(params[0], flags, 0600);
			else return bad_syntax;

			if (db == -1) return bad_db_create;

			write(db, pub, sz(pub));
			write(db, salt, sz(salt));
			write(db, priv_enc, sz(priv_enc));
			write(db, salt_enc, sz(salt_enc));
			close(db);

			for (int i = 0; i < sz(key); ++i) {
				printf("%2x ", key[i]);
				if (!((i+1)%8)) printf("\n");
			}
			break;
		}
	}

#	ifdef _CLIPBOARD
	if (geteuid() == 0 && copy_pw) {
		/* on a sane system, what we'd do is hike up the process
		 * tree til we found a non-root user. alas, this is UNIX. */
		const char* realuser = getenv("SUDO_USER");
		if (realuser == null) realuser = "nobody";

		say("\x1b[1;33mwarning:\x1b[m you are running \x1b[4m");
			size_t namelen = strlen(argv[0]);
			write(2,argv[0],namelen);
		say("\x1b[24m as \x1b[1mroot\x1b[m! dropping to \x1b[1m");
			write(2,realuser,strlen(realuser));
			setenv("USER", realuser, true);
		say("\x1b[m to prevent malicious behavior\n");

		struct passwd* nobody = getpwnam(realuser);
		if (nobody == null) {
			say("\x1b[1;31mfatal:\x1b[m could not get UID to drop privileges; bailing");
			return bad_user;
		} else {
			setenv("HOME", nobody -> pw_dir, true);
			setenv("SHELL", "/dev/null", true);
			setuid(nobody -> pw_uid);
		}

	}
#	endif
	

	/* char buf[len+1]; */
	/* *buf = 0; */
	/* mkpw(mode,buf,len); */
	/* write(1, buf, len + 1); */

#	ifdef _CLIPBOARD
		if (copy_pw) {
			copy(buf,len);
		}
#	endif
	
	return ok;
}

int
main (int argc, const char** argv) {
	int e = 0;
	const char* msg;
	uint16_t level;
	uint8_t rv;
	switch (e = kpw(argc, argv)) {
		kpw_emit_error_switch
	}

	alert(level, msg);
	return rv;
}

Added kpw.d/makefile version [0ee772709b].



















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
include ../makerules

cdeps = compose.c iaia.c tbl.c
cpaths = $(cdeps:%=$(root)/clib/%)

$(root)/kpw: kpw.c opt.inc err.inc $(cpaths)
	$(cc) -I$(root) $< -lsodium -o $@ $(flags) $(cc-post)

tab = cat $< | awk -v emit=$1 -F'\t+' -f $<.awk >> $@

opt.inc: optab optab.awk
	:>$@
	$(call tab,cond)
	$(call tab,enum)
	$(call tab,argtbl)
	$(call tab,olong)
	$(call tab,oshort)
	$(call tab,usage)

err.inc: errtab errtab.awk
	:>$@
	$(call tab,enum)
	$(call tab,msg)
	
.PHONY: dbg

Added kpw.d/optab version [4bfd28626b].



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
C	create	op = createdb			create a new password database
R	rekey	op = rekeydb			change database passphrase and key
a	add		op = addpw				add password to the database
g	gen		op = genpw				add account with a random password
d	del		op = delpw				delete password from the database
c	chpw	op = chpw				change password in the database
l	lower	mode = lower			generate lowercase password
m	mix		mode = mix				generate mix-case password
u	upper	mode = upper			generate uppercase password
s	stupid	mode = stupid			circumvent dumb pw restrictions
k	key		op = keyin				save database key in session memory
o	logout	op = logout				delete db key from session memory
n	nocopy	copy_pw = false			don't copy generated pw to clipboard	_CLIPBOARD
p	print	print = true			display generated password
q	quiet	_g_alert_quiet = true	hide non-fatal reports
v	verbose	_g_debug_msgs = true	display debug reports
!	clobber	clobber = true			disable safety checks

Added kpw.d/optab.awk version [b43a4e1b77].

















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
BEGIN {
	end = ""
	if (emit == "enum") {
		print "enum args {"
	} else if (emit == "argtbl") {
		print "struct tblrow argtbl[] = {"
	} else if (emit == "olong") {
		print "#define kpw_emit_long_option_switch \\"
		end = "\\"
	} else if (emit == "oshort") {
		print "#define kpw_emit_short_option_switch \\"
		end = "\\"
	} else if (emit == "usage") {
		print "const char kpw_usage[] ="
	}
	globalc = 0
}

function say(line) {
	if (NF == 5) {
		print "\tkpw_only_" $5 "(" line ") " end
		globals[globalc] = $5
		++ globalc
	} else {
		print "\t" line " " end
	}
}

{ optstr = optstr $1 }
emit == "enum"   { say("arg_" $2 ",") }
emit == "argtbl" { say("{ arg_" $2", \"" $2 "\" },") }
emit == "olong"  { say("case arg_" $2 ": " $3 "; break;") }
emit == "oshort" { say("case '" $1 "': " $3 "; break;") }
emit == "usage"  { say("\"\\t-"$1", --"$2": "$4"\\n\"") }

emit == "cond" {
	if (NF == 5 && !($5 in condlist)) {
		condlist[$5] = 1
		print "#ifdef " $5
		print "#	define kpw_only_" $5 "(x...) x"
		print "#else"
		print "#	define kpw_only_" $5 "(...)"
		print "#endif"
	}
}

END {
	if (emit == "olong" || emit == "oshort") {
		print ""
	} else if (emit == "usage") {
		print ";"
		print "const char kpw_optstr[] = \" [-" optstr "] [args]\\n\";"
	} else if (emit != "cond") {
		print "};"
	}
}

Modified makefile from [bb9a75f0d8] to [6fa77053ea].

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
..
24
25
26
27
28
29
30





# to build project contained in their own directory, run 
# $ make (dir).proj

all: ctl conv xutil gen

xutil: xpriv safekill
  # X11 tools
gen: mkpw rosshil
  # procedural generators
conv: ord
  # converters
ctl: nkvd.so bgrd
  # manipulating disobedient software

nkvd.so: nkvd.c
................................................................................
	$(cc) $< -lutil -o$@ $(cc-post)

safekill: safekill.c
	$(cc) $< -lX11 -o$@ $(cc-post)

xpriv: xpriv.c
	$(cc) $< -lrt -lutil -lX11 -o $@ $(cc-post)












|







 







>
>
>
>
>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
..
24
25
26
27
28
29
30
31
32
33
34
35
# to build project contained in their own directory, run 
# $ make (dir).proj

all: ctl conv xutil gen

xutil: xpriv safekill
  # X11 tools
gen: mkpw kpw rosshil
  # procedural generators
conv: ord
  # converters
ctl: nkvd.so bgrd
  # manipulating disobedient software

nkvd.so: nkvd.c
................................................................................
	$(cc) $< -lutil -o$@ $(cc-post)

safekill: safekill.c
	$(cc) $< -lX11 -o$@ $(cc-post)

xpriv: xpriv.c
	$(cc) $< -lrt -lutil -lX11 -o $@ $(cc-post)

kpw: kpw.d/makefile
	$(MAKE) root=$(realpath .) flags=$(kpw-flags) -C kpw.d $(realpath .)/$@

.PHONY: kpw

Modified makerules from [8c92429cd1] to [77fcbe1814].

29
30
31
32
33
34
35



	$(sc) $< -o $@ $(sc-post)

%.proj: %/makefile
	cd $* && make $*

%.proj: %/make.sh
	cd $* && ./make.sh










>
>
>
29
30
31
32
33
34
35
36
37
38
	$(sc) $< -o $@ $(sc-post)

%.proj: %/makefile
	cd $* && make $*

%.proj: %/make.sh
	cd $* && ./make.sh

dep/%:
	make -C dep $*

Modified mkpw.c from [d671829e71] to [f1f30ece5b].

7
8
9
10
11
12
13





14
15
16
17
18
19
20
..
43
44
45
46
47
48
49


50
51
52
53
54
55
56
...
195
196
197
198
199
200
201
202

203
204
205
206
207
208
209
...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
 *      does this by attempting to execute a sequence
 *      of binaries, and then writing the password
 *      to STDIN of the binary that succeeds.
 *  ? generates passwords
 *  → mkpw is unlikely to be portable to non-POSIX
 *    systems, but should run fine on Linux as well
 *    as BSDs with getrandom() support.





 */

#include <unistd.h>
#include <sys/random.h>
#include <sys/syscall.h>
#include <stddef.h>
#include <stdint.h>
................................................................................

typedef enum bad {
	ok = 0,
	fail = 1,
	bad_user,
	bad_option,
	bad_syntax,


	bad_usage = 64, /* fleabsd idiom */
} bad;

typedef _Bool bool;
typedef unsigned long long iaia_word_type;
typedef bad iaia_error_type;
enum /* iaia errors */ {
................................................................................

	char buf[len+1];
	/* *buf = 0; */

	unsigned char noise[len];
	for (size_t i = 0; i<q; ++i) {
		unsigned char* cur = noise;
		syscall(SYS_getrandom, noise, len, 0);

		/* getrandom(noise, len, 0); // android doesnt like it */

		for(char* ptr = buf; ptr < buf + len; ++ptr, ++cur) {
			*ptr = tbl[*cur % chars]; /* such a waste of entropy… :( */
		}

		buf[len] = '\n';
................................................................................
			if (pipe(fds) != 0) return 63;
			if (!fork()) {
				close(fds[1]);
				dup2(fds[0], 0);
				if (clipboard_env != null) {
					execvp(clipboard_env, (char* const[]){
							clipboard_env, clipboard_env_arg, null});
					return -1;
				} else for(char* const** cmd = cbd_cmds; *cmd != null; ++cmd) {
					execvp((*cmd)[0], *cmd);
				}
				return -1; /* exec failed */
			} else {
				close(fds[0]);
				write(fds[1], buf, len);
				write(fds[1], "\n", 1);
				close(fds[1]);
			}
		}
#	endif
	
	return ok;
}







>
>
>
>
>







 







>
>







 







|
>







 







|



|











7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
..
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
...
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
 *      does this by attempting to execute a sequence
 *      of binaries, and then writing the password
 *      to STDIN of the binary that succeeds.
 *  ? generates passwords
 *  → mkpw is unlikely to be portable to non-POSIX
 *    systems, but should run fine on Linux as well
 *    as BSDs with getrandom() support.
 *  ! for getrandom() to work with the version of
 *    libc on my android phone, the getrandom() call
 *    had to be converted to use the syscall()
 *    interface. this is unlikely to cause problems,
 *    but should be kept in mind.
 */

#include <unistd.h>
#include <sys/random.h>
#include <sys/syscall.h>
#include <stddef.h>
#include <stdint.h>
................................................................................

typedef enum bad {
	ok = 0,
	fail = 1,
	bad_user,
	bad_option,
	bad_syntax,
	bad_entropy,
	bad_copy,
	bad_usage = 64, /* fleabsd idiom */
} bad;

typedef _Bool bool;
typedef unsigned long long iaia_word_type;
typedef bad iaia_error_type;
enum /* iaia errors */ {
................................................................................

	char buf[len+1];
	/* *buf = 0; */

	unsigned char noise[len];
	for (size_t i = 0; i<q; ++i) {
		unsigned char* cur = noise;
		if(syscall(SYS_getrandom, noise, len, 0) == -1)
			return bad_entropy;
		/* getrandom(noise, len, 0); // android doesnt like it */

		for(char* ptr = buf; ptr < buf + len; ++ptr, ++cur) {
			*ptr = tbl[*cur % chars]; /* such a waste of entropy… :( */
		}

		buf[len] = '\n';
................................................................................
			if (pipe(fds) != 0) return 63;
			if (!fork()) {
				close(fds[1]);
				dup2(fds[0], 0);
				if (clipboard_env != null) {
					execvp(clipboard_env, (char* const[]){
							clipboard_env, clipboard_env_arg, null});
					return bad_copy;
				} else for(char* const** cmd = cbd_cmds; *cmd != null; ++cmd) {
					execvp((*cmd)[0], *cmd);
				}
				return bad_copy; /* exec failed */
			} else {
				close(fds[0]);
				write(fds[1], buf, len);
				write(fds[1], "\n", 1);
				close(fds[1]);
			}
		}
#	endif
	
	return ok;
}