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