1 /* vi: set sw=4 ts=4: */
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.
12 //config:config CRYPTPW
13 //config: bool "cryptpw"
16 //config: Encrypts the given password with the crypt(3) libc function
17 //config: using the given salt. Debian has this utility under mkpasswd
18 //config: name. Busybox provides mkpasswd as an alias for cryptpw.
20 //applet:IF_CRYPTPW(APPLET(cryptpw, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //applet:IF_CRYPTPW(APPLET_ODDNAME(mkpasswd, cryptpw, BB_DIR_USR_BIN, BB_SUID_DROP, mkpasswd))
23 //kbuild:lib-$(CONFIG_CRYPTPW) += cryptpw.o
25 //usage:#define cryptpw_trivial_usage
26 //usage: "[OPTIONS] [PASSWORD] [SALT]"
27 /* We do support -s, we just don't mention it */
28 //usage:#define cryptpw_full_usage "\n\n"
29 //usage: "Crypt PASSWORD using crypt(3)\n"
30 //usage: IF_LONG_OPTS(
31 //usage: "\n -P,--password-fd=N Read password from fd N"
32 /* //usage: "\n -s,--stdin Use stdin; like -P0" */
33 //usage: "\n -m,--method=TYPE Encryption method"
34 //usage: "\n -S,--salt=SALT"
36 //usage: IF_NOT_LONG_OPTS(
37 //usage: "\n -P N Read password from fd N"
38 /* //usage: "\n -s Use stdin; like -P0" */
39 //usage: "\n -m TYPE Encryption method TYPE"
43 /* mkpasswd is an alias to cryptpw */
44 //usage:#define mkpasswd_trivial_usage
45 //usage: "[OPTIONS] [PASSWORD] [SALT]"
46 /* We do support -s, we just don't mention it */
47 //usage:#define mkpasswd_full_usage "\n\n"
48 //usage: "Crypt PASSWORD using crypt(3)\n"
49 //usage: IF_LONG_OPTS(
50 //usage: "\n -P,--password-fd=N Read password from fd N"
51 /* //usage: "\n -s,--stdin Use stdin; like -P0" */
52 //usage: "\n -m,--method=TYPE Encryption method"
53 //usage: "\n -S,--salt=SALT"
55 //usage: IF_NOT_LONG_OPTS(
56 //usage: "\n -P N Read password from fd N"
57 /* //usage: "\n -s Use stdin; like -P0" */
58 //usage: "\n -m TYPE Encryption method TYPE"
64 /* Debian has 'mkpasswd' utility, manpage says:
67 mkpasswd - Overfeatured front end to crypt(3)
69 mkpasswd PASSWORD SALT
73 Use the STRING as salt. It must not contain prefixes such as
76 Use NUMBER rounds. This argument is ignored if the method
77 choosen does not support variable rounds. For the OpenBSD Blowfish
78 method this is the logarithm of the number of rounds.
80 Compute the password using the TYPE method. If TYPE is 'help'
81 then the available methods are printed.
83 Read the password from file descriptor NUM instead of using getpass(3).
84 If the file descriptor is not connected to a tty then
85 no other message than the hashed password is printed on stdout.
90 A list of options which will be evaluated before the ones
91 specified on the command line.
93 This programs suffers of a bad case of featuritis.
94 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98 cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
99 to cryptpw. -a option (alias for -m) came from cryptpw.
102 int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
103 int cryptpw_main(int argc UNUSED_PARAM, char **argv)
105 char salt[MAX_PW_SALT_LEN];
108 const char *opt_m, *opt_S;
112 static const char mkpasswd_longopts[] ALIGN1 =
113 "stdin\0" No_argument "s"
114 "password-fd\0" Required_argument "P"
115 "salt\0" Required_argument "S"
116 "method\0" Required_argument "m"
118 applet_long_options = mkpasswd_longopts;
121 opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
123 /* at most two non-option arguments; -P NUM */
124 opt_complementary = "?2:P+";
125 getopt32(argv, "sP:S:m:a:", &fd, &opt_S, &opt_m, &opt_m);
128 /* have no idea how to handle -s... */
130 if (argv[0] && !opt_S)
133 salt_ptr = crypt_make_pw_salt(salt, opt_m);
135 safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
137 xmove_fd(fd, STDIN_FILENO);
141 /* Only mkpasswd, and only from tty, prompts.
142 * Otherwise it is a plain read. */
143 password = (isatty(STDIN_FILENO) && applet_name[0] == 'm')
144 ? bb_ask_stdin("Password: ")
145 : xmalloc_fgetline(stdin)
147 /* may still be NULL on EOF/error */
151 puts(pw_encrypt(password, salt, 1));