]> code.citadel.org Git - citadel.git/blob - citadel/serv_ldap.c
* Reworked vCard etc. functions for addition of new vCard data to LDAP
[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 #include "serv_ldap.h"
44 #include "vcard.h"
45
46 #ifdef HAVE_LDAP
47
48 #include <ldap.h>
49
50 LDAP *dirserver = NULL;
51
52 /*
53  * LDAP connector cleanup function
54  */
55 void serv_ldap_cleanup(void)
56 {
57         if (!dirserver) return;
58
59         lprintf(7, "Unbinding from directory server\n");
60         ldap_unbind(dirserver);
61         dirserver = NULL;
62 }
63
64 #endif                          /* HAVE_LDAP */
65
66
67 void CtdlConnectToLdap(void) {
68         int i;
69         int ldap_version = 3;
70
71         lprintf(7, "Connecting to LDAP server %s:%d...\n",
72                 config.c_ldap_host, config.c_ldap_port);
73
74         dirserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
75         if (dirserver == NULL) {
76                 lprintf(3, "Could not connect to %s:%d : %s\n",
77                         config.c_ldap_host,
78                         config.c_ldap_port,
79                         strerror(errno));
80                 return;
81         }
82
83         ldap_set_option(dirserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
84
85         lprintf(7, "Binding to %s\n", config.c_ldap_bind_dn);
86
87         i = ldap_simple_bind_s(dirserver,
88                                 config.c_ldap_bind_dn,
89                                 config.c_ldap_bind_pw
90         );
91         if (i != LDAP_SUCCESS) {
92                 lprintf(3, "Cannot bind: %s (%d)\n", ldap_err2string(i), i);
93                 dirserver = NULL;       /* FIXME disconnect from ldap */
94         }
95 }
96
97
98
99
100 /*
101  * Write (add, or change if already exists) a directory entry to the
102  * LDAP server, based on the information supplied in a vCard.
103  */
104 void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
105         struct vCard *v = NULL;
106
107         char this_dn[SIZ];
108
109         if (msg == NULL) return;
110         if (msg->cm_fields['M'] == NULL) return;
111         if (msg->cm_fields['A'] == NULL) return;
112         if (msg->cm_fields['N'] == NULL) return;
113
114         sprintf(this_dn, "cn=%s,ou=%s,%s",
115                 msg->cm_fields['A'],
116                 msg->cm_fields['N'],
117                 config.c_ldap_base_dn
118         );
119
120         lprintf(9, "this_dn: <%s>\n", this_dn);
121
122         v = vcard_load(msg->cm_fields['M']);
123
124         vcard_free(v);
125 }
126
127
128
129
130 /*
131  * Initialize the LDAP connector module ... or don't, if we don't have LDAP.
132  */
133 char *serv_ldap_init(void)
134 {
135 #ifdef HAVE_LDAP
136         CtdlRegisterCleanupHook(serv_ldap_cleanup);
137
138         if (strlen(config.c_ldap_host) > 0) {
139                 CtdlConnectToLdap();
140         }
141
142 #endif                          /* HAVE_LDAP */
143         return "$Id$";
144 }