2 Unix SMB/Netbios implementation.
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.
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
52 #define SAM_ACCOUNT struct sam_passwd
55 struct ldap_enum_info {
61 static struct ldap_enum_info global_ldap_ent;
65 /*******************************************************************
66 open a connection to the ldap server.
67 ******************************************************************/
68 static BOOL ldap_open_connection (LDAP ** ldap_struct)
72 int tls = LDAP_OPT_X_TLS_HARD;
74 /* there should be an lp_ldap_ssl_port(), what happen if for some
75 reason we need to bind an SSLed LDAP on port 389 ?? ---simo */
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)
113 DEBUG(0,("Failed to issue the StartTLS instruction: %s\n",
114 ldap_err2string(rc)));
117 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 * No special needs to setup options prior to the LDAP
131 * bind (which should be called next via ldap_connect_system()
136 DEBUG(2, ("ldap_open_connection: connection opened\n"));
140 /*******************************************************************
141 connect to the ldap server under system privilege.
142 ******************************************************************/
143 static BOOL ldap_connect_system(LDAP * ldap_struct)
146 static BOOL got_pw = False;
147 static pstring ldap_secret;
149 /* get the password if we don't have it already */
150 if (!got_pw && !(got_pw=fetch_ldap_pw(lp_ldap_admin_dn(), ldap_secret, sizeof(pstring))))
152 DEBUG(0, ("ldap_connect_system: Failed to retrieve password for %s from secrets.tdb\n",
153 lp_ldap_admin_dn()));
157 /* removed the sasl_bind_s "EXTERNAL" stuff, as my testsuite
158 (OpenLDAP) doesnt' seem to support it */
160 DEBUG(10,("ldap_connect_system: Binding to ldap server as \"%s\"\n",
161 lp_ldap_admin_dn()));
163 if ((rc = ldap_simple_bind_s(ldap_struct, lp_ldap_admin_dn(),
164 ldap_secret)) != LDAP_SUCCESS)
166 DEBUG(0, ("Bind failed: %s\n", ldap_err2string(rc)));
170 DEBUG(2, ("ldap_connect_system: succesful connection to the LDAP server\n"));
174 /*******************************************************************
175 run the search by name.
176 ******************************************************************/
177 static int ldap_search_one_user (LDAP * ldap_struct, const char *filter, LDAPMessage ** result)
179 int scope = LDAP_SCOPE_SUBTREE;
182 DEBUG(2, ("ldap_search_one_user: searching for:[%s]\n", filter));
184 rc = ldap_search_s(ldap_struct, lp_ldap_suffix (), scope, filter, NULL, 0, result);
186 if (rc != LDAP_SUCCESS) {
187 DEBUG(0,("ldap_search_one_user: Problem during the LDAP search: %s\n",
188 ldap_err2string (rc)));
189 DEBUG(3,("ldap_search_one_user: Query was: %s, %s\n", lp_ldap_suffix(),
196 /*******************************************************************
197 run the search by name.
198 ******************************************************************/
199 static int ldap_search_one_user_by_name (LDAP * ldap_struct, const char *user,
200 LDAPMessage ** result)
205 * in the filter expression, replace %u with the real name
206 * so in ldap filter, %u MUST exist :-)
208 pstrcpy(filter, lp_ldap_filter());
211 * have to use this here because $ is filtered out
214 all_string_sub(filter, "%u", user, sizeof(pstring));
216 return ldap_search_one_user(ldap_struct, filter, result);
219 /*******************************************************************
220 run the search by uid.
221 ******************************************************************/
222 static int ldap_search_one_user_by_uid(LDAP * ldap_struct, int uid,
223 LDAPMessage ** result)
228 /* Get the username from the system and look that up in the LDAP */
230 if ((user = sys_getpwuid(uid)) == NULL) {
231 DEBUG(3,("ldap_search_one_user_by_uid: Failed to locate uid [%d]\n", uid));
232 return LDAP_NO_SUCH_OBJECT;
235 pstrcpy(filter, lp_ldap_filter());
237 all_string_sub(filter, "%u", user->pw_name, sizeof(pstring));
239 return ldap_search_one_user(ldap_struct, filter, result);
242 /*******************************************************************
243 run the search by rid.
244 ******************************************************************/
245 static int ldap_search_one_user_by_rid (LDAP * ldap_struct, uint32 rid,
246 LDAPMessage ** result)
251 /* check if the user rid exsists, if not, try searching on the uid */
253 snprintf(filter, sizeof(filter) - 1, "rid=%i", rid);
254 rc = ldap_search_one_user(ldap_struct, filter, result);
256 if (rc != LDAP_SUCCESS)
257 rc = ldap_search_one_user_by_uid(ldap_struct,
258 pdb_user_rid_to_uid(rid), result);
263 /*******************************************************************
264 search an attribute and return the first value found.
265 ******************************************************************/
266 static BOOL get_single_attribute (LDAP * ldap_struct, LDAPMessage * entry,
267 char *attribute, char *value)
271 if ((values = ldap_get_values (ldap_struct, entry, attribute)) == NULL) {
273 DEBUG (2, ("get_single_attribute: [%s] = [<does not exist>]\n", attribute));
278 pstrcpy(value, values[0]);
279 ldap_value_free(values);
280 DEBUG (2, ("get_single_attribute: [%s] = [%s]\n", attribute, value));
285 /************************************************************************
286 Routine to manage the LDAPMod structure array
287 manage memory used by the array, by each struct, and values
289 ************************************************************************/
290 static void make_a_mod (LDAPMod *** modlist, int modop, const char *attribute, const char *value)
298 if (attribute == NULL || *attribute == '\0')
301 if (value == NULL || *value == '\0')
306 mods = (LDAPMod **) malloc(sizeof(LDAPMod *));
309 DEBUG(0, ("make_a_mod: out of memory!\n"));
315 for (i = 0; mods[i] != NULL; ++i) {
316 if (mods[i]->mod_op == modop && !strcasecmp(mods[i]->mod_type, attribute))
322 mods = (LDAPMod **) Realloc (mods, (i + 2) * sizeof (LDAPMod *));
325 DEBUG(0, ("make_a_mod: out of memory!\n"));
328 mods[i] = (LDAPMod *) malloc(sizeof(LDAPMod));
331 DEBUG(0, ("make_a_mod: out of memory!\n"));
334 mods[i]->mod_op = modop;
335 mods[i]->mod_values = NULL;
336 mods[i]->mod_type = strdup(attribute);
343 if (mods[i]->mod_values != NULL) {
344 for (; mods[i]->mod_values[j] != NULL; j++);
346 mods[i]->mod_values = (char **)Realloc(mods[i]->mod_values,
347 (j + 2) * sizeof (char *));
349 if (mods[i]->mod_values == NULL) {
350 DEBUG (0, ("make_a_mod: Memory allocation failure!\n"));
353 mods[i]->mod_values[j] = strdup(value);
354 mods[i]->mod_values[j + 1] = NULL;
359 /* New Interface is being implemented here */
361 /**********************************************************************
362 Initialize SAM_ACCOUNT from an LDAP query
363 (Based on init_sam_from_buffer in pdb_tdb.c)
364 *********************************************************************/
365 static BOOL init_sam_from_ldap (SAM_ACCOUNT * sampass,
366 LDAP * ldap_struct, LDAPMessage * entry)
372 pass_can_change_time,
373 pass_must_change_time;
393 uint8 hours[MAX_HOURS_LEN];
396 gid_t gid = getegid();
400 * do a little initialization
404 nt_username[0] = '\0';
408 logon_script[0] = '\0';
409 profile_path[0] = '\0';
411 munged_dial[0] = '\0';
412 workstations[0] = '\0';
415 if (sampass == NULL || ldap_struct == NULL || entry == NULL) {
416 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
420 get_single_attribute(ldap_struct, entry, "uid", username);
421 DEBUG(2, ("Entry found for user: %s\n", username));
423 pstrcpy(nt_username, username);
425 pstrcpy(domain, lp_workgroup());
427 get_single_attribute(ldap_struct, entry, "pwdLastSet", temp);
428 pass_last_set_time = (time_t) strtol(temp, NULL, 16);
430 get_single_attribute(ldap_struct, entry, "logonTime", temp);
431 logon_time = (time_t) strtol(temp, NULL, 16);
433 get_single_attribute(ldap_struct, entry, "logoffTime", temp);
434 logoff_time = (time_t) strtol(temp, NULL, 16);
436 get_single_attribute(ldap_struct, entry, "kickoffTime", temp);
437 kickoff_time = (time_t) strtol(temp, NULL, 16);
439 get_single_attribute(ldap_struct, entry, "pwdCanChange", temp);
440 pass_can_change_time = (time_t) strtol(temp, NULL, 16);
442 get_single_attribute(ldap_struct, entry, "pwdMustChange", temp);
443 pass_must_change_time = (time_t) strtol(temp, NULL, 16);
445 /* recommend that 'gecos' and 'displayName' should refer to the same
446 * attribute OID. userFullName depreciated, only used by Samba
447 * primary rules of LDAP: don't make a new attribute when one is already defined
448 * that fits your needs; using cn then displayName rather than 'userFullName'
451 if (!get_single_attribute(ldap_struct, entry, "cn", fullname)) {
452 get_single_attribute(ldap_struct, entry, "displayName", fullname);
456 if (!get_single_attribute(ldap_struct, entry, "homeDrive", dir_drive)) {
457 pstrcpy(dir_drive, lp_logon_drive());
458 standard_sub_advanced(-1, username, "", gid, username, dir_drive);
459 DEBUG(5,("homeDrive fell back to %s\n",dir_drive));
460 pdb_set_dir_drive(sampass, dir_drive, False);
463 pdb_set_dir_drive(sampass, dir_drive, True);
465 if (!get_single_attribute(ldap_struct, entry, "smbHome", homedir)) {
466 pstrcpy(homedir, lp_logon_home());
467 standard_sub_advanced(-1, username, "", gid, username, homedir);
468 DEBUG(5,("smbHome fell back to %s\n",homedir));
469 pdb_set_homedir(sampass, homedir, False);
472 pdb_set_homedir(sampass, homedir, True);
474 if (!get_single_attribute(ldap_struct, entry, "scriptPath", logon_script)) {
475 pstrcpy(logon_script, lp_logon_script());
476 standard_sub_advanced(-1, username, "", gid, username, logon_script);
477 DEBUG(5,("scriptPath fell back to %s\n",logon_script));
478 pdb_set_logon_script(sampass, logon_script, False);
481 pdb_set_logon_script(sampass, logon_script, True);
483 if (!get_single_attribute(ldap_struct, entry, "profilePath", profile_path)) {
484 pstrcpy(profile_path, lp_logon_path());
485 standard_sub_advanced(-1, username, "", gid, username, profile_path);
486 DEBUG(5,("profilePath fell back to %s\n",profile_path));
487 pdb_set_profile_path(sampass, profile_path, False);
490 pdb_set_profile_path(sampass, profile_path, True);
492 get_single_attribute(ldap_struct, entry, "description", acct_desc);
493 get_single_attribute(ldap_struct, entry, "userWorkstations", workstations);
494 get_single_attribute(ldap_struct, entry, "rid", temp);
495 user_rid = (uint32)strtol(temp, NULL, 10);
496 get_single_attribute(ldap_struct, entry, "primaryGroupID", temp);
497 group_rid = (uint32)strtol(temp, NULL, 10);
500 /* These values MAY be in LDAP, but they can also be retrieved through
501 * sys_getpw*() which is how we're doing it
503 pw = getpwnam(username);
505 DEBUG (2,("init_sam_from_ldap: User [%s] does not ave a uid!\n", username));
511 /* FIXME: hours stuff should be cleaner */
515 memset(hours, 0xff, hours_len);
517 get_single_attribute (ldap_struct, entry, "lmPassword", temp);
518 pdb_gethexpwd(temp, smblmpwd);
519 memset((char *)temp, '\0', sizeof(temp));
520 get_single_attribute (ldap_struct, entry, "ntPassword", temp);
521 pdb_gethexpwd(temp, smbntpwd);
522 memset((char *)temp, '\0', sizeof(temp));
523 get_single_attribute (ldap_struct, entry, "acctFlags", temp);
524 acct_ctrl = pdb_decode_acct_ctrl(temp);
527 acct_ctrl |= ACB_NORMAL;
529 pdb_set_uid(sampass, uid);
530 pdb_set_gid(sampass, gid);
532 pdb_set_acct_ctrl(sampass, acct_ctrl);
533 pdb_set_logon_time(sampass, logon_time);
534 pdb_set_logoff_time(sampass, logoff_time);
535 pdb_set_kickoff_time(sampass, kickoff_time);
536 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
537 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
538 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
540 pdb_set_hours_len(sampass, hours_len);
541 pdb_set_logon_divs(sampass, logon_divs);
543 pdb_set_user_rid(sampass, user_rid);
544 pdb_set_group_rid(sampass, group_rid);
546 pdb_set_username(sampass, username);
548 pdb_set_domain(sampass, domain);
549 pdb_set_nt_username(sampass, nt_username);
551 pdb_set_fullname(sampass, fullname);
553 pdb_set_acct_desc(sampass, acct_desc);
554 pdb_set_workstations(sampass, workstations);
555 pdb_set_munged_dial(sampass, munged_dial);
557 if (!pdb_set_nt_passwd(sampass, smbntpwd))
559 if (!pdb_set_lanman_passwd(sampass, smblmpwd))
562 /* pdb_set_unknown_3(sampass, unknown3); */
563 /* pdb_set_unknown_5(sampass, unknown5); */
564 /* pdb_set_unknown_6(sampass, unknown6); */
566 pdb_set_hours(sampass, hours);
571 /**********************************************************************
572 Initialize SAM_ACCOUNT from an LDAP query
573 (Based on init_buffer_from_sam in pdb_tdb.c)
574 *********************************************************************/
575 static BOOL init_ldap_from_sam (LDAPMod *** mods, int ldap_state, const SAM_ACCOUNT * sampass)
579 if (mods == NULL || sampass == NULL) {
580 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
587 * took out adding "objectclass: sambaAccount"
588 * do this on a per-mod basis
592 make_a_mod(mods, ldap_state, "uid", pdb_get_username(sampass));
593 DEBUG(2, ("Setting entry for user: %s\n", pdb_get_username(sampass)));
595 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
596 make_a_mod(mods, ldap_state, "pwdLastSet", temp);
598 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
599 make_a_mod(mods, ldap_state, "logonTime", temp);
601 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
602 make_a_mod(mods, ldap_state, "logoffTime", temp);
604 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
605 make_a_mod(mods, ldap_state, "kickoffTime", temp);
607 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
608 make_a_mod(mods, ldap_state, "pwdCanChange", temp);
610 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
611 make_a_mod(mods, ldap_state, "pwdMustChange", temp);
613 /* displayName, cn, and gecos should all be the same
614 * most easily accomplished by giving them the same OID
615 * gecos isn't set here b/c it should be handled by the
619 make_a_mod(mods, ldap_state, "displayName", pdb_get_fullname(sampass));
620 make_a_mod(mods, ldap_state, "cn", pdb_get_fullname(sampass));
621 make_a_mod(mods, ldap_state, "description", pdb_get_acct_desc(sampass));
622 make_a_mod(mods, ldap_state, "userWorkstations", pdb_get_workstations(sampass));
625 * Only updates fields which have been set (not defaults from smb.conf)
628 if (IS_SAM_SET(sampass, FLAG_SAM_SMBHOME))
629 make_a_mod(mods, ldap_state, "smbHome", pdb_get_homedir(sampass));
631 if (IS_SAM_SET(sampass, FLAG_SAM_DRIVE))
632 make_a_mod(mods, ldap_state, "homeDrive", pdb_get_dirdrive(sampass));
634 if (IS_SAM_SET(sampass, FLAG_SAM_LOGONSCRIPT))
635 make_a_mod(mods, ldap_state, "scriptPath", pdb_get_logon_script(sampass));
637 if (IS_SAM_SET(sampass, FLAG_SAM_PROFILE))
638 make_a_mod(mods, ldap_state, "profilePath", pdb_get_profile_path(sampass));
641 if ( !pdb_get_user_rid(sampass))
642 slprintf(temp, sizeof(temp) - 1, "%i", pdb_uid_to_user_rid(pdb_get_uid(sampass)));
644 slprintf(temp, sizeof(temp) - 1, "%i", pdb_get_user_rid(sampass));
645 make_a_mod(mods, ldap_state, "rid", temp);
647 if ( !pdb_get_group_rid(sampass))
648 slprintf(temp, sizeof(temp) - 1, "%i", pdb_gid_to_group_rid(pdb_get_gid(sampass)));
650 slprintf(temp, sizeof(temp) - 1, "%i", pdb_get_group_rid(sampass));
651 make_a_mod(mods, ldap_state, "primaryGroupID", temp);
653 /* FIXME: Hours stuff goes in LDAP */
654 pdb_sethexpwd (temp, pdb_get_lanman_passwd(sampass), pdb_get_acct_ctrl(sampass));
655 make_a_mod (mods, ldap_state, "lmPassword", temp);
657 pdb_sethexpwd (temp, pdb_get_nt_passwd(sampass), pdb_get_acct_ctrl(sampass));
658 make_a_mod (mods, ldap_state, "ntPassword", temp);
660 make_a_mod (mods, ldap_state, "acctFlags", pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass),
661 NEW_PW_FORMAT_SPACE_PADDED_LEN));
666 /**********************************************************************
667 Connect to LDAP server for password enumeration
668 *********************************************************************/
669 BOOL pdb_setsampwent(BOOL update)
674 if (!ldap_open_connection(&global_ldap_ent.ldap_struct))
678 if (!ldap_connect_system(global_ldap_ent.ldap_struct))
680 ldap_unbind(global_ldap_ent.ldap_struct);
684 pstrcpy(filter, lp_ldap_filter());
685 all_string_sub(filter, "%u", "*", sizeof(pstring));
687 rc = ldap_search_s(global_ldap_ent.ldap_struct, lp_ldap_suffix(),
688 LDAP_SCOPE_SUBTREE, filter, NULL, 0,
689 &global_ldap_ent.result);
691 if (rc != LDAP_SUCCESS)
693 DEBUG(0, ("LDAP search failed: %s\n", ldap_err2string(rc)));
694 DEBUG(3, ("Query was: %s, %s\n", lp_ldap_suffix(), filter));
695 ldap_msgfree(global_ldap_ent.result);
696 ldap_unbind(global_ldap_ent.ldap_struct);
697 global_ldap_ent.ldap_struct = NULL;
698 global_ldap_ent.result = NULL;
702 DEBUG(2, ("pdb_setsampwent: %d entries in the base!\n",
703 ldap_count_entries(global_ldap_ent.ldap_struct,
704 global_ldap_ent.result)));
706 global_ldap_ent.entry = ldap_first_entry(global_ldap_ent.ldap_struct,
707 global_ldap_ent.result);
712 /**********************************************************************
713 End enumeration of the LDAP password list
714 *********************************************************************/
715 void pdb_endsampwent(void)
717 if (global_ldap_ent.ldap_struct && global_ldap_ent.result)
719 ldap_msgfree(global_ldap_ent.result);
720 ldap_unbind(global_ldap_ent.ldap_struct);
721 global_ldap_ent.ldap_struct = NULL;
722 global_ldap_ent.result = NULL;
726 /**********************************************************************
727 Get the next entry in the LDAP password database
728 *********************************************************************/
729 BOOL pdb_getsampwent(SAM_ACCOUNT * user)
731 if (!global_ldap_ent.entry)
734 global_ldap_ent.entry = ldap_next_entry(global_ldap_ent.ldap_struct,
735 global_ldap_ent.entry);
737 if (global_ldap_ent.entry != NULL)
739 return init_sam_from_ldap(user, global_ldap_ent.ldap_struct,
740 global_ldap_ent.entry);
745 /**********************************************************************
746 Get SAM_ACCOUNT entry from LDAP by username
747 *********************************************************************/
748 BOOL pdb_getsampwnam(SAM_ACCOUNT * user, const char *sname)
754 if (!ldap_open_connection(&ldap_struct))
756 if (!ldap_connect_system(ldap_struct))
758 ldap_unbind(ldap_struct);
761 if (ldap_search_one_user_by_name(ldap_struct, sname, &result) != LDAP_SUCCESS)
763 ldap_unbind(ldap_struct);
766 if (ldap_count_entries(ldap_struct, result) < 1)
769 ("We don't find this user [%s] count=%d\n", sname,
770 ldap_count_entries(ldap_struct, result)));
771 ldap_unbind(ldap_struct);
774 entry = ldap_first_entry(ldap_struct, result);
777 init_sam_from_ldap(user, ldap_struct, entry);
778 ldap_msgfree(result);
779 ldap_unbind(ldap_struct);
784 ldap_msgfree(result);
785 ldap_unbind(ldap_struct);
790 /**********************************************************************
791 Get SAM_ACCOUNT entry from LDAP by rid
792 *********************************************************************/
793 BOOL pdb_getsampwrid(SAM_ACCOUNT * user, uint32 rid)
799 if (!ldap_open_connection(&ldap_struct))
802 if (!ldap_connect_system(ldap_struct))
804 ldap_unbind(ldap_struct);
807 if (ldap_search_one_user_by_rid(ldap_struct, rid, &result) !=
810 ldap_unbind(ldap_struct);
814 if (ldap_count_entries(ldap_struct, result) < 1)
817 ("We don't find this rid [%i] count=%d\n", rid,
818 ldap_count_entries(ldap_struct, result)));
819 ldap_unbind(ldap_struct);
823 entry = ldap_first_entry(ldap_struct, result);
826 init_sam_from_ldap(user, ldap_struct, entry);
827 ldap_msgfree(result);
828 ldap_unbind(ldap_struct);
833 ldap_msgfree(result);
834 ldap_unbind(ldap_struct);
839 /**********************************************************************
840 Delete entry from LDAP for username
841 *********************************************************************/
842 BOOL pdb_delete_sam_account(SAM_ACCOUNT * sam_acct)
852 DEBUG(0, ("sam_acct was NULL!\n"));
856 sname = pdb_get_username(sam_acct);
858 if (!ldap_open_connection (&ldap_struct))
861 DEBUG (3, ("Deleting user %s from LDAP.\n", sname));
863 if (!ldap_connect_system (ldap_struct)) {
864 ldap_unbind (ldap_struct);
865 DEBUG(0, ("Failed to delete user %s from LDAP.\n", sname));
869 rc = ldap_search_one_user_by_name (ldap_struct, sname, &result);
870 if (ldap_count_entries (ldap_struct, result) == 0) {
871 DEBUG (0, ("User doesn't exit!\n"));
872 ldap_msgfree (result);
873 ldap_unbind (ldap_struct);
877 entry = ldap_first_entry (ldap_struct, result);
878 dn = ldap_get_dn (ldap_struct, entry);
880 rc = ldap_delete_s (ldap_struct, dn);
883 if (rc != LDAP_SUCCESS) {
885 ldap_get_option (ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error);
886 DEBUG (0,("failed to delete user with uid = %s with: %s\n\t%s\n",
887 sname, ldap_err2string (rc), ld_error));
889 ldap_unbind (ldap_struct);
893 DEBUG (2,("successfully deleted uid = %s from the LDAP database\n", sname));
894 ldap_unbind (ldap_struct);
898 /**********************************************************************
900 *********************************************************************/
901 BOOL pdb_update_sam_account(SAM_ACCOUNT * newpwd)
910 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
913 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
915 ldap_unbind(ldap_struct);
919 rc = ldap_search_one_user_by_name(ldap_struct,
920 pdb_get_username(newpwd), &result);
922 if (ldap_count_entries(ldap_struct, result) == 0)
924 DEBUG(0, ("No user to modify!\n"));
925 ldap_msgfree(result);
926 ldap_unbind(ldap_struct);
930 init_ldap_from_sam(&mods, LDAP_MOD_REPLACE, newpwd);
932 entry = ldap_first_entry(ldap_struct, result);
933 dn = ldap_get_dn(ldap_struct, entry);
935 rc = ldap_modify_s(ldap_struct, dn, mods);
937 if (rc != LDAP_SUCCESS)
940 ldap_get_option(ldap_struct, LDAP_OPT_ERROR_STRING,
943 ("failed to modify user with uid = %s with: %s\n\t%s\n",
944 pdb_get_username(newpwd), ldap_err2string(rc),
947 ldap_unbind(ldap_struct);
952 ("successfully modified uid = %s in the LDAP database\n",
953 pdb_get_username(newpwd)));
954 ldap_mods_free(mods, 1);
955 ldap_unbind(ldap_struct);
959 /**********************************************************************
960 Add SAM_ACCOUNT to LDAP
961 *********************************************************************/
962 BOOL pdb_add_sam_account(SAM_ACCOUNT * newpwd)
973 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
978 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
980 ldap_unbind(ldap_struct);
984 rc = ldap_search_one_user_by_name (ldap_struct, pdb_get_username(newpwd), &result);
986 if (ldap_count_entries(ldap_struct, result) != 0)
988 DEBUG(0,("User already in the base, with samba properties\n"));
989 ldap_msgfree(result);
990 ldap_unbind(ldap_struct);
993 ldap_msgfree(result);
995 slprintf (filter, sizeof (filter) - 1, "uid=%s", pdb_get_username(newpwd));
996 rc = ldap_search_one_user(ldap_struct, filter, &result);
997 num_result = ldap_count_entries(ldap_struct, result);
999 if (num_result > 1) {
1000 DEBUG (0, ("More than one user with that uid exists: bailing out!\n"));
1004 /* Check if we need to update an existing entry */
1005 if (num_result == 1) {
1009 DEBUG(3,("User exists without samba properties: adding them\n"));
1010 ldap_op = LDAP_MOD_REPLACE;
1011 entry = ldap_first_entry (ldap_struct, result);
1012 tmp = ldap_get_dn (ldap_struct, entry);
1013 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1017 /* Check if we need to add an entry */
1018 DEBUG(3,("Adding new user\n"));
1019 ldap_op = LDAP_MOD_ADD;
1020 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", pdb_get_username(newpwd), lp_ldap_suffix ());
1023 ldap_msgfree(result);
1025 init_ldap_from_sam(&mods, ldap_op, newpwd);
1026 make_a_mod(&mods, LDAP_MOD_ADD, "objectclass", "sambaAccount");
1028 if (ldap_op == LDAP_MOD_REPLACE) {
1029 rc = ldap_modify_s(ldap_struct, dn, mods);
1032 rc = ldap_add_s(ldap_struct, dn, mods);
1035 if (rc != LDAP_SUCCESS)
1039 ldap_get_option (ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error);
1040 DEBUG(0,("failed to modify user with uid = %s with: %s\n\t%s\n",
1041 pdb_get_username(newpwd), ldap_err2string (rc), ld_error));
1043 ldap_mods_free(mods, 1);
1044 ldap_unbind(ldap_struct);
1048 DEBUG(2,("added: uid = %s in the LDAP database\n", pdb_get_username(newpwd)));
1049 ldap_mods_free(mods, 1);
1050 ldap_unbind(ldap_struct);
1055 void dummy_function(void);
1057 dummy_function (void)
1059 } /* stop some compilers complaining */