Annotation of src/usr.bin/encrypt/encrypt.c, Revision 1.43
1.43 ! tedu 1: /* $OpenBSD: encrypt.c,v 1.42 2015/10/10 18:14:20 doug 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.27 jdixon 38: #include <limits.h>
1.43 ! tedu 39: #include <readpassphrase.h>
1.1 downsj 40:
41: /*
42: * Very simple little program, for encrypting passwords from the command
43: * line. Useful for scripts and such.
44: */
45:
1.15 millert 46: extern char *__progname;
1.19 deraadt 47:
48: void usage(void);
1.3 downsj 49:
1.34 tedu 50: #define DO_BLF 0
51:
1.15 millert 52: void
53: usage(void)
1.1 downsj 54: {
1.15 millert 55:
56: (void)fprintf(stderr,
1.34 tedu 57: "usage: %s [-b rounds] [-c class] [-p | string]\n",
1.15 millert 58: __progname);
59: exit(1);
1.1 downsj 60: }
61:
1.37 tedu 62: static void
63: print_passwd(char *string, int operation, char *extra)
1.5 provos 64: {
1.34 tedu 65: char buffer[_PASSWORD_LEN];
1.37 tedu 66: const char *pref;
1.39 tedu 67: char prefbuf[64];
1.15 millert 68:
1.33 tedu 69: if (operation == DO_BLF) {
1.39 tedu 70: if (snprintf(prefbuf, sizeof(prefbuf), "blowfish,%s", extra) >=
71: sizeof(prefbuf))
72: errx(1, "pref too long");
1.37 tedu 73: pref = prefbuf;
1.34 tedu 74: } else {
75: login_cap_t *lc;
1.15 millert 76:
1.18 millert 77: if ((lc = login_getclass(extra)) == NULL)
78: errx(1, "unable to get login class `%s'",
79: extra ? (char *)extra : "default");
1.34 tedu 80: pref = login_getcapstr(lc, "localcipher", NULL, NULL);
1.15 millert 81: }
1.37 tedu 82: if (crypt_newhash(string, pref, buffer, sizeof(buffer)) != 0)
1.40 tedu 83: err(1, "can't generate hash");
1.15 millert 84:
1.34 tedu 85: fputs(buffer, stdout);
1.5 provos 86: }
87:
1.15 millert 88: int
89: main(int argc, char **argv)
1.1 downsj 90: {
1.15 millert 91: int opt;
92: int operation = -1;
93: int prompt = 0;
1.40 tedu 94: char *extra = NULL; /* Store login class or number of rounds */
1.27 jdixon 95: const char *errstr;
1.41 deraadt 96:
1.42 doug 97: if (pledge("stdio rpath wpath tty", NULL) == -1)
1.41 deraadt 98: err(1, "pledge");
1.15 millert 99:
1.34 tedu 100: while ((opt = getopt(argc, argv, "pb:c:")) != -1) {
1.15 millert 101: switch (opt) {
102: case 'p':
103: prompt = 1;
104: break;
105: case 'b': /* Blowfish password hash */
106: if (operation != -1)
107: usage();
108: operation = DO_BLF;
1.37 tedu 109: if (strcmp(optarg, "a") != 0) {
110: (void)strtonum(optarg, 4, 31, &errstr);
1.36 deraadt 111: if (errstr != NULL)
1.40 tedu 112: errx(1, "rounds is %s: %s", errstr,
113: optarg);
1.36 deraadt 114: }
1.37 tedu 115: extra = optarg;
1.18 millert 116: break;
117: case 'c': /* user login class */
118: extra = optarg;
119: operation = -1;
1.15 millert 120: break;
121: default:
122: usage();
123: }
1.1 downsj 124: }
125:
1.34 tedu 126: if (((argc - optind) < 1)) {
1.43 ! tedu 127: char line[BUFSIZ];
! 128: char string[_PASSWORD_LEN + 1];
1.1 downsj 129:
1.15 millert 130: if (prompt) {
1.43 ! tedu 131: if (readpassphrase("Enter string: ", string,
! 132: sizeof(string), RPP_ECHO_OFF) == NULL)
! 133: err(1, "readpassphrase");
1.15 millert 134: print_passwd(string, operation, extra);
135: (void)fputc('\n', stdout);
136: } else {
1.28 krw 137: size_t len;
1.15 millert 138: /* Encrypt stdin to stdout. */
139: while (!feof(stdin) &&
140: (fgets(line, sizeof(line), stdin) != NULL)) {
1.28 krw 141: len = strlen(line);
142: if (len == 0 || line[0] == '\n')
1.15 millert 143: continue;
1.28 krw 144: if (line[len - 1] == '\n')
145: line[len - 1] = '\0';
146:
147: print_passwd(line, operation, extra);
1.15 millert 148:
149: (void)fputc('\n', stdout);
150: }
151: }
1.9 alex 152: } else {
1.15 millert 153: char *string;
154:
155: /* can't combine -p with a supplied string */
156: if (prompt)
157: usage();
158:
159: /* Perhaps it isn't worth worrying about, but... */
160: if ((string = strdup(argv[optind])) == NULL)
161: err(1, NULL);
162: /* Wipe the argument. */
1.40 tedu 163: explicit_bzero(argv[optind], strlen(argv[optind]));
1.15 millert 164:
1.9 alex 165: print_passwd(string, operation, extra);
166:
1.15 millert 167: (void)fputc('\n', stdout);
168:
169: /* Wipe our copy, before we free it. */
1.40 tedu 170: explicit_bzero(string, strlen(string));
1.15 millert 171: free(string);
1.1 downsj 172: }
1.15 millert 173: exit(0);
1.1 downsj 174: }