pwhash.c revision 1.11 1 /* $NetBSD: pwhash.c,v 1.11 2005/01/12 03:35:34 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.11 2005/01/12 03:35:34 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 <limits.h>
44 #include <login_cap.h>
45 #include <util.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, const char *extra)
87 {
88 char msalt[3];
89 const char *salt;
90 char buf[_PASSWORD_LEN];
91 char option[LINE_MAX], *key, *opt;
92 int error;
93
94 salt = buf;
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 (void)strlcpy(msalt, &string[8], sizeof(msalt));
106 salt = msalt;
107 error = 0;
108 break;
109
110 case DO_MD5:
111 error = pw_gensalt(buf, _PASSWORD_LEN, "md5", NULL);
112 salt = buf;
113 break;
114
115 case DO_SHA1:
116 error = pw_gensalt(buf, _PASSWORD_LEN, "sha1", NULL);
117 salt = buf;
118 break;
119
120 case DO_BLF:
121 error = pw_gensalt(buf, _PASSWORD_LEN, "blowfish", NULL);
122 salt = buf;
123 break;
124
125 case DO_DES:
126 error = 0;
127 salt = extra;
128 break;
129
130 default:
131 pw_getconf(option, sizeof(option), "default", "localcipher");
132 opt = option;
133 key = strsep(&opt, ",");
134 error = pw_gensalt(buf, _PASSWORD_LEN, key, opt);
135 salt = buf;
136 break;
137 }
138
139 if (error)
140 err(1, "Cannot generate salt");
141
142 (void)fputs(crypt(string, salt), stdout);
143 }
144
145 int
146 main(int argc, char **argv)
147 {
148 int opt;
149 int operation = -1;
150 int prompt = 0;
151 char *extra; /* Store salt or number of rounds */
152
153 setprogname(argv[0]);
154
155 if (strcmp(getprogname(), "makekey") == 0)
156 operation = DO_MAKEKEY;
157
158 while ((opt = getopt(argc, argv, "kmpS:s:b:")) != -1) {
159 switch (opt) {
160 case 'k': /* Stdin/Stdout Unix crypt */
161 if (operation != -1 || prompt)
162 usage();
163 operation = DO_MAKEKEY;
164 break;
165
166 case 'm': /* MD5 password hash */
167 if (operation != -1)
168 usage();
169 operation = DO_MD5;
170 extra = NULL;
171 break;
172
173 case 'p':
174 if (operation == DO_MAKEKEY)
175 usage();
176 prompt = 1;
177 break;
178
179 case 'S': /* SHA1 password hash */
180 if (operation != -1)
181 usage();
182 operation = DO_SHA1;
183 extra = optarg;
184 break;
185
186 case 's': /* Unix crypt (DES) */
187 if (operation != -1 || optarg[0] == '$')
188 usage();
189 operation = DO_DES;
190 extra = optarg;
191 break;
192
193 case 'b': /* Blowfish password hash */
194 if (operation != -1)
195 usage();
196 operation = DO_BLF;
197 extra = optarg;
198 break;
199
200 default:
201 usage();
202 }
203 }
204
205 if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
206 char line[BUFSIZ], *string;
207
208 if (prompt) {
209 string = getpass("Enter string: ");
210 print_passwd(string, operation, extra);
211 (void)fputc('\n', stdout);
212 } else {
213 /* Encrypt stdin to stdout. */
214 while (!feof(stdin) &&
215 (fgets(line, sizeof(line), stdin) != NULL)) {
216 /* Kill the whitesapce. */
217 string = trim(line);
218 if (*string == '\0')
219 continue;
220
221 print_passwd(string, operation, extra);
222
223 if (operation == DO_MAKEKEY) {
224 fflush(stdout);
225 break;
226 }
227 (void)fputc('\n', stdout);
228 }
229 }
230 } else {
231 char *string;
232
233 /* can't combine -p with a supplied string */
234 if (prompt)
235 usage();
236
237 /* Perhaps it isn't worth worrying about, but... */
238 if ((string = strdup(argv[optind])) == NULL)
239 err(1, NULL);
240 /* Wipe the argument. */
241 memset(argv[optind], 0, strlen(argv[optind]));
242
243 print_passwd(string, operation, extra);
244
245 (void)fputc('\n', stdout);
246
247 /* Wipe our copy, before we free it. */
248 memset(string, 0, strlen(string));
249 free(string);
250 }
251 exit(0);
252 }
253