Annotation of src/usr.bin/encrypt/encrypt.c, Revision 1.24
1.24 ! ray 1: /* $OpenBSD: encrypt.c,v 1.23 2006/05/27 23:42:08 moritz 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.24 ! ray 70:
! 71: if (line[0] == '\0')
! 72: return (line);
1.2 downsj 73:
1.15 millert 74: for (ptr = &line[strlen(line)-1]; ptr > line; ptr--) {
75: if (!isspace(*ptr))
76: break;
77: }
78: ptr[1] = '\0';
1.2 downsj 79:
1.15 millert 80: for (ptr = line; *ptr && isspace(*ptr); ptr++)
81: ;
1.2 downsj 82:
1.15 millert 83: return(ptr);
1.2 downsj 84: }
85:
1.15 millert 86: void
87: print_passwd(char *string, int operation, void *extra)
1.5 provos 88: {
1.23 moritz 89: char msalt[3], *salt, *cryptstr;
1.15 millert 90: login_cap_t *lc;
1.21 millert 91: int pwd_gensalt(char *, int, login_cap_t *, char);
1.22 deraadt 92: void to64(char *, u_int32_t, int n);
1.15 millert 93:
94: switch(operation) {
95: case DO_MAKEKEY:
96: /*
97: * makekey mode: parse string into separate DES key and salt.
98: */
99: if (strlen(string) != 10) {
100: /* To be compatible... */
101: errx(1, "%s", strerror(EFTYPE));
102: }
1.17 deraadt 103: strlcpy(msalt, &string[8], sizeof msalt);
1.15 millert 104: salt = msalt;
105: break;
106:
107: case DO_MD5:
1.17 deraadt 108: strlcpy(buffer, "$1$", sizeof buffer);
1.15 millert 109: to64(&buffer[3], arc4random(), 4);
110: to64(&buffer[7], arc4random(), 4);
1.17 deraadt 111: strlcpy(buffer + 11, "$", sizeof buffer - 11);
1.15 millert 112: salt = buffer;
113: break;
114:
115: case DO_BLF:
116: strlcpy(buffer, bcrypt_gensalt(*(int *)extra), _PASSWORD_LEN);
117: salt = buffer;
118: break;
119:
120: case DO_DES:
121: salt = extra;
122: break;
123:
124: default:
1.18 millert 125: if ((lc = login_getclass(extra)) == NULL)
126: errx(1, "unable to get login class `%s'",
127: extra ? (char *)extra : "default");
1.21 millert 128: if (!pwd_gensalt(buffer, _PASSWORD_LEN, lc, 'l'))
1.15 millert 129: errx(1, "can't generate salt");
130: salt = buffer;
131: break;
132: }
133:
1.23 moritz 134: if ((cryptstr = crypt(string, salt)) == NULL)
135: errx(1, "crypt failed");
136: fputs(cryptstr, stdout);
1.5 provos 137: }
138:
1.15 millert 139: int
140: main(int argc, char **argv)
1.1 downsj 141: {
1.15 millert 142: int opt;
143: int operation = -1;
144: int prompt = 0;
145: int rounds;
1.18 millert 146: void *extra = NULL; /* Store salt or number of rounds */
1.15 millert 147:
148: if (strcmp(__progname, "makekey") == 0)
149: operation = DO_MAKEKEY;
150:
1.18 millert 151: while ((opt = getopt(argc, argv, "kmps:b:c:")) != -1) {
1.15 millert 152: switch (opt) {
153: case 'k': /* Stdin/Stdout Unix crypt */
154: if (operation != -1 || prompt)
155: usage();
156: operation = DO_MAKEKEY;
157: break;
158:
159: case 'm': /* MD5 password hash */
160: if (operation != -1)
161: usage();
162: operation = DO_MD5;
163: break;
164:
165: case 'p':
166: if (operation == DO_MAKEKEY)
167: usage();
168: prompt = 1;
169: break;
170:
171: case 's': /* Unix crypt (DES) */
172: if (operation != -1 || optarg[0] == '$')
173: usage();
174: operation = DO_DES;
175: extra = optarg;
176: break;
177:
178: case 'b': /* Blowfish password hash */
179: if (operation != -1)
180: usage();
181: operation = DO_BLF;
182: rounds = atoi(optarg);
183: extra = &rounds;
1.18 millert 184: break;
185:
186: case 'c': /* user login class */
187: extra = optarg;
188: operation = -1;
1.15 millert 189: break;
190:
191: default:
192: usage();
193: }
1.1 downsj 194: }
195:
1.15 millert 196: if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
197: char line[BUFSIZ], *string;
1.1 downsj 198:
1.15 millert 199: if (prompt) {
1.20 otto 200: if ((string = getpass("Enter string: ")) == NULL)
201: err(1, "getpass");
1.15 millert 202: print_passwd(string, operation, extra);
203: (void)fputc('\n', stdout);
204: } else {
205: /* Encrypt stdin to stdout. */
206: while (!feof(stdin) &&
207: (fgets(line, sizeof(line), stdin) != NULL)) {
208: /* Kill the whitesapce. */
209: string = trim(line);
210: if (*string == '\0')
211: continue;
212:
213: print_passwd(string, operation, extra);
214:
215: if (operation == DO_MAKEKEY) {
216: fflush(stdout);
217: break;
218: }
219: (void)fputc('\n', stdout);
220: }
221: }
1.9 alex 222: } else {
1.15 millert 223: char *string;
224:
225: /* can't combine -p with a supplied string */
226: if (prompt)
227: usage();
228:
229: /* Perhaps it isn't worth worrying about, but... */
230: if ((string = strdup(argv[optind])) == NULL)
231: err(1, NULL);
232: /* Wipe the argument. */
233: memset(argv[optind], 0, strlen(argv[optind]));
234:
1.9 alex 235: print_passwd(string, operation, extra);
236:
1.15 millert 237: (void)fputc('\n', stdout);
238:
239: /* Wipe our copy, before we free it. */
240: memset(string, 0, strlen(string));
241: free(string);
1.1 downsj 242: }
1.15 millert 243: exit(0);
1.1 downsj 244: }