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
)));
3033 *p_num_groups
= num_sids
;
3039 TALLOC_FREE(escape_name
);
3043 /**********************************************************************
3044 * Augment a posixGroup object with a sambaGroupMapping domgroup
3045 *********************************************************************/
3047 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3048 struct ldapsam_privates
*ldap_state
,
3051 const char *filter
, *dn
;
3052 LDAPMessage
*msg
, *entry
;
3056 filter
= talloc_asprintf(mem_ctx
,
3057 "(&(objectClass=%s)(gidNumber=%u))",
3058 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3059 if (filter
== NULL
) {
3060 return NT_STATUS_NO_MEMORY
;
3063 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3064 get_attr_list(mem_ctx
, groupmap_attr_list
),
3066 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3068 if ((rc
!= LDAP_SUCCESS
) ||
3069 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3070 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3071 return NT_STATUS_NO_SUCH_GROUP
;
3074 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3076 return NT_STATUS_NO_MEMORY
;
3080 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3082 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3083 sid_string_talloc(mem_ctx
, &map
->sid
));
3084 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3085 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3086 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3088 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3090 talloc_autofree_ldapmod(mem_ctx
, mods
);
3092 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3093 if (rc
!= LDAP_SUCCESS
) {
3094 return NT_STATUS_ACCESS_DENIED
;
3097 return NT_STATUS_OK
;
3100 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3103 struct ldapsam_privates
*ldap_state
=
3104 (struct ldapsam_privates
*)methods
->private_data
;
3105 LDAPMessage
*msg
= NULL
;
3106 LDAPMod
**mods
= NULL
;
3107 const char *attrs
[] = { NULL
};
3111 TALLOC_CTX
*mem_ctx
;
3118 mem_ctx
= talloc_new(NULL
);
3119 if (mem_ctx
== NULL
) {
3120 DEBUG(0, ("talloc_new failed\n"));
3121 return NT_STATUS_NO_MEMORY
;
3124 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3125 sid_string_talloc(mem_ctx
, &map
->sid
));
3126 if (filter
== NULL
) {
3127 result
= NT_STATUS_NO_MEMORY
;
3131 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3132 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3133 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3135 if ((rc
== LDAP_SUCCESS
) &&
3136 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3138 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3139 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3140 result
= NT_STATUS_GROUP_EXISTS
;
3144 switch (map
->sid_name_use
) {
3146 case SID_NAME_DOM_GRP
:
3147 /* To map a domain group we need to have a posix group
3149 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3153 case SID_NAME_ALIAS
:
3154 if (!sid_check_is_in_our_domain(&map
->sid
)
3155 && !sid_check_is_in_builtin(&map
->sid
) )
3157 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3158 sid_string_dbg(&map
->sid
)));
3159 result
= NT_STATUS_INVALID_PARAMETER
;
3165 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3166 sid_type_lookup(map
->sid_name_use
)));
3167 result
= NT_STATUS_INVALID_PARAMETER
;
3171 /* Domain groups have been mapped in a separate routine, we have to
3172 * create an alias now */
3174 if (map
->gid
== -1) {
3175 DEBUG(10, ("Refusing to map gid==-1\n"));
3176 result
= NT_STATUS_INVALID_PARAMETER
;
3180 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3181 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3182 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3183 result
= NT_STATUS_GROUP_EXISTS
;
3187 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3188 * the best we can get out of LDAP. */
3190 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3191 sid_string_talloc(mem_ctx
, &map
->sid
),
3192 lp_ldap_group_suffix());
3194 result
= NT_STATUS_NO_MEMORY
;
3200 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3201 LDAP_OBJ_SID_ENTRY
);
3202 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3204 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3205 sid_string_talloc(mem_ctx
, &map
->sid
));
3206 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3207 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3208 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3210 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3212 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3213 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3214 talloc_autofree_ldapmod(mem_ctx
, mods
);
3216 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3218 result
= (rc
== LDAP_SUCCESS
) ?
3219 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3222 TALLOC_FREE(mem_ctx
);
3226 /**********************************************************************
3227 * Update a group mapping entry. We're quite strict about what can be changed:
3228 * Only the description and displayname may be changed. It simply does not
3229 * make any sense to change the SID, gid or the type in a mapping.
3230 *********************************************************************/
3232 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3235 struct ldapsam_privates
*ldap_state
=
3236 (struct ldapsam_privates
*)methods
->private_data
;
3238 const char *filter
, *dn
;
3239 LDAPMessage
*msg
= NULL
;
3240 LDAPMessage
*entry
= NULL
;
3241 LDAPMod
**mods
= NULL
;
3242 TALLOC_CTX
*mem_ctx
;
3245 mem_ctx
= talloc_new(NULL
);
3246 if (mem_ctx
== NULL
) {
3247 DEBUG(0, ("talloc_new failed\n"));
3248 return NT_STATUS_NO_MEMORY
;
3251 /* Make 100% sure that sid, gid and type are not changed by looking up
3252 * exactly the values we're given in LDAP. */
3254 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3255 "(sambaSid=%s)(gidNumber=%u)"
3256 "(sambaGroupType=%d))",
3258 sid_string_talloc(mem_ctx
, &map
->sid
),
3259 (unsigned int)map
->gid
, map
->sid_name_use
);
3260 if (filter
== NULL
) {
3261 result
= NT_STATUS_NO_MEMORY
;
3265 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3266 get_attr_list(mem_ctx
, groupmap_attr_list
),
3268 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3270 if ((rc
!= LDAP_SUCCESS
) ||
3271 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3272 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3273 result
= NT_STATUS_NO_SUCH_GROUP
;
3277 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3280 result
= NT_STATUS_NO_MEMORY
;
3285 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3287 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3289 talloc_autofree_ldapmod(mem_ctx
, mods
);
3292 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3293 "nothing to do\n"));
3294 result
= NT_STATUS_OK
;
3298 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3300 if (rc
!= LDAP_SUCCESS
) {
3301 result
= NT_STATUS_ACCESS_DENIED
;
3305 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3306 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3308 result
= NT_STATUS_OK
;
3311 TALLOC_FREE(mem_ctx
);
3315 /**********************************************************************
3316 *********************************************************************/
3318 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3321 struct ldapsam_privates
*priv
=
3322 (struct ldapsam_privates
*)methods
->private_data
;
3323 LDAPMessage
*msg
, *entry
;
3326 TALLOC_CTX
*mem_ctx
;
3329 mem_ctx
= talloc_new(NULL
);
3330 if (mem_ctx
== NULL
) {
3331 DEBUG(0, ("talloc_new failed\n"));
3332 return NT_STATUS_NO_MEMORY
;
3335 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3336 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3337 sid_string_talloc(mem_ctx
, &sid
));
3338 if (filter
== NULL
) {
3339 result
= NT_STATUS_NO_MEMORY
;
3342 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3343 get_attr_list(mem_ctx
, groupmap_attr_list
),
3345 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3347 if ((rc
!= LDAP_SUCCESS
) ||
3348 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3349 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3350 result
= NT_STATUS_NO_SUCH_GROUP
;
3354 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3355 get_attr_list(mem_ctx
,
3356 groupmap_attr_list_to_delete
));
3358 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3359 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3360 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3361 const char *attrs
[] = { "sambaGroupType", "description",
3362 "displayName", "sambaSIDList",
3365 /* Second try. Don't delete the sambaSID attribute, this is
3366 for "old" entries that are tacked on a winbind
3369 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3370 LDAP_OBJ_GROUPMAP
, attrs
);
3373 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3374 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3375 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3376 const char *attrs
[] = { "sambaGroupType", "description",
3377 "displayName", "sambaSIDList",
3378 "gidNumber", NULL
};
3380 /* Third try. This is a post-3.0.21 alias (containing only
3381 * sambaSidEntry and sambaGroupMapping classes), we also have
3382 * to delete the gidNumber attribute, only the sambaSidEntry
3385 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3386 LDAP_OBJ_GROUPMAP
, attrs
);
3389 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3392 TALLOC_FREE(mem_ctx
);
3396 /**********************************************************************
3397 *********************************************************************/
3399 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3402 struct ldapsam_privates
*ldap_state
=
3403 (struct ldapsam_privates
*)my_methods
->private_data
;
3404 char *filter
= NULL
;
3406 const char **attr_list
;
3408 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3410 return NT_STATUS_NO_MEMORY
;
3412 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3413 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3414 LDAP_SCOPE_SUBTREE
, filter
,
3415 attr_list
, 0, &ldap_state
->result
);
3416 TALLOC_FREE(attr_list
);
3418 if (rc
!= LDAP_SUCCESS
) {
3419 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3420 ldap_err2string(rc
)));
3421 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3422 lp_ldap_suffix(), filter
));
3423 ldap_msgfree(ldap_state
->result
);
3424 ldap_state
->result
= NULL
;
3425 TALLOC_FREE(filter
);
3426 return NT_STATUS_UNSUCCESSFUL
;
3429 TALLOC_FREE(filter
);
3431 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3432 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3433 ldap_state
->result
)));
3436 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3437 ldap_state
->result
);
3438 ldap_state
->index
= 0;
3440 return NT_STATUS_OK
;
3443 /**********************************************************************
3444 *********************************************************************/
3446 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3448 ldapsam_endsampwent(my_methods
);
3451 /**********************************************************************
3452 *********************************************************************/
3454 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3457 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3458 struct ldapsam_privates
*ldap_state
=
3459 (struct ldapsam_privates
*)my_methods
->private_data
;
3463 if (!ldap_state
->entry
)
3466 ldap_state
->index
++;
3467 bret
= init_group_from_ldap(ldap_state
, map
,
3471 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3475 return NT_STATUS_OK
;
3478 /**********************************************************************
3479 *********************************************************************/
3481 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3482 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3483 GROUP_MAP
***pp_rmap
,
3484 size_t *p_num_entries
,
3487 GROUP_MAP
*map
= NULL
;
3493 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3494 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3496 return NT_STATUS_ACCESS_DENIED
;
3501 map
= talloc_zero(NULL
, GROUP_MAP
);
3503 return NT_STATUS_NO_MEMORY
;
3506 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3511 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3512 sid_name_use
!= map
->sid_name_use
) {
3513 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3514 "not of the requested type\n",
3518 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3519 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3520 "non mapped\n", map
->nt_name
));
3524 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3525 GROUP_MAP
*, entries
+ 1);
3527 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3528 "enlarge group map!\n"));
3529 return NT_STATUS_UNSUCCESSFUL
;
3532 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3537 ldapsam_endsamgrent(methods
);
3539 *p_num_entries
= entries
;
3541 return NT_STATUS_OK
;
3544 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3545 const struct dom_sid
*alias
,
3546 const struct dom_sid
*member
,
3549 struct ldapsam_privates
*ldap_state
=
3550 (struct ldapsam_privates
*)methods
->private_data
;
3552 LDAPMessage
*result
= NULL
;
3553 LDAPMessage
*entry
= NULL
;
3555 LDAPMod
**mods
= NULL
;
3557 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3560 char *filter
= NULL
;
3562 if (sid_check_is_in_builtin(alias
)) {
3563 type
= SID_NAME_ALIAS
;
3566 if (sid_check_is_in_our_domain(alias
)) {
3567 type
= SID_NAME_ALIAS
;
3570 if (type
== SID_NAME_USE_NONE
) {
3571 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3572 sid_string_dbg(alias
)));
3573 return NT_STATUS_NO_SUCH_ALIAS
;
3576 if (asprintf(&filter
,
3577 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3578 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3580 return NT_STATUS_NO_MEMORY
;
3583 if (ldapsam_search_one_group(ldap_state
, filter
,
3584 &result
) != LDAP_SUCCESS
) {
3586 return NT_STATUS_NO_SUCH_ALIAS
;
3589 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3593 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3594 ldap_msgfree(result
);
3596 return NT_STATUS_NO_SUCH_ALIAS
;
3600 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3601 "filter %s: count=%d\n", filter
, count
));
3602 ldap_msgfree(result
);
3604 return NT_STATUS_NO_SUCH_ALIAS
;
3609 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3613 ldap_msgfree(result
);
3614 return NT_STATUS_UNSUCCESSFUL
;
3617 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3619 ldap_msgfree(result
);
3620 return NT_STATUS_UNSUCCESSFUL
;
3623 smbldap_set_mod(&mods
, modop
,
3624 get_attr_key2string(groupmap_attr_list
,
3625 LDAP_ATTR_SID_LIST
),
3626 sid_to_fstring(tmp
, member
));
3628 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3630 ldap_mods_free(mods
, True
);
3631 ldap_msgfree(result
);
3634 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3635 return NT_STATUS_MEMBER_IN_ALIAS
;
3638 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3639 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3642 if (rc
!= LDAP_SUCCESS
) {
3643 return NT_STATUS_UNSUCCESSFUL
;
3646 return NT_STATUS_OK
;
3649 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3650 const struct dom_sid
*alias
,
3651 const struct dom_sid
*member
)
3653 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3656 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3657 const struct dom_sid
*alias
,
3658 const struct dom_sid
*member
)
3660 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3664 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3665 const struct dom_sid
*alias
,
3666 TALLOC_CTX
*mem_ctx
,
3667 struct dom_sid
**pp_members
,
3668 size_t *p_num_members
)
3670 struct ldapsam_privates
*ldap_state
=
3671 (struct ldapsam_privates
*)methods
->private_data
;
3672 LDAPMessage
*result
= NULL
;
3673 LDAPMessage
*entry
= NULL
;
3675 char **values
= NULL
;
3677 char *filter
= NULL
;
3678 uint32_t num_members
= 0;
3679 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3685 if (sid_check_is_in_builtin(alias
)) {
3686 type
= SID_NAME_ALIAS
;
3689 if (sid_check_is_in_our_domain(alias
)) {
3690 type
= SID_NAME_ALIAS
;
3693 if (type
== SID_NAME_USE_NONE
) {
3694 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3695 sid_string_dbg(alias
)));
3696 return NT_STATUS_NO_SUCH_ALIAS
;
3699 if (asprintf(&filter
,
3700 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3701 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3703 return NT_STATUS_NO_MEMORY
;
3706 if (ldapsam_search_one_group(ldap_state
, filter
,
3707 &result
) != LDAP_SUCCESS
) {
3709 return NT_STATUS_NO_SUCH_ALIAS
;
3712 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3716 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3717 ldap_msgfree(result
);
3719 return NT_STATUS_NO_SUCH_ALIAS
;
3723 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3724 "filter %s: count=%d\n", filter
, count
));
3725 ldap_msgfree(result
);
3727 return NT_STATUS_NO_SUCH_ALIAS
;
3732 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3736 ldap_msgfree(result
);
3737 return NT_STATUS_UNSUCCESSFUL
;
3740 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3742 get_attr_key2string(groupmap_attr_list
,
3743 LDAP_ATTR_SID_LIST
));
3745 if (values
== NULL
) {
3746 ldap_msgfree(result
);
3747 return NT_STATUS_OK
;
3750 count
= ldap_count_values(values
);
3752 for (i
=0; i
<count
; i
++) {
3753 struct dom_sid member
;
3756 if (!string_to_sid(&member
, values
[i
]))
3759 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3761 if (!NT_STATUS_IS_OK(status
)) {
3762 ldap_value_free(values
);
3763 ldap_msgfree(result
);
3768 *p_num_members
= num_members
;
3769 ldap_value_free(values
);
3770 ldap_msgfree(result
);
3772 return NT_STATUS_OK
;
3775 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3776 TALLOC_CTX
*mem_ctx
,
3777 const struct dom_sid
*domain_sid
,
3778 const struct dom_sid
*members
,
3780 uint32_t **pp_alias_rids
,
3781 size_t *p_num_alias_rids
)
3783 struct ldapsam_privates
*ldap_state
=
3784 (struct ldapsam_privates
*)methods
->private_data
;
3787 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3789 LDAPMessage
*result
= NULL
;
3790 LDAPMessage
*entry
= NULL
;
3794 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3795 bool is_builtin
= false;
3796 bool sid_added
= false;
3798 *pp_alias_rids
= NULL
;
3799 *p_num_alias_rids
= 0;
3801 if (sid_check_is_builtin(domain_sid
)) {
3803 type
= SID_NAME_ALIAS
;
3806 if (sid_check_is_domain(domain_sid
)) {
3807 type
= SID_NAME_ALIAS
;
3810 if (type
== SID_NAME_USE_NONE
) {
3811 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3812 sid_string_dbg(domain_sid
)));
3813 return NT_STATUS_UNSUCCESSFUL
;
3816 if (num_members
== 0) {
3817 return NT_STATUS_OK
;
3820 filter
= talloc_asprintf(mem_ctx
,
3821 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3822 LDAP_OBJ_GROUPMAP
, type
);
3824 for (i
=0; i
<num_members
; i
++)
3825 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3827 sid_string_talloc(mem_ctx
,
3830 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3832 if (filter
== NULL
) {
3833 return NT_STATUS_NO_MEMORY
;
3837 ldap_state
->search_cache
.filter
&&
3838 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3839 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3840 result
= ldap_state
->search_cache
.result
;
3841 ldap_state
->search_cache
.result
= NULL
;
3843 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3844 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3845 if (rc
!= LDAP_SUCCESS
) {
3846 return NT_STATUS_UNSUCCESSFUL
;
3848 talloc_autofree_ldapmsg(filter
, result
);
3851 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3853 for (entry
= ldap_first_entry(ldap_struct
, result
);
3855 entry
= ldap_next_entry(ldap_struct
, entry
))
3861 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3867 if (!string_to_sid(&sid
, sid_str
))
3870 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3875 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3876 p_num_alias_rids
)) {
3877 return NT_STATUS_NO_MEMORY
;
3881 if (!is_builtin
&& !sid_added
) {
3882 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3884 * Note: result is a talloc child of filter because of the
3885 * talloc_autofree_ldapmsg() usage
3887 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3888 ldap_state
->search_cache
.result
= result
;
3891 return NT_STATUS_OK
;
3894 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3895 enum pdb_policy_type type
,
3898 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3900 LDAPMod
**mods
= NULL
;
3901 fstring value_string
;
3902 const char *policy_attr
= NULL
;
3904 struct ldapsam_privates
*ldap_state
=
3905 (struct ldapsam_privates
*)methods
->private_data
;
3907 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3909 if (!ldap_state
->domain_dn
) {
3910 return NT_STATUS_INVALID_PARAMETER
;
3913 policy_attr
= get_account_policy_attr(type
);
3914 if (policy_attr
== NULL
) {
3915 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3920 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3922 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3924 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3927 ldap_mods_free(mods
, True
);
3929 if (rc
!= LDAP_SUCCESS
) {
3933 if (!cache_account_policy_set(type
, value
)) {
3934 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3935 "update local tdb cache\n"));
3939 return NT_STATUS_OK
;
3942 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3943 enum pdb_policy_type type
,
3946 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3950 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3951 enum pdb_policy_type type
,
3954 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3955 LDAPMessage
*result
= NULL
;
3956 LDAPMessage
*entry
= NULL
;
3961 const char *policy_attr
= NULL
;
3963 struct ldapsam_privates
*ldap_state
=
3964 (struct ldapsam_privates
*)methods
->private_data
;
3966 const char *attrs
[2];
3968 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3970 if (!ldap_state
->domain_dn
) {
3971 return NT_STATUS_INVALID_PARAMETER
;
3974 policy_attr
= get_account_policy_attr(type
);
3976 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3977 "policy index: %d\n", type
));
3981 attrs
[0] = policy_attr
;
3984 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3985 if (filter
== NULL
) {
3986 return NT_STATUS_NO_MEMORY
;
3988 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3989 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
3991 TALLOC_FREE(filter
);
3992 if (rc
!= LDAP_SUCCESS
) {
3996 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4001 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4002 if (entry
== NULL
) {
4006 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4011 *value
= (uint32_t)atol(vals
[0]);
4013 ntstatus
= NT_STATUS_OK
;
4017 ldap_value_free(vals
);
4018 ldap_msgfree(result
);
4023 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4025 - if user hasn't decided to use account policies inside LDAP just reuse the
4028 - if there is a valid cache entry, return that
4029 - if there is an LDAP entry, update cache and return
4030 - otherwise set to default, update cache and return
4034 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4035 enum pdb_policy_type type
,
4038 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4040 if (cache_account_policy_get(type
, value
)) {
4041 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4043 return NT_STATUS_OK
;
4046 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4048 if (NT_STATUS_IS_OK(ntstatus
)) {
4052 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4056 /* should we automagically migrate old tdb value here ? */
4057 if (account_policy_get(type
, value
))
4060 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4061 "default\n", type
));
4064 if (!account_policy_get_default(type
, value
)) {
4070 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4071 if (!NT_STATUS_IS_OK(ntstatus
)) {
4077 if (!cache_account_policy_set(type
, *value
)) {
4078 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4079 "tdb as a cache\n"));
4080 return NT_STATUS_UNSUCCESSFUL
;
4083 return NT_STATUS_OK
;
4086 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4087 const struct dom_sid
*domain_sid
,
4091 enum lsa_SidType
*attrs
)
4093 struct ldapsam_privates
*ldap_state
=
4094 (struct ldapsam_privates
*)methods
->private_data
;
4095 LDAPMessage
*msg
= NULL
;
4097 char *allsids
= NULL
;
4098 int i
, rc
, num_mapped
;
4099 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4100 TALLOC_CTX
*mem_ctx
;
4104 mem_ctx
= talloc_new(NULL
);
4105 if (mem_ctx
== NULL
) {
4106 DEBUG(0, ("talloc_new failed\n"));
4110 if (!sid_check_is_builtin(domain_sid
) &&
4111 !sid_check_is_domain(domain_sid
)) {
4112 result
= NT_STATUS_INVALID_PARAMETER
;
4116 if (num_rids
== 0) {
4117 result
= NT_STATUS_NONE_MAPPED
;
4121 for (i
=0; i
<num_rids
; i
++)
4122 attrs
[i
] = SID_NAME_UNKNOWN
;
4124 allsids
= talloc_strdup(mem_ctx
, "");
4125 if (allsids
== NULL
) {
4129 for (i
=0; i
<num_rids
; i
++) {
4131 sid_compose(&sid
, domain_sid
, rids
[i
]);
4132 allsids
= talloc_asprintf_append_buffer(
4133 allsids
, "(sambaSid=%s)",
4134 sid_string_talloc(mem_ctx
, &sid
));
4135 if (allsids
== NULL
) {
4140 /* First look for users */
4144 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4146 filter
= talloc_asprintf(
4147 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4148 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4150 if (filter
== NULL
) {
4154 rc
= smbldap_search(ldap_state
->smbldap_state
,
4155 lp_ldap_user_suffix(),
4156 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4158 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4161 if (rc
!= LDAP_SUCCESS
)
4164 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4167 for (entry
= ldap_first_entry(ld
, msg
);
4169 entry
= ldap_next_entry(ld
, entry
)) {
4174 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4176 DEBUG(2, ("Could not find sid from ldap entry\n"));
4180 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4183 DEBUG(2, ("Could not retrieve uid attribute\n"));
4187 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4188 if (rid
== rids
[rid_index
])
4192 if (rid_index
== num_rids
) {
4193 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4197 attrs
[rid_index
] = SID_NAME_USER
;
4198 names
[rid_index
] = name
;
4202 if (num_mapped
== num_rids
) {
4203 /* No need to look for groups anymore -- we're done */
4204 result
= NT_STATUS_OK
;
4208 /* Same game for groups */
4212 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4213 "sambaGroupType", NULL
};
4215 filter
= talloc_asprintf(
4216 mem_ctx
, "(&(objectClass=%s)(|%s))",
4217 LDAP_OBJ_GROUPMAP
, allsids
);
4218 if (filter
== NULL
) {
4222 rc
= smbldap_search(ldap_state
->smbldap_state
,
4224 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4226 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4229 if (rc
!= LDAP_SUCCESS
)
4232 /* ldap_struct might have changed due to a reconnect */
4234 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4236 /* For consistency checks, we already checked we're only domain or builtin */
4238 is_builtin
= sid_check_is_builtin(domain_sid
);
4240 for (entry
= ldap_first_entry(ld
, msg
);
4242 entry
= ldap_next_entry(ld
, entry
))
4247 enum lsa_SidType type
;
4248 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4250 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4253 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4258 type
= (enum lsa_SidType
)atol(attr
);
4260 /* Consistency checks */
4261 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4262 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4263 (type
!= SID_NAME_DOM_GRP
)))) {
4264 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4267 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4269 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4273 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4276 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4278 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4282 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4287 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4288 if (rid
== rids
[rid_index
])
4292 if (rid_index
== num_rids
) {
4293 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4297 attrs
[rid_index
] = type
;
4298 names
[rid_index
] = attr
;
4302 result
= NT_STATUS_NONE_MAPPED
;
4305 result
= (num_mapped
== num_rids
) ?
4306 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4308 TALLOC_FREE(mem_ctx
);
4312 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4314 char *filter
= NULL
;
4315 char *escaped
= NULL
;
4316 char *result
= NULL
;
4318 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4319 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4323 escaped
= escape_ldap_string(talloc_tos(), username
);
4324 if (escaped
== NULL
) goto done
;
4326 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4330 TALLOC_FREE(escaped
);
4335 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4339 const char **result
;
4341 va_start(ap
, mem_ctx
);
4342 while (va_arg(ap
, const char *) != NULL
)
4346 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4350 va_start(ap
, mem_ctx
);
4351 for (i
=0; i
<num
; i
++) {
4352 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4353 if (result
[i
] == NULL
) {
4354 talloc_free(result
);
4365 struct ldap_search_state
{
4366 struct smbldap_state
*connection
;
4368 uint32_t acct_flags
;
4369 uint16_t group_type
;
4376 void *pagedresults_cookie
;
4378 LDAPMessage
*entries
, *current_entry
;
4379 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4380 TALLOC_CTX
*mem_ctx
,
4381 LDAP
*ld
, LDAPMessage
*entry
,
4382 struct samr_displayentry
*result
);
4385 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4387 struct ldap_search_state
*state
=
4388 (struct ldap_search_state
*)search
->private_data
;
4390 int rc
= LDAP_OPERATIONS_ERROR
;
4392 state
->entries
= NULL
;
4394 if (state
->connection
->paged_results
) {
4395 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4396 state
->scope
, state
->filter
,
4397 state
->attrs
, state
->attrsonly
,
4398 lp_ldap_page_size(), &state
->entries
,
4399 &state
->pagedresults_cookie
);
4402 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4404 if (state
->entries
!= NULL
) {
4405 /* Left over from unsuccessful paged attempt */
4406 ldap_msgfree(state
->entries
);
4407 state
->entries
= NULL
;
4410 rc
= smbldap_search(state
->connection
, state
->base
,
4411 state
->scope
, state
->filter
, state
->attrs
,
4412 state
->attrsonly
, &state
->entries
);
4414 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4417 /* Ok, the server was lying. It told us it could do paged
4418 * searches when it could not. */
4419 state
->connection
->paged_results
= False
;
4422 ld
= state
->connection
->ldap_struct
;
4424 DEBUG(5, ("Don't have an LDAP connection right after a "
4428 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4433 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4435 struct ldap_search_state
*state
=
4436 (struct ldap_search_state
*)search
->private_data
;
4439 if (!state
->connection
->paged_results
) {
4440 /* There is no next page when there are no paged results */
4444 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4445 state
->scope
, state
->filter
, state
->attrs
,
4446 state
->attrsonly
, lp_ldap_page_size(),
4448 &state
->pagedresults_cookie
);
4450 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4453 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4455 if (state
->current_entry
== NULL
) {
4456 ldap_msgfree(state
->entries
);
4457 state
->entries
= NULL
;
4464 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4465 struct samr_displayentry
*entry
)
4467 struct ldap_search_state
*state
=
4468 (struct ldap_search_state
*)search
->private_data
;
4472 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4475 if ((state
->entries
== NULL
) &&
4476 !ldapsam_search_nextpage(search
))
4479 if (state
->current_entry
== NULL
) {
4483 result
= state
->ldap2displayentry(state
, search
,
4484 state
->connection
->ldap_struct
,
4485 state
->current_entry
, entry
);
4489 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4490 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4491 if (dn
!= NULL
) ldap_memfree(dn
);
4494 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4496 if (state
->current_entry
== NULL
) {
4497 ldap_msgfree(state
->entries
);
4498 state
->entries
= NULL
;
4501 if (!result
) goto retry
;
4506 static void ldapsam_search_end(struct pdb_search
*search
)
4508 struct ldap_search_state
*state
=
4509 (struct ldap_search_state
*)search
->private_data
;
4512 if (state
->pagedresults_cookie
== NULL
)
4515 if (state
->entries
!= NULL
)
4516 ldap_msgfree(state
->entries
);
4518 state
->entries
= NULL
;
4519 state
->current_entry
= NULL
;
4521 if (!state
->connection
->paged_results
)
4524 /* Tell the LDAP server we're not interested in the rest anymore. */
4526 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4527 state
->filter
, state
->attrs
,
4528 state
->attrsonly
, 0, &state
->entries
,
4529 &state
->pagedresults_cookie
);
4531 if (rc
!= LDAP_SUCCESS
)
4532 DEBUG(5, ("Could not end search properly\n"));
4537 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4538 TALLOC_CTX
*mem_ctx
,
4539 LDAP
*ld
, LDAPMessage
*entry
,
4540 struct samr_displayentry
*result
)
4543 size_t converted_size
;
4545 uint32_t acct_flags
;
4547 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4548 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4549 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4552 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4553 ldap_value_free(vals
);
4555 if ((state
->acct_flags
!= 0) &&
4556 ((state
->acct_flags
& acct_flags
) == 0))
4559 result
->acct_flags
= acct_flags
;
4560 result
->account_name
= "";
4561 result
->fullname
= "";
4562 result
->description
= "";
4564 vals
= ldap_get_values(ld
, entry
, "uid");
4565 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4566 DEBUG(5, ("\"uid\" not found\n"));
4569 if (!pull_utf8_talloc(mem_ctx
,
4570 discard_const_p(char *, &result
->account_name
),
4571 vals
[0], &converted_size
))
4573 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4577 ldap_value_free(vals
);
4579 vals
= ldap_get_values(ld
, entry
, "displayName");
4580 if ((vals
== NULL
) || (vals
[0] == NULL
))
4581 DEBUG(8, ("\"displayName\" not found\n"));
4582 else if (!pull_utf8_talloc(mem_ctx
,
4583 discard_const_p(char *, &result
->fullname
),
4584 vals
[0], &converted_size
))
4586 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4590 ldap_value_free(vals
);
4592 vals
= ldap_get_values(ld
, entry
, "description");
4593 if ((vals
== NULL
) || (vals
[0] == NULL
))
4594 DEBUG(8, ("\"description\" not found\n"));
4595 else if (!pull_utf8_talloc(mem_ctx
,
4596 discard_const_p(char *, &result
->description
),
4597 vals
[0], &converted_size
))
4599 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4603 ldap_value_free(vals
);
4605 if ((result
->account_name
== NULL
) ||
4606 (result
->fullname
== NULL
) ||
4607 (result
->description
== NULL
)) {
4608 DEBUG(0, ("talloc failed\n"));
4612 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4613 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4614 DEBUG(0, ("\"objectSid\" not found\n"));
4618 if (!string_to_sid(&sid
, vals
[0])) {
4619 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4620 ldap_value_free(vals
);
4623 ldap_value_free(vals
);
4625 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4626 DEBUG(0, ("sid %s does not belong to our domain\n",
4627 sid_string_dbg(&sid
)));
4635 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4636 struct pdb_search
*search
,
4637 uint32_t acct_flags
)
4639 struct ldapsam_privates
*ldap_state
=
4640 (struct ldapsam_privates
*)methods
->private_data
;
4641 struct ldap_search_state
*state
;
4643 state
= talloc(search
, struct ldap_search_state
);
4644 if (state
== NULL
) {
4645 DEBUG(0, ("talloc failed\n"));
4649 state
->connection
= ldap_state
->smbldap_state
;
4651 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4652 state
->base
= lp_ldap_user_suffix();
4653 else if ((acct_flags
!= 0) &&
4654 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4655 state
->base
= lp_ldap_machine_suffix();
4657 state
->base
= lp_ldap_suffix();
4659 state
->acct_flags
= acct_flags
;
4660 state
->base
= talloc_strdup(search
, state
->base
);
4661 state
->scope
= LDAP_SCOPE_SUBTREE
;
4662 state
->filter
= get_ldap_filter(search
, "*");
4663 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4664 "displayName", "description",
4665 "sambaAcctFlags", NULL
);
4666 state
->attrsonly
= 0;
4667 state
->pagedresults_cookie
= NULL
;
4668 state
->entries
= NULL
;
4669 state
->ldap2displayentry
= ldapuser2displayentry
;
4671 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4672 DEBUG(0, ("talloc failed\n"));
4676 search
->private_data
= state
;
4677 search
->next_entry
= ldapsam_search_next_entry
;
4678 search
->search_end
= ldapsam_search_end
;
4680 return ldapsam_search_firstpage(search
);
4683 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4684 TALLOC_CTX
*mem_ctx
,
4685 LDAP
*ld
, LDAPMessage
*entry
,
4686 struct samr_displayentry
*result
)
4689 size_t converted_size
;
4691 uint16_t group_type
;
4693 result
->account_name
= "";
4694 result
->fullname
= "";
4695 result
->description
= "";
4698 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4699 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4700 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4702 ldap_value_free(vals
);
4707 group_type
= atoi(vals
[0]);
4709 if ((state
->group_type
!= 0) &&
4710 ((state
->group_type
!= group_type
))) {
4711 ldap_value_free(vals
);
4715 ldap_value_free(vals
);
4717 /* display name is the NT group name */
4719 vals
= ldap_get_values(ld
, entry
, "displayName");
4720 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4721 DEBUG(8, ("\"displayName\" not found\n"));
4723 /* fallback to the 'cn' attribute */
4724 vals
= ldap_get_values(ld
, entry
, "cn");
4725 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4726 DEBUG(5, ("\"cn\" not found\n"));
4729 if (!pull_utf8_talloc(mem_ctx
,
4730 discard_const_p(char *,
4731 &result
->account_name
),
4732 vals
[0], &converted_size
))
4734 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4735 "failed: %s", strerror(errno
)));
4738 else if (!pull_utf8_talloc(mem_ctx
,
4739 discard_const_p(char *,
4740 &result
->account_name
),
4741 vals
[0], &converted_size
))
4743 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4747 ldap_value_free(vals
);
4749 vals
= ldap_get_values(ld
, entry
, "description");
4750 if ((vals
== NULL
) || (vals
[0] == NULL
))
4751 DEBUG(8, ("\"description\" not found\n"));
4752 else if (!pull_utf8_talloc(mem_ctx
,
4753 discard_const_p(char *, &result
->description
),
4754 vals
[0], &converted_size
))
4756 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4759 ldap_value_free(vals
);
4761 if ((result
->account_name
== NULL
) ||
4762 (result
->fullname
== NULL
) ||
4763 (result
->description
== NULL
)) {
4764 DEBUG(0, ("talloc failed\n"));
4768 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4769 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4770 DEBUG(0, ("\"objectSid\" not found\n"));
4772 ldap_value_free(vals
);
4777 if (!string_to_sid(&sid
, vals
[0])) {
4778 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4782 ldap_value_free(vals
);
4784 switch (group_type
) {
4785 case SID_NAME_DOM_GRP
:
4786 case SID_NAME_ALIAS
:
4788 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4789 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4791 DEBUG(0, ("%s is not in our domain\n",
4792 sid_string_dbg(&sid
)));
4798 DEBUG(0,("unknown group type: %d\n", group_type
));
4802 result
->acct_flags
= 0;
4807 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4808 struct pdb_search
*search
,
4809 const struct dom_sid
*sid
,
4810 enum lsa_SidType type
)
4812 struct ldapsam_privates
*ldap_state
=
4813 (struct ldapsam_privates
*)methods
->private_data
;
4814 struct ldap_search_state
*state
;
4817 state
= talloc(search
, struct ldap_search_state
);
4818 if (state
== NULL
) {
4819 DEBUG(0, ("talloc failed\n"));
4823 state
->connection
= ldap_state
->smbldap_state
;
4825 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4826 state
->connection
= ldap_state
->smbldap_state
;
4827 state
->scope
= LDAP_SCOPE_SUBTREE
;
4828 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4829 "(sambaGroupType=%d)(sambaSID=%s*))",
4831 type
, sid_to_fstring(tmp
, sid
));
4832 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4833 "displayName", "description",
4834 "sambaGroupType", NULL
);
4835 state
->attrsonly
= 0;
4836 state
->pagedresults_cookie
= NULL
;
4837 state
->entries
= NULL
;
4838 state
->group_type
= type
;
4839 state
->ldap2displayentry
= ldapgroup2displayentry
;
4841 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4842 DEBUG(0, ("talloc failed\n"));
4846 search
->private_data
= state
;
4847 search
->next_entry
= ldapsam_search_next_entry
;
4848 search
->search_end
= ldapsam_search_end
;
4850 return ldapsam_search_firstpage(search
);
4853 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4854 struct pdb_search
*search
)
4856 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4859 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4860 struct pdb_search
*search
,
4861 const struct dom_sid
*sid
)
4863 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4866 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4868 return PDB_CAP_STORE_RIDS
;
4871 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4874 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4876 LDAPMessage
*result
= NULL
;
4877 LDAPMessage
*entry
= NULL
;
4878 LDAPMod
**mods
= NULL
;
4882 uint32_t nextRid
= 0;
4885 TALLOC_CTX
*mem_ctx
;
4887 mem_ctx
= talloc_new(NULL
);
4888 if (mem_ctx
== NULL
) {
4889 DEBUG(0, ("talloc_new failed\n"));
4890 return NT_STATUS_NO_MEMORY
;
4893 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4894 get_global_sam_name(), False
);
4895 if (!NT_STATUS_IS_OK(status
)) {
4896 DEBUG(3, ("Could not get domain info: %s\n",
4897 nt_errstr(status
)));
4901 talloc_autofree_ldapmsg(mem_ctx
, result
);
4903 entry
= ldap_first_entry(priv2ld(priv
), result
);
4904 if (entry
== NULL
) {
4905 DEBUG(0, ("Could not get domain info entry\n"));
4906 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4910 /* Find the largest of the three attributes "sambaNextRid",
4911 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4912 concept of differentiating between user and group rids, and will
4913 use only "sambaNextRid" in the future. But for compatibility
4914 reasons I look if others have chosen different strategies -- VL */
4916 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4917 "sambaNextRid", mem_ctx
);
4918 if (value
!= NULL
) {
4919 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4920 nextRid
= MAX(nextRid
, tmp
);
4923 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4924 "sambaNextUserRid", mem_ctx
);
4925 if (value
!= NULL
) {
4926 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4927 nextRid
= MAX(nextRid
, tmp
);
4930 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4931 "sambaNextGroupRid", mem_ctx
);
4932 if (value
!= NULL
) {
4933 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4934 nextRid
= MAX(nextRid
, tmp
);
4938 nextRid
= BASE_RID
-1;
4943 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4944 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4945 talloc_autofree_ldapmod(mem_ctx
, mods
);
4947 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4948 status
= NT_STATUS_NO_MEMORY
;
4952 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4954 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4957 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4960 if (NT_STATUS_IS_OK(status
)) {
4964 TALLOC_FREE(mem_ctx
);
4968 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4972 for (i
=0; i
<10; i
++) {
4973 NTSTATUS result
= ldapsam_get_new_rid(
4974 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4975 if (NT_STATUS_IS_OK(result
)) {
4979 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4983 /* The ldap update failed (maybe a race condition), retry */
4986 /* Tried 10 times, fail. */
4987 return NT_STATUS_ACCESS_DENIED
;
4990 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
4992 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4993 return NT_STATUS_IS_OK(result
) ? True
: False
;
4996 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4997 const struct dom_sid
*sid
,
4998 uid_t
*uid
, gid_t
*gid
,
4999 enum lsa_SidType
*type
)
5001 struct ldapsam_privates
*priv
=
5002 (struct ldapsam_privates
*)methods
->private_data
;
5004 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5006 LDAPMessage
*result
= NULL
;
5007 LDAPMessage
*entry
= NULL
;
5012 TALLOC_CTX
*mem_ctx
;
5014 mem_ctx
= talloc_new(NULL
);
5015 if (mem_ctx
== NULL
) {
5016 DEBUG(0, ("talloc_new failed\n"));
5020 filter
= talloc_asprintf(mem_ctx
,
5022 "(|(objectClass=%s)(objectClass=%s)))",
5023 sid_string_talloc(mem_ctx
, sid
),
5024 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5025 if (filter
== NULL
) {
5026 DEBUG(5, ("talloc_asprintf failed\n"));
5030 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5032 if (rc
!= LDAP_SUCCESS
) {
5035 talloc_autofree_ldapmsg(mem_ctx
, result
);
5037 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5038 DEBUG(10, ("Got %d entries, expected one\n",
5039 ldap_count_entries(priv2ld(priv
), result
)));
5043 entry
= ldap_first_entry(priv2ld(priv
), result
);
5045 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5046 "sambaGroupType", mem_ctx
);
5048 if (value
!= NULL
) {
5049 const char *gid_str
;
5052 gid_str
= smbldap_talloc_single_attribute(
5053 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5054 if (gid_str
== NULL
) {
5055 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5056 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5061 *gid
= strtoul(gid_str
, NULL
, 10);
5062 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
5063 store_gid_sid_cache(sid
, *gid
);
5064 idmap_cache_set_sid2gid(sid
, *gid
);
5069 /* It must be a user */
5071 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5072 "uidNumber", mem_ctx
);
5073 if (value
== NULL
) {
5074 DEBUG(1, ("Could not find uidNumber in %s\n",
5075 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5079 *uid
= strtoul(value
, NULL
, 10);
5080 *type
= SID_NAME_USER
;
5081 store_uid_sid_cache(sid
, *uid
);
5082 idmap_cache_set_sid2uid(sid
, *uid
);
5086 TALLOC_FREE(mem_ctx
);
5091 * Find the SID for a uid.
5092 * This is shortcut is only used if ldapsam:trusted is set to true.
5094 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5095 struct dom_sid
*sid
)
5097 struct ldapsam_privates
*priv
=
5098 (struct ldapsam_privates
*)methods
->private_data
;
5100 const char *attrs
[] = { "sambaSID", NULL
};
5101 LDAPMessage
*result
= NULL
;
5102 LDAPMessage
*entry
= NULL
;
5104 char *user_sid_string
;
5105 struct dom_sid user_sid
;
5107 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5109 filter
= talloc_asprintf(tmp_ctx
,
5112 "(objectClass=%s))",
5114 LDAP_OBJ_POSIXACCOUNT
,
5115 LDAP_OBJ_SAMBASAMACCOUNT
);
5116 if (filter
== NULL
) {
5117 DEBUG(3, ("talloc_asprintf failed\n"));
5121 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5122 if (rc
!= LDAP_SUCCESS
) {
5125 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5127 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5128 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5129 ldap_count_entries(priv2ld(priv
), result
),
5130 (unsigned int)uid
));
5134 entry
= ldap_first_entry(priv2ld(priv
), result
);
5136 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5137 "sambaSID", tmp_ctx
);
5138 if (user_sid_string
== NULL
) {
5139 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5140 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5144 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5145 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5150 sid_copy(sid
, &user_sid
);
5152 store_uid_sid_cache(sid
, uid
);
5153 idmap_cache_set_sid2uid(sid
, uid
);
5158 TALLOC_FREE(tmp_ctx
);
5163 * Find the SID for a gid.
5164 * This is shortcut is only used if ldapsam:trusted is set to true.
5166 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5167 struct dom_sid
*sid
)
5169 struct ldapsam_privates
*priv
=
5170 (struct ldapsam_privates
*)methods
->private_data
;
5172 const char *attrs
[] = { "sambaSID", NULL
};
5173 LDAPMessage
*result
= NULL
;
5174 LDAPMessage
*entry
= NULL
;
5176 char *group_sid_string
;
5177 struct dom_sid group_sid
;
5179 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5181 filter
= talloc_asprintf(tmp_ctx
,
5183 "(objectClass=%s))",
5186 if (filter
== NULL
) {
5187 DEBUG(3, ("talloc_asprintf failed\n"));
5191 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5192 if (rc
!= LDAP_SUCCESS
) {
5195 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5197 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5198 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5199 ldap_count_entries(priv2ld(priv
), result
),
5200 (unsigned int)gid
));
5204 entry
= ldap_first_entry(priv2ld(priv
), result
);
5206 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5207 "sambaSID", tmp_ctx
);
5208 if (group_sid_string
== NULL
) {
5209 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5210 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5214 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5215 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5220 sid_copy(sid
, &group_sid
);
5222 store_gid_sid_cache(sid
, gid
);
5223 idmap_cache_set_sid2gid(sid
, gid
);
5228 TALLOC_FREE(tmp_ctx
);
5234 * The following functions are called only if
5235 * ldapsam:trusted and ldapsam:editposix are
5240 * ldapsam_create_user creates a new
5241 * posixAccount and sambaSamAccount object
5242 * in the ldap users subtree
5244 * The uid is allocated by winbindd.
5247 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5248 TALLOC_CTX
*tmp_ctx
, const char *name
,
5249 uint32_t acb_info
, uint32_t *rid
)
5251 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5252 LDAPMessage
*entry
= NULL
;
5253 LDAPMessage
*result
= NULL
;
5254 uint32_t num_result
;
5255 bool is_machine
= False
;
5256 bool add_posix
= False
;
5257 LDAPMod
**mods
= NULL
;
5265 const char *dn
= NULL
;
5266 struct dom_sid group_sid
;
5267 struct dom_sid user_sid
;
5273 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5274 acb_info
& ACB_WSTRUST
||
5275 acb_info
& ACB_SVRTRUST
||
5276 acb_info
& ACB_DOMTRUST
) {
5280 username
= escape_ldap_string(talloc_tos(), name
);
5281 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5282 username
, LDAP_OBJ_POSIXACCOUNT
);
5283 TALLOC_FREE(username
);
5285 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5286 if (rc
!= LDAP_SUCCESS
) {
5287 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5288 return NT_STATUS_ACCESS_DENIED
;
5290 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5292 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5294 if (num_result
> 1) {
5295 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5296 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5299 if (num_result
== 1) {
5301 /* check if it is just a posix account.
5302 * or if there is a sid attached to this entry
5305 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5307 return NT_STATUS_UNSUCCESSFUL
;
5310 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5312 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5313 return NT_STATUS_USER_EXISTS
;
5316 /* it is just a posix account, retrieve the dn for later use */
5317 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5319 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5320 return NT_STATUS_NO_MEMORY
;
5324 if (num_result
== 0) {
5328 /* Create the basic samu structure and generate the mods for the ldap commit */
5329 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5330 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5334 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5336 user
= samu_new(tmp_ctx
);
5338 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5339 return NT_STATUS_NO_MEMORY
;
5342 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5343 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5344 return NT_STATUS_UNSUCCESSFUL
;
5346 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5347 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5348 return NT_STATUS_UNSUCCESSFUL
;
5351 if (acb_info
& ACB_NORMAL
) {
5352 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5353 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5354 return NT_STATUS_UNSUCCESSFUL
;
5357 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5358 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5359 return NT_STATUS_UNSUCCESSFUL
;
5363 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5364 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5365 return NT_STATUS_UNSUCCESSFUL
;
5369 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5370 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5371 return NT_STATUS_UNSUCCESSFUL
;
5374 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
)) {
5375 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5376 return NT_STATUS_UNSUCCESSFUL
;
5379 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5380 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5382 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5387 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5389 /* retrieve the Domain Users group gid */
5390 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5391 !sid_to_gid(&group_sid
, &gid
)) {
5392 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5393 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5396 /* lets allocate a new userid for this user */
5397 if (!winbind_allocate_uid(&uid
)) {
5398 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5399 return NT_STATUS_UNSUCCESSFUL
;
5404 /* TODO: choose a more appropriate default for machines */
5405 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5406 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5408 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5409 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5411 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5412 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5414 escape_name
= escape_rdn_val_string_alloc(name
);
5416 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5417 return NT_STATUS_NO_MEMORY
;
5421 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5423 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5426 SAFE_FREE(escape_name
);
5428 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5429 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5430 return NT_STATUS_NO_MEMORY
;
5433 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5434 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5435 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5436 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5437 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5438 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5439 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5442 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5445 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5447 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5450 if (rc
!= LDAP_SUCCESS
) {
5451 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5452 return NT_STATUS_UNSUCCESSFUL
;
5455 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5457 flush_pwnam_cache();
5459 return NT_STATUS_OK
;
5462 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5464 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5465 LDAPMessage
*result
= NULL
;
5466 LDAPMessage
*entry
= NULL
;
5472 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5474 filter
= talloc_asprintf(tmp_ctx
,
5477 "(objectClass=%s))",
5478 pdb_get_username(sam_acct
),
5479 LDAP_OBJ_POSIXACCOUNT
,
5480 LDAP_OBJ_SAMBASAMACCOUNT
);
5481 if (filter
== NULL
) {
5482 return NT_STATUS_NO_MEMORY
;
5485 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5486 if (rc
!= LDAP_SUCCESS
) {
5487 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5488 return NT_STATUS_UNSUCCESSFUL
;
5490 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5492 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5494 if (num_result
== 0) {
5495 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5496 return NT_STATUS_NO_SUCH_USER
;
5499 if (num_result
> 1) {
5500 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5501 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5504 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5506 return NT_STATUS_UNSUCCESSFUL
;
5509 /* it is just a posix account, retrieve the dn for later use */
5510 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5512 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5513 return NT_STATUS_NO_MEMORY
;
5516 /* try to remove memberships first */
5519 struct dom_sid
*sids
= NULL
;
5521 uint32_t num_groups
= 0;
5523 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5525 status
= ldapsam_enum_group_memberships(my_methods
,
5531 if (!NT_STATUS_IS_OK(status
)) {
5535 for (i
=0; i
< num_groups
; i
++) {
5539 sid_peek_rid(&sids
[i
], &group_rid
);
5541 ldapsam_del_groupmem(my_methods
,
5550 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5551 if (rc
!= LDAP_SUCCESS
) {
5552 return NT_STATUS_UNSUCCESSFUL
;
5555 flush_pwnam_cache();
5557 return NT_STATUS_OK
;
5561 * ldapsam_create_group creates a new
5562 * posixGroup and sambaGroupMapping object
5563 * in the ldap groups subtree
5565 * The gid is allocated by winbindd.
5568 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5569 TALLOC_CTX
*tmp_ctx
,
5573 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5575 LDAPMessage
*entry
= NULL
;
5576 LDAPMessage
*result
= NULL
;
5577 uint32_t num_result
;
5578 bool is_new_entry
= False
;
5579 LDAPMod
**mods
= NULL
;
5585 const char *dn
= NULL
;
5586 struct dom_sid group_sid
;
5590 groupname
= escape_ldap_string(talloc_tos(), name
);
5591 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5592 groupname
, LDAP_OBJ_POSIXGROUP
);
5593 TALLOC_FREE(groupname
);
5595 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5596 if (rc
!= LDAP_SUCCESS
) {
5597 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5598 return NT_STATUS_UNSUCCESSFUL
;
5600 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5602 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5604 if (num_result
> 1) {
5605 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5606 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5609 if (num_result
== 1) {
5611 /* check if it is just a posix group.
5612 * or if there is a sid attached to this entry
5615 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5617 return NT_STATUS_UNSUCCESSFUL
;
5620 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5622 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5623 return NT_STATUS_GROUP_EXISTS
;
5626 /* it is just a posix group, retrieve the gid and the dn for later use */
5627 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5629 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5630 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5633 gid
= strtoul(tmp
, NULL
, 10);
5635 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5637 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5638 return NT_STATUS_NO_MEMORY
;
5642 if (num_result
== 0) {
5643 is_new_entry
= true;
5646 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5647 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5651 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5653 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5655 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5657 if (!groupsidstr
|| !grouptype
) {
5658 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5659 return NT_STATUS_NO_MEMORY
;
5662 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5663 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5664 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5665 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5670 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5672 /* lets allocate a new groupid for this group */
5673 if (!winbind_allocate_gid(&gid
)) {
5674 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5675 return NT_STATUS_UNSUCCESSFUL
;
5678 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5680 escape_name
= escape_rdn_val_string_alloc(name
);
5682 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5683 return NT_STATUS_NO_MEMORY
;
5686 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5688 SAFE_FREE(escape_name
);
5690 if (!gidstr
|| !dn
) {
5691 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5692 return NT_STATUS_NO_MEMORY
;
5695 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5696 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5697 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5700 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5703 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5705 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5706 /* This call may fail with rfc2307bis schema */
5707 /* Retry adding a structural class */
5708 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5709 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5713 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5716 if (rc
!= LDAP_SUCCESS
) {
5717 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5718 return NT_STATUS_UNSUCCESSFUL
;
5721 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5723 return NT_STATUS_OK
;
5726 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5728 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5729 LDAPMessage
*result
= NULL
;
5730 LDAPMessage
*entry
= NULL
;
5735 struct dom_sid group_sid
;
5738 /* get the group sid */
5739 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5741 filter
= talloc_asprintf(tmp_ctx
,
5744 "(objectClass=%s))",
5745 sid_string_talloc(tmp_ctx
, &group_sid
),
5746 LDAP_OBJ_POSIXGROUP
,
5748 if (filter
== NULL
) {
5749 return NT_STATUS_NO_MEMORY
;
5752 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5753 if (rc
!= LDAP_SUCCESS
) {
5754 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5755 return NT_STATUS_UNSUCCESSFUL
;
5757 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5759 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5761 if (num_result
== 0) {
5762 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5763 return NT_STATUS_NO_SUCH_GROUP
;
5766 if (num_result
> 1) {
5767 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5768 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5771 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5773 return NT_STATUS_UNSUCCESSFUL
;
5776 /* here it is, retrieve the dn for later use */
5777 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5779 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5780 return NT_STATUS_NO_MEMORY
;
5783 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5785 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5786 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5789 /* check no user have this group marked as primary group */
5790 filter
= talloc_asprintf(tmp_ctx
,
5793 "(objectClass=%s))",
5795 LDAP_OBJ_POSIXACCOUNT
,
5796 LDAP_OBJ_SAMBASAMACCOUNT
);
5798 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5799 if (rc
!= LDAP_SUCCESS
) {
5800 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5801 return NT_STATUS_UNSUCCESSFUL
;
5803 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5805 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5807 if (num_result
!= 0) {
5808 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5809 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5812 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5813 if (rc
!= LDAP_SUCCESS
) {
5814 return NT_STATUS_UNSUCCESSFUL
;
5817 return NT_STATUS_OK
;
5820 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5821 TALLOC_CTX
*tmp_ctx
,
5823 uint32_t member_rid
,
5826 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5827 LDAPMessage
*entry
= NULL
;
5828 LDAPMessage
*result
= NULL
;
5829 uint32_t num_result
;
5830 LDAPMod
**mods
= NULL
;
5833 const char *dn
= NULL
;
5834 struct dom_sid group_sid
;
5835 struct dom_sid member_sid
;
5840 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5842 case LDAP_MOD_DELETE
:
5843 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5846 return NT_STATUS_UNSUCCESSFUL
;
5849 /* get member sid */
5850 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5852 /* get the group sid */
5853 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5855 filter
= talloc_asprintf(tmp_ctx
,
5858 "(objectClass=%s))",
5859 sid_string_talloc(tmp_ctx
, &member_sid
),
5860 LDAP_OBJ_POSIXACCOUNT
,
5861 LDAP_OBJ_SAMBASAMACCOUNT
);
5862 if (filter
== NULL
) {
5863 return NT_STATUS_NO_MEMORY
;
5866 /* get the member uid */
5867 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5868 if (rc
!= LDAP_SUCCESS
) {
5869 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5870 return NT_STATUS_UNSUCCESSFUL
;
5872 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5874 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5876 if (num_result
== 0) {
5877 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5878 return NT_STATUS_NO_SUCH_MEMBER
;
5881 if (num_result
> 1) {
5882 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5883 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5886 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5888 return NT_STATUS_UNSUCCESSFUL
;
5891 if (modop
== LDAP_MOD_DELETE
) {
5892 /* check if we are trying to remove the member from his primary group */
5894 gid_t user_gid
, group_gid
;
5896 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5898 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5899 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5902 user_gid
= strtoul(gidstr
, NULL
, 10);
5904 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5905 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5906 return NT_STATUS_UNSUCCESSFUL
;
5909 if (user_gid
== group_gid
) {
5910 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5911 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5915 /* here it is, retrieve the uid for later use */
5916 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5918 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5919 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5922 filter
= talloc_asprintf(tmp_ctx
,
5925 "(objectClass=%s))",
5926 sid_string_talloc(tmp_ctx
, &group_sid
),
5927 LDAP_OBJ_POSIXGROUP
,
5931 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5932 if (rc
!= LDAP_SUCCESS
) {
5933 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5934 return NT_STATUS_UNSUCCESSFUL
;
5936 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5938 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5940 if (num_result
== 0) {
5941 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5942 return NT_STATUS_NO_SUCH_GROUP
;
5945 if (num_result
> 1) {
5946 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5947 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5950 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5952 return NT_STATUS_UNSUCCESSFUL
;
5955 /* here it is, retrieve the dn for later use */
5956 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5958 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5959 return NT_STATUS_NO_MEMORY
;
5962 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5964 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5966 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5967 if (rc
!= LDAP_SUCCESS
) {
5968 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5969 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5970 return NT_STATUS_MEMBER_IN_GROUP
;
5972 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5973 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5974 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5976 return NT_STATUS_UNSUCCESSFUL
;
5979 return NT_STATUS_OK
;
5982 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5983 TALLOC_CTX
*tmp_ctx
,
5985 uint32_t member_rid
)
5987 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5989 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5990 TALLOC_CTX
*tmp_ctx
,
5992 uint32_t member_rid
)
5994 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5997 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5998 TALLOC_CTX
*mem_ctx
,
5999 struct samu
*sampass
)
6001 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6002 LDAPMessage
*entry
= NULL
;
6003 LDAPMessage
*result
= NULL
;
6004 uint32_t num_result
;
6005 LDAPMod
**mods
= NULL
;
6007 char *escape_username
;
6009 const char *dn
= NULL
;
6013 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6015 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6016 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6017 return NT_STATUS_UNSUCCESSFUL
;
6019 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6021 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6022 return NT_STATUS_NO_MEMORY
;
6025 escape_username
= escape_ldap_string(talloc_tos(),
6026 pdb_get_username(sampass
));
6027 if (escape_username
== NULL
) {
6028 return NT_STATUS_NO_MEMORY
;
6031 filter
= talloc_asprintf(mem_ctx
,
6034 "(objectClass=%s))",
6036 LDAP_OBJ_POSIXACCOUNT
,
6037 LDAP_OBJ_SAMBASAMACCOUNT
);
6039 TALLOC_FREE(escape_username
);
6041 if (filter
== NULL
) {
6042 return NT_STATUS_NO_MEMORY
;
6045 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6046 if (rc
!= LDAP_SUCCESS
) {
6047 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6048 return NT_STATUS_UNSUCCESSFUL
;
6050 talloc_autofree_ldapmsg(mem_ctx
, result
);
6052 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6054 if (num_result
== 0) {
6055 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6056 return NT_STATUS_NO_SUCH_USER
;
6059 if (num_result
> 1) {
6060 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6061 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6064 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6066 return NT_STATUS_UNSUCCESSFUL
;
6069 /* retrieve the dn for later use */
6070 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6072 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6073 return NT_STATUS_NO_MEMORY
;
6076 /* remove the old one, and add the new one, this way we do not risk races */
6077 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6080 return NT_STATUS_OK
;
6083 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6085 if (rc
!= LDAP_SUCCESS
) {
6086 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6087 pdb_get_username(sampass
), gidstr
));
6088 return NT_STATUS_UNSUCCESSFUL
;
6091 flush_pwnam_cache();
6093 return NT_STATUS_OK
;
6097 /**********************************************************************
6098 trusted domains functions
6099 *********************************************************************/
6101 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6104 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6105 ldap_state
->domain_dn
);
6108 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6109 TALLOC_CTX
*mem_ctx
,
6110 const char *domain
, LDAPMessage
**entry
)
6114 int scope
= LDAP_SCOPE_SUBTREE
;
6115 const char **attrs
= NULL
; /* NULL: get all attrs */
6116 int attrsonly
= 0; /* 0: return values too */
6117 LDAPMessage
*result
= NULL
;
6119 uint32_t num_result
;
6121 filter
= talloc_asprintf(talloc_tos(),
6122 "(&(objectClass=%s)(sambaDomainName=%s))",
6123 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6125 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6126 if (trusted_dn
== NULL
) {
6129 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6130 filter
, attrs
, attrsonly
, &result
);
6132 if (result
!= NULL
) {
6133 talloc_autofree_ldapmsg(mem_ctx
, result
);
6136 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6141 if (rc
!= LDAP_SUCCESS
) {
6145 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6147 if (num_result
> 1) {
6148 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6149 "%s object for domain '%s'?!\n",
6150 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6154 if (num_result
== 0) {
6155 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6156 "%s object for domain %s.\n",
6157 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6160 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6166 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6169 struct dom_sid
*sid
,
6170 time_t *pass_last_set_time
)
6172 struct ldapsam_privates
*ldap_state
=
6173 (struct ldapsam_privates
*)methods
->private_data
;
6174 LDAPMessage
*entry
= NULL
;
6176 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6178 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6187 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6188 entry
, "sambaClearTextPassword", talloc_tos());
6189 if (pwd_str
== NULL
) {
6192 /* trusteddom_pw routines do not use talloc yet... */
6193 *pwd
= SMB_STRDUP(pwd_str
);
6199 /* last change time */
6200 if (pass_last_set_time
!= NULL
) {
6202 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6203 entry
, "sambaPwdLastSet", talloc_tos());
6204 if (time_str
== NULL
) {
6207 *pass_last_set_time
= (time_t)atol(time_str
);
6213 struct dom_sid dom_sid
;
6214 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6217 if (sid_str
== NULL
) {
6220 if (!string_to_sid(&dom_sid
, sid_str
)) {
6223 sid_copy(sid
, &dom_sid
);
6229 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6232 const struct dom_sid
*sid
)
6234 struct ldapsam_privates
*ldap_state
=
6235 (struct ldapsam_privates
*)methods
->private_data
;
6236 LDAPMessage
*entry
= NULL
;
6237 LDAPMod
**mods
= NULL
;
6238 char *prev_pwd
= NULL
;
6239 char *trusted_dn
= NULL
;
6242 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6245 * get the current entry (if there is one) in order to put the
6246 * current password into the previous password attribute
6248 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6253 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6254 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6255 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6257 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6258 sid_string_tos(sid
));
6259 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6260 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6261 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6262 "sambaClearTextPassword", pwd
);
6264 if (entry
!= NULL
) {
6265 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6266 entry
, "sambaClearTextPassword", talloc_tos());
6267 if (prev_pwd
!= NULL
) {
6268 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6269 "sambaPreviousClearTextPassword",
6274 talloc_autofree_ldapmod(talloc_tos(), mods
);
6276 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6277 if (trusted_dn
== NULL
) {
6280 if (entry
== NULL
) {
6281 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6283 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6286 if (rc
!= LDAP_SUCCESS
) {
6287 DEBUG(1, ("error writing trusted domain password!\n"));
6294 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6298 struct ldapsam_privates
*ldap_state
=
6299 (struct ldapsam_privates
*)methods
->private_data
;
6300 LDAPMessage
*entry
= NULL
;
6301 const char *trusted_dn
;
6303 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6307 if (entry
== NULL
) {
6308 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6313 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6315 if (trusted_dn
== NULL
) {
6316 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6320 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6321 if (rc
!= LDAP_SUCCESS
) {
6328 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6329 TALLOC_CTX
*mem_ctx
,
6330 uint32_t *num_domains
,
6331 struct trustdom_info
***domains
)
6334 struct ldapsam_privates
*ldap_state
=
6335 (struct ldapsam_privates
*)methods
->private_data
;
6337 int scope
= LDAP_SCOPE_SUBTREE
;
6338 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6339 int attrsonly
= 0; /* 0: return values too */
6340 LDAPMessage
*result
= NULL
;
6341 LDAPMessage
*entry
= NULL
;
6343 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6344 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6346 rc
= smbldap_search(ldap_state
->smbldap_state
,
6347 ldap_state
->domain_dn
,
6354 if (result
!= NULL
) {
6355 talloc_autofree_ldapmsg(mem_ctx
, result
);
6358 if (rc
!= LDAP_SUCCESS
) {
6359 return NT_STATUS_UNSUCCESSFUL
;
6363 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6364 DEBUG(1, ("talloc failed\n"));
6365 return NT_STATUS_NO_MEMORY
;
6368 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6370 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6372 char *dom_name
, *dom_sid_str
;
6373 struct trustdom_info
*dom_info
;
6375 dom_info
= talloc(*domains
, struct trustdom_info
);
6376 if (dom_info
== NULL
) {
6377 DEBUG(1, ("talloc failed\n"));
6378 return NT_STATUS_NO_MEMORY
;
6381 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6385 if (dom_name
== NULL
) {
6386 DEBUG(1, ("talloc failed\n"));
6387 return NT_STATUS_NO_MEMORY
;
6389 dom_info
->name
= dom_name
;
6391 dom_sid_str
= smbldap_talloc_single_attribute(
6392 priv2ld(ldap_state
), entry
, "sambaSID",
6394 if (dom_sid_str
== NULL
) {
6395 DEBUG(1, ("talloc failed\n"));
6396 return NT_STATUS_NO_MEMORY
;
6398 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6399 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6401 return NT_STATUS_UNSUCCESSFUL
;
6404 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6405 domains
, num_domains
);
6407 if (*domains
== NULL
) {
6408 DEBUG(1, ("talloc failed\n"));
6409 return NT_STATUS_NO_MEMORY
;
6413 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6414 return NT_STATUS_OK
;
6418 /**********************************************************************
6420 *********************************************************************/
6422 static void free_private_data(void **vp
)
6424 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6426 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6428 if ((*ldap_state
)->result
!= NULL
) {
6429 ldap_msgfree((*ldap_state
)->result
);
6430 (*ldap_state
)->result
= NULL
;
6432 if ((*ldap_state
)->domain_dn
!= NULL
) {
6433 SAFE_FREE((*ldap_state
)->domain_dn
);
6438 /* No need to free any further, as it is talloc()ed */
6441 /*********************************************************************
6442 Intitalise the parts of the pdb_methods structure that are common to
6444 *********************************************************************/
6446 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6449 struct ldapsam_privates
*ldap_state
;
6450 char *bind_dn
= NULL
;
6451 char *bind_secret
= NULL
;
6453 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6457 (*pdb_method
)->name
= "ldapsam";
6459 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6460 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6461 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6462 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6463 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6464 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6466 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6467 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6468 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6469 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6470 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6471 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6472 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6474 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6475 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6477 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6479 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6480 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6482 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6483 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6484 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6485 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6487 /* TODO: Setup private data and free */
6489 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6490 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6491 return NT_STATUS_NO_MEMORY
;
6494 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6495 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6496 return NT_STATUS_NO_MEMORY
;
6499 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6500 location
, false, bind_dn
, bind_secret
,
6501 &ldap_state
->smbldap_state
);
6502 memset(bind_secret
, '\0', strlen(bind_secret
));
6503 SAFE_FREE(bind_secret
);
6505 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6509 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6510 return NT_STATUS_NO_MEMORY
;
6513 (*pdb_method
)->private_data
= ldap_state
;
6515 (*pdb_method
)->free_private_data
= free_private_data
;
6517 return NT_STATUS_OK
;
6520 /**********************************************************************
6521 Initialise the 'compat' mode for pdb_ldap
6522 *********************************************************************/
6524 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6527 struct ldapsam_privates
*ldap_state
;
6528 char *uri
= talloc_strdup( NULL
, location
);
6530 trim_char( uri
, '\"', '\"' );
6531 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6535 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6539 (*pdb_method
)->name
= "ldapsam_compat";
6541 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6542 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6544 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6546 return NT_STATUS_OK
;
6549 /**********************************************************************
6550 Initialise the normal mode for pdb_ldap
6551 *********************************************************************/
6553 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6556 struct ldapsam_privates
*ldap_state
= NULL
;
6557 uint32_t alg_rid_base
;
6558 char *alg_rid_base_string
= NULL
;
6559 LDAPMessage
*result
= NULL
;
6560 LDAPMessage
*entry
= NULL
;
6561 struct dom_sid ldap_domain_sid
;
6562 struct dom_sid secrets_domain_sid
;
6563 char *domain_sid_string
= NULL
;
6565 char *uri
= talloc_strdup( NULL
, location
);
6567 trim_char( uri
, '\"', '\"' );
6568 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6572 if (!NT_STATUS_IS_OK(nt_status
)) {
6576 (*pdb_method
)->name
= "ldapsam";
6578 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6579 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6580 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6581 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6582 (*pdb_method
)->search_users
= ldapsam_search_users
;
6583 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6584 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6586 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6587 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6588 (*pdb_method
)->enum_group_memberships
=
6589 ldapsam_enum_group_memberships
;
6590 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6591 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6592 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6593 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6595 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6596 (*pdb_method
)->create_user
= ldapsam_create_user
;
6597 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6598 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6599 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6600 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6601 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6602 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6606 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6607 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6609 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6611 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6613 ldap_state
->domain_name
, True
);
6615 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6616 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6617 "info, nor add one to the domain. "
6618 "We cannot work reliably without it.\n"));
6619 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6622 /* Given that the above might fail, everything below this must be
6625 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6628 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6630 ldap_msgfree(result
);
6631 return NT_STATUS_UNSUCCESSFUL
;
6634 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6636 ldap_msgfree(result
);
6637 return NT_STATUS_UNSUCCESSFUL
;
6640 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6643 domain_sid_string
= smbldap_talloc_single_attribute(
6644 ldap_state
->smbldap_state
->ldap_struct
,
6646 get_userattr_key2string(ldap_state
->schema_ver
,
6647 LDAP_ATTR_USER_SID
),
6650 if (domain_sid_string
) {
6652 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6653 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6654 "read as a valid SID\n", domain_sid_string
));
6655 ldap_msgfree(result
);
6656 TALLOC_FREE(domain_sid_string
);
6657 return NT_STATUS_INVALID_PARAMETER
;
6659 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6660 &secrets_domain_sid
);
6661 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6662 &ldap_domain_sid
)) {
6663 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6664 "%s based on pdb_ldap results %s -> %s\n",
6665 ldap_state
->domain_name
,
6666 sid_string_dbg(&secrets_domain_sid
),
6667 sid_string_dbg(&ldap_domain_sid
)));
6669 /* reset secrets.tdb sid */
6670 secrets_store_domain_sid(ldap_state
->domain_name
,
6672 DEBUG(1, ("New global sam SID: %s\n",
6673 sid_string_dbg(get_global_sam_sid())));
6675 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6676 TALLOC_FREE(domain_sid_string
);
6679 alg_rid_base_string
= smbldap_talloc_single_attribute(
6680 ldap_state
->smbldap_state
->ldap_struct
,
6682 get_attr_key2string( dominfo_attr_list
,
6683 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6685 if (alg_rid_base_string
) {
6686 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6687 if (alg_rid_base
!= algorithmic_rid_base()) {
6688 DEBUG(0, ("The value of 'algorithmic RID base' has "
6689 "changed since the LDAP\n"
6690 "database was initialised. Aborting. \n"));
6691 ldap_msgfree(result
);
6692 TALLOC_FREE(alg_rid_base_string
);
6693 return NT_STATUS_UNSUCCESSFUL
;
6695 TALLOC_FREE(alg_rid_base_string
);
6697 ldap_msgfree(result
);
6699 return NT_STATUS_OK
;
6702 NTSTATUS
pdb_ldap_init(void)
6705 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6708 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6711 /* Let pdb_nds register backends */
6716 return NT_STATUS_OK
;