blob: 29f0fbe91abc23b3fb3c690a7315a009cf8090ef [file] [log] [blame]
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +00001/* vi: set sw=4 ts=4: */
2/*
Bernhard Reutner-Fischer9c6d1292008-12-04 14:19:21 +00003 * cryptpw.c - output a crypt(3)ed password to stdout.
4 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko4b924f32007-05-30 00:29:55 +00006 *
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +00007 * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
Denis Vlasenkoe45930e2008-12-04 12:05:26 +00008 * mkpasswd compatible options added by Bernhard Reutner-Fischer
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00009 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000011 */
12
Pere Orga6a3e01d2011-04-01 22:56:30 +020013//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"
Denys Vlasenko0a8971d2011-05-12 03:14:12 +020017//usage: "Crypt PASSWORD using crypt(3)\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020018//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" */
Denys Vlasenko12a43272011-05-13 03:19:01 +020021//usage: "\n -m,--method=TYPE Encryption method"
Pere Orga6a3e01d2011-04-01 22:56:30 +020022//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: )
30
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"
Denys Vlasenko0a8971d2011-05-12 03:14:12 +020036//usage: "Crypt PASSWORD using crypt(3)\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020037//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" */
Denys Vlasenko12a43272011-05-13 03:19:01 +020040//usage: "\n -m,--method=TYPE Encryption method"
Pere Orga6a3e01d2011-04-01 22:56:30 +020041//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: )
49
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000050#include "libbb.h"
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000051
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000052/* Debian has 'mkpasswd' utility, manpage says:
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000053
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000054NAME
55 mkpasswd - Overfeatured front end to crypt(3)
56SYNOPSIS
57 mkpasswd PASSWORD SALT
58...
59OPTIONS
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.
76ENVIRONMENT
77 $MKPASSWD_OPTIONS
78 A list of options which will be evaluated before the ones
79 specified on the command line.
80BUGS
81 This programs suffers of a bad case of featuritis.
82 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000083
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000084Very true...
85
86cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
87to cryptpw. -a option (alias for -m) came from cryptpw.
88*/
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000089
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000090int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000091int cryptpw_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000092{
Denys Vlasenko12a43272011-05-13 03:19:01 +020093 char salt[MAX_PW_SALT_LEN];
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000094 char *salt_ptr;
maxwen27116ba2015-08-14 21:41:28 +020095 char *password;
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000096 const char *opt_m, *opt_S;
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000097 int fd;
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000098
Denys Vlasenkof3b92d32009-06-19 12:10:38 +020099#if ENABLE_LONG_OPTS
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000100 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"
105 ;
106 applet_long_options = mkpasswd_longopts;
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000107#endif
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000108 fd = STDIN_FILENO;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200109 opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000110 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;
115
116 /* have no idea how to handle -s... */
117
118 if (argv[0] && !opt_S)
119 opt_S = argv[1];
120
Denys Vlasenko12a43272011-05-13 03:19:01 +0200121 salt_ptr = crypt_make_pw_salt(salt, opt_m);
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000122 if (opt_S)
Denys Vlasenko12a43272011-05-13 03:19:01 +0200123 safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000124
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000125 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000126
maxwen27116ba2015-08-14 21:41:28 +0200127 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')
Bernhard Reutner-Fischer9c6d1292008-12-04 14:19:21 +0000132 ? bb_ask_stdin("Password: ")
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000133 : xmalloc_fgetline(stdin)
maxwen27116ba2015-08-14 21:41:28 +0200134 ;
135 /* may still be NULL on EOF/error */
136 }
137
138 if (password)
139 puts(pw_encrypt(password, salt, 1));
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000140
141 return EXIT_SUCCESS;
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000142}