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
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
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/struct samu
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)
82 /**********************************************************************
83 Simple helper function to make stuff better readable
84 **********************************************************************/
86 static LDAP
*priv2ld(struct ldapsam_privates
*priv
)
88 return priv
->smbldap_state
->ldap_struct
;
91 /**********************************************************************
92 Get the attribute name given a user schame version.
93 **********************************************************************/
95 static const char* get_userattr_key2string( int schema_ver
, int key
)
97 switch ( schema_ver
) {
98 case SCHEMAVER_SAMBAACCOUNT
:
99 return get_attr_key2string( attrib_map_v22
, key
);
101 case SCHEMAVER_SAMBASAMACCOUNT
:
102 return get_attr_key2string( attrib_map_v30
, key
);
105 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
111 /**********************************************************************
112 Return the list of attribute names given a user schema version.
113 **********************************************************************/
115 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
117 switch ( schema_ver
) {
118 case SCHEMAVER_SAMBAACCOUNT
:
119 return get_attr_list( mem_ctx
, attrib_map_v22
);
121 case SCHEMAVER_SAMBASAMACCOUNT
:
122 return get_attr_list( mem_ctx
, attrib_map_v30
);
124 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
131 /**************************************************************************
132 Return the list of attribute names to delete given a user schema version.
133 **************************************************************************/
135 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
138 switch ( schema_ver
) {
139 case SCHEMAVER_SAMBAACCOUNT
:
140 return get_attr_list( mem_ctx
,
141 attrib_map_to_delete_v22
);
143 case SCHEMAVER_SAMBASAMACCOUNT
:
144 return get_attr_list( mem_ctx
,
145 attrib_map_to_delete_v30
);
147 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
155 /*******************************************************************
156 Generate the LDAP search filter for the objectclass based on the
157 version of the schema we are using.
158 ******************************************************************/
160 static const char* get_objclass_filter( int schema_ver
)
162 static fstring objclass_filter
;
164 switch( schema_ver
) {
165 case SCHEMAVER_SAMBAACCOUNT
:
166 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
168 case SCHEMAVER_SAMBASAMACCOUNT
:
169 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
172 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
176 return objclass_filter
;
179 /*****************************************************************
180 Scan a sequence number off OpenLDAP's syncrepl contextCSN
181 ******************************************************************/
183 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
185 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
186 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
187 LDAPMessage
*msg
= NULL
;
188 LDAPMessage
*entry
= NULL
;
190 char **values
= NULL
;
191 int rc
, num_result
, num_values
, rid
;
197 /* Unfortunatly there is no proper way to detect syncrepl-support in
198 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
199 * but do not show up in the root-DSE yet. Neither we can query the
200 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
201 * objectclass. Currently we require lp_ldap_suffix() to show up as
202 * namingContext. - Guenther
205 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
210 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
214 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
215 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
216 "as top-level namingContext\n", lp_ldap_suffix()));
220 mem_ctx
= talloc_init("ldapsam_get_seq_num");
223 return NT_STATUS_NO_MEMORY
;
225 if ((attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2)) == NULL
) {
226 ntstatus
= NT_STATUS_NO_MEMORY
;
230 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
231 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
234 /* consumer syncreplCookie: */
235 /* csn=20050126161620Z#0000001#00#00000 */
236 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
238 pstr_sprintf( suffix
, "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
242 /* provider contextCSN */
243 /* 20050126161620Z#000009#00#000000 */
244 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
246 pstr_sprintf( suffix
, "cn=ldapsync,%s", lp_ldap_suffix());
250 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
251 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
253 if (rc
!= LDAP_SUCCESS
) {
257 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
258 if (num_result
!= 1) {
259 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
263 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
265 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
269 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
270 if (values
== NULL
) {
271 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
275 num_values
= ldap_count_values(values
);
276 if (num_values
== 0) {
277 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
282 if (!next_token(&p
, tok
, "#", sizeof(tok
))) {
283 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
288 if (!strncmp(p
, "csn=", strlen("csn=")))
291 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
293 *seq_num
= generalized_to_unix_time(p
);
295 /* very basic sanity check */
297 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
302 ntstatus
= NT_STATUS_OK
;
306 ldap_value_free(values
);
310 talloc_destroy(mem_ctx
);
315 /*******************************************************************
316 Run the search by name.
317 ******************************************************************/
319 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
321 LDAPMessage
** result
,
325 char *escape_user
= escape_ldap_string_alloc(user
);
328 return LDAP_NO_MEMORY
;
332 * in the filter expression, replace %u with the real name
333 * so in ldap filter, %u MUST exist :-)
335 pstr_sprintf(filter
, "(&%s%s)", "(uid=%u)",
336 get_objclass_filter(ldap_state
->schema_ver
));
339 * have to use this here because $ is filtered out
344 all_string_sub(filter
, "%u", escape_user
, sizeof(pstring
));
345 SAFE_FREE(escape_user
);
347 return smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
350 /*******************************************************************
351 Run the search by rid.
352 ******************************************************************/
354 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
355 uint32 rid
, LDAPMessage
** result
,
361 pstr_sprintf(filter
, "(&(rid=%i)%s)", rid
,
362 get_objclass_filter(ldap_state
->schema_ver
));
364 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
369 /*******************************************************************
370 Run the search by SID.
371 ******************************************************************/
373 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
374 const DOM_SID
*sid
, LDAPMessage
** result
,
381 pstr_sprintf(filter
, "(&(%s=%s)%s)",
382 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
383 sid_to_string(sid_string
, sid
),
384 get_objclass_filter(ldap_state
->schema_ver
));
386 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
391 /*******************************************************************
392 Delete complete object or objectclass and attrs from
393 object found in search_result depending on lp_ldap_delete_dn
394 ******************************************************************/
396 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
399 const char *objectclass
,
402 LDAPMod
**mods
= NULL
;
405 BerElement
*ptr
= NULL
;
407 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
409 return LDAP_NO_MEMORY
;
412 if (lp_ldap_delete_dn()) {
413 return smbldap_delete(priv
->smbldap_state
, dn
);
416 /* Ok, delete only the SAM attributes */
418 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
420 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
423 /* We are only allowed to delete the attributes that
426 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
427 if (strequal(*attrib
, name
)) {
428 DEBUG(10, ("ldapsam_delete_entry: deleting "
429 "attribute %s\n", name
));
430 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
441 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
442 talloc_autofree_ldapmod(mem_ctx
, mods
);
444 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
447 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
452 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
453 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
457 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
458 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
466 /**********************************************************************
467 Initialize struct samu from an LDAP query.
468 (Based on init_sam_from_buffer in pdb_tdb.c)
469 *********************************************************************/
471 static BOOL
init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
472 struct samu
* sampass
,
479 pass_can_change_time
,
480 pass_must_change_time
,
493 char munged_dial
[2048];
495 uint8 smblmpwd
[LM_HASH_LEN
],
496 smbntpwd
[NT_HASH_LEN
];
497 BOOL use_samba_attrs
= True
;
498 uint32 acct_ctrl
= 0;
500 uint16 bad_password_count
= 0,
503 uint8 hours
[MAX_HOURS_LEN
];
505 LOGIN_CACHE
*cache_entry
= NULL
;
508 BOOL expand_explicit
= lp_passdb_expand_explicit();
511 * do a little initialization
515 nt_username
[0] = '\0';
519 logon_script
[0] = '\0';
520 profile_path
[0] = '\0';
522 munged_dial
[0] = '\0';
523 workstations
[0] = '\0';
526 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
527 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
531 if (priv2ld(ldap_state
) == NULL
) {
532 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
533 "ldap_struct is NULL!\n"));
537 if (!smbldap_get_single_pstring(priv2ld(ldap_state
), entry
, "uid",
539 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
544 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
546 pstrcpy(nt_username
, username
);
548 pstrcpy(domain
, ldap_state
->domain_name
);
550 pdb_set_username(sampass
, username
, PDB_SET
);
552 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
553 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
555 /* deal with different attributes between the schema first */
557 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
558 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
559 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
), temp
)) {
560 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
563 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
564 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
), temp
)) {
565 user_rid
= (uint32
)atol(temp
);
566 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
570 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
571 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
572 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
573 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
578 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
579 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
), temp
)) {
580 /* leave as default */
582 pass_last_set_time
= (time_t) atol(temp
);
583 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
, PDB_SET
);
586 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
587 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
)) {
588 /* leave as default */
590 logon_time
= (time_t) atol(temp
);
591 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
594 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
595 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
)) {
596 /* leave as default */
598 logoff_time
= (time_t) atol(temp
);
599 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
602 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
603 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
)) {
604 /* leave as default */
606 kickoff_time
= (time_t) atol(temp
);
607 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
610 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
611 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
)) {
612 /* leave as default */
614 pass_can_change_time
= (time_t) atol(temp
);
615 pdb_set_pass_can_change_time(sampass
, pass_can_change_time
, PDB_SET
);
618 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
619 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
)) {
620 /* leave as default */
622 pass_must_change_time
= (time_t) atol(temp
);
623 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, PDB_SET
);
626 /* recommend that 'gecos' and 'displayName' should refer to the same
627 * attribute OID. userFullName depreciated, only used by Samba
628 * primary rules of LDAP: don't make a new attribute when one is already defined
629 * that fits your needs; using cn then displayName rather than 'userFullName'
632 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
633 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
), fullname
)) {
634 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
635 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_CN
), fullname
)) {
636 /* leave as default */
638 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
641 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
644 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
645 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
), dir_drive
))
647 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
649 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
652 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
653 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
), homedir
))
655 pdb_set_homedir( sampass
,
656 talloc_sub_basic(sampass
, username
, domain
,
660 pstrcpy( tmpstring
, homedir
);
661 if (expand_explicit
) {
662 standard_sub_basic( username
, domain
, tmpstring
,
665 pdb_set_homedir(sampass
, tmpstring
, PDB_SET
);
668 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
669 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
), logon_script
))
671 pdb_set_logon_script( sampass
,
672 talloc_sub_basic(sampass
, username
, domain
,
676 pstrcpy( tmpstring
, logon_script
);
677 if (expand_explicit
) {
678 standard_sub_basic( username
, domain
, tmpstring
,
681 pdb_set_logon_script(sampass
, tmpstring
, PDB_SET
);
684 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
685 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
), profile_path
))
687 pdb_set_profile_path( sampass
,
688 talloc_sub_basic( sampass
, username
, domain
,
692 pstrcpy( tmpstring
, profile_path
);
693 if (expand_explicit
) {
694 standard_sub_basic( username
, domain
, tmpstring
,
697 pdb_set_profile_path(sampass
, tmpstring
, PDB_SET
);
700 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
701 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
), acct_desc
))
703 /* leave as default */
705 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
708 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
709 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
), workstations
)) {
710 /* leave as default */;
712 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
715 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
716 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
), munged_dial
, sizeof(munged_dial
))) {
717 /* leave as default */;
719 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
722 /* FIXME: hours stuff should be cleaner */
726 memset(hours
, 0xff, hours_len
);
728 if (ldap_state
->is_nds_ldap
) {
731 char clear_text_pw
[512];
733 /* Make call to Novell eDirectory ldap extension to get clear text password.
734 NOTE: This will only work if we have an SSL connection to eDirectory. */
735 user_dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
736 if (user_dn
!= NULL
) {
737 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username
, user_dn
));
739 pwd_len
= sizeof(clear_text_pw
);
740 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
741 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
742 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
746 ZERO_STRUCT(smblmpwd
);
747 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
751 ZERO_STRUCT(smbntpwd
);
752 use_samba_attrs
= False
;
758 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
762 if (use_samba_attrs
) {
763 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
764 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
), temp
)) {
765 /* leave as default */
767 pdb_gethexpwd(temp
, smblmpwd
);
768 memset((char *)temp
, '\0', strlen(temp
)+1);
769 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
))
771 ZERO_STRUCT(smblmpwd
);
774 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
775 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
), temp
)) {
776 /* leave as default */
778 pdb_gethexpwd(temp
, smbntpwd
);
779 memset((char *)temp
, '\0', strlen(temp
)+1);
780 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
))
782 ZERO_STRUCT(smbntpwd
);
788 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
790 uint8
*pwhist
= NULL
;
792 char history_string
[MAX_PW_HISTORY_LEN
*64];
794 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
796 if ((pwhist
= SMB_MALLOC_ARRAY(uint8
, pwHistLen
* PW_HISTORY_ENTRY_LEN
)) == NULL
){
797 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
800 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
802 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
803 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
804 history_string
, sizeof(history_string
))) {
805 /* leave as default - zeros */
807 BOOL hex_failed
= False
;
808 for (i
= 0; i
< pwHistLen
; i
++){
809 /* Get the 16 byte salt. */
810 if (!pdb_gethexpwd(&history_string
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
814 /* Get the 16 byte MD5 hash of salt+passwd. */
815 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
816 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
])) {
822 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
824 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
827 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
834 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
835 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
), temp
)) {
836 acct_ctrl
|= ACB_NORMAL
;
838 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
841 acct_ctrl
|= ACB_NORMAL
;
843 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
846 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
847 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
849 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
850 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_COUNT
), temp
)) {
851 /* leave as default */
853 bad_password_count
= (uint32
) atol(temp
);
854 pdb_set_bad_password_count(sampass
, bad_password_count
, PDB_SET
);
857 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
858 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_TIME
), temp
)) {
859 /* leave as default */
861 bad_password_time
= (time_t) atol(temp
);
862 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
866 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
867 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_COUNT
), temp
)) {
868 /* leave as default */
870 logon_count
= (uint32
) atol(temp
);
871 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
874 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
876 if(!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
877 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_HOURS
), temp
)) {
878 /* leave as default */
880 pdb_gethexhours(temp
, hours
);
881 memset((char *)temp
, '\0', strlen(temp
) +1);
882 pdb_set_hours(sampass
, hours
, PDB_SET
);
886 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
887 if (smbldap_get_single_pstring(priv2ld(ldap_state
), entry
,
888 "uidNumber", temp
)) {
889 /* We've got a uid, feed the cache */
890 uid_t uid
= strtoul(temp
, NULL
, 10);
891 store_uid_sid_cache(pdb_get_user_sid(sampass
), uid
);
895 /* check the timestamp of the cache vs ldap entry */
896 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
900 /* see if we have newer updates */
901 if (!(cache_entry
= login_cache_read(sampass
))) {
902 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
903 (unsigned int)pdb_get_bad_password_count(sampass
),
904 (unsigned int)pdb_get_bad_password_time(sampass
)));
908 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
909 (unsigned int)ldap_entry_time
, (unsigned int)cache_entry
->entry_timestamp
,
910 (unsigned int)cache_entry
->bad_password_time
));
912 if (ldap_entry_time
> cache_entry
->entry_timestamp
) {
913 /* cache is older than directory , so
914 we need to delete the entry but allow the
915 fields to be written out */
916 login_cache_delentry(sampass
);
919 pdb_set_acct_ctrl(sampass
,
920 pdb_get_acct_ctrl(sampass
) |
921 (cache_entry
->acct_ctrl
& ACB_AUTOLOCK
),
923 pdb_set_bad_password_count(sampass
,
924 cache_entry
->bad_password_count
,
926 pdb_set_bad_password_time(sampass
,
927 cache_entry
->bad_password_time
,
931 SAFE_FREE(cache_entry
);
935 /**********************************************************************
936 Initialize the ldap db from a struct samu. Called on update.
937 (Based on init_buffer_from_sam in pdb_tdb.c)
938 *********************************************************************/
940 static BOOL
init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
941 LDAPMessage
*existing
,
942 LDAPMod
*** mods
, struct samu
* sampass
,
943 BOOL (*need_update
)(const struct samu
*,
949 if (mods
== NULL
|| sampass
== NULL
) {
950 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
957 * took out adding "objectclass: sambaAccount"
958 * do this on a per-mod basis
960 if (need_update(sampass
, PDB_USERNAME
)) {
961 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
962 "uid", pdb_get_username(sampass
));
963 if (ldap_state
->is_nds_ldap
) {
964 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
965 "cn", pdb_get_username(sampass
));
966 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
967 "sn", pdb_get_username(sampass
));
971 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
973 /* only update the RID if we actually need to */
974 if (need_update(sampass
, PDB_USERSID
)) {
976 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
978 switch ( ldap_state
->schema_ver
) {
979 case SCHEMAVER_SAMBAACCOUNT
:
980 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
981 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
982 sid_string_static(user_sid
),
983 sid_string_static(&ldap_state
->domain_sid
)));
986 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
987 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
988 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
992 case SCHEMAVER_SAMBASAMACCOUNT
:
993 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
994 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
995 sid_to_string(sid_string
, user_sid
));
999 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1004 /* we don't need to store the primary group RID - so leaving it
1005 'free' to hang off the unix primary group makes life easier */
1007 if (need_update(sampass
, PDB_GROUPSID
)) {
1009 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
1011 switch ( ldap_state
->schema_ver
) {
1012 case SCHEMAVER_SAMBAACCOUNT
:
1013 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1014 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1015 sid_string_static(group_sid
),
1016 sid_string_static(&ldap_state
->domain_sid
)));
1020 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
1021 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1022 get_userattr_key2string(ldap_state
->schema_ver
,
1023 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1026 case SCHEMAVER_SAMBASAMACCOUNT
:
1027 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1028 get_userattr_key2string(ldap_state
->schema_ver
,
1029 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_string(sid_string
, group_sid
));
1033 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1039 /* displayName, cn, and gecos should all be the same
1040 * most easily accomplished by giving them the same OID
1041 * gecos isn't set here b/c it should be handled by the
1043 * We change displayName only and fall back to cn if
1044 * it does not exist.
1047 if (need_update(sampass
, PDB_FULLNAME
))
1048 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1049 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1050 pdb_get_fullname(sampass
));
1052 if (need_update(sampass
, PDB_ACCTDESC
))
1053 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1054 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1055 pdb_get_acct_desc(sampass
));
1057 if (need_update(sampass
, PDB_WORKSTATIONS
))
1058 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1059 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1060 pdb_get_workstations(sampass
));
1062 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1063 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1064 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1065 pdb_get_munged_dial(sampass
));
1067 if (need_update(sampass
, PDB_SMBHOME
))
1068 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1069 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1070 pdb_get_homedir(sampass
));
1072 if (need_update(sampass
, PDB_DRIVE
))
1073 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1074 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1075 pdb_get_dir_drive(sampass
));
1077 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1078 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1079 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1080 pdb_get_logon_script(sampass
));
1082 if (need_update(sampass
, PDB_PROFILE
))
1083 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1084 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1085 pdb_get_profile_path(sampass
));
1087 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logon_time(sampass
));
1088 if (need_update(sampass
, PDB_LOGONTIME
))
1089 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1090 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1092 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logoff_time(sampass
));
1093 if (need_update(sampass
, PDB_LOGOFFTIME
))
1094 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1095 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1097 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_kickoff_time(sampass
));
1098 if (need_update(sampass
, PDB_KICKOFFTIME
))
1099 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1100 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1102 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_can_change_time_noncalc(sampass
));
1103 if (need_update(sampass
, PDB_CANCHANGETIME
))
1104 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1105 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1107 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_must_change_time(sampass
));
1108 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1109 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1110 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1113 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1114 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1116 if (need_update(sampass
, PDB_LMPASSWD
)) {
1117 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1119 pdb_sethexpwd(temp
, lm_pw
,
1120 pdb_get_acct_ctrl(sampass
));
1121 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1122 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1125 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1126 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1130 if (need_update(sampass
, PDB_NTPASSWD
)) {
1131 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1133 pdb_sethexpwd(temp
, nt_pw
,
1134 pdb_get_acct_ctrl(sampass
));
1135 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1136 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1139 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1140 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1145 if (need_update(sampass
, PDB_PWHISTORY
)) {
1146 uint32 pwHistLen
= 0;
1147 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
1148 if (pwHistLen
== 0) {
1149 /* Remove any password history from the LDAP store. */
1150 memset(temp
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1154 uint32 currHistLen
= 0;
1155 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1156 if (pwhist
!= NULL
) {
1157 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1158 pwHistLen
= MIN(pwHistLen
, ((sizeof(temp
)-1)/64));
1159 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1160 /* Store the salt. */
1161 pdb_sethexpwd(&temp
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1162 /* Followed by the md5 hash of salt + md4 hash */
1163 pdb_sethexpwd(&temp
[(i
*64)+32],
1164 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1165 DEBUG(100, ("temp=%s\n", temp
));
1169 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1170 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1174 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1175 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_last_set_time(sampass
));
1176 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1177 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1182 if (need_update(sampass
, PDB_HOURS
)) {
1183 const uint8
*hours
= pdb_get_hours(sampass
);
1185 pdb_sethexhours(temp
, hours
);
1186 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1189 get_userattr_key2string(ldap_state
->schema_ver
,
1190 LDAP_ATTR_LOGON_HOURS
),
1195 if (need_update(sampass
, PDB_ACCTCTRL
))
1196 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1197 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1198 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1200 /* password lockout cache:
1201 - If we are now autolocking or clearing, we write to ldap
1202 - If we are clearing, we delete the cache entry
1203 - If the count is > 0, we update the cache
1205 This even means when autolocking, we cache, just in case the
1206 update doesn't work, and we have to cache the autolock flag */
1208 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1209 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1210 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1211 time_t badtime
= pdb_get_bad_password_time(sampass
);
1213 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, &pol
);
1215 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1216 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1218 if ((badcount
>= pol
) || (badcount
== 0)) {
1219 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1220 (unsigned int)badcount
, (unsigned int)badtime
));
1221 slprintf (temp
, sizeof (temp
) - 1, "%li", (long)badcount
);
1223 ldap_state
->smbldap_state
->ldap_struct
,
1225 get_userattr_key2string(
1226 ldap_state
->schema_ver
,
1227 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1230 slprintf (temp
, sizeof (temp
) - 1, "%li", badtime
);
1232 ldap_state
->smbldap_state
->ldap_struct
,
1234 get_userattr_key2string(
1235 ldap_state
->schema_ver
,
1236 LDAP_ATTR_BAD_PASSWORD_TIME
),
1239 if (badcount
== 0) {
1240 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1241 login_cache_delentry(sampass
);
1243 LOGIN_CACHE cache_entry
;
1245 cache_entry
.entry_timestamp
= time(NULL
);
1246 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1247 cache_entry
.bad_password_count
= badcount
;
1248 cache_entry
.bad_password_time
= badtime
;
1250 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1251 login_cache_write(sampass
, cache_entry
);
1258 /**********************************************************************
1259 Connect to LDAP server for password enumeration.
1260 *********************************************************************/
1262 static NTSTATUS
ldapsam_setsampwent(struct pdb_methods
*my_methods
, BOOL update
, uint32 acb_mask
)
1264 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1266 pstring filter
, suffix
;
1267 const char **attr_list
;
1268 BOOL machine_mask
= False
, user_mask
= False
;
1270 pstr_sprintf( filter
, "(&%s%s)", "(uid=%u)",
1271 get_objclass_filter(ldap_state
->schema_ver
));
1272 all_string_sub(filter
, "%u", "*", sizeof(pstring
));
1274 machine_mask
= ((acb_mask
!= 0) && (acb_mask
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)));
1275 user_mask
= ((acb_mask
!= 0) && (acb_mask
& ACB_NORMAL
));
1278 pstrcpy(suffix
, lp_ldap_machine_suffix());
1279 } else if (user_mask
) {
1280 pstrcpy(suffix
, lp_ldap_user_suffix());
1282 pstrcpy(suffix
, lp_ldap_suffix());
1285 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1288 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1289 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
, LDAP_SCOPE_SUBTREE
, filter
,
1290 attr_list
, 0, &ldap_state
->result
);
1291 TALLOC_FREE( attr_list
);
1293 if (rc
!= LDAP_SUCCESS
) {
1294 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc
)));
1295 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix
, filter
));
1296 ldap_msgfree(ldap_state
->result
);
1297 ldap_state
->result
= NULL
;
1298 return NT_STATUS_UNSUCCESSFUL
;
1301 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1302 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
1303 ldap_state
->result
), suffix
));
1305 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
1306 ldap_state
->result
);
1307 ldap_state
->index
= 0;
1309 return NT_STATUS_OK
;
1312 /**********************************************************************
1313 End enumeration of the LDAP password list.
1314 *********************************************************************/
1316 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1318 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1319 if (ldap_state
->result
) {
1320 ldap_msgfree(ldap_state
->result
);
1321 ldap_state
->result
= NULL
;
1325 /**********************************************************************
1326 Get the next entry in the LDAP password database.
1327 *********************************************************************/
1329 static NTSTATUS
ldapsam_getsampwent(struct pdb_methods
*my_methods
,
1332 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1333 struct ldapsam_privates
*ldap_state
=
1334 (struct ldapsam_privates
*)my_methods
->private_data
;
1338 if (!ldap_state
->entry
)
1341 ldap_state
->index
++;
1342 bret
= init_sam_from_ldap(ldap_state
, user
, ldap_state
->entry
);
1344 ldap_state
->entry
= ldap_next_entry(priv2ld(ldap_state
),
1348 return NT_STATUS_OK
;
1351 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1352 const char *new_attr
)
1356 if (new_attr
== NULL
) {
1360 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1364 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1366 SMB_ASSERT((*attr_list
) != NULL
);
1367 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1368 (*attr_list
)[i
+1] = NULL
;
1371 /**********************************************************************
1372 Get struct samu entry from LDAP by username.
1373 *********************************************************************/
1375 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1377 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1378 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1379 LDAPMessage
*result
= NULL
;
1380 LDAPMessage
*entry
= NULL
;
1382 const char ** attr_list
;
1385 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1386 append_attr(user
, &attr_list
,
1387 get_userattr_key2string(ldap_state
->schema_ver
,
1388 LDAP_ATTR_MOD_TIMESTAMP
));
1389 append_attr(user
, &attr_list
, "uidNumber");
1390 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1392 TALLOC_FREE( attr_list
);
1394 if ( rc
!= LDAP_SUCCESS
)
1395 return NT_STATUS_NO_SUCH_USER
;
1397 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1400 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1401 ldap_msgfree(result
);
1402 return NT_STATUS_NO_SUCH_USER
;
1403 } else if (count
> 1) {
1404 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1405 ldap_msgfree(result
);
1406 return NT_STATUS_NO_SUCH_USER
;
1409 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1411 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1412 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1413 ldap_msgfree(result
);
1414 return NT_STATUS_NO_SUCH_USER
;
1416 pdb_set_backend_private_data(user
, result
, NULL
,
1417 my_methods
, PDB_CHANGED
);
1418 talloc_autofree_ldapmsg(user
, result
);
1421 ldap_msgfree(result
);
1426 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1427 const DOM_SID
*sid
, LDAPMessage
**result
)
1430 const char ** attr_list
;
1433 switch ( ldap_state
->schema_ver
) {
1434 case SCHEMAVER_SAMBASAMACCOUNT
: {
1435 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1436 if (tmp_ctx
== NULL
) {
1437 return LDAP_NO_MEMORY
;
1440 attr_list
= get_userattr_list(tmp_ctx
,
1441 ldap_state
->schema_ver
);
1442 append_attr(tmp_ctx
, &attr_list
,
1443 get_userattr_key2string(
1444 ldap_state
->schema_ver
,
1445 LDAP_ATTR_MOD_TIMESTAMP
));
1446 append_attr(tmp_ctx
, &attr_list
, "uidNumber");
1447 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1449 TALLOC_FREE(tmp_ctx
);
1451 if ( rc
!= LDAP_SUCCESS
)
1456 case SCHEMAVER_SAMBAACCOUNT
:
1457 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1461 attr_list
= get_userattr_list(NULL
,
1462 ldap_state
->schema_ver
);
1463 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1464 TALLOC_FREE( attr_list
);
1466 if ( rc
!= LDAP_SUCCESS
)
1473 /**********************************************************************
1474 Get struct samu entry from LDAP by SID.
1475 *********************************************************************/
1477 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1479 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1480 LDAPMessage
*result
= NULL
;
1481 LDAPMessage
*entry
= NULL
;
1486 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1488 if (rc
!= LDAP_SUCCESS
)
1489 return NT_STATUS_NO_SUCH_USER
;
1491 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1494 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string
, sid
),
1496 ldap_msgfree(result
);
1497 return NT_STATUS_NO_SUCH_USER
;
1498 } else if (count
> 1) {
1499 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string
, sid
),
1501 ldap_msgfree(result
);
1502 return NT_STATUS_NO_SUCH_USER
;
1505 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1507 ldap_msgfree(result
);
1508 return NT_STATUS_NO_SUCH_USER
;
1511 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1512 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1513 ldap_msgfree(result
);
1514 return NT_STATUS_NO_SUCH_USER
;
1517 pdb_set_backend_private_data(user
, result
, NULL
,
1518 my_methods
, PDB_CHANGED
);
1519 talloc_autofree_ldapmsg(user
, result
);
1520 return NT_STATUS_OK
;
1523 /********************************************************************
1524 Do the actual modification - also change a plaintext passord if
1526 **********************************************************************/
1528 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1529 struct samu
*newpwd
, char *dn
,
1530 LDAPMod
**mods
, int ldap_op
,
1531 BOOL (*need_update
)(const struct samu
*, enum pdb_elements
))
1533 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1536 if (!newpwd
|| !dn
) {
1537 return NT_STATUS_INVALID_PARAMETER
;
1541 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1542 /* may be password change below however */
1546 if (ldap_state
->is_nds_ldap
) {
1547 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1551 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1555 rc
= smbldap_add(ldap_state
->smbldap_state
,
1558 case LDAP_MOD_REPLACE
:
1559 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1563 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1565 return NT_STATUS_INVALID_PARAMETER
;
1568 if (rc
!=LDAP_SUCCESS
) {
1569 return NT_STATUS_UNSUCCESSFUL
;
1573 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1574 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1575 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1576 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1579 char *retoid
= NULL
;
1580 struct berval
*retdata
= NULL
;
1581 char *utf8_password
;
1584 if (!ldap_state
->is_nds_ldap
) {
1586 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1587 LDAP_EXOP_MODIFY_PASSWD
)) {
1588 DEBUG(2, ("ldap password change requested, but LDAP "
1589 "server does not support it -- ignoring\n"));
1590 return NT_STATUS_OK
;
1594 if (push_utf8_allocate(&utf8_password
, pdb_get_plaintext_passwd(newpwd
)) == (size_t)-1) {
1595 return NT_STATUS_NO_MEMORY
;
1598 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1599 SAFE_FREE(utf8_password
);
1600 return NT_STATUS_NO_MEMORY
;
1603 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1604 DEBUG(0,("ber_alloc_t returns NULL\n"));
1605 SAFE_FREE(utf8_password
);
1607 return NT_STATUS_UNSUCCESSFUL
;
1610 ber_printf (ber
, "{");
1611 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
, utf8_dn
);
1612 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
, utf8_password
);
1613 ber_printf (ber
, "n}");
1615 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1616 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1619 SAFE_FREE(utf8_password
);
1620 return NT_STATUS_UNSUCCESSFUL
;
1624 SAFE_FREE(utf8_password
);
1627 if (!ldap_state
->is_nds_ldap
) {
1628 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1629 LDAP_EXOP_MODIFY_PASSWD
,
1630 bv
, NULL
, NULL
, &retoid
,
1633 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1634 pdb_get_plaintext_passwd(newpwd
));
1636 if (rc
!= LDAP_SUCCESS
) {
1637 char *ld_error
= NULL
;
1639 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1640 DEBUG(3, ("Could not set userPassword "
1641 "attribute due to an objectClass "
1642 "violation -- ignoring\n"));
1644 return NT_STATUS_OK
;
1647 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1649 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1650 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1651 SAFE_FREE(ld_error
);
1653 return NT_STATUS_UNSUCCESSFUL
;
1655 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1656 #ifdef DEBUG_PASSWORD
1657 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1660 ber_bvfree(retdata
);
1662 ldap_memfree(retoid
);
1666 return NT_STATUS_OK
;
1669 /**********************************************************************
1670 Delete entry from LDAP for username.
1671 *********************************************************************/
1673 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1674 struct samu
* sam_acct
)
1676 struct ldapsam_privates
*priv
=
1677 (struct ldapsam_privates
*)my_methods
->private_data
;
1680 LDAPMessage
*msg
, *entry
;
1681 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1682 const char **attr_list
;
1683 TALLOC_CTX
*mem_ctx
;
1686 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1687 return NT_STATUS_INVALID_PARAMETER
;
1690 sname
= pdb_get_username(sam_acct
);
1692 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1695 mem_ctx
= talloc_new(NULL
);
1696 if (mem_ctx
== NULL
) {
1697 DEBUG(0, ("talloc_new failed\n"));
1701 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1702 if (attr_list
== NULL
) {
1706 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1708 if ((rc
!= LDAP_SUCCESS
) ||
1709 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1710 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1711 DEBUG(5, ("Could not find user %s\n", sname
));
1712 result
= NT_STATUS_NO_SUCH_USER
;
1716 rc
= ldapsam_delete_entry(
1717 priv
, mem_ctx
, entry
,
1718 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1719 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1722 result
= (rc
== LDAP_SUCCESS
) ?
1723 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1726 TALLOC_FREE(mem_ctx
);
1730 /**********************************************************************
1731 Helper function to determine for update_sam_account whether
1732 we need LDAP modification.
1733 *********************************************************************/
1735 static BOOL
element_is_changed(const struct samu
*sampass
,
1736 enum pdb_elements element
)
1738 return IS_SAM_CHANGED(sampass
, element
);
1741 /**********************************************************************
1743 *********************************************************************/
1745 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1747 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1748 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1751 LDAPMessage
*result
= NULL
;
1752 LDAPMessage
*entry
= NULL
;
1753 LDAPMod
**mods
= NULL
;
1754 const char **attr_list
;
1756 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1758 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1759 if (pdb_get_username(newpwd
) == NULL
) {
1760 return NT_STATUS_INVALID_PARAMETER
;
1762 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1763 TALLOC_FREE( attr_list
);
1764 if (rc
!= LDAP_SUCCESS
) {
1765 return NT_STATUS_UNSUCCESSFUL
;
1767 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1768 my_methods
, PDB_CHANGED
);
1769 talloc_autofree_ldapmsg(newpwd
, result
);
1772 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1773 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1774 return NT_STATUS_UNSUCCESSFUL
;
1777 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1778 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1780 return NT_STATUS_UNSUCCESSFUL
;
1783 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1785 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1786 element_is_changed
)) {
1787 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1790 ldap_mods_free(mods
,True
);
1791 return NT_STATUS_UNSUCCESSFUL
;
1795 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1796 pdb_get_username(newpwd
)));
1798 return NT_STATUS_OK
;
1801 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1802 ldap_mods_free(mods
,True
);
1806 * We need to set the backend private data to NULL here. For example
1807 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1808 * same one, and with the explicit delete / add logic for attribute
1809 * values the second time we would use the wrong "old" value which
1810 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1812 * The existing LDAPMessage is still being auto-freed by the
1815 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
1818 if (!NT_STATUS_IS_OK(ret
)) {
1822 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1823 pdb_get_username(newpwd
)));
1824 return NT_STATUS_OK
;
1827 /***************************************************************************
1828 Renames a struct samu
1829 - The "rename user script" has full responsibility for changing everything
1830 ***************************************************************************/
1832 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1833 struct samu
*old_acct
,
1834 const char *newname
)
1836 const char *oldname
;
1838 pstring rename_script
;
1839 fstring oldname_lower
, newname_lower
;
1842 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1843 return NT_STATUS_INVALID_PARAMETER
;
1846 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1847 return NT_STATUS_INVALID_PARAMETER
;
1850 oldname
= pdb_get_username(old_acct
);
1852 /* rename the posix user */
1853 pstrcpy(rename_script
, lp_renameuser_script());
1855 if (!(*rename_script
))
1856 return NT_STATUS_ACCESS_DENIED
;
1858 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1861 /* We have to allow the account name to end with a '$'.
1862 Also, follow the semantics in _samr_create_user() and lower case the
1863 posix name but preserve the case in passdb */
1865 fstrcpy( oldname_lower
, oldname
);
1866 strlower_m( oldname_lower
);
1867 fstrcpy( newname_lower
, newname
);
1868 strlower_m( newname_lower
);
1869 string_sub2(rename_script
, "%unew", newname_lower
, sizeof(pstring
),
1871 string_sub2(rename_script
, "%uold", oldname_lower
, sizeof(pstring
),
1873 rc
= smbrun(rename_script
, NULL
);
1875 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
1876 rename_script
, rc
));
1879 smb_nscd_flush_user_cache();
1883 return NT_STATUS_UNSUCCESSFUL
;
1885 return NT_STATUS_OK
;
1888 /**********************************************************************
1889 Helper function to determine for update_sam_account whether
1890 we need LDAP modification.
1891 *********************************************************************/
1893 static BOOL
element_is_set_or_changed(const struct samu
*sampass
,
1894 enum pdb_elements element
)
1896 return (IS_SAM_SET(sampass
, element
) ||
1897 IS_SAM_CHANGED(sampass
, element
));
1900 /**********************************************************************
1901 Add struct samu to LDAP.
1902 *********************************************************************/
1904 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1906 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1907 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1909 LDAPMessage
*result
= NULL
;
1910 LDAPMessage
*entry
= NULL
;
1912 LDAPMod
**mods
= NULL
;
1913 int ldap_op
= LDAP_MOD_REPLACE
;
1915 const char **attr_list
;
1917 const char *username
= pdb_get_username(newpwd
);
1918 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
1922 if (!username
|| !*username
) {
1923 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1924 return NT_STATUS_INVALID_PARAMETER
;
1927 /* free this list after the second search or in case we exit on failure */
1928 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1930 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
1932 if (rc
!= LDAP_SUCCESS
) {
1933 TALLOC_FREE( attr_list
);
1934 return NT_STATUS_UNSUCCESSFUL
;
1937 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1938 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1940 ldap_msgfree(result
);
1941 TALLOC_FREE( attr_list
);
1942 return NT_STATUS_UNSUCCESSFUL
;
1944 ldap_msgfree(result
);
1947 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
1948 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1950 if (rc
== LDAP_SUCCESS
) {
1951 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1952 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1953 sid_to_string(sid_string
, sid
)));
1954 TALLOC_FREE( attr_list
);
1955 ldap_msgfree(result
);
1956 return NT_STATUS_UNSUCCESSFUL
;
1958 ldap_msgfree(result
);
1962 /* does the entry already exist but without a samba attributes?
1963 we need to return the samba attributes here */
1965 escape_user
= escape_ldap_string_alloc( username
);
1966 pstrcpy( filter
, "(uid=%u)" );
1967 all_string_sub( filter
, "%u", escape_user
, sizeof(filter
) );
1968 SAFE_FREE( escape_user
);
1970 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1971 filter
, attr_list
, &result
);
1972 if ( rc
!= LDAP_SUCCESS
) {
1973 TALLOC_FREE( attr_list
);
1974 return NT_STATUS_UNSUCCESSFUL
;
1977 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1979 if (num_result
> 1) {
1980 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1981 TALLOC_FREE( attr_list
);
1982 ldap_msgfree(result
);
1983 return NT_STATUS_UNSUCCESSFUL
;
1986 /* Check if we need to update an existing entry */
1987 if (num_result
== 1) {
1990 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1991 ldap_op
= LDAP_MOD_REPLACE
;
1992 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1993 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1995 TALLOC_FREE( attr_list
);
1996 ldap_msgfree(result
);
1997 return NT_STATUS_UNSUCCESSFUL
;
1999 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
2002 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2004 /* There might be a SID for this account already - say an idmap entry */
2006 pstr_sprintf(filter
, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2007 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
2008 sid_to_string(sid_string
, sid
),
2009 LDAP_OBJ_IDMAP_ENTRY
,
2010 LDAP_OBJ_SID_ENTRY
);
2012 /* free old result before doing a new search */
2013 if (result
!= NULL
) {
2014 ldap_msgfree(result
);
2017 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2018 filter
, attr_list
, &result
);
2020 if ( rc
!= LDAP_SUCCESS
) {
2021 TALLOC_FREE( attr_list
);
2022 return NT_STATUS_UNSUCCESSFUL
;
2025 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2027 if (num_result
> 1) {
2028 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2029 TALLOC_FREE( attr_list
);
2030 ldap_msgfree(result
);
2031 return NT_STATUS_UNSUCCESSFUL
;
2034 /* Check if we need to update an existing entry */
2035 if (num_result
== 1) {
2038 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2039 ldap_op
= LDAP_MOD_REPLACE
;
2040 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2041 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
2043 TALLOC_FREE( attr_list
);
2044 ldap_msgfree(result
);
2045 return NT_STATUS_UNSUCCESSFUL
;
2047 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
2052 TALLOC_FREE( attr_list
);
2054 if (num_result
== 0) {
2055 char *escape_username
;
2056 /* Check if we need to add an entry */
2057 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2058 ldap_op
= LDAP_MOD_ADD
;
2060 escape_username
= escape_rdn_val_string_alloc(username
);
2061 if (!escape_username
) {
2062 DEBUG(0, ("Out of memory!\n"));
2063 ldap_msgfree(result
);
2064 return NT_STATUS_NO_MEMORY
;
2067 if (username
[strlen(username
)-1] == '$') {
2068 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", escape_username
, lp_ldap_machine_suffix ());
2070 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", escape_username
, lp_ldap_user_suffix ());
2073 SAFE_FREE(escape_username
);
2076 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2077 element_is_set_or_changed
)) {
2078 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2079 ldap_msgfree(result
);
2081 ldap_mods_free(mods
,True
);
2082 return NT_STATUS_UNSUCCESSFUL
;
2085 ldap_msgfree(result
);
2088 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2089 return NT_STATUS_UNSUCCESSFUL
;
2091 switch ( ldap_state
->schema_ver
) {
2092 case SCHEMAVER_SAMBAACCOUNT
:
2093 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2095 case SCHEMAVER_SAMBASAMACCOUNT
:
2096 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2099 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2103 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2104 if (!NT_STATUS_IS_OK(ret
)) {
2105 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2106 pdb_get_username(newpwd
),dn
));
2107 ldap_mods_free(mods
, True
);
2111 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2112 ldap_mods_free(mods
, True
);
2114 return NT_STATUS_OK
;
2117 /**********************************************************************
2118 *********************************************************************/
2120 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2122 LDAPMessage
** result
)
2124 int scope
= LDAP_SCOPE_SUBTREE
;
2126 const char **attr_list
;
2128 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2129 rc
= smbldap_search(ldap_state
->smbldap_state
,
2130 lp_ldap_group_suffix (), scope
,
2131 filter
, attr_list
, 0, result
);
2132 TALLOC_FREE(attr_list
);
2137 /**********************************************************************
2138 *********************************************************************/
2140 static BOOL
init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2141 GROUP_MAP
*map
, LDAPMessage
*entry
)
2145 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2146 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2147 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2151 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2152 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
), temp
)) {
2153 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2154 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2157 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2159 map
->gid
= (gid_t
)atol(temp
);
2161 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2162 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
), temp
)) {
2163 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2164 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2168 if (!string_to_sid(&map
->sid
, temp
)) {
2169 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2173 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2174 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
), temp
)) {
2175 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2176 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2179 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2181 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2182 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2183 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2187 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2188 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), temp
)) {
2190 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2191 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_CN
), temp
))
2193 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2194 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2198 fstrcpy(map
->nt_name
, temp
);
2200 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2201 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DESC
), temp
)) {
2204 fstrcpy(map
->comment
, temp
);
2206 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
2207 store_gid_sid_cache(&map
->sid
, map
->gid
);
2213 /**********************************************************************
2214 *********************************************************************/
2216 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2220 struct ldapsam_privates
*ldap_state
=
2221 (struct ldapsam_privates
*)methods
->private_data
;
2222 LDAPMessage
*result
= NULL
;
2223 LDAPMessage
*entry
= NULL
;
2226 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2228 return NT_STATUS_NO_SUCH_GROUP
;
2231 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2234 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2235 ldap_msgfree(result
);
2236 return NT_STATUS_NO_SUCH_GROUP
;
2240 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2241 "count=%d\n", filter
, count
));
2242 ldap_msgfree(result
);
2243 return NT_STATUS_NO_SUCH_GROUP
;
2246 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2249 ldap_msgfree(result
);
2250 return NT_STATUS_UNSUCCESSFUL
;
2253 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2254 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2255 "group filter %s\n", filter
));
2256 ldap_msgfree(result
);
2257 return NT_STATUS_NO_SUCH_GROUP
;
2260 ldap_msgfree(result
);
2261 return NT_STATUS_OK
;
2264 /**********************************************************************
2265 *********************************************************************/
2267 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2272 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%s))",
2274 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2275 sid_string_static(&sid
));
2277 return ldapsam_getgroup(methods
, filter
, map
);
2280 /**********************************************************************
2281 *********************************************************************/
2283 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2288 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%lu))",
2290 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2291 (unsigned long)gid
);
2293 return ldapsam_getgroup(methods
, filter
, map
);
2296 /**********************************************************************
2297 *********************************************************************/
2299 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2303 char *escape_name
= escape_ldap_string_alloc(name
);
2306 return NT_STATUS_NO_MEMORY
;
2309 pstr_sprintf(filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2311 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2312 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
), escape_name
);
2314 SAFE_FREE(escape_name
);
2316 return ldapsam_getgroup(methods
, filter
, map
);
2319 static BOOL
ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2321 const DOM_SID
*domain_sid
,
2327 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2328 str
, sizeof(str
)-1)) {
2329 DEBUG(10, ("Could not find sambaSID attribute\n"));
2333 if (!string_to_sid(&sid
, str
)) {
2334 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2338 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2339 DEBUG(10, ("SID %s is not in expected domain %s\n",
2340 str
, sid_string_static(domain_sid
)));
2344 if (!sid_peek_rid(&sid
, rid
)) {
2345 DEBUG(10, ("Could not peek into RID\n"));
2352 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2353 TALLOC_CTX
*mem_ctx
,
2354 const DOM_SID
*group
,
2355 uint32
**pp_member_rids
,
2356 size_t *p_num_members
)
2358 struct ldapsam_privates
*ldap_state
=
2359 (struct ldapsam_privates
*)methods
->private_data
;
2360 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2361 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2362 const char *sid_attrs
[] = { "sambaSID", NULL
};
2363 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2364 LDAPMessage
*result
= NULL
;
2367 char **values
= NULL
;
2372 *pp_member_rids
= NULL
;
2375 filter
= talloc_asprintf(mem_ctx
,
2376 "(&(objectClass=%s)"
2379 LDAP_OBJ_POSIXGROUP
,
2381 sid_string_static(group
));
2382 if (filter
== NULL
) {
2383 ret
= NT_STATUS_NO_MEMORY
;
2387 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2388 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2391 if (rc
!= LDAP_SUCCESS
)
2394 talloc_autofree_ldapmsg(mem_ctx
, result
);
2396 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2399 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2400 sid_string_static(group
)));
2401 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2406 ret
= NT_STATUS_NO_SUCH_GROUP
;
2410 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2414 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2416 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2417 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2421 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2425 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2426 if (filter
== NULL
) {
2427 ret
= NT_STATUS_NO_MEMORY
;
2431 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2432 char *escape_memberuid
;
2434 escape_memberuid
= escape_ldap_string_alloc(*memberuid
);
2435 if (escape_memberuid
== NULL
) {
2436 ret
= NT_STATUS_NO_MEMORY
;
2440 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2441 if (filter
== NULL
) {
2442 SAFE_FREE(escape_memberuid
);
2443 ret
= NT_STATUS_NO_MEMORY
;
2447 SAFE_FREE(escape_memberuid
);
2450 filter
= talloc_asprintf_append_buffer(filter
, "))");
2451 if (filter
== NULL
) {
2452 ret
= NT_STATUS_NO_MEMORY
;
2456 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2457 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2460 if (rc
!= LDAP_SUCCESS
)
2463 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2464 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2466 talloc_autofree_ldapmsg(mem_ctx
, result
);
2468 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2470 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2476 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2480 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2481 "the sambaSID attribute\n"));
2482 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2486 if (!string_to_sid(&sid
, sidstr
))
2489 if (!sid_check_is_in_our_domain(&sid
)) {
2490 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2491 "in our domain\n"));
2492 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2496 sid_peek_rid(&sid
, &rid
);
2498 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2500 ret
= NT_STATUS_NO_MEMORY
;
2506 filter
= talloc_asprintf(mem_ctx
,
2507 "(&(objectClass=%s)"
2509 LDAP_OBJ_SAMBASAMACCOUNT
,
2512 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2513 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2516 if (rc
!= LDAP_SUCCESS
)
2519 talloc_autofree_ldapmsg(mem_ctx
, result
);
2521 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2523 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2527 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2529 get_global_sam_sid(),
2531 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2532 "the sambaSID attribute\n"));
2533 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2537 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2539 ret
= NT_STATUS_NO_MEMORY
;
2549 ldap_value_free(values
);
2554 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2555 TALLOC_CTX
*mem_ctx
,
2559 size_t *p_num_groups
)
2561 struct ldapsam_privates
*ldap_state
=
2562 (struct ldapsam_privates
*)methods
->private_data
;
2563 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2565 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2568 LDAPMessage
*result
= NULL
;
2570 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2571 size_t num_sids
, num_gids
;
2573 gid_t primary_gid
= -1;
2578 if (pdb_get_username(user
) == NULL
) {
2579 return NT_STATUS_INVALID_PARAMETER
;
2582 escape_name
= escape_ldap_string_alloc(pdb_get_username(user
));
2583 if (escape_name
== NULL
)
2584 return NT_STATUS_NO_MEMORY
;
2586 /* retrieve the users primary gid */
2587 filter
= talloc_asprintf(mem_ctx
,
2588 "(&(objectClass=%s)(uid=%s))",
2589 LDAP_OBJ_SAMBASAMACCOUNT
,
2591 if (filter
== NULL
) {
2592 ret
= NT_STATUS_NO_MEMORY
;
2596 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2597 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2599 if (rc
!= LDAP_SUCCESS
)
2602 talloc_autofree_ldapmsg(mem_ctx
, result
);
2604 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2608 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2609 ret
= NT_STATUS_NO_SUCH_USER
;
2612 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2614 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2616 DEBUG (1, ("Unable to find the member's gid!\n"));
2617 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2620 primary_gid
= strtoul(gidstr
, NULL
, 10);
2623 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2624 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2628 filter
= talloc_asprintf(mem_ctx
,
2629 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
2630 LDAP_OBJ_POSIXGROUP
, escape_name
, primary_gid
);
2631 if (filter
== NULL
) {
2632 ret
= NT_STATUS_NO_MEMORY
;
2636 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2637 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2639 if (rc
!= LDAP_SUCCESS
)
2642 talloc_autofree_ldapmsg(mem_ctx
, result
);
2650 /* We need to add the primary group as the first gid/sid */
2652 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2653 ret
= NT_STATUS_NO_MEMORY
;
2657 /* This sid will be replaced later */
2659 if (!add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
, &num_sids
)) {
2660 ret
= NT_STATUS_NO_MEMORY
;
2664 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2666 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2673 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2675 str
, sizeof(str
)-1))
2678 if (!string_to_sid(&sid
, str
))
2681 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2683 str
, sizeof(str
)-1))
2686 gid
= strtoul(str
, &end
, 10);
2688 if (PTR_DIFF(end
, str
) != strlen(str
))
2691 if (gid
== primary_gid
) {
2692 sid_copy(&(*pp_sids
)[0], &sid
);
2694 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2696 ret
= NT_STATUS_NO_MEMORY
;
2699 if (!add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2701 ret
= NT_STATUS_NO_MEMORY
;
2707 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2708 DEBUG(3, ("primary group of [%s] not found\n",
2709 pdb_get_username(user
)));
2713 *p_num_groups
= num_sids
;
2719 SAFE_FREE(escape_name
);
2723 /**********************************************************************
2724 * Augment a posixGroup object with a sambaGroupMapping domgroup
2725 *********************************************************************/
2727 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2728 struct ldapsam_privates
*ldap_state
,
2731 const char *filter
, *dn
;
2732 LDAPMessage
*msg
, *entry
;
2736 filter
= talloc_asprintf(mem_ctx
,
2737 "(&(objectClass=posixGroup)(gidNumber=%u))",
2739 if (filter
== NULL
) {
2740 return NT_STATUS_NO_MEMORY
;
2743 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2744 get_attr_list(mem_ctx
, groupmap_attr_list
),
2746 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2748 if ((rc
!= LDAP_SUCCESS
) ||
2749 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2750 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2751 return NT_STATUS_NO_SUCH_GROUP
;
2754 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2756 return NT_STATUS_NO_MEMORY
;
2760 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
2761 "sambaGroupMapping");
2762 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
2763 sid_string_static(&map
->sid
));
2764 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
2765 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2766 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2768 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2770 talloc_autofree_ldapmod(mem_ctx
, mods
);
2772 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2773 if (rc
!= LDAP_SUCCESS
) {
2774 return NT_STATUS_ACCESS_DENIED
;
2777 return NT_STATUS_OK
;
2780 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
2783 struct ldapsam_privates
*ldap_state
=
2784 (struct ldapsam_privates
*)methods
->private_data
;
2785 LDAPMessage
*msg
= NULL
;
2786 LDAPMod
**mods
= NULL
;
2787 const char *attrs
[] = { NULL
};
2791 TALLOC_CTX
*mem_ctx
;
2798 mem_ctx
= talloc_new(NULL
);
2799 if (mem_ctx
== NULL
) {
2800 DEBUG(0, ("talloc_new failed\n"));
2801 return NT_STATUS_NO_MEMORY
;
2804 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
2805 sid_string_static(&map
->sid
));
2806 if (filter
== NULL
) {
2807 result
= NT_STATUS_NO_MEMORY
;
2811 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
2812 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
2813 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2815 if ((rc
== LDAP_SUCCESS
) &&
2816 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
2818 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
2819 "group mapping entry\n",
2820 sid_string_static(&map
->sid
)));
2821 result
= NT_STATUS_GROUP_EXISTS
;
2825 switch (map
->sid_name_use
) {
2827 case SID_NAME_DOM_GRP
:
2828 /* To map a domain group we need to have a posix group
2830 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
2834 case SID_NAME_ALIAS
:
2835 if (!sid_check_is_in_our_domain(&map
->sid
)
2836 && !sid_check_is_in_builtin(&map
->sid
) )
2838 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
2839 sid_string_static(&map
->sid
)));
2840 result
= NT_STATUS_INVALID_PARAMETER
;
2846 DEBUG(3, ("Got invalid use '%s' for mapping\n",
2847 sid_type_lookup(map
->sid_name_use
)));
2848 result
= NT_STATUS_INVALID_PARAMETER
;
2852 /* Domain groups have been mapped in a separate routine, we have to
2853 * create an alias now */
2855 if (map
->gid
== -1) {
2856 DEBUG(10, ("Refusing to map gid==-1\n"));
2857 result
= NT_STATUS_INVALID_PARAMETER
;
2861 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
2862 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
2863 "add\n", map
->gid
, sid_string_static(&sid
)));
2864 result
= NT_STATUS_GROUP_EXISTS
;
2868 /* Ok, enough checks done. It's still racy to go ahead now, but that's
2869 * the best we can get out of LDAP. */
2871 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
2872 sid_string_static(&map
->sid
),
2873 lp_ldap_group_suffix());
2875 result
= NT_STATUS_NO_MEMORY
;
2881 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
2883 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
2884 "sambaGroupMapping");
2886 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
2887 sid_string_static(&map
->sid
));
2888 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
2889 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2890 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
2892 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
2894 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
2895 talloc_asprintf(mem_ctx
, "%u", map
->gid
));
2896 talloc_autofree_ldapmod(mem_ctx
, mods
);
2898 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
2900 result
= (rc
== LDAP_SUCCESS
) ?
2901 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
2904 TALLOC_FREE(mem_ctx
);
2908 /**********************************************************************
2909 * Update a group mapping entry. We're quite strict about what can be changed:
2910 * Only the description and displayname may be changed. It simply does not
2911 * make any sense to change the SID, gid or the type in a mapping.
2912 *********************************************************************/
2914 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
2917 struct ldapsam_privates
*ldap_state
=
2918 (struct ldapsam_privates
*)methods
->private_data
;
2920 const char *filter
, *dn
;
2921 LDAPMessage
*msg
= NULL
;
2922 LDAPMessage
*entry
= NULL
;
2923 LDAPMod
**mods
= NULL
;
2924 TALLOC_CTX
*mem_ctx
;
2927 mem_ctx
= talloc_new(NULL
);
2928 if (mem_ctx
== NULL
) {
2929 DEBUG(0, ("talloc_new failed\n"));
2930 return NT_STATUS_NO_MEMORY
;
2933 /* Make 100% sure that sid, gid and type are not changed by looking up
2934 * exactly the values we're given in LDAP. */
2936 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
2937 "(sambaSid=%s)(gidNumber=%u)"
2938 "(sambaGroupType=%d))",
2940 sid_string_static(&map
->sid
), map
->gid
,
2942 if (filter
== NULL
) {
2943 result
= NT_STATUS_NO_MEMORY
;
2947 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2948 get_attr_list(mem_ctx
, groupmap_attr_list
),
2950 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2952 if ((rc
!= LDAP_SUCCESS
) ||
2953 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2954 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2955 result
= NT_STATUS_NO_SUCH_GROUP
;
2959 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2962 result
= NT_STATUS_NO_MEMORY
;
2967 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2969 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2971 talloc_autofree_ldapmod(mem_ctx
, mods
);
2974 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
2975 "nothing to do\n"));
2976 result
= NT_STATUS_OK
;
2980 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2982 if (rc
!= LDAP_SUCCESS
) {
2983 result
= NT_STATUS_ACCESS_DENIED
;
2987 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
2988 "group %lu in LDAP\n", (unsigned long)map
->gid
));
2990 result
= NT_STATUS_OK
;
2993 TALLOC_FREE(mem_ctx
);
2997 /**********************************************************************
2998 *********************************************************************/
3000 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3003 struct ldapsam_privates
*priv
=
3004 (struct ldapsam_privates
*)methods
->private_data
;
3005 LDAPMessage
*msg
, *entry
;
3008 TALLOC_CTX
*mem_ctx
;
3011 mem_ctx
= talloc_new(NULL
);
3012 if (mem_ctx
== NULL
) {
3013 DEBUG(0, ("talloc_new failed\n"));
3014 return NT_STATUS_NO_MEMORY
;
3017 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3018 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3019 sid_string_static(&sid
));
3020 if (filter
== NULL
) {
3021 result
= NT_STATUS_NO_MEMORY
;
3024 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3025 get_attr_list(mem_ctx
, groupmap_attr_list
),
3027 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3029 if ((rc
!= LDAP_SUCCESS
) ||
3030 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3031 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3032 result
= NT_STATUS_NO_SUCH_GROUP
;
3036 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3037 get_attr_list(mem_ctx
,
3038 groupmap_attr_list_to_delete
));
3040 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3041 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3042 const char *attrs
[] = { "sambaGroupType", "description",
3043 "displayName", "sambaSIDList",
3046 /* Second try. Don't delete the sambaSID attribute, this is
3047 for "old" entries that are tacked on a winbind
3050 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3051 LDAP_OBJ_GROUPMAP
, attrs
);
3054 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3055 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3056 const char *attrs
[] = { "sambaGroupType", "description",
3057 "displayName", "sambaSIDList",
3058 "gidNumber", NULL
};
3060 /* Third try. This is a post-3.0.21 alias (containing only
3061 * sambaSidEntry and sambaGroupMapping classes), we also have
3062 * to delete the gidNumber attribute, only the sambaSidEntry
3065 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3066 LDAP_OBJ_GROUPMAP
, attrs
);
3069 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3072 TALLOC_FREE(mem_ctx
);
3076 /**********************************************************************
3077 *********************************************************************/
3079 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3082 struct ldapsam_privates
*ldap_state
=
3083 (struct ldapsam_privates
*)my_methods
->private_data
;
3086 const char **attr_list
;
3088 pstr_sprintf( filter
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3089 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3090 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3091 LDAP_SCOPE_SUBTREE
, filter
,
3092 attr_list
, 0, &ldap_state
->result
);
3093 TALLOC_FREE(attr_list
);
3095 if (rc
!= LDAP_SUCCESS
) {
3096 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3097 ldap_err2string(rc
)));
3098 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3099 lp_ldap_group_suffix(), filter
));
3100 ldap_msgfree(ldap_state
->result
);
3101 ldap_state
->result
= NULL
;
3102 return NT_STATUS_UNSUCCESSFUL
;
3105 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3106 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3107 ldap_state
->result
)));
3110 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3111 ldap_state
->result
);
3112 ldap_state
->index
= 0;
3114 return NT_STATUS_OK
;
3117 /**********************************************************************
3118 *********************************************************************/
3120 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3122 ldapsam_endsampwent(my_methods
);
3125 /**********************************************************************
3126 *********************************************************************/
3128 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3131 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3132 struct ldapsam_privates
*ldap_state
=
3133 (struct ldapsam_privates
*)my_methods
->private_data
;
3137 if (!ldap_state
->entry
)
3140 ldap_state
->index
++;
3141 bret
= init_group_from_ldap(ldap_state
, map
,
3145 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3149 return NT_STATUS_OK
;
3152 /**********************************************************************
3153 *********************************************************************/
3155 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3156 const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
,
3157 GROUP_MAP
**pp_rmap
,
3158 size_t *p_num_entries
,
3167 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3168 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3170 return NT_STATUS_ACCESS_DENIED
;
3173 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3174 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3175 sid_name_use
!= map
.sid_name_use
) {
3176 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3177 "not of the requested type\n", map
.nt_name
));
3180 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3181 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3182 "non mapped\n", map
.nt_name
));
3186 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3188 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3189 "enlarge group map!\n"));
3190 return NT_STATUS_UNSUCCESSFUL
;
3193 (*pp_rmap
)[entries
] = map
;
3198 ldapsam_endsamgrent(methods
);
3200 *p_num_entries
= entries
;
3202 return NT_STATUS_OK
;
3205 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3206 const DOM_SID
*alias
,
3207 const DOM_SID
*member
,
3210 struct ldapsam_privates
*ldap_state
=
3211 (struct ldapsam_privates
*)methods
->private_data
;
3213 LDAPMessage
*result
= NULL
;
3214 LDAPMessage
*entry
= NULL
;
3216 LDAPMod
**mods
= NULL
;
3218 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3222 if (sid_check_is_in_builtin(alias
)) {
3223 type
= SID_NAME_ALIAS
;
3226 if (sid_check_is_in_our_domain(alias
)) {
3227 type
= SID_NAME_ALIAS
;
3230 if (type
== SID_NAME_USE_NONE
) {
3231 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3232 sid_string_static(alias
)));
3233 return NT_STATUS_NO_SUCH_ALIAS
;
3236 pstr_sprintf(filter
,
3237 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3238 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3241 if (ldapsam_search_one_group(ldap_state
, filter
,
3242 &result
) != LDAP_SUCCESS
)
3243 return NT_STATUS_NO_SUCH_ALIAS
;
3245 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3249 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3250 ldap_msgfree(result
);
3251 return NT_STATUS_NO_SUCH_ALIAS
;
3255 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3256 "filter %s: count=%d\n", filter
, count
));
3257 ldap_msgfree(result
);
3258 return NT_STATUS_NO_SUCH_ALIAS
;
3261 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3265 ldap_msgfree(result
);
3266 return NT_STATUS_UNSUCCESSFUL
;
3269 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
3271 ldap_msgfree(result
);
3272 return NT_STATUS_UNSUCCESSFUL
;
3275 smbldap_set_mod(&mods
, modop
,
3276 get_attr_key2string(groupmap_attr_list
,
3277 LDAP_ATTR_SID_LIST
),
3278 sid_string_static(member
));
3280 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3282 ldap_mods_free(mods
, True
);
3283 ldap_msgfree(result
);
3286 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3287 return NT_STATUS_MEMBER_IN_ALIAS
;
3290 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3291 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3294 if (rc
!= LDAP_SUCCESS
) {
3295 return NT_STATUS_UNSUCCESSFUL
;
3298 return NT_STATUS_OK
;
3301 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3302 const DOM_SID
*alias
,
3303 const DOM_SID
*member
)
3305 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3308 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3309 const DOM_SID
*alias
,
3310 const DOM_SID
*member
)
3312 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3316 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3317 const DOM_SID
*alias
,
3318 DOM_SID
**pp_members
,
3319 size_t *p_num_members
)
3321 struct ldapsam_privates
*ldap_state
=
3322 (struct ldapsam_privates
*)methods
->private_data
;
3323 LDAPMessage
*result
= NULL
;
3324 LDAPMessage
*entry
= NULL
;
3329 size_t num_members
= 0;
3330 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3335 if (sid_check_is_in_builtin(alias
)) {
3336 type
= SID_NAME_ALIAS
;
3339 if (sid_check_is_in_our_domain(alias
)) {
3340 type
= SID_NAME_ALIAS
;
3343 if (type
== SID_NAME_USE_NONE
) {
3344 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3345 sid_string_static(alias
)));
3346 return NT_STATUS_NO_SUCH_ALIAS
;
3349 pstr_sprintf(filter
,
3350 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3351 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3354 if (ldapsam_search_one_group(ldap_state
, filter
,
3355 &result
) != LDAP_SUCCESS
)
3356 return NT_STATUS_NO_SUCH_ALIAS
;
3358 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3362 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3363 ldap_msgfree(result
);
3364 return NT_STATUS_NO_SUCH_ALIAS
;
3368 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3369 "filter %s: count=%d\n", filter
, count
));
3370 ldap_msgfree(result
);
3371 return NT_STATUS_NO_SUCH_ALIAS
;
3374 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3378 ldap_msgfree(result
);
3379 return NT_STATUS_UNSUCCESSFUL
;
3382 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3384 get_attr_key2string(groupmap_attr_list
,
3385 LDAP_ATTR_SID_LIST
));
3387 if (values
== NULL
) {
3388 ldap_msgfree(result
);
3389 return NT_STATUS_OK
;
3392 count
= ldap_count_values(values
);
3394 for (i
=0; i
<count
; i
++) {
3397 if (!string_to_sid(&member
, values
[i
]))
3400 if (!add_sid_to_array(NULL
, &member
, pp_members
, &num_members
)) {
3401 ldap_value_free(values
);
3402 ldap_msgfree(result
);
3403 return NT_STATUS_NO_MEMORY
;
3407 *p_num_members
= num_members
;
3408 ldap_value_free(values
);
3409 ldap_msgfree(result
);
3411 return NT_STATUS_OK
;
3414 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3415 TALLOC_CTX
*mem_ctx
,
3416 const DOM_SID
*domain_sid
,
3417 const DOM_SID
*members
,
3419 uint32
**pp_alias_rids
,
3420 size_t *p_num_alias_rids
)
3422 struct ldapsam_privates
*ldap_state
=
3423 (struct ldapsam_privates
*)methods
->private_data
;
3426 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3428 LDAPMessage
*result
= NULL
;
3429 LDAPMessage
*entry
= NULL
;
3433 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3435 if (sid_check_is_builtin(domain_sid
)) {
3436 type
= SID_NAME_ALIAS
;
3439 if (sid_check_is_domain(domain_sid
)) {
3440 type
= SID_NAME_ALIAS
;
3443 if (type
== SID_NAME_USE_NONE
) {
3444 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3445 sid_string_static(domain_sid
)));
3446 return NT_STATUS_UNSUCCESSFUL
;
3449 filter
= talloc_asprintf(mem_ctx
,
3450 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3451 LDAP_OBJ_GROUPMAP
, type
);
3453 for (i
=0; i
<num_members
; i
++)
3454 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3456 sid_string_static(&members
[i
]));
3458 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3460 if (filter
== NULL
) {
3461 return NT_STATUS_NO_MEMORY
;
3464 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3465 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3467 if (rc
!= LDAP_SUCCESS
)
3468 return NT_STATUS_UNSUCCESSFUL
;
3470 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3472 for (entry
= ldap_first_entry(ldap_struct
, result
);
3474 entry
= ldap_next_entry(ldap_struct
, entry
))
3480 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3486 if (!string_to_sid(&sid
, sid_str
))
3489 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3492 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3493 p_num_alias_rids
)) {
3494 ldap_msgfree(result
);
3495 return NT_STATUS_NO_MEMORY
;
3499 ldap_msgfree(result
);
3500 return NT_STATUS_OK
;
3503 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3507 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3509 LDAPMod
**mods
= NULL
;
3510 fstring value_string
;
3511 const char *policy_attr
= NULL
;
3513 struct ldapsam_privates
*ldap_state
=
3514 (struct ldapsam_privates
*)methods
->private_data
;
3516 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3518 if (!ldap_state
->domain_dn
) {
3519 return NT_STATUS_INVALID_PARAMETER
;
3522 policy_attr
= get_account_policy_attr(policy_index
);
3523 if (policy_attr
== NULL
) {
3524 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3529 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3531 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3533 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3536 ldap_mods_free(mods
, True
);
3538 if (rc
!= LDAP_SUCCESS
) {
3542 if (!cache_account_policy_set(policy_index
, value
)) {
3543 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3544 "update local tdb cache\n"));
3548 return NT_STATUS_OK
;
3551 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3552 int policy_index
, uint32 value
)
3554 return ldapsam_set_account_policy_in_ldap(methods
, policy_index
,
3558 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3562 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3563 LDAPMessage
*result
= NULL
;
3564 LDAPMessage
*entry
= NULL
;
3568 const char *policy_attr
= NULL
;
3570 struct ldapsam_privates
*ldap_state
=
3571 (struct ldapsam_privates
*)methods
->private_data
;
3573 const char *attrs
[2];
3575 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3577 if (!ldap_state
->domain_dn
) {
3578 return NT_STATUS_INVALID_PARAMETER
;
3581 policy_attr
= get_account_policy_attr(policy_index
);
3583 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3584 "policy index: %d\n", policy_index
));
3588 attrs
[0] = policy_attr
;
3591 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3592 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0,
3595 if (rc
!= LDAP_SUCCESS
) {
3599 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3604 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3605 if (entry
== NULL
) {
3609 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3614 *value
= (uint32
)atol(vals
[0]);
3616 ntstatus
= NT_STATUS_OK
;
3620 ldap_value_free(vals
);
3621 ldap_msgfree(result
);
3626 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3628 - if user hasn't decided to use account policies inside LDAP just reuse the
3631 - if there is a valid cache entry, return that
3632 - if there is an LDAP entry, update cache and return
3633 - otherwise set to default, update cache and return
3637 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3638 int policy_index
, uint32
*value
)
3640 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3642 if (cache_account_policy_get(policy_index
, value
)) {
3643 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3645 return NT_STATUS_OK
;
3648 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, policy_index
,
3650 if (NT_STATUS_IS_OK(ntstatus
)) {
3654 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3658 /* should we automagically migrate old tdb value here ? */
3659 if (account_policy_get(policy_index
, value
))
3662 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3663 "default\n", policy_index
));
3666 if (!account_policy_get_default(policy_index
, value
)) {
3672 ntstatus
= ldapsam_set_account_policy(methods
, policy_index
, *value
);
3673 if (!NT_STATUS_IS_OK(ntstatus
)) {
3679 if (!cache_account_policy_set(policy_index
, *value
)) {
3680 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3681 "tdb as a cache\n"));
3682 return NT_STATUS_UNSUCCESSFUL
;
3685 return NT_STATUS_OK
;
3688 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3689 const DOM_SID
*domain_sid
,
3693 enum lsa_SidType
*attrs
)
3695 struct ldapsam_privates
*ldap_state
=
3696 (struct ldapsam_privates
*)methods
->private_data
;
3697 LDAPMessage
*msg
= NULL
;
3699 char *allsids
= NULL
;
3700 int i
, rc
, num_mapped
;
3701 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
3702 TALLOC_CTX
*mem_ctx
;
3706 mem_ctx
= talloc_new(NULL
);
3707 if (mem_ctx
== NULL
) {
3708 DEBUG(0, ("talloc_new failed\n"));
3712 if (!sid_check_is_builtin(domain_sid
) &&
3713 !sid_check_is_domain(domain_sid
)) {
3714 result
= NT_STATUS_INVALID_PARAMETER
;
3718 for (i
=0; i
<num_rids
; i
++)
3719 attrs
[i
] = SID_NAME_UNKNOWN
;
3721 allsids
= talloc_strdup(mem_ctx
, "");
3722 if (allsids
== NULL
) {
3726 for (i
=0; i
<num_rids
; i
++) {
3728 sid_compose(&sid
, domain_sid
, rids
[i
]);
3729 allsids
= talloc_asprintf_append_buffer(allsids
, "(sambaSid=%s)",
3730 sid_string_static(&sid
));
3731 if (allsids
== NULL
) {
3736 /* First look for users */
3740 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
3742 filter
= talloc_asprintf(
3743 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
3744 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
3746 if (filter
== NULL
) {
3750 rc
= smbldap_search(ldap_state
->smbldap_state
,
3751 lp_ldap_user_suffix(),
3752 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
3754 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3757 if (rc
!= LDAP_SUCCESS
)
3760 ld
= ldap_state
->smbldap_state
->ldap_struct
;
3763 for (entry
= ldap_first_entry(ld
, msg
);
3765 entry
= ldap_next_entry(ld
, entry
)) {
3770 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
3772 DEBUG(2, ("Could not find sid from ldap entry\n"));
3776 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
3779 DEBUG(2, ("Could not retrieve uid attribute\n"));
3783 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
3784 if (rid
== rids
[rid_index
])
3788 if (rid_index
== num_rids
) {
3789 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
3793 attrs
[rid_index
] = SID_NAME_USER
;
3794 names
[rid_index
] = name
;
3798 if (num_mapped
== num_rids
) {
3799 /* No need to look for groups anymore -- we're done */
3800 result
= NT_STATUS_OK
;
3804 /* Same game for groups */
3808 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
3809 "sambaGroupType", NULL
};
3811 filter
= talloc_asprintf(
3812 mem_ctx
, "(&(objectClass=%s)(|%s))",
3813 LDAP_OBJ_GROUPMAP
, allsids
);
3814 if (filter
== NULL
) {
3818 rc
= smbldap_search(ldap_state
->smbldap_state
,
3819 lp_ldap_group_suffix(),
3820 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
3822 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3825 if (rc
!= LDAP_SUCCESS
)
3828 /* ldap_struct might have changed due to a reconnect */
3830 ld
= ldap_state
->smbldap_state
->ldap_struct
;
3832 /* For consistency checks, we already checked we're only domain or builtin */
3834 is_builtin
= sid_check_is_builtin(domain_sid
);
3836 for (entry
= ldap_first_entry(ld
, msg
);
3838 entry
= ldap_next_entry(ld
, entry
))
3843 enum lsa_SidType type
;
3844 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
3846 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
3849 DEBUG(2, ("Could not extract type from ldap entry %s\n",
3854 type
= (enum lsa_SidType
)atol(attr
);
3856 /* Consistency checks */
3857 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
3858 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
3859 (type
!= SID_NAME_DOM_GRP
)))) {
3860 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
3863 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
3865 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
3869 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
3872 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
3874 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
3878 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
3883 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
3884 if (rid
== rids
[rid_index
])
3888 if (rid_index
== num_rids
) {
3889 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
3893 attrs
[rid_index
] = type
;
3894 names
[rid_index
] = attr
;
3898 result
= NT_STATUS_NONE_MAPPED
;
3901 result
= (num_mapped
== num_rids
) ?
3902 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
3904 TALLOC_FREE(mem_ctx
);
3908 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
3910 char *filter
= NULL
;
3911 char *escaped
= NULL
;
3912 char *result
= NULL
;
3914 asprintf(&filter
, "(&%s(objectclass=sambaSamAccount))",
3916 if (filter
== NULL
) goto done
;
3918 escaped
= escape_ldap_string_alloc(username
);
3919 if (escaped
== NULL
) goto done
;
3921 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
3930 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
3934 const char **result
;
3936 va_start(ap
, mem_ctx
);
3937 while (va_arg(ap
, const char *) != NULL
)
3941 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
3945 va_start(ap
, mem_ctx
);
3946 for (i
=0; i
<num
; i
++) {
3947 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
3948 if (result
[i
] == NULL
) {
3949 talloc_free(result
);
3959 struct ldap_search_state
{
3960 struct smbldap_state
*connection
;
3970 void *pagedresults_cookie
;
3972 LDAPMessage
*entries
, *current_entry
;
3973 BOOL (*ldap2displayentry
)(struct ldap_search_state
*state
,
3974 TALLOC_CTX
*mem_ctx
,
3975 LDAP
*ld
, LDAPMessage
*entry
,
3976 struct samr_displayentry
*result
);
3979 static BOOL
ldapsam_search_firstpage(struct pdb_search
*search
)
3981 struct ldap_search_state
*state
=
3982 (struct ldap_search_state
*)search
->private_data
;
3984 int rc
= LDAP_OPERATIONS_ERROR
;
3986 state
->entries
= NULL
;
3988 if (state
->connection
->paged_results
) {
3989 rc
= smbldap_search_paged(state
->connection
, state
->base
,
3990 state
->scope
, state
->filter
,
3991 state
->attrs
, state
->attrsonly
,
3992 lp_ldap_page_size(), &state
->entries
,
3993 &state
->pagedresults_cookie
);
3996 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
3998 if (state
->entries
!= NULL
) {
3999 /* Left over from unsuccessful paged attempt */
4000 ldap_msgfree(state
->entries
);
4001 state
->entries
= NULL
;
4004 rc
= smbldap_search(state
->connection
, state
->base
,
4005 state
->scope
, state
->filter
, state
->attrs
,
4006 state
->attrsonly
, &state
->entries
);
4008 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4011 /* Ok, the server was lying. It told us it could do paged
4012 * searches when it could not. */
4013 state
->connection
->paged_results
= False
;
4016 ld
= state
->connection
->ldap_struct
;
4018 DEBUG(5, ("Don't have an LDAP connection right after a "
4022 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4024 if (state
->current_entry
== NULL
) {
4025 ldap_msgfree(state
->entries
);
4026 state
->entries
= NULL
;
4032 static BOOL
ldapsam_search_nextpage(struct pdb_search
*search
)
4034 struct ldap_search_state
*state
=
4035 (struct ldap_search_state
*)search
->private_data
;
4038 if (!state
->connection
->paged_results
) {
4039 /* There is no next page when there are no paged results */
4043 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4044 state
->scope
, state
->filter
, state
->attrs
,
4045 state
->attrsonly
, lp_ldap_page_size(),
4047 &state
->pagedresults_cookie
);
4049 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4052 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4054 if (state
->current_entry
== NULL
) {
4055 ldap_msgfree(state
->entries
);
4056 state
->entries
= NULL
;
4062 static BOOL
ldapsam_search_next_entry(struct pdb_search
*search
,
4063 struct samr_displayentry
*entry
)
4065 struct ldap_search_state
*state
=
4066 (struct ldap_search_state
*)search
->private_data
;
4070 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4073 if ((state
->entries
== NULL
) &&
4074 !ldapsam_search_nextpage(search
))
4077 result
= state
->ldap2displayentry(state
, search
->mem_ctx
, state
->connection
->ldap_struct
,
4078 state
->current_entry
, entry
);
4082 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4083 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4084 if (dn
!= NULL
) ldap_memfree(dn
);
4087 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4089 if (state
->current_entry
== NULL
) {
4090 ldap_msgfree(state
->entries
);
4091 state
->entries
= NULL
;
4094 if (!result
) goto retry
;
4099 static void ldapsam_search_end(struct pdb_search
*search
)
4101 struct ldap_search_state
*state
=
4102 (struct ldap_search_state
*)search
->private_data
;
4105 if (state
->pagedresults_cookie
== NULL
)
4108 if (state
->entries
!= NULL
)
4109 ldap_msgfree(state
->entries
);
4111 state
->entries
= NULL
;
4112 state
->current_entry
= NULL
;
4114 if (!state
->connection
->paged_results
)
4117 /* Tell the LDAP server we're not interested in the rest anymore. */
4119 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4120 state
->filter
, state
->attrs
,
4121 state
->attrsonly
, 0, &state
->entries
,
4122 &state
->pagedresults_cookie
);
4124 if (rc
!= LDAP_SUCCESS
)
4125 DEBUG(5, ("Could not end search properly\n"));
4130 static BOOL
ldapuser2displayentry(struct ldap_search_state
*state
,
4131 TALLOC_CTX
*mem_ctx
,
4132 LDAP
*ld
, LDAPMessage
*entry
,
4133 struct samr_displayentry
*result
)
4139 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4140 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4141 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4144 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4145 ldap_value_free(vals
);
4147 if ((state
->acct_flags
!= 0) &&
4148 ((state
->acct_flags
& acct_flags
) == 0))
4151 result
->acct_flags
= acct_flags
;
4152 result
->account_name
= "";
4153 result
->fullname
= "";
4154 result
->description
= "";
4156 vals
= ldap_get_values(ld
, entry
, "uid");
4157 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4158 DEBUG(5, ("\"uid\" not found\n"));
4161 pull_utf8_talloc(mem_ctx
,
4162 CONST_DISCARD(char **, &result
->account_name
),
4164 ldap_value_free(vals
);
4166 vals
= ldap_get_values(ld
, entry
, "displayName");
4167 if ((vals
== NULL
) || (vals
[0] == NULL
))
4168 DEBUG(8, ("\"displayName\" not found\n"));
4170 pull_utf8_talloc(mem_ctx
,
4171 CONST_DISCARD(char **, &result
->fullname
),
4173 ldap_value_free(vals
);
4175 vals
= ldap_get_values(ld
, entry
, "description");
4176 if ((vals
== NULL
) || (vals
[0] == NULL
))
4177 DEBUG(8, ("\"description\" not found\n"));
4179 pull_utf8_talloc(mem_ctx
,
4180 CONST_DISCARD(char **, &result
->description
),
4182 ldap_value_free(vals
);
4184 if ((result
->account_name
== NULL
) ||
4185 (result
->fullname
== NULL
) ||
4186 (result
->description
== NULL
)) {
4187 DEBUG(0, ("talloc failed\n"));
4191 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4192 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4193 DEBUG(0, ("\"objectSid\" not found\n"));
4197 if (!string_to_sid(&sid
, vals
[0])) {
4198 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4199 ldap_value_free(vals
);
4202 ldap_value_free(vals
);
4204 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4205 DEBUG(0, ("sid %s does not belong to our domain\n",
4206 sid_string_static(&sid
)));
4214 static BOOL
ldapsam_search_users(struct pdb_methods
*methods
,
4215 struct pdb_search
*search
,
4218 struct ldapsam_privates
*ldap_state
=
4219 (struct ldapsam_privates
*)methods
->private_data
;
4220 struct ldap_search_state
*state
;
4222 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4223 if (state
== NULL
) {
4224 DEBUG(0, ("talloc failed\n"));
4228 state
->connection
= ldap_state
->smbldap_state
;
4230 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4231 state
->base
= lp_ldap_user_suffix();
4232 else if ((acct_flags
!= 0) &&
4233 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4234 state
->base
= lp_ldap_machine_suffix();
4236 state
->base
= lp_ldap_suffix();
4238 state
->acct_flags
= acct_flags
;
4239 state
->base
= talloc_strdup(search
->mem_ctx
, state
->base
);
4240 state
->scope
= LDAP_SCOPE_SUBTREE
;
4241 state
->filter
= get_ldap_filter(search
->mem_ctx
, "*");
4242 state
->attrs
= talloc_attrs(search
->mem_ctx
, "uid", "sambaSid",
4243 "displayName", "description",
4244 "sambaAcctFlags", NULL
);
4245 state
->attrsonly
= 0;
4246 state
->pagedresults_cookie
= NULL
;
4247 state
->entries
= NULL
;
4248 state
->ldap2displayentry
= ldapuser2displayentry
;
4250 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4251 DEBUG(0, ("talloc failed\n"));
4255 search
->private_data
= state
;
4256 search
->next_entry
= ldapsam_search_next_entry
;
4257 search
->search_end
= ldapsam_search_end
;
4259 return ldapsam_search_firstpage(search
);
4262 static BOOL
ldapgroup2displayentry(struct ldap_search_state
*state
,
4263 TALLOC_CTX
*mem_ctx
,
4264 LDAP
*ld
, LDAPMessage
*entry
,
4265 struct samr_displayentry
*result
)
4271 result
->account_name
= "";
4272 result
->fullname
= "";
4273 result
->description
= "";
4276 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4277 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4278 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4280 ldap_value_free(vals
);
4285 group_type
= atoi(vals
[0]);
4287 if ((state
->group_type
!= 0) &&
4288 ((state
->group_type
!= group_type
))) {
4289 ldap_value_free(vals
);
4293 ldap_value_free(vals
);
4295 /* display name is the NT group name */
4297 vals
= ldap_get_values(ld
, entry
, "displayName");
4298 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4299 DEBUG(8, ("\"displayName\" not found\n"));
4301 /* fallback to the 'cn' attribute */
4302 vals
= ldap_get_values(ld
, entry
, "cn");
4303 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4304 DEBUG(5, ("\"cn\" not found\n"));
4307 pull_utf8_talloc(mem_ctx
,
4308 CONST_DISCARD(char **, &result
->account_name
),
4312 pull_utf8_talloc(mem_ctx
,
4313 CONST_DISCARD(char **, &result
->account_name
),
4317 ldap_value_free(vals
);
4319 vals
= ldap_get_values(ld
, entry
, "description");
4320 if ((vals
== NULL
) || (vals
[0] == NULL
))
4321 DEBUG(8, ("\"description\" not found\n"));
4323 pull_utf8_talloc(mem_ctx
,
4324 CONST_DISCARD(char **, &result
->description
),
4326 ldap_value_free(vals
);
4328 if ((result
->account_name
== NULL
) ||
4329 (result
->fullname
== NULL
) ||
4330 (result
->description
== NULL
)) {
4331 DEBUG(0, ("talloc failed\n"));
4335 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4336 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4337 DEBUG(0, ("\"objectSid\" not found\n"));
4339 ldap_value_free(vals
);
4344 if (!string_to_sid(&sid
, vals
[0])) {
4345 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4349 ldap_value_free(vals
);
4351 switch (group_type
) {
4352 case SID_NAME_DOM_GRP
:
4353 case SID_NAME_ALIAS
:
4355 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4356 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4358 DEBUG(0, ("%s is not in our domain\n",
4359 sid_string_static(&sid
)));
4365 DEBUG(0,("unkown group type: %d\n", group_type
));
4372 static BOOL
ldapsam_search_grouptype(struct pdb_methods
*methods
,
4373 struct pdb_search
*search
,
4375 enum lsa_SidType type
)
4377 struct ldapsam_privates
*ldap_state
=
4378 (struct ldapsam_privates
*)methods
->private_data
;
4379 struct ldap_search_state
*state
;
4381 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4382 if (state
== NULL
) {
4383 DEBUG(0, ("talloc failed\n"));
4387 state
->connection
= ldap_state
->smbldap_state
;
4389 state
->base
= talloc_strdup(search
->mem_ctx
, lp_ldap_group_suffix());
4390 state
->connection
= ldap_state
->smbldap_state
;
4391 state
->scope
= LDAP_SCOPE_SUBTREE
;
4392 state
->filter
= talloc_asprintf(search
->mem_ctx
,
4393 "(&(objectclass=sambaGroupMapping)"
4394 "(sambaGroupType=%d)(sambaSID=%s*))",
4395 type
, sid_string_static(sid
));
4396 state
->attrs
= talloc_attrs(search
->mem_ctx
, "cn", "sambaSid",
4397 "displayName", "description",
4398 "sambaGroupType", NULL
);
4399 state
->attrsonly
= 0;
4400 state
->pagedresults_cookie
= NULL
;
4401 state
->entries
= NULL
;
4402 state
->group_type
= type
;
4403 state
->ldap2displayentry
= ldapgroup2displayentry
;
4405 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4406 DEBUG(0, ("talloc failed\n"));
4410 search
->private_data
= state
;
4411 search
->next_entry
= ldapsam_search_next_entry
;
4412 search
->search_end
= ldapsam_search_end
;
4414 return ldapsam_search_firstpage(search
);
4417 static BOOL
ldapsam_search_groups(struct pdb_methods
*methods
,
4418 struct pdb_search
*search
)
4420 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4423 static BOOL
ldapsam_search_aliases(struct pdb_methods
*methods
,
4424 struct pdb_search
*search
,
4427 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4430 static BOOL
ldapsam_rid_algorithm(struct pdb_methods
*methods
)
4435 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4438 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4440 LDAPMessage
*result
= NULL
;
4441 LDAPMessage
*entry
= NULL
;
4442 LDAPMod
**mods
= NULL
;
4449 TALLOC_CTX
*mem_ctx
;
4451 mem_ctx
= talloc_new(NULL
);
4452 if (mem_ctx
== NULL
) {
4453 DEBUG(0, ("talloc_new failed\n"));
4454 return NT_STATUS_NO_MEMORY
;
4457 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4458 get_global_sam_name(), False
);
4459 if (!NT_STATUS_IS_OK(status
)) {
4460 DEBUG(3, ("Could not get domain info: %s\n",
4461 nt_errstr(status
)));
4465 talloc_autofree_ldapmsg(mem_ctx
, result
);
4467 entry
= ldap_first_entry(priv2ld(priv
), result
);
4468 if (entry
== NULL
) {
4469 DEBUG(0, ("Could not get domain info entry\n"));
4470 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4474 /* Find the largest of the three attributes "sambaNextRid",
4475 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4476 concept of differentiating between user and group rids, and will
4477 use only "sambaNextRid" in the future. But for compatibility
4478 reasons I look if others have chosen different strategies -- VL */
4480 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4481 "sambaNextRid", mem_ctx
);
4482 if (value
!= NULL
) {
4483 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4484 nextRid
= MAX(nextRid
, tmp
);
4487 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4488 "sambaNextUserRid", mem_ctx
);
4489 if (value
!= NULL
) {
4490 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4491 nextRid
= MAX(nextRid
, tmp
);
4494 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4495 "sambaNextGroupRid", mem_ctx
);
4496 if (value
!= NULL
) {
4497 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4498 nextRid
= MAX(nextRid
, tmp
);
4502 nextRid
= BASE_RID
-1;
4507 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4508 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4509 talloc_autofree_ldapmod(mem_ctx
, mods
);
4511 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4512 status
= NT_STATUS_NO_MEMORY
;
4516 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4518 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4521 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4524 if (NT_STATUS_IS_OK(status
)) {
4528 TALLOC_FREE(mem_ctx
);
4532 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32
*rid
)
4536 for (i
=0; i
<10; i
++) {
4537 NTSTATUS result
= ldapsam_get_new_rid(
4538 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4539 if (NT_STATUS_IS_OK(result
)) {
4543 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4547 /* The ldap update failed (maybe a race condition), retry */
4550 /* Tried 10 times, fail. */
4551 return NT_STATUS_ACCESS_DENIED
;
4554 static BOOL
ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
4556 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4557 return NT_STATUS_IS_OK(result
) ? True
: False
;
4560 static BOOL
ldapsam_sid_to_id(struct pdb_methods
*methods
,
4562 union unid_t
*id
, enum lsa_SidType
*type
)
4564 struct ldapsam_privates
*priv
=
4565 (struct ldapsam_privates
*)methods
->private_data
;
4567 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4569 LDAPMessage
*result
= NULL
;
4570 LDAPMessage
*entry
= NULL
;
4575 TALLOC_CTX
*mem_ctx
;
4577 mem_ctx
= talloc_new(NULL
);
4578 if (mem_ctx
== NULL
) {
4579 DEBUG(0, ("talloc_new failed\n"));
4583 filter
= talloc_asprintf(mem_ctx
,
4585 "(|(objectClass=%s)(objectClass=%s)))",
4586 sid_string_static(sid
),
4587 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4588 if (filter
== NULL
) {
4589 DEBUG(5, ("talloc_asprintf failed\n"));
4593 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4595 if (rc
!= LDAP_SUCCESS
) {
4598 talloc_autofree_ldapmsg(mem_ctx
, result
);
4600 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4601 DEBUG(10, ("Got %d entries, expected one\n",
4602 ldap_count_entries(priv2ld(priv
), result
)));
4606 entry
= ldap_first_entry(priv2ld(priv
), result
);
4608 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4609 "sambaGroupType", mem_ctx
);
4611 if (value
!= NULL
) {
4612 const char *gid_str
;
4615 gid_str
= smbldap_talloc_single_attribute(
4616 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4617 if (gid_str
== NULL
) {
4618 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4619 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4624 id
->gid
= strtoul(gid_str
, NULL
, 10);
4625 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
4630 /* It must be a user */
4632 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4633 "uidNumber", mem_ctx
);
4634 if (value
== NULL
) {
4635 DEBUG(1, ("Could not find uidNumber in %s\n",
4636 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4640 id
->uid
= strtoul(value
, NULL
, 10);
4641 *type
= SID_NAME_USER
;
4645 TALLOC_FREE(mem_ctx
);
4650 * The following functions is called only if
4651 * ldapsam:trusted and ldapsam:editposix are
4656 * ldapsam_create_user creates a new
4657 * posixAccount and sambaSamAccount object
4658 * in the ldap users subtree
4660 * The uid is allocated by winbindd.
4663 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
4664 TALLOC_CTX
*tmp_ctx
, const char *name
,
4665 uint32 acb_info
, uint32
*rid
)
4667 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4668 LDAPMessage
*entry
= NULL
;
4669 LDAPMessage
*result
= NULL
;
4671 BOOL is_machine
= False
;
4672 BOOL add_posix
= False
;
4673 LDAPMod
**mods
= NULL
;
4681 const char *dn
= NULL
;
4689 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
4690 acb_info
& ACB_WSTRUST
||
4691 acb_info
& ACB_SVRTRUST
||
4692 acb_info
& ACB_DOMTRUST
) {
4696 username
= escape_ldap_string_alloc(name
);
4697 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
4698 username
, LDAP_OBJ_POSIXACCOUNT
);
4699 SAFE_FREE(username
);
4701 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4702 if (rc
!= LDAP_SUCCESS
) {
4703 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
4704 return NT_STATUS_UNSUCCESSFUL
;
4706 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4708 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4710 if (num_result
> 1) {
4711 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
4712 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4715 if (num_result
== 1) {
4717 /* check if it is just a posix account.
4718 * or if there is a sid attached to this entry
4721 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4723 return NT_STATUS_UNSUCCESSFUL
;
4726 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
4728 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
4729 return NT_STATUS_USER_EXISTS
;
4732 /* it is just a posix account, retrieve the dn for later use */
4733 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4735 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
4736 return NT_STATUS_NO_MEMORY
;
4740 if (num_result
== 0) {
4744 /* Create the basic samu structure and generate the mods for the ldap commit */
4745 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
4746 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
4750 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
4752 user
= samu_new(tmp_ctx
);
4754 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
4755 return NT_STATUS_NO_MEMORY
;
4758 if (!pdb_set_username(user
, name
, PDB_SET
)) {
4759 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4760 return NT_STATUS_UNSUCCESSFUL
;
4762 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
4763 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4764 return NT_STATUS_UNSUCCESSFUL
;
4767 if (acb_info
& ACB_NORMAL
) {
4768 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
4769 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4770 return NT_STATUS_UNSUCCESSFUL
;
4773 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
4774 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4775 return NT_STATUS_UNSUCCESSFUL
;
4779 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
4780 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4781 return NT_STATUS_UNSUCCESSFUL
;
4785 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
4786 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4787 return NT_STATUS_UNSUCCESSFUL
;
4790 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
4791 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4792 return NT_STATUS_UNSUCCESSFUL
;
4795 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
4796 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
4798 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
4803 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
4805 /* retrieve the Domain Users group gid */
4806 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
4807 !sid_to_gid(&group_sid
, &gid
)) {
4808 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
4809 return NT_STATUS_INVALID_PRIMARY_GROUP
;
4812 /* lets allocate a new userid for this user */
4813 if (!winbind_allocate_uid(&uid
)) {
4814 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
4815 return NT_STATUS_UNSUCCESSFUL
;
4820 /* TODO: choose a more appropriate default for machines */
4821 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
4822 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
4824 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
4825 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
4827 uidstr
= talloc_asprintf(tmp_ctx
, "%d", uid
);
4828 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
4830 escape_name
= escape_rdn_val_string_alloc(name
);
4832 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
4833 return NT_STATUS_NO_MEMORY
;
4837 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
4839 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
4842 SAFE_FREE(escape_name
);
4844 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
4845 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
4846 return NT_STATUS_NO_MEMORY
;
4849 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
4850 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
4851 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
4852 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
4853 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
4854 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
4855 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
4858 talloc_autofree_ldapmod(tmp_ctx
, mods
);
4861 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
4863 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
4866 if (rc
!= LDAP_SUCCESS
) {
4867 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
4868 return NT_STATUS_UNSUCCESSFUL
;
4871 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
4873 flush_pwnam_cache();
4875 return NT_STATUS_OK
;
4878 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
4880 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4881 LDAPMessage
*result
= NULL
;
4882 LDAPMessage
*entry
= NULL
;
4888 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
4890 filter
= talloc_asprintf(tmp_ctx
,
4893 "(objectClass=%s))",
4894 pdb_get_username(sam_acct
),
4895 LDAP_OBJ_POSIXACCOUNT
,
4896 LDAP_OBJ_SAMBASAMACCOUNT
);
4897 if (filter
== NULL
) {
4898 return NT_STATUS_NO_MEMORY
;
4901 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4902 if (rc
!= LDAP_SUCCESS
) {
4903 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
4904 return NT_STATUS_UNSUCCESSFUL
;
4906 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4908 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4910 if (num_result
== 0) {
4911 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
4912 return NT_STATUS_NO_SUCH_USER
;
4915 if (num_result
> 1) {
4916 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
4917 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4920 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4922 return NT_STATUS_UNSUCCESSFUL
;
4925 /* it is just a posix account, retrieve the dn for later use */
4926 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4928 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
4929 return NT_STATUS_NO_MEMORY
;
4932 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
4933 if (rc
!= LDAP_SUCCESS
) {
4934 return NT_STATUS_UNSUCCESSFUL
;
4937 flush_pwnam_cache();
4939 return NT_STATUS_OK
;
4943 * ldapsam_create_group creates a new
4944 * posixGroup and sambaGroupMapping object
4945 * in the ldap groups subtree
4947 * The gid is allocated by winbindd.
4950 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
4951 TALLOC_CTX
*tmp_ctx
,
4955 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4957 LDAPMessage
*entry
= NULL
;
4958 LDAPMessage
*result
= NULL
;
4960 BOOL is_new_entry
= False
;
4961 LDAPMod
**mods
= NULL
;
4967 const char *dn
= NULL
;
4972 groupname
= escape_ldap_string_alloc(name
);
4973 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
4974 groupname
, LDAP_OBJ_POSIXGROUP
);
4975 SAFE_FREE(groupname
);
4977 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4978 if (rc
!= LDAP_SUCCESS
) {
4979 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
4980 return NT_STATUS_UNSUCCESSFUL
;
4982 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4984 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4986 if (num_result
> 1) {
4987 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
4988 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4991 if (num_result
== 1) {
4993 /* check if it is just a posix group.
4994 * or if there is a sid attached to this entry
4997 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4999 return NT_STATUS_UNSUCCESSFUL
;
5002 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5004 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5005 return NT_STATUS_GROUP_EXISTS
;
5008 /* it is just a posix group, retrieve the gid and the dn for later use */
5009 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5011 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5012 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5015 gid
= strtoul(tmp
, NULL
, 10);
5017 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5019 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5020 return NT_STATUS_NO_MEMORY
;
5024 if (num_result
== 0) {
5027 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5029 is_new_entry
= True
;
5031 /* lets allocate a new groupid for this group */
5032 if (!winbind_allocate_gid(&gid
)) {
5033 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5034 return NT_STATUS_UNSUCCESSFUL
;
5037 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
5039 escape_name
= escape_rdn_val_string_alloc(name
);
5041 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5042 return NT_STATUS_NO_MEMORY
;
5045 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5047 SAFE_FREE(escape_name
);
5049 if (!gidstr
|| !dn
) {
5050 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5051 return NT_STATUS_NO_MEMORY
;
5054 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5055 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5056 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5059 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5060 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5064 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5066 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_static(&group_sid
));
5067 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5069 if (!groupsidstr
|| !grouptype
) {
5070 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5071 return NT_STATUS_NO_MEMORY
;
5074 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5075 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5076 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5077 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5078 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5081 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5083 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5084 /* This call may fail with rfc2307bis schema */
5085 /* Retry adding a structural class */
5086 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5087 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5091 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5094 if (rc
!= LDAP_SUCCESS
) {
5095 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5096 return NT_STATUS_UNSUCCESSFUL
;
5099 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5101 return NT_STATUS_OK
;
5104 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
5106 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5107 LDAPMessage
*result
= NULL
;
5108 LDAPMessage
*entry
= NULL
;
5116 /* get the group sid */
5117 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5119 filter
= talloc_asprintf(tmp_ctx
,
5122 "(objectClass=%s))",
5123 sid_string_static(&group_sid
),
5124 LDAP_OBJ_POSIXGROUP
,
5126 if (filter
== NULL
) {
5127 return NT_STATUS_NO_MEMORY
;
5130 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5131 if (rc
!= LDAP_SUCCESS
) {
5132 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5133 return NT_STATUS_UNSUCCESSFUL
;
5135 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5137 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5139 if (num_result
== 0) {
5140 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5141 return NT_STATUS_NO_SUCH_GROUP
;
5144 if (num_result
> 1) {
5145 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5146 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5149 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5151 return NT_STATUS_UNSUCCESSFUL
;
5154 /* here it is, retrieve the dn for later use */
5155 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5157 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5158 return NT_STATUS_NO_MEMORY
;
5161 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5163 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5164 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5167 /* check no user have this group marked as primary group */
5168 filter
= talloc_asprintf(tmp_ctx
,
5171 "(objectClass=%s))",
5173 LDAP_OBJ_POSIXACCOUNT
,
5174 LDAP_OBJ_SAMBASAMACCOUNT
);
5176 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5177 if (rc
!= LDAP_SUCCESS
) {
5178 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5179 return NT_STATUS_UNSUCCESSFUL
;
5181 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5183 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5185 if (num_result
!= 0) {
5186 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5187 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5190 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5191 if (rc
!= LDAP_SUCCESS
) {
5192 return NT_STATUS_UNSUCCESSFUL
;
5195 return NT_STATUS_OK
;
5198 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5199 TALLOC_CTX
*tmp_ctx
,
5204 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5205 LDAPMessage
*entry
= NULL
;
5206 LDAPMessage
*result
= NULL
;
5208 LDAPMod
**mods
= NULL
;
5211 const char *dn
= NULL
;
5218 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5220 case LDAP_MOD_DELETE
:
5221 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5224 return NT_STATUS_UNSUCCESSFUL
;
5227 /* get member sid */
5228 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5230 /* get the group sid */
5231 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5233 filter
= talloc_asprintf(tmp_ctx
,
5236 "(objectClass=%s))",
5237 sid_string_static(&member_sid
),
5238 LDAP_OBJ_POSIXACCOUNT
,
5239 LDAP_OBJ_SAMBASAMACCOUNT
);
5240 if (filter
== NULL
) {
5241 return NT_STATUS_NO_MEMORY
;
5244 /* get the member uid */
5245 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5246 if (rc
!= LDAP_SUCCESS
) {
5247 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5248 return NT_STATUS_UNSUCCESSFUL
;
5250 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5252 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5254 if (num_result
== 0) {
5255 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5256 return NT_STATUS_NO_SUCH_MEMBER
;
5259 if (num_result
> 1) {
5260 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5261 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5264 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5266 return NT_STATUS_UNSUCCESSFUL
;
5269 if (modop
== LDAP_MOD_DELETE
) {
5270 /* check if we are trying to remove the member from his primary group */
5272 gid_t user_gid
, group_gid
;
5274 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5276 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5277 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5280 user_gid
= strtoul(gidstr
, NULL
, 10);
5282 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5283 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5284 return NT_STATUS_UNSUCCESSFUL
;
5287 if (user_gid
== group_gid
) {
5288 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from it's own primary group!\n"));
5289 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5293 /* here it is, retrieve the uid for later use */
5294 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5296 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5297 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5300 filter
= talloc_asprintf(tmp_ctx
,
5303 "(objectClass=%s))",
5304 sid_string_static(&group_sid
),
5305 LDAP_OBJ_POSIXGROUP
,
5309 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5310 if (rc
!= LDAP_SUCCESS
) {
5311 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5312 return NT_STATUS_UNSUCCESSFUL
;
5314 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5316 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5318 if (num_result
== 0) {
5319 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5320 return NT_STATUS_NO_SUCH_GROUP
;
5323 if (num_result
> 1) {
5324 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5325 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5328 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5330 return NT_STATUS_UNSUCCESSFUL
;
5333 /* here it is, retrieve the dn for later use */
5334 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5336 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5337 return NT_STATUS_NO_MEMORY
;
5340 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5342 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5344 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5345 if (rc
!= LDAP_SUCCESS
) {
5346 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5347 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5348 return NT_STATUS_MEMBER_IN_GROUP
;
5350 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5351 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5352 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5354 return NT_STATUS_UNSUCCESSFUL
;
5357 return NT_STATUS_OK
;
5360 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5361 TALLOC_CTX
*tmp_ctx
,
5365 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5367 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5368 TALLOC_CTX
*tmp_ctx
,
5372 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5375 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5376 TALLOC_CTX
*mem_ctx
,
5377 struct samu
*sampass
)
5379 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5380 LDAPMessage
*entry
= NULL
;
5381 LDAPMessage
*result
= NULL
;
5383 LDAPMod
**mods
= NULL
;
5385 char *escape_username
;
5387 const char *dn
= NULL
;
5391 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5393 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5394 DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
5395 return NT_STATUS_UNSUCCESSFUL
;
5397 gidstr
= talloc_asprintf(mem_ctx
, "%d", gid
);
5399 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5400 return NT_STATUS_NO_MEMORY
;
5403 escape_username
= escape_ldap_string_alloc(pdb_get_username(sampass
));
5404 if (escape_username
== NULL
) {
5405 return NT_STATUS_NO_MEMORY
;
5408 filter
= talloc_asprintf(mem_ctx
,
5411 "(objectClass=%s))",
5413 LDAP_OBJ_POSIXACCOUNT
,
5414 LDAP_OBJ_SAMBASAMACCOUNT
);
5416 SAFE_FREE(escape_username
);
5418 if (filter
== NULL
) {
5419 return NT_STATUS_NO_MEMORY
;
5422 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5423 if (rc
!= LDAP_SUCCESS
) {
5424 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5425 return NT_STATUS_UNSUCCESSFUL
;
5427 talloc_autofree_ldapmsg(mem_ctx
, result
);
5429 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5431 if (num_result
== 0) {
5432 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5433 return NT_STATUS_NO_SUCH_USER
;
5436 if (num_result
> 1) {
5437 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5438 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5441 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5443 return NT_STATUS_UNSUCCESSFUL
;
5446 /* retrieve the dn for later use */
5447 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5449 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5450 return NT_STATUS_NO_MEMORY
;
5453 /* remove the old one, and add the new one, this way we do not risk races */
5454 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5457 return NT_STATUS_OK
;
5460 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5462 if (rc
!= LDAP_SUCCESS
) {
5463 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5464 pdb_get_username(sampass
), gidstr
));
5465 return NT_STATUS_UNSUCCESSFUL
;
5468 flush_pwnam_cache();
5470 return NT_STATUS_OK
;
5474 /**********************************************************************
5475 trusted domains functions
5476 *********************************************************************/
5478 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
5481 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
5482 ldap_state
->domain_dn
);
5485 static BOOL
get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
5486 const char *domain
, LDAPMessage
**entry
)
5490 int scope
= LDAP_SCOPE_SUBTREE
;
5491 const char **attrs
= NULL
; /* NULL: get all attrs */
5492 int attrsonly
= 0; /* 0: return values too */
5493 LDAPMessage
*result
= NULL
;
5497 filter
= talloc_asprintf(talloc_tos(),
5498 "(&(objectClass=%s)(sambaDomainName=%s))",
5499 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
5501 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5502 if (trusted_dn
== NULL
) {
5505 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
5506 filter
, attrs
, attrsonly
, &result
);
5508 if (rc
== LDAP_NO_SUCH_OBJECT
) {
5513 if (rc
!= LDAP_SUCCESS
) {
5517 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5519 if (num_result
> 1) {
5520 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5521 "sambaTrustedDomainPassword object for domain '%s'"
5526 if (num_result
== 0) {
5527 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5528 "sambaTrustedDomainPassword object for domain %s.\n",
5532 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5538 static BOOL
ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
5542 time_t *pass_last_set_time
)
5544 struct ldapsam_privates
*ldap_state
=
5545 (struct ldapsam_privates
*)methods
->private_data
;
5546 LDAPMessage
*entry
= NULL
;
5548 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
5550 if (!get_trusteddom_pw_int(ldap_state
, domain
, &entry
) ||
5559 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5560 entry
, "sambaClearTextPassword", talloc_tos());
5561 if (pwd_str
== NULL
) {
5564 /* trusteddom_pw routines do not use talloc yet... */
5565 *pwd
= SMB_STRDUP(pwd_str
);
5571 /* last change time */
5572 if (pass_last_set_time
!= NULL
) {
5574 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5575 entry
, "sambaPwdLastSet", talloc_tos());
5576 if (time_str
== NULL
) {
5579 *pass_last_set_time
= (time_t)atol(time_str
);
5586 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5589 if (sid_str
== NULL
) {
5592 dom_sid
= string_sid_talloc(talloc_tos(), sid_str
);
5593 if (dom_sid
== NULL
) {
5596 sid_copy(sid
, dom_sid
);
5602 static BOOL
ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
5607 struct ldapsam_privates
*ldap_state
=
5608 (struct ldapsam_privates
*)methods
->private_data
;
5609 LDAPMessage
*entry
= NULL
;
5610 LDAPMod
**mods
= NULL
;
5611 char *prev_pwd
= NULL
;
5612 char *trusted_dn
= NULL
;
5615 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
5618 * get the current entry (if there is one) in order to put the
5619 * current password into the previous password attribute
5621 if (!get_trusteddom_pw_int(ldap_state
, domain
, &entry
)) {
5626 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
5627 "sambaTrustedDomainPassword");
5628 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
5630 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
5631 sid_string_tos(sid
));
5632 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
5633 talloc_asprintf(talloc_tos(), "%li", time(NULL
)));
5634 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5635 "sambaClearTextPassword", pwd
);
5636 if (entry
!= NULL
) {
5637 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5638 entry
, "sambaClearTextPassword", talloc_tos());
5639 if (prev_pwd
!= NULL
) {
5640 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5641 "sambaPreviousClearTextPassword",
5646 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5647 if (trusted_dn
== NULL
) {
5650 if (entry
== NULL
) {
5651 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5653 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5656 if (rc
!= LDAP_SUCCESS
) {
5657 DEBUG(1, ("error writing trusted domain password!\n"));
5664 static BOOL
ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
5668 struct ldapsam_privates
*ldap_state
=
5669 (struct ldapsam_privates
*)methods
->private_data
;
5670 LDAPMessage
*entry
= NULL
;
5671 const char *trusted_dn
;
5673 if (!get_trusteddom_pw_int(ldap_state
, domain
, &entry
)) {
5677 if (entry
== NULL
) {
5678 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
5683 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
5685 if (trusted_dn
== NULL
) {
5686 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
5690 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
5691 if (rc
!= LDAP_SUCCESS
) {
5698 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
5699 TALLOC_CTX
*mem_ctx
,
5700 uint32
*num_domains
,
5701 struct trustdom_info
***domains
)
5704 struct ldapsam_privates
*ldap_state
=
5705 (struct ldapsam_privates
*)methods
->private_data
;
5707 int scope
= LDAP_SCOPE_SUBTREE
;
5708 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
5709 int attrsonly
= 0; /* 0: return values too */
5710 LDAPMessage
*result
= NULL
;
5711 LDAPMessage
*entry
= NULL
;
5713 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
5714 LDAP_OBJ_TRUSTDOM_PASSWORD
);
5716 rc
= smbldap_search(ldap_state
->smbldap_state
,
5717 ldap_state
->domain_dn
,
5724 if (rc
!= LDAP_SUCCESS
) {
5725 return NT_STATUS_UNSUCCESSFUL
;
5729 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
5730 DEBUG(1, ("talloc failed\n"));
5731 return NT_STATUS_NO_MEMORY
;
5734 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5736 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
5738 char *dom_name
, *dom_sid_str
;
5739 struct trustdom_info
*dom_info
;
5741 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
5742 if (dom_info
== NULL
) {
5743 DEBUG(1, ("talloc failed\n"));
5744 return NT_STATUS_NO_MEMORY
;
5747 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5751 if (dom_name
== NULL
) {
5752 DEBUG(1, ("talloc failed\n"));
5753 return NT_STATUS_NO_MEMORY
;
5755 dom_info
->name
= dom_name
;
5757 dom_sid_str
= smbldap_talloc_single_attribute(
5758 priv2ld(ldap_state
), entry
, "sambaSID",
5760 if (dom_sid_str
== NULL
) {
5761 DEBUG(1, ("talloc failed\n"));
5762 return NT_STATUS_NO_MEMORY
;
5764 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
5765 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
5767 return NT_STATUS_UNSUCCESSFUL
;
5770 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
5771 domains
, num_domains
);
5773 if (*domains
== NULL
) {
5774 DEBUG(1, ("talloc failed\n"));
5775 return NT_STATUS_NO_MEMORY
;
5779 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
5780 return NT_STATUS_OK
;
5784 /**********************************************************************
5786 *********************************************************************/
5788 static void free_private_data(void **vp
)
5790 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
5792 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
5794 if ((*ldap_state
)->result
!= NULL
) {
5795 ldap_msgfree((*ldap_state
)->result
);
5796 (*ldap_state
)->result
= NULL
;
5798 if ((*ldap_state
)->domain_dn
!= NULL
) {
5799 SAFE_FREE((*ldap_state
)->domain_dn
);
5804 /* No need to free any further, as it is talloc()ed */
5807 /*********************************************************************
5808 Intitalise the parts of the pdb_methods structure that are common to
5810 *********************************************************************/
5812 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
5815 struct ldapsam_privates
*ldap_state
;
5817 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
5821 (*pdb_method
)->name
= "ldapsam";
5823 (*pdb_method
)->setsampwent
= ldapsam_setsampwent
;
5824 (*pdb_method
)->endsampwent
= ldapsam_endsampwent
;
5825 (*pdb_method
)->getsampwent
= ldapsam_getsampwent
;
5826 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
5827 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
5828 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
5829 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
5830 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
5831 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
5833 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
5834 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
5835 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
5836 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
5837 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
5838 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
5839 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
5841 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
5842 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
5844 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
5846 (*pdb_method
)->rid_algorithm
= ldapsam_rid_algorithm
;
5847 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
5849 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
5850 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
5851 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
5852 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
5854 /* TODO: Setup private data and free */
5856 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
5857 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
5858 return NT_STATUS_NO_MEMORY
;
5861 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
5862 location
, &ldap_state
->smbldap_state
);
5864 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5868 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
5869 return NT_STATUS_NO_MEMORY
;
5872 (*pdb_method
)->private_data
= ldap_state
;
5874 (*pdb_method
)->free_private_data
= free_private_data
;
5876 return NT_STATUS_OK
;
5879 /**********************************************************************
5880 Initialise the 'compat' mode for pdb_ldap
5881 *********************************************************************/
5883 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
5886 struct ldapsam_privates
*ldap_state
;
5887 char *uri
= talloc_strdup( NULL
, location
);
5889 trim_char( uri
, '\"', '\"' );
5890 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
5894 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5898 (*pdb_method
)->name
= "ldapsam_compat";
5900 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
5901 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
5903 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
5905 return NT_STATUS_OK
;
5908 /**********************************************************************
5909 Initialise the normal mode for pdb_ldap
5910 *********************************************************************/
5912 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
5915 struct ldapsam_privates
*ldap_state
;
5916 uint32 alg_rid_base
;
5917 pstring alg_rid_base_string
;
5918 LDAPMessage
*result
= NULL
;
5919 LDAPMessage
*entry
= NULL
;
5920 DOM_SID ldap_domain_sid
;
5921 DOM_SID secrets_domain_sid
;
5922 pstring domain_sid_string
;
5924 char *uri
= talloc_strdup( NULL
, location
);
5926 trim_char( uri
, '\"', '\"' );
5927 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
5931 if (!NT_STATUS_IS_OK(nt_status
)) {
5935 (*pdb_method
)->name
= "ldapsam";
5937 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
5938 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
5939 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
5940 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
5941 (*pdb_method
)->search_users
= ldapsam_search_users
;
5942 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
5943 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
5945 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
5946 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
5947 (*pdb_method
)->enum_group_memberships
=
5948 ldapsam_enum_group_memberships
;
5949 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
5950 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
5952 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
5953 (*pdb_method
)->create_user
= ldapsam_create_user
;
5954 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
5955 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
5956 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
5957 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
5958 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
5959 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
5963 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
5964 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
5966 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
5968 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
5970 ldap_state
->domain_name
, True
);
5972 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5973 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
5974 "info, nor add one to the domain\n"));
5975 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
5976 "will be unable to allocate new users/groups, "
5977 "and will risk BDCs having inconsistant SIDs\n"));
5978 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
5979 return NT_STATUS_OK
;
5982 /* Given that the above might fail, everything below this must be
5985 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
5988 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
5990 ldap_msgfree(result
);
5991 return NT_STATUS_UNSUCCESSFUL
;
5994 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
5996 ldap_msgfree(result
);
5997 return NT_STATUS_UNSUCCESSFUL
;
6000 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6003 if (smbldap_get_single_pstring(
6004 ldap_state
->smbldap_state
->ldap_struct
,
6006 get_userattr_key2string(ldap_state
->schema_ver
,
6007 LDAP_ATTR_USER_SID
),
6008 domain_sid_string
)) {
6010 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6011 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6012 "read as a valid SID\n", domain_sid_string
));
6013 ldap_msgfree(result
);
6014 return NT_STATUS_INVALID_PARAMETER
;
6016 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6017 &secrets_domain_sid
);
6018 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
6019 &ldap_domain_sid
)) {
6020 fstring new_sid_str
, old_sid_str
;
6021 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6022 "%s based on pdb_ldap results %s -> %s\n",
6023 ldap_state
->domain_name
,
6024 sid_to_string(old_sid_str
,
6025 &secrets_domain_sid
),
6026 sid_to_string(new_sid_str
,
6027 &ldap_domain_sid
)));
6029 /* reset secrets.tdb sid */
6030 secrets_store_domain_sid(ldap_state
->domain_name
,
6032 DEBUG(1, ("New global sam SID: %s\n",
6033 sid_to_string(new_sid_str
,
6034 get_global_sam_sid())));
6036 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6039 if (smbldap_get_single_pstring(
6040 ldap_state
->smbldap_state
->ldap_struct
,
6042 get_attr_key2string( dominfo_attr_list
,
6043 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6044 alg_rid_base_string
)) {
6045 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
6046 if (alg_rid_base
!= algorithmic_rid_base()) {
6047 DEBUG(0, ("The value of 'algorithmic RID base' has "
6048 "changed since the LDAP\n"
6049 "database was initialised. Aborting. \n"));
6050 ldap_msgfree(result
);
6051 return NT_STATUS_UNSUCCESSFUL
;
6054 ldap_msgfree(result
);
6056 return NT_STATUS_OK
;
6059 NTSTATUS
pdb_ldap_init(void)
6062 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6065 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6068 /* Let pdb_nds register backends */
6071 return NT_STATUS_OK
;