busybox: update to 1.23.2
[tomato.git] / release / src / router / busybox / loginutils / cryptpw.c
blob29f0fbe91abc23b3fb3c690a7315a009cf8090ef
1 /* vi: set sw=4 ts=4: */
2 /*
3 * cryptpw.c - output a crypt(3)ed password to stdout.
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
8 * mkpasswd compatible options added by Bernhard Reutner-Fischer
10 * Licensed under GPLv2, see file LICENSE in this source tree.
13 //usage:#define cryptpw_trivial_usage
14 //usage: "[OPTIONS] [PASSWORD] [SALT]"
15 /* We do support -s, we just don't mention it */
16 //usage:#define cryptpw_full_usage "\n\n"
17 //usage: "Crypt PASSWORD using crypt(3)\n"
18 //usage: IF_LONG_OPTS(
19 //usage: "\n -P,--password-fd=N Read password from fd N"
20 /* //usage: "\n -s,--stdin Use stdin; like -P0" */
21 //usage: "\n -m,--method=TYPE Encryption method"
22 //usage: "\n -S,--salt=SALT"
23 //usage: )
24 //usage: IF_NOT_LONG_OPTS(
25 //usage: "\n -P N Read password from fd N"
26 /* //usage: "\n -s Use stdin; like -P0" */
27 //usage: "\n -m TYPE Encryption method TYPE"
28 //usage: "\n -S SALT"
29 //usage: )
31 /* mkpasswd is an alias to cryptpw */
32 //usage:#define mkpasswd_trivial_usage
33 //usage: "[OPTIONS] [PASSWORD] [SALT]"
34 /* We do support -s, we just don't mention it */
35 //usage:#define mkpasswd_full_usage "\n\n"
36 //usage: "Crypt PASSWORD using crypt(3)\n"
37 //usage: IF_LONG_OPTS(
38 //usage: "\n -P,--password-fd=N Read password from fd N"
39 /* //usage: "\n -s,--stdin Use stdin; like -P0" */
40 //usage: "\n -m,--method=TYPE Encryption method"
41 //usage: "\n -S,--salt=SALT"
42 //usage: )
43 //usage: IF_NOT_LONG_OPTS(
44 //usage: "\n -P N Read password from fd N"
45 /* //usage: "\n -s Use stdin; like -P0" */
46 //usage: "\n -m TYPE Encryption method TYPE"
47 //usage: "\n -S SALT"
48 //usage: )
50 #include "libbb.h"
52 /* Debian has 'mkpasswd' utility, manpage says:
54 NAME
55 mkpasswd - Overfeatured front end to crypt(3)
56 SYNOPSIS
57 mkpasswd PASSWORD SALT
58 ...
59 OPTIONS
60 -S, --salt=STRING
61 Use the STRING as salt. It must not contain prefixes such as
62 $1$.
63 -R, --rounds=NUMBER
64 Use NUMBER rounds. This argument is ignored if the method
65 choosen does not support variable rounds. For the OpenBSD Blowfish
66 method this is the logarithm of the number of rounds.
67 -m, --method=TYPE
68 Compute the password using the TYPE method. If TYPE is 'help'
69 then the available methods are printed.
70 -P, --password-fd=NUM
71 Read the password from file descriptor NUM instead of using getpass(3).
72 If the file descriptor is not connected to a tty then
73 no other message than the hashed password is printed on stdout.
74 -s, --stdin
75 Like --password-fd=0.
76 ENVIRONMENT
77 $MKPASSWD_OPTIONS
78 A list of options which will be evaluated before the ones
79 specified on the command line.
80 BUGS
81 This programs suffers of a bad case of featuritis.
82 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84 Very true...
86 cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
87 to cryptpw. -a option (alias for -m) came from cryptpw.
90 int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
91 int cryptpw_main(int argc UNUSED_PARAM, char **argv)
93 char salt[MAX_PW_SALT_LEN];
94 char *salt_ptr;
95 char *password;
96 const char *opt_m, *opt_S;
97 int fd;
99 #if ENABLE_LONG_OPTS
100 static const char mkpasswd_longopts[] ALIGN1 =
101 "stdin\0" No_argument "s"
102 "password-fd\0" Required_argument "P"
103 "salt\0" Required_argument "S"
104 "method\0" Required_argument "m"
106 applet_long_options = mkpasswd_longopts;
107 #endif
108 fd = STDIN_FILENO;
109 opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
110 opt_S = NULL;
111 /* at most two non-option arguments; -P NUM */
112 opt_complementary = "?2:P+";
113 getopt32(argv, "sP:S:m:a:", &fd, &opt_S, &opt_m, &opt_m);
114 argv += optind;
116 /* have no idea how to handle -s... */
118 if (argv[0] && !opt_S)
119 opt_S = argv[1];
121 salt_ptr = crypt_make_pw_salt(salt, opt_m);
122 if (opt_S)
123 safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
125 xmove_fd(fd, STDIN_FILENO);
127 password = argv[0];
128 if (!password) {
129 /* Only mkpasswd, and only from tty, prompts.
130 * Otherwise it is a plain read. */
131 password = (isatty(STDIN_FILENO) && applet_name[0] == 'm')
132 ? bb_ask_stdin("Password: ")
133 : xmalloc_fgetline(stdin)
135 /* may still be NULL on EOF/error */
138 if (password)
139 puts(pw_encrypt(password, salt, 1));
141 return EXIT_SUCCESS;