some merges from 2.2. Still need to merge in changes from pdb_tdb.c
[Samba.git] / source / passdb / pdb_ldap.c
blob408b22a62957c5865dfa44578875c0d34812175a
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.9.
4 LDAP protocol helper functions for SAMBA
5 Copyright (C) Gerald Carter 2001
6 Copyright (C) Shahms King 2001
7 Copyright (C) Jean François Micouleau 1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 #ifdef WITH_LDAP_SAM
28 /* TODO:
29 * persistent connections: if using NSS LDAP, many connections are made
30 * however, using only one within Samba would be nice
32 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
34 * Other LDAP based login attributes: accountExpires, etc.
35 * (should be the domain of Samba proper, but the sam_password/SAM_ACCOUNT
36 * structures don't have fields for some of these attributes)
38 * SSL is done, but can't get the certificate based authentication to work
39 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
42 /* NOTE: this will NOT work against an Active Directory server
43 * due to the fact that the two password fields cannot be retrieved
44 * from a server; recommend using security = domain in this situation
45 * and/or winbind
48 #include <lber.h>
49 #include <ldap.h>
51 #ifndef SAM_ACCOUNT
52 #define SAM_ACCOUNT struct sam_passwd
53 #endif
55 struct ldap_enum_info {
56 LDAP *ldap_struct;
57 LDAPMessage *result;
58 LDAPMessage *entry;
61 static struct ldap_enum_info global_ldap_ent;
64 extern pstring samlogon_user;
65 extern BOOL sam_logon_in_ssb;
68 /*******************************************************************
69 open a connection to the ldap server.
70 ******************************************************************/
71 static BOOL ldap_open_connection (LDAP ** ldap_struct)
73 int port;
74 int version, rc;
75 int tls = LDAP_OPT_X_TLS_HARD;
77 /* there should be an lp_ldap_ssl_port(), what happen if for some
78 reason we need to bind an SSLed LDAP on port 389 ?? ---simo */
79 if (lp_ldap_ssl() == LDAP_SSL_ON && lp_ldap_port() == 389) {
80 port = 636;
82 else {
83 port = lp_ldap_port();
86 if ((*ldap_struct = ldap_init(lp_ldap_server(), port)) == NULL) {
87 DEBUG(0, ("The LDAP server is not responding !\n"));
88 return False;
91 /* Connect to older servers using SSL and V2 rather than Start TLS */
92 if (ldap_get_option(*ldap_struct, LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS)
94 if (version != LDAP_VERSION2)
96 version = LDAP_VERSION2;
97 ldap_set_option (*ldap_struct, LDAP_OPT_PROTOCOL_VERSION, &version);
101 switch (lp_ldap_ssl())
103 case LDAP_SSL_START_TLS:
104 if (ldap_get_option (*ldap_struct, LDAP_OPT_PROTOCOL_VERSION,
105 &version) == LDAP_OPT_SUCCESS)
107 if (version < LDAP_VERSION3)
109 version = LDAP_VERSION3;
110 ldap_set_option (*ldap_struct, LDAP_OPT_PROTOCOL_VERSION,
111 &version);
114 if ((rc = ldap_start_tls_s (*ldap_struct, NULL, NULL)) != LDAP_SUCCESS)
116 DEBUG(0,("Failed to issue the StartTLS instruction: %s\n",
117 ldap_err2string(rc)));
118 return False;
120 DEBUG (2, ("StartTLS issued: using a TLS connection\n"));
121 break;
123 case LDAP_SSL_ON:
124 if (ldap_set_option (*ldap_struct, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
126 DEBUG(0, ("Failed to setup a TLS session\n"));
128 break;
130 case LDAP_SSL_OFF:
131 default:
133 * No special needs to setup options prior to the LDAP
134 * bind (which should be called next via ldap_connect_system()
136 break;
139 DEBUG(2, ("ldap_open_connection: connection opened\n"));
140 return True;
143 /*******************************************************************
144 connect to the ldap server under system privilege.
145 ******************************************************************/
146 static BOOL ldap_connect_system(LDAP * ldap_struct)
148 int rc;
149 static BOOL got_pw = False;
150 static pstring ldap_secret;
152 /* get the password if we don't have it already */
153 if (!got_pw && !(got_pw=fetch_ldap_pw(lp_ldap_admin_dn(), ldap_secret, sizeof(pstring))))
155 DEBUG(0, ("ldap_connect_system: Failed to retrieve password for %s from secrets.tdb\n",
156 lp_ldap_admin_dn()));
157 return False;
160 /* removed the sasl_bind_s "EXTERNAL" stuff, as my testsuite
161 (OpenLDAP) doesnt' seem to support it */
163 DEBUG(10,("ldap_connect_system: Binding to ldap server as \"%s\"\n",
164 lp_ldap_admin_dn()));
166 if ((rc = ldap_simple_bind_s(ldap_struct, lp_ldap_admin_dn(),
167 ldap_secret)) != LDAP_SUCCESS)
169 DEBUG(0, ("Bind failed: %s\n", ldap_err2string(rc)));
170 return False;
173 DEBUG(2, ("ldap_connect_system: succesful connection to the LDAP server\n"));
174 return True;
177 /*******************************************************************
178 run the search by name.
179 ******************************************************************/
180 static int ldap_search_one_user (LDAP * ldap_struct, const char *filter, LDAPMessage ** result)
182 int scope = LDAP_SCOPE_SUBTREE;
183 int rc;
185 DEBUG(2, ("ldap_search_one_user: searching for:[%s]\n", filter));
187 rc = ldap_search_s(ldap_struct, lp_ldap_suffix (), scope, filter, NULL, 0, result);
189 if (rc != LDAP_SUCCESS) {
190 DEBUG(0,("ldap_search_one_user: Problem during the LDAP search: %s\n",
191 ldap_err2string (rc)));
192 DEBUG(3,("ldap_search_one_user: Query was: %s, %s\n", lp_ldap_suffix(),
193 filter));
196 return rc;
199 /*******************************************************************
200 run the search by name.
201 ******************************************************************/
202 static int ldap_search_one_user_by_name (LDAP * ldap_struct, const char *user,
203 LDAPMessage ** result)
205 pstring filter;
208 * in the filter expression, replace %u with the real name
209 * so in ldap filter, %u MUST exist :-)
211 pstrcpy(filter, lp_ldap_filter());
214 * have to use this here because $ is filtered out
215 * in pstring_sub
217 all_string_sub(filter, "%u", user, sizeof(pstring));
219 return ldap_search_one_user(ldap_struct, filter, result);
222 /*******************************************************************
223 run the search by uid.
224 ******************************************************************/
225 static int ldap_search_one_user_by_uid(LDAP * ldap_struct, int uid,
226 LDAPMessage ** result)
228 struct passwd *user;
229 pstring filter;
231 /* Get the username from the system and look that up in the LDAP */
233 if ((user = sys_getpwuid(uid)) == NULL) {
234 DEBUG(3,("ldap_search_one_user_by_uid: Failed to locate uid [%d]\n", uid));
235 return LDAP_NO_SUCH_OBJECT;
238 pstrcpy(filter, lp_ldap_filter());
240 all_string_sub(filter, "%u", user->pw_name, sizeof(pstring));
242 return ldap_search_one_user(ldap_struct, filter, result);
245 /*******************************************************************
246 run the search by rid.
247 ******************************************************************/
248 static int ldap_search_one_user_by_rid (LDAP * ldap_struct, uint32 rid,
249 LDAPMessage ** result)
251 pstring filter;
252 int rc;
254 /* check if the user rid exsists, if not, try searching on the uid */
256 snprintf(filter, sizeof(filter) - 1, "rid=%i", rid);
257 rc = ldap_search_one_user(ldap_struct, filter, result);
259 if (rc != LDAP_SUCCESS)
260 rc = ldap_search_one_user_by_uid(ldap_struct,
261 pdb_user_rid_to_uid(rid), result);
263 return rc;
266 /*******************************************************************
267 search an attribute and return the first value found.
268 ******************************************************************/
269 static BOOL get_single_attribute (LDAP * ldap_struct, LDAPMessage * entry,
270 char *attribute, char *value)
272 char **values;
274 if ((values = ldap_get_values (ldap_struct, entry, attribute)) == NULL) {
275 value = NULL;
276 DEBUG (2, ("get_single_attribute: [%s] = [<does not exist>]\n", attribute));
278 return False;
281 pstrcpy(value, values[0]);
282 ldap_value_free(values);
283 DEBUG (2, ("get_single_attribute: [%s] = [%s]\n", attribute, value));
285 return True;
288 /************************************************************************
289 Routine to manage the LDAPMod structure array
290 manage memory used by the array, by each struct, and values
292 ************************************************************************/
293 static void make_a_mod (LDAPMod *** modlist, int modop, const char *attribute, const char *value)
295 LDAPMod **mods;
296 int i;
297 int j;
299 mods = *modlist;
301 if (attribute == NULL || *attribute == '\0')
302 return;
304 if (value == NULL || *value == '\0')
305 return;
307 if (mods == NULL)
309 mods = (LDAPMod **) malloc(sizeof(LDAPMod *));
310 if (mods == NULL)
312 DEBUG(0, ("make_a_mod: out of memory!\n"));
313 return;
315 mods[0] = NULL;
318 for (i = 0; mods[i] != NULL; ++i) {
319 if (mods[i]->mod_op == modop && !strcasecmp(mods[i]->mod_type, attribute))
320 break;
323 if (mods[i] == NULL)
325 mods = (LDAPMod **) Realloc (mods, (i + 2) * sizeof (LDAPMod *));
326 if (mods == NULL)
328 DEBUG(0, ("make_a_mod: out of memory!\n"));
329 return;
331 mods[i] = (LDAPMod *) malloc(sizeof(LDAPMod));
332 if (mods[i] == NULL)
334 DEBUG(0, ("make_a_mod: out of memory!\n"));
335 return;
337 mods[i]->mod_op = modop;
338 mods[i]->mod_values = NULL;
339 mods[i]->mod_type = strdup(attribute);
340 mods[i + 1] = NULL;
343 if (value != NULL)
345 j = 0;
346 if (mods[i]->mod_values != NULL) {
347 for (; mods[i]->mod_values[j] != NULL; j++);
349 mods[i]->mod_values = (char **)Realloc(mods[i]->mod_values,
350 (j + 2) * sizeof (char *));
352 if (mods[i]->mod_values == NULL) {
353 DEBUG (0, ("make_a_mod: Memory allocation failure!\n"));
354 return;
356 mods[i]->mod_values[j] = strdup(value);
357 mods[i]->mod_values[j + 1] = NULL;
359 *modlist = mods;
362 /* New Interface is being implemented here */
364 /**********************************************************************
365 Initialize SAM_ACCOUNT from an LDAP query
366 (Based on init_sam_from_buffer in pdb_tdb.c)
367 *********************************************************************/
368 static BOOL init_sam_from_ldap (SAM_ACCOUNT * sampass,
369 LDAP * ldap_struct, LDAPMessage * entry)
371 time_t logon_time,
372 logoff_time,
373 kickoff_time,
374 pass_last_set_time,
375 pass_can_change_time,
376 pass_must_change_time;
377 pstring username,
378 domain,
379 nt_username,
380 fullname,
381 homedir,
382 dir_drive,
383 logon_script,
384 profile_path,
385 acct_desc,
386 munged_dial,
387 workstations;
388 struct passwd *sys_user;
389 uint32 user_rid,
390 group_rid;
391 uint8 smblmpwd[16],
392 smbntpwd[16];
393 uint16 acct_ctrl,
394 logon_divs;
395 uint32 hours_len;
396 uint8 hours[MAX_HOURS_LEN];
397 pstring temp;
398 gid_t gid = getegid();
402 * do a little initialization
404 username[0] = '\0';
405 domain[0] = '\0';
406 nt_username[0] = '\0';
407 fullname[0] = '\0';
408 homedir[0] = '\0';
409 dir_drive[0] = '\0';
410 logon_script[0] = '\0';
411 profile_path[0] = '\0';
412 acct_desc[0] = '\0';
413 munged_dial[0] = '\0';
414 workstations[0] = '\0';
417 if (sampass == NULL || ldap_struct == NULL || entry == NULL) {
418 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
419 return False;
422 get_single_attribute(ldap_struct, entry, "uid", username);
423 DEBUG(2, ("Entry found for user: %s\n", username));
425 pstrcpy(samlogon_user, username);
427 pstrcpy(nt_username, username);
429 pstrcpy(domain, lp_workgroup());
431 get_single_attribute(ldap_struct, entry, "pwdLastSet", temp);
432 pass_last_set_time = (time_t) strtol(temp, NULL, 16);
434 get_single_attribute(ldap_struct, entry, "logonTime", temp);
435 logon_time = (time_t) strtol(temp, NULL, 16);
437 get_single_attribute(ldap_struct, entry, "logoffTime", temp);
438 logoff_time = (time_t) strtol(temp, NULL, 16);
440 get_single_attribute(ldap_struct, entry, "kickoffTime", temp);
441 kickoff_time = (time_t) strtol(temp, NULL, 16);
443 get_single_attribute(ldap_struct, entry, "pwdCanChange", temp);
444 pass_can_change_time = (time_t) strtol(temp, NULL, 16);
446 get_single_attribute(ldap_struct, entry, "pwdMustChange", temp);
447 pass_must_change_time = (time_t) strtol(temp, NULL, 16);
449 /* recommend that 'gecos' and 'displayName' should refer to the same
450 * attribute OID. userFullName depreciated, only used by Samba
451 * primary rules of LDAP: don't make a new attribute when one is already defined
452 * that fits your needs; using cn then displayName rather than 'userFullName'
455 sam_logon_in_ssb = True;
457 if (!get_single_attribute(ldap_struct, entry, "cn", fullname)) {
458 get_single_attribute(ldap_struct, entry, "displayName", fullname);
462 if (!get_single_attribute(ldap_struct, entry, "homeDrive", dir_drive)) {
463 pstrcpy(dir_drive, lp_logon_drive());
464 standard_sub_advanced(-1, username, "", gid, username, dir_drive);
465 DEBUG(5,("homeDrive fell back to %s\n",dir_drive));
466 pdb_set_dir_drive(sampass, dir_drive, False);
468 else
469 pdb_set_dir_drive(sampass, dir_drive, True);
471 if (!get_single_attribute(ldap_struct, entry, "smbHome", homedir)) {
472 pstrcpy(homedir, lp_logon_home());
473 standard_sub_advanced(-1, username, "", gid, username, homedir);
474 DEBUG(5,("smbHome fell back to %s\n",homedir));
475 pdb_set_homedir(sampass, homedir, False);
477 else
478 pdb_set_homedir(sampass, homedir, True);
480 if (!get_single_attribute(ldap_struct, entry, "scriptPath", logon_script)) {
481 pstrcpy(logon_script, lp_logon_script());
482 standard_sub_advanced(-1, username, "", gid, username, logon_script);
483 DEBUG(5,("scriptPath fell back to %s\n",logon_script));
484 pdb_set_logon_script(sampass, logon_script, False);
486 else
487 pdb_set_logon_script(sampass, logon_script, True);
489 if (!get_single_attribute(ldap_struct, entry, "profilePath", profile_path)) {
490 pstrcpy(profile_path, lp_logon_path());
491 standard_sub_advanced(-1, username, "", gid, username, profile_path);
492 DEBUG(5,("profilePath fell back to %s\n",profile_path));
493 pdb_set_profile_path(sampass, profile_path, False);
495 else
496 pdb_set_profile_path(sampass, profile_path, True);
498 sam_logon_in_ssb = False;
500 get_single_attribute(ldap_struct, entry, "description", acct_desc);
501 get_single_attribute(ldap_struct, entry, "userWorkstations", workstations);
502 get_single_attribute(ldap_struct, entry, "rid", temp);
503 user_rid = (uint32)strtol(temp, NULL, 10);
504 get_single_attribute(ldap_struct, entry, "primaryGroupID", temp);
505 group_rid = (uint32)strtol(temp, NULL, 10);
508 /* These values MAY be in LDAP, but they can also be retrieved through
509 * sys_getpw*() which is how we're doing it
511 sys_user = sys_getpwnam(username);
512 if (sys_user == NULL) {
513 DEBUG (2,("init_sam_from_ldap: User [%s] does not ave a uid!\n", username));
514 return False;
518 /* FIXME: hours stuff should be cleaner */
520 logon_divs = 168;
521 hours_len = 21;
522 memset(hours, 0xff, hours_len);
524 get_single_attribute (ldap_struct, entry, "lmPassword", temp);
525 pdb_gethexpwd(temp, smblmpwd);
526 memset((char *)temp, '\0', sizeof(temp));
527 get_single_attribute (ldap_struct, entry, "ntPassword", temp);
528 pdb_gethexpwd(temp, smbntpwd);
529 memset((char *)temp, '\0', sizeof(temp));
530 get_single_attribute (ldap_struct, entry, "acctFlags", temp);
531 acct_ctrl = pdb_decode_acct_ctrl(temp);
533 if (acct_ctrl == 0)
534 acct_ctrl |= ACB_NORMAL;
537 pdb_set_acct_ctrl(sampass, acct_ctrl);
538 pdb_set_logon_time(sampass, logon_time);
539 pdb_set_logoff_time(sampass, logoff_time);
540 pdb_set_kickoff_time(sampass, kickoff_time);
541 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
542 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
543 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
545 pdb_set_hours_len(sampass, hours_len);
546 pdb_set_logons_divs(sampass, logon_divs);
548 pdb_set_uid(sampass, sys_user->pw_uid);
549 pdb_set_gid(sampass, sys_user->pw_gid);
550 pdb_set_user_rid(sampass, user_rid);
551 pdb_set_group_rid(sampass, group_rid);
553 pdb_set_username(sampass, username);
555 pdb_set_domain(sampass, domain);
556 pdb_set_nt_username(sampass, nt_username);
558 pdb_set_fullname(sampass, fullname);
560 pdb_set_acct_desc(sampass, acct_desc);
561 pdb_set_workstations(sampass, workstations);
562 pdb_set_munged_dial(sampass, munged_dial);
564 if (!pdb_set_nt_passwd(sampass, smbntpwd))
565 return False;
566 if (!pdb_set_lanman_passwd(sampass, smblmpwd))
567 return False;
569 /* pdb_set_unknown_3(sampass, unknown3); */
570 /* pdb_set_unknown_5(sampass, unknown5); */
571 /* pdb_set_unknown_6(sampass, unknown6); */
573 pdb_set_hours(sampass, hours);
575 return True;
578 /**********************************************************************
579 Initialize SAM_ACCOUNT from an LDAP query
580 (Based on init_buffer_from_sam in pdb_tdb.c)
581 *********************************************************************/
582 static BOOL init_ldap_from_sam (LDAPMod *** mods, int ldap_state, const SAM_ACCOUNT * sampass)
584 pstring temp;
586 if (mods == NULL || sampass == NULL) {
587 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
588 return False;
591 *mods = NULL;
594 * took out adding "objectclass: sambaAccount"
595 * do this on a per-mod basis
599 make_a_mod(mods, ldap_state, "uid", pdb_get_username(sampass));
600 DEBUG(2, ("Setting entry for user: %s\n", pdb_get_username(sampass)));
602 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
603 make_a_mod(mods, ldap_state, "pwdLastSet", temp);
605 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
606 make_a_mod(mods, ldap_state, "logonTime", temp);
608 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
609 make_a_mod(mods, ldap_state, "logoffTime", temp);
611 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
612 make_a_mod(mods, ldap_state, "kickoffTime", temp);
614 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
615 make_a_mod(mods, ldap_state, "pwdCanChange", temp);
617 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
618 make_a_mod(mods, ldap_state, "pwdMustChange", temp);
620 /* displayName, cn, and gecos should all be the same
621 * most easily accomplished by giving them the same OID
622 * gecos isn't set here b/c it should be handled by the
623 * add-user script
626 make_a_mod(mods, ldap_state, "displayName", pdb_get_fullname(sampass));
627 make_a_mod(mods, ldap_state, "cn", pdb_get_fullname(sampass));
628 make_a_mod(mods, ldap_state, "description", pdb_get_acct_desc(sampass));
629 make_a_mod(mods, ldap_state, "userWorkstations", pdb_get_workstations(sampass));
632 * Only updates fields which have been set (not defaults from smb.conf)
635 if (IS_SAM_SET(sampass, FLAG_SAM_SMBHOME))
636 make_a_mod(mods, ldap_state, "smbHome", pdb_get_homedir(sampass));
638 if (IS_SAM_SET(sampass, FLAG_SAM_DRIVE))
639 make_a_mod(mods, ldap_state, "homeDrive", pdb_get_dirdrive(sampass));
641 if (IS_SAM_SET(sampass, FLAG_SAM_LOGONSCRIPT))
642 make_a_mod(mods, ldap_state, "scriptPath", pdb_get_logon_script(sampass));
644 if (IS_SAM_SET(sampass, FLAG_SAM_PROFILE))
645 make_a_mod(mods, ldap_state, "profilePath", pdb_get_profile_path(sampass));
648 if ( !sampass->user_rid)
649 slprintf(temp, sizeof(temp) - 1, "%i", pdb_uid_to_user_rid(pdb_get_uid(sampass)));
650 else
651 slprintf(temp, sizeof(temp) - 1, "%i", sampass->user_rid);
652 make_a_mod(mods, ldap_state, "rid", temp);
654 if ( !sampass->group_rid)
655 slprintf(temp, sizeof(temp) - 1, "%i", pdb_gid_to_group_rid(pdb_get_gid(sampass)));
656 else
657 slprintf(temp, sizeof(temp) - 1, "%i", sampass->group_rid);
658 make_a_mod(mods, ldap_state, "primaryGroupID", temp);
660 /* FIXME: Hours stuff goes in LDAP */
661 pdb_sethexpwd (temp, pdb_get_lanman_passwd(sampass), pdb_get_acct_ctrl(sampass));
662 make_a_mod (mods, ldap_state, "lmPassword", temp);
664 pdb_sethexpwd (temp, pdb_get_nt_passwd(sampass), pdb_get_acct_ctrl(sampass));
665 make_a_mod (mods, ldap_state, "ntPassword", temp);
667 make_a_mod (mods, ldap_state, "acctFlags", pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass),
668 NEW_PW_FORMAT_SPACE_PADDED_LEN));
670 return True;
673 /**********************************************************************
674 Connect to LDAP server for password enumeration
675 *********************************************************************/
676 BOOL pdb_setsampwent(BOOL update)
678 int rc;
679 pstring filter;
681 if (!ldap_open_connection(&global_ldap_ent.ldap_struct))
683 return False;
685 if (!ldap_connect_system(global_ldap_ent.ldap_struct))
687 ldap_unbind(global_ldap_ent.ldap_struct);
688 return False;
691 pstrcpy(filter, lp_ldap_filter());
692 all_string_sub(filter, "%u", "*", sizeof(pstring));
694 rc = ldap_search_s(global_ldap_ent.ldap_struct, lp_ldap_suffix(),
695 LDAP_SCOPE_SUBTREE, filter, NULL, 0,
696 &global_ldap_ent.result);
698 if (rc != LDAP_SUCCESS)
700 DEBUG(0, ("LDAP search failed: %s\n", ldap_err2string(rc)));
701 DEBUG(3, ("Query was: %s, %s\n", lp_ldap_suffix(), filter));
702 ldap_msgfree(global_ldap_ent.result);
703 ldap_unbind(global_ldap_ent.ldap_struct);
704 global_ldap_ent.ldap_struct = NULL;
705 global_ldap_ent.result = NULL;
706 return False;
709 DEBUG(2, ("pdb_setsampwent: %d entries in the base!\n",
710 ldap_count_entries(global_ldap_ent.ldap_struct,
711 global_ldap_ent.result)));
713 global_ldap_ent.entry = ldap_first_entry(global_ldap_ent.ldap_struct,
714 global_ldap_ent.result);
716 return True;
719 /**********************************************************************
720 End enumeration of the LDAP password list
721 *********************************************************************/
722 void pdb_endsampwent(void)
724 if (global_ldap_ent.ldap_struct && global_ldap_ent.result)
726 ldap_msgfree(global_ldap_ent.result);
727 ldap_unbind(global_ldap_ent.ldap_struct);
728 global_ldap_ent.ldap_struct = NULL;
729 global_ldap_ent.result = NULL;
733 /**********************************************************************
734 Get the next entry in the LDAP password database
735 *********************************************************************/
736 BOOL pdb_getsampwent(SAM_ACCOUNT * user)
738 if (!global_ldap_ent.entry)
739 return False;
741 global_ldap_ent.entry = ldap_next_entry(global_ldap_ent.ldap_struct,
742 global_ldap_ent.entry);
744 if (global_ldap_ent.entry != NULL)
746 return init_sam_from_ldap(user, global_ldap_ent.ldap_struct,
747 global_ldap_ent.entry);
749 return False;
752 /**********************************************************************
753 Get SAM_ACCOUNT entry from LDAP by username
754 *********************************************************************/
755 BOOL pdb_getsampwnam(SAM_ACCOUNT * user, const char *sname)
757 LDAP *ldap_struct;
758 LDAPMessage *result;
759 LDAPMessage *entry;
761 if (!ldap_open_connection(&ldap_struct))
762 return False;
763 if (!ldap_connect_system(ldap_struct))
765 ldap_unbind(ldap_struct);
766 return False;
768 if (ldap_search_one_user_by_name(ldap_struct, sname, &result) != LDAP_SUCCESS)
770 ldap_unbind(ldap_struct);
771 return False;
773 if (ldap_count_entries(ldap_struct, result) < 1)
775 DEBUG(0,
776 ("We don't find this user [%s] count=%d\n", sname,
777 ldap_count_entries(ldap_struct, result)));
778 ldap_unbind(ldap_struct);
779 return False;
781 entry = ldap_first_entry(ldap_struct, result);
782 if (entry)
784 init_sam_from_ldap(user, ldap_struct, entry);
785 ldap_msgfree(result);
786 ldap_unbind(ldap_struct);
787 return True;
789 else
791 ldap_msgfree(result);
792 ldap_unbind(ldap_struct);
793 return False;
797 /**********************************************************************
798 Get SAM_ACCOUNT entry from LDAP by rid
799 *********************************************************************/
800 BOOL pdb_getsampwrid(SAM_ACCOUNT * user, uint32 rid)
802 LDAP *ldap_struct;
803 LDAPMessage *result;
804 LDAPMessage *entry;
806 if (!ldap_open_connection(&ldap_struct))
807 return False;
809 if (!ldap_connect_system(ldap_struct))
811 ldap_unbind(ldap_struct);
812 return False;
814 if (ldap_search_one_user_by_rid(ldap_struct, rid, &result) !=
815 LDAP_SUCCESS)
817 ldap_unbind(ldap_struct);
818 return False;
821 if (ldap_count_entries(ldap_struct, result) < 1)
823 DEBUG(0,
824 ("We don't find this rid [%i] count=%d\n", rid,
825 ldap_count_entries(ldap_struct, result)));
826 ldap_unbind(ldap_struct);
827 return False;
830 entry = ldap_first_entry(ldap_struct, result);
831 if (entry)
833 init_sam_from_ldap(user, ldap_struct, entry);
834 ldap_msgfree(result);
835 ldap_unbind(ldap_struct);
836 return True;
838 else
840 ldap_msgfree(result);
841 ldap_unbind(ldap_struct);
842 return False;
846 /**********************************************************************
847 Delete entry from LDAP for username
848 *********************************************************************/
849 BOOL pdb_delete_sam_account(const char *sname)
851 int rc;
852 char *dn;
853 LDAP *ldap_struct;
854 LDAPMessage *entry;
855 LDAPMessage *result;
857 if (!ldap_open_connection (&ldap_struct))
858 return False;
860 DEBUG (3, ("Deleting user %s from LDAP.\n", sname));
862 if (!ldap_connect_system (ldap_struct)) {
863 ldap_unbind (ldap_struct);
864 DEBUG(0, ("Failed to delete user %s from LDAP.\n", sname));
865 return False;
868 rc = ldap_search_one_user_by_name (ldap_struct, sname, &result);
869 if (ldap_count_entries (ldap_struct, result) == 0) {
870 DEBUG (0, ("User doesn't exit!\n"));
871 ldap_msgfree (result);
872 ldap_unbind (ldap_struct);
873 return False;
876 entry = ldap_first_entry (ldap_struct, result);
877 dn = ldap_get_dn (ldap_struct, entry);
879 rc = ldap_delete_s (ldap_struct, dn);
881 ldap_memfree (dn);
882 if (rc != LDAP_SUCCESS) {
883 char *ld_error;
884 ldap_get_option (ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error);
885 DEBUG (0,("failed to delete user with uid = %s with: %s\n\t%s\n",
886 sname, ldap_err2string (rc), ld_error));
887 free (ld_error);
888 ldap_unbind (ldap_struct);
889 return False;
892 DEBUG (2,("successfully deleted uid = %s from the LDAP database\n", sname));
893 ldap_unbind (ldap_struct);
894 return True;
897 /**********************************************************************
898 Update SAM_ACCOUNT
899 *********************************************************************/
900 BOOL pdb_update_sam_account(const SAM_ACCOUNT * newpwd, BOOL override)
902 int rc;
903 char *dn;
904 LDAP *ldap_struct;
905 LDAPMessage *result;
906 LDAPMessage *entry;
907 LDAPMod **mods;
909 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
910 return False;
912 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
914 ldap_unbind(ldap_struct);
915 return False;
918 rc = ldap_search_one_user_by_name(ldap_struct,
919 pdb_get_username(newpwd), &result);
921 if (ldap_count_entries(ldap_struct, result) == 0)
923 DEBUG(0, ("No user to modify!\n"));
924 ldap_msgfree(result);
925 ldap_unbind(ldap_struct);
926 return False;
929 init_ldap_from_sam(&mods, LDAP_MOD_REPLACE, newpwd);
931 entry = ldap_first_entry(ldap_struct, result);
932 dn = ldap_get_dn(ldap_struct, entry);
934 rc = ldap_modify_s(ldap_struct, dn, mods);
936 if (rc != LDAP_SUCCESS)
938 char *ld_error;
939 ldap_get_option(ldap_struct, LDAP_OPT_ERROR_STRING,
940 &ld_error);
941 DEBUG(0,
942 ("failed to modify user with uid = %s with: %s\n\t%s\n",
943 pdb_get_username(newpwd), ldap_err2string(rc),
944 ld_error));
945 free(ld_error);
946 ldap_unbind(ldap_struct);
947 return False;
950 DEBUG(2,
951 ("successfully modified uid = %s in the LDAP database\n",
952 pdb_get_username(newpwd)));
953 ldap_mods_free(mods, 1);
954 ldap_unbind(ldap_struct);
955 return True;
958 /**********************************************************************
959 Add SAM_ACCOUNT to LDAP
960 *********************************************************************/
961 BOOL pdb_add_sam_account(const SAM_ACCOUNT * newpwd)
963 int rc;
964 pstring filter;
965 LDAP *ldap_struct;
966 LDAPMessage *result;
967 pstring dn;
968 LDAPMod **mods;
969 int ldap_op;
970 uint32 num_result;
972 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
974 return False;
977 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
979 ldap_unbind(ldap_struct);
980 return False;
983 rc = ldap_search_one_user_by_name (ldap_struct, pdb_get_username(newpwd), &result);
985 if (ldap_count_entries(ldap_struct, result) != 0)
987 DEBUG(0,("User already in the base, with samba properties\n"));
988 ldap_msgfree(result);
989 ldap_unbind(ldap_struct);
990 return False;
992 ldap_msgfree(result);
994 slprintf (filter, sizeof (filter) - 1, "uid=%s", pdb_get_username(newpwd));
995 rc = ldap_search_one_user(ldap_struct, filter, &result);
996 num_result = ldap_count_entries(ldap_struct, result);
998 if (num_result > 1) {
999 DEBUG (0, ("More than one user with that uid exists: bailing out!\n"));
1000 return False;
1003 /* Check if we need to update an existing entry */
1004 if (num_result == 1) {
1005 char *tmp;
1006 LDAPMessage *entry;
1008 DEBUG(3,("User exists without samba properties: adding them\n"));
1009 ldap_op = LDAP_MOD_REPLACE;
1010 entry = ldap_first_entry (ldap_struct, result);
1011 tmp = ldap_get_dn (ldap_struct, entry);
1012 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1013 ldap_memfree (tmp);
1015 else {
1016 /* Check if we need to add an entry */
1017 DEBUG(3,("Adding new user\n"));
1018 ldap_op = LDAP_MOD_ADD;
1019 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", pdb_get_username(newpwd), lp_ldap_suffix ());
1022 ldap_msgfree(result);
1024 init_ldap_from_sam(&mods, ldap_op, newpwd);
1025 make_a_mod(&mods, LDAP_MOD_ADD, "objectclass", "sambaAccount");
1027 if (ldap_op == LDAP_MOD_REPLACE) {
1028 rc = ldap_modify_s(ldap_struct, dn, mods);
1030 else {
1031 rc = ldap_add_s(ldap_struct, dn, mods);
1034 if (rc != LDAP_SUCCESS)
1036 char *ld_error;
1038 ldap_get_option (ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error);
1039 DEBUG(0,("failed to modify user with uid = %s with: %s\n\t%s\n",
1040 pdb_get_username(newpwd), ldap_err2string (rc), ld_error));
1041 free(ld_error);
1042 ldap_mods_free(mods, 1);
1043 ldap_unbind(ldap_struct);
1044 return False;
1047 DEBUG(2,("added: uid = %s in the LDAP database\n", pdb_get_username(newpwd)));
1048 ldap_mods_free(mods, 1);
1049 ldap_unbind(ldap_struct);
1050 return True;
1053 #else
1054 void dummy_function(void);
1055 void
1056 dummy_function (void)
1058 } /* stop some compilers complaining */
1059 #endif