1*93ce9b11SThomas Cort /* $NetBSD: pwhash.c,v 1.15 2011/09/16 15:39:28 joerg Exp $ */
2*93ce9b11SThomas Cort /* $OpenBSD: encrypt.c,v 1.16 2002/02/16 21:27:45 millert Exp $ */
3*93ce9b11SThomas Cort
4*93ce9b11SThomas Cort /*
5*93ce9b11SThomas Cort * Copyright (c) 1996, Jason Downs. All rights reserved.
6*93ce9b11SThomas Cort *
7*93ce9b11SThomas Cort * Redistribution and use in source and binary forms, with or without
8*93ce9b11SThomas Cort * modification, are permitted provided that the following conditions
9*93ce9b11SThomas Cort * are met:
10*93ce9b11SThomas Cort * 1. Redistributions of source code must retain the above copyright
11*93ce9b11SThomas Cort * notice, this list of conditions and the following disclaimer.
12*93ce9b11SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*93ce9b11SThomas Cort * notice, this list of conditions and the following disclaimer in the
14*93ce9b11SThomas Cort * documentation and/or other materials provided with the distribution.
15*93ce9b11SThomas Cort *
16*93ce9b11SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
17*93ce9b11SThomas Cort * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*93ce9b11SThomas Cort * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19*93ce9b11SThomas Cort * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
20*93ce9b11SThomas Cort * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21*93ce9b11SThomas Cort * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22*93ce9b11SThomas Cort * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23*93ce9b11SThomas Cort * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*93ce9b11SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*93ce9b11SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*93ce9b11SThomas Cort * SUCH DAMAGE.
27*93ce9b11SThomas Cort */
28*93ce9b11SThomas Cort #include <sys/cdefs.h>
29*93ce9b11SThomas Cort
30*93ce9b11SThomas Cort #ifndef lint
31*93ce9b11SThomas Cort __RCSID("$NetBSD: pwhash.c,v 1.15 2011/09/16 15:39:28 joerg Exp $");
32*93ce9b11SThomas Cort #endif
33*93ce9b11SThomas Cort
34*93ce9b11SThomas Cort #include <sys/types.h>
35*93ce9b11SThomas Cort #include <ctype.h>
36*93ce9b11SThomas Cort #include <err.h>
37*93ce9b11SThomas Cort #include <errno.h>
38*93ce9b11SThomas Cort #include <pwd.h>
39*93ce9b11SThomas Cort #include <stdio.h>
40*93ce9b11SThomas Cort #include <stdlib.h>
41*93ce9b11SThomas Cort #include <string.h>
42*93ce9b11SThomas Cort #include <unistd.h>
43*93ce9b11SThomas Cort #include <limits.h>
44*93ce9b11SThomas Cort #include <login_cap.h>
45*93ce9b11SThomas Cort #include <util.h>
46*93ce9b11SThomas Cort
47*93ce9b11SThomas Cort /*
48*93ce9b11SThomas Cort * Very simple little program, for encrypting passwords from the command
49*93ce9b11SThomas Cort * line. Useful for scripts and such.
50*93ce9b11SThomas Cort */
51*93ce9b11SThomas Cort
52*93ce9b11SThomas Cort #define DO_MAKEKEY 0
53*93ce9b11SThomas Cort #define DO_DES 1
54*93ce9b11SThomas Cort #define DO_MD5 2
55*93ce9b11SThomas Cort #define DO_BLF 3
56*93ce9b11SThomas Cort #define DO_SHA1 4
57*93ce9b11SThomas Cort
58*93ce9b11SThomas Cort __dead static void
usage(void)59*93ce9b11SThomas Cort usage(void)
60*93ce9b11SThomas Cort {
61*93ce9b11SThomas Cort
62*93ce9b11SThomas Cort (void)fprintf(stderr,
63*93ce9b11SThomas Cort "Usage: %s [-km] [-b rounds] [-S rounds] [-s salt] [-p | string]\n",
64*93ce9b11SThomas Cort getprogname());
65*93ce9b11SThomas Cort exit(1);
66*93ce9b11SThomas Cort }
67*93ce9b11SThomas Cort
68*93ce9b11SThomas Cort static char *
trim(char * line)69*93ce9b11SThomas Cort trim(char *line)
70*93ce9b11SThomas Cort {
71*93ce9b11SThomas Cort char *ptr;
72*93ce9b11SThomas Cort
73*93ce9b11SThomas Cort for (ptr = &line[strlen(line) - 1]; ptr > line; ptr--) {
74*93ce9b11SThomas Cort if (!isspace((unsigned char)*ptr))
75*93ce9b11SThomas Cort break;
76*93ce9b11SThomas Cort }
77*93ce9b11SThomas Cort ptr[1] = '\0';
78*93ce9b11SThomas Cort
79*93ce9b11SThomas Cort for (ptr = line; *ptr && isspace((unsigned char)*ptr); ptr++)
80*93ce9b11SThomas Cort continue;
81*93ce9b11SThomas Cort
82*93ce9b11SThomas Cort return ptr;
83*93ce9b11SThomas Cort }
84*93ce9b11SThomas Cort
85*93ce9b11SThomas Cort static void
print_passwd(char * string,int operation,const char * extra)86*93ce9b11SThomas Cort print_passwd(char *string, int operation, const char *extra)
87*93ce9b11SThomas Cort {
88*93ce9b11SThomas Cort char buf[_PASSWORD_LEN];
89*93ce9b11SThomas Cort char option[LINE_MAX], *key, *opt;
90*93ce9b11SThomas Cort int error = 0;
91*93ce9b11SThomas Cort const char *salt = buf;
92*93ce9b11SThomas Cort
93*93ce9b11SThomas Cort switch(operation) {
94*93ce9b11SThomas Cort case DO_MAKEKEY:
95*93ce9b11SThomas Cort /*
96*93ce9b11SThomas Cort * makekey mode: parse string into separate DES key and salt.
97*93ce9b11SThomas Cort */
98*93ce9b11SThomas Cort if (strlen(string) != 10) {
99*93ce9b11SThomas Cort /* To be compatible... */
100*93ce9b11SThomas Cort error = EFTYPE;
101*93ce9b11SThomas Cort break;
102*93ce9b11SThomas Cort }
103*93ce9b11SThomas Cort salt = &string[8];
104*93ce9b11SThomas Cort break;
105*93ce9b11SThomas Cort
106*93ce9b11SThomas Cort case DO_MD5:
107*93ce9b11SThomas Cort error = pw_gensalt(buf, _PASSWORD_LEN, "md5", extra);
108*93ce9b11SThomas Cort break;
109*93ce9b11SThomas Cort
110*93ce9b11SThomas Cort case DO_SHA1:
111*93ce9b11SThomas Cort error = pw_gensalt(buf, _PASSWORD_LEN, "sha1", extra);
112*93ce9b11SThomas Cort break;
113*93ce9b11SThomas Cort
114*93ce9b11SThomas Cort case DO_BLF:
115*93ce9b11SThomas Cort error = pw_gensalt(buf, _PASSWORD_LEN, "blowfish", extra);
116*93ce9b11SThomas Cort break;
117*93ce9b11SThomas Cort
118*93ce9b11SThomas Cort case DO_DES:
119*93ce9b11SThomas Cort salt = extra;
120*93ce9b11SThomas Cort break;
121*93ce9b11SThomas Cort
122*93ce9b11SThomas Cort default:
123*93ce9b11SThomas Cort pw_getconf(option, sizeof(option), "default", "localcipher");
124*93ce9b11SThomas Cort opt = option;
125*93ce9b11SThomas Cort key = strsep(&opt, ",");
126*93ce9b11SThomas Cort error = pw_gensalt(buf, _PASSWORD_LEN, key, opt);
127*93ce9b11SThomas Cort break;
128*93ce9b11SThomas Cort }
129*93ce9b11SThomas Cort
130*93ce9b11SThomas Cort if (error)
131*93ce9b11SThomas Cort err(1, "Cannot generate salt");
132*93ce9b11SThomas Cort
133*93ce9b11SThomas Cort (void)fputs(crypt(string, salt), stdout);
134*93ce9b11SThomas Cort }
135*93ce9b11SThomas Cort
136*93ce9b11SThomas Cort int
main(int argc,char ** argv)137*93ce9b11SThomas Cort main(int argc, char **argv)
138*93ce9b11SThomas Cort {
139*93ce9b11SThomas Cort int opt;
140*93ce9b11SThomas Cort int operation = -1;
141*93ce9b11SThomas Cort int prompt = 0;
142*93ce9b11SThomas Cort const char *extra = NULL; /* Store salt or number of rounds */
143*93ce9b11SThomas Cort
144*93ce9b11SThomas Cort setprogname(argv[0]);
145*93ce9b11SThomas Cort
146*93ce9b11SThomas Cort if (strcmp(getprogname(), "makekey") == 0)
147*93ce9b11SThomas Cort operation = DO_MAKEKEY;
148*93ce9b11SThomas Cort
149*93ce9b11SThomas Cort while ((opt = getopt(argc, argv, "kmpS:s:b:")) != -1) {
150*93ce9b11SThomas Cort switch (opt) {
151*93ce9b11SThomas Cort case 'k': /* Stdin/Stdout Unix crypt */
152*93ce9b11SThomas Cort if (operation != -1 || prompt)
153*93ce9b11SThomas Cort usage();
154*93ce9b11SThomas Cort operation = DO_MAKEKEY;
155*93ce9b11SThomas Cort break;
156*93ce9b11SThomas Cort
157*93ce9b11SThomas Cort case 'm': /* MD5 password hash */
158*93ce9b11SThomas Cort if (operation != -1)
159*93ce9b11SThomas Cort usage();
160*93ce9b11SThomas Cort operation = DO_MD5;
161*93ce9b11SThomas Cort extra = NULL;
162*93ce9b11SThomas Cort break;
163*93ce9b11SThomas Cort
164*93ce9b11SThomas Cort case 'p':
165*93ce9b11SThomas Cort if (operation == DO_MAKEKEY)
166*93ce9b11SThomas Cort usage();
167*93ce9b11SThomas Cort prompt = 1;
168*93ce9b11SThomas Cort break;
169*93ce9b11SThomas Cort
170*93ce9b11SThomas Cort case 'S': /* SHA1 password hash */
171*93ce9b11SThomas Cort if (operation != -1)
172*93ce9b11SThomas Cort usage();
173*93ce9b11SThomas Cort operation = DO_SHA1;
174*93ce9b11SThomas Cort extra = optarg;
175*93ce9b11SThomas Cort break;
176*93ce9b11SThomas Cort
177*93ce9b11SThomas Cort case 's': /* Unix crypt (DES) */
178*93ce9b11SThomas Cort if (operation != -1 || optarg[0] == '$')
179*93ce9b11SThomas Cort usage();
180*93ce9b11SThomas Cort operation = DO_DES;
181*93ce9b11SThomas Cort extra = optarg;
182*93ce9b11SThomas Cort break;
183*93ce9b11SThomas Cort
184*93ce9b11SThomas Cort case 'b': /* Blowfish password hash */
185*93ce9b11SThomas Cort if (operation != -1)
186*93ce9b11SThomas Cort usage();
187*93ce9b11SThomas Cort operation = DO_BLF;
188*93ce9b11SThomas Cort extra = optarg;
189*93ce9b11SThomas Cort break;
190*93ce9b11SThomas Cort
191*93ce9b11SThomas Cort default:
192*93ce9b11SThomas Cort usage();
193*93ce9b11SThomas Cort }
194*93ce9b11SThomas Cort }
195*93ce9b11SThomas Cort
196*93ce9b11SThomas Cort if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
197*93ce9b11SThomas Cort char line[LINE_MAX], *string;
198*93ce9b11SThomas Cort
199*93ce9b11SThomas Cort if (prompt) {
200*93ce9b11SThomas Cort string = getpass("Enter string: ");
201*93ce9b11SThomas Cort print_passwd(string, operation, extra);
202*93ce9b11SThomas Cort (void)fputc('\n', stdout);
203*93ce9b11SThomas Cort } else {
204*93ce9b11SThomas Cort /* Encrypt stdin to stdout. */
205*93ce9b11SThomas Cort while (!feof(stdin) &&
206*93ce9b11SThomas Cort (fgets(line, sizeof(line), stdin) != NULL)) {
207*93ce9b11SThomas Cort /* Kill the whitesapce. */
208*93ce9b11SThomas Cort string = trim(line);
209*93ce9b11SThomas Cort if (*string == '\0')
210*93ce9b11SThomas Cort continue;
211*93ce9b11SThomas Cort
212*93ce9b11SThomas Cort print_passwd(string, operation, extra);
213*93ce9b11SThomas Cort
214*93ce9b11SThomas Cort if (operation == DO_MAKEKEY) {
215*93ce9b11SThomas Cort (void)fflush(stdout);
216*93ce9b11SThomas Cort break;
217*93ce9b11SThomas Cort }
218*93ce9b11SThomas Cort (void)fputc('\n', stdout);
219*93ce9b11SThomas Cort }
220*93ce9b11SThomas Cort }
221*93ce9b11SThomas Cort } else {
222*93ce9b11SThomas Cort char *string;
223*93ce9b11SThomas Cort
224*93ce9b11SThomas Cort /* can't combine -p with a supplied string */
225*93ce9b11SThomas Cort if (prompt)
226*93ce9b11SThomas Cort usage();
227*93ce9b11SThomas Cort
228*93ce9b11SThomas Cort /* Perhaps it isn't worth worrying about, but... */
229*93ce9b11SThomas Cort if ((string = strdup(argv[optind])) == NULL)
230*93ce9b11SThomas Cort err(1, NULL);
231*93ce9b11SThomas Cort /* Wipe the argument. */
232*93ce9b11SThomas Cort (void)memset(argv[optind], 0, strlen(argv[optind]));
233*93ce9b11SThomas Cort
234*93ce9b11SThomas Cort print_passwd(string, operation, extra);
235*93ce9b11SThomas Cort
236*93ce9b11SThomas Cort (void)fputc('\n', stdout);
237*93ce9b11SThomas Cort
238*93ce9b11SThomas Cort /* Wipe our copy, before we free it. */
239*93ce9b11SThomas Cort (void)memset(string, 0, strlen(string));
240*93ce9b11SThomas Cort free(string);
241*93ce9b11SThomas Cort }
242*93ce9b11SThomas Cort return 0;
243*93ce9b11SThomas Cort }
244