Merge branch 'upstream' into debian
[hcoop/debian/courier-authlib.git] / checkpassword.c
1 /*
2 ** Copyright 1998 - 1999 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 static const char rcsid[]="$Id: checkpassword.c,v 1.16 2007/10/07 02:50:45 mrsam Exp $";
20
21 #if HAVE_CRYPT
22 #if NEED_CRYPT_PROTOTYPE
23 extern char *crypt(const char *, const char *);
24 #endif
25 #endif
26
27 #if HAVE_MD5LIB
28 extern int authcheckpasswordmd5(const char *, const char *);
29 #endif
30
31 #if HAVE_SHA1LIB
32 extern int authcheckpasswordsha1(const char *, const char *);
33 #endif
34
35 static int do_authcheckpassword(const char *password, const char *encrypted_password)
36 {
37 #if HAVE_MD5LIB
38 if (strncmp(encrypted_password, "$1$", 3) == 0
39 || strncasecmp(encrypted_password, "{MD5}", 5) == 0
40 || strncasecmp(encrypted_password, "{MD5RAW}", 8) == 0
41 )
42 return (authcheckpasswordmd5(password, encrypted_password));
43 #endif
44
45 #if HAVE_SHA1LIB
46 if (strncasecmp(encrypted_password, "{SHA}", 5) == 0 ||
47 strncasecmp(encrypted_password, "{SHA256}", 8) == 0
48 )
49 return (authcheckpasswordsha1(password, encrypted_password));
50 #endif
51
52 #if HAVE_CRYPT
53 if (strncasecmp(encrypted_password, "{CRYPT}", 7) == 0)
54 encrypted_password += 7;
55 #endif
56
57 return (
58 #if HAVE_CRYPT
59 strcmp(encrypted_password,
60 crypt(password, encrypted_password))
61 #else
62 strcmp(encrypted_password, password)
63 #endif
64 );
65 }
66
67 int authcheckpassword(const char *password, const char *encrypted_password)
68 {
69 int rc;
70
71 rc=do_authcheckpassword(password, encrypted_password);
72 if (rc == 0)
73 {
74 DPRINTF("password matches successfully");
75 }
76 else if (courier_authdebug_login_level >= 2)
77 {
78 DPRINTF("supplied password '%s' does not match encrypted password '%s'",
79 password, encrypted_password);
80 }
81 else
82 {
83 DPRINTF("supplied password does not match encrypted password");
84 }
85 return rc;
86 }