| 1 | /* |
| 2 | ** Copyright 1998 - 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 | #if HAVE_CRYPT_H |
| 14 | #include <crypt.h> |
| 15 | #endif |
| 16 | #include "auth.h" |
| 17 | #include "courierauthdebug.h" |
| 18 | |
| 19 | |
| 20 | #if HAVE_CRYPT |
| 21 | #if NEED_CRYPT_PROTOTYPE |
| 22 | extern char *crypt(const char *, const char *); |
| 23 | #endif |
| 24 | #endif |
| 25 | |
| 26 | extern int authcheckpasswordmd5(const char *, const char *); |
| 27 | extern int authcheckpasswordsha1(const char *, const char *); |
| 28 | |
| 29 | static int do_authcheckpassword(const char *password, const char *encrypted_password) |
| 30 | { |
| 31 | if (strncmp(encrypted_password, "$1$", 3) == 0 |
| 32 | || strncasecmp(encrypted_password, "{MD5}", 5) == 0 |
| 33 | || strncasecmp(encrypted_password, "{MD5RAW}", 8) == 0 |
| 34 | ) |
| 35 | return (authcheckpasswordmd5(password, encrypted_password)); |
| 36 | |
| 37 | if (strncasecmp(encrypted_password, "{SHA}", 5) == 0 || |
| 38 | strncasecmp(encrypted_password, "{SHA256}", 8) == 0 || |
| 39 | strncasecmp(encrypted_password, "{SHA512}", 8) == 0 || |
| 40 | strncasecmp(encrypted_password, "{SSHA}", 6) == 0) |
| 41 | return (authcheckpasswordsha1(password, encrypted_password)); |
| 42 | |
| 43 | |
| 44 | #if HAVE_CRYPT |
| 45 | if (strncasecmp(encrypted_password, "{CRYPT}", 7) == 0) |
| 46 | encrypted_password += 7; |
| 47 | #endif |
| 48 | |
| 49 | return ( |
| 50 | #if HAVE_CRYPT |
| 51 | strcmp(encrypted_password, |
| 52 | crypt(password, encrypted_password)) |
| 53 | #else |
| 54 | strcmp(encrypted_password, password) |
| 55 | #endif |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | int authcheckpassword(const char *password, const char *encrypted_password) |
| 60 | { |
| 61 | int rc; |
| 62 | |
| 63 | rc=do_authcheckpassword(password, encrypted_password); |
| 64 | if (rc == 0) |
| 65 | { |
| 66 | DPRINTF("password matches successfully"); |
| 67 | } |
| 68 | else if (courier_authdebug_login_level >= 2) |
| 69 | { |
| 70 | DPRINTF("supplied password '%s' does not match encrypted password '%s'", |
| 71 | password, encrypted_password); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | DPRINTF("supplied password does not match encrypted password"); |
| 76 | } |
| 77 | return rc; |
| 78 | } |