pwhash.c revision 1.10 1 /* $NetBSD: pwhash.c,v 1.10 2005/01/11 22:56:19 christos 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.10 2005/01/11 22:56:19 christos 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 [-km] [-b rounds] [-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((unsigned char)*ptr))
75 break;
76 }
77 ptr[1] = '\0';
78
79 for (ptr = line; *ptr && isspace((unsigned char)*ptr); ptr++)
80 ;
81
82 return(ptr);
83 }
84
85 static void
86 print_passwd(char *string, int operation, void *extra)
87 {
88 char msalt[3], *salt;
89 struct passwd pwd;
90 char buf[_PASSWORD_LEN];
91 int error;
92
93 salt = buf;
94
95 switch(operation) {
96 case DO_MAKEKEY:
97 /*
98 * makekey mode: parse string into separate DES key and salt.
99 */
100 if (strlen(string) != 10) {
101 /* To be compatible... */
102 errx(1, "%s", strerror(EFTYPE));
103 }
104 (void)strlcpy(msalt, &string[8], sizeof(msalt));
105 salt = msalt;
106 error = 0;
107 break;
108
109 case DO_MD5:
110 error = __gensalt_md5(buf, sizeof(buf), *(int *)extra);
111 break;
112
113 case DO_SHA1:
114 error = __gensalt_sha1(buf, sizeof(buf), *(int *)extra);
115 break;
116
117 case DO_BLF:
118 error = __gensalt_blowfish(buf, sizeof(buf), *(int *)extra);
119 break;
120
121 case DO_DES:
122 error = 0;
123 salt = extra;
124 break;
125
126 default:
127 pwd.pw_name = "default";
128 error = pw_gensalt(buf, _PASSWORD_LEN, &pwd, 'l');
129 salt = buf;
130 break;
131 }
132
133 if (error)
134 err(1, "Cannot generate salt");
135
136 (void)fputs(crypt(string, salt), stdout);
137 }
138
139 int
140 main(int argc, char **argv)
141 {
142 int opt;
143 int operation = -1;
144 int prompt = 0;
145 int rounds;
146 void *extra; /* Store salt or number of rounds */
147
148 setprogname(argv[0]);
149
150 if (strcmp(getprogname(), "makekey") == 0)
151 operation = DO_MAKEKEY;
152
153 while ((opt = getopt(argc, argv, "kmpS:s:b:")) != -1) {
154 switch (opt) {
155 case 'k': /* Stdin/Stdout Unix crypt */
156 if (operation != -1 || prompt)
157 usage();
158 operation = DO_MAKEKEY;
159 break;
160
161 case 'm': /* MD5 password hash */
162 if (operation != -1)
163 usage();
164 operation = DO_MD5;
165 break;
166
167 case 'p':
168 if (operation == DO_MAKEKEY)
169 usage();
170 prompt = 1;
171 break;
172
173 case 'S': /* SHA1 password hash */
174 if (operation != -1)
175 usage();
176 operation = DO_SHA1;
177 rounds = atoi(optarg);
178 extra = &rounds;
179 break;
180
181 case 's': /* Unix crypt (DES) */
182 if (operation != -1 || optarg[0] == '$')
183 usage();
184 operation = DO_DES;
185 extra = optarg;
186 break;
187
188 case 'b': /* Blowfish password hash */
189 if (operation != -1)
190 usage();
191 operation = DO_BLF;
192 rounds = atoi(optarg);
193 extra = &rounds;
194 break;
195
196 default:
197 usage();
198 }
199 }
200
201 if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
202 char line[BUFSIZ], *string;
203
204 if (prompt) {
205 string = getpass("Enter string: ");
206 print_passwd(string, operation, extra);
207 (void)fputc('\n', stdout);
208 } else {
209 /* Encrypt stdin to stdout. */
210 while (!feof(stdin) &&
211 (fgets(line, sizeof(line), stdin) != NULL)) {
212 /* Kill the whitesapce. */
213 string = trim(line);
214 if (*string == '\0')
215 continue;
216
217 print_passwd(string, operation, extra);
218
219 if (operation == DO_MAKEKEY) {
220 fflush(stdout);
221 break;
222 }
223 (void)fputc('\n', stdout);
224 }
225 }
226 } else {
227 char *string;
228
229 /* can't combine -p with a supplied string */
230 if (prompt)
231 usage();
232
233 /* Perhaps it isn't worth worrying about, but... */
234 if ((string = strdup(argv[optind])) == NULL)
235 err(1, NULL);
236 /* Wipe the argument. */
237 memset(argv[optind], 0, strlen(argv[optind]));
238
239 print_passwd(string, operation, extra);
240
241 (void)fputc('\n', stdout);
242
243 /* Wipe our copy, before we free it. */
244 memset(string, 0, strlen(string));
245 free(string);
246 }
247 exit(0);
248 }
249