Import new mkpasswd from ircd-ratbox.

This now uses libratbox.
This commit is contained in:
Jilles Tjoelker 2009-02-22 00:59:16 +01:00
parent 4a584d412e
commit f1d07f6b7c
2 changed files with 441 additions and 282 deletions

View file

@ -15,13 +15,14 @@ MV = @MV@
RM = @RM@
LN = @LN@
IRCDLIBS = @LIBS@
INCLUDES = -I../include
IRCDLIBS = -L../libratbox/src/.libs -lratbox @LIBS@
INCLUDES = -I../include -I../libratbox/include
CPPFLAGS = ${INCLUDES} @CPPFLAGS@
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
libdir = @libdir@
libexecdir = @libexecdir@
confdir = @confdir@
localstatedir = @localstatedir@

View file

@ -9,7 +9,7 @@
** /dev/random for salt generation added by
** Aaron Sethman <androsyn@ratbox.org>
**
** $Id: mkpasswd.c 6 2005-09-10 01:02:21Z nenolod $
** $Id: mkpasswd.c 26439 2009-02-01 15:27:24Z jilles $
*/
#include <stdio.h>
#include <string.h>
@ -17,6 +17,10 @@
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include "ratbox_lib.h"
#ifndef __MINGW32__
#include <pwd.h>
#endif
#define FLAG_MD5 0x00000001
#define FLAG_DES 0x00000002
@ -26,15 +30,19 @@
#define FLAG_BLOWFISH 0x00000020
#define FLAG_ROUNDS 0x00000040
#define FLAG_EXT 0x00000080
#define FLAG_SHA256 0x00000100
#define FLAG_SHA512 0x00000200
extern char *getpass();
extern char *crypt();
static char *make_des_salt(void);
static char *make_ext_salt(int);
static char *make_ext_salt_para(int, char *);
static char *make_md5_salt(int);
static char *make_md5_salt_para(char *);
static char *make_sha256_salt(int);
static char *make_sha256_salt_para(char *);
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);
@ -44,13 +52,50 @@ static char *generate_poor_salt(char *, int);
static void full_usage(void);
static void brief_usage(void);
static char saltChars[] =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static char saltChars[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
/* 0 .. 63, ascii - 64 */
extern char *optarg;
int main(int argc, char *argv[])
#ifdef __MINGW32__
#include <conio.h>
#ifdef PASS_MAX
#undef PASS_MAX
#endif
#define PASS_MAX 256
static char getpassbuf[PASS_MAX + 1];
static char *
getpass(const char *prompt)
{
int c;
int i = 0;
memset(getpassbuf, sizeof(getpassbuf), 0);
fputs(prompt, stderr);
for(;;)
{
c = _getch();
if(c == '\r')
{
getpassbuf[i] = '\0';
break;
}
else if(i < PASS_MAX)
{
getpassbuf[i++] = c;
}
}
fputs("\r\n", stderr);
return getpassbuf;
}
#endif
int
main(int argc, char *argv[])
{
char *plaintext = NULL;
int c;
@ -64,7 +109,7 @@ int main(int argc, char *argv[])
** parameter.
*/
while( (c=getopt(argc, argv, "mdber:h?l:s:p:")) != -1)
while((c = getopt(argc, argv, "xymdber:h?l:s:p:")) != -1)
{
switch (c)
{
@ -98,6 +143,12 @@ int main(int argc, char *argv[])
flag |= FLAG_PASS;
plaintext = optarg;
break;
case 'x':
flag |= FLAG_SHA256;
break;
case 'y':
flag |= FLAG_SHA512;
break;
case 'h':
full_usage();
/* NOT REACHED */
@ -130,6 +181,24 @@ int main(int argc, char *argv[])
else
salt = make_bf_salt(rounds, length);
}
else if(flag & FLAG_SHA256)
{
if(length == 0)
length = 16;
if(flag & FLAG_SALT)
salt = make_sha256_salt_para(saltpara);
else
salt = make_sha256_salt(length);
}
else if(flag & FLAG_SHA512)
{
if(length == 0)
length = 16;
if(flag & FLAG_SALT)
salt = make_sha512_salt_para(saltpara);
else
salt = make_sha512_salt(length);
}
else if(flag & FLAG_EXT)
{
/* XXX - rounds needs to be done */
@ -180,11 +249,12 @@ int main(int argc, char *argv[])
plaintext = getpass("plaintext: ");
}
printf("%s\n", crypt(plaintext, salt));
printf("%s\n", rb_crypt(plaintext, salt));
return 0;
}
static char *make_des_salt()
static char *
make_des_salt()
{
static char salt[3];
generate_random_salt(salt, 2);
@ -192,7 +262,8 @@ static char *make_des_salt()
return salt;
}
char *int_to_base64(int value)
char *
int_to_base64(int value)
{
static char buf[5];
int i;
@ -209,7 +280,8 @@ char *int_to_base64(int value)
return buf;
}
char *make_ext_salt(int rounds)
char *
make_ext_salt(int rounds)
{
static char salt[10];
@ -219,7 +291,8 @@ char *make_ext_salt(int rounds)
return salt;
}
char *make_ext_salt_para(int rounds, char *saltpara)
char *
make_ext_salt_para(int rounds, char *saltpara)
{
static char salt[10];
@ -227,7 +300,8 @@ char *make_ext_salt_para(int rounds, char *saltpara)
return salt;
}
char *make_md5_salt_para(char *saltpara)
char *
make_md5_salt_para(char *saltpara)
{
static char salt[21];
if(saltpara && (strlen(saltpara) <= 16))
@ -245,7 +319,8 @@ char *make_md5_salt_para(char *saltpara)
return NULL;
}
char *make_md5_salt(int length)
char *
make_md5_salt(int length)
{
static char salt[21];
if(length > 16)
@ -262,7 +337,83 @@ char *make_md5_salt(int length)
return salt;
}
char *make_bf_salt_para(int rounds, char *saltpara)
char *
make_sha256_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, "$5$%s$", saltpara);
return salt;
}
printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
exit(1);
/* NOT REACHED */
return NULL;
}
char *
make_sha512_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, "$6$%s$", saltpara);
return salt;
}
printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
exit(1);
/* NOT REACHED */
return NULL;
}
char *
make_sha256_salt(int length)
{
static char salt[21];
if(length > 16)
{
printf("SHA256 salt length too long\n");
exit(0);
}
salt[0] = '$';
salt[1] = '5';
salt[2] = '$';
generate_random_salt(&salt[3], length);
salt[length + 3] = '$';
salt[length + 4] = '\0';
return salt;
}
char *
make_sha512_salt(int length)
{
static char salt[21];
if(length > 16)
{
printf("SHA512 salt length too long\n");
exit(0);
}
salt[0] = '$';
salt[1] = '6';
salt[2] = '$';
generate_random_salt(&salt[3], length);
salt[length + 3] = '$';
salt[length + 4] = '\0';
return salt;
}
char *
make_bf_salt_para(int rounds, char *saltpara)
{
static char salt[31];
char tbuf[3];
@ -282,7 +433,8 @@ char *make_bf_salt_para(int rounds, char *saltpara)
return NULL;
}
char *make_bf_salt(int rounds, int length)
char *
make_bf_salt(int rounds, int length)
{
static char salt[31];
char tbuf[3];
@ -299,18 +451,20 @@ char *make_bf_salt(int rounds, int length)
return salt;
}
char *generate_poor_salt(char *salt, int length)
char *
generate_poor_salt(char *salt, int length)
{
int i;
srandom(time(NULL));
srand(time(NULL));
for(i = 0; i < length; i++)
{
salt[i] = saltChars[random() % 64];
salt[i] = saltChars[rand() % 64];
}
return (salt);
}
char *generate_random_salt(char *salt, int length)
char *
generate_random_salt(char *salt, int length)
{
char *buf;
int fd, i;
@ -333,9 +487,12 @@ char *generate_random_salt(char *salt, int length)
return (salt);
}
void full_usage()
void
full_usage()
{
printf("mkpasswd [-m|-d|-b|-e] [-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");
@ -351,7 +508,8 @@ void full_usage()
exit(0);
}
void brief_usage()
void
brief_usage()
{
printf("mkpasswd - password hash generator\n");
printf("Standard DES: mkpasswd [-d] [-s salt] [-p plaintext]\n");