Mercurial > hg > toybox
comparison toys/pending/mkpasswd.c @ 1363:e65f9a9ba62d draft
Cleanup pass on mkpasswd.c
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 25 Jun 2014 22:54:59 -0500 |
parents | 63db77909fc8 |
children |
comparison
equal
deleted
inserted
replaced
1362:b9605ebd3af4 | 1363:e65f9a9ba62d |
---|---|
9 | 9 |
10 config MKPASSWD | 10 config MKPASSWD |
11 bool "mkpasswd" | 11 bool "mkpasswd" |
12 default n | 12 default n |
13 help | 13 help |
14 usage: mkpasswd [OPTIONS] [PASSWORD] [SALT] | 14 usage: mkpasswd [-P FD] [-m TYPE] [-S SALT] [PASSWORD] [SALT] |
15 | 15 |
16 Crypt PASSWORD using crypt(3) | 16 Crypt PASSWORD using crypt(3) |
17 | 17 |
18 -P N Read password from fd N | 18 -P FD Read password from file descriptor FD |
19 -m TYPE Encryption method, when TYPE='help', then show the methods available | 19 -m TYPE Encryption method (des, md5, sha256, or sha512; default is des) |
20 -S SALT | 20 -S SALT |
21 */ | 21 */ |
22 | 22 |
23 #define FOR_mkpasswd | 23 #define FOR_mkpasswd |
24 #include "toys.h" | 24 #include "toys.h" |
27 long pfd; | 27 long pfd; |
28 char *method; | 28 char *method; |
29 char *salt; | 29 char *salt; |
30 ) | 30 ) |
31 | 31 |
32 | |
33 /* | |
34 * validate the salt provided by user. | |
35 * the allowed character set for salt is [./A-Za-z0-9] | |
36 */ | |
37 static void is_salt_valid(char *salt) | |
38 { | |
39 regex_t rp; | |
40 regmatch_t rm[1]; | |
41 char *regex = "[./A-Za-z0-9]*"; //salt REGEX | |
42 | |
43 xregcomp(&rp, regex, REG_NEWLINE); | |
44 | |
45 /* compare string against pattern -- remember that patterns | |
46 are anchored to the beginning of the line */ | |
47 if (regexec(&rp, salt, 1, rm, 0) == 0 && rm[0].rm_so == 0 | |
48 && rm[0].rm_eo == strlen(salt)) | |
49 return; | |
50 | |
51 error_exit("salt should be in character set [./A-Za-z0-9]"); | |
52 } | |
53 | |
54 void mkpasswd_main(void) | 32 void mkpasswd_main(void) |
55 { | 33 { |
56 int offset = 0; | |
57 char salt[MAX_SALT_LEN] = {0,}; | 34 char salt[MAX_SALT_LEN] = {0,}; |
35 int i; | |
58 | 36 |
59 if (!(toys.optflags & FLAG_m)) TT.method = "des"; | 37 if (!TT.method) TT.method = "des"; |
60 else if (!strcmp(TT.method, "help")) { | 38 if (toys.optc == 2) { |
61 xprintf("Available encryption methods are:\n" | 39 if (TT.salt) error_exit("duplicate salt"); |
62 " des\n md5\n sha256\n sha512\n"); | 40 TT.salt = toys.optargs[1]; |
63 return; | |
64 } | |
65 // If arguments are there, then the second argument is Salt, can be NULL also | |
66 if ((toys.optc == 2) && !(toys.optflags & FLAG_S)) TT.salt = toys.optargs[1]; | |
67 | |
68 offset= get_salt(salt, TT.method); | |
69 if (offset == -1) error_exit("unknown encryption method"); | |
70 if (TT.salt) { | |
71 is_salt_valid(TT.salt); | |
72 snprintf(salt + offset, MAX_SALT_LEN - offset, "%s", TT.salt); | |
73 } | 41 } |
74 | 42 |
75 if (toys.optflags & FLAG_P) { | 43 if (-1 == (i = get_salt(salt, TT.method))) error_exit("bad -m"); |
76 if (dup2(TT.pfd, STDIN_FILENO) == -1) perror_exit("fd"); | 44 if (TT.salt) { |
45 char *s = TT.salt; | |
46 | |
47 // In C locale, isalnum() means [A-Za-Z0-0] | |
48 while (isalnum(*s) || *s == '.' || *s == '/') s++; | |
49 if (*s) error_exit("salt not in [./A-Za-z0-9]"); | |
50 | |
51 snprintf(salt+i, sizeof(salt)-i, "%s", TT.salt); | |
52 } | |
53 | |
54 // Because read_password() doesn't have an fd argument | |
55 if (TT.pfd) { | |
56 if (dup2(TT.pfd, 0) == -1) perror_exit("fd"); | |
77 close(TT.pfd); | 57 close(TT.pfd); |
78 } | 58 } |
79 | 59 |
80 if (!toys.optc) { | 60 // If we haven't got a password on the command line, read it from tty or FD |
81 if (isatty(STDIN_FILENO)) { | 61 if (!*toys.optargs) { |
62 // Prompt and read interactively? | |
63 if (isatty(0)) { | |
82 if (read_password(toybuf, sizeof(toybuf), "Password: ")) | 64 if (read_password(toybuf, sizeof(toybuf), "Password: ")) |
83 perror_exit("password read failed"); | 65 perror_exit("password read failed"); |
84 } else { | 66 } else { |
85 // read from the given FD | 67 for (i = 0; i<sizeof(toybuf)-1; i++) { |
86 int i = 0; | 68 if (!xread(0, toybuf+i, 1)) break; |
87 while (1) { | 69 if (toybuf[i] == '\n' || toybuf[i] == '\r') break; |
88 int ret = read(0, &toybuf[i], 1); | |
89 if ( ret < 0 ) perror_exit("password read failed"); | |
90 else if (ret == 0 || toybuf[i] == '\n' || toybuf[i] == '\r' || | |
91 sizeof(toybuf) == i+1) { | |
92 toybuf[i] = '\0'; | |
93 break; | |
94 } | |
95 i++; | |
96 } | 70 } |
71 toybuf[i] = 0; | |
97 } | 72 } |
98 } else snprintf(toybuf, sizeof(toybuf), "%s", toys.optargs[0]); | 73 } |
99 | 74 |
100 // encrypt & print the password | 75 // encrypt & print the password |
101 xprintf("%s\n",crypt(toybuf, salt)); | 76 xprintf("%s\n",crypt(*toys.optargs ? *toys.optargs : toybuf, salt)); |
102 } | 77 } |