Annotation of src/usr.bin/encrypt/encrypt.c, Revision 1.22
1.22 ! deraadt 1: /* $OpenBSD: encrypt.c,v 1.21 2004/07/13 21:09:48 millert Exp $ */
1.1 downsj 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>
1.15 millert 29: #include <ctype.h>
1.1 downsj 30: #include <err.h>
1.3 downsj 31: #include <errno.h>
1.15 millert 32: #include <pwd.h>
33: #include <stdio.h>
1.7 kstailey 34: #include <stdlib.h>
1.1 downsj 35: #include <string.h>
36: #include <unistd.h>
1.14 millert 37: #include <login_cap.h>
1.1 downsj 38:
39: /*
40: * Very simple little program, for encrypting passwords from the command
41: * line. Useful for scripts and such.
42: */
43:
1.5 provos 44: #define DO_MAKEKEY 0
45: #define DO_DES 1
46: #define DO_MD5 2
47: #define DO_BLF 3
48:
1.15 millert 49: extern char *__progname;
1.5 provos 50: char buffer[_PASSWORD_LEN];
1.19 deraadt 51:
52: void usage(void);
53: char *trim(char *);
54: void print_passwd(char *, int, void *);
1.3 downsj 55:
1.15 millert 56: void
57: usage(void)
1.1 downsj 58: {
1.15 millert 59:
60: (void)fprintf(stderr,
1.18 millert 61: "usage: %s [-k] [-b rounds] [-c class] [-m] [-s salt] [-p | string]\n",
1.15 millert 62: __progname);
63: exit(1);
1.1 downsj 64: }
65:
1.15 millert 66: char *
67: trim(char *line)
1.2 downsj 68: {
1.15 millert 69: char *ptr;
1.2 downsj 70:
1.15 millert 71: for (ptr = &line[strlen(line)-1]; ptr > line; ptr--) {
72: if (!isspace(*ptr))
73: break;
74: }
75: ptr[1] = '\0';
1.2 downsj 76:
1.15 millert 77: for (ptr = line; *ptr && isspace(*ptr); ptr++)
78: ;
1.2 downsj 79:
1.15 millert 80: return(ptr);
1.2 downsj 81: }
82:
1.15 millert 83: void
84: print_passwd(char *string, int operation, void *extra)
1.5 provos 85: {
1.15 millert 86: char msalt[3], *salt;
87: login_cap_t *lc;
1.21 millert 88: int pwd_gensalt(char *, int, login_cap_t *, char);
1.22 ! deraadt 89: void to64(char *, u_int32_t, int n);
1.15 millert 90:
91: switch(operation) {
92: case DO_MAKEKEY:
93: /*
94: * makekey mode: parse string into separate DES key and salt.
95: */
96: if (strlen(string) != 10) {
97: /* To be compatible... */
98: errx(1, "%s", strerror(EFTYPE));
99: }
1.17 deraadt 100: strlcpy(msalt, &string[8], sizeof msalt);
1.15 millert 101: salt = msalt;
102: break;
103:
104: case DO_MD5:
1.17 deraadt 105: strlcpy(buffer, "$1$", sizeof buffer);
1.15 millert 106: to64(&buffer[3], arc4random(), 4);
107: to64(&buffer[7], arc4random(), 4);
1.17 deraadt 108: strlcpy(buffer + 11, "$", sizeof buffer - 11);
1.15 millert 109: salt = buffer;
110: break;
111:
112: case DO_BLF:
113: strlcpy(buffer, bcrypt_gensalt(*(int *)extra), _PASSWORD_LEN);
114: salt = buffer;
115: break;
116:
117: case DO_DES:
118: salt = extra;
119: break;
120:
121: default:
1.18 millert 122: if ((lc = login_getclass(extra)) == NULL)
123: errx(1, "unable to get login class `%s'",
124: extra ? (char *)extra : "default");
1.21 millert 125: if (!pwd_gensalt(buffer, _PASSWORD_LEN, lc, 'l'))
1.15 millert 126: errx(1, "can't generate salt");
127: salt = buffer;
128: break;
129: }
130:
131: (void)fputs(crypt(string, salt), stdout);
1.5 provos 132: }
133:
1.15 millert 134: int
135: main(int argc, char **argv)
1.1 downsj 136: {
1.15 millert 137: int opt;
138: int operation = -1;
139: int prompt = 0;
140: int rounds;
1.18 millert 141: void *extra = NULL; /* Store salt or number of rounds */
1.15 millert 142:
143: if (strcmp(__progname, "makekey") == 0)
144: operation = DO_MAKEKEY;
145:
1.18 millert 146: while ((opt = getopt(argc, argv, "kmps:b:c:")) != -1) {
1.15 millert 147: switch (opt) {
148: case 'k': /* Stdin/Stdout Unix crypt */
149: if (operation != -1 || prompt)
150: usage();
151: operation = DO_MAKEKEY;
152: break;
153:
154: case 'm': /* MD5 password hash */
155: if (operation != -1)
156: usage();
157: operation = DO_MD5;
158: break;
159:
160: case 'p':
161: if (operation == DO_MAKEKEY)
162: usage();
163: prompt = 1;
164: break;
165:
166: case 's': /* Unix crypt (DES) */
167: if (operation != -1 || optarg[0] == '$')
168: usage();
169: operation = DO_DES;
170: extra = optarg;
171: break;
172:
173: case 'b': /* Blowfish password hash */
174: if (operation != -1)
175: usage();
176: operation = DO_BLF;
177: rounds = atoi(optarg);
178: extra = &rounds;
1.18 millert 179: break;
180:
181: case 'c': /* user login class */
182: extra = optarg;
183: operation = -1;
1.15 millert 184: break;
185:
186: default:
187: usage();
188: }
1.1 downsj 189: }
190:
1.15 millert 191: if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
192: char line[BUFSIZ], *string;
1.1 downsj 193:
1.15 millert 194: if (prompt) {
1.20 otto 195: if ((string = getpass("Enter string: ")) == NULL)
196: err(1, "getpass");
1.15 millert 197: print_passwd(string, operation, extra);
198: (void)fputc('\n', stdout);
199: } else {
200: /* Encrypt stdin to stdout. */
201: while (!feof(stdin) &&
202: (fgets(line, sizeof(line), stdin) != NULL)) {
203: /* Kill the whitesapce. */
204: string = trim(line);
205: if (*string == '\0')
206: continue;
207:
208: print_passwd(string, operation, extra);
209:
210: if (operation == DO_MAKEKEY) {
211: fflush(stdout);
212: break;
213: }
214: (void)fputc('\n', stdout);
215: }
216: }
1.9 alex 217: } else {
1.15 millert 218: char *string;
219:
220: /* can't combine -p with a supplied string */
221: if (prompt)
222: usage();
223:
224: /* Perhaps it isn't worth worrying about, but... */
225: if ((string = strdup(argv[optind])) == NULL)
226: err(1, NULL);
227: /* Wipe the argument. */
228: memset(argv[optind], 0, strlen(argv[optind]));
229:
1.9 alex 230: print_passwd(string, operation, extra);
231:
1.15 millert 232: (void)fputc('\n', stdout);
233:
234: /* Wipe our copy, before we free it. */
235: memset(string, 0, strlen(string));
236: free(string);
1.1 downsj 237: }
1.15 millert 238: exit(0);
1.1 downsj 239: }