X-Git-Url: https://jfr.im/git/solanum.git/blobdiff_plain/9e7c93067226337decd23dd4fa0ed40dc123f6f4..d09c55338feb5627da978058432a15dccec16907:/tools/mkpasswd.c?ds=sidebyside diff --git a/tools/mkpasswd.c b/tools/mkpasswd.c index 2ca2312c..8b8c96c1 100644 --- a/tools/mkpasswd.c +++ b/tools/mkpasswd.c @@ -1,42 +1,35 @@ /* simple password generator by Nelson Minar (minar@reed.edu) -** copyright 1991, all rights reserved. -** You can use this code as long as my name stays with it. -** -** md5 patch by W. Campbell -** Modernization, getopt, etc for the Hybrid IRCD team -** by W. Campbell -** -** /dev/random for salt generation added by -** Aaron Sethman -** -** $Id: mkpasswd.c 26439 2009-02-01 15:27:24Z jilles $ -*/ + * copyright 1991, all rights reserved. + * You can use this code as long as my name stays with it. + * + * md5 patch by W. Campbell + * Modernization, getopt, etc for the Hybrid IRCD team + * by W. Campbell + * + * /dev/random for salt generation added by + * Aaron Sethman + */ #include #include #include #include #include #include -#include "ratbox_lib.h" +#include "rb_lib.h" #ifndef __MINGW32__ #include #endif #define FLAG_MD5 0x00000001 -#define FLAG_DES 0x00000002 -#define FLAG_SALT 0x00000004 -#define FLAG_PASS 0x00000008 -#define FLAG_LENGTH 0x00000010 -#define FLAG_BLOWFISH 0x00000020 -#define FLAG_ROUNDS 0x00000040 -#define FLAG_EXT 0x00000080 -#define FLAG_SHA256 0x00000100 -#define FLAG_SHA512 0x00000200 - - -static char *make_des_salt(void); -static char *make_ext_salt(int); -static char *make_ext_salt_para(int, char *); +#define FLAG_SALT 0x00000002 +#define FLAG_PASS 0x00000004 +#define FLAG_LENGTH 0x00000008 +#define FLAG_BLOWFISH 0x00000010 +#define FLAG_ROUNDS 0x00000020 +#define FLAG_SHA256 0x00000040 +#define FLAG_SHA512 0x00000080 + + static char *make_md5_salt(int); static char *make_md5_salt_para(char *); static char *make_sha256_salt(int); @@ -45,19 +38,15 @@ static char *make_sha512_salt(int); static char *make_sha512_salt_para(char *); static char *make_bf_salt(int, int); static char *make_bf_salt_para(int, char *); -static char *int_to_base64(int); static char *generate_random_salt(char *, int); static char *generate_poor_salt(char *, int); -static void full_usage(void); -static void brief_usage(void); +static void full_usage(void) __attribute__((noreturn)); +static void brief_usage(void) __attribute__((noreturn)); static char saltChars[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; /* 0 .. 63, ascii - 64 */ -extern char *optarg; - - #ifdef __MINGW32__ #include #ifdef PASS_MAX @@ -72,7 +61,7 @@ getpass(const char *prompt) int c; int i = 0; - memset(getpassbuf, sizeof(getpassbuf), 0); + memset(getpassbuf, 0, sizeof(getpassbuf)); fputs(prompt, stderr); for(;;) { @@ -101,32 +90,25 @@ main(int argc, char *argv[]) int c; char *saltpara = NULL; char *salt; + char *hashed, *hashed2; int flag = 0; int length = 0; /* Not Set */ - int rounds = 0; /* Not set, since extended DES needs 25 and blowfish needs - ** 4 by default, a side effect of this being the encryption - ** type parameter must be specified before the rounds - ** parameter. + int rounds = 0; /* Not set, since blowfish needs 4 by default, a side effect + * of this being the encryption type parameter must be + * specified before the rounds parameter. */ - while((c = getopt(argc, argv, "xymdber:h?l:s:p:")) != -1) + while((c = getopt(argc, argv, "xymbr:h?l:s:p:")) != -1) { switch (c) { case 'm': flag |= FLAG_MD5; break; - case 'd': - flag |= FLAG_DES; - break; case 'b': flag |= FLAG_BLOWFISH; rounds = 4; break; - case 'e': - flag |= FLAG_EXT; - rounds = 25; - break; case 'l': flag |= FLAG_LENGTH; length = atoi(optarg); @@ -190,45 +172,6 @@ main(int argc, char *argv[]) else salt = make_sha256_salt(length); } - else if(flag & FLAG_EXT) - { - /* XXX - rounds needs to be done */ - if(flag & FLAG_SALT) - { - if((strlen(saltpara) == 4)) - { - salt = make_ext_salt_para(rounds, saltpara); - } - else - { - printf("Invalid salt, please enter 4 alphanumeric characters\n"); - exit(1); - } - } - else - { - salt = make_ext_salt(rounds); - } - } - else if (flag & FLAG_DES) - { - if(flag & FLAG_SALT) - { - if((strlen(saltpara) == 2)) - { - salt = saltpara; - } - else - { - printf("Invalid salt, please enter 2 alphanumeric characters\n"); - exit(1); - } - } - else - { - salt = make_des_salt(); - } - } else { if(length == 0) @@ -242,62 +185,41 @@ main(int argc, char *argv[]) if(flag & FLAG_PASS) { if(!plaintext) - printf("Please enter a valid password\n"); + { + fprintf(stderr, "Please enter a valid password\n"); + return 1; + } + + hashed = rb_crypt(plaintext, salt); } else { plaintext = getpass("plaintext: "); - } - - printf("%s\n", rb_crypt(plaintext, salt)); - return 0; -} - -static char * -make_des_salt() -{ - static char salt[3]; - generate_random_salt(salt, 2); - salt[2] = '\0'; - return salt; -} + hashed = rb_crypt(plaintext, salt); + if (!hashed) + { + fprintf(stderr, "rb_crypt() failed\n"); + return 1; + } + hashed = strdup(hashed); -char * -int_to_base64(int value) -{ - static char buf[5]; - int i; + plaintext = getpass("again: "); + hashed2 = rb_crypt(plaintext, salt); + if (!hashed2) + { + fprintf(stderr, "rb_crypt() failed\n"); + return 1; + } - for(i = 0; i < 4; i++) - { - buf[i] = saltChars[value & 63]; - value >>= 6; /* Right shifting 6 places is the same as dividing by 64 */ + if (strcmp(hashed, hashed2) != 0) + { + fprintf(stderr, "Passwords do not match\n"); + return 1; + } } - buf[i] = '\0'; /* not REALLY needed as it's static, and thus initialized - ** to \0. - */ - return buf; -} - -char * -make_ext_salt(int rounds) -{ - static char salt[10]; - - sprintf(salt, "_%s", int_to_base64(rounds)); - generate_random_salt(&salt[5], 4); - salt[9] = '\0'; - return salt; -} - -char * -make_ext_salt_para(int rounds, char *saltpara) -{ - static char salt[10]; - - sprintf(salt, "_%s%s", int_to_base64(rounds), saltpara); - return salt; + printf("%s\n", hashed); + return 0; } char * @@ -490,19 +412,16 @@ generate_random_salt(char *salt, int length) void full_usage() { - printf("mkpasswd [-m|-d|-b|-e] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n"); + printf("mkpasswd [-m|-b|-x|-y] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n"); printf("-x Generate a SHA256 password\n"); printf("-y Generate a SHA512 password\n"); printf("-m Generate an MD5 password\n"); - printf("-d Generate a DES password\n"); printf("-b Generate a Blowfish password\n"); - printf("-e Generate an Extended DES password\n"); printf("-l Specify a length for a random MD5 or Blowfish salt\n"); - printf("-r Specify a number of rounds for a Blowfish or Extended DES password\n"); - printf(" Blowfish: default 4, no more than 6 recommended\n"); - printf(" Extended DES: default 25\n"); - printf("-s Specify a salt, 2 alphanumeric characters for DES, up to 16 for MD5,\n"); - printf(" up to 22 for Blowfish, and 4 for Extended DES\n"); + printf("-r Specify a number of rounds for a Blowfish password\n"); + printf(" Default 4, no more than 6 recommended\n"); + printf("-s Specify a salt, up to 16 for MD5, SHA256, and SHA512\n"); + printf(" up to 22 for Blowfish\n"); printf("-p Specify a plaintext password to use\n"); printf("Example: mkpasswd -m -s 3dr -p test\n"); exit(0); @@ -512,11 +431,11 @@ void brief_usage() { printf("mkpasswd - password hash generator\n"); - printf("Standard DES: mkpasswd [-d] [-s salt] [-p plaintext]\n"); - printf("Extended DES: mkpasswd -e [-r rounds] [-s salt] [-p plaintext]\n"); - printf(" MD5: mkpasswd -m [-l saltlength] [-s salt] [-p plaintext]\n"); - printf(" Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n"); - printf(" [-p plaintext]\n"); + printf(" SHA512: mkpasswd [-y] [-l saltlength] [-s salt] [-p plaintext]\n"); + printf(" SHA256: mkpasswd -x [-l saltlength] [-s salt] [-p plaintext]\n"); + printf(" MD5: mkpasswd -m [-l saltlength] [-s salt] [-p plaintext]\n"); + printf("Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n"); + printf(" [-p plaintext]\n"); printf("Use -h for full usage\n"); exit(0); }