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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-10-07 17:17:01 +05:30
|
|
|
#ident "$Id: salt.c,v 1.10 2005/08/31 17:24:58 kloczek Exp $"
|
|
|
|
|
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"
|
|
|
|
/*
|
|
|
|
* 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-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];
|
|
|
|
|
|
|
|
result[0] = '\0';
|
2007-10-07 17:17:01 +05:30
|
|
|
#ifndef USE_PAM
|
2007-10-07 17:15:23 +05:30
|
|
|
if (getdef_bool ("MD5_CRYPT_ENAB")) {
|
|
|
|
strcpy (result, "$1$"); /* magic for the new MD5 crypt() */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:17:01 +05:30
|
|
|
#endif
|
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-10-07 17:15:23 +05:30
|
|
|
if (strlen (result) > 3 + 8) /* magic+salt */
|
2007-10-07 17:14:02 +05:30
|
|
|
result[11] = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|