2 ** Copyright 2001-2002 Double Precision, Inc. See COPYING for
3 ** distribution information.
7 #include "courier_auth_config.h"
20 static const char rcsid
[]="$Id: cryptpassword.c,v 1.9 2007/10/07 18:33:22 mrsam Exp $";
23 #if NEED_CRYPT_PROTOTYPE
24 extern char *crypt(const char *, const char *);
33 #include "sha1/sha1.h"
36 static const char crypt_salt
[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
38 static const char *crypt_hash(const char *pw
)
43 gettimeofday(&tv
, NULL
);
45 tv
.tv_sec
|= tv
.tv_usec
;
46 tv
.tv_sec
^= getpid();
48 salt
[0]=crypt_salt
[ tv
.tv_sec
% 64 ];
52 salt
[1]=crypt_salt
[ tv
.tv_sec
% 64 ];
55 return (crypt(pw
, salt
));
59 static const char *crypt_md5_wrapper(const char *pw
)
65 gettimeofday(&tv
, NULL
);
67 tv
.tv_sec
|= tv
.tv_usec
;
68 tv
.tv_sec
^= getpid();
74 salt
[i
]=crypt_salt
[ tv
.tv_sec
% 64 ];
80 return (md5_crypt(pw
, salt
));
84 char *authcryptpasswd(const char *password
, const char *encryption_hint
)
86 const char *(*hash_func
)(const char *)=0;
93 if (!encryption_hint
|| strncmp(encryption_hint
, "$1$", 3) == 0)
96 hash_func
=crypt_md5_wrapper
;
99 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5}", 5) == 0)
101 hash_func
= &md5_hash_courier
;
105 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{MD5RAW}", 5)
108 hash_func
= &md5_hash_raw
;
114 if (!encryption_hint
|| strncasecmp(encryption_hint
, "{SHA}", 5) == 0)
116 hash_func
= &sha1_hash
;
120 if (!encryption_hint
||
121 strncasecmp(encryption_hint
, "{SHA256}", 8) == 0)
123 hash_func
= &sha256_hash
;
130 hash_func
= &crypt_hash
;
134 p
= (*hash_func
)(password
);
135 if (!p
|| (pp
=malloc(strlen(pfix
)+strlen(p
)+1)) == 0)
138 return (strcat(strcpy(pp
, pfix
), p
));