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