1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2001-2008 Marco d'Itri
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
5*4882a593Smuzhiyun * it under the terms of the GNU General Public License as published by
6*4882a593Smuzhiyun * the Free Software Foundation; either version 2 of the License, or
7*4882a593Smuzhiyun * (at your option) any later version.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful,
10*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*4882a593Smuzhiyun * GNU General Public License for more details.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
15*4882a593Smuzhiyun * along with this program; if not, write to the Free Software
16*4882a593Smuzhiyun * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*4882a593Smuzhiyun */
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /* for crypt, snprintf and strcasecmp */
20*4882a593Smuzhiyun #define _XOPEN_SOURCE
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * _BSD_SOURCE is deprecated as of GLIBC 2.20; _DEFAULT_SOURCE should be used
23*4882a593Smuzhiyun * instead. (https://lwn.net/Articles/611162/)
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun #define _DEFAULT_SOURCE
26*4882a593Smuzhiyun #define _BSD_SOURCE
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* System library */
29*4882a593Smuzhiyun #include <stdio.h>
30*4882a593Smuzhiyun #include <stdlib.h>
31*4882a593Smuzhiyun #include <unistd.h>
32*4882a593Smuzhiyun #include "config.h"
33*4882a593Smuzhiyun #ifdef HAVE_GETOPT_LONG
34*4882a593Smuzhiyun #include <getopt.h>
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun #include <fcntl.h>
37*4882a593Smuzhiyun #include <string.h>
38*4882a593Smuzhiyun #include <time.h>
39*4882a593Smuzhiyun #include <sys/types.h>
40*4882a593Smuzhiyun #ifdef HAVE_XCRYPT
41*4882a593Smuzhiyun #include <xcrypt.h>
42*4882a593Smuzhiyun #include <sys/stat.h>
43*4882a593Smuzhiyun #endif
44*4882a593Smuzhiyun #ifdef HAVE_LINUX_CRYPT_GENSALT
45*4882a593Smuzhiyun #define _OW_SOURCE
46*4882a593Smuzhiyun #include <crypt.h>
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun #ifdef HAVE_GETTIMEOFDAY
49*4882a593Smuzhiyun #include <sys/time.h>
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* glibc without crypt() */
53*4882a593Smuzhiyun #ifndef _XOPEN_CRYPT
54*4882a593Smuzhiyun #include <crypt.h>
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* Application-specific */
58*4882a593Smuzhiyun #include "utils.h"
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* Global variables */
61*4882a593Smuzhiyun #ifdef HAVE_GETOPT_LONG
62*4882a593Smuzhiyun static const struct option longopts[] = {
63*4882a593Smuzhiyun {"method", optional_argument, NULL, 'm'},
64*4882a593Smuzhiyun /* for backward compatibility with versions < 4.7.25 (< 20080321): */
65*4882a593Smuzhiyun {"hash", optional_argument, NULL, 'H'},
66*4882a593Smuzhiyun {"help", no_argument, NULL, 'h'},
67*4882a593Smuzhiyun {"password-fd", required_argument, NULL, 'P'},
68*4882a593Smuzhiyun {"stdin", no_argument, NULL, 's'},
69*4882a593Smuzhiyun {"salt", required_argument, NULL, 'S'},
70*4882a593Smuzhiyun {"rounds", required_argument, NULL, 'R'},
71*4882a593Smuzhiyun {"version", no_argument, NULL, 'V'},
72*4882a593Smuzhiyun {NULL, 0, NULL, 0 }
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun #else
75*4882a593Smuzhiyun extern char *optarg;
76*4882a593Smuzhiyun extern int optind;
77*4882a593Smuzhiyun #endif
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun static const char valid_salts[] = "abcdefghijklmnopqrstuvwxyz"
80*4882a593Smuzhiyun "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun struct crypt_method {
83*4882a593Smuzhiyun const char *method; /* short name used by the command line option */
84*4882a593Smuzhiyun const char *prefix; /* salt prefix */
85*4882a593Smuzhiyun const unsigned int minlen; /* minimum salt length */
86*4882a593Smuzhiyun const unsigned int maxlen; /* maximum salt length */
87*4882a593Smuzhiyun const unsigned int rounds; /* supports a variable number of rounds */
88*4882a593Smuzhiyun const char *desc; /* long description for the methods list */
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun static const struct crypt_method methods[] = {
92*4882a593Smuzhiyun /* method prefix minlen, maxlen rounds description */
93*4882a593Smuzhiyun { "des", "", 2, 2, 0,
94*4882a593Smuzhiyun N_("standard 56 bit DES-based crypt(3)") },
95*4882a593Smuzhiyun { "md5", "$1$", 8, 8, 0, "MD5" },
96*4882a593Smuzhiyun #if defined OpenBSD || defined FreeBSD || (defined __SVR4 && defined __sun)
97*4882a593Smuzhiyun { "bf", "$2a$", 22, 22, 1, "Blowfish" },
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun #if defined HAVE_LINUX_CRYPT_GENSALT
100*4882a593Smuzhiyun { "bf", "$2a$", 22, 22, 1, "Blowfish, system-specific on 8-bit chars" },
101*4882a593Smuzhiyun /* algorithm 2y fixes CVE-2011-2483 */
102*4882a593Smuzhiyun { "bfy", "$2y$", 22, 22, 1, "Blowfish, correct handling of 8-bit chars" },
103*4882a593Smuzhiyun #endif
104*4882a593Smuzhiyun #if defined FreeBSD
105*4882a593Smuzhiyun { "nt", "$3$", 0, 0, 0, "NT-Hash" },
106*4882a593Smuzhiyun #endif
107*4882a593Smuzhiyun #if defined HAVE_SHA_CRYPT
108*4882a593Smuzhiyun /* http://people.redhat.com/drepper/SHA-crypt.txt */
109*4882a593Smuzhiyun { "sha-256", "$5$", 8, 16, 1, "SHA-256" },
110*4882a593Smuzhiyun { "sha-512", "$6$", 8, 16, 1, "SHA-512" },
111*4882a593Smuzhiyun #endif
112*4882a593Smuzhiyun /* http://www.crypticide.com/dropsafe/article/1389 */
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun * Actually the maximum salt length is arbitrary, but Solaris by default
115*4882a593Smuzhiyun * always uses 8 characters:
116*4882a593Smuzhiyun * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/ \
117*4882a593Smuzhiyun * usr/src/lib/crypt_modules/sunmd5/sunmd5.c#crypt_gensalt_impl
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun #if defined __SVR4 && defined __sun
120*4882a593Smuzhiyun { "sunmd5", "$md5$", 8, 8, 1, "SunMD5" },
121*4882a593Smuzhiyun #endif
122*4882a593Smuzhiyun { NULL, NULL, 0, 0, 0, NULL }
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun void generate_salt(char *const buf, const unsigned int len);
126*4882a593Smuzhiyun void *get_random_bytes(const int len);
127*4882a593Smuzhiyun void display_help(int error);
128*4882a593Smuzhiyun void display_version(void);
129*4882a593Smuzhiyun void display_methods(void);
130*4882a593Smuzhiyun
main(int argc,char * argv[])131*4882a593Smuzhiyun int main(int argc, char *argv[])
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun int ch, i;
134*4882a593Smuzhiyun int password_fd = -1;
135*4882a593Smuzhiyun unsigned int salt_minlen = 0;
136*4882a593Smuzhiyun unsigned int salt_maxlen = 0;
137*4882a593Smuzhiyun unsigned int rounds_support = 0;
138*4882a593Smuzhiyun const char *salt_prefix = NULL;
139*4882a593Smuzhiyun const char *salt_arg = NULL;
140*4882a593Smuzhiyun unsigned int rounds = 0;
141*4882a593Smuzhiyun char *salt = NULL;
142*4882a593Smuzhiyun char rounds_str[30];
143*4882a593Smuzhiyun char *password = NULL;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun #ifdef ENABLE_NLS
146*4882a593Smuzhiyun setlocale(LC_ALL, "");
147*4882a593Smuzhiyun bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
148*4882a593Smuzhiyun textdomain(NLS_CAT_NAME);
149*4882a593Smuzhiyun #endif
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /* prepend options from environment */
152*4882a593Smuzhiyun argv = merge_args(getenv("MKPASSWD_OPTIONS"), argv, &argc);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun while ((ch = GETOPT_LONGISH(argc, argv, "hH:m:5P:R:sS:V", longopts, 0))
155*4882a593Smuzhiyun > 0) {
156*4882a593Smuzhiyun switch (ch) {
157*4882a593Smuzhiyun case '5':
158*4882a593Smuzhiyun optarg = (char *) "md5";
159*4882a593Smuzhiyun /* fall through */
160*4882a593Smuzhiyun case 'm':
161*4882a593Smuzhiyun case 'H':
162*4882a593Smuzhiyun if (!optarg || strcaseeq("help", optarg)) {
163*4882a593Smuzhiyun display_methods();
164*4882a593Smuzhiyun exit(0);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun for (i = 0; methods[i].method != NULL; i++)
167*4882a593Smuzhiyun if (strcaseeq(methods[i].method, optarg)) {
168*4882a593Smuzhiyun salt_prefix = methods[i].prefix;
169*4882a593Smuzhiyun salt_minlen = methods[i].minlen;
170*4882a593Smuzhiyun salt_maxlen = methods[i].maxlen;
171*4882a593Smuzhiyun rounds_support = methods[i].rounds;
172*4882a593Smuzhiyun break;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun if (!salt_prefix) {
175*4882a593Smuzhiyun fprintf(stderr, _("Invalid method '%s'.\n"), optarg);
176*4882a593Smuzhiyun exit(1);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun break;
179*4882a593Smuzhiyun case 'P':
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun char *p;
182*4882a593Smuzhiyun password_fd = strtol(optarg, &p, 10);
183*4882a593Smuzhiyun if (p == NULL || *p != '\0' || password_fd < 0) {
184*4882a593Smuzhiyun fprintf(stderr, _("Invalid number '%s'.\n"), optarg);
185*4882a593Smuzhiyun exit(1);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun break;
189*4882a593Smuzhiyun case 'R':
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun char *p;
192*4882a593Smuzhiyun rounds = strtol(optarg, &p, 10);
193*4882a593Smuzhiyun if (p == NULL || *p != '\0' || rounds < 0) {
194*4882a593Smuzhiyun fprintf(stderr, _("Invalid number '%s'.\n"), optarg);
195*4882a593Smuzhiyun exit(1);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun break;
199*4882a593Smuzhiyun case 's':
200*4882a593Smuzhiyun password_fd = 0;
201*4882a593Smuzhiyun break;
202*4882a593Smuzhiyun case 'S':
203*4882a593Smuzhiyun salt_arg = optarg;
204*4882a593Smuzhiyun break;
205*4882a593Smuzhiyun case 'V':
206*4882a593Smuzhiyun display_version();
207*4882a593Smuzhiyun exit(0);
208*4882a593Smuzhiyun case 'h':
209*4882a593Smuzhiyun display_help(EXIT_SUCCESS);
210*4882a593Smuzhiyun default:
211*4882a593Smuzhiyun fprintf(stderr, _("Try '%s --help' for more information.\n"),
212*4882a593Smuzhiyun argv[0]);
213*4882a593Smuzhiyun exit(1);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun argc -= optind;
217*4882a593Smuzhiyun argv += optind;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun if (argc == 2 && !salt_arg) {
220*4882a593Smuzhiyun password = argv[0];
221*4882a593Smuzhiyun salt_arg = argv[1];
222*4882a593Smuzhiyun } else if (argc == 1) {
223*4882a593Smuzhiyun password = argv[0];
224*4882a593Smuzhiyun } else if (argc == 0) {
225*4882a593Smuzhiyun } else {
226*4882a593Smuzhiyun display_help(EXIT_FAILURE);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /* default: DES password */
230*4882a593Smuzhiyun if (!salt_prefix) {
231*4882a593Smuzhiyun salt_minlen = methods[0].minlen;
232*4882a593Smuzhiyun salt_maxlen = methods[0].maxlen;
233*4882a593Smuzhiyun salt_prefix = methods[0].prefix;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (streq(salt_prefix, "$2a$") || streq(salt_prefix, "$2y$")) {
237*4882a593Smuzhiyun /* OpenBSD Blowfish and derivatives */
238*4882a593Smuzhiyun if (rounds <= 5)
239*4882a593Smuzhiyun rounds = 5;
240*4882a593Smuzhiyun /* actually for 2a/2y it is the logarithm of the number of rounds */
241*4882a593Smuzhiyun snprintf(rounds_str, sizeof(rounds_str), "%02u$", rounds);
242*4882a593Smuzhiyun } else if (rounds_support && rounds)
243*4882a593Smuzhiyun snprintf(rounds_str, sizeof(rounds_str), "rounds=%u$", rounds);
244*4882a593Smuzhiyun else
245*4882a593Smuzhiyun rounds_str[0] = '\0';
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun if (salt_arg) {
248*4882a593Smuzhiyun unsigned int c = strlen(salt_arg);
249*4882a593Smuzhiyun if (c < salt_minlen || c > salt_maxlen) {
250*4882a593Smuzhiyun if (salt_minlen == salt_maxlen)
251*4882a593Smuzhiyun fprintf(stderr, ngettext(
252*4882a593Smuzhiyun "Wrong salt length: %d byte when %d expected.\n",
253*4882a593Smuzhiyun "Wrong salt length: %d bytes when %d expected.\n", c),
254*4882a593Smuzhiyun c, salt_maxlen);
255*4882a593Smuzhiyun else
256*4882a593Smuzhiyun fprintf(stderr, ngettext(
257*4882a593Smuzhiyun "Wrong salt length: %d byte when %d <= n <= %d"
258*4882a593Smuzhiyun " expected.\n",
259*4882a593Smuzhiyun "Wrong salt length: %d bytes when %d <= n <= %d"
260*4882a593Smuzhiyun " expected.\n", c),
261*4882a593Smuzhiyun c, salt_minlen, salt_maxlen);
262*4882a593Smuzhiyun exit(1);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun while (c-- > 0) {
265*4882a593Smuzhiyun if (strchr(valid_salts, salt_arg[c]) == NULL) {
266*4882a593Smuzhiyun fprintf(stderr, _("Illegal salt character '%c'.\n"),
267*4882a593Smuzhiyun salt_arg[c]);
268*4882a593Smuzhiyun exit(1);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun salt = NOFAIL(malloc(strlen(salt_prefix) + strlen(rounds_str)
273*4882a593Smuzhiyun + strlen(salt_arg) + 1));
274*4882a593Smuzhiyun *salt = '\0';
275*4882a593Smuzhiyun strcat(salt, salt_prefix);
276*4882a593Smuzhiyun strcat(salt, rounds_str);
277*4882a593Smuzhiyun strcat(salt, salt_arg);
278*4882a593Smuzhiyun } else {
279*4882a593Smuzhiyun #ifdef HAVE_SOLARIS_CRYPT_GENSALT
280*4882a593Smuzhiyun #error "This code path is untested on Solaris. Please send a patch."
281*4882a593Smuzhiyun salt = crypt_gensalt(salt_prefix, NULL);
282*4882a593Smuzhiyun if (!salt)
283*4882a593Smuzhiyun perror(stderr, "crypt_gensalt");
284*4882a593Smuzhiyun #elif defined HAVE_LINUX_CRYPT_GENSALT
285*4882a593Smuzhiyun void *entropy = get_random_bytes(64);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun salt = crypt_gensalt(salt_prefix, rounds, entropy, 64);
288*4882a593Smuzhiyun if (!salt) {
289*4882a593Smuzhiyun fprintf(stderr, "crypt_gensalt failed.\n");
290*4882a593Smuzhiyun exit(2);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun free(entropy);
293*4882a593Smuzhiyun #else
294*4882a593Smuzhiyun unsigned int salt_len = salt_maxlen;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun if (salt_minlen != salt_maxlen) { /* salt length can vary */
297*4882a593Smuzhiyun srand(time(NULL) + getpid());
298*4882a593Smuzhiyun salt_len = rand() % (salt_maxlen - salt_minlen + 1) + salt_minlen;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun salt = NOFAIL(malloc(strlen(salt_prefix) + strlen(rounds_str)
302*4882a593Smuzhiyun + salt_len + 1));
303*4882a593Smuzhiyun *salt = '\0';
304*4882a593Smuzhiyun strcat(salt, salt_prefix);
305*4882a593Smuzhiyun strcat(salt, rounds_str);
306*4882a593Smuzhiyun generate_salt(salt + strlen(salt), salt_len);
307*4882a593Smuzhiyun #endif
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (password) {
311*4882a593Smuzhiyun } else if (password_fd != -1) {
312*4882a593Smuzhiyun FILE *fp;
313*4882a593Smuzhiyun char *p;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun if (isatty(password_fd))
316*4882a593Smuzhiyun fprintf(stderr, _("Password: "));
317*4882a593Smuzhiyun password = NOFAIL(malloc(128));
318*4882a593Smuzhiyun fp = fdopen(password_fd, "r");
319*4882a593Smuzhiyun if (!fp) {
320*4882a593Smuzhiyun perror("fdopen");
321*4882a593Smuzhiyun exit(2);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun if (!fgets(password, 128, fp)) {
324*4882a593Smuzhiyun perror("fgets");
325*4882a593Smuzhiyun exit(2);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun p = strpbrk(password, "\n\r");
329*4882a593Smuzhiyun if (p)
330*4882a593Smuzhiyun *p = '\0';
331*4882a593Smuzhiyun } else {
332*4882a593Smuzhiyun password = getpass(_("Password: "));
333*4882a593Smuzhiyun if (!password) {
334*4882a593Smuzhiyun perror("getpass");
335*4882a593Smuzhiyun exit(2);
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun const char *result;
341*4882a593Smuzhiyun result = crypt(password, salt);
342*4882a593Smuzhiyun /* xcrypt returns "*0" on errors */
343*4882a593Smuzhiyun if (!result || result[0] == '*') {
344*4882a593Smuzhiyun fprintf(stderr, "crypt failed.\n");
345*4882a593Smuzhiyun exit(2);
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun /* yes, using strlen(salt_prefix) on salt. It's not
348*4882a593Smuzhiyun * documented whether crypt_gensalt may change the prefix */
349*4882a593Smuzhiyun if (!strneq(result, salt, strlen(salt_prefix))) {
350*4882a593Smuzhiyun fprintf(stderr, _("Method not supported by crypt(3).\n"));
351*4882a593Smuzhiyun exit(2);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun printf("%s\n", result);
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun exit(0);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun #ifdef RANDOM_DEVICE
get_random_bytes(const int count)360*4882a593Smuzhiyun void* get_random_bytes(const int count)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun char *buf;
363*4882a593Smuzhiyun int fd;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun buf = NOFAIL(malloc(count));
366*4882a593Smuzhiyun fd = open(RANDOM_DEVICE, O_RDONLY);
367*4882a593Smuzhiyun if (fd < 0) {
368*4882a593Smuzhiyun perror("open(" RANDOM_DEVICE ")");
369*4882a593Smuzhiyun exit(2);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun if (read(fd, buf, count) != count) {
372*4882a593Smuzhiyun if (count < 0)
373*4882a593Smuzhiyun perror("read(" RANDOM_DEVICE ")");
374*4882a593Smuzhiyun else
375*4882a593Smuzhiyun fprintf(stderr, "Short read of %s.\n", RANDOM_DEVICE);
376*4882a593Smuzhiyun exit(2);
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun close(fd);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun return buf;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun #endif
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun #ifdef RANDOM_DEVICE
385*4882a593Smuzhiyun
generate_salt(char * const buf,const unsigned int len)386*4882a593Smuzhiyun void generate_salt(char *const buf, const unsigned int len)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun unsigned int i;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun unsigned char *entropy = get_random_bytes(len * sizeof(unsigned char));
391*4882a593Smuzhiyun for (i = 0; i < len; i++)
392*4882a593Smuzhiyun buf[i] = valid_salts[entropy[i] % (sizeof valid_salts - 1)];
393*4882a593Smuzhiyun buf[i] = '\0';
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun #else /* RANDOM_DEVICE */
397*4882a593Smuzhiyun
generate_salt(char * const buf,const unsigned int len)398*4882a593Smuzhiyun void generate_salt(char *const buf, const unsigned int len)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun unsigned int i;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun # ifdef HAVE_GETTIMEOFDAY
403*4882a593Smuzhiyun struct timeval tv;
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun gettimeofday(&tv, NULL);
406*4882a593Smuzhiyun srand(tv.tv_sec ^ tv.tv_usec);
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun # else /* HAVE_GETTIMEOFDAY */
409*4882a593Smuzhiyun # warning "This system lacks a strong enough random numbers generator!"
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun /*
412*4882a593Smuzhiyun * The possible values of time over one year are 31536000, which is
413*4882a593Smuzhiyun * two orders of magnitude less than the allowed entropy range (2^32).
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun srand(time(NULL) + getpid());
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun # endif /* HAVE_GETTIMEOFDAY */
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun for (i = 0; i < len; i++)
420*4882a593Smuzhiyun buf[i] = valid_salts[rand() % (sizeof valid_salts - 1)];
421*4882a593Smuzhiyun buf[i] = '\0';
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun #endif /* RANDOM_DEVICE */
425*4882a593Smuzhiyun
display_help(int error)426*4882a593Smuzhiyun void display_help(int error)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun fprintf((EXIT_SUCCESS == error) ? stdout : stderr,
429*4882a593Smuzhiyun _("Usage: mkpasswd [OPTIONS]... [PASSWORD [SALT]]\n"
430*4882a593Smuzhiyun "Crypts the PASSWORD using crypt(3).\n\n"));
431*4882a593Smuzhiyun fprintf(stderr, _(
432*4882a593Smuzhiyun " -m, --method=TYPE select method TYPE\n"
433*4882a593Smuzhiyun " -5 like --method=md5\n"
434*4882a593Smuzhiyun " -S, --salt=SALT use the specified SALT\n"
435*4882a593Smuzhiyun " -R, --rounds=NUMBER use the specified NUMBER of rounds\n"
436*4882a593Smuzhiyun " -P, --password-fd=NUM read the password from file descriptor NUM\n"
437*4882a593Smuzhiyun " instead of /dev/tty\n"
438*4882a593Smuzhiyun " -s, --stdin like --password-fd=0\n"
439*4882a593Smuzhiyun " -h, --help display this help and exit\n"
440*4882a593Smuzhiyun " -V, --version output version information and exit\n"
441*4882a593Smuzhiyun "\n"
442*4882a593Smuzhiyun "If PASSWORD is missing then it is asked interactively.\n"
443*4882a593Smuzhiyun "If no SALT is specified, a random one is generated.\n"
444*4882a593Smuzhiyun "If TYPE is 'help', available methods are printed.\n"
445*4882a593Smuzhiyun "\n"
446*4882a593Smuzhiyun "Report bugs to %s.\n"), "<md+whois@linux.it>");
447*4882a593Smuzhiyun exit(error);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
display_version(void)450*4882a593Smuzhiyun void display_version(void)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun printf("mkpasswd %s\n\n", VERSION);
453*4882a593Smuzhiyun puts("Copyright (C) 2001-2008 Marco d'Itri\n"
454*4882a593Smuzhiyun "This is free software; see the source for copying conditions. There is NO\n"
455*4882a593Smuzhiyun "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
display_methods(void)458*4882a593Smuzhiyun void display_methods(void)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun unsigned int i;
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun printf(_("Available methods:\n"));
463*4882a593Smuzhiyun for (i = 0; methods[i].method != NULL; i++)
464*4882a593Smuzhiyun printf("%s\t%s\n", methods[i].method, methods[i].desc);
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467