2 * Copyright (C) 2001-2008 Marco d'Itri
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /* for crypt, snprintf and strcasecmp */
28 #ifdef HAVE_GETOPT_LONG
34 #include <sys/types.h>
39 #ifdef HAVE_LINUX_CRYPT_GENSALT
43 #ifdef HAVE_GETTIMEOFDAY
47 /* Application-specific */
50 /* Global variables */
51 #ifdef HAVE_GETOPT_LONG
52 static const struct option longopts[] = {
53 {"method", optional_argument, NULL, 'm'},
54 /* for backward compatibility with versions < 4.7.25 (< 20080321): */
55 {"hash", optional_argument, NULL, 'H'},
56 {"help", no_argument, NULL, 'h'},
57 {"password-fd", required_argument, NULL, 'P'},
58 {"stdin", no_argument, NULL, 's'},
59 {"salt", required_argument, NULL, 'S'},
60 {"rounds", required_argument, NULL, 'R'},
61 {"version", no_argument, NULL, 'V'},
69 static const char valid_salts[] = "abcdefghijklmnopqrstuvwxyz"
70 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
73 const char *method; /* short name used by the command line option */
74 const char *prefix; /* salt prefix */
75 const unsigned int minlen; /* minimum salt length */
76 const unsigned int maxlen; /* maximum salt length */
77 const unsigned int rounds; /* supports a variable number of rounds */
78 const char *desc; /* long description for the methods list */
81 static const struct crypt_method methods[] = {
82 /* method prefix minlen, maxlen rounds description */
84 N_("standard 56 bit DES-based crypt(3)") },
85 { "md5", "$1$", 8, 8, 0, "MD5" },
86 #if defined OpenBSD || defined FreeBSD || (defined __SVR4 && defined __sun)
87 { "bf", "$2a$", 22, 22, 1, "Blowfish" },
89 #if defined HAVE_LINUX_CRYPT_GENSALT
90 { "bf", "$2a$", 22, 22, 1, "Blowfish, system-specific on 8-bit chars" },
91 /* algorithm 2y fixes CVE-2011-2483 */
92 { "bfy", "$2y$", 22, 22, 1, "Blowfish, correct handling of 8-bit chars" },
95 { "nt", "$3$", 0, 0, 0, "NT-Hash" },
97 #if defined HAVE_SHA_CRYPT
98 /* http://people.redhat.com/drepper/SHA-crypt.txt */
99 { "sha-256", "$5$", 8, 16, 1, "SHA-256" },
100 { "sha-512", "$6$", 8, 16, 1, "SHA-512" },
102 /* http://www.crypticide.com/dropsafe/article/1389 */
104 * Actually the maximum salt length is arbitrary, but Solaris by default
105 * always uses 8 characters:
106 * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/ \
107 * usr/src/lib/crypt_modules/sunmd5/sunmd5.c#crypt_gensalt_impl
109 #if defined __SVR4 && defined __sun
110 { "sunmd5", "$md5$", 8, 8, 1, "SunMD5" },
112 { NULL, NULL, 0, 0, 0, NULL }
115 void generate_salt(char *const buf, const unsigned int len);
116 void *get_random_bytes(const int len);
117 void display_help(int error);
118 void display_version(void);
119 void display_methods(void);
121 int main(int argc, char *argv[])
124 int password_fd = -1;
125 unsigned int salt_minlen = 0;
126 unsigned int salt_maxlen = 0;
127 unsigned int rounds_support = 0;
128 const char *salt_prefix = NULL;
129 const char *salt_arg = NULL;
130 unsigned int rounds = 0;
133 char *password = NULL;
136 setlocale(LC_ALL, "");
137 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
138 textdomain(NLS_CAT_NAME);
141 /* prepend options from environment */
142 argv = merge_args(getenv("MKPASSWD_OPTIONS"), argv, &argc);
144 while ((ch = GETOPT_LONGISH(argc, argv, "hH:m:5P:R:sS:V", longopts, 0))
148 optarg = (char *) "md5";
152 if (!optarg || strcaseeq("help", optarg)) {
156 for (i = 0; methods[i].method != NULL; i++)
157 if (strcaseeq(methods[i].method, optarg)) {
158 salt_prefix = methods[i].prefix;
159 salt_minlen = methods[i].minlen;
160 salt_maxlen = methods[i].maxlen;
161 rounds_support = methods[i].rounds;
165 fprintf(stderr, _("Invalid method '%s'.\n"), optarg);
172 password_fd = strtol(optarg, &p, 10);
173 if (p == NULL || *p != '\0' || password_fd < 0) {
174 fprintf(stderr, _("Invalid number '%s'.\n"), optarg);
182 rounds = strtol(optarg, &p, 10);
183 if (p == NULL || *p != '\0' || rounds < 0) {
184 fprintf(stderr, _("Invalid number '%s'.\n"), optarg);
199 display_help(EXIT_SUCCESS);
201 fprintf(stderr, _("Try '%s --help' for more information.\n"),
209 if (argc == 2 && !salt_arg) {
212 } else if (argc == 1) {
214 } else if (argc == 0) {
216 display_help(EXIT_FAILURE);
219 /* default: DES password */
221 salt_minlen = methods[0].minlen;
222 salt_maxlen = methods[0].maxlen;
223 salt_prefix = methods[0].prefix;
226 if (streq(salt_prefix, "$2a$") || streq(salt_prefix, "$2y$")) {
227 /* OpenBSD Blowfish and derivatives */
230 /* actually for 2a/2y it is the logarithm of the number of rounds */
231 snprintf(rounds_str, sizeof(rounds_str), "%02u$", rounds);
232 } else if (rounds_support && rounds)
233 snprintf(rounds_str, sizeof(rounds_str), "rounds=%u$", rounds);
235 rounds_str[0] = '\0';
238 unsigned int c = strlen(salt_arg);
239 if (c < salt_minlen || c > salt_maxlen) {
240 if (salt_minlen == salt_maxlen)
241 fprintf(stderr, ngettext(
242 "Wrong salt length: %d byte when %d expected.\n",
243 "Wrong salt length: %d bytes when %d expected.\n", c),
246 fprintf(stderr, ngettext(
247 "Wrong salt length: %d byte when %d <= n <= %d"
249 "Wrong salt length: %d bytes when %d <= n <= %d"
251 c, salt_minlen, salt_maxlen);
255 if (strchr(valid_salts, salt_arg[c]) == NULL) {
256 fprintf(stderr, _("Illegal salt character '%c'.\n"),
262 salt = NOFAIL(malloc(strlen(salt_prefix) + strlen(rounds_str)
263 + strlen(salt_arg) + 1));
265 strcat(salt, salt_prefix);
266 strcat(salt, rounds_str);
267 strcat(salt, salt_arg);
269 #ifdef HAVE_SOLARIS_CRYPT_GENSALT
270 #error "This code path is untested on Solaris. Please send a patch."
271 salt = crypt_gensalt(salt_prefix, NULL);
273 perror(stderr, "crypt_gensalt");
274 #elif defined HAVE_LINUX_CRYPT_GENSALT
275 void *entropy = get_random_bytes(64);
277 salt = crypt_gensalt(salt_prefix, rounds, entropy, 64);
279 fprintf(stderr, "crypt_gensalt failed.\n");
284 unsigned int salt_len = salt_maxlen;
286 if (salt_minlen != salt_maxlen) { /* salt length can vary */
287 srand(time(NULL) + getpid());
288 salt_len = rand() % (salt_maxlen - salt_minlen + 1) + salt_minlen;
291 salt = NOFAIL(malloc(strlen(salt_prefix) + strlen(rounds_str)
294 strcat(salt, salt_prefix);
295 strcat(salt, rounds_str);
296 generate_salt(salt + strlen(salt), salt_len);
301 } else if (password_fd != -1) {
305 if (isatty(password_fd))
306 fprintf(stderr, _("Password: "));
307 password = NOFAIL(malloc(128));
308 fp = fdopen(password_fd, "r");
313 if (!fgets(password, 128, fp)) {
318 p = strpbrk(password, "\n\r");
322 password = getpass(_("Password: "));
331 result = crypt(password, salt);
332 /* xcrypt returns "*0" on errors */
333 if (!result || result[0] == '*') {
334 fprintf(stderr, "crypt failed.\n");
337 /* yes, using strlen(salt_prefix) on salt. It's not
338 * documented whether crypt_gensalt may change the prefix */
339 if (!strneq(result, salt, strlen(salt_prefix))) {
340 fprintf(stderr, _("Method not supported by crypt(3).\n"));
343 printf("%s\n", result);
350 void* get_random_bytes(const int count)
355 buf = NOFAIL(malloc(count));
356 fd = open(RANDOM_DEVICE, O_RDONLY);
358 perror("open(" RANDOM_DEVICE ")");
361 if (read(fd, buf, count) != count) {
363 perror("read(" RANDOM_DEVICE ")");
365 fprintf(stderr, "Short read of %s.\n", RANDOM_DEVICE);
376 void generate_salt(char *const buf, const unsigned int len)
380 unsigned char *entropy = get_random_bytes(len * sizeof(unsigned char));
381 for (i = 0; i < len; i++)
382 buf[i] = valid_salts[entropy[i] % (sizeof valid_salts - 1)];
386 #else /* RANDOM_DEVICE */
388 void generate_salt(char *const buf, const unsigned int len)
392 # ifdef HAVE_GETTIMEOFDAY
395 gettimeofday(&tv, NULL);
396 srand(tv.tv_sec ^ tv.tv_usec);
398 # else /* HAVE_GETTIMEOFDAY */
399 # warning "This system lacks a strong enough random numbers generator!"
402 * The possible values of time over one year are 31536000, which is
403 * two orders of magnitude less than the allowed entropy range (2^32).
405 srand(time(NULL) + getpid());
407 # endif /* HAVE_GETTIMEOFDAY */
409 for (i = 0; i < len; i++)
410 buf[i] = valid_salts[rand() % (sizeof valid_salts - 1)];
414 #endif /* RANDOM_DEVICE */
416 void display_help(int error)
418 fprintf((EXIT_SUCCESS == error) ? stdout : stderr,
419 _("Usage: mkpasswd [OPTIONS]... [PASSWORD [SALT]]\n"
420 "Crypts the PASSWORD using crypt(3).\n\n"));
422 " -m, --method=TYPE select method TYPE\n"
423 " -5 like --method=md5\n"
424 " -S, --salt=SALT use the specified SALT\n"
425 " -R, --rounds=NUMBER use the specified NUMBER of rounds\n"
426 " -P, --password-fd=NUM read the password from file descriptor NUM\n"
427 " instead of /dev/tty\n"
428 " -s, --stdin like --password-fd=0\n"
429 " -h, --help display this help and exit\n"
430 " -V, --version output version information and exit\n"
432 "If PASSWORD is missing then it is asked interactively.\n"
433 "If no SALT is specified, a random one is generated.\n"
434 "If TYPE is 'help', available methods are printed.\n"
436 "Report bugs to %s.\n"), "<md+whois@linux.it>");
440 void display_version(void)
442 printf("mkpasswd %s\n\n", VERSION);
443 puts("Copyright (C) 2001-2008 Marco d'Itri\n"
444 "This is free software; see the source for copying conditions. There is NO\n"
445 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
448 void display_methods(void)
452 printf(_("Available methods:\n"));
453 for (i = 0; methods[i].method != NULL; i++)
454 printf("%s\t%s\n", methods[i].method, methods[i].desc);