compilation warnings due to missing (void) in ldap_close_connection.
[ira/wip.git] / source3 / passdb / ldap.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0.
4    LDAP protocol helper functions for SAMBA
5    Copyright (C) Jean François Micouleau 1998
6    Copyright (C) Matthew Chapman 1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #include "includes.h"
25
26 #ifdef WITH_LDAP
27
28 #include <lber.h>
29 #include <ldap.h>
30
31 extern int DEBUGLEVEL;
32
33 /* Internal state */
34 LDAP *ldap_struct;
35 LDAPMessage *ldap_results;
36 LDAPMessage *ldap_entry;
37
38 /* LDAP password */
39 static pstring ldap_secret;
40
41
42 /*******************************************************************
43   Open connections to the LDAP server.
44  ******************************************************************/    
45
46 BOOL ldap_open_connection(BOOL modify)
47 {
48         int err;
49
50         if (!(ldap_struct = ldap_open(lp_ldap_server(), lp_ldap_port()))) {
51                 DEBUG(0, ("open: %s\n", strerror(errno)));
52                 return (False);
53         }
54
55         err = ldap_simple_bind_s(ldap_struct, lp_ldap_bind_as(), ldap_secret);
56         if (err != LDAP_SUCCESS) {
57                 DEBUG(0, ("bind: %s\n", ldap_err2string(err)));
58                 return (False);
59         }
60
61         DEBUG(2,("Connected to LDAP server\n"));
62         return (True);
63 }
64
65 /*******************************************************************
66   close connections to the LDAP server.
67  ******************************************************************/    
68
69 void ldap_close_connection(void)
70 {
71         if(!ldap_struct)
72                 return;
73
74         if(ldap_results) {
75                 ldap_msgfree(ldap_results);
76                 ldap_results = NULL; }
77
78         ldap_unbind(ldap_struct);
79         ldap_struct = NULL;
80         
81         DEBUG(2,("Connection closed\n"));
82 }
83
84
85 /*******************************************************************
86   Search the directory using a given filter.
87  ******************************************************************/    
88
89 BOOL ldap_search_for(char *filter)
90 {
91         int err;
92
93         DEBUG(2,("Searching in [%s] for [%s]\n", lp_ldap_suffix(), filter));
94
95         err = ldap_search_s(ldap_struct, lp_ldap_suffix(), LDAP_SCOPE_ONELEVEL,
96                           filter, NULL, 0, &ldap_results);
97
98         if(err != LDAP_SUCCESS) {
99                 DEBUG(0, ("search: %s\n", ldap_err2string(err)));
100         }
101
102         DEBUG(2, ("%d matching entries found\n",
103                   ldap_count_entries(ldap_struct, ldap_results)));
104
105         ldap_entry = ldap_first_entry(ldap_struct, ldap_results);
106         return (True);
107 }
108
109 BOOL ldap_search_by_name(const char *user)
110 {
111         fstring filter;
112
113         slprintf(filter, sizeof(filter)-1,
114                  "(&(uid=%s)(objectclass=sambaAccount))", user);
115         return ldap_search_for(filter);
116 }
117
118 BOOL ldap_search_by_uid(int uid)
119 {
120         fstring filter;
121         
122         slprintf(filter, sizeof(filter)-1, 
123                  "(&(uidNumber=%d)(objectclass=sambaAccount))", uid);
124         return ldap_search_for(filter);
125 }
126
127
128 /*******************************************************************
129   Get the first value of an attribute.
130  ******************************************************************/
131
132 BOOL ldap_get_attribute(char *attribute, char *value)
133 {
134         char **values;
135         
136         if(!(values = ldap_get_values(ldap_struct, ldap_entry, attribute)))
137                 return (False);
138
139         pstrcpy(value, values[0]);
140         ldap_value_free(values);
141         DEBUG(3, ("get: [%s] = [%s]\n", attribute, value));
142         
143         return (True);
144 }
145
146
147 /*******************************************************************
148   Contruct an smb_passwd structure
149  ******************************************************************/
150
151 struct smb_passwd *ldap_getpw()
152 {
153         static struct smb_passwd smbpw;
154         static pstring unix_name;
155         static pstring nt_name;
156         static unsigned char smblmpwd[16];
157         static unsigned char smbntpwd[16];
158         pstring temp;
159
160         if(!ldap_entry)
161                 return NULL;
162
163         if(!ldap_get_attribute("uid", unix_name)) {
164                 DEBUG(0,("Missing uid\n"));
165                 return NULL; }
166         smbpw.unix_name = unix_name;
167
168         DEBUG(2,("Retrieving account [%s]\n",unix_name));
169
170         if(!ldap_get_attribute("uidNumber", temp)) {
171                 DEBUG(0,("Missing uidNumber\n"));
172                 return NULL; }
173         smbpw.unix_uid = atoi(temp);
174
175         if(ldap_get_attribute("ntuid", nt_name)) {
176                 DEBUG(0,("Missing ntuid\n"));
177                 return NULL; }
178         smbpw.nt_name = nt_name;
179
180         if(!ldap_get_attribute("rid", temp)) {
181                 DEBUG(0,("Missing rid\n"));
182                 return NULL; }
183         smbpw.user_rid = atoi(temp);
184
185         if(ldap_get_attribute("acctFlags", temp))
186                 smbpw.acct_ctrl = pwdb_decode_acct_ctrl(temp);
187         else
188                 smbpw.acct_ctrl = ACB_NORMAL;
189
190         if(ldap_get_attribute("lmPassword", temp)) {
191                 pwdb_gethexpwd(temp, smblmpwd);
192                 smbpw.smb_passwd = smblmpwd;
193         } else {
194                 smbpw.smb_passwd = NULL;
195                 smbpw.acct_ctrl |= ACB_DISABLED;
196         }
197
198         if(ldap_get_attribute("ntPassword", temp)) {
199                 pwdb_gethexpwd(temp, smbntpwd);
200                 smbpw.smb_nt_passwd = smbntpwd;
201         } else {
202                 smbpw.smb_nt_passwd = NULL;
203         }
204
205         if(ldap_get_attribute("pwdLastSet", temp))
206                 smbpw.pass_last_set_time = (time_t)strtol(temp, NULL, 16);
207         else
208                 smbpw.pass_last_set_time = (time_t)(-1);
209
210         ldap_entry = ldap_next_entry(ldap_struct, ldap_entry);
211         return &smbpw;
212 }
213
214
215 /************************************************************************
216   Adds a modification to a LDAPMod queue.
217  ************************************************************************/
218
219  void ldap_make_mod(LDAPMod ***modlist,int modop, char *attribute, char *value)
220 {
221         LDAPMod **mods;
222         int i;
223         int j;
224
225         DEBUG(3, ("set: [%s] = [%s]\n", attribute, value));
226         
227         mods = *modlist;
228         
229         if (mods == NULL) {
230                 mods = (LDAPMod **)malloc(sizeof(LDAPMod *));
231                 mods[0] = NULL;
232         }
233         
234         for (i = 0; mods[i] != NULL; ++i) {
235                 if (mods[i]->mod_op == modop && 
236                     !strcasecmp(mods[i]->mod_type, attribute)) {
237                         break;
238                 }
239         }
240         
241         if (mods[i] == NULL) {
242                 mods = (LDAPMod **)realloc(mods, (i+2) * sizeof(LDAPMod *));
243                 mods[i] = (LDAPMod *)malloc(sizeof(LDAPMod));
244                 mods[i]->mod_op = modop;
245                 mods[i]->mod_values = NULL;
246                 mods[i]->mod_type = strdup(attribute);
247                 mods[i+1] = NULL;
248         }
249
250         if (value) {
251                 j = 0;
252                 if (mods[i]->mod_values) {
253                         for (; mods[i]->mod_values[j]; j++);
254                 }
255                 mods[i]->mod_values = (char **)realloc(mods[i]->mod_values,
256                                                   (j+2) * sizeof(char *));
257                 mods[i]->mod_values[j] = strdup(value);
258                 mods[i]->mod_values[j+1] = NULL;
259         }
260
261         *modlist = mods;
262 }
263
264
265 /************************************************************************
266   Queues the necessary modifications to save a smb_passwd structure
267  ************************************************************************/
268
269  void ldap_smbpwmods(struct smb_passwd *newpwd, LDAPMod ***mods, int operation)
270 {
271         fstring temp;
272         int i;
273
274         *mods = NULL;
275         if(operation == LDAP_MOD_ADD) { /* immutable attributes */
276               ldap_make_mod(mods, LDAP_MOD_ADD, "objectclass", "sambaAccount");
277
278               ldap_make_mod(mods, LDAP_MOD_ADD, "uid", newpwd->unix_name);
279               slprintf(temp, sizeof(temp)-1, "%d", newpwd->unix_uid);
280               ldap_make_mod(mods, LDAP_MOD_ADD, "uidNumber", temp);
281
282               ldap_make_mod(mods, LDAP_MOD_ADD, "ntuid", newpwd->nt_name);
283               slprintf(temp, sizeof(temp)-1, "%d", newpwd->user_rid);
284               ldap_make_mod(mods, LDAP_MOD_ADD, "rid", temp);
285         }
286
287         if (newpwd->smb_passwd) {
288               for( i = 0; i < 16; i++) {
289                      slprintf(&temp[2*i], 3, "%02X", newpwd->smb_passwd[i]);
290               }
291               ldap_make_mod(mods, operation, "lmPassword", temp);
292         }
293
294         if (newpwd->smb_nt_passwd) {
295               for( i = 0; i < 16; i++) {
296                      slprintf(&temp[2*i], 3, "%02X", newpwd->smb_nt_passwd[i]);
297               }
298               ldap_make_mod(mods, operation, "ntPassword", temp);
299         }
300
301         newpwd->pass_last_set_time = time(NULL);
302         slprintf(temp, sizeof(temp)-1, "%08X", newpwd->pass_last_set_time);
303         ldap_make_mod(mods, operation, "pwdLastSet", temp);
304
305         ldap_make_mod(mods, operation, "acctFlags",
306                             pwdb_encode_acct_ctrl(newpwd->acct_ctrl,
307                                    NEW_PW_FORMAT_SPACE_PADDED_LEN));
308 }
309
310
311 /************************************************************************
312   Commit changes to a directory entry.
313  *************************************************************************/
314  BOOL ldap_makemods(char *attribute, char *value, LDAPMod **mods, BOOL add)
315 {
316         pstring dn;
317         int entries;
318         int err = 0;
319         BOOL rc;
320
321         slprintf(dn, sizeof(dn)-1, "%s=%s, %s", attribute, value,
322                  lp_ldap_suffix());
323
324         if(!ldap_open_connection(True))
325                 return (False);
326
327         if(add)
328                 err = ldap_add_s(ldap_struct, dn, mods);
329
330         if(!add || (err = LDAP_ALREADY_EXISTS))
331                 err = ldap_modify_s(ldap_struct, dn, mods);
332
333         if(err == LDAP_SUCCESS) {
334                 DEBUG(2,("Updated entry [%s]\n",value));
335                 rc = True;
336         } else {
337                 DEBUG(0,("update: %s\n", ldap_err2string(err)));
338                 rc = False;
339         }
340
341         ldap_close_connection();
342         ldap_mods_free(mods, 1);
343         return rc;
344 }
345
346
347 /***************************************************************
348   Begin/end account enumeration.
349  ****************************************************************/
350
351 static void *ldap_enumfirst(BOOL update)
352 {
353         if (!ldap_open_connection(False))
354                 return NULL;
355
356         ldap_search_for("objectclass=sambaAccount");
357
358         return ldap_struct;
359 }
360
361 static void ldap_enumclose(void *vp)
362 {
363         ldap_close_connection();
364 }
365
366
367 /*************************************************************************
368   Save/restore the current position in a query
369  *************************************************************************/
370
371 static SMB_BIG_UINT ldap_getdbpos(void *vp)
372 {
373         return (SMB_BIG_UINT)((ulong)ldap_entry);
374 }
375
376 static BOOL ldap_setdbpos(void *vp, SMB_BIG_UINT tok)
377 {
378         ldap_entry = (LDAPMessage *)((ulong)tok);
379         return (True);
380 }
381
382
383 /*************************************************************************
384   Return smb_passwd information.
385  *************************************************************************/
386
387 static struct smb_passwd *ldap_getpwbynam(const char *name)
388 {
389         struct smb_passwd *ret;
390
391         if(!ldap_open_connection(False))
392                 return NULL;
393
394         ldap_search_by_name(name);
395         ret = ldap_getpw();
396
397         ldap_close_connection();
398         return ret;
399 }
400
401 static struct smb_passwd *ldap_getpwbyuid(uid_t userid)
402 {
403         struct smb_passwd *ret;
404
405         if(!ldap_open_connection(False))
406                 return NULL;
407
408         ldap_search_by_uid(userid);
409         ret = ldap_getpw();
410
411         ldap_close_connection();
412         return ret;
413 }
414
415 static struct smb_passwd *ldap_getcurrentpw(void *vp)
416 {
417         return ldap_getpw();
418 }
419
420
421 /************************************************************************
422   Modify user information given an smb_passwd struct.
423  *************************************************************************/
424 static BOOL ldap_addpw(struct smb_passwd *newpwd)
425 {
426         LDAPMod **mods;
427
428         ldap_smbpwmods(newpwd, &mods, LDAP_MOD_ADD);
429         return ldap_makemods("uid", newpwd->unix_name, mods, True);
430 }
431
432 static BOOL ldap_modpw(struct smb_passwd *pwd, BOOL override)
433 {
434         LDAPMod **mods;
435
436         ldap_smbpwmods(pwd, &mods, LDAP_MOD_REPLACE);
437         return ldap_makemods("uid", pwd->unix_name, mods, False);
438 }
439
440
441 static struct smb_passdb_ops ldap_ops =
442 {
443         ldap_enumfirst,
444         ldap_enumclose,
445         ldap_getdbpos,
446         ldap_setdbpos,
447
448         ldap_getpwbynam,
449         ldap_getpwbyuid,
450         ldap_getcurrentpw,
451         ldap_addpw,
452         ldap_modpw
453 };
454
455 struct smb_passdb_ops *ldap_initialise_password_db(void)
456 {
457         FILE *pwdfile;
458         char *pwdfilename;
459         char *p;
460
461         pwdfilename = lp_ldap_passwd_file();
462
463         if(pwdfilename[0]) {
464                 if(pwdfile = sys_fopen(pwdfilename, "r")) {
465                         fgets(ldap_secret, sizeof(ldap_secret), pwdfile);
466                         if(p = strchr(ldap_secret, '\n'))
467                                 *p = 0;
468                         fclose(pwdfile);
469                 } else {
470                         DEBUG(0,("Failed to open LDAP passwd file\n"));
471                 }
472         }
473
474         return &ldap_ops;
475 }
476
477 #else
478  void ldap_dummy_function(void);
479  void ldap_dummy_function(void) { } /* stop some compilers complaining */
480 #endif