2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* salt.c - generate a random salt string for crypt()
|
|
|
|
*
|
|
|
|
* Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
|
|
|
|
* public domain.
|
2007-11-16 18:06:21 +05:30
|
|
|
*
|
|
|
|
* l64a was Written by J.T. Conklin <jtc@netbsd.org>. Public domain.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdlib.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "getdef.h"
|
2007-11-16 18:06:21 +05:30
|
|
|
|
|
|
|
#ifndef HAVE_L64A
|
|
|
|
char *l64a(long value)
|
|
|
|
{
|
|
|
|
static char buf[8];
|
|
|
|
char *s = buf;
|
|
|
|
int digit;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; value != 0 && i < 6; i++) {
|
|
|
|
digit = value & 0x3f;
|
|
|
|
|
|
|
|
if (digit < 2)
|
|
|
|
*s = digit + '.';
|
|
|
|
else if (digit < 12)
|
|
|
|
*s = digit + '0' - 2;
|
|
|
|
else if (digit < 38)
|
|
|
|
*s = digit + 'A' - 12;
|
|
|
|
else
|
|
|
|
*s = digit + 'a' - 38;
|
|
|
|
|
|
|
|
value >>= 6;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
return(buf);
|
|
|
|
}
|
|
|
|
#endif /* !HAVE_L64A */
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Generate 8 base64 ASCII characters of random salt. If MD5_CRYPT_ENAB
|
|
|
|
* in /etc/login.defs is "yes", the salt string will be prefixed by "$1$"
|
|
|
|
* (magic) and pw_encrypt() will execute the MD5-based FreeBSD-compatible
|
|
|
|
* version of crypt() instead of the standard one.
|
|
|
|
*/
|
2007-11-20 03:44:19 +05:30
|
|
|
|
2007-11-20 04:04:48 +05:30
|
|
|
#define MAGNUM(array,ch) (array)[0]= (array)[2] = '$',\
|
|
|
|
(array)[1]=(ch),\
|
|
|
|
(array)[2]='\0'
|
2007-11-20 03:44:19 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
char *crypt_make_salt (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
static char result[40];
|
2007-11-17 00:32:00 +05:30
|
|
|
int max_salt_len = 8;
|
2007-11-20 03:44:19 +05:30
|
|
|
char *method;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-11-20 04:04:48 +05:30
|
|
|
result[0] = '\0';
|
|
|
|
|
2007-10-07 17:17:01 +05:30
|
|
|
#ifndef USE_PAM
|
2007-11-20 03:44:19 +05:30
|
|
|
#ifdef ENCRYPTMETHOD_SELECT
|
|
|
|
if ((method = getdef_str ("ENCRYPT_METHOD")) == NULL) {
|
2007-10-07 17:17:01 +05:30
|
|
|
#endif
|
2007-11-20 03:44:19 +05:30
|
|
|
if (getdef_bool ("MD5_CRYPT_ENAB")) {
|
|
|
|
MAGNUM(result,'1');
|
|
|
|
max_salt_len = 11;
|
2007-11-20 04:04:48 +05:30
|
|
|
}
|
2007-11-20 03:44:19 +05:30
|
|
|
#ifdef ENCRYPTMETHOD_SELECT
|
|
|
|
} else {
|
|
|
|
if (!strncmp (method, "MD5", 3)) {
|
|
|
|
MAGNUM(result, '1');
|
|
|
|
max_salt_len = 11;
|
|
|
|
} else if (!strncmp (method, "SHA256", 6)) {
|
|
|
|
MAGNUM(result, '5');
|
|
|
|
max_salt_len = 11; /* XXX: should not be fixed */
|
|
|
|
} else if (!strncmp (method, "SHA512", 6)) {
|
|
|
|
MAGNUM(result, '6');
|
|
|
|
max_salt_len = 11; /* XXX: should not be fixed */
|
2007-11-20 04:04:48 +05:30
|
|
|
} else if (0 != strncmp (method, "DES", 3)) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_("Invalid ENCRYPT_METHOD value: '%s'.\n"
|
|
|
|
"Defaulting to DES.\n"),
|
|
|
|
method);
|
2007-11-20 03:44:19 +05:30
|
|
|
result[0] = '\0';
|
2007-11-20 04:04:48 +05:30
|
|
|
}
|
2007-11-20 03:44:19 +05:30
|
|
|
}
|
|
|
|
#endif /* ENCRYPTMETHOD_SELECT */
|
|
|
|
#endif /* USE_PAM */
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Generate 8 chars of salt, the old crypt() will use only first 2.
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
gettimeofday (&tv, (struct timezone *) 0);
|
|
|
|
strcat (result, l64a (tv.tv_usec));
|
|
|
|
strcat (result, l64a (tv.tv_sec + getpid () + clock ()));
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-11-17 00:32:00 +05:30
|
|
|
if (strlen (result) > max_salt_len) /* magic+salt */
|
|
|
|
result[max_salt_len] = '\0';
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|