A little more work on the LDAP Sync
[citadel.git] / citadel / ldap.c
1 /*
2  * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
3  * actually speak to the LDAP server.
4  *
5  * Copyright (c) 2011-2017 by the citadel.org development team.
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 int ctdl_require_ldap_version = 3;
17
18 #define _GNU_SOURCE             // Needed to suppress warning about vasprintf() when running on Linux/Linux
19 #include <stdio.h>
20 #include <libcitadel.h>
21 #include "citserver.h"
22 #include "citadel_ldap.h"
23 #include "ctdl_module.h"
24 #include "user_ops.h"
25 #include "internet_addressing.h"
26 #include "config.h"
27
28 #ifdef HAVE_LDAP
29 #define LDAP_DEPRECATED 1       // Suppress libldap's warning that we are using deprecated API calls
30 #include <ldap.h>
31
32
33 /*
34  * Utility function, supply a search result and get back the fullname (display name, common name, etc) from the first result
35  */
36 void derive_fullname_from_ldap_result(char *fullname, int fullname_size, LDAP *ldserver, LDAPMessage *search_result)
37 {
38         char **values;
39
40         if (fullname == NULL) return;
41
42         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
43                 values = ldap_get_values(ldserver, search_result, "displayName");
44                 if (values) {
45                         if (values[0]) {
46                                 if (fullname) safestrncpy(fullname, values[0], fullname_size);
47                                 syslog(LOG_DEBUG, "ldap: displayName = %s", values[0]);
48                         }
49                         ldap_value_free(values);
50                 }
51         }
52         else {
53                 values = ldap_get_values(ldserver, search_result, "cn");
54                 if (values) {
55                         if (values[0]) {
56                                 if (fullname) safestrncpy(fullname, values[0], fullname_size);
57                                 syslog(LOG_DEBUG, "ldap: cn = %s", values[0]);
58                         }
59                         ldap_value_free(values);
60                 }
61         }
62 }
63
64
65 /*
66  * Utility function, supply a search result and get back the uid from the first result
67  */
68 uid_t derive_uid_from_ldap(LDAP *ldserver, LDAPMessage *entry)
69 {
70         char **values;
71         uid_t uid = (-1);
72
73         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
74                 values = ldap_get_values(ldserver, entry, "objectGUID");        // AD schema: uid hashed from objectGUID
75                 if (values) {
76                         if (values[0]) {
77                                 uid = abs(HashLittle(values[0], strlen(values[0])));
78                         }
79                         ldap_value_free(values);
80                 }
81         }
82         else {
83                 values = ldap_get_values(ldserver, entry, "uidNumber");         // POSIX schema: uid = uidNumber
84                 if (values) {
85                         if (values[0]) {
86                                 uid = atoi(values[0]);
87                         }
88                         ldap_value_free(values);
89                 }
90         }
91
92         return(uid);
93 }
94
95
96 /*
97  * Wrapper function for ldap_initialize() that consistently fills in the correct fields
98  */
99 int ctdl_ldap_initialize(LDAP **ld) {
100
101         char server_url[256];
102         int ret;
103
104         snprintf(server_url, sizeof server_url, "ldap://%s:%d", CtdlGetConfigStr("c_ldap_host"), CtdlGetConfigInt("c_ldap_port"));
105         ret = ldap_initialize(ld, server_url);
106         if (ret != LDAP_SUCCESS) {
107                 syslog(LOG_ERR, "ldap: could not connect to %s : %m", server_url);
108                 *ld = NULL;
109                 return(errno);
110         }
111
112         return(ret);
113 }
114
115
116 /*
117  * Bind to the LDAP server and return a working handle
118  */
119 LDAP *ctdl_ldap_bind(void) {
120         LDAP *ldserver = NULL;
121         int i;
122
123         if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
124                 return(NULL);
125         }
126
127         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
128         ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
129
130         striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
131         striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
132         syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
133         i = ldap_simple_bind_s(ldserver,
134                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
135                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
136         );
137         if (i != LDAP_SUCCESS) {
138                 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
139                 return(NULL);
140         }
141
142         return(ldserver);
143 }
144
145
146 /*
147  * Look up a user in the directory to see if this is an account that can be authenticated
148  */
149 int CtdlTryUserLDAP(char *username,
150                 char *found_dn, int found_dn_size,
151                 char *fullname, int fullname_size,
152                 uid_t *uid)
153 {
154         LDAP *ldserver = NULL;
155         LDAPMessage *search_result = NULL;
156         LDAPMessage *entry = NULL;
157         char searchstring[1024];
158         struct timeval tv;
159         char *user_dn = NULL;
160
161         ldserver = ctdl_ldap_bind();
162         if (!ldserver) return(-1);
163
164         if (fullname) safestrncpy(fullname, username, fullname_size);
165         tv.tv_sec = 10;
166         tv.tv_usec = 0;
167
168         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
169                 snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
170         }
171         else {
172                 snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(cn=%s))", username);
173                 // snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
174         }
175
176         syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
177         (void) ldap_search_ext_s(
178                 ldserver,                                       /* ld                           */
179                 CtdlGetConfigStr("c_ldap_base_dn"),             /* base                         */
180                 LDAP_SCOPE_SUBTREE,                             /* scope                        */
181                 searchstring,                                   /* filter                       */
182                 NULL,                                           /* attrs (all attributes)       */
183                 0,                                              /* attrsonly (attrs + values)   */
184                 NULL,                                           /* serverctrls (none)           */
185                 NULL,                                           /* clientctrls (none)           */
186                 &tv,                                            /* timeout                      */
187                 1,                                              /* sizelimit (1 result max)     */
188                 &search_result                                  /* res                          */
189         );
190
191         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
192          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
193          */
194         if (search_result == NULL) {
195                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
196                 ldap_unbind(ldserver);
197                 return(2);
198         }
199
200         /* At this point we've got at least one result from our query.  If there are multiple
201          * results, we still only look at the first one.
202          */
203         entry = ldap_first_entry(ldserver, search_result);
204         if (entry) {
205
206                 user_dn = ldap_get_dn(ldserver, entry);
207                 if (user_dn) {
208                         syslog(LOG_DEBUG, "ldap: dn = %s", user_dn);
209                 }
210
211                 derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, search_result);
212                 *uid = derive_uid_from_ldap(ldserver, search_result);
213         }
214
215         /* free the results */
216         ldap_msgfree(search_result);
217
218         /* unbind so we can go back in as the authenticating user */
219         ldap_unbind(ldserver);
220
221         if (!user_dn) {
222                 syslog(LOG_DEBUG, "ldap: No such user was found.");
223                 return(4);
224         }
225
226         if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
227         ldap_memfree(user_dn);
228         return(0);
229 }
230
231
232 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
233 {
234         LDAP *ldserver = NULL;
235         int i = (-1);
236
237         if (IsEmptyStr(password)) {
238                 syslog(LOG_DEBUG, "ldap: empty passwords are not permitted");
239                 return(1);
240         }
241
242         syslog(LOG_DEBUG, "ldap: trying to bind as %s", user_dn);
243         i = ctdl_ldap_initialize(&ldserver);
244         if (i == LDAP_SUCCESS) {
245                 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
246                 i = ldap_simple_bind_s(ldserver, user_dn, password);
247                 if (i == LDAP_SUCCESS) {
248                         syslog(LOG_DEBUG, "ldap: bind succeeded");
249                 }
250                 else {
251                         syslog(LOG_DEBUG, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
252                 }
253                 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
254                 ldap_unbind(ldserver);
255         }
256
257         if (i == LDAP_SUCCESS) {
258                 return(0);
259         }
260
261         return(1);
262 }
263
264
265 //return !0 iff property changed.
266 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
267         int i;
268         char *oldval = "";
269         for(i=0;i<numvals;i++) {
270           oldval = vcard_get_prop(v,propname,0,i,0);
271           if (oldval == NULL) break;
272           if (strcmp(vals[i],oldval)) break;
273         }
274         if (i!=numvals) {
275                 syslog(LOG_DEBUG, "ldap: vcard property %s, element %d of %d changed from %s to %s\n", propname, i, numvals, oldval, vals[i]);
276                 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
277                 return 1;
278         }
279         return 0;
280 }
281
282
283 //return !0 iff property changed.
284 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
285         va_list args;
286         char *newvalue;
287         int changed_something;
288         va_start(args,newfmt);
289         if (-1==vasprintf(&newvalue,newfmt,args)) {
290                 syslog(LOG_ERR, "ldap: out of memory!");
291                 return 0;
292         }
293         changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
294         va_end(args);
295         free(newvalue);
296         return changed_something;
297 }
298
299
300 /*
301  * Learn LDAP attributes and stuff them into the vCard.
302  * Returns nonzero if we changed anything.
303  */
304 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
305 {
306         int changed_something = 0;
307         LDAP *ldserver = NULL;
308         struct timeval tv;
309         LDAPMessage *search_result = NULL;
310         LDAPMessage *entry = NULL;
311         char **givenName;
312         char **sn;
313         char **cn;
314         char **initials;
315         char **o;
316         char **street;
317         char **l;
318         char **st;
319         char **postalCode;
320         char **telephoneNumber;
321         char **mobile;
322         char **homePhone;
323         char **facsimileTelephoneNumber;
324         char **mail;
325         char **uid;
326         char **homeDirectory;
327         char **uidNumber;
328         char **loginShell;
329         char **gidNumber;
330         char **c;
331         char **title;
332         char **uuid;
333         char *attrs[] = { "*","+",NULL};
334
335         if (!ldap_dn) return(0);
336         if (!v) return(0);
337
338         ldserver = ctdl_ldap_bind();
339         if (!ldserver) return(-1);
340
341         tv.tv_sec = 10;
342         tv.tv_usec = 0;
343
344         syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
345         (void) ldap_search_ext_s(
346                 ldserver,                               // ld
347                 ldap_dn,                                // base
348                 LDAP_SCOPE_SUBTREE,                     // scope
349                 NULL,                                   // filter
350                 attrs,                                  // attrs (all attributes)
351                 0,                                      // attrsonly (attrs + values)
352                 NULL,                                   // serverctrls (none)
353                 NULL,                                   // clientctrls (none)
354                 &tv,                                    // timeout
355                 1,                                      // sizelimit (1 result max)
356                 &search_result                          // res
357         );
358         
359         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
360          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
361          */
362         if (search_result == NULL) {
363                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
364                 ldap_unbind(ldserver);
365                 return(0);
366         }
367
368         /* At this point we've got at least one result from our query.  If there are multiple
369          * results, we still only look at the first one.
370          */
371         entry = ldap_first_entry(ldserver, search_result);
372         if (entry) {
373                 syslog(LOG_DEBUG, "ldap: search got user details for vcard.");
374                 givenName=ldap_get_values(ldserver, search_result, "givenName");
375                 sn=ldap_get_values(ldserver, search_result, "sn");
376                 cn=ldap_get_values(ldserver, search_result, "cn");
377                 initials=ldap_get_values(ldserver, search_result, "initials");
378                 title=ldap_get_values(ldserver, search_result, "title");
379                 o=ldap_get_values(ldserver, search_result, "o");
380                 street=ldap_get_values(ldserver, search_result, "street");
381                 l=ldap_get_values(ldserver, search_result, "l");
382                 st=ldap_get_values(ldserver, search_result, "st");
383                 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
384                 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
385                 mobile=ldap_get_values(ldserver, search_result, "mobile");
386                 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
387                 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
388                 mail=ldap_get_values(ldserver, search_result, "mail");
389                 uid=ldap_get_values(ldserver, search_result, "uid");
390                 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
391                 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
392                 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
393                 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
394                 c=ldap_get_values(ldserver, search_result, "c");
395                 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
396
397                 if (street && l && st && postalCode && c) changed_something |= vcard_set_one_prop_iff_different(v,"adr",";;%s;%s;%s;%s;%s",street[0],l[0],st[0],postalCode[0],c[0]);
398                 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
399                 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
400                 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
401                 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
402                 if (givenName && sn) {
403                         if (initials) {
404                                 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
405                         }
406                         else {
407                                 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
408                         }
409                 }
410                 if (mail) {
411                         changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
412                 }
413                 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"X-uuid","%s",uuid[0]);
414                 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
415                 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
416                 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
417                 
418                 if (givenName) ldap_value_free(givenName);
419                 if (initials) ldap_value_free(initials);
420                 if (sn) ldap_value_free(sn);
421                 if (cn) ldap_value_free(cn);
422                 if (o) ldap_value_free(o);
423                 if (street) ldap_value_free(street);
424                 if (l) ldap_value_free(l);
425                 if (st) ldap_value_free(st);
426                 if (postalCode) ldap_value_free(postalCode);
427                 if (telephoneNumber) ldap_value_free(telephoneNumber);
428                 if (mobile) ldap_value_free(mobile);
429                 if (homePhone) ldap_value_free(homePhone);
430                 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
431                 if (mail) ldap_value_free(mail);
432                 if (uid) ldap_value_free(uid);
433                 if (homeDirectory) ldap_value_free(homeDirectory);
434                 if (uidNumber) ldap_value_free(uidNumber);
435                 if (loginShell) ldap_value_free(loginShell);
436                 if (gidNumber) ldap_value_free(gidNumber);
437                 if (c) ldap_value_free(c);
438                 if (title) ldap_value_free(title);
439                 if (uuid) ldap_value_free(uuid);
440         }
441         /* free the results */
442         ldap_msgfree(search_result);
443
444         /* unbind so we can go back in as the authenticating user */
445         ldap_unbind(ldserver);
446         return(changed_something);      /* tell the caller whether we made any changes */
447 }
448
449
450 /*
451  * Extract a user's Internet email addresses from LDAP.
452  * Returns zero if we got a valid set of addresses; nonzero for error.
453  */
454 int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
455 {
456         LDAP *ldserver = NULL;
457         struct timeval tv;
458         LDAPMessage *search_result = NULL;
459         LDAPMessage *entry = NULL;
460         char **mail;
461         char *attrs[] = { "*","+",NULL};
462
463         if (!ldap_dn) return(1);
464         if (!emailaddrs) return(1);
465
466         ldserver = ctdl_ldap_bind();
467         if (!ldserver) return(-1);
468
469         tv.tv_sec = 10;
470         tv.tv_usec = 0;
471
472         syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
473         (void) ldap_search_ext_s(
474                 ldserver,                               // ld
475                 ldap_dn,                                // base
476                 LDAP_SCOPE_SUBTREE,                     // scope
477                 NULL,                                   // filter
478                 attrs,                                  // attrs (all attributes)
479                 0,                                      // attrsonly (attrs + values)
480                 NULL,                                   // serverctrls (none)
481                 NULL,                                   // clientctrls (none)
482                 &tv,                                    // timeout
483                 1,                                      // sizelimit (1 result max)
484                 &search_result                          // res
485         );
486         
487         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
488          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
489          */
490         if (search_result == NULL) {
491                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
492                 ldap_unbind(ldserver);
493                 return(4);
494         }
495
496         /* At this point we've got at least one result from our query.  If there are multiple
497          * results, we still only look at the first one.
498          */
499         emailaddrs[0] = 0;      /* clear out any previous results */
500         entry = ldap_first_entry(ldserver, search_result);
501         if (entry) {
502                 syslog(LOG_DEBUG, "ldap: search got user details");
503                 mail = ldap_get_values(ldserver, search_result, "mail");
504
505                 if (mail) {
506                         int q;
507                         for (q=0; q<ldap_count_values(mail); ++q) {
508                                 if (IsDirectory(mail[q], 0)) {
509                                         if ((strlen(emailaddrs) + strlen(mail[q]) + 2) > 512) {
510                                                 syslog(LOG_ERR, "ldap: can't fit all email addresses into user record");
511                                         }
512                                         else {
513                                                 if (!IsEmptyStr(emailaddrs)) {
514                                                         strcat(emailaddrs, "|");
515                                                 }
516                                                 strcat(emailaddrs, mail[q]);
517                                         }
518                                 }
519                         }
520                 }
521         }
522
523         /* free the results */
524         ldap_msgfree(search_result);
525
526         /* unbind so we can go back in as the authenticating user */
527         ldap_unbind(ldserver);
528         return(0);
529 }
530
531
532 /*
533  * Scan LDAP for users and populate Citadel's user database with everyone
534  */
535 void CtdlSynchronizeUsersFromLDAP(void)
536 {
537         LDAP *ldserver = NULL;
538         LDAPMessage *search_result = NULL;
539         LDAPMessage *entry = NULL;
540         char *user_dn = NULL;
541         char searchstring[1024];
542         struct timeval tv;
543
544         if ((CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP) && (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP_AD)) {
545                 return;         // not running LDAP
546         }
547
548         syslog(LOG_INFO, "ldap: synchronizing Citadel user database from LDAP");
549
550         ldserver = ctdl_ldap_bind();
551         if (!ldserver) return;
552
553         tv.tv_sec = 10;
554         tv.tv_usec = 0;
555
556         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
557                         snprintf(searchstring, sizeof(searchstring), "(&(objectClass=user)(objectClass=person)(!(objectClass=computer)))");
558         } else {
559                         snprintf(searchstring, sizeof(searchstring), "(objectClass=inetOrgPerson)");
560         }
561
562         syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
563         (void) ldap_search_ext_s(
564                 ldserver,                                       // ld
565                 CtdlGetConfigStr("c_ldap_base_dn"),             // base
566                 LDAP_SCOPE_SUBTREE,                             // scope
567                 searchstring,                                   // filter
568                 NULL,                                           // attrs (all attributes)
569                 0,                                              // attrsonly (attrs + values)
570                 NULL,                                           // serverctrls (none)
571                 NULL,                                           // clientctrls (none)
572                 &tv,                                            // timeout
573                 INT_MAX,                                        // sizelimit (max)
574                 &search_result                                  // result
575         );
576
577         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
578          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
579          */
580         if (search_result == NULL) {
581                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
582                 ldap_unbind(ldserver);
583                 return;
584         }
585
586         syslog(LOG_DEBUG, "ldap: %d entries returned", ldap_count_entries(ldserver, search_result));
587         entry = ldap_first_entry(ldserver, search_result);
588         while (entry) {
589
590                 user_dn = ldap_get_dn(ldserver, entry);
591                 if (user_dn) {
592                         syslog(LOG_DEBUG, "ldap: found %s", user_dn);
593
594                         int fullname_size = 256;
595                         char fullname[256] = { 0 } ;
596                         uid_t uid = (-1);
597
598                         derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, entry);
599                         uid = derive_uid_from_ldap(ldserver, entry);
600                         syslog(LOG_DEBUG, "\033[33mldap: display name: <%s> , uid = <%d>\033[0m", fullname, uid);
601
602                         // FIXME now create or update the user
603                         int i;
604                         struct ctdluser usbuf;
605
606                         i = getuserbyuid(&usbuf, uid);
607                         if (i == 0) {
608                                 syslog(LOG_DEBUG, "\033[32m...and that user EXISTZ0RS!!!\033[0m");
609                         }
610                         else {
611                                 syslog(LOG_DEBUG, "\033[31m...and that user D0EZ N0T EXISTZ0R!!\033[0m");
612                         }
613
614
615                 }
616
617                 entry = ldap_next_entry(ldserver, entry);
618         }
619
620         /* free the results */
621         ldap_msgfree(search_result);
622
623         /* unbind so we can go back in as the authenticating user */
624         ldap_unbind(ldserver);
625 }
626
627 #endif /* HAVE_LDAP */