2 Unix SMB/CIFS mplementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/SAM_ACCOUNT
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
49 #define DBGC_CLASS DBGC_PASSDB
55 * Work around versions of the LDAP client libs that don't have the OIDs
56 * defined, or have them defined under the old name.
57 * This functionality is really a factor of the server, not the client
61 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
62 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
63 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
64 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
67 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
68 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
69 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
70 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
73 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
75 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
76 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
81 #define SAM_ACCOUNT struct sam_passwd
86 struct ldapsam_privates {
87 struct smbldap_state *smbldap_state;
94 const char *domain_name;
97 /* configuration items */
101 /**********************************************************************
102 Free a LDAPMessage (one is stored on the SAM_ACCOUNT).
103 **********************************************************************/
105 static void private_data_free_fn(void **result)
107 ldap_msgfree(*result);
111 /**********************************************************************
112 Get the attribute name given a user schame version.
113 **********************************************************************/
115 static const char* get_userattr_key2string( int schema_ver, int key )
117 switch ( schema_ver ) {
118 case SCHEMAVER_SAMBAACCOUNT:
119 return get_attr_key2string( attrib_map_v22, key );
121 case SCHEMAVER_SAMBASAMACCOUNT:
122 return get_attr_key2string( attrib_map_v30, key );
125 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
131 /**********************************************************************
132 Return the list of attribute names given a user schema version.
133 **********************************************************************/
135 static char** get_userattr_list( int schema_ver )
137 switch ( schema_ver ) {
138 case SCHEMAVER_SAMBAACCOUNT:
139 return get_attr_list( attrib_map_v22 );
141 case SCHEMAVER_SAMBASAMACCOUNT:
142 return get_attr_list( attrib_map_v30 );
144 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
151 /**************************************************************************
152 Return the list of attribute names to delete given a user schema version.
153 **************************************************************************/
155 static char** get_userattr_delete_list( int schema_ver )
157 switch ( schema_ver ) {
158 case SCHEMAVER_SAMBAACCOUNT:
159 return get_attr_list( attrib_map_to_delete_v22 );
161 case SCHEMAVER_SAMBASAMACCOUNT:
162 return get_attr_list( attrib_map_to_delete_v30 );
164 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
172 /*******************************************************************
173 Generate the LDAP search filter for the objectclass based on the
174 version of the schema we are using.
175 ******************************************************************/
177 static const char* get_objclass_filter( int schema_ver )
179 static fstring objclass_filter;
181 switch( schema_ver ) {
182 case SCHEMAVER_SAMBAACCOUNT:
183 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
185 case SCHEMAVER_SAMBASAMACCOUNT:
186 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
189 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
193 return objclass_filter;
196 /*******************************************************************
197 Run the search by name.
198 ******************************************************************/
200 static int ldapsam_search_suffix_by_name (struct ldapsam_privates *ldap_state,
202 LDAPMessage ** result, char **attr)
205 char *escape_user = escape_ldap_string_alloc(user);
208 return LDAP_NO_MEMORY;
212 * in the filter expression, replace %u with the real name
213 * so in ldap filter, %u MUST exist :-)
215 pstr_sprintf(filter, "(&%s%s)", lp_ldap_filter(),
216 get_objclass_filter(ldap_state->schema_ver));
219 * have to use this here because $ is filtered out
224 all_string_sub(filter, "%u", escape_user, sizeof(pstring));
225 SAFE_FREE(escape_user);
227 return smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
230 /*******************************************************************
231 Run the search by rid.
232 ******************************************************************/
234 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
235 uint32 rid, LDAPMessage ** result,
241 pstr_sprintf(filter, "(&(rid=%i)%s)", rid,
242 get_objclass_filter(ldap_state->schema_ver));
244 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
249 /*******************************************************************
250 Run the search by SID.
251 ******************************************************************/
253 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
254 const DOM_SID *sid, LDAPMessage ** result,
261 pstr_sprintf(filter, "(&(%s=%s)%s)",
262 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
263 sid_to_string(sid_string, sid),
264 get_objclass_filter(ldap_state->schema_ver));
266 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
271 /*******************************************************************
272 Delete complete object or objectclass and attrs from
273 object found in search_result depending on lp_ldap_delete_dn
274 ******************************************************************/
276 static NTSTATUS ldapsam_delete_entry(struct ldapsam_privates *ldap_state,
278 const char *objectclass,
282 LDAPMessage *entry = NULL;
283 LDAPMod **mods = NULL;
285 BerElement *ptr = NULL;
287 rc = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
290 DEBUG(0, ("ldapsam_delete_entry: Entry must exist exactly once!\n"));
291 return NT_STATUS_UNSUCCESSFUL;
294 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
295 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
297 return NT_STATUS_UNSUCCESSFUL;
300 if (lp_ldap_delete_dn()) {
301 NTSTATUS ret = NT_STATUS_OK;
302 rc = smbldap_delete(ldap_state->smbldap_state, dn);
304 if (rc != LDAP_SUCCESS) {
305 DEBUG(0, ("ldapsam_delete_entry: Could not delete object %s\n", dn));
306 ret = NT_STATUS_UNSUCCESSFUL;
312 /* Ok, delete only the SAM attributes */
314 for (name = ldap_first_attribute(ldap_state->smbldap_state->ldap_struct, entry, &ptr);
316 name = ldap_next_attribute(ldap_state->smbldap_state->ldap_struct, entry, ptr)) {
319 /* We are only allowed to delete the attributes that
322 for (attrib = attrs; *attrib != NULL; attrib++) {
323 /* Don't delete LDAP_ATTR_MOD_TIMESTAMP attribute. */
324 if (strequal(*attrib, get_userattr_key2string(ldap_state->schema_ver,
325 LDAP_ATTR_MOD_TIMESTAMP))) {
328 if (strequal(*attrib, name)) {
329 DEBUG(10, ("ldapsam_delete_entry: deleting "
330 "attribute %s\n", name));
331 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
343 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
345 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
346 ldap_mods_free(mods, True);
348 if (rc != LDAP_SUCCESS) {
349 char *ld_error = NULL;
350 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
353 DEBUG(0, ("ldapsam_delete_entry: Could not delete attributes for %s, error: %s (%s)\n",
354 dn, ldap_err2string(rc), ld_error?ld_error:"unknown"));
357 return NT_STATUS_UNSUCCESSFUL;
364 /* New Interface is being implemented here */
366 #if 0 /* JERRY - not uesed anymore */
368 /**********************************************************************
369 Initialize SAM_ACCOUNT from an LDAP query (unix attributes only)
370 *********************************************************************/
371 static BOOL get_unix_attributes (struct ldapsam_privates *ldap_state,
372 SAM_ACCOUNT * sampass,
381 if ((ldap_values = ldap_get_values (ldap_state->smbldap_state->ldap_struct, entry, "objectClass")) == NULL) {
382 DEBUG (1, ("get_unix_attributes: no objectClass! \n"));
386 for (values=ldap_values;*values;values++) {
387 if (strequal(*values, LDAP_OBJ_POSIXACCOUNT )) {
392 if (!*values) { /*end of array, no posixAccount */
393 DEBUG(10, ("user does not have %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
394 ldap_value_free(ldap_values);
397 ldap_value_free(ldap_values);
399 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
400 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_UNIX_HOME), homedir) )
405 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
406 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_GIDNUMBER), temp) )
411 *gid = (gid_t)atol(temp);
413 pdb_set_unix_homedir(sampass, homedir, PDB_SET);
415 DEBUG(10, ("user has %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
422 static time_t ldapsam_get_entry_timestamp(
423 struct ldapsam_privates *ldap_state,
429 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
430 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
434 strptime(temp, "%Y%m%d%H%M%SZ", &tm);
439 /**********************************************************************
440 Initialize SAM_ACCOUNT from an LDAP query.
441 (Based on init_sam_from_buffer in pdb_tdb.c)
442 *********************************************************************/
444 static BOOL init_sam_from_ldap (struct ldapsam_privates *ldap_state,
445 SAM_ACCOUNT * sampass,
452 pass_can_change_time,
453 pass_must_change_time,
466 char munged_dial[2048];
468 uint8 smblmpwd[LM_HASH_LEN],
469 smbntpwd[NT_HASH_LEN];
470 uint16 acct_ctrl = 0,
472 uint16 bad_password_count = 0,
475 uint8 hours[MAX_HOURS_LEN];
477 LOGIN_CACHE *cache_entry = NULL;
481 * do a little initialization
485 nt_username[0] = '\0';
489 logon_script[0] = '\0';
490 profile_path[0] = '\0';
492 munged_dial[0] = '\0';
493 workstations[0] = '\0';
496 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
497 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
501 if (ldap_state->smbldap_state->ldap_struct == NULL) {
502 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->ldap_struct is NULL!\n"));
506 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry, "uid", username)) {
507 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for this user!\n"));
511 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
513 pstrcpy(nt_username, username);
515 pstrcpy(domain, ldap_state->domain_name);
517 pdb_set_username(sampass, username, PDB_SET);
519 pdb_set_domain(sampass, domain, PDB_DEFAULT);
520 pdb_set_nt_username(sampass, nt_username, PDB_SET);
522 /* deal with different attributes between the schema first */
524 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
525 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
526 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID), temp)) {
527 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
530 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
531 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_SID), temp)) {
532 pdb_set_group_sid_from_string(sampass, temp, PDB_SET);
534 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
537 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
538 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID), temp)) {
539 user_rid = (uint32)atol(temp);
540 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
543 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
544 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_RID), temp)) {
545 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
549 group_rid = (uint32)atol(temp);
551 /* for some reason, we often have 0 as a primary group RID.
552 Make sure that we treat this just as a 'default' value */
555 pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
557 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
561 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
562 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
563 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
564 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
569 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
570 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET), temp)) {
571 /* leave as default */
573 pass_last_set_time = (time_t) atol(temp);
574 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
577 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
578 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp)) {
579 /* leave as default */
581 logon_time = (time_t) atol(temp);
582 pdb_set_logon_time(sampass, logon_time, PDB_SET);
585 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
586 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp)) {
587 /* leave as default */
589 logoff_time = (time_t) atol(temp);
590 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
593 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
594 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp)) {
595 /* leave as default */
597 kickoff_time = (time_t) atol(temp);
598 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
601 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
602 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp)) {
603 /* leave as default */
605 pass_can_change_time = (time_t) atol(temp);
606 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
609 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
610 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp)) {
611 /* leave as default */
613 pass_must_change_time = (time_t) atol(temp);
614 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
617 /* recommend that 'gecos' and 'displayName' should refer to the same
618 * attribute OID. userFullName depreciated, only used by Samba
619 * primary rules of LDAP: don't make a new attribute when one is already defined
620 * that fits your needs; using cn then displayName rather than 'userFullName'
623 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
624 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME), fullname)) {
625 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
626 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_CN), fullname)) {
627 /* leave as default */
629 pdb_set_fullname(sampass, fullname, PDB_SET);
632 pdb_set_fullname(sampass, fullname, PDB_SET);
635 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
636 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE), dir_drive))
638 pdb_set_dir_drive( sampass,
639 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_drive()),
642 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
645 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
646 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH), homedir))
648 pdb_set_homedir( sampass,
649 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_home()),
652 pdb_set_homedir(sampass, homedir, PDB_SET);
655 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
656 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT), logon_script))
658 pdb_set_logon_script( sampass,
659 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_script()),
662 pdb_set_logon_script(sampass, logon_script, PDB_SET);
665 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
666 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH), profile_path))
668 pdb_set_profile_path( sampass,
669 talloc_sub_basic( sampass->mem_ctx, username, lp_logon_path()),
672 pdb_set_profile_path(sampass, profile_path, PDB_SET);
675 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
676 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC), acct_desc))
678 /* leave as default */
680 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
683 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
684 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS), workstations)) {
685 /* leave as default */;
687 pdb_set_workstations(sampass, workstations, PDB_SET);
690 if (!smbldap_get_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
691 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL), munged_dial, sizeof(munged_dial))) {
692 /* leave as default */;
694 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
697 /* FIXME: hours stuff should be cleaner */
701 memset(hours, 0xff, hours_len);
703 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
704 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW), temp)) {
705 /* leave as default */
707 pdb_gethexpwd(temp, smblmpwd);
708 memset((char *)temp, '\0', strlen(temp)+1);
709 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET))
711 ZERO_STRUCT(smblmpwd);
714 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
715 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW), temp)) {
716 /* leave as default */
718 pdb_gethexpwd(temp, smbntpwd);
719 memset((char *)temp, '\0', strlen(temp)+1);
720 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET))
722 ZERO_STRUCT(smbntpwd);
725 account_policy_get(AP_PASSWORD_HISTORY, &pwHistLen);
727 uint8 *pwhist = NULL;
730 if ((pwhist = malloc(pwHistLen * PW_HISTORY_ENTRY_LEN)) == NULL){
731 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
734 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
736 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
737 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY), temp)) {
738 /* leave as default - zeros */
740 BOOL hex_failed = False;
741 for (i = 0; i < pwHistLen; i++){
742 /* Get the 16 byte salt. */
743 if (!pdb_gethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
747 /* Get the 16 byte MD5 hash of salt+passwd. */
748 if (!pdb_gethexpwd(&temp[(i*64)+32],
749 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN])) {
755 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
757 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
760 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
767 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
768 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO), temp)) {
769 acct_ctrl |= ACB_NORMAL;
771 acct_ctrl = pdb_decode_acct_ctrl(temp);
774 acct_ctrl |= ACB_NORMAL;
776 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
779 pdb_set_hours_len(sampass, hours_len, PDB_SET);
780 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
782 /* pdb_set_munged_dial(sampass, munged_dial, PDB_SET); */
784 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
785 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_COUNT), temp)) {
786 /* leave as default */
788 bad_password_count = (uint32) atol(temp);
789 pdb_set_bad_password_count(sampass, bad_password_count, PDB_SET);
792 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
793 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_TIME), temp)) {
794 /* leave as default */
796 bad_password_time = (time_t) atol(temp);
797 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
801 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
802 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_COUNT), temp)) {
803 /* leave as default */
805 logon_count = (uint32) atol(temp);
806 pdb_set_logon_count(sampass, logon_count, PDB_SET);
809 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
811 if(!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
812 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_HOURS), temp)) {
813 /* leave as default */
815 pdb_gethexhours(temp, hours);
816 memset((char *)temp, '\0', strlen(temp) +1);
817 pdb_set_hours(sampass, hours, PDB_SET);
821 /* check the timestamp of the cache vs ldap entry */
822 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
826 /* see if we have newer updates */
827 if (!(cache_entry = login_cache_read(sampass))) {
828 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
829 (unsigned int)pdb_get_bad_password_count(sampass),
830 (unsigned int)pdb_get_bad_password_time(sampass)));
834 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
835 (unsigned int)ldap_entry_time, (unsigned int)cache_entry->entry_timestamp,
836 (unsigned int)cache_entry->bad_password_time));
838 if (ldap_entry_time > cache_entry->entry_timestamp) {
839 /* cache is older than directory , so
840 we need to delete the entry but allow the
841 fields to be written out */
842 login_cache_delentry(sampass);
845 pdb_set_acct_ctrl(sampass,
846 pdb_get_acct_ctrl(sampass) |
847 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
849 pdb_set_bad_password_count(sampass,
850 cache_entry->bad_password_count,
852 pdb_set_bad_password_time(sampass,
853 cache_entry->bad_password_time,
857 SAFE_FREE(cache_entry);
861 /**********************************************************************
862 Initialize the ldap db from a SAM_ACCOUNT. Called on update.
863 (Based on init_buffer_from_sam in pdb_tdb.c)
864 *********************************************************************/
866 static BOOL init_ldap_from_sam (struct ldapsam_privates *ldap_state,
867 LDAPMessage *existing,
868 LDAPMod *** mods, SAM_ACCOUNT * sampass,
869 BOOL (*need_update)(const SAM_ACCOUNT *,
875 if (mods == NULL || sampass == NULL) {
876 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
883 * took out adding "objectclass: sambaAccount"
884 * do this on a per-mod basis
886 if (need_update(sampass, PDB_USERNAME))
887 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
888 "uid", pdb_get_username(sampass));
890 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
892 /* only update the RID if we actually need to */
893 if (need_update(sampass, PDB_USERSID)) {
895 fstring dom_sid_string;
896 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
898 switch ( ldap_state->schema_ver ) {
899 case SCHEMAVER_SAMBAACCOUNT:
900 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
901 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
902 sid_to_string(sid_string, user_sid),
903 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
906 slprintf(temp, sizeof(temp) - 1, "%i", rid);
907 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
908 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
912 case SCHEMAVER_SAMBASAMACCOUNT:
913 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
914 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
915 sid_to_string(sid_string, user_sid));
919 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
924 /* we don't need to store the primary group RID - so leaving it
925 'free' to hang off the unix primary group makes life easier */
927 if (need_update(sampass, PDB_GROUPSID)) {
929 fstring dom_sid_string;
930 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
932 switch ( ldap_state->schema_ver ) {
933 case SCHEMAVER_SAMBAACCOUNT:
934 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
935 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
936 sid_to_string(sid_string, group_sid),
937 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
941 slprintf(temp, sizeof(temp) - 1, "%i", rid);
942 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
943 get_userattr_key2string(ldap_state->schema_ver,
944 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
947 case SCHEMAVER_SAMBASAMACCOUNT:
948 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
949 get_userattr_key2string(ldap_state->schema_ver,
950 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_string(sid_string, group_sid));
954 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
960 /* displayName, cn, and gecos should all be the same
961 * most easily accomplished by giving them the same OID
962 * gecos isn't set here b/c it should be handled by the
964 * We change displayName only and fall back to cn if
968 if (need_update(sampass, PDB_FULLNAME))
969 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
970 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
971 pdb_get_fullname(sampass));
973 if (need_update(sampass, PDB_ACCTDESC))
974 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
975 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
976 pdb_get_acct_desc(sampass));
978 if (need_update(sampass, PDB_WORKSTATIONS))
979 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
980 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
981 pdb_get_workstations(sampass));
983 if (need_update(sampass, PDB_MUNGEDDIAL))
984 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
985 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
986 pdb_get_munged_dial(sampass));
988 if (need_update(sampass, PDB_SMBHOME))
989 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
990 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
991 pdb_get_homedir(sampass));
993 if (need_update(sampass, PDB_DRIVE))
994 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
995 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
996 pdb_get_dir_drive(sampass));
998 if (need_update(sampass, PDB_LOGONSCRIPT))
999 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1000 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1001 pdb_get_logon_script(sampass));
1003 if (need_update(sampass, PDB_PROFILE))
1004 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1005 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1006 pdb_get_profile_path(sampass));
1008 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
1009 if (need_update(sampass, PDB_LOGONTIME))
1010 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1011 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1013 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
1014 if (need_update(sampass, PDB_LOGOFFTIME))
1015 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1016 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1018 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
1019 if (need_update(sampass, PDB_KICKOFFTIME))
1020 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1021 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1023 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
1024 if (need_update(sampass, PDB_CANCHANGETIME))
1025 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1026 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1028 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
1029 if (need_update(sampass, PDB_MUSTCHANGETIME))
1030 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1031 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1034 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1035 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1037 if (need_update(sampass, PDB_LMPASSWD)) {
1038 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1040 pdb_sethexpwd(temp, lm_pw,
1041 pdb_get_acct_ctrl(sampass));
1042 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1043 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1046 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1047 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1051 if (need_update(sampass, PDB_NTPASSWD)) {
1052 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1054 pdb_sethexpwd(temp, nt_pw,
1055 pdb_get_acct_ctrl(sampass));
1056 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1057 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1060 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1061 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1066 if (need_update(sampass, PDB_PWHISTORY)) {
1068 account_policy_get(AP_PASSWORD_HISTORY, &pwHistLen);
1069 if (pwHistLen == 0) {
1070 /* Remove any password history from the LDAP store. */
1071 memset(temp, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1074 int i, currHistLen = 0;
1075 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1076 if (pwhist != NULL) {
1077 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1078 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
1079 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1080 /* Store the salt. */
1081 pdb_sethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1082 /* Followed by the md5 hash of salt + md4 hash */
1083 pdb_sethexpwd(&temp[(i*64)+32],
1084 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1085 DEBUG(100, ("temp=%s\n", temp));
1089 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1090 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1094 if (need_update(sampass, PDB_PASSLASTSET)) {
1095 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
1096 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1097 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1102 if (need_update(sampass, PDB_HOURS)) {
1103 const char *hours = pdb_get_hours(sampass);
1105 pdb_sethexhours(temp, hours);
1106 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1109 get_userattr_key2string(ldap_state->schema_ver,
1110 LDAP_ATTR_LOGON_HOURS),
1115 if (need_update(sampass, PDB_ACCTCTRL))
1116 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1117 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1118 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1120 /* password lockout cache:
1121 - If we are now autolocking or clearing, we write to ldap
1122 - If we are clearing, we delete the cache entry
1123 - If the count is > 0, we update the cache
1125 This even means when autolocking, we cache, just in case the
1126 update doesn't work, and we have to cache the autolock flag */
1128 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1129 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1130 uint16 badcount = pdb_get_bad_password_count(sampass);
1131 time_t badtime = pdb_get_bad_password_time(sampass);
1133 account_policy_get(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1135 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1136 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1138 if ((badcount >= pol) || (badcount == 0)) {
1139 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1140 (unsigned int)badcount, (unsigned int)badtime));
1141 slprintf (temp, sizeof (temp) - 1, "%li", (long)badcount);
1143 ldap_state->smbldap_state->ldap_struct,
1145 get_userattr_key2string(
1146 ldap_state->schema_ver,
1147 LDAP_ATTR_BAD_PASSWORD_COUNT),
1150 slprintf (temp, sizeof (temp) - 1, "%li", badtime);
1152 ldap_state->smbldap_state->ldap_struct,
1154 get_userattr_key2string(
1155 ldap_state->schema_ver,
1156 LDAP_ATTR_BAD_PASSWORD_TIME),
1159 if (badcount == 0) {
1160 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1161 login_cache_delentry(sampass);
1163 LOGIN_CACHE cache_entry;
1165 cache_entry.entry_timestamp = time(NULL);
1166 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1167 cache_entry.bad_password_count = badcount;
1168 cache_entry.bad_password_time = badtime;
1170 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1171 login_cache_write(sampass, cache_entry);
1178 /**********************************************************************
1179 Connect to LDAP server for password enumeration.
1180 *********************************************************************/
1182 static NTSTATUS ldapsam_setsampwent(struct pdb_methods *my_methods, BOOL update)
1184 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1189 pstr_sprintf( filter, "(&%s%s)", lp_ldap_filter(),
1190 get_objclass_filter(ldap_state->schema_ver));
1191 all_string_sub(filter, "%u", "*", sizeof(pstring));
1193 attr_list = get_userattr_list(ldap_state->schema_ver);
1194 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
1195 attr_list, &ldap_state->result);
1196 free_attr_list( attr_list );
1198 if (rc != LDAP_SUCCESS) {
1199 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc)));
1200 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", lp_ldap_suffix(), filter));
1201 ldap_msgfree(ldap_state->result);
1202 ldap_state->result = NULL;
1203 return NT_STATUS_UNSUCCESSFUL;
1206 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base!\n",
1207 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
1208 ldap_state->result)));
1210 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
1211 ldap_state->result);
1212 ldap_state->index = 0;
1214 return NT_STATUS_OK;
1217 /**********************************************************************
1218 End enumeration of the LDAP password list.
1219 *********************************************************************/
1221 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1223 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1224 if (ldap_state->result) {
1225 ldap_msgfree(ldap_state->result);
1226 ldap_state->result = NULL;
1230 /**********************************************************************
1231 Get the next entry in the LDAP password database.
1232 *********************************************************************/
1234 static NTSTATUS ldapsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
1236 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1237 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1241 if (!ldap_state->entry)
1244 ldap_state->index++;
1245 bret = init_sam_from_ldap(ldap_state, user, ldap_state->entry);
1247 ldap_state->entry = ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
1251 return NT_STATUS_OK;
1254 static void append_attr(char ***attr_list, const char *new_attr)
1258 if (new_attr == NULL) {
1262 for (i=0; (*attr_list)[i] != NULL; i++) {
1266 (*attr_list) = Realloc((*attr_list), sizeof(**attr_list) * (i+2));
1267 SMB_ASSERT((*attr_list) != NULL);
1268 (*attr_list)[i] = strdup(new_attr);
1269 (*attr_list)[i+1] = NULL;
1272 /**********************************************************************
1273 Get SAM_ACCOUNT entry from LDAP by username.
1274 *********************************************************************/
1276 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
1278 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1279 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1280 LDAPMessage *result = NULL;
1281 LDAPMessage *entry = NULL;
1286 attr_list = get_userattr_list( ldap_state->schema_ver );
1287 append_attr(&attr_list, get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP));
1288 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result, attr_list);
1289 free_attr_list( attr_list );
1291 if ( rc != LDAP_SUCCESS )
1292 return NT_STATUS_NO_SUCH_USER;
1294 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1297 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1298 ldap_msgfree(result);
1299 return NT_STATUS_NO_SUCH_USER;
1300 } else if (count > 1) {
1301 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1302 ldap_msgfree(result);
1303 return NT_STATUS_NO_SUCH_USER;
1306 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1308 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1309 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1310 ldap_msgfree(result);
1311 return NT_STATUS_NO_SUCH_USER;
1313 pdb_set_backend_private_data(user, result,
1314 private_data_free_fn,
1315 my_methods, PDB_CHANGED);
1318 ldap_msgfree(result);
1323 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1324 const DOM_SID *sid, LDAPMessage **result)
1330 switch ( ldap_state->schema_ver ) {
1331 case SCHEMAVER_SAMBASAMACCOUNT:
1332 attr_list = get_userattr_list(ldap_state->schema_ver);
1333 append_attr(&attr_list, get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP));
1334 rc = ldapsam_search_suffix_by_sid(ldap_state, sid, result, attr_list);
1335 free_attr_list( attr_list );
1337 if ( rc != LDAP_SUCCESS )
1341 case SCHEMAVER_SAMBAACCOUNT:
1342 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1346 attr_list = get_userattr_list(ldap_state->schema_ver);
1347 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1348 free_attr_list( attr_list );
1350 if ( rc != LDAP_SUCCESS )
1357 /**********************************************************************
1358 Get SAM_ACCOUNT entry from LDAP by SID.
1359 *********************************************************************/
1361 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1363 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1364 LDAPMessage *result = NULL;
1365 LDAPMessage *entry = NULL;
1370 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1372 if (rc != LDAP_SUCCESS)
1373 return NT_STATUS_NO_SUCH_USER;
1375 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1378 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string, sid),
1380 ldap_msgfree(result);
1381 return NT_STATUS_NO_SUCH_USER;
1382 } else if (count > 1) {
1383 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string, sid),
1385 ldap_msgfree(result);
1386 return NT_STATUS_NO_SUCH_USER;
1389 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1391 ldap_msgfree(result);
1392 return NT_STATUS_NO_SUCH_USER;
1395 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1396 DEBUG(1,("ldapsam_getsampwrid: init_sam_from_ldap failed!\n"));
1397 ldap_msgfree(result);
1398 return NT_STATUS_NO_SUCH_USER;
1401 pdb_set_backend_private_data(user, result,
1402 private_data_free_fn,
1403 my_methods, PDB_CHANGED);
1404 return NT_STATUS_OK;
1407 static BOOL ldapsam_can_pwchange_exop(struct smbldap_state *ldap_state)
1409 LDAPMessage *msg = NULL;
1410 LDAPMessage *entry = NULL;
1411 char **values = NULL;
1412 char *attrs[] = { "supportedExtension", NULL };
1413 int rc, num_result, num_values, i;
1414 BOOL result = False;
1416 rc = smbldap_search(ldap_state, "", LDAP_SCOPE_BASE, "(objectclass=*)",
1419 if (rc != LDAP_SUCCESS) {
1420 DEBUG(3, ("Could not search rootDSE\n"));
1424 num_result = ldap_count_entries(ldap_state->ldap_struct, msg);
1426 if (num_result != 1) {
1427 DEBUG(3, ("Expected one rootDSE, got %d\n", num_result));
1431 entry = ldap_first_entry(ldap_state->ldap_struct, msg);
1433 if (entry == NULL) {
1434 DEBUG(3, ("Could not retrieve rootDSE\n"));
1438 values = ldap_get_values(ldap_state->ldap_struct, entry,
1439 "supportedExtension");
1441 if (values == NULL) {
1442 DEBUG(9, ("LDAP Server does not support any extensions\n"));
1446 num_values = ldap_count_values(values);
1448 if (num_values == 0) {
1449 DEBUG(9, ("LDAP Server does not support any extensions\n"));
1453 for (i=0; i<num_values; i++) {
1454 if (strcmp(values[i], LDAP_EXOP_MODIFY_PASSWD) == 0)
1460 ldap_value_free(values);
1467 /********************************************************************
1468 Do the actual modification - also change a plaintext passord if
1470 **********************************************************************/
1472 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1473 SAM_ACCOUNT *newpwd, char *dn,
1474 LDAPMod **mods, int ldap_op,
1475 BOOL (*need_update)(const SAM_ACCOUNT *, enum pdb_elements))
1477 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1480 if (!my_methods || !newpwd || !dn) {
1481 return NT_STATUS_INVALID_PARAMETER;
1485 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1486 /* may be password change below however */
1490 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1493 rc = smbldap_add(ldap_state->smbldap_state,
1496 case LDAP_MOD_REPLACE:
1497 rc = smbldap_modify(ldap_state->smbldap_state,
1501 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1503 return NT_STATUS_INVALID_PARAMETER;
1506 if (rc!=LDAP_SUCCESS) {
1507 char *ld_error = NULL;
1508 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1510 DEBUG(1, ("ldapsam_modify_entry: Failed to %s user dn= %s with: %s\n\t%s\n",
1511 ldap_op == LDAP_MOD_ADD ? "add" : "modify",
1512 dn, ldap_err2string(rc),
1513 ld_error?ld_error:"unknown"));
1514 SAFE_FREE(ld_error);
1515 return NT_STATUS_UNSUCCESSFUL;
1519 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1520 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1521 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1522 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1526 struct berval *retdata;
1527 char *utf8_password;
1530 if (!ldapsam_can_pwchange_exop(ldap_state->smbldap_state)) {
1531 DEBUG(2, ("ldap password change requested, but LDAP "
1532 "server does not support it -- ignoring\n"));
1533 return NT_STATUS_OK;
1536 if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
1537 return NT_STATUS_NO_MEMORY;
1540 if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
1541 return NT_STATUS_NO_MEMORY;
1544 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1545 DEBUG(0,("ber_alloc_t returns NULL\n"));
1546 SAFE_FREE(utf8_password);
1547 return NT_STATUS_UNSUCCESSFUL;
1550 ber_printf (ber, "{");
1551 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
1552 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
1553 ber_printf (ber, "N}");
1555 if ((rc = ber_flatten (ber, &bv))<0) {
1556 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1559 SAFE_FREE(utf8_password);
1560 return NT_STATUS_UNSUCCESSFUL;
1564 SAFE_FREE(utf8_password);
1567 if ((rc = smbldap_extended_operation(ldap_state->smbldap_state,
1568 LDAP_EXOP_MODIFY_PASSWD,
1569 bv, NULL, NULL, &retoid,
1570 &retdata)) != LDAP_SUCCESS) {
1571 char *ld_error = NULL;
1573 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1574 DEBUG(3, ("Could not set userPassword "
1575 "attribute due to an objectClass "
1576 "violation -- ignoring\n"));
1578 return NT_STATUS_OK;
1581 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1583 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1584 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1585 SAFE_FREE(ld_error);
1587 return NT_STATUS_UNSUCCESSFUL;
1589 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1590 #ifdef DEBUG_PASSWORD
1591 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1593 ber_bvfree(retdata);
1594 ber_memfree(retoid);
1598 return NT_STATUS_OK;
1601 /**********************************************************************
1602 Delete entry from LDAP for username.
1603 *********************************************************************/
1605 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * sam_acct)
1607 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1610 LDAPMessage *result = NULL;
1616 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1617 return NT_STATUS_INVALID_PARAMETER;
1620 sname = pdb_get_username(sam_acct);
1622 DEBUG (3, ("ldapsam_delete_sam_account: Deleting user %s from LDAP.\n", sname));
1624 attr_list= get_userattr_delete_list( ldap_state->schema_ver );
1625 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result, attr_list);
1627 if (rc != LDAP_SUCCESS) {
1628 free_attr_list( attr_list );
1629 return NT_STATUS_NO_SUCH_USER;
1632 switch ( ldap_state->schema_ver ) {
1633 case SCHEMAVER_SAMBASAMACCOUNT:
1634 fstrcpy( objclass, LDAP_OBJ_SAMBASAMACCOUNT );
1637 case SCHEMAVER_SAMBAACCOUNT:
1638 fstrcpy( objclass, LDAP_OBJ_SAMBAACCOUNT );
1641 fstrcpy( objclass, "UNKNOWN" );
1642 DEBUG(0,("ldapsam_delete_sam_account: Unknown schema version specified!\n"));
1646 ret = ldapsam_delete_entry(ldap_state, result, objclass, attr_list );
1647 ldap_msgfree(result);
1648 free_attr_list( attr_list );
1653 /**********************************************************************
1654 Helper function to determine for update_sam_account whether
1655 we need LDAP modification.
1656 *********************************************************************/
1658 static BOOL element_is_changed(const SAM_ACCOUNT *sampass,
1659 enum pdb_elements element)
1661 return IS_SAM_CHANGED(sampass, element);
1664 /**********************************************************************
1666 *********************************************************************/
1668 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * newpwd)
1670 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1671 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1674 LDAPMessage *result = NULL;
1675 LDAPMessage *entry = NULL;
1676 LDAPMod **mods = NULL;
1679 result = pdb_get_backend_private_data(newpwd, my_methods);
1681 attr_list = get_userattr_list(ldap_state->schema_ver);
1682 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1683 free_attr_list( attr_list );
1684 if (rc != LDAP_SUCCESS) {
1685 return NT_STATUS_UNSUCCESSFUL;
1687 pdb_set_backend_private_data(newpwd, result, private_data_free_fn, my_methods, PDB_CHANGED);
1690 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1691 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1692 return NT_STATUS_UNSUCCESSFUL;
1695 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1696 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
1698 return NT_STATUS_UNSUCCESSFUL;
1701 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1703 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1704 element_is_changed)) {
1705 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1708 ldap_mods_free(mods,True);
1709 return NT_STATUS_UNSUCCESSFUL;
1713 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1714 pdb_get_username(newpwd)));
1716 return NT_STATUS_OK;
1719 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1720 ldap_mods_free(mods,True);
1723 if (!NT_STATUS_IS_OK(ret)) {
1724 char *ld_error = NULL;
1725 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1727 DEBUG(0,("ldapsam_update_sam_account: failed to modify user with uid = %s, error: %s (%s)\n",
1728 pdb_get_username(newpwd), ld_error?ld_error:"(unknwon)", ldap_err2string(rc)));
1729 SAFE_FREE(ld_error);
1733 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1734 pdb_get_username(newpwd)));
1735 return NT_STATUS_OK;
1738 /**********************************************************************
1739 Helper function to determine for update_sam_account whether
1740 we need LDAP modification.
1741 *********************************************************************/
1743 static BOOL element_is_set_or_changed(const SAM_ACCOUNT *sampass,
1744 enum pdb_elements element)
1746 return (IS_SAM_SET(sampass, element) ||
1747 IS_SAM_CHANGED(sampass, element));
1750 /**********************************************************************
1751 Add SAM_ACCOUNT to LDAP.
1752 *********************************************************************/
1754 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * newpwd)
1756 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1757 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1759 LDAPMessage *result = NULL;
1760 LDAPMessage *entry = NULL;
1762 LDAPMod **mods = NULL;
1763 int ldap_op = LDAP_MOD_REPLACE;
1767 const char *username = pdb_get_username(newpwd);
1768 const DOM_SID *sid = pdb_get_user_sid(newpwd);
1772 if (!username || !*username) {
1773 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1774 return NT_STATUS_INVALID_PARAMETER;
1777 /* free this list after the second search or in case we exit on failure */
1778 attr_list = get_userattr_list(ldap_state->schema_ver);
1780 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
1782 if (rc != LDAP_SUCCESS) {
1783 free_attr_list( attr_list );
1784 return NT_STATUS_UNSUCCESSFUL;
1787 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1788 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1790 ldap_msgfree(result);
1791 free_attr_list( attr_list );
1792 return NT_STATUS_UNSUCCESSFUL;
1794 ldap_msgfree(result);
1797 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
1798 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1800 if (rc == LDAP_SUCCESS) {
1801 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1802 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1803 sid_to_string(sid_string, sid)));
1804 free_attr_list( attr_list );
1805 ldap_msgfree(result);
1806 return NT_STATUS_UNSUCCESSFUL;
1808 ldap_msgfree(result);
1812 /* does the entry already exist but without a samba attributes?
1813 we need to return the samba attributes here */
1815 escape_user = escape_ldap_string_alloc( username );
1816 pstrcpy( filter, lp_ldap_filter() );
1817 all_string_sub( filter, "%u", escape_user, sizeof(filter) );
1818 SAFE_FREE( escape_user );
1820 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1821 filter, attr_list, &result);
1822 if ( rc != LDAP_SUCCESS ) {
1823 free_attr_list( attr_list );
1824 return NT_STATUS_UNSUCCESSFUL;
1827 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1829 if (num_result > 1) {
1830 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1831 free_attr_list( attr_list );
1832 ldap_msgfree(result);
1833 return NT_STATUS_UNSUCCESSFUL;
1836 /* Check if we need to update an existing entry */
1837 if (num_result == 1) {
1840 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1841 ldap_op = LDAP_MOD_REPLACE;
1842 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
1843 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
1845 free_attr_list( attr_list );
1846 ldap_msgfree(result);
1847 return NT_STATUS_UNSUCCESSFUL;
1849 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1852 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
1854 /* There might be a SID for this account already - say an idmap entry */
1856 pstr_sprintf(filter, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
1857 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1858 sid_to_string(sid_string, sid),
1859 LDAP_OBJ_IDMAP_ENTRY,
1860 LDAP_OBJ_SID_ENTRY);
1862 /* free old result before doing a new search */
1863 if (result != NULL) {
1864 ldap_msgfree(result);
1867 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1868 filter, attr_list, &result);
1870 if ( rc != LDAP_SUCCESS ) {
1871 free_attr_list( attr_list );
1872 return NT_STATUS_UNSUCCESSFUL;
1875 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1877 if (num_result > 1) {
1878 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1879 free_attr_list( attr_list );
1880 ldap_msgfree(result);
1881 return NT_STATUS_UNSUCCESSFUL;
1884 /* Check if we need to update an existing entry */
1885 if (num_result == 1) {
1888 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1889 ldap_op = LDAP_MOD_REPLACE;
1890 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
1891 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
1893 free_attr_list( attr_list );
1894 ldap_msgfree(result);
1895 return NT_STATUS_UNSUCCESSFUL;
1897 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1902 free_attr_list( attr_list );
1904 if (num_result == 0) {
1905 /* Check if we need to add an entry */
1906 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
1907 ldap_op = LDAP_MOD_ADD;
1908 if (username[strlen(username)-1] == '$') {
1909 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_machine_suffix ());
1911 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_user_suffix ());
1915 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1916 element_is_set_or_changed)) {
1917 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
1918 ldap_msgfree(result);
1920 ldap_mods_free(mods,True);
1921 return NT_STATUS_UNSUCCESSFUL;
1924 ldap_msgfree(result);
1927 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
1928 return NT_STATUS_UNSUCCESSFUL;
1930 switch ( ldap_state->schema_ver ) {
1931 case SCHEMAVER_SAMBAACCOUNT:
1932 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
1934 case SCHEMAVER_SAMBASAMACCOUNT:
1935 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
1938 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
1942 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
1943 if (!NT_STATUS_IS_OK(ret)) {
1944 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
1945 pdb_get_username(newpwd),dn));
1946 ldap_mods_free(mods, True);
1950 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
1951 ldap_mods_free(mods, True);
1953 return NT_STATUS_OK;
1956 /**********************************************************************
1957 *********************************************************************/
1959 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
1961 LDAPMessage ** result)
1963 int scope = LDAP_SCOPE_SUBTREE;
1967 attr_list = get_attr_list(groupmap_attr_list);
1968 rc = smbldap_search(ldap_state->smbldap_state,
1969 lp_ldap_group_suffix (), scope,
1970 filter, attr_list, 0, result);
1971 free_attr_list( attr_list );
1973 if (rc != LDAP_SUCCESS) {
1974 char *ld_error = NULL;
1975 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1977 DEBUG(0, ("ldapsam_search_one_group: "
1978 "Problem during the LDAP search: LDAP error: %s (%s)\n",
1979 ld_error?ld_error:"(unknown)", ldap_err2string(rc)));
1980 DEBUGADD(3, ("ldapsam_search_one_group: Query was: %s, %s\n",
1981 lp_ldap_group_suffix(), filter));
1982 SAFE_FREE(ld_error);
1988 /**********************************************************************
1989 *********************************************************************/
1991 static BOOL init_group_from_ldap(struct ldapsam_privates *ldap_state,
1992 GROUP_MAP *map, LDAPMessage *entry)
1996 if (ldap_state == NULL || map == NULL || entry == NULL ||
1997 ldap_state->smbldap_state->ldap_struct == NULL) {
1998 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2002 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2003 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER), temp)) {
2004 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2005 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2008 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2010 map->gid = (gid_t)atol(temp);
2012 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2013 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID), temp)) {
2014 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2015 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2019 if (!string_to_sid(&map->sid, temp)) {
2020 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2024 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2025 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), temp)) {
2026 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2027 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2030 map->sid_name_use = (enum SID_NAME_USE)atol(temp);
2032 if ((map->sid_name_use < SID_NAME_USER) ||
2033 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2034 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2038 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2039 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), temp)) {
2041 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2042 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_CN), temp))
2044 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2045 for gidNumber(%lu)\n",(unsigned long)map->gid));
2049 fstrcpy(map->nt_name, temp);
2051 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2052 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), temp)) {
2055 fstrcpy(map->comment, temp);
2060 /**********************************************************************
2061 *********************************************************************/
2063 static BOOL init_ldap_from_group(LDAP *ldap_struct,
2064 LDAPMessage *existing,
2066 const GROUP_MAP *map)
2070 if (mods == NULL || map == NULL) {
2071 DEBUG(0, ("init_ldap_from_group: NULL parameters found!\n"));
2077 sid_to_string(tmp, &map->sid);
2079 smbldap_make_mod(ldap_struct, existing, mods,
2080 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID), tmp);
2081 pstr_sprintf(tmp, "%i", map->sid_name_use);
2082 smbldap_make_mod(ldap_struct, existing, mods,
2083 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), tmp);
2085 smbldap_make_mod(ldap_struct, existing, mods,
2086 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), map->nt_name);
2087 smbldap_make_mod(ldap_struct, existing, mods,
2088 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), map->comment);
2093 /**********************************************************************
2094 *********************************************************************/
2096 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2100 struct ldapsam_privates *ldap_state =
2101 (struct ldapsam_privates *)methods->private_data;
2102 LDAPMessage *result = NULL;
2103 LDAPMessage *entry = NULL;
2106 if (ldapsam_search_one_group(ldap_state, filter, &result)
2108 return NT_STATUS_NO_SUCH_GROUP;
2111 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2114 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2115 ldap_msgfree(result);
2116 return NT_STATUS_NO_SUCH_GROUP;
2120 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: count=%d\n",
2122 ldap_msgfree(result);
2123 return NT_STATUS_NO_SUCH_GROUP;
2126 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2129 ldap_msgfree(result);
2130 return NT_STATUS_UNSUCCESSFUL;
2133 if (!init_group_from_ldap(ldap_state, map, entry)) {
2134 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for group filter %s\n",
2136 ldap_msgfree(result);
2137 return NT_STATUS_NO_SUCH_GROUP;
2140 ldap_msgfree(result);
2141 return NT_STATUS_OK;
2144 /**********************************************************************
2145 *********************************************************************/
2147 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2152 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2154 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2155 sid_string_static(&sid));
2157 return ldapsam_getgroup(methods, filter, map);
2160 /**********************************************************************
2161 *********************************************************************/
2163 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2168 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%lu))",
2170 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2171 (unsigned long)gid);
2173 return ldapsam_getgroup(methods, filter, map);
2176 /**********************************************************************
2177 *********************************************************************/
2179 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2183 char *escape_name = escape_ldap_string_alloc(name);
2186 return NT_STATUS_NO_MEMORY;
2189 pstr_sprintf(filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2191 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2192 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN), escape_name);
2194 SAFE_FREE(escape_name);
2196 return ldapsam_getgroup(methods, filter, map);
2199 /**********************************************************************
2200 *********************************************************************/
2202 static int ldapsam_search_one_group_by_gid(struct ldapsam_privates *ldap_state,
2204 LDAPMessage **result)
2208 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%lu))",
2209 LDAP_OBJ_POSIXGROUP, LDAP_OBJ_IDMAP_ENTRY,
2210 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2211 (unsigned long)gid);
2213 return ldapsam_search_one_group(ldap_state, filter, result);
2216 /**********************************************************************
2217 *********************************************************************/
2219 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
2222 struct ldapsam_privates *ldap_state =
2223 (struct ldapsam_privates *)methods->private_data;
2224 LDAPMessage *result = NULL;
2225 LDAPMod **mods = NULL;
2236 if (NT_STATUS_IS_OK(ldapsam_getgrgid(methods, &dummy,
2238 DEBUG(0, ("ldapsam_add_group_mapping_entry: Group %ld already exists in LDAP\n", (unsigned long)map->gid));
2239 return NT_STATUS_UNSUCCESSFUL;
2242 rc = ldapsam_search_one_group_by_gid(ldap_state, map->gid, &result);
2243 if (rc != LDAP_SUCCESS) {
2244 ldap_msgfree(result);
2245 return NT_STATUS_UNSUCCESSFUL;
2248 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2251 /* There's no posixGroup account, let's try to find an
2252 * appropriate idmap entry for aliases */
2258 ldap_msgfree(result);
2260 pstrcpy( suffix, lp_ldap_idmap_suffix() );
2261 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%u))",
2262 LDAP_OBJ_IDMAP_ENTRY, LDAP_ATTRIBUTE_GIDNUMBER,
2265 attr_list = get_attr_list( sidmap_attr_list );
2266 rc = smbldap_search(ldap_state->smbldap_state, suffix,
2267 LDAP_SCOPE_SUBTREE, filter, attr_list,
2270 free_attr_list(attr_list);
2272 if (rc != LDAP_SUCCESS) {
2273 DEBUG(3,("Failure looking up entry (%s)\n",
2274 ldap_err2string(rc) ));
2275 ldap_msgfree(result);
2276 return NT_STATUS_UNSUCCESSFUL;
2280 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2282 ldap_msgfree(result);
2283 return NT_STATUS_UNSUCCESSFUL;
2287 DEBUG(2, ("ldapsam_add_group_mapping_entry: Group %lu must exist exactly once in LDAP\n",
2288 (unsigned long)map->gid));
2289 ldap_msgfree(result);
2290 return NT_STATUS_UNSUCCESSFUL;
2293 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2294 tmp = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2296 ldap_msgfree(result);
2297 return NT_STATUS_UNSUCCESSFUL;
2302 if (!init_ldap_from_group(ldap_state->smbldap_state->ldap_struct,
2303 result, &mods, map)) {
2304 DEBUG(0, ("ldapsam_add_group_mapping_entry: init_ldap_from_group failed!\n"));
2305 ldap_mods_free(mods, True);
2306 ldap_msgfree(result);
2307 return NT_STATUS_UNSUCCESSFUL;
2310 ldap_msgfree(result);
2313 DEBUG(0, ("ldapsam_add_group_mapping_entry: mods is empty\n"));
2314 return NT_STATUS_UNSUCCESSFUL;
2317 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP );
2319 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2320 ldap_mods_free(mods, True);
2322 if (rc != LDAP_SUCCESS) {
2323 char *ld_error = NULL;
2324 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
2326 DEBUG(0, ("ldapsam_add_group_mapping_entry: failed to add group %lu error: %s (%s)\n", (unsigned long)map->gid,
2327 ld_error ? ld_error : "(unknown)", ldap_err2string(rc)));
2328 SAFE_FREE(ld_error);
2329 return NT_STATUS_UNSUCCESSFUL;
2332 DEBUG(2, ("ldapsam_add_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map->gid));
2333 return NT_STATUS_OK;
2336 /**********************************************************************
2337 *********************************************************************/
2339 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
2342 struct ldapsam_privates *ldap_state =
2343 (struct ldapsam_privates *)methods->private_data;
2346 LDAPMessage *result = NULL;
2347 LDAPMessage *entry = NULL;
2348 LDAPMod **mods = NULL;
2350 rc = ldapsam_search_one_group_by_gid(ldap_state, map->gid, &result);
2352 if (rc != LDAP_SUCCESS) {
2353 return NT_STATUS_UNSUCCESSFUL;
2356 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
2357 DEBUG(0, ("ldapsam_update_group_mapping_entry: No group to modify!\n"));
2358 ldap_msgfree(result);
2359 return NT_STATUS_UNSUCCESSFUL;
2362 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2364 if (!init_ldap_from_group(ldap_state->smbldap_state->ldap_struct,
2365 result, &mods, map)) {
2366 DEBUG(0, ("ldapsam_update_group_mapping_entry: init_ldap_from_group failed\n"));
2367 ldap_msgfree(result);
2369 ldap_mods_free(mods,True);
2370 return NT_STATUS_UNSUCCESSFUL;
2374 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: nothing to do\n"));
2375 ldap_msgfree(result);
2376 return NT_STATUS_OK;
2379 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2381 ldap_msgfree(result);
2382 return NT_STATUS_UNSUCCESSFUL;
2384 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2387 ldap_mods_free(mods, True);
2388 ldap_msgfree(result);
2390 if (rc != LDAP_SUCCESS) {
2391 char *ld_error = NULL;
2392 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
2394 DEBUG(0, ("ldapsam_update_group_mapping_entry: failed to modify group %lu error: %s (%s)\n", (unsigned long)map->gid,
2395 ld_error ? ld_error : "(unknown)", ldap_err2string(rc)));
2396 SAFE_FREE(ld_error);
2397 return NT_STATUS_UNSUCCESSFUL;
2400 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map->gid));
2401 return NT_STATUS_OK;
2404 /**********************************************************************
2405 *********************************************************************/
2407 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
2410 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)methods->private_data;
2411 pstring sidstring, filter;
2412 LDAPMessage *result = NULL;
2417 sid_to_string(sidstring, &sid);
2419 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2420 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID, sidstring);
2422 rc = ldapsam_search_one_group(ldap_state, filter, &result);
2424 if (rc != LDAP_SUCCESS) {
2425 return NT_STATUS_NO_SUCH_GROUP;
2428 attr_list = get_attr_list( groupmap_attr_list_to_delete );
2429 ret = ldapsam_delete_entry(ldap_state, result, LDAP_OBJ_GROUPMAP, attr_list);
2430 free_attr_list ( attr_list );
2432 ldap_msgfree(result);
2437 /**********************************************************************
2438 *********************************************************************/
2440 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods, BOOL update)
2442 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2447 pstr_sprintf( filter, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
2448 attr_list = get_attr_list( groupmap_attr_list );
2449 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
2450 LDAP_SCOPE_SUBTREE, filter,
2451 attr_list, 0, &ldap_state->result);
2452 free_attr_list( attr_list );
2454 if (rc != LDAP_SUCCESS) {
2455 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n", ldap_err2string(rc)));
2456 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n", lp_ldap_group_suffix(), filter));
2457 ldap_msgfree(ldap_state->result);
2458 ldap_state->result = NULL;
2459 return NT_STATUS_UNSUCCESSFUL;
2462 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base!\n",
2463 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2464 ldap_state->result)));
2466 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, ldap_state->result);
2467 ldap_state->index = 0;
2469 return NT_STATUS_OK;
2472 /**********************************************************************
2473 *********************************************************************/
2475 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
2477 ldapsam_endsampwent(my_methods);
2480 /**********************************************************************
2481 *********************************************************************/
2483 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
2486 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2487 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2491 if (!ldap_state->entry)
2494 ldap_state->index++;
2495 bret = init_group_from_ldap(ldap_state, map, ldap_state->entry);
2497 ldap_state->entry = ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
2501 return NT_STATUS_OK;
2504 /**********************************************************************
2505 *********************************************************************/
2507 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
2508 enum SID_NAME_USE sid_name_use,
2509 GROUP_MAP **rmap, int *num_entries,
2519 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
2520 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open passdb\n"));
2521 return NT_STATUS_ACCESS_DENIED;
2524 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
2525 if (sid_name_use != SID_NAME_UNKNOWN &&
2526 sid_name_use != map.sid_name_use) {
2527 DEBUG(11,("ldapsam_enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
2530 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
2531 DEBUG(11,("ldapsam_enum_group_mapping: group %s is non mapped\n", map.nt_name));
2535 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
2537 DEBUG(0,("ldapsam_enum_group_mapping: Unable to enlarge group map!\n"));
2539 return NT_STATUS_UNSUCCESSFUL;
2544 mapt[entries] = map;
2549 ldapsam_endsamgrent(methods);
2551 *num_entries = entries;
2553 return NT_STATUS_OK;
2556 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
2557 const DOM_SID *alias,
2558 const DOM_SID *member,
2561 struct ldapsam_privates *ldap_state =
2562 (struct ldapsam_privates *)methods->private_data;
2564 LDAPMessage *result = NULL;
2565 LDAPMessage *entry = NULL;
2567 LDAPMod **mods = NULL;
2572 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%s))",
2573 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY,
2574 get_attr_key2string(groupmap_attr_list,
2575 LDAP_ATTR_GROUP_SID),
2576 sid_string_static(alias));
2578 if (ldapsam_search_one_group(ldap_state, filter,
2579 &result) != LDAP_SUCCESS)
2580 return NT_STATUS_NO_SUCH_ALIAS;
2582 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2586 DEBUG(4, ("ldapsam_add_aliasmem: Did not find alias\n"));
2587 ldap_msgfree(result);
2588 return NT_STATUS_NO_SUCH_ALIAS;
2592 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2593 "count=%d\n", filter, count));
2594 ldap_msgfree(result);
2595 return NT_STATUS_NO_SUCH_ALIAS;
2598 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
2602 ldap_msgfree(result);
2603 return NT_STATUS_UNSUCCESSFUL;
2606 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2608 ldap_msgfree(result);
2609 return NT_STATUS_UNSUCCESSFUL;
2612 smbldap_set_mod(&mods, modop,
2613 get_attr_key2string(groupmap_attr_list,
2614 LDAP_ATTR_SID_LIST),
2615 sid_string_static(member));
2617 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2619 ldap_mods_free(mods, True);
2620 ldap_msgfree(result);
2622 if (rc != LDAP_SUCCESS) {
2623 char *ld_error = NULL;
2624 ldap_get_option(ldap_state->smbldap_state->ldap_struct,
2625 LDAP_OPT_ERROR_STRING,&ld_error);
2627 DEBUG(0, ("ldapsam_delete_entry: Could not delete attributes "
2628 "for %s, error: %s (%s)\n", dn, ldap_err2string(rc),
2629 ld_error?ld_error:"unknown"));
2630 SAFE_FREE(ld_error);
2632 return NT_STATUS_UNSUCCESSFUL;
2637 return NT_STATUS_OK;
2640 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
2641 const DOM_SID *alias,
2642 const DOM_SID *member)
2644 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
2647 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
2648 const DOM_SID *alias,
2649 const DOM_SID *member)
2651 return ldapsam_modify_aliasmem(methods, alias, member,
2655 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
2656 const DOM_SID *alias, DOM_SID **members,
2659 struct ldapsam_privates *ldap_state =
2660 (struct ldapsam_privates *)methods->private_data;
2661 LDAPMessage *result = NULL;
2662 LDAPMessage *entry = NULL;
2671 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%s))",
2672 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY,
2673 get_attr_key2string(groupmap_attr_list,
2674 LDAP_ATTR_GROUP_SID),
2675 sid_string_static(alias));
2677 if (ldapsam_search_one_group(ldap_state, filter,
2678 &result) != LDAP_SUCCESS)
2679 return NT_STATUS_NO_SUCH_ALIAS;
2681 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2685 DEBUG(4, ("ldapsam_add_aliasmem: Did not find alias\n"));
2686 ldap_msgfree(result);
2687 return NT_STATUS_NO_SUCH_ALIAS;
2691 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2692 "count=%d\n", filter, count));
2693 ldap_msgfree(result);
2694 return NT_STATUS_NO_SUCH_ALIAS;
2697 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
2701 ldap_msgfree(result);
2702 return NT_STATUS_UNSUCCESSFUL;
2705 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
2707 get_attr_key2string(groupmap_attr_list,
2708 LDAP_ATTR_SID_LIST));
2710 if (values == NULL) {
2711 ldap_msgfree(result);
2712 return NT_STATUS_OK;
2715 count = ldap_count_values(values);
2717 for (i=0; i<count; i++) {
2720 if (!string_to_sid(&member, values[i]))
2723 add_sid_to_array(&member, members, num_members);
2726 ldap_value_free(values);
2727 ldap_msgfree(result);
2729 return NT_STATUS_OK;
2732 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
2734 DOM_SID **aliases, int *num)
2736 struct ldapsam_privates *ldap_state =
2737 (struct ldapsam_privates *)methods->private_data;
2740 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
2742 LDAPMessage *result = NULL;
2743 LDAPMessage *entry = NULL;
2748 sid_to_string(sid_string, sid);
2749 pstr_sprintf(filter, "(&(|(objectclass=%s)(objectclass=%s))(%s=%s))",
2750 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY,
2751 get_attr_key2string(groupmap_attr_list,
2752 LDAP_ATTR_SID_LIST), sid_string);
2754 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
2755 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2757 if (rc != LDAP_SUCCESS)
2758 return NT_STATUS_UNSUCCESSFUL;
2763 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2767 ldap_msgfree(result);
2768 return NT_STATUS_OK;
2772 for (entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
2775 entry = ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
2780 vals = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
2781 entry, LDAP_ATTRIBUTE_SID);
2786 if (vals[0] == NULL) {
2787 ldap_value_free(vals);
2791 if (!string_to_sid(&alias, vals[0])) {
2792 ldap_value_free(vals);
2796 add_sid_to_array(&alias, aliases, num);
2797 ldap_value_free(vals);
2800 ldap_msgfree(result);
2801 return NT_STATUS_OK;
2804 /**********************************************************************
2806 *********************************************************************/
2808 static void free_private_data(void **vp)
2810 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
2812 smbldap_free_struct(&(*ldap_state)->smbldap_state);
2814 if ((*ldap_state)->result != NULL) {
2815 ldap_msgfree((*ldap_state)->result);
2816 (*ldap_state)->result = NULL;
2821 /* No need to free any further, as it is talloc()ed */
2824 /**********************************************************************
2825 Intitalise the parts of the pdb_context that are common to all pdb_ldap modes
2826 *********************************************************************/
2828 static NTSTATUS pdb_init_ldapsam_common(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method,
2829 const char *location)
2832 struct ldapsam_privates *ldap_state;
2834 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
2838 (*pdb_method)->name = "ldapsam";
2840 (*pdb_method)->setsampwent = ldapsam_setsampwent;
2841 (*pdb_method)->endsampwent = ldapsam_endsampwent;
2842 (*pdb_method)->getsampwent = ldapsam_getsampwent;
2843 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
2844 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
2845 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
2846 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
2847 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
2849 (*pdb_method)->getgrsid = ldapsam_getgrsid;
2850 (*pdb_method)->getgrgid = ldapsam_getgrgid;
2851 (*pdb_method)->getgrnam = ldapsam_getgrnam;
2852 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
2853 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
2854 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
2855 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
2857 /* TODO: Setup private data and free */
2859 ldap_state = talloc_zero(pdb_context->mem_ctx, sizeof(*ldap_state));
2861 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
2862 return NT_STATUS_NO_MEMORY;
2865 if (!NT_STATUS_IS_OK(nt_status =
2866 smbldap_init(pdb_context->mem_ctx, location,
2867 &ldap_state->smbldap_state)));
2869 ldap_state->domain_name = talloc_strdup(pdb_context->mem_ctx, get_global_sam_name());
2870 if (!ldap_state->domain_name) {
2871 return NT_STATUS_NO_MEMORY;
2874 (*pdb_method)->private_data = ldap_state;
2876 (*pdb_method)->free_private_data = free_private_data;
2878 return NT_STATUS_OK;
2881 /**********************************************************************
2882 Initialise the 'compat' mode for pdb_ldap
2883 *********************************************************************/
2885 static NTSTATUS pdb_init_ldapsam_compat(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
2888 struct ldapsam_privates *ldap_state;
2890 #ifdef WITH_LDAP_SAMCONFIG
2892 int ldap_port = lp_ldap_port();
2894 /* remap default port if not using SSL (ie clear or TLS) */
2895 if ( (lp_ldap_ssl() != LDAP_SSL_ON) && (ldap_port == 636) ) {
2899 location = talloc_asprintf(pdb_context->mem_ctx, "%s://%s:%d", lp_ldap_ssl() == LDAP_SSL_ON ? "ldaps" : "ldap", lp_ldap_server(), ldap_port);
2901 return NT_STATUS_NO_MEMORY;
2906 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common(pdb_context, pdb_method, location))) {
2910 (*pdb_method)->name = "ldapsam_compat";
2912 ldap_state = (*pdb_method)->private_data;
2913 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
2915 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
2917 return NT_STATUS_OK;
2920 /**********************************************************************
2921 Initialise the normal mode for pdb_ldap
2922 *********************************************************************/
2924 static NTSTATUS pdb_init_ldapsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
2927 struct ldapsam_privates *ldap_state;
2928 uint32 alg_rid_base;
2929 pstring alg_rid_base_string;
2930 LDAPMessage *result = NULL;
2931 LDAPMessage *entry = NULL;
2932 DOM_SID ldap_domain_sid;
2933 DOM_SID secrets_domain_sid;
2934 pstring domain_sid_string;
2936 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common(pdb_context, pdb_method, location))) {
2940 (*pdb_method)->name = "ldapsam";
2942 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
2943 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
2944 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
2945 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
2947 ldap_state = (*pdb_method)->private_data;
2948 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
2950 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
2952 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state, &result,
2953 ldap_state->domain_name, True);
2955 if ( !NT_STATUS_IS_OK(nt_status) ) {
2956 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain info, nor add one to the domain\n"));
2957 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, will be unable to allocate new users/groups, \
2958 and will risk BDCs having inconsistant SIDs\n"));
2959 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
2960 return NT_STATUS_OK;
2963 /* Given that the above might fail, everything below this must be optional */
2965 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2967 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info entry\n"));
2968 ldap_msgfree(result);
2969 return NT_STATUS_UNSUCCESSFUL;
2972 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2973 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
2974 domain_sid_string)) {
2976 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
2977 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be read as a valid SID\n", domain_sid_string));
2978 return NT_STATUS_INVALID_PARAMETER;
2980 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name, &secrets_domain_sid);
2981 if (!found_sid || !sid_equal(&secrets_domain_sid, &ldap_domain_sid)) {
2982 fstring new_sid_str, old_sid_str;
2983 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain %s based on pdb_ldap results %s -> %s\n",
2984 ldap_state->domain_name,
2985 sid_to_string(old_sid_str, &secrets_domain_sid),
2986 sid_to_string(new_sid_str, &ldap_domain_sid)));
2988 /* reset secrets.tdb sid */
2989 secrets_store_domain_sid(ldap_state->domain_name, &ldap_domain_sid);
2990 DEBUG(1, ("New global sam SID: %s\n", sid_to_string(new_sid_str, get_global_sam_sid())));
2992 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
2995 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2996 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ALGORITHMIC_RID_BASE),
2997 alg_rid_base_string)) {
2998 alg_rid_base = (uint32)atol(alg_rid_base_string);
2999 if (alg_rid_base != algorithmic_rid_base()) {
3000 DEBUG(0, ("The value of 'algorithmic RID base' has changed since the LDAP\n"
3001 "database was initialised. Aborting. \n"));
3002 ldap_msgfree(result);
3003 return NT_STATUS_UNSUCCESSFUL;
3006 ldap_msgfree(result);
3008 return NT_STATUS_OK;
3011 NTSTATUS pdb_ldap_init(void)
3014 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
3017 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
3020 return NT_STATUS_OK;