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
48 #include "../libcli/auth/libcli_auth.h"
50 #include "idmap_cache.h"
51 #include "../libcli/security/security.h"
52 #include "../lib/util/util_pw.h"
53 #include "lib/winbind_util.h"
54 #include "librpc/gen_ndr/idmap.h"
55 #include "lib/param/loadparm.h"
56 #include "lib/util_sid_passdb.h"
59 #define DBGC_CLASS DBGC_PASSDB
66 #include "passdb/pdb_ldap.h"
67 #include "passdb/pdb_nds.h"
68 #include "passdb/pdb_ipa.h"
69 #include "passdb/pdb_ldap_util.h"
70 #include "passdb/pdb_ldap_schema.h"
72 /**********************************************************************
73 Simple helper function to make stuff better readable
74 **********************************************************************/
76 LDAP
*priv2ld(struct ldapsam_privates
*priv
)
78 return priv
->smbldap_state
->ldap_struct
;
81 /**********************************************************************
82 Get the attribute name given a user schame version.
83 **********************************************************************/
85 static const char* get_userattr_key2string( int schema_ver
, int key
)
87 switch ( schema_ver
) {
88 case SCHEMAVER_SAMBASAMACCOUNT
:
89 return get_attr_key2string( attrib_map_v30
, key
);
92 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
98 /**********************************************************************
99 Return the list of attribute names given a user schema version.
100 **********************************************************************/
102 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
104 switch ( schema_ver
) {
105 case SCHEMAVER_SAMBASAMACCOUNT
:
106 return get_attr_list( mem_ctx
, attrib_map_v30
);
108 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
115 /**************************************************************************
116 Return the list of attribute names to delete given a user schema version.
117 **************************************************************************/
119 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
122 switch ( schema_ver
) {
123 case SCHEMAVER_SAMBASAMACCOUNT
:
124 return get_attr_list( mem_ctx
,
125 attrib_map_to_delete_v30
);
127 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
135 /*******************************************************************
136 Generate the LDAP search filter for the objectclass based on the
137 version of the schema we are using.
138 ******************************************************************/
140 static const char* get_objclass_filter( int schema_ver
)
142 fstring objclass_filter
;
145 switch( schema_ver
) {
146 case SCHEMAVER_SAMBASAMACCOUNT
:
147 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
150 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
151 objclass_filter
[0] = '\0';
155 result
= talloc_strdup(talloc_tos(), objclass_filter
);
156 SMB_ASSERT(result
!= NULL
);
160 /*****************************************************************
161 Scan a sequence number off OpenLDAP's syncrepl contextCSN
162 ******************************************************************/
164 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
166 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
167 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
168 LDAPMessage
*msg
= NULL
;
169 LDAPMessage
*entry
= NULL
;
171 char **values
= NULL
;
172 int rc
, num_result
, num_values
, rid
;
178 /* Unfortunatly there is no proper way to detect syncrepl-support in
179 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
180 * but do not show up in the root-DSE yet. Neither we can query the
181 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
182 * objectclass. Currently we require lp_ldap_suffix() to show up as
183 * namingContext. - Guenther
186 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
191 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
195 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix(talloc_tos()))) {
196 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
197 "as top-level namingContext\n", lp_ldap_suffix(talloc_tos())));
201 mem_ctx
= talloc_init("ldapsam_get_seq_num");
204 return NT_STATUS_NO_MEMORY
;
206 if ((attrs
= talloc_array(mem_ctx
, const char *, 2)) == NULL
) {
207 ntstatus
= NT_STATUS_NO_MEMORY
;
211 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
212 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
215 /* consumer syncreplCookie: */
216 /* csn=20050126161620Z#0000001#00#00000 */
217 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
219 suffix
= talloc_asprintf(mem_ctx
,
220 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix(talloc_tos()));
222 ntstatus
= NT_STATUS_NO_MEMORY
;
227 /* provider contextCSN */
228 /* 20050126161620Z#000009#00#000000 */
229 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
231 suffix
= talloc_asprintf(mem_ctx
,
232 "cn=ldapsync,%s", lp_ldap_suffix(talloc_tos()));
235 ntstatus
= NT_STATUS_NO_MEMORY
;
240 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
241 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
243 if (rc
!= LDAP_SUCCESS
) {
247 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
248 if (num_result
!= 1) {
249 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
253 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
255 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
259 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
260 if (values
== NULL
) {
261 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
265 num_values
= ldap_count_values(values
);
266 if (num_values
== 0) {
267 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
272 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
273 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
278 if (!strncmp(p
, "csn=", strlen("csn=")))
281 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
283 *seq_num
= generalized_to_unix_time(p
);
285 /* very basic sanity check */
287 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
292 ntstatus
= NT_STATUS_OK
;
296 ldap_value_free(values
);
300 talloc_destroy(mem_ctx
);
305 /*******************************************************************
306 Run the search by name.
307 ******************************************************************/
309 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
311 LDAPMessage
** result
,
315 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
319 return LDAP_NO_MEMORY
;
323 * in the filter expression, replace %u with the real name
324 * so in ldap filter, %u MUST exist :-)
326 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
327 get_objclass_filter(ldap_state
->schema_ver
));
329 TALLOC_FREE(escape_user
);
330 return LDAP_NO_MEMORY
;
333 * have to use this here because $ is filtered out
337 filter
= talloc_all_string_sub(talloc_tos(),
338 filter
, "%u", escape_user
);
339 TALLOC_FREE(escape_user
);
341 return LDAP_NO_MEMORY
;
344 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
345 filter
, attr
, result
);
350 /*******************************************************************
351 Run the search by SID.
352 ******************************************************************/
354 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
355 const struct dom_sid
*sid
, LDAPMessage
** result
,
362 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
363 get_userattr_key2string(ldap_state
->schema_ver
,
365 sid_to_fstring(sid_string
, sid
),
366 get_objclass_filter(ldap_state
->schema_ver
));
368 return LDAP_NO_MEMORY
;
371 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
372 filter
, attr
, result
);
378 /*******************************************************************
379 Delete complete object or objectclass and attrs from
380 object found in search_result depending on lp_ldap_delete_dn
381 ******************************************************************/
383 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
386 const char *objectclass
,
389 LDAPMod
**mods
= NULL
;
392 BerElement
*ptr
= NULL
;
394 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
396 return LDAP_NO_MEMORY
;
399 if (lp_ldap_delete_dn()) {
400 return smbldap_delete(priv
->smbldap_state
, dn
);
403 /* Ok, delete only the SAM attributes */
405 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
407 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
410 /* We are only allowed to delete the attributes that
413 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
414 if (strequal(*attrib
, name
)) {
415 DEBUG(10, ("ldapsam_delete_entry: deleting "
416 "attribute %s\n", name
));
417 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
428 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
429 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
431 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
434 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
439 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
440 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
446 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
447 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
457 /**********************************************************************
458 Initialize struct samu from an LDAP query.
459 (Based on init_sam_from_buffer in pdb_tdb.c)
460 *********************************************************************/
462 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
463 struct samu
* sampass
,
470 pass_can_change_time
,
473 char *username
= NULL
,
479 *logon_script
= NULL
,
480 *profile_path
= NULL
,
482 *workstations
= NULL
,
485 uint8 smblmpwd
[LM_HASH_LEN
],
486 smbntpwd
[NT_HASH_LEN
];
487 bool use_samba_attrs
= True
;
488 uint32_t acct_ctrl
= 0;
490 uint16_t bad_password_count
= 0,
493 uint8 hours
[MAX_HOURS_LEN
];
495 struct login_cache cache_entry
;
497 bool expand_explicit
= lp_passdb_expand_explicit();
499 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
504 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
505 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
509 if (priv2ld(ldap_state
) == NULL
) {
510 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
511 "ldap_struct is NULL!\n"));
515 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
519 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
524 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
526 nt_username
= talloc_strdup(ctx
, username
);
531 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
536 pdb_set_username(sampass
, username
, PDB_SET
);
538 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
539 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
541 /* deal with different attributes between the schema first */
543 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
544 if ((temp
= smbldap_talloc_single_attribute(
545 ldap_state
->smbldap_state
->ldap_struct
,
547 get_userattr_key2string(ldap_state
->schema_ver
,
550 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
553 if ((temp
= smbldap_talloc_single_attribute(
554 ldap_state
->smbldap_state
->ldap_struct
,
556 get_userattr_key2string(ldap_state
->schema_ver
,
559 user_rid
= (uint32_t)atol(temp
);
560 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
564 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
565 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
566 get_userattr_key2string(ldap_state
->schema_ver
,
568 get_userattr_key2string(ldap_state
->schema_ver
,
574 temp
= smbldap_talloc_single_attribute(
575 ldap_state
->smbldap_state
->ldap_struct
,
577 get_userattr_key2string(ldap_state
->schema_ver
,
578 LDAP_ATTR_PWD_LAST_SET
),
581 pass_last_set_time
= (time_t) atol(temp
);
582 pdb_set_pass_last_set_time(sampass
,
583 pass_last_set_time
, PDB_SET
);
586 temp
= smbldap_talloc_single_attribute(
587 ldap_state
->smbldap_state
->ldap_struct
,
589 get_userattr_key2string(ldap_state
->schema_ver
,
590 LDAP_ATTR_LOGON_TIME
),
593 logon_time
= (time_t) atol(temp
);
594 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
597 temp
= smbldap_talloc_single_attribute(
598 ldap_state
->smbldap_state
->ldap_struct
,
600 get_userattr_key2string(ldap_state
->schema_ver
,
601 LDAP_ATTR_LOGOFF_TIME
),
604 logoff_time
= (time_t) atol(temp
);
605 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
608 temp
= smbldap_talloc_single_attribute(
609 ldap_state
->smbldap_state
->ldap_struct
,
611 get_userattr_key2string(ldap_state
->schema_ver
,
612 LDAP_ATTR_KICKOFF_TIME
),
615 kickoff_time
= (time_t) atol(temp
);
616 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
619 temp
= smbldap_talloc_single_attribute(
620 ldap_state
->smbldap_state
->ldap_struct
,
622 get_userattr_key2string(ldap_state
->schema_ver
,
623 LDAP_ATTR_PWD_CAN_CHANGE
),
626 pass_can_change_time
= (time_t) atol(temp
);
627 pdb_set_pass_can_change_time(sampass
,
628 pass_can_change_time
, PDB_SET
);
631 /* recommend that 'gecos' and 'displayName' should refer to the same
632 * attribute OID. userFullName depreciated, only used by Samba
633 * primary rules of LDAP: don't make a new attribute when one is already defined
634 * that fits your needs; using cn then displayName rather than 'userFullName'
637 fullname
= smbldap_talloc_single_attribute(
638 ldap_state
->smbldap_state
->ldap_struct
,
640 get_userattr_key2string(ldap_state
->schema_ver
,
641 LDAP_ATTR_DISPLAY_NAME
),
644 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
646 fullname
= smbldap_talloc_single_attribute(
647 ldap_state
->smbldap_state
->ldap_struct
,
649 get_userattr_key2string(ldap_state
->schema_ver
,
653 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
657 dir_drive
= smbldap_talloc_single_attribute(
658 ldap_state
->smbldap_state
->ldap_struct
,
660 get_userattr_key2string(ldap_state
->schema_ver
,
661 LDAP_ATTR_HOME_DRIVE
),
664 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
666 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
669 homedir
= smbldap_talloc_single_attribute(
670 ldap_state
->smbldap_state
->ldap_struct
,
672 get_userattr_key2string(ldap_state
->schema_ver
,
673 LDAP_ATTR_HOME_PATH
),
676 if (expand_explicit
) {
677 homedir
= talloc_sub_basic(ctx
,
685 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
687 pdb_set_homedir(sampass
,
688 talloc_sub_basic(ctx
, username
, domain
,
693 logon_script
= smbldap_talloc_single_attribute(
694 ldap_state
->smbldap_state
->ldap_struct
,
696 get_userattr_key2string(ldap_state
->schema_ver
,
697 LDAP_ATTR_LOGON_SCRIPT
),
700 if (expand_explicit
) {
701 logon_script
= talloc_sub_basic(ctx
,
709 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
711 pdb_set_logon_script(sampass
,
712 talloc_sub_basic(ctx
, username
, domain
,
717 profile_path
= smbldap_talloc_single_attribute(
718 ldap_state
->smbldap_state
->ldap_struct
,
720 get_userattr_key2string(ldap_state
->schema_ver
,
721 LDAP_ATTR_PROFILE_PATH
),
724 if (expand_explicit
) {
725 profile_path
= talloc_sub_basic(ctx
,
733 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
735 pdb_set_profile_path(sampass
,
736 talloc_sub_basic(ctx
, username
, domain
,
741 acct_desc
= smbldap_talloc_single_attribute(
742 ldap_state
->smbldap_state
->ldap_struct
,
744 get_userattr_key2string(ldap_state
->schema_ver
,
748 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
751 workstations
= smbldap_talloc_single_attribute(
752 ldap_state
->smbldap_state
->ldap_struct
,
754 get_userattr_key2string(ldap_state
->schema_ver
,
758 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
761 munged_dial
= smbldap_talloc_single_attribute(
762 ldap_state
->smbldap_state
->ldap_struct
,
764 get_userattr_key2string(ldap_state
->schema_ver
,
765 LDAP_ATTR_MUNGED_DIAL
),
768 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
771 /* FIXME: hours stuff should be cleaner */
775 memset(hours
, 0xff, hours_len
);
777 if (ldap_state
->is_nds_ldap
) {
780 char clear_text_pw
[512];
782 /* Make call to Novell eDirectory ldap extension to get clear text password.
783 NOTE: This will only work if we have an SSL connection to eDirectory. */
784 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
785 if (user_dn
!= NULL
) {
786 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
788 pwd_len
= sizeof(clear_text_pw
);
789 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
790 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
791 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
792 TALLOC_FREE(user_dn
);
795 ZERO_STRUCT(smblmpwd
);
796 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
797 TALLOC_FREE(user_dn
);
800 ZERO_STRUCT(smbntpwd
);
801 use_samba_attrs
= False
;
804 TALLOC_FREE(user_dn
);
807 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
811 if (use_samba_attrs
) {
812 temp
= smbldap_talloc_single_attribute(
813 ldap_state
->smbldap_state
->ldap_struct
,
815 get_userattr_key2string(ldap_state
->schema_ver
,
819 pdb_gethexpwd(temp
, smblmpwd
);
820 memset((char *)temp
, '\0', strlen(temp
)+1);
821 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
824 ZERO_STRUCT(smblmpwd
);
827 temp
= smbldap_talloc_single_attribute(
828 ldap_state
->smbldap_state
->ldap_struct
,
830 get_userattr_key2string(ldap_state
->schema_ver
,
834 pdb_gethexpwd(temp
, smbntpwd
);
835 memset((char *)temp
, '\0', strlen(temp
)+1);
836 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
839 ZERO_STRUCT(smbntpwd
);
845 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
847 uint8
*pwhist
= NULL
;
849 char *history_string
= talloc_array(ctx
, char,
850 MAX_PW_HISTORY_LEN
*64);
852 if (!history_string
) {
856 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
858 pwhist
= talloc_array(ctx
, uint8
,
859 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
860 if (pwhist
== NULL
) {
861 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
864 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
866 if (smbldap_get_single_attribute(
867 ldap_state
->smbldap_state
->ldap_struct
,
869 get_userattr_key2string(ldap_state
->schema_ver
,
870 LDAP_ATTR_PWD_HISTORY
),
872 MAX_PW_HISTORY_LEN
*64)) {
873 bool hex_failed
= false;
874 for (i
= 0; i
< pwHistLen
; i
++){
875 /* Get the 16 byte salt. */
876 if (!pdb_gethexpwd(&history_string
[i
*64],
877 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
881 /* Get the 16 byte MD5 hash of salt+passwd. */
882 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
883 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
884 PW_HISTORY_SALT_LEN
])) {
890 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
892 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
895 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
900 temp
= smbldap_talloc_single_attribute(
901 ldap_state
->smbldap_state
->ldap_struct
,
903 get_userattr_key2string(ldap_state
->schema_ver
,
907 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
909 if (acct_ctrl
== 0) {
910 acct_ctrl
|= ACB_NORMAL
;
913 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
915 acct_ctrl
|= ACB_NORMAL
;
918 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
919 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
921 temp
= smbldap_talloc_single_attribute(
922 ldap_state
->smbldap_state
->ldap_struct
,
924 get_userattr_key2string(ldap_state
->schema_ver
,
925 LDAP_ATTR_BAD_PASSWORD_COUNT
),
928 bad_password_count
= (uint32_t) atol(temp
);
929 pdb_set_bad_password_count(sampass
,
930 bad_password_count
, PDB_SET
);
933 temp
= smbldap_talloc_single_attribute(
934 ldap_state
->smbldap_state
->ldap_struct
,
936 get_userattr_key2string(ldap_state
->schema_ver
,
937 LDAP_ATTR_BAD_PASSWORD_TIME
),
940 bad_password_time
= (time_t) atol(temp
);
941 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
945 temp
= smbldap_talloc_single_attribute(
946 ldap_state
->smbldap_state
->ldap_struct
,
948 get_userattr_key2string(ldap_state
->schema_ver
,
949 LDAP_ATTR_LOGON_COUNT
),
952 logon_count
= (uint32_t) atol(temp
);
953 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
956 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
958 temp
= smbldap_talloc_single_attribute(
959 ldap_state
->smbldap_state
->ldap_struct
,
961 get_userattr_key2string(ldap_state
->schema_ver
,
962 LDAP_ATTR_LOGON_HOURS
),
965 pdb_gethexhours(temp
, hours
);
966 memset((char *)temp
, '\0', strlen(temp
) +1);
967 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
971 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
972 struct passwd unix_pw
;
973 bool have_uid
= false;
974 bool have_gid
= false;
975 struct dom_sid mapped_gsid
;
976 const struct dom_sid
*primary_gsid
;
979 ZERO_STRUCT(unix_pw
);
981 unix_pw
.pw_name
= username
;
982 unix_pw
.pw_passwd
= discard_const_p(char, "x");
984 temp
= smbldap_talloc_single_attribute(
990 /* We've got a uid, feed the cache */
991 unix_pw
.pw_uid
= strtoul(temp
, NULL
, 10);
994 temp
= smbldap_talloc_single_attribute(
1000 /* We've got a uid, feed the cache */
1001 unix_pw
.pw_gid
= strtoul(temp
, NULL
, 10);
1004 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1005 priv2ld(ldap_state
),
1009 if (unix_pw
.pw_gecos
) {
1010 unix_pw
.pw_gecos
= fullname
;
1012 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1013 priv2ld(ldap_state
),
1017 if (unix_pw
.pw_dir
) {
1018 unix_pw
.pw_dir
= discard_const_p(char, "");
1020 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1021 priv2ld(ldap_state
),
1025 if (unix_pw
.pw_shell
) {
1026 unix_pw
.pw_shell
= discard_const_p(char, "");
1029 if (have_uid
&& have_gid
) {
1030 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1032 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1035 if (sampass
->unix_pw
== NULL
) {
1036 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1037 pdb_get_username(sampass
)));
1041 id
.id
= sampass
->unix_pw
->pw_uid
;
1042 id
.type
= ID_TYPE_UID
;
1044 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass
), &id
);
1046 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1047 primary_gsid
= pdb_get_group_sid(sampass
);
1048 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1049 id
.id
= sampass
->unix_pw
->pw_gid
;
1050 id
.type
= ID_TYPE_GID
;
1052 idmap_cache_set_sid2unixid(primary_gsid
, &id
);
1056 /* check the timestamp of the cache vs ldap entry */
1057 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1063 /* see if we have newer updates */
1064 if (!login_cache_read(sampass
, &cache_entry
)) {
1065 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1066 (unsigned int)pdb_get_bad_password_count(sampass
),
1067 (unsigned int)pdb_get_bad_password_time(sampass
)));
1072 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1073 (unsigned int)ldap_entry_time
,
1074 (unsigned int)cache_entry
.entry_timestamp
,
1075 (unsigned int)cache_entry
.bad_password_time
));
1077 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1078 /* cache is older than directory , so
1079 we need to delete the entry but allow the
1080 fields to be written out */
1081 login_cache_delentry(sampass
);
1084 pdb_set_acct_ctrl(sampass
,
1085 pdb_get_acct_ctrl(sampass
) |
1086 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1088 pdb_set_bad_password_count(sampass
,
1089 cache_entry
.bad_password_count
,
1091 pdb_set_bad_password_time(sampass
,
1092 cache_entry
.bad_password_time
,
1104 /**********************************************************************
1105 Initialize the ldap db from a struct samu. Called on update.
1106 (Based on init_buffer_from_sam in pdb_tdb.c)
1107 *********************************************************************/
1109 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1110 LDAPMessage
*existing
,
1111 LDAPMod
*** mods
, struct samu
* sampass
,
1112 bool (*need_update
)(const struct samu
*,
1117 if (mods
== NULL
|| sampass
== NULL
) {
1118 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1125 * took out adding "objectclass: sambaAccount"
1126 * do this on a per-mod basis
1128 if (need_update(sampass
, PDB_USERNAME
)) {
1129 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1130 "uid", pdb_get_username(sampass
));
1131 if (ldap_state
->is_nds_ldap
) {
1132 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1133 "cn", pdb_get_username(sampass
));
1134 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1135 "sn", pdb_get_username(sampass
));
1139 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1141 /* only update the RID if we actually need to */
1142 if (need_update(sampass
, PDB_USERSID
)) {
1144 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1146 switch ( ldap_state
->schema_ver
) {
1147 case SCHEMAVER_SAMBASAMACCOUNT
:
1148 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1149 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1150 sid_to_fstring(sid_string
, user_sid
));
1154 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1159 /* we don't need to store the primary group RID - so leaving it
1160 'free' to hang off the unix primary group makes life easier */
1162 if (need_update(sampass
, PDB_GROUPSID
)) {
1164 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1166 switch ( ldap_state
->schema_ver
) {
1167 case SCHEMAVER_SAMBASAMACCOUNT
:
1168 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1169 get_userattr_key2string(ldap_state
->schema_ver
,
1170 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1174 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1180 /* displayName, cn, and gecos should all be the same
1181 * most easily accomplished by giving them the same OID
1182 * gecos isn't set here b/c it should be handled by the
1184 * We change displayName only and fall back to cn if
1185 * it does not exist.
1188 if (need_update(sampass
, PDB_FULLNAME
))
1189 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1190 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1191 pdb_get_fullname(sampass
));
1193 if (need_update(sampass
, PDB_ACCTDESC
))
1194 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1195 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1196 pdb_get_acct_desc(sampass
));
1198 if (need_update(sampass
, PDB_WORKSTATIONS
))
1199 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1200 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1201 pdb_get_workstations(sampass
));
1203 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1204 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1205 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1206 pdb_get_munged_dial(sampass
));
1208 if (need_update(sampass
, PDB_SMBHOME
))
1209 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1210 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1211 pdb_get_homedir(sampass
));
1213 if (need_update(sampass
, PDB_DRIVE
))
1214 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1215 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1216 pdb_get_dir_drive(sampass
));
1218 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1219 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1220 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1221 pdb_get_logon_script(sampass
));
1223 if (need_update(sampass
, PDB_PROFILE
))
1224 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1225 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1226 pdb_get_profile_path(sampass
));
1228 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1231 if (need_update(sampass
, PDB_LOGONTIME
))
1232 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1233 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1236 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1239 if (need_update(sampass
, PDB_LOGOFFTIME
))
1240 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1241 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1244 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1247 if (need_update(sampass
, PDB_KICKOFFTIME
))
1248 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1249 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1252 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1255 if (need_update(sampass
, PDB_CANCHANGETIME
))
1256 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1257 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1260 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1261 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1263 if (need_update(sampass
, PDB_LMPASSWD
)) {
1264 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1267 pdb_sethexpwd(pwstr
, lm_pw
,
1268 pdb_get_acct_ctrl(sampass
));
1269 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1270 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1273 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1274 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1278 if (need_update(sampass
, PDB_NTPASSWD
)) {
1279 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1282 pdb_sethexpwd(pwstr
, nt_pw
,
1283 pdb_get_acct_ctrl(sampass
));
1284 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1285 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1288 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1289 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1294 if (need_update(sampass
, PDB_PWHISTORY
)) {
1296 uint32_t pwHistLen
= 0;
1297 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1299 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1303 if (pwHistLen
== 0) {
1304 /* Remove any password history from the LDAP store. */
1305 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1309 uint32_t currHistLen
= 0;
1310 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1311 if (pwhist
!= NULL
) {
1312 /* We can only store (1024-1/64 password history entries. */
1313 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1314 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1315 /* Store the salt. */
1316 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1317 /* Followed by the md5 hash of salt + md4 hash */
1318 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1319 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1320 DEBUG(100, ("pwstr=%s\n", pwstr
));
1324 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1325 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1330 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1331 if (asprintf(&temp
, "%li",
1332 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1335 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1336 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1342 if (need_update(sampass
, PDB_HOURS
)) {
1343 const uint8
*hours
= pdb_get_hours(sampass
);
1346 pdb_sethexhours(hourstr
, hours
);
1347 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1350 get_userattr_key2string(ldap_state
->schema_ver
,
1351 LDAP_ATTR_LOGON_HOURS
),
1356 if (need_update(sampass
, PDB_ACCTCTRL
))
1357 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1358 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1359 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1361 /* password lockout cache:
1362 - If we are now autolocking or clearing, we write to ldap
1363 - If we are clearing, we delete the cache entry
1364 - If the count is > 0, we update the cache
1366 This even means when autolocking, we cache, just in case the
1367 update doesn't work, and we have to cache the autolock flag */
1369 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1370 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1371 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1372 time_t badtime
= pdb_get_bad_password_time(sampass
);
1374 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1376 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1377 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1379 if ((badcount
>= pol
) || (badcount
== 0)) {
1380 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1381 (unsigned int)badcount
, (unsigned int)badtime
));
1382 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1386 ldap_state
->smbldap_state
->ldap_struct
,
1388 get_userattr_key2string(
1389 ldap_state
->schema_ver
,
1390 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1394 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1398 ldap_state
->smbldap_state
->ldap_struct
,
1400 get_userattr_key2string(
1401 ldap_state
->schema_ver
,
1402 LDAP_ATTR_BAD_PASSWORD_TIME
),
1406 if (badcount
== 0) {
1407 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1408 login_cache_delentry(sampass
);
1410 struct login_cache cache_entry
;
1412 cache_entry
.entry_timestamp
= time(NULL
);
1413 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1414 cache_entry
.bad_password_count
= badcount
;
1415 cache_entry
.bad_password_time
= badtime
;
1417 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1418 login_cache_write(sampass
, &cache_entry
);
1425 /**********************************************************************
1426 End enumeration of the LDAP password list.
1427 *********************************************************************/
1429 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1431 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1432 if (ldap_state
->result
) {
1433 ldap_msgfree(ldap_state
->result
);
1434 ldap_state
->result
= NULL
;
1438 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1439 const char *new_attr
)
1443 if (new_attr
== NULL
) {
1447 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1451 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1453 SMB_ASSERT((*attr_list
) != NULL
);
1454 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1455 (*attr_list
)[i
+1] = NULL
;
1458 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1459 const char ***attr_list
)
1461 append_attr(mem_ctx
, attr_list
, "uidNumber");
1462 append_attr(mem_ctx
, attr_list
, "gidNumber");
1463 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1464 append_attr(mem_ctx
, attr_list
, "loginShell");
1465 append_attr(mem_ctx
, attr_list
, "gecos");
1468 /**********************************************************************
1469 Get struct samu entry from LDAP by username.
1470 *********************************************************************/
1472 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1474 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1475 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1476 LDAPMessage
*result
= NULL
;
1477 LDAPMessage
*entry
= NULL
;
1479 const char ** attr_list
;
1482 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1483 append_attr(user
, &attr_list
,
1484 get_userattr_key2string(ldap_state
->schema_ver
,
1485 LDAP_ATTR_MOD_TIMESTAMP
));
1486 ldapsam_add_unix_attributes(user
, &attr_list
);
1487 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1489 TALLOC_FREE( attr_list
);
1491 if ( rc
!= LDAP_SUCCESS
)
1492 return NT_STATUS_NO_SUCH_USER
;
1494 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1497 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1498 ldap_msgfree(result
);
1499 return NT_STATUS_NO_SUCH_USER
;
1500 } else if (count
> 1) {
1501 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1502 ldap_msgfree(result
);
1503 return NT_STATUS_NO_SUCH_USER
;
1506 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1508 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1509 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1510 ldap_msgfree(result
);
1511 return NT_STATUS_NO_SUCH_USER
;
1513 pdb_set_backend_private_data(user
, result
, NULL
,
1514 my_methods
, PDB_CHANGED
);
1515 smbldap_talloc_autofree_ldapmsg(user
, result
);
1518 ldap_msgfree(result
);
1523 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1524 const struct dom_sid
*sid
, LDAPMessage
**result
)
1527 const char ** attr_list
;
1529 switch ( ldap_state
->schema_ver
) {
1530 case SCHEMAVER_SAMBASAMACCOUNT
: {
1531 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1532 if (tmp_ctx
== NULL
) {
1533 return LDAP_NO_MEMORY
;
1536 attr_list
= get_userattr_list(tmp_ctx
,
1537 ldap_state
->schema_ver
);
1538 append_attr(tmp_ctx
, &attr_list
,
1539 get_userattr_key2string(
1540 ldap_state
->schema_ver
,
1541 LDAP_ATTR_MOD_TIMESTAMP
));
1542 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1543 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1545 TALLOC_FREE(tmp_ctx
);
1547 if ( rc
!= LDAP_SUCCESS
)
1553 DEBUG(0,("Invalid schema version specified\n"));
1559 /**********************************************************************
1560 Get struct samu entry from LDAP by SID.
1561 *********************************************************************/
1563 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1565 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1566 LDAPMessage
*result
= NULL
;
1567 LDAPMessage
*entry
= NULL
;
1571 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1573 if (rc
!= LDAP_SUCCESS
)
1574 return NT_STATUS_NO_SUCH_USER
;
1576 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1579 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1580 "count=%d\n", sid_string_dbg(sid
), count
));
1581 ldap_msgfree(result
);
1582 return NT_STATUS_NO_SUCH_USER
;
1583 } else if (count
> 1) {
1584 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1585 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1587 ldap_msgfree(result
);
1588 return NT_STATUS_NO_SUCH_USER
;
1591 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1593 ldap_msgfree(result
);
1594 return NT_STATUS_NO_SUCH_USER
;
1597 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1598 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1599 ldap_msgfree(result
);
1600 return NT_STATUS_NO_SUCH_USER
;
1603 pdb_set_backend_private_data(user
, result
, NULL
,
1604 my_methods
, PDB_CHANGED
);
1605 smbldap_talloc_autofree_ldapmsg(user
, result
);
1606 return NT_STATUS_OK
;
1609 /********************************************************************
1610 Do the actual modification - also change a plaintext passord if
1612 **********************************************************************/
1614 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1615 struct samu
*newpwd
, char *dn
,
1616 LDAPMod
**mods
, int ldap_op
,
1617 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1619 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1622 if (!newpwd
|| !dn
) {
1623 return NT_STATUS_INVALID_PARAMETER
;
1626 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1627 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1628 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1629 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1632 char *retoid
= NULL
;
1633 struct berval
*retdata
= NULL
;
1634 char *utf8_password
;
1636 size_t converted_size
;
1639 if (!ldap_state
->is_nds_ldap
) {
1641 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1642 LDAP_EXOP_MODIFY_PASSWD
)) {
1643 DEBUG(2, ("ldap password change requested, but LDAP "
1644 "server does not support it -- ignoring\n"));
1645 return NT_STATUS_OK
;
1649 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1650 pdb_get_plaintext_passwd(newpwd
),
1653 return NT_STATUS_NO_MEMORY
;
1656 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1657 TALLOC_FREE(utf8_password
);
1658 return NT_STATUS_NO_MEMORY
;
1661 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1662 DEBUG(0,("ber_alloc_t returns NULL\n"));
1663 TALLOC_FREE(utf8_password
);
1664 TALLOC_FREE(utf8_dn
);
1665 return NT_STATUS_UNSUCCESSFUL
;
1668 if ((ber_printf (ber
, "{") < 0) ||
1669 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1671 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1674 TALLOC_FREE(utf8_dn
);
1675 TALLOC_FREE(utf8_password
);
1676 return NT_STATUS_UNSUCCESSFUL
;
1679 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1680 ret
= ber_printf(ber
, "ts}",
1681 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1684 ret
= ber_printf(ber
, "}");
1688 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1691 TALLOC_FREE(utf8_dn
);
1692 TALLOC_FREE(utf8_password
);
1693 return NT_STATUS_UNSUCCESSFUL
;
1696 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1697 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1699 TALLOC_FREE(utf8_dn
);
1700 TALLOC_FREE(utf8_password
);
1701 return NT_STATUS_UNSUCCESSFUL
;
1704 TALLOC_FREE(utf8_dn
);
1705 TALLOC_FREE(utf8_password
);
1708 if (!ldap_state
->is_nds_ldap
) {
1709 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1710 LDAP_EXOP_MODIFY_PASSWD
,
1711 bv
, NULL
, NULL
, &retoid
,
1714 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1715 pdb_get_plaintext_passwd(newpwd
));
1717 if (rc
!= LDAP_SUCCESS
) {
1718 char *ld_error
= NULL
;
1720 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1721 DEBUG(3, ("Could not set userPassword "
1722 "attribute due to an objectClass "
1723 "violation -- ignoring\n"));
1725 return NT_STATUS_OK
;
1728 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1730 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1731 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1732 SAFE_FREE(ld_error
);
1734 #if defined(LDAP_CONSTRAINT_VIOLATION)
1735 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1736 return NT_STATUS_PASSWORD_RESTRICTION
;
1738 return NT_STATUS_UNSUCCESSFUL
;
1740 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1741 #ifdef DEBUG_PASSWORD
1742 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1745 ber_bvfree(retdata
);
1747 ldap_memfree(retoid
);
1753 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1754 /* may be password change below however */
1758 if (ldap_state
->is_nds_ldap
) {
1759 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1763 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1767 rc
= smbldap_add(ldap_state
->smbldap_state
,
1770 case LDAP_MOD_REPLACE
:
1771 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1775 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1777 return NT_STATUS_INVALID_PARAMETER
;
1780 if (rc
!=LDAP_SUCCESS
) {
1781 return NT_STATUS_UNSUCCESSFUL
;
1785 return NT_STATUS_OK
;
1788 /**********************************************************************
1789 Delete entry from LDAP for username.
1790 *********************************************************************/
1792 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1793 struct samu
* sam_acct
)
1795 struct ldapsam_privates
*priv
=
1796 (struct ldapsam_privates
*)my_methods
->private_data
;
1799 LDAPMessage
*msg
, *entry
;
1800 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1801 const char **attr_list
;
1802 TALLOC_CTX
*mem_ctx
;
1805 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1806 return NT_STATUS_INVALID_PARAMETER
;
1809 sname
= pdb_get_username(sam_acct
);
1811 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1814 mem_ctx
= talloc_new(NULL
);
1815 if (mem_ctx
== NULL
) {
1816 DEBUG(0, ("talloc_new failed\n"));
1820 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1821 if (attr_list
== NULL
) {
1825 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1827 if ((rc
!= LDAP_SUCCESS
) ||
1828 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1829 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1830 DEBUG(5, ("Could not find user %s\n", sname
));
1831 result
= NT_STATUS_NO_SUCH_USER
;
1835 rc
= ldapsam_delete_entry(
1836 priv
, mem_ctx
, entry
,
1837 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1838 LDAP_OBJ_SAMBASAMACCOUNT
: 0,
1841 result
= (rc
== LDAP_SUCCESS
) ?
1842 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1845 TALLOC_FREE(mem_ctx
);
1849 /**********************************************************************
1851 *********************************************************************/
1853 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1855 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1856 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1859 LDAPMessage
*result
= NULL
;
1860 LDAPMessage
*entry
= NULL
;
1861 LDAPMod
**mods
= NULL
;
1862 const char **attr_list
;
1864 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1866 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1867 if (pdb_get_username(newpwd
) == NULL
) {
1868 return NT_STATUS_INVALID_PARAMETER
;
1870 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1871 TALLOC_FREE( attr_list
);
1872 if (rc
!= LDAP_SUCCESS
) {
1873 return NT_STATUS_UNSUCCESSFUL
;
1875 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1876 my_methods
, PDB_CHANGED
);
1877 smbldap_talloc_autofree_ldapmsg(newpwd
, result
);
1880 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1881 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1882 return NT_STATUS_UNSUCCESSFUL
;
1885 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1886 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
1888 return NT_STATUS_UNSUCCESSFUL
;
1891 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1893 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1894 pdb_element_is_changed
)) {
1895 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1898 ldap_mods_free(mods
,True
);
1899 return NT_STATUS_UNSUCCESSFUL
;
1902 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1903 && (mods
== NULL
)) {
1904 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1905 pdb_get_username(newpwd
)));
1907 return NT_STATUS_OK
;
1910 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
1913 ldap_mods_free(mods
,True
);
1919 * We need to set the backend private data to NULL here. For example
1920 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1921 * same one, and with the explicit delete / add logic for attribute
1922 * values the second time we would use the wrong "old" value which
1923 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1925 * The existing LDAPMessage is still being auto-freed by the
1928 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
1931 if (!NT_STATUS_IS_OK(ret
)) {
1935 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1936 pdb_get_username(newpwd
)));
1937 return NT_STATUS_OK
;
1940 /***************************************************************************
1941 Renames a struct samu
1942 - The "rename user script" has full responsibility for changing everything
1943 ***************************************************************************/
1945 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
1946 TALLOC_CTX
*tmp_ctx
,
1948 uint32_t member_rid
);
1950 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
1951 TALLOC_CTX
*mem_ctx
,
1953 struct dom_sid
**pp_sids
,
1955 uint32_t *p_num_groups
);
1957 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1958 struct samu
*old_acct
,
1959 const char *newname
)
1961 const char *oldname
;
1963 char *rename_script
= NULL
;
1964 fstring oldname_lower
, newname_lower
;
1967 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1968 return NT_STATUS_INVALID_PARAMETER
;
1971 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1972 return NT_STATUS_INVALID_PARAMETER
;
1975 oldname
= pdb_get_username(old_acct
);
1977 /* rename the posix user */
1978 rename_script
= lp_rename_user_script(talloc_tos());
1979 if (rename_script
== NULL
) {
1980 return NT_STATUS_NO_MEMORY
;
1983 if (!(*rename_script
)) {
1984 TALLOC_FREE(rename_script
);
1985 return NT_STATUS_ACCESS_DENIED
;
1988 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1991 /* We have to allow the account name to end with a '$'.
1992 Also, follow the semantics in _samr_create_user() and lower case the
1993 posix name but preserve the case in passdb */
1995 fstrcpy( oldname_lower
, oldname
);
1996 if (!strlower_m( oldname_lower
)) {
1997 return NT_STATUS_INVALID_PARAMETER
;
1999 fstrcpy( newname_lower
, newname
);
2000 if (!strlower_m( newname_lower
)) {
2001 return NT_STATUS_INVALID_PARAMETER
;
2004 rename_script
= realloc_string_sub2(rename_script
,
2009 if (!rename_script
) {
2010 return NT_STATUS_NO_MEMORY
;
2012 rename_script
= realloc_string_sub2(rename_script
,
2017 rc
= smbrun(rename_script
, NULL
);
2019 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2020 rename_script
, rc
));
2022 TALLOC_FREE(rename_script
);
2025 smb_nscd_flush_user_cache();
2029 return NT_STATUS_UNSUCCESSFUL
;
2031 return NT_STATUS_OK
;
2034 /**********************************************************************
2035 Add struct samu to LDAP.
2036 *********************************************************************/
2038 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2040 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2041 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2043 LDAPMessage
*result
= NULL
;
2044 LDAPMessage
*entry
= NULL
;
2045 LDAPMod
**mods
= NULL
;
2046 int ldap_op
= LDAP_MOD_REPLACE
;
2047 uint32_t num_result
;
2048 const char **attr_list
;
2049 char *escape_user
= NULL
;
2050 const char *username
= pdb_get_username(newpwd
);
2051 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2052 char *filter
= NULL
;
2054 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2055 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2058 return NT_STATUS_NO_MEMORY
;
2061 if (!username
|| !*username
) {
2062 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2063 status
= NT_STATUS_INVALID_PARAMETER
;
2067 /* free this list after the second search or in case we exit on failure */
2068 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2070 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2072 if (rc
!= LDAP_SUCCESS
) {
2076 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2077 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2081 ldap_msgfree(result
);
2084 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2085 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2087 if (rc
== LDAP_SUCCESS
) {
2088 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2089 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2090 "already in the base, with samba "
2091 "attributes\n", sid_string_dbg(sid
)));
2094 ldap_msgfree(result
);
2099 /* does the entry already exist but without a samba attributes?
2100 we need to return the samba attributes here */
2102 escape_user
= escape_ldap_string(talloc_tos(), username
);
2103 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2105 status
= NT_STATUS_NO_MEMORY
;
2108 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2109 TALLOC_FREE(escape_user
);
2111 status
= NT_STATUS_NO_MEMORY
;
2115 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2116 filter
, attr_list
, &result
);
2117 if ( rc
!= LDAP_SUCCESS
) {
2121 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2123 if (num_result
> 1) {
2124 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2128 /* Check if we need to update an existing entry */
2129 if (num_result
== 1) {
2130 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2131 ldap_op
= LDAP_MOD_REPLACE
;
2132 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2133 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2135 status
= NT_STATUS_NO_MEMORY
;
2139 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2141 /* There might be a SID for this account already - say an idmap entry */
2143 filter
= talloc_asprintf(ctx
,
2144 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2145 get_userattr_key2string(ldap_state
->schema_ver
,
2146 LDAP_ATTR_USER_SID
),
2147 sid_string_talloc(ctx
, sid
),
2148 LDAP_OBJ_IDMAP_ENTRY
,
2149 LDAP_OBJ_SID_ENTRY
);
2151 status
= NT_STATUS_NO_MEMORY
;
2155 /* free old result before doing a new search */
2156 if (result
!= NULL
) {
2157 ldap_msgfree(result
);
2160 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2161 filter
, attr_list
, &result
);
2163 if ( rc
!= LDAP_SUCCESS
) {
2167 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2169 if (num_result
> 1) {
2170 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2174 /* Check if we need to update an existing entry */
2175 if (num_result
== 1) {
2177 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2178 ldap_op
= LDAP_MOD_REPLACE
;
2179 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2180 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2182 status
= NT_STATUS_NO_MEMORY
;
2188 if (num_result
== 0) {
2189 char *escape_username
;
2190 /* Check if we need to add an entry */
2191 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2192 ldap_op
= LDAP_MOD_ADD
;
2194 escape_username
= escape_rdn_val_string_alloc(username
);
2195 if (!escape_username
) {
2196 status
= NT_STATUS_NO_MEMORY
;
2200 if (username
[strlen(username
)-1] == '$') {
2201 dn
= talloc_asprintf(ctx
,
2204 lp_ldap_machine_suffix(talloc_tos()));
2206 dn
= talloc_asprintf(ctx
,
2209 lp_ldap_user_suffix(talloc_tos()));
2212 SAFE_FREE(escape_username
);
2214 status
= NT_STATUS_NO_MEMORY
;
2219 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2220 pdb_element_is_set_or_changed
)) {
2221 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2223 ldap_mods_free(mods
, true);
2229 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2232 switch ( ldap_state
->schema_ver
) {
2233 case SCHEMAVER_SAMBASAMACCOUNT
:
2234 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2237 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2241 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2242 if (!NT_STATUS_IS_OK(ret
)) {
2243 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2244 pdb_get_username(newpwd
),dn
));
2245 ldap_mods_free(mods
, true);
2249 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2250 ldap_mods_free(mods
, true);
2252 status
= NT_STATUS_OK
;
2258 ldap_msgfree(result
);
2263 /**********************************************************************
2264 *********************************************************************/
2266 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2268 LDAPMessage
** result
)
2270 int scope
= LDAP_SCOPE_SUBTREE
;
2272 const char **attr_list
;
2274 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2275 rc
= smbldap_search(ldap_state
->smbldap_state
,
2276 lp_ldap_suffix (talloc_tos()), scope
,
2277 filter
, attr_list
, 0, result
);
2278 TALLOC_FREE(attr_list
);
2283 /**********************************************************************
2284 *********************************************************************/
2286 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2287 GROUP_MAP
*map
, LDAPMessage
*entry
)
2290 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2292 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2293 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2294 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2299 temp
= smbldap_talloc_single_attribute(
2300 ldap_state
->smbldap_state
->ldap_struct
,
2302 get_attr_key2string(groupmap_attr_list
,
2303 LDAP_ATTR_GIDNUMBER
),
2306 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2307 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2311 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2313 map
->gid
= (gid_t
)atol(temp
);
2316 temp
= smbldap_talloc_single_attribute(
2317 ldap_state
->smbldap_state
->ldap_struct
,
2319 get_attr_key2string(groupmap_attr_list
,
2320 LDAP_ATTR_GROUP_SID
),
2323 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2324 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2329 if (!string_to_sid(&map
->sid
, temp
)) {
2330 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2336 temp
= smbldap_talloc_single_attribute(
2337 ldap_state
->smbldap_state
->ldap_struct
,
2339 get_attr_key2string(groupmap_attr_list
,
2340 LDAP_ATTR_GROUP_TYPE
),
2343 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2344 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2348 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2350 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2351 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2352 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2358 temp
= smbldap_talloc_single_attribute(
2359 ldap_state
->smbldap_state
->ldap_struct
,
2361 get_attr_key2string(groupmap_attr_list
,
2362 LDAP_ATTR_DISPLAY_NAME
),
2365 temp
= smbldap_talloc_single_attribute(
2366 ldap_state
->smbldap_state
->ldap_struct
,
2368 get_attr_key2string(groupmap_attr_list
,
2372 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2373 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2378 map
->nt_name
= talloc_strdup(map
, temp
);
2379 if (!map
->nt_name
) {
2385 temp
= smbldap_talloc_single_attribute(
2386 ldap_state
->smbldap_state
->ldap_struct
,
2388 get_attr_key2string(groupmap_attr_list
,
2392 temp
= talloc_strdup(ctx
, "");
2398 map
->comment
= talloc_strdup(map
, temp
);
2399 if (!map
->comment
) {
2404 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2407 id
.type
= ID_TYPE_GID
;
2409 idmap_cache_set_sid2unixid(&map
->sid
, &id
);
2416 /**********************************************************************
2417 *********************************************************************/
2419 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2423 struct ldapsam_privates
*ldap_state
=
2424 (struct ldapsam_privates
*)methods
->private_data
;
2425 LDAPMessage
*result
= NULL
;
2426 LDAPMessage
*entry
= NULL
;
2429 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2431 return NT_STATUS_NO_SUCH_GROUP
;
2434 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2437 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2439 ldap_msgfree(result
);
2440 return NT_STATUS_NO_SUCH_GROUP
;
2444 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2445 "count=%d\n", filter
, count
));
2446 ldap_msgfree(result
);
2447 return NT_STATUS_NO_SUCH_GROUP
;
2450 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2453 ldap_msgfree(result
);
2454 return NT_STATUS_UNSUCCESSFUL
;
2457 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2458 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2459 "group filter %s\n", filter
));
2460 ldap_msgfree(result
);
2461 return NT_STATUS_NO_SUCH_GROUP
;
2464 ldap_msgfree(result
);
2465 return NT_STATUS_OK
;
2468 /**********************************************************************
2469 *********************************************************************/
2471 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2474 char *filter
= NULL
;
2478 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2480 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2481 sid_to_fstring(tmp
, &sid
)) < 0) {
2482 return NT_STATUS_NO_MEMORY
;
2485 status
= ldapsam_getgroup(methods
, filter
, map
);
2490 /**********************************************************************
2491 *********************************************************************/
2493 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2496 char *filter
= NULL
;
2499 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2501 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2502 (unsigned long)gid
) < 0) {
2503 return NT_STATUS_NO_MEMORY
;
2506 status
= ldapsam_getgroup(methods
, filter
, map
);
2511 /**********************************************************************
2512 *********************************************************************/
2514 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2517 char *filter
= NULL
;
2518 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2522 return NT_STATUS_NO_MEMORY
;
2525 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2527 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2528 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2530 TALLOC_FREE(escape_name
);
2531 return NT_STATUS_NO_MEMORY
;
2534 TALLOC_FREE(escape_name
);
2535 status
= ldapsam_getgroup(methods
, filter
, map
);
2540 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2542 const struct dom_sid
*domain_sid
,
2548 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2549 str
, sizeof(str
)-1)) {
2550 DEBUG(10, ("Could not find sambaSID attribute\n"));
2554 if (!string_to_sid(&sid
, str
)) {
2555 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2559 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2560 DEBUG(10, ("SID %s is not in expected domain %s\n",
2561 str
, sid_string_dbg(domain_sid
)));
2565 if (!sid_peek_rid(&sid
, rid
)) {
2566 DEBUG(10, ("Could not peek into RID\n"));
2573 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2574 TALLOC_CTX
*mem_ctx
,
2575 const struct dom_sid
*group
,
2576 uint32_t **pp_member_rids
,
2577 size_t *p_num_members
)
2579 struct ldapsam_privates
*ldap_state
=
2580 (struct ldapsam_privates
*)methods
->private_data
;
2581 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2582 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2583 const char *sid_attrs
[] = { "sambaSID", NULL
};
2584 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2585 LDAPMessage
*result
= NULL
;
2588 char **values
= NULL
;
2593 *pp_member_rids
= NULL
;
2596 filter
= talloc_asprintf(mem_ctx
,
2597 "(&(objectClass=%s)"
2600 LDAP_OBJ_POSIXGROUP
,
2602 sid_string_talloc(mem_ctx
, group
));
2603 if (filter
== NULL
) {
2604 ret
= NT_STATUS_NO_MEMORY
;
2608 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2609 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2612 if (rc
!= LDAP_SUCCESS
)
2615 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2617 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2620 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2621 sid_string_dbg(group
)));
2622 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2627 ret
= NT_STATUS_NO_SUCH_GROUP
;
2631 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2635 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2637 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2638 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2642 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2644 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2646 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2647 if (filter
== NULL
) {
2648 ret
= NT_STATUS_NO_MEMORY
;
2652 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2653 char *escape_memberuid
;
2655 escape_memberuid
= escape_ldap_string(talloc_tos(),
2657 if (escape_memberuid
== NULL
) {
2658 ret
= NT_STATUS_NO_MEMORY
;
2662 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2663 TALLOC_FREE(escape_memberuid
);
2664 if (filter
== NULL
) {
2665 ret
= NT_STATUS_NO_MEMORY
;
2670 filter
= talloc_asprintf_append_buffer(filter
, "))");
2671 if (filter
== NULL
) {
2672 ret
= NT_STATUS_NO_MEMORY
;
2676 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2677 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2680 if (rc
!= LDAP_SUCCESS
)
2683 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2684 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2686 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2688 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2690 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2696 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2700 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2701 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2702 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2706 if (!string_to_sid(&sid
, sidstr
))
2709 if (!sid_check_is_in_our_sam(&sid
)) {
2710 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2711 "in our domain\n"));
2712 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2716 sid_peek_rid(&sid
, &rid
);
2718 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2720 ret
= NT_STATUS_NO_MEMORY
;
2726 filter
= talloc_asprintf(mem_ctx
,
2727 "(&(objectClass=%s)"
2729 LDAP_OBJ_SAMBASAMACCOUNT
,
2732 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2733 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2736 if (rc
!= LDAP_SUCCESS
)
2739 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2741 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2743 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2747 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2749 get_global_sam_sid(),
2751 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2752 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2756 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2758 ret
= NT_STATUS_NO_MEMORY
;
2768 ldap_value_free(values
);
2773 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2774 TALLOC_CTX
*mem_ctx
,
2776 struct dom_sid
**pp_sids
,
2778 uint32_t *p_num_groups
)
2780 struct ldapsam_privates
*ldap_state
=
2781 (struct ldapsam_privates
*)methods
->private_data
;
2782 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2784 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2787 LDAPMessage
*result
= NULL
;
2789 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2793 gid_t primary_gid
= -1;
2798 if (pdb_get_username(user
) == NULL
) {
2799 return NT_STATUS_INVALID_PARAMETER
;
2802 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2803 if (escape_name
== NULL
)
2804 return NT_STATUS_NO_MEMORY
;
2806 if (user
->unix_pw
) {
2807 primary_gid
= user
->unix_pw
->pw_gid
;
2809 /* retrieve the users primary gid */
2810 filter
= talloc_asprintf(mem_ctx
,
2811 "(&(objectClass=%s)(uid=%s))",
2812 LDAP_OBJ_SAMBASAMACCOUNT
,
2814 if (filter
== NULL
) {
2815 ret
= NT_STATUS_NO_MEMORY
;
2819 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2820 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2822 if (rc
!= LDAP_SUCCESS
)
2825 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2827 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2831 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2832 ret
= NT_STATUS_NO_SUCH_USER
;
2835 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2837 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2839 DEBUG (1, ("Unable to find the member's gid!\n"));
2840 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2843 primary_gid
= strtoul(gidstr
, NULL
, 10);
2846 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2847 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2852 filter
= talloc_asprintf(mem_ctx
,
2853 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2854 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2855 if (filter
== NULL
) {
2856 ret
= NT_STATUS_NO_MEMORY
;
2860 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2861 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2863 if (rc
!= LDAP_SUCCESS
)
2866 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2874 /* We need to add the primary group as the first gid/sid */
2876 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2877 ret
= NT_STATUS_NO_MEMORY
;
2881 /* This sid will be replaced later */
2883 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2885 if (!NT_STATUS_IS_OK(ret
)) {
2889 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2891 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2898 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2900 str
, sizeof(str
)-1))
2903 if (!string_to_sid(&sid
, str
))
2906 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2908 str
, sizeof(str
)-1))
2911 gid
= strtoul(str
, &end
, 10);
2913 if (PTR_DIFF(end
, str
) != strlen(str
))
2916 if (gid
== primary_gid
) {
2917 sid_copy(&(*pp_sids
)[0], &sid
);
2919 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2921 ret
= NT_STATUS_NO_MEMORY
;
2924 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2926 if (!NT_STATUS_IS_OK(ret
)) {
2932 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2933 DEBUG(3, ("primary group of [%s] not found\n",
2934 pdb_get_username(user
)));
2935 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2939 *p_num_groups
= num_sids
;
2945 TALLOC_FREE(escape_name
);
2949 /**********************************************************************
2950 * Augment a posixGroup object with a sambaGroupMapping domgroup
2951 *********************************************************************/
2953 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2954 struct ldapsam_privates
*ldap_state
,
2957 const char *filter
, *dn
;
2958 LDAPMessage
*msg
, *entry
;
2962 filter
= talloc_asprintf(mem_ctx
,
2963 "(&(objectClass=%s)(gidNumber=%u))",
2964 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
2965 if (filter
== NULL
) {
2966 return NT_STATUS_NO_MEMORY
;
2969 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2970 get_attr_list(mem_ctx
, groupmap_attr_list
),
2972 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
2974 if ((rc
!= LDAP_SUCCESS
) ||
2975 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2976 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2977 return NT_STATUS_NO_SUCH_GROUP
;
2980 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2982 return NT_STATUS_NO_MEMORY
;
2986 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
2988 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
2989 sid_string_talloc(mem_ctx
, &map
->sid
));
2990 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
2991 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2992 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2994 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2996 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
2998 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2999 if (rc
!= LDAP_SUCCESS
) {
3000 return NT_STATUS_ACCESS_DENIED
;
3003 return NT_STATUS_OK
;
3006 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3009 struct ldapsam_privates
*ldap_state
=
3010 (struct ldapsam_privates
*)methods
->private_data
;
3011 LDAPMessage
*msg
= NULL
;
3012 LDAPMod
**mods
= NULL
;
3013 const char *attrs
[] = { NULL
};
3017 TALLOC_CTX
*mem_ctx
;
3024 mem_ctx
= talloc_new(NULL
);
3025 if (mem_ctx
== NULL
) {
3026 DEBUG(0, ("talloc_new failed\n"));
3027 return NT_STATUS_NO_MEMORY
;
3030 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3031 sid_string_talloc(mem_ctx
, &map
->sid
));
3032 if (filter
== NULL
) {
3033 result
= NT_STATUS_NO_MEMORY
;
3037 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3038 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3039 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3041 if ((rc
== LDAP_SUCCESS
) &&
3042 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3044 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3045 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3046 result
= NT_STATUS_GROUP_EXISTS
;
3050 switch (map
->sid_name_use
) {
3052 case SID_NAME_DOM_GRP
:
3053 /* To map a domain group we need to have a posix group
3055 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3059 case SID_NAME_ALIAS
:
3060 if (!sid_check_is_in_our_sam(&map
->sid
)
3061 && !sid_check_is_in_builtin(&map
->sid
) )
3063 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3064 sid_string_dbg(&map
->sid
)));
3065 result
= NT_STATUS_INVALID_PARAMETER
;
3071 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3072 sid_type_lookup(map
->sid_name_use
)));
3073 result
= NT_STATUS_INVALID_PARAMETER
;
3077 /* Domain groups have been mapped in a separate routine, we have to
3078 * create an alias now */
3080 if (map
->gid
== -1) {
3081 DEBUG(10, ("Refusing to map gid==-1\n"));
3082 result
= NT_STATUS_INVALID_PARAMETER
;
3086 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3087 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3088 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3089 result
= NT_STATUS_GROUP_EXISTS
;
3093 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3094 * the best we can get out of LDAP. */
3096 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3097 sid_string_talloc(mem_ctx
, &map
->sid
),
3098 lp_ldap_group_suffix(talloc_tos()));
3100 result
= NT_STATUS_NO_MEMORY
;
3106 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3107 LDAP_OBJ_SID_ENTRY
);
3108 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3110 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3111 sid_string_talloc(mem_ctx
, &map
->sid
));
3112 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3113 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3114 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3116 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3118 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3119 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3120 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3122 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3124 result
= (rc
== LDAP_SUCCESS
) ?
3125 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3128 TALLOC_FREE(mem_ctx
);
3132 /**********************************************************************
3133 * Update a group mapping entry. We're quite strict about what can be changed:
3134 * Only the description and displayname may be changed. It simply does not
3135 * make any sense to change the SID, gid or the type in a mapping.
3136 *********************************************************************/
3138 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3141 struct ldapsam_privates
*ldap_state
=
3142 (struct ldapsam_privates
*)methods
->private_data
;
3144 const char *filter
, *dn
;
3145 LDAPMessage
*msg
= NULL
;
3146 LDAPMessage
*entry
= NULL
;
3147 LDAPMod
**mods
= NULL
;
3148 TALLOC_CTX
*mem_ctx
;
3151 mem_ctx
= talloc_new(NULL
);
3152 if (mem_ctx
== NULL
) {
3153 DEBUG(0, ("talloc_new failed\n"));
3154 return NT_STATUS_NO_MEMORY
;
3157 /* Make 100% sure that sid, gid and type are not changed by looking up
3158 * exactly the values we're given in LDAP. */
3160 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3161 "(sambaSid=%s)(gidNumber=%u)"
3162 "(sambaGroupType=%d))",
3164 sid_string_talloc(mem_ctx
, &map
->sid
),
3165 (unsigned int)map
->gid
, map
->sid_name_use
);
3166 if (filter
== NULL
) {
3167 result
= NT_STATUS_NO_MEMORY
;
3171 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3172 get_attr_list(mem_ctx
, groupmap_attr_list
),
3174 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3176 if ((rc
!= LDAP_SUCCESS
) ||
3177 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3178 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3179 result
= NT_STATUS_NO_SUCH_GROUP
;
3183 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3186 result
= NT_STATUS_NO_MEMORY
;
3191 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3193 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3195 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3198 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3199 "nothing to do\n"));
3200 result
= NT_STATUS_OK
;
3204 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3206 if (rc
!= LDAP_SUCCESS
) {
3207 result
= NT_STATUS_ACCESS_DENIED
;
3211 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3212 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3214 result
= NT_STATUS_OK
;
3217 TALLOC_FREE(mem_ctx
);
3221 /**********************************************************************
3222 *********************************************************************/
3224 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3227 struct ldapsam_privates
*priv
=
3228 (struct ldapsam_privates
*)methods
->private_data
;
3229 LDAPMessage
*msg
, *entry
;
3232 TALLOC_CTX
*mem_ctx
;
3235 mem_ctx
= talloc_new(NULL
);
3236 if (mem_ctx
== NULL
) {
3237 DEBUG(0, ("talloc_new failed\n"));
3238 return NT_STATUS_NO_MEMORY
;
3241 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3242 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3243 sid_string_talloc(mem_ctx
, &sid
));
3244 if (filter
== NULL
) {
3245 result
= NT_STATUS_NO_MEMORY
;
3248 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3249 get_attr_list(mem_ctx
, groupmap_attr_list
),
3251 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3253 if ((rc
!= LDAP_SUCCESS
) ||
3254 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3255 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3256 result
= NT_STATUS_NO_SUCH_GROUP
;
3260 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3261 get_attr_list(mem_ctx
,
3262 groupmap_attr_list_to_delete
));
3264 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3265 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3266 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3267 const char *attrs
[] = { "sambaGroupType", "description",
3268 "displayName", "sambaSIDList",
3271 /* Second try. Don't delete the sambaSID attribute, this is
3272 for "old" entries that are tacked on a winbind
3275 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3276 LDAP_OBJ_GROUPMAP
, attrs
);
3279 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3280 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3281 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3282 const char *attrs
[] = { "sambaGroupType", "description",
3283 "displayName", "sambaSIDList",
3284 "gidNumber", NULL
};
3286 /* Third try. This is a post-3.0.21 alias (containing only
3287 * sambaSidEntry and sambaGroupMapping classes), we also have
3288 * to delete the gidNumber attribute, only the sambaSidEntry
3291 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3292 LDAP_OBJ_GROUPMAP
, attrs
);
3295 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3298 TALLOC_FREE(mem_ctx
);
3302 /**********************************************************************
3303 *********************************************************************/
3305 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3308 struct ldapsam_privates
*ldap_state
=
3309 (struct ldapsam_privates
*)my_methods
->private_data
;
3310 char *filter
= NULL
;
3312 const char **attr_list
;
3314 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3316 return NT_STATUS_NO_MEMORY
;
3318 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3319 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3320 LDAP_SCOPE_SUBTREE
, filter
,
3321 attr_list
, 0, &ldap_state
->result
);
3322 TALLOC_FREE(attr_list
);
3324 if (rc
!= LDAP_SUCCESS
) {
3325 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3326 ldap_err2string(rc
)));
3327 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3328 lp_ldap_suffix(talloc_tos()), filter
));
3329 ldap_msgfree(ldap_state
->result
);
3330 ldap_state
->result
= NULL
;
3331 TALLOC_FREE(filter
);
3332 return NT_STATUS_UNSUCCESSFUL
;
3335 TALLOC_FREE(filter
);
3337 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3338 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3339 ldap_state
->result
)));
3342 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3343 ldap_state
->result
);
3344 ldap_state
->index
= 0;
3346 return NT_STATUS_OK
;
3349 /**********************************************************************
3350 *********************************************************************/
3352 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3354 ldapsam_endsampwent(my_methods
);
3357 /**********************************************************************
3358 *********************************************************************/
3360 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3363 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3364 struct ldapsam_privates
*ldap_state
=
3365 (struct ldapsam_privates
*)my_methods
->private_data
;
3369 if (!ldap_state
->entry
)
3372 ldap_state
->index
++;
3373 bret
= init_group_from_ldap(ldap_state
, map
,
3377 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3381 return NT_STATUS_OK
;
3384 /**********************************************************************
3385 *********************************************************************/
3387 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3388 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3389 GROUP_MAP
***pp_rmap
,
3390 size_t *p_num_entries
,
3393 GROUP_MAP
*map
= NULL
;
3399 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3400 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3402 return NT_STATUS_ACCESS_DENIED
;
3407 map
= talloc_zero(NULL
, GROUP_MAP
);
3409 return NT_STATUS_NO_MEMORY
;
3412 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3417 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3418 sid_name_use
!= map
->sid_name_use
) {
3419 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3420 "not of the requested type\n",
3424 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3425 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3426 "non mapped\n", map
->nt_name
));
3430 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3431 GROUP_MAP
*, entries
+ 1);
3433 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3434 "enlarge group map!\n"));
3435 return NT_STATUS_UNSUCCESSFUL
;
3438 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3443 ldapsam_endsamgrent(methods
);
3445 *p_num_entries
= entries
;
3447 return NT_STATUS_OK
;
3450 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3451 const struct dom_sid
*alias
,
3452 const struct dom_sid
*member
,
3455 struct ldapsam_privates
*ldap_state
=
3456 (struct ldapsam_privates
*)methods
->private_data
;
3458 LDAPMessage
*result
= NULL
;
3459 LDAPMessage
*entry
= NULL
;
3461 LDAPMod
**mods
= NULL
;
3463 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3466 char *filter
= NULL
;
3468 if (sid_check_is_in_builtin(alias
)) {
3469 type
= SID_NAME_ALIAS
;
3472 if (sid_check_is_in_our_sam(alias
)) {
3473 type
= SID_NAME_ALIAS
;
3476 if (type
== SID_NAME_USE_NONE
) {
3477 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3478 sid_string_dbg(alias
)));
3479 return NT_STATUS_NO_SUCH_ALIAS
;
3482 if (asprintf(&filter
,
3483 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3484 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3486 return NT_STATUS_NO_MEMORY
;
3489 if (ldapsam_search_one_group(ldap_state
, filter
,
3490 &result
) != LDAP_SUCCESS
) {
3492 return NT_STATUS_NO_SUCH_ALIAS
;
3495 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3499 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3500 ldap_msgfree(result
);
3502 return NT_STATUS_NO_SUCH_ALIAS
;
3506 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3507 "filter %s: count=%d\n", filter
, count
));
3508 ldap_msgfree(result
);
3510 return NT_STATUS_NO_SUCH_ALIAS
;
3515 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3519 ldap_msgfree(result
);
3520 return NT_STATUS_UNSUCCESSFUL
;
3523 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3525 ldap_msgfree(result
);
3526 return NT_STATUS_UNSUCCESSFUL
;
3529 smbldap_set_mod(&mods
, modop
,
3530 get_attr_key2string(groupmap_attr_list
,
3531 LDAP_ATTR_SID_LIST
),
3532 sid_to_fstring(tmp
, member
));
3534 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3536 ldap_mods_free(mods
, True
);
3537 ldap_msgfree(result
);
3540 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3541 return NT_STATUS_MEMBER_IN_ALIAS
;
3544 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3545 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3548 if (rc
!= LDAP_SUCCESS
) {
3549 return NT_STATUS_UNSUCCESSFUL
;
3552 return NT_STATUS_OK
;
3555 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3556 const struct dom_sid
*alias
,
3557 const struct dom_sid
*member
)
3559 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3562 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3563 const struct dom_sid
*alias
,
3564 const struct dom_sid
*member
)
3566 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3570 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3571 const struct dom_sid
*alias
,
3572 TALLOC_CTX
*mem_ctx
,
3573 struct dom_sid
**pp_members
,
3574 size_t *p_num_members
)
3576 struct ldapsam_privates
*ldap_state
=
3577 (struct ldapsam_privates
*)methods
->private_data
;
3578 LDAPMessage
*result
= NULL
;
3579 LDAPMessage
*entry
= NULL
;
3581 char **values
= NULL
;
3583 char *filter
= NULL
;
3584 uint32_t num_members
= 0;
3585 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3591 if (sid_check_is_in_builtin(alias
)) {
3592 type
= SID_NAME_ALIAS
;
3595 if (sid_check_is_in_our_sam(alias
)) {
3596 type
= SID_NAME_ALIAS
;
3599 if (type
== SID_NAME_USE_NONE
) {
3600 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3601 sid_string_dbg(alias
)));
3602 return NT_STATUS_NO_SUCH_ALIAS
;
3605 if (asprintf(&filter
,
3606 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3607 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3609 return NT_STATUS_NO_MEMORY
;
3612 if (ldapsam_search_one_group(ldap_state
, filter
,
3613 &result
) != LDAP_SUCCESS
) {
3615 return NT_STATUS_NO_SUCH_ALIAS
;
3618 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3622 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3623 ldap_msgfree(result
);
3625 return NT_STATUS_NO_SUCH_ALIAS
;
3629 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3630 "filter %s: count=%d\n", filter
, count
));
3631 ldap_msgfree(result
);
3633 return NT_STATUS_NO_SUCH_ALIAS
;
3638 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3642 ldap_msgfree(result
);
3643 return NT_STATUS_UNSUCCESSFUL
;
3646 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3648 get_attr_key2string(groupmap_attr_list
,
3649 LDAP_ATTR_SID_LIST
));
3651 if (values
== NULL
) {
3652 ldap_msgfree(result
);
3653 return NT_STATUS_OK
;
3656 count
= ldap_count_values(values
);
3658 for (i
=0; i
<count
; i
++) {
3659 struct dom_sid member
;
3662 if (!string_to_sid(&member
, values
[i
]))
3665 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3667 if (!NT_STATUS_IS_OK(status
)) {
3668 ldap_value_free(values
);
3669 ldap_msgfree(result
);
3674 *p_num_members
= num_members
;
3675 ldap_value_free(values
);
3676 ldap_msgfree(result
);
3678 return NT_STATUS_OK
;
3681 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3682 TALLOC_CTX
*mem_ctx
,
3683 const struct dom_sid
*domain_sid
,
3684 const struct dom_sid
*members
,
3686 uint32_t **pp_alias_rids
,
3687 size_t *p_num_alias_rids
)
3689 struct ldapsam_privates
*ldap_state
=
3690 (struct ldapsam_privates
*)methods
->private_data
;
3693 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3695 LDAPMessage
*result
= NULL
;
3696 LDAPMessage
*entry
= NULL
;
3700 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3701 bool is_builtin
= false;
3702 bool sid_added
= false;
3704 *pp_alias_rids
= NULL
;
3705 *p_num_alias_rids
= 0;
3707 if (sid_check_is_builtin(domain_sid
)) {
3709 type
= SID_NAME_ALIAS
;
3712 if (sid_check_is_our_sam(domain_sid
)) {
3713 type
= SID_NAME_ALIAS
;
3716 if (type
== SID_NAME_USE_NONE
) {
3717 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3718 sid_string_dbg(domain_sid
)));
3719 return NT_STATUS_UNSUCCESSFUL
;
3722 if (num_members
== 0) {
3723 return NT_STATUS_OK
;
3726 filter
= talloc_asprintf(mem_ctx
,
3727 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3728 LDAP_OBJ_GROUPMAP
, type
);
3730 for (i
=0; i
<num_members
; i
++)
3731 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3733 sid_string_talloc(mem_ctx
,
3736 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3738 if (filter
== NULL
) {
3739 return NT_STATUS_NO_MEMORY
;
3743 ldap_state
->search_cache
.filter
&&
3744 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3745 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3746 result
= ldap_state
->search_cache
.result
;
3747 ldap_state
->search_cache
.result
= NULL
;
3749 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3750 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3751 if (rc
!= LDAP_SUCCESS
) {
3752 return NT_STATUS_UNSUCCESSFUL
;
3754 smbldap_talloc_autofree_ldapmsg(filter
, result
);
3757 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3759 for (entry
= ldap_first_entry(ldap_struct
, result
);
3761 entry
= ldap_next_entry(ldap_struct
, entry
))
3767 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3773 if (!string_to_sid(&sid
, sid_str
))
3776 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3781 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3782 p_num_alias_rids
)) {
3783 return NT_STATUS_NO_MEMORY
;
3787 if (!is_builtin
&& !sid_added
) {
3788 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3790 * Note: result is a talloc child of filter because of the
3791 * smbldap_talloc_autofree_ldapmsg() usage
3793 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3794 ldap_state
->search_cache
.result
= result
;
3797 return NT_STATUS_OK
;
3800 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3801 enum pdb_policy_type type
,
3804 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3806 LDAPMod
**mods
= NULL
;
3807 fstring value_string
;
3808 const char *policy_attr
= NULL
;
3810 struct ldapsam_privates
*ldap_state
=
3811 (struct ldapsam_privates
*)methods
->private_data
;
3813 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3815 if (!ldap_state
->domain_dn
) {
3816 return NT_STATUS_INVALID_PARAMETER
;
3819 policy_attr
= get_account_policy_attr(type
);
3820 if (policy_attr
== NULL
) {
3821 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3826 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3828 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3830 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3833 ldap_mods_free(mods
, True
);
3835 if (rc
!= LDAP_SUCCESS
) {
3839 if (!cache_account_policy_set(type
, value
)) {
3840 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3841 "update local tdb cache\n"));
3845 return NT_STATUS_OK
;
3848 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3849 enum pdb_policy_type type
,
3852 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3856 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3857 enum pdb_policy_type type
,
3860 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3861 LDAPMessage
*result
= NULL
;
3862 LDAPMessage
*entry
= NULL
;
3867 const char *policy_attr
= NULL
;
3869 struct ldapsam_privates
*ldap_state
=
3870 (struct ldapsam_privates
*)methods
->private_data
;
3872 const char *attrs
[2];
3874 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3876 if (!ldap_state
->domain_dn
) {
3877 return NT_STATUS_INVALID_PARAMETER
;
3880 policy_attr
= get_account_policy_attr(type
);
3882 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3883 "policy index: %d\n", type
));
3887 attrs
[0] = policy_attr
;
3890 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3891 if (filter
== NULL
) {
3892 return NT_STATUS_NO_MEMORY
;
3894 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3895 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
3897 TALLOC_FREE(filter
);
3898 if (rc
!= LDAP_SUCCESS
) {
3902 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3907 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3908 if (entry
== NULL
) {
3912 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3917 *value
= (uint32_t)atol(vals
[0]);
3919 ntstatus
= NT_STATUS_OK
;
3923 ldap_value_free(vals
);
3924 ldap_msgfree(result
);
3929 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3931 - if user hasn't decided to use account policies inside LDAP just reuse the
3934 - if there is a valid cache entry, return that
3935 - if there is an LDAP entry, update cache and return
3936 - otherwise set to default, update cache and return
3940 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3941 enum pdb_policy_type type
,
3944 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3946 if (cache_account_policy_get(type
, value
)) {
3947 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3949 return NT_STATUS_OK
;
3952 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
3954 if (NT_STATUS_IS_OK(ntstatus
)) {
3958 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3962 /* should we automagically migrate old tdb value here ? */
3963 if (account_policy_get(type
, value
))
3966 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3967 "default\n", type
));
3970 if (!account_policy_get_default(type
, value
)) {
3976 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
3977 if (!NT_STATUS_IS_OK(ntstatus
)) {
3983 if (!cache_account_policy_set(type
, *value
)) {
3984 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3985 "tdb as a cache\n"));
3986 return NT_STATUS_UNSUCCESSFUL
;
3989 return NT_STATUS_OK
;
3992 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3993 const struct dom_sid
*domain_sid
,
3997 enum lsa_SidType
*attrs
)
3999 struct ldapsam_privates
*ldap_state
=
4000 (struct ldapsam_privates
*)methods
->private_data
;
4001 LDAPMessage
*msg
= NULL
;
4003 char *allsids
= NULL
;
4004 int i
, rc
, num_mapped
;
4005 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4006 TALLOC_CTX
*mem_ctx
;
4010 mem_ctx
= talloc_new(NULL
);
4011 if (mem_ctx
== NULL
) {
4012 DEBUG(0, ("talloc_new failed\n"));
4016 if (!sid_check_is_builtin(domain_sid
) &&
4017 !sid_check_is_our_sam(domain_sid
)) {
4018 result
= NT_STATUS_INVALID_PARAMETER
;
4022 if (num_rids
== 0) {
4023 result
= NT_STATUS_NONE_MAPPED
;
4027 for (i
=0; i
<num_rids
; i
++)
4028 attrs
[i
] = SID_NAME_UNKNOWN
;
4030 allsids
= talloc_strdup(mem_ctx
, "");
4031 if (allsids
== NULL
) {
4035 for (i
=0; i
<num_rids
; i
++) {
4037 sid_compose(&sid
, domain_sid
, rids
[i
]);
4038 allsids
= talloc_asprintf_append_buffer(
4039 allsids
, "(sambaSid=%s)",
4040 sid_string_talloc(mem_ctx
, &sid
));
4041 if (allsids
== NULL
) {
4046 /* First look for users */
4050 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4052 filter
= talloc_asprintf(
4053 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4054 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4056 if (filter
== NULL
) {
4060 rc
= smbldap_search(ldap_state
->smbldap_state
,
4061 lp_ldap_user_suffix(talloc_tos()),
4062 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4064 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4067 if (rc
!= LDAP_SUCCESS
)
4070 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4073 for (entry
= ldap_first_entry(ld
, msg
);
4075 entry
= ldap_next_entry(ld
, entry
)) {
4080 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4082 DEBUG(2, ("Could not find sid from ldap entry\n"));
4086 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4089 DEBUG(2, ("Could not retrieve uid attribute\n"));
4093 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4094 if (rid
== rids
[rid_index
])
4098 if (rid_index
== num_rids
) {
4099 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4103 attrs
[rid_index
] = SID_NAME_USER
;
4104 names
[rid_index
] = name
;
4108 if (num_mapped
== num_rids
) {
4109 /* No need to look for groups anymore -- we're done */
4110 result
= NT_STATUS_OK
;
4114 /* Same game for groups */
4118 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4119 "sambaGroupType", NULL
};
4121 filter
= talloc_asprintf(
4122 mem_ctx
, "(&(objectClass=%s)(|%s))",
4123 LDAP_OBJ_GROUPMAP
, allsids
);
4124 if (filter
== NULL
) {
4128 rc
= smbldap_search(ldap_state
->smbldap_state
,
4129 lp_ldap_suffix(talloc_tos()),
4130 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4132 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4135 if (rc
!= LDAP_SUCCESS
)
4138 /* ldap_struct might have changed due to a reconnect */
4140 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4142 /* For consistency checks, we already checked we're only domain or builtin */
4144 is_builtin
= sid_check_is_builtin(domain_sid
);
4146 for (entry
= ldap_first_entry(ld
, msg
);
4148 entry
= ldap_next_entry(ld
, entry
))
4153 enum lsa_SidType type
;
4154 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4156 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4159 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4164 type
= (enum lsa_SidType
)atol(attr
);
4166 /* Consistency checks */
4167 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4168 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4169 (type
!= SID_NAME_DOM_GRP
)))) {
4170 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4173 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4175 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4179 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4182 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4184 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4188 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4193 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4194 if (rid
== rids
[rid_index
])
4198 if (rid_index
== num_rids
) {
4199 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4203 attrs
[rid_index
] = type
;
4204 names
[rid_index
] = attr
;
4208 result
= NT_STATUS_NONE_MAPPED
;
4211 result
= (num_mapped
== num_rids
) ?
4212 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4214 TALLOC_FREE(mem_ctx
);
4218 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4220 char *filter
= NULL
;
4221 char *escaped
= NULL
;
4222 char *result
= NULL
;
4224 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4225 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4229 escaped
= escape_ldap_string(talloc_tos(), username
);
4230 if (escaped
== NULL
) goto done
;
4232 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4236 TALLOC_FREE(escaped
);
4241 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4245 const char **result
;
4247 va_start(ap
, mem_ctx
);
4248 while (va_arg(ap
, const char *) != NULL
)
4252 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4256 va_start(ap
, mem_ctx
);
4257 for (i
=0; i
<num
; i
++) {
4258 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4259 if (result
[i
] == NULL
) {
4260 talloc_free(result
);
4271 struct ldap_search_state
{
4272 struct smbldap_state
*connection
;
4274 uint32_t acct_flags
;
4275 uint16_t group_type
;
4282 void *pagedresults_cookie
;
4284 LDAPMessage
*entries
, *current_entry
;
4285 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4286 TALLOC_CTX
*mem_ctx
,
4287 LDAP
*ld
, LDAPMessage
*entry
,
4288 struct samr_displayentry
*result
);
4291 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4293 struct ldap_search_state
*state
=
4294 (struct ldap_search_state
*)search
->private_data
;
4296 int rc
= LDAP_OPERATIONS_ERROR
;
4298 state
->entries
= NULL
;
4300 if (state
->connection
->paged_results
) {
4301 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4302 state
->scope
, state
->filter
,
4303 state
->attrs
, state
->attrsonly
,
4304 lp_ldap_page_size(), &state
->entries
,
4305 &state
->pagedresults_cookie
);
4308 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4310 if (state
->entries
!= NULL
) {
4311 /* Left over from unsuccessful paged attempt */
4312 ldap_msgfree(state
->entries
);
4313 state
->entries
= NULL
;
4316 rc
= smbldap_search(state
->connection
, state
->base
,
4317 state
->scope
, state
->filter
, state
->attrs
,
4318 state
->attrsonly
, &state
->entries
);
4320 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4323 /* Ok, the server was lying. It told us it could do paged
4324 * searches when it could not. */
4325 state
->connection
->paged_results
= False
;
4328 ld
= state
->connection
->ldap_struct
;
4330 DEBUG(5, ("Don't have an LDAP connection right after a "
4334 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4339 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4341 struct ldap_search_state
*state
=
4342 (struct ldap_search_state
*)search
->private_data
;
4345 if (!state
->connection
->paged_results
) {
4346 /* There is no next page when there are no paged results */
4350 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4351 state
->scope
, state
->filter
, state
->attrs
,
4352 state
->attrsonly
, lp_ldap_page_size(),
4354 &state
->pagedresults_cookie
);
4356 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4359 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4361 if (state
->current_entry
== NULL
) {
4362 ldap_msgfree(state
->entries
);
4363 state
->entries
= NULL
;
4370 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4371 struct samr_displayentry
*entry
)
4373 struct ldap_search_state
*state
=
4374 (struct ldap_search_state
*)search
->private_data
;
4378 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4381 if ((state
->entries
== NULL
) &&
4382 !ldapsam_search_nextpage(search
))
4385 if (state
->current_entry
== NULL
) {
4389 result
= state
->ldap2displayentry(state
, search
,
4390 state
->connection
->ldap_struct
,
4391 state
->current_entry
, entry
);
4395 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4396 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4397 if (dn
!= NULL
) ldap_memfree(dn
);
4400 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4402 if (state
->current_entry
== NULL
) {
4403 ldap_msgfree(state
->entries
);
4404 state
->entries
= NULL
;
4407 if (!result
) goto retry
;
4412 static void ldapsam_search_end(struct pdb_search
*search
)
4414 struct ldap_search_state
*state
=
4415 (struct ldap_search_state
*)search
->private_data
;
4418 if (state
->pagedresults_cookie
== NULL
)
4421 if (state
->entries
!= NULL
)
4422 ldap_msgfree(state
->entries
);
4424 state
->entries
= NULL
;
4425 state
->current_entry
= NULL
;
4427 if (!state
->connection
->paged_results
)
4430 /* Tell the LDAP server we're not interested in the rest anymore. */
4432 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4433 state
->filter
, state
->attrs
,
4434 state
->attrsonly
, 0, &state
->entries
,
4435 &state
->pagedresults_cookie
);
4437 if (rc
!= LDAP_SUCCESS
)
4438 DEBUG(5, ("Could not end search properly\n"));
4443 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4444 TALLOC_CTX
*mem_ctx
,
4445 LDAP
*ld
, LDAPMessage
*entry
,
4446 struct samr_displayentry
*result
)
4449 size_t converted_size
;
4451 uint32_t acct_flags
;
4453 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4454 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4455 acct_flags
= ACB_NORMAL
;
4457 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4458 ldap_value_free(vals
);
4461 if ((state
->acct_flags
!= 0) &&
4462 ((state
->acct_flags
& acct_flags
) == 0))
4465 result
->acct_flags
= acct_flags
;
4466 result
->account_name
= "";
4467 result
->fullname
= "";
4468 result
->description
= "";
4470 vals
= ldap_get_values(ld
, entry
, "uid");
4471 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4472 DEBUG(5, ("\"uid\" not found\n"));
4475 if (!pull_utf8_talloc(mem_ctx
,
4476 discard_const_p(char *, &result
->account_name
),
4477 vals
[0], &converted_size
))
4479 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4483 ldap_value_free(vals
);
4485 vals
= ldap_get_values(ld
, entry
, "displayName");
4486 if ((vals
== NULL
) || (vals
[0] == NULL
))
4487 DEBUG(8, ("\"displayName\" not found\n"));
4488 else if (!pull_utf8_talloc(mem_ctx
,
4489 discard_const_p(char *, &result
->fullname
),
4490 vals
[0], &converted_size
))
4492 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4496 ldap_value_free(vals
);
4498 vals
= ldap_get_values(ld
, entry
, "description");
4499 if ((vals
== NULL
) || (vals
[0] == NULL
))
4500 DEBUG(8, ("\"description\" not found\n"));
4501 else if (!pull_utf8_talloc(mem_ctx
,
4502 discard_const_p(char *, &result
->description
),
4503 vals
[0], &converted_size
))
4505 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4509 ldap_value_free(vals
);
4511 if ((result
->account_name
== NULL
) ||
4512 (result
->fullname
== NULL
) ||
4513 (result
->description
== NULL
)) {
4514 DEBUG(0, ("talloc failed\n"));
4518 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4519 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4520 DEBUG(0, ("\"objectSid\" not found\n"));
4524 if (!string_to_sid(&sid
, vals
[0])) {
4525 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4526 ldap_value_free(vals
);
4529 ldap_value_free(vals
);
4531 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4532 DEBUG(0, ("sid %s does not belong to our domain\n",
4533 sid_string_dbg(&sid
)));
4541 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4542 struct pdb_search
*search
,
4543 uint32_t acct_flags
)
4545 struct ldapsam_privates
*ldap_state
=
4546 (struct ldapsam_privates
*)methods
->private_data
;
4547 struct ldap_search_state
*state
;
4549 state
= talloc(search
, struct ldap_search_state
);
4550 if (state
== NULL
) {
4551 DEBUG(0, ("talloc failed\n"));
4555 state
->connection
= ldap_state
->smbldap_state
;
4557 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4558 state
->base
= lp_ldap_user_suffix(talloc_tos());
4559 else if ((acct_flags
!= 0) &&
4560 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4561 state
->base
= lp_ldap_machine_suffix(talloc_tos());
4563 state
->base
= lp_ldap_suffix(talloc_tos());
4565 state
->acct_flags
= acct_flags
;
4566 state
->base
= talloc_strdup(search
, state
->base
);
4567 state
->scope
= LDAP_SCOPE_SUBTREE
;
4568 state
->filter
= get_ldap_filter(search
, "*");
4569 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4570 "displayName", "description",
4571 "sambaAcctFlags", NULL
);
4572 state
->attrsonly
= 0;
4573 state
->pagedresults_cookie
= NULL
;
4574 state
->entries
= NULL
;
4575 state
->ldap2displayentry
= ldapuser2displayentry
;
4577 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4578 DEBUG(0, ("talloc failed\n"));
4582 search
->private_data
= state
;
4583 search
->next_entry
= ldapsam_search_next_entry
;
4584 search
->search_end
= ldapsam_search_end
;
4586 return ldapsam_search_firstpage(search
);
4589 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4590 TALLOC_CTX
*mem_ctx
,
4591 LDAP
*ld
, LDAPMessage
*entry
,
4592 struct samr_displayentry
*result
)
4595 size_t converted_size
;
4597 uint16_t group_type
;
4599 result
->account_name
= "";
4600 result
->fullname
= "";
4601 result
->description
= "";
4604 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4605 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4606 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4608 ldap_value_free(vals
);
4613 group_type
= atoi(vals
[0]);
4615 if ((state
->group_type
!= 0) &&
4616 ((state
->group_type
!= group_type
))) {
4617 ldap_value_free(vals
);
4621 ldap_value_free(vals
);
4623 /* display name is the NT group name */
4625 vals
= ldap_get_values(ld
, entry
, "displayName");
4626 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4627 DEBUG(8, ("\"displayName\" not found\n"));
4629 /* fallback to the 'cn' attribute */
4630 vals
= ldap_get_values(ld
, entry
, "cn");
4631 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4632 DEBUG(5, ("\"cn\" not found\n"));
4635 if (!pull_utf8_talloc(mem_ctx
,
4636 discard_const_p(char *,
4637 &result
->account_name
),
4638 vals
[0], &converted_size
))
4640 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4641 "failed: %s", strerror(errno
)));
4644 else if (!pull_utf8_talloc(mem_ctx
,
4645 discard_const_p(char *,
4646 &result
->account_name
),
4647 vals
[0], &converted_size
))
4649 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4653 ldap_value_free(vals
);
4655 vals
= ldap_get_values(ld
, entry
, "description");
4656 if ((vals
== NULL
) || (vals
[0] == NULL
))
4657 DEBUG(8, ("\"description\" not found\n"));
4658 else if (!pull_utf8_talloc(mem_ctx
,
4659 discard_const_p(char *, &result
->description
),
4660 vals
[0], &converted_size
))
4662 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4665 ldap_value_free(vals
);
4667 if ((result
->account_name
== NULL
) ||
4668 (result
->fullname
== NULL
) ||
4669 (result
->description
== NULL
)) {
4670 DEBUG(0, ("talloc failed\n"));
4674 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4675 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4676 DEBUG(0, ("\"objectSid\" not found\n"));
4678 ldap_value_free(vals
);
4683 if (!string_to_sid(&sid
, vals
[0])) {
4684 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4688 ldap_value_free(vals
);
4690 switch (group_type
) {
4691 case SID_NAME_DOM_GRP
:
4692 case SID_NAME_ALIAS
:
4694 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4695 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4697 DEBUG(0, ("%s is not in our domain\n",
4698 sid_string_dbg(&sid
)));
4704 DEBUG(0,("unknown group type: %d\n", group_type
));
4708 result
->acct_flags
= 0;
4713 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4714 struct pdb_search
*search
,
4715 const struct dom_sid
*sid
,
4716 enum lsa_SidType type
)
4718 struct ldapsam_privates
*ldap_state
=
4719 (struct ldapsam_privates
*)methods
->private_data
;
4720 struct ldap_search_state
*state
;
4723 state
= talloc(search
, struct ldap_search_state
);
4724 if (state
== NULL
) {
4725 DEBUG(0, ("talloc failed\n"));
4729 state
->connection
= ldap_state
->smbldap_state
;
4731 state
->base
= lp_ldap_suffix(search
);
4732 state
->connection
= ldap_state
->smbldap_state
;
4733 state
->scope
= LDAP_SCOPE_SUBTREE
;
4734 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4735 "(sambaGroupType=%d)(sambaSID=%s*))",
4737 type
, sid_to_fstring(tmp
, sid
));
4738 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4739 "displayName", "description",
4740 "sambaGroupType", NULL
);
4741 state
->attrsonly
= 0;
4742 state
->pagedresults_cookie
= NULL
;
4743 state
->entries
= NULL
;
4744 state
->group_type
= type
;
4745 state
->ldap2displayentry
= ldapgroup2displayentry
;
4747 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4748 DEBUG(0, ("talloc failed\n"));
4752 search
->private_data
= state
;
4753 search
->next_entry
= ldapsam_search_next_entry
;
4754 search
->search_end
= ldapsam_search_end
;
4756 return ldapsam_search_firstpage(search
);
4759 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4760 struct pdb_search
*search
)
4762 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4765 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4766 struct pdb_search
*search
,
4767 const struct dom_sid
*sid
)
4769 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4772 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4774 return PDB_CAP_STORE_RIDS
;
4777 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4780 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4782 LDAPMessage
*result
= NULL
;
4783 LDAPMessage
*entry
= NULL
;
4784 LDAPMod
**mods
= NULL
;
4788 uint32_t nextRid
= 0;
4791 TALLOC_CTX
*mem_ctx
;
4793 mem_ctx
= talloc_new(NULL
);
4794 if (mem_ctx
== NULL
) {
4795 DEBUG(0, ("talloc_new failed\n"));
4796 return NT_STATUS_NO_MEMORY
;
4799 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4800 get_global_sam_name(), False
);
4801 if (!NT_STATUS_IS_OK(status
)) {
4802 DEBUG(3, ("Could not get domain info: %s\n",
4803 nt_errstr(status
)));
4807 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4809 entry
= ldap_first_entry(priv2ld(priv
), result
);
4810 if (entry
== NULL
) {
4811 DEBUG(0, ("Could not get domain info entry\n"));
4812 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4816 /* Find the largest of the three attributes "sambaNextRid",
4817 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4818 concept of differentiating between user and group rids, and will
4819 use only "sambaNextRid" in the future. But for compatibility
4820 reasons I look if others have chosen different strategies -- VL */
4822 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4823 "sambaNextRid", mem_ctx
);
4824 if (value
!= NULL
) {
4825 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4826 nextRid
= MAX(nextRid
, tmp
);
4829 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4830 "sambaNextUserRid", mem_ctx
);
4831 if (value
!= NULL
) {
4832 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4833 nextRid
= MAX(nextRid
, tmp
);
4836 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4837 "sambaNextGroupRid", mem_ctx
);
4838 if (value
!= NULL
) {
4839 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4840 nextRid
= MAX(nextRid
, tmp
);
4844 nextRid
= BASE_RID
-1;
4849 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4850 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4851 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
4853 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4854 status
= NT_STATUS_NO_MEMORY
;
4858 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4860 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4863 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4866 if (NT_STATUS_IS_OK(status
)) {
4870 TALLOC_FREE(mem_ctx
);
4874 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4878 for (i
=0; i
<10; i
++) {
4879 NTSTATUS result
= ldapsam_get_new_rid(
4880 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4881 if (NT_STATUS_IS_OK(result
)) {
4885 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4889 /* The ldap update failed (maybe a race condition), retry */
4892 /* Tried 10 times, fail. */
4893 return NT_STATUS_ACCESS_DENIED
;
4896 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
4898 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4899 return NT_STATUS_IS_OK(result
) ? True
: False
;
4902 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4903 const struct dom_sid
*sid
,
4906 struct ldapsam_privates
*priv
=
4907 (struct ldapsam_privates
*)methods
->private_data
;
4909 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4911 LDAPMessage
*result
= NULL
;
4912 LDAPMessage
*entry
= NULL
;
4917 TALLOC_CTX
*mem_ctx
;
4919 ret
= pdb_sid_to_id_unix_users_and_groups(sid
, id
);
4924 mem_ctx
= talloc_new(NULL
);
4925 if (mem_ctx
== NULL
) {
4926 DEBUG(0, ("talloc_new failed\n"));
4930 filter
= talloc_asprintf(mem_ctx
,
4932 "(|(objectClass=%s)(objectClass=%s)))",
4933 sid_string_talloc(mem_ctx
, sid
),
4934 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4935 if (filter
== NULL
) {
4936 DEBUG(5, ("talloc_asprintf failed\n"));
4940 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4942 if (rc
!= LDAP_SUCCESS
) {
4945 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4947 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4948 DEBUG(10, ("Got %d entries, expected one\n",
4949 ldap_count_entries(priv2ld(priv
), result
)));
4953 entry
= ldap_first_entry(priv2ld(priv
), result
);
4955 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4956 "sambaGroupType", mem_ctx
);
4958 if (value
!= NULL
) {
4959 const char *gid_str
;
4962 gid_str
= smbldap_talloc_single_attribute(
4963 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4964 if (gid_str
== NULL
) {
4965 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4966 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4971 id
->id
= strtoul(gid_str
, NULL
, 10);
4972 id
->type
= ID_TYPE_GID
;
4977 /* It must be a user */
4979 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4980 "uidNumber", mem_ctx
);
4981 if (value
== NULL
) {
4982 DEBUG(1, ("Could not find uidNumber in %s\n",
4983 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4987 id
->id
= strtoul(value
, NULL
, 10);
4988 id
->type
= ID_TYPE_UID
;
4992 TALLOC_FREE(mem_ctx
);
4997 * Find the SID for a uid.
4998 * This is shortcut is only used if ldapsam:trusted is set to true.
5000 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5001 struct dom_sid
*sid
)
5003 struct ldapsam_privates
*priv
=
5004 (struct ldapsam_privates
*)methods
->private_data
;
5006 const char *attrs
[] = { "sambaSID", NULL
};
5007 LDAPMessage
*result
= NULL
;
5008 LDAPMessage
*entry
= NULL
;
5010 char *user_sid_string
;
5011 struct dom_sid user_sid
;
5013 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5015 filter
= talloc_asprintf(tmp_ctx
,
5018 "(objectClass=%s))",
5020 LDAP_OBJ_POSIXACCOUNT
,
5021 LDAP_OBJ_SAMBASAMACCOUNT
);
5022 if (filter
== NULL
) {
5023 DEBUG(3, ("talloc_asprintf failed\n"));
5027 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5028 if (rc
!= LDAP_SUCCESS
) {
5031 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5033 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5034 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5035 ldap_count_entries(priv2ld(priv
), result
),
5036 (unsigned int)uid
));
5040 entry
= ldap_first_entry(priv2ld(priv
), result
);
5042 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5043 "sambaSID", tmp_ctx
);
5044 if (user_sid_string
== NULL
) {
5045 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5046 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5050 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5051 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5056 sid_copy(sid
, &user_sid
);
5061 TALLOC_FREE(tmp_ctx
);
5066 * Find the SID for a gid.
5067 * This is shortcut is only used if ldapsam:trusted is set to true.
5069 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5070 struct dom_sid
*sid
)
5072 struct ldapsam_privates
*priv
=
5073 (struct ldapsam_privates
*)methods
->private_data
;
5075 const char *attrs
[] = { "sambaSID", NULL
};
5076 LDAPMessage
*result
= NULL
;
5077 LDAPMessage
*entry
= NULL
;
5079 char *group_sid_string
;
5080 struct dom_sid group_sid
;
5082 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5084 filter
= talloc_asprintf(tmp_ctx
,
5086 "(objectClass=%s))",
5089 if (filter
== NULL
) {
5090 DEBUG(3, ("talloc_asprintf failed\n"));
5094 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5095 if (rc
!= LDAP_SUCCESS
) {
5098 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5100 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5101 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5102 ldap_count_entries(priv2ld(priv
), result
),
5103 (unsigned int)gid
));
5107 entry
= ldap_first_entry(priv2ld(priv
), result
);
5109 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5110 "sambaSID", tmp_ctx
);
5111 if (group_sid_string
== NULL
) {
5112 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5113 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5117 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5118 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5123 sid_copy(sid
, &group_sid
);
5128 TALLOC_FREE(tmp_ctx
);
5134 * The following functions are called only if
5135 * ldapsam:trusted and ldapsam:editposix are
5140 * ldapsam_create_user creates a new
5141 * posixAccount and sambaSamAccount object
5142 * in the ldap users subtree
5144 * The uid is allocated by winbindd.
5147 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5148 TALLOC_CTX
*tmp_ctx
, const char *name
,
5149 uint32_t acb_info
, uint32_t *rid
)
5151 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5152 LDAPMessage
*entry
= NULL
;
5153 LDAPMessage
*result
= NULL
;
5154 uint32_t num_result
;
5155 bool is_machine
= False
;
5156 bool add_posix
= False
;
5157 bool init_okay
= False
;
5158 LDAPMod
**mods
= NULL
;
5166 const char *dn
= NULL
;
5167 struct dom_sid group_sid
;
5168 struct dom_sid user_sid
;
5174 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5175 acb_info
& ACB_WSTRUST
||
5176 acb_info
& ACB_SVRTRUST
||
5177 acb_info
& ACB_DOMTRUST
) {
5181 username
= escape_ldap_string(talloc_tos(), name
);
5182 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5183 username
, LDAP_OBJ_POSIXACCOUNT
);
5184 TALLOC_FREE(username
);
5186 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5187 if (rc
!= LDAP_SUCCESS
) {
5188 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5189 return NT_STATUS_ACCESS_DENIED
;
5191 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5193 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5195 if (num_result
> 1) {
5196 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5197 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5200 if (num_result
== 1) {
5202 /* check if it is just a posix account.
5203 * or if there is a sid attached to this entry
5206 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5208 return NT_STATUS_UNSUCCESSFUL
;
5211 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5213 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5214 return NT_STATUS_USER_EXISTS
;
5217 /* it is just a posix account, retrieve the dn for later use */
5218 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5220 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5221 return NT_STATUS_NO_MEMORY
;
5225 if (num_result
== 0) {
5229 /* Create the basic samu structure and generate the mods for the ldap commit */
5230 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5231 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5235 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5237 user
= samu_new(tmp_ctx
);
5239 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5240 return NT_STATUS_NO_MEMORY
;
5243 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5244 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5245 return NT_STATUS_UNSUCCESSFUL
;
5247 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5248 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5249 return NT_STATUS_UNSUCCESSFUL
;
5252 if (acb_info
& ACB_NORMAL
) {
5253 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5254 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5255 return NT_STATUS_UNSUCCESSFUL
;
5258 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5259 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5260 return NT_STATUS_UNSUCCESSFUL
;
5264 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5265 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5266 return NT_STATUS_UNSUCCESSFUL
;
5270 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5271 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5272 return NT_STATUS_UNSUCCESSFUL
;
5275 init_okay
= init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
);
5276 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5279 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5280 return NT_STATUS_UNSUCCESSFUL
;
5283 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5284 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5286 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5291 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5293 /* retrieve the Domain Users group gid */
5294 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5295 !sid_to_gid(&group_sid
, &gid
)) {
5296 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5297 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5300 /* lets allocate a new userid for this user */
5301 if (!winbind_allocate_uid(&uid
)) {
5302 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5303 return NT_STATUS_UNSUCCESSFUL
;
5308 /* TODO: choose a more appropriate default for machines */
5309 homedir
= talloc_sub_specified(tmp_ctx
,
5310 lp_template_homedir(),
5311 "SMB_workstations_home",
5313 ldap_state
->domain_name
,
5316 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5318 homedir
= talloc_sub_specified(tmp_ctx
,
5319 lp_template_homedir(),
5322 ldap_state
->domain_name
,
5325 shell
= talloc_sub_specified(tmp_ctx
,
5326 lp_template_shell(),
5329 ldap_state
->domain_name
,
5333 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5334 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5336 escape_name
= escape_rdn_val_string_alloc(name
);
5338 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5339 return NT_STATUS_NO_MEMORY
;
5343 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix (talloc_tos()));
5345 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix (talloc_tos()));
5348 SAFE_FREE(escape_name
);
5350 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5351 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5352 return NT_STATUS_NO_MEMORY
;
5355 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5356 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5357 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5358 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5359 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5360 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5361 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5365 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5367 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5370 if (rc
!= LDAP_SUCCESS
) {
5371 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5372 return NT_STATUS_UNSUCCESSFUL
;
5375 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5377 flush_pwnam_cache();
5379 return NT_STATUS_OK
;
5382 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5384 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5385 LDAPMessage
*result
= NULL
;
5386 LDAPMessage
*entry
= NULL
;
5392 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5394 filter
= talloc_asprintf(tmp_ctx
,
5397 "(objectClass=%s))",
5398 pdb_get_username(sam_acct
),
5399 LDAP_OBJ_POSIXACCOUNT
,
5400 LDAP_OBJ_SAMBASAMACCOUNT
);
5401 if (filter
== NULL
) {
5402 return NT_STATUS_NO_MEMORY
;
5405 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5406 if (rc
!= LDAP_SUCCESS
) {
5407 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5408 return NT_STATUS_UNSUCCESSFUL
;
5410 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5412 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5414 if (num_result
== 0) {
5415 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5416 return NT_STATUS_NO_SUCH_USER
;
5419 if (num_result
> 1) {
5420 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5421 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5424 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5426 return NT_STATUS_UNSUCCESSFUL
;
5429 /* it is just a posix account, retrieve the dn for later use */
5430 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5432 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5433 return NT_STATUS_NO_MEMORY
;
5436 /* try to remove memberships first */
5439 struct dom_sid
*sids
= NULL
;
5441 uint32_t num_groups
= 0;
5443 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5445 status
= ldapsam_enum_group_memberships(my_methods
,
5451 if (!NT_STATUS_IS_OK(status
)) {
5455 for (i
=0; i
< num_groups
; i
++) {
5459 sid_peek_rid(&sids
[i
], &group_rid
);
5461 ldapsam_del_groupmem(my_methods
,
5470 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5471 if (rc
!= LDAP_SUCCESS
) {
5472 return NT_STATUS_UNSUCCESSFUL
;
5475 flush_pwnam_cache();
5477 return NT_STATUS_OK
;
5481 * ldapsam_create_group creates a new
5482 * posixGroup and sambaGroupMapping object
5483 * in the ldap groups subtree
5485 * The gid is allocated by winbindd.
5488 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5489 TALLOC_CTX
*tmp_ctx
,
5493 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5495 LDAPMessage
*entry
= NULL
;
5496 LDAPMessage
*result
= NULL
;
5497 uint32_t num_result
;
5498 bool is_new_entry
= False
;
5499 LDAPMod
**mods
= NULL
;
5505 const char *dn
= NULL
;
5506 struct dom_sid group_sid
;
5510 groupname
= escape_ldap_string(talloc_tos(), name
);
5511 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5512 groupname
, LDAP_OBJ_POSIXGROUP
);
5513 TALLOC_FREE(groupname
);
5515 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5516 if (rc
!= LDAP_SUCCESS
) {
5517 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5518 return NT_STATUS_UNSUCCESSFUL
;
5520 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5522 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5524 if (num_result
> 1) {
5525 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5526 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5529 if (num_result
== 1) {
5531 /* check if it is just a posix group.
5532 * or if there is a sid attached to this entry
5535 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5537 return NT_STATUS_UNSUCCESSFUL
;
5540 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5542 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5543 return NT_STATUS_GROUP_EXISTS
;
5546 /* it is just a posix group, retrieve the gid and the dn for later use */
5547 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5549 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5550 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5553 gid
= strtoul(tmp
, NULL
, 10);
5555 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5557 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5558 return NT_STATUS_NO_MEMORY
;
5562 if (num_result
== 0) {
5563 is_new_entry
= true;
5566 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5567 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5571 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5573 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5575 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5577 if (!groupsidstr
|| !grouptype
) {
5578 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5579 return NT_STATUS_NO_MEMORY
;
5582 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5583 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5584 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5585 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5590 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5592 /* lets allocate a new groupid for this group */
5593 if (!winbind_allocate_gid(&gid
)) {
5594 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5595 return NT_STATUS_UNSUCCESSFUL
;
5598 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5600 escape_name
= escape_rdn_val_string_alloc(name
);
5602 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5603 return NT_STATUS_NO_MEMORY
;
5606 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix(talloc_tos()));
5608 SAFE_FREE(escape_name
);
5610 if (!gidstr
|| !dn
) {
5611 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5612 return NT_STATUS_NO_MEMORY
;
5615 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5616 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5617 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5620 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5623 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5625 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5626 /* This call may fail with rfc2307bis schema */
5627 /* Retry adding a structural class */
5628 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5629 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5633 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5636 if (rc
!= LDAP_SUCCESS
) {
5637 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5638 return NT_STATUS_UNSUCCESSFUL
;
5641 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5643 return NT_STATUS_OK
;
5646 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5648 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5649 LDAPMessage
*result
= NULL
;
5650 LDAPMessage
*entry
= NULL
;
5655 struct dom_sid group_sid
;
5658 /* get the group sid */
5659 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5661 filter
= talloc_asprintf(tmp_ctx
,
5664 "(objectClass=%s))",
5665 sid_string_talloc(tmp_ctx
, &group_sid
),
5666 LDAP_OBJ_POSIXGROUP
,
5668 if (filter
== NULL
) {
5669 return NT_STATUS_NO_MEMORY
;
5672 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5673 if (rc
!= LDAP_SUCCESS
) {
5674 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5675 return NT_STATUS_UNSUCCESSFUL
;
5677 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5679 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5681 if (num_result
== 0) {
5682 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5683 return NT_STATUS_NO_SUCH_GROUP
;
5686 if (num_result
> 1) {
5687 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5688 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5691 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5693 return NT_STATUS_UNSUCCESSFUL
;
5696 /* here it is, retrieve the dn for later use */
5697 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5699 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5700 return NT_STATUS_NO_MEMORY
;
5703 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5705 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5706 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5709 /* check no user have this group marked as primary group */
5710 filter
= talloc_asprintf(tmp_ctx
,
5713 "(objectClass=%s))",
5715 LDAP_OBJ_POSIXACCOUNT
,
5716 LDAP_OBJ_SAMBASAMACCOUNT
);
5718 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5719 if (rc
!= LDAP_SUCCESS
) {
5720 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5721 return NT_STATUS_UNSUCCESSFUL
;
5723 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5725 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5727 if (num_result
!= 0) {
5728 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5729 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5732 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5733 if (rc
!= LDAP_SUCCESS
) {
5734 return NT_STATUS_UNSUCCESSFUL
;
5737 return NT_STATUS_OK
;
5740 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5741 TALLOC_CTX
*tmp_ctx
,
5743 uint32_t member_rid
,
5746 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5747 LDAPMessage
*entry
= NULL
;
5748 LDAPMessage
*result
= NULL
;
5749 uint32_t num_result
;
5750 LDAPMod
**mods
= NULL
;
5753 const char *dn
= NULL
;
5754 struct dom_sid group_sid
;
5755 struct dom_sid member_sid
;
5760 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5762 case LDAP_MOD_DELETE
:
5763 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5766 return NT_STATUS_UNSUCCESSFUL
;
5769 /* get member sid */
5770 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5772 /* get the group sid */
5773 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5775 filter
= talloc_asprintf(tmp_ctx
,
5778 "(objectClass=%s))",
5779 sid_string_talloc(tmp_ctx
, &member_sid
),
5780 LDAP_OBJ_POSIXACCOUNT
,
5781 LDAP_OBJ_SAMBASAMACCOUNT
);
5782 if (filter
== NULL
) {
5783 return NT_STATUS_NO_MEMORY
;
5786 /* get the member uid */
5787 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5788 if (rc
!= LDAP_SUCCESS
) {
5789 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5790 return NT_STATUS_UNSUCCESSFUL
;
5792 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5794 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5796 if (num_result
== 0) {
5797 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5798 return NT_STATUS_NO_SUCH_MEMBER
;
5801 if (num_result
> 1) {
5802 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5803 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5806 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5808 return NT_STATUS_UNSUCCESSFUL
;
5811 if (modop
== LDAP_MOD_DELETE
) {
5812 /* check if we are trying to remove the member from his primary group */
5814 gid_t user_gid
, group_gid
;
5816 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5818 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5819 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5822 user_gid
= strtoul(gidstr
, NULL
, 10);
5824 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5825 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5826 return NT_STATUS_UNSUCCESSFUL
;
5829 if (user_gid
== group_gid
) {
5830 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5831 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5835 /* here it is, retrieve the uid for later use */
5836 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5838 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5839 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5842 filter
= talloc_asprintf(tmp_ctx
,
5845 "(objectClass=%s))",
5846 sid_string_talloc(tmp_ctx
, &group_sid
),
5847 LDAP_OBJ_POSIXGROUP
,
5851 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5852 if (rc
!= LDAP_SUCCESS
) {
5853 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5854 return NT_STATUS_UNSUCCESSFUL
;
5856 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5858 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5860 if (num_result
== 0) {
5861 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5862 return NT_STATUS_NO_SUCH_GROUP
;
5865 if (num_result
> 1) {
5866 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5867 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5870 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5872 return NT_STATUS_UNSUCCESSFUL
;
5875 /* here it is, retrieve the dn for later use */
5876 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5878 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5879 return NT_STATUS_NO_MEMORY
;
5882 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5884 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5886 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5887 if (rc
!= LDAP_SUCCESS
) {
5888 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5889 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5890 return NT_STATUS_MEMBER_IN_GROUP
;
5892 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5893 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5894 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5896 return NT_STATUS_UNSUCCESSFUL
;
5899 return NT_STATUS_OK
;
5902 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5903 TALLOC_CTX
*tmp_ctx
,
5905 uint32_t member_rid
)
5907 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5909 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5910 TALLOC_CTX
*tmp_ctx
,
5912 uint32_t member_rid
)
5914 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5917 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5918 TALLOC_CTX
*mem_ctx
,
5919 struct samu
*sampass
)
5921 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5922 LDAPMessage
*entry
= NULL
;
5923 LDAPMessage
*result
= NULL
;
5924 uint32_t num_result
;
5925 LDAPMod
**mods
= NULL
;
5927 char *escape_username
;
5929 const char *dn
= NULL
;
5933 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5935 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5936 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5937 return NT_STATUS_UNSUCCESSFUL
;
5939 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
5941 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5942 return NT_STATUS_NO_MEMORY
;
5945 escape_username
= escape_ldap_string(talloc_tos(),
5946 pdb_get_username(sampass
));
5947 if (escape_username
== NULL
) {
5948 return NT_STATUS_NO_MEMORY
;
5951 filter
= talloc_asprintf(mem_ctx
,
5954 "(objectClass=%s))",
5956 LDAP_OBJ_POSIXACCOUNT
,
5957 LDAP_OBJ_SAMBASAMACCOUNT
);
5959 TALLOC_FREE(escape_username
);
5961 if (filter
== NULL
) {
5962 return NT_STATUS_NO_MEMORY
;
5965 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5966 if (rc
!= LDAP_SUCCESS
) {
5967 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5968 return NT_STATUS_UNSUCCESSFUL
;
5970 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
5972 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5974 if (num_result
== 0) {
5975 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5976 return NT_STATUS_NO_SUCH_USER
;
5979 if (num_result
> 1) {
5980 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5981 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5984 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5986 return NT_STATUS_UNSUCCESSFUL
;
5989 /* retrieve the dn for later use */
5990 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5992 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5993 return NT_STATUS_NO_MEMORY
;
5996 /* remove the old one, and add the new one, this way we do not risk races */
5997 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6000 return NT_STATUS_OK
;
6003 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6005 if (rc
!= LDAP_SUCCESS
) {
6006 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6007 pdb_get_username(sampass
), gidstr
));
6008 return NT_STATUS_UNSUCCESSFUL
;
6011 flush_pwnam_cache();
6013 return NT_STATUS_OK
;
6017 /**********************************************************************
6018 trusted domains functions
6019 *********************************************************************/
6021 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6024 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6025 ldap_state
->domain_dn
);
6028 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6029 TALLOC_CTX
*mem_ctx
,
6030 const char *domain
, LDAPMessage
**entry
)
6034 int scope
= LDAP_SCOPE_SUBTREE
;
6035 const char **attrs
= NULL
; /* NULL: get all attrs */
6036 int attrsonly
= 0; /* 0: return values too */
6037 LDAPMessage
*result
= NULL
;
6039 uint32_t num_result
;
6041 filter
= talloc_asprintf(talloc_tos(),
6042 "(&(objectClass=%s)(sambaDomainName=%s))",
6043 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6045 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6046 if (trusted_dn
== NULL
) {
6049 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6050 filter
, attrs
, attrsonly
, &result
);
6052 if (result
!= NULL
) {
6053 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6056 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6061 if (rc
!= LDAP_SUCCESS
) {
6065 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6067 if (num_result
> 1) {
6068 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6069 "%s object for domain '%s'?!\n",
6070 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6074 if (num_result
== 0) {
6075 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6076 "%s object for domain %s.\n",
6077 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6080 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6086 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6089 struct dom_sid
*sid
,
6090 time_t *pass_last_set_time
)
6092 struct ldapsam_privates
*ldap_state
=
6093 (struct ldapsam_privates
*)methods
->private_data
;
6094 LDAPMessage
*entry
= NULL
;
6096 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6098 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6107 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6108 entry
, "sambaClearTextPassword", talloc_tos());
6109 if (pwd_str
== NULL
) {
6112 /* trusteddom_pw routines do not use talloc yet... */
6113 *pwd
= SMB_STRDUP(pwd_str
);
6119 /* last change time */
6120 if (pass_last_set_time
!= NULL
) {
6122 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6123 entry
, "sambaPwdLastSet", talloc_tos());
6124 if (time_str
== NULL
) {
6127 *pass_last_set_time
= (time_t)atol(time_str
);
6133 struct dom_sid dom_sid
;
6134 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6137 if (sid_str
== NULL
) {
6140 if (!string_to_sid(&dom_sid
, sid_str
)) {
6143 sid_copy(sid
, &dom_sid
);
6149 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6152 const struct dom_sid
*sid
)
6154 struct ldapsam_privates
*ldap_state
=
6155 (struct ldapsam_privates
*)methods
->private_data
;
6156 LDAPMessage
*entry
= NULL
;
6157 LDAPMod
**mods
= NULL
;
6158 char *prev_pwd
= NULL
;
6159 char *trusted_dn
= NULL
;
6162 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6165 * get the current entry (if there is one) in order to put the
6166 * current password into the previous password attribute
6168 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6173 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6174 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6175 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6177 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6178 sid_string_tos(sid
));
6179 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6180 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6181 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6182 "sambaClearTextPassword", pwd
);
6184 if (entry
!= NULL
) {
6185 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6186 entry
, "sambaClearTextPassword", talloc_tos());
6187 if (prev_pwd
!= NULL
) {
6188 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6189 "sambaPreviousClearTextPassword",
6194 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods
);
6196 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6197 if (trusted_dn
== NULL
) {
6200 if (entry
== NULL
) {
6201 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6203 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6206 if (rc
!= LDAP_SUCCESS
) {
6207 DEBUG(1, ("error writing trusted domain password!\n"));
6214 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6218 struct ldapsam_privates
*ldap_state
=
6219 (struct ldapsam_privates
*)methods
->private_data
;
6220 LDAPMessage
*entry
= NULL
;
6221 const char *trusted_dn
;
6223 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6227 if (entry
== NULL
) {
6228 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6233 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6235 if (trusted_dn
== NULL
) {
6236 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6240 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6241 if (rc
!= LDAP_SUCCESS
) {
6248 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6249 TALLOC_CTX
*mem_ctx
,
6250 uint32_t *num_domains
,
6251 struct trustdom_info
***domains
)
6254 struct ldapsam_privates
*ldap_state
=
6255 (struct ldapsam_privates
*)methods
->private_data
;
6257 int scope
= LDAP_SCOPE_SUBTREE
;
6258 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6259 int attrsonly
= 0; /* 0: return values too */
6260 LDAPMessage
*result
= NULL
;
6261 LDAPMessage
*entry
= NULL
;
6263 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6264 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6266 rc
= smbldap_search(ldap_state
->smbldap_state
,
6267 ldap_state
->domain_dn
,
6274 if (result
!= NULL
) {
6275 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6278 if (rc
!= LDAP_SUCCESS
) {
6279 return NT_STATUS_UNSUCCESSFUL
;
6283 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6284 DEBUG(1, ("talloc failed\n"));
6285 return NT_STATUS_NO_MEMORY
;
6288 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6290 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6292 char *dom_name
, *dom_sid_str
;
6293 struct trustdom_info
*dom_info
;
6295 dom_info
= talloc(*domains
, struct trustdom_info
);
6296 if (dom_info
== NULL
) {
6297 DEBUG(1, ("talloc failed\n"));
6298 return NT_STATUS_NO_MEMORY
;
6301 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6305 if (dom_name
== NULL
) {
6306 DEBUG(1, ("talloc failed\n"));
6307 return NT_STATUS_NO_MEMORY
;
6309 dom_info
->name
= dom_name
;
6311 dom_sid_str
= smbldap_talloc_single_attribute(
6312 priv2ld(ldap_state
), entry
, "sambaSID",
6314 if (dom_sid_str
== NULL
) {
6315 DEBUG(1, ("talloc failed\n"));
6316 return NT_STATUS_NO_MEMORY
;
6318 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6319 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6321 return NT_STATUS_UNSUCCESSFUL
;
6324 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6325 domains
, num_domains
);
6327 if (*domains
== NULL
) {
6328 DEBUG(1, ("talloc failed\n"));
6329 return NT_STATUS_NO_MEMORY
;
6333 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6334 return NT_STATUS_OK
;
6338 /**********************************************************************
6340 *********************************************************************/
6342 static void free_private_data(void **vp
)
6344 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6346 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6348 if ((*ldap_state
)->result
!= NULL
) {
6349 ldap_msgfree((*ldap_state
)->result
);
6350 (*ldap_state
)->result
= NULL
;
6352 if ((*ldap_state
)->domain_dn
!= NULL
) {
6353 SAFE_FREE((*ldap_state
)->domain_dn
);
6358 /* No need to free any further, as it is talloc()ed */
6361 /*********************************************************************
6362 Intitalise the parts of the pdb_methods structure that are common to
6364 *********************************************************************/
6366 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6369 struct ldapsam_privates
*ldap_state
;
6370 char *bind_dn
= NULL
;
6371 char *bind_secret
= NULL
;
6373 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6377 (*pdb_method
)->name
= "ldapsam";
6379 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6380 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6381 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6382 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6383 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6384 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6386 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6387 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6388 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6389 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6390 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6391 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6392 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6394 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6395 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6397 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6399 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6400 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6402 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6403 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6404 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6405 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6407 /* TODO: Setup private data and free */
6409 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6410 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6411 return NT_STATUS_NO_MEMORY
;
6414 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6415 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6416 return NT_STATUS_NO_MEMORY
;
6419 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6420 location
, false, bind_dn
, bind_secret
,
6421 &ldap_state
->smbldap_state
);
6422 memset(bind_secret
, '\0', strlen(bind_secret
));
6423 SAFE_FREE(bind_secret
);
6425 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6429 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6430 return NT_STATUS_NO_MEMORY
;
6433 (*pdb_method
)->private_data
= ldap_state
;
6435 (*pdb_method
)->free_private_data
= free_private_data
;
6437 return NT_STATUS_OK
;
6440 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods
*m
)
6445 /**********************************************************************
6446 Initialise the normal mode for pdb_ldap
6447 *********************************************************************/
6449 NTSTATUS
pdb_ldapsam_init_common(struct pdb_methods
**pdb_method
,
6450 const char *location
)
6453 struct ldapsam_privates
*ldap_state
= NULL
;
6454 uint32_t alg_rid_base
;
6455 char *alg_rid_base_string
= NULL
;
6456 LDAPMessage
*result
= NULL
;
6457 LDAPMessage
*entry
= NULL
;
6458 struct dom_sid ldap_domain_sid
;
6459 struct dom_sid secrets_domain_sid
;
6460 char *domain_sid_string
= NULL
;
6462 char *uri
= talloc_strdup( NULL
, location
);
6464 trim_char( uri
, '\"', '\"' );
6465 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6469 if (!NT_STATUS_IS_OK(nt_status
)) {
6473 (*pdb_method
)->name
= "ldapsam";
6475 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6476 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6477 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6478 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6479 (*pdb_method
)->search_users
= ldapsam_search_users
;
6480 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6481 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6482 (*pdb_method
)->is_responsible_for_wellknown
=
6483 ldapsam_is_responsible_for_wellknown
;
6485 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6486 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6487 (*pdb_method
)->enum_group_memberships
=
6488 ldapsam_enum_group_memberships
;
6489 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6490 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6491 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6492 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6494 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6495 (*pdb_method
)->create_user
= ldapsam_create_user
;
6496 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6497 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6498 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6499 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6500 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6501 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6505 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6506 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6508 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6510 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6512 ldap_state
->domain_name
, True
);
6514 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6515 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6516 "info, nor add one to the domain. "
6517 "We cannot work reliably without it.\n"));
6518 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6521 /* Given that the above might fail, everything below this must be
6524 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6527 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6529 ldap_msgfree(result
);
6530 return NT_STATUS_UNSUCCESSFUL
;
6533 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6535 ldap_msgfree(result
);
6536 return NT_STATUS_UNSUCCESSFUL
;
6539 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6542 domain_sid_string
= smbldap_talloc_single_attribute(
6543 ldap_state
->smbldap_state
->ldap_struct
,
6545 get_userattr_key2string(ldap_state
->schema_ver
,
6546 LDAP_ATTR_USER_SID
),
6549 if (domain_sid_string
) {
6551 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6552 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6553 "read as a valid SID\n", domain_sid_string
));
6554 ldap_msgfree(result
);
6555 TALLOC_FREE(domain_sid_string
);
6556 return NT_STATUS_INVALID_PARAMETER
;
6558 found_sid
= PDB_secrets_fetch_domain_sid(ldap_state
->domain_name
,
6559 &secrets_domain_sid
);
6560 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6561 &ldap_domain_sid
)) {
6562 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6563 "%s based on pdb_ldap results %s -> %s\n",
6564 ldap_state
->domain_name
,
6565 sid_string_dbg(&secrets_domain_sid
),
6566 sid_string_dbg(&ldap_domain_sid
)));
6568 /* reset secrets.tdb sid */
6569 PDB_secrets_store_domain_sid(ldap_state
->domain_name
,
6571 DEBUG(1, ("New global sam SID: %s\n",
6572 sid_string_dbg(get_global_sam_sid())));
6574 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6575 TALLOC_FREE(domain_sid_string
);
6578 alg_rid_base_string
= smbldap_talloc_single_attribute(
6579 ldap_state
->smbldap_state
->ldap_struct
,
6581 get_attr_key2string( dominfo_attr_list
,
6582 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6584 if (alg_rid_base_string
) {
6585 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6586 if (alg_rid_base
!= algorithmic_rid_base()) {
6587 DEBUG(0, ("The value of 'algorithmic RID base' has "
6588 "changed since the LDAP\n"
6589 "database was initialised. Aborting. \n"));
6590 ldap_msgfree(result
);
6591 TALLOC_FREE(alg_rid_base_string
);
6592 return NT_STATUS_UNSUCCESSFUL
;
6594 TALLOC_FREE(alg_rid_base_string
);
6596 ldap_msgfree(result
);
6598 return NT_STATUS_OK
;
6601 NTSTATUS
pdb_ldapsam_init(void)
6605 nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
,
6607 pdb_ldapsam_init_common
);
6608 if (!NT_STATUS_IS_OK(nt_status
)) {
6612 /* Let pdb_nds register backends */
6617 return NT_STATUS_OK
;