]>
jfr.im git - irc/rqf/shadowircd.git/blob - tools/mkpasswd.c
1 /* simple password generator by Nelson Minar (minar@reed.edu)
2 ** copyright 1991, all rights reserved.
3 ** You can use this code as long as my name stays with it.
5 ** md5 patch by W. Campbell <wcampbel@botbay.net>
6 ** Modernization, getopt, etc for the Hybrid IRCD team
9 ** /dev/random for salt generation added by
10 ** Aaron Sethman <androsyn@ratbox.org>
19 #include "ratbox_lib.h"
24 #define FLAG_MD5 0x00000001
25 #define FLAG_DES 0x00000002
26 #define FLAG_SALT 0x00000004
27 #define FLAG_PASS 0x00000008
28 #define FLAG_LENGTH 0x00000010
29 #define FLAG_BLOWFISH 0x00000020
30 #define FLAG_ROUNDS 0x00000040
31 #define FLAG_EXT 0x00000080
32 #define FLAG_SHA256 0x00000100
33 #define FLAG_SHA512 0x00000200
36 static char *make_des_salt(void);
37 static char *make_ext_salt(int);
38 static char *make_ext_salt_para(int, char *);
39 static char *make_md5_salt(int);
40 static char *make_md5_salt_para(char *);
41 static char *make_sha256_salt(int);
42 static char *make_sha256_salt_para(char *);
43 static char *make_sha512_salt(int);
44 static char *make_sha512_salt_para(char *);
45 static char *make_bf_salt(int, int);
46 static char *make_bf_salt_para(int, char *);
47 static char *int_to_base64(int);
48 static char *generate_random_salt(char *, int);
49 static char *generate_poor_salt(char *, int);
51 static void full_usage(void);
52 static void brief_usage(void);
54 static char saltChars
[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
55 /* 0 .. 63, ascii - 64 */
66 static char getpassbuf
[PASS_MAX
+ 1];
69 getpass(const char *prompt
)
74 memset(getpassbuf
, sizeof(getpassbuf
), 0);
75 fputs(prompt
, stderr
);
89 fputs("\r\n", stderr
);
97 main(int argc
, char *argv
[])
99 char *plaintext
= NULL
;
101 char *saltpara
= NULL
;
104 int length
= 0; /* Not Set */
105 int rounds
= 0; /* Not set, since extended DES needs 25 and blowfish needs
106 ** 4 by default, a side effect of this being the encryption
107 ** type parameter must be specified before the rounds
111 while((c
= getopt(argc
, argv
, "xymdber:h?l:s:p:")) != -1)
122 flag
|= FLAG_BLOWFISH
;
131 length
= atoi(optarg
);
135 rounds
= atoi(optarg
);
160 printf("Invalid Option: -%c\n", c
);
165 if(flag
& FLAG_BLOWFISH
)
170 salt
= make_bf_salt_para(rounds
, saltpara
);
172 salt
= make_bf_salt(rounds
, length
);
174 else if(flag
& FLAG_SHA256
)
179 salt
= make_sha256_salt_para(saltpara
);
181 salt
= make_sha256_salt(length
);
183 else if(flag
& FLAG_SHA512
)
188 salt
= make_sha512_salt_para(saltpara
);
190 salt
= make_sha512_salt(length
);
192 else if(flag
& FLAG_EXT
)
194 /* XXX - rounds needs to be done */
197 if((strlen(saltpara
) == 4))
199 salt
= make_ext_salt_para(rounds
, saltpara
);
203 printf("Invalid salt, please enter 4 alphanumeric characters\n");
209 salt
= make_ext_salt(rounds
);
212 else if (flag
& FLAG_DES
)
216 if((strlen(saltpara
) == 2))
222 printf("Invalid salt, please enter 2 alphanumeric characters\n");
228 salt
= make_des_salt();
236 salt
= make_md5_salt_para(saltpara
);
238 salt
= make_md5_salt(length
);
244 printf("Please enter a valid password\n");
248 plaintext
= getpass("plaintext: ");
251 printf("%s\n", rb_crypt(plaintext
, salt
));
259 generate_random_salt(salt
, 2);
265 int_to_base64(int value
)
270 for(i
= 0; i
< 4; i
++)
272 buf
[i
] = saltChars
[value
& 63];
273 value
>>= 6; /* Right shifting 6 places is the same as dividing by 64 */
276 buf
[i
] = '\0'; /* not REALLY needed as it's static, and thus initialized
283 make_ext_salt(int rounds
)
285 static char salt
[10];
287 sprintf(salt
, "_%s", int_to_base64(rounds
));
288 generate_random_salt(&salt
[5], 4);
294 make_ext_salt_para(int rounds
, char *saltpara
)
296 static char salt
[10];
298 sprintf(salt
, "_%s%s", int_to_base64(rounds
), saltpara
);
303 make_md5_salt_para(char *saltpara
)
305 static char salt
[21];
306 if(saltpara
&& (strlen(saltpara
) <= 16))
308 /* sprintf used because of portability requirements, the length
309 ** is checked above, so it should not be too much of a concern
311 sprintf(salt
, "$1$%s$", saltpara
);
314 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
322 make_md5_salt(int length
)
324 static char salt
[21];
327 printf("MD5 salt length too long\n");
333 generate_random_salt(&salt
[3], length
);
334 salt
[length
+ 3] = '$';
335 salt
[length
+ 4] = '\0';
340 make_sha256_salt_para(char *saltpara
)
342 static char salt
[21];
343 if(saltpara
&& (strlen(saltpara
) <= 16))
345 /* sprintf used because of portability requirements, the length
346 ** is checked above, so it should not be too much of a concern
348 sprintf(salt
, "$5$%s$", saltpara
);
351 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
359 make_sha512_salt_para(char *saltpara
)
361 static char salt
[21];
362 if(saltpara
&& (strlen(saltpara
) <= 16))
364 /* sprintf used because of portability requirements, the length
365 ** is checked above, so it should not be too much of a concern
367 sprintf(salt
, "$6$%s$", saltpara
);
370 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
379 make_sha256_salt(int length
)
381 static char salt
[21];
384 printf("SHA256 salt length too long\n");
390 generate_random_salt(&salt
[3], length
);
391 salt
[length
+ 3] = '$';
392 salt
[length
+ 4] = '\0';
397 make_sha512_salt(int length
)
399 static char salt
[21];
402 printf("SHA512 salt length too long\n");
408 generate_random_salt(&salt
[3], length
);
409 salt
[length
+ 3] = '$';
410 salt
[length
+ 4] = '\0';
415 make_bf_salt_para(int rounds
, char *saltpara
)
417 static char salt
[31];
419 if(saltpara
&& (strlen(saltpara
) <= 22))
421 /* sprintf used because of portability requirements, the length
422 ** is checked above, so it should not be too much of a concern
424 sprintf(tbuf
, "%02d", rounds
);
425 sprintf(salt
, "$2a$%s$%s$", tbuf
, saltpara
);
428 printf("Invalid Salt, please use up to 22 random alphanumeric characters\n");
436 make_bf_salt(int rounds
, int length
)
438 static char salt
[31];
442 printf("BlowFish salt length too long\n");
445 sprintf(tbuf
, "%02d", rounds
);
446 sprintf(salt
, "$2a$%s$", tbuf
);
447 generate_random_salt(&salt
[7], length
);
448 salt
[length
+ 7] = '$';
449 salt
[length
+ 8] = '\0';
454 generate_poor_salt(char *salt
, int length
)
458 for(i
= 0; i
< length
; i
++)
460 salt
[i
] = saltChars
[rand() % 64];
466 generate_random_salt(char *salt
, int length
)
470 if((fd
= open("/dev/random", O_RDONLY
)) < 0)
472 return (generate_poor_salt(salt
, length
));
474 buf
= calloc(1, length
);
475 if(read(fd
, buf
, length
) != length
)
478 return (generate_poor_salt(salt
, length
));
481 for(i
= 0; i
< length
; i
++)
483 salt
[i
] = saltChars
[abs(buf
[i
]) % 64];
492 printf("mkpasswd [-m|-d|-b|-e] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n");
493 printf("-x Generate a SHA256 password\n");
494 printf("-y Generate a SHA512 password\n");
495 printf("-m Generate an MD5 password\n");
496 printf("-d Generate a DES password\n");
497 printf("-b Generate a BlowFish password\n");
498 printf("-e Generate an Extended DES password\n");
499 printf("-l Specify a length for a random MD5 or BlowFish salt\n");
500 printf("-r Specify a number of rounds for a BlowFish or Extended DES password\n");
501 printf(" BlowFish: default 4, no more than 6 recommended\n");
502 printf(" Extended DES: default 25\n");
503 printf("-s Specify a salt, 2 alphanumeric characters for DES, up to 16 for MD5,\n");
504 printf(" up to 22 for BlowFish, and 4 for Extended DES\n");
505 printf("-p Specify a plaintext password to use\n");
506 printf("Example: mkpasswd -m -s 3dr -p test\n");
513 printf("mkpasswd - password hash generator\n");
514 printf("Standard DES: mkpasswd [-d] [-s salt] [-p plaintext]\n");
515 printf("Extended DES: mkpasswd -e [-r rounds] [-s salt] [-p plaintext]\n");
516 printf(" MD5: mkpasswd -m [-l saltlength] [-s salt] [-p plaintext]\n");
517 printf(" BlowFish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n");
518 printf(" [-p plaintext]\n");
519 printf("Use -h for full usage\n");