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
, 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 attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2);
228 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
229 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
232 /* consumer syncreplCookie: */
233 /* csn=20050126161620Z#0000001#00#00000 */
234 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
236 pstr_sprintf( suffix
, "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
240 /* provider contextCSN */
241 /* 20050126161620Z#000009#00#000000 */
242 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
244 pstr_sprintf( suffix
, "cn=ldapsync,%s", lp_ldap_suffix());
248 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
249 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
251 if (rc
!= LDAP_SUCCESS
) {
255 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
256 if (num_result
!= 1) {
257 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
261 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
263 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
267 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
268 if (values
== NULL
) {
269 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
273 num_values
= ldap_count_values(values
);
274 if (num_values
== 0) {
275 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
280 if (!next_token(&p
, tok
, "#", sizeof(tok
))) {
281 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
286 if (!strncmp(p
, "csn=", strlen("csn=")))
289 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
291 *seq_num
= generalized_to_unix_time(p
);
293 /* very basic sanity check */
295 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
300 ntstatus
= NT_STATUS_OK
;
304 ldap_value_free(values
);
308 talloc_destroy(mem_ctx
);
313 /*******************************************************************
314 Run the search by name.
315 ******************************************************************/
317 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
319 LDAPMessage
** result
,
323 char *escape_user
= escape_ldap_string_alloc(user
);
326 return LDAP_NO_MEMORY
;
330 * in the filter expression, replace %u with the real name
331 * so in ldap filter, %u MUST exist :-)
333 pstr_sprintf(filter
, "(&%s%s)", "(uid=%u)",
334 get_objclass_filter(ldap_state
->schema_ver
));
337 * have to use this here because $ is filtered out
342 all_string_sub(filter
, "%u", escape_user
, sizeof(pstring
));
343 SAFE_FREE(escape_user
);
345 return smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
348 /*******************************************************************
349 Run the search by rid.
350 ******************************************************************/
352 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
353 uint32 rid
, LDAPMessage
** result
,
359 pstr_sprintf(filter
, "(&(rid=%i)%s)", rid
,
360 get_objclass_filter(ldap_state
->schema_ver
));
362 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
367 /*******************************************************************
368 Run the search by SID.
369 ******************************************************************/
371 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
372 const DOM_SID
*sid
, LDAPMessage
** result
,
379 pstr_sprintf(filter
, "(&(%s=%s)%s)",
380 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
381 sid_to_string(sid_string
, sid
),
382 get_objclass_filter(ldap_state
->schema_ver
));
384 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
389 /*******************************************************************
390 Delete complete object or objectclass and attrs from
391 object found in search_result depending on lp_ldap_delete_dn
392 ******************************************************************/
394 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
397 const char *objectclass
,
400 LDAPMod
**mods
= NULL
;
403 BerElement
*ptr
= NULL
;
405 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
407 return LDAP_NO_MEMORY
;
410 if (lp_ldap_delete_dn()) {
411 return smbldap_delete(priv
->smbldap_state
, dn
);
414 /* Ok, delete only the SAM attributes */
416 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
418 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
421 /* We are only allowed to delete the attributes that
424 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
425 if (strequal(*attrib
, name
)) {
426 DEBUG(10, ("ldapsam_delete_entry: deleting "
427 "attribute %s\n", name
));
428 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
439 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
440 talloc_autofree_ldapmod(mem_ctx
, mods
);
442 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
445 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
450 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
451 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
455 strptime(temp
, "%Y%m%d%H%M%SZ", &tm
);
460 /**********************************************************************
461 Initialize struct samu from an LDAP query.
462 (Based on init_sam_from_buffer in pdb_tdb.c)
463 *********************************************************************/
465 static BOOL
init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
466 struct samu
* sampass
,
473 pass_can_change_time
,
474 pass_must_change_time
,
487 char munged_dial
[2048];
489 uint8 smblmpwd
[LM_HASH_LEN
],
490 smbntpwd
[NT_HASH_LEN
];
491 BOOL use_samba_attrs
= True
;
492 uint32 acct_ctrl
= 0;
494 uint16 bad_password_count
= 0,
497 uint8 hours
[MAX_HOURS_LEN
];
499 LOGIN_CACHE
*cache_entry
= NULL
;
502 BOOL expand_explicit
= lp_passdb_expand_explicit();
505 * do a little initialization
509 nt_username
[0] = '\0';
513 logon_script
[0] = '\0';
514 profile_path
[0] = '\0';
516 munged_dial
[0] = '\0';
517 workstations
[0] = '\0';
520 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
521 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
525 if (priv2ld(ldap_state
) == NULL
) {
526 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
527 "ldap_struct is NULL!\n"));
531 if (!smbldap_get_single_pstring(priv2ld(ldap_state
), entry
, "uid",
533 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
538 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
540 pstrcpy(nt_username
, username
);
542 pstrcpy(domain
, ldap_state
->domain_name
);
544 pdb_set_username(sampass
, username
, PDB_SET
);
546 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
547 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
549 /* deal with different attributes between the schema first */
551 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
552 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
553 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
), temp
)) {
554 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
557 if (smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
558 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
), temp
)) {
559 user_rid
= (uint32
)atol(temp
);
560 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
564 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
565 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
566 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
567 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
572 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
573 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
), temp
)) {
574 /* leave as default */
576 pass_last_set_time
= (time_t) atol(temp
);
577 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
, PDB_SET
);
580 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
581 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
)) {
582 /* leave as default */
584 logon_time
= (time_t) atol(temp
);
585 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
588 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
589 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
)) {
590 /* leave as default */
592 logoff_time
= (time_t) atol(temp
);
593 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
596 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
597 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
)) {
598 /* leave as default */
600 kickoff_time
= (time_t) atol(temp
);
601 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
604 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
605 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
)) {
606 /* leave as default */
608 pass_can_change_time
= (time_t) atol(temp
);
609 pdb_set_pass_can_change_time(sampass
, pass_can_change_time
, PDB_SET
);
612 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
613 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
)) {
614 /* leave as default */
616 pass_must_change_time
= (time_t) atol(temp
);
617 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, PDB_SET
);
620 /* recommend that 'gecos' and 'displayName' should refer to the same
621 * attribute OID. userFullName depreciated, only used by Samba
622 * primary rules of LDAP: don't make a new attribute when one is already defined
623 * that fits your needs; using cn then displayName rather than 'userFullName'
626 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
627 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
), fullname
)) {
628 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
629 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_CN
), fullname
)) {
630 /* leave as default */
632 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
635 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
638 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
639 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
), dir_drive
))
641 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
643 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
646 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
647 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
), homedir
))
649 pdb_set_homedir( sampass
,
650 talloc_sub_basic(sampass
, username
, lp_logon_home()),
653 pstrcpy( tmpstring
, homedir
);
654 if (expand_explicit
) {
655 standard_sub_basic( username
, tmpstring
,
658 pdb_set_homedir(sampass
, tmpstring
, PDB_SET
);
661 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
662 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
), logon_script
))
664 pdb_set_logon_script( sampass
,
665 talloc_sub_basic(sampass
, username
, lp_logon_script()),
668 pstrcpy( tmpstring
, logon_script
);
669 if (expand_explicit
) {
670 standard_sub_basic( username
, tmpstring
,
673 pdb_set_logon_script(sampass
, tmpstring
, PDB_SET
);
676 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
677 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
), profile_path
))
679 pdb_set_profile_path( sampass
,
680 talloc_sub_basic( sampass
, username
, lp_logon_path()),
683 pstrcpy( tmpstring
, profile_path
);
684 if (expand_explicit
) {
685 standard_sub_basic( username
, tmpstring
,
688 pdb_set_profile_path(sampass
, tmpstring
, PDB_SET
);
691 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
692 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
), acct_desc
))
694 /* leave as default */
696 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
699 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
700 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
), workstations
)) {
701 /* leave as default */;
703 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
706 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
707 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
), munged_dial
, sizeof(munged_dial
))) {
708 /* leave as default */;
710 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
713 /* FIXME: hours stuff should be cleaner */
717 memset(hours
, 0xff, hours_len
);
719 if (ldap_state
->is_nds_ldap
) {
722 char clear_text_pw
[512];
724 /* Make call to Novell eDirectory ldap extension to get clear text password.
725 NOTE: This will only work if we have an SSL connection to eDirectory. */
726 user_dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
727 if (user_dn
!= NULL
) {
728 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username
, user_dn
));
730 pwd_len
= sizeof(clear_text_pw
);
731 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
732 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
733 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
))
735 ZERO_STRUCT(smblmpwd
);
736 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
))
738 ZERO_STRUCT(smbntpwd
);
739 use_samba_attrs
= False
;
742 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
746 if (use_samba_attrs
) {
747 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
748 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
), temp
)) {
749 /* leave as default */
751 pdb_gethexpwd(temp
, smblmpwd
);
752 memset((char *)temp
, '\0', strlen(temp
)+1);
753 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
))
755 ZERO_STRUCT(smblmpwd
);
758 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
759 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
), temp
)) {
760 /* leave as default */
762 pdb_gethexpwd(temp
, smbntpwd
);
763 memset((char *)temp
, '\0', strlen(temp
)+1);
764 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
))
766 ZERO_STRUCT(smbntpwd
);
772 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
774 uint8
*pwhist
= NULL
;
777 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
778 pwHistLen
= MIN(pwHistLen
, ((sizeof(temp
)-1)/64));
780 if ((pwhist
= SMB_MALLOC(pwHistLen
* PW_HISTORY_ENTRY_LEN
)) == NULL
){
781 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
784 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
786 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
787 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
), temp
)) {
788 /* leave as default - zeros */
790 BOOL hex_failed
= False
;
791 for (i
= 0; i
< pwHistLen
; i
++){
792 /* Get the 16 byte salt. */
793 if (!pdb_gethexpwd(&temp
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
797 /* Get the 16 byte MD5 hash of salt+passwd. */
798 if (!pdb_gethexpwd(&temp
[(i
*64)+32],
799 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
])) {
805 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
807 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
810 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
817 if (!smbldap_get_single_pstring (ldap_state
->smbldap_state
->ldap_struct
, entry
,
818 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
), temp
)) {
819 acct_ctrl
|= ACB_NORMAL
;
821 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
824 acct_ctrl
|= ACB_NORMAL
;
826 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
829 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
830 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
832 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
833 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_COUNT
), temp
)) {
834 /* leave as default */
836 bad_password_count
= (uint32
) atol(temp
);
837 pdb_set_bad_password_count(sampass
, bad_password_count
, PDB_SET
);
840 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
841 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_TIME
), temp
)) {
842 /* leave as default */
844 bad_password_time
= (time_t) atol(temp
);
845 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
849 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
850 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_COUNT
), temp
)) {
851 /* leave as default */
853 logon_count
= (uint32
) atol(temp
);
854 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
857 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
859 if(!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
860 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_HOURS
), temp
)) {
861 /* leave as default */
863 pdb_gethexhours(temp
, hours
);
864 memset((char *)temp
, '\0', strlen(temp
) +1);
865 pdb_set_hours(sampass
, hours
, PDB_SET
);
869 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
870 if (smbldap_get_single_pstring(priv2ld(ldap_state
), entry
,
871 "uidNumber", temp
)) {
872 /* We've got a uid, feed the cache */
873 uid_t uid
= strtoul(temp
, NULL
, 10);
874 store_uid_sid_cache(pdb_get_user_sid(sampass
), uid
);
878 /* check the timestamp of the cache vs ldap entry */
879 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
883 /* see if we have newer updates */
884 if (!(cache_entry
= login_cache_read(sampass
))) {
885 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
886 (unsigned int)pdb_get_bad_password_count(sampass
),
887 (unsigned int)pdb_get_bad_password_time(sampass
)));
891 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
892 (unsigned int)ldap_entry_time
, (unsigned int)cache_entry
->entry_timestamp
,
893 (unsigned int)cache_entry
->bad_password_time
));
895 if (ldap_entry_time
> cache_entry
->entry_timestamp
) {
896 /* cache is older than directory , so
897 we need to delete the entry but allow the
898 fields to be written out */
899 login_cache_delentry(sampass
);
902 pdb_set_acct_ctrl(sampass
,
903 pdb_get_acct_ctrl(sampass
) |
904 (cache_entry
->acct_ctrl
& ACB_AUTOLOCK
),
906 pdb_set_bad_password_count(sampass
,
907 cache_entry
->bad_password_count
,
909 pdb_set_bad_password_time(sampass
,
910 cache_entry
->bad_password_time
,
914 SAFE_FREE(cache_entry
);
918 /**********************************************************************
919 Initialize the ldap db from a struct samu. Called on update.
920 (Based on init_buffer_from_sam in pdb_tdb.c)
921 *********************************************************************/
923 static BOOL
init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
924 LDAPMessage
*existing
,
925 LDAPMod
*** mods
, struct samu
* sampass
,
926 BOOL (*need_update
)(const struct samu
*,
932 if (mods
== NULL
|| sampass
== NULL
) {
933 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
940 * took out adding "objectclass: sambaAccount"
941 * do this on a per-mod basis
943 if (need_update(sampass
, PDB_USERNAME
))
944 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
945 "uid", pdb_get_username(sampass
));
947 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
949 /* only update the RID if we actually need to */
950 if (need_update(sampass
, PDB_USERSID
)) {
952 fstring dom_sid_string
;
953 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
955 switch ( ldap_state
->schema_ver
) {
956 case SCHEMAVER_SAMBAACCOUNT
:
957 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
958 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
959 sid_to_string(sid_string
, user_sid
),
960 sid_to_string(dom_sid_string
, &ldap_state
->domain_sid
)));
963 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
964 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
965 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
969 case SCHEMAVER_SAMBASAMACCOUNT
:
970 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
971 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
972 sid_to_string(sid_string
, user_sid
));
976 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
981 /* we don't need to store the primary group RID - so leaving it
982 'free' to hang off the unix primary group makes life easier */
984 if (need_update(sampass
, PDB_GROUPSID
)) {
986 fstring dom_sid_string
;
987 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
989 switch ( ldap_state
->schema_ver
) {
990 case SCHEMAVER_SAMBAACCOUNT
:
991 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
992 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
993 sid_to_string(sid_string
, group_sid
),
994 sid_to_string(dom_sid_string
, &ldap_state
->domain_sid
)));
998 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
999 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1000 get_userattr_key2string(ldap_state
->schema_ver
,
1001 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1004 case SCHEMAVER_SAMBASAMACCOUNT
:
1005 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1006 get_userattr_key2string(ldap_state
->schema_ver
,
1007 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_string(sid_string
, group_sid
));
1011 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1017 /* displayName, cn, and gecos should all be the same
1018 * most easily accomplished by giving them the same OID
1019 * gecos isn't set here b/c it should be handled by the
1021 * We change displayName only and fall back to cn if
1022 * it does not exist.
1025 if (need_update(sampass
, PDB_FULLNAME
))
1026 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1027 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1028 pdb_get_fullname(sampass
));
1030 if (need_update(sampass
, PDB_ACCTDESC
))
1031 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1032 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1033 pdb_get_acct_desc(sampass
));
1035 if (need_update(sampass
, PDB_WORKSTATIONS
))
1036 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1037 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1038 pdb_get_workstations(sampass
));
1040 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1041 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1042 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1043 pdb_get_munged_dial(sampass
));
1045 if (need_update(sampass
, PDB_SMBHOME
))
1046 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1047 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1048 pdb_get_homedir(sampass
));
1050 if (need_update(sampass
, PDB_DRIVE
))
1051 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1052 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1053 pdb_get_dir_drive(sampass
));
1055 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1056 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1057 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1058 pdb_get_logon_script(sampass
));
1060 if (need_update(sampass
, PDB_PROFILE
))
1061 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1062 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1063 pdb_get_profile_path(sampass
));
1065 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logon_time(sampass
));
1066 if (need_update(sampass
, PDB_LOGONTIME
))
1067 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1068 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1070 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logoff_time(sampass
));
1071 if (need_update(sampass
, PDB_LOGOFFTIME
))
1072 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1073 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1075 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_kickoff_time(sampass
));
1076 if (need_update(sampass
, PDB_KICKOFFTIME
))
1077 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1078 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1080 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_can_change_time(sampass
));
1081 if (need_update(sampass
, PDB_CANCHANGETIME
))
1082 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1083 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1085 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_must_change_time(sampass
));
1086 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1087 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1088 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1091 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1092 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1094 if (need_update(sampass
, PDB_LMPASSWD
)) {
1095 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1097 pdb_sethexpwd(temp
, lm_pw
,
1098 pdb_get_acct_ctrl(sampass
));
1099 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1100 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1103 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1104 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1108 if (need_update(sampass
, PDB_NTPASSWD
)) {
1109 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1111 pdb_sethexpwd(temp
, nt_pw
,
1112 pdb_get_acct_ctrl(sampass
));
1113 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1114 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1117 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1118 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1123 if (need_update(sampass
, PDB_PWHISTORY
)) {
1124 uint32 pwHistLen
= 0;
1125 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
1126 if (pwHistLen
== 0) {
1127 /* Remove any password history from the LDAP store. */
1128 memset(temp
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1132 uint32 currHistLen
= 0;
1133 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1134 if (pwhist
!= NULL
) {
1135 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1136 pwHistLen
= MIN(pwHistLen
, ((sizeof(temp
)-1)/64));
1137 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1138 /* Store the salt. */
1139 pdb_sethexpwd(&temp
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1140 /* Followed by the md5 hash of salt + md4 hash */
1141 pdb_sethexpwd(&temp
[(i
*64)+32],
1142 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1143 DEBUG(100, ("temp=%s\n", temp
));
1147 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1148 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1152 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1153 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_last_set_time(sampass
));
1154 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1155 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1160 if (need_update(sampass
, PDB_HOURS
)) {
1161 const uint8
*hours
= pdb_get_hours(sampass
);
1163 pdb_sethexhours(temp
, hours
);
1164 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1167 get_userattr_key2string(ldap_state
->schema_ver
,
1168 LDAP_ATTR_LOGON_HOURS
),
1173 if (need_update(sampass
, PDB_ACCTCTRL
))
1174 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1175 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1176 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1178 /* password lockout cache:
1179 - If we are now autolocking or clearing, we write to ldap
1180 - If we are clearing, we delete the cache entry
1181 - If the count is > 0, we update the cache
1183 This even means when autolocking, we cache, just in case the
1184 update doesn't work, and we have to cache the autolock flag */
1186 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1187 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1188 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1189 time_t badtime
= pdb_get_bad_password_time(sampass
);
1191 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, &pol
);
1193 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1194 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1196 if ((badcount
>= pol
) || (badcount
== 0)) {
1197 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1198 (unsigned int)badcount
, (unsigned int)badtime
));
1199 slprintf (temp
, sizeof (temp
) - 1, "%li", (long)badcount
);
1201 ldap_state
->smbldap_state
->ldap_struct
,
1203 get_userattr_key2string(
1204 ldap_state
->schema_ver
,
1205 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1208 slprintf (temp
, sizeof (temp
) - 1, "%li", badtime
);
1210 ldap_state
->smbldap_state
->ldap_struct
,
1212 get_userattr_key2string(
1213 ldap_state
->schema_ver
,
1214 LDAP_ATTR_BAD_PASSWORD_TIME
),
1217 if (badcount
== 0) {
1218 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1219 login_cache_delentry(sampass
);
1221 LOGIN_CACHE cache_entry
;
1223 cache_entry
.entry_timestamp
= time(NULL
);
1224 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1225 cache_entry
.bad_password_count
= badcount
;
1226 cache_entry
.bad_password_time
= badtime
;
1228 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1229 login_cache_write(sampass
, cache_entry
);
1236 /**********************************************************************
1237 Connect to LDAP server for password enumeration.
1238 *********************************************************************/
1240 static NTSTATUS
ldapsam_setsampwent(struct pdb_methods
*my_methods
, BOOL update
, uint32 acb_mask
)
1242 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1244 pstring filter
, suffix
;
1245 const char **attr_list
;
1246 BOOL machine_mask
= False
, user_mask
= False
;
1248 pstr_sprintf( filter
, "(&%s%s)", "(uid=%u)",
1249 get_objclass_filter(ldap_state
->schema_ver
));
1250 all_string_sub(filter
, "%u", "*", sizeof(pstring
));
1252 machine_mask
= ((acb_mask
!= 0) && (acb_mask
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)));
1253 user_mask
= ((acb_mask
!= 0) && (acb_mask
& ACB_NORMAL
));
1256 pstrcpy(suffix
, lp_ldap_machine_suffix());
1257 } else if (user_mask
) {
1258 pstrcpy(suffix
, lp_ldap_user_suffix());
1260 pstrcpy(suffix
, lp_ldap_suffix());
1263 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1266 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1267 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
, LDAP_SCOPE_SUBTREE
, filter
,
1268 attr_list
, 0, &ldap_state
->result
);
1269 TALLOC_FREE( attr_list
);
1271 if (rc
!= LDAP_SUCCESS
) {
1272 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc
)));
1273 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix
, filter
));
1274 ldap_msgfree(ldap_state
->result
);
1275 ldap_state
->result
= NULL
;
1276 return NT_STATUS_UNSUCCESSFUL
;
1279 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1280 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
1281 ldap_state
->result
), suffix
));
1283 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
1284 ldap_state
->result
);
1285 ldap_state
->index
= 0;
1287 return NT_STATUS_OK
;
1290 /**********************************************************************
1291 End enumeration of the LDAP password list.
1292 *********************************************************************/
1294 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1296 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1297 if (ldap_state
->result
) {
1298 ldap_msgfree(ldap_state
->result
);
1299 ldap_state
->result
= NULL
;
1303 /**********************************************************************
1304 Get the next entry in the LDAP password database.
1305 *********************************************************************/
1307 static NTSTATUS
ldapsam_getsampwent(struct pdb_methods
*my_methods
,
1310 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1311 struct ldapsam_privates
*ldap_state
=
1312 (struct ldapsam_privates
*)my_methods
->private_data
;
1316 if (!ldap_state
->entry
)
1319 ldap_state
->index
++;
1320 bret
= init_sam_from_ldap(ldap_state
, user
, ldap_state
->entry
);
1322 ldap_state
->entry
= ldap_next_entry(priv2ld(ldap_state
),
1326 return NT_STATUS_OK
;
1329 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1330 const char *new_attr
)
1334 if (new_attr
== NULL
) {
1338 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1342 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1344 SMB_ASSERT((*attr_list
) != NULL
);
1345 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1346 (*attr_list
)[i
+1] = NULL
;
1349 /**********************************************************************
1350 Get struct samu entry from LDAP by username.
1351 *********************************************************************/
1353 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1355 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1356 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1357 LDAPMessage
*result
= NULL
;
1358 LDAPMessage
*entry
= NULL
;
1360 const char ** attr_list
;
1363 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1364 append_attr(user
, &attr_list
,
1365 get_userattr_key2string(ldap_state
->schema_ver
,
1366 LDAP_ATTR_MOD_TIMESTAMP
));
1367 append_attr(user
, &attr_list
, "uidNumber");
1368 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1370 TALLOC_FREE( attr_list
);
1372 if ( rc
!= LDAP_SUCCESS
)
1373 return NT_STATUS_NO_SUCH_USER
;
1375 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1378 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1379 ldap_msgfree(result
);
1380 return NT_STATUS_NO_SUCH_USER
;
1381 } else if (count
> 1) {
1382 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1383 ldap_msgfree(result
);
1384 return NT_STATUS_NO_SUCH_USER
;
1387 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1389 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1390 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1391 ldap_msgfree(result
);
1392 return NT_STATUS_NO_SUCH_USER
;
1394 pdb_set_backend_private_data(user
, result
, NULL
,
1395 my_methods
, PDB_CHANGED
);
1396 talloc_autofree_ldapmsg(user
, result
);
1399 ldap_msgfree(result
);
1404 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1405 const DOM_SID
*sid
, LDAPMessage
**result
)
1408 const char ** attr_list
;
1411 switch ( ldap_state
->schema_ver
) {
1412 case SCHEMAVER_SAMBASAMACCOUNT
: {
1413 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1414 if (tmp_ctx
== NULL
) {
1415 return LDAP_NO_MEMORY
;
1418 attr_list
= get_userattr_list(tmp_ctx
,
1419 ldap_state
->schema_ver
);
1420 append_attr(tmp_ctx
, &attr_list
,
1421 get_userattr_key2string(
1422 ldap_state
->schema_ver
,
1423 LDAP_ATTR_MOD_TIMESTAMP
));
1424 append_attr(tmp_ctx
, &attr_list
, "uidNumber");
1425 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1427 TALLOC_FREE(tmp_ctx
);
1429 if ( rc
!= LDAP_SUCCESS
)
1434 case SCHEMAVER_SAMBAACCOUNT
:
1435 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1439 attr_list
= get_userattr_list(NULL
,
1440 ldap_state
->schema_ver
);
1441 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1442 TALLOC_FREE( attr_list
);
1444 if ( rc
!= LDAP_SUCCESS
)
1451 /**********************************************************************
1452 Get struct samu entry from LDAP by SID.
1453 *********************************************************************/
1455 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1457 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1458 LDAPMessage
*result
= NULL
;
1459 LDAPMessage
*entry
= NULL
;
1464 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1466 if (rc
!= LDAP_SUCCESS
)
1467 return NT_STATUS_NO_SUCH_USER
;
1469 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1472 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string
, sid
),
1474 ldap_msgfree(result
);
1475 return NT_STATUS_NO_SUCH_USER
;
1476 } else if (count
> 1) {
1477 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string
, sid
),
1479 ldap_msgfree(result
);
1480 return NT_STATUS_NO_SUCH_USER
;
1483 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1485 ldap_msgfree(result
);
1486 return NT_STATUS_NO_SUCH_USER
;
1489 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1490 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1491 ldap_msgfree(result
);
1492 return NT_STATUS_NO_SUCH_USER
;
1495 pdb_set_backend_private_data(user
, result
, NULL
,
1496 my_methods
, PDB_CHANGED
);
1497 talloc_autofree_ldapmsg(user
, result
);
1498 return NT_STATUS_OK
;
1501 static BOOL
ldapsam_can_pwchange_exop(struct smbldap_state
*ldap_state
)
1503 return smbldap_has_extension(ldap_state
, LDAP_EXOP_MODIFY_PASSWD
);
1506 /********************************************************************
1507 Do the actual modification - also change a plaintext passord if
1509 **********************************************************************/
1511 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1512 struct samu
*newpwd
, char *dn
,
1513 LDAPMod
**mods
, int ldap_op
,
1514 BOOL (*need_update
)(const struct samu
*, enum pdb_elements
))
1516 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1519 if (!my_methods
|| !newpwd
|| !dn
) {
1520 return NT_STATUS_INVALID_PARAMETER
;
1524 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1525 /* may be password change below however */
1529 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1532 rc
= smbldap_add(ldap_state
->smbldap_state
,
1535 case LDAP_MOD_REPLACE
:
1536 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1540 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1542 return NT_STATUS_INVALID_PARAMETER
;
1545 if (rc
!=LDAP_SUCCESS
) {
1546 return NT_STATUS_UNSUCCESSFUL
;
1550 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1551 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1552 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1553 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1556 char *retoid
= NULL
;
1557 struct berval
*retdata
= NULL
;
1558 char *utf8_password
;
1561 if (!ldap_state
->is_nds_ldap
) {
1562 if (!ldapsam_can_pwchange_exop(ldap_state
->smbldap_state
)) {
1563 DEBUG(2, ("ldap password change requested, but LDAP "
1564 "server does not support it -- ignoring\n"));
1565 return NT_STATUS_OK
;
1569 if (push_utf8_allocate(&utf8_password
, pdb_get_plaintext_passwd(newpwd
)) == (size_t)-1) {
1570 return NT_STATUS_NO_MEMORY
;
1573 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1574 return NT_STATUS_NO_MEMORY
;
1577 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1578 DEBUG(0,("ber_alloc_t returns NULL\n"));
1579 SAFE_FREE(utf8_password
);
1580 return NT_STATUS_UNSUCCESSFUL
;
1583 ber_printf (ber
, "{");
1584 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
, utf8_dn
);
1585 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
, utf8_password
);
1586 ber_printf (ber
, "N}");
1588 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1589 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1592 SAFE_FREE(utf8_password
);
1593 return NT_STATUS_UNSUCCESSFUL
;
1597 SAFE_FREE(utf8_password
);
1600 if (!ldap_state
->is_nds_ldap
) {
1601 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1602 LDAP_EXOP_MODIFY_PASSWD
,
1603 bv
, NULL
, NULL
, &retoid
,
1606 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1607 pdb_get_plaintext_passwd(newpwd
));
1609 if (rc
!= LDAP_SUCCESS
) {
1610 char *ld_error
= NULL
;
1612 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1613 DEBUG(3, ("Could not set userPassword "
1614 "attribute due to an objectClass "
1615 "violation -- ignoring\n"));
1617 return NT_STATUS_OK
;
1620 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1622 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1623 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1624 SAFE_FREE(ld_error
);
1626 return NT_STATUS_UNSUCCESSFUL
;
1628 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1629 #ifdef DEBUG_PASSWORD
1630 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1633 ber_bvfree(retdata
);
1635 ldap_memfree(retoid
);
1639 return NT_STATUS_OK
;
1642 /**********************************************************************
1643 Delete entry from LDAP for username.
1644 *********************************************************************/
1646 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1647 struct samu
* sam_acct
)
1649 struct ldapsam_privates
*priv
=
1650 (struct ldapsam_privates
*)my_methods
->private_data
;
1653 LDAPMessage
*msg
, *entry
;
1654 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1655 const char **attr_list
;
1656 TALLOC_CTX
*mem_ctx
;
1659 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1660 return NT_STATUS_INVALID_PARAMETER
;
1663 sname
= pdb_get_username(sam_acct
);
1665 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1668 mem_ctx
= talloc_new(NULL
);
1669 if (mem_ctx
== NULL
) {
1670 DEBUG(0, ("talloc_new failed\n"));
1674 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1675 if (attr_list
== NULL
) {
1679 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1681 if ((rc
!= LDAP_SUCCESS
) ||
1682 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1683 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1684 DEBUG(5, ("Could not find user %s\n", sname
));
1685 result
= NT_STATUS_NO_SUCH_USER
;
1689 rc
= ldapsam_delete_entry(
1690 priv
, mem_ctx
, entry
,
1691 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1692 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1695 result
= (rc
== LDAP_SUCCESS
) ?
1696 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1699 TALLOC_FREE(mem_ctx
);
1703 /**********************************************************************
1704 Helper function to determine for update_sam_account whether
1705 we need LDAP modification.
1706 *********************************************************************/
1708 static BOOL
element_is_changed(const struct samu
*sampass
,
1709 enum pdb_elements element
)
1711 return IS_SAM_CHANGED(sampass
, element
);
1714 /**********************************************************************
1716 *********************************************************************/
1718 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1720 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1721 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1724 LDAPMessage
*result
= NULL
;
1725 LDAPMessage
*entry
= NULL
;
1726 LDAPMod
**mods
= NULL
;
1727 const char **attr_list
;
1729 result
= pdb_get_backend_private_data(newpwd
, my_methods
);
1731 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1732 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1733 TALLOC_FREE( attr_list
);
1734 if (rc
!= LDAP_SUCCESS
) {
1735 return NT_STATUS_UNSUCCESSFUL
;
1737 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1738 my_methods
, PDB_CHANGED
);
1739 talloc_autofree_ldapmsg(newpwd
, result
);
1742 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1743 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1744 return NT_STATUS_UNSUCCESSFUL
;
1747 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1748 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1750 return NT_STATUS_UNSUCCESSFUL
;
1753 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1755 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1756 element_is_changed
)) {
1757 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1760 ldap_mods_free(mods
,True
);
1761 return NT_STATUS_UNSUCCESSFUL
;
1765 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1766 pdb_get_username(newpwd
)));
1768 return NT_STATUS_OK
;
1771 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1772 ldap_mods_free(mods
,True
);
1775 if (!NT_STATUS_IS_OK(ret
)) {
1779 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1780 pdb_get_username(newpwd
)));
1781 return NT_STATUS_OK
;
1784 /***************************************************************************
1785 Renames a struct samu
1786 - The "rename user script" has full responsibility for changing everything
1787 ***************************************************************************/
1789 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1790 struct samu
*old_acct
,
1791 const char *newname
)
1793 const char *oldname
;
1795 pstring rename_script
;
1798 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1799 return NT_STATUS_INVALID_PARAMETER
;
1802 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1803 return NT_STATUS_INVALID_PARAMETER
;
1806 oldname
= pdb_get_username(old_acct
);
1808 /* rename the posix user */
1809 pstrcpy(rename_script
, lp_renameuser_script());
1811 if (!(*rename_script
))
1812 return NT_STATUS_ACCESS_DENIED
;
1814 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1817 /* we have to allow the account name to end with a '$' */
1818 string_sub2(rename_script
, "%unew", newname
, sizeof(pstring
),
1820 string_sub2(rename_script
, "%uold", oldname
, sizeof(pstring
),
1822 rc
= smbrun(rename_script
, NULL
);
1824 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
1825 rename_script
, rc
));
1828 return NT_STATUS_UNSUCCESSFUL
;
1830 return NT_STATUS_OK
;
1833 /**********************************************************************
1834 Helper function to determine for update_sam_account whether
1835 we need LDAP modification.
1836 *********************************************************************/
1838 static BOOL
element_is_set_or_changed(const struct samu
*sampass
,
1839 enum pdb_elements element
)
1841 return (IS_SAM_SET(sampass
, element
) ||
1842 IS_SAM_CHANGED(sampass
, element
));
1845 /**********************************************************************
1846 Add struct samu to LDAP.
1847 *********************************************************************/
1849 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1851 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1852 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1854 LDAPMessage
*result
= NULL
;
1855 LDAPMessage
*entry
= NULL
;
1857 LDAPMod
**mods
= NULL
;
1858 int ldap_op
= LDAP_MOD_REPLACE
;
1860 const char **attr_list
;
1862 const char *username
= pdb_get_username(newpwd
);
1863 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
1867 if (!username
|| !*username
) {
1868 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1869 return NT_STATUS_INVALID_PARAMETER
;
1872 /* free this list after the second search or in case we exit on failure */
1873 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1875 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
1877 if (rc
!= LDAP_SUCCESS
) {
1878 TALLOC_FREE( attr_list
);
1879 return NT_STATUS_UNSUCCESSFUL
;
1882 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1883 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1885 ldap_msgfree(result
);
1886 TALLOC_FREE( attr_list
);
1887 return NT_STATUS_UNSUCCESSFUL
;
1889 ldap_msgfree(result
);
1892 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
1893 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1895 if (rc
== LDAP_SUCCESS
) {
1896 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1897 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1898 sid_to_string(sid_string
, sid
)));
1899 TALLOC_FREE( attr_list
);
1900 ldap_msgfree(result
);
1901 return NT_STATUS_UNSUCCESSFUL
;
1903 ldap_msgfree(result
);
1907 /* does the entry already exist but without a samba attributes?
1908 we need to return the samba attributes here */
1910 escape_user
= escape_ldap_string_alloc( username
);
1911 pstrcpy( filter
, "(uid=%u)" );
1912 all_string_sub( filter
, "%u", escape_user
, sizeof(filter
) );
1913 SAFE_FREE( escape_user
);
1915 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1916 filter
, attr_list
, &result
);
1917 if ( rc
!= LDAP_SUCCESS
) {
1918 TALLOC_FREE( attr_list
);
1919 return NT_STATUS_UNSUCCESSFUL
;
1922 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1924 if (num_result
> 1) {
1925 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1926 TALLOC_FREE( attr_list
);
1927 ldap_msgfree(result
);
1928 return NT_STATUS_UNSUCCESSFUL
;
1931 /* Check if we need to update an existing entry */
1932 if (num_result
== 1) {
1935 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1936 ldap_op
= LDAP_MOD_REPLACE
;
1937 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1938 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1940 TALLOC_FREE( attr_list
);
1941 ldap_msgfree(result
);
1942 return NT_STATUS_UNSUCCESSFUL
;
1944 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
1947 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
1949 /* There might be a SID for this account already - say an idmap entry */
1951 pstr_sprintf(filter
, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
1952 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1953 sid_to_string(sid_string
, sid
),
1954 LDAP_OBJ_IDMAP_ENTRY
,
1955 LDAP_OBJ_SID_ENTRY
);
1957 /* free old result before doing a new search */
1958 if (result
!= NULL
) {
1959 ldap_msgfree(result
);
1962 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1963 filter
, attr_list
, &result
);
1965 if ( rc
!= LDAP_SUCCESS
) {
1966 TALLOC_FREE( attr_list
);
1967 return NT_STATUS_UNSUCCESSFUL
;
1970 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1972 if (num_result
> 1) {
1973 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1974 TALLOC_FREE( attr_list
);
1975 ldap_msgfree(result
);
1976 return NT_STATUS_UNSUCCESSFUL
;
1979 /* Check if we need to update an existing entry */
1980 if (num_result
== 1) {
1983 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1984 ldap_op
= LDAP_MOD_REPLACE
;
1985 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1986 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1988 TALLOC_FREE( attr_list
);
1989 ldap_msgfree(result
);
1990 return NT_STATUS_UNSUCCESSFUL
;
1992 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
1997 TALLOC_FREE( attr_list
);
1999 if (num_result
== 0) {
2000 /* Check if we need to add an entry */
2001 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2002 ldap_op
= LDAP_MOD_ADD
;
2003 if (username
[strlen(username
)-1] == '$') {
2004 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_machine_suffix ());
2006 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_user_suffix ());
2010 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2011 element_is_set_or_changed
)) {
2012 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2013 ldap_msgfree(result
);
2015 ldap_mods_free(mods
,True
);
2016 return NT_STATUS_UNSUCCESSFUL
;
2019 ldap_msgfree(result
);
2022 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2023 return NT_STATUS_UNSUCCESSFUL
;
2025 switch ( ldap_state
->schema_ver
) {
2026 case SCHEMAVER_SAMBAACCOUNT
:
2027 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2029 case SCHEMAVER_SAMBASAMACCOUNT
:
2030 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2033 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2037 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2038 if (!NT_STATUS_IS_OK(ret
)) {
2039 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2040 pdb_get_username(newpwd
),dn
));
2041 ldap_mods_free(mods
, True
);
2045 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2046 ldap_mods_free(mods
, True
);
2048 return NT_STATUS_OK
;
2051 /**********************************************************************
2052 *********************************************************************/
2054 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2056 LDAPMessage
** result
)
2058 int scope
= LDAP_SCOPE_SUBTREE
;
2060 const char **attr_list
;
2062 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2063 rc
= smbldap_search(ldap_state
->smbldap_state
,
2064 lp_ldap_group_suffix (), scope
,
2065 filter
, attr_list
, 0, result
);
2066 TALLOC_FREE(attr_list
);
2071 /**********************************************************************
2072 *********************************************************************/
2074 static BOOL
init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2075 GROUP_MAP
*map
, LDAPMessage
*entry
)
2079 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2080 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2081 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2085 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2086 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
), temp
)) {
2087 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2088 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2091 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2093 map
->gid
= (gid_t
)atol(temp
);
2095 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2096 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
), temp
)) {
2097 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2098 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2102 if (!string_to_sid(&map
->sid
, temp
)) {
2103 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2107 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2108 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
), temp
)) {
2109 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2110 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2113 map
->sid_name_use
= (enum SID_NAME_USE
)atol(temp
);
2115 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2116 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2117 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2121 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2122 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), temp
)) {
2124 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2125 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_CN
), temp
))
2127 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2128 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2132 fstrcpy(map
->nt_name
, temp
);
2134 if (!smbldap_get_single_pstring(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2135 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DESC
), temp
)) {
2138 fstrcpy(map
->comment
, temp
);
2140 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
2141 store_gid_sid_cache(&map
->sid
, map
->gid
);
2147 /**********************************************************************
2148 *********************************************************************/
2150 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2154 struct ldapsam_privates
*ldap_state
=
2155 (struct ldapsam_privates
*)methods
->private_data
;
2156 LDAPMessage
*result
= NULL
;
2157 LDAPMessage
*entry
= NULL
;
2160 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2162 return NT_STATUS_NO_SUCH_GROUP
;
2165 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2168 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2169 ldap_msgfree(result
);
2170 return NT_STATUS_NO_SUCH_GROUP
;
2174 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2175 "count=%d\n", filter
, count
));
2176 ldap_msgfree(result
);
2177 return NT_STATUS_NO_SUCH_GROUP
;
2180 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2183 ldap_msgfree(result
);
2184 return NT_STATUS_UNSUCCESSFUL
;
2187 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2188 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2189 "group filter %s\n", filter
));
2190 ldap_msgfree(result
);
2191 return NT_STATUS_NO_SUCH_GROUP
;
2194 ldap_msgfree(result
);
2195 return NT_STATUS_OK
;
2198 /**********************************************************************
2199 *********************************************************************/
2201 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2206 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%s))",
2208 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2209 sid_string_static(&sid
));
2211 return ldapsam_getgroup(methods
, filter
, map
);
2214 /**********************************************************************
2215 *********************************************************************/
2217 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2222 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%lu))",
2224 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2225 (unsigned long)gid
);
2227 return ldapsam_getgroup(methods
, filter
, map
);
2230 /**********************************************************************
2231 *********************************************************************/
2233 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2237 char *escape_name
= escape_ldap_string_alloc(name
);
2240 return NT_STATUS_NO_MEMORY
;
2243 pstr_sprintf(filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2245 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2246 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
), escape_name
);
2248 SAFE_FREE(escape_name
);
2250 return ldapsam_getgroup(methods
, filter
, map
);
2253 static void add_rid_to_array_unique(TALLOC_CTX
*mem_ctx
,
2254 uint32 rid
, uint32
**pp_rids
, size_t *p_num
)
2258 for (i
=0; i
<*p_num
; i
++) {
2259 if ((*pp_rids
)[i
] == rid
)
2263 *pp_rids
= TALLOC_REALLOC_ARRAY(mem_ctx
, *pp_rids
, uint32
, *p_num
+1);
2265 if (*pp_rids
== NULL
)
2268 (*pp_rids
)[*p_num
] = rid
;
2272 static BOOL
ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2274 const DOM_SID
*domain_sid
,
2280 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2281 str
, sizeof(str
)-1)) {
2282 DEBUG(10, ("Could not find sambaSID attribute\n"));
2286 if (!string_to_sid(&sid
, str
)) {
2287 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2291 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2292 DEBUG(10, ("SID %s is not in expected domain %s\n",
2293 str
, sid_string_static(domain_sid
)));
2297 if (!sid_peek_rid(&sid
, rid
)) {
2298 DEBUG(10, ("Could not peek into RID\n"));
2305 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2306 TALLOC_CTX
*mem_ctx
,
2307 const DOM_SID
*group
,
2308 uint32
**pp_member_rids
,
2309 size_t *p_num_members
)
2311 struct ldapsam_privates
*ldap_state
=
2312 (struct ldapsam_privates
*)methods
->private_data
;
2313 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2314 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2315 const char *sid_attrs
[] = { "sambaSID", NULL
};
2316 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2317 LDAPMessage
*result
= NULL
;
2320 char **values
= NULL
;
2325 *pp_member_rids
= NULL
;
2328 filter
= talloc_asprintf(mem_ctx
,
2329 "(&(objectClass=sambaGroupMapping)"
2330 "(objectClass=posixGroup)"
2332 sid_string_static(group
));
2334 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2335 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2338 if (rc
!= LDAP_SUCCESS
)
2341 talloc_autofree_ldapmsg(mem_ctx
, result
);
2343 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2346 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2347 sid_string_static(group
)));
2348 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2353 ret
= NT_STATUS_NO_SUCH_GROUP
;
2357 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2361 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2363 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2364 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2368 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2372 filter
= talloc_strdup(mem_ctx
, "(&(objectClass=sambaSamAccount)(|");
2373 if (filter
== NULL
) {
2374 ret
= NT_STATUS_NO_MEMORY
;
2378 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2379 filter
= talloc_asprintf_append(filter
, "(uid=%s)", *memberuid
);
2380 if (filter
== NULL
) {
2381 ret
= NT_STATUS_NO_MEMORY
;
2386 filter
= talloc_asprintf_append(filter
, "))");
2387 if (filter
== NULL
) {
2388 ret
= NT_STATUS_NO_MEMORY
;
2392 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2393 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2396 if (rc
!= LDAP_SUCCESS
)
2399 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2400 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2402 talloc_autofree_ldapmsg(mem_ctx
, result
);
2404 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2406 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2412 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2416 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2417 "the sambaSID attribute\n"));
2418 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2422 if (!string_to_sid(&sid
, sidstr
))
2425 if (!sid_check_is_in_our_domain(&sid
)) {
2426 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2427 "in our domain\n"));
2428 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2432 sid_peek_rid(&sid
, &rid
);
2434 add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2439 filter
= talloc_asprintf(mem_ctx
,
2440 "(&(objectClass=sambaSamAccount)"
2441 "(gidNumber=%s))", gidstr
);
2443 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2444 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2447 if (rc
!= LDAP_SUCCESS
)
2450 talloc_autofree_ldapmsg(mem_ctx
, result
);
2452 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2454 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2458 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2460 get_global_sam_sid(),
2462 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2463 "the sambaSID attribute\n"));
2464 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2468 add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2477 ldap_value_free(values
);
2482 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2483 TALLOC_CTX
*mem_ctx
,
2487 size_t *p_num_groups
)
2489 struct ldapsam_privates
*ldap_state
=
2490 (struct ldapsam_privates
*)methods
->private_data
;
2491 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2493 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2496 LDAPMessage
*msg
= NULL
;
2498 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2499 size_t num_sids
, num_gids
;
2505 if (!sid_to_gid(pdb_get_group_sid(user
), &primary_gid
)) {
2506 DEBUG(1, ("sid_to_gid failed for user's primary group\n"));
2507 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2510 escape_name
= escape_ldap_string_alloc(pdb_get_username(user
));
2512 if (escape_name
== NULL
)
2513 return NT_STATUS_NO_MEMORY
;
2515 pstr_sprintf(filter
, "(&(objectClass=posixGroup)"
2516 "(|(memberUid=%s)(gidNumber=%d)))",
2517 escape_name
, primary_gid
);
2519 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2520 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &msg
);
2522 if (rc
!= LDAP_SUCCESS
)
2531 /* We need to add the primary group as the first gid/sid */
2533 add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
);
2535 /* This sid will be replaced later */
2537 add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
, &num_sids
);
2539 for (entry
= ldap_first_entry(conn
->ldap_struct
, msg
);
2541 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2548 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2550 str
, sizeof(str
)-1))
2553 if (!string_to_sid(&sid
, str
))
2556 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2558 str
, sizeof(str
)-1))
2561 gid
= strtoul(str
, &end
, 10);
2563 if (PTR_DIFF(end
, str
) != strlen(str
))
2566 if (gid
== primary_gid
) {
2567 sid_copy(&(*pp_sids
)[0], &sid
);
2569 add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2571 add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2576 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2577 DEBUG(3, ("primary group of [%s] not found\n",
2578 pdb_get_username(user
)));
2582 *p_num_groups
= num_sids
;
2584 result
= NT_STATUS_OK
;
2588 SAFE_FREE(escape_name
);
2595 /**********************************************************************
2596 * Augment a posixGroup object with a sambaGroupMapping domgroup
2597 *********************************************************************/
2599 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2600 struct ldapsam_privates
*ldap_state
,
2603 const char *filter
, *dn
;
2604 LDAPMessage
*msg
, *entry
;
2608 filter
= talloc_asprintf(mem_ctx
,
2609 "(&(objectClass=posixGroup)(gidNumber=%u))",
2611 if (filter
== NULL
) {
2612 return NT_STATUS_NO_MEMORY
;
2615 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2616 get_attr_list(mem_ctx
, groupmap_attr_list
),
2618 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2620 if ((rc
!= LDAP_SUCCESS
) ||
2621 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2622 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2623 return NT_STATUS_NO_SUCH_GROUP
;
2626 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2628 return NT_STATUS_NO_MEMORY
;
2632 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
2633 "sambaGroupMapping");
2634 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
2635 sid_string_static(&map
->sid
));
2636 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
2637 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2638 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2640 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2642 talloc_autofree_ldapmod(mem_ctx
, mods
);
2644 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2645 if (rc
!= LDAP_SUCCESS
) {
2646 return NT_STATUS_ACCESS_DENIED
;
2649 return NT_STATUS_OK
;
2652 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
2655 struct ldapsam_privates
*ldap_state
=
2656 (struct ldapsam_privates
*)methods
->private_data
;
2657 LDAPMessage
*msg
= NULL
;
2658 LDAPMod
**mods
= NULL
;
2659 const char *attrs
[] = { NULL
};
2663 TALLOC_CTX
*mem_ctx
;
2670 mem_ctx
= talloc_new(NULL
);
2671 if (mem_ctx
== NULL
) {
2672 DEBUG(0, ("talloc_new failed\n"));
2673 return NT_STATUS_NO_MEMORY
;
2676 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
2677 sid_string_static(&map
->sid
));
2678 if (filter
== NULL
) {
2679 result
= NT_STATUS_NO_MEMORY
;
2683 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
2684 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
2685 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2687 if ((rc
== LDAP_SUCCESS
) &&
2688 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
2690 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
2691 "group mapping entry\n",
2692 sid_string_static(&map
->sid
)));
2693 result
= NT_STATUS_GROUP_EXISTS
;
2697 switch (map
->sid_name_use
) {
2699 case SID_NAME_DOM_GRP
:
2700 /* To map a domain group we need to have a posix group
2702 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
2706 case SID_NAME_ALIAS
:
2707 if (!sid_check_is_in_our_domain(&map
->sid
)) {
2708 DEBUG(3, ("Refusing to map sid %s as an alias, not "
2710 sid_string_static(&map
->sid
)));
2711 result
= NT_STATUS_INVALID_PARAMETER
;
2716 case SID_NAME_WKN_GRP
:
2717 if (!sid_check_is_in_builtin(&map
->sid
)) {
2718 DEBUG(3, ("Refusing to map sid %s as an alias, not "
2719 "in builtin domain\n",
2720 sid_string_static(&map
->sid
)));
2721 result
= NT_STATUS_INVALID_PARAMETER
;
2727 DEBUG(3, ("Got invalid use '%s' for mapping\n",
2728 sid_type_lookup(map
->sid_name_use
)));
2729 result
= NT_STATUS_INVALID_PARAMETER
;
2733 /* Domain groups have been mapped in a separate routine, we have to
2734 * create an alias now */
2736 if (map
->gid
== -1) {
2737 DEBUG(10, ("Refusing to map gid==-1\n"));
2738 result
= NT_STATUS_INVALID_PARAMETER
;
2742 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
2743 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
2744 "add\n", map
->gid
, sid_string_static(&sid
)));
2745 result
= NT_STATUS_GROUP_EXISTS
;
2749 /* Ok, enough checks done. It's still racy to go ahead now, but that's
2750 * the best we can get out of LDAP. */
2752 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
2753 sid_string_static(&map
->sid
),
2754 lp_ldap_group_suffix());
2756 result
= NT_STATUS_NO_MEMORY
;
2762 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
2764 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
2765 "sambaGroupMapping");
2767 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
2768 sid_string_static(&map
->sid
));
2769 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
2770 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2771 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
2773 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
2775 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
2776 talloc_asprintf(mem_ctx
, "%u", map
->gid
));
2777 talloc_autofree_ldapmod(mem_ctx
, mods
);
2779 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
2781 result
= (rc
== LDAP_SUCCESS
) ?
2782 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
2785 TALLOC_FREE(mem_ctx
);
2789 /**********************************************************************
2790 * Update a group mapping entry. We're quite strict about what can be changed:
2791 * Only the description and displayname may be changed. It simply does not
2792 * make any sense to change the SID, gid or the type in a mapping.
2793 *********************************************************************/
2795 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
2798 struct ldapsam_privates
*ldap_state
=
2799 (struct ldapsam_privates
*)methods
->private_data
;
2801 const char *filter
, *dn
;
2802 LDAPMessage
*msg
= NULL
;
2803 LDAPMessage
*entry
= NULL
;
2804 LDAPMod
**mods
= NULL
;
2805 TALLOC_CTX
*mem_ctx
;
2808 mem_ctx
= talloc_new(NULL
);
2809 if (mem_ctx
== NULL
) {
2810 DEBUG(0, ("talloc_new failed\n"));
2811 return NT_STATUS_NO_MEMORY
;
2814 /* Make 100% sure that sid, gid and type are not changed by looking up
2815 * exactly the values we're given in LDAP. */
2817 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=sambaGroupMapping)"
2818 "(sambaSid=%s)(gidNumber=%u)"
2819 "(sambaGroupType=%d))",
2820 sid_string_static(&map
->sid
), map
->gid
,
2822 if (filter
== NULL
) {
2823 result
= NT_STATUS_NO_MEMORY
;
2827 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2828 get_attr_list(mem_ctx
, groupmap_attr_list
),
2830 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2832 if ((rc
!= LDAP_SUCCESS
) ||
2833 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2834 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2835 result
= NT_STATUS_NO_SUCH_GROUP
;
2839 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2842 result
= NT_STATUS_NO_MEMORY
;
2847 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2849 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2851 talloc_autofree_ldapmod(mem_ctx
, mods
);
2854 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
2855 "nothing to do\n"));
2856 result
= NT_STATUS_OK
;
2860 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2862 if (rc
!= LDAP_SUCCESS
) {
2863 result
= NT_STATUS_ACCESS_DENIED
;
2867 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
2868 "group %lu in LDAP\n", (unsigned long)map
->gid
));
2870 result
= NT_STATUS_OK
;
2873 TALLOC_FREE(mem_ctx
);
2877 /**********************************************************************
2878 *********************************************************************/
2880 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
2883 struct ldapsam_privates
*priv
=
2884 (struct ldapsam_privates
*)methods
->private_data
;
2885 LDAPMessage
*msg
, *entry
;
2888 TALLOC_CTX
*mem_ctx
;
2891 mem_ctx
= talloc_new(NULL
);
2892 if (mem_ctx
== NULL
) {
2893 DEBUG(0, ("talloc_new failed\n"));
2894 return NT_STATUS_NO_MEMORY
;
2897 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
2898 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
2899 sid_string_static(&sid
));
2900 if (filter
== NULL
) {
2901 result
= NT_STATUS_NO_MEMORY
;
2904 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
2905 get_attr_list(mem_ctx
, groupmap_attr_list
),
2907 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2909 if ((rc
!= LDAP_SUCCESS
) ||
2910 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
2911 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
2912 result
= NT_STATUS_NO_SUCH_GROUP
;
2916 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
2917 get_attr_list(mem_ctx
,
2918 groupmap_attr_list_to_delete
));
2920 if ((rc
== LDAP_NAMING_VIOLATION
) ||
2921 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
2922 const char *attrs
[] = { "sambaGroupType", "description",
2923 "displayName", "sambaSIDList",
2926 /* Second try. Don't delete the sambaSID attribute, this is
2927 for "old" entries that are tacked on a winbind
2930 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
2931 LDAP_OBJ_GROUPMAP
, attrs
);
2934 if ((rc
== LDAP_NAMING_VIOLATION
) ||
2935 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
2936 const char *attrs
[] = { "sambaGroupType", "description",
2937 "displayName", "sambaSIDList",
2938 "gidNumber", NULL
};
2940 /* Third try. This is a post-3.0.21 alias (containing only
2941 * sambaSidEntry and sambaGroupMapping classes), we also have
2942 * to delete the gidNumber attribute, only the sambaSidEntry
2945 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
2946 LDAP_OBJ_GROUPMAP
, attrs
);
2949 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
2952 TALLOC_FREE(mem_ctx
);
2956 /**********************************************************************
2957 *********************************************************************/
2959 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
2962 struct ldapsam_privates
*ldap_state
=
2963 (struct ldapsam_privates
*)my_methods
->private_data
;
2966 const char **attr_list
;
2968 pstr_sprintf( filter
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
2969 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
2970 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
2971 LDAP_SCOPE_SUBTREE
, filter
,
2972 attr_list
, 0, &ldap_state
->result
);
2973 TALLOC_FREE(attr_list
);
2975 if (rc
!= LDAP_SUCCESS
) {
2976 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
2977 ldap_err2string(rc
)));
2978 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
2979 lp_ldap_group_suffix(), filter
));
2980 ldap_msgfree(ldap_state
->result
);
2981 ldap_state
->result
= NULL
;
2982 return NT_STATUS_UNSUCCESSFUL
;
2985 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
2986 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
2987 ldap_state
->result
)));
2990 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
2991 ldap_state
->result
);
2992 ldap_state
->index
= 0;
2994 return NT_STATUS_OK
;
2997 /**********************************************************************
2998 *********************************************************************/
3000 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3002 ldapsam_endsampwent(my_methods
);
3005 /**********************************************************************
3006 *********************************************************************/
3008 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3011 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3012 struct ldapsam_privates
*ldap_state
=
3013 (struct ldapsam_privates
*)my_methods
->private_data
;
3017 if (!ldap_state
->entry
)
3020 ldap_state
->index
++;
3021 bret
= init_group_from_ldap(ldap_state
, map
,
3025 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3029 return NT_STATUS_OK
;
3032 /**********************************************************************
3033 *********************************************************************/
3035 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3036 enum SID_NAME_USE sid_name_use
,
3037 GROUP_MAP
**pp_rmap
,
3038 size_t *p_num_entries
,
3048 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3049 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3051 return NT_STATUS_ACCESS_DENIED
;
3054 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3055 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3056 sid_name_use
!= map
.sid_name_use
) {
3057 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3058 "not of the requested type\n", map
.nt_name
));
3061 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3062 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3063 "non mapped\n", map
.nt_name
));
3067 mapt
=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3069 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3070 "enlarge group map!\n"));
3071 SAFE_FREE(*pp_rmap
);
3072 return NT_STATUS_UNSUCCESSFUL
;
3077 mapt
[entries
] = map
;
3082 ldapsam_endsamgrent(methods
);
3084 *p_num_entries
= entries
;
3086 return NT_STATUS_OK
;
3089 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3090 const DOM_SID
*alias
,
3091 const DOM_SID
*member
,
3094 struct ldapsam_privates
*ldap_state
=
3095 (struct ldapsam_privates
*)methods
->private_data
;
3097 LDAPMessage
*result
= NULL
;
3098 LDAPMessage
*entry
= NULL
;
3100 LDAPMod
**mods
= NULL
;
3102 enum SID_NAME_USE type
= SID_NAME_USE_NONE
;
3106 if (sid_check_is_in_builtin(alias
)) {
3107 type
= SID_NAME_WKN_GRP
;
3110 if (sid_check_is_in_our_domain(alias
)) {
3111 type
= SID_NAME_ALIAS
;
3114 if (type
== SID_NAME_USE_NONE
) {
3115 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3116 sid_string_static(alias
)));
3117 return NT_STATUS_NO_SUCH_ALIAS
;
3120 pstr_sprintf(filter
,
3121 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3122 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3125 if (ldapsam_search_one_group(ldap_state
, filter
,
3126 &result
) != LDAP_SUCCESS
)
3127 return NT_STATUS_NO_SUCH_ALIAS
;
3129 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3133 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3134 ldap_msgfree(result
);
3135 return NT_STATUS_NO_SUCH_ALIAS
;
3139 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3140 "filter %s: count=%d\n", filter
, count
));
3141 ldap_msgfree(result
);
3142 return NT_STATUS_NO_SUCH_ALIAS
;
3145 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3149 ldap_msgfree(result
);
3150 return NT_STATUS_UNSUCCESSFUL
;
3153 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
3155 ldap_msgfree(result
);
3156 return NT_STATUS_UNSUCCESSFUL
;
3159 smbldap_set_mod(&mods
, modop
,
3160 get_attr_key2string(groupmap_attr_list
,
3161 LDAP_ATTR_SID_LIST
),
3162 sid_string_static(member
));
3164 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3166 ldap_mods_free(mods
, True
);
3167 ldap_msgfree(result
);
3170 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3171 return NT_STATUS_MEMBER_IN_ALIAS
;
3174 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3175 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3178 if (rc
!= LDAP_SUCCESS
) {
3179 return NT_STATUS_UNSUCCESSFUL
;
3182 return NT_STATUS_OK
;
3185 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3186 const DOM_SID
*alias
,
3187 const DOM_SID
*member
)
3189 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3192 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3193 const DOM_SID
*alias
,
3194 const DOM_SID
*member
)
3196 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3200 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3201 const DOM_SID
*alias
,
3202 DOM_SID
**pp_members
,
3203 size_t *p_num_members
)
3205 struct ldapsam_privates
*ldap_state
=
3206 (struct ldapsam_privates
*)methods
->private_data
;
3207 LDAPMessage
*result
= NULL
;
3208 LDAPMessage
*entry
= NULL
;
3213 size_t num_members
= 0;
3214 enum SID_NAME_USE type
= SID_NAME_USE_NONE
;
3219 if (sid_check_is_in_builtin(alias
)) {
3220 type
= SID_NAME_WKN_GRP
;
3223 if (sid_check_is_in_our_domain(alias
)) {
3224 type
= SID_NAME_ALIAS
;
3227 if (type
== SID_NAME_USE_NONE
) {
3228 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3229 sid_string_static(alias
)));
3230 return NT_STATUS_NO_SUCH_ALIAS
;
3233 pstr_sprintf(filter
,
3234 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3235 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3238 if (ldapsam_search_one_group(ldap_state
, filter
,
3239 &result
) != LDAP_SUCCESS
)
3240 return NT_STATUS_NO_SUCH_ALIAS
;
3242 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3246 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3247 ldap_msgfree(result
);
3248 return NT_STATUS_NO_SUCH_ALIAS
;
3252 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3253 "filter %s: count=%d\n", filter
, count
));
3254 ldap_msgfree(result
);
3255 return NT_STATUS_NO_SUCH_ALIAS
;
3258 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3262 ldap_msgfree(result
);
3263 return NT_STATUS_UNSUCCESSFUL
;
3266 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3268 get_attr_key2string(groupmap_attr_list
,
3269 LDAP_ATTR_SID_LIST
));
3271 if (values
== NULL
) {
3272 ldap_msgfree(result
);
3273 return NT_STATUS_OK
;
3276 count
= ldap_count_values(values
);
3278 for (i
=0; i
<count
; i
++) {
3281 if (!string_to_sid(&member
, values
[i
]))
3284 add_sid_to_array(NULL
, &member
, pp_members
, &num_members
);
3287 *p_num_members
= num_members
;
3288 ldap_value_free(values
);
3289 ldap_msgfree(result
);
3291 return NT_STATUS_OK
;
3294 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3295 TALLOC_CTX
*mem_ctx
,
3296 const DOM_SID
*domain_sid
,
3297 const DOM_SID
*members
,
3299 uint32
**pp_alias_rids
,
3300 size_t *p_num_alias_rids
)
3302 struct ldapsam_privates
*ldap_state
=
3303 (struct ldapsam_privates
*)methods
->private_data
;
3306 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3308 LDAPMessage
*result
= NULL
;
3309 LDAPMessage
*entry
= NULL
;
3313 enum SID_NAME_USE type
= SID_NAME_USE_NONE
;
3315 if (sid_check_is_builtin(domain_sid
)) {
3316 type
= SID_NAME_WKN_GRP
;
3319 if (sid_check_is_domain(domain_sid
)) {
3320 type
= SID_NAME_ALIAS
;
3323 if (type
== SID_NAME_USE_NONE
) {
3324 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3325 sid_string_static(domain_sid
)));
3326 return NT_STATUS_UNSUCCESSFUL
;
3329 filter
= talloc_asprintf(mem_ctx
,
3330 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3331 LDAP_OBJ_GROUPMAP
, type
);
3333 for (i
=0; i
<num_members
; i
++)
3334 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3336 sid_string_static(&members
[i
]));
3338 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3340 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3341 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3343 if (rc
!= LDAP_SUCCESS
)
3344 return NT_STATUS_UNSUCCESSFUL
;
3346 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3348 for (entry
= ldap_first_entry(ldap_struct
, result
);
3350 entry
= ldap_next_entry(ldap_struct
, entry
))
3356 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3362 if (!string_to_sid(&sid
, sid_str
))
3365 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3368 add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3372 ldap_msgfree(result
);
3373 return NT_STATUS_OK
;
3376 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3380 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3382 LDAPMod
**mods
= NULL
;
3383 fstring value_string
;
3384 const char *policy_attr
= NULL
;
3386 struct ldapsam_privates
*ldap_state
=
3387 (struct ldapsam_privates
*)methods
->private_data
;
3389 const char *attrs
[2];
3391 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3393 if (!ldap_state
->domain_dn
) {
3394 return NT_STATUS_INVALID_PARAMETER
;
3397 policy_attr
= get_account_policy_attr(policy_index
);
3398 if (policy_attr
== NULL
) {
3399 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3404 attrs
[0] = policy_attr
;
3407 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3409 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3411 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3414 ldap_mods_free(mods
, True
);
3416 if (rc
!= LDAP_SUCCESS
) {
3420 if (!cache_account_policy_set(policy_index
, value
)) {
3421 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3422 "update local tdb cache\n"));
3426 return NT_STATUS_OK
;
3429 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3430 int policy_index
, uint32 value
)
3432 if (!account_policy_migrated(False
)) {
3433 return (account_policy_set(policy_index
, value
)) ?
3434 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3437 return ldapsam_set_account_policy_in_ldap(methods
, policy_index
,
3441 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3445 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3446 LDAPMessage
*result
= NULL
;
3447 LDAPMessage
*entry
= NULL
;
3451 const char *policy_attr
= NULL
;
3453 struct ldapsam_privates
*ldap_state
=
3454 (struct ldapsam_privates
*)methods
->private_data
;
3456 const char *attrs
[2];
3458 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3460 if (!ldap_state
->domain_dn
) {
3461 return NT_STATUS_INVALID_PARAMETER
;
3464 policy_attr
= get_account_policy_attr(policy_index
);
3466 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3467 "policy index: %d\n", policy_index
));
3471 attrs
[0] = policy_attr
;
3474 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3475 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0,
3478 if (rc
!= LDAP_SUCCESS
) {
3482 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3487 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3488 if (entry
== NULL
) {
3492 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3497 *value
= (uint32
)atol(vals
[0]);
3499 ntstatus
= NT_STATUS_OK
;
3503 ldap_value_free(vals
);
3504 ldap_msgfree(result
);
3509 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3511 - if user hasn't decided to use account policies inside LDAP just reuse the
3514 - if there is a valid cache entry, return that
3515 - if there is an LDAP entry, update cache and return
3516 - otherwise set to default, update cache and return
3520 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3521 int policy_index
, uint32
*value
)
3523 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3525 if (!account_policy_migrated(False
)) {
3526 return (account_policy_get(policy_index
, value
))
3527 ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3530 if (cache_account_policy_get(policy_index
, value
)) {
3531 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3533 return NT_STATUS_OK
;
3536 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, policy_index
,
3538 if (NT_STATUS_IS_OK(ntstatus
)) {
3542 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3546 /* should we automagically migrate old tdb value here ? */
3547 if (account_policy_get(policy_index
, value
))
3550 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3551 "default\n", policy_index
));
3554 if (!account_policy_get_default(policy_index
, value
)) {
3560 ntstatus
= ldapsam_set_account_policy(methods
, policy_index
, *value
);
3561 if (!NT_STATUS_IS_OK(ntstatus
)) {
3567 if (!cache_account_policy_set(policy_index
, *value
)) {
3568 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3569 "tdb as a cache\n"));
3570 return NT_STATUS_UNSUCCESSFUL
;
3573 return NT_STATUS_OK
;
3576 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3577 const DOM_SID
*domain_sid
,
3583 struct ldapsam_privates
*ldap_state
=
3584 (struct ldapsam_privates
*)methods
->private_data
;
3585 LDAPMessage
*msg
= NULL
;
3587 char *allsids
= NULL
;
3588 int i
, rc
, num_mapped
;
3589 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
3590 TALLOC_CTX
*mem_ctx
;
3594 mem_ctx
= talloc_new(NULL
);
3595 if (mem_ctx
== NULL
) {
3596 DEBUG(0, ("talloc_new failed\n"));
3600 if (!sid_check_is_builtin(domain_sid
) &&
3601 !sid_check_is_domain(domain_sid
)) {
3602 result
= NT_STATUS_INVALID_PARAMETER
;
3606 for (i
=0; i
<num_rids
; i
++)
3607 attrs
[i
] = SID_NAME_UNKNOWN
;
3609 allsids
= talloc_strdup(mem_ctx
, "");
3610 if (allsids
== NULL
) {
3614 for (i
=0; i
<num_rids
; i
++) {
3616 sid_copy(&sid
, domain_sid
);
3617 sid_append_rid(&sid
, rids
[i
]);
3618 allsids
= talloc_asprintf_append(allsids
, "(sambaSid=%s)",
3619 sid_string_static(&sid
));
3620 if (allsids
== NULL
) {
3625 /* First look for users */
3629 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
3631 filter
= talloc_asprintf(
3632 mem_ctx
, ("(&(objectClass=sambaSamAccount)(|%s))"),
3635 if (filter
== NULL
) {
3639 rc
= smbldap_search(ldap_state
->smbldap_state
,
3640 lp_ldap_user_suffix(),
3641 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
3643 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3646 if (rc
!= LDAP_SUCCESS
)
3649 ld
= ldap_state
->smbldap_state
->ldap_struct
;
3652 for (entry
= ldap_first_entry(ld
, msg
);
3654 entry
= ldap_next_entry(ld
, entry
)) {
3659 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
3661 DEBUG(2, ("Could not find sid from ldap entry\n"));
3665 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
3668 DEBUG(2, ("Could not retrieve uid attribute\n"));
3672 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
3673 if (rid
== rids
[rid_index
])
3677 if (rid_index
== num_rids
) {
3678 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
3682 attrs
[rid_index
] = SID_NAME_USER
;
3683 names
[rid_index
] = name
;
3687 if (num_mapped
== num_rids
) {
3688 /* No need to look for groups anymore -- we're done */
3689 result
= NT_STATUS_OK
;
3693 /* Same game for groups */
3697 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
3698 "sambaGroupType", NULL
};
3700 filter
= talloc_asprintf(
3701 mem_ctx
, "(&(objectClass=sambaGroupMapping)(|%s))",
3703 if (filter
== NULL
) {
3707 rc
= smbldap_search(ldap_state
->smbldap_state
,
3708 lp_ldap_group_suffix(),
3709 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
3711 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3714 if (rc
!= LDAP_SUCCESS
)
3717 /* ldap_struct might have changed due to a reconnect */
3719 ld
= ldap_state
->smbldap_state
->ldap_struct
;
3721 /* For consistency checks, we already checked we're only domain or builtin */
3723 is_builtin
= sid_check_is_builtin(domain_sid
);
3725 for (entry
= ldap_first_entry(ld
, msg
);
3727 entry
= ldap_next_entry(ld
, entry
))
3732 enum SID_NAME_USE type
;
3733 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
3735 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
3738 DEBUG(2, ("Could not extract type from ldap entry %s\n",
3745 /* Consistency checks */
3746 if ((is_builtin
&& (type
!= SID_NAME_WKN_GRP
)) ||
3747 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
3748 (type
!= SID_NAME_DOM_GRP
)))) {
3749 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
3752 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
3754 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
3758 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
3761 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
3763 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
3767 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
3772 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
3773 if (rid
== rids
[rid_index
])
3777 if (rid_index
== num_rids
) {
3778 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
3782 attrs
[rid_index
] = type
;
3783 names
[rid_index
] = attr
;
3787 result
= NT_STATUS_NONE_MAPPED
;
3790 result
= (num_mapped
== num_rids
) ?
3791 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
3793 TALLOC_FREE(mem_ctx
);
3797 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
3799 char *filter
= NULL
;
3800 char *escaped
= NULL
;
3801 char *result
= NULL
;
3803 asprintf(&filter
, "(&%s(objectclass=sambaSamAccount))",
3805 if (filter
== NULL
) goto done
;
3807 escaped
= escape_ldap_string_alloc(username
);
3808 if (escaped
== NULL
) goto done
;
3810 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
3819 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
3823 const char **result
;
3825 va_start(ap
, mem_ctx
);
3826 while (va_arg(ap
, const char *) != NULL
)
3830 result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1);
3832 va_start(ap
, mem_ctx
);
3833 for (i
=0; i
<num
; i
++)
3834 result
[i
] = talloc_strdup(mem_ctx
, va_arg(ap
, const char*));
3841 struct ldap_search_state
{
3842 struct smbldap_state
*connection
;
3852 void *pagedresults_cookie
;
3854 LDAPMessage
*entries
, *current_entry
;
3855 BOOL (*ldap2displayentry
)(struct ldap_search_state
*state
,
3856 TALLOC_CTX
*mem_ctx
,
3857 LDAP
*ld
, LDAPMessage
*entry
,
3858 struct samr_displayentry
*result
);
3861 static BOOL
ldapsam_search_firstpage(struct pdb_search
*search
)
3863 struct ldap_search_state
*state
= search
->private_data
;
3865 int rc
= LDAP_OPERATIONS_ERROR
;
3867 state
->entries
= NULL
;
3869 if (state
->connection
->paged_results
) {
3870 rc
= smbldap_search_paged(state
->connection
, state
->base
,
3871 state
->scope
, state
->filter
,
3872 state
->attrs
, state
->attrsonly
,
3873 lp_ldap_page_size(), &state
->entries
,
3874 &state
->pagedresults_cookie
);
3877 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
3879 if (state
->entries
!= NULL
) {
3880 /* Left over from unsuccessful paged attempt */
3881 ldap_msgfree(state
->entries
);
3882 state
->entries
= NULL
;
3885 rc
= smbldap_search(state
->connection
, state
->base
,
3886 state
->scope
, state
->filter
, state
->attrs
,
3887 state
->attrsonly
, &state
->entries
);
3889 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
3892 /* Ok, the server was lying. It told us it could do paged
3893 * searches when it could not. */
3894 state
->connection
->paged_results
= False
;
3897 ld
= state
->connection
->ldap_struct
;
3899 DEBUG(5, ("Don't have an LDAP connection right after a "
3903 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
3905 if (state
->current_entry
== NULL
) {
3906 ldap_msgfree(state
->entries
);
3907 state
->entries
= NULL
;
3913 static BOOL
ldapsam_search_nextpage(struct pdb_search
*search
)
3915 struct ldap_search_state
*state
= search
->private_data
;
3918 if (!state
->connection
->paged_results
) {
3919 /* There is no next page when there are no paged results */
3923 rc
= smbldap_search_paged(state
->connection
, state
->base
,
3924 state
->scope
, state
->filter
, state
->attrs
,
3925 state
->attrsonly
, lp_ldap_page_size(),
3927 &state
->pagedresults_cookie
);
3929 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
3932 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
3934 if (state
->current_entry
== NULL
) {
3935 ldap_msgfree(state
->entries
);
3936 state
->entries
= NULL
;
3942 static BOOL
ldapsam_search_next_entry(struct pdb_search
*search
,
3943 struct samr_displayentry
*entry
)
3945 struct ldap_search_state
*state
= search
->private_data
;
3949 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
3952 if ((state
->entries
== NULL
) &&
3953 !ldapsam_search_nextpage(search
))
3956 result
= state
->ldap2displayentry(state
, search
->mem_ctx
, state
->connection
->ldap_struct
,
3957 state
->current_entry
, entry
);
3961 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
3962 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
3963 if (dn
!= NULL
) ldap_memfree(dn
);
3966 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
3968 if (state
->current_entry
== NULL
) {
3969 ldap_msgfree(state
->entries
);
3970 state
->entries
= NULL
;
3973 if (!result
) goto retry
;
3978 static void ldapsam_search_end(struct pdb_search
*search
)
3980 struct ldap_search_state
*state
= search
->private_data
;
3983 if (state
->pagedresults_cookie
== NULL
)
3986 if (state
->entries
!= NULL
)
3987 ldap_msgfree(state
->entries
);
3989 state
->entries
= NULL
;
3990 state
->current_entry
= NULL
;
3992 if (!state
->connection
->paged_results
)
3995 /* Tell the LDAP server we're not interested in the rest anymore. */
3997 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
3998 state
->filter
, state
->attrs
,
3999 state
->attrsonly
, 0, &state
->entries
,
4000 &state
->pagedresults_cookie
);
4002 if (rc
!= LDAP_SUCCESS
)
4003 DEBUG(5, ("Could not end search properly\n"));
4008 static BOOL
ldapuser2displayentry(struct ldap_search_state
*state
,
4009 TALLOC_CTX
*mem_ctx
,
4010 LDAP
*ld
, LDAPMessage
*entry
,
4011 struct samr_displayentry
*result
)
4017 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4018 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4019 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4022 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4023 ldap_value_free(vals
);
4025 if ((state
->acct_flags
!= 0) &&
4026 ((state
->acct_flags
& acct_flags
) == 0))
4029 result
->acct_flags
= acct_flags
;
4030 result
->account_name
= "";
4031 result
->fullname
= "";
4032 result
->description
= "";
4034 vals
= ldap_get_values(ld
, entry
, "uid");
4035 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4036 DEBUG(5, ("\"uid\" not found\n"));
4039 pull_utf8_talloc(mem_ctx
,
4040 CONST_DISCARD(char **, &result
->account_name
),
4042 ldap_value_free(vals
);
4044 vals
= ldap_get_values(ld
, entry
, "displayName");
4045 if ((vals
== NULL
) || (vals
[0] == NULL
))
4046 DEBUG(8, ("\"displayName\" not found\n"));
4048 pull_utf8_talloc(mem_ctx
,
4049 CONST_DISCARD(char **, &result
->fullname
),
4051 ldap_value_free(vals
);
4053 vals
= ldap_get_values(ld
, entry
, "description");
4054 if ((vals
== NULL
) || (vals
[0] == NULL
))
4055 DEBUG(8, ("\"description\" not found\n"));
4057 pull_utf8_talloc(mem_ctx
,
4058 CONST_DISCARD(char **, &result
->description
),
4060 ldap_value_free(vals
);
4062 if ((result
->account_name
== NULL
) ||
4063 (result
->fullname
== NULL
) ||
4064 (result
->description
== NULL
)) {
4065 DEBUG(0, ("talloc failed\n"));
4069 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4070 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4071 DEBUG(0, ("\"objectSid\" not found\n"));
4075 if (!string_to_sid(&sid
, vals
[0])) {
4076 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4077 ldap_value_free(vals
);
4080 ldap_value_free(vals
);
4082 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4083 DEBUG(0, ("sid %s does not belong to our domain\n",
4084 sid_string_static(&sid
)));
4092 static BOOL
ldapsam_search_users(struct pdb_methods
*methods
,
4093 struct pdb_search
*search
,
4096 struct ldapsam_privates
*ldap_state
= methods
->private_data
;
4097 struct ldap_search_state
*state
;
4099 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4100 if (state
== NULL
) {
4101 DEBUG(0, ("talloc failed\n"));
4105 state
->connection
= ldap_state
->smbldap_state
;
4107 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4108 state
->base
= lp_ldap_user_suffix();
4109 else if ((acct_flags
!= 0) &&
4110 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4111 state
->base
= lp_ldap_machine_suffix();
4113 state
->base
= lp_ldap_suffix();
4115 state
->acct_flags
= acct_flags
;
4116 state
->base
= talloc_strdup(search
->mem_ctx
, state
->base
);
4117 state
->scope
= LDAP_SCOPE_SUBTREE
;
4118 state
->filter
= get_ldap_filter(search
->mem_ctx
, "*");
4119 state
->attrs
= talloc_attrs(search
->mem_ctx
, "uid", "sambaSid",
4120 "displayName", "description",
4121 "sambaAcctFlags", NULL
);
4122 state
->attrsonly
= 0;
4123 state
->pagedresults_cookie
= NULL
;
4124 state
->entries
= NULL
;
4125 state
->ldap2displayentry
= ldapuser2displayentry
;
4127 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4128 DEBUG(0, ("talloc failed\n"));
4132 search
->private_data
= state
;
4133 search
->next_entry
= ldapsam_search_next_entry
;
4134 search
->search_end
= ldapsam_search_end
;
4136 return ldapsam_search_firstpage(search
);
4139 static BOOL
ldapgroup2displayentry(struct ldap_search_state
*state
,
4140 TALLOC_CTX
*mem_ctx
,
4141 LDAP
*ld
, LDAPMessage
*entry
,
4142 struct samr_displayentry
*result
)
4148 result
->account_name
= "";
4149 result
->fullname
= "";
4150 result
->description
= "";
4153 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4154 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4155 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4157 ldap_value_free(vals
);
4162 group_type
= atoi(vals
[0]);
4164 if ((state
->group_type
!= 0) &&
4165 ((state
->group_type
!= group_type
))) {
4166 ldap_value_free(vals
);
4170 ldap_value_free(vals
);
4172 /* display name is the NT group name */
4174 vals
= ldap_get_values(ld
, entry
, "displayName");
4175 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4176 DEBUG(8, ("\"displayName\" not found\n"));
4178 /* fallback to the 'cn' attribute */
4179 vals
= ldap_get_values(ld
, entry
, "cn");
4180 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4181 DEBUG(5, ("\"cn\" not found\n"));
4184 pull_utf8_talloc(mem_ctx
,
4185 CONST_DISCARD(char **, &result
->account_name
),
4189 pull_utf8_talloc(mem_ctx
,
4190 CONST_DISCARD(char **, &result
->account_name
),
4194 ldap_value_free(vals
);
4196 vals
= ldap_get_values(ld
, entry
, "description");
4197 if ((vals
== NULL
) || (vals
[0] == NULL
))
4198 DEBUG(8, ("\"description\" not found\n"));
4200 pull_utf8_talloc(mem_ctx
,
4201 CONST_DISCARD(char **, &result
->description
),
4203 ldap_value_free(vals
);
4205 if ((result
->account_name
== NULL
) ||
4206 (result
->fullname
== NULL
) ||
4207 (result
->description
== NULL
)) {
4208 DEBUG(0, ("talloc failed\n"));
4212 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4213 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4214 DEBUG(0, ("\"objectSid\" not found\n"));
4216 ldap_value_free(vals
);
4221 if (!string_to_sid(&sid
, vals
[0])) {
4222 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4226 ldap_value_free(vals
);
4228 switch (group_type
) {
4229 case SID_NAME_DOM_GRP
:
4230 case SID_NAME_ALIAS
:
4232 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
,
4234 DEBUG(0, ("%s is not in our domain\n",
4235 sid_string_static(&sid
)));
4240 case SID_NAME_WKN_GRP
:
4242 if (!sid_peek_check_rid(&global_sid_Builtin
, &sid
,
4245 DEBUG(0, ("%s is not in builtin sid\n",
4246 sid_string_static(&sid
)));
4252 DEBUG(0,("unkown group type: %d\n", group_type
));
4259 static BOOL
ldapsam_search_grouptype(struct pdb_methods
*methods
,
4260 struct pdb_search
*search
,
4261 enum SID_NAME_USE type
)
4263 struct ldapsam_privates
*ldap_state
= methods
->private_data
;
4264 struct ldap_search_state
*state
;
4266 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4267 if (state
== NULL
) {
4268 DEBUG(0, ("talloc failed\n"));
4272 state
->connection
= ldap_state
->smbldap_state
;
4274 state
->base
= talloc_strdup(search
->mem_ctx
, lp_ldap_group_suffix());
4275 state
->connection
= ldap_state
->smbldap_state
;
4276 state
->scope
= LDAP_SCOPE_SUBTREE
;
4277 state
->filter
= talloc_asprintf(search
->mem_ctx
,
4278 "(&(objectclass=sambaGroupMapping)"
4279 "(sambaGroupType=%d))", type
);
4280 state
->attrs
= talloc_attrs(search
->mem_ctx
, "cn", "sambaSid",
4281 "displayName", "description",
4282 "sambaGroupType", NULL
);
4283 state
->attrsonly
= 0;
4284 state
->pagedresults_cookie
= NULL
;
4285 state
->entries
= NULL
;
4286 state
->group_type
= type
;
4287 state
->ldap2displayentry
= ldapgroup2displayentry
;
4289 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4290 DEBUG(0, ("talloc failed\n"));
4294 search
->private_data
= state
;
4295 search
->next_entry
= ldapsam_search_next_entry
;
4296 search
->search_end
= ldapsam_search_end
;
4298 return ldapsam_search_firstpage(search
);
4301 static BOOL
ldapsam_search_groups(struct pdb_methods
*methods
,
4302 struct pdb_search
*search
)
4304 return ldapsam_search_grouptype(methods
, search
, SID_NAME_DOM_GRP
);
4307 static BOOL
ldapsam_search_aliases(struct pdb_methods
*methods
,
4308 struct pdb_search
*search
,
4311 if (sid_check_is_domain(sid
))
4312 return ldapsam_search_grouptype(methods
, search
,
4315 if (sid_check_is_builtin(sid
))
4316 return ldapsam_search_grouptype(methods
, search
,
4319 DEBUG(5, ("Don't know SID %s\n", sid_string_static(sid
)));
4323 static BOOL
ldapsam_rid_algorithm(struct pdb_methods
*methods
)
4328 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4331 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4333 LDAPMessage
*result
= NULL
;
4334 LDAPMessage
*entry
= NULL
;
4335 LDAPMod
**mods
= NULL
;
4341 TALLOC_CTX
*mem_ctx
;
4343 mem_ctx
= talloc_new(NULL
);
4344 if (mem_ctx
== NULL
) {
4345 DEBUG(0, ("talloc_new failed\n"));
4346 return NT_STATUS_NO_MEMORY
;
4349 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4350 get_global_sam_name(), False
);
4351 if (!NT_STATUS_IS_OK(status
)) {
4352 DEBUG(3, ("Could not get domain info: %s\n",
4353 nt_errstr(status
)));
4357 talloc_autofree_ldapmsg(mem_ctx
, result
);
4359 entry
= ldap_first_entry(priv2ld(priv
), result
);
4360 if (entry
== NULL
) {
4361 DEBUG(0, ("Could not get domain info entry\n"));
4362 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4366 /* Find the largest of the three attributes "sambaNextRid",
4367 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4368 concept of differentiating between user and group rids, and will
4369 use only "sambaNextRid" in the future. But for compatibility
4370 reasons I look if others have chosen different strategies -- VL */
4372 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4373 "sambaNextRid", mem_ctx
);
4374 if (value
!= NULL
) {
4375 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4376 nextRid
= MAX(nextRid
, tmp
);
4379 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4380 "sambaNextUserRid", mem_ctx
);
4381 if (value
!= NULL
) {
4382 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4383 nextRid
= MAX(nextRid
, tmp
);
4386 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4387 "sambaNextGroupRid", mem_ctx
);
4388 if (value
!= NULL
) {
4389 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4390 nextRid
= MAX(nextRid
, tmp
);
4394 nextRid
= BASE_RID
-1;
4399 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4400 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4401 talloc_autofree_ldapmod(mem_ctx
, mods
);
4403 rc
= smbldap_modify(smbldap_state
,
4404 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
),
4407 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4410 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4413 if (NT_STATUS_IS_OK(status
)) {
4417 TALLOC_FREE(mem_ctx
);
4421 static BOOL
ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
4425 for (i
=0; i
<10; i
++) {
4426 NTSTATUS result
= ldapsam_get_new_rid(methods
->private_data
,
4428 if (NT_STATUS_IS_OK(result
)) {
4432 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4436 /* The ldap update failed (maybe a race condition), retry */
4439 /* Tried 10 times, fail. */
4443 static BOOL
ldapsam_sid_to_id(struct pdb_methods
*methods
,
4445 union unid_t
*id
, enum SID_NAME_USE
*type
)
4447 struct ldapsam_privates
*priv
= methods
->private_data
;
4449 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4451 LDAPMessage
*result
= NULL
;
4452 LDAPMessage
*entry
= NULL
;
4457 TALLOC_CTX
*mem_ctx
;
4459 mem_ctx
= talloc_new(NULL
);
4460 if (mem_ctx
== NULL
) {
4461 DEBUG(0, ("talloc_new failed\n"));
4465 filter
= talloc_asprintf(mem_ctx
,
4467 "(|(objectClass=sambaGroupMapping)"
4468 "(objectClass=sambaSamAccount)))",
4469 sid_string_static(sid
));
4470 if (filter
== NULL
) {
4471 DEBUG(5, ("talloc_asprintf failed\n"));
4475 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4477 if (rc
!= LDAP_SUCCESS
) {
4480 talloc_autofree_ldapmsg(mem_ctx
, result
);
4482 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4483 DEBUG(10, ("Got %d entries, expected one\n",
4484 ldap_count_entries(priv2ld(priv
), result
)));
4488 entry
= ldap_first_entry(priv2ld(priv
), result
);
4490 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4491 "sambaGroupType", mem_ctx
);
4493 if (value
!= NULL
) {
4494 const char *gid_str
;
4497 gid_str
= smbldap_talloc_single_attribute(
4498 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4499 if (gid_str
== NULL
) {
4500 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4501 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4506 id
->gid
= strtoul(gid_str
, NULL
, 10);
4507 *type
= strtoul(value
, NULL
, 10);
4512 /* It must be a user */
4514 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4515 "uidNumber", mem_ctx
);
4516 if (value
== NULL
) {
4517 DEBUG(1, ("Could not find uidNumber in %s\n",
4518 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4522 id
->uid
= strtoul(value
, NULL
, 10);
4523 *type
= SID_NAME_USER
;
4527 TALLOC_FREE(mem_ctx
);
4532 * The following functions is called only if
4533 * ldapsam:trusted and ldapsam:editposix are
4538 * ldapsam_create_user creates a new
4539 * posixAccount and sambaSamAccount object
4540 * in the ldap users subtree
4542 * The uid is allocated by winbindd.
4545 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
4546 TALLOC_CTX
*tmp_ctx
, const char *name
,
4547 uint32 acb_info
, uint32
*rid
)
4549 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4550 LDAPMessage
*entry
= NULL
;
4551 LDAPMessage
*result
= NULL
;
4553 BOOL is_machine
= False
;
4554 BOOL add_posix
= False
;
4555 LDAPMod
**mods
= NULL
;
4563 const char *dn
= NULL
;
4571 if ((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') {
4575 username
= escape_ldap_string_alloc(name
);
4576 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=posixAccount))", username
);
4577 SAFE_FREE(username
);
4579 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4580 if (rc
!= LDAP_SUCCESS
) {
4581 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
4582 return NT_STATUS_UNSUCCESSFUL
;
4584 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4586 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4588 if (num_result
> 1) {
4589 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
4590 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4593 if (num_result
== 1) {
4595 /* check if it is just a posix account.
4596 * or if there is a sid attached to this entry
4599 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4601 return NT_STATUS_UNSUCCESSFUL
;
4604 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
4606 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
4607 return NT_STATUS_USER_EXISTS
;
4610 /* it is just a posix account, retrieve the dn for later use */
4611 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4613 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
4614 return NT_STATUS_NO_MEMORY
;
4618 if (num_result
== 0) {
4622 /* Create the basic samu structure and generate the mods for the ldap commit */
4623 if (!NT_STATUS_IS_OK((ret
= ldapsam_get_new_rid(ldap_state
, rid
)))) {
4624 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
4628 sid_copy(&user_sid
, get_global_sam_sid());
4629 sid_append_rid(&user_sid
, *rid
);
4631 user
= samu_new(tmp_ctx
);
4633 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
4634 return NT_STATUS_NO_MEMORY
;
4637 if (!pdb_set_username(user
, name
, PDB_SET
)) {
4638 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4639 return NT_STATUS_UNSUCCESSFUL
;
4641 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
4642 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4643 return NT_STATUS_UNSUCCESSFUL
;
4646 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
| ACB_DISABLED
, PDB_SET
)) {
4647 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4648 return NT_STATUS_UNSUCCESSFUL
;
4651 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
4652 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4653 return NT_STATUS_UNSUCCESSFUL
;
4657 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
4658 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4659 return NT_STATUS_UNSUCCESSFUL
;
4662 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
4663 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4664 return NT_STATUS_UNSUCCESSFUL
;
4667 switch ( ldap_state
->schema_ver
) {
4668 case SCHEMAVER_SAMBAACCOUNT
:
4669 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBAACCOUNT
);
4671 case SCHEMAVER_SAMBASAMACCOUNT
:
4672 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
4675 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
4680 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
4682 /* retrieve the Domain Users group gid */
4683 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
4684 !sid_to_gid(&group_sid
, &gid
)) {
4685 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
4686 return NT_STATUS_INVALID_PRIMARY_GROUP
;
4689 /* lets allocate a new userid for this user */
4690 if (!winbind_allocate_uid(&uid
)) {
4691 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
4692 return NT_STATUS_UNSUCCESSFUL
;
4697 /* TODO: choose a more appropriate default for machines */
4698 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
4699 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
4701 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
4702 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
4704 uidstr
= talloc_asprintf(tmp_ctx
, "%d", uid
);
4705 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
4707 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", name
, lp_ldap_machine_suffix ());
4709 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", name
, lp_ldap_user_suffix ());
4712 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
4713 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
4714 return NT_STATUS_NO_MEMORY
;
4717 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
4718 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
4719 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
4720 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
4721 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
4722 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
4723 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
4726 talloc_autofree_ldapmod(tmp_ctx
, mods
);
4729 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
4731 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
4734 if (rc
!= LDAP_SUCCESS
) {
4735 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
4736 return NT_STATUS_UNSUCCESSFUL
;
4739 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
4741 flush_pwnam_cache();
4743 return NT_STATUS_OK
;
4746 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
4748 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4749 LDAPMessage
*result
= NULL
;
4750 LDAPMessage
*entry
= NULL
;
4756 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
4758 filter
= talloc_asprintf(tmp_ctx
,
4760 "(objectClass=posixAccount)"
4761 "(objectClass=sambaSamAccount))",
4762 pdb_get_username(sam_acct
));
4764 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4765 if (rc
!= LDAP_SUCCESS
) {
4766 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
4767 return NT_STATUS_UNSUCCESSFUL
;
4769 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4771 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4773 if (num_result
== 0) {
4774 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
4775 return NT_STATUS_NO_SUCH_USER
;
4778 if (num_result
> 1) {
4779 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
4780 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4783 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4785 return NT_STATUS_UNSUCCESSFUL
;
4788 /* it is just a posix account, retrieve the dn for later use */
4789 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4791 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
4792 return NT_STATUS_NO_MEMORY
;
4795 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
4796 if (rc
!= LDAP_SUCCESS
) {
4797 return NT_STATUS_UNSUCCESSFUL
;
4800 flush_pwnam_cache();
4802 return NT_STATUS_OK
;
4806 * ldapsam_create_group creates a new
4807 * posixGroup and sambaGroupMapping object
4808 * in the ldap groups subtree
4810 * The gid is allocated by winbindd.
4813 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
4814 TALLOC_CTX
*tmp_ctx
,
4818 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4820 LDAPMessage
*entry
= NULL
;
4821 LDAPMessage
*result
= NULL
;
4823 BOOL is_new_entry
= False
;
4824 LDAPMod
**mods
= NULL
;
4830 const char *dn
= NULL
;
4835 groupname
= escape_ldap_string_alloc(name
);
4836 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=posixGroup))", groupname
);
4837 SAFE_FREE(groupname
);
4839 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4840 if (rc
!= LDAP_SUCCESS
) {
4841 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
4842 return NT_STATUS_UNSUCCESSFUL
;
4844 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4846 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4848 if (num_result
> 1) {
4849 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
4850 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4853 if (num_result
== 1) {
4855 /* check if it is just a posix group.
4856 * or if there is a sid attached to this entry
4859 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4861 return NT_STATUS_UNSUCCESSFUL
;
4864 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
4866 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
4867 return NT_STATUS_GROUP_EXISTS
;
4870 /* it is just a posix group, retrieve the gid and the dn for later use */
4871 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
4873 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
4874 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4877 gid
= strtoul(tmp
, NULL
, 10);
4879 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
4881 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
4882 return NT_STATUS_NO_MEMORY
;
4886 if (num_result
== 0) {
4887 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
4889 is_new_entry
= True
;
4891 /* lets allocate a new groupid for this group */
4892 if (!winbind_allocate_gid(&gid
)) {
4893 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
4894 return NT_STATUS_UNSUCCESSFUL
;
4897 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
4898 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", name
, lp_ldap_group_suffix());
4900 if (!gidstr
|| !dn
) {
4901 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
4902 return NT_STATUS_NO_MEMORY
;
4905 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
4906 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
4907 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
4910 if (!NT_STATUS_IS_OK((ret
= ldapsam_get_new_rid(ldap_state
, rid
)))) {
4911 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
4915 sid_copy(&group_sid
, get_global_sam_sid());
4916 sid_append_rid(&group_sid
, *rid
);
4918 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_static(&group_sid
));
4919 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
4921 if (!groupsidstr
|| !grouptype
) {
4922 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
4923 return NT_STATUS_NO_MEMORY
;
4926 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "sambaGroupMapping");
4927 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
4928 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
4929 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
4930 talloc_autofree_ldapmod(tmp_ctx
, mods
);
4933 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
4935 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
4936 /* This call may fail with rfc2307bis schema */
4937 /* Retry adding a structural class */
4938 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
4939 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
4943 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
4946 if (rc
!= LDAP_SUCCESS
) {
4947 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
4948 return NT_STATUS_UNSUCCESSFUL
;
4951 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
4953 return NT_STATUS_OK
;
4956 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
4958 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4959 LDAPMessage
*result
= NULL
;
4960 LDAPMessage
*entry
= NULL
;
4968 /* get the group sid */
4969 sid_copy(&group_sid
, get_global_sam_sid());
4970 sid_append_rid(&group_sid
, rid
);
4972 filter
= talloc_asprintf(tmp_ctx
,
4974 "(objectClass=posixGroup)"
4975 "(objectClass=sambaGroupMapping))",
4976 sid_string_static(&group_sid
));
4978 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
4979 if (rc
!= LDAP_SUCCESS
) {
4980 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
4981 return NT_STATUS_UNSUCCESSFUL
;
4983 talloc_autofree_ldapmsg(tmp_ctx
, result
);
4985 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
4987 if (num_result
== 0) {
4988 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
4989 return NT_STATUS_NO_SUCH_GROUP
;
4992 if (num_result
> 1) {
4993 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
4994 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
4997 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4999 return NT_STATUS_UNSUCCESSFUL
;
5002 /* here it is, retrieve the dn for later use */
5003 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5005 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5006 return NT_STATUS_NO_MEMORY
;
5009 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5011 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5012 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5015 /* check no user have this group marked as primary group */
5016 filter
= talloc_asprintf(tmp_ctx
,
5018 "(objectClass=posixAccount)"
5019 "(objectClass=sambaSamAccount))",
5022 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5023 if (rc
!= LDAP_SUCCESS
) {
5024 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5025 return NT_STATUS_UNSUCCESSFUL
;
5027 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5029 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5031 if (num_result
!= 0) {
5032 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5033 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5036 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5037 if (rc
!= LDAP_SUCCESS
) {
5038 return NT_STATUS_UNSUCCESSFUL
;
5041 return NT_STATUS_OK
;
5044 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5045 TALLOC_CTX
*tmp_ctx
,
5050 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5051 LDAPMessage
*entry
= NULL
;
5052 LDAPMessage
*result
= NULL
;
5054 LDAPMod
**mods
= NULL
;
5057 const char *dn
= NULL
;
5064 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5066 case LDAP_MOD_DELETE
:
5067 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5070 return NT_STATUS_UNSUCCESSFUL
;
5073 /* get member sid */
5074 sid_copy(&member_sid
, get_global_sam_sid());
5075 sid_append_rid(&member_sid
, member_rid
);
5077 /* get the group sid */
5078 sid_copy(&group_sid
, get_global_sam_sid());
5079 sid_append_rid(&group_sid
, group_rid
);
5081 filter
= talloc_asprintf(tmp_ctx
,
5083 "(objectClass=posixAccount)"
5084 "(objectClass=sambaSamAccount))",
5085 sid_string_static(&member_sid
));
5087 /* get the member uid */
5088 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5089 if (rc
!= LDAP_SUCCESS
) {
5090 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5091 return NT_STATUS_UNSUCCESSFUL
;
5093 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5095 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5097 if (num_result
== 0) {
5098 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5099 return NT_STATUS_NO_SUCH_MEMBER
;
5102 if (num_result
> 1) {
5103 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5104 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5107 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5109 return NT_STATUS_UNSUCCESSFUL
;
5112 if (modop
== LDAP_MOD_DELETE
) {
5113 /* check if we are trying to remove the member from his primary group */
5115 gid_t user_gid
, group_gid
;
5117 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5119 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5120 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5123 user_gid
= strtoul(gidstr
, NULL
, 10);
5125 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5126 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5127 return NT_STATUS_UNSUCCESSFUL
;
5130 if (user_gid
== group_gid
) {
5131 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from it's own primary group!\n"));
5132 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5136 /* here it is, retrieve the uid for later use */
5137 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5139 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5140 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5143 filter
= talloc_asprintf(tmp_ctx
,
5145 "(objectClass=posixGroup)"
5146 "(objectClass=sambaGroupMapping))",
5147 sid_string_static(&group_sid
));
5150 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5151 if (rc
!= LDAP_SUCCESS
) {
5152 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5153 return NT_STATUS_UNSUCCESSFUL
;
5155 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5157 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5159 if (num_result
== 0) {
5160 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5161 return NT_STATUS_NO_SUCH_GROUP
;
5164 if (num_result
> 1) {
5165 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5166 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5169 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5171 return NT_STATUS_UNSUCCESSFUL
;
5174 /* here it is, retrieve the dn for later use */
5175 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5177 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5178 return NT_STATUS_NO_MEMORY
;
5181 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5183 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5185 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5186 if (rc
!= LDAP_SUCCESS
) {
5187 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5188 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5189 return NT_STATUS_MEMBER_IN_GROUP
;
5191 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5192 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5193 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5195 return NT_STATUS_UNSUCCESSFUL
;
5198 return NT_STATUS_OK
;
5201 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5202 TALLOC_CTX
*tmp_ctx
,
5206 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5208 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5209 TALLOC_CTX
*tmp_ctx
,
5213 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5216 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5217 TALLOC_CTX
*mem_ctx
,
5218 struct samu
*sampass
)
5220 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5221 LDAPMessage
*entry
= NULL
;
5222 LDAPMessage
*result
= NULL
;
5224 LDAPMod
**mods
= NULL
;
5227 const char *dn
= NULL
;
5231 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5233 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5234 DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
5235 return NT_STATUS_UNSUCCESSFUL
;
5237 gidstr
= talloc_asprintf(mem_ctx
, "%d", gid
);
5239 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5240 return NT_STATUS_NO_MEMORY
;
5243 filter
= talloc_asprintf(mem_ctx
,
5245 "(objectClass=posixAccount)"
5246 "(objectClass=sambaSamAccount))",
5247 pdb_get_username(sampass
));
5249 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5250 if (rc
!= LDAP_SUCCESS
) {
5251 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5252 return NT_STATUS_UNSUCCESSFUL
;
5254 talloc_autofree_ldapmsg(mem_ctx
, result
);
5256 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5258 if (num_result
== 0) {
5259 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5260 return NT_STATUS_NO_SUCH_USER
;
5263 if (num_result
> 1) {
5264 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5265 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5268 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5270 return NT_STATUS_UNSUCCESSFUL
;
5273 /* retrieve the dn for later use */
5274 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5276 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5277 return NT_STATUS_NO_MEMORY
;
5280 /* remove the old one, and add the new one, this way we do not risk races */
5281 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5284 return NT_STATUS_OK
;
5287 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5289 if (rc
!= LDAP_SUCCESS
) {
5290 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5291 pdb_get_username(sampass
), gidstr
));
5292 return NT_STATUS_UNSUCCESSFUL
;
5295 flush_pwnam_cache();
5297 return NT_STATUS_OK
;
5300 /**********************************************************************
5302 *********************************************************************/
5304 static void free_private_data(void **vp
)
5306 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
5308 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
5310 if ((*ldap_state
)->result
!= NULL
) {
5311 ldap_msgfree((*ldap_state
)->result
);
5312 (*ldap_state
)->result
= NULL
;
5314 if ((*ldap_state
)->domain_dn
!= NULL
) {
5315 SAFE_FREE((*ldap_state
)->domain_dn
);
5320 /* No need to free any further, as it is talloc()ed */
5323 /*********************************************************************
5324 Intitalise the parts of the pdb_methods structure that are common to
5326 *********************************************************************/
5328 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
5331 struct ldapsam_privates
*ldap_state
;
5333 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
5337 (*pdb_method
)->name
= "ldapsam";
5339 (*pdb_method
)->setsampwent
= ldapsam_setsampwent
;
5340 (*pdb_method
)->endsampwent
= ldapsam_endsampwent
;
5341 (*pdb_method
)->getsampwent
= ldapsam_getsampwent
;
5342 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
5343 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
5344 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
5345 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
5346 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
5347 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
5349 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
5350 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
5351 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
5352 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
5353 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
5354 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
5355 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
5357 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
5358 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
5360 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
5362 (*pdb_method
)->rid_algorithm
= ldapsam_rid_algorithm
;
5363 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
5365 /* TODO: Setup private data and free */
5367 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
5368 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
5369 return NT_STATUS_NO_MEMORY
;
5372 nt_status
= smbldap_init(*pdb_method
, location
, &ldap_state
->smbldap_state
);
5374 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5378 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
5379 return NT_STATUS_NO_MEMORY
;
5382 (*pdb_method
)->private_data
= ldap_state
;
5384 (*pdb_method
)->free_private_data
= free_private_data
;
5386 return NT_STATUS_OK
;
5389 /**********************************************************************
5390 Initialise the 'compat' mode for pdb_ldap
5391 *********************************************************************/
5393 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
5396 struct ldapsam_privates
*ldap_state
;
5397 char *uri
= talloc_strdup( NULL
, location
);
5399 #ifdef WITH_LDAP_SAMCONFIG
5401 int ldap_port
= lp_ldap_port();
5403 /* remap default port if not using SSL (ie clear or TLS) */
5404 if ( (lp_ldap_ssl() != LDAP_SSL_ON
) && (ldap_port
== 636) ) {
5408 uri
= talloc_asprintf(NULL
, "%s://%s:%d", lp_ldap_ssl() == LDAP_SSL_ON
? "ldaps" : "ldap", lp_ldap_server(), ldap_port
);
5410 return NT_STATUS_NO_MEMORY
;
5416 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
))) {
5420 /* the module itself stores a copy of the location so throw this one away */
5425 (*pdb_method
)->name
= "ldapsam_compat";
5427 ldap_state
= (*pdb_method
)->private_data
;
5428 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
5430 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
5432 return NT_STATUS_OK
;
5435 /**********************************************************************
5436 Initialise the normal mode for pdb_ldap
5437 *********************************************************************/
5439 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
5442 struct ldapsam_privates
*ldap_state
;
5443 uint32 alg_rid_base
;
5444 pstring alg_rid_base_string
;
5445 LDAPMessage
*result
= NULL
;
5446 LDAPMessage
*entry
= NULL
;
5447 DOM_SID ldap_domain_sid
;
5448 DOM_SID secrets_domain_sid
;
5449 pstring domain_sid_string
;
5452 nt_status
= pdb_init_ldapsam_common(pdb_method
, location
);
5453 if (!NT_STATUS_IS_OK(nt_status
)) {
5457 (*pdb_method
)->name
= "ldapsam";
5459 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
5460 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
5461 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
5462 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
5463 (*pdb_method
)->search_users
= ldapsam_search_users
;
5464 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
5465 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
5467 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
5468 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
5469 (*pdb_method
)->enum_group_memberships
=
5470 ldapsam_enum_group_memberships
;
5471 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
5472 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
5474 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
5475 (*pdb_method
)->create_user
= ldapsam_create_user
;
5476 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
5477 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
5478 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
5479 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
5480 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
5481 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
5485 ldap_state
= (*pdb_method
)->private_data
;
5486 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
5488 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
5490 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
5492 ldap_state
->domain_name
, True
);
5494 if ( !NT_STATUS_IS_OK(nt_status
) ) {
5495 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
5496 "info, nor add one to the domain\n"));
5497 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
5498 "will be unable to allocate new users/groups, "
5499 "and will risk BDCs having inconsistant SIDs\n"));
5500 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
5501 return NT_STATUS_OK
;
5504 /* Given that the above might fail, everything below this must be
5507 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
5510 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
5512 ldap_msgfree(result
);
5513 return NT_STATUS_UNSUCCESSFUL
;
5516 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
5518 return NT_STATUS_UNSUCCESSFUL
;
5521 ldap_state
->domain_dn
= smb_xstrdup(dn
);
5524 if (smbldap_get_single_pstring(
5525 ldap_state
->smbldap_state
->ldap_struct
,
5527 get_userattr_key2string(ldap_state
->schema_ver
,
5528 LDAP_ATTR_USER_SID
),
5529 domain_sid_string
)) {
5531 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
5532 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
5533 "read as a valid SID\n", domain_sid_string
));
5534 return NT_STATUS_INVALID_PARAMETER
;
5536 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
5537 &secrets_domain_sid
);
5538 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
5539 &ldap_domain_sid
)) {
5540 fstring new_sid_str
, old_sid_str
;
5541 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
5542 "%s based on pdb_ldap results %s -> %s\n",
5543 ldap_state
->domain_name
,
5544 sid_to_string(old_sid_str
,
5545 &secrets_domain_sid
),
5546 sid_to_string(new_sid_str
,
5547 &ldap_domain_sid
)));
5549 /* reset secrets.tdb sid */
5550 secrets_store_domain_sid(ldap_state
->domain_name
,
5552 DEBUG(1, ("New global sam SID: %s\n",
5553 sid_to_string(new_sid_str
,
5554 get_global_sam_sid())));
5556 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
5559 if (smbldap_get_single_pstring(
5560 ldap_state
->smbldap_state
->ldap_struct
,
5562 get_attr_key2string( dominfo_attr_list
,
5563 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
5564 alg_rid_base_string
)) {
5565 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
5566 if (alg_rid_base
!= algorithmic_rid_base()) {
5567 DEBUG(0, ("The value of 'algorithmic RID base' has "
5568 "changed since the LDAP\n"
5569 "database was initialised. Aborting. \n"));
5570 ldap_msgfree(result
);
5571 return NT_STATUS_UNSUCCESSFUL
;
5574 ldap_msgfree(result
);
5576 return NT_STATUS_OK
;
5579 NTSTATUS
pdb_ldap_init(void)
5582 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
5585 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
5588 /* Let pdb_nds register backends */
5591 return NT_STATUS_OK
;