2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
47 #include "../libcli/auth/libcli_auth.h"
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 fstring objclass_filter
;
166 switch( schema_ver
) {
167 case SCHEMAVER_SAMBAACCOUNT
:
168 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
170 case SCHEMAVER_SAMBASAMACCOUNT
:
171 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
174 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
175 objclass_filter
[0] = '\0';
179 result
= talloc_strdup(talloc_tos(), objclass_filter
);
180 SMB_ASSERT(result
!= NULL
);
184 /*****************************************************************
185 Scan a sequence number off OpenLDAP's syncrepl contextCSN
186 ******************************************************************/
188 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
190 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
191 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
192 LDAPMessage
*msg
= NULL
;
193 LDAPMessage
*entry
= NULL
;
195 char **values
= NULL
;
196 int rc
, num_result
, num_values
, rid
;
202 /* Unfortunatly there is no proper way to detect syncrepl-support in
203 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
204 * but do not show up in the root-DSE yet. Neither we can query the
205 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
206 * objectclass. Currently we require lp_ldap_suffix() to show up as
207 * namingContext. - Guenther
210 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
215 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
219 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
220 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
221 "as top-level namingContext\n", lp_ldap_suffix()));
225 mem_ctx
= talloc_init("ldapsam_get_seq_num");
228 return NT_STATUS_NO_MEMORY
;
230 if ((attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2)) == NULL
) {
231 ntstatus
= NT_STATUS_NO_MEMORY
;
235 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
236 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
239 /* consumer syncreplCookie: */
240 /* csn=20050126161620Z#0000001#00#00000 */
241 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
243 suffix
= talloc_asprintf(mem_ctx
,
244 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
246 ntstatus
= NT_STATUS_NO_MEMORY
;
251 /* provider contextCSN */
252 /* 20050126161620Z#000009#00#000000 */
253 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
255 suffix
= talloc_asprintf(mem_ctx
,
256 "cn=ldapsync,%s", lp_ldap_suffix());
259 ntstatus
= NT_STATUS_NO_MEMORY
;
264 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
265 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
267 if (rc
!= LDAP_SUCCESS
) {
271 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
272 if (num_result
!= 1) {
273 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
277 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
279 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
283 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
284 if (values
== NULL
) {
285 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
289 num_values
= ldap_count_values(values
);
290 if (num_values
== 0) {
291 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
296 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
297 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
302 if (!strncmp(p
, "csn=", strlen("csn=")))
305 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
307 *seq_num
= generalized_to_unix_time(p
);
309 /* very basic sanity check */
311 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
316 ntstatus
= NT_STATUS_OK
;
320 ldap_value_free(values
);
324 talloc_destroy(mem_ctx
);
329 /*******************************************************************
330 Run the search by name.
331 ******************************************************************/
333 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
335 LDAPMessage
** result
,
339 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
343 return LDAP_NO_MEMORY
;
347 * in the filter expression, replace %u with the real name
348 * so in ldap filter, %u MUST exist :-)
350 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
351 get_objclass_filter(ldap_state
->schema_ver
));
353 TALLOC_FREE(escape_user
);
354 return LDAP_NO_MEMORY
;
357 * have to use this here because $ is filtered out
361 filter
= talloc_all_string_sub(talloc_tos(),
362 filter
, "%u", escape_user
);
363 TALLOC_FREE(escape_user
);
365 return LDAP_NO_MEMORY
;
368 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
369 filter
, attr
, result
);
374 /*******************************************************************
375 Run the search by rid.
376 ******************************************************************/
378 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
379 uint32_t rid
, LDAPMessage
** result
,
385 filter
= talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid
,
386 get_objclass_filter(ldap_state
->schema_ver
));
388 return LDAP_NO_MEMORY
;
391 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
392 filter
, attr
, result
);
397 /*******************************************************************
398 Run the search by SID.
399 ******************************************************************/
401 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
402 const struct dom_sid
*sid
, LDAPMessage
** result
,
409 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
410 get_userattr_key2string(ldap_state
->schema_ver
,
412 sid_to_fstring(sid_string
, sid
),
413 get_objclass_filter(ldap_state
->schema_ver
));
415 return LDAP_NO_MEMORY
;
418 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
419 filter
, attr
, result
);
425 /*******************************************************************
426 Delete complete object or objectclass and attrs from
427 object found in search_result depending on lp_ldap_delete_dn
428 ******************************************************************/
430 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
433 const char *objectclass
,
436 LDAPMod
**mods
= NULL
;
439 BerElement
*ptr
= NULL
;
441 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
443 return LDAP_NO_MEMORY
;
446 if (lp_ldap_delete_dn()) {
447 return smbldap_delete(priv
->smbldap_state
, dn
);
450 /* Ok, delete only the SAM attributes */
452 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
454 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
457 /* We are only allowed to delete the attributes that
460 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
461 if (strequal(*attrib
, name
)) {
462 DEBUG(10, ("ldapsam_delete_entry: deleting "
463 "attribute %s\n", name
));
464 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
475 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
476 talloc_autofree_ldapmod(mem_ctx
, mods
);
478 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
481 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
486 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
487 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
493 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
494 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
504 /**********************************************************************
505 Initialize struct samu from an LDAP query.
506 (Based on init_sam_from_buffer in pdb_tdb.c)
507 *********************************************************************/
509 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
510 struct samu
* sampass
,
517 pass_can_change_time
,
518 pass_must_change_time
,
521 char *username
= NULL
,
527 *logon_script
= NULL
,
528 *profile_path
= NULL
,
530 *workstations
= NULL
,
533 uint8 smblmpwd
[LM_HASH_LEN
],
534 smbntpwd
[NT_HASH_LEN
];
535 bool use_samba_attrs
= True
;
536 uint32_t acct_ctrl
= 0;
538 uint16_t bad_password_count
= 0,
541 uint8 hours
[MAX_HOURS_LEN
];
543 struct login_cache cache_entry
;
545 bool expand_explicit
= lp_passdb_expand_explicit();
547 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
552 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
553 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
557 if (priv2ld(ldap_state
) == NULL
) {
558 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
559 "ldap_struct is NULL!\n"));
563 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
567 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
572 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
574 nt_username
= talloc_strdup(ctx
, username
);
579 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
584 pdb_set_username(sampass
, username
, PDB_SET
);
586 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
587 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
589 /* deal with different attributes between the schema first */
591 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
592 if ((temp
= smbldap_talloc_single_attribute(
593 ldap_state
->smbldap_state
->ldap_struct
,
595 get_userattr_key2string(ldap_state
->schema_ver
,
598 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
601 if ((temp
= smbldap_talloc_single_attribute(
602 ldap_state
->smbldap_state
->ldap_struct
,
604 get_userattr_key2string(ldap_state
->schema_ver
,
607 user_rid
= (uint32_t)atol(temp
);
608 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
612 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
613 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
614 get_userattr_key2string(ldap_state
->schema_ver
,
616 get_userattr_key2string(ldap_state
->schema_ver
,
622 temp
= smbldap_talloc_single_attribute(
623 ldap_state
->smbldap_state
->ldap_struct
,
625 get_userattr_key2string(ldap_state
->schema_ver
,
626 LDAP_ATTR_PWD_LAST_SET
),
629 pass_last_set_time
= (time_t) atol(temp
);
630 pdb_set_pass_last_set_time(sampass
,
631 pass_last_set_time
, PDB_SET
);
634 temp
= smbldap_talloc_single_attribute(
635 ldap_state
->smbldap_state
->ldap_struct
,
637 get_userattr_key2string(ldap_state
->schema_ver
,
638 LDAP_ATTR_LOGON_TIME
),
641 logon_time
= (time_t) atol(temp
);
642 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
645 temp
= smbldap_talloc_single_attribute(
646 ldap_state
->smbldap_state
->ldap_struct
,
648 get_userattr_key2string(ldap_state
->schema_ver
,
649 LDAP_ATTR_LOGOFF_TIME
),
652 logoff_time
= (time_t) atol(temp
);
653 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
656 temp
= smbldap_talloc_single_attribute(
657 ldap_state
->smbldap_state
->ldap_struct
,
659 get_userattr_key2string(ldap_state
->schema_ver
,
660 LDAP_ATTR_KICKOFF_TIME
),
663 kickoff_time
= (time_t) atol(temp
);
664 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
667 temp
= smbldap_talloc_single_attribute(
668 ldap_state
->smbldap_state
->ldap_struct
,
670 get_userattr_key2string(ldap_state
->schema_ver
,
671 LDAP_ATTR_PWD_CAN_CHANGE
),
674 pass_can_change_time
= (time_t) atol(temp
);
675 pdb_set_pass_can_change_time(sampass
,
676 pass_can_change_time
, PDB_SET
);
679 temp
= smbldap_talloc_single_attribute(
680 ldap_state
->smbldap_state
->ldap_struct
,
682 get_userattr_key2string(ldap_state
->schema_ver
,
683 LDAP_ATTR_PWD_MUST_CHANGE
),
686 pass_must_change_time
= (time_t) atol(temp
);
687 pdb_set_pass_must_change_time(sampass
,
688 pass_must_change_time
, PDB_SET
);
691 /* recommend that 'gecos' and 'displayName' should refer to the same
692 * attribute OID. userFullName depreciated, only used by Samba
693 * primary rules of LDAP: don't make a new attribute when one is already defined
694 * that fits your needs; using cn then displayName rather than 'userFullName'
697 fullname
= smbldap_talloc_single_attribute(
698 ldap_state
->smbldap_state
->ldap_struct
,
700 get_userattr_key2string(ldap_state
->schema_ver
,
701 LDAP_ATTR_DISPLAY_NAME
),
704 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
706 fullname
= smbldap_talloc_single_attribute(
707 ldap_state
->smbldap_state
->ldap_struct
,
709 get_userattr_key2string(ldap_state
->schema_ver
,
713 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
717 dir_drive
= smbldap_talloc_single_attribute(
718 ldap_state
->smbldap_state
->ldap_struct
,
720 get_userattr_key2string(ldap_state
->schema_ver
,
721 LDAP_ATTR_HOME_DRIVE
),
724 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
726 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
729 homedir
= smbldap_talloc_single_attribute(
730 ldap_state
->smbldap_state
->ldap_struct
,
732 get_userattr_key2string(ldap_state
->schema_ver
,
733 LDAP_ATTR_HOME_PATH
),
736 if (expand_explicit
) {
737 homedir
= talloc_sub_basic(ctx
,
745 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
747 pdb_set_homedir(sampass
,
748 talloc_sub_basic(ctx
, username
, domain
,
753 logon_script
= smbldap_talloc_single_attribute(
754 ldap_state
->smbldap_state
->ldap_struct
,
756 get_userattr_key2string(ldap_state
->schema_ver
,
757 LDAP_ATTR_LOGON_SCRIPT
),
760 if (expand_explicit
) {
761 logon_script
= talloc_sub_basic(ctx
,
769 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
771 pdb_set_logon_script(sampass
,
772 talloc_sub_basic(ctx
, username
, domain
,
777 profile_path
= smbldap_talloc_single_attribute(
778 ldap_state
->smbldap_state
->ldap_struct
,
780 get_userattr_key2string(ldap_state
->schema_ver
,
781 LDAP_ATTR_PROFILE_PATH
),
784 if (expand_explicit
) {
785 profile_path
= talloc_sub_basic(ctx
,
793 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
795 pdb_set_profile_path(sampass
,
796 talloc_sub_basic(ctx
, username
, domain
,
801 acct_desc
= smbldap_talloc_single_attribute(
802 ldap_state
->smbldap_state
->ldap_struct
,
804 get_userattr_key2string(ldap_state
->schema_ver
,
808 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
811 workstations
= smbldap_talloc_single_attribute(
812 ldap_state
->smbldap_state
->ldap_struct
,
814 get_userattr_key2string(ldap_state
->schema_ver
,
818 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
821 munged_dial
= smbldap_talloc_single_attribute(
822 ldap_state
->smbldap_state
->ldap_struct
,
824 get_userattr_key2string(ldap_state
->schema_ver
,
825 LDAP_ATTR_MUNGED_DIAL
),
828 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
831 /* FIXME: hours stuff should be cleaner */
835 memset(hours
, 0xff, hours_len
);
837 if (ldap_state
->is_nds_ldap
) {
840 char clear_text_pw
[512];
842 /* Make call to Novell eDirectory ldap extension to get clear text password.
843 NOTE: This will only work if we have an SSL connection to eDirectory. */
844 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
845 if (user_dn
!= NULL
) {
846 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
848 pwd_len
= sizeof(clear_text_pw
);
849 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
850 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
851 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
852 TALLOC_FREE(user_dn
);
855 ZERO_STRUCT(smblmpwd
);
856 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
857 TALLOC_FREE(user_dn
);
860 ZERO_STRUCT(smbntpwd
);
861 use_samba_attrs
= False
;
864 TALLOC_FREE(user_dn
);
867 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
871 if (use_samba_attrs
) {
872 temp
= smbldap_talloc_single_attribute(
873 ldap_state
->smbldap_state
->ldap_struct
,
875 get_userattr_key2string(ldap_state
->schema_ver
,
879 pdb_gethexpwd(temp
, smblmpwd
);
880 memset((char *)temp
, '\0', strlen(temp
)+1);
881 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
884 ZERO_STRUCT(smblmpwd
);
887 temp
= smbldap_talloc_single_attribute(
888 ldap_state
->smbldap_state
->ldap_struct
,
890 get_userattr_key2string(ldap_state
->schema_ver
,
894 pdb_gethexpwd(temp
, smbntpwd
);
895 memset((char *)temp
, '\0', strlen(temp
)+1);
896 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
899 ZERO_STRUCT(smbntpwd
);
905 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
907 uint8
*pwhist
= NULL
;
909 char *history_string
= TALLOC_ARRAY(ctx
, char,
910 MAX_PW_HISTORY_LEN
*64);
912 if (!history_string
) {
916 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
918 pwhist
= TALLOC_ARRAY(ctx
, uint8
,
919 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
920 if (pwhist
== NULL
) {
921 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
924 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
926 if (smbldap_get_single_attribute(
927 ldap_state
->smbldap_state
->ldap_struct
,
929 get_userattr_key2string(ldap_state
->schema_ver
,
930 LDAP_ATTR_PWD_HISTORY
),
932 MAX_PW_HISTORY_LEN
*64)) {
933 bool hex_failed
= false;
934 for (i
= 0; i
< pwHistLen
; i
++){
935 /* Get the 16 byte salt. */
936 if (!pdb_gethexpwd(&history_string
[i
*64],
937 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
941 /* Get the 16 byte MD5 hash of salt+passwd. */
942 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
943 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
944 PW_HISTORY_SALT_LEN
])) {
950 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
952 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
955 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
960 temp
= smbldap_talloc_single_attribute(
961 ldap_state
->smbldap_state
->ldap_struct
,
963 get_userattr_key2string(ldap_state
->schema_ver
,
967 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
969 if (acct_ctrl
== 0) {
970 acct_ctrl
|= ACB_NORMAL
;
973 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
975 acct_ctrl
|= ACB_NORMAL
;
978 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
979 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
981 temp
= smbldap_talloc_single_attribute(
982 ldap_state
->smbldap_state
->ldap_struct
,
984 get_userattr_key2string(ldap_state
->schema_ver
,
985 LDAP_ATTR_BAD_PASSWORD_COUNT
),
988 bad_password_count
= (uint32_t) atol(temp
);
989 pdb_set_bad_password_count(sampass
,
990 bad_password_count
, PDB_SET
);
993 temp
= smbldap_talloc_single_attribute(
994 ldap_state
->smbldap_state
->ldap_struct
,
996 get_userattr_key2string(ldap_state
->schema_ver
,
997 LDAP_ATTR_BAD_PASSWORD_TIME
),
1000 bad_password_time
= (time_t) atol(temp
);
1001 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
1005 temp
= smbldap_talloc_single_attribute(
1006 ldap_state
->smbldap_state
->ldap_struct
,
1008 get_userattr_key2string(ldap_state
->schema_ver
,
1009 LDAP_ATTR_LOGON_COUNT
),
1012 logon_count
= (uint32_t) atol(temp
);
1013 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
1016 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1018 temp
= smbldap_talloc_single_attribute(
1019 ldap_state
->smbldap_state
->ldap_struct
,
1021 get_userattr_key2string(ldap_state
->schema_ver
,
1022 LDAP_ATTR_LOGON_HOURS
),
1025 pdb_gethexhours(temp
, hours
);
1026 memset((char *)temp
, '\0', strlen(temp
) +1);
1027 pdb_set_hours(sampass
, hours
, PDB_SET
);
1031 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1032 struct passwd unix_pw
;
1033 bool have_uid
= false;
1034 bool have_gid
= false;
1035 struct dom_sid mapped_gsid
;
1036 const struct dom_sid
*primary_gsid
;
1038 ZERO_STRUCT(unix_pw
);
1040 unix_pw
.pw_name
= username
;
1041 unix_pw
.pw_passwd
= discard_const_p(char, "x");
1043 temp
= smbldap_talloc_single_attribute(
1044 priv2ld(ldap_state
),
1049 /* We've got a uid, feed the cache */
1050 unix_pw
.pw_uid
= strtoul(temp
, NULL
, 10);
1053 temp
= smbldap_talloc_single_attribute(
1054 priv2ld(ldap_state
),
1059 /* We've got a uid, feed the cache */
1060 unix_pw
.pw_gid
= strtoul(temp
, NULL
, 10);
1063 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1064 priv2ld(ldap_state
),
1068 if (unix_pw
.pw_gecos
) {
1069 unix_pw
.pw_gecos
= fullname
;
1071 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1072 priv2ld(ldap_state
),
1076 if (unix_pw
.pw_dir
) {
1077 unix_pw
.pw_dir
= discard_const_p(char, "");
1079 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1080 priv2ld(ldap_state
),
1084 if (unix_pw
.pw_shell
) {
1085 unix_pw
.pw_shell
= discard_const_p(char, "");
1088 if (have_uid
&& have_gid
) {
1089 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1091 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1094 if (sampass
->unix_pw
== NULL
) {
1095 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1096 pdb_get_username(sampass
)));
1100 store_uid_sid_cache(pdb_get_user_sid(sampass
),
1101 sampass
->unix_pw
->pw_uid
);
1102 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass
),
1103 sampass
->unix_pw
->pw_uid
);
1105 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1106 primary_gsid
= pdb_get_group_sid(sampass
);
1107 if (primary_gsid
&& sid_equal(primary_gsid
, &mapped_gsid
)) {
1108 store_gid_sid_cache(primary_gsid
,
1109 sampass
->unix_pw
->pw_gid
);
1110 idmap_cache_set_sid2gid(primary_gsid
,
1111 sampass
->unix_pw
->pw_gid
);
1115 /* check the timestamp of the cache vs ldap entry */
1116 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1122 /* see if we have newer updates */
1123 if (!login_cache_read(sampass
, &cache_entry
)) {
1124 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1125 (unsigned int)pdb_get_bad_password_count(sampass
),
1126 (unsigned int)pdb_get_bad_password_time(sampass
)));
1131 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1132 (unsigned int)ldap_entry_time
,
1133 (unsigned int)cache_entry
.entry_timestamp
,
1134 (unsigned int)cache_entry
.bad_password_time
));
1136 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1137 /* cache is older than directory , so
1138 we need to delete the entry but allow the
1139 fields to be written out */
1140 login_cache_delentry(sampass
);
1143 pdb_set_acct_ctrl(sampass
,
1144 pdb_get_acct_ctrl(sampass
) |
1145 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1147 pdb_set_bad_password_count(sampass
,
1148 cache_entry
.bad_password_count
,
1150 pdb_set_bad_password_time(sampass
,
1151 cache_entry
.bad_password_time
,
1163 /**********************************************************************
1164 Initialize the ldap db from a struct samu. Called on update.
1165 (Based on init_buffer_from_sam in pdb_tdb.c)
1166 *********************************************************************/
1168 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1169 LDAPMessage
*existing
,
1170 LDAPMod
*** mods
, struct samu
* sampass
,
1171 bool (*need_update
)(const struct samu
*,
1177 if (mods
== NULL
|| sampass
== NULL
) {
1178 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1185 * took out adding "objectclass: sambaAccount"
1186 * do this on a per-mod basis
1188 if (need_update(sampass
, PDB_USERNAME
)) {
1189 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1190 "uid", pdb_get_username(sampass
));
1191 if (ldap_state
->is_nds_ldap
) {
1192 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1193 "cn", pdb_get_username(sampass
));
1194 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1195 "sn", pdb_get_username(sampass
));
1199 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1201 /* only update the RID if we actually need to */
1202 if (need_update(sampass
, PDB_USERSID
)) {
1204 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1206 switch ( ldap_state
->schema_ver
) {
1207 case SCHEMAVER_SAMBAACCOUNT
:
1208 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1209 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1210 sid_string_dbg(user_sid
),
1212 &ldap_state
->domain_sid
)));
1215 if (asprintf(&temp
, "%i", rid
) < 0) {
1218 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1219 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1224 case SCHEMAVER_SAMBASAMACCOUNT
:
1225 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1226 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1227 sid_to_fstring(sid_string
, user_sid
));
1231 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1236 /* we don't need to store the primary group RID - so leaving it
1237 'free' to hang off the unix primary group makes life easier */
1239 if (need_update(sampass
, PDB_GROUPSID
)) {
1241 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1243 switch ( ldap_state
->schema_ver
) {
1244 case SCHEMAVER_SAMBAACCOUNT
:
1245 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1246 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1247 sid_string_dbg(group_sid
),
1249 &ldap_state
->domain_sid
)));
1253 if (asprintf(&temp
, "%i", rid
) < 0) {
1256 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1257 get_userattr_key2string(ldap_state
->schema_ver
,
1258 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1262 case SCHEMAVER_SAMBASAMACCOUNT
:
1263 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1264 get_userattr_key2string(ldap_state
->schema_ver
,
1265 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1269 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1275 /* displayName, cn, and gecos should all be the same
1276 * most easily accomplished by giving them the same OID
1277 * gecos isn't set here b/c it should be handled by the
1279 * We change displayName only and fall back to cn if
1280 * it does not exist.
1283 if (need_update(sampass
, PDB_FULLNAME
))
1284 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1285 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1286 pdb_get_fullname(sampass
));
1288 if (need_update(sampass
, PDB_ACCTDESC
))
1289 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1290 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1291 pdb_get_acct_desc(sampass
));
1293 if (need_update(sampass
, PDB_WORKSTATIONS
))
1294 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1295 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1296 pdb_get_workstations(sampass
));
1298 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1299 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1300 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1301 pdb_get_munged_dial(sampass
));
1303 if (need_update(sampass
, PDB_SMBHOME
))
1304 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1305 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1306 pdb_get_homedir(sampass
));
1308 if (need_update(sampass
, PDB_DRIVE
))
1309 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1310 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1311 pdb_get_dir_drive(sampass
));
1313 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1314 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1315 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1316 pdb_get_logon_script(sampass
));
1318 if (need_update(sampass
, PDB_PROFILE
))
1319 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1320 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1321 pdb_get_profile_path(sampass
));
1323 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1326 if (need_update(sampass
, PDB_LOGONTIME
))
1327 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1328 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1331 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1334 if (need_update(sampass
, PDB_LOGOFFTIME
))
1335 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1336 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1339 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1342 if (need_update(sampass
, PDB_KICKOFFTIME
))
1343 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1344 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1347 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1350 if (need_update(sampass
, PDB_CANCHANGETIME
))
1351 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1352 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1355 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_must_change_time(sampass
)) < 0) {
1358 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1359 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1360 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1363 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1364 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1366 if (need_update(sampass
, PDB_LMPASSWD
)) {
1367 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1370 pdb_sethexpwd(pwstr
, lm_pw
,
1371 pdb_get_acct_ctrl(sampass
));
1372 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1373 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1376 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1377 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1381 if (need_update(sampass
, PDB_NTPASSWD
)) {
1382 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1385 pdb_sethexpwd(pwstr
, nt_pw
,
1386 pdb_get_acct_ctrl(sampass
));
1387 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1388 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1391 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1392 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1397 if (need_update(sampass
, PDB_PWHISTORY
)) {
1399 uint32_t pwHistLen
= 0;
1400 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1402 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1406 if (pwHistLen
== 0) {
1407 /* Remove any password history from the LDAP store. */
1408 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1412 uint32_t currHistLen
= 0;
1413 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1414 if (pwhist
!= NULL
) {
1415 /* We can only store (1024-1/64 password history entries. */
1416 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1417 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1418 /* Store the salt. */
1419 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1420 /* Followed by the md5 hash of salt + md4 hash */
1421 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1422 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1423 DEBUG(100, ("pwstr=%s\n", pwstr
));
1427 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1428 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1433 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1434 if (asprintf(&temp
, "%li",
1435 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1438 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1439 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1445 if (need_update(sampass
, PDB_HOURS
)) {
1446 const uint8
*hours
= pdb_get_hours(sampass
);
1449 pdb_sethexhours(hourstr
, hours
);
1450 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1453 get_userattr_key2string(ldap_state
->schema_ver
,
1454 LDAP_ATTR_LOGON_HOURS
),
1459 if (need_update(sampass
, PDB_ACCTCTRL
))
1460 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1461 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1462 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1464 /* password lockout cache:
1465 - If we are now autolocking or clearing, we write to ldap
1466 - If we are clearing, we delete the cache entry
1467 - If the count is > 0, we update the cache
1469 This even means when autolocking, we cache, just in case the
1470 update doesn't work, and we have to cache the autolock flag */
1472 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1473 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1474 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1475 time_t badtime
= pdb_get_bad_password_time(sampass
);
1477 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1479 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1480 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1482 if ((badcount
>= pol
) || (badcount
== 0)) {
1483 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1484 (unsigned int)badcount
, (unsigned int)badtime
));
1485 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1489 ldap_state
->smbldap_state
->ldap_struct
,
1491 get_userattr_key2string(
1492 ldap_state
->schema_ver
,
1493 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1497 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1501 ldap_state
->smbldap_state
->ldap_struct
,
1503 get_userattr_key2string(
1504 ldap_state
->schema_ver
,
1505 LDAP_ATTR_BAD_PASSWORD_TIME
),
1509 if (badcount
== 0) {
1510 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1511 login_cache_delentry(sampass
);
1513 struct login_cache cache_entry
;
1515 cache_entry
.entry_timestamp
= time(NULL
);
1516 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1517 cache_entry
.bad_password_count
= badcount
;
1518 cache_entry
.bad_password_time
= badtime
;
1520 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1521 login_cache_write(sampass
, &cache_entry
);
1528 /**********************************************************************
1529 End enumeration of the LDAP password list.
1530 *********************************************************************/
1532 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1534 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1535 if (ldap_state
->result
) {
1536 ldap_msgfree(ldap_state
->result
);
1537 ldap_state
->result
= NULL
;
1541 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1542 const char *new_attr
)
1546 if (new_attr
== NULL
) {
1550 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1554 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1556 SMB_ASSERT((*attr_list
) != NULL
);
1557 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1558 (*attr_list
)[i
+1] = NULL
;
1561 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1562 const char ***attr_list
)
1564 append_attr(mem_ctx
, attr_list
, "uidNumber");
1565 append_attr(mem_ctx
, attr_list
, "gidNumber");
1566 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1567 append_attr(mem_ctx
, attr_list
, "loginShell");
1568 append_attr(mem_ctx
, attr_list
, "gecos");
1571 /**********************************************************************
1572 Get struct samu entry from LDAP by username.
1573 *********************************************************************/
1575 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1577 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1578 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1579 LDAPMessage
*result
= NULL
;
1580 LDAPMessage
*entry
= NULL
;
1582 const char ** attr_list
;
1585 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1586 append_attr(user
, &attr_list
,
1587 get_userattr_key2string(ldap_state
->schema_ver
,
1588 LDAP_ATTR_MOD_TIMESTAMP
));
1589 ldapsam_add_unix_attributes(user
, &attr_list
);
1590 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1592 TALLOC_FREE( attr_list
);
1594 if ( rc
!= LDAP_SUCCESS
)
1595 return NT_STATUS_NO_SUCH_USER
;
1597 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1600 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1601 ldap_msgfree(result
);
1602 return NT_STATUS_NO_SUCH_USER
;
1603 } else if (count
> 1) {
1604 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1605 ldap_msgfree(result
);
1606 return NT_STATUS_NO_SUCH_USER
;
1609 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1611 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1612 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1613 ldap_msgfree(result
);
1614 return NT_STATUS_NO_SUCH_USER
;
1616 pdb_set_backend_private_data(user
, result
, NULL
,
1617 my_methods
, PDB_CHANGED
);
1618 talloc_autofree_ldapmsg(user
, result
);
1621 ldap_msgfree(result
);
1626 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1627 const struct dom_sid
*sid
, LDAPMessage
**result
)
1630 const char ** attr_list
;
1633 switch ( ldap_state
->schema_ver
) {
1634 case SCHEMAVER_SAMBASAMACCOUNT
: {
1635 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1636 if (tmp_ctx
== NULL
) {
1637 return LDAP_NO_MEMORY
;
1640 attr_list
= get_userattr_list(tmp_ctx
,
1641 ldap_state
->schema_ver
);
1642 append_attr(tmp_ctx
, &attr_list
,
1643 get_userattr_key2string(
1644 ldap_state
->schema_ver
,
1645 LDAP_ATTR_MOD_TIMESTAMP
));
1646 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1647 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1649 TALLOC_FREE(tmp_ctx
);
1651 if ( rc
!= LDAP_SUCCESS
)
1656 case SCHEMAVER_SAMBAACCOUNT
:
1657 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1661 attr_list
= get_userattr_list(NULL
,
1662 ldap_state
->schema_ver
);
1663 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1664 TALLOC_FREE( attr_list
);
1666 if ( rc
!= LDAP_SUCCESS
)
1673 /**********************************************************************
1674 Get struct samu entry from LDAP by SID.
1675 *********************************************************************/
1677 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1679 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1680 LDAPMessage
*result
= NULL
;
1681 LDAPMessage
*entry
= NULL
;
1685 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1687 if (rc
!= LDAP_SUCCESS
)
1688 return NT_STATUS_NO_SUCH_USER
;
1690 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1693 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1694 "count=%d\n", sid_string_dbg(sid
), count
));
1695 ldap_msgfree(result
);
1696 return NT_STATUS_NO_SUCH_USER
;
1697 } else if (count
> 1) {
1698 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1699 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1701 ldap_msgfree(result
);
1702 return NT_STATUS_NO_SUCH_USER
;
1705 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1707 ldap_msgfree(result
);
1708 return NT_STATUS_NO_SUCH_USER
;
1711 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1712 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1713 ldap_msgfree(result
);
1714 return NT_STATUS_NO_SUCH_USER
;
1717 pdb_set_backend_private_data(user
, result
, NULL
,
1718 my_methods
, PDB_CHANGED
);
1719 talloc_autofree_ldapmsg(user
, result
);
1720 return NT_STATUS_OK
;
1723 /********************************************************************
1724 Do the actual modification - also change a plaintext passord if
1726 **********************************************************************/
1728 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1729 struct samu
*newpwd
, char *dn
,
1730 LDAPMod
**mods
, int ldap_op
,
1731 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1733 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1736 if (!newpwd
|| !dn
) {
1737 return NT_STATUS_INVALID_PARAMETER
;
1741 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1742 /* may be password change below however */
1746 if (ldap_state
->is_nds_ldap
) {
1747 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1751 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1755 rc
= smbldap_add(ldap_state
->smbldap_state
,
1758 case LDAP_MOD_REPLACE
:
1759 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1763 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1765 return NT_STATUS_INVALID_PARAMETER
;
1768 if (rc
!=LDAP_SUCCESS
) {
1769 return NT_STATUS_UNSUCCESSFUL
;
1773 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1774 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1775 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1776 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1779 char *retoid
= NULL
;
1780 struct berval
*retdata
= NULL
;
1781 char *utf8_password
;
1783 size_t converted_size
;
1786 if (!ldap_state
->is_nds_ldap
) {
1788 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1789 LDAP_EXOP_MODIFY_PASSWD
)) {
1790 DEBUG(2, ("ldap password change requested, but LDAP "
1791 "server does not support it -- ignoring\n"));
1792 return NT_STATUS_OK
;
1796 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1797 pdb_get_plaintext_passwd(newpwd
),
1800 return NT_STATUS_NO_MEMORY
;
1803 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1804 TALLOC_FREE(utf8_password
);
1805 return NT_STATUS_NO_MEMORY
;
1808 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1809 DEBUG(0,("ber_alloc_t returns NULL\n"));
1810 TALLOC_FREE(utf8_password
);
1811 TALLOC_FREE(utf8_dn
);
1812 return NT_STATUS_UNSUCCESSFUL
;
1815 if ((ber_printf (ber
, "{") < 0) ||
1816 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1818 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1821 TALLOC_FREE(utf8_dn
);
1822 TALLOC_FREE(utf8_password
);
1823 return NT_STATUS_UNSUCCESSFUL
;
1826 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1827 ret
= ber_printf(ber
, "ts}",
1828 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1831 ret
= ber_printf(ber
, "}");
1835 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1838 TALLOC_FREE(utf8_dn
);
1839 TALLOC_FREE(utf8_password
);
1840 return NT_STATUS_UNSUCCESSFUL
;
1843 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1844 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1846 TALLOC_FREE(utf8_dn
);
1847 TALLOC_FREE(utf8_password
);
1848 return NT_STATUS_UNSUCCESSFUL
;
1851 TALLOC_FREE(utf8_dn
);
1852 TALLOC_FREE(utf8_password
);
1855 if (!ldap_state
->is_nds_ldap
) {
1856 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1857 LDAP_EXOP_MODIFY_PASSWD
,
1858 bv
, NULL
, NULL
, &retoid
,
1861 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1862 pdb_get_plaintext_passwd(newpwd
));
1864 if (rc
!= LDAP_SUCCESS
) {
1865 char *ld_error
= NULL
;
1867 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1868 DEBUG(3, ("Could not set userPassword "
1869 "attribute due to an objectClass "
1870 "violation -- ignoring\n"));
1872 return NT_STATUS_OK
;
1875 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1877 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1878 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1879 SAFE_FREE(ld_error
);
1881 #if defined(LDAP_CONSTRAINT_VIOLATION)
1882 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1883 return NT_STATUS_PASSWORD_RESTRICTION
;
1885 return NT_STATUS_UNSUCCESSFUL
;
1887 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1888 #ifdef DEBUG_PASSWORD
1889 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1892 ber_bvfree(retdata
);
1894 ldap_memfree(retoid
);
1898 return NT_STATUS_OK
;
1901 /**********************************************************************
1902 Delete entry from LDAP for username.
1903 *********************************************************************/
1905 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1906 struct samu
* sam_acct
)
1908 struct ldapsam_privates
*priv
=
1909 (struct ldapsam_privates
*)my_methods
->private_data
;
1912 LDAPMessage
*msg
, *entry
;
1913 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1914 const char **attr_list
;
1915 TALLOC_CTX
*mem_ctx
;
1918 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1919 return NT_STATUS_INVALID_PARAMETER
;
1922 sname
= pdb_get_username(sam_acct
);
1924 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1927 mem_ctx
= talloc_new(NULL
);
1928 if (mem_ctx
== NULL
) {
1929 DEBUG(0, ("talloc_new failed\n"));
1933 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1934 if (attr_list
== NULL
) {
1938 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1940 if ((rc
!= LDAP_SUCCESS
) ||
1941 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1942 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1943 DEBUG(5, ("Could not find user %s\n", sname
));
1944 result
= NT_STATUS_NO_SUCH_USER
;
1948 rc
= ldapsam_delete_entry(
1949 priv
, mem_ctx
, entry
,
1950 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1951 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1954 result
= (rc
== LDAP_SUCCESS
) ?
1955 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1958 TALLOC_FREE(mem_ctx
);
1962 /**********************************************************************
1963 Helper function to determine for update_sam_account whether
1964 we need LDAP modification.
1965 *********************************************************************/
1967 static bool element_is_changed(const struct samu
*sampass
,
1968 enum pdb_elements element
)
1970 return IS_SAM_CHANGED(sampass
, element
);
1973 /**********************************************************************
1975 *********************************************************************/
1977 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1979 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1980 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1983 LDAPMessage
*result
= NULL
;
1984 LDAPMessage
*entry
= NULL
;
1985 LDAPMod
**mods
= NULL
;
1986 const char **attr_list
;
1988 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1990 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1991 if (pdb_get_username(newpwd
) == NULL
) {
1992 return NT_STATUS_INVALID_PARAMETER
;
1994 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1995 TALLOC_FREE( attr_list
);
1996 if (rc
!= LDAP_SUCCESS
) {
1997 return NT_STATUS_UNSUCCESSFUL
;
1999 pdb_set_backend_private_data(newpwd
, result
, NULL
,
2000 my_methods
, PDB_CHANGED
);
2001 talloc_autofree_ldapmsg(newpwd
, result
);
2004 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
2005 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
2006 return NT_STATUS_UNSUCCESSFUL
;
2009 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2010 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
2012 return NT_STATUS_UNSUCCESSFUL
;
2015 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
2017 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2018 element_is_changed
)) {
2019 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2022 ldap_mods_free(mods
,True
);
2023 return NT_STATUS_UNSUCCESSFUL
;
2026 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
2027 && (mods
== NULL
)) {
2028 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2029 pdb_get_username(newpwd
)));
2031 return NT_STATUS_OK
;
2034 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
2037 ldap_mods_free(mods
,True
);
2043 * We need to set the backend private data to NULL here. For example
2044 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2045 * same one, and with the explicit delete / add logic for attribute
2046 * values the second time we would use the wrong "old" value which
2047 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2049 * The existing LDAPMessage is still being auto-freed by the
2052 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2055 if (!NT_STATUS_IS_OK(ret
)) {
2059 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2060 pdb_get_username(newpwd
)));
2061 return NT_STATUS_OK
;
2064 /***************************************************************************
2065 Renames a struct samu
2066 - The "rename user script" has full responsibility for changing everything
2067 ***************************************************************************/
2069 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2070 TALLOC_CTX
*tmp_ctx
,
2072 uint32_t member_rid
);
2074 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2075 TALLOC_CTX
*mem_ctx
,
2077 struct dom_sid
**pp_sids
,
2079 size_t *p_num_groups
);
2081 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2082 struct samu
*old_acct
,
2083 const char *newname
)
2085 const char *oldname
;
2087 char *rename_script
= NULL
;
2088 fstring oldname_lower
, newname_lower
;
2091 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2092 return NT_STATUS_INVALID_PARAMETER
;
2095 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2096 return NT_STATUS_INVALID_PARAMETER
;
2099 oldname
= pdb_get_username(old_acct
);
2101 /* rename the posix user */
2102 rename_script
= SMB_STRDUP(lp_renameuser_script());
2103 if (rename_script
== NULL
) {
2104 return NT_STATUS_NO_MEMORY
;
2107 if (!(*rename_script
)) {
2108 SAFE_FREE(rename_script
);
2109 return NT_STATUS_ACCESS_DENIED
;
2112 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2115 /* We have to allow the account name to end with a '$'.
2116 Also, follow the semantics in _samr_create_user() and lower case the
2117 posix name but preserve the case in passdb */
2119 fstrcpy( oldname_lower
, oldname
);
2120 strlower_m( oldname_lower
);
2121 fstrcpy( newname_lower
, newname
);
2122 strlower_m( newname_lower
);
2123 rename_script
= realloc_string_sub2(rename_script
,
2128 if (!rename_script
) {
2129 return NT_STATUS_NO_MEMORY
;
2131 rename_script
= realloc_string_sub2(rename_script
,
2136 rc
= smbrun(rename_script
, NULL
);
2138 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2139 rename_script
, rc
));
2141 SAFE_FREE(rename_script
);
2144 smb_nscd_flush_user_cache();
2148 return NT_STATUS_UNSUCCESSFUL
;
2150 return NT_STATUS_OK
;
2153 /**********************************************************************
2154 Helper function to determine for update_sam_account whether
2155 we need LDAP modification.
2156 *********************************************************************/
2158 static bool element_is_set_or_changed(const struct samu
*sampass
,
2159 enum pdb_elements element
)
2161 return (IS_SAM_SET(sampass
, element
) ||
2162 IS_SAM_CHANGED(sampass
, element
));
2165 /**********************************************************************
2166 Add struct samu to LDAP.
2167 *********************************************************************/
2169 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2171 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2172 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2174 LDAPMessage
*result
= NULL
;
2175 LDAPMessage
*entry
= NULL
;
2176 LDAPMod
**mods
= NULL
;
2177 int ldap_op
= LDAP_MOD_REPLACE
;
2178 uint32_t num_result
;
2179 const char **attr_list
;
2180 char *escape_user
= NULL
;
2181 const char *username
= pdb_get_username(newpwd
);
2182 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2183 char *filter
= NULL
;
2185 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2186 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2189 return NT_STATUS_NO_MEMORY
;
2192 if (!username
|| !*username
) {
2193 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2194 status
= NT_STATUS_INVALID_PARAMETER
;
2198 /* free this list after the second search or in case we exit on failure */
2199 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2201 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2203 if (rc
!= LDAP_SUCCESS
) {
2207 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2208 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2212 ldap_msgfree(result
);
2215 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2216 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2218 if (rc
== LDAP_SUCCESS
) {
2219 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2220 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2221 "already in the base, with samba "
2222 "attributes\n", sid_string_dbg(sid
)));
2225 ldap_msgfree(result
);
2230 /* does the entry already exist but without a samba attributes?
2231 we need to return the samba attributes here */
2233 escape_user
= escape_ldap_string(talloc_tos(), username
);
2234 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2236 status
= NT_STATUS_NO_MEMORY
;
2239 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2240 TALLOC_FREE(escape_user
);
2242 status
= NT_STATUS_NO_MEMORY
;
2246 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2247 filter
, attr_list
, &result
);
2248 if ( rc
!= LDAP_SUCCESS
) {
2252 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2254 if (num_result
> 1) {
2255 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2259 /* Check if we need to update an existing entry */
2260 if (num_result
== 1) {
2261 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2262 ldap_op
= LDAP_MOD_REPLACE
;
2263 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2264 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2266 status
= NT_STATUS_NO_MEMORY
;
2270 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2272 /* There might be a SID for this account already - say an idmap entry */
2274 filter
= talloc_asprintf(ctx
,
2275 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2276 get_userattr_key2string(ldap_state
->schema_ver
,
2277 LDAP_ATTR_USER_SID
),
2278 sid_string_talloc(ctx
, sid
),
2279 LDAP_OBJ_IDMAP_ENTRY
,
2280 LDAP_OBJ_SID_ENTRY
);
2282 status
= NT_STATUS_NO_MEMORY
;
2286 /* free old result before doing a new search */
2287 if (result
!= NULL
) {
2288 ldap_msgfree(result
);
2291 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2292 filter
, attr_list
, &result
);
2294 if ( rc
!= LDAP_SUCCESS
) {
2298 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2300 if (num_result
> 1) {
2301 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2305 /* Check if we need to update an existing entry */
2306 if (num_result
== 1) {
2308 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2309 ldap_op
= LDAP_MOD_REPLACE
;
2310 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2311 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2313 status
= NT_STATUS_NO_MEMORY
;
2319 if (num_result
== 0) {
2320 char *escape_username
;
2321 /* Check if we need to add an entry */
2322 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2323 ldap_op
= LDAP_MOD_ADD
;
2325 escape_username
= escape_rdn_val_string_alloc(username
);
2326 if (!escape_username
) {
2327 status
= NT_STATUS_NO_MEMORY
;
2331 if (username
[strlen(username
)-1] == '$') {
2332 dn
= talloc_asprintf(ctx
,
2335 lp_ldap_machine_suffix());
2337 dn
= talloc_asprintf(ctx
,
2340 lp_ldap_user_suffix());
2343 SAFE_FREE(escape_username
);
2345 status
= NT_STATUS_NO_MEMORY
;
2350 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2351 element_is_set_or_changed
)) {
2352 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2354 ldap_mods_free(mods
, true);
2360 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2363 switch ( ldap_state
->schema_ver
) {
2364 case SCHEMAVER_SAMBAACCOUNT
:
2365 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2367 case SCHEMAVER_SAMBASAMACCOUNT
:
2368 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2371 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2375 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2376 if (!NT_STATUS_IS_OK(ret
)) {
2377 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2378 pdb_get_username(newpwd
),dn
));
2379 ldap_mods_free(mods
, true);
2383 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2384 ldap_mods_free(mods
, true);
2386 status
= NT_STATUS_OK
;
2392 ldap_msgfree(result
);
2397 /**********************************************************************
2398 *********************************************************************/
2400 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2402 LDAPMessage
** result
)
2404 int scope
= LDAP_SCOPE_SUBTREE
;
2406 const char **attr_list
;
2408 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2409 rc
= smbldap_search(ldap_state
->smbldap_state
,
2410 lp_ldap_suffix (), scope
,
2411 filter
, attr_list
, 0, result
);
2412 TALLOC_FREE(attr_list
);
2417 /**********************************************************************
2418 *********************************************************************/
2420 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2421 GROUP_MAP
*map
, LDAPMessage
*entry
)
2424 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2426 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2427 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2428 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2433 temp
= smbldap_talloc_single_attribute(
2434 ldap_state
->smbldap_state
->ldap_struct
,
2436 get_attr_key2string(groupmap_attr_list
,
2437 LDAP_ATTR_GIDNUMBER
),
2440 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2441 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2445 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2447 map
->gid
= (gid_t
)atol(temp
);
2450 temp
= smbldap_talloc_single_attribute(
2451 ldap_state
->smbldap_state
->ldap_struct
,
2453 get_attr_key2string(groupmap_attr_list
,
2454 LDAP_ATTR_GROUP_SID
),
2457 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2458 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2463 if (!string_to_sid(&map
->sid
, temp
)) {
2464 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2470 temp
= smbldap_talloc_single_attribute(
2471 ldap_state
->smbldap_state
->ldap_struct
,
2473 get_attr_key2string(groupmap_attr_list
,
2474 LDAP_ATTR_GROUP_TYPE
),
2477 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2478 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2482 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2484 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2485 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2486 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2492 temp
= smbldap_talloc_single_attribute(
2493 ldap_state
->smbldap_state
->ldap_struct
,
2495 get_attr_key2string(groupmap_attr_list
,
2496 LDAP_ATTR_DISPLAY_NAME
),
2499 temp
= smbldap_talloc_single_attribute(
2500 ldap_state
->smbldap_state
->ldap_struct
,
2502 get_attr_key2string(groupmap_attr_list
,
2506 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2507 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2512 fstrcpy(map
->nt_name
, temp
);
2515 temp
= smbldap_talloc_single_attribute(
2516 ldap_state
->smbldap_state
->ldap_struct
,
2518 get_attr_key2string(groupmap_attr_list
,
2522 temp
= talloc_strdup(ctx
, "");
2528 fstrcpy(map
->comment
, temp
);
2530 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2531 store_gid_sid_cache(&map
->sid
, map
->gid
);
2532 idmap_cache_set_sid2gid(&map
->sid
, map
->gid
);
2539 /**********************************************************************
2540 *********************************************************************/
2542 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2546 struct ldapsam_privates
*ldap_state
=
2547 (struct ldapsam_privates
*)methods
->private_data
;
2548 LDAPMessage
*result
= NULL
;
2549 LDAPMessage
*entry
= NULL
;
2552 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2554 return NT_STATUS_NO_SUCH_GROUP
;
2557 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2560 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2562 ldap_msgfree(result
);
2563 return NT_STATUS_NO_SUCH_GROUP
;
2567 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2568 "count=%d\n", filter
, count
));
2569 ldap_msgfree(result
);
2570 return NT_STATUS_NO_SUCH_GROUP
;
2573 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2576 ldap_msgfree(result
);
2577 return NT_STATUS_UNSUCCESSFUL
;
2580 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2581 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2582 "group filter %s\n", filter
));
2583 ldap_msgfree(result
);
2584 return NT_STATUS_NO_SUCH_GROUP
;
2587 ldap_msgfree(result
);
2588 return NT_STATUS_OK
;
2591 /**********************************************************************
2592 *********************************************************************/
2594 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2597 char *filter
= NULL
;
2601 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2603 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2604 sid_to_fstring(tmp
, &sid
)) < 0) {
2605 return NT_STATUS_NO_MEMORY
;
2608 status
= ldapsam_getgroup(methods
, filter
, map
);
2613 /**********************************************************************
2614 *********************************************************************/
2616 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2619 char *filter
= NULL
;
2622 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2624 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2625 (unsigned long)gid
) < 0) {
2626 return NT_STATUS_NO_MEMORY
;
2629 status
= ldapsam_getgroup(methods
, filter
, map
);
2634 /**********************************************************************
2635 *********************************************************************/
2637 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2640 char *filter
= NULL
;
2641 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2645 return NT_STATUS_NO_MEMORY
;
2648 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2650 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2651 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2653 TALLOC_FREE(escape_name
);
2654 return NT_STATUS_NO_MEMORY
;
2657 TALLOC_FREE(escape_name
);
2658 status
= ldapsam_getgroup(methods
, filter
, map
);
2663 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2665 const struct dom_sid
*domain_sid
,
2671 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2672 str
, sizeof(str
)-1)) {
2673 DEBUG(10, ("Could not find sambaSID attribute\n"));
2677 if (!string_to_sid(&sid
, str
)) {
2678 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2682 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2683 DEBUG(10, ("SID %s is not in expected domain %s\n",
2684 str
, sid_string_dbg(domain_sid
)));
2688 if (!sid_peek_rid(&sid
, rid
)) {
2689 DEBUG(10, ("Could not peek into RID\n"));
2696 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2697 TALLOC_CTX
*mem_ctx
,
2698 const struct dom_sid
*group
,
2699 uint32_t **pp_member_rids
,
2700 size_t *p_num_members
)
2702 struct ldapsam_privates
*ldap_state
=
2703 (struct ldapsam_privates
*)methods
->private_data
;
2704 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2705 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2706 const char *sid_attrs
[] = { "sambaSID", NULL
};
2707 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2708 LDAPMessage
*result
= NULL
;
2711 char **values
= NULL
;
2716 *pp_member_rids
= NULL
;
2719 filter
= talloc_asprintf(mem_ctx
,
2720 "(&(objectClass=%s)"
2723 LDAP_OBJ_POSIXGROUP
,
2725 sid_string_talloc(mem_ctx
, group
));
2726 if (filter
== NULL
) {
2727 ret
= NT_STATUS_NO_MEMORY
;
2731 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2732 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2735 if (rc
!= LDAP_SUCCESS
)
2738 talloc_autofree_ldapmsg(mem_ctx
, result
);
2740 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2743 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2744 sid_string_dbg(group
)));
2745 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2750 ret
= NT_STATUS_NO_SUCH_GROUP
;
2754 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2758 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2760 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2761 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2765 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2767 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2769 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2770 if (filter
== NULL
) {
2771 ret
= NT_STATUS_NO_MEMORY
;
2775 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2776 char *escape_memberuid
;
2778 escape_memberuid
= escape_ldap_string(talloc_tos(),
2780 if (escape_memberuid
== NULL
) {
2781 ret
= NT_STATUS_NO_MEMORY
;
2785 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2786 TALLOC_FREE(escape_memberuid
);
2787 if (filter
== NULL
) {
2788 ret
= NT_STATUS_NO_MEMORY
;
2793 filter
= talloc_asprintf_append_buffer(filter
, "))");
2794 if (filter
== NULL
) {
2795 ret
= NT_STATUS_NO_MEMORY
;
2799 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2800 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2803 if (rc
!= LDAP_SUCCESS
)
2806 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2807 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2809 talloc_autofree_ldapmsg(mem_ctx
, result
);
2811 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2813 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2819 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2823 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2824 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2825 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2829 if (!string_to_sid(&sid
, sidstr
))
2832 if (!sid_check_is_in_our_domain(&sid
)) {
2833 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2834 "in our domain\n"));
2835 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2839 sid_peek_rid(&sid
, &rid
);
2841 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2843 ret
= NT_STATUS_NO_MEMORY
;
2849 filter
= talloc_asprintf(mem_ctx
,
2850 "(&(objectClass=%s)"
2852 LDAP_OBJ_SAMBASAMACCOUNT
,
2855 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2856 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2859 if (rc
!= LDAP_SUCCESS
)
2862 talloc_autofree_ldapmsg(mem_ctx
, result
);
2864 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2866 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2870 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2872 get_global_sam_sid(),
2874 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2875 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2879 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2881 ret
= NT_STATUS_NO_MEMORY
;
2891 ldap_value_free(values
);
2896 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2897 TALLOC_CTX
*mem_ctx
,
2899 struct dom_sid
**pp_sids
,
2901 size_t *p_num_groups
)
2903 struct ldapsam_privates
*ldap_state
=
2904 (struct ldapsam_privates
*)methods
->private_data
;
2905 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2907 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2910 LDAPMessage
*result
= NULL
;
2912 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2913 size_t num_sids
, num_gids
;
2915 gid_t primary_gid
= -1;
2920 if (pdb_get_username(user
) == NULL
) {
2921 return NT_STATUS_INVALID_PARAMETER
;
2924 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2925 if (escape_name
== NULL
)
2926 return NT_STATUS_NO_MEMORY
;
2928 if (user
->unix_pw
) {
2929 primary_gid
= user
->unix_pw
->pw_gid
;
2931 /* retrieve the users primary gid */
2932 filter
= talloc_asprintf(mem_ctx
,
2933 "(&(objectClass=%s)(uid=%s))",
2934 LDAP_OBJ_SAMBASAMACCOUNT
,
2936 if (filter
== NULL
) {
2937 ret
= NT_STATUS_NO_MEMORY
;
2941 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2942 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2944 if (rc
!= LDAP_SUCCESS
)
2947 talloc_autofree_ldapmsg(mem_ctx
, result
);
2949 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2953 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2954 ret
= NT_STATUS_NO_SUCH_USER
;
2957 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2959 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2961 DEBUG (1, ("Unable to find the member's gid!\n"));
2962 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2965 primary_gid
= strtoul(gidstr
, NULL
, 10);
2968 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2969 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2974 filter
= talloc_asprintf(mem_ctx
,
2975 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2976 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2977 if (filter
== NULL
) {
2978 ret
= NT_STATUS_NO_MEMORY
;
2982 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2983 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2985 if (rc
!= LDAP_SUCCESS
)
2988 talloc_autofree_ldapmsg(mem_ctx
, result
);
2996 /* We need to add the primary group as the first gid/sid */
2998 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2999 ret
= NT_STATUS_NO_MEMORY
;
3003 /* This sid will be replaced later */
3005 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
3007 if (!NT_STATUS_IS_OK(ret
)) {
3011 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
3013 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
3020 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3022 str
, sizeof(str
)-1))
3025 if (!string_to_sid(&sid
, str
))
3028 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3030 str
, sizeof(str
)-1))
3033 gid
= strtoul(str
, &end
, 10);
3035 if (PTR_DIFF(end
, str
) != strlen(str
))
3038 if (gid
== primary_gid
) {
3039 sid_copy(&(*pp_sids
)[0], &sid
);
3041 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3043 ret
= NT_STATUS_NO_MEMORY
;
3046 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3048 if (!NT_STATUS_IS_OK(ret
)) {
3054 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3055 DEBUG(3, ("primary group of [%s] not found\n",
3056 pdb_get_username(user
)));
3060 *p_num_groups
= num_sids
;
3066 TALLOC_FREE(escape_name
);
3070 /**********************************************************************
3071 * Augment a posixGroup object with a sambaGroupMapping domgroup
3072 *********************************************************************/
3074 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3075 struct ldapsam_privates
*ldap_state
,
3078 const char *filter
, *dn
;
3079 LDAPMessage
*msg
, *entry
;
3083 filter
= talloc_asprintf(mem_ctx
,
3084 "(&(objectClass=%s)(gidNumber=%u))",
3085 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3086 if (filter
== NULL
) {
3087 return NT_STATUS_NO_MEMORY
;
3090 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3091 get_attr_list(mem_ctx
, groupmap_attr_list
),
3093 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3095 if ((rc
!= LDAP_SUCCESS
) ||
3096 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3097 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3098 return NT_STATUS_NO_SUCH_GROUP
;
3101 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3103 return NT_STATUS_NO_MEMORY
;
3107 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3109 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3110 sid_string_talloc(mem_ctx
, &map
->sid
));
3111 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3112 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3113 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3115 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3117 talloc_autofree_ldapmod(mem_ctx
, mods
);
3119 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3120 if (rc
!= LDAP_SUCCESS
) {
3121 return NT_STATUS_ACCESS_DENIED
;
3124 return NT_STATUS_OK
;
3127 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3130 struct ldapsam_privates
*ldap_state
=
3131 (struct ldapsam_privates
*)methods
->private_data
;
3132 LDAPMessage
*msg
= NULL
;
3133 LDAPMod
**mods
= NULL
;
3134 const char *attrs
[] = { NULL
};
3138 TALLOC_CTX
*mem_ctx
;
3145 mem_ctx
= talloc_new(NULL
);
3146 if (mem_ctx
== NULL
) {
3147 DEBUG(0, ("talloc_new failed\n"));
3148 return NT_STATUS_NO_MEMORY
;
3151 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3152 sid_string_talloc(mem_ctx
, &map
->sid
));
3153 if (filter
== NULL
) {
3154 result
= NT_STATUS_NO_MEMORY
;
3158 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3159 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3160 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3162 if ((rc
== LDAP_SUCCESS
) &&
3163 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3165 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3166 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3167 result
= NT_STATUS_GROUP_EXISTS
;
3171 switch (map
->sid_name_use
) {
3173 case SID_NAME_DOM_GRP
:
3174 /* To map a domain group we need to have a posix group
3176 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3180 case SID_NAME_ALIAS
:
3181 if (!sid_check_is_in_our_domain(&map
->sid
)
3182 && !sid_check_is_in_builtin(&map
->sid
) )
3184 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3185 sid_string_dbg(&map
->sid
)));
3186 result
= NT_STATUS_INVALID_PARAMETER
;
3192 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3193 sid_type_lookup(map
->sid_name_use
)));
3194 result
= NT_STATUS_INVALID_PARAMETER
;
3198 /* Domain groups have been mapped in a separate routine, we have to
3199 * create an alias now */
3201 if (map
->gid
== -1) {
3202 DEBUG(10, ("Refusing to map gid==-1\n"));
3203 result
= NT_STATUS_INVALID_PARAMETER
;
3207 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3208 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3209 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3210 result
= NT_STATUS_GROUP_EXISTS
;
3214 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3215 * the best we can get out of LDAP. */
3217 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3218 sid_string_talloc(mem_ctx
, &map
->sid
),
3219 lp_ldap_group_suffix());
3221 result
= NT_STATUS_NO_MEMORY
;
3227 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3228 LDAP_OBJ_SID_ENTRY
);
3229 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3231 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3232 sid_string_talloc(mem_ctx
, &map
->sid
));
3233 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3234 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3235 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3237 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3239 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3240 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3241 talloc_autofree_ldapmod(mem_ctx
, mods
);
3243 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3245 result
= (rc
== LDAP_SUCCESS
) ?
3246 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3249 TALLOC_FREE(mem_ctx
);
3253 /**********************************************************************
3254 * Update a group mapping entry. We're quite strict about what can be changed:
3255 * Only the description and displayname may be changed. It simply does not
3256 * make any sense to change the SID, gid or the type in a mapping.
3257 *********************************************************************/
3259 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3262 struct ldapsam_privates
*ldap_state
=
3263 (struct ldapsam_privates
*)methods
->private_data
;
3265 const char *filter
, *dn
;
3266 LDAPMessage
*msg
= NULL
;
3267 LDAPMessage
*entry
= NULL
;
3268 LDAPMod
**mods
= NULL
;
3269 TALLOC_CTX
*mem_ctx
;
3272 mem_ctx
= talloc_new(NULL
);
3273 if (mem_ctx
== NULL
) {
3274 DEBUG(0, ("talloc_new failed\n"));
3275 return NT_STATUS_NO_MEMORY
;
3278 /* Make 100% sure that sid, gid and type are not changed by looking up
3279 * exactly the values we're given in LDAP. */
3281 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3282 "(sambaSid=%s)(gidNumber=%u)"
3283 "(sambaGroupType=%d))",
3285 sid_string_talloc(mem_ctx
, &map
->sid
),
3286 (unsigned int)map
->gid
, map
->sid_name_use
);
3287 if (filter
== NULL
) {
3288 result
= NT_STATUS_NO_MEMORY
;
3292 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3293 get_attr_list(mem_ctx
, groupmap_attr_list
),
3295 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3297 if ((rc
!= LDAP_SUCCESS
) ||
3298 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3299 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3300 result
= NT_STATUS_NO_SUCH_GROUP
;
3304 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3307 result
= NT_STATUS_NO_MEMORY
;
3312 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3314 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3316 talloc_autofree_ldapmod(mem_ctx
, mods
);
3319 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3320 "nothing to do\n"));
3321 result
= NT_STATUS_OK
;
3325 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3327 if (rc
!= LDAP_SUCCESS
) {
3328 result
= NT_STATUS_ACCESS_DENIED
;
3332 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3333 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3335 result
= NT_STATUS_OK
;
3338 TALLOC_FREE(mem_ctx
);
3342 /**********************************************************************
3343 *********************************************************************/
3345 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3348 struct ldapsam_privates
*priv
=
3349 (struct ldapsam_privates
*)methods
->private_data
;
3350 LDAPMessage
*msg
, *entry
;
3353 TALLOC_CTX
*mem_ctx
;
3356 mem_ctx
= talloc_new(NULL
);
3357 if (mem_ctx
== NULL
) {
3358 DEBUG(0, ("talloc_new failed\n"));
3359 return NT_STATUS_NO_MEMORY
;
3362 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3363 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3364 sid_string_talloc(mem_ctx
, &sid
));
3365 if (filter
== NULL
) {
3366 result
= NT_STATUS_NO_MEMORY
;
3369 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3370 get_attr_list(mem_ctx
, groupmap_attr_list
),
3372 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3374 if ((rc
!= LDAP_SUCCESS
) ||
3375 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3376 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3377 result
= NT_STATUS_NO_SUCH_GROUP
;
3381 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3382 get_attr_list(mem_ctx
,
3383 groupmap_attr_list_to_delete
));
3385 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3386 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3387 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3388 const char *attrs
[] = { "sambaGroupType", "description",
3389 "displayName", "sambaSIDList",
3392 /* Second try. Don't delete the sambaSID attribute, this is
3393 for "old" entries that are tacked on a winbind
3396 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3397 LDAP_OBJ_GROUPMAP
, attrs
);
3400 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3401 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3402 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3403 const char *attrs
[] = { "sambaGroupType", "description",
3404 "displayName", "sambaSIDList",
3405 "gidNumber", NULL
};
3407 /* Third try. This is a post-3.0.21 alias (containing only
3408 * sambaSidEntry and sambaGroupMapping classes), we also have
3409 * to delete the gidNumber attribute, only the sambaSidEntry
3412 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3413 LDAP_OBJ_GROUPMAP
, attrs
);
3416 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3419 TALLOC_FREE(mem_ctx
);
3423 /**********************************************************************
3424 *********************************************************************/
3426 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3429 struct ldapsam_privates
*ldap_state
=
3430 (struct ldapsam_privates
*)my_methods
->private_data
;
3431 char *filter
= NULL
;
3433 const char **attr_list
;
3435 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3437 return NT_STATUS_NO_MEMORY
;
3439 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3440 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3441 LDAP_SCOPE_SUBTREE
, filter
,
3442 attr_list
, 0, &ldap_state
->result
);
3443 TALLOC_FREE(attr_list
);
3445 if (rc
!= LDAP_SUCCESS
) {
3446 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3447 ldap_err2string(rc
)));
3448 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3449 lp_ldap_suffix(), filter
));
3450 ldap_msgfree(ldap_state
->result
);
3451 ldap_state
->result
= NULL
;
3452 TALLOC_FREE(filter
);
3453 return NT_STATUS_UNSUCCESSFUL
;
3456 TALLOC_FREE(filter
);
3458 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3459 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3460 ldap_state
->result
)));
3463 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3464 ldap_state
->result
);
3465 ldap_state
->index
= 0;
3467 return NT_STATUS_OK
;
3470 /**********************************************************************
3471 *********************************************************************/
3473 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3475 ldapsam_endsampwent(my_methods
);
3478 /**********************************************************************
3479 *********************************************************************/
3481 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3484 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3485 struct ldapsam_privates
*ldap_state
=
3486 (struct ldapsam_privates
*)my_methods
->private_data
;
3490 if (!ldap_state
->entry
)
3493 ldap_state
->index
++;
3494 bret
= init_group_from_ldap(ldap_state
, map
,
3498 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3502 return NT_STATUS_OK
;
3505 /**********************************************************************
3506 *********************************************************************/
3508 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3509 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3510 GROUP_MAP
**pp_rmap
,
3511 size_t *p_num_entries
,
3520 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3521 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3523 return NT_STATUS_ACCESS_DENIED
;
3526 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3527 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3528 sid_name_use
!= map
.sid_name_use
) {
3529 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3530 "not of the requested type\n", map
.nt_name
));
3533 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3534 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3535 "non mapped\n", map
.nt_name
));
3539 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3541 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3542 "enlarge group map!\n"));
3543 return NT_STATUS_UNSUCCESSFUL
;
3546 (*pp_rmap
)[entries
] = map
;
3551 ldapsam_endsamgrent(methods
);
3553 *p_num_entries
= entries
;
3555 return NT_STATUS_OK
;
3558 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3559 const struct dom_sid
*alias
,
3560 const struct dom_sid
*member
,
3563 struct ldapsam_privates
*ldap_state
=
3564 (struct ldapsam_privates
*)methods
->private_data
;
3566 LDAPMessage
*result
= NULL
;
3567 LDAPMessage
*entry
= NULL
;
3569 LDAPMod
**mods
= NULL
;
3571 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3574 char *filter
= NULL
;
3576 if (sid_check_is_in_builtin(alias
)) {
3577 type
= SID_NAME_ALIAS
;
3580 if (sid_check_is_in_our_domain(alias
)) {
3581 type
= SID_NAME_ALIAS
;
3584 if (type
== SID_NAME_USE_NONE
) {
3585 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3586 sid_string_dbg(alias
)));
3587 return NT_STATUS_NO_SUCH_ALIAS
;
3590 if (asprintf(&filter
,
3591 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3592 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3594 return NT_STATUS_NO_MEMORY
;
3597 if (ldapsam_search_one_group(ldap_state
, filter
,
3598 &result
) != LDAP_SUCCESS
) {
3600 return NT_STATUS_NO_SUCH_ALIAS
;
3603 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3607 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3608 ldap_msgfree(result
);
3610 return NT_STATUS_NO_SUCH_ALIAS
;
3614 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3615 "filter %s: count=%d\n", filter
, count
));
3616 ldap_msgfree(result
);
3618 return NT_STATUS_NO_SUCH_ALIAS
;
3623 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3627 ldap_msgfree(result
);
3628 return NT_STATUS_UNSUCCESSFUL
;
3631 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3633 ldap_msgfree(result
);
3634 return NT_STATUS_UNSUCCESSFUL
;
3637 smbldap_set_mod(&mods
, modop
,
3638 get_attr_key2string(groupmap_attr_list
,
3639 LDAP_ATTR_SID_LIST
),
3640 sid_to_fstring(tmp
, member
));
3642 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3644 ldap_mods_free(mods
, True
);
3645 ldap_msgfree(result
);
3648 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3649 return NT_STATUS_MEMBER_IN_ALIAS
;
3652 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3653 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3656 if (rc
!= LDAP_SUCCESS
) {
3657 return NT_STATUS_UNSUCCESSFUL
;
3660 return NT_STATUS_OK
;
3663 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3664 const struct dom_sid
*alias
,
3665 const struct dom_sid
*member
)
3667 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3670 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3671 const struct dom_sid
*alias
,
3672 const struct dom_sid
*member
)
3674 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3678 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3679 const struct dom_sid
*alias
,
3680 TALLOC_CTX
*mem_ctx
,
3681 struct dom_sid
**pp_members
,
3682 size_t *p_num_members
)
3684 struct ldapsam_privates
*ldap_state
=
3685 (struct ldapsam_privates
*)methods
->private_data
;
3686 LDAPMessage
*result
= NULL
;
3687 LDAPMessage
*entry
= NULL
;
3689 char **values
= NULL
;
3691 char *filter
= NULL
;
3692 size_t num_members
= 0;
3693 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3699 if (sid_check_is_in_builtin(alias
)) {
3700 type
= SID_NAME_ALIAS
;
3703 if (sid_check_is_in_our_domain(alias
)) {
3704 type
= SID_NAME_ALIAS
;
3707 if (type
== SID_NAME_USE_NONE
) {
3708 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3709 sid_string_dbg(alias
)));
3710 return NT_STATUS_NO_SUCH_ALIAS
;
3713 if (asprintf(&filter
,
3714 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3715 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3717 return NT_STATUS_NO_MEMORY
;
3720 if (ldapsam_search_one_group(ldap_state
, filter
,
3721 &result
) != LDAP_SUCCESS
) {
3723 return NT_STATUS_NO_SUCH_ALIAS
;
3726 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3730 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3731 ldap_msgfree(result
);
3733 return NT_STATUS_NO_SUCH_ALIAS
;
3737 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3738 "filter %s: count=%d\n", filter
, count
));
3739 ldap_msgfree(result
);
3741 return NT_STATUS_NO_SUCH_ALIAS
;
3746 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3750 ldap_msgfree(result
);
3751 return NT_STATUS_UNSUCCESSFUL
;
3754 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3756 get_attr_key2string(groupmap_attr_list
,
3757 LDAP_ATTR_SID_LIST
));
3759 if (values
== NULL
) {
3760 ldap_msgfree(result
);
3761 return NT_STATUS_OK
;
3764 count
= ldap_count_values(values
);
3766 for (i
=0; i
<count
; i
++) {
3767 struct dom_sid member
;
3770 if (!string_to_sid(&member
, values
[i
]))
3773 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3775 if (!NT_STATUS_IS_OK(status
)) {
3776 ldap_value_free(values
);
3777 ldap_msgfree(result
);
3782 *p_num_members
= num_members
;
3783 ldap_value_free(values
);
3784 ldap_msgfree(result
);
3786 return NT_STATUS_OK
;
3789 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3790 TALLOC_CTX
*mem_ctx
,
3791 const struct dom_sid
*domain_sid
,
3792 const struct dom_sid
*members
,
3794 uint32_t **pp_alias_rids
,
3795 size_t *p_num_alias_rids
)
3797 struct ldapsam_privates
*ldap_state
=
3798 (struct ldapsam_privates
*)methods
->private_data
;
3801 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3803 LDAPMessage
*result
= NULL
;
3804 LDAPMessage
*entry
= NULL
;
3808 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3809 bool is_builtin
= false;
3810 bool sid_added
= false;
3812 *pp_alias_rids
= NULL
;
3813 *p_num_alias_rids
= 0;
3815 if (sid_check_is_builtin(domain_sid
)) {
3817 type
= SID_NAME_ALIAS
;
3820 if (sid_check_is_domain(domain_sid
)) {
3821 type
= SID_NAME_ALIAS
;
3824 if (type
== SID_NAME_USE_NONE
) {
3825 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3826 sid_string_dbg(domain_sid
)));
3827 return NT_STATUS_UNSUCCESSFUL
;
3830 if (num_members
== 0) {
3831 return NT_STATUS_OK
;
3834 filter
= talloc_asprintf(mem_ctx
,
3835 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3836 LDAP_OBJ_GROUPMAP
, type
);
3838 for (i
=0; i
<num_members
; i
++)
3839 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3841 sid_string_talloc(mem_ctx
,
3844 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3846 if (filter
== NULL
) {
3847 return NT_STATUS_NO_MEMORY
;
3851 ldap_state
->search_cache
.filter
&&
3852 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3853 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3854 result
= ldap_state
->search_cache
.result
;
3855 ldap_state
->search_cache
.result
= NULL
;
3857 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3858 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3859 if (rc
!= LDAP_SUCCESS
) {
3860 return NT_STATUS_UNSUCCESSFUL
;
3862 talloc_autofree_ldapmsg(filter
, result
);
3865 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3867 for (entry
= ldap_first_entry(ldap_struct
, result
);
3869 entry
= ldap_next_entry(ldap_struct
, entry
))
3875 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3881 if (!string_to_sid(&sid
, sid_str
))
3884 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3889 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3890 p_num_alias_rids
)) {
3891 return NT_STATUS_NO_MEMORY
;
3895 if (!is_builtin
&& !sid_added
) {
3896 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3898 * Note: result is a talloc child of filter because of the
3899 * talloc_autofree_ldapmsg() usage
3901 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3902 ldap_state
->search_cache
.result
= result
;
3905 return NT_STATUS_OK
;
3908 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3909 enum pdb_policy_type type
,
3912 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3914 LDAPMod
**mods
= NULL
;
3915 fstring value_string
;
3916 const char *policy_attr
= NULL
;
3918 struct ldapsam_privates
*ldap_state
=
3919 (struct ldapsam_privates
*)methods
->private_data
;
3921 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3923 if (!ldap_state
->domain_dn
) {
3924 return NT_STATUS_INVALID_PARAMETER
;
3927 policy_attr
= get_account_policy_attr(type
);
3928 if (policy_attr
== NULL
) {
3929 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3934 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3936 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3938 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3941 ldap_mods_free(mods
, True
);
3943 if (rc
!= LDAP_SUCCESS
) {
3947 if (!cache_account_policy_set(type
, value
)) {
3948 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3949 "update local tdb cache\n"));
3953 return NT_STATUS_OK
;
3956 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3957 enum pdb_policy_type type
,
3960 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3964 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3965 enum pdb_policy_type type
,
3968 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3969 LDAPMessage
*result
= NULL
;
3970 LDAPMessage
*entry
= NULL
;
3975 const char *policy_attr
= NULL
;
3977 struct ldapsam_privates
*ldap_state
=
3978 (struct ldapsam_privates
*)methods
->private_data
;
3980 const char *attrs
[2];
3982 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3984 if (!ldap_state
->domain_dn
) {
3985 return NT_STATUS_INVALID_PARAMETER
;
3988 policy_attr
= get_account_policy_attr(type
);
3990 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3991 "policy index: %d\n", type
));
3995 attrs
[0] = policy_attr
;
3998 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3999 if (filter
== NULL
) {
4000 return NT_STATUS_NO_MEMORY
;
4002 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
4003 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
4005 TALLOC_FREE(filter
);
4006 if (rc
!= LDAP_SUCCESS
) {
4010 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4015 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4016 if (entry
== NULL
) {
4020 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4025 *value
= (uint32_t)atol(vals
[0]);
4027 ntstatus
= NT_STATUS_OK
;
4031 ldap_value_free(vals
);
4032 ldap_msgfree(result
);
4037 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4039 - if user hasn't decided to use account policies inside LDAP just reuse the
4042 - if there is a valid cache entry, return that
4043 - if there is an LDAP entry, update cache and return
4044 - otherwise set to default, update cache and return
4048 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4049 enum pdb_policy_type type
,
4052 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4054 if (cache_account_policy_get(type
, value
)) {
4055 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4057 return NT_STATUS_OK
;
4060 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4062 if (NT_STATUS_IS_OK(ntstatus
)) {
4066 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4070 /* should we automagically migrate old tdb value here ? */
4071 if (account_policy_get(type
, value
))
4074 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4075 "default\n", type
));
4078 if (!account_policy_get_default(type
, value
)) {
4084 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4085 if (!NT_STATUS_IS_OK(ntstatus
)) {
4091 if (!cache_account_policy_set(type
, *value
)) {
4092 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4093 "tdb as a cache\n"));
4094 return NT_STATUS_UNSUCCESSFUL
;
4097 return NT_STATUS_OK
;
4100 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4101 const struct dom_sid
*domain_sid
,
4105 enum lsa_SidType
*attrs
)
4107 struct ldapsam_privates
*ldap_state
=
4108 (struct ldapsam_privates
*)methods
->private_data
;
4109 LDAPMessage
*msg
= NULL
;
4111 char *allsids
= NULL
;
4112 int i
, rc
, num_mapped
;
4113 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4114 TALLOC_CTX
*mem_ctx
;
4118 mem_ctx
= talloc_new(NULL
);
4119 if (mem_ctx
== NULL
) {
4120 DEBUG(0, ("talloc_new failed\n"));
4124 if (!sid_check_is_builtin(domain_sid
) &&
4125 !sid_check_is_domain(domain_sid
)) {
4126 result
= NT_STATUS_INVALID_PARAMETER
;
4130 if (num_rids
== 0) {
4131 result
= NT_STATUS_NONE_MAPPED
;
4135 for (i
=0; i
<num_rids
; i
++)
4136 attrs
[i
] = SID_NAME_UNKNOWN
;
4138 allsids
= talloc_strdup(mem_ctx
, "");
4139 if (allsids
== NULL
) {
4143 for (i
=0; i
<num_rids
; i
++) {
4145 sid_compose(&sid
, domain_sid
, rids
[i
]);
4146 allsids
= talloc_asprintf_append_buffer(
4147 allsids
, "(sambaSid=%s)",
4148 sid_string_talloc(mem_ctx
, &sid
));
4149 if (allsids
== NULL
) {
4154 /* First look for users */
4158 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4160 filter
= talloc_asprintf(
4161 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4162 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4164 if (filter
== NULL
) {
4168 rc
= smbldap_search(ldap_state
->smbldap_state
,
4169 lp_ldap_user_suffix(),
4170 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4172 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4175 if (rc
!= LDAP_SUCCESS
)
4178 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4181 for (entry
= ldap_first_entry(ld
, msg
);
4183 entry
= ldap_next_entry(ld
, entry
)) {
4188 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4190 DEBUG(2, ("Could not find sid from ldap entry\n"));
4194 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4197 DEBUG(2, ("Could not retrieve uid attribute\n"));
4201 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4202 if (rid
== rids
[rid_index
])
4206 if (rid_index
== num_rids
) {
4207 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4211 attrs
[rid_index
] = SID_NAME_USER
;
4212 names
[rid_index
] = name
;
4216 if (num_mapped
== num_rids
) {
4217 /* No need to look for groups anymore -- we're done */
4218 result
= NT_STATUS_OK
;
4222 /* Same game for groups */
4226 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4227 "sambaGroupType", NULL
};
4229 filter
= talloc_asprintf(
4230 mem_ctx
, "(&(objectClass=%s)(|%s))",
4231 LDAP_OBJ_GROUPMAP
, allsids
);
4232 if (filter
== NULL
) {
4236 rc
= smbldap_search(ldap_state
->smbldap_state
,
4238 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4240 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4243 if (rc
!= LDAP_SUCCESS
)
4246 /* ldap_struct might have changed due to a reconnect */
4248 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4250 /* For consistency checks, we already checked we're only domain or builtin */
4252 is_builtin
= sid_check_is_builtin(domain_sid
);
4254 for (entry
= ldap_first_entry(ld
, msg
);
4256 entry
= ldap_next_entry(ld
, entry
))
4261 enum lsa_SidType type
;
4262 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4264 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4267 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4272 type
= (enum lsa_SidType
)atol(attr
);
4274 /* Consistency checks */
4275 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4276 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4277 (type
!= SID_NAME_DOM_GRP
)))) {
4278 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4281 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4283 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4287 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4290 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4292 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4296 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4301 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4302 if (rid
== rids
[rid_index
])
4306 if (rid_index
== num_rids
) {
4307 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4311 attrs
[rid_index
] = type
;
4312 names
[rid_index
] = attr
;
4316 result
= NT_STATUS_NONE_MAPPED
;
4319 result
= (num_mapped
== num_rids
) ?
4320 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4322 TALLOC_FREE(mem_ctx
);
4326 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4328 char *filter
= NULL
;
4329 char *escaped
= NULL
;
4330 char *result
= NULL
;
4332 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4333 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4337 escaped
= escape_ldap_string(talloc_tos(), username
);
4338 if (escaped
== NULL
) goto done
;
4340 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4344 TALLOC_FREE(escaped
);
4349 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4353 const char **result
;
4355 va_start(ap
, mem_ctx
);
4356 while (va_arg(ap
, const char *) != NULL
)
4360 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4364 va_start(ap
, mem_ctx
);
4365 for (i
=0; i
<num
; i
++) {
4366 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4367 if (result
[i
] == NULL
) {
4368 talloc_free(result
);
4379 struct ldap_search_state
{
4380 struct smbldap_state
*connection
;
4382 uint32_t acct_flags
;
4383 uint16_t group_type
;
4390 void *pagedresults_cookie
;
4392 LDAPMessage
*entries
, *current_entry
;
4393 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4394 TALLOC_CTX
*mem_ctx
,
4395 LDAP
*ld
, LDAPMessage
*entry
,
4396 struct samr_displayentry
*result
);
4399 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4401 struct ldap_search_state
*state
=
4402 (struct ldap_search_state
*)search
->private_data
;
4404 int rc
= LDAP_OPERATIONS_ERROR
;
4406 state
->entries
= NULL
;
4408 if (state
->connection
->paged_results
) {
4409 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4410 state
->scope
, state
->filter
,
4411 state
->attrs
, state
->attrsonly
,
4412 lp_ldap_page_size(), &state
->entries
,
4413 &state
->pagedresults_cookie
);
4416 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4418 if (state
->entries
!= NULL
) {
4419 /* Left over from unsuccessful paged attempt */
4420 ldap_msgfree(state
->entries
);
4421 state
->entries
= NULL
;
4424 rc
= smbldap_search(state
->connection
, state
->base
,
4425 state
->scope
, state
->filter
, state
->attrs
,
4426 state
->attrsonly
, &state
->entries
);
4428 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4431 /* Ok, the server was lying. It told us it could do paged
4432 * searches when it could not. */
4433 state
->connection
->paged_results
= False
;
4436 ld
= state
->connection
->ldap_struct
;
4438 DEBUG(5, ("Don't have an LDAP connection right after a "
4442 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4447 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4449 struct ldap_search_state
*state
=
4450 (struct ldap_search_state
*)search
->private_data
;
4453 if (!state
->connection
->paged_results
) {
4454 /* There is no next page when there are no paged results */
4458 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4459 state
->scope
, state
->filter
, state
->attrs
,
4460 state
->attrsonly
, lp_ldap_page_size(),
4462 &state
->pagedresults_cookie
);
4464 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4467 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4469 if (state
->current_entry
== NULL
) {
4470 ldap_msgfree(state
->entries
);
4471 state
->entries
= NULL
;
4478 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4479 struct samr_displayentry
*entry
)
4481 struct ldap_search_state
*state
=
4482 (struct ldap_search_state
*)search
->private_data
;
4486 if (state
->current_entry
== NULL
) {
4490 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4493 if ((state
->entries
== NULL
) &&
4494 !ldapsam_search_nextpage(search
))
4497 result
= state
->ldap2displayentry(state
, search
,
4498 state
->connection
->ldap_struct
,
4499 state
->current_entry
, entry
);
4503 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4504 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4505 if (dn
!= NULL
) ldap_memfree(dn
);
4508 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4510 if (state
->current_entry
== NULL
) {
4511 ldap_msgfree(state
->entries
);
4512 state
->entries
= NULL
;
4515 if (!result
) goto retry
;
4520 static void ldapsam_search_end(struct pdb_search
*search
)
4522 struct ldap_search_state
*state
=
4523 (struct ldap_search_state
*)search
->private_data
;
4526 if (state
->pagedresults_cookie
== NULL
)
4529 if (state
->entries
!= NULL
)
4530 ldap_msgfree(state
->entries
);
4532 state
->entries
= NULL
;
4533 state
->current_entry
= NULL
;
4535 if (!state
->connection
->paged_results
)
4538 /* Tell the LDAP server we're not interested in the rest anymore. */
4540 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4541 state
->filter
, state
->attrs
,
4542 state
->attrsonly
, 0, &state
->entries
,
4543 &state
->pagedresults_cookie
);
4545 if (rc
!= LDAP_SUCCESS
)
4546 DEBUG(5, ("Could not end search properly\n"));
4551 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4552 TALLOC_CTX
*mem_ctx
,
4553 LDAP
*ld
, LDAPMessage
*entry
,
4554 struct samr_displayentry
*result
)
4557 size_t converted_size
;
4559 uint32_t acct_flags
;
4561 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4562 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4563 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4566 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4567 ldap_value_free(vals
);
4569 if ((state
->acct_flags
!= 0) &&
4570 ((state
->acct_flags
& acct_flags
) == 0))
4573 result
->acct_flags
= acct_flags
;
4574 result
->account_name
= "";
4575 result
->fullname
= "";
4576 result
->description
= "";
4578 vals
= ldap_get_values(ld
, entry
, "uid");
4579 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4580 DEBUG(5, ("\"uid\" not found\n"));
4583 if (!pull_utf8_talloc(mem_ctx
,
4584 CONST_DISCARD(char **, &result
->account_name
),
4585 vals
[0], &converted_size
))
4587 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4591 ldap_value_free(vals
);
4593 vals
= ldap_get_values(ld
, entry
, "displayName");
4594 if ((vals
== NULL
) || (vals
[0] == NULL
))
4595 DEBUG(8, ("\"displayName\" not found\n"));
4596 else if (!pull_utf8_talloc(mem_ctx
,
4597 CONST_DISCARD(char **, &result
->fullname
),
4598 vals
[0], &converted_size
))
4600 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4604 ldap_value_free(vals
);
4606 vals
= ldap_get_values(ld
, entry
, "description");
4607 if ((vals
== NULL
) || (vals
[0] == NULL
))
4608 DEBUG(8, ("\"description\" not found\n"));
4609 else if (!pull_utf8_talloc(mem_ctx
,
4610 CONST_DISCARD(char **, &result
->description
),
4611 vals
[0], &converted_size
))
4613 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4617 ldap_value_free(vals
);
4619 if ((result
->account_name
== NULL
) ||
4620 (result
->fullname
== NULL
) ||
4621 (result
->description
== NULL
)) {
4622 DEBUG(0, ("talloc failed\n"));
4626 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4627 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4628 DEBUG(0, ("\"objectSid\" not found\n"));
4632 if (!string_to_sid(&sid
, vals
[0])) {
4633 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4634 ldap_value_free(vals
);
4637 ldap_value_free(vals
);
4639 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4640 DEBUG(0, ("sid %s does not belong to our domain\n",
4641 sid_string_dbg(&sid
)));
4649 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4650 struct pdb_search
*search
,
4651 uint32_t acct_flags
)
4653 struct ldapsam_privates
*ldap_state
=
4654 (struct ldapsam_privates
*)methods
->private_data
;
4655 struct ldap_search_state
*state
;
4657 state
= talloc(search
, struct ldap_search_state
);
4658 if (state
== NULL
) {
4659 DEBUG(0, ("talloc failed\n"));
4663 state
->connection
= ldap_state
->smbldap_state
;
4665 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4666 state
->base
= lp_ldap_user_suffix();
4667 else if ((acct_flags
!= 0) &&
4668 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4669 state
->base
= lp_ldap_machine_suffix();
4671 state
->base
= lp_ldap_suffix();
4673 state
->acct_flags
= acct_flags
;
4674 state
->base
= talloc_strdup(search
, state
->base
);
4675 state
->scope
= LDAP_SCOPE_SUBTREE
;
4676 state
->filter
= get_ldap_filter(search
, "*");
4677 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4678 "displayName", "description",
4679 "sambaAcctFlags", NULL
);
4680 state
->attrsonly
= 0;
4681 state
->pagedresults_cookie
= NULL
;
4682 state
->entries
= NULL
;
4683 state
->ldap2displayentry
= ldapuser2displayentry
;
4685 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4686 DEBUG(0, ("talloc failed\n"));
4690 search
->private_data
= state
;
4691 search
->next_entry
= ldapsam_search_next_entry
;
4692 search
->search_end
= ldapsam_search_end
;
4694 return ldapsam_search_firstpage(search
);
4697 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4698 TALLOC_CTX
*mem_ctx
,
4699 LDAP
*ld
, LDAPMessage
*entry
,
4700 struct samr_displayentry
*result
)
4703 size_t converted_size
;
4705 uint16_t group_type
;
4707 result
->account_name
= "";
4708 result
->fullname
= "";
4709 result
->description
= "";
4712 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4713 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4714 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4716 ldap_value_free(vals
);
4721 group_type
= atoi(vals
[0]);
4723 if ((state
->group_type
!= 0) &&
4724 ((state
->group_type
!= group_type
))) {
4725 ldap_value_free(vals
);
4729 ldap_value_free(vals
);
4731 /* display name is the NT group name */
4733 vals
= ldap_get_values(ld
, entry
, "displayName");
4734 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4735 DEBUG(8, ("\"displayName\" not found\n"));
4737 /* fallback to the 'cn' attribute */
4738 vals
= ldap_get_values(ld
, entry
, "cn");
4739 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4740 DEBUG(5, ("\"cn\" not found\n"));
4743 if (!pull_utf8_talloc(mem_ctx
,
4744 CONST_DISCARD(char **,
4745 &result
->account_name
),
4746 vals
[0], &converted_size
))
4748 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4749 "failed: %s", strerror(errno
)));
4752 else if (!pull_utf8_talloc(mem_ctx
,
4753 CONST_DISCARD(char **,
4754 &result
->account_name
),
4755 vals
[0], &converted_size
))
4757 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4761 ldap_value_free(vals
);
4763 vals
= ldap_get_values(ld
, entry
, "description");
4764 if ((vals
== NULL
) || (vals
[0] == NULL
))
4765 DEBUG(8, ("\"description\" not found\n"));
4766 else if (!pull_utf8_talloc(mem_ctx
,
4767 CONST_DISCARD(char **, &result
->description
),
4768 vals
[0], &converted_size
))
4770 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4773 ldap_value_free(vals
);
4775 if ((result
->account_name
== NULL
) ||
4776 (result
->fullname
== NULL
) ||
4777 (result
->description
== NULL
)) {
4778 DEBUG(0, ("talloc failed\n"));
4782 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4783 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4784 DEBUG(0, ("\"objectSid\" not found\n"));
4786 ldap_value_free(vals
);
4791 if (!string_to_sid(&sid
, vals
[0])) {
4792 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4796 ldap_value_free(vals
);
4798 switch (group_type
) {
4799 case SID_NAME_DOM_GRP
:
4800 case SID_NAME_ALIAS
:
4802 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4803 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4805 DEBUG(0, ("%s is not in our domain\n",
4806 sid_string_dbg(&sid
)));
4812 DEBUG(0,("unkown group type: %d\n", group_type
));
4816 result
->acct_flags
= 0;
4821 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4822 struct pdb_search
*search
,
4823 const struct dom_sid
*sid
,
4824 enum lsa_SidType type
)
4826 struct ldapsam_privates
*ldap_state
=
4827 (struct ldapsam_privates
*)methods
->private_data
;
4828 struct ldap_search_state
*state
;
4831 state
= talloc(search
, struct ldap_search_state
);
4832 if (state
== NULL
) {
4833 DEBUG(0, ("talloc failed\n"));
4837 state
->connection
= ldap_state
->smbldap_state
;
4839 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4840 state
->connection
= ldap_state
->smbldap_state
;
4841 state
->scope
= LDAP_SCOPE_SUBTREE
;
4842 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4843 "(sambaGroupType=%d)(sambaSID=%s*))",
4845 type
, sid_to_fstring(tmp
, sid
));
4846 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4847 "displayName", "description",
4848 "sambaGroupType", NULL
);
4849 state
->attrsonly
= 0;
4850 state
->pagedresults_cookie
= NULL
;
4851 state
->entries
= NULL
;
4852 state
->group_type
= type
;
4853 state
->ldap2displayentry
= ldapgroup2displayentry
;
4855 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4856 DEBUG(0, ("talloc failed\n"));
4860 search
->private_data
= state
;
4861 search
->next_entry
= ldapsam_search_next_entry
;
4862 search
->search_end
= ldapsam_search_end
;
4864 return ldapsam_search_firstpage(search
);
4867 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4868 struct pdb_search
*search
)
4870 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4873 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4874 struct pdb_search
*search
,
4875 const struct dom_sid
*sid
)
4877 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4880 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4882 return PDB_CAP_STORE_RIDS
;
4885 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4888 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4890 LDAPMessage
*result
= NULL
;
4891 LDAPMessage
*entry
= NULL
;
4892 LDAPMod
**mods
= NULL
;
4896 uint32_t nextRid
= 0;
4899 TALLOC_CTX
*mem_ctx
;
4901 mem_ctx
= talloc_new(NULL
);
4902 if (mem_ctx
== NULL
) {
4903 DEBUG(0, ("talloc_new failed\n"));
4904 return NT_STATUS_NO_MEMORY
;
4907 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4908 get_global_sam_name(), False
);
4909 if (!NT_STATUS_IS_OK(status
)) {
4910 DEBUG(3, ("Could not get domain info: %s\n",
4911 nt_errstr(status
)));
4915 talloc_autofree_ldapmsg(mem_ctx
, result
);
4917 entry
= ldap_first_entry(priv2ld(priv
), result
);
4918 if (entry
== NULL
) {
4919 DEBUG(0, ("Could not get domain info entry\n"));
4920 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4924 /* Find the largest of the three attributes "sambaNextRid",
4925 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4926 concept of differentiating between user and group rids, and will
4927 use only "sambaNextRid" in the future. But for compatibility
4928 reasons I look if others have chosen different strategies -- VL */
4930 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4931 "sambaNextRid", mem_ctx
);
4932 if (value
!= NULL
) {
4933 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4934 nextRid
= MAX(nextRid
, tmp
);
4937 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4938 "sambaNextUserRid", mem_ctx
);
4939 if (value
!= NULL
) {
4940 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4941 nextRid
= MAX(nextRid
, tmp
);
4944 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4945 "sambaNextGroupRid", mem_ctx
);
4946 if (value
!= NULL
) {
4947 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4948 nextRid
= MAX(nextRid
, tmp
);
4952 nextRid
= BASE_RID
-1;
4957 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4958 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4959 talloc_autofree_ldapmod(mem_ctx
, mods
);
4961 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4962 status
= NT_STATUS_NO_MEMORY
;
4966 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4968 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4971 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4974 if (NT_STATUS_IS_OK(status
)) {
4978 TALLOC_FREE(mem_ctx
);
4982 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4986 for (i
=0; i
<10; i
++) {
4987 NTSTATUS result
= ldapsam_get_new_rid(
4988 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4989 if (NT_STATUS_IS_OK(result
)) {
4993 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4997 /* The ldap update failed (maybe a race condition), retry */
5000 /* Tried 10 times, fail. */
5001 return NT_STATUS_ACCESS_DENIED
;
5004 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
5006 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
5007 return NT_STATUS_IS_OK(result
) ? True
: False
;
5010 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
5011 const struct dom_sid
*sid
,
5012 union unid_t
*id
, enum lsa_SidType
*type
)
5014 struct ldapsam_privates
*priv
=
5015 (struct ldapsam_privates
*)methods
->private_data
;
5017 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5019 LDAPMessage
*result
= NULL
;
5020 LDAPMessage
*entry
= NULL
;
5025 TALLOC_CTX
*mem_ctx
;
5027 mem_ctx
= talloc_new(NULL
);
5028 if (mem_ctx
== NULL
) {
5029 DEBUG(0, ("talloc_new failed\n"));
5033 filter
= talloc_asprintf(mem_ctx
,
5035 "(|(objectClass=%s)(objectClass=%s)))",
5036 sid_string_talloc(mem_ctx
, sid
),
5037 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5038 if (filter
== NULL
) {
5039 DEBUG(5, ("talloc_asprintf failed\n"));
5043 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5045 if (rc
!= LDAP_SUCCESS
) {
5048 talloc_autofree_ldapmsg(mem_ctx
, result
);
5050 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5051 DEBUG(10, ("Got %d entries, expected one\n",
5052 ldap_count_entries(priv2ld(priv
), result
)));
5056 entry
= ldap_first_entry(priv2ld(priv
), result
);
5058 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5059 "sambaGroupType", mem_ctx
);
5061 if (value
!= NULL
) {
5062 const char *gid_str
;
5065 gid_str
= smbldap_talloc_single_attribute(
5066 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5067 if (gid_str
== NULL
) {
5068 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5069 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5074 id
->gid
= strtoul(gid_str
, NULL
, 10);
5075 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
5076 store_gid_sid_cache(sid
, id
->gid
);
5077 idmap_cache_set_sid2gid(sid
, id
->gid
);
5082 /* It must be a user */
5084 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5085 "uidNumber", mem_ctx
);
5086 if (value
== NULL
) {
5087 DEBUG(1, ("Could not find uidNumber in %s\n",
5088 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5092 id
->uid
= strtoul(value
, NULL
, 10);
5093 *type
= SID_NAME_USER
;
5094 store_uid_sid_cache(sid
, id
->uid
);
5095 idmap_cache_set_sid2uid(sid
, id
->uid
);
5099 TALLOC_FREE(mem_ctx
);
5104 * Find the SID for a uid.
5105 * This is shortcut is only used if ldapsam:trusted is set to true.
5107 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5108 struct dom_sid
*sid
)
5110 struct ldapsam_privates
*priv
=
5111 (struct ldapsam_privates
*)methods
->private_data
;
5113 const char *attrs
[] = { "sambaSID", NULL
};
5114 LDAPMessage
*result
= NULL
;
5115 LDAPMessage
*entry
= NULL
;
5117 char *user_sid_string
;
5118 struct dom_sid user_sid
;
5120 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5122 filter
= talloc_asprintf(tmp_ctx
,
5125 "(objectClass=%s))",
5127 LDAP_OBJ_POSIXACCOUNT
,
5128 LDAP_OBJ_SAMBASAMACCOUNT
);
5129 if (filter
== NULL
) {
5130 DEBUG(3, ("talloc_asprintf failed\n"));
5134 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5135 if (rc
!= LDAP_SUCCESS
) {
5138 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5140 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5141 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5142 ldap_count_entries(priv2ld(priv
), result
),
5143 (unsigned int)uid
));
5147 entry
= ldap_first_entry(priv2ld(priv
), result
);
5149 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5150 "sambaSID", tmp_ctx
);
5151 if (user_sid_string
== NULL
) {
5152 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5153 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5157 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5158 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5163 sid_copy(sid
, &user_sid
);
5165 store_uid_sid_cache(sid
, uid
);
5166 idmap_cache_set_sid2uid(sid
, uid
);
5171 TALLOC_FREE(tmp_ctx
);
5176 * Find the SID for a gid.
5177 * This is shortcut is only used if ldapsam:trusted is set to true.
5179 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5180 struct dom_sid
*sid
)
5182 struct ldapsam_privates
*priv
=
5183 (struct ldapsam_privates
*)methods
->private_data
;
5185 const char *attrs
[] = { "sambaSID", NULL
};
5186 LDAPMessage
*result
= NULL
;
5187 LDAPMessage
*entry
= NULL
;
5189 char *group_sid_string
;
5190 struct dom_sid group_sid
;
5192 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5194 filter
= talloc_asprintf(tmp_ctx
,
5196 "(objectClass=%s))",
5199 if (filter
== NULL
) {
5200 DEBUG(3, ("talloc_asprintf failed\n"));
5204 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5205 if (rc
!= LDAP_SUCCESS
) {
5208 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5210 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5211 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5212 ldap_count_entries(priv2ld(priv
), result
),
5213 (unsigned int)gid
));
5217 entry
= ldap_first_entry(priv2ld(priv
), result
);
5219 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5220 "sambaSID", tmp_ctx
);
5221 if (group_sid_string
== NULL
) {
5222 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5223 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5227 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5228 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5233 sid_copy(sid
, &group_sid
);
5235 store_gid_sid_cache(sid
, gid
);
5236 idmap_cache_set_sid2gid(sid
, gid
);
5241 TALLOC_FREE(tmp_ctx
);
5247 * The following functions are called only if
5248 * ldapsam:trusted and ldapsam:editposix are
5253 * ldapsam_create_user creates a new
5254 * posixAccount and sambaSamAccount object
5255 * in the ldap users subtree
5257 * The uid is allocated by winbindd.
5260 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5261 TALLOC_CTX
*tmp_ctx
, const char *name
,
5262 uint32_t acb_info
, uint32_t *rid
)
5264 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5265 LDAPMessage
*entry
= NULL
;
5266 LDAPMessage
*result
= NULL
;
5267 uint32_t num_result
;
5268 bool is_machine
= False
;
5269 bool add_posix
= False
;
5270 LDAPMod
**mods
= NULL
;
5278 const char *dn
= NULL
;
5279 struct dom_sid group_sid
;
5280 struct dom_sid user_sid
;
5286 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5287 acb_info
& ACB_WSTRUST
||
5288 acb_info
& ACB_SVRTRUST
||
5289 acb_info
& ACB_DOMTRUST
) {
5293 username
= escape_ldap_string(talloc_tos(), name
);
5294 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5295 username
, LDAP_OBJ_POSIXACCOUNT
);
5296 TALLOC_FREE(username
);
5298 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5299 if (rc
!= LDAP_SUCCESS
) {
5300 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5301 return NT_STATUS_ACCESS_DENIED
;
5303 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5305 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5307 if (num_result
> 1) {
5308 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5309 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5312 if (num_result
== 1) {
5314 /* check if it is just a posix account.
5315 * or if there is a sid attached to this entry
5318 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5320 return NT_STATUS_UNSUCCESSFUL
;
5323 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5325 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5326 return NT_STATUS_USER_EXISTS
;
5329 /* it is just a posix account, retrieve the dn for later use */
5330 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5332 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5333 return NT_STATUS_NO_MEMORY
;
5337 if (num_result
== 0) {
5341 /* Create the basic samu structure and generate the mods for the ldap commit */
5342 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5343 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5347 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5349 user
= samu_new(tmp_ctx
);
5351 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5352 return NT_STATUS_NO_MEMORY
;
5355 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5356 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5357 return NT_STATUS_UNSUCCESSFUL
;
5359 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5360 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5361 return NT_STATUS_UNSUCCESSFUL
;
5364 if (acb_info
& ACB_NORMAL
) {
5365 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5366 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5367 return NT_STATUS_UNSUCCESSFUL
;
5370 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5371 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5372 return NT_STATUS_UNSUCCESSFUL
;
5376 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5377 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5378 return NT_STATUS_UNSUCCESSFUL
;
5382 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5383 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5384 return NT_STATUS_UNSUCCESSFUL
;
5387 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5388 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5389 return NT_STATUS_UNSUCCESSFUL
;
5392 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5393 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5395 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5400 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5402 /* retrieve the Domain Users group gid */
5403 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5404 !sid_to_gid(&group_sid
, &gid
)) {
5405 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5406 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5409 /* lets allocate a new userid for this user */
5410 if (!winbind_allocate_uid(&uid
)) {
5411 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5412 return NT_STATUS_UNSUCCESSFUL
;
5417 /* TODO: choose a more appropriate default for machines */
5418 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5419 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5421 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5422 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5424 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5425 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5427 escape_name
= escape_rdn_val_string_alloc(name
);
5429 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5430 return NT_STATUS_NO_MEMORY
;
5434 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5436 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5439 SAFE_FREE(escape_name
);
5441 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5442 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5443 return NT_STATUS_NO_MEMORY
;
5446 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5447 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5448 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5449 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5450 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5451 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5452 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5455 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5458 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5460 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5463 if (rc
!= LDAP_SUCCESS
) {
5464 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5465 return NT_STATUS_UNSUCCESSFUL
;
5468 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5470 flush_pwnam_cache();
5472 return NT_STATUS_OK
;
5475 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5477 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5478 LDAPMessage
*result
= NULL
;
5479 LDAPMessage
*entry
= NULL
;
5485 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5487 filter
= talloc_asprintf(tmp_ctx
,
5490 "(objectClass=%s))",
5491 pdb_get_username(sam_acct
),
5492 LDAP_OBJ_POSIXACCOUNT
,
5493 LDAP_OBJ_SAMBASAMACCOUNT
);
5494 if (filter
== NULL
) {
5495 return NT_STATUS_NO_MEMORY
;
5498 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5499 if (rc
!= LDAP_SUCCESS
) {
5500 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5501 return NT_STATUS_UNSUCCESSFUL
;
5503 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5505 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5507 if (num_result
== 0) {
5508 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5509 return NT_STATUS_NO_SUCH_USER
;
5512 if (num_result
> 1) {
5513 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5514 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5517 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5519 return NT_STATUS_UNSUCCESSFUL
;
5522 /* it is just a posix account, retrieve the dn for later use */
5523 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5525 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5526 return NT_STATUS_NO_MEMORY
;
5529 /* try to remove memberships first */
5532 struct dom_sid
*sids
= NULL
;
5534 size_t num_groups
= 0;
5536 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5538 status
= ldapsam_enum_group_memberships(my_methods
,
5544 if (!NT_STATUS_IS_OK(status
)) {
5548 for (i
=0; i
< num_groups
; i
++) {
5552 sid_peek_rid(&sids
[i
], &group_rid
);
5554 ldapsam_del_groupmem(my_methods
,
5563 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5564 if (rc
!= LDAP_SUCCESS
) {
5565 return NT_STATUS_UNSUCCESSFUL
;
5568 flush_pwnam_cache();
5570 return NT_STATUS_OK
;
5574 * ldapsam_create_group creates a new
5575 * posixGroup and sambaGroupMapping object
5576 * in the ldap groups subtree
5578 * The gid is allocated by winbindd.
5581 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5582 TALLOC_CTX
*tmp_ctx
,
5586 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5588 LDAPMessage
*entry
= NULL
;
5589 LDAPMessage
*result
= NULL
;
5590 uint32_t num_result
;
5591 bool is_new_entry
= False
;
5592 LDAPMod
**mods
= NULL
;
5598 const char *dn
= NULL
;
5599 struct dom_sid group_sid
;
5603 groupname
= escape_ldap_string(talloc_tos(), name
);
5604 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5605 groupname
, LDAP_OBJ_POSIXGROUP
);
5606 TALLOC_FREE(groupname
);
5608 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5609 if (rc
!= LDAP_SUCCESS
) {
5610 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5611 return NT_STATUS_UNSUCCESSFUL
;
5613 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5615 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5617 if (num_result
> 1) {
5618 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5619 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5622 if (num_result
== 1) {
5624 /* check if it is just a posix group.
5625 * or if there is a sid attached to this entry
5628 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5630 return NT_STATUS_UNSUCCESSFUL
;
5633 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5635 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5636 return NT_STATUS_GROUP_EXISTS
;
5639 /* it is just a posix group, retrieve the gid and the dn for later use */
5640 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5642 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5643 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5646 gid
= strtoul(tmp
, NULL
, 10);
5648 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5650 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5651 return NT_STATUS_NO_MEMORY
;
5655 if (num_result
== 0) {
5658 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5660 is_new_entry
= True
;
5662 /* lets allocate a new groupid for this group */
5663 if (!winbind_allocate_gid(&gid
)) {
5664 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5665 return NT_STATUS_UNSUCCESSFUL
;
5668 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5670 escape_name
= escape_rdn_val_string_alloc(name
);
5672 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5673 return NT_STATUS_NO_MEMORY
;
5676 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5678 SAFE_FREE(escape_name
);
5680 if (!gidstr
|| !dn
) {
5681 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5682 return NT_STATUS_NO_MEMORY
;
5685 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5686 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5687 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5690 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5691 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5695 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5697 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5699 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5701 if (!groupsidstr
|| !grouptype
) {
5702 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5703 return NT_STATUS_NO_MEMORY
;
5706 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5707 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5708 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5709 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5710 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5713 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5715 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5716 /* This call may fail with rfc2307bis schema */
5717 /* Retry adding a structural class */
5718 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5719 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5723 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5726 if (rc
!= LDAP_SUCCESS
) {
5727 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5728 return NT_STATUS_UNSUCCESSFUL
;
5731 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5733 return NT_STATUS_OK
;
5736 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5738 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5739 LDAPMessage
*result
= NULL
;
5740 LDAPMessage
*entry
= NULL
;
5745 struct dom_sid group_sid
;
5748 /* get the group sid */
5749 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5751 filter
= talloc_asprintf(tmp_ctx
,
5754 "(objectClass=%s))",
5755 sid_string_talloc(tmp_ctx
, &group_sid
),
5756 LDAP_OBJ_POSIXGROUP
,
5758 if (filter
== NULL
) {
5759 return NT_STATUS_NO_MEMORY
;
5762 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5763 if (rc
!= LDAP_SUCCESS
) {
5764 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5765 return NT_STATUS_UNSUCCESSFUL
;
5767 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5769 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5771 if (num_result
== 0) {
5772 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5773 return NT_STATUS_NO_SUCH_GROUP
;
5776 if (num_result
> 1) {
5777 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5778 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5781 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5783 return NT_STATUS_UNSUCCESSFUL
;
5786 /* here it is, retrieve the dn for later use */
5787 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5789 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5790 return NT_STATUS_NO_MEMORY
;
5793 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5795 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5796 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5799 /* check no user have this group marked as primary group */
5800 filter
= talloc_asprintf(tmp_ctx
,
5803 "(objectClass=%s))",
5805 LDAP_OBJ_POSIXACCOUNT
,
5806 LDAP_OBJ_SAMBASAMACCOUNT
);
5808 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5809 if (rc
!= LDAP_SUCCESS
) {
5810 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5811 return NT_STATUS_UNSUCCESSFUL
;
5813 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5815 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5817 if (num_result
!= 0) {
5818 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5819 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5822 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5823 if (rc
!= LDAP_SUCCESS
) {
5824 return NT_STATUS_UNSUCCESSFUL
;
5827 return NT_STATUS_OK
;
5830 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5831 TALLOC_CTX
*tmp_ctx
,
5833 uint32_t member_rid
,
5836 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5837 LDAPMessage
*entry
= NULL
;
5838 LDAPMessage
*result
= NULL
;
5839 uint32_t num_result
;
5840 LDAPMod
**mods
= NULL
;
5843 const char *dn
= NULL
;
5844 struct dom_sid group_sid
;
5845 struct dom_sid member_sid
;
5850 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5852 case LDAP_MOD_DELETE
:
5853 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5856 return NT_STATUS_UNSUCCESSFUL
;
5859 /* get member sid */
5860 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5862 /* get the group sid */
5863 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5865 filter
= talloc_asprintf(tmp_ctx
,
5868 "(objectClass=%s))",
5869 sid_string_talloc(tmp_ctx
, &member_sid
),
5870 LDAP_OBJ_POSIXACCOUNT
,
5871 LDAP_OBJ_SAMBASAMACCOUNT
);
5872 if (filter
== NULL
) {
5873 return NT_STATUS_NO_MEMORY
;
5876 /* get the member uid */
5877 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5878 if (rc
!= LDAP_SUCCESS
) {
5879 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5880 return NT_STATUS_UNSUCCESSFUL
;
5882 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5884 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5886 if (num_result
== 0) {
5887 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5888 return NT_STATUS_NO_SUCH_MEMBER
;
5891 if (num_result
> 1) {
5892 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5893 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5896 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5898 return NT_STATUS_UNSUCCESSFUL
;
5901 if (modop
== LDAP_MOD_DELETE
) {
5902 /* check if we are trying to remove the member from his primary group */
5904 gid_t user_gid
, group_gid
;
5906 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5908 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5909 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5912 user_gid
= strtoul(gidstr
, NULL
, 10);
5914 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5915 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5916 return NT_STATUS_UNSUCCESSFUL
;
5919 if (user_gid
== group_gid
) {
5920 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5921 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5925 /* here it is, retrieve the uid for later use */
5926 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5928 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5929 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5932 filter
= talloc_asprintf(tmp_ctx
,
5935 "(objectClass=%s))",
5936 sid_string_talloc(tmp_ctx
, &group_sid
),
5937 LDAP_OBJ_POSIXGROUP
,
5941 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5942 if (rc
!= LDAP_SUCCESS
) {
5943 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5944 return NT_STATUS_UNSUCCESSFUL
;
5946 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5948 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5950 if (num_result
== 0) {
5951 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5952 return NT_STATUS_NO_SUCH_GROUP
;
5955 if (num_result
> 1) {
5956 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5957 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5960 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5962 return NT_STATUS_UNSUCCESSFUL
;
5965 /* here it is, retrieve the dn for later use */
5966 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5968 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5969 return NT_STATUS_NO_MEMORY
;
5972 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5974 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5976 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5977 if (rc
!= LDAP_SUCCESS
) {
5978 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5979 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5980 return NT_STATUS_MEMBER_IN_GROUP
;
5982 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5983 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5984 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5986 return NT_STATUS_UNSUCCESSFUL
;
5989 return NT_STATUS_OK
;
5992 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5993 TALLOC_CTX
*tmp_ctx
,
5995 uint32_t member_rid
)
5997 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5999 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
6000 TALLOC_CTX
*tmp_ctx
,
6002 uint32_t member_rid
)
6004 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
6007 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
6008 TALLOC_CTX
*mem_ctx
,
6009 struct samu
*sampass
)
6011 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6012 LDAPMessage
*entry
= NULL
;
6013 LDAPMessage
*result
= NULL
;
6014 uint32_t num_result
;
6015 LDAPMod
**mods
= NULL
;
6017 char *escape_username
;
6019 const char *dn
= NULL
;
6023 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6025 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6026 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6027 return NT_STATUS_UNSUCCESSFUL
;
6029 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6031 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6032 return NT_STATUS_NO_MEMORY
;
6035 escape_username
= escape_ldap_string(talloc_tos(),
6036 pdb_get_username(sampass
));
6037 if (escape_username
== NULL
) {
6038 return NT_STATUS_NO_MEMORY
;
6041 filter
= talloc_asprintf(mem_ctx
,
6044 "(objectClass=%s))",
6046 LDAP_OBJ_POSIXACCOUNT
,
6047 LDAP_OBJ_SAMBASAMACCOUNT
);
6049 TALLOC_FREE(escape_username
);
6051 if (filter
== NULL
) {
6052 return NT_STATUS_NO_MEMORY
;
6055 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6056 if (rc
!= LDAP_SUCCESS
) {
6057 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6058 return NT_STATUS_UNSUCCESSFUL
;
6060 talloc_autofree_ldapmsg(mem_ctx
, result
);
6062 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6064 if (num_result
== 0) {
6065 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6066 return NT_STATUS_NO_SUCH_USER
;
6069 if (num_result
> 1) {
6070 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6071 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6074 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6076 return NT_STATUS_UNSUCCESSFUL
;
6079 /* retrieve the dn for later use */
6080 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6082 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6083 return NT_STATUS_NO_MEMORY
;
6086 /* remove the old one, and add the new one, this way we do not risk races */
6087 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6090 return NT_STATUS_OK
;
6093 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6095 if (rc
!= LDAP_SUCCESS
) {
6096 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6097 pdb_get_username(sampass
), gidstr
));
6098 return NT_STATUS_UNSUCCESSFUL
;
6101 flush_pwnam_cache();
6103 return NT_STATUS_OK
;
6107 /**********************************************************************
6108 trusted domains functions
6109 *********************************************************************/
6111 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6114 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6115 ldap_state
->domain_dn
);
6118 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6119 TALLOC_CTX
*mem_ctx
,
6120 const char *domain
, LDAPMessage
**entry
)
6124 int scope
= LDAP_SCOPE_SUBTREE
;
6125 const char **attrs
= NULL
; /* NULL: get all attrs */
6126 int attrsonly
= 0; /* 0: return values too */
6127 LDAPMessage
*result
= NULL
;
6129 uint32_t num_result
;
6131 filter
= talloc_asprintf(talloc_tos(),
6132 "(&(objectClass=%s)(sambaDomainName=%s))",
6133 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6135 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6136 if (trusted_dn
== NULL
) {
6139 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6140 filter
, attrs
, attrsonly
, &result
);
6142 if (result
!= NULL
) {
6143 talloc_autofree_ldapmsg(mem_ctx
, result
);
6146 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6151 if (rc
!= LDAP_SUCCESS
) {
6155 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6157 if (num_result
> 1) {
6158 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6159 "%s object for domain '%s'?!\n",
6160 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6164 if (num_result
== 0) {
6165 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6166 "%s object for domain %s.\n",
6167 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6170 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6176 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6179 struct dom_sid
*sid
,
6180 time_t *pass_last_set_time
)
6182 struct ldapsam_privates
*ldap_state
=
6183 (struct ldapsam_privates
*)methods
->private_data
;
6184 LDAPMessage
*entry
= NULL
;
6186 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6188 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6197 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6198 entry
, "sambaClearTextPassword", talloc_tos());
6199 if (pwd_str
== NULL
) {
6202 /* trusteddom_pw routines do not use talloc yet... */
6203 *pwd
= SMB_STRDUP(pwd_str
);
6209 /* last change time */
6210 if (pass_last_set_time
!= NULL
) {
6212 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6213 entry
, "sambaPwdLastSet", talloc_tos());
6214 if (time_str
== NULL
) {
6217 *pass_last_set_time
= (time_t)atol(time_str
);
6223 struct dom_sid dom_sid
;
6224 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6227 if (sid_str
== NULL
) {
6230 if (!string_to_sid(&dom_sid
, sid_str
)) {
6233 sid_copy(sid
, &dom_sid
);
6239 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6242 const struct dom_sid
*sid
)
6244 struct ldapsam_privates
*ldap_state
=
6245 (struct ldapsam_privates
*)methods
->private_data
;
6246 LDAPMessage
*entry
= NULL
;
6247 LDAPMod
**mods
= NULL
;
6248 char *prev_pwd
= NULL
;
6249 char *trusted_dn
= NULL
;
6252 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6255 * get the current entry (if there is one) in order to put the
6256 * current password into the previous password attribute
6258 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6263 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6264 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6265 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6267 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6268 sid_string_tos(sid
));
6269 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6270 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6271 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6272 "sambaClearTextPassword", pwd
);
6274 if (entry
!= NULL
) {
6275 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6276 entry
, "sambaClearTextPassword", talloc_tos());
6277 if (prev_pwd
!= NULL
) {
6278 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6279 "sambaPreviousClearTextPassword",
6284 talloc_autofree_ldapmod(talloc_tos(), mods
);
6286 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6287 if (trusted_dn
== NULL
) {
6290 if (entry
== NULL
) {
6291 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6293 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6296 if (rc
!= LDAP_SUCCESS
) {
6297 DEBUG(1, ("error writing trusted domain password!\n"));
6304 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6308 struct ldapsam_privates
*ldap_state
=
6309 (struct ldapsam_privates
*)methods
->private_data
;
6310 LDAPMessage
*entry
= NULL
;
6311 const char *trusted_dn
;
6313 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6317 if (entry
== NULL
) {
6318 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6323 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6325 if (trusted_dn
== NULL
) {
6326 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6330 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6331 if (rc
!= LDAP_SUCCESS
) {
6338 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6339 TALLOC_CTX
*mem_ctx
,
6340 uint32_t *num_domains
,
6341 struct trustdom_info
***domains
)
6344 struct ldapsam_privates
*ldap_state
=
6345 (struct ldapsam_privates
*)methods
->private_data
;
6347 int scope
= LDAP_SCOPE_SUBTREE
;
6348 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6349 int attrsonly
= 0; /* 0: return values too */
6350 LDAPMessage
*result
= NULL
;
6351 LDAPMessage
*entry
= NULL
;
6353 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6354 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6356 rc
= smbldap_search(ldap_state
->smbldap_state
,
6357 ldap_state
->domain_dn
,
6364 if (result
!= NULL
) {
6365 talloc_autofree_ldapmsg(mem_ctx
, result
);
6368 if (rc
!= LDAP_SUCCESS
) {
6369 return NT_STATUS_UNSUCCESSFUL
;
6373 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6374 DEBUG(1, ("talloc failed\n"));
6375 return NT_STATUS_NO_MEMORY
;
6378 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6380 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6382 char *dom_name
, *dom_sid_str
;
6383 struct trustdom_info
*dom_info
;
6385 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6386 if (dom_info
== NULL
) {
6387 DEBUG(1, ("talloc failed\n"));
6388 return NT_STATUS_NO_MEMORY
;
6391 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6395 if (dom_name
== NULL
) {
6396 DEBUG(1, ("talloc failed\n"));
6397 return NT_STATUS_NO_MEMORY
;
6399 dom_info
->name
= dom_name
;
6401 dom_sid_str
= smbldap_talloc_single_attribute(
6402 priv2ld(ldap_state
), entry
, "sambaSID",
6404 if (dom_sid_str
== NULL
) {
6405 DEBUG(1, ("talloc failed\n"));
6406 return NT_STATUS_NO_MEMORY
;
6408 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6409 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6411 return NT_STATUS_UNSUCCESSFUL
;
6414 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6415 domains
, num_domains
);
6417 if (*domains
== NULL
) {
6418 DEBUG(1, ("talloc failed\n"));
6419 return NT_STATUS_NO_MEMORY
;
6423 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6424 return NT_STATUS_OK
;
6428 /**********************************************************************
6430 *********************************************************************/
6432 static void free_private_data(void **vp
)
6434 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6436 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6438 if ((*ldap_state
)->result
!= NULL
) {
6439 ldap_msgfree((*ldap_state
)->result
);
6440 (*ldap_state
)->result
= NULL
;
6442 if ((*ldap_state
)->domain_dn
!= NULL
) {
6443 SAFE_FREE((*ldap_state
)->domain_dn
);
6448 /* No need to free any further, as it is talloc()ed */
6451 /*********************************************************************
6452 Intitalise the parts of the pdb_methods structure that are common to
6454 *********************************************************************/
6456 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6459 struct ldapsam_privates
*ldap_state
;
6461 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6465 (*pdb_method
)->name
= "ldapsam";
6467 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6468 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6469 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6470 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6471 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6472 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6474 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6475 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6476 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6477 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6478 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6479 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6480 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6482 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6483 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6485 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6487 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6488 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6490 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6491 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6492 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6493 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6495 /* TODO: Setup private data and free */
6497 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6498 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6499 return NT_STATUS_NO_MEMORY
;
6502 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6503 location
, &ldap_state
->smbldap_state
);
6505 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6509 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6510 return NT_STATUS_NO_MEMORY
;
6513 (*pdb_method
)->private_data
= ldap_state
;
6515 (*pdb_method
)->free_private_data
= free_private_data
;
6517 return NT_STATUS_OK
;
6520 /**********************************************************************
6521 Initialise the 'compat' mode for pdb_ldap
6522 *********************************************************************/
6524 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6527 struct ldapsam_privates
*ldap_state
;
6528 char *uri
= talloc_strdup( NULL
, location
);
6530 trim_char( uri
, '\"', '\"' );
6531 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6535 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6539 (*pdb_method
)->name
= "ldapsam_compat";
6541 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6542 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6544 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6546 return NT_STATUS_OK
;
6549 /**********************************************************************
6550 Initialise the normal mode for pdb_ldap
6551 *********************************************************************/
6553 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6556 struct ldapsam_privates
*ldap_state
= NULL
;
6557 uint32_t alg_rid_base
;
6558 char *alg_rid_base_string
= NULL
;
6559 LDAPMessage
*result
= NULL
;
6560 LDAPMessage
*entry
= NULL
;
6561 struct dom_sid ldap_domain_sid
;
6562 struct dom_sid secrets_domain_sid
;
6563 char *domain_sid_string
= NULL
;
6565 char *uri
= talloc_strdup( NULL
, location
);
6567 trim_char( uri
, '\"', '\"' );
6568 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6572 if (!NT_STATUS_IS_OK(nt_status
)) {
6576 (*pdb_method
)->name
= "ldapsam";
6578 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6579 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6580 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6581 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6582 (*pdb_method
)->search_users
= ldapsam_search_users
;
6583 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6584 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6586 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6587 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6588 (*pdb_method
)->enum_group_memberships
=
6589 ldapsam_enum_group_memberships
;
6590 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6591 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6592 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6593 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6595 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6596 (*pdb_method
)->create_user
= ldapsam_create_user
;
6597 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6598 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6599 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6600 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6601 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6602 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6606 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6607 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6609 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6611 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6613 ldap_state
->domain_name
, True
);
6615 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6616 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6617 "info, nor add one to the domain\n"));
6618 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6619 "will be unable to allocate new users/groups, "
6620 "and will risk BDCs having inconsistant SIDs\n"));
6621 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6622 return NT_STATUS_OK
;
6625 /* Given that the above might fail, everything below this must be
6628 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6631 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6633 ldap_msgfree(result
);
6634 return NT_STATUS_UNSUCCESSFUL
;
6637 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6639 ldap_msgfree(result
);
6640 return NT_STATUS_UNSUCCESSFUL
;
6643 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6646 domain_sid_string
= smbldap_talloc_single_attribute(
6647 ldap_state
->smbldap_state
->ldap_struct
,
6649 get_userattr_key2string(ldap_state
->schema_ver
,
6650 LDAP_ATTR_USER_SID
),
6653 if (domain_sid_string
) {
6655 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6656 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6657 "read as a valid SID\n", domain_sid_string
));
6658 ldap_msgfree(result
);
6659 TALLOC_FREE(domain_sid_string
);
6660 return NT_STATUS_INVALID_PARAMETER
;
6662 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6663 &secrets_domain_sid
);
6664 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
6665 &ldap_domain_sid
)) {
6666 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6667 "%s based on pdb_ldap results %s -> %s\n",
6668 ldap_state
->domain_name
,
6669 sid_string_dbg(&secrets_domain_sid
),
6670 sid_string_dbg(&ldap_domain_sid
)));
6672 /* reset secrets.tdb sid */
6673 secrets_store_domain_sid(ldap_state
->domain_name
,
6675 DEBUG(1, ("New global sam SID: %s\n",
6676 sid_string_dbg(get_global_sam_sid())));
6678 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6679 TALLOC_FREE(domain_sid_string
);
6682 alg_rid_base_string
= smbldap_talloc_single_attribute(
6683 ldap_state
->smbldap_state
->ldap_struct
,
6685 get_attr_key2string( dominfo_attr_list
,
6686 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6688 if (alg_rid_base_string
) {
6689 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6690 if (alg_rid_base
!= algorithmic_rid_base()) {
6691 DEBUG(0, ("The value of 'algorithmic RID base' has "
6692 "changed since the LDAP\n"
6693 "database was initialised. Aborting. \n"));
6694 ldap_msgfree(result
);
6695 TALLOC_FREE(alg_rid_base_string
);
6696 return NT_STATUS_UNSUCCESSFUL
;
6698 TALLOC_FREE(alg_rid_base_string
);
6700 ldap_msgfree(result
);
6702 return NT_STATUS_OK
;
6705 NTSTATUS
pdb_ldap_init(void)
6708 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6711 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6714 /* Let pdb_nds register backends */
6717 return NT_STATUS_OK
;