hcoop release
[hcoop/debian/courier-authlib.git] / authldap.c
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 <stdio.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include "auth.h"
16 #include "authldap.h"
17 #include "courierauth.h"
18 #include "courierauthstaticlist.h"
19 #include "courierauthdebug.h"
20 #include "libhmac/hmac.h"
21
22 static int auth_ldap_login(const char *service, char *authdata,
23 int (*callback_func)(struct authinfo *, void *),
24 void *callback_arg)
25 {
26 const char *user, *pass;
27
28 if ((user=strtok(authdata, "\n")) == 0 ||
29 (pass=strtok(0, "\n")) == 0)
30 {
31 DPRINTF("incomplete authentication data");
32 errno=EPERM;
33 return (-1);
34 }
35
36 return authldapcommon(service, user, pass, callback_func,
37 callback_arg);
38 }
39
40 static int auth_ldap_cram(const char *service,
41 const char *authtype, char *authdata,
42 int (*callback_func)(struct authinfo *, void *),
43 void *callback_arg)
44 {
45 struct cram_callback_info cci;
46
47 if (auth_get_cram(authtype, authdata, &cci))
48 return (-1);
49
50 cci.callback_func=callback_func;
51 cci.callback_arg=callback_arg;
52
53 return authldapcommon(service, cci.user, 0, &auth_cram_callback, &cci);
54 }
55
56 int auth_ldap(const char *service, const char *authtype, char *authdata,
57 int (*callback_func)(struct authinfo *, void *),
58 void *callback_arg)
59 {
60 if (strcmp(authtype, AUTHTYPE_LOGIN) == 0)
61 return (auth_ldap_login(service, authdata,
62 callback_func, callback_arg));
63
64 return (auth_ldap_cram(service, authtype, authdata,
65 callback_func, callback_arg));
66 }
67
68
69 extern int auth_ldap_pre(const char *userid, const char *service,
70 int (*callback)(struct authinfo *, void *),
71 void *arg);
72
73 static struct authstaticinfo authldap_info={
74 "authldap",
75 auth_ldap,
76 auth_ldap_pre,
77 authldapclose,
78 auth_ldap_changepw,
79 authldapclose,
80 auth_ldap_enumerate};
81
82
83 struct authstaticinfo *courier_authldap_init()
84 {
85 return &authldap_info;
86 }