1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#define FOR_mkpasswd
25#include "toys.h"
26
27GLOBALS(
28 long P;
29 char *m, *S;
30)
31
32void mkpasswd_main(void)
33{
34 char salt[MAX_SALT_LEN] = {0,};
35 int i;
36
37 if (!TT.m) TT.m = "des";
38 if (toys.optc == 2) {
39 if (TT.S) error_exit("duplicate salt");
40 TT.S = toys.optargs[1];
41 }
42
43 if (-1 == (i = get_salt(salt, TT.m))) error_exit("bad -m");
44 if (TT.S) {
45 char *s = TT.S;
46
47
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.S);
52 }
53
54
55 if (TT.P) {
56 if (dup2(TT.P, 0) == -1) perror_exit("fd");
57 close(TT.P);
58 }
59
60
61 if (!*toys.optargs) {
62
63 if (isatty(0)) {
64 if (read_password(toybuf, sizeof(toybuf), "Password: "))
65 perror_exit("password read failed");
66 } else {
67 for (i = 0; i<sizeof(toybuf)-1; i++) {
68 if (!xread(0, toybuf+i, 1)) break;
69 if (toybuf[i] == '\n' || toybuf[i] == '\r') break;
70 }
71 toybuf[i] = 0;
72 }
73 }
74
75
76 xprintf("%s\n",crypt(*toys.optargs ? *toys.optargs : toybuf, salt));
77}
78