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 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 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 acct_ctrl
= 0;
538 uint16 bad_password_count
= 0,
541 uint8 hours
[MAX_HOURS_LEN
];
543 LOGIN_CACHE
*cache_entry
= NULL
;
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
)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
) 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
) 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 DOM_SID mapped_gsid
;
1036 const 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_sid2uid(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 (!(cache_entry
= login_cache_read(sampass
))) {
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
,
1160 SAFE_FREE(cache_entry
);
1164 /**********************************************************************
1165 Initialize the ldap db from a struct samu. Called on update.
1166 (Based on init_buffer_from_sam in pdb_tdb.c)
1167 *********************************************************************/
1169 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1170 LDAPMessage
*existing
,
1171 LDAPMod
*** mods
, struct samu
* sampass
,
1172 bool (*need_update
)(const struct samu
*,
1178 if (mods
== NULL
|| sampass
== NULL
) {
1179 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1186 * took out adding "objectclass: sambaAccount"
1187 * do this on a per-mod basis
1189 if (need_update(sampass
, PDB_USERNAME
)) {
1190 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1191 "uid", pdb_get_username(sampass
));
1192 if (ldap_state
->is_nds_ldap
) {
1193 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1194 "cn", pdb_get_username(sampass
));
1195 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1196 "sn", pdb_get_username(sampass
));
1200 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1202 /* only update the RID if we actually need to */
1203 if (need_update(sampass
, PDB_USERSID
)) {
1205 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
1207 switch ( ldap_state
->schema_ver
) {
1208 case SCHEMAVER_SAMBAACCOUNT
:
1209 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1210 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1211 sid_string_dbg(user_sid
),
1213 &ldap_state
->domain_sid
)));
1216 if (asprintf(&temp
, "%i", rid
) < 0) {
1219 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1220 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1225 case SCHEMAVER_SAMBASAMACCOUNT
:
1226 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1227 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1228 sid_to_fstring(sid_string
, user_sid
));
1232 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1237 /* we don't need to store the primary group RID - so leaving it
1238 'free' to hang off the unix primary group makes life easier */
1240 if (need_update(sampass
, PDB_GROUPSID
)) {
1242 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
1244 switch ( ldap_state
->schema_ver
) {
1245 case SCHEMAVER_SAMBAACCOUNT
:
1246 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1247 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1248 sid_string_dbg(group_sid
),
1250 &ldap_state
->domain_sid
)));
1254 if (asprintf(&temp
, "%i", rid
) < 0) {
1257 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1258 get_userattr_key2string(ldap_state
->schema_ver
,
1259 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1263 case SCHEMAVER_SAMBASAMACCOUNT
:
1264 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1265 get_userattr_key2string(ldap_state
->schema_ver
,
1266 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1270 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1276 /* displayName, cn, and gecos should all be the same
1277 * most easily accomplished by giving them the same OID
1278 * gecos isn't set here b/c it should be handled by the
1280 * We change displayName only and fall back to cn if
1281 * it does not exist.
1284 if (need_update(sampass
, PDB_FULLNAME
))
1285 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1286 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1287 pdb_get_fullname(sampass
));
1289 if (need_update(sampass
, PDB_ACCTDESC
))
1290 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1291 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1292 pdb_get_acct_desc(sampass
));
1294 if (need_update(sampass
, PDB_WORKSTATIONS
))
1295 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1296 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1297 pdb_get_workstations(sampass
));
1299 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1300 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1301 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1302 pdb_get_munged_dial(sampass
));
1304 if (need_update(sampass
, PDB_SMBHOME
))
1305 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1306 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1307 pdb_get_homedir(sampass
));
1309 if (need_update(sampass
, PDB_DRIVE
))
1310 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1311 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1312 pdb_get_dir_drive(sampass
));
1314 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1315 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1316 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1317 pdb_get_logon_script(sampass
));
1319 if (need_update(sampass
, PDB_PROFILE
))
1320 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1321 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1322 pdb_get_profile_path(sampass
));
1324 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1327 if (need_update(sampass
, PDB_LOGONTIME
))
1328 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1329 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1332 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1335 if (need_update(sampass
, PDB_LOGOFFTIME
))
1336 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1337 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1340 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1343 if (need_update(sampass
, PDB_KICKOFFTIME
))
1344 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1345 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1348 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1351 if (need_update(sampass
, PDB_CANCHANGETIME
))
1352 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1353 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1356 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_must_change_time(sampass
)) < 0) {
1359 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1360 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1361 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1364 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1365 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1367 if (need_update(sampass
, PDB_LMPASSWD
)) {
1368 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1371 pdb_sethexpwd(pwstr
, lm_pw
,
1372 pdb_get_acct_ctrl(sampass
));
1373 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1374 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1377 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1378 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1382 if (need_update(sampass
, PDB_NTPASSWD
)) {
1383 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1386 pdb_sethexpwd(pwstr
, nt_pw
,
1387 pdb_get_acct_ctrl(sampass
));
1388 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1389 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1392 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1393 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1398 if (need_update(sampass
, PDB_PWHISTORY
)) {
1400 uint32 pwHistLen
= 0;
1401 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1403 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1407 if (pwHistLen
== 0) {
1408 /* Remove any password history from the LDAP store. */
1409 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1413 uint32 currHistLen
= 0;
1414 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1415 if (pwhist
!= NULL
) {
1416 /* We can only store (1024-1/64 password history entries. */
1417 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1418 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1419 /* Store the salt. */
1420 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1421 /* Followed by the md5 hash of salt + md4 hash */
1422 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1423 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1424 DEBUG(100, ("pwstr=%s\n", pwstr
));
1428 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1429 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1434 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1435 if (asprintf(&temp
, "%li",
1436 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1439 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1440 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1446 if (need_update(sampass
, PDB_HOURS
)) {
1447 const uint8
*hours
= pdb_get_hours(sampass
);
1450 pdb_sethexhours(hourstr
, hours
);
1451 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1454 get_userattr_key2string(ldap_state
->schema_ver
,
1455 LDAP_ATTR_LOGON_HOURS
),
1460 if (need_update(sampass
, PDB_ACCTCTRL
))
1461 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1462 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1463 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1465 /* password lockout cache:
1466 - If we are now autolocking or clearing, we write to ldap
1467 - If we are clearing, we delete the cache entry
1468 - If the count is > 0, we update the cache
1470 This even means when autolocking, we cache, just in case the
1471 update doesn't work, and we have to cache the autolock flag */
1473 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1474 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1475 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1476 time_t badtime
= pdb_get_bad_password_time(sampass
);
1478 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1480 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1481 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1483 if ((badcount
>= pol
) || (badcount
== 0)) {
1484 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1485 (unsigned int)badcount
, (unsigned int)badtime
));
1486 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1490 ldap_state
->smbldap_state
->ldap_struct
,
1492 get_userattr_key2string(
1493 ldap_state
->schema_ver
,
1494 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1498 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1502 ldap_state
->smbldap_state
->ldap_struct
,
1504 get_userattr_key2string(
1505 ldap_state
->schema_ver
,
1506 LDAP_ATTR_BAD_PASSWORD_TIME
),
1510 if (badcount
== 0) {
1511 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1512 login_cache_delentry(sampass
);
1514 LOGIN_CACHE cache_entry
;
1516 cache_entry
.entry_timestamp
= time(NULL
);
1517 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1518 cache_entry
.bad_password_count
= badcount
;
1519 cache_entry
.bad_password_time
= badtime
;
1521 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1522 login_cache_write(sampass
, cache_entry
);
1529 /**********************************************************************
1530 End enumeration of the LDAP password list.
1531 *********************************************************************/
1533 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1535 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1536 if (ldap_state
->result
) {
1537 ldap_msgfree(ldap_state
->result
);
1538 ldap_state
->result
= NULL
;
1542 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1543 const char *new_attr
)
1547 if (new_attr
== NULL
) {
1551 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1555 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1557 SMB_ASSERT((*attr_list
) != NULL
);
1558 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1559 (*attr_list
)[i
+1] = NULL
;
1562 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1563 const char ***attr_list
)
1565 append_attr(mem_ctx
, attr_list
, "uidNumber");
1566 append_attr(mem_ctx
, attr_list
, "gidNumber");
1567 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1568 append_attr(mem_ctx
, attr_list
, "loginShell");
1569 append_attr(mem_ctx
, attr_list
, "gecos");
1572 /**********************************************************************
1573 Get struct samu entry from LDAP by username.
1574 *********************************************************************/
1576 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1578 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1579 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1580 LDAPMessage
*result
= NULL
;
1581 LDAPMessage
*entry
= NULL
;
1583 const char ** attr_list
;
1586 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1587 append_attr(user
, &attr_list
,
1588 get_userattr_key2string(ldap_state
->schema_ver
,
1589 LDAP_ATTR_MOD_TIMESTAMP
));
1590 ldapsam_add_unix_attributes(user
, &attr_list
);
1591 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1593 TALLOC_FREE( attr_list
);
1595 if ( rc
!= LDAP_SUCCESS
)
1596 return NT_STATUS_NO_SUCH_USER
;
1598 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1601 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1602 ldap_msgfree(result
);
1603 return NT_STATUS_NO_SUCH_USER
;
1604 } else if (count
> 1) {
1605 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1606 ldap_msgfree(result
);
1607 return NT_STATUS_NO_SUCH_USER
;
1610 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1612 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1613 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1614 ldap_msgfree(result
);
1615 return NT_STATUS_NO_SUCH_USER
;
1617 pdb_set_backend_private_data(user
, result
, NULL
,
1618 my_methods
, PDB_CHANGED
);
1619 talloc_autofree_ldapmsg(user
, result
);
1622 ldap_msgfree(result
);
1627 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1628 const DOM_SID
*sid
, LDAPMessage
**result
)
1631 const char ** attr_list
;
1634 switch ( ldap_state
->schema_ver
) {
1635 case SCHEMAVER_SAMBASAMACCOUNT
: {
1636 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1637 if (tmp_ctx
== NULL
) {
1638 return LDAP_NO_MEMORY
;
1641 attr_list
= get_userattr_list(tmp_ctx
,
1642 ldap_state
->schema_ver
);
1643 append_attr(tmp_ctx
, &attr_list
,
1644 get_userattr_key2string(
1645 ldap_state
->schema_ver
,
1646 LDAP_ATTR_MOD_TIMESTAMP
));
1647 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1648 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1650 TALLOC_FREE(tmp_ctx
);
1652 if ( rc
!= LDAP_SUCCESS
)
1657 case SCHEMAVER_SAMBAACCOUNT
:
1658 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1662 attr_list
= get_userattr_list(NULL
,
1663 ldap_state
->schema_ver
);
1664 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1665 TALLOC_FREE( attr_list
);
1667 if ( rc
!= LDAP_SUCCESS
)
1674 /**********************************************************************
1675 Get struct samu entry from LDAP by SID.
1676 *********************************************************************/
1678 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1680 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1681 LDAPMessage
*result
= NULL
;
1682 LDAPMessage
*entry
= NULL
;
1686 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1688 if (rc
!= LDAP_SUCCESS
)
1689 return NT_STATUS_NO_SUCH_USER
;
1691 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1694 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1695 "count=%d\n", sid_string_dbg(sid
), count
));
1696 ldap_msgfree(result
);
1697 return NT_STATUS_NO_SUCH_USER
;
1698 } else if (count
> 1) {
1699 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1700 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1702 ldap_msgfree(result
);
1703 return NT_STATUS_NO_SUCH_USER
;
1706 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1708 ldap_msgfree(result
);
1709 return NT_STATUS_NO_SUCH_USER
;
1712 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1713 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1714 ldap_msgfree(result
);
1715 return NT_STATUS_NO_SUCH_USER
;
1718 pdb_set_backend_private_data(user
, result
, NULL
,
1719 my_methods
, PDB_CHANGED
);
1720 talloc_autofree_ldapmsg(user
, result
);
1721 return NT_STATUS_OK
;
1724 /********************************************************************
1725 Do the actual modification - also change a plaintext passord if
1727 **********************************************************************/
1729 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1730 struct samu
*newpwd
, char *dn
,
1731 LDAPMod
**mods
, int ldap_op
,
1732 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1734 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1737 if (!newpwd
|| !dn
) {
1738 return NT_STATUS_INVALID_PARAMETER
;
1742 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1743 /* may be password change below however */
1747 if (ldap_state
->is_nds_ldap
) {
1748 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1752 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1756 rc
= smbldap_add(ldap_state
->smbldap_state
,
1759 case LDAP_MOD_REPLACE
:
1760 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1764 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1766 return NT_STATUS_INVALID_PARAMETER
;
1769 if (rc
!=LDAP_SUCCESS
) {
1770 return NT_STATUS_UNSUCCESSFUL
;
1774 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1775 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1776 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1777 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1780 char *retoid
= NULL
;
1781 struct berval
*retdata
= NULL
;
1782 char *utf8_password
;
1784 size_t converted_size
;
1787 if (!ldap_state
->is_nds_ldap
) {
1789 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1790 LDAP_EXOP_MODIFY_PASSWD
)) {
1791 DEBUG(2, ("ldap password change requested, but LDAP "
1792 "server does not support it -- ignoring\n"));
1793 return NT_STATUS_OK
;
1797 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1798 pdb_get_plaintext_passwd(newpwd
),
1801 return NT_STATUS_NO_MEMORY
;
1804 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1805 TALLOC_FREE(utf8_password
);
1806 return NT_STATUS_NO_MEMORY
;
1809 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1810 DEBUG(0,("ber_alloc_t returns NULL\n"));
1811 TALLOC_FREE(utf8_password
);
1812 TALLOC_FREE(utf8_dn
);
1813 return NT_STATUS_UNSUCCESSFUL
;
1816 if ((ber_printf (ber
, "{") < 0) ||
1817 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1819 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1822 TALLOC_FREE(utf8_dn
);
1823 TALLOC_FREE(utf8_password
);
1824 return NT_STATUS_UNSUCCESSFUL
;
1827 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1828 ret
= ber_printf(ber
, "ts}",
1829 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1832 ret
= ber_printf(ber
, "}");
1836 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1839 TALLOC_FREE(utf8_dn
);
1840 TALLOC_FREE(utf8_password
);
1841 return NT_STATUS_UNSUCCESSFUL
;
1844 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1845 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1847 TALLOC_FREE(utf8_dn
);
1848 TALLOC_FREE(utf8_password
);
1849 return NT_STATUS_UNSUCCESSFUL
;
1852 TALLOC_FREE(utf8_dn
);
1853 TALLOC_FREE(utf8_password
);
1856 if (!ldap_state
->is_nds_ldap
) {
1857 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1858 LDAP_EXOP_MODIFY_PASSWD
,
1859 bv
, NULL
, NULL
, &retoid
,
1862 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1863 pdb_get_plaintext_passwd(newpwd
));
1865 if (rc
!= LDAP_SUCCESS
) {
1866 char *ld_error
= NULL
;
1868 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1869 DEBUG(3, ("Could not set userPassword "
1870 "attribute due to an objectClass "
1871 "violation -- ignoring\n"));
1873 return NT_STATUS_OK
;
1876 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1878 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1879 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1880 SAFE_FREE(ld_error
);
1882 #if defined(LDAP_CONSTRAINT_VIOLATION)
1883 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1884 return NT_STATUS_PASSWORD_RESTRICTION
;
1886 return NT_STATUS_UNSUCCESSFUL
;
1888 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1889 #ifdef DEBUG_PASSWORD
1890 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1893 ber_bvfree(retdata
);
1895 ldap_memfree(retoid
);
1899 return NT_STATUS_OK
;
1902 /**********************************************************************
1903 Delete entry from LDAP for username.
1904 *********************************************************************/
1906 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1907 struct samu
* sam_acct
)
1909 struct ldapsam_privates
*priv
=
1910 (struct ldapsam_privates
*)my_methods
->private_data
;
1913 LDAPMessage
*msg
, *entry
;
1914 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1915 const char **attr_list
;
1916 TALLOC_CTX
*mem_ctx
;
1919 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1920 return NT_STATUS_INVALID_PARAMETER
;
1923 sname
= pdb_get_username(sam_acct
);
1925 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1928 mem_ctx
= talloc_new(NULL
);
1929 if (mem_ctx
== NULL
) {
1930 DEBUG(0, ("talloc_new failed\n"));
1934 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1935 if (attr_list
== NULL
) {
1939 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1941 if ((rc
!= LDAP_SUCCESS
) ||
1942 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1943 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1944 DEBUG(5, ("Could not find user %s\n", sname
));
1945 result
= NT_STATUS_NO_SUCH_USER
;
1949 rc
= ldapsam_delete_entry(
1950 priv
, mem_ctx
, entry
,
1951 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1952 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1955 result
= (rc
== LDAP_SUCCESS
) ?
1956 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1959 TALLOC_FREE(mem_ctx
);
1963 /**********************************************************************
1964 Helper function to determine for update_sam_account whether
1965 we need LDAP modification.
1966 *********************************************************************/
1968 static bool element_is_changed(const struct samu
*sampass
,
1969 enum pdb_elements element
)
1971 return IS_SAM_CHANGED(sampass
, element
);
1974 /**********************************************************************
1976 *********************************************************************/
1978 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1980 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1981 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1984 LDAPMessage
*result
= NULL
;
1985 LDAPMessage
*entry
= NULL
;
1986 LDAPMod
**mods
= NULL
;
1987 const char **attr_list
;
1989 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1991 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1992 if (pdb_get_username(newpwd
) == NULL
) {
1993 return NT_STATUS_INVALID_PARAMETER
;
1995 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1996 TALLOC_FREE( attr_list
);
1997 if (rc
!= LDAP_SUCCESS
) {
1998 return NT_STATUS_UNSUCCESSFUL
;
2000 pdb_set_backend_private_data(newpwd
, result
, NULL
,
2001 my_methods
, PDB_CHANGED
);
2002 talloc_autofree_ldapmsg(newpwd
, result
);
2005 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
2006 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
2007 return NT_STATUS_UNSUCCESSFUL
;
2010 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2011 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
2013 return NT_STATUS_UNSUCCESSFUL
;
2016 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
2018 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2019 element_is_changed
)) {
2020 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2023 ldap_mods_free(mods
,True
);
2024 return NT_STATUS_UNSUCCESSFUL
;
2027 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
2028 && (mods
== NULL
)) {
2029 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2030 pdb_get_username(newpwd
)));
2032 return NT_STATUS_OK
;
2035 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
2038 ldap_mods_free(mods
,True
);
2044 * We need to set the backend private data to NULL here. For example
2045 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2046 * same one, and with the explicit delete / add logic for attribute
2047 * values the second time we would use the wrong "old" value which
2048 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2050 * The existing LDAPMessage is still being auto-freed by the
2053 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2056 if (!NT_STATUS_IS_OK(ret
)) {
2060 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2061 pdb_get_username(newpwd
)));
2062 return NT_STATUS_OK
;
2065 /***************************************************************************
2066 Renames a struct samu
2067 - The "rename user script" has full responsibility for changing everything
2068 ***************************************************************************/
2070 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2071 TALLOC_CTX
*tmp_ctx
,
2075 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2076 TALLOC_CTX
*mem_ctx
,
2080 size_t *p_num_groups
);
2082 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2083 struct samu
*old_acct
,
2084 const char *newname
)
2086 const char *oldname
;
2088 char *rename_script
= NULL
;
2089 fstring oldname_lower
, newname_lower
;
2092 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2093 return NT_STATUS_INVALID_PARAMETER
;
2096 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2097 return NT_STATUS_INVALID_PARAMETER
;
2100 oldname
= pdb_get_username(old_acct
);
2102 /* rename the posix user */
2103 rename_script
= SMB_STRDUP(lp_renameuser_script());
2104 if (rename_script
== NULL
) {
2105 return NT_STATUS_NO_MEMORY
;
2108 if (!(*rename_script
)) {
2109 SAFE_FREE(rename_script
);
2110 return NT_STATUS_ACCESS_DENIED
;
2113 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2116 /* We have to allow the account name to end with a '$'.
2117 Also, follow the semantics in _samr_create_user() and lower case the
2118 posix name but preserve the case in passdb */
2120 fstrcpy( oldname_lower
, oldname
);
2121 strlower_m( oldname_lower
);
2122 fstrcpy( newname_lower
, newname
);
2123 strlower_m( newname_lower
);
2124 rename_script
= realloc_string_sub2(rename_script
,
2129 if (!rename_script
) {
2130 return NT_STATUS_NO_MEMORY
;
2132 rename_script
= realloc_string_sub2(rename_script
,
2137 rc
= smbrun(rename_script
, NULL
);
2139 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2140 rename_script
, rc
));
2142 SAFE_FREE(rename_script
);
2145 smb_nscd_flush_user_cache();
2149 return NT_STATUS_UNSUCCESSFUL
;
2151 return NT_STATUS_OK
;
2154 /**********************************************************************
2155 Helper function to determine for update_sam_account whether
2156 we need LDAP modification.
2157 *********************************************************************/
2159 static bool element_is_set_or_changed(const struct samu
*sampass
,
2160 enum pdb_elements element
)
2162 return (IS_SAM_SET(sampass
, element
) ||
2163 IS_SAM_CHANGED(sampass
, element
));
2166 /**********************************************************************
2167 Add struct samu to LDAP.
2168 *********************************************************************/
2170 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2172 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2173 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2175 LDAPMessage
*result
= NULL
;
2176 LDAPMessage
*entry
= NULL
;
2177 LDAPMod
**mods
= NULL
;
2178 int ldap_op
= LDAP_MOD_REPLACE
;
2180 const char **attr_list
;
2181 char *escape_user
= NULL
;
2182 const char *username
= pdb_get_username(newpwd
);
2183 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
2184 char *filter
= NULL
;
2186 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2187 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2190 return NT_STATUS_NO_MEMORY
;
2193 if (!username
|| !*username
) {
2194 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2195 status
= NT_STATUS_INVALID_PARAMETER
;
2199 /* free this list after the second search or in case we exit on failure */
2200 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2202 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2204 if (rc
!= LDAP_SUCCESS
) {
2208 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2209 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2213 ldap_msgfree(result
);
2216 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2217 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2219 if (rc
== LDAP_SUCCESS
) {
2220 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2221 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2222 "already in the base, with samba "
2223 "attributes\n", sid_string_dbg(sid
)));
2226 ldap_msgfree(result
);
2231 /* does the entry already exist but without a samba attributes?
2232 we need to return the samba attributes here */
2234 escape_user
= escape_ldap_string(talloc_tos(), username
);
2235 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2237 status
= NT_STATUS_NO_MEMORY
;
2240 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2241 TALLOC_FREE(escape_user
);
2243 status
= NT_STATUS_NO_MEMORY
;
2247 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2248 filter
, attr_list
, &result
);
2249 if ( rc
!= LDAP_SUCCESS
) {
2253 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2255 if (num_result
> 1) {
2256 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2260 /* Check if we need to update an existing entry */
2261 if (num_result
== 1) {
2262 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2263 ldap_op
= LDAP_MOD_REPLACE
;
2264 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2265 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2267 status
= NT_STATUS_NO_MEMORY
;
2271 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2273 /* There might be a SID for this account already - say an idmap entry */
2275 filter
= talloc_asprintf(ctx
,
2276 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2277 get_userattr_key2string(ldap_state
->schema_ver
,
2278 LDAP_ATTR_USER_SID
),
2279 sid_string_talloc(ctx
, sid
),
2280 LDAP_OBJ_IDMAP_ENTRY
,
2281 LDAP_OBJ_SID_ENTRY
);
2283 status
= NT_STATUS_NO_MEMORY
;
2287 /* free old result before doing a new search */
2288 if (result
!= NULL
) {
2289 ldap_msgfree(result
);
2292 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2293 filter
, attr_list
, &result
);
2295 if ( rc
!= LDAP_SUCCESS
) {
2299 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2301 if (num_result
> 1) {
2302 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2306 /* Check if we need to update an existing entry */
2307 if (num_result
== 1) {
2309 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2310 ldap_op
= LDAP_MOD_REPLACE
;
2311 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2312 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2314 status
= NT_STATUS_NO_MEMORY
;
2320 if (num_result
== 0) {
2321 char *escape_username
;
2322 /* Check if we need to add an entry */
2323 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2324 ldap_op
= LDAP_MOD_ADD
;
2326 escape_username
= escape_rdn_val_string_alloc(username
);
2327 if (!escape_username
) {
2328 status
= NT_STATUS_NO_MEMORY
;
2332 if (username
[strlen(username
)-1] == '$') {
2333 dn
= talloc_asprintf(ctx
,
2336 lp_ldap_machine_suffix());
2338 dn
= talloc_asprintf(ctx
,
2341 lp_ldap_user_suffix());
2344 SAFE_FREE(escape_username
);
2346 status
= NT_STATUS_NO_MEMORY
;
2351 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2352 element_is_set_or_changed
)) {
2353 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2355 ldap_mods_free(mods
, true);
2361 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2364 switch ( ldap_state
->schema_ver
) {
2365 case SCHEMAVER_SAMBAACCOUNT
:
2366 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2368 case SCHEMAVER_SAMBASAMACCOUNT
:
2369 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2372 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2376 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2377 if (!NT_STATUS_IS_OK(ret
)) {
2378 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2379 pdb_get_username(newpwd
),dn
));
2380 ldap_mods_free(mods
, true);
2384 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2385 ldap_mods_free(mods
, true);
2387 status
= NT_STATUS_OK
;
2393 ldap_msgfree(result
);
2398 /**********************************************************************
2399 *********************************************************************/
2401 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2403 LDAPMessage
** result
)
2405 int scope
= LDAP_SCOPE_SUBTREE
;
2407 const char **attr_list
;
2409 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2410 rc
= smbldap_search(ldap_state
->smbldap_state
,
2411 lp_ldap_suffix (), scope
,
2412 filter
, attr_list
, 0, result
);
2413 TALLOC_FREE(attr_list
);
2418 /**********************************************************************
2419 *********************************************************************/
2421 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2422 GROUP_MAP
*map
, LDAPMessage
*entry
)
2425 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2427 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2428 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2429 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2434 temp
= smbldap_talloc_single_attribute(
2435 ldap_state
->smbldap_state
->ldap_struct
,
2437 get_attr_key2string(groupmap_attr_list
,
2438 LDAP_ATTR_GIDNUMBER
),
2441 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2442 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2446 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2448 map
->gid
= (gid_t
)atol(temp
);
2451 temp
= smbldap_talloc_single_attribute(
2452 ldap_state
->smbldap_state
->ldap_struct
,
2454 get_attr_key2string(groupmap_attr_list
,
2455 LDAP_ATTR_GROUP_SID
),
2458 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2459 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2464 if (!string_to_sid(&map
->sid
, temp
)) {
2465 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2471 temp
= smbldap_talloc_single_attribute(
2472 ldap_state
->smbldap_state
->ldap_struct
,
2474 get_attr_key2string(groupmap_attr_list
,
2475 LDAP_ATTR_GROUP_TYPE
),
2478 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2479 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2483 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2485 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2486 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2487 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2493 temp
= smbldap_talloc_single_attribute(
2494 ldap_state
->smbldap_state
->ldap_struct
,
2496 get_attr_key2string(groupmap_attr_list
,
2497 LDAP_ATTR_DISPLAY_NAME
),
2500 temp
= smbldap_talloc_single_attribute(
2501 ldap_state
->smbldap_state
->ldap_struct
,
2503 get_attr_key2string(groupmap_attr_list
,
2507 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2508 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2513 fstrcpy(map
->nt_name
, temp
);
2516 temp
= smbldap_talloc_single_attribute(
2517 ldap_state
->smbldap_state
->ldap_struct
,
2519 get_attr_key2string(groupmap_attr_list
,
2523 temp
= talloc_strdup(ctx
, "");
2529 fstrcpy(map
->comment
, temp
);
2531 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2532 store_gid_sid_cache(&map
->sid
, map
->gid
);
2533 idmap_cache_set_sid2gid(&map
->sid
, map
->gid
);
2540 /**********************************************************************
2541 *********************************************************************/
2543 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2547 struct ldapsam_privates
*ldap_state
=
2548 (struct ldapsam_privates
*)methods
->private_data
;
2549 LDAPMessage
*result
= NULL
;
2550 LDAPMessage
*entry
= NULL
;
2553 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2555 return NT_STATUS_NO_SUCH_GROUP
;
2558 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2561 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2563 ldap_msgfree(result
);
2564 return NT_STATUS_NO_SUCH_GROUP
;
2568 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2569 "count=%d\n", filter
, count
));
2570 ldap_msgfree(result
);
2571 return NT_STATUS_NO_SUCH_GROUP
;
2574 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2577 ldap_msgfree(result
);
2578 return NT_STATUS_UNSUCCESSFUL
;
2581 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2582 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2583 "group filter %s\n", filter
));
2584 ldap_msgfree(result
);
2585 return NT_STATUS_NO_SUCH_GROUP
;
2588 ldap_msgfree(result
);
2589 return NT_STATUS_OK
;
2592 /**********************************************************************
2593 *********************************************************************/
2595 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2598 char *filter
= NULL
;
2602 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2604 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2605 sid_to_fstring(tmp
, &sid
)) < 0) {
2606 return NT_STATUS_NO_MEMORY
;
2609 status
= ldapsam_getgroup(methods
, filter
, map
);
2614 /**********************************************************************
2615 *********************************************************************/
2617 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2620 char *filter
= NULL
;
2623 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2625 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2626 (unsigned long)gid
) < 0) {
2627 return NT_STATUS_NO_MEMORY
;
2630 status
= ldapsam_getgroup(methods
, filter
, map
);
2635 /**********************************************************************
2636 *********************************************************************/
2638 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2641 char *filter
= NULL
;
2642 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2646 return NT_STATUS_NO_MEMORY
;
2649 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2651 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2652 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2654 TALLOC_FREE(escape_name
);
2655 return NT_STATUS_NO_MEMORY
;
2658 TALLOC_FREE(escape_name
);
2659 status
= ldapsam_getgroup(methods
, filter
, map
);
2664 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2666 const DOM_SID
*domain_sid
,
2672 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2673 str
, sizeof(str
)-1)) {
2674 DEBUG(10, ("Could not find sambaSID attribute\n"));
2678 if (!string_to_sid(&sid
, str
)) {
2679 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2683 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2684 DEBUG(10, ("SID %s is not in expected domain %s\n",
2685 str
, sid_string_dbg(domain_sid
)));
2689 if (!sid_peek_rid(&sid
, rid
)) {
2690 DEBUG(10, ("Could not peek into RID\n"));
2697 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2698 TALLOC_CTX
*mem_ctx
,
2699 const DOM_SID
*group
,
2700 uint32
**pp_member_rids
,
2701 size_t *p_num_members
)
2703 struct ldapsam_privates
*ldap_state
=
2704 (struct ldapsam_privates
*)methods
->private_data
;
2705 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2706 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2707 const char *sid_attrs
[] = { "sambaSID", NULL
};
2708 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2709 LDAPMessage
*result
= NULL
;
2712 char **values
= NULL
;
2717 *pp_member_rids
= NULL
;
2720 filter
= talloc_asprintf(mem_ctx
,
2721 "(&(objectClass=%s)"
2724 LDAP_OBJ_POSIXGROUP
,
2726 sid_string_talloc(mem_ctx
, group
));
2727 if (filter
== NULL
) {
2728 ret
= NT_STATUS_NO_MEMORY
;
2732 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2733 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2736 if (rc
!= LDAP_SUCCESS
)
2739 talloc_autofree_ldapmsg(mem_ctx
, result
);
2741 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2744 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2745 sid_string_dbg(group
)));
2746 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2751 ret
= NT_STATUS_NO_SUCH_GROUP
;
2755 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2759 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2761 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2762 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2766 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2768 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2770 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2771 if (filter
== NULL
) {
2772 ret
= NT_STATUS_NO_MEMORY
;
2776 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2777 char *escape_memberuid
;
2779 escape_memberuid
= escape_ldap_string(talloc_tos(),
2781 if (escape_memberuid
== NULL
) {
2782 ret
= NT_STATUS_NO_MEMORY
;
2786 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2787 TALLOC_FREE(escape_memberuid
);
2788 if (filter
== NULL
) {
2789 ret
= NT_STATUS_NO_MEMORY
;
2794 filter
= talloc_asprintf_append_buffer(filter
, "))");
2795 if (filter
== NULL
) {
2796 ret
= NT_STATUS_NO_MEMORY
;
2800 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2801 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2804 if (rc
!= LDAP_SUCCESS
)
2807 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2808 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2810 talloc_autofree_ldapmsg(mem_ctx
, result
);
2812 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2814 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2820 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2824 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2825 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2826 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2830 if (!string_to_sid(&sid
, sidstr
))
2833 if (!sid_check_is_in_our_domain(&sid
)) {
2834 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2835 "in our domain\n"));
2836 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2840 sid_peek_rid(&sid
, &rid
);
2842 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2844 ret
= NT_STATUS_NO_MEMORY
;
2850 filter
= talloc_asprintf(mem_ctx
,
2851 "(&(objectClass=%s)"
2853 LDAP_OBJ_SAMBASAMACCOUNT
,
2856 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2857 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2860 if (rc
!= LDAP_SUCCESS
)
2863 talloc_autofree_ldapmsg(mem_ctx
, result
);
2865 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2867 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2871 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2873 get_global_sam_sid(),
2875 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2876 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2880 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2882 ret
= NT_STATUS_NO_MEMORY
;
2892 ldap_value_free(values
);
2897 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2898 TALLOC_CTX
*mem_ctx
,
2902 size_t *p_num_groups
)
2904 struct ldapsam_privates
*ldap_state
=
2905 (struct ldapsam_privates
*)methods
->private_data
;
2906 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2908 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2911 LDAPMessage
*result
= NULL
;
2913 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2914 size_t num_sids
, num_gids
;
2916 gid_t primary_gid
= -1;
2921 if (pdb_get_username(user
) == NULL
) {
2922 return NT_STATUS_INVALID_PARAMETER
;
2925 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2926 if (escape_name
== NULL
)
2927 return NT_STATUS_NO_MEMORY
;
2929 if (user
->unix_pw
) {
2930 primary_gid
= user
->unix_pw
->pw_gid
;
2932 /* retrieve the users primary gid */
2933 filter
= talloc_asprintf(mem_ctx
,
2934 "(&(objectClass=%s)(uid=%s))",
2935 LDAP_OBJ_SAMBASAMACCOUNT
,
2937 if (filter
== NULL
) {
2938 ret
= NT_STATUS_NO_MEMORY
;
2942 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2943 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2945 if (rc
!= LDAP_SUCCESS
)
2948 talloc_autofree_ldapmsg(mem_ctx
, result
);
2950 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2954 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2955 ret
= NT_STATUS_NO_SUCH_USER
;
2958 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2960 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2962 DEBUG (1, ("Unable to find the member's gid!\n"));
2963 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2966 primary_gid
= strtoul(gidstr
, NULL
, 10);
2969 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2970 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2975 filter
= talloc_asprintf(mem_ctx
,
2976 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2977 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2978 if (filter
== NULL
) {
2979 ret
= NT_STATUS_NO_MEMORY
;
2983 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2984 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2986 if (rc
!= LDAP_SUCCESS
)
2989 talloc_autofree_ldapmsg(mem_ctx
, result
);
2997 /* We need to add the primary group as the first gid/sid */
2999 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
3000 ret
= NT_STATUS_NO_MEMORY
;
3004 /* This sid will be replaced later */
3006 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
3008 if (!NT_STATUS_IS_OK(ret
)) {
3012 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
3014 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
3021 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3023 str
, sizeof(str
)-1))
3026 if (!string_to_sid(&sid
, str
))
3029 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3031 str
, sizeof(str
)-1))
3034 gid
= strtoul(str
, &end
, 10);
3036 if (PTR_DIFF(end
, str
) != strlen(str
))
3039 if (gid
== primary_gid
) {
3040 sid_copy(&(*pp_sids
)[0], &sid
);
3042 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3044 ret
= NT_STATUS_NO_MEMORY
;
3047 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3049 if (!NT_STATUS_IS_OK(ret
)) {
3055 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3056 DEBUG(3, ("primary group of [%s] not found\n",
3057 pdb_get_username(user
)));
3061 *p_num_groups
= num_sids
;
3067 TALLOC_FREE(escape_name
);
3071 /**********************************************************************
3072 * Augment a posixGroup object with a sambaGroupMapping domgroup
3073 *********************************************************************/
3075 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3076 struct ldapsam_privates
*ldap_state
,
3079 const char *filter
, *dn
;
3080 LDAPMessage
*msg
, *entry
;
3084 filter
= talloc_asprintf(mem_ctx
,
3085 "(&(objectClass=%s)(gidNumber=%u))",
3086 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3087 if (filter
== NULL
) {
3088 return NT_STATUS_NO_MEMORY
;
3091 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3092 get_attr_list(mem_ctx
, groupmap_attr_list
),
3094 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3096 if ((rc
!= LDAP_SUCCESS
) ||
3097 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3098 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3099 return NT_STATUS_NO_SUCH_GROUP
;
3102 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3104 return NT_STATUS_NO_MEMORY
;
3108 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3110 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3111 sid_string_talloc(mem_ctx
, &map
->sid
));
3112 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3113 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3114 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3116 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3118 talloc_autofree_ldapmod(mem_ctx
, mods
);
3120 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3121 if (rc
!= LDAP_SUCCESS
) {
3122 return NT_STATUS_ACCESS_DENIED
;
3125 return NT_STATUS_OK
;
3128 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3131 struct ldapsam_privates
*ldap_state
=
3132 (struct ldapsam_privates
*)methods
->private_data
;
3133 LDAPMessage
*msg
= NULL
;
3134 LDAPMod
**mods
= NULL
;
3135 const char *attrs
[] = { NULL
};
3139 TALLOC_CTX
*mem_ctx
;
3146 mem_ctx
= talloc_new(NULL
);
3147 if (mem_ctx
== NULL
) {
3148 DEBUG(0, ("talloc_new failed\n"));
3149 return NT_STATUS_NO_MEMORY
;
3152 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3153 sid_string_talloc(mem_ctx
, &map
->sid
));
3154 if (filter
== NULL
) {
3155 result
= NT_STATUS_NO_MEMORY
;
3159 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3160 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3161 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3163 if ((rc
== LDAP_SUCCESS
) &&
3164 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3166 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3167 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3168 result
= NT_STATUS_GROUP_EXISTS
;
3172 switch (map
->sid_name_use
) {
3174 case SID_NAME_DOM_GRP
:
3175 /* To map a domain group we need to have a posix group
3177 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3181 case SID_NAME_ALIAS
:
3182 if (!sid_check_is_in_our_domain(&map
->sid
)
3183 && !sid_check_is_in_builtin(&map
->sid
) )
3185 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3186 sid_string_dbg(&map
->sid
)));
3187 result
= NT_STATUS_INVALID_PARAMETER
;
3193 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3194 sid_type_lookup(map
->sid_name_use
)));
3195 result
= NT_STATUS_INVALID_PARAMETER
;
3199 /* Domain groups have been mapped in a separate routine, we have to
3200 * create an alias now */
3202 if (map
->gid
== -1) {
3203 DEBUG(10, ("Refusing to map gid==-1\n"));
3204 result
= NT_STATUS_INVALID_PARAMETER
;
3208 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3209 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3210 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3211 result
= NT_STATUS_GROUP_EXISTS
;
3215 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3216 * the best we can get out of LDAP. */
3218 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3219 sid_string_talloc(mem_ctx
, &map
->sid
),
3220 lp_ldap_group_suffix());
3222 result
= NT_STATUS_NO_MEMORY
;
3228 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3229 LDAP_OBJ_SID_ENTRY
);
3230 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3232 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3233 sid_string_talloc(mem_ctx
, &map
->sid
));
3234 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3235 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3236 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3238 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3240 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3241 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3242 talloc_autofree_ldapmod(mem_ctx
, mods
);
3244 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3246 result
= (rc
== LDAP_SUCCESS
) ?
3247 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3250 TALLOC_FREE(mem_ctx
);
3254 /**********************************************************************
3255 * Update a group mapping entry. We're quite strict about what can be changed:
3256 * Only the description and displayname may be changed. It simply does not
3257 * make any sense to change the SID, gid or the type in a mapping.
3258 *********************************************************************/
3260 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3263 struct ldapsam_privates
*ldap_state
=
3264 (struct ldapsam_privates
*)methods
->private_data
;
3266 const char *filter
, *dn
;
3267 LDAPMessage
*msg
= NULL
;
3268 LDAPMessage
*entry
= NULL
;
3269 LDAPMod
**mods
= NULL
;
3270 TALLOC_CTX
*mem_ctx
;
3273 mem_ctx
= talloc_new(NULL
);
3274 if (mem_ctx
== NULL
) {
3275 DEBUG(0, ("talloc_new failed\n"));
3276 return NT_STATUS_NO_MEMORY
;
3279 /* Make 100% sure that sid, gid and type are not changed by looking up
3280 * exactly the values we're given in LDAP. */
3282 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3283 "(sambaSid=%s)(gidNumber=%u)"
3284 "(sambaGroupType=%d))",
3286 sid_string_talloc(mem_ctx
, &map
->sid
),
3287 (unsigned int)map
->gid
, map
->sid_name_use
);
3288 if (filter
== NULL
) {
3289 result
= NT_STATUS_NO_MEMORY
;
3293 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3294 get_attr_list(mem_ctx
, groupmap_attr_list
),
3296 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3298 if ((rc
!= LDAP_SUCCESS
) ||
3299 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3300 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3301 result
= NT_STATUS_NO_SUCH_GROUP
;
3305 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3308 result
= NT_STATUS_NO_MEMORY
;
3313 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3315 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3317 talloc_autofree_ldapmod(mem_ctx
, mods
);
3320 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3321 "nothing to do\n"));
3322 result
= NT_STATUS_OK
;
3326 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3328 if (rc
!= LDAP_SUCCESS
) {
3329 result
= NT_STATUS_ACCESS_DENIED
;
3333 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3334 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3336 result
= NT_STATUS_OK
;
3339 TALLOC_FREE(mem_ctx
);
3343 /**********************************************************************
3344 *********************************************************************/
3346 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3349 struct ldapsam_privates
*priv
=
3350 (struct ldapsam_privates
*)methods
->private_data
;
3351 LDAPMessage
*msg
, *entry
;
3354 TALLOC_CTX
*mem_ctx
;
3357 mem_ctx
= talloc_new(NULL
);
3358 if (mem_ctx
== NULL
) {
3359 DEBUG(0, ("talloc_new failed\n"));
3360 return NT_STATUS_NO_MEMORY
;
3363 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3364 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3365 sid_string_talloc(mem_ctx
, &sid
));
3366 if (filter
== NULL
) {
3367 result
= NT_STATUS_NO_MEMORY
;
3370 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3371 get_attr_list(mem_ctx
, groupmap_attr_list
),
3373 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3375 if ((rc
!= LDAP_SUCCESS
) ||
3376 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3377 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3378 result
= NT_STATUS_NO_SUCH_GROUP
;
3382 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3383 get_attr_list(mem_ctx
,
3384 groupmap_attr_list_to_delete
));
3386 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3387 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3388 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3389 const char *attrs
[] = { "sambaGroupType", "description",
3390 "displayName", "sambaSIDList",
3393 /* Second try. Don't delete the sambaSID attribute, this is
3394 for "old" entries that are tacked on a winbind
3397 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3398 LDAP_OBJ_GROUPMAP
, attrs
);
3401 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3402 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3403 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3404 const char *attrs
[] = { "sambaGroupType", "description",
3405 "displayName", "sambaSIDList",
3406 "gidNumber", NULL
};
3408 /* Third try. This is a post-3.0.21 alias (containing only
3409 * sambaSidEntry and sambaGroupMapping classes), we also have
3410 * to delete the gidNumber attribute, only the sambaSidEntry
3413 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3414 LDAP_OBJ_GROUPMAP
, attrs
);
3417 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3420 TALLOC_FREE(mem_ctx
);
3424 /**********************************************************************
3425 *********************************************************************/
3427 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3430 struct ldapsam_privates
*ldap_state
=
3431 (struct ldapsam_privates
*)my_methods
->private_data
;
3432 char *filter
= NULL
;
3434 const char **attr_list
;
3436 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3438 return NT_STATUS_NO_MEMORY
;
3440 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3441 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3442 LDAP_SCOPE_SUBTREE
, filter
,
3443 attr_list
, 0, &ldap_state
->result
);
3444 TALLOC_FREE(attr_list
);
3446 if (rc
!= LDAP_SUCCESS
) {
3447 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3448 ldap_err2string(rc
)));
3449 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3450 lp_ldap_suffix(), filter
));
3451 ldap_msgfree(ldap_state
->result
);
3452 ldap_state
->result
= NULL
;
3453 TALLOC_FREE(filter
);
3454 return NT_STATUS_UNSUCCESSFUL
;
3457 TALLOC_FREE(filter
);
3459 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3460 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3461 ldap_state
->result
)));
3464 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3465 ldap_state
->result
);
3466 ldap_state
->index
= 0;
3468 return NT_STATUS_OK
;
3471 /**********************************************************************
3472 *********************************************************************/
3474 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3476 ldapsam_endsampwent(my_methods
);
3479 /**********************************************************************
3480 *********************************************************************/
3482 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3485 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3486 struct ldapsam_privates
*ldap_state
=
3487 (struct ldapsam_privates
*)my_methods
->private_data
;
3491 if (!ldap_state
->entry
)
3494 ldap_state
->index
++;
3495 bret
= init_group_from_ldap(ldap_state
, map
,
3499 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3503 return NT_STATUS_OK
;
3506 /**********************************************************************
3507 *********************************************************************/
3509 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3510 const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
,
3511 GROUP_MAP
**pp_rmap
,
3512 size_t *p_num_entries
,
3521 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3522 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3524 return NT_STATUS_ACCESS_DENIED
;
3527 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3528 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3529 sid_name_use
!= map
.sid_name_use
) {
3530 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3531 "not of the requested type\n", map
.nt_name
));
3534 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3535 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3536 "non mapped\n", map
.nt_name
));
3540 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3542 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3543 "enlarge group map!\n"));
3544 return NT_STATUS_UNSUCCESSFUL
;
3547 (*pp_rmap
)[entries
] = map
;
3552 ldapsam_endsamgrent(methods
);
3554 *p_num_entries
= entries
;
3556 return NT_STATUS_OK
;
3559 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3560 const DOM_SID
*alias
,
3561 const DOM_SID
*member
,
3564 struct ldapsam_privates
*ldap_state
=
3565 (struct ldapsam_privates
*)methods
->private_data
;
3567 LDAPMessage
*result
= NULL
;
3568 LDAPMessage
*entry
= NULL
;
3570 LDAPMod
**mods
= NULL
;
3572 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3575 char *filter
= NULL
;
3577 if (sid_check_is_in_builtin(alias
)) {
3578 type
= SID_NAME_ALIAS
;
3581 if (sid_check_is_in_our_domain(alias
)) {
3582 type
= SID_NAME_ALIAS
;
3585 if (type
== SID_NAME_USE_NONE
) {
3586 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3587 sid_string_dbg(alias
)));
3588 return NT_STATUS_NO_SUCH_ALIAS
;
3591 if (asprintf(&filter
,
3592 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3593 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3595 return NT_STATUS_NO_MEMORY
;
3598 if (ldapsam_search_one_group(ldap_state
, filter
,
3599 &result
) != LDAP_SUCCESS
) {
3601 return NT_STATUS_NO_SUCH_ALIAS
;
3604 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3608 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3609 ldap_msgfree(result
);
3611 return NT_STATUS_NO_SUCH_ALIAS
;
3615 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3616 "filter %s: count=%d\n", filter
, count
));
3617 ldap_msgfree(result
);
3619 return NT_STATUS_NO_SUCH_ALIAS
;
3624 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3628 ldap_msgfree(result
);
3629 return NT_STATUS_UNSUCCESSFUL
;
3632 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3634 ldap_msgfree(result
);
3635 return NT_STATUS_UNSUCCESSFUL
;
3638 smbldap_set_mod(&mods
, modop
,
3639 get_attr_key2string(groupmap_attr_list
,
3640 LDAP_ATTR_SID_LIST
),
3641 sid_to_fstring(tmp
, member
));
3643 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3645 ldap_mods_free(mods
, True
);
3646 ldap_msgfree(result
);
3649 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3650 return NT_STATUS_MEMBER_IN_ALIAS
;
3653 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3654 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3657 if (rc
!= LDAP_SUCCESS
) {
3658 return NT_STATUS_UNSUCCESSFUL
;
3661 return NT_STATUS_OK
;
3664 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3665 const DOM_SID
*alias
,
3666 const DOM_SID
*member
)
3668 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3671 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3672 const DOM_SID
*alias
,
3673 const DOM_SID
*member
)
3675 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3679 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3680 const DOM_SID
*alias
,
3681 TALLOC_CTX
*mem_ctx
,
3682 DOM_SID
**pp_members
,
3683 size_t *p_num_members
)
3685 struct ldapsam_privates
*ldap_state
=
3686 (struct ldapsam_privates
*)methods
->private_data
;
3687 LDAPMessage
*result
= NULL
;
3688 LDAPMessage
*entry
= NULL
;
3690 char **values
= NULL
;
3692 char *filter
= NULL
;
3693 size_t num_members
= 0;
3694 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3700 if (sid_check_is_in_builtin(alias
)) {
3701 type
= SID_NAME_ALIAS
;
3704 if (sid_check_is_in_our_domain(alias
)) {
3705 type
= SID_NAME_ALIAS
;
3708 if (type
== SID_NAME_USE_NONE
) {
3709 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3710 sid_string_dbg(alias
)));
3711 return NT_STATUS_NO_SUCH_ALIAS
;
3714 if (asprintf(&filter
,
3715 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3716 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3718 return NT_STATUS_NO_MEMORY
;
3721 if (ldapsam_search_one_group(ldap_state
, filter
,
3722 &result
) != LDAP_SUCCESS
) {
3724 return NT_STATUS_NO_SUCH_ALIAS
;
3727 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3731 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3732 ldap_msgfree(result
);
3734 return NT_STATUS_NO_SUCH_ALIAS
;
3738 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3739 "filter %s: count=%d\n", filter
, count
));
3740 ldap_msgfree(result
);
3742 return NT_STATUS_NO_SUCH_ALIAS
;
3747 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3751 ldap_msgfree(result
);
3752 return NT_STATUS_UNSUCCESSFUL
;
3755 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3757 get_attr_key2string(groupmap_attr_list
,
3758 LDAP_ATTR_SID_LIST
));
3760 if (values
== NULL
) {
3761 ldap_msgfree(result
);
3762 return NT_STATUS_OK
;
3765 count
= ldap_count_values(values
);
3767 for (i
=0; i
<count
; i
++) {
3771 if (!string_to_sid(&member
, values
[i
]))
3774 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3776 if (!NT_STATUS_IS_OK(status
)) {
3777 ldap_value_free(values
);
3778 ldap_msgfree(result
);
3783 *p_num_members
= num_members
;
3784 ldap_value_free(values
);
3785 ldap_msgfree(result
);
3787 return NT_STATUS_OK
;
3790 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3791 TALLOC_CTX
*mem_ctx
,
3792 const DOM_SID
*domain_sid
,
3793 const DOM_SID
*members
,
3795 uint32
**pp_alias_rids
,
3796 size_t *p_num_alias_rids
)
3798 struct ldapsam_privates
*ldap_state
=
3799 (struct ldapsam_privates
*)methods
->private_data
;
3802 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3804 LDAPMessage
*result
= NULL
;
3805 LDAPMessage
*entry
= NULL
;
3809 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3810 bool is_builtin
= false;
3811 bool sid_added
= false;
3813 *pp_alias_rids
= NULL
;
3814 *p_num_alias_rids
= 0;
3816 if (sid_check_is_builtin(domain_sid
)) {
3818 type
= SID_NAME_ALIAS
;
3821 if (sid_check_is_domain(domain_sid
)) {
3822 type
= SID_NAME_ALIAS
;
3825 if (type
== SID_NAME_USE_NONE
) {
3826 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3827 sid_string_dbg(domain_sid
)));
3828 return NT_STATUS_UNSUCCESSFUL
;
3831 if (num_members
== 0) {
3832 return NT_STATUS_OK
;
3835 filter
= talloc_asprintf(mem_ctx
,
3836 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3837 LDAP_OBJ_GROUPMAP
, type
);
3839 for (i
=0; i
<num_members
; i
++)
3840 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3842 sid_string_talloc(mem_ctx
,
3845 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3847 if (filter
== NULL
) {
3848 return NT_STATUS_NO_MEMORY
;
3852 ldap_state
->search_cache
.filter
&&
3853 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3854 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3855 result
= ldap_state
->search_cache
.result
;
3856 ldap_state
->search_cache
.result
= NULL
;
3858 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3859 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3860 if (rc
!= LDAP_SUCCESS
) {
3861 return NT_STATUS_UNSUCCESSFUL
;
3863 talloc_autofree_ldapmsg(filter
, result
);
3866 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3868 for (entry
= ldap_first_entry(ldap_struct
, result
);
3870 entry
= ldap_next_entry(ldap_struct
, entry
))
3876 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3882 if (!string_to_sid(&sid
, sid_str
))
3885 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3890 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3891 p_num_alias_rids
)) {
3892 return NT_STATUS_NO_MEMORY
;
3896 if (!is_builtin
&& !sid_added
) {
3897 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3899 * Note: result is a talloc child of filter because of the
3900 * talloc_autofree_ldapmsg() usage
3902 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3903 ldap_state
->search_cache
.result
= result
;
3906 return NT_STATUS_OK
;
3909 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3910 enum pdb_policy_type type
,
3913 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3915 LDAPMod
**mods
= NULL
;
3916 fstring value_string
;
3917 const char *policy_attr
= NULL
;
3919 struct ldapsam_privates
*ldap_state
=
3920 (struct ldapsam_privates
*)methods
->private_data
;
3922 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3924 if (!ldap_state
->domain_dn
) {
3925 return NT_STATUS_INVALID_PARAMETER
;
3928 policy_attr
= get_account_policy_attr(type
);
3929 if (policy_attr
== NULL
) {
3930 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3935 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3937 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3939 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3942 ldap_mods_free(mods
, True
);
3944 if (rc
!= LDAP_SUCCESS
) {
3948 if (!cache_account_policy_set(type
, value
)) {
3949 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3950 "update local tdb cache\n"));
3954 return NT_STATUS_OK
;
3957 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3958 enum pdb_policy_type type
,
3961 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3965 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3966 enum pdb_policy_type type
,
3969 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3970 LDAPMessage
*result
= NULL
;
3971 LDAPMessage
*entry
= NULL
;
3976 const char *policy_attr
= NULL
;
3978 struct ldapsam_privates
*ldap_state
=
3979 (struct ldapsam_privates
*)methods
->private_data
;
3981 const char *attrs
[2];
3983 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3985 if (!ldap_state
->domain_dn
) {
3986 return NT_STATUS_INVALID_PARAMETER
;
3989 policy_attr
= get_account_policy_attr(type
);
3991 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3992 "policy index: %d\n", type
));
3996 attrs
[0] = policy_attr
;
3999 filter
= talloc_asprintf(NULL
, "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
4000 if (filter
== NULL
) {
4001 return NT_STATUS_NO_MEMORY
;
4003 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
4004 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
4007 if (rc
!= LDAP_SUCCESS
) {
4011 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4016 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4017 if (entry
== NULL
) {
4021 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4026 *value
= (uint32
)atol(vals
[0]);
4028 ntstatus
= NT_STATUS_OK
;
4032 ldap_value_free(vals
);
4033 ldap_msgfree(result
);
4038 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4040 - if user hasn't decided to use account policies inside LDAP just reuse the
4043 - if there is a valid cache entry, return that
4044 - if there is an LDAP entry, update cache and return
4045 - otherwise set to default, update cache and return
4049 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4050 enum pdb_policy_type type
,
4053 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4055 if (cache_account_policy_get(type
, value
)) {
4056 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4058 return NT_STATUS_OK
;
4061 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4063 if (NT_STATUS_IS_OK(ntstatus
)) {
4067 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4071 /* should we automagically migrate old tdb value here ? */
4072 if (account_policy_get(type
, value
))
4075 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4076 "default\n", type
));
4079 if (!account_policy_get_default(type
, value
)) {
4085 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4086 if (!NT_STATUS_IS_OK(ntstatus
)) {
4092 if (!cache_account_policy_set(type
, *value
)) {
4093 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4094 "tdb as a cache\n"));
4095 return NT_STATUS_UNSUCCESSFUL
;
4098 return NT_STATUS_OK
;
4101 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4102 const DOM_SID
*domain_sid
,
4106 enum lsa_SidType
*attrs
)
4108 struct ldapsam_privates
*ldap_state
=
4109 (struct ldapsam_privates
*)methods
->private_data
;
4110 LDAPMessage
*msg
= NULL
;
4112 char *allsids
= NULL
;
4113 int i
, rc
, num_mapped
;
4114 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4115 TALLOC_CTX
*mem_ctx
;
4119 mem_ctx
= talloc_new(NULL
);
4120 if (mem_ctx
== NULL
) {
4121 DEBUG(0, ("talloc_new failed\n"));
4125 if (!sid_check_is_builtin(domain_sid
) &&
4126 !sid_check_is_domain(domain_sid
)) {
4127 result
= NT_STATUS_INVALID_PARAMETER
;
4131 if (num_rids
== 0) {
4132 result
= NT_STATUS_NONE_MAPPED
;
4136 for (i
=0; i
<num_rids
; i
++)
4137 attrs
[i
] = SID_NAME_UNKNOWN
;
4139 allsids
= talloc_strdup(mem_ctx
, "");
4140 if (allsids
== NULL
) {
4144 for (i
=0; i
<num_rids
; i
++) {
4146 sid_compose(&sid
, domain_sid
, rids
[i
]);
4147 allsids
= talloc_asprintf_append_buffer(
4148 allsids
, "(sambaSid=%s)",
4149 sid_string_talloc(mem_ctx
, &sid
));
4150 if (allsids
== NULL
) {
4155 /* First look for users */
4159 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4161 filter
= talloc_asprintf(
4162 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4163 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4165 if (filter
== NULL
) {
4169 rc
= smbldap_search(ldap_state
->smbldap_state
,
4170 lp_ldap_user_suffix(),
4171 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4173 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4176 if (rc
!= LDAP_SUCCESS
)
4179 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4182 for (entry
= ldap_first_entry(ld
, msg
);
4184 entry
= ldap_next_entry(ld
, entry
)) {
4189 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4191 DEBUG(2, ("Could not find sid from ldap entry\n"));
4195 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4198 DEBUG(2, ("Could not retrieve uid attribute\n"));
4202 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4203 if (rid
== rids
[rid_index
])
4207 if (rid_index
== num_rids
) {
4208 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4212 attrs
[rid_index
] = SID_NAME_USER
;
4213 names
[rid_index
] = name
;
4217 if (num_mapped
== num_rids
) {
4218 /* No need to look for groups anymore -- we're done */
4219 result
= NT_STATUS_OK
;
4223 /* Same game for groups */
4227 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4228 "sambaGroupType", NULL
};
4230 filter
= talloc_asprintf(
4231 mem_ctx
, "(&(objectClass=%s)(|%s))",
4232 LDAP_OBJ_GROUPMAP
, allsids
);
4233 if (filter
== NULL
) {
4237 rc
= smbldap_search(ldap_state
->smbldap_state
,
4239 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4241 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4244 if (rc
!= LDAP_SUCCESS
)
4247 /* ldap_struct might have changed due to a reconnect */
4249 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4251 /* For consistency checks, we already checked we're only domain or builtin */
4253 is_builtin
= sid_check_is_builtin(domain_sid
);
4255 for (entry
= ldap_first_entry(ld
, msg
);
4257 entry
= ldap_next_entry(ld
, entry
))
4262 enum lsa_SidType type
;
4263 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4265 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4268 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4273 type
= (enum lsa_SidType
)atol(attr
);
4275 /* Consistency checks */
4276 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4277 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4278 (type
!= SID_NAME_DOM_GRP
)))) {
4279 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4282 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4284 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4288 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4291 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4293 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4297 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4302 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4303 if (rid
== rids
[rid_index
])
4307 if (rid_index
== num_rids
) {
4308 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4312 attrs
[rid_index
] = type
;
4313 names
[rid_index
] = attr
;
4317 result
= NT_STATUS_NONE_MAPPED
;
4320 result
= (num_mapped
== num_rids
) ?
4321 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4323 TALLOC_FREE(mem_ctx
);
4327 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4329 char *filter
= NULL
;
4330 char *escaped
= NULL
;
4331 char *result
= NULL
;
4333 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4334 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4338 escaped
= escape_ldap_string(talloc_tos(), username
);
4339 if (escaped
== NULL
) goto done
;
4341 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4345 TALLOC_FREE(escaped
);
4350 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4354 const char **result
;
4356 va_start(ap
, mem_ctx
);
4357 while (va_arg(ap
, const char *) != NULL
)
4361 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4365 va_start(ap
, mem_ctx
);
4366 for (i
=0; i
<num
; i
++) {
4367 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4368 if (result
[i
] == NULL
) {
4369 talloc_free(result
);
4380 struct ldap_search_state
{
4381 struct smbldap_state
*connection
;
4391 void *pagedresults_cookie
;
4393 LDAPMessage
*entries
, *current_entry
;
4394 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4395 TALLOC_CTX
*mem_ctx
,
4396 LDAP
*ld
, LDAPMessage
*entry
,
4397 struct samr_displayentry
*result
);
4400 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4402 struct ldap_search_state
*state
=
4403 (struct ldap_search_state
*)search
->private_data
;
4405 int rc
= LDAP_OPERATIONS_ERROR
;
4407 state
->entries
= NULL
;
4409 if (state
->connection
->paged_results
) {
4410 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4411 state
->scope
, state
->filter
,
4412 state
->attrs
, state
->attrsonly
,
4413 lp_ldap_page_size(), &state
->entries
,
4414 &state
->pagedresults_cookie
);
4417 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4419 if (state
->entries
!= NULL
) {
4420 /* Left over from unsuccessful paged attempt */
4421 ldap_msgfree(state
->entries
);
4422 state
->entries
= NULL
;
4425 rc
= smbldap_search(state
->connection
, state
->base
,
4426 state
->scope
, state
->filter
, state
->attrs
,
4427 state
->attrsonly
, &state
->entries
);
4429 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4432 /* Ok, the server was lying. It told us it could do paged
4433 * searches when it could not. */
4434 state
->connection
->paged_results
= False
;
4437 ld
= state
->connection
->ldap_struct
;
4439 DEBUG(5, ("Don't have an LDAP connection right after a "
4443 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4445 if (state
->current_entry
== NULL
) {
4446 ldap_msgfree(state
->entries
);
4447 state
->entries
= NULL
;
4454 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4456 struct ldap_search_state
*state
=
4457 (struct ldap_search_state
*)search
->private_data
;
4460 if (!state
->connection
->paged_results
) {
4461 /* There is no next page when there are no paged results */
4465 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4466 state
->scope
, state
->filter
, state
->attrs
,
4467 state
->attrsonly
, lp_ldap_page_size(),
4469 &state
->pagedresults_cookie
);
4471 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4474 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4476 if (state
->current_entry
== NULL
) {
4477 ldap_msgfree(state
->entries
);
4478 state
->entries
= NULL
;
4485 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4486 struct samr_displayentry
*entry
)
4488 struct ldap_search_state
*state
=
4489 (struct ldap_search_state
*)search
->private_data
;
4493 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4496 if ((state
->entries
== NULL
) &&
4497 !ldapsam_search_nextpage(search
))
4500 result
= state
->ldap2displayentry(state
, search
,
4501 state
->connection
->ldap_struct
,
4502 state
->current_entry
, entry
);
4506 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4507 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4508 if (dn
!= NULL
) ldap_memfree(dn
);
4511 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4513 if (state
->current_entry
== NULL
) {
4514 ldap_msgfree(state
->entries
);
4515 state
->entries
= NULL
;
4518 if (!result
) goto retry
;
4523 static void ldapsam_search_end(struct pdb_search
*search
)
4525 struct ldap_search_state
*state
=
4526 (struct ldap_search_state
*)search
->private_data
;
4529 if (state
->pagedresults_cookie
== NULL
)
4532 if (state
->entries
!= NULL
)
4533 ldap_msgfree(state
->entries
);
4535 state
->entries
= NULL
;
4536 state
->current_entry
= NULL
;
4538 if (!state
->connection
->paged_results
)
4541 /* Tell the LDAP server we're not interested in the rest anymore. */
4543 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4544 state
->filter
, state
->attrs
,
4545 state
->attrsonly
, 0, &state
->entries
,
4546 &state
->pagedresults_cookie
);
4548 if (rc
!= LDAP_SUCCESS
)
4549 DEBUG(5, ("Could not end search properly\n"));
4554 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4555 TALLOC_CTX
*mem_ctx
,
4556 LDAP
*ld
, LDAPMessage
*entry
,
4557 struct samr_displayentry
*result
)
4560 size_t converted_size
;
4564 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4565 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4566 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4569 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4570 ldap_value_free(vals
);
4572 if ((state
->acct_flags
!= 0) &&
4573 ((state
->acct_flags
& acct_flags
) == 0))
4576 result
->acct_flags
= acct_flags
;
4577 result
->account_name
= "";
4578 result
->fullname
= "";
4579 result
->description
= "";
4581 vals
= ldap_get_values(ld
, entry
, "uid");
4582 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4583 DEBUG(5, ("\"uid\" not found\n"));
4586 if (!pull_utf8_talloc(mem_ctx
,
4587 CONST_DISCARD(char **, &result
->account_name
),
4588 vals
[0], &converted_size
))
4590 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4594 ldap_value_free(vals
);
4596 vals
= ldap_get_values(ld
, entry
, "displayName");
4597 if ((vals
== NULL
) || (vals
[0] == NULL
))
4598 DEBUG(8, ("\"displayName\" not found\n"));
4599 else if (!pull_utf8_talloc(mem_ctx
,
4600 CONST_DISCARD(char **, &result
->fullname
),
4601 vals
[0], &converted_size
))
4603 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4607 ldap_value_free(vals
);
4609 vals
= ldap_get_values(ld
, entry
, "description");
4610 if ((vals
== NULL
) || (vals
[0] == NULL
))
4611 DEBUG(8, ("\"description\" not found\n"));
4612 else if (!pull_utf8_talloc(mem_ctx
,
4613 CONST_DISCARD(char **, &result
->description
),
4614 vals
[0], &converted_size
))
4616 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4620 ldap_value_free(vals
);
4622 if ((result
->account_name
== NULL
) ||
4623 (result
->fullname
== NULL
) ||
4624 (result
->description
== NULL
)) {
4625 DEBUG(0, ("talloc failed\n"));
4629 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4630 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4631 DEBUG(0, ("\"objectSid\" not found\n"));
4635 if (!string_to_sid(&sid
, vals
[0])) {
4636 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4637 ldap_value_free(vals
);
4640 ldap_value_free(vals
);
4642 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4643 DEBUG(0, ("sid %s does not belong to our domain\n",
4644 sid_string_dbg(&sid
)));
4652 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4653 struct pdb_search
*search
,
4656 struct ldapsam_privates
*ldap_state
=
4657 (struct ldapsam_privates
*)methods
->private_data
;
4658 struct ldap_search_state
*state
;
4660 state
= talloc(search
, struct ldap_search_state
);
4661 if (state
== NULL
) {
4662 DEBUG(0, ("talloc failed\n"));
4666 state
->connection
= ldap_state
->smbldap_state
;
4668 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4669 state
->base
= lp_ldap_user_suffix();
4670 else if ((acct_flags
!= 0) &&
4671 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4672 state
->base
= lp_ldap_machine_suffix();
4674 state
->base
= lp_ldap_suffix();
4676 state
->acct_flags
= acct_flags
;
4677 state
->base
= talloc_strdup(search
, state
->base
);
4678 state
->scope
= LDAP_SCOPE_SUBTREE
;
4679 state
->filter
= get_ldap_filter(search
, "*");
4680 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4681 "displayName", "description",
4682 "sambaAcctFlags", NULL
);
4683 state
->attrsonly
= 0;
4684 state
->pagedresults_cookie
= NULL
;
4685 state
->entries
= NULL
;
4686 state
->ldap2displayentry
= ldapuser2displayentry
;
4688 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4689 DEBUG(0, ("talloc failed\n"));
4693 search
->private_data
= state
;
4694 search
->next_entry
= ldapsam_search_next_entry
;
4695 search
->search_end
= ldapsam_search_end
;
4697 return ldapsam_search_firstpage(search
);
4700 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4701 TALLOC_CTX
*mem_ctx
,
4702 LDAP
*ld
, LDAPMessage
*entry
,
4703 struct samr_displayentry
*result
)
4706 size_t converted_size
;
4710 result
->account_name
= "";
4711 result
->fullname
= "";
4712 result
->description
= "";
4715 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4716 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4717 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4719 ldap_value_free(vals
);
4724 group_type
= atoi(vals
[0]);
4726 if ((state
->group_type
!= 0) &&
4727 ((state
->group_type
!= group_type
))) {
4728 ldap_value_free(vals
);
4732 ldap_value_free(vals
);
4734 /* display name is the NT group name */
4736 vals
= ldap_get_values(ld
, entry
, "displayName");
4737 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4738 DEBUG(8, ("\"displayName\" not found\n"));
4740 /* fallback to the 'cn' attribute */
4741 vals
= ldap_get_values(ld
, entry
, "cn");
4742 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4743 DEBUG(5, ("\"cn\" not found\n"));
4746 if (!pull_utf8_talloc(mem_ctx
,
4747 CONST_DISCARD(char **,
4748 &result
->account_name
),
4749 vals
[0], &converted_size
))
4751 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4752 "failed: %s", strerror(errno
)));
4755 else if (!pull_utf8_talloc(mem_ctx
,
4756 CONST_DISCARD(char **,
4757 &result
->account_name
),
4758 vals
[0], &converted_size
))
4760 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4764 ldap_value_free(vals
);
4766 vals
= ldap_get_values(ld
, entry
, "description");
4767 if ((vals
== NULL
) || (vals
[0] == NULL
))
4768 DEBUG(8, ("\"description\" not found\n"));
4769 else if (!pull_utf8_talloc(mem_ctx
,
4770 CONST_DISCARD(char **, &result
->description
),
4771 vals
[0], &converted_size
))
4773 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4776 ldap_value_free(vals
);
4778 if ((result
->account_name
== NULL
) ||
4779 (result
->fullname
== NULL
) ||
4780 (result
->description
== NULL
)) {
4781 DEBUG(0, ("talloc failed\n"));
4785 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4786 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4787 DEBUG(0, ("\"objectSid\" not found\n"));
4789 ldap_value_free(vals
);
4794 if (!string_to_sid(&sid
, vals
[0])) {
4795 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4799 ldap_value_free(vals
);
4801 switch (group_type
) {
4802 case SID_NAME_DOM_GRP
:
4803 case SID_NAME_ALIAS
:
4805 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4806 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4808 DEBUG(0, ("%s is not in our domain\n",
4809 sid_string_dbg(&sid
)));
4815 DEBUG(0,("unkown group type: %d\n", group_type
));
4819 result
->acct_flags
= 0;
4824 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4825 struct pdb_search
*search
,
4827 enum lsa_SidType type
)
4829 struct ldapsam_privates
*ldap_state
=
4830 (struct ldapsam_privates
*)methods
->private_data
;
4831 struct ldap_search_state
*state
;
4834 state
= talloc(search
, struct ldap_search_state
);
4835 if (state
== NULL
) {
4836 DEBUG(0, ("talloc failed\n"));
4840 state
->connection
= ldap_state
->smbldap_state
;
4842 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4843 state
->connection
= ldap_state
->smbldap_state
;
4844 state
->scope
= LDAP_SCOPE_SUBTREE
;
4845 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4846 "(sambaGroupType=%d)(sambaSID=%s*))",
4848 type
, sid_to_fstring(tmp
, sid
));
4849 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4850 "displayName", "description",
4851 "sambaGroupType", NULL
);
4852 state
->attrsonly
= 0;
4853 state
->pagedresults_cookie
= NULL
;
4854 state
->entries
= NULL
;
4855 state
->group_type
= type
;
4856 state
->ldap2displayentry
= ldapgroup2displayentry
;
4858 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4859 DEBUG(0, ("talloc failed\n"));
4863 search
->private_data
= state
;
4864 search
->next_entry
= ldapsam_search_next_entry
;
4865 search
->search_end
= ldapsam_search_end
;
4867 return ldapsam_search_firstpage(search
);
4870 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4871 struct pdb_search
*search
)
4873 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4876 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4877 struct pdb_search
*search
,
4880 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4883 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4885 return PDB_CAP_STORE_RIDS
;
4888 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4891 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4893 LDAPMessage
*result
= NULL
;
4894 LDAPMessage
*entry
= NULL
;
4895 LDAPMod
**mods
= NULL
;
4902 TALLOC_CTX
*mem_ctx
;
4904 mem_ctx
= talloc_new(NULL
);
4905 if (mem_ctx
== NULL
) {
4906 DEBUG(0, ("talloc_new failed\n"));
4907 return NT_STATUS_NO_MEMORY
;
4910 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4911 get_global_sam_name(), False
);
4912 if (!NT_STATUS_IS_OK(status
)) {
4913 DEBUG(3, ("Could not get domain info: %s\n",
4914 nt_errstr(status
)));
4918 talloc_autofree_ldapmsg(mem_ctx
, result
);
4920 entry
= ldap_first_entry(priv2ld(priv
), result
);
4921 if (entry
== NULL
) {
4922 DEBUG(0, ("Could not get domain info entry\n"));
4923 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4927 /* Find the largest of the three attributes "sambaNextRid",
4928 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4929 concept of differentiating between user and group rids, and will
4930 use only "sambaNextRid" in the future. But for compatibility
4931 reasons I look if others have chosen different strategies -- VL */
4933 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4934 "sambaNextRid", mem_ctx
);
4935 if (value
!= NULL
) {
4936 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4937 nextRid
= MAX(nextRid
, tmp
);
4940 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4941 "sambaNextUserRid", mem_ctx
);
4942 if (value
!= NULL
) {
4943 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4944 nextRid
= MAX(nextRid
, tmp
);
4947 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4948 "sambaNextGroupRid", mem_ctx
);
4949 if (value
!= NULL
) {
4950 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4951 nextRid
= MAX(nextRid
, tmp
);
4955 nextRid
= BASE_RID
-1;
4960 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4961 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4962 talloc_autofree_ldapmod(mem_ctx
, mods
);
4964 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4965 status
= NT_STATUS_NO_MEMORY
;
4969 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4971 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4974 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4977 if (NT_STATUS_IS_OK(status
)) {
4981 TALLOC_FREE(mem_ctx
);
4985 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32
*rid
)
4989 for (i
=0; i
<10; i
++) {
4990 NTSTATUS result
= ldapsam_get_new_rid(
4991 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4992 if (NT_STATUS_IS_OK(result
)) {
4996 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
5000 /* The ldap update failed (maybe a race condition), retry */
5003 /* Tried 10 times, fail. */
5004 return NT_STATUS_ACCESS_DENIED
;
5007 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
5009 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
5010 return NT_STATUS_IS_OK(result
) ? True
: False
;
5013 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
5015 union unid_t
*id
, enum lsa_SidType
*type
)
5017 struct ldapsam_privates
*priv
=
5018 (struct ldapsam_privates
*)methods
->private_data
;
5020 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5022 LDAPMessage
*result
= NULL
;
5023 LDAPMessage
*entry
= NULL
;
5028 TALLOC_CTX
*mem_ctx
;
5030 mem_ctx
= talloc_new(NULL
);
5031 if (mem_ctx
== NULL
) {
5032 DEBUG(0, ("talloc_new failed\n"));
5036 filter
= talloc_asprintf(mem_ctx
,
5038 "(|(objectClass=%s)(objectClass=%s)))",
5039 sid_string_talloc(mem_ctx
, sid
),
5040 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5041 if (filter
== NULL
) {
5042 DEBUG(5, ("talloc_asprintf failed\n"));
5046 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5048 if (rc
!= LDAP_SUCCESS
) {
5051 talloc_autofree_ldapmsg(mem_ctx
, result
);
5053 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5054 DEBUG(10, ("Got %d entries, expected one\n",
5055 ldap_count_entries(priv2ld(priv
), result
)));
5059 entry
= ldap_first_entry(priv2ld(priv
), result
);
5061 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5062 "sambaGroupType", mem_ctx
);
5064 if (value
!= NULL
) {
5065 const char *gid_str
;
5068 gid_str
= smbldap_talloc_single_attribute(
5069 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5070 if (gid_str
== NULL
) {
5071 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5072 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5077 id
->gid
= strtoul(gid_str
, NULL
, 10);
5078 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
5079 store_gid_sid_cache(sid
, id
->gid
);
5080 idmap_cache_set_sid2gid(sid
, id
->gid
);
5085 /* It must be a user */
5087 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5088 "uidNumber", mem_ctx
);
5089 if (value
== NULL
) {
5090 DEBUG(1, ("Could not find uidNumber in %s\n",
5091 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5095 id
->uid
= strtoul(value
, NULL
, 10);
5096 *type
= SID_NAME_USER
;
5097 store_uid_sid_cache(sid
, id
->uid
);
5098 idmap_cache_set_sid2uid(sid
, id
->uid
);
5102 TALLOC_FREE(mem_ctx
);
5107 * Find the SID for a uid.
5108 * This is shortcut is only used if ldapsam:trusted is set to true.
5110 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5113 struct ldapsam_privates
*priv
=
5114 (struct ldapsam_privates
*)methods
->private_data
;
5116 const char *attrs
[] = { "sambaSID", NULL
};
5117 LDAPMessage
*result
= NULL
;
5118 LDAPMessage
*entry
= NULL
;
5120 char *user_sid_string
;
5121 struct dom_sid user_sid
;
5123 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5125 filter
= talloc_asprintf(tmp_ctx
,
5128 "(objectClass=%s))",
5130 LDAP_OBJ_POSIXACCOUNT
,
5131 LDAP_OBJ_SAMBASAMACCOUNT
);
5132 if (filter
== NULL
) {
5133 DEBUG(3, ("talloc_asprintf failed\n"));
5137 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5138 if (rc
!= LDAP_SUCCESS
) {
5141 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5143 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5144 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5145 ldap_count_entries(priv2ld(priv
), result
),
5146 (unsigned int)uid
));
5150 entry
= ldap_first_entry(priv2ld(priv
), result
);
5152 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5153 "sambaSID", tmp_ctx
);
5154 if (user_sid_string
== NULL
) {
5155 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5156 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5160 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5161 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5166 sid_copy(sid
, &user_sid
);
5168 store_uid_sid_cache(sid
, uid
);
5169 idmap_cache_set_sid2uid(sid
, uid
);
5174 TALLOC_FREE(tmp_ctx
);
5179 * Find the SID for a gid.
5180 * This is shortcut is only used if ldapsam:trusted is set to true.
5182 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5185 struct ldapsam_privates
*priv
=
5186 (struct ldapsam_privates
*)methods
->private_data
;
5188 const char *attrs
[] = { "sambaSID", NULL
};
5189 LDAPMessage
*result
= NULL
;
5190 LDAPMessage
*entry
= NULL
;
5192 char *group_sid_string
;
5195 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5197 filter
= talloc_asprintf(tmp_ctx
,
5199 "(objectClass=%s))",
5202 if (filter
== NULL
) {
5203 DEBUG(3, ("talloc_asprintf failed\n"));
5207 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5208 if (rc
!= LDAP_SUCCESS
) {
5211 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5213 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5214 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5215 ldap_count_entries(priv2ld(priv
), result
),
5216 (unsigned int)gid
));
5220 entry
= ldap_first_entry(priv2ld(priv
), result
);
5222 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5223 "sambaSID", tmp_ctx
);
5224 if (group_sid_string
== NULL
) {
5225 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5226 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5230 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5231 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5236 sid_copy(sid
, &group_sid
);
5238 store_gid_sid_cache(sid
, gid
);
5239 idmap_cache_set_sid2gid(sid
, gid
);
5244 TALLOC_FREE(tmp_ctx
);
5250 * The following functions are called only if
5251 * ldapsam:trusted and ldapsam:editposix are
5256 * ldapsam_create_user creates a new
5257 * posixAccount and sambaSamAccount object
5258 * in the ldap users subtree
5260 * The uid is allocated by winbindd.
5263 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5264 TALLOC_CTX
*tmp_ctx
, const char *name
,
5265 uint32 acb_info
, uint32
*rid
)
5267 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5268 LDAPMessage
*entry
= NULL
;
5269 LDAPMessage
*result
= NULL
;
5271 bool is_machine
= False
;
5272 bool add_posix
= False
;
5273 LDAPMod
**mods
= NULL
;
5281 const char *dn
= NULL
;
5289 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5290 acb_info
& ACB_WSTRUST
||
5291 acb_info
& ACB_SVRTRUST
||
5292 acb_info
& ACB_DOMTRUST
) {
5296 username
= escape_ldap_string(talloc_tos(), name
);
5297 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5298 username
, LDAP_OBJ_POSIXACCOUNT
);
5299 TALLOC_FREE(username
);
5301 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5302 if (rc
!= LDAP_SUCCESS
) {
5303 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5304 return NT_STATUS_ACCESS_DENIED
;
5306 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5308 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5310 if (num_result
> 1) {
5311 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5312 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5315 if (num_result
== 1) {
5317 /* check if it is just a posix account.
5318 * or if there is a sid attached to this entry
5321 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5323 return NT_STATUS_UNSUCCESSFUL
;
5326 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5328 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5329 return NT_STATUS_USER_EXISTS
;
5332 /* it is just a posix account, retrieve the dn for later use */
5333 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5335 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5336 return NT_STATUS_NO_MEMORY
;
5340 if (num_result
== 0) {
5344 /* Create the basic samu structure and generate the mods for the ldap commit */
5345 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5346 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5350 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5352 user
= samu_new(tmp_ctx
);
5354 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5355 return NT_STATUS_NO_MEMORY
;
5358 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5359 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5360 return NT_STATUS_UNSUCCESSFUL
;
5362 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5363 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5364 return NT_STATUS_UNSUCCESSFUL
;
5367 if (acb_info
& ACB_NORMAL
) {
5368 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5369 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5370 return NT_STATUS_UNSUCCESSFUL
;
5373 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5374 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5375 return NT_STATUS_UNSUCCESSFUL
;
5379 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5380 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5381 return NT_STATUS_UNSUCCESSFUL
;
5385 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5386 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5387 return NT_STATUS_UNSUCCESSFUL
;
5390 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5391 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5392 return NT_STATUS_UNSUCCESSFUL
;
5395 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5396 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5398 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5403 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5405 /* retrieve the Domain Users group gid */
5406 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
5407 !sid_to_gid(&group_sid
, &gid
)) {
5408 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5409 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5412 /* lets allocate a new userid for this user */
5413 if (!winbind_allocate_uid(&uid
)) {
5414 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5415 return NT_STATUS_UNSUCCESSFUL
;
5420 /* TODO: choose a more appropriate default for machines */
5421 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5422 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5424 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5425 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5427 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5428 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5430 escape_name
= escape_rdn_val_string_alloc(name
);
5432 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5433 return NT_STATUS_NO_MEMORY
;
5437 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5439 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5442 SAFE_FREE(escape_name
);
5444 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5445 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5446 return NT_STATUS_NO_MEMORY
;
5449 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5450 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5451 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5452 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5453 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5454 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5455 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5458 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5461 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5463 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5466 if (rc
!= LDAP_SUCCESS
) {
5467 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5468 return NT_STATUS_UNSUCCESSFUL
;
5471 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5473 flush_pwnam_cache();
5475 return NT_STATUS_OK
;
5478 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5480 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5481 LDAPMessage
*result
= NULL
;
5482 LDAPMessage
*entry
= NULL
;
5488 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5490 filter
= talloc_asprintf(tmp_ctx
,
5493 "(objectClass=%s))",
5494 pdb_get_username(sam_acct
),
5495 LDAP_OBJ_POSIXACCOUNT
,
5496 LDAP_OBJ_SAMBASAMACCOUNT
);
5497 if (filter
== NULL
) {
5498 return NT_STATUS_NO_MEMORY
;
5501 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5502 if (rc
!= LDAP_SUCCESS
) {
5503 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5504 return NT_STATUS_UNSUCCESSFUL
;
5506 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5508 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5510 if (num_result
== 0) {
5511 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5512 return NT_STATUS_NO_SUCH_USER
;
5515 if (num_result
> 1) {
5516 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5517 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5520 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5522 return NT_STATUS_UNSUCCESSFUL
;
5525 /* it is just a posix account, retrieve the dn for later use */
5526 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5528 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5529 return NT_STATUS_NO_MEMORY
;
5532 /* try to remove memberships first */
5535 struct dom_sid
*sids
= NULL
;
5537 size_t num_groups
= 0;
5539 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5541 status
= ldapsam_enum_group_memberships(my_methods
,
5547 if (!NT_STATUS_IS_OK(status
)) {
5551 for (i
=0; i
< num_groups
; i
++) {
5555 sid_peek_rid(&sids
[i
], &group_rid
);
5557 ldapsam_del_groupmem(my_methods
,
5566 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5567 if (rc
!= LDAP_SUCCESS
) {
5568 return NT_STATUS_UNSUCCESSFUL
;
5571 flush_pwnam_cache();
5573 return NT_STATUS_OK
;
5577 * ldapsam_create_group creates a new
5578 * posixGroup and sambaGroupMapping object
5579 * in the ldap groups subtree
5581 * The gid is allocated by winbindd.
5584 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5585 TALLOC_CTX
*tmp_ctx
,
5589 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5591 LDAPMessage
*entry
= NULL
;
5592 LDAPMessage
*result
= NULL
;
5594 bool is_new_entry
= False
;
5595 LDAPMod
**mods
= NULL
;
5601 const char *dn
= NULL
;
5606 groupname
= escape_ldap_string(talloc_tos(), name
);
5607 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5608 groupname
, LDAP_OBJ_POSIXGROUP
);
5609 TALLOC_FREE(groupname
);
5611 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5612 if (rc
!= LDAP_SUCCESS
) {
5613 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5614 return NT_STATUS_UNSUCCESSFUL
;
5616 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5618 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5620 if (num_result
> 1) {
5621 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5622 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5625 if (num_result
== 1) {
5627 /* check if it is just a posix group.
5628 * or if there is a sid attached to this entry
5631 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5633 return NT_STATUS_UNSUCCESSFUL
;
5636 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5638 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5639 return NT_STATUS_GROUP_EXISTS
;
5642 /* it is just a posix group, retrieve the gid and the dn for later use */
5643 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5645 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5646 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5649 gid
= strtoul(tmp
, NULL
, 10);
5651 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5653 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5654 return NT_STATUS_NO_MEMORY
;
5658 if (num_result
== 0) {
5661 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5663 is_new_entry
= True
;
5665 /* lets allocate a new groupid for this group */
5666 if (!winbind_allocate_gid(&gid
)) {
5667 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5668 return NT_STATUS_UNSUCCESSFUL
;
5671 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5673 escape_name
= escape_rdn_val_string_alloc(name
);
5675 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5676 return NT_STATUS_NO_MEMORY
;
5679 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5681 SAFE_FREE(escape_name
);
5683 if (!gidstr
|| !dn
) {
5684 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5685 return NT_STATUS_NO_MEMORY
;
5688 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5689 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5690 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5693 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5694 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5698 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5700 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5702 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5704 if (!groupsidstr
|| !grouptype
) {
5705 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5706 return NT_STATUS_NO_MEMORY
;
5709 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5710 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5711 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5712 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5713 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5716 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5718 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5719 /* This call may fail with rfc2307bis schema */
5720 /* Retry adding a structural class */
5721 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5722 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5726 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5729 if (rc
!= LDAP_SUCCESS
) {
5730 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5731 return NT_STATUS_UNSUCCESSFUL
;
5734 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5736 return NT_STATUS_OK
;
5739 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
5741 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5742 LDAPMessage
*result
= NULL
;
5743 LDAPMessage
*entry
= NULL
;
5751 /* get the group sid */
5752 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5754 filter
= talloc_asprintf(tmp_ctx
,
5757 "(objectClass=%s))",
5758 sid_string_talloc(tmp_ctx
, &group_sid
),
5759 LDAP_OBJ_POSIXGROUP
,
5761 if (filter
== NULL
) {
5762 return NT_STATUS_NO_MEMORY
;
5765 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5766 if (rc
!= LDAP_SUCCESS
) {
5767 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5768 return NT_STATUS_UNSUCCESSFUL
;
5770 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5772 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5774 if (num_result
== 0) {
5775 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5776 return NT_STATUS_NO_SUCH_GROUP
;
5779 if (num_result
> 1) {
5780 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5781 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5784 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5786 return NT_STATUS_UNSUCCESSFUL
;
5789 /* here it is, retrieve the dn for later use */
5790 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5792 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5793 return NT_STATUS_NO_MEMORY
;
5796 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5798 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5799 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5802 /* check no user have this group marked as primary group */
5803 filter
= talloc_asprintf(tmp_ctx
,
5806 "(objectClass=%s))",
5808 LDAP_OBJ_POSIXACCOUNT
,
5809 LDAP_OBJ_SAMBASAMACCOUNT
);
5811 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5812 if (rc
!= LDAP_SUCCESS
) {
5813 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5814 return NT_STATUS_UNSUCCESSFUL
;
5816 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5818 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5820 if (num_result
!= 0) {
5821 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5822 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5825 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5826 if (rc
!= LDAP_SUCCESS
) {
5827 return NT_STATUS_UNSUCCESSFUL
;
5830 return NT_STATUS_OK
;
5833 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5834 TALLOC_CTX
*tmp_ctx
,
5839 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5840 LDAPMessage
*entry
= NULL
;
5841 LDAPMessage
*result
= NULL
;
5843 LDAPMod
**mods
= NULL
;
5846 const char *dn
= NULL
;
5853 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5855 case LDAP_MOD_DELETE
:
5856 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5859 return NT_STATUS_UNSUCCESSFUL
;
5862 /* get member sid */
5863 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5865 /* get the group sid */
5866 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5868 filter
= talloc_asprintf(tmp_ctx
,
5871 "(objectClass=%s))",
5872 sid_string_talloc(tmp_ctx
, &member_sid
),
5873 LDAP_OBJ_POSIXACCOUNT
,
5874 LDAP_OBJ_SAMBASAMACCOUNT
);
5875 if (filter
== NULL
) {
5876 return NT_STATUS_NO_MEMORY
;
5879 /* get the member uid */
5880 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5881 if (rc
!= LDAP_SUCCESS
) {
5882 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5883 return NT_STATUS_UNSUCCESSFUL
;
5885 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5887 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5889 if (num_result
== 0) {
5890 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5891 return NT_STATUS_NO_SUCH_MEMBER
;
5894 if (num_result
> 1) {
5895 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5896 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5899 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5901 return NT_STATUS_UNSUCCESSFUL
;
5904 if (modop
== LDAP_MOD_DELETE
) {
5905 /* check if we are trying to remove the member from his primary group */
5907 gid_t user_gid
, group_gid
;
5909 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5911 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5912 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5915 user_gid
= strtoul(gidstr
, NULL
, 10);
5917 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5918 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5919 return NT_STATUS_UNSUCCESSFUL
;
5922 if (user_gid
== group_gid
) {
5923 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5924 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5928 /* here it is, retrieve the uid for later use */
5929 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5931 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5932 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5935 filter
= talloc_asprintf(tmp_ctx
,
5938 "(objectClass=%s))",
5939 sid_string_talloc(tmp_ctx
, &group_sid
),
5940 LDAP_OBJ_POSIXGROUP
,
5944 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5945 if (rc
!= LDAP_SUCCESS
) {
5946 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5947 return NT_STATUS_UNSUCCESSFUL
;
5949 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5951 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5953 if (num_result
== 0) {
5954 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5955 return NT_STATUS_NO_SUCH_GROUP
;
5958 if (num_result
> 1) {
5959 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5960 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5963 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5965 return NT_STATUS_UNSUCCESSFUL
;
5968 /* here it is, retrieve the dn for later use */
5969 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5971 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5972 return NT_STATUS_NO_MEMORY
;
5975 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5977 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5979 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5980 if (rc
!= LDAP_SUCCESS
) {
5981 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5982 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5983 return NT_STATUS_MEMBER_IN_GROUP
;
5985 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5986 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5987 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5989 return NT_STATUS_UNSUCCESSFUL
;
5992 return NT_STATUS_OK
;
5995 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5996 TALLOC_CTX
*tmp_ctx
,
6000 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
6002 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
6003 TALLOC_CTX
*tmp_ctx
,
6007 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
6010 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
6011 TALLOC_CTX
*mem_ctx
,
6012 struct samu
*sampass
)
6014 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6015 LDAPMessage
*entry
= NULL
;
6016 LDAPMessage
*result
= NULL
;
6018 LDAPMod
**mods
= NULL
;
6020 char *escape_username
;
6022 const char *dn
= NULL
;
6026 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6028 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6029 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6030 return NT_STATUS_UNSUCCESSFUL
;
6032 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6034 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6035 return NT_STATUS_NO_MEMORY
;
6038 escape_username
= escape_ldap_string(talloc_tos(),
6039 pdb_get_username(sampass
));
6040 if (escape_username
== NULL
) {
6041 return NT_STATUS_NO_MEMORY
;
6044 filter
= talloc_asprintf(mem_ctx
,
6047 "(objectClass=%s))",
6049 LDAP_OBJ_POSIXACCOUNT
,
6050 LDAP_OBJ_SAMBASAMACCOUNT
);
6052 TALLOC_FREE(escape_username
);
6054 if (filter
== NULL
) {
6055 return NT_STATUS_NO_MEMORY
;
6058 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6059 if (rc
!= LDAP_SUCCESS
) {
6060 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6061 return NT_STATUS_UNSUCCESSFUL
;
6063 talloc_autofree_ldapmsg(mem_ctx
, result
);
6065 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6067 if (num_result
== 0) {
6068 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6069 return NT_STATUS_NO_SUCH_USER
;
6072 if (num_result
> 1) {
6073 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6074 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6077 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6079 return NT_STATUS_UNSUCCESSFUL
;
6082 /* retrieve the dn for later use */
6083 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6085 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6086 return NT_STATUS_NO_MEMORY
;
6089 /* remove the old one, and add the new one, this way we do not risk races */
6090 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6093 return NT_STATUS_OK
;
6096 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6098 if (rc
!= LDAP_SUCCESS
) {
6099 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6100 pdb_get_username(sampass
), gidstr
));
6101 return NT_STATUS_UNSUCCESSFUL
;
6104 flush_pwnam_cache();
6106 return NT_STATUS_OK
;
6110 /**********************************************************************
6111 trusted domains functions
6112 *********************************************************************/
6114 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6117 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6118 ldap_state
->domain_dn
);
6121 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6122 TALLOC_CTX
*mem_ctx
,
6123 const char *domain
, LDAPMessage
**entry
)
6127 int scope
= LDAP_SCOPE_SUBTREE
;
6128 const char **attrs
= NULL
; /* NULL: get all attrs */
6129 int attrsonly
= 0; /* 0: return values too */
6130 LDAPMessage
*result
= NULL
;
6134 filter
= talloc_asprintf(talloc_tos(),
6135 "(&(objectClass=%s)(sambaDomainName=%s))",
6136 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6138 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6139 if (trusted_dn
== NULL
) {
6142 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6143 filter
, attrs
, attrsonly
, &result
);
6145 if (result
!= NULL
) {
6146 talloc_autofree_ldapmsg(mem_ctx
, result
);
6149 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6154 if (rc
!= LDAP_SUCCESS
) {
6158 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6160 if (num_result
> 1) {
6161 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6162 "%s object for domain '%s'?!\n",
6163 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6167 if (num_result
== 0) {
6168 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6169 "%s object for domain %s.\n",
6170 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6173 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6179 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6183 time_t *pass_last_set_time
)
6185 struct ldapsam_privates
*ldap_state
=
6186 (struct ldapsam_privates
*)methods
->private_data
;
6187 LDAPMessage
*entry
= NULL
;
6189 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6191 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6200 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6201 entry
, "sambaClearTextPassword", talloc_tos());
6202 if (pwd_str
== NULL
) {
6205 /* trusteddom_pw routines do not use talloc yet... */
6206 *pwd
= SMB_STRDUP(pwd_str
);
6212 /* last change time */
6213 if (pass_last_set_time
!= NULL
) {
6215 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6216 entry
, "sambaPwdLastSet", talloc_tos());
6217 if (time_str
== NULL
) {
6220 *pass_last_set_time
= (time_t)atol(time_str
);
6226 struct dom_sid dom_sid
;
6227 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6230 if (sid_str
== NULL
) {
6233 if (!string_to_sid(&dom_sid
, sid_str
)) {
6236 sid_copy(sid
, &dom_sid
);
6242 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6247 struct ldapsam_privates
*ldap_state
=
6248 (struct ldapsam_privates
*)methods
->private_data
;
6249 LDAPMessage
*entry
= NULL
;
6250 LDAPMod
**mods
= NULL
;
6251 char *prev_pwd
= NULL
;
6252 char *trusted_dn
= NULL
;
6255 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6258 * get the current entry (if there is one) in order to put the
6259 * current password into the previous password attribute
6261 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6266 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6267 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6268 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6270 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6271 sid_string_tos(sid
));
6272 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6273 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6274 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6275 "sambaClearTextPassword", pwd
);
6277 if (entry
!= NULL
) {
6278 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6279 entry
, "sambaClearTextPassword", talloc_tos());
6280 if (prev_pwd
!= NULL
) {
6281 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6282 "sambaPreviousClearTextPassword",
6287 talloc_autofree_ldapmod(talloc_tos(), mods
);
6289 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6290 if (trusted_dn
== NULL
) {
6293 if (entry
== NULL
) {
6294 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6296 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6299 if (rc
!= LDAP_SUCCESS
) {
6300 DEBUG(1, ("error writing trusted domain password!\n"));
6307 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6311 struct ldapsam_privates
*ldap_state
=
6312 (struct ldapsam_privates
*)methods
->private_data
;
6313 LDAPMessage
*entry
= NULL
;
6314 const char *trusted_dn
;
6316 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6320 if (entry
== NULL
) {
6321 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6326 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6328 if (trusted_dn
== NULL
) {
6329 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6333 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6334 if (rc
!= LDAP_SUCCESS
) {
6341 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6342 TALLOC_CTX
*mem_ctx
,
6343 uint32
*num_domains
,
6344 struct trustdom_info
***domains
)
6347 struct ldapsam_privates
*ldap_state
=
6348 (struct ldapsam_privates
*)methods
->private_data
;
6350 int scope
= LDAP_SCOPE_SUBTREE
;
6351 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6352 int attrsonly
= 0; /* 0: return values too */
6353 LDAPMessage
*result
= NULL
;
6354 LDAPMessage
*entry
= NULL
;
6356 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6357 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6359 rc
= smbldap_search(ldap_state
->smbldap_state
,
6360 ldap_state
->domain_dn
,
6367 if (result
!= NULL
) {
6368 talloc_autofree_ldapmsg(mem_ctx
, result
);
6371 if (rc
!= LDAP_SUCCESS
) {
6372 return NT_STATUS_UNSUCCESSFUL
;
6376 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6377 DEBUG(1, ("talloc failed\n"));
6378 return NT_STATUS_NO_MEMORY
;
6381 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6383 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6385 char *dom_name
, *dom_sid_str
;
6386 struct trustdom_info
*dom_info
;
6388 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6389 if (dom_info
== NULL
) {
6390 DEBUG(1, ("talloc failed\n"));
6391 return NT_STATUS_NO_MEMORY
;
6394 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6398 if (dom_name
== NULL
) {
6399 DEBUG(1, ("talloc failed\n"));
6400 return NT_STATUS_NO_MEMORY
;
6402 dom_info
->name
= dom_name
;
6404 dom_sid_str
= smbldap_talloc_single_attribute(
6405 priv2ld(ldap_state
), entry
, "sambaSID",
6407 if (dom_sid_str
== NULL
) {
6408 DEBUG(1, ("talloc failed\n"));
6409 return NT_STATUS_NO_MEMORY
;
6411 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6412 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6414 return NT_STATUS_UNSUCCESSFUL
;
6417 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6418 domains
, num_domains
);
6420 if (*domains
== NULL
) {
6421 DEBUG(1, ("talloc failed\n"));
6422 return NT_STATUS_NO_MEMORY
;
6426 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6427 return NT_STATUS_OK
;
6431 /**********************************************************************
6433 *********************************************************************/
6435 static void free_private_data(void **vp
)
6437 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6439 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6441 if ((*ldap_state
)->result
!= NULL
) {
6442 ldap_msgfree((*ldap_state
)->result
);
6443 (*ldap_state
)->result
= NULL
;
6445 if ((*ldap_state
)->domain_dn
!= NULL
) {
6446 SAFE_FREE((*ldap_state
)->domain_dn
);
6451 /* No need to free any further, as it is talloc()ed */
6454 /*********************************************************************
6455 Intitalise the parts of the pdb_methods structure that are common to
6457 *********************************************************************/
6459 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6462 struct ldapsam_privates
*ldap_state
;
6464 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6468 (*pdb_method
)->name
= "ldapsam";
6470 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6471 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6472 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6473 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6474 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6475 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6477 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6478 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6479 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6480 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6481 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6482 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6483 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6485 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6486 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6488 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6490 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6491 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6493 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6494 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6495 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6496 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6498 /* TODO: Setup private data and free */
6500 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6501 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6502 return NT_STATUS_NO_MEMORY
;
6505 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6506 location
, &ldap_state
->smbldap_state
);
6508 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6512 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6513 return NT_STATUS_NO_MEMORY
;
6516 (*pdb_method
)->private_data
= ldap_state
;
6518 (*pdb_method
)->free_private_data
= free_private_data
;
6520 return NT_STATUS_OK
;
6523 /**********************************************************************
6524 Initialise the 'compat' mode for pdb_ldap
6525 *********************************************************************/
6527 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6530 struct ldapsam_privates
*ldap_state
;
6531 char *uri
= talloc_strdup( NULL
, location
);
6533 trim_char( uri
, '\"', '\"' );
6534 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6538 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6542 (*pdb_method
)->name
= "ldapsam_compat";
6544 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6545 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6547 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6549 return NT_STATUS_OK
;
6552 /**********************************************************************
6553 Initialise the normal mode for pdb_ldap
6554 *********************************************************************/
6556 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6559 struct ldapsam_privates
*ldap_state
= NULL
;
6560 uint32 alg_rid_base
;
6561 char *alg_rid_base_string
= NULL
;
6562 LDAPMessage
*result
= NULL
;
6563 LDAPMessage
*entry
= NULL
;
6564 DOM_SID ldap_domain_sid
;
6565 DOM_SID secrets_domain_sid
;
6566 char *domain_sid_string
= NULL
;
6568 char *uri
= talloc_strdup( NULL
, location
);
6570 trim_char( uri
, '\"', '\"' );
6571 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6575 if (!NT_STATUS_IS_OK(nt_status
)) {
6579 (*pdb_method
)->name
= "ldapsam";
6581 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6582 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6583 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6584 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6585 (*pdb_method
)->search_users
= ldapsam_search_users
;
6586 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6587 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6589 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6590 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6591 (*pdb_method
)->enum_group_memberships
=
6592 ldapsam_enum_group_memberships
;
6593 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6594 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6595 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6596 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6598 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6599 (*pdb_method
)->create_user
= ldapsam_create_user
;
6600 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6601 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6602 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6603 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6604 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6605 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6609 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6610 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6612 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6614 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6616 ldap_state
->domain_name
, True
);
6618 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6619 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6620 "info, nor add one to the domain\n"));
6621 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6622 "will be unable to allocate new users/groups, "
6623 "and will risk BDCs having inconsistant SIDs\n"));
6624 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6625 return NT_STATUS_OK
;
6628 /* Given that the above might fail, everything below this must be
6631 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6634 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6636 ldap_msgfree(result
);
6637 return NT_STATUS_UNSUCCESSFUL
;
6640 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6642 ldap_msgfree(result
);
6643 return NT_STATUS_UNSUCCESSFUL
;
6646 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6649 domain_sid_string
= smbldap_talloc_single_attribute(
6650 ldap_state
->smbldap_state
->ldap_struct
,
6652 get_userattr_key2string(ldap_state
->schema_ver
,
6653 LDAP_ATTR_USER_SID
),
6656 if (domain_sid_string
) {
6658 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6659 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6660 "read as a valid SID\n", domain_sid_string
));
6661 ldap_msgfree(result
);
6662 TALLOC_FREE(domain_sid_string
);
6663 return NT_STATUS_INVALID_PARAMETER
;
6665 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6666 &secrets_domain_sid
);
6667 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
6668 &ldap_domain_sid
)) {
6669 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6670 "%s based on pdb_ldap results %s -> %s\n",
6671 ldap_state
->domain_name
,
6672 sid_string_dbg(&secrets_domain_sid
),
6673 sid_string_dbg(&ldap_domain_sid
)));
6675 /* reset secrets.tdb sid */
6676 secrets_store_domain_sid(ldap_state
->domain_name
,
6678 DEBUG(1, ("New global sam SID: %s\n",
6679 sid_string_dbg(get_global_sam_sid())));
6681 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6682 TALLOC_FREE(domain_sid_string
);
6685 alg_rid_base_string
= smbldap_talloc_single_attribute(
6686 ldap_state
->smbldap_state
->ldap_struct
,
6688 get_attr_key2string( dominfo_attr_list
,
6689 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6691 if (alg_rid_base_string
) {
6692 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
6693 if (alg_rid_base
!= algorithmic_rid_base()) {
6694 DEBUG(0, ("The value of 'algorithmic RID base' has "
6695 "changed since the LDAP\n"
6696 "database was initialised. Aborting. \n"));
6697 ldap_msgfree(result
);
6698 TALLOC_FREE(alg_rid_base_string
);
6699 return NT_STATUS_UNSUCCESSFUL
;
6701 TALLOC_FREE(alg_rid_base_string
);
6703 ldap_msgfree(result
);
6705 return NT_STATUS_OK
;
6708 NTSTATUS
pdb_ldap_init(void)
6711 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6714 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6717 /* Let pdb_nds register backends */
6720 return NT_STATUS_OK
;