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