2 Unix SMB/CIFS mplementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/SAM_ACCOUNT
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
49 #define DBGC_CLASS DBGC_PASSDB
55 * Work around versions of the LDAP client libs that don't have the OIDs
56 * defined, or have them defined under the old name.
57 * This functionality is really a factor of the server, not the client
61 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
62 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
63 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
64 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
67 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
68 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
69 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
70 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
73 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
75 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
76 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
81 #define SAM_ACCOUNT struct sam_passwd
86 struct ldapsam_privates
{
87 struct smbldap_state
*smbldap_state
;
94 const char *domain_name
;
97 /* configuration items */
101 /**********************************************************************
102 Free a LDAPMessage (one is stored on the SAM_ACCOUNT).
103 **********************************************************************/
105 static void private_data_free_fn(void **result
)
107 ldap_msgfree(*result
);
111 /**********************************************************************
112 Get the attribute name given a user schame version.
113 **********************************************************************/
115 static const char* get_userattr_key2string( int schema_ver
, int key
)
117 switch ( schema_ver
) {
118 case SCHEMAVER_SAMBAACCOUNT
:
119 return get_attr_key2string( attrib_map_v22
, key
);
121 case SCHEMAVER_SAMBASAMACCOUNT
:
122 return get_attr_key2string( attrib_map_v30
, key
);
125 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
131 /**********************************************************************
132 Return the list of attribute names given a user schema version.
133 **********************************************************************/
135 static char** get_userattr_list( int schema_ver
)
137 switch ( schema_ver
) {
138 case SCHEMAVER_SAMBAACCOUNT
:
139 return get_attr_list( attrib_map_v22
);
141 case SCHEMAVER_SAMBASAMACCOUNT
:
142 return get_attr_list( attrib_map_v30
);
144 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
151 /*******************************************************************
152 Generate the LDAP search filter for the objectclass based on the
153 version of the schema we are using.
154 ******************************************************************/
156 static const char* get_objclass_filter( int schema_ver
)
158 static fstring objclass_filter
;
160 switch( schema_ver
) {
161 case SCHEMAVER_SAMBAACCOUNT
:
162 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
164 case SCHEMAVER_SAMBASAMACCOUNT
:
165 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
168 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
172 return objclass_filter
;
175 /*******************************************************************
176 Run the search by name.
177 ******************************************************************/
179 static int ldapsam_search_suffix_by_name (struct ldapsam_privates
*ldap_state
,
181 LDAPMessage
** result
, char **attr
)
184 char *escape_user
= escape_ldap_string_alloc(user
);
187 return LDAP_NO_MEMORY
;
191 * in the filter expression, replace %u with the real name
192 * so in ldap filter, %u MUST exist :-)
194 pstr_sprintf(filter
, "(&%s%s)", lp_ldap_filter(),
195 get_objclass_filter(ldap_state
->schema_ver
));
198 * have to use this here because $ is filtered out
203 all_string_sub(filter
, "%u", escape_user
, sizeof(pstring
));
204 SAFE_FREE(escape_user
);
206 return smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
209 /*******************************************************************
210 Run the search by rid.
211 ******************************************************************/
213 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
214 uint32 rid
, LDAPMessage
** result
,
220 pstr_sprintf(filter
, "(&(rid=%i)%s)", rid
,
221 get_objclass_filter(ldap_state
->schema_ver
));
223 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
228 /*******************************************************************
229 Run the search by SID.
230 ******************************************************************/
232 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
233 const DOM_SID
*sid
, LDAPMessage
** result
,
240 pstr_sprintf(filter
, "(&(%s=%s)%s)",
241 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
242 sid_to_string(sid_string
, sid
),
243 get_objclass_filter(ldap_state
->schema_ver
));
245 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
250 /*******************************************************************
251 Delete complete object or objectclass and attrs from
252 object found in search_result depending on lp_ldap_delete_dn
253 ******************************************************************/
255 static NTSTATUS
ldapsam_delete_entry(struct ldapsam_privates
*ldap_state
,
257 const char *objectclass
,
261 LDAPMessage
*entry
= NULL
;
262 LDAPMod
**mods
= NULL
;
264 BerElement
*ptr
= NULL
;
266 rc
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
269 DEBUG(0, ("ldapsam_delete_entry: Entry must exist exactly once!\n"));
270 return NT_STATUS_UNSUCCESSFUL
;
273 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
274 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
276 return NT_STATUS_UNSUCCESSFUL
;
279 if (lp_ldap_delete_dn()) {
280 NTSTATUS ret
= NT_STATUS_OK
;
281 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
283 if (rc
!= LDAP_SUCCESS
) {
284 DEBUG(0, ("ldapsam_delete_entry: Could not delete object %s\n", dn
));
285 ret
= NT_STATUS_UNSUCCESSFUL
;
291 /* Ok, delete only the SAM attributes */
293 for (name
= ldap_first_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
, &ptr
);
295 name
= ldap_next_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
, ptr
)) {
298 /* We are only allowed to delete the attributes that
301 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
302 if (StrCaseCmp(*attrib
, name
) == 0) {
303 DEBUG(10, ("ldapsam_delete_entry: deleting attribute %s\n", name
));
304 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
, NULL
);
315 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
317 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
318 ldap_mods_free(mods
, True
);
320 if (rc
!= LDAP_SUCCESS
) {
321 char *ld_error
= NULL
;
322 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
325 DEBUG(0, ("ldapsam_delete_entry: Could not delete attributes for %s, error: %s (%s)\n",
326 dn
, ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
329 return NT_STATUS_UNSUCCESSFUL
;
336 /* New Interface is being implemented here */
338 #if 0 /* JERRY - not uesed anymore */
340 /**********************************************************************
341 Initialize SAM_ACCOUNT from an LDAP query (unix attributes only)
342 *********************************************************************/
343 static BOOL
get_unix_attributes (struct ldapsam_privates
*ldap_state
,
344 SAM_ACCOUNT
* sampass
,
353 if ((ldap_values
= ldap_get_values (ldap_state
->smbldap_state
->ldap_struct
, entry
, "objectClass")) == NULL
) {
354 DEBUG (1, ("get_unix_attributes: no objectClass! \n"));
358 for (values
=ldap_values
;*values
;values
++) {
359 if (strequal(*values
, LDAP_OBJ_POSIXACCOUNT
)) {
364 if (!*values
) { /*end of array, no posixAccount */
365 DEBUG(10, ("user does not have %s attributes\n", LDAP_OBJ_POSIXACCOUNT
));
366 ldap_value_free(ldap_values
);
369 ldap_value_free(ldap_values
);
371 if ( !smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
372 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_UNIX_HOME
), homedir
) )
377 if ( !smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
378 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_GIDNUMBER
), temp
) )
383 *gid
= (gid_t
)atol(temp
);
385 pdb_set_unix_homedir(sampass
, homedir
, PDB_SET
);
387 DEBUG(10, ("user has %s attributes\n", LDAP_OBJ_POSIXACCOUNT
));
394 static time_t ldapsam_get_entry_timestamp(
395 struct ldapsam_privates
*ldap_state
,
401 if (!smbldap_get_single_pstring(
402 ldap_state
->smbldap_state
->ldap_struct
, entry
,
403 get_userattr_key2string(ldap_state
->schema_ver
,
404 LDAP_ATTR_MOD_TIMESTAMP
),
408 strptime(temp
, "%Y%m%d%H%M%SZ", &tm
);
410 return (mktime(&tm
) - timezone
);
413 /**********************************************************************
414 Initialize SAM_ACCOUNT from an LDAP query.
415 (Based on init_sam_from_buffer in pdb_tdb.c)
416 *********************************************************************/
418 static BOOL
init_sam_from_ldap (struct ldapsam_privates
*ldap_state
,
419 SAM_ACCOUNT
* sampass
,
426 pass_can_change_time
,
427 pass_must_change_time
,
440 char munged_dial
[2048];
442 uint8 smblmpwd
[LM_HASH_LEN
],
443 smbntpwd
[NT_HASH_LEN
];
444 uint16 acct_ctrl
= 0,
446 uint16 bad_password_count
= 0,
449 uint8 hours
[MAX_HOURS_LEN
];
451 LOGIN_CACHE
*cache_entry
= NULL
;
454 * do a little initialization
458 nt_username
[0] = '\0';
462 logon_script
[0] = '\0';
463 profile_path
[0] = '\0';
465 munged_dial
[0] = '\0';
466 workstations
[0] = '\0';
469 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
470 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
474 if (ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
475 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->ldap_struct is NULL!\n"));
479 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
, "uid", username
)) {
480 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for this user!\n"));
484 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
486 pstrcpy(nt_username
, username
);
488 pstrcpy(domain
, ldap_state
->domain_name
);
490 pdb_set_username(sampass
, username
, PDB_SET
);
492 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
493 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
495 /* deal with different attributes between the schema first */
497 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
498 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
499 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
), temp
)) {
500 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
503 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
504 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PRIMARY_GROUP_SID
), temp
)) {
505 pdb_set_group_sid_from_string(sampass
, temp
, PDB_SET
);
507 pdb_set_group_sid_from_rid(sampass
, DOMAIN_GROUP_RID_USERS
, PDB_DEFAULT
);
510 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
511 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
), temp
)) {
512 user_rid
= (uint32
)atol(temp
);
513 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
516 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
517 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PRIMARY_GROUP_RID
), temp
)) {
518 pdb_set_group_sid_from_rid(sampass
, DOMAIN_GROUP_RID_USERS
, PDB_DEFAULT
);
522 group_rid
= (uint32
)atol(temp
);
524 /* for some reason, we often have 0 as a primary group RID.
525 Make sure that we treat this just as a 'default' value */
528 pdb_set_group_sid_from_rid(sampass
, group_rid
, PDB_SET
);
530 pdb_set_group_sid_from_rid(sampass
, DOMAIN_GROUP_RID_USERS
, PDB_DEFAULT
);
534 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
535 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
536 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
537 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
543 #if 0 /* JERRY -- not used anymore */
545 * If so configured, try and get the values from LDAP
548 if (lp_ldap_trust_ids() && (get_unix_attributes(ldap_state
, sampass
, entry
, &gid
)))
550 if (pdb_get_init_flags(sampass
,PDB_GROUPSID
) == PDB_DEFAULT
)
553 /* call the mapping code here */
554 if(pdb_getgrgid(&map
, gid
)) {
555 pdb_set_group_sid(sampass
, &map
.sid
, PDB_SET
);
558 pdb_set_group_sid_from_rid(sampass
, pdb_gid_to_group_rid(gid
), PDB_SET
);
564 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
565 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
), temp
)) {
566 /* leave as default */
568 pass_last_set_time
= (time_t) atol(temp
);
569 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
, PDB_SET
);
572 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
573 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
)) {
574 /* leave as default */
576 logon_time
= (time_t) atol(temp
);
577 pdb_set_logon_time(sampass
, logon_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_LOGOFF_TIME
), temp
)) {
582 /* leave as default */
584 logoff_time
= (time_t) atol(temp
);
585 pdb_set_logoff_time(sampass
, logoff_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_KICKOFF_TIME
), temp
)) {
590 /* leave as default */
592 kickoff_time
= (time_t) atol(temp
);
593 pdb_set_kickoff_time(sampass
, kickoff_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_PWD_CAN_CHANGE
), temp
)) {
598 /* leave as default */
600 pass_can_change_time
= (time_t) atol(temp
);
601 pdb_set_pass_can_change_time(sampass
, pass_can_change_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_MUST_CHANGE
), temp
)) {
606 /* leave as default */
608 pass_must_change_time
= (time_t) atol(temp
);
609 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, PDB_SET
);
612 /* recommend that 'gecos' and 'displayName' should refer to the same
613 * attribute OID. userFullName depreciated, only used by Samba
614 * primary rules of LDAP: don't make a new attribute when one is already defined
615 * that fits your needs; using cn then displayName rather than 'userFullName'
618 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
619 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
), fullname
)) {
620 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
621 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_CN
), fullname
)) {
622 /* leave as default */
624 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
627 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
630 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
631 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
), dir_drive
))
633 pdb_set_dir_drive( sampass
,
634 talloc_sub_basic(sampass
->mem_ctx
, username
, lp_logon_drive()),
637 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
640 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
641 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
), homedir
))
643 pdb_set_homedir( sampass
,
644 talloc_sub_basic(sampass
->mem_ctx
, username
, lp_logon_home()),
647 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
650 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
651 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
), logon_script
))
653 pdb_set_logon_script( sampass
,
654 talloc_sub_basic(sampass
->mem_ctx
, username
, lp_logon_script()),
657 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
660 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
661 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
), profile_path
))
663 pdb_set_profile_path( sampass
,
664 talloc_sub_basic( sampass
->mem_ctx
, username
, lp_logon_path()),
667 pdb_set_profile_path(sampass
, profile_path
, 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_DESC
), acct_desc
))
673 /* leave as default */
675 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
678 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
679 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
), workstations
)) {
680 /* leave as default */;
682 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
685 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
686 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
), munged_dial
, sizeof(munged_dial
))) {
687 /* leave as default */;
689 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
692 /* FIXME: hours stuff should be cleaner */
696 memset(hours
, 0xff, hours_len
);
698 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
699 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
), temp
)) {
700 /* leave as default */
702 pdb_gethexpwd(temp
, smblmpwd
);
703 memset((char *)temp
, '\0', strlen(temp
)+1);
704 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
))
706 ZERO_STRUCT(smblmpwd
);
709 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
710 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
), temp
)) {
711 /* leave as default */
713 pdb_gethexpwd(temp
, smbntpwd
);
714 memset((char *)temp
, '\0', strlen(temp
)+1);
715 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
))
717 ZERO_STRUCT(smbntpwd
);
720 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
721 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
), temp
)) {
722 acct_ctrl
|= ACB_NORMAL
;
724 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
727 acct_ctrl
|= ACB_NORMAL
;
729 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
732 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
733 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
735 /* pdb_set_munged_dial(sampass, munged_dial, PDB_SET); */
737 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
738 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_COUNT
), temp
)) {
739 /* leave as default */
741 bad_password_count
= (uint32
) atol(temp
);
742 pdb_set_bad_password_count(sampass
, bad_password_count
, PDB_SET
);
745 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
746 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_TIME
), temp
)) {
747 /* leave as default */
749 bad_password_time
= (time_t) atol(temp
);
750 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
754 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
755 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_COUNT
), temp
)) {
756 /* leave as default */
758 logon_count
= (uint32
) atol(temp
);
759 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
762 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
764 pdb_set_hours(sampass
, hours
, PDB_SET
);
766 /* check the timestamp of the cache vs ldap entry */
767 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
771 /* see if we have newer updates */
772 if (!(cache_entry
= login_cache_read(sampass
))) {
773 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
774 (unsigned int)pdb_get_bad_password_count(sampass
),
775 (unsigned int)pdb_get_bad_password_time(sampass
)));
779 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
780 (unsigned int)ldap_entry_time
, (unsigned int)cache_entry
->entry_timestamp
,
781 (unsigned int)cache_entry
->bad_password_time
));
783 if (ldap_entry_time
> cache_entry
->entry_timestamp
) {
784 /* cache is older than directory , so
785 we need to delete the entry but allow the
786 fields to be written out */
787 login_cache_delentry(sampass
);
790 pdb_set_acct_ctrl(sampass
,
791 pdb_get_acct_ctrl(sampass
) |
792 (cache_entry
->acct_ctrl
& ACB_AUTOLOCK
),
794 pdb_set_bad_password_count(sampass
,
795 cache_entry
->bad_password_count
,
797 pdb_set_bad_password_time(sampass
,
798 cache_entry
->bad_password_time
,
802 SAFE_FREE(cache_entry
);
806 /**********************************************************************
807 Initialize SAM_ACCOUNT from an LDAP query.
808 (Based on init_buffer_from_sam in pdb_tdb.c)
809 *********************************************************************/
811 static BOOL
init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
812 LDAPMessage
*existing
,
813 LDAPMod
*** mods
, SAM_ACCOUNT
* sampass
,
814 BOOL (*need_update
)(const SAM_ACCOUNT
*,
820 if (mods
== NULL
|| sampass
== NULL
) {
821 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
828 * took out adding "objectclass: sambaAccount"
829 * do this on a per-mod basis
831 if (need_update(sampass
, PDB_USERNAME
))
832 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
833 "uid", pdb_get_username(sampass
));
835 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
837 /* only update the RID if we actually need to */
838 if (need_update(sampass
, PDB_USERSID
)) {
840 fstring dom_sid_string
;
841 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
843 switch ( ldap_state
->schema_ver
) {
844 case SCHEMAVER_SAMBAACCOUNT
:
845 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
846 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
847 sid_to_string(sid_string
, user_sid
),
848 sid_to_string(dom_sid_string
, &ldap_state
->domain_sid
)));
851 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
852 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
853 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
857 case SCHEMAVER_SAMBASAMACCOUNT
:
858 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
859 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
860 sid_to_string(sid_string
, user_sid
));
864 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
869 /* we don't need to store the primary group RID - so leaving it
870 'free' to hang off the unix primary group makes life easier */
872 if (need_update(sampass
, PDB_GROUPSID
)) {
874 fstring dom_sid_string
;
875 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
877 switch ( ldap_state
->schema_ver
) {
878 case SCHEMAVER_SAMBAACCOUNT
:
879 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
880 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
881 sid_to_string(sid_string
, group_sid
),
882 sid_to_string(dom_sid_string
, &ldap_state
->domain_sid
)));
886 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
887 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
888 get_userattr_key2string(ldap_state
->schema_ver
,
889 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
892 case SCHEMAVER_SAMBASAMACCOUNT
:
893 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
894 get_userattr_key2string(ldap_state
->schema_ver
,
895 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_string(sid_string
, group_sid
));
899 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
905 /* displayName, cn, and gecos should all be the same
906 * most easily accomplished by giving them the same OID
907 * gecos isn't set here b/c it should be handled by the
909 * We change displayName only and fall back to cn if
913 if (need_update(sampass
, PDB_FULLNAME
))
914 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
915 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
916 pdb_get_fullname(sampass
));
918 if (need_update(sampass
, PDB_ACCTDESC
))
919 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
920 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
921 pdb_get_acct_desc(sampass
));
923 if (need_update(sampass
, PDB_WORKSTATIONS
))
924 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
925 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
926 pdb_get_workstations(sampass
));
928 if (need_update(sampass
, PDB_MUNGEDDIAL
))
929 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
930 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
931 pdb_get_munged_dial(sampass
));
933 if (need_update(sampass
, PDB_SMBHOME
))
934 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
935 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
936 pdb_get_homedir(sampass
));
938 if (need_update(sampass
, PDB_DRIVE
))
939 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
940 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
941 pdb_get_dir_drive(sampass
));
943 if (need_update(sampass
, PDB_LOGONSCRIPT
))
944 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
945 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
946 pdb_get_logon_script(sampass
));
948 if (need_update(sampass
, PDB_PROFILE
))
949 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
950 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
951 pdb_get_profile_path(sampass
));
953 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logon_time(sampass
));
954 if (need_update(sampass
, PDB_LOGONTIME
))
955 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
956 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
958 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logoff_time(sampass
));
959 if (need_update(sampass
, PDB_LOGOFFTIME
))
960 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
961 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
963 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_kickoff_time(sampass
));
964 if (need_update(sampass
, PDB_KICKOFFTIME
))
965 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
966 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
968 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_can_change_time(sampass
));
969 if (need_update(sampass
, PDB_CANCHANGETIME
))
970 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
971 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
973 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_must_change_time(sampass
));
974 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
975 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
976 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
979 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
980 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
982 if (need_update(sampass
, PDB_LMPASSWD
)) {
983 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
985 pdb_sethexpwd(temp
, lm_pw
,
986 pdb_get_acct_ctrl(sampass
));
987 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
988 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
991 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
992 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
996 if (need_update(sampass
, PDB_NTPASSWD
)) {
997 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
999 pdb_sethexpwd(temp
, nt_pw
,
1000 pdb_get_acct_ctrl(sampass
));
1001 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1002 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1005 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1006 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1011 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1012 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_last_set_time(sampass
));
1013 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1014 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1019 /* FIXME: Hours stuff goes in LDAP */
1021 if (need_update(sampass
, PDB_ACCTCTRL
))
1022 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1023 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1024 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1026 /* password lockout cache:
1027 - If we are now autolocking or clearing, we write to ldap
1028 - If we are clearing, we delete the cache entry
1029 - If the count is > 0, we update the cache
1031 This even means when autolocking, we cache, just in case the
1032 update doesn't work, and we have to cache the autolock flag */
1034 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1035 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1036 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1037 time_t badtime
= pdb_get_bad_password_time(sampass
);
1039 account_policy_get(AP_BAD_ATTEMPT_LOCKOUT
, &pol
);
1041 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1042 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1044 if ((badcount
>= pol
) || (badcount
== 0)) {
1045 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1046 (unsigned int)badcount
, (unsigned int)badtime
));
1047 slprintf (temp
, sizeof (temp
) - 1, "%li", (long)badcount
);
1049 ldap_state
->smbldap_state
->ldap_struct
,
1051 get_userattr_key2string(
1052 ldap_state
->schema_ver
,
1053 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1056 slprintf (temp
, sizeof (temp
) - 1, "%li", badtime
);
1058 ldap_state
->smbldap_state
->ldap_struct
,
1060 get_userattr_key2string(
1061 ldap_state
->schema_ver
,
1062 LDAP_ATTR_BAD_PASSWORD_TIME
),
1065 if (badcount
== 0) {
1066 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1067 login_cache_delentry(sampass
);
1069 LOGIN_CACHE cache_entry
={time(NULL
),
1070 pdb_get_acct_ctrl(sampass
),
1072 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1073 login_cache_write(sampass
, cache_entry
);
1080 /**********************************************************************
1081 Connect to LDAP server for password enumeration.
1082 *********************************************************************/
1084 static NTSTATUS
ldapsam_setsampwent(struct pdb_methods
*my_methods
, BOOL update
)
1086 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1091 pstr_sprintf( filter
, "(&%s%s)", lp_ldap_filter(),
1092 get_objclass_filter(ldap_state
->schema_ver
));
1093 all_string_sub(filter
, "%u", "*", sizeof(pstring
));
1095 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1096 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
1097 attr_list
, &ldap_state
->result
);
1098 free_attr_list( attr_list
);
1100 if (rc
!= LDAP_SUCCESS
) {
1101 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc
)));
1102 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", lp_ldap_suffix(), filter
));
1103 ldap_msgfree(ldap_state
->result
);
1104 ldap_state
->result
= NULL
;
1105 return NT_STATUS_UNSUCCESSFUL
;
1108 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base!\n",
1109 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
1110 ldap_state
->result
)));
1112 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
1113 ldap_state
->result
);
1114 ldap_state
->index
= 0;
1116 return NT_STATUS_OK
;
1119 /**********************************************************************
1120 End enumeration of the LDAP password list.
1121 *********************************************************************/
1123 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1125 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1126 if (ldap_state
->result
) {
1127 ldap_msgfree(ldap_state
->result
);
1128 ldap_state
->result
= NULL
;
1132 /**********************************************************************
1133 Get the next entry in the LDAP password database.
1134 *********************************************************************/
1136 static NTSTATUS
ldapsam_getsampwent(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
)
1138 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1139 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1143 if (!ldap_state
->entry
)
1146 ldap_state
->index
++;
1147 bret
= init_sam_from_ldap(ldap_state
, user
, ldap_state
->entry
);
1149 ldap_state
->entry
= ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
1153 return NT_STATUS_OK
;
1156 /**********************************************************************
1157 Get SAM_ACCOUNT entry from LDAP by username.
1158 *********************************************************************/
1160 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
, const char *sname
)
1162 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1163 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1164 LDAPMessage
*result
= NULL
;
1165 LDAPMessage
*entry
= NULL
;
1170 attr_list
= get_userattr_list( ldap_state
->schema_ver
);
1171 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
, attr_list
);
1172 free_attr_list( attr_list
);
1174 if ( rc
!= LDAP_SUCCESS
)
1175 return NT_STATUS_NO_SUCH_USER
;
1177 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1180 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1181 ldap_msgfree(result
);
1182 return NT_STATUS_NO_SUCH_USER
;
1183 } else if (count
> 1) {
1184 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1185 ldap_msgfree(result
);
1186 return NT_STATUS_NO_SUCH_USER
;
1189 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1191 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1192 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1193 ldap_msgfree(result
);
1194 return NT_STATUS_NO_SUCH_USER
;
1196 pdb_set_backend_private_data(user
, result
,
1197 private_data_free_fn
,
1198 my_methods
, PDB_CHANGED
);
1201 ldap_msgfree(result
);
1206 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1207 const DOM_SID
*sid
, LDAPMessage
**result
)
1213 switch ( ldap_state
->schema_ver
) {
1214 case SCHEMAVER_SAMBASAMACCOUNT
:
1215 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1216 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
, result
, attr_list
);
1217 free_attr_list( attr_list
);
1219 if ( rc
!= LDAP_SUCCESS
)
1223 case SCHEMAVER_SAMBAACCOUNT
:
1224 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1228 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1229 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1230 free_attr_list( attr_list
);
1232 if ( rc
!= LDAP_SUCCESS
)
1239 /**********************************************************************
1240 Get SAM_ACCOUNT entry from LDAP by SID.
1241 *********************************************************************/
1243 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* user
, const DOM_SID
*sid
)
1245 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1246 LDAPMessage
*result
= NULL
;
1247 LDAPMessage
*entry
= NULL
;
1252 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1254 if (rc
!= LDAP_SUCCESS
)
1255 return NT_STATUS_NO_SUCH_USER
;
1257 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1260 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string
, sid
),
1262 ldap_msgfree(result
);
1263 return NT_STATUS_NO_SUCH_USER
;
1264 } else if (count
> 1) {
1265 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string
, sid
),
1267 ldap_msgfree(result
);
1268 return NT_STATUS_NO_SUCH_USER
;
1271 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1273 ldap_msgfree(result
);
1274 return NT_STATUS_NO_SUCH_USER
;
1277 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1278 DEBUG(1,("ldapsam_getsampwrid: init_sam_from_ldap failed!\n"));
1279 ldap_msgfree(result
);
1280 return NT_STATUS_NO_SUCH_USER
;
1283 pdb_set_backend_private_data(user
, result
,
1284 private_data_free_fn
,
1285 my_methods
, PDB_CHANGED
);
1286 return NT_STATUS_OK
;
1289 /********************************************************************
1290 Do the actual modification - also change a plaintext passord if
1292 **********************************************************************/
1294 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1295 SAM_ACCOUNT
*newpwd
, char *dn
,
1296 LDAPMod
**mods
, int ldap_op
,
1297 BOOL (*need_update
)(const SAM_ACCOUNT
*, enum pdb_elements
))
1299 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1302 if (!my_methods
|| !newpwd
|| !dn
) {
1303 return NT_STATUS_INVALID_PARAMETER
;
1307 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1308 /* may be password change below however */
1312 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1315 rc
= smbldap_add(ldap_state
->smbldap_state
,
1318 case LDAP_MOD_REPLACE
:
1319 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1323 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1325 return NT_STATUS_INVALID_PARAMETER
;
1328 if (rc
!=LDAP_SUCCESS
) {
1329 char *ld_error
= NULL
;
1330 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1332 DEBUG(1, ("ldapsam_modify_entry: Failed to %s user dn= %s with: %s\n\t%s\n",
1333 ldap_op
== LDAP_MOD_ADD
? "add" : "modify",
1334 dn
, ldap_err2string(rc
),
1335 ld_error
?ld_error
:"unknown"));
1336 SAFE_FREE(ld_error
);
1337 return NT_STATUS_UNSUCCESSFUL
;
1341 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1342 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1343 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1344 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1348 struct berval
*retdata
;
1349 char *utf8_password
;
1352 if (push_utf8_allocate(&utf8_password
, pdb_get_plaintext_passwd(newpwd
)) == (size_t)-1) {
1353 return NT_STATUS_NO_MEMORY
;
1356 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1357 return NT_STATUS_NO_MEMORY
;
1360 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1361 DEBUG(0,("ber_alloc_t returns NULL\n"));
1362 SAFE_FREE(utf8_password
);
1363 return NT_STATUS_UNSUCCESSFUL
;
1366 ber_printf (ber
, "{");
1367 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
, utf8_dn
);
1368 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
, utf8_password
);
1369 ber_printf (ber
, "N}");
1371 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1372 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1375 SAFE_FREE(utf8_password
);
1376 return NT_STATUS_UNSUCCESSFUL
;
1380 SAFE_FREE(utf8_password
);
1383 if ((rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1384 LDAP_EXOP_MODIFY_PASSWD
,
1385 bv
, NULL
, NULL
, &retoid
,
1386 &retdata
)) != LDAP_SUCCESS
) {
1387 char *ld_error
= NULL
;
1388 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1390 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1391 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1392 SAFE_FREE(ld_error
);
1394 return NT_STATUS_UNSUCCESSFUL
;
1396 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1397 #ifdef DEBUG_PASSWORD
1398 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1400 ber_bvfree(retdata
);
1401 ber_memfree(retoid
);
1405 return NT_STATUS_OK
;
1408 /**********************************************************************
1409 Delete entry from LDAP for username.
1410 *********************************************************************/
1412 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* sam_acct
)
1414 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1417 LDAPMessage
*result
= NULL
;
1423 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1424 return NT_STATUS_INVALID_PARAMETER
;
1427 sname
= pdb_get_username(sam_acct
);
1429 DEBUG (3, ("ldapsam_delete_sam_account: Deleting user %s from LDAP.\n", sname
));
1431 attr_list
= get_userattr_list( ldap_state
->schema_ver
);
1432 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
, attr_list
);
1434 if (rc
!= LDAP_SUCCESS
) {
1435 free_attr_list( attr_list
);
1436 return NT_STATUS_NO_SUCH_USER
;
1439 switch ( ldap_state
->schema_ver
) {
1440 case SCHEMAVER_SAMBASAMACCOUNT
:
1441 fstrcpy( objclass
, LDAP_OBJ_SAMBASAMACCOUNT
);
1444 case SCHEMAVER_SAMBAACCOUNT
:
1445 fstrcpy( objclass
, LDAP_OBJ_SAMBAACCOUNT
);
1448 fstrcpy( objclass
, "UNKNOWN" );
1449 DEBUG(0,("ldapsam_delete_sam_account: Unknown schema version specified!\n"));
1453 ret
= ldapsam_delete_entry(ldap_state
, result
, objclass
, attr_list
);
1454 ldap_msgfree(result
);
1455 free_attr_list( attr_list
);
1460 /**********************************************************************
1461 Helper function to determine for update_sam_account whether
1462 we need LDAP modification.
1463 *********************************************************************/
1465 static BOOL
element_is_changed(const SAM_ACCOUNT
*sampass
,
1466 enum pdb_elements element
)
1468 return IS_SAM_CHANGED(sampass
, element
);
1471 /**********************************************************************
1473 *********************************************************************/
1475 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* newpwd
)
1477 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1478 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1481 LDAPMessage
*result
= NULL
;
1482 LDAPMessage
*entry
= NULL
;
1483 LDAPMod
**mods
= NULL
;
1486 result
= pdb_get_backend_private_data(newpwd
, my_methods
);
1488 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1489 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1490 free_attr_list( attr_list
);
1491 if (rc
!= LDAP_SUCCESS
) {
1492 return NT_STATUS_UNSUCCESSFUL
;
1494 pdb_set_backend_private_data(newpwd
, result
, private_data_free_fn
, my_methods
, PDB_CHANGED
);
1497 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1498 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1499 return NT_STATUS_UNSUCCESSFUL
;
1502 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1503 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1505 return NT_STATUS_UNSUCCESSFUL
;
1508 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1510 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1511 element_is_changed
)) {
1512 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1515 ldap_mods_free(mods
,True
);
1516 return NT_STATUS_UNSUCCESSFUL
;
1520 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1521 pdb_get_username(newpwd
)));
1523 return NT_STATUS_OK
;
1526 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1527 ldap_mods_free(mods
,True
);
1530 if (!NT_STATUS_IS_OK(ret
)) {
1531 char *ld_error
= NULL
;
1532 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1534 DEBUG(0,("ldapsam_update_sam_account: failed to modify user with uid = %s, error: %s (%s)\n",
1535 pdb_get_username(newpwd
), ld_error
?ld_error
:"(unknwon)", ldap_err2string(rc
)));
1536 SAFE_FREE(ld_error
);
1540 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1541 pdb_get_username(newpwd
)));
1542 return NT_STATUS_OK
;
1545 /**********************************************************************
1546 Helper function to determine for update_sam_account whether
1547 we need LDAP modification.
1548 *********************************************************************/
1550 static BOOL
element_is_set_or_changed(const SAM_ACCOUNT
*sampass
,
1551 enum pdb_elements element
)
1553 return (IS_SAM_SET(sampass
, element
) ||
1554 IS_SAM_CHANGED(sampass
, element
));
1557 /**********************************************************************
1558 Add SAM_ACCOUNT to LDAP.
1559 *********************************************************************/
1561 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* newpwd
)
1563 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1564 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1566 LDAPMessage
*result
= NULL
;
1567 LDAPMessage
*entry
= NULL
;
1569 LDAPMod
**mods
= NULL
;
1570 int ldap_op
= LDAP_MOD_REPLACE
;
1574 const char *username
= pdb_get_username(newpwd
);
1575 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
1579 if (!username
|| !*username
) {
1580 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1581 return NT_STATUS_INVALID_PARAMETER
;
1584 /* free this list after the second search or in case we exit on failure */
1585 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1587 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
1589 if (rc
!= LDAP_SUCCESS
) {
1590 free_attr_list( attr_list
);
1591 return NT_STATUS_UNSUCCESSFUL
;
1594 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1595 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1597 ldap_msgfree(result
);
1598 free_attr_list( attr_list
);
1599 return NT_STATUS_UNSUCCESSFUL
;
1601 ldap_msgfree(result
);
1604 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
1605 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1607 if (rc
== LDAP_SUCCESS
) {
1608 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1609 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1610 sid_to_string(sid_string
, sid
)));
1611 free_attr_list( attr_list
);
1612 ldap_msgfree(result
);
1613 return NT_STATUS_UNSUCCESSFUL
;
1615 ldap_msgfree(result
);
1619 /* does the entry already exist but without a samba attributes?
1620 we need to return the samba attributes here */
1622 escape_user
= escape_ldap_string_alloc( username
);
1623 pstrcpy( filter
, lp_ldap_filter() );
1624 all_string_sub( filter
, "%u", escape_user
, sizeof(filter
) );
1625 SAFE_FREE( escape_user
);
1627 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1628 filter
, attr_list
, &result
);
1629 if ( rc
!= LDAP_SUCCESS
) {
1630 free_attr_list( attr_list
);
1631 return NT_STATUS_UNSUCCESSFUL
;
1634 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1636 if (num_result
> 1) {
1637 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1638 free_attr_list( attr_list
);
1639 ldap_msgfree(result
);
1640 return NT_STATUS_UNSUCCESSFUL
;
1643 /* Check if we need to update an existing entry */
1644 if (num_result
== 1) {
1647 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1648 ldap_op
= LDAP_MOD_REPLACE
;
1649 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1650 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1652 free_attr_list( attr_list
);
1653 ldap_msgfree(result
);
1654 return NT_STATUS_UNSUCCESSFUL
;
1656 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
1659 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
1661 /* There might be a SID for this account already - say an idmap entry */
1663 pstr_sprintf(filter
, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
1664 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1665 sid_to_string(sid_string
, sid
),
1666 LDAP_OBJ_IDMAP_ENTRY
,
1667 LDAP_OBJ_SID_ENTRY
);
1669 /* free old result before doing a new search */
1670 if (result
!= NULL
) {
1671 ldap_msgfree(result
);
1674 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1675 filter
, attr_list
, &result
);
1677 if ( rc
!= LDAP_SUCCESS
) {
1678 free_attr_list( attr_list
);
1679 return NT_STATUS_UNSUCCESSFUL
;
1682 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1684 if (num_result
> 1) {
1685 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1686 free_attr_list( attr_list
);
1687 ldap_msgfree(result
);
1688 return NT_STATUS_UNSUCCESSFUL
;
1691 /* Check if we need to update an existing entry */
1692 if (num_result
== 1) {
1695 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1696 ldap_op
= LDAP_MOD_REPLACE
;
1697 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1698 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1700 free_attr_list( attr_list
);
1701 ldap_msgfree(result
);
1702 return NT_STATUS_UNSUCCESSFUL
;
1704 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
1709 free_attr_list( attr_list
);
1711 if (num_result
== 0) {
1712 /* Check if we need to add an entry */
1713 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
1714 ldap_op
= LDAP_MOD_ADD
;
1715 if (username
[strlen(username
)-1] == '$') {
1716 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_machine_suffix ());
1718 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_user_suffix ());
1722 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1723 element_is_set_or_changed
)) {
1724 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
1725 ldap_msgfree(result
);
1727 ldap_mods_free(mods
,True
);
1728 return NT_STATUS_UNSUCCESSFUL
;
1731 ldap_msgfree(result
);
1734 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
1735 return NT_STATUS_UNSUCCESSFUL
;
1737 switch ( ldap_state
->schema_ver
) {
1738 case SCHEMAVER_SAMBAACCOUNT
:
1739 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
1741 case SCHEMAVER_SAMBASAMACCOUNT
:
1742 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
1745 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
1749 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
1750 if (!NT_STATUS_IS_OK(ret
)) {
1751 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
1752 pdb_get_username(newpwd
),dn
));
1753 ldap_mods_free(mods
, True
);
1757 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
1758 ldap_mods_free(mods
, True
);
1760 return NT_STATUS_OK
;
1763 /**********************************************************************
1764 *********************************************************************/
1766 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
1768 LDAPMessage
** result
)
1770 int scope
= LDAP_SCOPE_SUBTREE
;
1774 attr_list
= get_attr_list(groupmap_attr_list
);
1775 rc
= smbldap_search(ldap_state
->smbldap_state
,
1776 lp_ldap_group_suffix (), scope
,
1777 filter
, attr_list
, 0, result
);
1778 free_attr_list( attr_list
);
1780 if (rc
!= LDAP_SUCCESS
) {
1781 char *ld_error
= NULL
;
1782 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1784 DEBUG(0, ("ldapsam_search_one_group: "
1785 "Problem during the LDAP search: LDAP error: %s (%s)\n",
1786 ld_error
?ld_error
:"(unknown)", ldap_err2string(rc
)));
1787 DEBUGADD(3, ("ldapsam_search_one_group: Query was: %s, %s\n",
1788 lp_ldap_group_suffix(), filter
));
1789 SAFE_FREE(ld_error
);
1795 /**********************************************************************
1796 *********************************************************************/
1798 static BOOL
init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
1799 GROUP_MAP
*map
, LDAPMessage
*entry
)
1803 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
1804 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
1805 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
1809 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1810 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
), temp
)) {
1811 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1812 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
1815 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
1817 map
->gid
= (gid_t
)atol(temp
);
1819 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1820 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
), temp
)) {
1821 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1822 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
1826 if (!string_to_sid(&map
->sid
, temp
)) {
1827 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
1831 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1832 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
), temp
)) {
1833 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1834 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
1837 map
->sid_name_use
= (enum SID_NAME_USE
)atol(temp
);
1839 if ((map
->sid_name_use
< SID_NAME_USER
) ||
1840 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
1841 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
1845 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1846 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), temp
)) {
1848 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1849 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_CN
), temp
))
1851 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
1852 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
1856 fstrcpy(map
->nt_name
, temp
);
1858 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1859 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DESC
), temp
)) {
1862 fstrcpy(map
->comment
, temp
);
1867 /**********************************************************************
1868 *********************************************************************/
1870 static BOOL
init_ldap_from_group(LDAP
*ldap_struct
,
1871 LDAPMessage
*existing
,
1873 const GROUP_MAP
*map
)
1877 if (mods
== NULL
|| map
== NULL
) {
1878 DEBUG(0, ("init_ldap_from_group: NULL parameters found!\n"));
1884 sid_to_string(tmp
, &map
->sid
);
1886 smbldap_make_mod(ldap_struct
, existing
, mods
,
1887 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
), tmp
);
1888 pstr_sprintf(tmp
, "%i", map
->sid_name_use
);
1889 smbldap_make_mod(ldap_struct
, existing
, mods
,
1890 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
), tmp
);
1892 smbldap_make_mod(ldap_struct
, existing
, mods
,
1893 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), map
->nt_name
);
1894 smbldap_make_mod(ldap_struct
, existing
, mods
,
1895 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DESC
), map
->comment
);
1900 /**********************************************************************
1901 *********************************************************************/
1903 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
1907 struct ldapsam_privates
*ldap_state
=
1908 (struct ldapsam_privates
*)methods
->private_data
;
1909 LDAPMessage
*result
= NULL
;
1910 LDAPMessage
*entry
= NULL
;
1913 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
1915 return NT_STATUS_NO_SUCH_GROUP
;
1918 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1921 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
1922 ldap_msgfree(result
);
1923 return NT_STATUS_NO_SUCH_GROUP
;
1927 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: count=%d\n",
1929 ldap_msgfree(result
);
1930 return NT_STATUS_NO_SUCH_GROUP
;
1933 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1936 ldap_msgfree(result
);
1937 return NT_STATUS_UNSUCCESSFUL
;
1940 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
1941 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for group filter %s\n",
1943 ldap_msgfree(result
);
1944 return NT_STATUS_NO_SUCH_GROUP
;
1947 ldap_msgfree(result
);
1948 return NT_STATUS_OK
;
1951 /**********************************************************************
1952 *********************************************************************/
1954 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1959 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%s))",
1961 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
1962 sid_string_static(&sid
));
1964 return ldapsam_getgroup(methods
, filter
, map
);
1967 /**********************************************************************
1968 *********************************************************************/
1970 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1975 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%lu))",
1977 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
1978 (unsigned long)gid
);
1980 return ldapsam_getgroup(methods
, filter
, map
);
1983 /**********************************************************************
1984 *********************************************************************/
1986 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1990 char *escape_name
= escape_ldap_string_alloc(name
);
1993 return NT_STATUS_NO_MEMORY
;
1996 pstr_sprintf(filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
1998 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
1999 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
), escape_name
);
2001 SAFE_FREE(escape_name
);
2003 return ldapsam_getgroup(methods
, filter
, map
);
2006 /**********************************************************************
2007 *********************************************************************/
2009 static int ldapsam_search_one_group_by_gid(struct ldapsam_privates
*ldap_state
,
2011 LDAPMessage
**result
)
2015 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%lu))",
2016 LDAP_OBJ_POSIXGROUP
,
2017 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2018 (unsigned long)gid
);
2020 return ldapsam_search_one_group(ldap_state
, filter
, result
);
2023 /**********************************************************************
2024 *********************************************************************/
2026 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
2029 struct ldapsam_privates
*ldap_state
=
2030 (struct ldapsam_privates
*)methods
->private_data
;
2031 LDAPMessage
*result
= NULL
;
2032 LDAPMod
**mods
= NULL
;
2043 if (NT_STATUS_IS_OK(ldapsam_getgrgid(methods
, &dummy
,
2045 DEBUG(0, ("ldapsam_add_group_mapping_entry: Group %ld already exists in LDAP\n", (unsigned long)map
->gid
));
2046 return NT_STATUS_UNSUCCESSFUL
;
2049 rc
= ldapsam_search_one_group_by_gid(ldap_state
, map
->gid
, &result
);
2050 if (rc
!= LDAP_SUCCESS
) {
2051 ldap_msgfree(result
);
2052 return NT_STATUS_UNSUCCESSFUL
;
2055 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2058 ldap_msgfree(result
);
2059 return NT_STATUS_UNSUCCESSFUL
;
2063 DEBUG(2, ("ldapsam_add_group_mapping_entry: Group %lu must exist exactly once in LDAP\n",
2064 (unsigned long)map
->gid
));
2065 ldap_msgfree(result
);
2066 return NT_STATUS_UNSUCCESSFUL
;
2069 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2070 tmp
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
2072 ldap_msgfree(result
);
2073 return NT_STATUS_UNSUCCESSFUL
;
2078 if (!init_ldap_from_group(ldap_state
->smbldap_state
->ldap_struct
,
2079 result
, &mods
, map
)) {
2080 DEBUG(0, ("ldapsam_add_group_mapping_entry: init_ldap_from_group failed!\n"));
2081 ldap_mods_free(mods
, True
);
2082 ldap_msgfree(result
);
2083 return NT_STATUS_UNSUCCESSFUL
;
2086 ldap_msgfree(result
);
2089 DEBUG(0, ("ldapsam_add_group_mapping_entry: mods is empty\n"));
2090 return NT_STATUS_UNSUCCESSFUL
;
2093 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
2095 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2096 ldap_mods_free(mods
, True
);
2098 if (rc
!= LDAP_SUCCESS
) {
2099 char *ld_error
= NULL
;
2100 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
2102 DEBUG(0, ("ldapsam_add_group_mapping_entry: failed to add group %lu error: %s (%s)\n", (unsigned long)map
->gid
,
2103 ld_error
? ld_error
: "(unknown)", ldap_err2string(rc
)));
2104 SAFE_FREE(ld_error
);
2105 return NT_STATUS_UNSUCCESSFUL
;
2108 DEBUG(2, ("ldapsam_add_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map
->gid
));
2109 return NT_STATUS_OK
;
2112 /**********************************************************************
2113 *********************************************************************/
2115 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
2118 struct ldapsam_privates
*ldap_state
=
2119 (struct ldapsam_privates
*)methods
->private_data
;
2122 LDAPMessage
*result
= NULL
;
2123 LDAPMessage
*entry
= NULL
;
2124 LDAPMod
**mods
= NULL
;
2126 rc
= ldapsam_search_one_group_by_gid(ldap_state
, map
->gid
, &result
);
2128 if (rc
!= LDAP_SUCCESS
) {
2129 return NT_STATUS_UNSUCCESSFUL
;
2132 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
2133 DEBUG(0, ("ldapsam_update_group_mapping_entry: No group to modify!\n"));
2134 ldap_msgfree(result
);
2135 return NT_STATUS_UNSUCCESSFUL
;
2138 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2140 if (!init_ldap_from_group(ldap_state
->smbldap_state
->ldap_struct
,
2141 result
, &mods
, map
)) {
2142 DEBUG(0, ("ldapsam_update_group_mapping_entry: init_ldap_from_group failed\n"));
2143 ldap_msgfree(result
);
2145 ldap_mods_free(mods
,True
);
2146 return NT_STATUS_UNSUCCESSFUL
;
2150 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: nothing to do\n"));
2151 ldap_msgfree(result
);
2152 return NT_STATUS_OK
;
2155 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
2157 ldap_msgfree(result
);
2158 return NT_STATUS_UNSUCCESSFUL
;
2160 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2163 ldap_mods_free(mods
, True
);
2164 ldap_msgfree(result
);
2166 if (rc
!= LDAP_SUCCESS
) {
2167 char *ld_error
= NULL
;
2168 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
2170 DEBUG(0, ("ldapsam_update_group_mapping_entry: failed to modify group %lu error: %s (%s)\n", (unsigned long)map
->gid
,
2171 ld_error
? ld_error
: "(unknown)", ldap_err2string(rc
)));
2172 SAFE_FREE(ld_error
);
2173 return NT_STATUS_UNSUCCESSFUL
;
2176 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map
->gid
));
2177 return NT_STATUS_OK
;
2180 /**********************************************************************
2181 *********************************************************************/
2183 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
2186 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)methods
->private_data
;
2187 pstring sidstring
, filter
;
2188 LDAPMessage
*result
= NULL
;
2193 sid_to_string(sidstring
, &sid
);
2195 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%s))",
2196 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
, sidstring
);
2198 rc
= ldapsam_search_one_group(ldap_state
, filter
, &result
);
2200 if (rc
!= LDAP_SUCCESS
) {
2201 return NT_STATUS_NO_SUCH_GROUP
;
2204 attr_list
= get_attr_list( groupmap_attr_list_to_delete
);
2205 ret
= ldapsam_delete_entry(ldap_state
, result
, LDAP_OBJ_GROUPMAP
, attr_list
);
2206 free_attr_list ( attr_list
);
2208 ldap_msgfree(result
);
2213 /**********************************************************************
2214 *********************************************************************/
2216 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
, BOOL update
)
2218 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2223 pstr_sprintf( filter
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
2224 attr_list
= get_attr_list( groupmap_attr_list
);
2225 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
2226 LDAP_SCOPE_SUBTREE
, filter
,
2227 attr_list
, 0, &ldap_state
->result
);
2228 free_attr_list( attr_list
);
2230 if (rc
!= LDAP_SUCCESS
) {
2231 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n", ldap_err2string(rc
)));
2232 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n", lp_ldap_group_suffix(), filter
));
2233 ldap_msgfree(ldap_state
->result
);
2234 ldap_state
->result
= NULL
;
2235 return NT_STATUS_UNSUCCESSFUL
;
2238 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base!\n",
2239 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
2240 ldap_state
->result
)));
2242 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
);
2243 ldap_state
->index
= 0;
2245 return NT_STATUS_OK
;
2248 /**********************************************************************
2249 *********************************************************************/
2251 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
2253 ldapsam_endsampwent(my_methods
);
2256 /**********************************************************************
2257 *********************************************************************/
2259 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
2262 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2263 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2267 if (!ldap_state
->entry
)
2270 ldap_state
->index
++;
2271 bret
= init_group_from_ldap(ldap_state
, map
, ldap_state
->entry
);
2273 ldap_state
->entry
= ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
2277 return NT_STATUS_OK
;
2280 /**********************************************************************
2281 *********************************************************************/
2283 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
2284 enum SID_NAME_USE sid_name_use
,
2285 GROUP_MAP
**rmap
, int *num_entries
,
2295 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
2296 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open passdb\n"));
2297 return NT_STATUS_ACCESS_DENIED
;
2300 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
2301 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
2302 sid_name_use
!= map
.sid_name_use
) {
2303 DEBUG(11,("ldapsam_enum_group_mapping: group %s is not of the requested type\n", map
.nt_name
));
2306 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
2307 DEBUG(11,("ldapsam_enum_group_mapping: group %s is non mapped\n", map
.nt_name
));
2311 mapt
=(GROUP_MAP
*)Realloc((*rmap
), (entries
+1)*sizeof(GROUP_MAP
));
2313 DEBUG(0,("ldapsam_enum_group_mapping: Unable to enlarge group map!\n"));
2315 return NT_STATUS_UNSUCCESSFUL
;
2320 mapt
[entries
] = map
;
2325 ldapsam_endsamgrent(methods
);
2327 *num_entries
= entries
;
2329 return NT_STATUS_OK
;
2332 /**********************************************************************
2333 Privileges related functions
2334 *********************************************************************/
2336 static NTSTATUS
ldapsam_modify_sid_list_for_privilege(struct pdb_methods
*my_methods
, const char *privname
, const DOM_SID
*sid
, int ldap_op
)
2338 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2339 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2340 LDAPMessage
*entry
= NULL
;
2341 LDAPMod
**mods
= NULL
;
2344 char **attr_list
, *dn
;
2347 if ((sid
== NULL
) || (!sid_to_string(sid_str
, sid
))) {
2348 DEBUG(3, ("ldapsam_modify_sid_list_for_privilege: Invalid SID\n"));
2349 return NT_STATUS_INVALID_PARAMETER
;
2352 pstr_sprintf(filter
, "(&(objectclass=%s)(sambaPrivName=%s))", LDAP_OBJ_PRIVILEGE
, privname
);
2353 attr_list
= get_attr_list(privilege_attr_list
);
2354 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_privilege_suffix(),
2355 LDAP_SCOPE_SUBTREE
, filter
,
2356 attr_list
, 0, &ldap_state
->result
);
2357 free_attr_list(attr_list
);
2360 if (rc
!= LDAP_SUCCESS
) {
2361 DEBUG(0, ("ldapsam_modify_sid_list_for_privilege: LDAP search failed: %s\n", ldap_err2string(rc
)));
2362 DEBUG(3, ("ldapsam_modify_sid_list_for_privilege: Query was: %s, %s\n", lp_ldap_privilege_suffix(), filter
));
2363 ldap_msgfree(ldap_state
->result
);
2364 ldap_state
->result
= NULL
;
2368 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
) == 0) {
2369 /* if the privilege does not exist and we are adding then
2371 if (ldap_op
== LDAP_MOD_ADD
) {
2373 DEBUG(3, ("Privilege not found on ldap tree, creating a new entry\n"));
2374 if (asprintf(&dn
, "sambaPrivName=%s,%s", privname
, lp_ldap_privilege_suffix()) < 0) {
2375 DEBUG(0, ("ldapsam_modify_sid_list_for_privilege: Out of memory\n"));
2379 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaPrivName", privname
);
2381 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_PRIVILEGE
);
2383 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
2385 if (rc
!= LDAP_SUCCESS
) {
2386 char *ld_error
= NULL
;
2388 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
, &ld_error
);
2390 ("ldapsam_modify_sid_list_for_privilege:"
2391 "Failed to add privilege (%s) dn= %s with: %s\n\t%s\n",
2393 dn
, ldap_err2string(rc
),
2394 ld_error
? ld_error
: "unknown")
2397 SAFE_FREE(ld_error
);
2401 pstr_sprintf(filter
, "(&(objectclass=%s)(sambaPrivName=%s))", LDAP_OBJ_PRIVILEGE
, privname
);
2402 attr_list
= get_attr_list(privilege_attr_list
);
2403 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_privilege_suffix(),
2404 LDAP_SCOPE_SUBTREE
, filter
,
2405 attr_list
, 0, &ldap_state
->result
);
2406 free_attr_list(attr_list
);
2408 if (rc
!= LDAP_SUCCESS
) {
2409 DEBUG(0, ("ldapsam_modify_sid_list_for_privilege: LDAP search failed: %s\n", ldap_err2string(rc
)));
2410 DEBUG(3, ("ldapsam_modify_sid_list_for_privilege: Query was: %s, %s\n", lp_ldap_privilege_suffix(), filter
));
2411 ldap_msgfree(ldap_state
->result
);
2412 ldap_state
->result
= NULL
;
2420 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
);
2422 /* retrieve the dn */
2423 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
2428 /* prepare the modification */
2429 smbldap_set_mod(&mods
, ldap_op
, "sambaSIDList", sid_str
);
2431 /* modify the privilege */
2432 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2434 /* free used structures */
2435 ldap_mods_free(mods
, True
);
2437 if (rc
!= LDAP_SUCCESS
) {
2438 char *ld_error
= NULL
;
2440 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
, &ld_error
);
2442 ("ldapsam_modify_sid_list_for_privilege:"
2443 "Failed to %s sid for privilege (%s) dn= %s with: %s\n\t%s\n",
2444 (ldap_op
== LDAP_MOD_ADD
) ? "add" : "remove",
2446 dn
, ldap_err2string(rc
),
2447 ld_error
? ld_error
: "unknown")
2449 SAFE_FREE(ld_error
);
2459 static NTSTATUS
ldapsam_add_sid_to_privilege(struct pdb_methods
*my_methods
, const char *privname
, const DOM_SID
*sid
)
2461 return ldapsam_modify_sid_list_for_privilege(my_methods
, privname
, sid
, LDAP_MOD_ADD
);
2464 static NTSTATUS
ldapsam_remove_sid_from_privilege(struct pdb_methods
*my_methods
, const char *privname
, const DOM_SID
*sid
)
2466 return ldapsam_modify_sid_list_for_privilege(my_methods
, privname
, sid
, LDAP_MOD_DELETE
);
2469 static NTSTATUS
ldapsam_get_privilege_set(struct pdb_methods
*my_methods
, NT_USER_TOKEN
*token
, PRIVILEGE_SET
*privset
)
2471 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2472 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2473 LDAPMessage
*entry
= NULL
;
2480 sid_list
= (char **)malloc(sizeof(char *) * (token
->num_sids
+ 1));
2481 for (i
= 0; i
< token
->num_sids
; i
++) {
2482 sid_to_string(sid_str
, &token
->user_sids
[i
]);
2483 sid_list
[i
] = strdup(sid_str
);
2484 if ( ! sid_list
[i
]) {
2485 ret
= NT_STATUS_NO_MEMORY
;
2491 pstr_sprintf(filter
, "(objectclass=%s)", LDAP_OBJ_PRIVILEGE
);
2492 attr_list
= get_attr_list(privilege_attr_list
);
2493 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_privilege_suffix(),
2494 LDAP_SCOPE_SUBTREE
, filter
,
2495 attr_list
, 0, &ldap_state
->result
);
2496 free_attr_list(attr_list
);
2498 if (rc
!= LDAP_SUCCESS
) {
2499 DEBUG(0, ("ldapsam_get_privilege_set: LDAP search failed: %s\n", ldap_err2string(rc
)));
2500 DEBUG(3, ("ldapsam_get_privilege_set: Query was: %s, %s\n", lp_ldap_privilege_suffix(), filter
));
2501 ldap_msgfree(ldap_state
->result
);
2502 ldap_state
->result
= NULL
;
2506 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
) == 0) {
2507 DEBUG(3, ("ldapsam_get_privilege_set: No privileges in ldap tree\n"));
2512 DEBUG(2, ("ldapsam_get_privilege_set: %d entries in the base!\n",
2513 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
)));
2515 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
);
2517 while (entry
!= NULL
) {
2518 char **values
= NULL
;
2520 for(i
=0; sid_list
[i
] != NULL
; i
++) {
2525 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
, "sambaPrivName", privname
, sizeof(pstring
))) {
2529 if ((values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, LDAP_ATTRIBUTE_SID_LIST
)) == NULL
) {
2530 DEBUG(10, ("ldapsam_get_privilege_set: SID List not found skipping privilege\n"));
2535 while (values
[j
] != 0) {
2536 if (strcmp(values
[j
], sid_list
[i
]) == 0) {
2537 DEBUG(10, ("sid [%s] found in users sid list\n", sid_list
[i
]));
2538 DEBUG(10, ("adding privilege [%s] to the users privilege list\n", privname
));
2539 add_privilege_by_name(privset
, privname
);
2546 ldap_value_free(values
);
2552 ldap_value_free(values
);
2555 entry
= ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
, entry
);
2562 while (sid_list
[i
]) {
2571 static NTSTATUS
ldapsam_get_privilege_entry(struct pdb_methods
*my_methods
, const char *privname
,
2574 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2575 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2576 LDAPMessage
*entry
= NULL
;
2579 char **attr_list
, **values
;
2583 pstr_sprintf(filter
, "(&(objectclass=%s)(sambaPrivName=%s))", LDAP_OBJ_PRIVILEGE
, privname
);
2584 attr_list
= get_attr_list(privilege_attr_list
);
2585 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_privilege_suffix(),
2586 LDAP_SCOPE_SUBTREE
, filter
,
2587 attr_list
, 0, &ldap_state
->result
);
2588 free_attr_list(attr_list
);
2590 if (rc
!= LDAP_SUCCESS
) {
2591 DEBUG(0, ("ldapsam_get_privilege_entry: LDAP search failed: %s\n", ldap_err2string(rc
)));
2592 DEBUG(3, ("ldapsam_get_privilege_entry: Query was: %s, %s\n", lp_ldap_privilege_suffix(), filter
));
2593 ldap_msgfree(ldap_state
->result
);
2594 ldap_state
->result
= NULL
;
2595 ret
= NT_STATUS_UNSUCCESSFUL
;
2599 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
) == 0) {
2600 DEBUG(3, ("ldapsam_get_privilege_entry: No such privilege (%s) in ldap tree\n", privname
));
2604 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
);
2606 if ((values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, LDAP_ATTRIBUTE_SID_LIST
)) == NULL
) {
2607 DEBUG(10, ("ldapsam_get_privilege_entry: SID List not found skipping privilege\n"));
2612 for (i
= 0, len
= 0; values
[i
] != 0; i
++ ) {
2613 len
= len
+ strlen(values
[i
]) + 1;
2616 *sid_list
= (char *)malloc(len
);
2617 if ((*sid_list
) == NULL
) {
2618 DEBUG(0, ("ldapsam_get_privilege_entry: Out of memory!\n"));
2619 ldap_value_free(values
);
2620 ret
= NT_STATUS_NO_MEMORY
;
2624 (*sid_list
)[0] = '\0';
2626 for (i
= 0; values
[i
] != 0; i
++ ) {
2628 strlcat(*sid_list
, ",", len
);
2630 DEBUG(0, ("sid_list = [%s]\n", *sid_list
));
2631 DEBUG(0, ("values = [%s]\n", values
[i
]));
2632 DEBUG(0, ("len = [%d]\n", len
));
2633 strlcat(*sid_list
, values
[i
], len
);
2634 DEBUG(0, ("sid_list = [%s]\n", *sid_list
));
2637 ldap_value_free(values
);
2644 /**********************************************************************
2646 *********************************************************************/
2648 static void free_private_data(void **vp
)
2650 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
2652 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
2654 if ((*ldap_state
)->result
!= NULL
) {
2655 ldap_msgfree((*ldap_state
)->result
);
2656 (*ldap_state
)->result
= NULL
;
2661 /* No need to free any further, as it is talloc()ed */
2664 /**********************************************************************
2665 Intitalise the parts of the pdb_context that are common to all pdb_ldap modes
2666 *********************************************************************/
2668 static NTSTATUS
pdb_init_ldapsam_common(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
,
2669 const char *location
)
2672 struct ldapsam_privates
*ldap_state
;
2674 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_methods(pdb_context
->mem_ctx
, pdb_method
))) {
2678 (*pdb_method
)->name
= "ldapsam";
2680 (*pdb_method
)->setsampwent
= ldapsam_setsampwent
;
2681 (*pdb_method
)->endsampwent
= ldapsam_endsampwent
;
2682 (*pdb_method
)->getsampwent
= ldapsam_getsampwent
;
2683 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
2684 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
2685 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
2686 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
2687 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
2689 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
2690 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
2691 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
2692 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
2693 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
2694 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
2695 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
2697 (*pdb_method
)->add_sid_to_privilege
= ldapsam_add_sid_to_privilege
;
2698 (*pdb_method
)->remove_sid_from_privilege
= ldapsam_remove_sid_from_privilege
;
2699 (*pdb_method
)->get_privilege_set
= ldapsam_get_privilege_set
;
2700 (*pdb_method
)->get_privilege_entry
= ldapsam_get_privilege_entry
;
2702 /* TODO: Setup private data and free */
2704 ldap_state
= talloc_zero(pdb_context
->mem_ctx
, sizeof(*ldap_state
));
2706 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
2707 return NT_STATUS_NO_MEMORY
;
2710 if (!NT_STATUS_IS_OK(nt_status
=
2711 smbldap_init(pdb_context
->mem_ctx
, location
,
2712 &ldap_state
->smbldap_state
)));
2714 ldap_state
->domain_name
= talloc_strdup(pdb_context
->mem_ctx
, get_global_sam_name());
2715 if (!ldap_state
->domain_name
) {
2716 return NT_STATUS_NO_MEMORY
;
2719 (*pdb_method
)->private_data
= ldap_state
;
2721 (*pdb_method
)->free_private_data
= free_private_data
;
2723 return NT_STATUS_OK
;
2726 /**********************************************************************
2727 Initialise the 'compat' mode for pdb_ldap
2728 *********************************************************************/
2730 static NTSTATUS
pdb_init_ldapsam_compat(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
2733 struct ldapsam_privates
*ldap_state
;
2735 #ifdef WITH_LDAP_SAMCONFIG
2737 int ldap_port
= lp_ldap_port();
2739 /* remap default port if not using SSL (ie clear or TLS) */
2740 if ( (lp_ldap_ssl() != LDAP_SSL_ON
) && (ldap_port
== 636) ) {
2744 location
= talloc_asprintf(pdb_context
->mem_ctx
, "%s://%s:%d", lp_ldap_ssl() == LDAP_SSL_ON
? "ldaps" : "ldap", lp_ldap_server(), ldap_port
);
2746 return NT_STATUS_NO_MEMORY
;
2751 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_ldapsam_common(pdb_context
, pdb_method
, location
))) {
2755 (*pdb_method
)->name
= "ldapsam_compat";
2757 ldap_state
= (*pdb_method
)->private_data
;
2758 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
2760 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
2762 return NT_STATUS_OK
;
2765 /**********************************************************************
2766 Initialise the normal mode for pdb_ldap
2767 *********************************************************************/
2769 static NTSTATUS
pdb_init_ldapsam(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
2772 struct ldapsam_privates
*ldap_state
;
2773 uint32 alg_rid_base
;
2774 pstring alg_rid_base_string
;
2775 LDAPMessage
*result
= NULL
;
2776 LDAPMessage
*entry
= NULL
;
2777 DOM_SID ldap_domain_sid
;
2778 DOM_SID secrets_domain_sid
;
2779 pstring domain_sid_string
;
2781 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_ldapsam_common(pdb_context
, pdb_method
, location
))) {
2785 (*pdb_method
)->name
= "ldapsam";
2787 ldap_state
= (*pdb_method
)->private_data
;
2788 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
2790 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
2792 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
, &result
,
2793 ldap_state
->domain_name
, True
);
2795 if ( !NT_STATUS_IS_OK(nt_status
) ) {
2796 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain info, nor add one to the domain\n"));
2797 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, will be unable to allocate new users/groups, \
2798 and will risk BDCs having inconsistant SIDs\n"));
2799 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
2800 return NT_STATUS_OK
;
2803 /* Given that the above might fail, everything below this must be optional */
2805 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2807 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info entry\n"));
2808 ldap_msgfree(result
);
2809 return NT_STATUS_UNSUCCESSFUL
;
2812 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2813 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
2814 domain_sid_string
)) {
2816 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
2817 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be read as a valid SID\n", domain_sid_string
));
2818 return NT_STATUS_INVALID_PARAMETER
;
2820 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
, &secrets_domain_sid
);
2821 if (!found_sid
|| !sid_equal(&secrets_domain_sid
, &ldap_domain_sid
)) {
2822 fstring new_sid_str
, old_sid_str
;
2823 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain %s based on pdb_ldap results %s -> %s\n",
2824 ldap_state
->domain_name
,
2825 sid_to_string(old_sid_str
, &secrets_domain_sid
),
2826 sid_to_string(new_sid_str
, &ldap_domain_sid
)));
2828 /* reset secrets.tdb sid */
2829 secrets_store_domain_sid(ldap_state
->domain_name
, &ldap_domain_sid
);
2830 DEBUG(1, ("New global sam SID: %s\n", sid_to_string(new_sid_str
, get_global_sam_sid())));
2832 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
2835 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2836 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ALGORITHMIC_RID_BASE
),
2837 alg_rid_base_string
)) {
2838 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
2839 if (alg_rid_base
!= algorithmic_rid_base()) {
2840 DEBUG(0, ("The value of 'algorithmic RID base' has changed since the LDAP\n"
2841 "database was initialised. Aborting. \n"));
2842 ldap_msgfree(result
);
2843 return NT_STATUS_UNSUCCESSFUL
;
2846 ldap_msgfree(result
);
2848 return NT_STATUS_OK
;
2851 NTSTATUS
pdb_ldap_init(void)
2854 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
2857 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
2860 return NT_STATUS_OK
;