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"
27 #if NEED_CRYPT_PROTOTYPE
28 extern char *crypt(const char *, const char *);
32 static const char crypt_salt
[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
34 static const char *crypt_hash(const char *pw
)
36 random128binbuf randbuf
;
39 random128_binary(&randbuf
);
41 salt
[0]=crypt_salt
[ randbuf
[0] % 64 ];
42 salt
[1]=crypt_salt
[ randbuf
[1] % 64 ];
45 return (crypt(pw
, salt
));
48 static const char *ssha_hash_int(const char *pw
)
50 random128binbuf randbuf
;
52 random128_binary(&randbuf
);
54 return ssha_hash(pw
, randbuf
);
57 static const char *crypt_md5_wrapper(const char *pw
)
63 gettimeofday(&tv
, NULL
);
65 tv
.tv_sec
|= tv
.tv_usec
;
66 tv
.tv_sec
^= getpid();
72 salt
[i
]=crypt_salt
[ tv
.tv_sec
% 64 ];
78 return (md5_crypt(pw
, salt
));
81 char *authcryptpasswd(const char *password
, const char *encryption_hint
)
83 const char *(*hash_func
)(const char *)=0;
88 if (!encryption_hint
|| strncmp(encryption_hint
, "$1$", 3) == 0)
91 hash_func
=crypt_md5_wrapper
;
94 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5}", 5) == 0)
96 hash_func
= &md5_hash_courier
;
100 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5RAW}", 5)
103 hash_func
= &md5_hash_raw
;
107 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{SHA}", 5) == 0)
109 hash_func
= &sha1_hash
;
113 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{SSHA}", 6) == 0)
115 hash_func
= &ssha_hash_int
;
119 if (!encryption_hint
||
120 strncasecmp(encryption_hint
, "{SHA256}", 8) == 0)
122 hash_func
= &sha256_hash
;
126 if (!encryption_hint
||
127 strncasecmp(encryption_hint
, "{SHA512}", 8) == 0)
129 hash_func
= &sha512_hash
;
135 hash_func
= &crypt_hash
;
139 p
= (*hash_func
)(password
);
140 if (!p
|| (pp
=malloc(strlen(pfix
)+strlen(p
)+1)) == 0)
143 return (strcat(strcpy(pp
, pfix
), p
));