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