| 2 |
|
** copyright 1991, all rights reserved. |
| 3 |
|
** You can use this code as long as my name stays with it. |
| 4 |
|
** |
| 5 |
– |
** md5 patch by W. Campbell <wcampbel@botbay.net> |
| 5 |
|
** Modernization, getopt, etc for the Hybrid IRCD team |
| 6 |
|
** by W. Campbell |
| 7 |
|
** |
| 20 |
|
|
| 21 |
|
enum |
| 22 |
|
{ |
| 23 |
< |
FLAG_MD5 = 0x00000001, |
| 24 |
< |
FLAG_SALT = 0x00000002, |
| 25 |
< |
FLAG_PASS = 0x00000004, |
| 26 |
< |
FLAG_LENGTH = 0x00000008, |
| 27 |
< |
FLAG_BLOWFISH = 0x00000010, |
| 28 |
< |
FLAG_ROUNDS = 0x00000020, |
| 29 |
< |
FLAG_RAW = 0x00000040, |
| 30 |
< |
FLAG_SHA256 = 0x00000080, |
| 32 |
< |
FLAG_SHA512 = 0x00000100 |
| 23 |
> |
FLAG_SHA256 = 0x00000001, |
| 24 |
> |
FLAG_SHA512 = 0x00000002, |
| 25 |
> |
FLAG_BLOWFISH = 0x00000004, |
| 26 |
> |
FLAG_RAW = 0x00000008, |
| 27 |
> |
FLAG_SALT = 0x00000010, |
| 28 |
> |
FLAG_LENGTH = 0x00000020, |
| 29 |
> |
FLAG_ROUNDS = 0x00000040, |
| 30 |
> |
FLAG_PASS = 0x00000080, |
| 31 |
|
}; |
| 32 |
|
|
| 33 |
|
|
| 38 |
|
static const char *make_sha256_salt_para(const char *); |
| 39 |
|
static const char *make_sha512_salt(unsigned int); |
| 40 |
|
static const char *make_sha512_salt_para(const char *); |
| 43 |
– |
static const char *make_md5_salt(unsigned int); |
| 44 |
– |
static const char *make_md5_salt_para(const char *); |
| 41 |
|
static const char *make_blowfish_salt(unsigned int, unsigned int); |
| 42 |
|
static const char *make_blowfish_salt_para(unsigned int, const char *); |
| 43 |
|
static const char *generate_random_salt(char *, unsigned int); |
| 64 |
|
** parameter. |
| 65 |
|
*/ |
| 66 |
|
|
| 67 |
< |
for (int c = 0; (c = getopt(argc, argv, "56mbr:h?l:s:p:R:")) != -1; ) |
| 67 |
> |
for (int c = 0; (c = getopt(argc, argv, "56br:h?l:s:p:R:")) != -1; ) |
| 68 |
|
{ |
| 69 |
|
switch (c) |
| 70 |
|
{ |
| 74 |
|
case '6': |
| 75 |
|
flag |= FLAG_SHA512; |
| 76 |
|
break; |
| 81 |
– |
case 'm': |
| 82 |
– |
flag |= FLAG_MD5; |
| 83 |
– |
break; |
| 77 |
|
case 'b': |
| 78 |
|
flag |= FLAG_BLOWFISH; |
| 79 |
|
rounds = 4; |
| 118 |
|
{ |
| 119 |
|
if (length == 0) |
| 120 |
|
length = 16; |
| 121 |
+ |
|
| 122 |
|
if (flag & FLAG_SALT) |
| 123 |
|
salt = make_sha256_salt_para(saltpara); |
| 124 |
|
else |
| 128 |
|
{ |
| 129 |
|
if (length == 0) |
| 130 |
|
length = 16; |
| 131 |
+ |
|
| 132 |
|
if (flag & FLAG_SALT) |
| 133 |
|
salt = make_sha512_salt_para(saltpara); |
| 134 |
|
else |
| 138 |
|
{ |
| 139 |
|
if (length == 0) |
| 140 |
|
length = 22; |
| 141 |
+ |
|
| 142 |
|
if (flag & FLAG_SALT) |
| 143 |
|
salt = make_blowfish_salt_para(rounds, saltpara); |
| 144 |
|
else |
| 146 |
|
} |
| 147 |
|
else if (flag & FLAG_RAW) |
| 148 |
|
salt = saltpara; |
| 149 |
< |
else /* Default to MD5 */ |
| 149 |
> |
else |
| 150 |
|
{ |
| 151 |
< |
if (length == 0) |
| 152 |
< |
length = 8; |
| 157 |
< |
if (flag & FLAG_SALT) |
| 158 |
< |
salt = make_md5_salt_para(saltpara); |
| 159 |
< |
else |
| 160 |
< |
salt = make_md5_salt(length); |
| 151 |
> |
printf("No hashing algorithm specified\n"); |
| 152 |
> |
exit(EXIT_FAILURE); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
if (flag & FLAG_PASS) |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
static const char * |
| 262 |
– |
make_md5_salt_para(const char *saltpara) |
| 263 |
– |
{ |
| 264 |
– |
static char salt[21]; |
| 265 |
– |
|
| 266 |
– |
if (saltpara && strlen(saltpara) <= 16) |
| 267 |
– |
{ |
| 268 |
– |
snprintf(salt, sizeof(salt), "$1$%s$", saltpara); |
| 269 |
– |
return salt; |
| 270 |
– |
} |
| 271 |
– |
|
| 272 |
– |
printf("Invalid salt, please use up to 16 random alphanumeric characters\n"); |
| 273 |
– |
exit(EXIT_FAILURE); |
| 274 |
– |
|
| 275 |
– |
/* NOT REACHED */ |
| 276 |
– |
return NULL; |
| 277 |
– |
} |
| 278 |
– |
|
| 279 |
– |
static const char * |
| 280 |
– |
make_md5_salt(unsigned int length) |
| 281 |
– |
{ |
| 282 |
– |
static char salt[21]; |
| 283 |
– |
|
| 284 |
– |
if (length > 16) |
| 285 |
– |
{ |
| 286 |
– |
printf("MD5 salt length too long\n"); |
| 287 |
– |
exit(EXIT_FAILURE); |
| 288 |
– |
} |
| 289 |
– |
|
| 290 |
– |
salt[0] = '$'; |
| 291 |
– |
salt[1] = '1'; |
| 292 |
– |
salt[2] = '$'; |
| 293 |
– |
|
| 294 |
– |
generate_random_salt(&salt[3], length); |
| 295 |
– |
|
| 296 |
– |
salt[length + 3] = '$'; |
| 297 |
– |
salt[length + 4] = '\0'; |
| 298 |
– |
|
| 299 |
– |
return salt; |
| 300 |
– |
} |
| 301 |
– |
|
| 302 |
– |
static const char * |
| 254 |
|
make_blowfish_salt_para(unsigned int rounds, const char *saltpara) |
| 255 |
|
{ |
| 256 |
|
static char salt[31]; |
| 335 |
|
static void |
| 336 |
|
full_usage(void) |
| 337 |
|
{ |
| 338 |
< |
printf("mkpasswd [-5|-6|-b|-m] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n"); |
| 338 |
> |
printf("mkpasswd [-5|-6|-b] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n"); |
| 339 |
|
printf(" [-R rawsalt]\n"); |
| 340 |
|
printf("-5 Generate a SHA-256 password\n"); |
| 341 |
|
printf("-6 Generate a SHA-512 password\n"); |
| 342 |
|
printf("-b Generate a Blowfish password\n"); |
| 343 |
< |
printf("-m Generate an MD5 password\n"); |
| 393 |
< |
printf("-l Specify a length for a random MD5 or Blowfish salt\n"); |
| 343 |
> |
printf("-l Specify a length for a random SHA-256/SHA-512 or Blowfish salt\n"); |
| 344 |
|
printf("-r Specify a number of rounds for a Blowfish password;\n"); |
| 345 |
|
printf(" default is 4, no more than 6 recommended\n"); |
| 346 |
< |
printf("-s Specify a salt, up to 16 alphanumeric characters for SHA/MD5,\n"); |
| 346 |
> |
printf("-s Specify a salt, up to 16 alphanumeric characters for SHA-256/SHA-512,\n"); |
| 347 |
|
printf(" and at least 22 for Blowfish\n"); |
| 348 |
|
printf("-R Specify a raw salt passed directly to crypt()\n"); |
| 349 |
|
printf("-p Specify a plaintext password to use\n"); |
| 355 |
|
brief_usage(void) |
| 356 |
|
{ |
| 357 |
|
printf("mkpasswd - password hash generator\n"); |
| 408 |
– |
printf(" MD5: mkpasswd [-m] [-l saltlength] [-s salt] [-p plaintext]\n"); |
| 358 |
|
printf(" SHA-256: mkpasswd -5 [-l saltlength] [-s salt] [-p plaintext]\n"); |
| 359 |
|
printf(" SHA-512: mkpasswd -6 [-l saltlength] [-s salt] [-p plaintext]\n"); |
| 360 |
< |
printf("Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n"); |
| 412 |
< |
printf(" [-p plaintext]\n"); |
| 360 |
> |
printf("Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt] [-p plaintext]\n"); |
| 361 |
|
printf(" Raw: mkpasswd -R <rawsalt> [-p plaintext]\n"); |
| 362 |
|
printf("Use -h for full usage\n"); |
| 363 |
|
exit(EXIT_SUCCESS); |