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"
56 #define DBGC_CLASS DBGC_PASSDB
63 #include "passdb/pdb_ldap.h"
64 #include "passdb/pdb_nds.h"
65 #include "passdb/pdb_ipa.h"
66 #include "passdb/pdb_ldap_util.h"
67 #include "passdb/pdb_ldap_schema.h"
69 /**********************************************************************
70 Simple helper function to make stuff better readable
71 **********************************************************************/
73 LDAP
*priv2ld(struct ldapsam_privates
*priv
)
75 return priv
->smbldap_state
->ldap_struct
;
78 /**********************************************************************
79 Get the attribute name given a user schame version.
80 **********************************************************************/
82 static const char* get_userattr_key2string( int schema_ver
, int key
)
84 switch ( schema_ver
) {
85 case SCHEMAVER_SAMBAACCOUNT
:
86 return get_attr_key2string( attrib_map_v22
, key
);
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_SAMBAACCOUNT
:
106 return get_attr_list( mem_ctx
, attrib_map_v22
);
108 case SCHEMAVER_SAMBASAMACCOUNT
:
109 return get_attr_list( mem_ctx
, attrib_map_v30
);
111 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
118 /**************************************************************************
119 Return the list of attribute names to delete given a user schema version.
120 **************************************************************************/
122 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
125 switch ( schema_ver
) {
126 case SCHEMAVER_SAMBAACCOUNT
:
127 return get_attr_list( mem_ctx
,
128 attrib_map_to_delete_v22
);
130 case SCHEMAVER_SAMBASAMACCOUNT
:
131 return get_attr_list( mem_ctx
,
132 attrib_map_to_delete_v30
);
134 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
142 /*******************************************************************
143 Generate the LDAP search filter for the objectclass based on the
144 version of the schema we are using.
145 ******************************************************************/
147 static const char* get_objclass_filter( int schema_ver
)
149 fstring objclass_filter
;
152 switch( schema_ver
) {
153 case SCHEMAVER_SAMBAACCOUNT
:
154 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
156 case SCHEMAVER_SAMBASAMACCOUNT
:
157 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
160 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
161 objclass_filter
[0] = '\0';
165 result
= talloc_strdup(talloc_tos(), objclass_filter
);
166 SMB_ASSERT(result
!= NULL
);
170 /*****************************************************************
171 Scan a sequence number off OpenLDAP's syncrepl contextCSN
172 ******************************************************************/
174 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
176 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
177 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
178 LDAPMessage
*msg
= NULL
;
179 LDAPMessage
*entry
= NULL
;
181 char **values
= NULL
;
182 int rc
, num_result
, num_values
, rid
;
188 /* Unfortunatly there is no proper way to detect syncrepl-support in
189 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
190 * but do not show up in the root-DSE yet. Neither we can query the
191 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
192 * objectclass. Currently we require lp_ldap_suffix() to show up as
193 * namingContext. - Guenther
196 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
201 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
205 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
206 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
207 "as top-level namingContext\n", lp_ldap_suffix()));
211 mem_ctx
= talloc_init("ldapsam_get_seq_num");
214 return NT_STATUS_NO_MEMORY
;
216 if ((attrs
= talloc_array(mem_ctx
, const char *, 2)) == NULL
) {
217 ntstatus
= NT_STATUS_NO_MEMORY
;
221 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
222 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
225 /* consumer syncreplCookie: */
226 /* csn=20050126161620Z#0000001#00#00000 */
227 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
229 suffix
= talloc_asprintf(mem_ctx
,
230 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
232 ntstatus
= NT_STATUS_NO_MEMORY
;
237 /* provider contextCSN */
238 /* 20050126161620Z#000009#00#000000 */
239 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
241 suffix
= talloc_asprintf(mem_ctx
,
242 "cn=ldapsync,%s", lp_ldap_suffix());
245 ntstatus
= NT_STATUS_NO_MEMORY
;
250 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
251 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
253 if (rc
!= LDAP_SUCCESS
) {
257 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
258 if (num_result
!= 1) {
259 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
263 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
265 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
269 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
270 if (values
== NULL
) {
271 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
275 num_values
= ldap_count_values(values
);
276 if (num_values
== 0) {
277 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
282 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
283 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
288 if (!strncmp(p
, "csn=", strlen("csn=")))
291 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
293 *seq_num
= generalized_to_unix_time(p
);
295 /* very basic sanity check */
297 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
302 ntstatus
= NT_STATUS_OK
;
306 ldap_value_free(values
);
310 talloc_destroy(mem_ctx
);
315 /*******************************************************************
316 Run the search by name.
317 ******************************************************************/
319 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
321 LDAPMessage
** result
,
325 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
329 return LDAP_NO_MEMORY
;
333 * in the filter expression, replace %u with the real name
334 * so in ldap filter, %u MUST exist :-)
336 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
337 get_objclass_filter(ldap_state
->schema_ver
));
339 TALLOC_FREE(escape_user
);
340 return LDAP_NO_MEMORY
;
343 * have to use this here because $ is filtered out
347 filter
= talloc_all_string_sub(talloc_tos(),
348 filter
, "%u", escape_user
);
349 TALLOC_FREE(escape_user
);
351 return LDAP_NO_MEMORY
;
354 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
355 filter
, attr
, result
);
360 /*******************************************************************
361 Run the search by rid.
362 ******************************************************************/
364 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
365 uint32_t rid
, LDAPMessage
** result
,
371 filter
= talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid
,
372 get_objclass_filter(ldap_state
->schema_ver
));
374 return LDAP_NO_MEMORY
;
377 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
378 filter
, attr
, result
);
383 /*******************************************************************
384 Run the search by SID.
385 ******************************************************************/
387 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
388 const struct dom_sid
*sid
, LDAPMessage
** result
,
395 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
396 get_userattr_key2string(ldap_state
->schema_ver
,
398 sid_to_fstring(sid_string
, sid
),
399 get_objclass_filter(ldap_state
->schema_ver
));
401 return LDAP_NO_MEMORY
;
404 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
405 filter
, attr
, result
);
411 /*******************************************************************
412 Delete complete object or objectclass and attrs from
413 object found in search_result depending on lp_ldap_delete_dn
414 ******************************************************************/
416 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
419 const char *objectclass
,
422 LDAPMod
**mods
= NULL
;
425 BerElement
*ptr
= NULL
;
427 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
429 return LDAP_NO_MEMORY
;
432 if (lp_ldap_delete_dn()) {
433 return smbldap_delete(priv
->smbldap_state
, dn
);
436 /* Ok, delete only the SAM attributes */
438 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
440 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
443 /* We are only allowed to delete the attributes that
446 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
447 if (strequal(*attrib
, name
)) {
448 DEBUG(10, ("ldapsam_delete_entry: deleting "
449 "attribute %s\n", name
));
450 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
461 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
462 talloc_autofree_ldapmod(mem_ctx
, mods
);
464 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
467 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
472 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
473 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
479 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
480 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
490 /**********************************************************************
491 Initialize struct samu from an LDAP query.
492 (Based on init_sam_from_buffer in pdb_tdb.c)
493 *********************************************************************/
495 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
496 struct samu
* sampass
,
503 pass_can_change_time
,
504 pass_must_change_time
,
507 char *username
= NULL
,
513 *logon_script
= NULL
,
514 *profile_path
= NULL
,
516 *workstations
= NULL
,
519 uint8 smblmpwd
[LM_HASH_LEN
],
520 smbntpwd
[NT_HASH_LEN
];
521 bool use_samba_attrs
= True
;
522 uint32_t acct_ctrl
= 0;
524 uint16_t bad_password_count
= 0,
527 uint8 hours
[MAX_HOURS_LEN
];
529 struct login_cache cache_entry
;
531 bool expand_explicit
= lp_passdb_expand_explicit();
533 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
538 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
539 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
543 if (priv2ld(ldap_state
) == NULL
) {
544 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
545 "ldap_struct is NULL!\n"));
549 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
553 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
558 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
560 nt_username
= talloc_strdup(ctx
, username
);
565 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
570 pdb_set_username(sampass
, username
, PDB_SET
);
572 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
573 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
575 /* deal with different attributes between the schema first */
577 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
578 if ((temp
= smbldap_talloc_single_attribute(
579 ldap_state
->smbldap_state
->ldap_struct
,
581 get_userattr_key2string(ldap_state
->schema_ver
,
584 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
587 if ((temp
= smbldap_talloc_single_attribute(
588 ldap_state
->smbldap_state
->ldap_struct
,
590 get_userattr_key2string(ldap_state
->schema_ver
,
593 user_rid
= (uint32_t)atol(temp
);
594 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
598 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
599 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
600 get_userattr_key2string(ldap_state
->schema_ver
,
602 get_userattr_key2string(ldap_state
->schema_ver
,
608 temp
= smbldap_talloc_single_attribute(
609 ldap_state
->smbldap_state
->ldap_struct
,
611 get_userattr_key2string(ldap_state
->schema_ver
,
612 LDAP_ATTR_PWD_LAST_SET
),
615 pass_last_set_time
= (time_t) atol(temp
);
616 pdb_set_pass_last_set_time(sampass
,
617 pass_last_set_time
, PDB_SET
);
620 temp
= smbldap_talloc_single_attribute(
621 ldap_state
->smbldap_state
->ldap_struct
,
623 get_userattr_key2string(ldap_state
->schema_ver
,
624 LDAP_ATTR_LOGON_TIME
),
627 logon_time
= (time_t) atol(temp
);
628 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
631 temp
= smbldap_talloc_single_attribute(
632 ldap_state
->smbldap_state
->ldap_struct
,
634 get_userattr_key2string(ldap_state
->schema_ver
,
635 LDAP_ATTR_LOGOFF_TIME
),
638 logoff_time
= (time_t) atol(temp
);
639 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
642 temp
= smbldap_talloc_single_attribute(
643 ldap_state
->smbldap_state
->ldap_struct
,
645 get_userattr_key2string(ldap_state
->schema_ver
,
646 LDAP_ATTR_KICKOFF_TIME
),
649 kickoff_time
= (time_t) atol(temp
);
650 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
653 temp
= smbldap_talloc_single_attribute(
654 ldap_state
->smbldap_state
->ldap_struct
,
656 get_userattr_key2string(ldap_state
->schema_ver
,
657 LDAP_ATTR_PWD_CAN_CHANGE
),
660 pass_can_change_time
= (time_t) atol(temp
);
661 pdb_set_pass_can_change_time(sampass
,
662 pass_can_change_time
, PDB_SET
);
665 temp
= smbldap_talloc_single_attribute(
666 ldap_state
->smbldap_state
->ldap_struct
,
668 get_userattr_key2string(ldap_state
->schema_ver
,
669 LDAP_ATTR_PWD_MUST_CHANGE
),
672 pass_must_change_time
= (time_t) atol(temp
);
673 pdb_set_pass_must_change_time(sampass
,
674 pass_must_change_time
, PDB_SET
);
677 /* recommend that 'gecos' and 'displayName' should refer to the same
678 * attribute OID. userFullName depreciated, only used by Samba
679 * primary rules of LDAP: don't make a new attribute when one is already defined
680 * that fits your needs; using cn then displayName rather than 'userFullName'
683 fullname
= smbldap_talloc_single_attribute(
684 ldap_state
->smbldap_state
->ldap_struct
,
686 get_userattr_key2string(ldap_state
->schema_ver
,
687 LDAP_ATTR_DISPLAY_NAME
),
690 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
692 fullname
= smbldap_talloc_single_attribute(
693 ldap_state
->smbldap_state
->ldap_struct
,
695 get_userattr_key2string(ldap_state
->schema_ver
,
699 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
703 dir_drive
= smbldap_talloc_single_attribute(
704 ldap_state
->smbldap_state
->ldap_struct
,
706 get_userattr_key2string(ldap_state
->schema_ver
,
707 LDAP_ATTR_HOME_DRIVE
),
710 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
712 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
715 homedir
= smbldap_talloc_single_attribute(
716 ldap_state
->smbldap_state
->ldap_struct
,
718 get_userattr_key2string(ldap_state
->schema_ver
,
719 LDAP_ATTR_HOME_PATH
),
722 if (expand_explicit
) {
723 homedir
= talloc_sub_basic(ctx
,
731 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
733 pdb_set_homedir(sampass
,
734 talloc_sub_basic(ctx
, username
, domain
,
739 logon_script
= smbldap_talloc_single_attribute(
740 ldap_state
->smbldap_state
->ldap_struct
,
742 get_userattr_key2string(ldap_state
->schema_ver
,
743 LDAP_ATTR_LOGON_SCRIPT
),
746 if (expand_explicit
) {
747 logon_script
= talloc_sub_basic(ctx
,
755 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
757 pdb_set_logon_script(sampass
,
758 talloc_sub_basic(ctx
, username
, domain
,
763 profile_path
= smbldap_talloc_single_attribute(
764 ldap_state
->smbldap_state
->ldap_struct
,
766 get_userattr_key2string(ldap_state
->schema_ver
,
767 LDAP_ATTR_PROFILE_PATH
),
770 if (expand_explicit
) {
771 profile_path
= talloc_sub_basic(ctx
,
779 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
781 pdb_set_profile_path(sampass
,
782 talloc_sub_basic(ctx
, username
, domain
,
787 acct_desc
= smbldap_talloc_single_attribute(
788 ldap_state
->smbldap_state
->ldap_struct
,
790 get_userattr_key2string(ldap_state
->schema_ver
,
794 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
797 workstations
= smbldap_talloc_single_attribute(
798 ldap_state
->smbldap_state
->ldap_struct
,
800 get_userattr_key2string(ldap_state
->schema_ver
,
804 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
807 munged_dial
= smbldap_talloc_single_attribute(
808 ldap_state
->smbldap_state
->ldap_struct
,
810 get_userattr_key2string(ldap_state
->schema_ver
,
811 LDAP_ATTR_MUNGED_DIAL
),
814 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
817 /* FIXME: hours stuff should be cleaner */
821 memset(hours
, 0xff, hours_len
);
823 if (ldap_state
->is_nds_ldap
) {
826 char clear_text_pw
[512];
828 /* Make call to Novell eDirectory ldap extension to get clear text password.
829 NOTE: This will only work if we have an SSL connection to eDirectory. */
830 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
831 if (user_dn
!= NULL
) {
832 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
834 pwd_len
= sizeof(clear_text_pw
);
835 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
836 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
837 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
838 TALLOC_FREE(user_dn
);
841 ZERO_STRUCT(smblmpwd
);
842 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
843 TALLOC_FREE(user_dn
);
846 ZERO_STRUCT(smbntpwd
);
847 use_samba_attrs
= False
;
850 TALLOC_FREE(user_dn
);
853 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
857 if (use_samba_attrs
) {
858 temp
= smbldap_talloc_single_attribute(
859 ldap_state
->smbldap_state
->ldap_struct
,
861 get_userattr_key2string(ldap_state
->schema_ver
,
865 pdb_gethexpwd(temp
, smblmpwd
);
866 memset((char *)temp
, '\0', strlen(temp
)+1);
867 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
870 ZERO_STRUCT(smblmpwd
);
873 temp
= smbldap_talloc_single_attribute(
874 ldap_state
->smbldap_state
->ldap_struct
,
876 get_userattr_key2string(ldap_state
->schema_ver
,
880 pdb_gethexpwd(temp
, smbntpwd
);
881 memset((char *)temp
, '\0', strlen(temp
)+1);
882 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
885 ZERO_STRUCT(smbntpwd
);
891 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
893 uint8
*pwhist
= NULL
;
895 char *history_string
= talloc_array(ctx
, char,
896 MAX_PW_HISTORY_LEN
*64);
898 if (!history_string
) {
902 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
904 pwhist
= talloc_array(ctx
, uint8
,
905 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
906 if (pwhist
== NULL
) {
907 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
910 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
912 if (smbldap_get_single_attribute(
913 ldap_state
->smbldap_state
->ldap_struct
,
915 get_userattr_key2string(ldap_state
->schema_ver
,
916 LDAP_ATTR_PWD_HISTORY
),
918 MAX_PW_HISTORY_LEN
*64)) {
919 bool hex_failed
= false;
920 for (i
= 0; i
< pwHistLen
; i
++){
921 /* Get the 16 byte salt. */
922 if (!pdb_gethexpwd(&history_string
[i
*64],
923 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
927 /* Get the 16 byte MD5 hash of salt+passwd. */
928 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
929 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
930 PW_HISTORY_SALT_LEN
])) {
936 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
938 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
941 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
946 temp
= smbldap_talloc_single_attribute(
947 ldap_state
->smbldap_state
->ldap_struct
,
949 get_userattr_key2string(ldap_state
->schema_ver
,
953 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
955 if (acct_ctrl
== 0) {
956 acct_ctrl
|= ACB_NORMAL
;
959 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
961 acct_ctrl
|= ACB_NORMAL
;
964 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
965 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
967 temp
= smbldap_talloc_single_attribute(
968 ldap_state
->smbldap_state
->ldap_struct
,
970 get_userattr_key2string(ldap_state
->schema_ver
,
971 LDAP_ATTR_BAD_PASSWORD_COUNT
),
974 bad_password_count
= (uint32_t) atol(temp
);
975 pdb_set_bad_password_count(sampass
,
976 bad_password_count
, PDB_SET
);
979 temp
= smbldap_talloc_single_attribute(
980 ldap_state
->smbldap_state
->ldap_struct
,
982 get_userattr_key2string(ldap_state
->schema_ver
,
983 LDAP_ATTR_BAD_PASSWORD_TIME
),
986 bad_password_time
= (time_t) atol(temp
);
987 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
991 temp
= smbldap_talloc_single_attribute(
992 ldap_state
->smbldap_state
->ldap_struct
,
994 get_userattr_key2string(ldap_state
->schema_ver
,
995 LDAP_ATTR_LOGON_COUNT
),
998 logon_count
= (uint32_t) atol(temp
);
999 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
1002 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1004 temp
= smbldap_talloc_single_attribute(
1005 ldap_state
->smbldap_state
->ldap_struct
,
1007 get_userattr_key2string(ldap_state
->schema_ver
,
1008 LDAP_ATTR_LOGON_HOURS
),
1011 pdb_gethexhours(temp
, hours
);
1012 memset((char *)temp
, '\0', strlen(temp
) +1);
1013 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
1017 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1018 struct passwd unix_pw
;
1019 bool have_uid
= false;
1020 bool have_gid
= false;
1021 struct dom_sid mapped_gsid
;
1022 const struct dom_sid
*primary_gsid
;
1024 ZERO_STRUCT(unix_pw
);
1026 unix_pw
.pw_name
= username
;
1027 unix_pw
.pw_passwd
= discard_const_p(char, "x");
1029 temp
= smbldap_talloc_single_attribute(
1030 priv2ld(ldap_state
),
1035 /* We've got a uid, feed the cache */
1036 unix_pw
.pw_uid
= strtoul(temp
, NULL
, 10);
1039 temp
= smbldap_talloc_single_attribute(
1040 priv2ld(ldap_state
),
1045 /* We've got a uid, feed the cache */
1046 unix_pw
.pw_gid
= strtoul(temp
, NULL
, 10);
1049 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1050 priv2ld(ldap_state
),
1054 if (unix_pw
.pw_gecos
) {
1055 unix_pw
.pw_gecos
= fullname
;
1057 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1058 priv2ld(ldap_state
),
1062 if (unix_pw
.pw_dir
) {
1063 unix_pw
.pw_dir
= discard_const_p(char, "");
1065 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1066 priv2ld(ldap_state
),
1070 if (unix_pw
.pw_shell
) {
1071 unix_pw
.pw_shell
= discard_const_p(char, "");
1074 if (have_uid
&& have_gid
) {
1075 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1077 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1080 if (sampass
->unix_pw
== NULL
) {
1081 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1082 pdb_get_username(sampass
)));
1086 store_uid_sid_cache(pdb_get_user_sid(sampass
),
1087 sampass
->unix_pw
->pw_uid
);
1088 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass
),
1089 sampass
->unix_pw
->pw_uid
);
1091 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1092 primary_gsid
= pdb_get_group_sid(sampass
);
1093 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1094 store_gid_sid_cache(primary_gsid
,
1095 sampass
->unix_pw
->pw_gid
);
1096 idmap_cache_set_sid2gid(primary_gsid
,
1097 sampass
->unix_pw
->pw_gid
);
1101 /* check the timestamp of the cache vs ldap entry */
1102 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1108 /* see if we have newer updates */
1109 if (!login_cache_read(sampass
, &cache_entry
)) {
1110 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1111 (unsigned int)pdb_get_bad_password_count(sampass
),
1112 (unsigned int)pdb_get_bad_password_time(sampass
)));
1117 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1118 (unsigned int)ldap_entry_time
,
1119 (unsigned int)cache_entry
.entry_timestamp
,
1120 (unsigned int)cache_entry
.bad_password_time
));
1122 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1123 /* cache is older than directory , so
1124 we need to delete the entry but allow the
1125 fields to be written out */
1126 login_cache_delentry(sampass
);
1129 pdb_set_acct_ctrl(sampass
,
1130 pdb_get_acct_ctrl(sampass
) |
1131 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1133 pdb_set_bad_password_count(sampass
,
1134 cache_entry
.bad_password_count
,
1136 pdb_set_bad_password_time(sampass
,
1137 cache_entry
.bad_password_time
,
1149 /**********************************************************************
1150 Initialize the ldap db from a struct samu. Called on update.
1151 (Based on init_buffer_from_sam in pdb_tdb.c)
1152 *********************************************************************/
1154 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1155 LDAPMessage
*existing
,
1156 LDAPMod
*** mods
, struct samu
* sampass
,
1157 bool (*need_update
)(const struct samu
*,
1163 if (mods
== NULL
|| sampass
== NULL
) {
1164 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1171 * took out adding "objectclass: sambaAccount"
1172 * do this on a per-mod basis
1174 if (need_update(sampass
, PDB_USERNAME
)) {
1175 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1176 "uid", pdb_get_username(sampass
));
1177 if (ldap_state
->is_nds_ldap
) {
1178 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1179 "cn", pdb_get_username(sampass
));
1180 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1181 "sn", pdb_get_username(sampass
));
1185 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1187 /* only update the RID if we actually need to */
1188 if (need_update(sampass
, PDB_USERSID
)) {
1190 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1192 switch ( ldap_state
->schema_ver
) {
1193 case SCHEMAVER_SAMBAACCOUNT
:
1194 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1195 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1196 sid_string_dbg(user_sid
),
1198 &ldap_state
->domain_sid
)));
1201 if (asprintf(&temp
, "%i", rid
) < 0) {
1204 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1205 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1210 case SCHEMAVER_SAMBASAMACCOUNT
:
1211 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1212 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1213 sid_to_fstring(sid_string
, user_sid
));
1217 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1222 /* we don't need to store the primary group RID - so leaving it
1223 'free' to hang off the unix primary group makes life easier */
1225 if (need_update(sampass
, PDB_GROUPSID
)) {
1227 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1229 switch ( ldap_state
->schema_ver
) {
1230 case SCHEMAVER_SAMBAACCOUNT
:
1231 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1232 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1233 sid_string_dbg(group_sid
),
1235 &ldap_state
->domain_sid
)));
1239 if (asprintf(&temp
, "%i", rid
) < 0) {
1242 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1243 get_userattr_key2string(ldap_state
->schema_ver
,
1244 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1248 case SCHEMAVER_SAMBASAMACCOUNT
:
1249 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1250 get_userattr_key2string(ldap_state
->schema_ver
,
1251 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1255 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1261 /* displayName, cn, and gecos should all be the same
1262 * most easily accomplished by giving them the same OID
1263 * gecos isn't set here b/c it should be handled by the
1265 * We change displayName only and fall back to cn if
1266 * it does not exist.
1269 if (need_update(sampass
, PDB_FULLNAME
))
1270 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1271 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1272 pdb_get_fullname(sampass
));
1274 if (need_update(sampass
, PDB_ACCTDESC
))
1275 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1276 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1277 pdb_get_acct_desc(sampass
));
1279 if (need_update(sampass
, PDB_WORKSTATIONS
))
1280 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1281 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1282 pdb_get_workstations(sampass
));
1284 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1285 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1286 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1287 pdb_get_munged_dial(sampass
));
1289 if (need_update(sampass
, PDB_SMBHOME
))
1290 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1291 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1292 pdb_get_homedir(sampass
));
1294 if (need_update(sampass
, PDB_DRIVE
))
1295 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1296 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1297 pdb_get_dir_drive(sampass
));
1299 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1300 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1301 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1302 pdb_get_logon_script(sampass
));
1304 if (need_update(sampass
, PDB_PROFILE
))
1305 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1306 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1307 pdb_get_profile_path(sampass
));
1309 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1312 if (need_update(sampass
, PDB_LOGONTIME
))
1313 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1314 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1317 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1320 if (need_update(sampass
, PDB_LOGOFFTIME
))
1321 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1322 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1325 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1328 if (need_update(sampass
, PDB_KICKOFFTIME
))
1329 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1330 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1333 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1336 if (need_update(sampass
, PDB_CANCHANGETIME
))
1337 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1338 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1341 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_must_change_time(sampass
)) < 0) {
1344 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1345 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1346 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1349 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1350 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1352 if (need_update(sampass
, PDB_LMPASSWD
)) {
1353 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1356 pdb_sethexpwd(pwstr
, lm_pw
,
1357 pdb_get_acct_ctrl(sampass
));
1358 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1359 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1362 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1363 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1367 if (need_update(sampass
, PDB_NTPASSWD
)) {
1368 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1371 pdb_sethexpwd(pwstr
, nt_pw
,
1372 pdb_get_acct_ctrl(sampass
));
1373 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1374 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1377 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1378 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1383 if (need_update(sampass
, PDB_PWHISTORY
)) {
1385 uint32_t pwHistLen
= 0;
1386 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1388 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1392 if (pwHistLen
== 0) {
1393 /* Remove any password history from the LDAP store. */
1394 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1398 uint32_t currHistLen
= 0;
1399 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1400 if (pwhist
!= NULL
) {
1401 /* We can only store (1024-1/64 password history entries. */
1402 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1403 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1404 /* Store the salt. */
1405 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1406 /* Followed by the md5 hash of salt + md4 hash */
1407 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1408 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1409 DEBUG(100, ("pwstr=%s\n", pwstr
));
1413 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1414 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1419 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1420 if (asprintf(&temp
, "%li",
1421 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1424 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1425 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1431 if (need_update(sampass
, PDB_HOURS
)) {
1432 const uint8
*hours
= pdb_get_hours(sampass
);
1435 pdb_sethexhours(hourstr
, hours
);
1436 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1439 get_userattr_key2string(ldap_state
->schema_ver
,
1440 LDAP_ATTR_LOGON_HOURS
),
1445 if (need_update(sampass
, PDB_ACCTCTRL
))
1446 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1447 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1448 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1450 /* password lockout cache:
1451 - If we are now autolocking or clearing, we write to ldap
1452 - If we are clearing, we delete the cache entry
1453 - If the count is > 0, we update the cache
1455 This even means when autolocking, we cache, just in case the
1456 update doesn't work, and we have to cache the autolock flag */
1458 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1459 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1460 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1461 time_t badtime
= pdb_get_bad_password_time(sampass
);
1463 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1465 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1466 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1468 if ((badcount
>= pol
) || (badcount
== 0)) {
1469 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1470 (unsigned int)badcount
, (unsigned int)badtime
));
1471 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1475 ldap_state
->smbldap_state
->ldap_struct
,
1477 get_userattr_key2string(
1478 ldap_state
->schema_ver
,
1479 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1483 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1487 ldap_state
->smbldap_state
->ldap_struct
,
1489 get_userattr_key2string(
1490 ldap_state
->schema_ver
,
1491 LDAP_ATTR_BAD_PASSWORD_TIME
),
1495 if (badcount
== 0) {
1496 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1497 login_cache_delentry(sampass
);
1499 struct login_cache cache_entry
;
1501 cache_entry
.entry_timestamp
= time(NULL
);
1502 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1503 cache_entry
.bad_password_count
= badcount
;
1504 cache_entry
.bad_password_time
= badtime
;
1506 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1507 login_cache_write(sampass
, &cache_entry
);
1514 /**********************************************************************
1515 End enumeration of the LDAP password list.
1516 *********************************************************************/
1518 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1520 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1521 if (ldap_state
->result
) {
1522 ldap_msgfree(ldap_state
->result
);
1523 ldap_state
->result
= NULL
;
1527 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1528 const char *new_attr
)
1532 if (new_attr
== NULL
) {
1536 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1540 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1542 SMB_ASSERT((*attr_list
) != NULL
);
1543 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1544 (*attr_list
)[i
+1] = NULL
;
1547 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1548 const char ***attr_list
)
1550 append_attr(mem_ctx
, attr_list
, "uidNumber");
1551 append_attr(mem_ctx
, attr_list
, "gidNumber");
1552 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1553 append_attr(mem_ctx
, attr_list
, "loginShell");
1554 append_attr(mem_ctx
, attr_list
, "gecos");
1557 /**********************************************************************
1558 Get struct samu entry from LDAP by username.
1559 *********************************************************************/
1561 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1563 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1564 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1565 LDAPMessage
*result
= NULL
;
1566 LDAPMessage
*entry
= NULL
;
1568 const char ** attr_list
;
1571 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1572 append_attr(user
, &attr_list
,
1573 get_userattr_key2string(ldap_state
->schema_ver
,
1574 LDAP_ATTR_MOD_TIMESTAMP
));
1575 ldapsam_add_unix_attributes(user
, &attr_list
);
1576 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1578 TALLOC_FREE( attr_list
);
1580 if ( rc
!= LDAP_SUCCESS
)
1581 return NT_STATUS_NO_SUCH_USER
;
1583 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1586 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1587 ldap_msgfree(result
);
1588 return NT_STATUS_NO_SUCH_USER
;
1589 } else if (count
> 1) {
1590 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1591 ldap_msgfree(result
);
1592 return NT_STATUS_NO_SUCH_USER
;
1595 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1597 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1598 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1599 ldap_msgfree(result
);
1600 return NT_STATUS_NO_SUCH_USER
;
1602 pdb_set_backend_private_data(user
, result
, NULL
,
1603 my_methods
, PDB_CHANGED
);
1604 talloc_autofree_ldapmsg(user
, result
);
1607 ldap_msgfree(result
);
1612 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1613 const struct dom_sid
*sid
, LDAPMessage
**result
)
1616 const char ** attr_list
;
1619 switch ( ldap_state
->schema_ver
) {
1620 case SCHEMAVER_SAMBASAMACCOUNT
: {
1621 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1622 if (tmp_ctx
== NULL
) {
1623 return LDAP_NO_MEMORY
;
1626 attr_list
= get_userattr_list(tmp_ctx
,
1627 ldap_state
->schema_ver
);
1628 append_attr(tmp_ctx
, &attr_list
,
1629 get_userattr_key2string(
1630 ldap_state
->schema_ver
,
1631 LDAP_ATTR_MOD_TIMESTAMP
));
1632 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1633 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1635 TALLOC_FREE(tmp_ctx
);
1637 if ( rc
!= LDAP_SUCCESS
)
1642 case SCHEMAVER_SAMBAACCOUNT
:
1643 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1647 attr_list
= get_userattr_list(NULL
,
1648 ldap_state
->schema_ver
);
1649 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1650 TALLOC_FREE( attr_list
);
1652 if ( rc
!= LDAP_SUCCESS
)
1659 /**********************************************************************
1660 Get struct samu entry from LDAP by SID.
1661 *********************************************************************/
1663 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1665 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1666 LDAPMessage
*result
= NULL
;
1667 LDAPMessage
*entry
= NULL
;
1671 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1673 if (rc
!= LDAP_SUCCESS
)
1674 return NT_STATUS_NO_SUCH_USER
;
1676 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1679 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1680 "count=%d\n", sid_string_dbg(sid
), count
));
1681 ldap_msgfree(result
);
1682 return NT_STATUS_NO_SUCH_USER
;
1683 } else if (count
> 1) {
1684 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1685 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1687 ldap_msgfree(result
);
1688 return NT_STATUS_NO_SUCH_USER
;
1691 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1693 ldap_msgfree(result
);
1694 return NT_STATUS_NO_SUCH_USER
;
1697 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1698 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1699 ldap_msgfree(result
);
1700 return NT_STATUS_NO_SUCH_USER
;
1703 pdb_set_backend_private_data(user
, result
, NULL
,
1704 my_methods
, PDB_CHANGED
);
1705 talloc_autofree_ldapmsg(user
, result
);
1706 return NT_STATUS_OK
;
1709 /********************************************************************
1710 Do the actual modification - also change a plaintext passord if
1712 **********************************************************************/
1714 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1715 struct samu
*newpwd
, char *dn
,
1716 LDAPMod
**mods
, int ldap_op
,
1717 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1719 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1722 if (!newpwd
|| !dn
) {
1723 return NT_STATUS_INVALID_PARAMETER
;
1726 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1727 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1728 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1729 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1732 char *retoid
= NULL
;
1733 struct berval
*retdata
= NULL
;
1734 char *utf8_password
;
1736 size_t converted_size
;
1739 if (!ldap_state
->is_nds_ldap
) {
1741 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1742 LDAP_EXOP_MODIFY_PASSWD
)) {
1743 DEBUG(2, ("ldap password change requested, but LDAP "
1744 "server does not support it -- ignoring\n"));
1745 return NT_STATUS_OK
;
1749 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1750 pdb_get_plaintext_passwd(newpwd
),
1753 return NT_STATUS_NO_MEMORY
;
1756 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1757 TALLOC_FREE(utf8_password
);
1758 return NT_STATUS_NO_MEMORY
;
1761 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1762 DEBUG(0,("ber_alloc_t returns NULL\n"));
1763 TALLOC_FREE(utf8_password
);
1764 TALLOC_FREE(utf8_dn
);
1765 return NT_STATUS_UNSUCCESSFUL
;
1768 if ((ber_printf (ber
, "{") < 0) ||
1769 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1771 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1774 TALLOC_FREE(utf8_dn
);
1775 TALLOC_FREE(utf8_password
);
1776 return NT_STATUS_UNSUCCESSFUL
;
1779 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1780 ret
= ber_printf(ber
, "ts}",
1781 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1784 ret
= ber_printf(ber
, "}");
1788 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1791 TALLOC_FREE(utf8_dn
);
1792 TALLOC_FREE(utf8_password
);
1793 return NT_STATUS_UNSUCCESSFUL
;
1796 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1797 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1799 TALLOC_FREE(utf8_dn
);
1800 TALLOC_FREE(utf8_password
);
1801 return NT_STATUS_UNSUCCESSFUL
;
1804 TALLOC_FREE(utf8_dn
);
1805 TALLOC_FREE(utf8_password
);
1808 if (!ldap_state
->is_nds_ldap
) {
1809 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1810 LDAP_EXOP_MODIFY_PASSWD
,
1811 bv
, NULL
, NULL
, &retoid
,
1814 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1815 pdb_get_plaintext_passwd(newpwd
));
1817 if (rc
!= LDAP_SUCCESS
) {
1818 char *ld_error
= NULL
;
1820 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1821 DEBUG(3, ("Could not set userPassword "
1822 "attribute due to an objectClass "
1823 "violation -- ignoring\n"));
1825 return NT_STATUS_OK
;
1828 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1830 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1831 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1832 SAFE_FREE(ld_error
);
1834 #if defined(LDAP_CONSTRAINT_VIOLATION)
1835 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1836 return NT_STATUS_PASSWORD_RESTRICTION
;
1838 return NT_STATUS_UNSUCCESSFUL
;
1840 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1841 #ifdef DEBUG_PASSWORD
1842 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1845 ber_bvfree(retdata
);
1847 ldap_memfree(retoid
);
1853 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1854 /* may be password change below however */
1858 if (ldap_state
->is_nds_ldap
) {
1859 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1863 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1867 rc
= smbldap_add(ldap_state
->smbldap_state
,
1870 case LDAP_MOD_REPLACE
:
1871 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1875 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1877 return NT_STATUS_INVALID_PARAMETER
;
1880 if (rc
!=LDAP_SUCCESS
) {
1881 return NT_STATUS_UNSUCCESSFUL
;
1885 return NT_STATUS_OK
;
1888 /**********************************************************************
1889 Delete entry from LDAP for username.
1890 *********************************************************************/
1892 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1893 struct samu
* sam_acct
)
1895 struct ldapsam_privates
*priv
=
1896 (struct ldapsam_privates
*)my_methods
->private_data
;
1899 LDAPMessage
*msg
, *entry
;
1900 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1901 const char **attr_list
;
1902 TALLOC_CTX
*mem_ctx
;
1905 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1906 return NT_STATUS_INVALID_PARAMETER
;
1909 sname
= pdb_get_username(sam_acct
);
1911 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1914 mem_ctx
= talloc_new(NULL
);
1915 if (mem_ctx
== NULL
) {
1916 DEBUG(0, ("talloc_new failed\n"));
1920 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1921 if (attr_list
== NULL
) {
1925 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1927 if ((rc
!= LDAP_SUCCESS
) ||
1928 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1929 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1930 DEBUG(5, ("Could not find user %s\n", sname
));
1931 result
= NT_STATUS_NO_SUCH_USER
;
1935 rc
= ldapsam_delete_entry(
1936 priv
, mem_ctx
, entry
,
1937 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1938 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1941 result
= (rc
== LDAP_SUCCESS
) ?
1942 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1945 TALLOC_FREE(mem_ctx
);
1949 /**********************************************************************
1951 *********************************************************************/
1953 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1955 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1956 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1959 LDAPMessage
*result
= NULL
;
1960 LDAPMessage
*entry
= NULL
;
1961 LDAPMod
**mods
= NULL
;
1962 const char **attr_list
;
1964 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1966 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1967 if (pdb_get_username(newpwd
) == NULL
) {
1968 return NT_STATUS_INVALID_PARAMETER
;
1970 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1971 TALLOC_FREE( attr_list
);
1972 if (rc
!= LDAP_SUCCESS
) {
1973 return NT_STATUS_UNSUCCESSFUL
;
1975 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1976 my_methods
, PDB_CHANGED
);
1977 talloc_autofree_ldapmsg(newpwd
, result
);
1980 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1981 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1982 return NT_STATUS_UNSUCCESSFUL
;
1985 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1986 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
1988 return NT_STATUS_UNSUCCESSFUL
;
1991 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1993 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1994 pdb_element_is_changed
)) {
1995 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1998 ldap_mods_free(mods
,True
);
1999 return NT_STATUS_UNSUCCESSFUL
;
2002 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
2003 && (mods
== NULL
)) {
2004 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2005 pdb_get_username(newpwd
)));
2007 return NT_STATUS_OK
;
2010 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
2013 ldap_mods_free(mods
,True
);
2019 * We need to set the backend private data to NULL here. For example
2020 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2021 * same one, and with the explicit delete / add logic for attribute
2022 * values the second time we would use the wrong "old" value which
2023 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2025 * The existing LDAPMessage is still being auto-freed by the
2028 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2031 if (!NT_STATUS_IS_OK(ret
)) {
2035 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2036 pdb_get_username(newpwd
)));
2037 return NT_STATUS_OK
;
2040 /***************************************************************************
2041 Renames a struct samu
2042 - The "rename user script" has full responsibility for changing everything
2043 ***************************************************************************/
2045 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2046 TALLOC_CTX
*tmp_ctx
,
2048 uint32_t member_rid
);
2050 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2051 TALLOC_CTX
*mem_ctx
,
2053 struct dom_sid
**pp_sids
,
2055 uint32_t *p_num_groups
);
2057 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2058 struct samu
*old_acct
,
2059 const char *newname
)
2061 const char *oldname
;
2063 char *rename_script
= NULL
;
2064 fstring oldname_lower
, newname_lower
;
2067 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2068 return NT_STATUS_INVALID_PARAMETER
;
2071 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2072 return NT_STATUS_INVALID_PARAMETER
;
2075 oldname
= pdb_get_username(old_acct
);
2077 /* rename the posix user */
2078 rename_script
= talloc_strdup(talloc_tos(), lp_renameuser_script());
2079 if (rename_script
== NULL
) {
2080 return NT_STATUS_NO_MEMORY
;
2083 if (!(*rename_script
)) {
2084 TALLOC_FREE(rename_script
);
2085 return NT_STATUS_ACCESS_DENIED
;
2088 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2091 /* We have to allow the account name to end with a '$'.
2092 Also, follow the semantics in _samr_create_user() and lower case the
2093 posix name but preserve the case in passdb */
2095 fstrcpy( oldname_lower
, oldname
);
2096 strlower_m( oldname_lower
);
2097 fstrcpy( newname_lower
, newname
);
2098 strlower_m( newname_lower
);
2099 rename_script
= realloc_string_sub2(rename_script
,
2104 if (!rename_script
) {
2105 return NT_STATUS_NO_MEMORY
;
2107 rename_script
= realloc_string_sub2(rename_script
,
2112 rc
= smbrun(rename_script
, NULL
);
2114 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2115 rename_script
, rc
));
2117 TALLOC_FREE(rename_script
);
2120 smb_nscd_flush_user_cache();
2124 return NT_STATUS_UNSUCCESSFUL
;
2126 return NT_STATUS_OK
;
2129 /**********************************************************************
2130 Add struct samu to LDAP.
2131 *********************************************************************/
2133 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2135 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2136 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2138 LDAPMessage
*result
= NULL
;
2139 LDAPMessage
*entry
= NULL
;
2140 LDAPMod
**mods
= NULL
;
2141 int ldap_op
= LDAP_MOD_REPLACE
;
2142 uint32_t num_result
;
2143 const char **attr_list
;
2144 char *escape_user
= NULL
;
2145 const char *username
= pdb_get_username(newpwd
);
2146 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2147 char *filter
= NULL
;
2149 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2150 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2153 return NT_STATUS_NO_MEMORY
;
2156 if (!username
|| !*username
) {
2157 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2158 status
= NT_STATUS_INVALID_PARAMETER
;
2162 /* free this list after the second search or in case we exit on failure */
2163 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2165 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2167 if (rc
!= LDAP_SUCCESS
) {
2171 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2172 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2176 ldap_msgfree(result
);
2179 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2180 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2182 if (rc
== LDAP_SUCCESS
) {
2183 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2184 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2185 "already in the base, with samba "
2186 "attributes\n", sid_string_dbg(sid
)));
2189 ldap_msgfree(result
);
2194 /* does the entry already exist but without a samba attributes?
2195 we need to return the samba attributes here */
2197 escape_user
= escape_ldap_string(talloc_tos(), username
);
2198 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2200 status
= NT_STATUS_NO_MEMORY
;
2203 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2204 TALLOC_FREE(escape_user
);
2206 status
= NT_STATUS_NO_MEMORY
;
2210 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2211 filter
, attr_list
, &result
);
2212 if ( rc
!= LDAP_SUCCESS
) {
2216 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2218 if (num_result
> 1) {
2219 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2223 /* Check if we need to update an existing entry */
2224 if (num_result
== 1) {
2225 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2226 ldap_op
= LDAP_MOD_REPLACE
;
2227 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2228 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2230 status
= NT_STATUS_NO_MEMORY
;
2234 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2236 /* There might be a SID for this account already - say an idmap entry */
2238 filter
= talloc_asprintf(ctx
,
2239 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2240 get_userattr_key2string(ldap_state
->schema_ver
,
2241 LDAP_ATTR_USER_SID
),
2242 sid_string_talloc(ctx
, sid
),
2243 LDAP_OBJ_IDMAP_ENTRY
,
2244 LDAP_OBJ_SID_ENTRY
);
2246 status
= NT_STATUS_NO_MEMORY
;
2250 /* free old result before doing a new search */
2251 if (result
!= NULL
) {
2252 ldap_msgfree(result
);
2255 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2256 filter
, attr_list
, &result
);
2258 if ( rc
!= LDAP_SUCCESS
) {
2262 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2264 if (num_result
> 1) {
2265 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2269 /* Check if we need to update an existing entry */
2270 if (num_result
== 1) {
2272 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2273 ldap_op
= LDAP_MOD_REPLACE
;
2274 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2275 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2277 status
= NT_STATUS_NO_MEMORY
;
2283 if (num_result
== 0) {
2284 char *escape_username
;
2285 /* Check if we need to add an entry */
2286 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2287 ldap_op
= LDAP_MOD_ADD
;
2289 escape_username
= escape_rdn_val_string_alloc(username
);
2290 if (!escape_username
) {
2291 status
= NT_STATUS_NO_MEMORY
;
2295 if (username
[strlen(username
)-1] == '$') {
2296 dn
= talloc_asprintf(ctx
,
2299 lp_ldap_machine_suffix());
2301 dn
= talloc_asprintf(ctx
,
2304 lp_ldap_user_suffix());
2307 SAFE_FREE(escape_username
);
2309 status
= NT_STATUS_NO_MEMORY
;
2314 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2315 pdb_element_is_set_or_changed
)) {
2316 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2318 ldap_mods_free(mods
, true);
2324 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2327 switch ( ldap_state
->schema_ver
) {
2328 case SCHEMAVER_SAMBAACCOUNT
:
2329 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2331 case SCHEMAVER_SAMBASAMACCOUNT
:
2332 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2335 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2339 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2340 if (!NT_STATUS_IS_OK(ret
)) {
2341 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2342 pdb_get_username(newpwd
),dn
));
2343 ldap_mods_free(mods
, true);
2347 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2348 ldap_mods_free(mods
, true);
2350 status
= NT_STATUS_OK
;
2356 ldap_msgfree(result
);
2361 /**********************************************************************
2362 *********************************************************************/
2364 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2366 LDAPMessage
** result
)
2368 int scope
= LDAP_SCOPE_SUBTREE
;
2370 const char **attr_list
;
2372 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2373 rc
= smbldap_search(ldap_state
->smbldap_state
,
2374 lp_ldap_suffix (), scope
,
2375 filter
, attr_list
, 0, result
);
2376 TALLOC_FREE(attr_list
);
2381 /**********************************************************************
2382 *********************************************************************/
2384 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2385 GROUP_MAP
*map
, LDAPMessage
*entry
)
2388 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2390 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2391 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2392 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2397 temp
= smbldap_talloc_single_attribute(
2398 ldap_state
->smbldap_state
->ldap_struct
,
2400 get_attr_key2string(groupmap_attr_list
,
2401 LDAP_ATTR_GIDNUMBER
),
2404 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2405 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2409 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2411 map
->gid
= (gid_t
)atol(temp
);
2414 temp
= smbldap_talloc_single_attribute(
2415 ldap_state
->smbldap_state
->ldap_struct
,
2417 get_attr_key2string(groupmap_attr_list
,
2418 LDAP_ATTR_GROUP_SID
),
2421 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2422 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2427 if (!string_to_sid(&map
->sid
, temp
)) {
2428 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2434 temp
= smbldap_talloc_single_attribute(
2435 ldap_state
->smbldap_state
->ldap_struct
,
2437 get_attr_key2string(groupmap_attr_list
,
2438 LDAP_ATTR_GROUP_TYPE
),
2441 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2442 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2446 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2448 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2449 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2450 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2456 temp
= smbldap_talloc_single_attribute(
2457 ldap_state
->smbldap_state
->ldap_struct
,
2459 get_attr_key2string(groupmap_attr_list
,
2460 LDAP_ATTR_DISPLAY_NAME
),
2463 temp
= smbldap_talloc_single_attribute(
2464 ldap_state
->smbldap_state
->ldap_struct
,
2466 get_attr_key2string(groupmap_attr_list
,
2470 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2471 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2476 map
->nt_name
= talloc_strdup(map
, temp
);
2477 if (!map
->nt_name
) {
2483 temp
= smbldap_talloc_single_attribute(
2484 ldap_state
->smbldap_state
->ldap_struct
,
2486 get_attr_key2string(groupmap_attr_list
,
2490 temp
= talloc_strdup(ctx
, "");
2496 map
->comment
= talloc_strdup(map
, temp
);
2497 if (!map
->comment
) {
2502 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2503 store_gid_sid_cache(&map
->sid
, map
->gid
);
2504 idmap_cache_set_sid2gid(&map
->sid
, map
->gid
);
2511 /**********************************************************************
2512 *********************************************************************/
2514 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2518 struct ldapsam_privates
*ldap_state
=
2519 (struct ldapsam_privates
*)methods
->private_data
;
2520 LDAPMessage
*result
= NULL
;
2521 LDAPMessage
*entry
= NULL
;
2524 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2526 return NT_STATUS_NO_SUCH_GROUP
;
2529 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2532 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2534 ldap_msgfree(result
);
2535 return NT_STATUS_NO_SUCH_GROUP
;
2539 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2540 "count=%d\n", filter
, count
));
2541 ldap_msgfree(result
);
2542 return NT_STATUS_NO_SUCH_GROUP
;
2545 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2548 ldap_msgfree(result
);
2549 return NT_STATUS_UNSUCCESSFUL
;
2552 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2553 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2554 "group filter %s\n", filter
));
2555 ldap_msgfree(result
);
2556 return NT_STATUS_NO_SUCH_GROUP
;
2559 ldap_msgfree(result
);
2560 return NT_STATUS_OK
;
2563 /**********************************************************************
2564 *********************************************************************/
2566 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2569 char *filter
= NULL
;
2573 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2575 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2576 sid_to_fstring(tmp
, &sid
)) < 0) {
2577 return NT_STATUS_NO_MEMORY
;
2580 status
= ldapsam_getgroup(methods
, filter
, map
);
2585 /**********************************************************************
2586 *********************************************************************/
2588 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2591 char *filter
= NULL
;
2594 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2596 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2597 (unsigned long)gid
) < 0) {
2598 return NT_STATUS_NO_MEMORY
;
2601 status
= ldapsam_getgroup(methods
, filter
, map
);
2606 /**********************************************************************
2607 *********************************************************************/
2609 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2612 char *filter
= NULL
;
2613 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2617 return NT_STATUS_NO_MEMORY
;
2620 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2622 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2623 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2625 TALLOC_FREE(escape_name
);
2626 return NT_STATUS_NO_MEMORY
;
2629 TALLOC_FREE(escape_name
);
2630 status
= ldapsam_getgroup(methods
, filter
, map
);
2635 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2637 const struct dom_sid
*domain_sid
,
2643 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2644 str
, sizeof(str
)-1)) {
2645 DEBUG(10, ("Could not find sambaSID attribute\n"));
2649 if (!string_to_sid(&sid
, str
)) {
2650 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2654 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2655 DEBUG(10, ("SID %s is not in expected domain %s\n",
2656 str
, sid_string_dbg(domain_sid
)));
2660 if (!sid_peek_rid(&sid
, rid
)) {
2661 DEBUG(10, ("Could not peek into RID\n"));
2668 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2669 TALLOC_CTX
*mem_ctx
,
2670 const struct dom_sid
*group
,
2671 uint32_t **pp_member_rids
,
2672 size_t *p_num_members
)
2674 struct ldapsam_privates
*ldap_state
=
2675 (struct ldapsam_privates
*)methods
->private_data
;
2676 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2677 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2678 const char *sid_attrs
[] = { "sambaSID", NULL
};
2679 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2680 LDAPMessage
*result
= NULL
;
2683 char **values
= NULL
;
2688 *pp_member_rids
= NULL
;
2691 filter
= talloc_asprintf(mem_ctx
,
2692 "(&(objectClass=%s)"
2695 LDAP_OBJ_POSIXGROUP
,
2697 sid_string_talloc(mem_ctx
, group
));
2698 if (filter
== NULL
) {
2699 ret
= NT_STATUS_NO_MEMORY
;
2703 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2704 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2707 if (rc
!= LDAP_SUCCESS
)
2710 talloc_autofree_ldapmsg(mem_ctx
, result
);
2712 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2715 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2716 sid_string_dbg(group
)));
2717 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2722 ret
= NT_STATUS_NO_SUCH_GROUP
;
2726 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2730 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2732 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2733 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2737 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2739 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2741 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2742 if (filter
== NULL
) {
2743 ret
= NT_STATUS_NO_MEMORY
;
2747 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2748 char *escape_memberuid
;
2750 escape_memberuid
= escape_ldap_string(talloc_tos(),
2752 if (escape_memberuid
== NULL
) {
2753 ret
= NT_STATUS_NO_MEMORY
;
2757 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2758 TALLOC_FREE(escape_memberuid
);
2759 if (filter
== NULL
) {
2760 ret
= NT_STATUS_NO_MEMORY
;
2765 filter
= talloc_asprintf_append_buffer(filter
, "))");
2766 if (filter
== NULL
) {
2767 ret
= NT_STATUS_NO_MEMORY
;
2771 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2772 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2775 if (rc
!= LDAP_SUCCESS
)
2778 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2779 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2781 talloc_autofree_ldapmsg(mem_ctx
, result
);
2783 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2785 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2791 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2795 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2796 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2797 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2801 if (!string_to_sid(&sid
, sidstr
))
2804 if (!sid_check_is_in_our_domain(&sid
)) {
2805 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2806 "in our domain\n"));
2807 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2811 sid_peek_rid(&sid
, &rid
);
2813 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2815 ret
= NT_STATUS_NO_MEMORY
;
2821 filter
= talloc_asprintf(mem_ctx
,
2822 "(&(objectClass=%s)"
2824 LDAP_OBJ_SAMBASAMACCOUNT
,
2827 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2828 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2831 if (rc
!= LDAP_SUCCESS
)
2834 talloc_autofree_ldapmsg(mem_ctx
, result
);
2836 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2838 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2842 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2844 get_global_sam_sid(),
2846 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2847 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2851 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2853 ret
= NT_STATUS_NO_MEMORY
;
2863 ldap_value_free(values
);
2868 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2869 TALLOC_CTX
*mem_ctx
,
2871 struct dom_sid
**pp_sids
,
2873 uint32_t *p_num_groups
)
2875 struct ldapsam_privates
*ldap_state
=
2876 (struct ldapsam_privates
*)methods
->private_data
;
2877 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2879 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2882 LDAPMessage
*result
= NULL
;
2884 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2888 gid_t primary_gid
= -1;
2893 if (pdb_get_username(user
) == NULL
) {
2894 return NT_STATUS_INVALID_PARAMETER
;
2897 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2898 if (escape_name
== NULL
)
2899 return NT_STATUS_NO_MEMORY
;
2901 if (user
->unix_pw
) {
2902 primary_gid
= user
->unix_pw
->pw_gid
;
2904 /* retrieve the users primary gid */
2905 filter
= talloc_asprintf(mem_ctx
,
2906 "(&(objectClass=%s)(uid=%s))",
2907 LDAP_OBJ_SAMBASAMACCOUNT
,
2909 if (filter
== NULL
) {
2910 ret
= NT_STATUS_NO_MEMORY
;
2914 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2915 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2917 if (rc
!= LDAP_SUCCESS
)
2920 talloc_autofree_ldapmsg(mem_ctx
, result
);
2922 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2926 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2927 ret
= NT_STATUS_NO_SUCH_USER
;
2930 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2932 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2934 DEBUG (1, ("Unable to find the member's gid!\n"));
2935 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2938 primary_gid
= strtoul(gidstr
, NULL
, 10);
2941 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2942 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2947 filter
= talloc_asprintf(mem_ctx
,
2948 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2949 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2950 if (filter
== NULL
) {
2951 ret
= NT_STATUS_NO_MEMORY
;
2955 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2956 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2958 if (rc
!= LDAP_SUCCESS
)
2961 talloc_autofree_ldapmsg(mem_ctx
, result
);
2969 /* We need to add the primary group as the first gid/sid */
2971 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2972 ret
= NT_STATUS_NO_MEMORY
;
2976 /* This sid will be replaced later */
2978 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2980 if (!NT_STATUS_IS_OK(ret
)) {
2984 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2986 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2993 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2995 str
, sizeof(str
)-1))
2998 if (!string_to_sid(&sid
, str
))
3001 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3003 str
, sizeof(str
)-1))
3006 gid
= strtoul(str
, &end
, 10);
3008 if (PTR_DIFF(end
, str
) != strlen(str
))
3011 if (gid
== primary_gid
) {
3012 sid_copy(&(*pp_sids
)[0], &sid
);
3014 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3016 ret
= NT_STATUS_NO_MEMORY
;
3019 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3021 if (!NT_STATUS_IS_OK(ret
)) {
3027 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3028 DEBUG(3, ("primary group of [%s] not found\n",
3029 pdb_get_username(user
)));
3030 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
3034 *p_num_groups
= num_sids
;
3040 TALLOC_FREE(escape_name
);
3044 /**********************************************************************
3045 * Augment a posixGroup object with a sambaGroupMapping domgroup
3046 *********************************************************************/
3048 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3049 struct ldapsam_privates
*ldap_state
,
3052 const char *filter
, *dn
;
3053 LDAPMessage
*msg
, *entry
;
3057 filter
= talloc_asprintf(mem_ctx
,
3058 "(&(objectClass=%s)(gidNumber=%u))",
3059 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3060 if (filter
== NULL
) {
3061 return NT_STATUS_NO_MEMORY
;
3064 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3065 get_attr_list(mem_ctx
, groupmap_attr_list
),
3067 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3069 if ((rc
!= LDAP_SUCCESS
) ||
3070 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3071 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3072 return NT_STATUS_NO_SUCH_GROUP
;
3075 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3077 return NT_STATUS_NO_MEMORY
;
3081 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3083 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3084 sid_string_talloc(mem_ctx
, &map
->sid
));
3085 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3086 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3087 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3089 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3091 talloc_autofree_ldapmod(mem_ctx
, mods
);
3093 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3094 if (rc
!= LDAP_SUCCESS
) {
3095 return NT_STATUS_ACCESS_DENIED
;
3098 return NT_STATUS_OK
;
3101 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3104 struct ldapsam_privates
*ldap_state
=
3105 (struct ldapsam_privates
*)methods
->private_data
;
3106 LDAPMessage
*msg
= NULL
;
3107 LDAPMod
**mods
= NULL
;
3108 const char *attrs
[] = { NULL
};
3112 TALLOC_CTX
*mem_ctx
;
3119 mem_ctx
= talloc_new(NULL
);
3120 if (mem_ctx
== NULL
) {
3121 DEBUG(0, ("talloc_new failed\n"));
3122 return NT_STATUS_NO_MEMORY
;
3125 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3126 sid_string_talloc(mem_ctx
, &map
->sid
));
3127 if (filter
== NULL
) {
3128 result
= NT_STATUS_NO_MEMORY
;
3132 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3133 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3134 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3136 if ((rc
== LDAP_SUCCESS
) &&
3137 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3139 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3140 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3141 result
= NT_STATUS_GROUP_EXISTS
;
3145 switch (map
->sid_name_use
) {
3147 case SID_NAME_DOM_GRP
:
3148 /* To map a domain group we need to have a posix group
3150 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3154 case SID_NAME_ALIAS
:
3155 if (!sid_check_is_in_our_domain(&map
->sid
)
3156 && !sid_check_is_in_builtin(&map
->sid
) )
3158 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3159 sid_string_dbg(&map
->sid
)));
3160 result
= NT_STATUS_INVALID_PARAMETER
;
3166 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3167 sid_type_lookup(map
->sid_name_use
)));
3168 result
= NT_STATUS_INVALID_PARAMETER
;
3172 /* Domain groups have been mapped in a separate routine, we have to
3173 * create an alias now */
3175 if (map
->gid
== -1) {
3176 DEBUG(10, ("Refusing to map gid==-1\n"));
3177 result
= NT_STATUS_INVALID_PARAMETER
;
3181 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3182 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3183 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3184 result
= NT_STATUS_GROUP_EXISTS
;
3188 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3189 * the best we can get out of LDAP. */
3191 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3192 sid_string_talloc(mem_ctx
, &map
->sid
),
3193 lp_ldap_group_suffix());
3195 result
= NT_STATUS_NO_MEMORY
;
3201 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3202 LDAP_OBJ_SID_ENTRY
);
3203 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3205 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3206 sid_string_talloc(mem_ctx
, &map
->sid
));
3207 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3208 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3209 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3211 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3213 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3214 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3215 talloc_autofree_ldapmod(mem_ctx
, mods
);
3217 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3219 result
= (rc
== LDAP_SUCCESS
) ?
3220 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3223 TALLOC_FREE(mem_ctx
);
3227 /**********************************************************************
3228 * Update a group mapping entry. We're quite strict about what can be changed:
3229 * Only the description and displayname may be changed. It simply does not
3230 * make any sense to change the SID, gid or the type in a mapping.
3231 *********************************************************************/
3233 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3236 struct ldapsam_privates
*ldap_state
=
3237 (struct ldapsam_privates
*)methods
->private_data
;
3239 const char *filter
, *dn
;
3240 LDAPMessage
*msg
= NULL
;
3241 LDAPMessage
*entry
= NULL
;
3242 LDAPMod
**mods
= NULL
;
3243 TALLOC_CTX
*mem_ctx
;
3246 mem_ctx
= talloc_new(NULL
);
3247 if (mem_ctx
== NULL
) {
3248 DEBUG(0, ("talloc_new failed\n"));
3249 return NT_STATUS_NO_MEMORY
;
3252 /* Make 100% sure that sid, gid and type are not changed by looking up
3253 * exactly the values we're given in LDAP. */
3255 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3256 "(sambaSid=%s)(gidNumber=%u)"
3257 "(sambaGroupType=%d))",
3259 sid_string_talloc(mem_ctx
, &map
->sid
),
3260 (unsigned int)map
->gid
, map
->sid_name_use
);
3261 if (filter
== NULL
) {
3262 result
= NT_STATUS_NO_MEMORY
;
3266 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3267 get_attr_list(mem_ctx
, groupmap_attr_list
),
3269 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3271 if ((rc
!= LDAP_SUCCESS
) ||
3272 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3273 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3274 result
= NT_STATUS_NO_SUCH_GROUP
;
3278 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3281 result
= NT_STATUS_NO_MEMORY
;
3286 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3288 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3290 talloc_autofree_ldapmod(mem_ctx
, mods
);
3293 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3294 "nothing to do\n"));
3295 result
= NT_STATUS_OK
;
3299 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3301 if (rc
!= LDAP_SUCCESS
) {
3302 result
= NT_STATUS_ACCESS_DENIED
;
3306 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3307 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3309 result
= NT_STATUS_OK
;
3312 TALLOC_FREE(mem_ctx
);
3316 /**********************************************************************
3317 *********************************************************************/
3319 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3322 struct ldapsam_privates
*priv
=
3323 (struct ldapsam_privates
*)methods
->private_data
;
3324 LDAPMessage
*msg
, *entry
;
3327 TALLOC_CTX
*mem_ctx
;
3330 mem_ctx
= talloc_new(NULL
);
3331 if (mem_ctx
== NULL
) {
3332 DEBUG(0, ("talloc_new failed\n"));
3333 return NT_STATUS_NO_MEMORY
;
3336 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3337 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3338 sid_string_talloc(mem_ctx
, &sid
));
3339 if (filter
== NULL
) {
3340 result
= NT_STATUS_NO_MEMORY
;
3343 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3344 get_attr_list(mem_ctx
, groupmap_attr_list
),
3346 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3348 if ((rc
!= LDAP_SUCCESS
) ||
3349 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3350 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3351 result
= NT_STATUS_NO_SUCH_GROUP
;
3355 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3356 get_attr_list(mem_ctx
,
3357 groupmap_attr_list_to_delete
));
3359 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3360 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3361 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3362 const char *attrs
[] = { "sambaGroupType", "description",
3363 "displayName", "sambaSIDList",
3366 /* Second try. Don't delete the sambaSID attribute, this is
3367 for "old" entries that are tacked on a winbind
3370 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3371 LDAP_OBJ_GROUPMAP
, attrs
);
3374 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3375 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3376 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3377 const char *attrs
[] = { "sambaGroupType", "description",
3378 "displayName", "sambaSIDList",
3379 "gidNumber", NULL
};
3381 /* Third try. This is a post-3.0.21 alias (containing only
3382 * sambaSidEntry and sambaGroupMapping classes), we also have
3383 * to delete the gidNumber attribute, only the sambaSidEntry
3386 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3387 LDAP_OBJ_GROUPMAP
, attrs
);
3390 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3393 TALLOC_FREE(mem_ctx
);
3397 /**********************************************************************
3398 *********************************************************************/
3400 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3403 struct ldapsam_privates
*ldap_state
=
3404 (struct ldapsam_privates
*)my_methods
->private_data
;
3405 char *filter
= NULL
;
3407 const char **attr_list
;
3409 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3411 return NT_STATUS_NO_MEMORY
;
3413 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3414 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3415 LDAP_SCOPE_SUBTREE
, filter
,
3416 attr_list
, 0, &ldap_state
->result
);
3417 TALLOC_FREE(attr_list
);
3419 if (rc
!= LDAP_SUCCESS
) {
3420 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3421 ldap_err2string(rc
)));
3422 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3423 lp_ldap_suffix(), filter
));
3424 ldap_msgfree(ldap_state
->result
);
3425 ldap_state
->result
= NULL
;
3426 TALLOC_FREE(filter
);
3427 return NT_STATUS_UNSUCCESSFUL
;
3430 TALLOC_FREE(filter
);
3432 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3433 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3434 ldap_state
->result
)));
3437 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3438 ldap_state
->result
);
3439 ldap_state
->index
= 0;
3441 return NT_STATUS_OK
;
3444 /**********************************************************************
3445 *********************************************************************/
3447 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3449 ldapsam_endsampwent(my_methods
);
3452 /**********************************************************************
3453 *********************************************************************/
3455 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3458 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3459 struct ldapsam_privates
*ldap_state
=
3460 (struct ldapsam_privates
*)my_methods
->private_data
;
3464 if (!ldap_state
->entry
)
3467 ldap_state
->index
++;
3468 bret
= init_group_from_ldap(ldap_state
, map
,
3472 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3476 return NT_STATUS_OK
;
3479 /**********************************************************************
3480 *********************************************************************/
3482 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3483 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3484 GROUP_MAP
***pp_rmap
,
3485 size_t *p_num_entries
,
3488 GROUP_MAP
*map
= NULL
;
3494 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3495 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3497 return NT_STATUS_ACCESS_DENIED
;
3502 map
= talloc_zero(NULL
, GROUP_MAP
);
3504 return NT_STATUS_NO_MEMORY
;
3507 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3512 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3513 sid_name_use
!= map
->sid_name_use
) {
3514 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3515 "not of the requested type\n",
3519 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3520 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3521 "non mapped\n", map
->nt_name
));
3525 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3526 GROUP_MAP
*, entries
+ 1);
3528 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3529 "enlarge group map!\n"));
3530 return NT_STATUS_UNSUCCESSFUL
;
3533 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3538 ldapsam_endsamgrent(methods
);
3540 *p_num_entries
= entries
;
3542 return NT_STATUS_OK
;
3545 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3546 const struct dom_sid
*alias
,
3547 const struct dom_sid
*member
,
3550 struct ldapsam_privates
*ldap_state
=
3551 (struct ldapsam_privates
*)methods
->private_data
;
3553 LDAPMessage
*result
= NULL
;
3554 LDAPMessage
*entry
= NULL
;
3556 LDAPMod
**mods
= NULL
;
3558 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3561 char *filter
= NULL
;
3563 if (sid_check_is_in_builtin(alias
)) {
3564 type
= SID_NAME_ALIAS
;
3567 if (sid_check_is_in_our_domain(alias
)) {
3568 type
= SID_NAME_ALIAS
;
3571 if (type
== SID_NAME_USE_NONE
) {
3572 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3573 sid_string_dbg(alias
)));
3574 return NT_STATUS_NO_SUCH_ALIAS
;
3577 if (asprintf(&filter
,
3578 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3579 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3581 return NT_STATUS_NO_MEMORY
;
3584 if (ldapsam_search_one_group(ldap_state
, filter
,
3585 &result
) != LDAP_SUCCESS
) {
3587 return NT_STATUS_NO_SUCH_ALIAS
;
3590 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3594 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3595 ldap_msgfree(result
);
3597 return NT_STATUS_NO_SUCH_ALIAS
;
3601 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3602 "filter %s: count=%d\n", filter
, count
));
3603 ldap_msgfree(result
);
3605 return NT_STATUS_NO_SUCH_ALIAS
;
3610 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3614 ldap_msgfree(result
);
3615 return NT_STATUS_UNSUCCESSFUL
;
3618 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3620 ldap_msgfree(result
);
3621 return NT_STATUS_UNSUCCESSFUL
;
3624 smbldap_set_mod(&mods
, modop
,
3625 get_attr_key2string(groupmap_attr_list
,
3626 LDAP_ATTR_SID_LIST
),
3627 sid_to_fstring(tmp
, member
));
3629 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3631 ldap_mods_free(mods
, True
);
3632 ldap_msgfree(result
);
3635 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3636 return NT_STATUS_MEMBER_IN_ALIAS
;
3639 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3640 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3643 if (rc
!= LDAP_SUCCESS
) {
3644 return NT_STATUS_UNSUCCESSFUL
;
3647 return NT_STATUS_OK
;
3650 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3651 const struct dom_sid
*alias
,
3652 const struct dom_sid
*member
)
3654 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3657 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3658 const struct dom_sid
*alias
,
3659 const struct dom_sid
*member
)
3661 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3665 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3666 const struct dom_sid
*alias
,
3667 TALLOC_CTX
*mem_ctx
,
3668 struct dom_sid
**pp_members
,
3669 size_t *p_num_members
)
3671 struct ldapsam_privates
*ldap_state
=
3672 (struct ldapsam_privates
*)methods
->private_data
;
3673 LDAPMessage
*result
= NULL
;
3674 LDAPMessage
*entry
= NULL
;
3676 char **values
= NULL
;
3678 char *filter
= NULL
;
3679 uint32_t num_members
= 0;
3680 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3686 if (sid_check_is_in_builtin(alias
)) {
3687 type
= SID_NAME_ALIAS
;
3690 if (sid_check_is_in_our_domain(alias
)) {
3691 type
= SID_NAME_ALIAS
;
3694 if (type
== SID_NAME_USE_NONE
) {
3695 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3696 sid_string_dbg(alias
)));
3697 return NT_STATUS_NO_SUCH_ALIAS
;
3700 if (asprintf(&filter
,
3701 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3702 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3704 return NT_STATUS_NO_MEMORY
;
3707 if (ldapsam_search_one_group(ldap_state
, filter
,
3708 &result
) != LDAP_SUCCESS
) {
3710 return NT_STATUS_NO_SUCH_ALIAS
;
3713 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3717 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3718 ldap_msgfree(result
);
3720 return NT_STATUS_NO_SUCH_ALIAS
;
3724 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3725 "filter %s: count=%d\n", filter
, count
));
3726 ldap_msgfree(result
);
3728 return NT_STATUS_NO_SUCH_ALIAS
;
3733 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3737 ldap_msgfree(result
);
3738 return NT_STATUS_UNSUCCESSFUL
;
3741 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3743 get_attr_key2string(groupmap_attr_list
,
3744 LDAP_ATTR_SID_LIST
));
3746 if (values
== NULL
) {
3747 ldap_msgfree(result
);
3748 return NT_STATUS_OK
;
3751 count
= ldap_count_values(values
);
3753 for (i
=0; i
<count
; i
++) {
3754 struct dom_sid member
;
3757 if (!string_to_sid(&member
, values
[i
]))
3760 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3762 if (!NT_STATUS_IS_OK(status
)) {
3763 ldap_value_free(values
);
3764 ldap_msgfree(result
);
3769 *p_num_members
= num_members
;
3770 ldap_value_free(values
);
3771 ldap_msgfree(result
);
3773 return NT_STATUS_OK
;
3776 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3777 TALLOC_CTX
*mem_ctx
,
3778 const struct dom_sid
*domain_sid
,
3779 const struct dom_sid
*members
,
3781 uint32_t **pp_alias_rids
,
3782 size_t *p_num_alias_rids
)
3784 struct ldapsam_privates
*ldap_state
=
3785 (struct ldapsam_privates
*)methods
->private_data
;
3788 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3790 LDAPMessage
*result
= NULL
;
3791 LDAPMessage
*entry
= NULL
;
3795 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3796 bool is_builtin
= false;
3797 bool sid_added
= false;
3799 *pp_alias_rids
= NULL
;
3800 *p_num_alias_rids
= 0;
3802 if (sid_check_is_builtin(domain_sid
)) {
3804 type
= SID_NAME_ALIAS
;
3807 if (sid_check_is_domain(domain_sid
)) {
3808 type
= SID_NAME_ALIAS
;
3811 if (type
== SID_NAME_USE_NONE
) {
3812 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3813 sid_string_dbg(domain_sid
)));
3814 return NT_STATUS_UNSUCCESSFUL
;
3817 if (num_members
== 0) {
3818 return NT_STATUS_OK
;
3821 filter
= talloc_asprintf(mem_ctx
,
3822 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3823 LDAP_OBJ_GROUPMAP
, type
);
3825 for (i
=0; i
<num_members
; i
++)
3826 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3828 sid_string_talloc(mem_ctx
,
3831 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3833 if (filter
== NULL
) {
3834 return NT_STATUS_NO_MEMORY
;
3838 ldap_state
->search_cache
.filter
&&
3839 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3840 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3841 result
= ldap_state
->search_cache
.result
;
3842 ldap_state
->search_cache
.result
= NULL
;
3844 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3845 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3846 if (rc
!= LDAP_SUCCESS
) {
3847 return NT_STATUS_UNSUCCESSFUL
;
3849 talloc_autofree_ldapmsg(filter
, result
);
3852 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3854 for (entry
= ldap_first_entry(ldap_struct
, result
);
3856 entry
= ldap_next_entry(ldap_struct
, entry
))
3862 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3868 if (!string_to_sid(&sid
, sid_str
))
3871 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3876 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3877 p_num_alias_rids
)) {
3878 return NT_STATUS_NO_MEMORY
;
3882 if (!is_builtin
&& !sid_added
) {
3883 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3885 * Note: result is a talloc child of filter because of the
3886 * talloc_autofree_ldapmsg() usage
3888 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3889 ldap_state
->search_cache
.result
= result
;
3892 return NT_STATUS_OK
;
3895 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3896 enum pdb_policy_type type
,
3899 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3901 LDAPMod
**mods
= NULL
;
3902 fstring value_string
;
3903 const char *policy_attr
= NULL
;
3905 struct ldapsam_privates
*ldap_state
=
3906 (struct ldapsam_privates
*)methods
->private_data
;
3908 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3910 if (!ldap_state
->domain_dn
) {
3911 return NT_STATUS_INVALID_PARAMETER
;
3914 policy_attr
= get_account_policy_attr(type
);
3915 if (policy_attr
== NULL
) {
3916 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3921 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3923 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3925 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3928 ldap_mods_free(mods
, True
);
3930 if (rc
!= LDAP_SUCCESS
) {
3934 if (!cache_account_policy_set(type
, value
)) {
3935 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3936 "update local tdb cache\n"));
3940 return NT_STATUS_OK
;
3943 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3944 enum pdb_policy_type type
,
3947 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3951 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3952 enum pdb_policy_type type
,
3955 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3956 LDAPMessage
*result
= NULL
;
3957 LDAPMessage
*entry
= NULL
;
3962 const char *policy_attr
= NULL
;
3964 struct ldapsam_privates
*ldap_state
=
3965 (struct ldapsam_privates
*)methods
->private_data
;
3967 const char *attrs
[2];
3969 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3971 if (!ldap_state
->domain_dn
) {
3972 return NT_STATUS_INVALID_PARAMETER
;
3975 policy_attr
= get_account_policy_attr(type
);
3977 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3978 "policy index: %d\n", type
));
3982 attrs
[0] = policy_attr
;
3985 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3986 if (filter
== NULL
) {
3987 return NT_STATUS_NO_MEMORY
;
3989 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3990 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
3992 TALLOC_FREE(filter
);
3993 if (rc
!= LDAP_SUCCESS
) {
3997 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4002 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4003 if (entry
== NULL
) {
4007 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4012 *value
= (uint32_t)atol(vals
[0]);
4014 ntstatus
= NT_STATUS_OK
;
4018 ldap_value_free(vals
);
4019 ldap_msgfree(result
);
4024 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4026 - if user hasn't decided to use account policies inside LDAP just reuse the
4029 - if there is a valid cache entry, return that
4030 - if there is an LDAP entry, update cache and return
4031 - otherwise set to default, update cache and return
4035 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4036 enum pdb_policy_type type
,
4039 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4041 if (cache_account_policy_get(type
, value
)) {
4042 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4044 return NT_STATUS_OK
;
4047 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4049 if (NT_STATUS_IS_OK(ntstatus
)) {
4053 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4057 /* should we automagically migrate old tdb value here ? */
4058 if (account_policy_get(type
, value
))
4061 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4062 "default\n", type
));
4065 if (!account_policy_get_default(type
, value
)) {
4071 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4072 if (!NT_STATUS_IS_OK(ntstatus
)) {
4078 if (!cache_account_policy_set(type
, *value
)) {
4079 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4080 "tdb as a cache\n"));
4081 return NT_STATUS_UNSUCCESSFUL
;
4084 return NT_STATUS_OK
;
4087 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4088 const struct dom_sid
*domain_sid
,
4092 enum lsa_SidType
*attrs
)
4094 struct ldapsam_privates
*ldap_state
=
4095 (struct ldapsam_privates
*)methods
->private_data
;
4096 LDAPMessage
*msg
= NULL
;
4098 char *allsids
= NULL
;
4099 int i
, rc
, num_mapped
;
4100 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4101 TALLOC_CTX
*mem_ctx
;
4105 mem_ctx
= talloc_new(NULL
);
4106 if (mem_ctx
== NULL
) {
4107 DEBUG(0, ("talloc_new failed\n"));
4111 if (!sid_check_is_builtin(domain_sid
) &&
4112 !sid_check_is_domain(domain_sid
)) {
4113 result
= NT_STATUS_INVALID_PARAMETER
;
4117 if (num_rids
== 0) {
4118 result
= NT_STATUS_NONE_MAPPED
;
4122 for (i
=0; i
<num_rids
; i
++)
4123 attrs
[i
] = SID_NAME_UNKNOWN
;
4125 allsids
= talloc_strdup(mem_ctx
, "");
4126 if (allsids
== NULL
) {
4130 for (i
=0; i
<num_rids
; i
++) {
4132 sid_compose(&sid
, domain_sid
, rids
[i
]);
4133 allsids
= talloc_asprintf_append_buffer(
4134 allsids
, "(sambaSid=%s)",
4135 sid_string_talloc(mem_ctx
, &sid
));
4136 if (allsids
== NULL
) {
4141 /* First look for users */
4145 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4147 filter
= talloc_asprintf(
4148 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4149 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4151 if (filter
== NULL
) {
4155 rc
= smbldap_search(ldap_state
->smbldap_state
,
4156 lp_ldap_user_suffix(),
4157 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4159 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4162 if (rc
!= LDAP_SUCCESS
)
4165 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4168 for (entry
= ldap_first_entry(ld
, msg
);
4170 entry
= ldap_next_entry(ld
, entry
)) {
4175 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4177 DEBUG(2, ("Could not find sid from ldap entry\n"));
4181 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4184 DEBUG(2, ("Could not retrieve uid attribute\n"));
4188 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4189 if (rid
== rids
[rid_index
])
4193 if (rid_index
== num_rids
) {
4194 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4198 attrs
[rid_index
] = SID_NAME_USER
;
4199 names
[rid_index
] = name
;
4203 if (num_mapped
== num_rids
) {
4204 /* No need to look for groups anymore -- we're done */
4205 result
= NT_STATUS_OK
;
4209 /* Same game for groups */
4213 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4214 "sambaGroupType", NULL
};
4216 filter
= talloc_asprintf(
4217 mem_ctx
, "(&(objectClass=%s)(|%s))",
4218 LDAP_OBJ_GROUPMAP
, allsids
);
4219 if (filter
== NULL
) {
4223 rc
= smbldap_search(ldap_state
->smbldap_state
,
4225 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4227 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4230 if (rc
!= LDAP_SUCCESS
)
4233 /* ldap_struct might have changed due to a reconnect */
4235 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4237 /* For consistency checks, we already checked we're only domain or builtin */
4239 is_builtin
= sid_check_is_builtin(domain_sid
);
4241 for (entry
= ldap_first_entry(ld
, msg
);
4243 entry
= ldap_next_entry(ld
, entry
))
4248 enum lsa_SidType type
;
4249 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4251 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4254 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4259 type
= (enum lsa_SidType
)atol(attr
);
4261 /* Consistency checks */
4262 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4263 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4264 (type
!= SID_NAME_DOM_GRP
)))) {
4265 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4268 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4270 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4274 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4277 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4279 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4283 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4288 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4289 if (rid
== rids
[rid_index
])
4293 if (rid_index
== num_rids
) {
4294 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4298 attrs
[rid_index
] = type
;
4299 names
[rid_index
] = attr
;
4303 result
= NT_STATUS_NONE_MAPPED
;
4306 result
= (num_mapped
== num_rids
) ?
4307 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4309 TALLOC_FREE(mem_ctx
);
4313 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4315 char *filter
= NULL
;
4316 char *escaped
= NULL
;
4317 char *result
= NULL
;
4319 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4320 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4324 escaped
= escape_ldap_string(talloc_tos(), username
);
4325 if (escaped
== NULL
) goto done
;
4327 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4331 TALLOC_FREE(escaped
);
4336 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4340 const char **result
;
4342 va_start(ap
, mem_ctx
);
4343 while (va_arg(ap
, const char *) != NULL
)
4347 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4351 va_start(ap
, mem_ctx
);
4352 for (i
=0; i
<num
; i
++) {
4353 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4354 if (result
[i
] == NULL
) {
4355 talloc_free(result
);
4366 struct ldap_search_state
{
4367 struct smbldap_state
*connection
;
4369 uint32_t acct_flags
;
4370 uint16_t group_type
;
4377 void *pagedresults_cookie
;
4379 LDAPMessage
*entries
, *current_entry
;
4380 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4381 TALLOC_CTX
*mem_ctx
,
4382 LDAP
*ld
, LDAPMessage
*entry
,
4383 struct samr_displayentry
*result
);
4386 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4388 struct ldap_search_state
*state
=
4389 (struct ldap_search_state
*)search
->private_data
;
4391 int rc
= LDAP_OPERATIONS_ERROR
;
4393 state
->entries
= NULL
;
4395 if (state
->connection
->paged_results
) {
4396 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4397 state
->scope
, state
->filter
,
4398 state
->attrs
, state
->attrsonly
,
4399 lp_ldap_page_size(), &state
->entries
,
4400 &state
->pagedresults_cookie
);
4403 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4405 if (state
->entries
!= NULL
) {
4406 /* Left over from unsuccessful paged attempt */
4407 ldap_msgfree(state
->entries
);
4408 state
->entries
= NULL
;
4411 rc
= smbldap_search(state
->connection
, state
->base
,
4412 state
->scope
, state
->filter
, state
->attrs
,
4413 state
->attrsonly
, &state
->entries
);
4415 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4418 /* Ok, the server was lying. It told us it could do paged
4419 * searches when it could not. */
4420 state
->connection
->paged_results
= False
;
4423 ld
= state
->connection
->ldap_struct
;
4425 DEBUG(5, ("Don't have an LDAP connection right after a "
4429 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4434 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4436 struct ldap_search_state
*state
=
4437 (struct ldap_search_state
*)search
->private_data
;
4440 if (!state
->connection
->paged_results
) {
4441 /* There is no next page when there are no paged results */
4445 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4446 state
->scope
, state
->filter
, state
->attrs
,
4447 state
->attrsonly
, lp_ldap_page_size(),
4449 &state
->pagedresults_cookie
);
4451 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4454 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4456 if (state
->current_entry
== NULL
) {
4457 ldap_msgfree(state
->entries
);
4458 state
->entries
= NULL
;
4465 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4466 struct samr_displayentry
*entry
)
4468 struct ldap_search_state
*state
=
4469 (struct ldap_search_state
*)search
->private_data
;
4473 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4476 if ((state
->entries
== NULL
) &&
4477 !ldapsam_search_nextpage(search
))
4480 if (state
->current_entry
== NULL
) {
4484 result
= state
->ldap2displayentry(state
, search
,
4485 state
->connection
->ldap_struct
,
4486 state
->current_entry
, entry
);
4490 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4491 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4492 if (dn
!= NULL
) ldap_memfree(dn
);
4495 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4497 if (state
->current_entry
== NULL
) {
4498 ldap_msgfree(state
->entries
);
4499 state
->entries
= NULL
;
4502 if (!result
) goto retry
;
4507 static void ldapsam_search_end(struct pdb_search
*search
)
4509 struct ldap_search_state
*state
=
4510 (struct ldap_search_state
*)search
->private_data
;
4513 if (state
->pagedresults_cookie
== NULL
)
4516 if (state
->entries
!= NULL
)
4517 ldap_msgfree(state
->entries
);
4519 state
->entries
= NULL
;
4520 state
->current_entry
= NULL
;
4522 if (!state
->connection
->paged_results
)
4525 /* Tell the LDAP server we're not interested in the rest anymore. */
4527 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4528 state
->filter
, state
->attrs
,
4529 state
->attrsonly
, 0, &state
->entries
,
4530 &state
->pagedresults_cookie
);
4532 if (rc
!= LDAP_SUCCESS
)
4533 DEBUG(5, ("Could not end search properly\n"));
4538 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4539 TALLOC_CTX
*mem_ctx
,
4540 LDAP
*ld
, LDAPMessage
*entry
,
4541 struct samr_displayentry
*result
)
4544 size_t converted_size
;
4546 uint32_t acct_flags
;
4548 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4549 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4550 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4553 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4554 ldap_value_free(vals
);
4556 if ((state
->acct_flags
!= 0) &&
4557 ((state
->acct_flags
& acct_flags
) == 0))
4560 result
->acct_flags
= acct_flags
;
4561 result
->account_name
= "";
4562 result
->fullname
= "";
4563 result
->description
= "";
4565 vals
= ldap_get_values(ld
, entry
, "uid");
4566 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4567 DEBUG(5, ("\"uid\" not found\n"));
4570 if (!pull_utf8_talloc(mem_ctx
,
4571 discard_const_p(char *, &result
->account_name
),
4572 vals
[0], &converted_size
))
4574 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4578 ldap_value_free(vals
);
4580 vals
= ldap_get_values(ld
, entry
, "displayName");
4581 if ((vals
== NULL
) || (vals
[0] == NULL
))
4582 DEBUG(8, ("\"displayName\" not found\n"));
4583 else if (!pull_utf8_talloc(mem_ctx
,
4584 discard_const_p(char *, &result
->fullname
),
4585 vals
[0], &converted_size
))
4587 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4591 ldap_value_free(vals
);
4593 vals
= ldap_get_values(ld
, entry
, "description");
4594 if ((vals
== NULL
) || (vals
[0] == NULL
))
4595 DEBUG(8, ("\"description\" not found\n"));
4596 else if (!pull_utf8_talloc(mem_ctx
,
4597 discard_const_p(char *, &result
->description
),
4598 vals
[0], &converted_size
))
4600 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4604 ldap_value_free(vals
);
4606 if ((result
->account_name
== NULL
) ||
4607 (result
->fullname
== NULL
) ||
4608 (result
->description
== NULL
)) {
4609 DEBUG(0, ("talloc failed\n"));
4613 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4614 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4615 DEBUG(0, ("\"objectSid\" not found\n"));
4619 if (!string_to_sid(&sid
, vals
[0])) {
4620 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4621 ldap_value_free(vals
);
4624 ldap_value_free(vals
);
4626 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4627 DEBUG(0, ("sid %s does not belong to our domain\n",
4628 sid_string_dbg(&sid
)));
4636 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4637 struct pdb_search
*search
,
4638 uint32_t acct_flags
)
4640 struct ldapsam_privates
*ldap_state
=
4641 (struct ldapsam_privates
*)methods
->private_data
;
4642 struct ldap_search_state
*state
;
4644 state
= talloc(search
, struct ldap_search_state
);
4645 if (state
== NULL
) {
4646 DEBUG(0, ("talloc failed\n"));
4650 state
->connection
= ldap_state
->smbldap_state
;
4652 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4653 state
->base
= lp_ldap_user_suffix();
4654 else if ((acct_flags
!= 0) &&
4655 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4656 state
->base
= lp_ldap_machine_suffix();
4658 state
->base
= lp_ldap_suffix();
4660 state
->acct_flags
= acct_flags
;
4661 state
->base
= talloc_strdup(search
, state
->base
);
4662 state
->scope
= LDAP_SCOPE_SUBTREE
;
4663 state
->filter
= get_ldap_filter(search
, "*");
4664 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4665 "displayName", "description",
4666 "sambaAcctFlags", NULL
);
4667 state
->attrsonly
= 0;
4668 state
->pagedresults_cookie
= NULL
;
4669 state
->entries
= NULL
;
4670 state
->ldap2displayentry
= ldapuser2displayentry
;
4672 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4673 DEBUG(0, ("talloc failed\n"));
4677 search
->private_data
= state
;
4678 search
->next_entry
= ldapsam_search_next_entry
;
4679 search
->search_end
= ldapsam_search_end
;
4681 return ldapsam_search_firstpage(search
);
4684 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4685 TALLOC_CTX
*mem_ctx
,
4686 LDAP
*ld
, LDAPMessage
*entry
,
4687 struct samr_displayentry
*result
)
4690 size_t converted_size
;
4692 uint16_t group_type
;
4694 result
->account_name
= "";
4695 result
->fullname
= "";
4696 result
->description
= "";
4699 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4700 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4701 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4703 ldap_value_free(vals
);
4708 group_type
= atoi(vals
[0]);
4710 if ((state
->group_type
!= 0) &&
4711 ((state
->group_type
!= group_type
))) {
4712 ldap_value_free(vals
);
4716 ldap_value_free(vals
);
4718 /* display name is the NT group name */
4720 vals
= ldap_get_values(ld
, entry
, "displayName");
4721 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4722 DEBUG(8, ("\"displayName\" not found\n"));
4724 /* fallback to the 'cn' attribute */
4725 vals
= ldap_get_values(ld
, entry
, "cn");
4726 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4727 DEBUG(5, ("\"cn\" not found\n"));
4730 if (!pull_utf8_talloc(mem_ctx
,
4731 discard_const_p(char *,
4732 &result
->account_name
),
4733 vals
[0], &converted_size
))
4735 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4736 "failed: %s", strerror(errno
)));
4739 else if (!pull_utf8_talloc(mem_ctx
,
4740 discard_const_p(char *,
4741 &result
->account_name
),
4742 vals
[0], &converted_size
))
4744 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4748 ldap_value_free(vals
);
4750 vals
= ldap_get_values(ld
, entry
, "description");
4751 if ((vals
== NULL
) || (vals
[0] == NULL
))
4752 DEBUG(8, ("\"description\" not found\n"));
4753 else if (!pull_utf8_talloc(mem_ctx
,
4754 discard_const_p(char *, &result
->description
),
4755 vals
[0], &converted_size
))
4757 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4760 ldap_value_free(vals
);
4762 if ((result
->account_name
== NULL
) ||
4763 (result
->fullname
== NULL
) ||
4764 (result
->description
== NULL
)) {
4765 DEBUG(0, ("talloc failed\n"));
4769 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4770 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4771 DEBUG(0, ("\"objectSid\" not found\n"));
4773 ldap_value_free(vals
);
4778 if (!string_to_sid(&sid
, vals
[0])) {
4779 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4783 ldap_value_free(vals
);
4785 switch (group_type
) {
4786 case SID_NAME_DOM_GRP
:
4787 case SID_NAME_ALIAS
:
4789 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4790 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4792 DEBUG(0, ("%s is not in our domain\n",
4793 sid_string_dbg(&sid
)));
4799 DEBUG(0,("unknown group type: %d\n", group_type
));
4803 result
->acct_flags
= 0;
4808 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4809 struct pdb_search
*search
,
4810 const struct dom_sid
*sid
,
4811 enum lsa_SidType type
)
4813 struct ldapsam_privates
*ldap_state
=
4814 (struct ldapsam_privates
*)methods
->private_data
;
4815 struct ldap_search_state
*state
;
4818 state
= talloc(search
, struct ldap_search_state
);
4819 if (state
== NULL
) {
4820 DEBUG(0, ("talloc failed\n"));
4824 state
->connection
= ldap_state
->smbldap_state
;
4826 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4827 state
->connection
= ldap_state
->smbldap_state
;
4828 state
->scope
= LDAP_SCOPE_SUBTREE
;
4829 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4830 "(sambaGroupType=%d)(sambaSID=%s*))",
4832 type
, sid_to_fstring(tmp
, sid
));
4833 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4834 "displayName", "description",
4835 "sambaGroupType", NULL
);
4836 state
->attrsonly
= 0;
4837 state
->pagedresults_cookie
= NULL
;
4838 state
->entries
= NULL
;
4839 state
->group_type
= type
;
4840 state
->ldap2displayentry
= ldapgroup2displayentry
;
4842 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4843 DEBUG(0, ("talloc failed\n"));
4847 search
->private_data
= state
;
4848 search
->next_entry
= ldapsam_search_next_entry
;
4849 search
->search_end
= ldapsam_search_end
;
4851 return ldapsam_search_firstpage(search
);
4854 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4855 struct pdb_search
*search
)
4857 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4860 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4861 struct pdb_search
*search
,
4862 const struct dom_sid
*sid
)
4864 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4867 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4869 return PDB_CAP_STORE_RIDS
;
4872 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4875 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4877 LDAPMessage
*result
= NULL
;
4878 LDAPMessage
*entry
= NULL
;
4879 LDAPMod
**mods
= NULL
;
4883 uint32_t nextRid
= 0;
4886 TALLOC_CTX
*mem_ctx
;
4888 mem_ctx
= talloc_new(NULL
);
4889 if (mem_ctx
== NULL
) {
4890 DEBUG(0, ("talloc_new failed\n"));
4891 return NT_STATUS_NO_MEMORY
;
4894 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4895 get_global_sam_name(), False
);
4896 if (!NT_STATUS_IS_OK(status
)) {
4897 DEBUG(3, ("Could not get domain info: %s\n",
4898 nt_errstr(status
)));
4902 talloc_autofree_ldapmsg(mem_ctx
, result
);
4904 entry
= ldap_first_entry(priv2ld(priv
), result
);
4905 if (entry
== NULL
) {
4906 DEBUG(0, ("Could not get domain info entry\n"));
4907 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4911 /* Find the largest of the three attributes "sambaNextRid",
4912 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4913 concept of differentiating between user and group rids, and will
4914 use only "sambaNextRid" in the future. But for compatibility
4915 reasons I look if others have chosen different strategies -- VL */
4917 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4918 "sambaNextRid", mem_ctx
);
4919 if (value
!= NULL
) {
4920 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4921 nextRid
= MAX(nextRid
, tmp
);
4924 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4925 "sambaNextUserRid", mem_ctx
);
4926 if (value
!= NULL
) {
4927 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4928 nextRid
= MAX(nextRid
, tmp
);
4931 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4932 "sambaNextGroupRid", mem_ctx
);
4933 if (value
!= NULL
) {
4934 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4935 nextRid
= MAX(nextRid
, tmp
);
4939 nextRid
= BASE_RID
-1;
4944 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4945 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4946 talloc_autofree_ldapmod(mem_ctx
, mods
);
4948 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4949 status
= NT_STATUS_NO_MEMORY
;
4953 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4955 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4958 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4961 if (NT_STATUS_IS_OK(status
)) {
4965 TALLOC_FREE(mem_ctx
);
4969 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4973 for (i
=0; i
<10; i
++) {
4974 NTSTATUS result
= ldapsam_get_new_rid(
4975 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4976 if (NT_STATUS_IS_OK(result
)) {
4980 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4984 /* The ldap update failed (maybe a race condition), retry */
4987 /* Tried 10 times, fail. */
4988 return NT_STATUS_ACCESS_DENIED
;
4991 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
4993 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4994 return NT_STATUS_IS_OK(result
) ? True
: False
;
4997 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4998 const struct dom_sid
*sid
,
4999 uid_t
*uid
, gid_t
*gid
,
5000 enum lsa_SidType
*type
)
5002 struct ldapsam_privates
*priv
=
5003 (struct ldapsam_privates
*)methods
->private_data
;
5005 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5007 LDAPMessage
*result
= NULL
;
5008 LDAPMessage
*entry
= NULL
;
5013 TALLOC_CTX
*mem_ctx
;
5015 mem_ctx
= talloc_new(NULL
);
5016 if (mem_ctx
== NULL
) {
5017 DEBUG(0, ("talloc_new failed\n"));
5021 filter
= talloc_asprintf(mem_ctx
,
5023 "(|(objectClass=%s)(objectClass=%s)))",
5024 sid_string_talloc(mem_ctx
, sid
),
5025 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5026 if (filter
== NULL
) {
5027 DEBUG(5, ("talloc_asprintf failed\n"));
5031 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5033 if (rc
!= LDAP_SUCCESS
) {
5036 talloc_autofree_ldapmsg(mem_ctx
, result
);
5038 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5039 DEBUG(10, ("Got %d entries, expected one\n",
5040 ldap_count_entries(priv2ld(priv
), result
)));
5044 entry
= ldap_first_entry(priv2ld(priv
), result
);
5046 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5047 "sambaGroupType", mem_ctx
);
5049 if (value
!= NULL
) {
5050 const char *gid_str
;
5053 gid_str
= smbldap_talloc_single_attribute(
5054 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5055 if (gid_str
== NULL
) {
5056 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5057 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5062 *gid
= strtoul(gid_str
, NULL
, 10);
5063 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
5064 store_gid_sid_cache(sid
, *gid
);
5065 idmap_cache_set_sid2gid(sid
, *gid
);
5070 /* It must be a user */
5072 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5073 "uidNumber", mem_ctx
);
5074 if (value
== NULL
) {
5075 DEBUG(1, ("Could not find uidNumber in %s\n",
5076 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5080 *uid
= strtoul(value
, NULL
, 10);
5081 *type
= SID_NAME_USER
;
5082 store_uid_sid_cache(sid
, *uid
);
5083 idmap_cache_set_sid2uid(sid
, *uid
);
5087 TALLOC_FREE(mem_ctx
);
5092 * Find the SID for a uid.
5093 * This is shortcut is only used if ldapsam:trusted is set to true.
5095 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5096 struct dom_sid
*sid
)
5098 struct ldapsam_privates
*priv
=
5099 (struct ldapsam_privates
*)methods
->private_data
;
5101 const char *attrs
[] = { "sambaSID", NULL
};
5102 LDAPMessage
*result
= NULL
;
5103 LDAPMessage
*entry
= NULL
;
5105 char *user_sid_string
;
5106 struct dom_sid user_sid
;
5108 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5110 filter
= talloc_asprintf(tmp_ctx
,
5113 "(objectClass=%s))",
5115 LDAP_OBJ_POSIXACCOUNT
,
5116 LDAP_OBJ_SAMBASAMACCOUNT
);
5117 if (filter
== NULL
) {
5118 DEBUG(3, ("talloc_asprintf failed\n"));
5122 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5123 if (rc
!= LDAP_SUCCESS
) {
5126 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5128 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5129 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5130 ldap_count_entries(priv2ld(priv
), result
),
5131 (unsigned int)uid
));
5135 entry
= ldap_first_entry(priv2ld(priv
), result
);
5137 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5138 "sambaSID", tmp_ctx
);
5139 if (user_sid_string
== NULL
) {
5140 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5141 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5145 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5146 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5151 sid_copy(sid
, &user_sid
);
5153 store_uid_sid_cache(sid
, uid
);
5154 idmap_cache_set_sid2uid(sid
, uid
);
5159 TALLOC_FREE(tmp_ctx
);
5164 * Find the SID for a gid.
5165 * This is shortcut is only used if ldapsam:trusted is set to true.
5167 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5168 struct dom_sid
*sid
)
5170 struct ldapsam_privates
*priv
=
5171 (struct ldapsam_privates
*)methods
->private_data
;
5173 const char *attrs
[] = { "sambaSID", NULL
};
5174 LDAPMessage
*result
= NULL
;
5175 LDAPMessage
*entry
= NULL
;
5177 char *group_sid_string
;
5178 struct dom_sid group_sid
;
5180 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5182 filter
= talloc_asprintf(tmp_ctx
,
5184 "(objectClass=%s))",
5187 if (filter
== NULL
) {
5188 DEBUG(3, ("talloc_asprintf failed\n"));
5192 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5193 if (rc
!= LDAP_SUCCESS
) {
5196 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5198 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5199 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5200 ldap_count_entries(priv2ld(priv
), result
),
5201 (unsigned int)gid
));
5205 entry
= ldap_first_entry(priv2ld(priv
), result
);
5207 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5208 "sambaSID", tmp_ctx
);
5209 if (group_sid_string
== NULL
) {
5210 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5211 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5215 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5216 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5221 sid_copy(sid
, &group_sid
);
5223 store_gid_sid_cache(sid
, gid
);
5224 idmap_cache_set_sid2gid(sid
, gid
);
5229 TALLOC_FREE(tmp_ctx
);
5235 * The following functions are called only if
5236 * ldapsam:trusted and ldapsam:editposix are
5241 * ldapsam_create_user creates a new
5242 * posixAccount and sambaSamAccount object
5243 * in the ldap users subtree
5245 * The uid is allocated by winbindd.
5248 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5249 TALLOC_CTX
*tmp_ctx
, const char *name
,
5250 uint32_t acb_info
, uint32_t *rid
)
5252 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5253 LDAPMessage
*entry
= NULL
;
5254 LDAPMessage
*result
= NULL
;
5255 uint32_t num_result
;
5256 bool is_machine
= False
;
5257 bool add_posix
= False
;
5258 LDAPMod
**mods
= NULL
;
5266 const char *dn
= NULL
;
5267 struct dom_sid group_sid
;
5268 struct dom_sid user_sid
;
5274 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5275 acb_info
& ACB_WSTRUST
||
5276 acb_info
& ACB_SVRTRUST
||
5277 acb_info
& ACB_DOMTRUST
) {
5281 username
= escape_ldap_string(talloc_tos(), name
);
5282 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5283 username
, LDAP_OBJ_POSIXACCOUNT
);
5284 TALLOC_FREE(username
);
5286 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5287 if (rc
!= LDAP_SUCCESS
) {
5288 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5289 return NT_STATUS_ACCESS_DENIED
;
5291 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5293 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5295 if (num_result
> 1) {
5296 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5297 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5300 if (num_result
== 1) {
5302 /* check if it is just a posix account.
5303 * or if there is a sid attached to this entry
5306 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5308 return NT_STATUS_UNSUCCESSFUL
;
5311 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5313 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5314 return NT_STATUS_USER_EXISTS
;
5317 /* it is just a posix account, retrieve the dn for later use */
5318 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5320 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5321 return NT_STATUS_NO_MEMORY
;
5325 if (num_result
== 0) {
5329 /* Create the basic samu structure and generate the mods for the ldap commit */
5330 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5331 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5335 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5337 user
= samu_new(tmp_ctx
);
5339 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5340 return NT_STATUS_NO_MEMORY
;
5343 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5344 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5345 return NT_STATUS_UNSUCCESSFUL
;
5347 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5348 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5349 return NT_STATUS_UNSUCCESSFUL
;
5352 if (acb_info
& ACB_NORMAL
) {
5353 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5354 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5355 return NT_STATUS_UNSUCCESSFUL
;
5358 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5359 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5360 return NT_STATUS_UNSUCCESSFUL
;
5364 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5365 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5366 return NT_STATUS_UNSUCCESSFUL
;
5370 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5371 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5372 return NT_STATUS_UNSUCCESSFUL
;
5375 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
)) {
5376 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5377 return NT_STATUS_UNSUCCESSFUL
;
5380 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5381 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5383 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5388 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5390 /* retrieve the Domain Users group gid */
5391 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5392 !sid_to_gid(&group_sid
, &gid
)) {
5393 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5394 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5397 /* lets allocate a new userid for this user */
5398 if (!winbind_allocate_uid(&uid
)) {
5399 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5400 return NT_STATUS_UNSUCCESSFUL
;
5405 /* TODO: choose a more appropriate default for machines */
5406 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5407 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5409 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5410 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5412 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5413 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5415 escape_name
= escape_rdn_val_string_alloc(name
);
5417 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5418 return NT_STATUS_NO_MEMORY
;
5422 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5424 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5427 SAFE_FREE(escape_name
);
5429 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5430 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5431 return NT_STATUS_NO_MEMORY
;
5434 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5435 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5436 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5437 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5438 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5439 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5440 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5443 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5446 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5448 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5451 if (rc
!= LDAP_SUCCESS
) {
5452 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5453 return NT_STATUS_UNSUCCESSFUL
;
5456 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5458 flush_pwnam_cache();
5460 return NT_STATUS_OK
;
5463 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5465 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5466 LDAPMessage
*result
= NULL
;
5467 LDAPMessage
*entry
= NULL
;
5473 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5475 filter
= talloc_asprintf(tmp_ctx
,
5478 "(objectClass=%s))",
5479 pdb_get_username(sam_acct
),
5480 LDAP_OBJ_POSIXACCOUNT
,
5481 LDAP_OBJ_SAMBASAMACCOUNT
);
5482 if (filter
== NULL
) {
5483 return NT_STATUS_NO_MEMORY
;
5486 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5487 if (rc
!= LDAP_SUCCESS
) {
5488 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5489 return NT_STATUS_UNSUCCESSFUL
;
5491 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5493 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5495 if (num_result
== 0) {
5496 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5497 return NT_STATUS_NO_SUCH_USER
;
5500 if (num_result
> 1) {
5501 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5502 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5505 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5507 return NT_STATUS_UNSUCCESSFUL
;
5510 /* it is just a posix account, retrieve the dn for later use */
5511 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5513 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5514 return NT_STATUS_NO_MEMORY
;
5517 /* try to remove memberships first */
5520 struct dom_sid
*sids
= NULL
;
5522 uint32_t num_groups
= 0;
5524 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5526 status
= ldapsam_enum_group_memberships(my_methods
,
5532 if (!NT_STATUS_IS_OK(status
)) {
5536 for (i
=0; i
< num_groups
; i
++) {
5540 sid_peek_rid(&sids
[i
], &group_rid
);
5542 ldapsam_del_groupmem(my_methods
,
5551 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5552 if (rc
!= LDAP_SUCCESS
) {
5553 return NT_STATUS_UNSUCCESSFUL
;
5556 flush_pwnam_cache();
5558 return NT_STATUS_OK
;
5562 * ldapsam_create_group creates a new
5563 * posixGroup and sambaGroupMapping object
5564 * in the ldap groups subtree
5566 * The gid is allocated by winbindd.
5569 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5570 TALLOC_CTX
*tmp_ctx
,
5574 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5576 LDAPMessage
*entry
= NULL
;
5577 LDAPMessage
*result
= NULL
;
5578 uint32_t num_result
;
5579 bool is_new_entry
= False
;
5580 LDAPMod
**mods
= NULL
;
5586 const char *dn
= NULL
;
5587 struct dom_sid group_sid
;
5591 groupname
= escape_ldap_string(talloc_tos(), name
);
5592 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5593 groupname
, LDAP_OBJ_POSIXGROUP
);
5594 TALLOC_FREE(groupname
);
5596 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5597 if (rc
!= LDAP_SUCCESS
) {
5598 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5599 return NT_STATUS_UNSUCCESSFUL
;
5601 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5603 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5605 if (num_result
> 1) {
5606 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5607 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5610 if (num_result
== 1) {
5612 /* check if it is just a posix group.
5613 * or if there is a sid attached to this entry
5616 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5618 return NT_STATUS_UNSUCCESSFUL
;
5621 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5623 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5624 return NT_STATUS_GROUP_EXISTS
;
5627 /* it is just a posix group, retrieve the gid and the dn for later use */
5628 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5630 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5631 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5634 gid
= strtoul(tmp
, NULL
, 10);
5636 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5638 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5639 return NT_STATUS_NO_MEMORY
;
5643 if (num_result
== 0) {
5644 is_new_entry
= true;
5647 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5648 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5652 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5654 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5656 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5658 if (!groupsidstr
|| !grouptype
) {
5659 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5660 return NT_STATUS_NO_MEMORY
;
5663 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5664 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5665 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5666 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5671 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5673 /* lets allocate a new groupid for this group */
5674 if (!winbind_allocate_gid(&gid
)) {
5675 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5676 return NT_STATUS_UNSUCCESSFUL
;
5679 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5681 escape_name
= escape_rdn_val_string_alloc(name
);
5683 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5684 return NT_STATUS_NO_MEMORY
;
5687 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5689 SAFE_FREE(escape_name
);
5691 if (!gidstr
|| !dn
) {
5692 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5693 return NT_STATUS_NO_MEMORY
;
5696 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5697 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5698 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5701 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5704 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5706 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5707 /* This call may fail with rfc2307bis schema */
5708 /* Retry adding a structural class */
5709 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5710 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5714 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5717 if (rc
!= LDAP_SUCCESS
) {
5718 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5719 return NT_STATUS_UNSUCCESSFUL
;
5722 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5724 return NT_STATUS_OK
;
5727 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5729 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5730 LDAPMessage
*result
= NULL
;
5731 LDAPMessage
*entry
= NULL
;
5736 struct dom_sid group_sid
;
5739 /* get the group sid */
5740 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5742 filter
= talloc_asprintf(tmp_ctx
,
5745 "(objectClass=%s))",
5746 sid_string_talloc(tmp_ctx
, &group_sid
),
5747 LDAP_OBJ_POSIXGROUP
,
5749 if (filter
== NULL
) {
5750 return NT_STATUS_NO_MEMORY
;
5753 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5754 if (rc
!= LDAP_SUCCESS
) {
5755 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5756 return NT_STATUS_UNSUCCESSFUL
;
5758 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5760 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5762 if (num_result
== 0) {
5763 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5764 return NT_STATUS_NO_SUCH_GROUP
;
5767 if (num_result
> 1) {
5768 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5769 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5772 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5774 return NT_STATUS_UNSUCCESSFUL
;
5777 /* here it is, retrieve the dn for later use */
5778 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5780 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5781 return NT_STATUS_NO_MEMORY
;
5784 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5786 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5787 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5790 /* check no user have this group marked as primary group */
5791 filter
= talloc_asprintf(tmp_ctx
,
5794 "(objectClass=%s))",
5796 LDAP_OBJ_POSIXACCOUNT
,
5797 LDAP_OBJ_SAMBASAMACCOUNT
);
5799 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5800 if (rc
!= LDAP_SUCCESS
) {
5801 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5802 return NT_STATUS_UNSUCCESSFUL
;
5804 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5806 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5808 if (num_result
!= 0) {
5809 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5810 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5813 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5814 if (rc
!= LDAP_SUCCESS
) {
5815 return NT_STATUS_UNSUCCESSFUL
;
5818 return NT_STATUS_OK
;
5821 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5822 TALLOC_CTX
*tmp_ctx
,
5824 uint32_t member_rid
,
5827 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5828 LDAPMessage
*entry
= NULL
;
5829 LDAPMessage
*result
= NULL
;
5830 uint32_t num_result
;
5831 LDAPMod
**mods
= NULL
;
5834 const char *dn
= NULL
;
5835 struct dom_sid group_sid
;
5836 struct dom_sid member_sid
;
5841 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5843 case LDAP_MOD_DELETE
:
5844 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5847 return NT_STATUS_UNSUCCESSFUL
;
5850 /* get member sid */
5851 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5853 /* get the group sid */
5854 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5856 filter
= talloc_asprintf(tmp_ctx
,
5859 "(objectClass=%s))",
5860 sid_string_talloc(tmp_ctx
, &member_sid
),
5861 LDAP_OBJ_POSIXACCOUNT
,
5862 LDAP_OBJ_SAMBASAMACCOUNT
);
5863 if (filter
== NULL
) {
5864 return NT_STATUS_NO_MEMORY
;
5867 /* get the member uid */
5868 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5869 if (rc
!= LDAP_SUCCESS
) {
5870 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5871 return NT_STATUS_UNSUCCESSFUL
;
5873 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5875 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5877 if (num_result
== 0) {
5878 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5879 return NT_STATUS_NO_SUCH_MEMBER
;
5882 if (num_result
> 1) {
5883 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5884 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5887 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5889 return NT_STATUS_UNSUCCESSFUL
;
5892 if (modop
== LDAP_MOD_DELETE
) {
5893 /* check if we are trying to remove the member from his primary group */
5895 gid_t user_gid
, group_gid
;
5897 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5899 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5900 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5903 user_gid
= strtoul(gidstr
, NULL
, 10);
5905 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5906 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5907 return NT_STATUS_UNSUCCESSFUL
;
5910 if (user_gid
== group_gid
) {
5911 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5912 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5916 /* here it is, retrieve the uid for later use */
5917 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5919 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5920 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5923 filter
= talloc_asprintf(tmp_ctx
,
5926 "(objectClass=%s))",
5927 sid_string_talloc(tmp_ctx
, &group_sid
),
5928 LDAP_OBJ_POSIXGROUP
,
5932 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5933 if (rc
!= LDAP_SUCCESS
) {
5934 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5935 return NT_STATUS_UNSUCCESSFUL
;
5937 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5939 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5941 if (num_result
== 0) {
5942 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5943 return NT_STATUS_NO_SUCH_GROUP
;
5946 if (num_result
> 1) {
5947 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5948 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5951 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5953 return NT_STATUS_UNSUCCESSFUL
;
5956 /* here it is, retrieve the dn for later use */
5957 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5959 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5960 return NT_STATUS_NO_MEMORY
;
5963 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5965 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5967 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5968 if (rc
!= LDAP_SUCCESS
) {
5969 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5970 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5971 return NT_STATUS_MEMBER_IN_GROUP
;
5973 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5974 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5975 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5977 return NT_STATUS_UNSUCCESSFUL
;
5980 return NT_STATUS_OK
;
5983 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5984 TALLOC_CTX
*tmp_ctx
,
5986 uint32_t member_rid
)
5988 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5990 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5991 TALLOC_CTX
*tmp_ctx
,
5993 uint32_t member_rid
)
5995 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5998 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5999 TALLOC_CTX
*mem_ctx
,
6000 struct samu
*sampass
)
6002 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6003 LDAPMessage
*entry
= NULL
;
6004 LDAPMessage
*result
= NULL
;
6005 uint32_t num_result
;
6006 LDAPMod
**mods
= NULL
;
6008 char *escape_username
;
6010 const char *dn
= NULL
;
6014 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6016 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6017 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6018 return NT_STATUS_UNSUCCESSFUL
;
6020 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6022 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6023 return NT_STATUS_NO_MEMORY
;
6026 escape_username
= escape_ldap_string(talloc_tos(),
6027 pdb_get_username(sampass
));
6028 if (escape_username
== NULL
) {
6029 return NT_STATUS_NO_MEMORY
;
6032 filter
= talloc_asprintf(mem_ctx
,
6035 "(objectClass=%s))",
6037 LDAP_OBJ_POSIXACCOUNT
,
6038 LDAP_OBJ_SAMBASAMACCOUNT
);
6040 TALLOC_FREE(escape_username
);
6042 if (filter
== NULL
) {
6043 return NT_STATUS_NO_MEMORY
;
6046 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6047 if (rc
!= LDAP_SUCCESS
) {
6048 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6049 return NT_STATUS_UNSUCCESSFUL
;
6051 talloc_autofree_ldapmsg(mem_ctx
, result
);
6053 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6055 if (num_result
== 0) {
6056 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6057 return NT_STATUS_NO_SUCH_USER
;
6060 if (num_result
> 1) {
6061 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6062 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6065 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6067 return NT_STATUS_UNSUCCESSFUL
;
6070 /* retrieve the dn for later use */
6071 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6073 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6074 return NT_STATUS_NO_MEMORY
;
6077 /* remove the old one, and add the new one, this way we do not risk races */
6078 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6081 return NT_STATUS_OK
;
6084 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6086 if (rc
!= LDAP_SUCCESS
) {
6087 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6088 pdb_get_username(sampass
), gidstr
));
6089 return NT_STATUS_UNSUCCESSFUL
;
6092 flush_pwnam_cache();
6094 return NT_STATUS_OK
;
6098 /**********************************************************************
6099 trusted domains functions
6100 *********************************************************************/
6102 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6105 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6106 ldap_state
->domain_dn
);
6109 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6110 TALLOC_CTX
*mem_ctx
,
6111 const char *domain
, LDAPMessage
**entry
)
6115 int scope
= LDAP_SCOPE_SUBTREE
;
6116 const char **attrs
= NULL
; /* NULL: get all attrs */
6117 int attrsonly
= 0; /* 0: return values too */
6118 LDAPMessage
*result
= NULL
;
6120 uint32_t num_result
;
6122 filter
= talloc_asprintf(talloc_tos(),
6123 "(&(objectClass=%s)(sambaDomainName=%s))",
6124 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6126 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6127 if (trusted_dn
== NULL
) {
6130 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6131 filter
, attrs
, attrsonly
, &result
);
6133 if (result
!= NULL
) {
6134 talloc_autofree_ldapmsg(mem_ctx
, result
);
6137 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6142 if (rc
!= LDAP_SUCCESS
) {
6146 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6148 if (num_result
> 1) {
6149 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6150 "%s object for domain '%s'?!\n",
6151 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6155 if (num_result
== 0) {
6156 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6157 "%s object for domain %s.\n",
6158 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6161 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6167 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6170 struct dom_sid
*sid
,
6171 time_t *pass_last_set_time
)
6173 struct ldapsam_privates
*ldap_state
=
6174 (struct ldapsam_privates
*)methods
->private_data
;
6175 LDAPMessage
*entry
= NULL
;
6177 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6179 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6188 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6189 entry
, "sambaClearTextPassword", talloc_tos());
6190 if (pwd_str
== NULL
) {
6193 /* trusteddom_pw routines do not use talloc yet... */
6194 *pwd
= SMB_STRDUP(pwd_str
);
6200 /* last change time */
6201 if (pass_last_set_time
!= NULL
) {
6203 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6204 entry
, "sambaPwdLastSet", talloc_tos());
6205 if (time_str
== NULL
) {
6208 *pass_last_set_time
= (time_t)atol(time_str
);
6214 struct dom_sid dom_sid
;
6215 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6218 if (sid_str
== NULL
) {
6221 if (!string_to_sid(&dom_sid
, sid_str
)) {
6224 sid_copy(sid
, &dom_sid
);
6230 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6233 const struct dom_sid
*sid
)
6235 struct ldapsam_privates
*ldap_state
=
6236 (struct ldapsam_privates
*)methods
->private_data
;
6237 LDAPMessage
*entry
= NULL
;
6238 LDAPMod
**mods
= NULL
;
6239 char *prev_pwd
= NULL
;
6240 char *trusted_dn
= NULL
;
6243 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6246 * get the current entry (if there is one) in order to put the
6247 * current password into the previous password attribute
6249 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6254 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6255 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6256 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6258 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6259 sid_string_tos(sid
));
6260 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6261 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6262 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6263 "sambaClearTextPassword", pwd
);
6265 if (entry
!= NULL
) {
6266 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6267 entry
, "sambaClearTextPassword", talloc_tos());
6268 if (prev_pwd
!= NULL
) {
6269 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6270 "sambaPreviousClearTextPassword",
6275 talloc_autofree_ldapmod(talloc_tos(), mods
);
6277 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6278 if (trusted_dn
== NULL
) {
6281 if (entry
== NULL
) {
6282 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6284 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6287 if (rc
!= LDAP_SUCCESS
) {
6288 DEBUG(1, ("error writing trusted domain password!\n"));
6295 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6299 struct ldapsam_privates
*ldap_state
=
6300 (struct ldapsam_privates
*)methods
->private_data
;
6301 LDAPMessage
*entry
= NULL
;
6302 const char *trusted_dn
;
6304 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6308 if (entry
== NULL
) {
6309 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6314 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6316 if (trusted_dn
== NULL
) {
6317 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6321 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6322 if (rc
!= LDAP_SUCCESS
) {
6329 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6330 TALLOC_CTX
*mem_ctx
,
6331 uint32_t *num_domains
,
6332 struct trustdom_info
***domains
)
6335 struct ldapsam_privates
*ldap_state
=
6336 (struct ldapsam_privates
*)methods
->private_data
;
6338 int scope
= LDAP_SCOPE_SUBTREE
;
6339 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6340 int attrsonly
= 0; /* 0: return values too */
6341 LDAPMessage
*result
= NULL
;
6342 LDAPMessage
*entry
= NULL
;
6344 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6345 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6347 rc
= smbldap_search(ldap_state
->smbldap_state
,
6348 ldap_state
->domain_dn
,
6355 if (result
!= NULL
) {
6356 talloc_autofree_ldapmsg(mem_ctx
, result
);
6359 if (rc
!= LDAP_SUCCESS
) {
6360 return NT_STATUS_UNSUCCESSFUL
;
6364 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6365 DEBUG(1, ("talloc failed\n"));
6366 return NT_STATUS_NO_MEMORY
;
6369 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6371 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6373 char *dom_name
, *dom_sid_str
;
6374 struct trustdom_info
*dom_info
;
6376 dom_info
= talloc(*domains
, struct trustdom_info
);
6377 if (dom_info
== NULL
) {
6378 DEBUG(1, ("talloc failed\n"));
6379 return NT_STATUS_NO_MEMORY
;
6382 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6386 if (dom_name
== NULL
) {
6387 DEBUG(1, ("talloc failed\n"));
6388 return NT_STATUS_NO_MEMORY
;
6390 dom_info
->name
= dom_name
;
6392 dom_sid_str
= smbldap_talloc_single_attribute(
6393 priv2ld(ldap_state
), entry
, "sambaSID",
6395 if (dom_sid_str
== NULL
) {
6396 DEBUG(1, ("talloc failed\n"));
6397 return NT_STATUS_NO_MEMORY
;
6399 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6400 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6402 return NT_STATUS_UNSUCCESSFUL
;
6405 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6406 domains
, num_domains
);
6408 if (*domains
== NULL
) {
6409 DEBUG(1, ("talloc failed\n"));
6410 return NT_STATUS_NO_MEMORY
;
6414 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6415 return NT_STATUS_OK
;
6419 /**********************************************************************
6421 *********************************************************************/
6423 static void free_private_data(void **vp
)
6425 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6427 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6429 if ((*ldap_state
)->result
!= NULL
) {
6430 ldap_msgfree((*ldap_state
)->result
);
6431 (*ldap_state
)->result
= NULL
;
6433 if ((*ldap_state
)->domain_dn
!= NULL
) {
6434 SAFE_FREE((*ldap_state
)->domain_dn
);
6439 /* No need to free any further, as it is talloc()ed */
6442 /*********************************************************************
6443 Intitalise the parts of the pdb_methods structure that are common to
6445 *********************************************************************/
6447 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6450 struct ldapsam_privates
*ldap_state
;
6451 char *bind_dn
= NULL
;
6452 char *bind_secret
= NULL
;
6454 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6458 (*pdb_method
)->name
= "ldapsam";
6460 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6461 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6462 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6463 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6464 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6465 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6467 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6468 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6469 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6470 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6471 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6472 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6473 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6475 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6476 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6478 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6480 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6481 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6483 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6484 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6485 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6486 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6488 /* TODO: Setup private data and free */
6490 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6491 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6492 return NT_STATUS_NO_MEMORY
;
6495 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6496 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6497 return NT_STATUS_NO_MEMORY
;
6500 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6501 location
, false, bind_dn
, bind_secret
,
6502 &ldap_state
->smbldap_state
);
6503 memset(bind_secret
, '\0', strlen(bind_secret
));
6504 SAFE_FREE(bind_secret
);
6506 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6510 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6511 return NT_STATUS_NO_MEMORY
;
6514 (*pdb_method
)->private_data
= ldap_state
;
6516 (*pdb_method
)->free_private_data
= free_private_data
;
6518 return NT_STATUS_OK
;
6521 /**********************************************************************
6522 Initialise the 'compat' mode for pdb_ldap
6523 *********************************************************************/
6525 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6528 struct ldapsam_privates
*ldap_state
;
6529 char *uri
= talloc_strdup( NULL
, location
);
6531 trim_char( uri
, '\"', '\"' );
6532 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6536 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6540 (*pdb_method
)->name
= "ldapsam_compat";
6542 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6543 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6545 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6547 return NT_STATUS_OK
;
6550 /**********************************************************************
6551 Initialise the normal mode for pdb_ldap
6552 *********************************************************************/
6554 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6557 struct ldapsam_privates
*ldap_state
= NULL
;
6558 uint32_t alg_rid_base
;
6559 char *alg_rid_base_string
= NULL
;
6560 LDAPMessage
*result
= NULL
;
6561 LDAPMessage
*entry
= NULL
;
6562 struct dom_sid ldap_domain_sid
;
6563 struct dom_sid secrets_domain_sid
;
6564 char *domain_sid_string
= NULL
;
6566 char *uri
= talloc_strdup( NULL
, location
);
6568 trim_char( uri
, '\"', '\"' );
6569 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6573 if (!NT_STATUS_IS_OK(nt_status
)) {
6577 (*pdb_method
)->name
= "ldapsam";
6579 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6580 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6581 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6582 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6583 (*pdb_method
)->search_users
= ldapsam_search_users
;
6584 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6585 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6587 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6588 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6589 (*pdb_method
)->enum_group_memberships
=
6590 ldapsam_enum_group_memberships
;
6591 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6592 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6593 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6594 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6596 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6597 (*pdb_method
)->create_user
= ldapsam_create_user
;
6598 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6599 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6600 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6601 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6602 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6603 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6607 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6608 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6610 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6612 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6614 ldap_state
->domain_name
, True
);
6616 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6617 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6618 "info, nor add one to the domain. "
6619 "We cannot work reliably without it.\n"));
6620 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6623 /* Given that the above might fail, everything below this must be
6626 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6629 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6631 ldap_msgfree(result
);
6632 return NT_STATUS_UNSUCCESSFUL
;
6635 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6637 ldap_msgfree(result
);
6638 return NT_STATUS_UNSUCCESSFUL
;
6641 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6644 domain_sid_string
= smbldap_talloc_single_attribute(
6645 ldap_state
->smbldap_state
->ldap_struct
,
6647 get_userattr_key2string(ldap_state
->schema_ver
,
6648 LDAP_ATTR_USER_SID
),
6651 if (domain_sid_string
) {
6653 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6654 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6655 "read as a valid SID\n", domain_sid_string
));
6656 ldap_msgfree(result
);
6657 TALLOC_FREE(domain_sid_string
);
6658 return NT_STATUS_INVALID_PARAMETER
;
6660 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6661 &secrets_domain_sid
);
6662 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6663 &ldap_domain_sid
)) {
6664 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6665 "%s based on pdb_ldap results %s -> %s\n",
6666 ldap_state
->domain_name
,
6667 sid_string_dbg(&secrets_domain_sid
),
6668 sid_string_dbg(&ldap_domain_sid
)));
6670 /* reset secrets.tdb sid */
6671 secrets_store_domain_sid(ldap_state
->domain_name
,
6673 DEBUG(1, ("New global sam SID: %s\n",
6674 sid_string_dbg(get_global_sam_sid())));
6676 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6677 TALLOC_FREE(domain_sid_string
);
6680 alg_rid_base_string
= smbldap_talloc_single_attribute(
6681 ldap_state
->smbldap_state
->ldap_struct
,
6683 get_attr_key2string( dominfo_attr_list
,
6684 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6686 if (alg_rid_base_string
) {
6687 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6688 if (alg_rid_base
!= algorithmic_rid_base()) {
6689 DEBUG(0, ("The value of 'algorithmic RID base' has "
6690 "changed since the LDAP\n"
6691 "database was initialised. Aborting. \n"));
6692 ldap_msgfree(result
);
6693 TALLOC_FREE(alg_rid_base_string
);
6694 return NT_STATUS_UNSUCCESSFUL
;
6696 TALLOC_FREE(alg_rid_base_string
);
6698 ldap_msgfree(result
);
6700 return NT_STATUS_OK
;
6703 NTSTATUS
pdb_ldap_init(void)
6706 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6709 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6712 /* Let pdb_nds register backends */
6717 return NT_STATUS_OK
;