pwhash.c revision 1.6 1 /* $NetBSD: pwhash.c,v 1.6 2004/07/02 00:05:23 sjg Exp $ */
2 /* $OpenBSD: encrypt.c,v 1.16 2002/02/16 21:27:45 millert Exp $ */
3
4 /*
5 * Copyright (c) 1996, Jason Downs. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28 #include <sys/cdefs.h>
29
30 #ifndef lint
31 __RCSID("$NetBSD: pwhash.c,v 1.6 2004/07/02 00:05:23 sjg Exp $");
32 #endif
33
34 #include <sys/types.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <pwd.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <login_cap.h>
44
45 #include <crypt.h>
46
47 /*
48 * Very simple little program, for encrypting passwords from the command
49 * line. Useful for scripts and such.
50 */
51
52 #define DO_MAKEKEY 0
53 #define DO_DES 1
54 #define DO_MD5 2
55 #define DO_BLF 3
56 #define DO_SHA1 4
57
58 static void
59 usage(void)
60 {
61
62 (void)fprintf(stderr,
63 "usage: %s [-b rounds] [-k] [-m] [-S rounds] [-s salt] [-p | string]\n",
64 getprogname());
65 exit(1);
66 }
67
68 static char *
69 trim(char *line)
70 {
71 char *ptr;
72
73 for (ptr = &line[strlen(line)-1]; ptr > line; ptr--) {
74 if (!isspace(*ptr))
75 break;
76 }
77 ptr[1] = '\0';
78
79 for (ptr = line; *ptr && isspace(*ptr); ptr++)
80 ;
81
82 return(ptr);
83 }
84
85 /* these are pulled from usr.bin/passwd/pwd_gensalt.c */
86 int pwd_gensalt(char *, int, struct passwd *, login_cap_t *, char);
87
88 static void
89 print_passwd(char *string, int operation, void *extra)
90 {
91 char msalt[3], *salt;
92 struct passwd pwd;
93 login_cap_t *lc;
94 char buffer[_PASSWORD_LEN];
95
96 switch(operation) {
97 case DO_MAKEKEY:
98 /*
99 * makekey mode: parse string into separate DES key and salt.
100 */
101 if (strlen(string) != 10) {
102 /* To be compatible... */
103 errx(1, "%s", strerror(EFTYPE));
104 }
105 strlcpy(msalt, &string[8], sizeof(msalt));
106 salt = msalt;
107 break;
108
109 case DO_MD5:
110 strlcpy(buffer, "$1$", sizeof(buffer));
111 __crypt_to64(&buffer[3], arc4random(), 4);
112 __crypt_to64(&buffer[7], arc4random(), 4);
113 strlcpy(buffer + 11, "$", sizeof(buffer) - 11);
114 salt = buffer;
115 break;
116
117 case DO_SHA1:
118 {
119 int n;
120
121 n = snprintf(buffer, sizeof(buffer),
122 "%s%u$", SHA1_MAGIC,
123 __crypt_sha1_iterations(*(int *)extra));
124 __crypt_to64(&buffer[n], arc4random(), 4);
125 __crypt_to64(&buffer[n + 4], arc4random(), 4);
126 buffer[n + 8] = '$';
127 buffer[n + 9] = '\0';
128 }
129 break;
130 case DO_BLF:
131 strlcpy(buffer, bcrypt_gensalt(*(int *)extra), _PASSWORD_LEN);
132 salt = buffer;
133 break;
134
135 case DO_DES:
136 salt = extra;
137 break;
138
139 default:
140 pwd.pw_name = "default";
141 if ((lc = login_getclass(NULL)) == NULL)
142 errx(1, "unable to get default login class.");
143 if (!pwd_gensalt(buffer, _PASSWORD_LEN, &pwd, lc, 'l'))
144 errx(1, "can't generate salt");
145 salt = buffer;
146 break;
147 }
148
149 (void)fputs(crypt(string, salt), stdout);
150 }
151
152 int
153 main(int argc, char **argv)
154 {
155 int opt;
156 int operation = -1;
157 int prompt = 0;
158 int rounds;
159 void *extra; /* Store salt or number of rounds */
160
161 setprogname(argv[0]);
162
163 if (strcmp(getprogname(), "makekey") == 0)
164 operation = DO_MAKEKEY;
165
166 while ((opt = getopt(argc, argv, "kmpS:s:b:")) != -1) {
167 switch (opt) {
168 case 'k': /* Stdin/Stdout Unix crypt */
169 if (operation != -1 || prompt)
170 usage();
171 operation = DO_MAKEKEY;
172 break;
173
174 case 'm': /* MD5 password hash */
175 if (operation != -1)
176 usage();
177 operation = DO_MD5;
178 break;
179
180 case 'p':
181 if (operation == DO_MAKEKEY)
182 usage();
183 prompt = 1;
184 break;
185
186 case 'S': /* SHA1 password hash */
187 if (operation != -1)
188 usage();
189 operation = DO_SHA1;
190 rounds = atoi(optarg);
191 extra = &rounds;
192 break;
193
194 case 's': /* Unix crypt (DES) */
195 if (operation != -1 || optarg[0] == '$')
196 usage();
197 operation = DO_DES;
198 extra = optarg;
199 break;
200
201 case 'b': /* Blowfish password hash */
202 if (operation != -1)
203 usage();
204 operation = DO_BLF;
205 rounds = atoi(optarg);
206 extra = &rounds;
207 break;
208
209 default:
210 usage();
211 }
212 }
213
214 if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
215 char line[BUFSIZ], *string;
216
217 if (prompt) {
218 string = getpass("Enter string: ");
219 print_passwd(string, operation, extra);
220 (void)fputc('\n', stdout);
221 } else {
222 /* Encrypt stdin to stdout. */
223 while (!feof(stdin) &&
224 (fgets(line, sizeof(line), stdin) != NULL)) {
225 /* Kill the whitesapce. */
226 string = trim(line);
227 if (*string == '\0')
228 continue;
229
230 print_passwd(string, operation, extra);
231
232 if (operation == DO_MAKEKEY) {
233 fflush(stdout);
234 break;
235 }
236 (void)fputc('\n', stdout);
237 }
238 }
239 } else {
240 char *string;
241
242 /* can't combine -p with a supplied string */
243 if (prompt)
244 usage();
245
246 /* Perhaps it isn't worth worrying about, but... */
247 if ((string = strdup(argv[optind])) == NULL)
248 err(1, NULL);
249 /* Wipe the argument. */
250 memset(argv[optind], 0, strlen(argv[optind]));
251
252 print_passwd(string, operation, extra);
253
254 (void)fputc('\n', stdout);
255
256 /* Wipe our copy, before we free it. */
257 memset(string, 0, strlen(string));
258 free(string);
259 }
260 exit(0);
261 }
262