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 *ssha_hash_int(const char *pw
)
36 random128binbuf randbuf
;
38 random128_binary(&randbuf
);
40 return ssha_hash(pw
, randbuf
);
43 static const char *crypt_md5_wrapper(const char *pw
)
49 gettimeofday(&tv
, NULL
);
51 tv
.tv_sec
|= tv
.tv_usec
;
52 tv
.tv_sec
^= getpid();
58 salt
[i
]=crypt_salt
[ tv
.tv_sec
% 64 ];
64 return (md5_crypt(pw
, salt
));
67 char *authcryptpasswd(const char *password
, const char *encryption_hint
)
69 const char *(*hash_func
)(const char *)=0;
74 if (!encryption_hint
|| strncmp(encryption_hint
, "$1$", 3) == 0)
77 hash_func
=crypt_md5_wrapper
;
80 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5}", 5) == 0)
82 hash_func
= &md5_hash_courier
;
86 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5RAW}", 5)
89 hash_func
= &md5_hash_raw
;
93 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{SHA}", 5) == 0)
95 hash_func
= &sha1_hash
;
99 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{SSHA}", 6) == 0)
101 hash_func
= &ssha_hash_int
;
105 if (!encryption_hint
||
106 strncasecmp(encryption_hint
, "{SHA256}", 8) == 0)
108 hash_func
= &sha256_hash
;
112 if (!encryption_hint
||
113 strncasecmp(encryption_hint
, "{SHA512}", 8) == 0)
115 hash_func
= &sha512_hash
;
121 hash_func
= &ssha_hash_int
;
125 p
= (*hash_func
)(password
);
126 if (!p
|| (pp
=malloc(strlen(pfix
)+strlen(p
)+1)) == 0)
129 return (strcat(strcpy(pp
, pfix
), p
));