1 |
/* vi: set sw=4 ts=4: */ |
/* vi: set sw=4 ts=4: */ |
2 |
/* |
/* |
3 |
* cryptpw.c |
* cryptpw.c - output a crypt(3)ed password to stdout. |
4 |
|
* |
5 |
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
6 |
* |
* |
7 |
* Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no> |
* Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no> |
8 |
|
* mkpasswd compatible options added by Bernhard Reutner-Fischer |
9 |
|
* |
10 |
|
* Licensed under GPLv2, see file LICENSE in this tarball for details. |
11 |
*/ |
*/ |
12 |
|
|
13 |
#include "libbb.h" |
#include "libbb.h" |
14 |
|
|
15 |
#define TESTING 0 |
/* Debian has 'mkpasswd' utility, manpage says: |
16 |
|
|
17 |
/* |
NAME |
18 |
set TESTING to 1 and pipe some file through this script |
mkpasswd - Overfeatured front end to crypt(3) |
19 |
if you played with bbox's crypt implementation. |
SYNOPSIS |
20 |
|
mkpasswd PASSWORD SALT |
21 |
while read line; do |
... |
22 |
n=`./busybox cryptpw -a des -- "$line"` |
OPTIONS |
23 |
o=`./busybox_org cryptpw -a des -- "$line"` |
-S, --salt=STRING |
24 |
test "$n" != "$o" && { |
Use the STRING as salt. It must not contain prefixes such as |
25 |
echo n="$n" |
$1$. |
26 |
echo o="$o" |
-R, --rounds=NUMBER |
27 |
exit |
Use NUMBER rounds. This argument is ignored if the method |
28 |
} |
choosen does not support variable rounds. For the OpenBSD Blowfish |
29 |
n=`./busybox cryptpw -- "$line"` |
method this is the logarithm of the number of rounds. |
30 |
o=`./busybox_org cryptpw -- "$line"` |
-m, --method=TYPE |
31 |
test "$n" != "$o" && { |
Compute the password using the TYPE method. If TYPE is 'help' |
32 |
echo n="$n" |
then the available methods are printed. |
33 |
echo o="$o" |
-P, --password-fd=NUM |
34 |
exit |
Read the password from file descriptor NUM instead of using getpass(3). |
35 |
} |
If the file descriptor is not connected to a tty then |
36 |
done |
no other message than the hashed password is printed on stdout. |
37 |
*/ |
-s, --stdin |
38 |
|
Like --password-fd=0. |
39 |
|
ENVIRONMENT |
40 |
|
$MKPASSWD_OPTIONS |
41 |
|
A list of options which will be evaluated before the ones |
42 |
|
specified on the command line. |
43 |
|
BUGS |
44 |
|
This programs suffers of a bad case of featuritis. |
45 |
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
46 |
|
|
47 |
|
Very true... |
48 |
|
|
49 |
|
cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd |
50 |
|
to cryptpw. -a option (alias for -m) came from cryptpw. |
51 |
|
*/ |
52 |
|
|
53 |
int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
54 |
int cryptpw_main(int argc UNUSED_PARAM, char **argv) |
int cryptpw_main(int argc UNUSED_PARAM, char **argv) |
55 |
{ |
{ |
56 |
char salt[sizeof("$N$XXXXXXXX")]; |
/* $N$ + sha_salt_16_bytes + NUL */ |
57 |
char *opt_a; |
char salt[3 + 16 + 1]; |
58 |
|
char *salt_ptr; |
59 |
if (!getopt32(argv, "a:", &opt_a) || opt_a[0] != 'd') { |
const char *opt_m, *opt_S; |
60 |
salt[0] = '$'; |
int len; |
61 |
salt[1] = '1'; |
int fd; |
62 |
salt[2] = '$'; |
|
63 |
crypt_make_salt(salt + 3, 4, 0); /* md5 */ |
#if ENABLE_LONG_OPTS |
64 |
#if TESTING |
static const char mkpasswd_longopts[] ALIGN1 = |
65 |
strcpy(salt + 3, "ajg./bcf"); |
"stdin\0" No_argument "s" |
66 |
|
"password-fd\0" Required_argument "P" |
67 |
|
"salt\0" Required_argument "S" |
68 |
|
"method\0" Required_argument "m" |
69 |
|
; |
70 |
|
applet_long_options = mkpasswd_longopts; |
71 |
#endif |
#endif |
72 |
} else { |
fd = STDIN_FILENO; |
73 |
crypt_make_salt(salt, 1, 0); /* des */ |
opt_m = "d"; |
74 |
#if TESTING |
opt_S = NULL; |
75 |
strcpy(salt, "a."); |
/* at most two non-option arguments; -P NUM */ |
76 |
|
opt_complementary = "?2:P+"; |
77 |
|
getopt32(argv, "sP:S:m:a:", &fd, &opt_S, &opt_m, &opt_m); |
78 |
|
argv += optind; |
79 |
|
|
80 |
|
/* have no idea how to handle -s... */ |
81 |
|
|
82 |
|
if (argv[0] && !opt_S) |
83 |
|
opt_S = argv[1]; |
84 |
|
|
85 |
|
len = 2/2; |
86 |
|
salt_ptr = salt; |
87 |
|
if (opt_m[0] != 'd') { /* not des */ |
88 |
|
len = 8/2; /* so far assuming md5 */ |
89 |
|
*salt_ptr++ = '$'; |
90 |
|
*salt_ptr++ = '1'; |
91 |
|
*salt_ptr++ = '$'; |
92 |
|
#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA |
93 |
|
if (opt_m[0] == 's') { /* sha */ |
94 |
|
salt[1] = '5' + (strcmp(opt_m, "sha512") == 0); |
95 |
|
len = 16/2; |
96 |
|
} |
97 |
#endif |
#endif |
98 |
} |
} |
99 |
|
if (opt_S) |
100 |
|
safe_strncpy(salt_ptr, opt_S, sizeof(salt) - 3); |
101 |
|
else |
102 |
|
crypt_make_salt(salt_ptr, len, 0); |
103 |
|
|
104 |
|
xmove_fd(fd, STDIN_FILENO); |
105 |
|
|
106 |
|
puts(pw_encrypt( |
107 |
|
argv[0] ? argv[0] : ( |
108 |
|
/* Only mkpasswd, and only from tty, prompts. |
109 |
|
* Otherwise it is a plain read. */ |
110 |
|
(isatty(STDIN_FILENO) && applet_name[0] == 'm') |
111 |
|
? bb_ask_stdin("Password: ") |
112 |
|
: xmalloc_fgetline(stdin) |
113 |
|
), |
114 |
|
salt, 1)); |
115 |
|
|
116 |
puts(pw_encrypt(argv[optind] ? argv[optind] : xmalloc_fgetline(stdin), salt, 1)); |
return EXIT_SUCCESS; |
|
|
|
|
return 0; |
|
117 |
} |
} |