| 1 | /* |
| 2 | ** Copyright 2001-2008 Double Precision, Inc. See COPYING for |
| 3 | ** distribution information. |
| 4 | */ |
| 5 | |
| 6 | #if HAVE_CONFIG_H |
| 7 | #include "courier_auth_config.h" |
| 8 | #endif |
| 9 | #include <string.h> |
| 10 | #if HAVE_UNISTD_H |
| 11 | #include <unistd.h> |
| 12 | #endif |
| 13 | #include <stdlib.h> |
| 14 | #if HAVE_CRYPT_H |
| 15 | #include <crypt.h> |
| 16 | #endif |
| 17 | #include "auth.h" |
| 18 | #include <sys/time.h> |
| 19 | #include "md5/md5.h" |
| 20 | #include "sha1/sha1.h" |
| 21 | |
| 22 | static const char rcsid[]="$Id: cryptpassword.c,v 1.10 2008/07/10 02:43:55 mrsam Exp $"; |
| 23 | |
| 24 | #if HAVE_CRYPT |
| 25 | #if NEED_CRYPT_PROTOTYPE |
| 26 | extern char *crypt(const char *, const char *); |
| 27 | #endif |
| 28 | #endif |
| 29 | |
| 30 | static const char crypt_salt[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./"; |
| 31 | |
| 32 | static const char *crypt_hash(const char *pw) |
| 33 | { |
| 34 | struct timeval tv; |
| 35 | char salt[3]; |
| 36 | |
| 37 | gettimeofday(&tv, NULL); |
| 38 | |
| 39 | tv.tv_sec |= tv.tv_usec; |
| 40 | tv.tv_sec ^= getpid(); |
| 41 | |
| 42 | salt[0]=crypt_salt[ tv.tv_sec % 64 ]; |
| 43 | |
| 44 | tv.tv_sec /= 64; |
| 45 | |
| 46 | salt[1]=crypt_salt[ tv.tv_sec % 64 ]; |
| 47 | salt[2]=0; |
| 48 | |
| 49 | return (crypt(pw, salt)); |
| 50 | } |
| 51 | |
| 52 | static const char *crypt_md5_wrapper(const char *pw) |
| 53 | { |
| 54 | struct timeval tv; |
| 55 | char salt[10]; |
| 56 | int i; |
| 57 | |
| 58 | gettimeofday(&tv, NULL); |
| 59 | |
| 60 | tv.tv_sec |= tv.tv_usec; |
| 61 | tv.tv_sec ^= getpid(); |
| 62 | |
| 63 | strcpy(salt, "$1$"); |
| 64 | |
| 65 | for (i=3; i<8; i++) |
| 66 | { |
| 67 | salt[i]=crypt_salt[ tv.tv_sec % 64 ]; |
| 68 | tv.tv_sec /= 64; |
| 69 | } |
| 70 | |
| 71 | strcpy(salt+i, "$"); |
| 72 | |
| 73 | return (md5_crypt(pw, salt)); |
| 74 | } |
| 75 | |
| 76 | char *authcryptpasswd(const char *password, const char *encryption_hint) |
| 77 | { |
| 78 | const char *(*hash_func)(const char *)=0; |
| 79 | const char *pfix=0; |
| 80 | const char *p; |
| 81 | char *pp; |
| 82 | |
| 83 | if (!encryption_hint || strncmp(encryption_hint, "$1$", 3) == 0) |
| 84 | { |
| 85 | pfix=""; |
| 86 | hash_func=crypt_md5_wrapper; |
| 87 | } |
| 88 | |
| 89 | if (!encryption_hint || strncasecmp(encryption_hint, "{MD5}", 5) == 0) |
| 90 | { |
| 91 | hash_func= &md5_hash_courier; |
| 92 | pfix="{MD5}"; |
| 93 | } |
| 94 | |
| 95 | if (!encryption_hint || strncasecmp(encryption_hint, "{MD5RAW}", 5) |
| 96 | == 0) |
| 97 | { |
| 98 | hash_func= &md5_hash_raw; |
| 99 | pfix="{MD5RAW}"; |
| 100 | } |
| 101 | |
| 102 | if (!encryption_hint || strncasecmp(encryption_hint, "{SHA}", 5) == 0) |
| 103 | { |
| 104 | hash_func= &sha1_hash; |
| 105 | pfix="{SHA}"; |
| 106 | } |
| 107 | |
| 108 | if (!encryption_hint || |
| 109 | strncasecmp(encryption_hint, "{SHA256}", 8) == 0) |
| 110 | { |
| 111 | hash_func= &sha256_hash; |
| 112 | pfix="{SHA256}"; |
| 113 | } |
| 114 | |
| 115 | if (!hash_func) |
| 116 | { |
| 117 | hash_func= &crypt_hash; |
| 118 | pfix="{CRYPT}"; |
| 119 | } |
| 120 | |
| 121 | p= (*hash_func)(password); |
| 122 | if (!p || (pp=malloc(strlen(pfix)+strlen(p)+1)) == 0) |
| 123 | return (0); |
| 124 | |
| 125 | return (strcat(strcpy(pp, pfix), p)); |
| 126 | } |