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 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 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, char **attr)
207 char *escape_user = escape_ldap_string_alloc(user);
210 return LDAP_NO_MEMORY;
214 * in the filter expression, replace %u with the real name
215 * so in ldap filter, %u MUST exist :-)
217 pstr_sprintf(filter, "(&%s%s)", lp_ldap_filter(),
218 get_objclass_filter(ldap_state->schema_ver));
221 * have to use this here because $ is filtered out
226 all_string_sub(filter, "%u", escape_user, sizeof(pstring));
227 SAFE_FREE(escape_user);
229 return smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
232 /*******************************************************************
233 Run the search by rid.
234 ******************************************************************/
236 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
237 uint32 rid, LDAPMessage ** result,
243 pstr_sprintf(filter, "(&(rid=%i)%s)", rid,
244 get_objclass_filter(ldap_state->schema_ver));
246 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
251 /*******************************************************************
252 Run the search by SID.
253 ******************************************************************/
255 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
256 const DOM_SID *sid, LDAPMessage ** result,
263 pstr_sprintf(filter, "(&(%s=%s)%s)",
264 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
265 sid_to_string(sid_string, sid),
266 get_objclass_filter(ldap_state->schema_ver));
268 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
273 /*******************************************************************
274 Delete complete object or objectclass and attrs from
275 object found in search_result depending on lp_ldap_delete_dn
276 ******************************************************************/
278 static NTSTATUS ldapsam_delete_entry(struct ldapsam_privates *ldap_state,
280 const char *objectclass,
284 LDAPMessage *entry = NULL;
285 LDAPMod **mods = NULL;
287 BerElement *ptr = NULL;
289 rc = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
292 DEBUG(0, ("ldapsam_delete_entry: Entry must exist exactly once!\n"));
293 return NT_STATUS_UNSUCCESSFUL;
296 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
297 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
299 return NT_STATUS_UNSUCCESSFUL;
302 if (lp_ldap_delete_dn()) {
303 NTSTATUS ret = NT_STATUS_OK;
304 rc = smbldap_delete(ldap_state->smbldap_state, dn);
306 if (rc != LDAP_SUCCESS) {
307 DEBUG(0, ("ldapsam_delete_entry: Could not delete object %s\n", dn));
308 ret = NT_STATUS_UNSUCCESSFUL;
314 /* Ok, delete only the SAM attributes */
316 for (name = ldap_first_attribute(ldap_state->smbldap_state->ldap_struct, entry, &ptr);
318 name = ldap_next_attribute(ldap_state->smbldap_state->ldap_struct, entry, ptr)) {
321 /* We are only allowed to delete the attributes that
324 for (attrib = attrs; *attrib != NULL; attrib++) {
325 /* Don't delete LDAP_ATTR_MOD_TIMESTAMP attribute. */
326 if (strequal(*attrib, get_userattr_key2string(ldap_state->schema_ver,
327 LDAP_ATTR_MOD_TIMESTAMP))) {
330 if (strequal(*attrib, name)) {
331 DEBUG(10, ("ldapsam_delete_entry: deleting "
332 "attribute %s\n", name));
333 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
345 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
347 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
348 ldap_mods_free(mods, True);
350 if (rc != LDAP_SUCCESS) {
351 char *ld_error = NULL;
352 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
355 DEBUG(0, ("ldapsam_delete_entry: Could not delete attributes for %s, error: %s (%s)\n",
356 dn, ldap_err2string(rc), ld_error?ld_error:"unknown"));
359 return NT_STATUS_UNSUCCESSFUL;
366 /* New Interface is being implemented here */
368 #if 0 /* JERRY - not uesed anymore */
370 /**********************************************************************
371 Initialize SAM_ACCOUNT from an LDAP query (unix attributes only)
372 *********************************************************************/
373 static BOOL get_unix_attributes (struct ldapsam_privates *ldap_state,
374 SAM_ACCOUNT * sampass,
383 if ((ldap_values = ldap_get_values (ldap_state->smbldap_state->ldap_struct, entry, "objectClass")) == NULL) {
384 DEBUG (1, ("get_unix_attributes: no objectClass! \n"));
388 for (values=ldap_values;*values;values++) {
389 if (strequal(*values, LDAP_OBJ_POSIXACCOUNT )) {
394 if (!*values) { /*end of array, no posixAccount */
395 DEBUG(10, ("user does not have %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
396 ldap_value_free(ldap_values);
399 ldap_value_free(ldap_values);
401 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
402 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_UNIX_HOME), homedir) )
407 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
408 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_GIDNUMBER), temp) )
413 *gid = (gid_t)atol(temp);
415 pdb_set_unix_homedir(sampass, homedir, PDB_SET);
417 DEBUG(10, ("user has %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
424 static time_t ldapsam_get_entry_timestamp(
425 struct ldapsam_privates *ldap_state,
431 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
432 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
436 strptime(temp, "%Y%m%d%H%M%SZ", &tm);
441 /**********************************************************************
442 Initialize SAM_ACCOUNT from an LDAP query.
443 (Based on init_sam_from_buffer in pdb_tdb.c)
444 *********************************************************************/
446 static BOOL init_sam_from_ldap (struct ldapsam_privates *ldap_state,
447 SAM_ACCOUNT * sampass,
454 pass_can_change_time,
455 pass_must_change_time,
468 char munged_dial[2048];
470 uint8 smblmpwd[LM_HASH_LEN],
471 smbntpwd[NT_HASH_LEN];
472 uint16 acct_ctrl = 0,
474 uint16 bad_password_count = 0,
477 uint8 hours[MAX_HOURS_LEN];
479 LOGIN_CACHE *cache_entry = NULL;
484 * do a little initialization
488 nt_username[0] = '\0';
492 logon_script[0] = '\0';
493 profile_path[0] = '\0';
495 munged_dial[0] = '\0';
496 workstations[0] = '\0';
499 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
500 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
504 if (ldap_state->smbldap_state->ldap_struct == NULL) {
505 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->ldap_struct is NULL!\n"));
509 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry, "uid", username)) {
510 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for this user!\n"));
514 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
516 pstrcpy(nt_username, username);
518 pstrcpy(domain, ldap_state->domain_name);
520 pdb_set_username(sampass, username, PDB_SET);
522 pdb_set_domain(sampass, domain, PDB_DEFAULT);
523 pdb_set_nt_username(sampass, nt_username, PDB_SET);
525 /* deal with different attributes between the schema first */
527 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
528 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
529 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID), temp)) {
530 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
533 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
534 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_SID), temp)) {
535 pdb_set_group_sid_from_string(sampass, temp, PDB_SET);
537 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
540 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
541 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID), temp)) {
542 user_rid = (uint32)atol(temp);
543 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
546 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
547 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_RID), temp)) {
548 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
552 group_rid = (uint32)atol(temp);
554 /* for some reason, we often have 0 as a primary group RID.
555 Make sure that we treat this just as a 'default' value */
558 pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
560 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
564 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
565 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
566 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
567 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
572 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
573 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET), temp)) {
574 /* leave as default */
576 pass_last_set_time = (time_t) atol(temp);
577 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
580 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
581 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp)) {
582 /* leave as default */
584 logon_time = (time_t) atol(temp);
585 pdb_set_logon_time(sampass, logon_time, PDB_SET);
588 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
589 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp)) {
590 /* leave as default */
592 logoff_time = (time_t) atol(temp);
593 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
596 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
597 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp)) {
598 /* leave as default */
600 kickoff_time = (time_t) atol(temp);
601 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
604 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
605 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp)) {
606 /* leave as default */
608 pass_can_change_time = (time_t) atol(temp);
609 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
612 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
613 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp)) {
614 /* leave as default */
616 pass_must_change_time = (time_t) atol(temp);
617 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
620 /* recommend that 'gecos' and 'displayName' should refer to the same
621 * attribute OID. userFullName depreciated, only used by Samba
622 * primary rules of LDAP: don't make a new attribute when one is already defined
623 * that fits your needs; using cn then displayName rather than 'userFullName'
626 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
627 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME), fullname)) {
628 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
629 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_CN), fullname)) {
630 /* leave as default */
632 pdb_set_fullname(sampass, fullname, PDB_SET);
635 pdb_set_fullname(sampass, fullname, PDB_SET);
638 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
639 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE), dir_drive))
641 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
643 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
646 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
647 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH), homedir))
649 pdb_set_homedir( sampass,
650 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_home()),
653 pstrcpy( tmpstring, homedir );
654 standard_sub_basic( username, tmpstring, sizeof(tmpstring) );
655 pdb_set_homedir(sampass, tmpstring, PDB_SET);
658 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
659 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT), logon_script))
661 pdb_set_logon_script( sampass,
662 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_script()),
665 pstrcpy( tmpstring, logon_script );
666 standard_sub_basic( username, tmpstring, sizeof(tmpstring) );
667 pdb_set_logon_script(sampass, tmpstring, PDB_SET);
670 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
671 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH), profile_path))
673 pdb_set_profile_path( sampass,
674 talloc_sub_basic( sampass->mem_ctx, username, lp_logon_path()),
677 pstrcpy( tmpstring, profile_path );
678 standard_sub_basic( username, tmpstring, sizeof(tmpstring) );
679 pdb_set_profile_path(sampass, tmpstring, PDB_SET);
682 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
683 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC), acct_desc))
685 /* leave as default */
687 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
690 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
691 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS), workstations)) {
692 /* leave as default */;
694 pdb_set_workstations(sampass, workstations, PDB_SET);
697 if (!smbldap_get_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
698 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL), munged_dial, sizeof(munged_dial))) {
699 /* leave as default */;
701 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
704 /* FIXME: hours stuff should be cleaner */
708 memset(hours, 0xff, hours_len);
710 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
711 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW), temp)) {
712 /* leave as default */
714 pdb_gethexpwd(temp, smblmpwd);
715 memset((char *)temp, '\0', strlen(temp)+1);
716 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET))
718 ZERO_STRUCT(smblmpwd);
721 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
722 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW), temp)) {
723 /* leave as default */
725 pdb_gethexpwd(temp, smbntpwd);
726 memset((char *)temp, '\0', strlen(temp)+1);
727 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET))
729 ZERO_STRUCT(smbntpwd);
732 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
734 uint8 *pwhist = NULL;
737 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
738 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
740 if ((pwhist = SMB_MALLOC(pwHistLen * PW_HISTORY_ENTRY_LEN)) == NULL){
741 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
744 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
746 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
747 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY), temp)) {
748 /* leave as default - zeros */
750 BOOL hex_failed = False;
751 for (i = 0; i < pwHistLen; i++){
752 /* Get the 16 byte salt. */
753 if (!pdb_gethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
757 /* Get the 16 byte MD5 hash of salt+passwd. */
758 if (!pdb_gethexpwd(&temp[(i*64)+32],
759 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN])) {
765 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
767 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
770 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
777 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
778 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO), temp)) {
779 acct_ctrl |= ACB_NORMAL;
781 acct_ctrl = pdb_decode_acct_ctrl(temp);
784 acct_ctrl |= ACB_NORMAL;
786 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
789 pdb_set_hours_len(sampass, hours_len, PDB_SET);
790 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
792 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
793 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_COUNT), temp)) {
794 /* leave as default */
796 bad_password_count = (uint32) atol(temp);
797 pdb_set_bad_password_count(sampass, bad_password_count, PDB_SET);
800 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
801 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_TIME), temp)) {
802 /* leave as default */
804 bad_password_time = (time_t) atol(temp);
805 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
809 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
810 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_COUNT), temp)) {
811 /* leave as default */
813 logon_count = (uint32) atol(temp);
814 pdb_set_logon_count(sampass, logon_count, PDB_SET);
817 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
819 if(!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
820 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_HOURS), temp)) {
821 /* leave as default */
823 pdb_gethexhours(temp, hours);
824 memset((char *)temp, '\0', strlen(temp) +1);
825 pdb_set_hours(sampass, hours, PDB_SET);
829 /* check the timestamp of the cache vs ldap entry */
830 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
834 /* see if we have newer updates */
835 if (!(cache_entry = login_cache_read(sampass))) {
836 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
837 (unsigned int)pdb_get_bad_password_count(sampass),
838 (unsigned int)pdb_get_bad_password_time(sampass)));
842 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
843 (unsigned int)ldap_entry_time, (unsigned int)cache_entry->entry_timestamp,
844 (unsigned int)cache_entry->bad_password_time));
846 if (ldap_entry_time > cache_entry->entry_timestamp) {
847 /* cache is older than directory , so
848 we need to delete the entry but allow the
849 fields to be written out */
850 login_cache_delentry(sampass);
853 pdb_set_acct_ctrl(sampass,
854 pdb_get_acct_ctrl(sampass) |
855 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
857 pdb_set_bad_password_count(sampass,
858 cache_entry->bad_password_count,
860 pdb_set_bad_password_time(sampass,
861 cache_entry->bad_password_time,
865 SAFE_FREE(cache_entry);
869 /**********************************************************************
870 Initialize the ldap db from a SAM_ACCOUNT. Called on update.
871 (Based on init_buffer_from_sam in pdb_tdb.c)
872 *********************************************************************/
874 static BOOL init_ldap_from_sam (struct ldapsam_privates *ldap_state,
875 LDAPMessage *existing,
876 LDAPMod *** mods, SAM_ACCOUNT * sampass,
877 BOOL (*need_update)(const SAM_ACCOUNT *,
883 if (mods == NULL || sampass == NULL) {
884 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
891 * took out adding "objectclass: sambaAccount"
892 * do this on a per-mod basis
894 if (need_update(sampass, PDB_USERNAME))
895 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
896 "uid", pdb_get_username(sampass));
898 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
900 /* only update the RID if we actually need to */
901 if (need_update(sampass, PDB_USERSID)) {
903 fstring dom_sid_string;
904 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
906 switch ( ldap_state->schema_ver ) {
907 case SCHEMAVER_SAMBAACCOUNT:
908 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
909 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
910 sid_to_string(sid_string, user_sid),
911 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
914 slprintf(temp, sizeof(temp) - 1, "%i", rid);
915 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
916 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
920 case SCHEMAVER_SAMBASAMACCOUNT:
921 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
922 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
923 sid_to_string(sid_string, user_sid));
927 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
932 /* we don't need to store the primary group RID - so leaving it
933 'free' to hang off the unix primary group makes life easier */
935 if (need_update(sampass, PDB_GROUPSID)) {
937 fstring dom_sid_string;
938 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
940 switch ( ldap_state->schema_ver ) {
941 case SCHEMAVER_SAMBAACCOUNT:
942 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
943 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
944 sid_to_string(sid_string, group_sid),
945 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
949 slprintf(temp, sizeof(temp) - 1, "%i", rid);
950 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
951 get_userattr_key2string(ldap_state->schema_ver,
952 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
955 case SCHEMAVER_SAMBASAMACCOUNT:
956 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
957 get_userattr_key2string(ldap_state->schema_ver,
958 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_string(sid_string, group_sid));
962 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
968 /* displayName, cn, and gecos should all be the same
969 * most easily accomplished by giving them the same OID
970 * gecos isn't set here b/c it should be handled by the
972 * We change displayName only and fall back to cn if
976 if (need_update(sampass, PDB_FULLNAME))
977 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
978 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
979 pdb_get_fullname(sampass));
981 if (need_update(sampass, PDB_ACCTDESC))
982 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
983 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
984 pdb_get_acct_desc(sampass));
986 if (need_update(sampass, PDB_WORKSTATIONS))
987 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
988 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
989 pdb_get_workstations(sampass));
991 if (need_update(sampass, PDB_MUNGEDDIAL))
992 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
993 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
994 pdb_get_munged_dial(sampass));
996 if (need_update(sampass, PDB_SMBHOME))
997 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
998 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
999 pdb_get_homedir(sampass));
1001 if (need_update(sampass, PDB_DRIVE))
1002 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1003 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1004 pdb_get_dir_drive(sampass));
1006 if (need_update(sampass, PDB_LOGONSCRIPT))
1007 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1008 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1009 pdb_get_logon_script(sampass));
1011 if (need_update(sampass, PDB_PROFILE))
1012 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1013 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1014 pdb_get_profile_path(sampass));
1016 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
1017 if (need_update(sampass, PDB_LOGONTIME))
1018 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1019 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1021 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
1022 if (need_update(sampass, PDB_LOGOFFTIME))
1023 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1024 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1026 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
1027 if (need_update(sampass, PDB_KICKOFFTIME))
1028 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1029 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1031 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
1032 if (need_update(sampass, PDB_CANCHANGETIME))
1033 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1034 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1036 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
1037 if (need_update(sampass, PDB_MUSTCHANGETIME))
1038 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1039 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1042 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1043 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1045 if (need_update(sampass, PDB_LMPASSWD)) {
1046 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1048 pdb_sethexpwd(temp, lm_pw,
1049 pdb_get_acct_ctrl(sampass));
1050 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1051 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1054 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1055 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1059 if (need_update(sampass, PDB_NTPASSWD)) {
1060 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1062 pdb_sethexpwd(temp, nt_pw,
1063 pdb_get_acct_ctrl(sampass));
1064 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1065 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1068 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1069 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1074 if (need_update(sampass, PDB_PWHISTORY)) {
1076 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1077 if (pwHistLen == 0) {
1078 /* Remove any password history from the LDAP store. */
1079 memset(temp, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1082 int i, currHistLen = 0;
1083 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1084 if (pwhist != NULL) {
1085 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1086 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
1087 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1088 /* Store the salt. */
1089 pdb_sethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1090 /* Followed by the md5 hash of salt + md4 hash */
1091 pdb_sethexpwd(&temp[(i*64)+32],
1092 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1093 DEBUG(100, ("temp=%s\n", temp));
1097 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1098 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1102 if (need_update(sampass, PDB_PASSLASTSET)) {
1103 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
1104 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1105 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1110 if (need_update(sampass, PDB_HOURS)) {
1111 const char *hours = pdb_get_hours(sampass);
1113 pdb_sethexhours(temp, hours);
1114 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1117 get_userattr_key2string(ldap_state->schema_ver,
1118 LDAP_ATTR_LOGON_HOURS),
1123 if (need_update(sampass, PDB_ACCTCTRL))
1124 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1125 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1126 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1128 /* password lockout cache:
1129 - If we are now autolocking or clearing, we write to ldap
1130 - If we are clearing, we delete the cache entry
1131 - If the count is > 0, we update the cache
1133 This even means when autolocking, we cache, just in case the
1134 update doesn't work, and we have to cache the autolock flag */
1136 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1137 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1138 uint16 badcount = pdb_get_bad_password_count(sampass);
1139 time_t badtime = pdb_get_bad_password_time(sampass);
1141 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1143 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1144 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1146 if ((badcount >= pol) || (badcount == 0)) {
1147 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1148 (unsigned int)badcount, (unsigned int)badtime));
1149 slprintf (temp, sizeof (temp) - 1, "%li", (long)badcount);
1151 ldap_state->smbldap_state->ldap_struct,
1153 get_userattr_key2string(
1154 ldap_state->schema_ver,
1155 LDAP_ATTR_BAD_PASSWORD_COUNT),
1158 slprintf (temp, sizeof (temp) - 1, "%li", badtime);
1160 ldap_state->smbldap_state->ldap_struct,
1162 get_userattr_key2string(
1163 ldap_state->schema_ver,
1164 LDAP_ATTR_BAD_PASSWORD_TIME),
1167 if (badcount == 0) {
1168 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1169 login_cache_delentry(sampass);
1171 LOGIN_CACHE cache_entry;
1173 cache_entry.entry_timestamp = time(NULL);
1174 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1175 cache_entry.bad_password_count = badcount;
1176 cache_entry.bad_password_time = badtime;
1178 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1179 login_cache_write(sampass, cache_entry);
1186 /**********************************************************************
1187 Connect to LDAP server for password enumeration.
1188 *********************************************************************/
1190 static NTSTATUS ldapsam_setsampwent(struct pdb_methods *my_methods, BOOL update, uint16 acb_mask)
1192 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1194 pstring filter, suffix;
1196 BOOL machine_mask = False, user_mask = False;
1198 pstr_sprintf( filter, "(&%s%s)", lp_ldap_filter(),
1199 get_objclass_filter(ldap_state->schema_ver));
1200 all_string_sub(filter, "%u", "*", sizeof(pstring));
1202 machine_mask = ((acb_mask != 0) && (acb_mask & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)));
1203 user_mask = ((acb_mask != 0) && (acb_mask & ACB_NORMAL));
1206 pstrcpy(suffix, lp_ldap_machine_suffix());
1207 } else if (user_mask) {
1208 pstrcpy(suffix, lp_ldap_user_suffix());
1210 pstrcpy(suffix, lp_ldap_suffix());
1213 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1216 attr_list = get_userattr_list(ldap_state->schema_ver);
1217 rc = smbldap_search(ldap_state->smbldap_state, suffix, LDAP_SCOPE_SUBTREE, filter,
1218 attr_list, 0, &ldap_state->result);
1219 free_attr_list( attr_list );
1221 if (rc != LDAP_SUCCESS) {
1222 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc)));
1223 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix, filter));
1224 ldap_msgfree(ldap_state->result);
1225 ldap_state->result = NULL;
1226 return NT_STATUS_UNSUCCESSFUL;
1229 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1230 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
1231 ldap_state->result), suffix));
1233 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
1234 ldap_state->result);
1235 ldap_state->index = 0;
1237 return NT_STATUS_OK;
1240 /**********************************************************************
1241 End enumeration of the LDAP password list.
1242 *********************************************************************/
1244 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1246 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1247 if (ldap_state->result) {
1248 ldap_msgfree(ldap_state->result);
1249 ldap_state->result = NULL;
1253 /**********************************************************************
1254 Get the next entry in the LDAP password database.
1255 *********************************************************************/
1257 static NTSTATUS ldapsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
1259 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1260 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1264 if (!ldap_state->entry)
1267 ldap_state->index++;
1268 bret = init_sam_from_ldap(ldap_state, user, ldap_state->entry);
1270 ldap_state->entry = ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
1274 return NT_STATUS_OK;
1277 static void append_attr(char ***attr_list, const char *new_attr)
1281 if (new_attr == NULL) {
1285 for (i=0; (*attr_list)[i] != NULL; i++) {
1289 (*attr_list) = SMB_REALLOC_ARRAY((*attr_list), char *, i+2);
1290 SMB_ASSERT((*attr_list) != NULL);
1291 (*attr_list)[i] = SMB_STRDUP(new_attr);
1292 (*attr_list)[i+1] = NULL;
1295 /**********************************************************************
1296 Get SAM_ACCOUNT entry from LDAP by username.
1297 *********************************************************************/
1299 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
1301 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1302 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1303 LDAPMessage *result = NULL;
1304 LDAPMessage *entry = NULL;
1309 attr_list = get_userattr_list( ldap_state->schema_ver );
1310 append_attr(&attr_list, get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP));
1311 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result, attr_list);
1312 free_attr_list( attr_list );
1314 if ( rc != LDAP_SUCCESS )
1315 return NT_STATUS_NO_SUCH_USER;
1317 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1320 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1321 ldap_msgfree(result);
1322 return NT_STATUS_NO_SUCH_USER;
1323 } else if (count > 1) {
1324 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1325 ldap_msgfree(result);
1326 return NT_STATUS_NO_SUCH_USER;
1329 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1331 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1332 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1333 ldap_msgfree(result);
1334 return NT_STATUS_NO_SUCH_USER;
1336 pdb_set_backend_private_data(user, result,
1337 private_data_free_fn,
1338 my_methods, PDB_CHANGED);
1341 ldap_msgfree(result);
1346 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1347 const DOM_SID *sid, LDAPMessage **result)
1353 switch ( ldap_state->schema_ver ) {
1354 case SCHEMAVER_SAMBASAMACCOUNT:
1355 attr_list = get_userattr_list(ldap_state->schema_ver);
1356 append_attr(&attr_list, get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP));
1357 rc = ldapsam_search_suffix_by_sid(ldap_state, sid, result, attr_list);
1358 free_attr_list( attr_list );
1360 if ( rc != LDAP_SUCCESS )
1364 case SCHEMAVER_SAMBAACCOUNT:
1365 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1369 attr_list = get_userattr_list(ldap_state->schema_ver);
1370 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1371 free_attr_list( attr_list );
1373 if ( rc != LDAP_SUCCESS )
1380 /**********************************************************************
1381 Get SAM_ACCOUNT entry from LDAP by SID.
1382 *********************************************************************/
1384 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1386 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1387 LDAPMessage *result = NULL;
1388 LDAPMessage *entry = NULL;
1393 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1395 if (rc != LDAP_SUCCESS)
1396 return NT_STATUS_NO_SUCH_USER;
1398 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1401 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string, sid),
1403 ldap_msgfree(result);
1404 return NT_STATUS_NO_SUCH_USER;
1405 } else if (count > 1) {
1406 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string, sid),
1408 ldap_msgfree(result);
1409 return NT_STATUS_NO_SUCH_USER;
1412 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1414 ldap_msgfree(result);
1415 return NT_STATUS_NO_SUCH_USER;
1418 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1419 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1420 ldap_msgfree(result);
1421 return NT_STATUS_NO_SUCH_USER;
1424 pdb_set_backend_private_data(user, result,
1425 private_data_free_fn,
1426 my_methods, PDB_CHANGED);
1427 return NT_STATUS_OK;
1430 static BOOL ldapsam_can_pwchange_exop(struct smbldap_state *ldap_state)
1432 return smbldap_has_extension(ldap_state, LDAP_EXOP_MODIFY_PASSWD);
1435 /********************************************************************
1436 Do the actual modification - also change a plaintext passord if
1438 **********************************************************************/
1440 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1441 SAM_ACCOUNT *newpwd, char *dn,
1442 LDAPMod **mods, int ldap_op,
1443 BOOL (*need_update)(const SAM_ACCOUNT *, enum pdb_elements))
1445 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1448 if (!my_methods || !newpwd || !dn) {
1449 return NT_STATUS_INVALID_PARAMETER;
1453 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1454 /* may be password change below however */
1458 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1461 rc = smbldap_add(ldap_state->smbldap_state,
1464 case LDAP_MOD_REPLACE:
1465 rc = smbldap_modify(ldap_state->smbldap_state,
1469 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1471 return NT_STATUS_INVALID_PARAMETER;
1474 if (rc!=LDAP_SUCCESS) {
1475 char *ld_error = NULL;
1476 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1478 DEBUG(1, ("ldapsam_modify_entry: Failed to %s user dn= %s with: %s\n\t%s\n",
1479 ldap_op == LDAP_MOD_ADD ? "add" : "modify",
1480 dn, ldap_err2string(rc),
1481 ld_error?ld_error:"unknown"));
1482 SAFE_FREE(ld_error);
1483 return NT_STATUS_UNSUCCESSFUL;
1487 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1488 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1489 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1490 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1494 struct berval *retdata;
1495 char *utf8_password;
1498 if (!ldapsam_can_pwchange_exop(ldap_state->smbldap_state)) {
1499 DEBUG(2, ("ldap password change requested, but LDAP "
1500 "server does not support it -- ignoring\n"));
1501 return NT_STATUS_OK;
1504 if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
1505 return NT_STATUS_NO_MEMORY;
1508 if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
1509 return NT_STATUS_NO_MEMORY;
1512 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1513 DEBUG(0,("ber_alloc_t returns NULL\n"));
1514 SAFE_FREE(utf8_password);
1515 return NT_STATUS_UNSUCCESSFUL;
1518 ber_printf (ber, "{");
1519 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
1520 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
1521 ber_printf (ber, "N}");
1523 if ((rc = ber_flatten (ber, &bv))<0) {
1524 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1527 SAFE_FREE(utf8_password);
1528 return NT_STATUS_UNSUCCESSFUL;
1532 SAFE_FREE(utf8_password);
1535 if ((rc = smbldap_extended_operation(ldap_state->smbldap_state,
1536 LDAP_EXOP_MODIFY_PASSWD,
1537 bv, NULL, NULL, &retoid,
1538 &retdata)) != LDAP_SUCCESS) {
1539 char *ld_error = NULL;
1541 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1542 DEBUG(3, ("Could not set userPassword "
1543 "attribute due to an objectClass "
1544 "violation -- ignoring\n"));
1546 return NT_STATUS_OK;
1549 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1551 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1552 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1553 SAFE_FREE(ld_error);
1555 return NT_STATUS_UNSUCCESSFUL;
1557 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1558 #ifdef DEBUG_PASSWORD
1559 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1561 ber_bvfree(retdata);
1562 ber_memfree(retoid);
1566 return NT_STATUS_OK;
1569 /**********************************************************************
1570 Delete entry from LDAP for username.
1571 *********************************************************************/
1573 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * sam_acct)
1575 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1578 LDAPMessage *result = NULL;
1584 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1585 return NT_STATUS_INVALID_PARAMETER;
1588 sname = pdb_get_username(sam_acct);
1590 DEBUG (3, ("ldapsam_delete_sam_account: Deleting user %s from LDAP.\n", sname));
1592 attr_list= get_userattr_delete_list( ldap_state->schema_ver );
1593 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result, attr_list);
1595 if (rc != LDAP_SUCCESS) {
1596 free_attr_list( attr_list );
1597 return NT_STATUS_NO_SUCH_USER;
1600 switch ( ldap_state->schema_ver ) {
1601 case SCHEMAVER_SAMBASAMACCOUNT:
1602 fstrcpy( objclass, LDAP_OBJ_SAMBASAMACCOUNT );
1605 case SCHEMAVER_SAMBAACCOUNT:
1606 fstrcpy( objclass, LDAP_OBJ_SAMBAACCOUNT );
1609 fstrcpy( objclass, "UNKNOWN" );
1610 DEBUG(0,("ldapsam_delete_sam_account: Unknown schema version specified!\n"));
1614 ret = ldapsam_delete_entry(ldap_state, result, objclass, attr_list );
1615 ldap_msgfree(result);
1616 free_attr_list( attr_list );
1621 /**********************************************************************
1622 Helper function to determine for update_sam_account whether
1623 we need LDAP modification.
1624 *********************************************************************/
1626 static BOOL element_is_changed(const SAM_ACCOUNT *sampass,
1627 enum pdb_elements element)
1629 return IS_SAM_CHANGED(sampass, element);
1632 /**********************************************************************
1634 *********************************************************************/
1636 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * newpwd)
1638 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1639 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1642 LDAPMessage *result = NULL;
1643 LDAPMessage *entry = NULL;
1644 LDAPMod **mods = NULL;
1647 result = pdb_get_backend_private_data(newpwd, my_methods);
1649 attr_list = get_userattr_list(ldap_state->schema_ver);
1650 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1651 free_attr_list( attr_list );
1652 if (rc != LDAP_SUCCESS) {
1653 return NT_STATUS_UNSUCCESSFUL;
1655 pdb_set_backend_private_data(newpwd, result, private_data_free_fn, my_methods, PDB_CHANGED);
1658 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1659 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1660 return NT_STATUS_UNSUCCESSFUL;
1663 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1664 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
1666 return NT_STATUS_UNSUCCESSFUL;
1669 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1671 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1672 element_is_changed)) {
1673 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1676 ldap_mods_free(mods,True);
1677 return NT_STATUS_UNSUCCESSFUL;
1681 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1682 pdb_get_username(newpwd)));
1684 return NT_STATUS_OK;
1687 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1688 ldap_mods_free(mods,True);
1691 if (!NT_STATUS_IS_OK(ret)) {
1692 char *ld_error = NULL;
1693 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1695 DEBUG(0,("ldapsam_update_sam_account: failed to modify user with uid = %s, error: %s (%s)\n",
1696 pdb_get_username(newpwd), ld_error?ld_error:"(unknwon)", ldap_err2string(rc)));
1697 SAFE_FREE(ld_error);
1701 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1702 pdb_get_username(newpwd)));
1703 return NT_STATUS_OK;
1706 /**********************************************************************
1707 Helper function to determine for update_sam_account whether
1708 we need LDAP modification.
1709 *********************************************************************/
1711 static BOOL element_is_set_or_changed(const SAM_ACCOUNT *sampass,
1712 enum pdb_elements element)
1714 return (IS_SAM_SET(sampass, element) ||
1715 IS_SAM_CHANGED(sampass, element));
1718 /**********************************************************************
1719 Add SAM_ACCOUNT to LDAP.
1720 *********************************************************************/
1722 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT * newpwd)
1724 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1725 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1727 LDAPMessage *result = NULL;
1728 LDAPMessage *entry = NULL;
1730 LDAPMod **mods = NULL;
1731 int ldap_op = LDAP_MOD_REPLACE;
1735 const char *username = pdb_get_username(newpwd);
1736 const DOM_SID *sid = pdb_get_user_sid(newpwd);
1740 if (!username || !*username) {
1741 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1742 return NT_STATUS_INVALID_PARAMETER;
1745 /* free this list after the second search or in case we exit on failure */
1746 attr_list = get_userattr_list(ldap_state->schema_ver);
1748 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
1750 if (rc != LDAP_SUCCESS) {
1751 free_attr_list( attr_list );
1752 return NT_STATUS_UNSUCCESSFUL;
1755 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1756 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1758 ldap_msgfree(result);
1759 free_attr_list( attr_list );
1760 return NT_STATUS_UNSUCCESSFUL;
1762 ldap_msgfree(result);
1765 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
1766 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1768 if (rc == LDAP_SUCCESS) {
1769 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1770 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1771 sid_to_string(sid_string, sid)));
1772 free_attr_list( attr_list );
1773 ldap_msgfree(result);
1774 return NT_STATUS_UNSUCCESSFUL;
1776 ldap_msgfree(result);
1780 /* does the entry already exist but without a samba attributes?
1781 we need to return the samba attributes here */
1783 escape_user = escape_ldap_string_alloc( username );
1784 pstrcpy( filter, lp_ldap_filter() );
1785 all_string_sub( filter, "%u", escape_user, sizeof(filter) );
1786 SAFE_FREE( escape_user );
1788 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1789 filter, attr_list, &result);
1790 if ( rc != LDAP_SUCCESS ) {
1791 free_attr_list( attr_list );
1792 return NT_STATUS_UNSUCCESSFUL;
1795 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1797 if (num_result > 1) {
1798 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1799 free_attr_list( attr_list );
1800 ldap_msgfree(result);
1801 return NT_STATUS_UNSUCCESSFUL;
1804 /* Check if we need to update an existing entry */
1805 if (num_result == 1) {
1808 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1809 ldap_op = LDAP_MOD_REPLACE;
1810 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
1811 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
1813 free_attr_list( attr_list );
1814 ldap_msgfree(result);
1815 return NT_STATUS_UNSUCCESSFUL;
1817 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1820 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
1822 /* There might be a SID for this account already - say an idmap entry */
1824 pstr_sprintf(filter, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
1825 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1826 sid_to_string(sid_string, sid),
1827 LDAP_OBJ_IDMAP_ENTRY,
1828 LDAP_OBJ_SID_ENTRY);
1830 /* free old result before doing a new search */
1831 if (result != NULL) {
1832 ldap_msgfree(result);
1835 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1836 filter, attr_list, &result);
1838 if ( rc != LDAP_SUCCESS ) {
1839 free_attr_list( attr_list );
1840 return NT_STATUS_UNSUCCESSFUL;
1843 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1845 if (num_result > 1) {
1846 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1847 free_attr_list( attr_list );
1848 ldap_msgfree(result);
1849 return NT_STATUS_UNSUCCESSFUL;
1852 /* Check if we need to update an existing entry */
1853 if (num_result == 1) {
1856 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1857 ldap_op = LDAP_MOD_REPLACE;
1858 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
1859 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
1861 free_attr_list( attr_list );
1862 ldap_msgfree(result);
1863 return NT_STATUS_UNSUCCESSFUL;
1865 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1870 free_attr_list( attr_list );
1872 if (num_result == 0) {
1873 /* Check if we need to add an entry */
1874 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
1875 ldap_op = LDAP_MOD_ADD;
1876 if (username[strlen(username)-1] == '$') {
1877 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_machine_suffix ());
1879 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_user_suffix ());
1883 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1884 element_is_set_or_changed)) {
1885 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
1886 ldap_msgfree(result);
1888 ldap_mods_free(mods,True);
1889 return NT_STATUS_UNSUCCESSFUL;
1892 ldap_msgfree(result);
1895 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
1896 return NT_STATUS_UNSUCCESSFUL;
1898 switch ( ldap_state->schema_ver ) {
1899 case SCHEMAVER_SAMBAACCOUNT:
1900 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
1902 case SCHEMAVER_SAMBASAMACCOUNT:
1903 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
1906 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
1910 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
1911 if (!NT_STATUS_IS_OK(ret)) {
1912 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
1913 pdb_get_username(newpwd),dn));
1914 ldap_mods_free(mods, True);
1918 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
1919 ldap_mods_free(mods, True);
1921 return NT_STATUS_OK;
1924 /**********************************************************************
1925 *********************************************************************/
1927 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
1929 LDAPMessage ** result)
1931 int scope = LDAP_SCOPE_SUBTREE;
1935 attr_list = get_attr_list(groupmap_attr_list);
1936 rc = smbldap_search(ldap_state->smbldap_state,
1937 lp_ldap_group_suffix (), scope,
1938 filter, attr_list, 0, result);
1939 free_attr_list( attr_list );
1941 if (rc != LDAP_SUCCESS) {
1942 char *ld_error = NULL;
1943 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1945 DEBUG(0, ("ldapsam_search_one_group: "
1946 "Problem during the LDAP search: LDAP error: %s (%s)\n",
1947 ld_error?ld_error:"(unknown)", ldap_err2string(rc)));
1948 DEBUGADD(3, ("ldapsam_search_one_group: Query was: %s, %s\n",
1949 lp_ldap_group_suffix(), filter));
1950 SAFE_FREE(ld_error);
1956 /**********************************************************************
1957 *********************************************************************/
1959 static BOOL init_group_from_ldap(struct ldapsam_privates *ldap_state,
1960 GROUP_MAP *map, LDAPMessage *entry)
1964 if (ldap_state == NULL || map == NULL || entry == NULL ||
1965 ldap_state->smbldap_state->ldap_struct == NULL) {
1966 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
1970 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
1971 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER), temp)) {
1972 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1973 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
1976 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
1978 map->gid = (gid_t)atol(temp);
1980 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
1981 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID), temp)) {
1982 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1983 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
1987 if (!string_to_sid(&map->sid, temp)) {
1988 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
1992 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
1993 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), temp)) {
1994 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1995 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
1998 map->sid_name_use = (enum SID_NAME_USE)atol(temp);
2000 if ((map->sid_name_use < SID_NAME_USER) ||
2001 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2002 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2006 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2007 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), temp)) {
2009 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2010 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_CN), temp))
2012 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2013 for gidNumber(%lu)\n",(unsigned long)map->gid));
2017 fstrcpy(map->nt_name, temp);
2019 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2020 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), temp)) {
2023 fstrcpy(map->comment, temp);
2028 /**********************************************************************
2029 *********************************************************************/
2031 static BOOL init_ldap_from_group(LDAP *ldap_struct,
2032 LDAPMessage *existing,
2034 const GROUP_MAP *map)
2038 if (mods == NULL || map == NULL) {
2039 DEBUG(0, ("init_ldap_from_group: NULL parameters found!\n"));
2045 sid_to_string(tmp, &map->sid);
2047 smbldap_make_mod(ldap_struct, existing, mods,
2048 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID), tmp);
2049 pstr_sprintf(tmp, "%i", map->sid_name_use);
2050 smbldap_make_mod(ldap_struct, existing, mods,
2051 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), tmp);
2053 smbldap_make_mod(ldap_struct, existing, mods,
2054 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), map->nt_name);
2055 smbldap_make_mod(ldap_struct, existing, mods,
2056 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), map->comment);
2061 /**********************************************************************
2062 *********************************************************************/
2064 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2068 struct ldapsam_privates *ldap_state =
2069 (struct ldapsam_privates *)methods->private_data;
2070 LDAPMessage *result = NULL;
2071 LDAPMessage *entry = NULL;
2074 if (ldapsam_search_one_group(ldap_state, filter, &result)
2076 return NT_STATUS_NO_SUCH_GROUP;
2079 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2082 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2083 ldap_msgfree(result);
2084 return NT_STATUS_NO_SUCH_GROUP;
2088 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: count=%d\n",
2090 ldap_msgfree(result);
2091 return NT_STATUS_NO_SUCH_GROUP;
2094 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2097 ldap_msgfree(result);
2098 return NT_STATUS_UNSUCCESSFUL;
2101 if (!init_group_from_ldap(ldap_state, map, entry)) {
2102 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for group filter %s\n",
2104 ldap_msgfree(result);
2105 return NT_STATUS_NO_SUCH_GROUP;
2108 ldap_msgfree(result);
2109 return NT_STATUS_OK;
2112 /**********************************************************************
2113 *********************************************************************/
2115 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2120 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2122 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2123 sid_string_static(&sid));
2125 return ldapsam_getgroup(methods, filter, map);
2128 /**********************************************************************
2129 *********************************************************************/
2131 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2136 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%lu))",
2138 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2139 (unsigned long)gid);
2141 return ldapsam_getgroup(methods, filter, map);
2144 /**********************************************************************
2145 *********************************************************************/
2147 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2151 char *escape_name = escape_ldap_string_alloc(name);
2154 return NT_STATUS_NO_MEMORY;
2157 pstr_sprintf(filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2159 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2160 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN), escape_name);
2162 SAFE_FREE(escape_name);
2164 return ldapsam_getgroup(methods, filter, map);
2167 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2168 const char *username,
2170 DOM_SID **sids, gid_t **gids,
2173 struct ldapsam_privates *ldap_state =
2174 (struct ldapsam_privates *)methods->private_data;
2175 struct smbldap_state *conn = ldap_state->smbldap_state;
2177 char *attrs[] = { "gidNumber", "sambaSID", NULL };
2180 LDAPMessage *msg = NULL;
2182 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2183 int num_sids, num_gids;
2184 extern DOM_SID global_sid_NULL;
2186 if (!lp_parm_bool(-1, "ldapsam", "trusted", False))
2187 return pdb_default_enum_group_memberships(methods, username,
2194 escape_name = escape_ldap_string_alloc(username);
2196 if (escape_name == NULL)
2197 return NT_STATUS_NO_MEMORY;
2199 pstr_sprintf(filter, "(&(objectClass=posixGroup)"
2200 "(|(memberUid=%s)(gidNumber=%d)))",
2201 username, primary_gid);
2203 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2204 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &msg);
2206 if (rc != LDAP_SUCCESS)
2215 /* We need to add the primary group as the first gid/sid */
2217 add_gid_to_array_unique(primary_gid, gids, &num_gids);
2219 /* This sid will be replaced later */
2221 add_sid_to_array_unique(&global_sid_NULL, sids, &num_sids);
2223 for (entry = ldap_first_entry(conn->ldap_struct, msg);
2225 entry = ldap_next_entry(conn->ldap_struct, entry))
2232 if (!smbldap_get_single_attribute(conn->ldap_struct,
2234 str, sizeof(str)-1))
2237 if (!string_to_sid(&sid, str))
2240 if (!smbldap_get_single_attribute(conn->ldap_struct,
2242 str, sizeof(str)-1))
2245 gid = strtoul(str, &end, 10);
2247 if (PTR_DIFF(end, str) != strlen(str))
2250 if (gid == primary_gid) {
2251 sid_copy(&(*sids)[0], &sid);
2253 add_gid_to_array_unique(gid, gids, &num_gids);
2254 add_sid_to_array_unique(&sid, sids, &num_sids);
2258 if (sid_compare(&global_sid_NULL, &(*sids)[0]) == 0) {
2259 DEBUG(3, ("primary group of [%s] not found\n", username));
2263 *num_groups = num_sids;
2265 result = NT_STATUS_OK;
2269 SAFE_FREE(escape_name);
2276 /**********************************************************************
2277 *********************************************************************/
2279 static int ldapsam_search_one_group_by_gid(struct ldapsam_privates *ldap_state,
2281 LDAPMessage **result)
2285 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%lu))",
2286 LDAP_OBJ_POSIXGROUP, LDAP_OBJ_IDMAP_ENTRY,
2287 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2288 (unsigned long)gid);
2290 return ldapsam_search_one_group(ldap_state, filter, result);
2293 /**********************************************************************
2294 *********************************************************************/
2296 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
2299 struct ldapsam_privates *ldap_state =
2300 (struct ldapsam_privates *)methods->private_data;
2301 LDAPMessage *result = NULL;
2302 LDAPMod **mods = NULL;
2313 if (NT_STATUS_IS_OK(ldapsam_getgrgid(methods, &dummy,
2315 DEBUG(0, ("ldapsam_add_group_mapping_entry: Group %ld already exists in LDAP\n", (unsigned long)map->gid));
2316 return NT_STATUS_UNSUCCESSFUL;
2319 rc = ldapsam_search_one_group_by_gid(ldap_state, map->gid, &result);
2320 if (rc != LDAP_SUCCESS) {
2321 ldap_msgfree(result);
2322 return NT_STATUS_UNSUCCESSFUL;
2325 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2328 /* There's no posixGroup account, let's try to find an
2329 * appropriate idmap entry for aliases */
2335 ldap_msgfree(result);
2337 pstrcpy( suffix, lp_ldap_idmap_suffix() );
2338 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%u))",
2339 LDAP_OBJ_IDMAP_ENTRY, LDAP_ATTRIBUTE_GIDNUMBER,
2342 attr_list = get_attr_list( sidmap_attr_list );
2343 rc = smbldap_search(ldap_state->smbldap_state, suffix,
2344 LDAP_SCOPE_SUBTREE, filter, attr_list,
2347 free_attr_list(attr_list);
2349 if (rc != LDAP_SUCCESS) {
2350 DEBUG(3,("Failure looking up entry (%s)\n",
2351 ldap_err2string(rc) ));
2352 ldap_msgfree(result);
2353 return NT_STATUS_UNSUCCESSFUL;
2357 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2359 ldap_msgfree(result);
2360 return NT_STATUS_UNSUCCESSFUL;
2364 DEBUG(2, ("ldapsam_add_group_mapping_entry: Group %lu must exist exactly once in LDAP\n",
2365 (unsigned long)map->gid));
2366 ldap_msgfree(result);
2367 return NT_STATUS_UNSUCCESSFUL;
2370 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2371 tmp = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2373 ldap_msgfree(result);
2374 return NT_STATUS_UNSUCCESSFUL;
2379 if (!init_ldap_from_group(ldap_state->smbldap_state->ldap_struct,
2380 result, &mods, map)) {
2381 DEBUG(0, ("ldapsam_add_group_mapping_entry: init_ldap_from_group failed!\n"));
2382 ldap_mods_free(mods, True);
2383 ldap_msgfree(result);
2384 return NT_STATUS_UNSUCCESSFUL;
2387 ldap_msgfree(result);
2390 DEBUG(0, ("ldapsam_add_group_mapping_entry: mods is empty\n"));
2391 return NT_STATUS_UNSUCCESSFUL;
2394 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP );
2396 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2397 ldap_mods_free(mods, True);
2399 if (rc != LDAP_SUCCESS) {
2400 char *ld_error = NULL;
2401 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
2403 DEBUG(0, ("ldapsam_add_group_mapping_entry: failed to add group %lu error: %s (%s)\n", (unsigned long)map->gid,
2404 ld_error ? ld_error : "(unknown)", ldap_err2string(rc)));
2405 SAFE_FREE(ld_error);
2406 return NT_STATUS_UNSUCCESSFUL;
2409 DEBUG(2, ("ldapsam_add_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map->gid));
2410 return NT_STATUS_OK;
2413 /**********************************************************************
2414 *********************************************************************/
2416 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
2419 struct ldapsam_privates *ldap_state =
2420 (struct ldapsam_privates *)methods->private_data;
2423 LDAPMessage *result = NULL;
2424 LDAPMessage *entry = NULL;
2425 LDAPMod **mods = NULL;
2427 rc = ldapsam_search_one_group_by_gid(ldap_state, map->gid, &result);
2429 if (rc != LDAP_SUCCESS) {
2430 return NT_STATUS_UNSUCCESSFUL;
2433 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
2434 DEBUG(0, ("ldapsam_update_group_mapping_entry: No group to modify!\n"));
2435 ldap_msgfree(result);
2436 return NT_STATUS_UNSUCCESSFUL;
2439 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2441 if (!init_ldap_from_group(ldap_state->smbldap_state->ldap_struct,
2442 result, &mods, map)) {
2443 DEBUG(0, ("ldapsam_update_group_mapping_entry: init_ldap_from_group failed\n"));
2444 ldap_msgfree(result);
2446 ldap_mods_free(mods,True);
2447 return NT_STATUS_UNSUCCESSFUL;
2451 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: nothing to do\n"));
2452 ldap_msgfree(result);
2453 return NT_STATUS_OK;
2456 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2458 ldap_msgfree(result);
2459 return NT_STATUS_UNSUCCESSFUL;
2461 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2464 ldap_mods_free(mods, True);
2465 ldap_msgfree(result);
2467 if (rc != LDAP_SUCCESS) {
2468 char *ld_error = NULL;
2469 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
2471 DEBUG(0, ("ldapsam_update_group_mapping_entry: failed to modify group %lu error: %s (%s)\n", (unsigned long)map->gid,
2472 ld_error ? ld_error : "(unknown)", ldap_err2string(rc)));
2473 SAFE_FREE(ld_error);
2474 return NT_STATUS_UNSUCCESSFUL;
2477 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map->gid));
2478 return NT_STATUS_OK;
2481 /**********************************************************************
2482 *********************************************************************/
2484 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
2487 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)methods->private_data;
2488 pstring sidstring, filter;
2489 LDAPMessage *result = NULL;
2494 sid_to_string(sidstring, &sid);
2496 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2497 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID, sidstring);
2499 rc = ldapsam_search_one_group(ldap_state, filter, &result);
2501 if (rc != LDAP_SUCCESS) {
2502 return NT_STATUS_NO_SUCH_GROUP;
2505 attr_list = get_attr_list( groupmap_attr_list_to_delete );
2506 ret = ldapsam_delete_entry(ldap_state, result, LDAP_OBJ_GROUPMAP, attr_list);
2507 free_attr_list ( attr_list );
2509 ldap_msgfree(result);
2514 /**********************************************************************
2515 *********************************************************************/
2517 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods, BOOL update)
2519 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2524 pstr_sprintf( filter, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
2525 attr_list = get_attr_list( groupmap_attr_list );
2526 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
2527 LDAP_SCOPE_SUBTREE, filter,
2528 attr_list, 0, &ldap_state->result);
2529 free_attr_list( attr_list );
2531 if (rc != LDAP_SUCCESS) {
2532 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n", ldap_err2string(rc)));
2533 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n", lp_ldap_group_suffix(), filter));
2534 ldap_msgfree(ldap_state->result);
2535 ldap_state->result = NULL;
2536 return NT_STATUS_UNSUCCESSFUL;
2539 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
2540 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2541 ldap_state->result)));
2543 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, ldap_state->result);
2544 ldap_state->index = 0;
2546 return NT_STATUS_OK;
2549 /**********************************************************************
2550 *********************************************************************/
2552 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
2554 ldapsam_endsampwent(my_methods);
2557 /**********************************************************************
2558 *********************************************************************/
2560 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
2563 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2564 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2568 if (!ldap_state->entry)
2571 ldap_state->index++;
2572 bret = init_group_from_ldap(ldap_state, map, ldap_state->entry);
2574 ldap_state->entry = ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
2578 return NT_STATUS_OK;
2581 /**********************************************************************
2582 *********************************************************************/
2584 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
2585 enum SID_NAME_USE sid_name_use,
2586 GROUP_MAP **rmap, int *num_entries,
2596 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
2597 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open passdb\n"));
2598 return NT_STATUS_ACCESS_DENIED;
2601 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
2602 if (sid_name_use != SID_NAME_UNKNOWN &&
2603 sid_name_use != map.sid_name_use) {
2604 DEBUG(11,("ldapsam_enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
2607 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
2608 DEBUG(11,("ldapsam_enum_group_mapping: group %s is non mapped\n", map.nt_name));
2612 mapt=SMB_REALLOC_ARRAY((*rmap), GROUP_MAP, entries+1);
2614 DEBUG(0,("ldapsam_enum_group_mapping: Unable to enlarge group map!\n"));
2616 return NT_STATUS_UNSUCCESSFUL;
2621 mapt[entries] = map;
2626 ldapsam_endsamgrent(methods);
2628 *num_entries = entries;
2630 return NT_STATUS_OK;
2633 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
2634 const DOM_SID *alias,
2635 const DOM_SID *member,
2638 struct ldapsam_privates *ldap_state =
2639 (struct ldapsam_privates *)methods->private_data;
2641 LDAPMessage *result = NULL;
2642 LDAPMessage *entry = NULL;
2644 LDAPMod **mods = NULL;
2649 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%s))",
2650 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY,
2651 get_attr_key2string(groupmap_attr_list,
2652 LDAP_ATTR_GROUP_SID),
2653 sid_string_static(alias));
2655 if (ldapsam_search_one_group(ldap_state, filter,
2656 &result) != LDAP_SUCCESS)
2657 return NT_STATUS_NO_SUCH_ALIAS;
2659 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2663 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
2664 ldap_msgfree(result);
2665 return NT_STATUS_NO_SUCH_ALIAS;
2669 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for filter %s: "
2670 "count=%d\n", filter, count));
2671 ldap_msgfree(result);
2672 return NT_STATUS_NO_SUCH_ALIAS;
2675 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
2679 ldap_msgfree(result);
2680 return NT_STATUS_UNSUCCESSFUL;
2683 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2685 ldap_msgfree(result);
2686 return NT_STATUS_UNSUCCESSFUL;
2689 smbldap_set_mod(&mods, modop,
2690 get_attr_key2string(groupmap_attr_list,
2691 LDAP_ATTR_SID_LIST),
2692 sid_string_static(member));
2694 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2696 ldap_mods_free(mods, True);
2697 ldap_msgfree(result);
2699 if (rc != LDAP_SUCCESS) {
2700 char *ld_error = NULL;
2701 ldap_get_option(ldap_state->smbldap_state->ldap_struct,
2702 LDAP_OPT_ERROR_STRING,&ld_error);
2704 DEBUG(0, ("ldapsam_modify_aliasmem: Could not modify alias "
2705 "for %s, error: %s (%s)\n", dn, ldap_err2string(rc),
2706 ld_error?ld_error:"unknown"));
2707 SAFE_FREE(ld_error);
2709 return NT_STATUS_UNSUCCESSFUL;
2714 return NT_STATUS_OK;
2717 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
2718 const DOM_SID *alias,
2719 const DOM_SID *member)
2721 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
2724 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
2725 const DOM_SID *alias,
2726 const DOM_SID *member)
2728 return ldapsam_modify_aliasmem(methods, alias, member,
2732 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
2733 const DOM_SID *alias, DOM_SID **members,
2736 struct ldapsam_privates *ldap_state =
2737 (struct ldapsam_privates *)methods->private_data;
2738 LDAPMessage *result = NULL;
2739 LDAPMessage *entry = NULL;
2748 pstr_sprintf(filter, "(&(|(objectClass=%s)(objectclass=%s))(%s=%s))",
2749 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY,
2750 get_attr_key2string(groupmap_attr_list,
2751 LDAP_ATTR_GROUP_SID),
2752 sid_string_static(alias));
2754 if (ldapsam_search_one_group(ldap_state, filter,
2755 &result) != LDAP_SUCCESS)
2756 return NT_STATUS_NO_SUCH_ALIAS;
2758 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2762 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
2763 ldap_msgfree(result);
2764 return NT_STATUS_NO_SUCH_ALIAS;
2768 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for filter %s: "
2769 "count=%d\n", filter, count));
2770 ldap_msgfree(result);
2771 return NT_STATUS_NO_SUCH_ALIAS;
2774 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
2778 ldap_msgfree(result);
2779 return NT_STATUS_UNSUCCESSFUL;
2782 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
2784 get_attr_key2string(groupmap_attr_list,
2785 LDAP_ATTR_SID_LIST));
2787 if (values == NULL) {
2788 ldap_msgfree(result);
2789 return NT_STATUS_OK;
2792 count = ldap_count_values(values);
2794 for (i=0; i<count; i++) {
2797 if (!string_to_sid(&member, values[i]))
2800 add_sid_to_array(&member, members, num_members);
2803 ldap_value_free(values);
2804 ldap_msgfree(result);
2806 return NT_STATUS_OK;
2809 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
2810 const DOM_SID *members,
2812 DOM_SID **aliases, int *num_aliases)
2814 struct ldapsam_privates *ldap_state =
2815 (struct ldapsam_privates *)methods->private_data;
2818 char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
2820 LDAPMessage *result = NULL;
2821 LDAPMessage *entry = NULL;
2825 TALLOC_CTX *mem_ctx;
2827 mem_ctx = talloc_init("ldapsam_alias_memberships");
2829 if (mem_ctx == NULL)
2830 return NT_STATUS_NO_MEMORY;
2832 /* This query could be further optimized by adding a
2833 (&(sambaSID=<domain-sid>*)) so that only those aliases that are
2834 asked for in the getuseraliases are returned. */
2836 filter = talloc_asprintf(mem_ctx,
2837 "(&(|(objectclass=%s)(objectclass=%s))(|",
2838 LDAP_OBJ_GROUPMAP, LDAP_OBJ_IDMAP_ENTRY);
2840 for (i=0; i<num_members; i++)
2841 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
2843 sid_string_static(&members[i]));
2845 filter = talloc_asprintf(mem_ctx, "%s))", filter);
2847 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
2848 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2850 talloc_destroy(mem_ctx);
2852 if (rc != LDAP_SUCCESS)
2853 return NT_STATUS_UNSUCCESSFUL;
2858 ldap_struct = ldap_state->smbldap_state->ldap_struct;
2860 for (entry = ldap_first_entry(ldap_struct, result);
2862 entry = ldap_next_entry(ldap_struct, entry))
2867 if (!smbldap_get_single_attribute(ldap_struct, entry,
2873 if (!string_to_sid(&sid, sid_str))
2876 add_sid_to_array_unique(&sid, aliases, num_aliases);
2879 ldap_msgfree(result);
2880 return NT_STATUS_OK;
2883 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods, int policy_index, int *value)
2885 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
2886 LDAPMessage *result = NULL;
2887 LDAPMessage *entry = NULL;
2892 const char *policy_string = NULL;
2894 BOOL found_tdb = False;
2896 struct ldapsam_privates *ldap_state =
2897 (struct ldapsam_privates *)methods->private_data;
2900 (char *)get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_NAME),
2901 (char *)get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_VAL),
2905 if (cache_account_policy_get(policy_index, value)) {
2906 DEBUG(11,("ldapsam_get_account_policy: got valid value from cache\n"));
2907 return NT_STATUS_OK;
2910 policy_string = decode_account_policy_name(policy_index);
2911 if (!policy_string) {
2912 DEBUG(0,("ldapsam_get_account_policy: invalid policy index: %d\n", policy_index));
2916 pstr_sprintf(filter, "(&(objectclass=%s)(%s=%s))",
2917 LDAP_OBJ_ACCOUNT_POLICY,
2918 get_attr_key2string(acctpol_attr_list,
2919 LDAP_ATTR_ACCOUNT_POLICY_NAME), policy_string);
2921 if (!ldap_state->domain_dn) {
2922 return NT_STATUS_INVALID_PARAMETER;
2926 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
2927 LDAP_SCOPE_ONELEVEL, filter, attrs, 0, &result);
2929 if (rc != LDAP_SUCCESS)
2932 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
2935 /* handle deleted ldap-entries (migrate on the fly, use a default as last resort) - gd */
2936 if (count < 1 && !found_tdb) {
2940 DEBUG(3,("ldapsam_get_account_policy: no entry for that policy in ldap found\n"));
2942 if (!account_policy_get(policy_index, &tmp_val)) {
2943 DEBUG(10,("ldapsam_get_account_policy: failed to get account_policy from tdb\n"));
2947 if (!found_tdb && !account_policy_get_default(policy_index, &tmp_val)) {
2948 ldap_msgfree(result);
2952 if (!pdb_set_account_policy(policy_index, tmp_val)) {
2953 DEBUG(1,("ldapsam_get_account_policy: failed to set account_policy\n"));
2954 ldap_msgfree(result);
2958 DEBUG(3,("ldapsam_get_account_policy: set account policy value based on %s value.\n",
2959 found_tdb ? "tdb":"default"));
2961 ldap_msgfree(result);
2965 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2968 ldap_msgfree(result);
2969 return NT_STATUS_UNSUCCESSFUL;
2972 vals = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry,
2973 get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_VAL));
2978 *value = (uint32)atol(vals[0]);
2980 if (!cache_account_policy_set(policy_index, *value)) {
2981 DEBUG(0,("ldapsam_get_account_policy: failed to update local tdb as a cache\n"));
2985 ntstatus = NT_STATUS_OK;
2988 ldap_value_free(vals);
2989 ldap_msgfree(result);
2994 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods, int policy_index, int value)
2996 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
2997 LDAPMessage *result = NULL;
2998 LDAPMessage *entry = NULL;
3003 LDAPMod **mods = NULL;
3004 fstring value_string;
3005 char *old_dn = NULL;
3006 const char *policy_string = NULL;
3007 const char *policy_description = NULL;
3009 struct ldapsam_privates *ldap_state =
3010 (struct ldapsam_privates *)methods->private_data;
3013 (char *)get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_NAME),
3014 (char *)get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_VAL),
3018 policy_string = decode_account_policy_name(policy_index);
3019 if (!policy_string) {
3020 DEBUG(0,("ldapsam_set_account_policy: invalid policy\n"));
3024 policy_description = account_policy_get_comment(policy_index);
3025 if (!policy_description) {
3026 DEBUG(0,("ldapsam_set_account_policy: no description for policy found\n"));
3030 pstr_sprintf(filter, "(&(objectclass=%s)(%s=%s))",
3031 LDAP_OBJ_ACCOUNT_POLICY,
3032 get_attr_key2string(acctpol_attr_list,
3033 LDAP_ATTR_ACCOUNT_POLICY_NAME), policy_string);
3035 if (!ldap_state->domain_dn) {
3036 return NT_STATUS_INVALID_PARAMETER;
3039 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3040 LDAP_SCOPE_ONELEVEL, filter, attrs, 0, &result);
3042 if (rc != LDAP_SUCCESS)
3045 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
3047 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3051 modop = LDAP_MOD_REPLACE;
3053 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
3056 ldap_msgfree(result);
3057 return NT_STATUS_UNSUCCESSFUL;
3060 old_dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
3062 ldap_msgfree(result);
3066 smbldap_set_mod(&mods, modop,
3067 get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_VAL),
3070 rc = smbldap_modify(ldap_state->smbldap_state, old_dn, mods);
3074 modop = LDAP_MOD_ADD;
3076 pstr_sprintf(dn, "%s=%s,%s",
3077 get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_NAME), policy_string,
3078 ldap_state->domain_dn);
3080 smbldap_set_mod( &mods, modop, "objectClass", LDAP_OBJ_ACCOUNT_POLICY );
3082 smbldap_make_mod( ldap_state->smbldap_state->ldap_struct, entry, &mods,
3083 get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_NAME),
3086 smbldap_make_mod( ldap_state->smbldap_state->ldap_struct, entry, &mods,
3087 get_attr_key2string(acctpol_attr_list, LDAP_ATTR_ACCOUNT_POLICY_VAL),
3090 smbldap_make_mod( ldap_state->smbldap_state->ldap_struct, entry, &mods,
3092 policy_description);
3094 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3097 ldap_mods_free(mods, True);
3098 ldap_msgfree(result);
3100 if (rc != LDAP_SUCCESS) {
3101 char *ld_error = NULL;
3102 ldap_get_option(ldap_state->smbldap_state->ldap_struct,
3103 LDAP_OPT_ERROR_STRING,&ld_error);
3105 DEBUG(0, ("ldapsam_set_account_policy: Could not set account policy "
3106 "for %s, error: %s (%s)\n", dn, ldap_err2string(rc),
3107 ld_error?ld_error:"unknown"));
3108 SAFE_FREE(ld_error);
3115 if (!cache_account_policy_set(policy_index, value)) {
3116 DEBUG(0,("ldapsam_set_account_policy: failed to update local tdb cache\n"));
3120 return NT_STATUS_OK;
3123 /**********************************************************************
3125 *********************************************************************/
3127 static void free_private_data(void **vp)
3129 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
3131 smbldap_free_struct(&(*ldap_state)->smbldap_state);
3133 if ((*ldap_state)->result != NULL) {
3134 ldap_msgfree((*ldap_state)->result);
3135 (*ldap_state)->result = NULL;
3137 if ((*ldap_state)->domain_dn != NULL) {
3138 SAFE_FREE((*ldap_state)->domain_dn);
3143 /* No need to free any further, as it is talloc()ed */
3146 /**********************************************************************
3147 Intitalise the parts of the pdb_context that are common to all pdb_ldap modes
3148 *********************************************************************/
3150 static NTSTATUS pdb_init_ldapsam_common(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method,
3151 const char *location)
3154 struct ldapsam_privates *ldap_state;
3156 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
3160 (*pdb_method)->name = "ldapsam";
3162 (*pdb_method)->setsampwent = ldapsam_setsampwent;
3163 (*pdb_method)->endsampwent = ldapsam_endsampwent;
3164 (*pdb_method)->getsampwent = ldapsam_getsampwent;
3165 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
3166 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
3167 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
3168 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
3169 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
3171 (*pdb_method)->getgrsid = ldapsam_getgrsid;
3172 (*pdb_method)->getgrgid = ldapsam_getgrgid;
3173 (*pdb_method)->getgrnam = ldapsam_getgrnam;
3174 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
3175 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
3176 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
3177 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
3178 (*pdb_method)->enum_group_memberships = ldapsam_enum_group_memberships;
3180 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
3181 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
3183 /* TODO: Setup private data and free */
3185 ldap_state = TALLOC_ZERO_P(pdb_context->mem_ctx, struct ldapsam_privates);
3187 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
3188 return NT_STATUS_NO_MEMORY;
3191 if (!NT_STATUS_IS_OK(nt_status =
3192 smbldap_init(pdb_context->mem_ctx, location,
3193 &ldap_state->smbldap_state)));
3195 ldap_state->domain_name = talloc_strdup(pdb_context->mem_ctx, get_global_sam_name());
3196 if (!ldap_state->domain_name) {
3197 return NT_STATUS_NO_MEMORY;
3200 (*pdb_method)->private_data = ldap_state;
3202 (*pdb_method)->free_private_data = free_private_data;
3204 return NT_STATUS_OK;
3207 /**********************************************************************
3208 Initialise the 'compat' mode for pdb_ldap
3209 *********************************************************************/
3211 static NTSTATUS pdb_init_ldapsam_compat(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
3214 struct ldapsam_privates *ldap_state;
3216 #ifdef WITH_LDAP_SAMCONFIG
3218 int ldap_port = lp_ldap_port();
3220 /* remap default port if not using SSL (ie clear or TLS) */
3221 if ( (lp_ldap_ssl() != LDAP_SSL_ON) && (ldap_port == 636) ) {
3225 location = talloc_asprintf(pdb_context->mem_ctx, "%s://%s:%d", lp_ldap_ssl() == LDAP_SSL_ON ? "ldaps" : "ldap", lp_ldap_server(), ldap_port);
3227 return NT_STATUS_NO_MEMORY;
3232 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common(pdb_context, pdb_method, location))) {
3236 (*pdb_method)->name = "ldapsam_compat";
3238 ldap_state = (*pdb_method)->private_data;
3239 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
3241 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
3243 return NT_STATUS_OK;
3246 /**********************************************************************
3247 Initialise the normal mode for pdb_ldap
3248 *********************************************************************/
3250 static NTSTATUS pdb_init_ldapsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
3253 struct ldapsam_privates *ldap_state;
3254 uint32 alg_rid_base;
3255 pstring alg_rid_base_string;
3256 LDAPMessage *result = NULL;
3257 LDAPMessage *entry = NULL;
3258 DOM_SID ldap_domain_sid;
3259 DOM_SID secrets_domain_sid;
3260 pstring domain_sid_string;
3263 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common(pdb_context, pdb_method, location))) {
3267 (*pdb_method)->name = "ldapsam";
3269 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
3270 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
3271 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
3272 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
3274 ldap_state = (*pdb_method)->private_data;
3275 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
3277 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
3279 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state, &result,
3280 ldap_state->domain_name, True);
3282 if ( !NT_STATUS_IS_OK(nt_status) ) {
3283 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain info, nor add one to the domain\n"));
3284 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, will be unable to allocate new users/groups, \
3285 and will risk BDCs having inconsistant SIDs\n"));
3286 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
3287 return NT_STATUS_OK;
3290 /* Given that the above might fail, everything below this must be optional */
3292 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
3294 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info entry\n"));
3295 ldap_msgfree(result);
3296 return NT_STATUS_UNSUCCESSFUL;
3299 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
3301 return NT_STATUS_UNSUCCESSFUL;
3304 ldap_state->domain_dn = smb_xstrdup(dn);
3305 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
3306 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
3307 domain_sid_string)) {
3309 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
3310 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be read as a valid SID\n", domain_sid_string));
3311 return NT_STATUS_INVALID_PARAMETER;
3313 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name, &secrets_domain_sid);
3314 if (!found_sid || !sid_equal(&secrets_domain_sid, &ldap_domain_sid)) {
3315 fstring new_sid_str, old_sid_str;
3316 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain %s based on pdb_ldap results %s -> %s\n",
3317 ldap_state->domain_name,
3318 sid_to_string(old_sid_str, &secrets_domain_sid),
3319 sid_to_string(new_sid_str, &ldap_domain_sid)));
3321 /* reset secrets.tdb sid */
3322 secrets_store_domain_sid(ldap_state->domain_name, &ldap_domain_sid);
3323 DEBUG(1, ("New global sam SID: %s\n", sid_to_string(new_sid_str, get_global_sam_sid())));
3325 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
3328 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
3329 get_attr_key2string( dominfo_attr_list, LDAP_ATTR_ALGORITHMIC_RID_BASE ),
3330 alg_rid_base_string)) {
3331 alg_rid_base = (uint32)atol(alg_rid_base_string);
3332 if (alg_rid_base != algorithmic_rid_base()) {
3333 DEBUG(0, ("The value of 'algorithmic RID base' has changed since the LDAP\n"
3334 "database was initialised. Aborting. \n"));
3335 ldap_msgfree(result);
3336 return NT_STATUS_UNSUCCESSFUL;
3339 ldap_msgfree(result);
3341 return NT_STATUS_OK;
3344 NTSTATUS pdb_ldap_init(void)
3347 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
3350 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
3353 return NT_STATUS_OK;