2 ** Copyright 2001-2008 Double Precision, Inc. See COPYING for
3 ** distribution information.
7 #include "courier_auth_config.h"
22 #include "sha1/sha1.h"
23 #include "random128/random128.h"
25 static const char rcsid
[]="$Id: cryptpassword.c,v 1.13 2008/12/25 14:52:38 mrsam Exp $";
28 #if NEED_CRYPT_PROTOTYPE
29 extern char *crypt(const char *, const char *);
33 static const char crypt_salt
[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
35 static const char *crypt_hash(const char *pw
)
37 random128binbuf randbuf
;
40 random128_binary(&randbuf
);
42 salt
[0]=crypt_salt
[ randbuf
[0] % 64 ];
43 salt
[1]=crypt_salt
[ randbuf
[1] % 64 ];
46 return (crypt(pw
, salt
));
49 static const char *ssha_hash_int(const char *pw
)
51 random128binbuf randbuf
;
53 random128_binary(&randbuf
);
55 return ssha_hash(pw
, randbuf
);
58 static const char *crypt_md5_wrapper(const char *pw
)
64 gettimeofday(&tv
, NULL
);
66 tv
.tv_sec
|= tv
.tv_usec
;
67 tv
.tv_sec
^= getpid();
73 salt
[i
]=crypt_salt
[ tv
.tv_sec
% 64 ];
79 return (md5_crypt(pw
, salt
));
82 char *authcryptpasswd(const char *password
, const char *encryption_hint
)
84 const char *(*hash_func
)(const char *)=0;
89 if (!encryption_hint
|| strncmp(encryption_hint
, "$1$", 3) == 0)
92 hash_func
=crypt_md5_wrapper
;
95 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5}", 5) == 0)
97 hash_func
= &md5_hash_courier
;
101 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5RAW}", 5)
104 hash_func
= &md5_hash_raw
;
108 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{SHA}", 5) == 0)
110 hash_func
= &sha1_hash
;
114 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{SSHA}", 6) == 0)
116 hash_func
= &ssha_hash_int
;
120 if (!encryption_hint
||
121 strncasecmp(encryption_hint
, "{SHA256}", 8) == 0)
123 hash_func
= &sha256_hash
;
127 if (!encryption_hint
||
128 strncasecmp(encryption_hint
, "{SHA512}", 8) == 0)
130 hash_func
= &sha512_hash
;
136 hash_func
= &crypt_hash
;
140 p
= (*hash_func
)(password
);
141 if (!p
|| (pp
=malloc(strlen(pfix
)+strlen(p
)+1)) == 0)
144 return (strcat(strcpy(pp
, pfix
), p
));