-
Notifications
You must be signed in to change notification settings - Fork 5
/
passtokey.c
94 lines (84 loc) · 1.58 KB
/
passtokey.c
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
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "util.h"
char *readpass(char *pw, int n);
void setecho(int);
static void
usage(void)
{
fprint(2, "usage: passtokey\n");
exit(1);
}
int
main(int argc, char *argv[])
{
int c;
char pw0[28+1], pw1[28+1];
uchar key[Deskeylen];
char *err;
while((c = getopt(argc, argv, "")) != -1)
switch(c) {
default:
usage();
}
argc -= optind;
argv += optind;
if(argc != 0)
usage();
setecho(0);
for(;;) {
fprintf(stderr, "password: ");
fflush(stderr);
if((err = readpass(pw0, sizeof pw0)) != nil) {
fprintf(stderr, "\nbad password (%s), try again\n", err);
continue;
}
fprintf(stderr, "\nconfirm: ");
readpass(pw1, sizeof pw1);
if(strcmp(pw0, pw1) == 0)
break;
fprintf(stderr, "\nmismatch, try again\n");
}
setecho(1);
fprintf(stderr, "\n");
passtokey(key, pw0);
if(write(1, key, sizeof key) != sizeof key) {
fprintf(stderr, "writing key: %s\n", strerror(errno));
exit(1);
}
exit(0);
}
void
setecho(int on)
{
struct termios term;
tcgetattr(0, &term);
if(on)
term.c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL);
else
term.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
tcsetattr(0, TCSAFLUSH, &term);
}
char *
readpass(char *pw, int n)
{
size_t len;
char buf[64];
if(fgets(buf, sizeof buf, stdin) == nil)
return "read error";
len = strlen(buf);
if(buf[len-1] == '\n')
buf[--len] = '\0';
if(len < 8)
return "too short";
if(len >= n)
return "too long";
memmove(pw, buf, len+1);
return nil;
}