1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/* 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 <wcampbel@botbay.net>
** Modernization, getopt, etc for the Hybrid IRCD team
** by W. Campbell
**
** $Id: mkpasswd.c 1350 2005-11-29 11:40:00Z kreator $
*/
#include "stdinc.h"
#define FLAG_MD5 0x00000001
#define FLAG_DES 0x00000002
#define FLAG_SALT 0x00000004
#define FLAG_PASS 0x00000008
#define FLAG_LENGTH 0x00000010
extern char *getpass(const char *);
extern char *crypt(const char *, const char *);
#ifdef HAVE_SOLARIS
extern char *crypt_md5(const char *, const char *);
#endif
char *make_des_salt(void);
char *make_md5_salt(int);
char *make_md5_salt_para(char *);
void usage();
static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
int main(int argc, char *argv[])
{
char *plaintext = NULL;
extern char *optarg;
int c;
char *saltpara = NULL;
char *salt;
int flag = 0;
int length = 8;
srandom(time(NULL));
while( (c=getopt(argc, argv, "mdh?l:s:p:")) != -1)
{
switch(c)
{
case 'm':
flag |= FLAG_MD5;
break;
case 'd':
flag |= FLAG_DES;
break;
case 'l':
flag |= FLAG_LENGTH;
length = atoi(optarg);
break;
case 's':
flag |= FLAG_SALT;
saltpara = optarg;
break;
case 'p':
flag |= FLAG_PASS;
plaintext = optarg;
break;
case 'h':
case '?':
usage();
break;
default:
printf("Invalid Option: -%c\n", c);
break;
}
}
if (flag & FLAG_MD5)
{
if (flag & FLAG_SALT)
salt = make_md5_salt_para(saltpara);
else
salt = make_md5_salt(length);
}
else
{
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();
}
}
if (flag & FLAG_PASS)
{
if (!plaintext)
printf("Please enter a valid password\n");
}
else
{
plaintext = getpass("plaintext: ");
}
#ifdef HAVE_SOLARIS
if (flag & FLAG_MD5)
printf("%s\n", crypt_md5(plaintext, salt));
else
#endif
printf("%s\n", crypt(plaintext, salt));
return 0;
}
char *make_des_salt(void)
{
static char salt[3];
salt[0] = saltChars[random() % 64];
salt[1] = saltChars[random() % 64];
salt[2] = '\0';
return salt;
}
char *make_md5_salt_para(char *saltpara)
{
static char salt[21];
if (saltpara && (strlen(saltpara) <= 16))
{
/* sprintf used because of portability requirements, the length
** is checked above, so it should not be too much of a concern
*/
sprintf(salt, "$1$%s$", saltpara);
return salt;
}
printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
exit(1);
/* NOT REACHED */
return NULL;
}
char *make_md5_salt(int length)
{
static char salt[21];
int i;
if (length > 16)
{
printf("MD5 salt length too long\n");
exit(0);
}
salt[0] = '$';
salt[1] = '1';
salt[2] = '$';
for (i=3; i<(length+3); i++)
salt[i] = saltChars[random() % 64];
salt[length+3] = '$';
salt[length+4] = '\0';
return salt;
}
void usage()
{
printf("mkpasswd [-m|-d] [-l saltlength] [-s salt] [-p plaintext]\n");
printf("-m Generate an MD5 password\n");
printf("-d Generate a DES password\n");
printf("-l Specify a length for a random MD5 salt\n");
printf("-s Specify a salt, 2 alphanumeric characters for DES, up to 16 for MD5\n");
printf("-p Specify a plaintext password to use\n");
printf("Example: mkpasswd -m -s 3dr -p test\n");
exit(0);
}
|