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 2 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, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * persistent connections: if using NSS LDAP, many connections are made
29 * however, using only one within Samba would be nice
31 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
33 * Other LDAP based login attributes: accountExpires, etc.
34 * (should be the domain of Samba proper, but the sam_password/struct samu
35 * structures don't have fields for some of these attributes)
37 * SSL is done, but can't get the certificate based authentication to work
38 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
41 /* NOTE: this will NOT work against an Active Directory server
42 * due to the fact that the two password fields cannot be retrieved
43 * from a server; recommend using security = domain in this situation
50 #define DBGC_CLASS DBGC_PASSDB
56 * Work around versions of the LDAP client libs that don't have the OIDs
57 * defined, or have them defined under the old name.
58 * This functionality is really a factor of the server, not the client
62 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
63 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
64 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
65 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
68 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
69 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
70 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
71 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
74 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
75 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
76 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
77 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
83 /**********************************************************************
84 Simple helper function to make stuff better readable
85 **********************************************************************/
87 static LDAP
*priv2ld(struct ldapsam_privates
*priv
)
89 return priv
->smbldap_state
->ldap_struct
;
92 /**********************************************************************
93 Get the attribute name given a user schame version.
94 **********************************************************************/
96 static const char* get_userattr_key2string( int schema_ver
, int key
)
98 switch ( schema_ver
) {
99 case SCHEMAVER_SAMBAACCOUNT
:
100 return get_attr_key2string( attrib_map_v22
, key
);
102 case SCHEMAVER_SAMBASAMACCOUNT
:
103 return get_attr_key2string( attrib_map_v30
, key
);
106 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
112 /**********************************************************************
113 Return the list of attribute names given a user schema version.
114 **********************************************************************/
116 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
118 switch ( schema_ver
) {
119 case SCHEMAVER_SAMBAACCOUNT
:
120 return get_attr_list( mem_ctx
, attrib_map_v22
);
122 case SCHEMAVER_SAMBASAMACCOUNT
:
123 return get_attr_list( mem_ctx
, attrib_map_v30
);
125 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
132 /**************************************************************************
133 Return the list of attribute names to delete given a user schema version.
134 **************************************************************************/
136 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
139 switch ( schema_ver
) {
140 case SCHEMAVER_SAMBAACCOUNT
:
141 return get_attr_list( mem_ctx
,
142 attrib_map_to_delete_v22
);
144 case SCHEMAVER_SAMBASAMACCOUNT
:
145 return get_attr_list( mem_ctx
,
146 attrib_map_to_delete_v30
);
148 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
156 /*******************************************************************
157 Generate the LDAP search filter for the objectclass based on the
158 version of the schema we are using.
159 ******************************************************************/
161 static const char* get_objclass_filter( int schema_ver
)
163 static fstring objclass_filter
;
165 switch( schema_ver
) {
166 case SCHEMAVER_SAMBAACCOUNT
:
167 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
169 case SCHEMAVER_SAMBASAMACCOUNT
:
170 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
173 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
177 return objclass_filter
;
180 /*****************************************************************
181 Scan a sequence number off OpenLDAP's syncrepl contextCSN
182 ******************************************************************/
184 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
186 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
187 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
188 LDAPMessage
*msg
= NULL
;
189 LDAPMessage
*entry
= NULL
;
191 char **values
= NULL
;
192 int rc
, num_result
, num_values
, rid
;
198 /* Unfortunatly there is no proper way to detect syncrepl-support in
199 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
200 * but do not show up in the root-DSE yet. Neither we can query the
201 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
202 * objectclass. Currently we require lp_ldap_suffix() to show up as
203 * namingContext. - Guenther
206 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
211 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
215 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
216 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
217 "as top-level namingContext\n", lp_ldap_suffix()));
221 mem_ctx
= talloc_init("ldapsam_get_seq_num");
224 return NT_STATUS_NO_MEMORY
;
226 if ((attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2)) == NULL
) {
227 ntstatus
= NT_STATUS_NO_MEMORY
;
231 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
232 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
235 /* consumer syncreplCookie: */
236 /* csn=20050126161620Z#0000001#00#00000 */
237 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
239 pstr_sprintf( suffix
, "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
243 /* provider contextCSN */
244 /* 20050126161620Z#000009#00#000000 */
245 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
247 pstr_sprintf( suffix
, "cn=ldapsync,%s", lp_ldap_suffix());
251 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
252 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
254 if (rc
!= LDAP_SUCCESS
) {
258 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
259 if (num_result
!= 1) {
260 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
264 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
266 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
270 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
271 if (values
== NULL
) {
272 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
276 num_values
= ldap_count_values(values
);
277 if (num_values
== 0) {
278 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
283 if (!next_token(&p
, tok
, "#", sizeof(tok
))) {
284 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
289 if (!strncmp(p
, "csn=", strlen("csn=")))
292 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
294 *seq_num
= generalized_to_unix_time(p
);
296 /* very basic sanity check */
298 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
303 ntstatus
= NT_STATUS_OK
;
307 ldap_value_free(values
);
311 talloc_destroy(mem_ctx
);
316 /*******************************************************************
317 Run the search by name.
318 ******************************************************************/
320 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
322 LDAPMessage
** result
,
326 char *escape_user
= escape_ldap_string_alloc(user
);
329 return LDAP_NO_MEMORY
;
333 * in the filter expression, replace %u with the real name
334 * so in ldap filter, %u MUST exist :-)
336 pstr_sprintf(filter
, "(&%s%s)", "(uid=%u)",
337 get_objclass_filter(ldap_state
->schema_ver
));
340 * have to use this here because $ is filtered out
345 all_string_sub(filter
, "%u", escape_user
, sizeof(pstring
));
346 SAFE_FREE(escape_user
);
348 return smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
351 /*******************************************************************
352 Run the search by rid.
353 ******************************************************************/
355 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
356 uint32 rid
, LDAPMessage
** result
,
362 pstr_sprintf(filter
, "(&(rid=%i)%s)", rid
,
363 get_objclass_filter(ldap_state
->schema_ver
));
365 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
370 /*******************************************************************
371 Run the search by SID.
372 ******************************************************************/
374 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
375 const DOM_SID
*sid
, LDAPMessage
** result
,
382 pstr_sprintf(filter
, "(&(%s=%s)%s)",
383 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
384 sid_to_string(sid_string
, sid
),
385 get_objclass_filter(ldap_state
->schema_ver
));
387 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
392 /*******************************************************************
393 Delete complete object or objectclass and attrs from
394 object found in search_result depending on lp_ldap_delete_dn
395 ******************************************************************/
397 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
400 const char *objectclass
,
403 LDAPMod
**mods
= NULL
;
406 BerElement
*ptr
= NULL
;
408 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
410 return LDAP_NO_MEMORY
;
413 if (lp_ldap_delete_dn()) {
414 return smbldap_delete(priv
->smbldap_state
, dn
);
417 /* Ok, delete only the SAM attributes */
419 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
421 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
424 /* We are only allowed to delete the attributes that
427 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
428 if (strequal(*attrib
, name
)) {
429 DEBUG(10, ("ldapsam_delete_entry: deleting "
430 "attribute %s\n", name
));
431 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
442 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
443 talloc_autofree_ldapmod(mem_ctx
, mods
);
445 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
448 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
453 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
454 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
458 strptime(temp
, "%Y%m%d%H%M%SZ", &tm
);
463 /**********************************************************************
464 Initialize struct samu from an LDAP query.
465 (Based on init_sam_from_buffer in pdb_tdb.c)
466 *********************************************************************/
468 static BOOL
init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
469 struct samu
* sampass
,
476 pass_can_change_time
,
477 pass_must_change_time
,
490 char munged_dial
[2048];
492 uint8 smblmpwd
[LM_HASH_LEN
],
493 smbntpwd
[NT_HASH_LEN
];
494 BOOL use_samba_attrs
= True
;
495 uint32 acct_ctrl
= 0;
497 uint16 bad_password_count
= 0,
500 uint8 hours
[MAX_HOURS_LEN
];
502 LOGIN_CACHE
*cache_entry
= NULL
;
505 BOOL expand_explicit
= lp_passdb_expand_explicit();
508 * do a little initialization
512 nt_username
[0] = '\0';
516 logon_script
[0] = '\0';
517 profile_path
[0] = '\0';
519 munged_dial
[0] = '\0';
520 workstations
[0] = '\0';
523 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
524 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
528 if (priv2ld(ldap_state
) == NULL
) {
529 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
530 "ldap_struct is NULL!\n"));
534 if (!smbldap_get_single_pstring(priv2ld(ldap_state
), entry
, "uid",
536 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
541 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
543 pstrcpy(nt_username
, username
);
545 pstrcpy(domain
, ldap_state
->domain_name
);
547 pdb_set_username(sampass
, username
, PDB_SET
);
549 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
550 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
552 /* deal with different attributes between the schema first */
554 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
555 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
556 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
), temp
)) {
557 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
560 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
561 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
), temp
)) {
562 user_rid
= (uint32
)atol(temp
);
563 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
567 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
568 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
569 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
570 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
575 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
576 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
), temp
)) {
577 /* leave as default */
579 pass_last_set_time
= (time_t) atol(temp
);
580 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
, PDB_SET
);
583 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
584 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
)) {
585 /* leave as default */
587 logon_time
= (time_t) atol(temp
);
588 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
591 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
592 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
)) {
593 /* leave as default */
595 logoff_time
= (time_t) atol(temp
);
596 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
599 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
600 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
)) {
601 /* leave as default */
603 kickoff_time
= (time_t) atol(temp
);
604 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
607 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
608 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
)) {
609 /* leave as default */
611 pass_can_change_time
= (time_t) atol(temp
);
612 pdb_set_pass_can_change_time(sampass
, pass_can_change_time
, PDB_SET
);
615 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
616 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
)) {
617 /* leave as default */
619 pass_must_change_time
= (time_t) atol(temp
);
620 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, PDB_SET
);
623 /* recommend that 'gecos' and 'displayName' should refer to the same
624 * attribute OID. userFullName depreciated, only used by Samba
625 * primary rules of LDAP: don't make a new attribute when one is already defined
626 * that fits your needs; using cn then displayName rather than 'userFullName'
629 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
630 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
), fullname
)) {
631 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
632 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_CN
), fullname
)) {
633 /* leave as default */
635 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
638 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
641 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
642 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
), dir_drive
))
644 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
646 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
649 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
650 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
), homedir
))
652 pdb_set_homedir( sampass
,
653 talloc_sub_basic(sampass
, username
, domain
,
657 pstrcpy( tmpstring
, homedir
);
658 if (expand_explicit
) {
659 standard_sub_basic( username
, domain
, tmpstring
,
662 pdb_set_homedir(sampass
, tmpstring
, PDB_SET
);
665 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
666 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
), logon_script
))
668 pdb_set_logon_script( sampass
,
669 talloc_sub_basic(sampass
, username
, domain
,
673 pstrcpy( tmpstring
, logon_script
);
674 if (expand_explicit
) {
675 standard_sub_basic( username
, domain
, tmpstring
,
678 pdb_set_logon_script(sampass
, tmpstring
, PDB_SET
);
681 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
682 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
), profile_path
))
684 pdb_set_profile_path( sampass
,
685 talloc_sub_basic( sampass
, username
, domain
,
689 pstrcpy( tmpstring
, profile_path
);
690 if (expand_explicit
) {
691 standard_sub_basic( username
, domain
, tmpstring
,
694 pdb_set_profile_path(sampass
, tmpstring
, PDB_SET
);
697 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
698 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
), acct_desc
))
700 /* leave as default */
702 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
705 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
706 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
), workstations
)) {
707 /* leave as default */;
709 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
712 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
713 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
), munged_dial
, sizeof(munged_dial
))) {
714 /* leave as default */;
716 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
719 /* FIXME: hours stuff should be cleaner */
723 memset(hours
, 0xff, hours_len
);
725 if (ldap_state
->is_nds_ldap
) {
728 char clear_text_pw
[512];
730 /* Make call to Novell eDirectory ldap extension to get clear text password.
731 NOTE: This will only work if we have an SSL connection to eDirectory. */
732 user_dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
733 if (user_dn
!= NULL
) {
734 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username
, user_dn
));
736 pwd_len
= sizeof(clear_text_pw
);
737 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
738 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
739 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
743 ZERO_STRUCT(smblmpwd
);
744 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
748 ZERO_STRUCT(smbntpwd
);
749 use_samba_attrs
= False
;
755 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
759 if (use_samba_attrs
) {
760 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
761 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
), temp
)) {
762 /* leave as default */
764 pdb_gethexpwd(temp
, smblmpwd
);
765 memset((char *)temp
, '\0', strlen(temp
)+1);
766 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
))
768 ZERO_STRUCT(smblmpwd
);
771 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
772 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
), temp
)) {
773 /* leave as default */
775 pdb_gethexpwd(temp
, smbntpwd
);
776 memset((char *)temp
, '\0', strlen(temp
)+1);
777 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
))
779 ZERO_STRUCT(smbntpwd
);
785 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
787 uint8
*pwhist
= NULL
;
789 char history_string
[MAX_PW_HISTORY_LEN
*64];
791 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
793 if ((pwhist
= SMB_MALLOC_ARRAY(uint8
, pwHistLen
* PW_HISTORY_ENTRY_LEN
)) == NULL
){
794 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
797 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
799 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
800 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
801 history_string
, sizeof(history_string
))) {
802 /* leave as default - zeros */
804 BOOL hex_failed
= False
;
805 for (i
= 0; i
< pwHistLen
; i
++){
806 /* Get the 16 byte salt. */
807 if (!pdb_gethexpwd(&history_string
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
811 /* Get the 16 byte MD5 hash of salt+passwd. */
812 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
813 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
])) {
819 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
821 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
824 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
831 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
832 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
), temp
)) {
833 acct_ctrl
|= ACB_NORMAL
;
835 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
838 acct_ctrl
|= ACB_NORMAL
;
840 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
843 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
844 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
846 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
847 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_COUNT
), temp
)) {
848 /* leave as default */
850 bad_password_count
= (uint32
) atol(temp
);
851 pdb_set_bad_password_count(sampass
, bad_password_count
, PDB_SET
);
854 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
855 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_TIME
), temp
)) {
856 /* leave as default */
858 bad_password_time
= (time_t) atol(temp
);
859 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
863 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
864 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_COUNT
), temp
)) {
865 /* leave as default */
867 logon_count
= (uint32
) atol(temp
);
868 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
871 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
873 if(!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
874 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_HOURS
), temp
)) {
875 /* leave as default */
877 pdb_gethexhours(temp
, hours
);
878 memset((char *)temp
, '\0', strlen(temp
) +1);
879 pdb_set_hours(sampass
, hours
, PDB_SET
);
883 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
884 if (smbldap_get_single_pstring(priv2ld(ldap_state
), entry
,
885 "uidNumber", temp
)) {
886 /* We've got a uid, feed the cache */
887 uid_t uid
= strtoul(temp
, NULL
, 10);
888 store_uid_sid_cache(pdb_get_user_sid(sampass
), uid
);
892 /* check the timestamp of the cache vs ldap entry */
893 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
897 /* see if we have newer updates */
898 if (!(cache_entry
= login_cache_read(sampass
))) {
899 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
900 (unsigned int)pdb_get_bad_password_count(sampass
),
901 (unsigned int)pdb_get_bad_password_time(sampass
)));
905 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
906 (unsigned int)ldap_entry_time
, (unsigned int)cache_entry
->entry_timestamp
,
907 (unsigned int)cache_entry
->bad_password_time
));
909 if (ldap_entry_time
> cache_entry
->entry_timestamp
) {
910 /* cache is older than directory , so
911 we need to delete the entry but allow the
912 fields to be written out */
913 login_cache_delentry(sampass
);
916 pdb_set_acct_ctrl(sampass
,
917 pdb_get_acct_ctrl(sampass
) |
918 (cache_entry
->acct_ctrl
& ACB_AUTOLOCK
),
920 pdb_set_bad_password_count(sampass
,
921 cache_entry
->bad_password_count
,
923 pdb_set_bad_password_time(sampass
,
924 cache_entry
->bad_password_time
,
928 SAFE_FREE(cache_entry
);
932 /**********************************************************************
933 Initialize the ldap db from a struct samu. Called on update.
934 (Based on init_buffer_from_sam in pdb_tdb.c)
935 *********************************************************************/
937 static BOOL
init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
938 LDAPMessage
*existing
,
939 LDAPMod
*** mods
, struct samu
* sampass
,
940 BOOL (*need_update
)(const struct samu
*,
946 if (mods
== NULL
|| sampass
== NULL
) {
947 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
954 * took out adding "objectclass: sambaAccount"
955 * do this on a per-mod basis
957 if (need_update(sampass
, PDB_USERNAME
)) {
958 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
959 "uid", pdb_get_username(sampass
));
960 if (ldap_state
->is_nds_ldap
) {
961 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
962 "cn", pdb_get_username(sampass
));
963 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
964 "sn", pdb_get_username(sampass
));
968 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
970 /* only update the RID if we actually need to */
971 if (need_update(sampass
, PDB_USERSID
)) {
973 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
975 switch ( ldap_state
->schema_ver
) {
976 case SCHEMAVER_SAMBAACCOUNT
:
977 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
978 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
979 sid_string_static(user_sid
),
980 sid_string_static(&ldap_state
->domain_sid
)));
983 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
984 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
985 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
989 case SCHEMAVER_SAMBASAMACCOUNT
:
990 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
991 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
992 sid_to_string(sid_string
, user_sid
));
996 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1001 /* we don't need to store the primary group RID - so leaving it
1002 'free' to hang off the unix primary group makes life easier */
1004 if (need_update(sampass
, PDB_GROUPSID
)) {
1006 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
1008 switch ( ldap_state
->schema_ver
) {
1009 case SCHEMAVER_SAMBAACCOUNT
:
1010 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1011 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1012 sid_string_static(group_sid
),
1013 sid_string_static(&ldap_state
->domain_sid
)));
1017 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
1018 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1019 get_userattr_key2string(ldap_state
->schema_ver
,
1020 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1023 case SCHEMAVER_SAMBASAMACCOUNT
:
1024 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1025 get_userattr_key2string(ldap_state
->schema_ver
,
1026 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_string(sid_string
, group_sid
));
1030 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1036 /* displayName, cn, and gecos should all be the same
1037 * most easily accomplished by giving them the same OID
1038 * gecos isn't set here b/c it should be handled by the
1040 * We change displayName only and fall back to cn if
1041 * it does not exist.
1044 if (need_update(sampass
, PDB_FULLNAME
))
1045 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1046 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1047 pdb_get_fullname(sampass
));
1049 if (need_update(sampass
, PDB_ACCTDESC
))
1050 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1051 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1052 pdb_get_acct_desc(sampass
));
1054 if (need_update(sampass
, PDB_WORKSTATIONS
))
1055 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1056 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1057 pdb_get_workstations(sampass
));
1059 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1060 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1061 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1062 pdb_get_munged_dial(sampass
));
1064 if (need_update(sampass
, PDB_SMBHOME
))
1065 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1066 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1067 pdb_get_homedir(sampass
));
1069 if (need_update(sampass
, PDB_DRIVE
))
1070 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1071 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1072 pdb_get_dir_drive(sampass
));
1074 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1075 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1076 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1077 pdb_get_logon_script(sampass
));
1079 if (need_update(sampass
, PDB_PROFILE
))
1080 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1081 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1082 pdb_get_profile_path(sampass
));
1084 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logon_time(sampass
));
1085 if (need_update(sampass
, PDB_LOGONTIME
))
1086 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1087 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1089 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logoff_time(sampass
));
1090 if (need_update(sampass
, PDB_LOGOFFTIME
))
1091 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1092 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1094 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_kickoff_time(sampass
));
1095 if (need_update(sampass
, PDB_KICKOFFTIME
))
1096 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1097 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1099 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_can_change_time_noncalc(sampass
));
1100 if (need_update(sampass
, PDB_CANCHANGETIME
))
1101 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1102 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1104 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_must_change_time(sampass
));
1105 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1106 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1107 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1110 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1111 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1113 if (need_update(sampass
, PDB_LMPASSWD
)) {
1114 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1116 pdb_sethexpwd(temp
, lm_pw
,
1117 pdb_get_acct_ctrl(sampass
));
1118 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1119 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1122 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1123 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1127 if (need_update(sampass
, PDB_NTPASSWD
)) {
1128 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1130 pdb_sethexpwd(temp
, nt_pw
,
1131 pdb_get_acct_ctrl(sampass
));
1132 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1133 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1136 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1137 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1142 if (need_update(sampass
, PDB_PWHISTORY
)) {
1143 uint32 pwHistLen
= 0;
1144 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
1145 if (pwHistLen
== 0) {
1146 /* Remove any password history from the LDAP store. */
1147 memset(temp
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1151 uint32 currHistLen
= 0;
1152 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1153 if (pwhist
!= NULL
) {
1154 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1155 pwHistLen
= MIN(pwHistLen
, ((sizeof(temp
)-1)/64));
1156 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1157 /* Store the salt. */
1158 pdb_sethexpwd(&temp
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1159 /* Followed by the md5 hash of salt + md4 hash */
1160 pdb_sethexpwd(&temp
[(i
*64)+32],
1161 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1162 DEBUG(100, ("temp=%s\n", temp
));
1166 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1167 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1171 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1172 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_last_set_time(sampass
));
1173 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1174 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1179 if (need_update(sampass
, PDB_HOURS
)) {
1180 const uint8
*hours
= pdb_get_hours(sampass
);
1182 pdb_sethexhours(temp
, hours
);
1183 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1186 get_userattr_key2string(ldap_state
->schema_ver
,
1187 LDAP_ATTR_LOGON_HOURS
),
1192 if (need_update(sampass
, PDB_ACCTCTRL
))
1193 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1194 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1195 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1197 /* password lockout cache:
1198 - If we are now autolocking or clearing, we write to ldap
1199 - If we are clearing, we delete the cache entry
1200 - If the count is > 0, we update the cache
1202 This even means when autolocking, we cache, just in case the
1203 update doesn't work, and we have to cache the autolock flag */
1205 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1206 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1207 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1208 time_t badtime
= pdb_get_bad_password_time(sampass
);
1210 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, &pol
);
1212 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1213 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1215 if ((badcount
>= pol
) || (badcount
== 0)) {
1216 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1217 (unsigned int)badcount
, (unsigned int)badtime
));
1218 slprintf (temp
, sizeof (temp
) - 1, "%li", (long)badcount
);
1220 ldap_state
->smbldap_state
->ldap_struct
,
1222 get_userattr_key2string(
1223 ldap_state
->schema_ver
,
1224 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1227 slprintf (temp
, sizeof (temp
) - 1, "%li", badtime
);
1229 ldap_state
->smbldap_state
->ldap_struct
,
1231 get_userattr_key2string(
1232 ldap_state
->schema_ver
,
1233 LDAP_ATTR_BAD_PASSWORD_TIME
),
1236 if (badcount
== 0) {
1237 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1238 login_cache_delentry(sampass
);
1240 LOGIN_CACHE cache_entry
;
1242 cache_entry
.entry_timestamp
= time(NULL
);
1243 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1244 cache_entry
.bad_password_count
= badcount
;
1245 cache_entry
.bad_password_time
= badtime
;
1247 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1248 login_cache_write(sampass
, cache_entry
);
1255 /**********************************************************************
1256 Connect to LDAP server for password enumeration.
1257 *********************************************************************/
1259 static NTSTATUS
ldapsam_setsampwent(struct pdb_methods
*my_methods
, BOOL update
, uint32 acb_mask
)
1261 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1263 pstring filter
, suffix
;
1264 const char **attr_list
;
1265 BOOL machine_mask
= False
, user_mask
= False
;
1267 pstr_sprintf( filter
, "(&%s%s)", "(uid=%u)",
1268 get_objclass_filter(ldap_state
->schema_ver
));
1269 all_string_sub(filter
, "%u", "*", sizeof(pstring
));
1271 machine_mask
= ((acb_mask
!= 0) && (acb_mask
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)));
1272 user_mask
= ((acb_mask
!= 0) && (acb_mask
& ACB_NORMAL
));
1275 pstrcpy(suffix
, lp_ldap_machine_suffix());
1276 } else if (user_mask
) {
1277 pstrcpy(suffix
, lp_ldap_user_suffix());
1279 pstrcpy(suffix
, lp_ldap_suffix());
1282 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1285 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1286 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
, LDAP_SCOPE_SUBTREE
, filter
,
1287 attr_list
, 0, &ldap_state
->result
);
1288 TALLOC_FREE( attr_list
);
1290 if (rc
!= LDAP_SUCCESS
) {
1291 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc
)));
1292 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix
, filter
));
1293 ldap_msgfree(ldap_state
->result
);
1294 ldap_state
->result
= NULL
;
1295 return NT_STATUS_UNSUCCESSFUL
;
1298 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1299 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
1300 ldap_state
->result
), suffix
));
1302 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
1303 ldap_state
->result
);
1304 ldap_state
->index
= 0;
1306 return NT_STATUS_OK
;
1309 /**********************************************************************
1310 End enumeration of the LDAP password list.
1311 *********************************************************************/
1313 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1315 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1316 if (ldap_state
->result
) {
1317 ldap_msgfree(ldap_state
->result
);
1318 ldap_state
->result
= NULL
;
1322 /**********************************************************************
1323 Get the next entry in the LDAP password database.
1324 *********************************************************************/
1326 static NTSTATUS
ldapsam_getsampwent(struct pdb_methods
*my_methods
,
1329 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1330 struct ldapsam_privates
*ldap_state
=
1331 (struct ldapsam_privates
*)my_methods
->private_data
;
1335 if (!ldap_state
->entry
)
1338 ldap_state
->index
++;
1339 bret
= init_sam_from_ldap(ldap_state
, user
, ldap_state
->entry
);
1341 ldap_state
->entry
= ldap_next_entry(priv2ld(ldap_state
),
1345 return NT_STATUS_OK
;
1348 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1349 const char *new_attr
)
1353 if (new_attr
== NULL
) {
1357 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1361 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1363 SMB_ASSERT((*attr_list
) != NULL
);
1364 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1365 (*attr_list
)[i
+1] = NULL
;
1368 /**********************************************************************
1369 Get struct samu entry from LDAP by username.
1370 *********************************************************************/
1372 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1374 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1375 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1376 LDAPMessage
*result
= NULL
;
1377 LDAPMessage
*entry
= NULL
;
1379 const char ** attr_list
;
1382 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1383 append_attr(user
, &attr_list
,
1384 get_userattr_key2string(ldap_state
->schema_ver
,
1385 LDAP_ATTR_MOD_TIMESTAMP
));
1386 append_attr(user
, &attr_list
, "uidNumber");
1387 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1389 TALLOC_FREE( attr_list
);
1391 if ( rc
!= LDAP_SUCCESS
)
1392 return NT_STATUS_NO_SUCH_USER
;
1394 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1397 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1398 ldap_msgfree(result
);
1399 return NT_STATUS_NO_SUCH_USER
;
1400 } else if (count
> 1) {
1401 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1402 ldap_msgfree(result
);
1403 return NT_STATUS_NO_SUCH_USER
;
1406 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1408 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1409 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1410 ldap_msgfree(result
);
1411 return NT_STATUS_NO_SUCH_USER
;
1413 pdb_set_backend_private_data(user
, result
, NULL
,
1414 my_methods
, PDB_CHANGED
);
1415 talloc_autofree_ldapmsg(user
, result
);
1418 ldap_msgfree(result
);
1423 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1424 const DOM_SID
*sid
, LDAPMessage
**result
)
1427 const char ** attr_list
;
1430 switch ( ldap_state
->schema_ver
) {
1431 case SCHEMAVER_SAMBASAMACCOUNT
: {
1432 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1433 if (tmp_ctx
== NULL
) {
1434 return LDAP_NO_MEMORY
;
1437 attr_list
= get_userattr_list(tmp_ctx
,
1438 ldap_state
->schema_ver
);
1439 append_attr(tmp_ctx
, &attr_list
,
1440 get_userattr_key2string(
1441 ldap_state
->schema_ver
,
1442 LDAP_ATTR_MOD_TIMESTAMP
));
1443 append_attr(tmp_ctx
, &attr_list
, "uidNumber");
1444 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1446 TALLOC_FREE(tmp_ctx
);
1448 if ( rc
!= LDAP_SUCCESS
)
1453 case SCHEMAVER_SAMBAACCOUNT
:
1454 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1458 attr_list
= get_userattr_list(NULL
,
1459 ldap_state
->schema_ver
);
1460 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1461 TALLOC_FREE( attr_list
);
1463 if ( rc
!= LDAP_SUCCESS
)
1470 /**********************************************************************
1471 Get struct samu entry from LDAP by SID.
1472 *********************************************************************/
1474 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1476 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1477 LDAPMessage
*result
= NULL
;
1478 LDAPMessage
*entry
= NULL
;
1483 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1485 if (rc
!= LDAP_SUCCESS
)
1486 return NT_STATUS_NO_SUCH_USER
;
1488 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1491 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string
, sid
),
1493 ldap_msgfree(result
);
1494 return NT_STATUS_NO_SUCH_USER
;
1495 } else if (count
> 1) {
1496 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string
, sid
),
1498 ldap_msgfree(result
);
1499 return NT_STATUS_NO_SUCH_USER
;
1502 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1504 ldap_msgfree(result
);
1505 return NT_STATUS_NO_SUCH_USER
;
1508 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1509 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1510 ldap_msgfree(result
);
1511 return NT_STATUS_NO_SUCH_USER
;
1514 pdb_set_backend_private_data(user
, result
, NULL
,
1515 my_methods
, PDB_CHANGED
);
1516 talloc_autofree_ldapmsg(user
, result
);
1517 return NT_STATUS_OK
;
1520 /********************************************************************
1521 Do the actual modification - also change a plaintext passord if
1523 **********************************************************************/
1525 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1526 struct samu
*newpwd
, char *dn
,
1527 LDAPMod
**mods
, int ldap_op
,
1528 BOOL (*need_update
)(const struct samu
*, enum pdb_elements
))
1530 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1533 if (!newpwd
|| !dn
) {
1534 return NT_STATUS_INVALID_PARAMETER
;
1538 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1539 /* may be password change below however */
1543 if (ldap_state
->is_nds_ldap
) {
1544 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1548 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1552 rc
= smbldap_add(ldap_state
->smbldap_state
,
1555 case LDAP_MOD_REPLACE
:
1556 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1560 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1562 return NT_STATUS_INVALID_PARAMETER
;
1565 if (rc
!=LDAP_SUCCESS
) {
1566 return NT_STATUS_UNSUCCESSFUL
;
1570 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1571 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1572 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1573 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1576 char *retoid
= NULL
;
1577 struct berval
*retdata
= NULL
;
1578 char *utf8_password
;
1581 if (!ldap_state
->is_nds_ldap
) {
1583 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1584 LDAP_EXOP_MODIFY_PASSWD
)) {
1585 DEBUG(2, ("ldap password change requested, but LDAP "
1586 "server does not support it -- ignoring\n"));
1587 return NT_STATUS_OK
;
1591 if (push_utf8_allocate(&utf8_password
, pdb_get_plaintext_passwd(newpwd
)) == (size_t)-1) {
1592 return NT_STATUS_NO_MEMORY
;
1595 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1596 SAFE_FREE(utf8_password
);
1597 return NT_STATUS_NO_MEMORY
;
1600 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1601 DEBUG(0,("ber_alloc_t returns NULL\n"));
1602 SAFE_FREE(utf8_password
);
1604 return NT_STATUS_UNSUCCESSFUL
;
1607 ber_printf (ber
, "{");
1608 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
, utf8_dn
);
1609 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
, utf8_password
);
1610 ber_printf (ber
, "N}");
1612 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1613 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1616 SAFE_FREE(utf8_password
);
1617 return NT_STATUS_UNSUCCESSFUL
;
1621 SAFE_FREE(utf8_password
);
1624 if (!ldap_state
->is_nds_ldap
) {
1625 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1626 LDAP_EXOP_MODIFY_PASSWD
,
1627 bv
, NULL
, NULL
, &retoid
,
1630 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1631 pdb_get_plaintext_passwd(newpwd
));
1633 if (rc
!= LDAP_SUCCESS
) {
1634 char *ld_error
= NULL
;
1636 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1637 DEBUG(3, ("Could not set userPassword "
1638 "attribute due to an objectClass "
1639 "violation -- ignoring\n"));
1641 return NT_STATUS_OK
;
1644 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1646 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1647 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1648 SAFE_FREE(ld_error
);
1650 return NT_STATUS_UNSUCCESSFUL
;
1652 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1653 #ifdef DEBUG_PASSWORD
1654 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1657 ber_bvfree(retdata
);
1659 ldap_memfree(retoid
);
1663 return NT_STATUS_OK
;
1666 /**********************************************************************
1667 Delete entry from LDAP for username.
1668 *********************************************************************/
1670 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1671 struct samu
* sam_acct
)
1673 struct ldapsam_privates
*priv
=
1674 (struct ldapsam_privates
*)my_methods
->private_data
;
1677 LDAPMessage
*msg
, *entry
;
1678 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1679 const char **attr_list
;
1680 TALLOC_CTX
*mem_ctx
;
1683 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1684 return NT_STATUS_INVALID_PARAMETER
;
1687 sname
= pdb_get_username(sam_acct
);
1689 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1692 mem_ctx
= talloc_new(NULL
);
1693 if (mem_ctx
== NULL
) {
1694 DEBUG(0, ("talloc_new failed\n"));
1698 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1699 if (attr_list
== NULL
) {
1703 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1705 if ((rc
!= LDAP_SUCCESS
) ||
1706 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1707 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1708 DEBUG(5, ("Could not find user %s\n", sname
));
1709 result
= NT_STATUS_NO_SUCH_USER
;
1713 rc
= ldapsam_delete_entry(
1714 priv
, mem_ctx
, entry
,
1715 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1716 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1719 result
= (rc
== LDAP_SUCCESS
) ?
1720 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1723 TALLOC_FREE(mem_ctx
);
1727 /**********************************************************************
1728 Helper function to determine for update_sam_account whether
1729 we need LDAP modification.
1730 *********************************************************************/
1732 static BOOL
element_is_changed(const struct samu
*sampass
,
1733 enum pdb_elements element
)
1735 return IS_SAM_CHANGED(sampass
, element
);
1738 /**********************************************************************
1740 *********************************************************************/
1742 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1744 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1745 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1748 LDAPMessage
*result
= NULL
;
1749 LDAPMessage
*entry
= NULL
;
1750 LDAPMod
**mods
= NULL
;
1751 const char **attr_list
;
1753 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1755 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1756 if (pdb_get_username(newpwd
) == NULL
) {
1757 return NT_STATUS_INVALID_PARAMETER
;
1759 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1760 TALLOC_FREE( attr_list
);
1761 if (rc
!= LDAP_SUCCESS
) {
1762 return NT_STATUS_UNSUCCESSFUL
;
1764 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1765 my_methods
, PDB_CHANGED
);
1766 talloc_autofree_ldapmsg(newpwd
, result
);
1769 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1770 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1771 return NT_STATUS_UNSUCCESSFUL
;
1774 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1775 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1777 return NT_STATUS_UNSUCCESSFUL
;
1780 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1782 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1783 element_is_changed
)) {
1784 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1787 ldap_mods_free(mods
,True
);
1788 return NT_STATUS_UNSUCCESSFUL
;
1792 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1793 pdb_get_username(newpwd
)));
1795 return NT_STATUS_OK
;
1798 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1799 ldap_mods_free(mods
,True
);
1803 * We need to set the backend private data to NULL here. For example
1804 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1805 * same one, and with the explicit delete / add logic for attribute
1806 * values the second time we would use the wrong "old" value which
1807 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1809 * The existing LDAPMessage is still being auto-freed by the
1812 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
1815 if (!NT_STATUS_IS_OK(ret
)) {
1819 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1820 pdb_get_username(newpwd
)));
1821 return NT_STATUS_OK
;
1824 /***************************************************************************
1825 Renames a struct samu
1826 - The "rename user script" has full responsibility for changing everything
1827 ***************************************************************************/
1829 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1830 struct samu
*old_acct
,
1831 const char *newname
)
1833 const char *oldname
;
1835 pstring rename_script
;
1836 fstring oldname_lower
, newname_lower
;
1839 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1840 return NT_STATUS_INVALID_PARAMETER
;
1843 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1844 return NT_STATUS_INVALID_PARAMETER
;
1847 oldname
= pdb_get_username(old_acct
);
1849 /* rename the posix user */
1850 pstrcpy(rename_script
, lp_renameuser_script());
1852 if (!(*rename_script
))
1853 return NT_STATUS_ACCESS_DENIED
;
1855 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1858 /* We have to allow the account name to end with a '$'.
1859 Also, follow the semantics in _samr_create_user() and lower case the
1860 posix name but preserve the case in passdb */
1862 fstrcpy( oldname_lower
, oldname
);
1863 strlower_m( oldname_lower
);
1864 fstrcpy( newname_lower
, newname
);
1865 strlower_m( newname_lower
);
1866 string_sub2(rename_script
, "%unew", newname_lower
, sizeof(pstring
),
1868 string_sub2(rename_script
, "%uold", oldname_lower
, sizeof(pstring
),
1870 rc
= smbrun(rename_script
, NULL
);
1872 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
1873 rename_script
, rc
));
1876 smb_nscd_flush_user_cache();
1880 return NT_STATUS_UNSUCCESSFUL
;
1882 return NT_STATUS_OK
;
1885 /**********************************************************************
1886 Helper function to determine for update_sam_account whether
1887 we need LDAP modification.
1888 *********************************************************************/
1890 static BOOL
element_is_set_or_changed(const struct samu
*sampass
,
1891 enum pdb_elements element
)
1893 return (IS_SAM_SET(sampass
, element
) ||
1894 IS_SAM_CHANGED(sampass
, element
));
1897 /**********************************************************************
1898 Add struct samu to LDAP.
1899 *********************************************************************/
1901 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1903 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1904 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1906 LDAPMessage
*result
= NULL
;
1907 LDAPMessage
*entry
= NULL
;
1909 LDAPMod
**mods
= NULL
;
1910 int ldap_op
= LDAP_MOD_REPLACE
;
1912 const char **attr_list
;
1914 const char *username
= pdb_get_username(newpwd
);
1915 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
1919 if (!username
|| !*username
) {
1920 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1921 return NT_STATUS_INVALID_PARAMETER
;
1924 /* free this list after the second search or in case we exit on failure */
1925 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1927 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
1929 if (rc
!= LDAP_SUCCESS
) {
1930 TALLOC_FREE( attr_list
);
1931 return NT_STATUS_UNSUCCESSFUL
;
1934 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1935 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1937 ldap_msgfree(result
);
1938 TALLOC_FREE( attr_list
);
1939 return NT_STATUS_UNSUCCESSFUL
;
1941 ldap_msgfree(result
);
1944 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
1945 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1947 if (rc
== LDAP_SUCCESS
) {
1948 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1949 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1950 sid_to_string(sid_string
, sid
)));
1951 TALLOC_FREE( attr_list
);
1952 ldap_msgfree(result
);
1953 return NT_STATUS_UNSUCCESSFUL
;
1955 ldap_msgfree(result
);
1959 /* does the entry already exist but without a samba attributes?
1960 we need to return the samba attributes here */
1962 escape_user
= escape_ldap_string_alloc( username
);
1963 pstrcpy( filter
, "(uid=%u)" );
1964 all_string_sub( filter
, "%u", escape_user
, sizeof(filter
) );
1965 SAFE_FREE( escape_user
);
1967 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1968 filter
, attr_list
, &result
);
1969 if ( rc
!= LDAP_SUCCESS
) {
1970 TALLOC_FREE( attr_list
);
1971 return NT_STATUS_UNSUCCESSFUL
;
1974 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1976 if (num_result
> 1) {
1977 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1978 TALLOC_FREE( attr_list
);
1979 ldap_msgfree(result
);
1980 return NT_STATUS_UNSUCCESSFUL
;
1983 /* Check if we need to update an existing entry */
1984 if (num_result
== 1) {
1987 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1988 ldap_op
= LDAP_MOD_REPLACE
;
1989 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1990 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1992 TALLOC_FREE( attr_list
);
1993 ldap_msgfree(result
);
1994 return NT_STATUS_UNSUCCESSFUL
;
1996 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
1999 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2001 /* There might be a SID for this account already - say an idmap entry */
2003 pstr_sprintf(filter
, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2004 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
2005 sid_to_string(sid_string
, sid
),
2006 LDAP_OBJ_IDMAP_ENTRY
,
2007 LDAP_OBJ_SID_ENTRY
);
2009 /* free old result before doing a new search */
2010 if (result
!= NULL
) {
2011 ldap_msgfree(result
);
2014 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2015 filter
, attr_list
, &result
);
2017 if ( rc
!= LDAP_SUCCESS
) {
2018 TALLOC_FREE( attr_list
);
2019 return NT_STATUS_UNSUCCESSFUL
;
2022 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2024 if (num_result
> 1) {
2025 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2026 TALLOC_FREE( attr_list
);
2027 ldap_msgfree(result
);
2028 return NT_STATUS_UNSUCCESSFUL
;
2031 /* Check if we need to update an existing entry */
2032 if (num_result
== 1) {
2035 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2036 ldap_op
= LDAP_MOD_REPLACE
;
2037 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2038 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
2040 TALLOC_FREE( attr_list
);
2041 ldap_msgfree(result
);
2042 return NT_STATUS_UNSUCCESSFUL
;
2044 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
2049 TALLOC_FREE( attr_list
);
2051 if (num_result
== 0) {
2052 /* Check if we need to add an entry */
2053 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2054 ldap_op
= LDAP_MOD_ADD
;
2055 if (username
[strlen(username
)-1] == '$') {
2056 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_machine_suffix ());
2058 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_user_suffix ());
2062 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2063 element_is_set_or_changed
)) {
2064 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2065 ldap_msgfree(result
);
2067 ldap_mods_free(mods
,True
);
2068 return NT_STATUS_UNSUCCESSFUL
;
2071 ldap_msgfree(result
);
2074 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2075 return NT_STATUS_UNSUCCESSFUL
;
2077 switch ( ldap_state
->schema_ver
) {
2078 case SCHEMAVER_SAMBAACCOUNT
:
2079 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2081 case SCHEMAVER_SAMBASAMACCOUNT
:
2082 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2085 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2089 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2090 if (!NT_STATUS_IS_OK(ret
)) {
2091 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2092 pdb_get_username(newpwd
),dn
));
2093 ldap_mods_free(mods
, True
);
2097 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2098 ldap_mods_free(mods
, True
);
2100 return NT_STATUS_OK
;
2103 /**********************************************************************
2104 *********************************************************************/
2106 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2108 LDAPMessage
** result
)
2110 int scope
= LDAP_SCOPE_SUBTREE
;
2112 const char **attr_list
;
2114 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2115 rc
= smbldap_search(ldap_state
->smbldap_state
,
2116 lp_ldap_group_suffix (), scope
,
2117 filter
, attr_list
, 0, result
);
2118 TALLOC_FREE(attr_list
);
2123 /**********************************************************************
2124 *********************************************************************/
2126 static BOOL
init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2127 GROUP_MAP
*map
, LDAPMessage
*entry
)
2131 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2132 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2133 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2137 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2138 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
), temp
)) {
2139 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2140 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2143 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2145 map
->gid
= (gid_t
)atol(temp
);
2147 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2148 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
), temp
)) {
2149 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2150 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2154 if (!string_to_sid(&map
->sid
, temp
)) {
2155 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2159 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2160 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
), temp
)) {
2161 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2162 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2165 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2167 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2168 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2169 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2173 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2174 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), temp
)) {
2176 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2177 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_CN
), temp
))
2179 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2180 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2184 fstrcpy(map
->nt_name
, temp
);
2186 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2187 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DESC
), temp
)) {
2190 fstrcpy(map
->comment
, temp
);
2192 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
2193 store_gid_sid_cache(&map
->sid
, map
->gid
);
2199 /**********************************************************************
2200 *********************************************************************/
2202 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2206 struct ldapsam_privates
*ldap_state
=
2207 (struct ldapsam_privates
*)methods
->private_data
;
2208 LDAPMessage
*result
= NULL
;
2209 LDAPMessage
*entry
= NULL
;
2212 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2214 return NT_STATUS_NO_SUCH_GROUP
;
2217 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2220 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2221 ldap_msgfree(result
);
2222 return NT_STATUS_NO_SUCH_GROUP
;
2226 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2227 "count=%d\n", filter
, count
));
2228 ldap_msgfree(result
);
2229 return NT_STATUS_NO_SUCH_GROUP
;
2232 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2235 ldap_msgfree(result
);
2236 return NT_STATUS_UNSUCCESSFUL
;
2239 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2240 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2241 "group filter %s\n", filter
));
2242 ldap_msgfree(result
);
2243 return NT_STATUS_NO_SUCH_GROUP
;
2246 ldap_msgfree(result
);
2247 return NT_STATUS_OK
;
2250 /**********************************************************************
2251 *********************************************************************/
2253 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2258 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%s))",
2260 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2261 sid_string_static(&sid
));
2263 return ldapsam_getgroup(methods
, filter
, map
);
2266 /**********************************************************************
2267 *********************************************************************/
2269 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2274 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%lu))",
2276 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2277 (unsigned long)gid
);
2279 return ldapsam_getgroup(methods
, filter
, map
);
2282 /**********************************************************************
2283 *********************************************************************/
2285 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2289 char *escape_name
= escape_ldap_string_alloc(name
);
2292 return NT_STATUS_NO_MEMORY
;
2295 pstr_sprintf(filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2297 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2298 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
), escape_name
);
2300 SAFE_FREE(escape_name
);
2302 return ldapsam_getgroup(methods
, filter
, map
);
2305 static BOOL
ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2307 const DOM_SID
*domain_sid
,
2313 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2314 str
, sizeof(str
)-1)) {
2315 DEBUG(10, ("Could not find sambaSID attribute\n"));
2319 if (!string_to_sid(&sid
, str
)) {
2320 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2324 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2325 DEBUG(10, ("SID %s is not in expected domain %s\n",
2326 str
, sid_string_static(domain_sid
)));
2330 if (!sid_peek_rid(&sid
, rid
)) {
2331 DEBUG(10, ("Could not peek into RID\n"));
2338 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2339 TALLOC_CTX
*mem_ctx
,
2340 const DOM_SID
*group
,
2341 uint32
**pp_member_rids
,
2342 size_t *p_num_members
)
2344 struct ldapsam_privates
*ldap_state
=
2345 (struct ldapsam_privates
*)methods
->private_data
;
2346 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2347 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2348 const char *sid_attrs
[] = { "sambaSID", NULL
};
2349 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2350 LDAPMessage
*result
= NULL
;
2353 char **values
= NULL
;
2358 *pp_member_rids
= NULL
;
2361 filter
= talloc_asprintf(mem_ctx
,
2362 "(&(objectClass=%s)"
2365 LDAP_OBJ_POSIXGROUP
,
2367 sid_string_static(group
));
2368 if (filter
== NULL
) {
2369 ret
= NT_STATUS_NO_MEMORY
;
2373 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2374 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2377 if (rc
!= LDAP_SUCCESS
)
2380 talloc_autofree_ldapmsg(mem_ctx
, result
);
2382 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2385 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2386 sid_string_static(group
)));
2387 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2392 ret
= NT_STATUS_NO_SUCH_GROUP
;
2396 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2400 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2402 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2403 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2407 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2411 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2412 if (filter
== NULL
) {
2413 ret
= NT_STATUS_NO_MEMORY
;
2417 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2418 filter
= talloc_asprintf_append(filter
, "(uid=%s)", *memberuid
);
2419 if (filter
== NULL
) {
2420 ret
= NT_STATUS_NO_MEMORY
;
2425 filter
= talloc_asprintf_append(filter
, "))");
2426 if (filter
== NULL
) {
2427 ret
= NT_STATUS_NO_MEMORY
;
2431 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2432 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2435 if (rc
!= LDAP_SUCCESS
)
2438 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2439 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2441 talloc_autofree_ldapmsg(mem_ctx
, result
);
2443 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2445 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2451 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2455 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2456 "the sambaSID attribute\n"));
2457 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2461 if (!string_to_sid(&sid
, sidstr
))
2464 if (!sid_check_is_in_our_domain(&sid
)) {
2465 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2466 "in our domain\n"));
2467 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2471 sid_peek_rid(&sid
, &rid
);
2473 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2475 ret
= NT_STATUS_NO_MEMORY
;
2481 filter
= talloc_asprintf(mem_ctx
,
2482 "(&(objectClass=%s)"
2484 LDAP_OBJ_SAMBASAMACCOUNT
,
2487 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2488 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2491 if (rc
!= LDAP_SUCCESS
)
2494 talloc_autofree_ldapmsg(mem_ctx
, result
);
2496 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2498 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2502 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2504 get_global_sam_sid(),
2506 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2507 "the sambaSID attribute\n"));
2508 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2512 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2514 ret
= NT_STATUS_NO_MEMORY
;
2524 ldap_value_free(values
);
2529 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2530 TALLOC_CTX
*mem_ctx
,
2534 size_t *p_num_groups
)
2536 struct ldapsam_privates
*ldap_state
=
2537 (struct ldapsam_privates
*)methods
->private_data
;
2538 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2540 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2543 LDAPMessage
*result
= NULL
;
2545 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2546 size_t num_sids
, num_gids
;
2548 gid_t primary_gid
= -1;
2553 if (pdb_get_username(user
) == NULL
) {
2554 return NT_STATUS_INVALID_PARAMETER
;
2557 escape_name
= escape_ldap_string_alloc(pdb_get_username(user
));
2558 if (escape_name
== NULL
)
2559 return NT_STATUS_NO_MEMORY
;
2561 /* retrieve the users primary gid */
2562 filter
= talloc_asprintf(mem_ctx
,
2563 "(&(objectClass=%s)(uid=%s))",
2564 LDAP_OBJ_SAMBASAMACCOUNT
,
2566 if (filter
== NULL
) {
2567 ret
= NT_STATUS_NO_MEMORY
;
2571 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2572 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2574 if (rc
!= LDAP_SUCCESS
)
2577 talloc_autofree_ldapmsg(mem_ctx
, result
);
2579 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2583 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2584 ret
= NT_STATUS_NO_SUCH_USER
;
2587 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2589 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2591 DEBUG (1, ("Unable to find the member's gid!\n"));
2592 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2595 primary_gid
= strtoul(gidstr
, NULL
, 10);
2598 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2599 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2603 filter
= talloc_asprintf(mem_ctx
,
2604 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
2605 LDAP_OBJ_POSIXGROUP
, escape_name
, primary_gid
);
2606 if (filter
== NULL
) {
2607 ret
= NT_STATUS_NO_MEMORY
;
2611 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2612 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2614 if (rc
!= LDAP_SUCCESS
)
2617 talloc_autofree_ldapmsg(mem_ctx
, result
);
2625 /* We need to add the primary group as the first gid/sid */
2627 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2628 ret
= NT_STATUS_NO_MEMORY
;
2632 /* This sid will be replaced later */
2634 if (!add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
, &num_sids
)) {
2635 ret
= NT_STATUS_NO_MEMORY
;
2639 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2641 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2648 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2650 str
, sizeof(str
)-1))
2653 if (!string_to_sid(&sid
, str
))
2656 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2658 str
, sizeof(str
)-1))
2661 gid
= strtoul(str
, &end
, 10);
2663 if (PTR_DIFF(end
, str
) != strlen(str
))
2666 if (gid
== primary_gid
) {
2667 sid_copy(&(*pp_sids
)[0], &sid
);
2669 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2671 ret
= NT_STATUS_NO_MEMORY
;
2674 if (!add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2676 ret
= NT_STATUS_NO_MEMORY
;
2682 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2683 DEBUG(3, ("primary group of [%s] not found\n",
2684 pdb_get_username(user
)));
2688 *p_num_groups
= num_sids
;
2694 SAFE_FREE(escape_name
);
2698 /**********************************************************************
2699 * Augment a posixGroup object with a sambaGroupMapping domgroup
2700 *********************************************************************/
2702 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2703 struct ldapsam_privates
*ldap_state
,
2706 const char *filter
, *dn
;
2707 LDAPMessage
*msg
, *entry
;
2711 filter
= talloc_asprintf(mem_ctx
,
2712 "(&(objectClass=posixGroup)(gidNumber=%u))",
2714 if (filter
== NULL
) {
2715 return NT_STATUS_NO_MEMORY
;
2718 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2719 get_attr_list(mem_ctx
, groupmap_attr_list
),
2721 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2723 if ((rc
!= LDAP_SUCCESS
) ||
2724 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2725 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2726 return NT_STATUS_NO_SUCH_GROUP
;
2729 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2731 return NT_STATUS_NO_MEMORY
;
2735 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
2736 "sambaGroupMapping");
2737 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
2738 sid_string_static(&map
->sid
));
2739 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
2740 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2741 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2743 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2745 talloc_autofree_ldapmod(mem_ctx
, mods
);
2747 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2748 if (rc
!= LDAP_SUCCESS
) {
2749 return NT_STATUS_ACCESS_DENIED
;
2752 return NT_STATUS_OK
;
2755 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
2758 struct ldapsam_privates
*ldap_state
=
2759 (struct ldapsam_privates
*)methods
->private_data
;
2760 LDAPMessage
*msg
= NULL
;
2761 LDAPMod
**mods
= NULL
;
2762 const char *attrs
[] = { NULL
};
2766 TALLOC_CTX
*mem_ctx
;
2773 mem_ctx
= talloc_new(NULL
);
2774 if (mem_ctx
== NULL
) {
2775 DEBUG(0, ("talloc_new failed\n"));
2776 return NT_STATUS_NO_MEMORY
;
2779 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
2780 sid_string_static(&map
->sid
));
2781 if (filter
== NULL
) {
2782 result
= NT_STATUS_NO_MEMORY
;
2786 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
2787 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
2788 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2790 if ((rc
== LDAP_SUCCESS
) &&
2791 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
2793 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
2794 "group mapping entry\n",
2795 sid_string_static(&map
->sid
)));
2796 result
= NT_STATUS_GROUP_EXISTS
;
2800 switch (map
->sid_name_use
) {
2802 case SID_NAME_DOM_GRP
:
2803 /* To map a domain group we need to have a posix group
2805 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
2809 case SID_NAME_ALIAS
:
2810 if (!sid_check_is_in_our_domain(&map
->sid
)
2811 && !sid_check_is_in_builtin(&map
->sid
) )
2813 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
2814 sid_string_static(&map
->sid
)));
2815 result
= NT_STATUS_INVALID_PARAMETER
;
2821 DEBUG(3, ("Got invalid use '%s' for mapping\n",
2822 sid_type_lookup(map
->sid_name_use
)));
2823 result
= NT_STATUS_INVALID_PARAMETER
;
2827 /* Domain groups have been mapped in a separate routine, we have to
2828 * create an alias now */
2830 if (map
->gid
== -1) {
2831 DEBUG(10, ("Refusing to map gid==-1\n"));
2832 result
= NT_STATUS_INVALID_PARAMETER
;
2836 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
2837 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
2838 "add\n", map
->gid
, sid_string_static(&sid
)));
2839 result
= NT_STATUS_GROUP_EXISTS
;
2843 /* Ok, enough checks done. It's still racy to go ahead now, but that's
2844 * the best we can get out of LDAP. */
2846 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
2847 sid_string_static(&map
->sid
),
2848 lp_ldap_group_suffix());
2850 result
= NT_STATUS_NO_MEMORY
;
2856 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
2858 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
2859 "sambaGroupMapping");
2861 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
2862 sid_string_static(&map
->sid
));
2863 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
2864 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2865 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
2867 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
2869 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
2870 talloc_asprintf(mem_ctx
, "%u", map
->gid
));
2871 talloc_autofree_ldapmod(mem_ctx
, mods
);
2873 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
2875 result
= (rc
== LDAP_SUCCESS
) ?
2876 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
2879 TALLOC_FREE(mem_ctx
);
2883 /**********************************************************************
2884 * Update a group mapping entry. We're quite strict about what can be changed:
2885 * Only the description and displayname may be changed. It simply does not
2886 * make any sense to change the SID, gid or the type in a mapping.
2887 *********************************************************************/
2889 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
2892 struct ldapsam_privates
*ldap_state
=
2893 (struct ldapsam_privates
*)methods
->private_data
;
2895 const char *filter
, *dn
;
2896 LDAPMessage
*msg
= NULL
;
2897 LDAPMessage
*entry
= NULL
;
2898 LDAPMod
**mods
= NULL
;
2899 TALLOC_CTX
*mem_ctx
;
2902 mem_ctx
= talloc_new(NULL
);
2903 if (mem_ctx
== NULL
) {
2904 DEBUG(0, ("talloc_new failed\n"));
2905 return NT_STATUS_NO_MEMORY
;
2908 /* Make 100% sure that sid, gid and type are not changed by looking up
2909 * exactly the values we're given in LDAP. */
2911 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
2912 "(sambaSid=%s)(gidNumber=%u)"
2913 "(sambaGroupType=%d))",
2915 sid_string_static(&map
->sid
), map
->gid
,
2917 if (filter
== NULL
) {
2918 result
= NT_STATUS_NO_MEMORY
;
2922 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2923 get_attr_list(mem_ctx
, groupmap_attr_list
),
2925 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2927 if ((rc
!= LDAP_SUCCESS
) ||
2928 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2929 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2930 result
= NT_STATUS_NO_SUCH_GROUP
;
2934 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2937 result
= NT_STATUS_NO_MEMORY
;
2942 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2944 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2946 talloc_autofree_ldapmod(mem_ctx
, mods
);
2949 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
2950 "nothing to do\n"));
2951 result
= NT_STATUS_OK
;
2955 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2957 if (rc
!= LDAP_SUCCESS
) {
2958 result
= NT_STATUS_ACCESS_DENIED
;
2962 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
2963 "group %lu in LDAP\n", (unsigned long)map
->gid
));
2965 result
= NT_STATUS_OK
;
2968 TALLOC_FREE(mem_ctx
);
2972 /**********************************************************************
2973 *********************************************************************/
2975 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
2978 struct ldapsam_privates
*priv
=
2979 (struct ldapsam_privates
*)methods
->private_data
;
2980 LDAPMessage
*msg
, *entry
;
2983 TALLOC_CTX
*mem_ctx
;
2986 mem_ctx
= talloc_new(NULL
);
2987 if (mem_ctx
== NULL
) {
2988 DEBUG(0, ("talloc_new failed\n"));
2989 return NT_STATUS_NO_MEMORY
;
2992 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
2993 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
2994 sid_string_static(&sid
));
2995 if (filter
== NULL
) {
2996 result
= NT_STATUS_NO_MEMORY
;
2999 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3000 get_attr_list(mem_ctx
, groupmap_attr_list
),
3002 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3004 if ((rc
!= LDAP_SUCCESS
) ||
3005 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3006 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3007 result
= NT_STATUS_NO_SUCH_GROUP
;
3011 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3012 get_attr_list(mem_ctx
,
3013 groupmap_attr_list_to_delete
));
3015 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3016 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3017 const char *attrs
[] = { "sambaGroupType", "description",
3018 "displayName", "sambaSIDList",
3021 /* Second try. Don't delete the sambaSID attribute, this is
3022 for "old" entries that are tacked on a winbind
3025 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3026 LDAP_OBJ_GROUPMAP
, attrs
);
3029 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3030 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3031 const char *attrs
[] = { "sambaGroupType", "description",
3032 "displayName", "sambaSIDList",
3033 "gidNumber", NULL
};
3035 /* Third try. This is a post-3.0.21 alias (containing only
3036 * sambaSidEntry and sambaGroupMapping classes), we also have
3037 * to delete the gidNumber attribute, only the sambaSidEntry
3040 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3041 LDAP_OBJ_GROUPMAP
, attrs
);
3044 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3047 TALLOC_FREE(mem_ctx
);
3051 /**********************************************************************
3052 *********************************************************************/
3054 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3057 struct ldapsam_privates
*ldap_state
=
3058 (struct ldapsam_privates
*)my_methods
->private_data
;
3061 const char **attr_list
;
3063 pstr_sprintf( filter
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3064 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3065 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3066 LDAP_SCOPE_SUBTREE
, filter
,
3067 attr_list
, 0, &ldap_state
->result
);
3068 TALLOC_FREE(attr_list
);
3070 if (rc
!= LDAP_SUCCESS
) {
3071 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3072 ldap_err2string(rc
)));
3073 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3074 lp_ldap_group_suffix(), filter
));
3075 ldap_msgfree(ldap_state
->result
);
3076 ldap_state
->result
= NULL
;
3077 return NT_STATUS_UNSUCCESSFUL
;
3080 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3081 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3082 ldap_state
->result
)));
3085 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3086 ldap_state
->result
);
3087 ldap_state
->index
= 0;
3089 return NT_STATUS_OK
;
3092 /**********************************************************************
3093 *********************************************************************/
3095 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3097 ldapsam_endsampwent(my_methods
);
3100 /**********************************************************************
3101 *********************************************************************/
3103 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3106 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3107 struct ldapsam_privates
*ldap_state
=
3108 (struct ldapsam_privates
*)my_methods
->private_data
;
3112 if (!ldap_state
->entry
)
3115 ldap_state
->index
++;
3116 bret
= init_group_from_ldap(ldap_state
, map
,
3120 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3124 return NT_STATUS_OK
;
3127 /**********************************************************************
3128 *********************************************************************/
3130 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3131 const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
,
3132 GROUP_MAP
**pp_rmap
,
3133 size_t *p_num_entries
,
3142 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3143 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3145 return NT_STATUS_ACCESS_DENIED
;
3148 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3149 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3150 sid_name_use
!= map
.sid_name_use
) {
3151 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3152 "not of the requested type\n", map
.nt_name
));
3155 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3156 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3157 "non mapped\n", map
.nt_name
));
3161 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3163 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3164 "enlarge group map!\n"));
3165 return NT_STATUS_UNSUCCESSFUL
;
3168 (*pp_rmap
)[entries
] = map
;
3173 ldapsam_endsamgrent(methods
);
3175 *p_num_entries
= entries
;
3177 return NT_STATUS_OK
;
3180 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3181 const DOM_SID
*alias
,
3182 const DOM_SID
*member
,
3185 struct ldapsam_privates
*ldap_state
=
3186 (struct ldapsam_privates
*)methods
->private_data
;
3188 LDAPMessage
*result
= NULL
;
3189 LDAPMessage
*entry
= NULL
;
3191 LDAPMod
**mods
= NULL
;
3193 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3197 if (sid_check_is_in_builtin(alias
)) {
3198 type
= SID_NAME_ALIAS
;
3201 if (sid_check_is_in_our_domain(alias
)) {
3202 type
= SID_NAME_ALIAS
;
3205 if (type
== SID_NAME_USE_NONE
) {
3206 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3207 sid_string_static(alias
)));
3208 return NT_STATUS_NO_SUCH_ALIAS
;
3211 pstr_sprintf(filter
,
3212 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3213 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3216 if (ldapsam_search_one_group(ldap_state
, filter
,
3217 &result
) != LDAP_SUCCESS
)
3218 return NT_STATUS_NO_SUCH_ALIAS
;
3220 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3224 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3225 ldap_msgfree(result
);
3226 return NT_STATUS_NO_SUCH_ALIAS
;
3230 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3231 "filter %s: count=%d\n", filter
, count
));
3232 ldap_msgfree(result
);
3233 return NT_STATUS_NO_SUCH_ALIAS
;
3236 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3240 ldap_msgfree(result
);
3241 return NT_STATUS_UNSUCCESSFUL
;
3244 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
3246 ldap_msgfree(result
);
3247 return NT_STATUS_UNSUCCESSFUL
;
3250 smbldap_set_mod(&mods
, modop
,
3251 get_attr_key2string(groupmap_attr_list
,
3252 LDAP_ATTR_SID_LIST
),
3253 sid_string_static(member
));
3255 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3257 ldap_mods_free(mods
, True
);
3258 ldap_msgfree(result
);
3261 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3262 return NT_STATUS_MEMBER_IN_ALIAS
;
3265 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3266 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3269 if (rc
!= LDAP_SUCCESS
) {
3270 return NT_STATUS_UNSUCCESSFUL
;
3273 return NT_STATUS_OK
;
3276 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3277 const DOM_SID
*alias
,
3278 const DOM_SID
*member
)
3280 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3283 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3284 const DOM_SID
*alias
,
3285 const DOM_SID
*member
)
3287 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3291 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3292 const DOM_SID
*alias
,
3293 DOM_SID
**pp_members
,
3294 size_t *p_num_members
)
3296 struct ldapsam_privates
*ldap_state
=
3297 (struct ldapsam_privates
*)methods
->private_data
;
3298 LDAPMessage
*result
= NULL
;
3299 LDAPMessage
*entry
= NULL
;
3304 size_t num_members
= 0;
3305 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3310 if (sid_check_is_in_builtin(alias
)) {
3311 type
= SID_NAME_ALIAS
;
3314 if (sid_check_is_in_our_domain(alias
)) {
3315 type
= SID_NAME_ALIAS
;
3318 if (type
== SID_NAME_USE_NONE
) {
3319 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3320 sid_string_static(alias
)));
3321 return NT_STATUS_NO_SUCH_ALIAS
;
3324 pstr_sprintf(filter
,
3325 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3326 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3329 if (ldapsam_search_one_group(ldap_state
, filter
,
3330 &result
) != LDAP_SUCCESS
)
3331 return NT_STATUS_NO_SUCH_ALIAS
;
3333 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3337 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3338 ldap_msgfree(result
);
3339 return NT_STATUS_NO_SUCH_ALIAS
;
3343 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3344 "filter %s: count=%d\n", filter
, count
));
3345 ldap_msgfree(result
);
3346 return NT_STATUS_NO_SUCH_ALIAS
;
3349 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3353 ldap_msgfree(result
);
3354 return NT_STATUS_UNSUCCESSFUL
;
3357 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3359 get_attr_key2string(groupmap_attr_list
,
3360 LDAP_ATTR_SID_LIST
));
3362 if (values
== NULL
) {
3363 ldap_msgfree(result
);
3364 return NT_STATUS_OK
;
3367 count
= ldap_count_values(values
);
3369 for (i
=0; i
<count
; i
++) {
3372 if (!string_to_sid(&member
, values
[i
]))
3375 if (!add_sid_to_array(NULL
, &member
, pp_members
, &num_members
)) {
3376 ldap_value_free(values
);
3377 ldap_msgfree(result
);
3378 return NT_STATUS_NO_MEMORY
;
3382 *p_num_members
= num_members
;
3383 ldap_value_free(values
);
3384 ldap_msgfree(result
);
3386 return NT_STATUS_OK
;
3389 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3390 TALLOC_CTX
*mem_ctx
,
3391 const DOM_SID
*domain_sid
,
3392 const DOM_SID
*members
,
3394 uint32
**pp_alias_rids
,
3395 size_t *p_num_alias_rids
)
3397 struct ldapsam_privates
*ldap_state
=
3398 (struct ldapsam_privates
*)methods
->private_data
;
3401 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3403 LDAPMessage
*result
= NULL
;
3404 LDAPMessage
*entry
= NULL
;
3408 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3410 if (sid_check_is_builtin(domain_sid
)) {
3411 type
= SID_NAME_ALIAS
;
3414 if (sid_check_is_domain(domain_sid
)) {
3415 type
= SID_NAME_ALIAS
;
3418 if (type
== SID_NAME_USE_NONE
) {
3419 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3420 sid_string_static(domain_sid
)));
3421 return NT_STATUS_UNSUCCESSFUL
;
3424 filter
= talloc_asprintf(mem_ctx
,
3425 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3426 LDAP_OBJ_GROUPMAP
, type
);
3428 for (i
=0; i
<num_members
; i
++)
3429 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3431 sid_string_static(&members
[i
]));
3433 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3435 if (filter
== NULL
) {
3436 return NT_STATUS_NO_MEMORY
;
3439 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3440 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3442 if (rc
!= LDAP_SUCCESS
)
3443 return NT_STATUS_UNSUCCESSFUL
;
3445 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3447 for (entry
= ldap_first_entry(ldap_struct
, result
);
3449 entry
= ldap_next_entry(ldap_struct
, entry
))
3455 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3461 if (!string_to_sid(&sid
, sid_str
))
3464 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3467 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3468 p_num_alias_rids
)) {
3469 ldap_msgfree(result
);
3470 return NT_STATUS_NO_MEMORY
;
3474 ldap_msgfree(result
);
3475 return NT_STATUS_OK
;
3478 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3482 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3484 LDAPMod
**mods
= NULL
;
3485 fstring value_string
;
3486 const char *policy_attr
= NULL
;
3488 struct ldapsam_privates
*ldap_state
=
3489 (struct ldapsam_privates
*)methods
->private_data
;
3491 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3493 if (!ldap_state
->domain_dn
) {
3494 return NT_STATUS_INVALID_PARAMETER
;
3497 policy_attr
= get_account_policy_attr(policy_index
);
3498 if (policy_attr
== NULL
) {
3499 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3504 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3506 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3508 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3511 ldap_mods_free(mods
, True
);
3513 if (rc
!= LDAP_SUCCESS
) {
3517 if (!cache_account_policy_set(policy_index
, value
)) {
3518 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3519 "update local tdb cache\n"));
3523 return NT_STATUS_OK
;
3526 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3527 int policy_index
, uint32 value
)
3529 return ldapsam_set_account_policy_in_ldap(methods
, policy_index
,
3533 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3537 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3538 LDAPMessage
*result
= NULL
;
3539 LDAPMessage
*entry
= NULL
;
3543 const char *policy_attr
= NULL
;
3545 struct ldapsam_privates
*ldap_state
=
3546 (struct ldapsam_privates
*)methods
->private_data
;
3548 const char *attrs
[2];
3550 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3552 if (!ldap_state
->domain_dn
) {
3553 return NT_STATUS_INVALID_PARAMETER
;
3556 policy_attr
= get_account_policy_attr(policy_index
);
3558 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3559 "policy index: %d\n", policy_index
));
3563 attrs
[0] = policy_attr
;
3566 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3567 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0,
3570 if (rc
!= LDAP_SUCCESS
) {
3574 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3579 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3580 if (entry
== NULL
) {
3584 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3589 *value
= (uint32
)atol(vals
[0]);
3591 ntstatus
= NT_STATUS_OK
;
3595 ldap_value_free(vals
);
3596 ldap_msgfree(result
);
3601 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3603 - if user hasn't decided to use account policies inside LDAP just reuse the
3606 - if there is a valid cache entry, return that
3607 - if there is an LDAP entry, update cache and return
3608 - otherwise set to default, update cache and return
3612 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3613 int policy_index
, uint32
*value
)
3615 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3617 if (cache_account_policy_get(policy_index
, value
)) {
3618 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3620 return NT_STATUS_OK
;
3623 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, policy_index
,
3625 if (NT_STATUS_IS_OK(ntstatus
)) {
3629 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3633 /* should we automagically migrate old tdb value here ? */
3634 if (account_policy_get(policy_index
, value
))
3637 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3638 "default\n", policy_index
));
3641 if (!account_policy_get_default(policy_index
, value
)) {
3647 ntstatus
= ldapsam_set_account_policy(methods
, policy_index
, *value
);
3648 if (!NT_STATUS_IS_OK(ntstatus
)) {
3654 if (!cache_account_policy_set(policy_index
, *value
)) {
3655 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3656 "tdb as a cache\n"));
3657 return NT_STATUS_UNSUCCESSFUL
;
3660 return NT_STATUS_OK
;
3663 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3664 const DOM_SID
*domain_sid
,
3668 enum lsa_SidType
*attrs
)
3670 struct ldapsam_privates
*ldap_state
=
3671 (struct ldapsam_privates
*)methods
->private_data
;
3672 LDAPMessage
*msg
= NULL
;
3674 char *allsids
= NULL
;
3675 int i
, rc
, num_mapped
;
3676 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
3677 TALLOC_CTX
*mem_ctx
;
3681 mem_ctx
= talloc_new(NULL
);
3682 if (mem_ctx
== NULL
) {
3683 DEBUG(0, ("talloc_new failed\n"));
3687 if (!sid_check_is_builtin(domain_sid
) &&
3688 !sid_check_is_domain(domain_sid
)) {
3689 result
= NT_STATUS_INVALID_PARAMETER
;
3693 for (i
=0; i
<num_rids
; i
++)
3694 attrs
[i
] = SID_NAME_UNKNOWN
;
3696 allsids
= talloc_strdup(mem_ctx
, "");
3697 if (allsids
== NULL
) {
3701 for (i
=0; i
<num_rids
; i
++) {
3703 sid_compose(&sid
, domain_sid
, rids
[i
]);
3704 allsids
= talloc_asprintf_append(allsids
, "(sambaSid=%s)",
3705 sid_string_static(&sid
));
3706 if (allsids
== NULL
) {
3711 /* First look for users */
3715 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
3717 filter
= talloc_asprintf(
3718 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
3719 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
3721 if (filter
== NULL
) {
3725 rc
= smbldap_search(ldap_state
->smbldap_state
,
3726 lp_ldap_user_suffix(),
3727 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
3729 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3732 if (rc
!= LDAP_SUCCESS
)
3735 ld
= ldap_state
->smbldap_state
->ldap_struct
;
3738 for (entry
= ldap_first_entry(ld
, msg
);
3740 entry
= ldap_next_entry(ld
, entry
)) {
3745 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
3747 DEBUG(2, ("Could not find sid from ldap entry\n"));
3751 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
3754 DEBUG(2, ("Could not retrieve uid attribute\n"));
3758 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
3759 if (rid
== rids
[rid_index
])
3763 if (rid_index
== num_rids
) {
3764 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
3768 attrs
[rid_index
] = SID_NAME_USER
;
3769 names
[rid_index
] = name
;
3773 if (num_mapped
== num_rids
) {
3774 /* No need to look for groups anymore -- we're done */
3775 result
= NT_STATUS_OK
;
3779 /* Same game for groups */
3783 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
3784 "sambaGroupType", NULL
};
3786 filter
= talloc_asprintf(
3787 mem_ctx
, "(&(objectClass=%s)(|%s))",
3788 LDAP_OBJ_GROUPMAP
, allsids
);
3789 if (filter
== NULL
) {
3793 rc
= smbldap_search(ldap_state
->smbldap_state
,
3794 lp_ldap_group_suffix(),
3795 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
3797 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3800 if (rc
!= LDAP_SUCCESS
)
3803 /* ldap_struct might have changed due to a reconnect */
3805 ld
= ldap_state
->smbldap_state
->ldap_struct
;
3807 /* For consistency checks, we already checked we're only domain or builtin */
3809 is_builtin
= sid_check_is_builtin(domain_sid
);
3811 for (entry
= ldap_first_entry(ld
, msg
);
3813 entry
= ldap_next_entry(ld
, entry
))
3818 enum lsa_SidType type
;
3819 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
3821 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
3824 DEBUG(2, ("Could not extract type from ldap entry %s\n",
3829 type
= (enum lsa_SidType
)atol(attr
);
3831 /* Consistency checks */
3832 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
3833 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
3834 (type
!= SID_NAME_DOM_GRP
)))) {
3835 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
3838 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
3840 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
3844 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
3847 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
3849 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
3853 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
3858 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
3859 if (rid
== rids
[rid_index
])
3863 if (rid_index
== num_rids
) {
3864 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
3868 attrs
[rid_index
] = type
;
3869 names
[rid_index
] = attr
;
3873 result
= NT_STATUS_NONE_MAPPED
;
3876 result
= (num_mapped
== num_rids
) ?
3877 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
3879 TALLOC_FREE(mem_ctx
);
3883 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
3885 char *filter
= NULL
;
3886 char *escaped
= NULL
;
3887 char *result
= NULL
;
3889 asprintf(&filter
, "(&%s(objectclass=sambaSamAccount))",
3891 if (filter
== NULL
) goto done
;
3893 escaped
= escape_ldap_string_alloc(username
);
3894 if (escaped
== NULL
) goto done
;
3896 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
3905 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
3909 const char **result
;
3911 va_start(ap
, mem_ctx
);
3912 while (va_arg(ap
, const char *) != NULL
)
3916 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
3920 va_start(ap
, mem_ctx
);
3921 for (i
=0; i
<num
; i
++) {
3922 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
3923 if (result
[i
] == NULL
) {
3924 talloc_free(result
);
3934 struct ldap_search_state
{
3935 struct smbldap_state
*connection
;
3945 void *pagedresults_cookie
;
3947 LDAPMessage
*entries
, *current_entry
;
3948 BOOL (*ldap2displayentry
)(struct ldap_search_state
*state
,
3949 TALLOC_CTX
*mem_ctx
,
3950 LDAP
*ld
, LDAPMessage
*entry
,
3951 struct samr_displayentry
*result
);
3954 static BOOL
ldapsam_search_firstpage(struct pdb_search
*search
)
3956 struct ldap_search_state
*state
=
3957 (struct ldap_search_state
*)search
->private_data
;
3959 int rc
= LDAP_OPERATIONS_ERROR
;
3961 state
->entries
= NULL
;
3963 if (state
->connection
->paged_results
) {
3964 rc
= smbldap_search_paged(state
->connection
, state
->base
,
3965 state
->scope
, state
->filter
,
3966 state
->attrs
, state
->attrsonly
,
3967 lp_ldap_page_size(), &state
->entries
,
3968 &state
->pagedresults_cookie
);
3971 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
3973 if (state
->entries
!= NULL
) {
3974 /* Left over from unsuccessful paged attempt */
3975 ldap_msgfree(state
->entries
);
3976 state
->entries
= NULL
;
3979 rc
= smbldap_search(state
->connection
, state
->base
,
3980 state
->scope
, state
->filter
, state
->attrs
,
3981 state
->attrsonly
, &state
->entries
);
3983 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
3986 /* Ok, the server was lying. It told us it could do paged
3987 * searches when it could not. */
3988 state
->connection
->paged_results
= False
;
3991 ld
= state
->connection
->ldap_struct
;
3993 DEBUG(5, ("Don't have an LDAP connection right after a "
3997 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
3999 if (state
->current_entry
== NULL
) {
4000 ldap_msgfree(state
->entries
);
4001 state
->entries
= NULL
;
4007 static BOOL
ldapsam_search_nextpage(struct pdb_search
*search
)
4009 struct ldap_search_state
*state
=
4010 (struct ldap_search_state
*)search
->private_data
;
4013 if (!state
->connection
->paged_results
) {
4014 /* There is no next page when there are no paged results */
4018 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4019 state
->scope
, state
->filter
, state
->attrs
,
4020 state
->attrsonly
, lp_ldap_page_size(),
4022 &state
->pagedresults_cookie
);
4024 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4027 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4029 if (state
->current_entry
== NULL
) {
4030 ldap_msgfree(state
->entries
);
4031 state
->entries
= NULL
;
4037 static BOOL
ldapsam_search_next_entry(struct pdb_search
*search
,
4038 struct samr_displayentry
*entry
)
4040 struct ldap_search_state
*state
=
4041 (struct ldap_search_state
*)search
->private_data
;
4045 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4048 if ((state
->entries
== NULL
) &&
4049 !ldapsam_search_nextpage(search
))
4052 result
= state
->ldap2displayentry(state
, search
->mem_ctx
, state
->connection
->ldap_struct
,
4053 state
->current_entry
, entry
);
4057 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4058 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4059 if (dn
!= NULL
) ldap_memfree(dn
);
4062 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4064 if (state
->current_entry
== NULL
) {
4065 ldap_msgfree(state
->entries
);
4066 state
->entries
= NULL
;
4069 if (!result
) goto retry
;
4074 static void ldapsam_search_end(struct pdb_search
*search
)
4076 struct ldap_search_state
*state
=
4077 (struct ldap_search_state
*)search
->private_data
;
4080 if (state
->pagedresults_cookie
== NULL
)
4083 if (state
->entries
!= NULL
)
4084 ldap_msgfree(state
->entries
);
4086 state
->entries
= NULL
;
4087 state
->current_entry
= NULL
;
4089 if (!state
->connection
->paged_results
)
4092 /* Tell the LDAP server we're not interested in the rest anymore. */
4094 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4095 state
->filter
, state
->attrs
,
4096 state
->attrsonly
, 0, &state
->entries
,
4097 &state
->pagedresults_cookie
);
4099 if (rc
!= LDAP_SUCCESS
)
4100 DEBUG(5, ("Could not end search properly\n"));
4105 static BOOL
ldapuser2displayentry(struct ldap_search_state
*state
,
4106 TALLOC_CTX
*mem_ctx
,
4107 LDAP
*ld
, LDAPMessage
*entry
,
4108 struct samr_displayentry
*result
)
4114 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4115 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4116 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4119 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4120 ldap_value_free(vals
);
4122 if ((state
->acct_flags
!= 0) &&
4123 ((state
->acct_flags
& acct_flags
) == 0))
4126 result
->acct_flags
= acct_flags
;
4127 result
->account_name
= "";
4128 result
->fullname
= "";
4129 result
->description
= "";
4131 vals
= ldap_get_values(ld
, entry
, "uid");
4132 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4133 DEBUG(5, ("\"uid\" not found\n"));
4136 pull_utf8_talloc(mem_ctx
,
4137 CONST_DISCARD(char **, &result
->account_name
),
4139 ldap_value_free(vals
);
4141 vals
= ldap_get_values(ld
, entry
, "displayName");
4142 if ((vals
== NULL
) || (vals
[0] == NULL
))
4143 DEBUG(8, ("\"displayName\" not found\n"));
4145 pull_utf8_talloc(mem_ctx
,
4146 CONST_DISCARD(char **, &result
->fullname
),
4148 ldap_value_free(vals
);
4150 vals
= ldap_get_values(ld
, entry
, "description");
4151 if ((vals
== NULL
) || (vals
[0] == NULL
))
4152 DEBUG(8, ("\"description\" not found\n"));
4154 pull_utf8_talloc(mem_ctx
,
4155 CONST_DISCARD(char **, &result
->description
),
4157 ldap_value_free(vals
);
4159 if ((result
->account_name
== NULL
) ||
4160 (result
->fullname
== NULL
) ||
4161 (result
->description
== NULL
)) {
4162 DEBUG(0, ("talloc failed\n"));
4166 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4167 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4168 DEBUG(0, ("\"objectSid\" not found\n"));
4172 if (!string_to_sid(&sid
, vals
[0])) {
4173 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4174 ldap_value_free(vals
);
4177 ldap_value_free(vals
);
4179 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4180 DEBUG(0, ("sid %s does not belong to our domain\n",
4181 sid_string_static(&sid
)));
4189 static BOOL
ldapsam_search_users(struct pdb_methods
*methods
,
4190 struct pdb_search
*search
,
4193 struct ldapsam_privates
*ldap_state
=
4194 (struct ldapsam_privates
*)methods
->private_data
;
4195 struct ldap_search_state
*state
;
4197 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4198 if (state
== NULL
) {
4199 DEBUG(0, ("talloc failed\n"));
4203 state
->connection
= ldap_state
->smbldap_state
;
4205 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4206 state
->base
= lp_ldap_user_suffix();
4207 else if ((acct_flags
!= 0) &&
4208 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4209 state
->base
= lp_ldap_machine_suffix();
4211 state
->base
= lp_ldap_suffix();
4213 state
->acct_flags
= acct_flags
;
4214 state
->base
= talloc_strdup(search
->mem_ctx
, state
->base
);
4215 state
->scope
= LDAP_SCOPE_SUBTREE
;
4216 state
->filter
= get_ldap_filter(search
->mem_ctx
, "*");
4217 state
->attrs
= talloc_attrs(search
->mem_ctx
, "uid", "sambaSid",
4218 "displayName", "description",
4219 "sambaAcctFlags", NULL
);
4220 state
->attrsonly
= 0;
4221 state
->pagedresults_cookie
= NULL
;
4222 state
->entries
= NULL
;
4223 state
->ldap2displayentry
= ldapuser2displayentry
;
4225 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4226 DEBUG(0, ("talloc failed\n"));
4230 search
->private_data
= state
;
4231 search
->next_entry
= ldapsam_search_next_entry
;
4232 search
->search_end
= ldapsam_search_end
;
4234 return ldapsam_search_firstpage(search
);
4237 static BOOL
ldapgroup2displayentry(struct ldap_search_state
*state
,
4238 TALLOC_CTX
*mem_ctx
,
4239 LDAP
*ld
, LDAPMessage
*entry
,
4240 struct samr_displayentry
*result
)
4246 result
->account_name
= "";
4247 result
->fullname
= "";
4248 result
->description
= "";
4251 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4252 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4253 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4255 ldap_value_free(vals
);
4260 group_type
= atoi(vals
[0]);
4262 if ((state
->group_type
!= 0) &&
4263 ((state
->group_type
!= group_type
))) {
4264 ldap_value_free(vals
);
4268 ldap_value_free(vals
);
4270 /* display name is the NT group name */
4272 vals
= ldap_get_values(ld
, entry
, "displayName");
4273 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4274 DEBUG(8, ("\"displayName\" not found\n"));
4276 /* fallback to the 'cn' attribute */
4277 vals
= ldap_get_values(ld
, entry
, "cn");
4278 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4279 DEBUG(5, ("\"cn\" not found\n"));
4282 pull_utf8_talloc(mem_ctx
,
4283 CONST_DISCARD(char **, &result
->account_name
),
4287 pull_utf8_talloc(mem_ctx
,
4288 CONST_DISCARD(char **, &result
->account_name
),
4292 ldap_value_free(vals
);
4294 vals
= ldap_get_values(ld
, entry
, "description");
4295 if ((vals
== NULL
) || (vals
[0] == NULL
))
4296 DEBUG(8, ("\"description\" not found\n"));
4298 pull_utf8_talloc(mem_ctx
,
4299 CONST_DISCARD(char **, &result
->description
),
4301 ldap_value_free(vals
);
4303 if ((result
->account_name
== NULL
) ||
4304 (result
->fullname
== NULL
) ||
4305 (result
->description
== NULL
)) {
4306 DEBUG(0, ("talloc failed\n"));
4310 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4311 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4312 DEBUG(0, ("\"objectSid\" not found\n"));
4314 ldap_value_free(vals
);
4319 if (!string_to_sid(&sid
, vals
[0])) {
4320 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4324 ldap_value_free(vals
);
4326 switch (group_type
) {
4327 case SID_NAME_DOM_GRP
:
4328 case SID_NAME_ALIAS
:
4330 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4331 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4333 DEBUG(0, ("%s is not in our domain\n",
4334 sid_string_static(&sid
)));
4340 DEBUG(0,("unkown group type: %d\n", group_type
));
4347 static BOOL
ldapsam_search_grouptype(struct pdb_methods
*methods
,
4348 struct pdb_search
*search
,
4350 enum lsa_SidType type
)
4352 struct ldapsam_privates
*ldap_state
=
4353 (struct ldapsam_privates
*)methods
->private_data
;
4354 struct ldap_search_state
*state
;
4356 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4357 if (state
== NULL
) {
4358 DEBUG(0, ("talloc failed\n"));
4362 state
->connection
= ldap_state
->smbldap_state
;
4364 state
->base
= talloc_strdup(search
->mem_ctx
, lp_ldap_group_suffix());
4365 state
->connection
= ldap_state
->smbldap_state
;
4366 state
->scope
= LDAP_SCOPE_SUBTREE
;
4367 state
->filter
= talloc_asprintf(search
->mem_ctx
,
4368 "(&(objectclass=sambaGroupMapping)"
4369 "(sambaGroupType=%d)(sambaSID=%s*))",
4370 type
, sid_string_static(sid
));
4371 state
->attrs
= talloc_attrs(search
->mem_ctx
, "cn", "sambaSid",
4372 "displayName", "description",
4373 "sambaGroupType", NULL
);
4374 state
->attrsonly
= 0;
4375 state
->pagedresults_cookie
= NULL
;
4376 state
->entries
= NULL
;
4377 state
->group_type
= type
;
4378 state
->ldap2displayentry
= ldapgroup2displayentry
;
4380 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4381 DEBUG(0, ("talloc failed\n"));
4385 search
->private_data
= state
;
4386 search
->next_entry
= ldapsam_search_next_entry
;
4387 search
->search_end
= ldapsam_search_end
;
4389 return ldapsam_search_firstpage(search
);
4392 static BOOL
ldapsam_search_groups(struct pdb_methods
*methods
,
4393 struct pdb_search
*search
)
4395 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4398 static BOOL
ldapsam_search_aliases(struct pdb_methods
*methods
,
4399 struct pdb_search
*search
,
4402 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4405 static BOOL
ldapsam_rid_algorithm(struct pdb_methods
*methods
)
4410 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4413 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4415 LDAPMessage
*result
= NULL
;
4416 LDAPMessage
*entry
= NULL
;
4417 LDAPMod
**mods
= NULL
;
4424 TALLOC_CTX
*mem_ctx
;
4426 mem_ctx
= talloc_new(NULL
);
4427 if (mem_ctx
== NULL
) {
4428 DEBUG(0, ("talloc_new failed\n"));
4429 return NT_STATUS_NO_MEMORY
;
4432 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4433 get_global_sam_name(), False
);
4434 if (!NT_STATUS_IS_OK(status
)) {
4435 DEBUG(3, ("Could not get domain info: %s\n",
4436 nt_errstr(status
)));
4440 talloc_autofree_ldapmsg(mem_ctx
, result
);
4442 entry
= ldap_first_entry(priv2ld(priv
), result
);
4443 if (entry
== NULL
) {
4444 DEBUG(0, ("Could not get domain info entry\n"));
4445 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4449 /* Find the largest of the three attributes "sambaNextRid",
4450 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4451 concept of differentiating between user and group rids, and will
4452 use only "sambaNextRid" in the future. But for compatibility
4453 reasons I look if others have chosen different strategies -- VL */
4455 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4456 "sambaNextRid", mem_ctx
);
4457 if (value
!= NULL
) {
4458 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4459 nextRid
= MAX(nextRid
, tmp
);
4462 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4463 "sambaNextUserRid", mem_ctx
);
4464 if (value
!= NULL
) {
4465 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4466 nextRid
= MAX(nextRid
, tmp
);
4469 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4470 "sambaNextGroupRid", mem_ctx
);
4471 if (value
!= NULL
) {
4472 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4473 nextRid
= MAX(nextRid
, tmp
);
4477 nextRid
= BASE_RID
-1;
4482 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4483 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4484 talloc_autofree_ldapmod(mem_ctx
, mods
);
4486 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4487 status
= NT_STATUS_NO_MEMORY
;
4491 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4493 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4496 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4499 if (NT_STATUS_IS_OK(status
)) {
4503 TALLOC_FREE(mem_ctx
);
4507 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32
*rid
)
4511 for (i
=0; i
<10; i
++) {
4512 NTSTATUS result
= ldapsam_get_new_rid(
4513 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4514 if (NT_STATUS_IS_OK(result
)) {
4518 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4522 /* The ldap update failed (maybe a race condition), retry */
4525 /* Tried 10 times, fail. */
4526 return NT_STATUS_ACCESS_DENIED
;
4529 static BOOL
ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
4531 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4532 return NT_STATUS_IS_OK(result
) ? True
: False
;
4535 static BOOL
ldapsam_sid_to_id(struct pdb_methods
*methods
,
4537 union unid_t
*id
, enum lsa_SidType
*type
)
4539 struct ldapsam_privates
*priv
=
4540 (struct ldapsam_privates
*)methods
->private_data
;
4542 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4544 LDAPMessage
*result
= NULL
;
4545 LDAPMessage
*entry
= NULL
;
4550 TALLOC_CTX
*mem_ctx
;
4552 mem_ctx
= talloc_new(NULL
);
4553 if (mem_ctx
== NULL
) {
4554 DEBUG(0, ("talloc_new failed\n"));
4558 filter
= talloc_asprintf(mem_ctx
,
4560 "(|(objectClass=%s)(objectClass=%s)))",
4561 sid_string_static(sid
),
4562 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4563 if (filter
== NULL
) {
4564 DEBUG(5, ("talloc_asprintf failed\n"));
4568 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4570 if (rc
!= LDAP_SUCCESS
) {
4573 talloc_autofree_ldapmsg(mem_ctx
, result
);
4575 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4576 DEBUG(10, ("Got %d entries, expected one\n",
4577 ldap_count_entries(priv2ld(priv
), result
)));
4581 entry
= ldap_first_entry(priv2ld(priv
), result
);
4583 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4584 "sambaGroupType", mem_ctx
);
4586 if (value
!= NULL
) {
4587 const char *gid_str
;
4590 gid_str
= smbldap_talloc_single_attribute(
4591 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4592 if (gid_str
== NULL
) {
4593 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4594 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4599 id
->gid
= strtoul(gid_str
, NULL
, 10);
4600 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
4605 /* It must be a user */
4607 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4608 "uidNumber", mem_ctx
);
4609 if (value
== NULL
) {
4610 DEBUG(1, ("Could not find uidNumber in %s\n",
4611 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4615 id
->uid
= strtoul(value
, NULL
, 10);
4616 *type
= SID_NAME_USER
;
4620 TALLOC_FREE(mem_ctx
);
4625 * The following functions is called only if
4626 * ldapsam:trusted and ldapsam:editposix are
4631 * ldapsam_create_user creates a new
4632 * posixAccount and sambaSamAccount object
4633 * in the ldap users subtree
4635 * The uid is allocated by winbindd.
4638 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
4639 TALLOC_CTX
*tmp_ctx
, const char *name
,
4640 uint32 acb_info
, uint32
*rid
)
4642 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4643 LDAPMessage
*entry
= NULL
;
4644 LDAPMessage
*result
= NULL
;
4646 BOOL is_machine
= False
;
4647 BOOL add_posix
= False
;
4648 LDAPMod
**mods
= NULL
;
4656 const char *dn
= NULL
;
4664 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
4665 acb_info
& ACB_WSTRUST
||
4666 acb_info
& ACB_SVRTRUST
||
4667 acb_info
& ACB_DOMTRUST
) {
4671 username
= escape_ldap_string_alloc(name
);
4672 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
4673 username
, LDAP_OBJ_POSIXACCOUNT
);
4674 SAFE_FREE(username
);
4676 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4677 if (rc
!= LDAP_SUCCESS
) {
4678 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
4679 return NT_STATUS_UNSUCCESSFUL
;
4681 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4683 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4685 if (num_result
> 1) {
4686 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
4687 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4690 if (num_result
== 1) {
4692 /* check if it is just a posix account.
4693 * or if there is a sid attached to this entry
4696 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4698 return NT_STATUS_UNSUCCESSFUL
;
4701 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
4703 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
4704 return NT_STATUS_USER_EXISTS
;
4707 /* it is just a posix account, retrieve the dn for later use */
4708 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4710 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
4711 return NT_STATUS_NO_MEMORY
;
4715 if (num_result
== 0) {
4719 /* Create the basic samu structure and generate the mods for the ldap commit */
4720 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
4721 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
4725 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
4727 user
= samu_new(tmp_ctx
);
4729 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
4730 return NT_STATUS_NO_MEMORY
;
4733 if (!pdb_set_username(user
, name
, PDB_SET
)) {
4734 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4735 return NT_STATUS_UNSUCCESSFUL
;
4737 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
4738 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4739 return NT_STATUS_UNSUCCESSFUL
;
4742 if (acb_info
& ACB_NORMAL
) {
4743 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
4744 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4745 return NT_STATUS_UNSUCCESSFUL
;
4748 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
4749 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4750 return NT_STATUS_UNSUCCESSFUL
;
4754 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
4755 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4756 return NT_STATUS_UNSUCCESSFUL
;
4760 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
4761 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4762 return NT_STATUS_UNSUCCESSFUL
;
4765 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
4766 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4767 return NT_STATUS_UNSUCCESSFUL
;
4770 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
4771 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
4773 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
4776 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
4778 /* retrieve the Domain Users group gid */
4779 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
4780 !sid_to_gid(&group_sid
, &gid
)) {
4781 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
4782 return NT_STATUS_INVALID_PRIMARY_GROUP
;
4785 /* lets allocate a new userid for this user */
4786 if (!winbind_allocate_uid(&uid
)) {
4787 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
4788 return NT_STATUS_UNSUCCESSFUL
;
4793 /* TODO: choose a more appropriate default for machines */
4794 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
4795 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
4797 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
4798 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
4800 uidstr
= talloc_asprintf(tmp_ctx
, "%d", uid
);
4801 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
4803 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", name
, lp_ldap_machine_suffix ());
4805 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", name
, lp_ldap_user_suffix ());
4808 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
4809 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
4810 return NT_STATUS_NO_MEMORY
;
4813 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
4814 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
4815 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
4816 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
4817 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
4818 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
4819 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
4822 talloc_autofree_ldapmod(tmp_ctx
, mods
);
4825 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
4827 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
4830 if (rc
!= LDAP_SUCCESS
) {
4831 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
4832 return NT_STATUS_UNSUCCESSFUL
;
4835 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
4837 flush_pwnam_cache();
4839 return NT_STATUS_OK
;
4842 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
4844 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4845 LDAPMessage
*result
= NULL
;
4846 LDAPMessage
*entry
= NULL
;
4852 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
4854 filter
= talloc_asprintf(tmp_ctx
,
4857 "(objectClass=%s))",
4858 pdb_get_username(sam_acct
),
4859 LDAP_OBJ_POSIXACCOUNT
,
4860 LDAP_OBJ_SAMBASAMACCOUNT
);
4861 if (filter
== NULL
) {
4862 return NT_STATUS_NO_MEMORY
;
4865 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4866 if (rc
!= LDAP_SUCCESS
) {
4867 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
4868 return NT_STATUS_UNSUCCESSFUL
;
4870 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4872 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4874 if (num_result
== 0) {
4875 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
4876 return NT_STATUS_NO_SUCH_USER
;
4879 if (num_result
> 1) {
4880 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
4881 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4884 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4886 return NT_STATUS_UNSUCCESSFUL
;
4889 /* it is just a posix account, retrieve the dn for later use */
4890 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4892 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
4893 return NT_STATUS_NO_MEMORY
;
4896 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
4897 if (rc
!= LDAP_SUCCESS
) {
4898 return NT_STATUS_UNSUCCESSFUL
;
4901 flush_pwnam_cache();
4903 return NT_STATUS_OK
;
4907 * ldapsam_create_group creates a new
4908 * posixGroup and sambaGroupMapping object
4909 * in the ldap groups subtree
4911 * The gid is allocated by winbindd.
4914 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
4915 TALLOC_CTX
*tmp_ctx
,
4919 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4921 LDAPMessage
*entry
= NULL
;
4922 LDAPMessage
*result
= NULL
;
4924 BOOL is_new_entry
= False
;
4925 LDAPMod
**mods
= NULL
;
4931 const char *dn
= NULL
;
4936 groupname
= escape_ldap_string_alloc(name
);
4937 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
4938 groupname
, LDAP_OBJ_POSIXGROUP
);
4939 SAFE_FREE(groupname
);
4941 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4942 if (rc
!= LDAP_SUCCESS
) {
4943 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
4944 return NT_STATUS_UNSUCCESSFUL
;
4946 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4948 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4950 if (num_result
> 1) {
4951 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
4952 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4955 if (num_result
== 1) {
4957 /* check if it is just a posix group.
4958 * or if there is a sid attached to this entry
4961 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4963 return NT_STATUS_UNSUCCESSFUL
;
4966 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
4968 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
4969 return NT_STATUS_GROUP_EXISTS
;
4972 /* it is just a posix group, retrieve the gid and the dn for later use */
4973 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
4975 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
4976 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4979 gid
= strtoul(tmp
, NULL
, 10);
4981 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4983 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
4984 return NT_STATUS_NO_MEMORY
;
4988 if (num_result
== 0) {
4989 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
4991 is_new_entry
= True
;
4993 /* lets allocate a new groupid for this group */
4994 if (!winbind_allocate_gid(&gid
)) {
4995 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
4996 return NT_STATUS_UNSUCCESSFUL
;
4999 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
5000 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", name
, lp_ldap_group_suffix());
5002 if (!gidstr
|| !dn
) {
5003 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5004 return NT_STATUS_NO_MEMORY
;
5007 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5008 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5009 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5012 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5013 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5017 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5019 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_static(&group_sid
));
5020 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5022 if (!groupsidstr
|| !grouptype
) {
5023 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5024 return NT_STATUS_NO_MEMORY
;
5027 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5028 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5029 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5030 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5031 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5034 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5036 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5037 /* This call may fail with rfc2307bis schema */
5038 /* Retry adding a structural class */
5039 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5040 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5044 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5047 if (rc
!= LDAP_SUCCESS
) {
5048 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5049 return NT_STATUS_UNSUCCESSFUL
;
5052 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5054 return NT_STATUS_OK
;
5057 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
5059 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5060 LDAPMessage
*result
= NULL
;
5061 LDAPMessage
*entry
= NULL
;
5069 /* get the group sid */
5070 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5072 filter
= talloc_asprintf(tmp_ctx
,
5075 "(objectClass=%s))",
5076 sid_string_static(&group_sid
),
5077 LDAP_OBJ_POSIXGROUP
,
5079 if (filter
== NULL
) {
5080 return NT_STATUS_NO_MEMORY
;
5083 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5084 if (rc
!= LDAP_SUCCESS
) {
5085 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5086 return NT_STATUS_UNSUCCESSFUL
;
5088 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5090 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5092 if (num_result
== 0) {
5093 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5094 return NT_STATUS_NO_SUCH_GROUP
;
5097 if (num_result
> 1) {
5098 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5099 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5102 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5104 return NT_STATUS_UNSUCCESSFUL
;
5107 /* here it is, retrieve the dn for later use */
5108 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5110 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5111 return NT_STATUS_NO_MEMORY
;
5114 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5116 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5117 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5120 /* check no user have this group marked as primary group */
5121 filter
= talloc_asprintf(tmp_ctx
,
5124 "(objectClass=%s))",
5126 LDAP_OBJ_POSIXACCOUNT
,
5127 LDAP_OBJ_SAMBASAMACCOUNT
);
5129 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5130 if (rc
!= LDAP_SUCCESS
) {
5131 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5132 return NT_STATUS_UNSUCCESSFUL
;
5134 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5136 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5138 if (num_result
!= 0) {
5139 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5140 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5143 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5144 if (rc
!= LDAP_SUCCESS
) {
5145 return NT_STATUS_UNSUCCESSFUL
;
5148 return NT_STATUS_OK
;
5151 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5152 TALLOC_CTX
*tmp_ctx
,
5157 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5158 LDAPMessage
*entry
= NULL
;
5159 LDAPMessage
*result
= NULL
;
5161 LDAPMod
**mods
= NULL
;
5164 const char *dn
= NULL
;
5171 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5173 case LDAP_MOD_DELETE
:
5174 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5177 return NT_STATUS_UNSUCCESSFUL
;
5180 /* get member sid */
5181 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5183 /* get the group sid */
5184 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5186 filter
= talloc_asprintf(tmp_ctx
,
5189 "(objectClass=%s))",
5190 sid_string_static(&member_sid
),
5191 LDAP_OBJ_POSIXACCOUNT
,
5192 LDAP_OBJ_SAMBASAMACCOUNT
);
5193 if (filter
== NULL
) {
5194 return NT_STATUS_NO_MEMORY
;
5197 /* get the member uid */
5198 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5199 if (rc
!= LDAP_SUCCESS
) {
5200 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5201 return NT_STATUS_UNSUCCESSFUL
;
5203 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5205 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5207 if (num_result
== 0) {
5208 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5209 return NT_STATUS_NO_SUCH_MEMBER
;
5212 if (num_result
> 1) {
5213 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5214 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5217 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5219 return NT_STATUS_UNSUCCESSFUL
;
5222 if (modop
== LDAP_MOD_DELETE
) {
5223 /* check if we are trying to remove the member from his primary group */
5225 gid_t user_gid
, group_gid
;
5227 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5229 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5230 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5233 user_gid
= strtoul(gidstr
, NULL
, 10);
5235 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5236 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5237 return NT_STATUS_UNSUCCESSFUL
;
5240 if (user_gid
== group_gid
) {
5241 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from it's own primary group!\n"));
5242 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5246 /* here it is, retrieve the uid for later use */
5247 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5249 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5250 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5253 filter
= talloc_asprintf(tmp_ctx
,
5256 "(objectClass=%s))",
5257 sid_string_static(&group_sid
),
5258 LDAP_OBJ_POSIXGROUP
,
5262 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5263 if (rc
!= LDAP_SUCCESS
) {
5264 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5265 return NT_STATUS_UNSUCCESSFUL
;
5267 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5269 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5271 if (num_result
== 0) {
5272 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5273 return NT_STATUS_NO_SUCH_GROUP
;
5276 if (num_result
> 1) {
5277 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5278 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5281 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5283 return NT_STATUS_UNSUCCESSFUL
;
5286 /* here it is, retrieve the dn for later use */
5287 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5289 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5290 return NT_STATUS_NO_MEMORY
;
5293 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5295 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5297 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5298 if (rc
!= LDAP_SUCCESS
) {
5299 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5300 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5301 return NT_STATUS_MEMBER_IN_GROUP
;
5303 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5304 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5305 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5307 return NT_STATUS_UNSUCCESSFUL
;
5310 return NT_STATUS_OK
;
5313 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5314 TALLOC_CTX
*tmp_ctx
,
5318 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5320 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5321 TALLOC_CTX
*tmp_ctx
,
5325 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5328 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5329 TALLOC_CTX
*mem_ctx
,
5330 struct samu
*sampass
)
5332 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5333 LDAPMessage
*entry
= NULL
;
5334 LDAPMessage
*result
= NULL
;
5336 LDAPMod
**mods
= NULL
;
5339 const char *dn
= NULL
;
5343 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5345 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5346 DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
5347 return NT_STATUS_UNSUCCESSFUL
;
5349 gidstr
= talloc_asprintf(mem_ctx
, "%d", gid
);
5351 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5352 return NT_STATUS_NO_MEMORY
;
5355 filter
= talloc_asprintf(mem_ctx
,
5358 "(objectClass=%s))",
5359 pdb_get_username(sampass
),
5360 LDAP_OBJ_POSIXACCOUNT
,
5361 LDAP_OBJ_SAMBASAMACCOUNT
);
5362 if (filter
== NULL
) {
5363 return NT_STATUS_NO_MEMORY
;
5366 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5367 if (rc
!= LDAP_SUCCESS
) {
5368 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5369 return NT_STATUS_UNSUCCESSFUL
;
5371 talloc_autofree_ldapmsg(mem_ctx
, result
);
5373 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5375 if (num_result
== 0) {
5376 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5377 return NT_STATUS_NO_SUCH_USER
;
5380 if (num_result
> 1) {
5381 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5382 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5385 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5387 return NT_STATUS_UNSUCCESSFUL
;
5390 /* retrieve the dn for later use */
5391 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5393 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5394 return NT_STATUS_NO_MEMORY
;
5397 /* remove the old one, and add the new one, this way we do not risk races */
5398 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5401 return NT_STATUS_OK
;
5404 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5406 if (rc
!= LDAP_SUCCESS
) {
5407 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5408 pdb_get_username(sampass
), gidstr
));
5409 return NT_STATUS_UNSUCCESSFUL
;
5412 flush_pwnam_cache();
5414 return NT_STATUS_OK
;
5417 /**********************************************************************
5419 *********************************************************************/
5421 static void free_private_data(void **vp
)
5423 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
5425 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
5427 if ((*ldap_state
)->result
!= NULL
) {
5428 ldap_msgfree((*ldap_state
)->result
);
5429 (*ldap_state
)->result
= NULL
;
5431 if ((*ldap_state
)->domain_dn
!= NULL
) {
5432 SAFE_FREE((*ldap_state
)->domain_dn
);
5437 /* No need to free any further, as it is talloc()ed */
5440 /*********************************************************************
5441 Intitalise the parts of the pdb_methods structure that are common to
5443 *********************************************************************/
5445 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
5448 struct ldapsam_privates
*ldap_state
;
5450 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
5454 (*pdb_method
)->name
= "ldapsam";
5456 (*pdb_method
)->setsampwent
= ldapsam_setsampwent
;
5457 (*pdb_method
)->endsampwent
= ldapsam_endsampwent
;
5458 (*pdb_method
)->getsampwent
= ldapsam_getsampwent
;
5459 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
5460 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
5461 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
5462 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
5463 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
5464 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
5466 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
5467 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
5468 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
5469 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
5470 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
5471 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
5472 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
5474 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
5475 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
5477 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
5479 (*pdb_method
)->rid_algorithm
= ldapsam_rid_algorithm
;
5480 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
5482 /* TODO: Setup private data and free */
5484 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
5485 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
5486 return NT_STATUS_NO_MEMORY
;
5489 nt_status
= smbldap_init(*pdb_method
, location
, &ldap_state
->smbldap_state
);
5491 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5495 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
5496 return NT_STATUS_NO_MEMORY
;
5499 (*pdb_method
)->private_data
= ldap_state
;
5501 (*pdb_method
)->free_private_data
= free_private_data
;
5503 return NT_STATUS_OK
;
5506 /**********************************************************************
5507 Initialise the 'compat' mode for pdb_ldap
5508 *********************************************************************/
5510 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
5513 struct ldapsam_privates
*ldap_state
;
5514 char *uri
= talloc_strdup( NULL
, location
);
5516 trim_char( uri
, '\"', '\"' );
5517 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
5521 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5525 (*pdb_method
)->name
= "ldapsam_compat";
5527 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
5528 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
5530 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
5532 return NT_STATUS_OK
;
5535 /**********************************************************************
5536 Initialise the normal mode for pdb_ldap
5537 *********************************************************************/
5539 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
5542 struct ldapsam_privates
*ldap_state
;
5543 uint32 alg_rid_base
;
5544 pstring alg_rid_base_string
;
5545 LDAPMessage
*result
= NULL
;
5546 LDAPMessage
*entry
= NULL
;
5547 DOM_SID ldap_domain_sid
;
5548 DOM_SID secrets_domain_sid
;
5549 pstring domain_sid_string
;
5551 char *uri
= talloc_strdup( NULL
, location
);
5553 trim_char( uri
, '\"', '\"' );
5554 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
5558 if (!NT_STATUS_IS_OK(nt_status
)) {
5562 (*pdb_method
)->name
= "ldapsam";
5564 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
5565 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
5566 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
5567 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
5568 (*pdb_method
)->search_users
= ldapsam_search_users
;
5569 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
5570 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
5572 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
5573 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
5574 (*pdb_method
)->enum_group_memberships
=
5575 ldapsam_enum_group_memberships
;
5576 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
5577 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
5579 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
5580 (*pdb_method
)->create_user
= ldapsam_create_user
;
5581 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
5582 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
5583 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
5584 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
5585 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
5586 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
5590 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
5591 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
5593 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
5595 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
5597 ldap_state
->domain_name
, True
);
5599 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5600 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
5601 "info, nor add one to the domain\n"));
5602 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
5603 "will be unable to allocate new users/groups, "
5604 "and will risk BDCs having inconsistant SIDs\n"));
5605 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
5606 return NT_STATUS_OK
;
5609 /* Given that the above might fail, everything below this must be
5612 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
5615 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
5617 ldap_msgfree(result
);
5618 return NT_STATUS_UNSUCCESSFUL
;
5621 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
5623 return NT_STATUS_UNSUCCESSFUL
;
5626 ldap_state
->domain_dn
= smb_xstrdup(dn
);
5629 if (smbldap_get_single_pstring(
5630 ldap_state
->smbldap_state
->ldap_struct
,
5632 get_userattr_key2string(ldap_state
->schema_ver
,
5633 LDAP_ATTR_USER_SID
),
5634 domain_sid_string
)) {
5636 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
5637 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
5638 "read as a valid SID\n", domain_sid_string
));
5639 return NT_STATUS_INVALID_PARAMETER
;
5641 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
5642 &secrets_domain_sid
);
5643 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
5644 &ldap_domain_sid
)) {
5645 fstring new_sid_str
, old_sid_str
;
5646 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
5647 "%s based on pdb_ldap results %s -> %s\n",
5648 ldap_state
->domain_name
,
5649 sid_to_string(old_sid_str
,
5650 &secrets_domain_sid
),
5651 sid_to_string(new_sid_str
,
5652 &ldap_domain_sid
)));
5654 /* reset secrets.tdb sid */
5655 secrets_store_domain_sid(ldap_state
->domain_name
,
5657 DEBUG(1, ("New global sam SID: %s\n",
5658 sid_to_string(new_sid_str
,
5659 get_global_sam_sid())));
5661 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
5664 if (smbldap_get_single_pstring(
5665 ldap_state
->smbldap_state
->ldap_struct
,
5667 get_attr_key2string( dominfo_attr_list
,
5668 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
5669 alg_rid_base_string
)) {
5670 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
5671 if (alg_rid_base
!= algorithmic_rid_base()) {
5672 DEBUG(0, ("The value of 'algorithmic RID base' has "
5673 "changed since the LDAP\n"
5674 "database was initialised. Aborting. \n"));
5675 ldap_msgfree(result
);
5676 return NT_STATUS_UNSUCCESSFUL
;
5679 ldap_msgfree(result
);
5681 return NT_STATUS_OK
;
5684 NTSTATUS
pdb_ldap_init(void)
5687 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
5690 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
5693 /* Let pdb_nds register backends */
5696 return NT_STATUS_OK
;