2 Unix SMB/CIFS implementation.
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 */
103 /**********************************************************************
104 Free a LDAPMessage (one is stored on the SAM_ACCOUNT).
105 **********************************************************************/
107 static void private_data_free_fn(void **result)
109 ldap_msgfree(*result);
113 /**********************************************************************
114 Get the attribute name given a user schame version.
115 **********************************************************************/
117 static const char* get_userattr_key2string( int schema_ver, int key )
119 switch ( schema_ver ) {
120 case SCHEMAVER_SAMBAACCOUNT:
121 return get_attr_key2string( attrib_map_v22, key );
123 case SCHEMAVER_SAMBASAMACCOUNT:
124 return get_attr_key2string( attrib_map_v30, key );
127 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
133 /**********************************************************************
134 Return the list of attribute names given a user schema version.
135 **********************************************************************/
137 static const char** get_userattr_list( int schema_ver )
139 switch ( schema_ver ) {
140 case SCHEMAVER_SAMBAACCOUNT:
141 return get_attr_list( attrib_map_v22 );
143 case SCHEMAVER_SAMBASAMACCOUNT:
144 return get_attr_list( attrib_map_v30 );
146 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
153 /**************************************************************************
154 Return the list of attribute names to delete given a user schema version.
155 **************************************************************************/
157 static const char** get_userattr_delete_list( int schema_ver )
159 switch ( schema_ver ) {
160 case SCHEMAVER_SAMBAACCOUNT:
161 return get_attr_list( attrib_map_to_delete_v22 );
163 case SCHEMAVER_SAMBASAMACCOUNT:
164 return get_attr_list( attrib_map_to_delete_v30 );
166 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
174 /*******************************************************************
175 Generate the LDAP search filter for the objectclass based on the
176 version of the schema we are using.
177 ******************************************************************/
179 static const char* get_objclass_filter( int schema_ver )
181 static fstring objclass_filter;
183 switch( schema_ver ) {
184 case SCHEMAVER_SAMBAACCOUNT:
185 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
187 case SCHEMAVER_SAMBASAMACCOUNT:
188 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
191 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
195 return objclass_filter;
198 /*******************************************************************
199 Run the search by name.
200 ******************************************************************/
202 static int ldapsam_search_suffix_by_name (struct ldapsam_privates *ldap_state,
204 LDAPMessage ** result,
208 char *escape_user = escape_ldap_string_alloc(user);
211 return LDAP_NO_MEMORY;
215 * in the filter expression, replace %u with the real name
216 * so in ldap filter, %u MUST exist :-)
218 pstr_sprintf(filter, "(&%s%s)", lp_ldap_filter(),
219 get_objclass_filter(ldap_state->schema_ver));
222 * have to use this here because $ is filtered out
227 all_string_sub(filter, "%u", escape_user, sizeof(pstring));
228 SAFE_FREE(escape_user);
230 return smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
233 /*******************************************************************
234 Run the search by rid.
235 ******************************************************************/
237 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
238 uint32 rid, LDAPMessage ** result,
244 pstr_sprintf(filter, "(&(rid=%i)%s)", rid,
245 get_objclass_filter(ldap_state->schema_ver));
247 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
252 /*******************************************************************
253 Run the search by SID.
254 ******************************************************************/
256 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
257 const DOM_SID *sid, LDAPMessage ** result,
264 pstr_sprintf(filter, "(&(%s=%s)%s)",
265 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
266 sid_to_string(sid_string, sid),
267 get_objclass_filter(ldap_state->schema_ver));
269 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
274 /*******************************************************************
275 Delete complete object or objectclass and attrs from
276 object found in search_result depending on lp_ldap_delete_dn
277 ******************************************************************/
279 static NTSTATUS ldapsam_delete_entry(struct ldapsam_privates *ldap_state,
281 const char *objectclass,
285 LDAPMessage *entry = NULL;
286 LDAPMod **mods = NULL;
288 BerElement *ptr = NULL;
290 rc = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
293 DEBUG(0, ("ldapsam_delete_entry: Entry must exist exactly once!\n"));
294 return NT_STATUS_UNSUCCESSFUL;
297 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
298 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
300 return NT_STATUS_UNSUCCESSFUL;
303 if (lp_ldap_delete_dn()) {
304 NTSTATUS ret = NT_STATUS_OK;
305 rc = smbldap_delete(ldap_state->smbldap_state, dn);
307 if (rc != LDAP_SUCCESS) {
308 DEBUG(0, ("ldapsam_delete_entry: Could not delete object %s\n", dn));
309 ret = NT_STATUS_UNSUCCESSFUL;
315 /* Ok, delete only the SAM attributes */
317 for (name = ldap_first_attribute(ldap_state->smbldap_state->ldap_struct, entry, &ptr);
319 name = ldap_next_attribute(ldap_state->smbldap_state->ldap_struct, entry, ptr)) {
322 /* We are only allowed to delete the attributes that
325 for (attrib = attrs; *attrib != NULL; attrib++) {
326 /* Don't delete LDAP_ATTR_MOD_TIMESTAMP attribute. */
327 if (strequal(*attrib, get_userattr_key2string(ldap_state->schema_ver,
328 LDAP_ATTR_MOD_TIMESTAMP))) {
331 if (strequal(*attrib, name)) {
332 DEBUG(10, ("ldapsam_delete_entry: deleting "
333 "attribute %s\n", name));
334 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
346 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
348 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
349 ldap_mods_free(mods, True);
351 if (rc != LDAP_SUCCESS) {
352 char *ld_error = NULL;
353 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
356 DEBUG(0, ("ldapsam_delete_entry: Could not delete attributes for %s, error: %s (%s)\n",
357 dn, ldap_err2string(rc), ld_error?ld_error:"unknown"));
360 return NT_STATUS_UNSUCCESSFUL;
367 /* New Interface is being implemented here */
369 #if 0 /* JERRY - not uesed anymore */
371 /**********************************************************************
372 Initialize SAM_ACCOUNT from an LDAP query (unix attributes only)
373 *********************************************************************/
374 static BOOL get_unix_attributes (struct ldapsam_privates *ldap_state,
375 SAM_ACCOUNT * sampass,
384 if ((ldap_values = ldap_get_values (ldap_state->smbldap_state->ldap_struct, entry, "objectClass")) == NULL) {
385 DEBUG (1, ("get_unix_attributes: no objectClass! \n"));
389 for (values=ldap_values;*values;values++) {
390 if (strequal(*values, LDAP_OBJ_POSIXACCOUNT )) {
395 if (!*values) { /*end of array, no posixAccount */
396 DEBUG(10, ("user does not have %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
397 ldap_value_free(ldap_values);
400 ldap_value_free(ldap_values);
402 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
403 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_UNIX_HOME), homedir) )
408 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
409 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_GIDNUMBER), temp) )
414 *gid = (gid_t)atol(temp);
416 pdb_set_unix_homedir(sampass, homedir, PDB_SET);
418 DEBUG(10, ("user has %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
425 static time_t ldapsam_get_entry_timestamp(
426 struct ldapsam_privates *ldap_state,
432 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
433 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
437 strptime(temp, "%Y%m%d%H%M%SZ", &tm);
442 /**********************************************************************
443 Initialize SAM_ACCOUNT from an LDAP query.
444 (Based on init_sam_from_buffer in pdb_tdb.c)
445 *********************************************************************/
447 static BOOL init_sam_from_ldap (struct ldapsam_privates *ldap_state,
448 SAM_ACCOUNT * sampass,
455 pass_can_change_time,
456 pass_must_change_time,
469 char munged_dial[2048];
471 uint8 smblmpwd[LM_HASH_LEN],
472 smbntpwd[NT_HASH_LEN];
473 uint16 acct_ctrl = 0,
475 uint16 bad_password_count = 0,
478 uint8 hours[MAX_HOURS_LEN];
480 LOGIN_CACHE *cache_entry = NULL;
485 * do a little initialization
489 nt_username[0] = '\0';
493 logon_script[0] = '\0';
494 profile_path[0] = '\0';
496 munged_dial[0] = '\0';
497 workstations[0] = '\0';
500 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
501 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
505 if (ldap_state->smbldap_state->ldap_struct == NULL) {
506 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->ldap_struct is NULL!\n"));
510 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry, "uid", username)) {
511 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for this user!\n"));
515 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
517 pstrcpy(nt_username, username);
519 pstrcpy(domain, ldap_state->domain_name);
521 pdb_set_username(sampass, username, PDB_SET);
523 pdb_set_domain(sampass, domain, PDB_DEFAULT);
524 pdb_set_nt_username(sampass, nt_username, PDB_SET);
526 /* deal with different attributes between the schema first */
528 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
529 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
530 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID), temp)) {
531 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
534 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
535 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_SID), temp)) {
536 pdb_set_group_sid_from_string(sampass, temp, PDB_SET);
538 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
541 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
542 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID), temp)) {
543 user_rid = (uint32)atol(temp);
544 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
547 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
548 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_RID), temp)) {
549 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
553 group_rid = (uint32)atol(temp);
555 /* for some reason, we often have 0 as a primary group RID.
556 Make sure that we treat this just as a 'default' value */
559 pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
561 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
565 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
566 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
567 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
568 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
573 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
574 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET), temp)) {
575 /* leave as default */
577 pass_last_set_time = (time_t) atol(temp);
578 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
581 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
582 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp)) {
583 /* leave as default */
585 logon_time = (time_t) atol(temp);
586 pdb_set_logon_time(sampass, logon_time, PDB_SET);
589 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
590 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp)) {
591 /* leave as default */
593 logoff_time = (time_t) atol(temp);
594 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
597 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
598 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp)) {
599 /* leave as default */
601 kickoff_time = (time_t) atol(temp);
602 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
605 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
606 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp)) {
607 /* leave as default */
609 pass_can_change_time = (time_t) atol(temp);
610 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
613 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
614 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp)) {
615 /* leave as default */
617 pass_must_change_time = (time_t) atol(temp);
618 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
621 /* recommend that 'gecos' and 'displayName' should refer to the same
622 * attribute OID. userFullName depreciated, only used by Samba
623 * primary rules of LDAP: don't make a new attribute when one is already defined
624 * that fits your needs; using cn then displayName rather than 'userFullName'
627 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
628 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME), fullname)) {
629 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
630 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_CN), fullname)) {
631 /* leave as default */
633 pdb_set_fullname(sampass, fullname, PDB_SET);
636 pdb_set_fullname(sampass, fullname, PDB_SET);
639 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
640 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE), dir_drive))
642 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
644 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
647 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
648 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH), homedir))
650 pdb_set_homedir( sampass,
651 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_home()),
654 pstrcpy( tmpstring, homedir );
655 standard_sub_basic( username, tmpstring, sizeof(tmpstring) );
656 pdb_set_homedir(sampass, tmpstring, PDB_SET);
659 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
660 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT), logon_script))
662 pdb_set_logon_script( sampass,
663 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_script()),
666 pstrcpy( tmpstring, logon_script );
667 standard_sub_basic( username, tmpstring, sizeof(tmpstring) );
668 pdb_set_logon_script(sampass, tmpstring, PDB_SET);
671 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
672 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH), profile_path))
674 pdb_set_profile_path( sampass,
675 talloc_sub_basic( sampass->mem_ctx, username, lp_logon_path()),
678 pstrcpy( tmpstring, profile_path );
679 standard_sub_basic( username, tmpstring, sizeof(tmpstring) );
680 pdb_set_profile_path(sampass, tmpstring, 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_DESC), acct_desc))
686 /* leave as default */
688 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
691 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
692 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS), workstations)) {
693 /* leave as default */;
695 pdb_set_workstations(sampass, workstations, PDB_SET);
698 if (!smbldap_get_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
699 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL), munged_dial, sizeof(munged_dial))) {
700 /* leave as default */;
702 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
705 /* FIXME: hours stuff should be cleaner */
709 memset(hours, 0xff, hours_len);
711 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
712 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW), temp)) {
713 /* leave as default */
715 pdb_gethexpwd(temp, smblmpwd);
716 memset((char *)temp, '\0', strlen(temp)+1);
717 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET))
719 ZERO_STRUCT(smblmpwd);
722 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
723 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW), temp)) {
724 /* leave as default */
726 pdb_gethexpwd(temp, smbntpwd);
727 memset((char *)temp, '\0', strlen(temp)+1);
728 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET))
730 ZERO_STRUCT(smbntpwd);
733 account_policy_get(AP_PASSWORD_HISTORY, &pwHistLen);
735 uint8 *pwhist = NULL;
738 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
739 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
741 if ((pwhist = SMB_MALLOC(pwHistLen * PW_HISTORY_ENTRY_LEN)) == NULL){
742 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
745 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
747 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
748 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY), temp)) {
749 /* leave as default - zeros */
751 BOOL hex_failed = False;
752 for (i = 0; i < pwHistLen; i++){
753 /* Get the 16 byte salt. */
754 if (!pdb_gethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
758 /* Get the 16 byte MD5 hash of salt+passwd. */
759 if (!pdb_gethexpwd(&temp[(i*64)+32],
760 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN])) {
766 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
768 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
771 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
778 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
779 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO), temp)) {
780 acct_ctrl |= ACB_NORMAL;
782 acct_ctrl = pdb_decode_acct_ctrl(temp);
785 acct_ctrl |= ACB_NORMAL;
787 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
790 pdb_set_hours_len(sampass, hours_len, PDB_SET);
791 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
793 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
794 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_COUNT), temp)) {
795 /* leave as default */
797 bad_password_count = (uint32) atol(temp);
798 pdb_set_bad_password_count(sampass, bad_password_count, 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_BAD_PASSWORD_TIME), temp)) {
803 /* leave as default */
805 bad_password_time = (time_t) atol(temp);
806 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
810 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
811 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_COUNT), temp)) {
812 /* leave as default */
814 logon_count = (uint32) atol(temp);
815 pdb_set_logon_count(sampass, logon_count, PDB_SET);
818 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
820 if(!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
821 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_HOURS), temp)) {
822 /* leave as default */
824 pdb_gethexhours(temp, hours);
825 memset((char *)temp, '\0', strlen(temp) +1);
826 pdb_set_hours(sampass, hours, PDB_SET);
830 /* check the timestamp of the cache vs ldap entry */
831 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
835 /* see if we have newer updates */
836 if (!(cache_entry = login_cache_read(sampass))) {
837 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
838 (unsigned int)pdb_get_bad_password_count(sampass),
839 (unsigned int)pdb_get_bad_password_time(sampass)));
843 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
844 (unsigned int)ldap_entry_time, (unsigned int)cache_entry->entry_timestamp,
845 (unsigned int)cache_entry->bad_password_time));
847 if (ldap_entry_time > cache_entry->entry_timestamp) {
848 /* cache is older than directory , so
849 we need to delete the entry but allow the
850 fields to be written out */
851 login_cache_delentry(sampass);
854 pdb_set_acct_ctrl(sampass,
855 pdb_get_acct_ctrl(sampass) |
856 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
858 pdb_set_bad_password_count(sampass,
859 cache_entry->bad_password_count,
861 pdb_set_bad_password_time(sampass,
862 cache_entry->bad_password_time,
866 SAFE_FREE(cache_entry);
870 /**********************************************************************
871 Initialize the ldap db from a SAM_ACCOUNT. Called on update.
872 (Based on init_buffer_from_sam in pdb_tdb.c)
873 *********************************************************************/
875 static BOOL init_ldap_from_sam (struct ldapsam_privates *ldap_state,
876 LDAPMessage *existing,
877 LDAPMod *** mods, SAM_ACCOUNT * sampass,
878 BOOL (*need_update)(const SAM_ACCOUNT *,
884 if (mods == NULL || sampass == NULL) {
885 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
892 * took out adding "objectclass: sambaAccount"
893 * do this on a per-mod basis
895 if (need_update(sampass, PDB_USERNAME))
896 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
897 "uid", pdb_get_username(sampass));
899 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
901 /* only update the RID if we actually need to */
902 if (need_update(sampass, PDB_USERSID)) {
904 fstring dom_sid_string;
905 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
907 switch ( ldap_state->schema_ver ) {
908 case SCHEMAVER_SAMBAACCOUNT:
909 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
910 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
911 sid_to_string(sid_string, user_sid),
912 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
915 slprintf(temp, sizeof(temp) - 1, "%i", rid);
916 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
917 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
921 case SCHEMAVER_SAMBASAMACCOUNT:
922 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
923 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
924 sid_to_string(sid_string, user_sid));
928 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
933 /* we don't need to store the primary group RID - so leaving it
934 'free' to hang off the unix primary group makes life easier */
936 if (need_update(sampass, PDB_GROUPSID)) {
938 fstring dom_sid_string;
939 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
941 switch ( ldap_state->schema_ver ) {
942 case SCHEMAVER_SAMBAACCOUNT:
943 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
944 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
945 sid_to_string(sid_string, group_sid),
946 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
950 slprintf(temp, sizeof(temp) - 1, "%i", rid);
951 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
952 get_userattr_key2string(ldap_state->schema_ver,
953 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
956 case SCHEMAVER_SAMBASAMACCOUNT:
957 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
958 get_userattr_key2string(ldap_state->schema_ver,
959 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_string(sid_string, group_sid));
963 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
969 /* displayName, cn, and gecos should all be the same
970 * most easily accomplished by giving them the same OID
971 * gecos isn't set here b/c it should be handled by the
973 * We change displayName only and fall back to cn if
977 if (need_update(sampass, PDB_FULLNAME))
978 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
979 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
980 pdb_get_fullname(sampass));
982 if (need_update(sampass, PDB_ACCTDESC))
983 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
984 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
985 pdb_get_acct_desc(sampass));
987 if (need_update(sampass, PDB_WORKSTATIONS))
988 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
989 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
990 pdb_get_workstations(sampass));
992 if (need_update(sampass, PDB_MUNGEDDIAL))
993 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
994 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
995 pdb_get_munged_dial(sampass));
997 if (need_update(sampass, PDB_SMBHOME))
998 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
999 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1000 pdb_get_homedir(sampass));
1002 if (need_update(sampass, PDB_DRIVE))
1003 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1004 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1005 pdb_get_dir_drive(sampass));
1007 if (need_update(sampass, PDB_LOGONSCRIPT))
1008 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1009 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1010 pdb_get_logon_script(sampass));
1012 if (need_update(sampass, PDB_PROFILE))
1013 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1014 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1015 pdb_get_profile_path(sampass));
1017 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
1018 if (need_update(sampass, PDB_LOGONTIME))
1019 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1020 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1022 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
1023 if (need_update(sampass, PDB_LOGOFFTIME))
1024 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1025 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1027 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
1028 if (need_update(sampass, PDB_KICKOFFTIME))
1029 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1030 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1032 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
1033 if (need_update(sampass, PDB_CANCHANGETIME))
1034 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1035 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1037 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
1038 if (need_update(sampass, PDB_MUSTCHANGETIME))
1039 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1040 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1043 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1044 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1046 if (need_update(sampass, PDB_LMPASSWD)) {
1047 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1049 pdb_sethexpwd(temp, lm_pw,
1050 pdb_get_acct_ctrl(sampass));
1051 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1052 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1055 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1056 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1060 if (need_update(sampass, PDB_NTPASSWD)) {
1061 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1063 pdb_sethexpwd(temp, nt_pw,
1064 pdb_get_acct_ctrl(sampass));
1065 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1066 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1069 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1070 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1075 if (need_update(sampass, PDB_PWHISTORY)) {
1077 account_policy_get(AP_PASSWORD_HISTORY, &pwHistLen);
1078 if (pwHistLen == 0) {
1079 /* Remove any password history from the LDAP store. */
1080 memset(temp, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1083 int i, currHistLen = 0;
1084 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1085 if (pwhist != NULL) {
1086 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1087 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
1088 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1089 /* Store the salt. */
1090 pdb_sethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1091 /* Followed by the md5 hash of salt + md4 hash */
1092 pdb_sethexpwd(&temp[(i*64)+32],
1093 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1094 DEBUG(100, ("temp=%s\n", temp));
1098 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1099 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1103 if (need_update(sampass, PDB_PASSLASTSET)) {
1104 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
1105 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1106 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1111 if (need_update(sampass, PDB_HOURS)) {
1112 const char *hours = pdb_get_hours(sampass);
1114 pdb_sethexhours(temp, hours);
1115 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1118 get_userattr_key2string(ldap_state->schema_ver,
1119 LDAP_ATTR_LOGON_HOURS),
1124 if (need_update(sampass, PDB_ACCTCTRL))
1125 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1126 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1127 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1129 /* password lockout cache:
1130 - If we are now autolocking or clearing, we write to ldap
1131 - If we are clearing, we delete the cache entry
1132 - If the count is > 0, we update the cache
1134 This even means when autolocking, we cache, just in case the
1135 update doesn't work, and we have to cache the autolock flag */
1137 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1138 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1139 uint16 badcount = pdb_get_bad_password_count(sampass);
1140 time_t badtime = pdb_get_bad_password_time(sampass);
1142 account_policy_get(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1144 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1145 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1147 if ((badcount >= pol) || (badcount == 0)) {
1148 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1149 (unsigned int)badcount, (unsigned int)badtime));
1150 slprintf (temp, sizeof (temp) - 1, "%li", (long)badcount);
1152 ldap_state->smbldap_state->ldap_struct,
1154 get_userattr_key2string(
1155 ldap_state->schema_ver,
1156 LDAP_ATTR_BAD_PASSWORD_COUNT),
1159 slprintf (temp, sizeof (temp) - 1, "%li", badtime);
1161 ldap_state->smbldap_state->ldap_struct,
1163 get_userattr_key2string(
1164 ldap_state->schema_ver,
1165 LDAP_ATTR_BAD_PASSWORD_TIME),
1168 if (badcount == 0) {
1169 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1170 login_cache_delentry(sampass);
1172 LOGIN_CACHE cache_entry;
1174 cache_entry.entry_timestamp = time(NULL);
1175 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1176 cache_entry.bad_password_count = badcount;
1177 cache_entry.bad_password_time = badtime;
1179 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1180 login_cache_write(sampass, cache_entry);
1187 /**********************************************************************
1188 Connect to LDAP server for password enumeration.
1189 *********************************************************************/
1191 static NTSTATUS ldapsam_setsampwent(struct pdb_methods *my_methods, BOOL update, uint16 acb_mask)
1193 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1195 pstring filter, suffix;
1196 const char **attr_list;
1197 BOOL machine_mask = False, user_mask = False;
1199 pstr_sprintf( filter, "(&%s%s)", lp_ldap_filter(),
1200 get_objclass_filter(ldap_state->schema_ver));
1201 all_string_sub(filter, "%u", "*", sizeof(pstring));
1203 machine_mask = ((acb_mask != 0) && (acb_mask & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)));
1204 user_mask = ((acb_mask != 0) && (acb_mask & ACB_NORMAL));
1207 pstrcpy(suffix, lp_ldap_machine_suffix());
1208 } else if (user_mask) {
1209 pstrcpy(suffix, lp_ldap_user_suffix());
1211 pstrcpy(suffix, lp_ldap_suffix());
1214 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1217 attr_list = get_userattr_list(ldap_state->schema_ver);
1218 rc = smbldap_search(ldap_state->smbldap_state, suffix, LDAP_SCOPE_SUBTREE, filter,
1219 attr_list, 0, &ldap_state->result);
1220 free_attr_list( attr_list );
1222 if (rc != LDAP_SUCCESS) {
1223 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc)));
1224 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix, filter));
1225 ldap_msgfree(ldap_state->result);
1226 ldap_state->result = NULL;
1227 return NT_STATUS_UNSUCCESSFUL;
1230 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1231 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
1232 ldap_state->result), suffix));
1234 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
1235 ldap_state->result);
1236 ldap_state->index = 0;
1238 return NT_STATUS_OK;
1241 /**********************************************************************
1242 End enumeration of the LDAP password list.
1243 *********************************************************************/
1245 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1247 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1248 if (ldap_state->result) {
1249 ldap_msgfree(ldap_state->result);
1250 ldap_state->result = NULL;
1254 /**********************************************************************
1255 Get the next entry in the LDAP password database.
1256 *********************************************************************/
1258 static NTSTATUS ldapsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
1260 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1261 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1265 if (!ldap_state->entry)
1268 ldap_state->index++;
1269 bret = init_sam_from_ldap(ldap_state, user, ldap_state->entry);
1271 ldap_state->entry = ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
1275 return NT_STATUS_OK;
1278 static void append_attr(const char ***attr_list, const char *new_attr)
1282 if (new_attr == NULL) {
1286 for (i=0; (*attr_list)[i] != NULL; i++) {
1290 (*attr_list) = SMB_REALLOC_ARRAY((*attr_list), const char *, i+2);
1291 SMB_ASSERT((*attr_list) != NULL);
1292 (*attr_list)[i] = SMB_STRDUP(new_attr);
1293 (*attr_list)[i+1] = NULL;
1296 /**********************************************************************
1297 Get SAM_ACCOUNT entry from LDAP by username.
1298 *********************************************************************/
1300 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
1302 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1303 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1304 LDAPMessage *result = NULL;
1305 LDAPMessage *entry = NULL;
1307 const char ** attr_list;
1310 attr_list = get_userattr_list( ldap_state->schema_ver );
1311 append_attr(&attr_list, get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP));
1312 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result, attr_list);
1313 free_attr_list( attr_list );
1315 if ( rc != LDAP_SUCCESS )
1316 return NT_STATUS_NO_SUCH_USER;
1318 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1321 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1322 ldap_msgfree(result);
1323 return NT_STATUS_NO_SUCH_USER;
1324 } else if (count > 1) {
1325 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1326 ldap_msgfree(result);
1327 return NT_STATUS_NO_SUCH_USER;
1330 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1332 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1333 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1334 ldap_msgfree(result);
1335 return NT_STATUS_NO_SUCH_USER;
1337 pdb_set_backend_private_data(user, result,
1338 private_data_free_fn,
1339 my_methods, PDB_CHANGED);
1342 ldap_msgfree(result);
1347 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1348 const DOM_SID *sid, LDAPMessage **result)
1351 const char ** attr_list;
1354 switch ( ldap_state->schema_ver ) {
1355 case SCHEMAVER_SAMBASAMACCOUNT:
1356 attr_list = get_userattr_list(ldap_state->schema_ver);
1357 append_attr(&attr_list, get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP));
1358 rc = ldapsam_search_suffix_by_sid(ldap_state, sid, result, attr_list);
1359 free_attr_list( attr_list );
1361 if ( rc != LDAP_SUCCESS )
1365 case SCHEMAVER_SAMBAACCOUNT:
1366 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1370 attr_list = get_userattr_list(ldap_state->schema_ver);
1371 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1372 free_attr_list( attr_list );
1374 if ( rc != LDAP_SUCCESS )
1381 /**********************************************************************
1382 Get SAM_ACCOUNT entry from LDAP by SID.
1383 *********************************************************************/
1385 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1387 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1388 LDAPMessage *result = NULL;
1389 LDAPMessage *entry = NULL;
1394 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1396 if (rc != LDAP_SUCCESS)
1397 return NT_STATUS_NO_SUCH_USER;
1399 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1402 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string, sid),
1404 ldap_msgfree(result);
1405 return NT_STATUS_NO_SUCH_USER;
1406 } else if (count > 1) {
1407 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string, sid),
1409 ldap_msgfree(result);
1410 return NT_STATUS_NO_SUCH_USER;
1413 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1415 ldap_msgfree(result);
1416 return NT_STATUS_NO_SUCH_USER;
1419 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1420 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1421 ldap_msgfree(result);
1422 return NT_STATUS_NO_SUCH_USER;
1425 pdb_set_backend_private_data(user, result,
1426 private_data_free_fn,
1427 my_methods, PDB_CHANGED);
1428 return NT_STATUS_OK;
1431 static BOOL ldapsam_can_pwchange_exop(struct smbldap_state *ldap_state)
1433 return smbldap_has_extension(ldap_state, LDAP_EXOP_MODIFY_PASSWD);
1436 /********************************************************************
1437 Do the actual modification - also change a plaintext passord if
1439 **********************************************************************/
1441 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1442 SAM_ACCOUNT *newpwd, char *dn,
1443 LDAPMod **mods, int ldap_op,
1444 BOOL (*need_update)(const SAM_ACCOUNT *, enum pdb_elements))
1446 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1449 if (!my_methods || !newpwd || !dn) {
1450 return NT_STATUS_INVALID_PARAMETER;
1454 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1455 /* may be password change below however */
1459 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1462 rc = smbldap_add(ldap_state->smbldap_state,
1465 case LDAP_MOD_REPLACE:
1466 rc = smbldap_modify(ldap_state->smbldap_state,
1470 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1472 return NT_STATUS_INVALID_PARAMETER;
1475 if (rc!=LDAP_SUCCESS) {
1476 char *ld_error = NULL;
1477 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1479 DEBUG(1, ("ldapsam_modify_entry: Failed to %s user dn= %s with: %s\n\t%s\n",
1480 ldap_op == LDAP_MOD_ADD ? "add" : "modify",
1481 dn, ldap_err2string(rc),
1482 ld_error?ld_error:"unknown"));
1483 SAFE_FREE(ld_error);
1484 return NT_STATUS_UNSUCCESSFUL;
1488 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1489 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1490 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1491 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1495 struct berval *retdata;
1496 char *utf8_password;
1499 if (!ldapsam_can_pwchange_exop(ldap_state->smbldap_state)) {
1500 DEBUG(2, ("ldap password change requested, but LDAP "
1501 "server does not support it -- ignoring\n"));
1502 return NT_STATUS_OK;
1505 if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
1506 return NT_STATUS_NO_MEMORY;
1509 if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
1510 return NT_STATUS_NO_MEMORY;
1513 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1514 DEBUG(0,("ber_alloc_t returns NULL\n"));
1515 SAFE_FREE(utf8_password);
1516 return NT_STATUS_UNSUCCESSFUL;
1519 ber_printf (ber, "{");
1520 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
1521 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
1522 ber_printf (ber, "N}");
1524 if ((rc = ber_flatten (ber, &bv))<0) {
1525 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1528 SAFE_FREE(utf8_password);
1529 return NT_STATUS_UNSUCCESSFUL;
1533 SAFE_FREE(utf8_password);
1536 if ((rc = smbldap_extended_operation(ldap_state->smbldap_state,
1537 LDAP_EXOP_MODIFY_PASSWD,
1538 bv, NULL, NULL, &retoid,
1539 &retdata)) != LDAP_SUCCESS) {
1540 char *ld_error = NULL;
1542 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1543 DEBUG(3, ("Could not set userPassword "
1544 "attribute due to an objectClass "
1545 "violation -- ignoring\n"));
1547 return NT_STATUS_OK;
1550 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1552 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1553 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1554 SAFE_FREE(ld_error);
1556 return NT_STATUS_UNSUCCESSFUL;
1558 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1559 #ifdef DEBUG_PASSWORD
1560 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1562 ber_bvfree(retdata);
1563 ber_memfree(retoid);
1567 return NT_STATUS_OK;
1570 /**********************************************************************
1571 Delete entry from LDAP for username.
1572 *********************************************************************/
1574 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * sam_acct)
1576 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1579 LDAPMessage *result = NULL;
1581 const char **attr_list;
1585 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1586 return NT_STATUS_INVALID_PARAMETER;
1589 sname = pdb_get_username(sam_acct);
1591 DEBUG (3, ("ldapsam_delete_sam_account: Deleting user %s from LDAP.\n", sname));
1593 attr_list= get_userattr_delete_list( ldap_state->schema_ver );
1594 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result, attr_list);
1596 if (rc != LDAP_SUCCESS) {
1597 free_attr_list( attr_list );
1598 return NT_STATUS_NO_SUCH_USER;
1601 switch ( ldap_state->schema_ver ) {
1602 case SCHEMAVER_SAMBASAMACCOUNT:
1603 fstrcpy( objclass, LDAP_OBJ_SAMBASAMACCOUNT );
1606 case SCHEMAVER_SAMBAACCOUNT:
1607 fstrcpy( objclass, LDAP_OBJ_SAMBAACCOUNT );
1610 fstrcpy( objclass, "UNKNOWN" );
1611 DEBUG(0,("ldapsam_delete_sam_account: Unknown schema version specified!\n"));
1615 ret = ldapsam_delete_entry(ldap_state, result, objclass, attr_list );
1616 ldap_msgfree(result);
1617 free_attr_list( attr_list );
1622 /**********************************************************************
1623 Helper function to determine for update_sam_account whether
1624 we need LDAP modification.
1625 *********************************************************************/
1627 static BOOL element_is_changed(const SAM_ACCOUNT *sampass,
1628 enum pdb_elements element)
1630 return IS_SAM_CHANGED(sampass, element);
1633 /**********************************************************************
1635 *********************************************************************/
1637 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * newpwd)
1639 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1640 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1643 LDAPMessage *result = NULL;
1644 LDAPMessage *entry = NULL;
1645 LDAPMod **mods = NULL;
1646 const char **attr_list;
1648 result = pdb_get_backend_private_data(newpwd, my_methods);
1650 attr_list = get_userattr_list(ldap_state->schema_ver);
1651 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1652 free_attr_list( attr_list );
1653 if (rc != LDAP_SUCCESS) {
1654 return NT_STATUS_UNSUCCESSFUL;
1656 pdb_set_backend_private_data(newpwd, result, private_data_free_fn, my_methods, PDB_CHANGED);
1659 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1660 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1661 return NT_STATUS_UNSUCCESSFUL;
1664 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1665 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
1667 return NT_STATUS_UNSUCCESSFUL;
1670 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1672 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1673 element_is_changed)) {
1674 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1677 ldap_mods_free(mods,True);
1678 return NT_STATUS_UNSUCCESSFUL;
1682 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1683 pdb_get_username(newpwd)));
1685 return NT_STATUS_OK;
1688 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1689 ldap_mods_free(mods,True);
1692 if (!NT_STATUS_IS_OK(ret)) {
1693 char *ld_error = NULL;
1694 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1696 DEBUG(0,("ldapsam_update_sam_account: failed to modify user with uid = %s, error: %s (%s)\n",
1697 pdb_get_username(newpwd), ld_error?ld_error:"(unknwon)", ldap_err2string(rc)));
1698 SAFE_FREE(ld_error);
1702 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1703 pdb_get_username(newpwd)));
1704 return NT_STATUS_OK;
1707 /**********************************************************************
1708 Helper function to determine for update_sam_account whether
1709 we need LDAP modification.
1710 *********************************************************************/
1712 static BOOL element_is_set_or_changed(const SAM_ACCOUNT *sampass,
1713 enum pdb_elements element)
1715 return (IS_SAM_SET(sampass, element) ||
1716 IS_SAM_CHANGED(sampass, element));
1719 /**********************************************************************
1720 Add SAM_ACCOUNT to LDAP.
1721 *********************************************************************/
1723 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * newpwd)
1725 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1726 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1728 LDAPMessage *result = NULL;
1729 LDAPMessage *entry = NULL;
1731 LDAPMod **mods = NULL;
1732 int ldap_op = LDAP_MOD_REPLACE;
1734 const char **attr_list;
1736 const char *username = pdb_get_username(newpwd);
1737 const DOM_SID *sid = pdb_get_user_sid(newpwd);
1741 if (!username || !*username) {
1742 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1743 return NT_STATUS_INVALID_PARAMETER;
1746 /* free this list after the second search or in case we exit on failure */
1747 attr_list = get_userattr_list(ldap_state->schema_ver);
1749 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
1751 if (rc != LDAP_SUCCESS) {
1752 free_attr_list( attr_list );
1753 return NT_STATUS_UNSUCCESSFUL;
1756 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1757 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1759 ldap_msgfree(result);
1760 free_attr_list( attr_list );
1761 return NT_STATUS_UNSUCCESSFUL;
1763 ldap_msgfree(result);
1766 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
1767 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1769 if (rc == LDAP_SUCCESS) {
1770 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1771 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1772 sid_to_string(sid_string, sid)));
1773 free_attr_list( attr_list );
1774 ldap_msgfree(result);
1775 return NT_STATUS_UNSUCCESSFUL;
1777 ldap_msgfree(result);
1781 /* does the entry already exist but without a samba attributes?
1782 we need to return the samba attributes here */
1784 escape_user = escape_ldap_string_alloc( username );
1785 pstrcpy( filter, lp_ldap_filter() );
1786 all_string_sub( filter, "%u", escape_user, sizeof(filter) );
1787 SAFE_FREE( escape_user );
1789 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1790 filter, attr_list, &result);
1791 if ( rc != LDAP_SUCCESS ) {
1792 free_attr_list( attr_list );
1793 return NT_STATUS_UNSUCCESSFUL;
1796 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1798 if (num_result > 1) {
1799 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1800 free_attr_list( attr_list );
1801 ldap_msgfree(result);
1802 return NT_STATUS_UNSUCCESSFUL;
1805 /* Check if we need to update an existing entry */
1806 if (num_result == 1) {
1809 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1810 ldap_op = LDAP_MOD_REPLACE;
1811 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
1812 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
1814 free_attr_list( attr_list );
1815 ldap_msgfree(result);
1816 return NT_STATUS_UNSUCCESSFUL;
1818 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1821 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
1823 /* There might be a SID for this account already - say an idmap entry */
1825 pstr_sprintf(filter, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
1826 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1827 sid_to_string(sid_string, sid),
1828 LDAP_OBJ_IDMAP_ENTRY,
1829 LDAP_OBJ_SID_ENTRY);
1831 /* free old result before doing a new search */
1832 if (result != NULL) {
1833 ldap_msgfree(result);
1836 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1837 filter, attr_list, &result);
1839 if ( rc != LDAP_SUCCESS ) {
1840 free_attr_list( attr_list );
1841 return NT_STATUS_UNSUCCESSFUL;
1844 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1846 if (num_result > 1) {
1847 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1848 free_attr_list( attr_list );
1849 ldap_msgfree(result);
1850 return NT_STATUS_UNSUCCESSFUL;
1853 /* Check if we need to update an existing entry */
1854 if (num_result == 1) {
1857 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1858 ldap_op = LDAP_MOD_REPLACE;
1859 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
1860 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
1862 free_attr_list( attr_list );
1863 ldap_msgfree(result);
1864 return NT_STATUS_UNSUCCESSFUL;
1866 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1871 free_attr_list( attr_list );
1873 if (num_result == 0) {
1874 /* Check if we need to add an entry */
1875 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
1876 ldap_op = LDAP_MOD_ADD;
1877 if (username[strlen(username)-1] == '$') {
1878 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_machine_suffix ());
1880 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_user_suffix ());
1884 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1885 element_is_set_or_changed)) {
1886 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
1887 ldap_msgfree(result);
1889 ldap_mods_free(mods,True);
1890 return NT_STATUS_UNSUCCESSFUL;
1893 ldap_msgfree(result);
1896 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
1897 return NT_STATUS_UNSUCCESSFUL;
1899 switch ( ldap_state->schema_ver ) {
1900 case SCHEMAVER_SAMBAACCOUNT:
1901 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
1903 case SCHEMAVER_SAMBASAMACCOUNT:
1904 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
1907 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
1911 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
1912 if (!NT_STATUS_IS_OK(ret)) {
1913 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
1914 pdb_get_username(newpwd),dn));
1915 ldap_mods_free(mods, True);
1919 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
1920 ldap_mods_free(mods, True);
1922 return NT_STATUS_OK;
1925 /**********************************************************************
1926 *********************************************************************/
1928 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
1930 LDAPMessage ** result)
1932 int scope = LDAP_SCOPE_SUBTREE;
1934 const char **attr_list;
1936 attr_list = get_attr_list(groupmap_attr_list);
1937 rc = smbldap_search(ldap_state->smbldap_state,
1938 lp_ldap_group_suffix (), scope,
1939 filter, attr_list, 0, result);
1940 free_attr_list( attr_list );
1942 if (rc != LDAP_SUCCESS) {
1943 char *ld_error = NULL;
1944 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1946 DEBUG(0, ("ldapsam_search_one_group: "
1947 "Problem during the LDAP search: LDAP error: %s (%s)\n",
1948 ld_error?ld_error:"(unknown)", ldap_err2string(rc)));
1949 DEBUGADD(3, ("ldapsam_search_one_group: Query was: %s, %s\n",
1950 lp_ldap_group_suffix(), filter));
1951 SAFE_FREE(ld_error);
1957 /**********************************************************************
1958 *********************************************************************/
1960 static BOOL init_group_from_ldap(struct ldapsam_privates *ldap_state,
1961 GROUP_MAP *map, LDAPMessage *entry)
1965 if (ldap_state == NULL || map == NULL || entry == NULL ||
1966 ldap_state->smbldap_state->ldap_struct == NULL) {
1967 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
1971 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
1972 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER), temp)) {
1973 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1974 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
1977 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
1979 map->gid = (gid_t)atol(temp);
1981 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
1982 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID), temp)) {
1983 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1984 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
1988 if (!string_to_sid(&map->sid, temp)) {
1989 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
1993 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
1994 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), temp)) {
1995 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1996 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
1999 map->sid_name_use = (enum SID_NAME_USE)atol(temp);
2001 if ((map->sid_name_use < SID_NAME_USER) ||
2002 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2003 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2007 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2008 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), temp)) {
2010 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2011 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_CN), temp))
2013 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2014 for gidNumber(%lu)\n",(unsigned long)map->gid));
2018 fstrcpy(map->nt_name, temp);
2020 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2021 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), temp)) {
2024 fstrcpy(map->comment, temp);
2029 /**********************************************************************
2030 *********************************************************************/
2032 static BOOL init_ldap_from_group(LDAP *ldap_struct,
2033 LDAPMessage *existing,
2035 const GROUP_MAP *map)
2039 if (mods == NULL || map == NULL) {
2040 DEBUG(0, ("init_ldap_from_group: NULL parameters found!\n"));
2046 sid_to_string(tmp, &map->sid);
2048 smbldap_make_mod(ldap_struct, existing, mods,
2049 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID), tmp);
2050 pstr_sprintf(tmp, "%i", map->sid_name_use);
2051 smbldap_make_mod(ldap_struct, existing, mods,
2052 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), tmp);
2054 smbldap_make_mod(ldap_struct, existing, mods,
2055 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), map->nt_name);
2056 smbldap_make_mod(ldap_struct, existing, mods,
2057 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), map->comment);
2062 /**********************************************************************
2063 *********************************************************************/
2065 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2069 struct ldapsam_privates *ldap_state =
2070 (struct ldapsam_privates *)methods->private_data;
2071 LDAPMessage *result = NULL;
2072 LDAPMessage *entry = NULL;
2075 if (ldapsam_search_one_group(ldap_state, filter, &result)
2077 return NT_STATUS_NO_SUCH_GROUP;
2080 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2083 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2084 ldap_msgfree(result);
2085 return NT_STATUS_NO_SUCH_GROUP;
2089 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: count=%d\n",
2091 ldap_msgfree(result);
2092 return NT_STATUS_NO_SUCH_GROUP;
2095 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2098 ldap_msgfree(result);
2099 return NT_STATUS_UNSUCCESSFUL;
2102 if (!init_group_from_ldap(ldap_state, map, entry)) {
2103 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for group filter %s\n",
2105 ldap_msgfree(result);
2106 return NT_STATUS_NO_SUCH_GROUP;
2109 ldap_msgfree(result);
2110 return NT_STATUS_OK;
2113 /**********************************************************************
2114 *********************************************************************/
2116 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2121 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2123 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2124 sid_string_static(&sid));
2126 return ldapsam_getgroup(methods, filter, map);
2129 /**********************************************************************
2130 *********************************************************************/
2132 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2137 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%lu))",
2139 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2140 (unsigned long)gid);
2142 return ldapsam_getgroup(methods, filter, map);
2145 /**********************************************************************
2146 *********************************************************************/
2148 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2152 char *escape_name = escape_ldap_string_alloc(name);
2155 return NT_STATUS_NO_MEMORY;
2158 pstr_sprintf(filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2160 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2161 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN), escape_name);
2163 SAFE_FREE(escape_name);
2165 return ldapsam_getgroup(methods, filter, map);
2168 static void add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
2169 uint32 rid, uint32 **rids, int *num)
2173 for (i=0; i<*num; i++) {
2174 if ((*rids)[i] == rid)
2178 *rids = TALLOC_REALLOC_ARRAY(mem_ctx, *rids, uint32, *num+1);
2183 (*rids)[*num] = rid;
2187 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2188 TALLOC_CTX *mem_ctx,
2189 const DOM_SID *group,
2190 uint32 **member_rids,
2193 struct ldapsam_privates *ldap_state =
2194 (struct ldapsam_privates *)methods->private_data;
2195 struct smbldap_state *conn = ldap_state->smbldap_state;
2198 LDAPMessage *msg = NULL;
2200 char **values = NULL;
2202 char *sid_filter = NULL;
2204 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2206 if (!lp_parm_bool(-1, "ldapsam", "trusted", False))
2207 return pdb_default_enum_group_members(methods, mem_ctx, group,
2211 *member_rids = NULL;
2214 pstr_sprintf(filter,
2215 "(&(objectClass=sambaSamAccount)"
2216 "(sambaPrimaryGroupSid=%s))",
2217 sid_string_static(group));
2220 const char *attrs[] = { "sambaSID", NULL };
2221 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2222 LDAP_SCOPE_SUBTREE, filter, attrs, 0,
2226 if (rc != LDAP_SUCCESS)
2229 for (entry = ldap_first_entry(conn->ldap_struct, msg);
2231 entry = ldap_next_entry(conn->ldap_struct, entry))
2237 if (!smbldap_get_single_attribute(conn->ldap_struct,
2239 str, sizeof(str)-1))
2242 if (!string_to_sid(&sid, str))
2245 if (!sid_check_is_in_our_domain(&sid)) {
2246 DEBUG(1, ("Inconsistent SAM -- group member uid not "
2247 "in our domain\n"));
2251 sid_peek_rid(&sid, &rid);
2253 add_rid_to_array_unique(mem_ctx, rid, member_rids,
2260 pstr_sprintf(filter,
2261 "(&(objectClass=sambaGroupMapping)"
2262 "(objectClass=posixGroup)"
2264 sid_string_static(group));
2267 const char *attrs[] = { "memberUid", NULL };
2268 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2269 LDAP_SCOPE_SUBTREE, filter, attrs, 0,
2273 if (rc != LDAP_SUCCESS)
2276 count = ldap_count_entries(conn->ldap_struct, msg);
2279 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2280 sid_string_static(group)));
2285 result = NT_STATUS_OK;
2289 entry = ldap_first_entry(conn->ldap_struct, msg);
2293 values = ldap_get_values(conn->ldap_struct, msg, "memberUid");
2294 if (values == NULL) {
2295 result = NT_STATUS_OK;
2299 sid_filter = strdup("(&(objectClass=sambaSamAccount)(|");
2300 if (sid_filter == NULL) {
2301 result = NT_STATUS_NO_MEMORY;
2305 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2307 asprintf(&sid_filter, "%s(uid=%s)", tmp, *memberuid);
2309 if (sid_filter == NULL) {
2310 result = NT_STATUS_NO_MEMORY;
2316 asprintf(&sid_filter, "%s))", sid_filter);
2318 if (sid_filter == NULL) {
2319 result = NT_STATUS_NO_MEMORY;
2324 const char *attrs[] = { "sambaSID", NULL };
2325 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2326 LDAP_SCOPE_SUBTREE, sid_filter, attrs, 0,
2330 if (rc != LDAP_SUCCESS)
2333 for (entry = ldap_first_entry(conn->ldap_struct, msg);
2335 entry = ldap_next_entry(conn->ldap_struct, entry))
2341 if (!smbldap_get_single_attribute(conn->ldap_struct,
2343 str, sizeof(str)-1))
2346 if (!string_to_sid(&sid, str))
2349 if (!sid_check_is_in_our_domain(&sid)) {
2350 DEBUG(1, ("Inconsistent SAM -- group member uid not "
2351 "in our domain\n"));
2355 sid_peek_rid(&sid, &rid);
2357 add_rid_to_array_unique(mem_ctx, rid, member_rids,
2361 result = NT_STATUS_OK;
2364 SAFE_FREE(sid_filter);
2367 ldap_value_free(values);
2375 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2376 const char *username,
2378 DOM_SID **sids, gid_t **gids,
2381 struct ldapsam_privates *ldap_state =
2382 (struct ldapsam_privates *)methods->private_data;
2383 struct smbldap_state *conn = ldap_state->smbldap_state;
2385 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2388 LDAPMessage *msg = NULL;
2390 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2391 int num_sids, num_gids;
2392 extern DOM_SID global_sid_NULL;
2394 if (!lp_parm_bool(-1, "ldapsam", "trusted", False))
2395 return pdb_default_enum_group_memberships(methods, username,
2402 escape_name = escape_ldap_string_alloc(username);
2404 if (escape_name == NULL)
2405 return NT_STATUS_NO_MEMORY;
2407 pstr_sprintf(filter, "(&(objectClass=posixGroup)"
2408 "(|(memberUid=%s)(gidNumber=%d)))",
2409 username, primary_gid);
2411 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2412 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &msg);
2414 if (rc != LDAP_SUCCESS)
2423 /* We need to add the primary group as the first gid/sid */
2425 add_gid_to_array_unique(primary_gid, gids, &num_gids);
2427 /* This sid will be replaced later */
2429 add_sid_to_array_unique(&global_sid_NULL, sids, &num_sids);
2431 for (entry = ldap_first_entry(conn->ldap_struct, msg);
2433 entry = ldap_next_entry(conn->ldap_struct, entry))
2440 if (!smbldap_get_single_attribute(conn->ldap_struct,
2442 str, sizeof(str)-1))
2445 if (!string_to_sid(&sid, str))
2448 if (!smbldap_get_single_attribute(conn->ldap_struct,
2450 str, sizeof(str)-1))
2453 gid = strtoul(str, &end, 10);
2455 if (PTR_DIFF(end, str) != strlen(str))
2458 if (gid == primary_gid) {
2459 sid_copy(&(*sids)[0], &sid);
2461 add_gid_to_array_unique(gid, gids, &num_gids);
2462 add_sid_to_array_unique(&sid, sids, &num_sids);
2466 if (sid_compare(&global_sid_NULL, &(*sids)[0]) == 0) {
2467 DEBUG(3, ("primary group of [%s] not found\n", username));
2471 *num_groups = num_sids;
2473 result = NT_STATUS_OK;
2477 SAFE_FREE(escape_name);
2484 /**********************************************************************
2485 *********************************************************************/
2487 static int ldapsam_search_one_group_by_gid(struct ldapsam_privates *ldap_state,
2489 LDAPMessage **result)
2493 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%lu))",
2494 LDAP_OBJ_POSIXGROUP, LDAP_OBJ_IDMAP_ENTRY,
2495 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2496 (unsigned long)gid);
2498 return ldapsam_search_one_group(ldap_state, filter, result);
2501 /**********************************************************************
2502 *********************************************************************/
2504 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
2507 struct ldapsam_privates *ldap_state =
2508 (struct ldapsam_privates *)methods->private_data;
2509 LDAPMessage *result = NULL;
2510 LDAPMod **mods = NULL;
2521 if (NT_STATUS_IS_OK(ldapsam_getgrgid(methods, &dummy,
2523 DEBUG(0, ("ldapsam_add_group_mapping_entry: Group %ld already exists in LDAP\n", (unsigned long)map->gid));
2524 return NT_STATUS_UNSUCCESSFUL;
2527 rc = ldapsam_search_one_group_by_gid(ldap_state, map->gid, &result);
2528 if (rc != LDAP_SUCCESS) {
2529 ldap_msgfree(result);
2530 return NT_STATUS_UNSUCCESSFUL;
2533 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2536 /* There's no posixGroup account, let's try to find an
2537 * appropriate idmap entry for aliases */
2541 const char **attr_list;
2543 ldap_msgfree(result);
2545 pstrcpy( suffix, lp_ldap_idmap_suffix() );
2546 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%u))",
2547 LDAP_OBJ_IDMAP_ENTRY, LDAP_ATTRIBUTE_GIDNUMBER,
2550 attr_list = get_attr_list( sidmap_attr_list );
2551 rc = smbldap_search(ldap_state->smbldap_state, suffix,
2552 LDAP_SCOPE_SUBTREE, filter, attr_list,
2555 free_attr_list(attr_list);
2557 if (rc != LDAP_SUCCESS) {
2558 DEBUG(3,("Failure looking up entry (%s)\n",
2559 ldap_err2string(rc) ));
2560 ldap_msgfree(result);
2561 return NT_STATUS_UNSUCCESSFUL;
2565 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2567 ldap_msgfree(result);
2568 return NT_STATUS_UNSUCCESSFUL;
2572 DEBUG(2, ("ldapsam_add_group_mapping_entry: Group %lu must exist exactly once in LDAP\n",
2573 (unsigned long)map->gid));
2574 ldap_msgfree(result);
2575 return NT_STATUS_UNSUCCESSFUL;
2578 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2579 tmp = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2581 ldap_msgfree(result);
2582 return NT_STATUS_UNSUCCESSFUL;
2587 if (!init_ldap_from_group(ldap_state->smbldap_state->ldap_struct,
2588 result, &mods, map)) {
2589 DEBUG(0, ("ldapsam_add_group_mapping_entry: init_ldap_from_group failed!\n"));
2590 ldap_mods_free(mods, True);
2591 ldap_msgfree(result);
2592 return NT_STATUS_UNSUCCESSFUL;
2595 ldap_msgfree(result);
2598 DEBUG(0, ("ldapsam_add_group_mapping_entry: mods is empty\n"));
2599 return NT_STATUS_UNSUCCESSFUL;
2602 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP );
2604 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2605 ldap_mods_free(mods, True);
2607 if (rc != LDAP_SUCCESS) {
2608 char *ld_error = NULL;
2609 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
2611 DEBUG(0, ("ldapsam_add_group_mapping_entry: failed to add group %lu error: %s (%s)\n", (unsigned long)map->gid,
2612 ld_error ? ld_error : "(unknown)", ldap_err2string(rc)));
2613 SAFE_FREE(ld_error);
2614 return NT_STATUS_UNSUCCESSFUL;
2617 DEBUG(2, ("ldapsam_add_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map->gid));
2618 return NT_STATUS_OK;
2621 /**********************************************************************
2622 *********************************************************************/
2624 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
2627 struct ldapsam_privates *ldap_state =
2628 (struct ldapsam_privates *)methods->private_data;
2631 LDAPMessage *result = NULL;
2632 LDAPMessage *entry = NULL;
2633 LDAPMod **mods = NULL;
2635 rc = ldapsam_search_one_group_by_gid(ldap_state, map->gid, &result);
2637 if (rc != LDAP_SUCCESS) {
2638 return NT_STATUS_UNSUCCESSFUL;
2641 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
2642 DEBUG(0, ("ldapsam_update_group_mapping_entry: No group to modify!\n"));
2643 ldap_msgfree(result);
2644 return NT_STATUS_UNSUCCESSFUL;
2647 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2649 if (!init_ldap_from_group(ldap_state->smbldap_state->ldap_struct,
2650 result, &mods, map)) {
2651 DEBUG(0, ("ldapsam_update_group_mapping_entry: init_ldap_from_group failed\n"));
2652 ldap_msgfree(result);
2654 ldap_mods_free(mods,True);
2655 return NT_STATUS_UNSUCCESSFUL;
2659 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: nothing to do\n"));
2660 ldap_msgfree(result);
2661 return NT_STATUS_OK;
2664 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2666 ldap_msgfree(result);
2667 return NT_STATUS_UNSUCCESSFUL;
2669 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2672 ldap_mods_free(mods, True);
2673 ldap_msgfree(result);
2675 if (rc != LDAP_SUCCESS) {
2676 char *ld_error = NULL;
2677 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
2679 DEBUG(0, ("ldapsam_update_group_mapping_entry: failed to modify group %lu error: %s (%s)\n", (unsigned long)map->gid,
2680 ld_error ? ld_error : "(unknown)", ldap_err2string(rc)));
2681 SAFE_FREE(ld_error);
2682 return NT_STATUS_UNSUCCESSFUL;
2685 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map->gid));
2686 return NT_STATUS_OK;
2689 /**********************************************************************
2690 *********************************************************************/
2692 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
2695 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)methods->private_data;
2696 pstring sidstring, filter;
2697 LDAPMessage *result = NULL;
2700 const char **attr_list;
2702 sid_to_string(sidstring, &sid);
2704 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2705 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID, sidstring);
2707 rc = ldapsam_search_one_group(ldap_state, filter, &result);
2709 if (rc != LDAP_SUCCESS) {
2710 return NT_STATUS_NO_SUCH_GROUP;
2713 attr_list = get_attr_list( groupmap_attr_list_to_delete );
2714 ret = ldapsam_delete_entry(ldap_state, result, LDAP_OBJ_GROUPMAP, attr_list);
2715 free_attr_list ( attr_list );
2717 ldap_msgfree(result);
2722 /**********************************************************************
2723 *********************************************************************/
2725 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods, BOOL update)
2727 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2730 const char **attr_list;
2732 pstr_sprintf( filter, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
2733 attr_list = get_attr_list( groupmap_attr_list );
2734 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
2735 LDAP_SCOPE_SUBTREE, filter,
2736 attr_list, 0, &ldap_state->result);
2737 free_attr_list( attr_list );
2739 if (rc != LDAP_SUCCESS) {
2740 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n", ldap_err2string(rc)));
2741 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n", lp_ldap_group_suffix(), filter));
2742 ldap_msgfree(ldap_state->result);
2743 ldap_state->result = NULL;
2744 return NT_STATUS_UNSUCCESSFUL;
2747 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
2748 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2749 ldap_state->result)));
2751 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, ldap_state->result);
2752 ldap_state->index = 0;
2754 return NT_STATUS_OK;
2757 /**********************************************************************
2758 *********************************************************************/
2760 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
2762 ldapsam_endsampwent(my_methods);
2765 /**********************************************************************
2766 *********************************************************************/
2768 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
2771 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2772 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2776 if (!ldap_state->entry)
2779 ldap_state->index++;
2780 bret = init_group_from_ldap(ldap_state, map, ldap_state->entry);
2782 ldap_state->entry = ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
2786 return NT_STATUS_OK;
2789 /**********************************************************************
2790 *********************************************************************/
2792 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
2793 enum SID_NAME_USE sid_name_use,
2794 GROUP_MAP **rmap, int *num_entries,
2804 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
2805 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open passdb\n"));
2806 return NT_STATUS_ACCESS_DENIED;
2809 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
2810 if (sid_name_use != SID_NAME_UNKNOWN &&
2811 sid_name_use != map.sid_name_use) {
2812 DEBUG(11,("ldapsam_enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
2815 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
2816 DEBUG(11,("ldapsam_enum_group_mapping: group %s is non mapped\n", map.nt_name));
2820 mapt=SMB_REALLOC_ARRAY((*rmap), GROUP_MAP, entries+1);
2822 DEBUG(0,("ldapsam_enum_group_mapping: Unable to enlarge group map!\n"));
2824 return NT_STATUS_UNSUCCESSFUL;
2829 mapt[entries] = map;
2834 ldapsam_endsamgrent(methods);
2836 *num_entries = entries;
2838 return NT_STATUS_OK;
2841 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
2842 const DOM_SID *alias,
2843 const DOM_SID *member,
2846 struct ldapsam_privates *ldap_state =
2847 (struct ldapsam_privates *)methods->private_data;
2849 LDAPMessage *result = NULL;
2850 LDAPMessage *entry = NULL;
2852 LDAPMod **mods = NULL;
2857 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%s))",
2858 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY,
2859 get_attr_key2string(groupmap_attr_list,
2860 LDAP_ATTR_GROUP_SID),
2861 sid_string_static(alias));
2863 if (ldapsam_search_one_group(ldap_state, filter,
2864 &result) != LDAP_SUCCESS)
2865 return NT_STATUS_NO_SUCH_ALIAS;
2867 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2871 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
2872 ldap_msgfree(result);
2873 return NT_STATUS_NO_SUCH_ALIAS;
2877 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for filter %s: "
2878 "count=%d\n", filter, count));
2879 ldap_msgfree(result);
2880 return NT_STATUS_NO_SUCH_ALIAS;
2883 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
2887 ldap_msgfree(result);
2888 return NT_STATUS_UNSUCCESSFUL;
2891 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2893 ldap_msgfree(result);
2894 return NT_STATUS_UNSUCCESSFUL;
2897 smbldap_set_mod(&mods, modop,
2898 get_attr_key2string(groupmap_attr_list,
2899 LDAP_ATTR_SID_LIST),
2900 sid_string_static(member));
2902 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2904 ldap_mods_free(mods, True);
2905 ldap_msgfree(result);
2907 if (rc != LDAP_SUCCESS) {
2908 char *ld_error = NULL;
2909 ldap_get_option(ldap_state->smbldap_state->ldap_struct,
2910 LDAP_OPT_ERROR_STRING,&ld_error);
2912 DEBUG(0, ("ldapsam_modify_aliasmem: Could not modify alias "
2913 "for %s, error: %s (%s)\n", dn, ldap_err2string(rc),
2914 ld_error?ld_error:"unknown"));
2915 SAFE_FREE(ld_error);
2917 return NT_STATUS_UNSUCCESSFUL;
2922 return NT_STATUS_OK;
2925 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
2926 const DOM_SID *alias,
2927 const DOM_SID *member)
2929 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
2932 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
2933 const DOM_SID *alias,
2934 const DOM_SID *member)
2936 return ldapsam_modify_aliasmem(methods, alias, member,
2940 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
2941 const DOM_SID *alias, DOM_SID **members,
2944 struct ldapsam_privates *ldap_state =
2945 (struct ldapsam_privates *)methods->private_data;
2946 LDAPMessage *result = NULL;
2947 LDAPMessage *entry = NULL;
2956 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%s))",
2957 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY,
2958 get_attr_key2string(groupmap_attr_list,
2959 LDAP_ATTR_GROUP_SID),
2960 sid_string_static(alias));
2962 if (ldapsam_search_one_group(ldap_state, filter,
2963 &result) != LDAP_SUCCESS)
2964 return NT_STATUS_NO_SUCH_ALIAS;
2966 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2970 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
2971 ldap_msgfree(result);
2972 return NT_STATUS_NO_SUCH_ALIAS;
2976 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for filter %s: "
2977 "count=%d\n", filter, count));
2978 ldap_msgfree(result);
2979 return NT_STATUS_NO_SUCH_ALIAS;
2982 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
2986 ldap_msgfree(result);
2987 return NT_STATUS_UNSUCCESSFUL;
2990 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
2992 get_attr_key2string(groupmap_attr_list,
2993 LDAP_ATTR_SID_LIST));
2995 if (values == NULL) {
2996 ldap_msgfree(result);
2997 return NT_STATUS_OK;
3000 count = ldap_count_values(values);
3002 for (i=0; i<count; i++) {
3005 if (!string_to_sid(&member, values[i]))
3008 add_sid_to_array(&member, members, num_members);
3011 ldap_value_free(values);
3012 ldap_msgfree(result);
3014 return NT_STATUS_OK;
3017 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3018 const DOM_SID *members,
3020 DOM_SID **aliases, int *num_aliases)
3022 struct ldapsam_privates *ldap_state =
3023 (struct ldapsam_privates *)methods->private_data;
3026 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3028 LDAPMessage *result = NULL;
3029 LDAPMessage *entry = NULL;
3033 TALLOC_CTX *mem_ctx;
3035 mem_ctx = talloc_init("ldapsam_alias_memberships");
3037 if (mem_ctx == NULL)
3038 return NT_STATUS_NO_MEMORY;
3040 /* This query could be further optimized by adding a
3041 (&(sambaSID=<domain-sid>*)) so that only those aliases that are
3042 asked for in the getuseraliases are returned. */
3044 filter = talloc_asprintf(mem_ctx,
3045 "(&(|(objectclass=%s)(objectclass=%s))(|",
3046 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY);
3048 for (i=0; i<num_members; i++)
3049 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3051 sid_string_static(&members[i]));
3053 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3055 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3056 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3058 talloc_destroy(mem_ctx);
3060 if (rc != LDAP_SUCCESS)
3061 return NT_STATUS_UNSUCCESSFUL;
3066 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3068 for (entry = ldap_first_entry(ldap_struct, result);
3070 entry = ldap_next_entry(ldap_struct, entry))
3075 if (!smbldap_get_single_attribute(ldap_struct, entry,
3081 if (!string_to_sid(&sid, sid_str))
3084 add_sid_to_array_unique(&sid, aliases, num_aliases);
3087 ldap_msgfree(result);
3088 return NT_STATUS_OK;
3091 /**********************************************************************
3093 *********************************************************************/
3095 static void free_private_data(void **vp)
3097 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
3099 smbldap_free_struct(&(*ldap_state)->smbldap_state);
3101 if ((*ldap_state)->result != NULL) {
3102 ldap_msgfree((*ldap_state)->result);
3103 (*ldap_state)->result = NULL;
3105 if ((*ldap_state)->domain_dn != NULL) {
3106 SAFE_FREE((*ldap_state)->domain_dn);
3111 /* No need to free any further, as it is talloc()ed */
3114 /**********************************************************************
3115 Intitalise the parts of the pdb_context that are common to all pdb_ldap modes
3116 *********************************************************************/
3118 static NTSTATUS pdb_init_ldapsam_common(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method,
3119 const char *location)
3122 struct ldapsam_privates *ldap_state;
3124 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
3128 (*pdb_method)->name = "ldapsam";
3130 (*pdb_method)->setsampwent = ldapsam_setsampwent;
3131 (*pdb_method)->endsampwent = ldapsam_endsampwent;
3132 (*pdb_method)->getsampwent = ldapsam_getsampwent;
3133 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
3134 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
3135 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
3136 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
3137 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
3139 (*pdb_method)->getgrsid = ldapsam_getgrsid;
3140 (*pdb_method)->getgrgid = ldapsam_getgrgid;
3141 (*pdb_method)->getgrnam = ldapsam_getgrnam;
3142 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
3143 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
3144 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
3145 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
3146 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
3147 (*pdb_method)->enum_group_memberships = ldapsam_enum_group_memberships;
3149 /* TODO: Setup private data and free */
3151 ldap_state = TALLOC_ZERO_P(pdb_context->mem_ctx, struct ldapsam_privates);
3153 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
3154 return NT_STATUS_NO_MEMORY;
3157 if (!NT_STATUS_IS_OK(nt_status =
3158 smbldap_init(pdb_context->mem_ctx, location,
3159 &ldap_state->smbldap_state)));
3161 ldap_state->domain_name = talloc_strdup(pdb_context->mem_ctx, get_global_sam_name());
3162 if (!ldap_state->domain_name) {
3163 return NT_STATUS_NO_MEMORY;
3166 (*pdb_method)->private_data = ldap_state;
3168 (*pdb_method)->free_private_data = free_private_data;
3170 return NT_STATUS_OK;
3173 /**********************************************************************
3174 Initialise the 'compat' mode for pdb_ldap
3175 *********************************************************************/
3177 static NTSTATUS pdb_init_ldapsam_compat(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
3180 struct ldapsam_privates *ldap_state;
3182 #ifdef WITH_LDAP_SAMCONFIG
3184 int ldap_port = lp_ldap_port();
3186 /* remap default port if not using SSL (ie clear or TLS) */
3187 if ( (lp_ldap_ssl() != LDAP_SSL_ON) && (ldap_port == 636) ) {
3191 location = talloc_asprintf(pdb_context->mem_ctx, "%s://%s:%d", lp_ldap_ssl() == LDAP_SSL_ON ? "ldaps" : "ldap", lp_ldap_server(), ldap_port);
3193 return NT_STATUS_NO_MEMORY;
3198 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common(pdb_context, pdb_method, location))) {
3202 (*pdb_method)->name = "ldapsam_compat";
3204 ldap_state = (*pdb_method)->private_data;
3205 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
3207 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
3209 return NT_STATUS_OK;
3212 /**********************************************************************
3213 Initialise the normal mode for pdb_ldap
3214 *********************************************************************/
3216 static NTSTATUS pdb_init_ldapsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
3219 struct ldapsam_privates *ldap_state;
3220 uint32 alg_rid_base;
3221 pstring alg_rid_base_string;
3222 LDAPMessage *result = NULL;
3223 LDAPMessage *entry = NULL;
3224 DOM_SID ldap_domain_sid;
3225 DOM_SID secrets_domain_sid;
3226 pstring domain_sid_string;
3229 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common(pdb_context, pdb_method, location))) {
3233 (*pdb_method)->name = "ldapsam";
3235 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
3236 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
3237 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
3238 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
3240 ldap_state = (*pdb_method)->private_data;
3241 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
3243 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
3245 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state, &result,
3246 ldap_state->domain_name, True);
3248 if ( !NT_STATUS_IS_OK(nt_status) ) {
3249 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain info, nor add one to the domain\n"));
3250 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, will be unable to allocate new users/groups, \
3251 and will risk BDCs having inconsistant SIDs\n"));
3252 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
3253 return NT_STATUS_OK;
3256 /* Given that the above might fail, everything below this must be optional */
3258 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
3260 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info entry\n"));
3261 ldap_msgfree(result);
3262 return NT_STATUS_UNSUCCESSFUL;
3265 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
3267 return NT_STATUS_UNSUCCESSFUL;
3270 ldap_state->domain_dn = smb_xstrdup(dn);
3271 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
3272 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
3273 domain_sid_string)) {
3275 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
3276 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be read as a valid SID\n", domain_sid_string));
3277 return NT_STATUS_INVALID_PARAMETER;
3279 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name, &secrets_domain_sid);
3280 if (!found_sid || !sid_equal(&secrets_domain_sid, &ldap_domain_sid)) {
3281 fstring new_sid_str, old_sid_str;
3282 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain %s based on pdb_ldap results %s -> %s\n",
3283 ldap_state->domain_name,
3284 sid_to_string(old_sid_str, &secrets_domain_sid),
3285 sid_to_string(new_sid_str, &ldap_domain_sid)));
3287 /* reset secrets.tdb sid */
3288 secrets_store_domain_sid(ldap_state->domain_name, &ldap_domain_sid);
3289 DEBUG(1, ("New global sam SID: %s\n", sid_to_string(new_sid_str, get_global_sam_sid())));
3291 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
3294 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
3295 get_attr_key2string( dominfo_attr_list, LDAP_ATTR_ALGORITHMIC_RID_BASE ),
3296 alg_rid_base_string)) {
3297 alg_rid_base = (uint32)atol(alg_rid_base_string);
3298 if (alg_rid_base != algorithmic_rid_base()) {
3299 DEBUG(0, ("The value of 'algorithmic RID base' has changed since the LDAP\n"
3300 "database was initialised. Aborting. \n"));
3301 ldap_msgfree(result);
3302 return NT_STATUS_UNSUCCESSFUL;
3305 ldap_msgfree(result);
3307 return NT_STATUS_OK;
3310 NTSTATUS pdb_ldap_init(void)
3313 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
3316 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
3319 return NT_STATUS_OK;