Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / checkpassword.c
CommitLineData
d9898ee8 1/*
ac40fd9e 2** Copyright 1998 - 2008 Double Precision, Inc. See COPYING for
d9898ee8 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
8d138742 19static const char rcsid[]="$Id: checkpassword.c,v 1.19 2008/12/18 12:08:25 mrsam Exp $";
d9898ee8 20
21#if HAVE_CRYPT
22#if NEED_CRYPT_PROTOTYPE
23extern char *crypt(const char *, const char *);
24#endif
25#endif
26
d9898ee8 27extern int authcheckpasswordmd5(const char *, const char *);
d9898ee8 28extern int authcheckpasswordsha1(const char *, const char *);
d9898ee8 29
30static int do_authcheckpassword(const char *password, const char *encrypted_password)
31{
d9898ee8 32 if (strncmp(encrypted_password, "$1$", 3) == 0
dd184caf 33 || strncasecmp(encrypted_password, "{MD5}", 5) == 0
34 || strncasecmp(encrypted_password, "{MD5RAW}", 8) == 0
35 )
d9898ee8 36 return (authcheckpasswordmd5(password, encrypted_password));
d9898ee8 37
d9898ee8 38 if (strncasecmp(encrypted_password, "{SHA}", 5) == 0 ||
8d138742
CE
39 strncasecmp(encrypted_password, "{SHA256}", 8) == 0 ||
40 strncasecmp(encrypted_password, "{SHA512}", 8) == 0 ||
41 strncasecmp(encrypted_password, "{SSHA}", 6) == 0)
d9898ee8 42 return (authcheckpasswordsha1(password, encrypted_password));
d9898ee8 43
8d138742 44
d9898ee8 45#if HAVE_CRYPT
46 if (strncasecmp(encrypted_password, "{CRYPT}", 7) == 0)
47 encrypted_password += 7;
48#endif
49
50 return (
51#if HAVE_CRYPT
52 strcmp(encrypted_password,
53 crypt(password, encrypted_password))
54#else
55 strcmp(encrypted_password, password)
56#endif
57 );
58}
59
60int authcheckpassword(const char *password, const char *encrypted_password)
61{
62int rc;
63
64 rc=do_authcheckpassword(password, encrypted_password);
65 if (rc == 0)
66 {
67 DPRINTF("password matches successfully");
68 }
69 else if (courier_authdebug_login_level >= 2)
70 {
71 DPRINTF("supplied password '%s' does not match encrypted password '%s'",
72 password, encrypted_password);
73 }
74 else
75 {
76 DPRINTF("supplied password does not match encrypted password");
77 }
78 return rc;
79}