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.
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"
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"
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"
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"
52 /* Debian has 'mkpasswd' utility, manpage says:
55 mkpasswd - Overfeatured front end to crypt(3)
57 mkpasswd PASSWORD SALT
61 Use the STRING as salt. It must not contain prefixes such as
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.
68 Compute the password using the TYPE method. If TYPE is 'help'
69 then the available methods are printed.
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.
78 A list of options which will be evaluated before the ones
79 specified on the command line.
81 This programs suffers of a bad case of featuritis.
82 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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];
96 const char *opt_m, *opt_S;
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;
109 opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
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);
116 /* have no idea how to handle -s... */
118 if (argv[0] && !opt_S)
121 salt_ptr = crypt_make_pw_salt(salt, opt_m);
123 safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
125 xmove_fd(fd, STDIN_FILENO);
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 */
139 puts(pw_encrypt(password, salt, 1));