* Added "Base DN" "Bind DN" and "Bind DN password" config options
[citadel.git] / citadel / serv_ldap.c
1 /*
2  * $Id$
3  *
4  * A module which implements the LDAP connector for Citadel.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <limits.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "sysdep_decls.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "serv_extensions.h"
39 #include "room_ops.h"
40 #include "policy.h"
41 #include "database.h"
42 #include "msgbase.h"
43
44 #ifdef HAVE_LDAP
45
46 #include <ldap.h>
47
48 LDAP *dirserver = NULL;
49
50 /*
51  * LDAP connector cleanup function
52  */
53 void serv_ldap_cleanup(void)
54 {
55         if (!dirserver) return;
56
57         lprintf(7, "Unbinding from directory server\n");
58         ldap_unbind(dirserver);
59         dirserver = NULL;
60 }
61
62 #endif                          /* HAVE_LDAP */
63
64
65 void CtdlConnectToLdap(void) {
66         int i;
67         int ldap_version = 3;
68
69         lprintf(7, "Connecting to LDAP server %s:%d...\n",
70                 config.c_ldap_host, config.c_ldap_port);
71
72         dirserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
73         if (dirserver == NULL) {
74                 lprintf(3, "Could not connect to %s:%d : %s\n",
75                         config.c_ldap_host,
76                         config.c_ldap_port,
77                         strerror(errno));
78                 return;
79         }
80
81         ldap_set_option(dirserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
82
83         lprintf(7, "Binding to %s\n", config.c_ldap_bind_dn);
84
85         i = ldap_simple_bind_s(dirserver,
86                                 config.c_ldap_bind_dn,
87                                 config.c_ldap_bind_pw
88         );
89         if (i != LDAP_SUCCESS) {
90                 lprintf(3, "Cannot bind: %s (%d)\n", ldap_err2string(i), i);
91                 dirserver = NULL;       /* FIXME disconnect from ldap */
92         }
93 }
94
95
96 /*
97  * Initialize the LDAP connector module ... or don't, if we don't have LDAP.
98  */
99 char *serv_ldap_init(void)
100 {
101 #ifdef HAVE_LDAP
102         CtdlRegisterCleanupHook(serv_ldap_cleanup);
103
104         if (strlen(config.c_ldap_host) > 0) {
105                 CtdlConnectToLdap();
106         }
107
108 #endif                          /* HAVE_LDAP */
109         return "$Id$";
110 }