2 Unix SMB/Netbios implementation.
4 LDAP protocol helper functions for SAMBA
5 Copyright (C) Shahms King 2001
6 Copyright (C) Jean François Micouleau 1998
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.
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.
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.
28 * persistent connections: if using NSS LDAP, many connections are made
29 * however, using only one within Samba would be nice
31 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
33 * Other LDAP based login attributes: accountExpires, etc.
34 * (should be the domain of Samba proper, but the sam_password/SAM_ACCOUNT
35 * structures don't have fields for some of these attributes)
37 * SSL is done, but can't get the certificate based authentication to work
38 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
41 /* NOTE: this will NOT work against an Active Directory server
42 * due to the fact that the two password fields cannot be retrieved
43 * from a server; recommend using security = domain in this situation
51 #define SAM_ACCOUNT struct sam_passwd
54 extern int DEBUGLEVEL;
63 static struct ldap_enum_info global_ldap_ent;
66 /*******************************************************************
67 open a connection to the ldap server.
68 ******************************************************************/
70 ldap_open_connection (LDAP ** ldap_struct)
74 int tls = LDAP_OPT_X_TLS_HARD;
76 if (lp_ldap_ssl() == LDAP_SSL_ON && lp_ldap_port() == 389) {
80 port = lp_ldap_port();
83 if ((*ldap_struct = ldap_init(lp_ldap_server(), port)) == NULL) {
84 DEBUG(0, ("The LDAP server is not responding !\n"));
88 /* Connect to older servers using SSL and V2 rather than Start TLS */
89 if (ldap_get_option(*ldap_struct, LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS)
91 if (version != LDAP_VERSION2)
93 version = LDAP_VERSION2;
94 ldap_set_option (*ldap_struct, LDAP_OPT_PROTOCOL_VERSION, &version);
98 switch (lp_ldap_ssl())
100 case LDAP_SSL_START_TLS:
101 if (ldap_get_option (*ldap_struct, LDAP_OPT_PROTOCOL_VERSION,
102 &version) == LDAP_OPT_SUCCESS)
104 if (version < LDAP_VERSION3)
106 version = LDAP_VERSION3;
107 ldap_set_option (*ldap_struct, LDAP_OPT_PROTOCOL_VERSION,
111 if ((rc = ldap_start_tls_s (*ldap_struct, NULL, NULL)) != LDAP_SUCCESS)
114 ("Failed to issue the StartTLS instruction: %s\n",
115 ldap_err2string(rc)));
118 DEBUG (2, ("StartTLS issued: using a TLS connection\n"));
121 if (ldap_set_option (*ldap_struct, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
123 DEBUG(0, ("Failed to setup a TLS session\n"));
130 DEBUG(2, ("ldap_open_connection: connection opened\n"));
134 /*******************************************************************
135 connect to the ldap server under system privilege.
136 ******************************************************************/
137 static BOOL ldap_connect_system(LDAP * ldap_struct)
140 static BOOL got_pw = False;
141 static pstring ldap_secret;
143 /* get the password if we don't have it already */
144 if (!got_pw && !(got_pw=fetch_ldap_pw(lp_ldap_admin_dn(), ldap_secret, sizeof(pstring))))
146 DEBUG(0, ("ldap_connect_system: Failed to retrieve password for %s from secrets.tdb\n",
147 lp_ldap_admin_dn()));
151 /* removed the sasl_bind_s "EXTERNAL" stuff, as my testsuite
152 (OpenLDAP) doesnt' seem to support it */
153 if ((rc = ldap_simple_bind_s(ldap_struct, lp_ldap_admin_dn(),
154 ldap_secret)) != LDAP_SUCCESS)
156 DEBUG(0, ("Bind failed: %s\n", ldap_err2string(rc)));
160 DEBUG(2, ("ldap_connect_system: succesful connection to the LDAP server\n"));
164 /*******************************************************************
165 run the search by name.
166 ******************************************************************/
167 static int ldap_search_one_user (LDAP * ldap_struct, const char *filter, LDAPMessage ** result)
169 int scope = LDAP_SCOPE_SUBTREE;
172 DEBUG(2, ("ldap_search_one_user: searching for:[%s]\n", filter));
174 rc = ldap_search_s (ldap_struct, lp_ldap_suffix (), scope,
175 filter, NULL, 0, result);
177 if (rc != LDAP_SUCCESS) {
178 DEBUG(0,("ldap_search_one_user: Problem during the LDAP search: %s\n",
179 ldap_err2string (rc)));
180 DEBUG(3,("ldap_search_one_user: Query was: %s, %s\n", lp_ldap_suffix(),
186 /*******************************************************************
187 run the search by name.
188 ******************************************************************/
189 static int ldap_search_one_user_by_name (LDAP * ldap_struct, const char *user,
190 LDAPMessage ** result)
195 in the filter expression, replace %u with the real name
196 so in ldap filter, %u MUST exist :-)
198 pstrcpy(filter, lp_ldap_filter());
200 /* have to use this here because $ is filtered out
203 all_string_sub(filter, "%u", user, sizeof(pstring));
205 return ldap_search_one_user(ldap_struct, filter, result);
208 /*******************************************************************
209 run the search by uid.
210 ******************************************************************/
211 static int ldap_search_one_user_by_uid(LDAP * ldap_struct, int uid,
212 LDAPMessage ** result)
217 /* Get the username from the system and look that up in the LDAP */
218 user = sys_getpwuid(uid);
219 pstrcpy(filter, lp_ldap_filter());
220 all_string_sub(filter, "%u", user->pw_name, sizeof(pstring));
222 return ldap_search_one_user(ldap_struct, filter, result);
225 /*******************************************************************
226 run the search by rid.
227 ******************************************************************/
228 static int ldap_search_one_user_by_rid (LDAP * ldap_struct, uint32 rid,
229 LDAPMessage ** result)
234 /* check if the user rid exsists, if not, try searching on the uid */
235 snprintf(filter, sizeof(filter) - 1, "rid=%i", rid);
236 rc = ldap_search_one_user(ldap_struct, filter, result);
238 if (rc != LDAP_SUCCESS)
239 rc = ldap_search_one_user_by_uid(ldap_struct,
240 pdb_user_rid_to_uid(rid), result);
245 /*******************************************************************
246 search an attribute and return the first value found.
247 ******************************************************************/
248 static void get_single_attribute (LDAP * ldap_struct, LDAPMessage * entry,
249 char *attribute, char *value)
253 if ((valeurs = ldap_get_values (ldap_struct, entry, attribute)) != NULL) {
254 pstrcpy(value, valeurs[0]);
255 ldap_value_free(valeurs);
256 DEBUG (2, ("get_single_attribute: [%s] = [%s]\n", attribute, value));
260 DEBUG (2, ("get_single_attribute: [%s] = [NULL]\n", attribute));
264 /************************************************************************
265 Routine to manage the LDAPMod structure array
266 manage memory used by the array, by each struct, and values
268 ************************************************************************/
269 static void make_a_mod (LDAPMod *** modlist, int modop, char *attribute, char *value)
277 if (attribute == NULL || *attribute == '\0')
280 if (value == NULL || *value == '\0')
285 mods = (LDAPMod **) malloc(sizeof(LDAPMod *));
288 DEBUG(0, ("make_a_mod: out of memory!\n"));
294 for (i = 0; mods[i] != NULL; ++i) {
295 if (mods[i]->mod_op == modop && !strcasecmp(mods[i]->mod_type, attribute))
301 mods = (LDAPMod **) realloc (mods, (i + 2) * sizeof (LDAPMod *));
304 DEBUG(0, ("make_a_mod: out of memory!\n"));
307 mods[i] = (LDAPMod *) malloc(sizeof(LDAPMod));
310 DEBUG(0, ("make_a_mod: out of memory!\n"));
313 mods[i]->mod_op = modop;
314 mods[i]->mod_values = NULL;
315 mods[i]->mod_type = strdup(attribute);
322 if (mods[i]->mod_values != NULL) {
323 for (; mods[i]->mod_values[j] != NULL; j++);
325 mods[i]->mod_values = (char **)realloc(mods[i]->mod_values,
326 (j + 2) * sizeof (char *));
328 if (mods[i]->mod_values == NULL) {
329 DEBUG (0, ("make_a_mod: Memory allocation failure!\n"));
332 mods[i]->mod_values[j] = strdup(value);
333 mods[i]->mod_values[j + 1] = NULL;
338 /* New Interface is being implemented here */
340 /**********************************************************************
341 Initialize SAM_ACCOUNT from an LDAP query
342 (Based on init_sam_from_buffer in pdb_tdb.c)
343 *********************************************************************/
344 static BOOL init_sam_from_ldap (SAM_ACCOUNT * sampass,
345 LDAP * ldap_struct, LDAPMessage * entry)
351 pass_can_change_time,
352 pass_must_change_time;
353 static pstring username;
354 static pstring domain;
355 static pstring nt_username;
356 static pstring fullname;
357 static pstring homedir;
358 static pstring dir_drive;
359 static pstring logon_script;
360 static pstring profile_path;
361 static pstring acct_desc;
362 static pstring munged_dial;
363 static pstring workstations;
364 struct passwd *sys_user;
365 uint32 user_rid, group_rid;
366 static uint8 smblmpwd[16];
367 static uint8 smbntpwd[16];
368 uint16 acct_ctrl, logon_divs;
373 get_single_attribute(ldap_struct, entry, "uid", username);
374 DEBUG(2, ("Entry found for user: %s\n", username));
376 /* not sure about this for nt_username */
377 get_single_attribute (ldap_struct, entry, "sAMAccountName", nt_username);
379 pstrcpy(nt_username, username);
381 get_single_attribute(ldap_struct, entry, "sambaDomain", domain);
383 pstrcpy(domain, lp_workgroup());
385 get_single_attribute(ldap_struct, entry, "pwdLastSet", temp);
386 pass_last_set_time = (time_t) strtol(temp, NULL, 16);
388 get_single_attribute(ldap_struct, entry, "logonTime", temp);
389 logon_time = (time_t) strtol(temp, NULL, 16);
391 get_single_attribute(ldap_struct, entry, "logoffTime", temp);
392 logoff_time = (time_t) strtol(temp, NULL, 16);
394 get_single_attribute(ldap_struct, entry, "kickoffTime", temp);
395 kickoff_time = (time_t) strtol(temp, NULL, 16);
397 get_single_attribute(ldap_struct, entry, "pwdCanChange", temp);
398 pass_can_change_time = (time_t) strtol(temp, NULL, 16);
400 get_single_attribute(ldap_struct, entry, "pwdMustChange", temp);
401 pass_must_change_time = (time_t) strtol(temp, NULL, 16);
403 /* recommend that 'gecos' and 'displayName' should refer to the same
404 * attribute OID. userFullName depreciated, only used by Samba
405 * primary rules of LDAP: don't make a new attribute when one is already defined
406 * that fits your needs; using gecos then displayName then cn rather than 'userFullName'
409 get_single_attribute(ldap_struct, entry, "gecos", fullname);
412 get_single_attribute(ldap_struct, entry, "displayName", fullname);
413 get_single_attribute(ldap_struct, entry, "cn", fullname);
416 get_single_attribute(ldap_struct, entry, "homeDrive", dir_drive);
417 get_single_attribute(ldap_struct, entry, "scriptPath", logon_script);
418 get_single_attribute(ldap_struct, entry, "profilePath", profile_path);
419 get_single_attribute(ldap_struct, entry, "description", acct_desc);
420 get_single_attribute (ldap_struct, entry, "userWorkstations", workstations);
422 get_single_attribute(ldap_struct, entry, "rid", temp);
423 user_rid = (uint32)strtol(temp, NULL, 16);
424 get_single_attribute(ldap_struct, entry, "primaryGroupID", temp);
425 group_rid = (uint32)strtol(temp, NULL, 16);
428 /* These values MAY be in LDAP, but they can also be retrieved through
429 * sys_getpw*() which is how we're doing it (if you use nss_ldap, then
430 * these values will be stored in LDAP as well, but if not, we want the
431 * local values to override the LDAP for this anyway
432 * homeDirectory attribute
434 sys_user = sys_getpwnam(username);
435 if (sys_user == NULL)
437 pstrcpy(homedir, sys_user->pw_dir);
440 /* FIXME: hours stuff should be cleaner */
443 hours = malloc(sizeof(hours) * hours_len);
444 memset(hours, 0xff, hours_len);
446 get_single_attribute (ldap_struct, entry, "lmPassword", temp);
447 pdb_gethexpwd(temp, smblmpwd);
448 memset((char *)temp, '\0', sizeof(temp));
449 get_single_attribute (ldap_struct, entry, "ntPassword", temp);
450 pdb_gethexpwd(temp, smbntpwd);
451 memset((char *)temp, '\0', sizeof(temp));
452 get_single_attribute (ldap_struct, entry, "acctFlags", temp);
453 acct_ctrl = pdb_decode_acct_ctrl(temp);
456 acct_ctrl |= ACB_NORMAL;
459 pdb_set_acct_ctrl(sampass, acct_ctrl);
460 pdb_set_logon_time(sampass, logon_time);
461 pdb_set_logoff_time(sampass, logoff_time);
462 pdb_set_kickoff_time(sampass, kickoff_time);
463 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
464 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
465 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
467 pdb_set_hours_len(sampass, hours_len);
468 pdb_set_logons_divs(sampass, logon_divs);
470 pdb_set_uid(sampass, sys_user->pw_uid);
471 pdb_set_gid(sampass, sys_user->pw_gid);
472 pdb_set_user_rid(sampass, user_rid);
473 pdb_set_group_rid(sampass, group_rid);
475 pdb_set_username(sampass, username);
477 pdb_set_domain(sampass, domain);
478 pdb_set_nt_username(sampass, nt_username);
480 pdb_set_fullname(sampass, fullname);
482 pdb_set_logon_script(sampass, logon_script);
483 pdb_set_profile_path(sampass, profile_path);
484 pdb_set_dir_drive(sampass, dir_drive);
485 pdb_set_homedir(sampass, homedir);
486 pdb_set_acct_desc(sampass, acct_desc);
487 pdb_set_workstations(sampass, workstations);
488 pdb_set_munged_dial(sampass, munged_dial);
489 pdb_set_nt_passwd(sampass, smbntpwd);
490 pdb_set_lanman_passwd(sampass, smblmpwd);
492 /* pdb_set_unknown_3(sampass, unknown3); */
493 /* pdb_set_unknown_5(sampass, unknown5); */
494 /* pdb_set_unknown_6(sampass, unknown6); */
496 pdb_set_hours(sampass, hours);
501 /**********************************************************************
502 Initialize SAM_ACCOUNT from an LDAP query
503 (Based on init_buffer_from_sam in pdb_tdb.c)
504 *********************************************************************/
505 static BOOL init_ldap_from_sam (LDAPMod *** mods, int ldap_state, SAM_ACCOUNT * sampass)
512 * took out adding "objectclass: sambaAccount"
513 * do this on a per-mod basis
517 make_a_mod(mods, ldap_state, "uid", pdb_get_username(sampass));
518 DEBUG(2, ("Setting entry for user: %s\n", pdb_get_username(sampass)));
520 /* not sure about using this for the nt_username */
521 make_a_mod (mods, ldap_state, "sAMAccountName", pdb_get_nt_username(sampass));
522 make_a_mod(mods, ldap_state, "sambaDomain", pdb_get_domain(sampass));
524 slprintf(temp, sizeof(temp) - 1, "%i", pdb_get_uid(sampass));
525 make_a_mod(mods, ldap_state, "uidNumber", temp);
527 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
528 make_a_mod(mods, ldap_state, "pwdLastSet", temp);
530 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
531 make_a_mod(mods, ldap_state, "logonTime", temp);
533 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
534 make_a_mod(mods, ldap_state, "logoffTime", temp);
536 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
537 make_a_mod(mods, ldap_state, "kickoffTime", temp);
539 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
540 make_a_mod(mods, ldap_state, "pwdCanChange", temp);
542 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
543 make_a_mod(mods, ldap_state, "pwdMustChange", temp);
545 /* displayName, cn, and gecos should all be the same
546 * most easily accomplished by giving them the same OID
547 * gecos isn't set here b/c it should be handled by the
551 make_a_mod(mods, ldap_state, "displayName", pdb_get_fullname(sampass));
552 make_a_mod(mods, ldap_state, "cn", pdb_get_fullname(sampass));
554 make_a_mod(mods, ldap_state, "homeDirectory", pdb_get_homedir(sampass));
555 make_a_mod(mods, ldap_state, "homeDrive", pdb_get_dirdrive(sampass));
556 make_a_mod(mods, ldap_state, "scriptPath", pdb_get_logon_script(sampass));
557 make_a_mod(mods, ldap_state, "profilePath", pdb_get_profile_path(sampass));
558 make_a_mod(mods, ldap_state, "description", pdb_get_acct_desc(sampass));
559 make_a_mod(mods, ldap_state, "userWorkstations", pdb_get_workstations(sampass));
561 slprintf(temp, sizeof(temp) - 1, "%i", sampass->user_rid);
562 make_a_mod(mods, ldap_state, "rid", temp);
564 slprintf(temp, sizeof(temp) - 1, "%i", sampass->group_rid);
565 make_a_mod(mods, ldap_state, "primaryGroupID", temp);
567 /* FIXME: Hours stuff goes in LDAP */
568 pdb_sethexpwd (temp, pdb_get_lanman_passwd(sampass), pdb_get_acct_ctrl(sampass));
569 make_a_mod (mods, ldap_state, "lmPassword", temp);
570 pdb_sethexpwd (temp, pdb_get_nt_passwd(sampass), pdb_get_acct_ctrl(sampass));
571 make_a_mod (mods, ldap_state, "ntPassword", temp);
572 make_a_mod (mods, ldap_state, "acctFlags", pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass),
573 NEW_PW_FORMAT_SPACE_PADDED_LEN));
578 /**********************************************************************
579 Connect to LDAP server for password enumeration
580 *********************************************************************/
581 BOOL pdb_setsampwent(BOOL update)
586 if (!ldap_open_connection(&global_ldap_ent.ldap_struct))
590 if (!ldap_connect_system(global_ldap_ent.ldap_struct))
592 ldap_unbind(global_ldap_ent.ldap_struct);
596 pstrcpy(filter, lp_ldap_filter());
597 all_string_sub(filter, "%u", "*", sizeof(pstring));
599 rc = ldap_search_s(global_ldap_ent.ldap_struct, lp_ldap_suffix(),
600 LDAP_SCOPE_SUBTREE, filter, NULL, 0,
601 &global_ldap_ent.result);
603 if (rc != LDAP_SUCCESS)
605 DEBUG(0, ("LDAP search failed: %s\n", ldap_err2string(rc)));
606 DEBUG(3, ("Query was: %s, %s\n", lp_ldap_suffix(), filter));
607 ldap_msgfree(global_ldap_ent.result);
608 ldap_unbind(global_ldap_ent.ldap_struct);
609 global_ldap_ent.ldap_struct = NULL;
610 global_ldap_ent.result = NULL;
614 DEBUG(2, ("pdb_setsampwent: %d entries in the base!\n",
615 ldap_count_entries(global_ldap_ent.ldap_struct,
616 global_ldap_ent.result)));
618 global_ldap_ent.entry = ldap_first_entry(global_ldap_ent.ldap_struct,
619 global_ldap_ent.result);
624 /**********************************************************************
625 End enumeration of the LDAP password list
626 *********************************************************************/
627 void pdb_endsampwent(void)
629 if (global_ldap_ent.ldap_struct && global_ldap_ent.result)
631 ldap_msgfree(global_ldap_ent.result);
632 ldap_unbind(global_ldap_ent.ldap_struct);
633 global_ldap_ent.ldap_struct = NULL;
634 global_ldap_ent.result = NULL;
638 /**********************************************************************
639 Get the next entry in the LDAP password database
640 *********************************************************************/
641 BOOL pdb_getsampwent(SAM_ACCOUNT * user)
643 if (!global_ldap_ent.entry)
646 global_ldap_ent.entry = ldap_next_entry(global_ldap_ent.ldap_struct,
647 global_ldap_ent.entry);
649 if (global_ldap_ent.entry != NULL)
651 return init_sam_from_ldap(user, global_ldap_ent.ldap_struct,
652 global_ldap_ent.entry);
657 /**********************************************************************
658 Get SAM_ACCOUNT entry from LDAP by username
659 *********************************************************************/
660 BOOL pdb_getsampwnam(SAM_ACCOUNT * user, char *sname)
666 if (!ldap_open_connection(&ldap_struct))
668 if (!ldap_connect_system(ldap_struct))
670 ldap_unbind(ldap_struct);
673 if (ldap_search_one_user_by_name(ldap_struct, sname, &result) !=
676 ldap_unbind(ldap_struct);
679 if (ldap_count_entries(ldap_struct, result) < 1)
682 ("We don't find this user [%s] count=%d\n", sname,
683 ldap_count_entries(ldap_struct, result)));
684 ldap_unbind(ldap_struct);
687 entry = ldap_first_entry(ldap_struct, result);
690 init_sam_from_ldap(user, ldap_struct, entry);
691 ldap_msgfree(result);
692 ldap_unbind(ldap_struct);
697 ldap_msgfree(result);
698 ldap_unbind(ldap_struct);
703 /**********************************************************************
704 Get SAM_ACCOUNT entry from LDAP by rid
705 *********************************************************************/
706 BOOL pdb_getsampwrid(SAM_ACCOUNT * user, uint32 rid)
712 if (!ldap_open_connection(&ldap_struct))
715 if (!ldap_connect_system(ldap_struct))
717 ldap_unbind(ldap_struct);
720 if (ldap_search_one_user_by_rid(ldap_struct, rid, &result) !=
723 ldap_unbind(ldap_struct);
727 if (ldap_count_entries(ldap_struct, result) < 1)
730 ("We don't find this rid [%i] count=%d\n", rid,
731 ldap_count_entries(ldap_struct, result)));
732 ldap_unbind(ldap_struct);
736 entry = ldap_first_entry(ldap_struct, result);
739 init_sam_from_ldap(user, ldap_struct, entry);
740 ldap_msgfree(result);
741 ldap_unbind(ldap_struct);
746 ldap_msgfree(result);
747 ldap_unbind(ldap_struct);
752 /**********************************************************************
753 Get SAM_ACCOUNT entry from LDAP by uid
754 *********************************************************************/
755 BOOL pdb_getsampwuid(SAM_ACCOUNT * user, uid_t uid)
761 if (!ldap_open_connection(&ldap_struct))
764 if (!ldap_connect_system(ldap_struct))
766 ldap_unbind(ldap_struct);
769 if (ldap_search_one_user_by_uid(ldap_struct, uid, &result) !=
772 ldap_unbind(ldap_struct);
776 if (ldap_count_entries(ldap_struct, result) < 1)
779 ("We don't find this uid [%i] count=%d\n", uid,
780 ldap_count_entries(ldap_struct, result)));
781 ldap_unbind(ldap_struct);
784 entry = ldap_first_entry(ldap_struct, result);
787 init_sam_from_ldap(user, ldap_struct, entry);
788 ldap_msgfree(result);
789 ldap_unbind(ldap_struct);
794 ldap_msgfree(result);
795 ldap_unbind(ldap_struct);
801 /**********************************************************************
802 Delete entry from LDAP for username
803 *********************************************************************/
804 BOOL pdb_delete_sam_account(char *sname)
812 if (!ldap_open_connection (&ldap_struct))
815 DEBUG (3, ("Deleting user %s from LDAP.\n", sname));
817 if (!ldap_connect_system (ldap_struct)) {
818 ldap_unbind (ldap_struct);
819 DEBUG(0, ("Failed to delete user %s from LDAP.\n", sname));
823 rc = ldap_search_one_user_by_name (ldap_struct, sname, &result);
824 if (ldap_count_entries (ldap_struct, result) == 0) {
825 DEBUG (0, ("User doesn't exit!\n"));
826 ldap_msgfree (result);
827 ldap_unbind (ldap_struct);
831 entry = ldap_first_entry (ldap_struct, result);
832 dn = ldap_get_dn (ldap_struct, entry);
834 rc = ldap_delete_s (ldap_struct, dn);
837 if (rc != LDAP_SUCCESS) {
839 ldap_get_option (ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error);
840 DEBUG (0,("failed to delete user with uid = %s with: %s\n\t%s\n",
841 sname, ldap_err2string (rc), ld_error));
843 ldap_unbind (ldap_struct);
847 DEBUG (2,("successfully deleted uid = %s from the LDAP database\n", sname));
848 ldap_unbind (ldap_struct);
852 /**********************************************************************
854 *********************************************************************/
855 BOOL pdb_update_sam_account(SAM_ACCOUNT * newpwd, BOOL override)
864 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
867 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
869 ldap_unbind(ldap_struct);
873 rc = ldap_search_one_user_by_name(ldap_struct,
874 pdb_get_username(newpwd), &result);
876 if (ldap_count_entries(ldap_struct, result) == 0)
878 DEBUG(0, ("No user to modify!\n"));
879 ldap_msgfree(result);
880 ldap_unbind(ldap_struct);
884 init_ldap_from_sam(&mods, LDAP_MOD_REPLACE, newpwd);
886 entry = ldap_first_entry(ldap_struct, result);
887 dn = ldap_get_dn(ldap_struct, entry);
889 rc = ldap_modify_s(ldap_struct, dn, mods);
891 if (rc != LDAP_SUCCESS)
894 ldap_get_option(ldap_struct, LDAP_OPT_ERROR_STRING,
897 ("failed to modify user with uid = %s with: %s\n\t%s\n",
898 pdb_get_username(newpwd), ldap_err2string(rc),
901 ldap_unbind(ldap_struct);
906 ("successfully modified uid = %s in the LDAP database\n",
907 pdb_get_username(newpwd)));
908 ldap_mods_free(mods, 1);
909 ldap_unbind(ldap_struct);
913 /**********************************************************************
914 Add SAM_ACCOUNT to LDAP
915 *********************************************************************/
916 BOOL pdb_add_sam_account(SAM_ACCOUNT * newpwd)
924 int ldap_op = LDAP_MOD_ADD;
926 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
931 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
933 ldap_unbind(ldap_struct);
937 if (pdb_get_username(newpwd) != NULL) {
938 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s",
939 pdb_get_username(newpwd), lp_ldap_suffix ());
947 rc = ldap_search_one_user_by_name (ldap_struct, pdb_get_username(newpwd), &result);
949 if (ldap_count_entries(ldap_struct, result) != 0)
951 DEBUG(0,("User already in the base, with samba properties\n"));
952 ldap_msgfree(result);
953 ldap_unbind(ldap_struct);
956 ldap_msgfree(result);
958 slprintf (filter, sizeof (filter) - 1, "uid=%s", pdb_get_username(newpwd));
959 rc = ldap_search_one_user(ldap_struct, filter, &result);
960 if (ldap_count_entries(ldap_struct, result) == 1)
964 DEBUG(3,("User exists without samba properties: adding them\n"));
965 ldap_op = LDAP_MOD_REPLACE;
966 entry = ldap_first_entry (ldap_struct, result);
967 tmp = ldap_get_dn (ldap_struct, entry);
968 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
973 DEBUG (3, ("More than one user with that uid exists: bailing out!\n"));
977 ldap_msgfree(result);
979 init_ldap_from_sam(&mods, ldap_op, newpwd);
980 make_a_mod(&mods, LDAP_MOD_ADD, "objectclass", "sambaAccount");
982 if (ldap_op == LDAP_MOD_REPLACE) {
983 rc = ldap_modify_s(ldap_struct, dn, mods);
986 rc = ldap_add_s(ldap_struct, dn, mods);
989 if (rc != LDAP_SUCCESS)
993 ldap_get_option (ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error);
994 DEBUG(0,("failed to modify user with uid = %s with: %s\n\t%s\n",
995 pdb_get_username(newpwd), ldap_err2string (rc), ld_error));
997 ldap_mods_free(mods, 1);
998 ldap_unbind(ldap_struct);
1002 DEBUG(2,("added: uid = %s in the LDAP database\n", pdb_get_username(newpwd)));
1003 ldap_mods_free(mods, 1);
1004 ldap_unbind(ldap_struct);
1009 void dummy_function(void);
1011 dummy_function (void)
1013 } /* stop some compilers complaining */