blob: bbaa858dafa2b1c1d72783e57f60ef0c1be49369 [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: "\nOptions:"
19//usage: IF_LONG_OPTS(
20//usage: "\n -P,--password-fd=N Read password from fd N"
21/* //usage: "\n -s,--stdin Use stdin; like -P0" */
22//usage: "\n -m,--method=TYPE Encryption method TYPE"
23//usage: "\n -S,--salt=SALT"
24//usage: )
25//usage: IF_NOT_LONG_OPTS(
26//usage: "\n -P N Read password from fd N"
27/* //usage: "\n -s Use stdin; like -P0" */
28//usage: "\n -m TYPE Encryption method TYPE"
29//usage: "\n -S SALT"
30//usage: )
31
32/* mkpasswd is an alias to cryptpw */
33//usage:#define mkpasswd_trivial_usage
34//usage: "[OPTIONS] [PASSWORD] [SALT]"
35/* We do support -s, we just don't mention it */
36//usage:#define mkpasswd_full_usage "\n\n"
Denys Vlasenko0a8971d2011-05-12 03:14:12 +020037//usage: "Crypt PASSWORD using crypt(3)\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020038//usage: "\nOptions:"
39//usage: IF_LONG_OPTS(
40//usage: "\n -P,--password-fd=N Read password from fd N"
41/* //usage: "\n -s,--stdin Use stdin; like -P0" */
42//usage: "\n -m,--method=TYPE Encryption method TYPE"
43//usage: "\n -S,--salt=SALT"
44//usage: )
45//usage: IF_NOT_LONG_OPTS(
46//usage: "\n -P N Read password from fd N"
47/* //usage: "\n -s Use stdin; like -P0" */
48//usage: "\n -m TYPE Encryption method TYPE"
49//usage: "\n -S SALT"
50//usage: )
51
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000052#include "libbb.h"
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000053
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000054/* Debian has 'mkpasswd' utility, manpage says:
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000055
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000056NAME
57 mkpasswd - Overfeatured front end to crypt(3)
58SYNOPSIS
59 mkpasswd PASSWORD SALT
60...
61OPTIONS
62-S, --salt=STRING
63 Use the STRING as salt. It must not contain prefixes such as
64 $1$.
65-R, --rounds=NUMBER
66 Use NUMBER rounds. This argument is ignored if the method
67 choosen does not support variable rounds. For the OpenBSD Blowfish
68 method this is the logarithm of the number of rounds.
69-m, --method=TYPE
70 Compute the password using the TYPE method. If TYPE is 'help'
71 then the available methods are printed.
72-P, --password-fd=NUM
73 Read the password from file descriptor NUM instead of using getpass(3).
74 If the file descriptor is not connected to a tty then
75 no other message than the hashed password is printed on stdout.
76-s, --stdin
77 Like --password-fd=0.
78ENVIRONMENT
79 $MKPASSWD_OPTIONS
80 A list of options which will be evaluated before the ones
81 specified on the command line.
82BUGS
83 This programs suffers of a bad case of featuritis.
84 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000085
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000086Very true...
87
88cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
89to cryptpw. -a option (alias for -m) came from cryptpw.
90*/
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +000091
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000092int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000093int cryptpw_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000094{
Denis Vlasenkoe45930e2008-12-04 12:05:26 +000095 /* $N$ + sha_salt_16_bytes + NUL */
96 char salt[3 + 16 + 1];
97 char *salt_ptr;
98 const char *opt_m, *opt_S;
99 int len;
100 int fd;
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000101
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200102#if ENABLE_LONG_OPTS
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000103 static const char mkpasswd_longopts[] ALIGN1 =
104 "stdin\0" No_argument "s"
105 "password-fd\0" Required_argument "P"
106 "salt\0" Required_argument "S"
107 "method\0" Required_argument "m"
108 ;
109 applet_long_options = mkpasswd_longopts;
Denis Vlasenko4ea83bf2008-06-12 16:55:59 +0000110#endif
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000111 fd = STDIN_FILENO;
112 opt_m = "d";
113 opt_S = NULL;
114 /* at most two non-option arguments; -P NUM */
115 opt_complementary = "?2:P+";
116 getopt32(argv, "sP:S:m:a:", &fd, &opt_S, &opt_m, &opt_m);
117 argv += optind;
118
119 /* have no idea how to handle -s... */
120
121 if (argv[0] && !opt_S)
122 opt_S = argv[1];
123
124 len = 2/2;
125 salt_ptr = salt;
126 if (opt_m[0] != 'd') { /* not des */
127 len = 8/2; /* so far assuming md5 */
128 *salt_ptr++ = '$';
129 *salt_ptr++ = '1';
130 *salt_ptr++ = '$';
Denis Vlasenko2211d522008-11-10 18:52:35 +0000131#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000132 if (opt_m[0] == 's') { /* sha */
133 salt[1] = '5' + (strcmp(opt_m, "sha512") == 0);
134 len = 16/2;
Denis Vlasenko2211d522008-11-10 18:52:35 +0000135 }
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000136#endif
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000137 }
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000138 if (opt_S)
139 safe_strncpy(salt_ptr, opt_S, sizeof(salt) - 3);
140 else
141 crypt_make_salt(salt_ptr, len, 0);
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000142
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000143 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000144
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000145 puts(pw_encrypt(
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200146 argv[0] ? argv[0] : (
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000147 /* Only mkpasswd, and only from tty, prompts.
148 * Otherwise it is a plain read. */
Bernhard Reutner-Fischer9c6d1292008-12-04 14:19:21 +0000149 (isatty(STDIN_FILENO) && applet_name[0] == 'm')
150 ? bb_ask_stdin("Password: ")
Denis Vlasenkoe45930e2008-12-04 12:05:26 +0000151 : xmalloc_fgetline(stdin)
152 ),
153 salt, 1));
154
155 return EXIT_SUCCESS;
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +0000156}