55dcc291447385ec8cc140b149aa38e84800b513
[oweals/busybox.git] / loginutils / cryptpw.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * cryptpw.c - output a crypt(3)ed password to stdout.
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6  *
7  * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
8  * mkpasswd compatible options added by Bernhard Reutner-Fischer
9  *
10  * Licensed under GPLv2, see file LICENSE in this source tree.
11  */
12 //config:config CRYPTPW
13 //config:       bool "cryptpw"
14 //config:       default y
15 //config:       help
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.
19
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))
22
23 //kbuild:lib-$(CONFIG_CRYPTPW) += cryptpw.o
24
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"
35 //usage:        )
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"
40 //usage:     "\n        -S SALT"
41 //usage:        )
42
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"
54 //usage:        )
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"
59 //usage:     "\n        -S SALT"
60 //usage:        )
61
62 #include "libbb.h"
63
64 /* Debian has 'mkpasswd' utility, manpage says:
65
66 NAME
67     mkpasswd - Overfeatured front end to crypt(3)
68 SYNOPSIS
69     mkpasswd PASSWORD SALT
70 ...
71 OPTIONS
72 -S, --salt=STRING
73     Use the STRING as salt. It must not  contain  prefixes  such  as
74     $1$.
75 -R, --rounds=NUMBER
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.
79 -m, --method=TYPE
80     Compute the password using the TYPE method. If TYPE is 'help'
81     then the available methods are printed.
82 -P, --password-fd=NUM
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.
86 -s, --stdin
87     Like --password-fd=0.
88 ENVIRONMENT
89     $MKPASSWD_OPTIONS
90     A list of options which will be evaluated before the ones
91     specified on the command line.
92 BUGS
93     This programs suffers of a bad case of featuritis.
94     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
95
96 Very true...
97
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.
100 */
101
102 int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
103 int cryptpw_main(int argc UNUSED_PARAM, char **argv)
104 {
105         char salt[MAX_PW_SALT_LEN];
106         char *salt_ptr;
107         char *password;
108         const char *opt_m, *opt_S;
109         int fd;
110
111 #if ENABLE_LONG_OPTS
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"
117         ;
118         applet_long_options = mkpasswd_longopts;
119 #endif
120         fd = STDIN_FILENO;
121         opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
122         opt_S = NULL;
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);
126         argv += optind;
127
128         /* have no idea how to handle -s... */
129
130         if (argv[0] && !opt_S)
131                 opt_S = argv[1];
132
133         salt_ptr = crypt_make_pw_salt(salt, opt_m);
134         if (opt_S)
135                 safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
136
137         xmove_fd(fd, STDIN_FILENO);
138
139         password = argv[0];
140         if (!password) {
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)
146                 ;
147                 /* may still be NULL on EOF/error */
148         }
149
150         if (password)
151                 puts(pw_encrypt(password, salt, 1));
152
153         return EXIT_SUCCESS;
154 }