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
,
506 char *username
= NULL
,
512 *logon_script
= NULL
,
513 *profile_path
= NULL
,
515 *workstations
= NULL
,
518 uint8 smblmpwd
[LM_HASH_LEN
],
519 smbntpwd
[NT_HASH_LEN
];
520 bool use_samba_attrs
= True
;
521 uint32_t acct_ctrl
= 0;
523 uint16_t bad_password_count
= 0,
526 uint8 hours
[MAX_HOURS_LEN
];
528 struct login_cache cache_entry
;
530 bool expand_explicit
= lp_passdb_expand_explicit();
532 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
537 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
538 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
542 if (priv2ld(ldap_state
) == NULL
) {
543 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
544 "ldap_struct is NULL!\n"));
548 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
552 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
557 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
559 nt_username
= talloc_strdup(ctx
, username
);
564 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
569 pdb_set_username(sampass
, username
, PDB_SET
);
571 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
572 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
574 /* deal with different attributes between the schema first */
576 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
577 if ((temp
= smbldap_talloc_single_attribute(
578 ldap_state
->smbldap_state
->ldap_struct
,
580 get_userattr_key2string(ldap_state
->schema_ver
,
583 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
586 if ((temp
= smbldap_talloc_single_attribute(
587 ldap_state
->smbldap_state
->ldap_struct
,
589 get_userattr_key2string(ldap_state
->schema_ver
,
592 user_rid
= (uint32_t)atol(temp
);
593 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
597 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
598 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
599 get_userattr_key2string(ldap_state
->schema_ver
,
601 get_userattr_key2string(ldap_state
->schema_ver
,
607 temp
= smbldap_talloc_single_attribute(
608 ldap_state
->smbldap_state
->ldap_struct
,
610 get_userattr_key2string(ldap_state
->schema_ver
,
611 LDAP_ATTR_PWD_LAST_SET
),
614 pass_last_set_time
= (time_t) atol(temp
);
615 pdb_set_pass_last_set_time(sampass
,
616 pass_last_set_time
, PDB_SET
);
619 temp
= smbldap_talloc_single_attribute(
620 ldap_state
->smbldap_state
->ldap_struct
,
622 get_userattr_key2string(ldap_state
->schema_ver
,
623 LDAP_ATTR_LOGON_TIME
),
626 logon_time
= (time_t) atol(temp
);
627 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
630 temp
= smbldap_talloc_single_attribute(
631 ldap_state
->smbldap_state
->ldap_struct
,
633 get_userattr_key2string(ldap_state
->schema_ver
,
634 LDAP_ATTR_LOGOFF_TIME
),
637 logoff_time
= (time_t) atol(temp
);
638 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
641 temp
= smbldap_talloc_single_attribute(
642 ldap_state
->smbldap_state
->ldap_struct
,
644 get_userattr_key2string(ldap_state
->schema_ver
,
645 LDAP_ATTR_KICKOFF_TIME
),
648 kickoff_time
= (time_t) atol(temp
);
649 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
652 temp
= smbldap_talloc_single_attribute(
653 ldap_state
->smbldap_state
->ldap_struct
,
655 get_userattr_key2string(ldap_state
->schema_ver
,
656 LDAP_ATTR_PWD_CAN_CHANGE
),
659 pass_can_change_time
= (time_t) atol(temp
);
660 pdb_set_pass_can_change_time(sampass
,
661 pass_can_change_time
, PDB_SET
);
664 /* recommend that 'gecos' and 'displayName' should refer to the same
665 * attribute OID. userFullName depreciated, only used by Samba
666 * primary rules of LDAP: don't make a new attribute when one is already defined
667 * that fits your needs; using cn then displayName rather than 'userFullName'
670 fullname
= smbldap_talloc_single_attribute(
671 ldap_state
->smbldap_state
->ldap_struct
,
673 get_userattr_key2string(ldap_state
->schema_ver
,
674 LDAP_ATTR_DISPLAY_NAME
),
677 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
679 fullname
= smbldap_talloc_single_attribute(
680 ldap_state
->smbldap_state
->ldap_struct
,
682 get_userattr_key2string(ldap_state
->schema_ver
,
686 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
690 dir_drive
= smbldap_talloc_single_attribute(
691 ldap_state
->smbldap_state
->ldap_struct
,
693 get_userattr_key2string(ldap_state
->schema_ver
,
694 LDAP_ATTR_HOME_DRIVE
),
697 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
699 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
702 homedir
= smbldap_talloc_single_attribute(
703 ldap_state
->smbldap_state
->ldap_struct
,
705 get_userattr_key2string(ldap_state
->schema_ver
,
706 LDAP_ATTR_HOME_PATH
),
709 if (expand_explicit
) {
710 homedir
= talloc_sub_basic(ctx
,
718 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
720 pdb_set_homedir(sampass
,
721 talloc_sub_basic(ctx
, username
, domain
,
726 logon_script
= smbldap_talloc_single_attribute(
727 ldap_state
->smbldap_state
->ldap_struct
,
729 get_userattr_key2string(ldap_state
->schema_ver
,
730 LDAP_ATTR_LOGON_SCRIPT
),
733 if (expand_explicit
) {
734 logon_script
= talloc_sub_basic(ctx
,
742 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
744 pdb_set_logon_script(sampass
,
745 talloc_sub_basic(ctx
, username
, domain
,
750 profile_path
= smbldap_talloc_single_attribute(
751 ldap_state
->smbldap_state
->ldap_struct
,
753 get_userattr_key2string(ldap_state
->schema_ver
,
754 LDAP_ATTR_PROFILE_PATH
),
757 if (expand_explicit
) {
758 profile_path
= talloc_sub_basic(ctx
,
766 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
768 pdb_set_profile_path(sampass
,
769 talloc_sub_basic(ctx
, username
, domain
,
774 acct_desc
= smbldap_talloc_single_attribute(
775 ldap_state
->smbldap_state
->ldap_struct
,
777 get_userattr_key2string(ldap_state
->schema_ver
,
781 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
784 workstations
= smbldap_talloc_single_attribute(
785 ldap_state
->smbldap_state
->ldap_struct
,
787 get_userattr_key2string(ldap_state
->schema_ver
,
791 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
794 munged_dial
= smbldap_talloc_single_attribute(
795 ldap_state
->smbldap_state
->ldap_struct
,
797 get_userattr_key2string(ldap_state
->schema_ver
,
798 LDAP_ATTR_MUNGED_DIAL
),
801 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
804 /* FIXME: hours stuff should be cleaner */
808 memset(hours
, 0xff, hours_len
);
810 if (ldap_state
->is_nds_ldap
) {
813 char clear_text_pw
[512];
815 /* Make call to Novell eDirectory ldap extension to get clear text password.
816 NOTE: This will only work if we have an SSL connection to eDirectory. */
817 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
818 if (user_dn
!= NULL
) {
819 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
821 pwd_len
= sizeof(clear_text_pw
);
822 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
823 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
824 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
825 TALLOC_FREE(user_dn
);
828 ZERO_STRUCT(smblmpwd
);
829 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
830 TALLOC_FREE(user_dn
);
833 ZERO_STRUCT(smbntpwd
);
834 use_samba_attrs
= False
;
837 TALLOC_FREE(user_dn
);
840 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
844 if (use_samba_attrs
) {
845 temp
= smbldap_talloc_single_attribute(
846 ldap_state
->smbldap_state
->ldap_struct
,
848 get_userattr_key2string(ldap_state
->schema_ver
,
852 pdb_gethexpwd(temp
, smblmpwd
);
853 memset((char *)temp
, '\0', strlen(temp
)+1);
854 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
857 ZERO_STRUCT(smblmpwd
);
860 temp
= smbldap_talloc_single_attribute(
861 ldap_state
->smbldap_state
->ldap_struct
,
863 get_userattr_key2string(ldap_state
->schema_ver
,
867 pdb_gethexpwd(temp
, smbntpwd
);
868 memset((char *)temp
, '\0', strlen(temp
)+1);
869 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
872 ZERO_STRUCT(smbntpwd
);
878 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
880 uint8
*pwhist
= NULL
;
882 char *history_string
= talloc_array(ctx
, char,
883 MAX_PW_HISTORY_LEN
*64);
885 if (!history_string
) {
889 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
891 pwhist
= talloc_array(ctx
, uint8
,
892 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
893 if (pwhist
== NULL
) {
894 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
897 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
899 if (smbldap_get_single_attribute(
900 ldap_state
->smbldap_state
->ldap_struct
,
902 get_userattr_key2string(ldap_state
->schema_ver
,
903 LDAP_ATTR_PWD_HISTORY
),
905 MAX_PW_HISTORY_LEN
*64)) {
906 bool hex_failed
= false;
907 for (i
= 0; i
< pwHistLen
; i
++){
908 /* Get the 16 byte salt. */
909 if (!pdb_gethexpwd(&history_string
[i
*64],
910 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
914 /* Get the 16 byte MD5 hash of salt+passwd. */
915 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
916 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
917 PW_HISTORY_SALT_LEN
])) {
923 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
925 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
928 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
933 temp
= smbldap_talloc_single_attribute(
934 ldap_state
->smbldap_state
->ldap_struct
,
936 get_userattr_key2string(ldap_state
->schema_ver
,
940 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
942 if (acct_ctrl
== 0) {
943 acct_ctrl
|= ACB_NORMAL
;
946 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
948 acct_ctrl
|= ACB_NORMAL
;
951 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
952 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
954 temp
= smbldap_talloc_single_attribute(
955 ldap_state
->smbldap_state
->ldap_struct
,
957 get_userattr_key2string(ldap_state
->schema_ver
,
958 LDAP_ATTR_BAD_PASSWORD_COUNT
),
961 bad_password_count
= (uint32_t) atol(temp
);
962 pdb_set_bad_password_count(sampass
,
963 bad_password_count
, PDB_SET
);
966 temp
= smbldap_talloc_single_attribute(
967 ldap_state
->smbldap_state
->ldap_struct
,
969 get_userattr_key2string(ldap_state
->schema_ver
,
970 LDAP_ATTR_BAD_PASSWORD_TIME
),
973 bad_password_time
= (time_t) atol(temp
);
974 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
978 temp
= smbldap_talloc_single_attribute(
979 ldap_state
->smbldap_state
->ldap_struct
,
981 get_userattr_key2string(ldap_state
->schema_ver
,
982 LDAP_ATTR_LOGON_COUNT
),
985 logon_count
= (uint32_t) atol(temp
);
986 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
989 /* pdb_set_unknown_6(sampass, unknown6, 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_HOURS
),
998 pdb_gethexhours(temp
, hours
);
999 memset((char *)temp
, '\0', strlen(temp
) +1);
1000 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
1004 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1005 struct passwd unix_pw
;
1006 bool have_uid
= false;
1007 bool have_gid
= false;
1008 struct dom_sid mapped_gsid
;
1009 const struct dom_sid
*primary_gsid
;
1011 ZERO_STRUCT(unix_pw
);
1013 unix_pw
.pw_name
= username
;
1014 unix_pw
.pw_passwd
= discard_const_p(char, "x");
1016 temp
= smbldap_talloc_single_attribute(
1017 priv2ld(ldap_state
),
1022 /* We've got a uid, feed the cache */
1023 unix_pw
.pw_uid
= strtoul(temp
, NULL
, 10);
1026 temp
= smbldap_talloc_single_attribute(
1027 priv2ld(ldap_state
),
1032 /* We've got a uid, feed the cache */
1033 unix_pw
.pw_gid
= strtoul(temp
, NULL
, 10);
1036 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state
),
1041 if (unix_pw
.pw_gecos
) {
1042 unix_pw
.pw_gecos
= fullname
;
1044 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1045 priv2ld(ldap_state
),
1049 if (unix_pw
.pw_dir
) {
1050 unix_pw
.pw_dir
= discard_const_p(char, "");
1052 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1053 priv2ld(ldap_state
),
1057 if (unix_pw
.pw_shell
) {
1058 unix_pw
.pw_shell
= discard_const_p(char, "");
1061 if (have_uid
&& have_gid
) {
1062 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1064 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1067 if (sampass
->unix_pw
== NULL
) {
1068 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1069 pdb_get_username(sampass
)));
1073 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass
),
1074 sampass
->unix_pw
->pw_uid
);
1076 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1077 primary_gsid
= pdb_get_group_sid(sampass
);
1078 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1079 idmap_cache_set_sid2gid(primary_gsid
,
1080 sampass
->unix_pw
->pw_gid
);
1084 /* check the timestamp of the cache vs ldap entry */
1085 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1091 /* see if we have newer updates */
1092 if (!login_cache_read(sampass
, &cache_entry
)) {
1093 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1094 (unsigned int)pdb_get_bad_password_count(sampass
),
1095 (unsigned int)pdb_get_bad_password_time(sampass
)));
1100 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1101 (unsigned int)ldap_entry_time
,
1102 (unsigned int)cache_entry
.entry_timestamp
,
1103 (unsigned int)cache_entry
.bad_password_time
));
1105 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1106 /* cache is older than directory , so
1107 we need to delete the entry but allow the
1108 fields to be written out */
1109 login_cache_delentry(sampass
);
1112 pdb_set_acct_ctrl(sampass
,
1113 pdb_get_acct_ctrl(sampass
) |
1114 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1116 pdb_set_bad_password_count(sampass
,
1117 cache_entry
.bad_password_count
,
1119 pdb_set_bad_password_time(sampass
,
1120 cache_entry
.bad_password_time
,
1132 /**********************************************************************
1133 Initialize the ldap db from a struct samu. Called on update.
1134 (Based on init_buffer_from_sam in pdb_tdb.c)
1135 *********************************************************************/
1137 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1138 LDAPMessage
*existing
,
1139 LDAPMod
*** mods
, struct samu
* sampass
,
1140 bool (*need_update
)(const struct samu
*,
1146 if (mods
== NULL
|| sampass
== NULL
) {
1147 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1154 * took out adding "objectclass: sambaAccount"
1155 * do this on a per-mod basis
1157 if (need_update(sampass
, PDB_USERNAME
)) {
1158 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1159 "uid", pdb_get_username(sampass
));
1160 if (ldap_state
->is_nds_ldap
) {
1161 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1162 "cn", pdb_get_username(sampass
));
1163 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1164 "sn", pdb_get_username(sampass
));
1168 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1170 /* only update the RID if we actually need to */
1171 if (need_update(sampass
, PDB_USERSID
)) {
1173 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1175 switch ( ldap_state
->schema_ver
) {
1176 case SCHEMAVER_SAMBAACCOUNT
:
1177 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1178 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1179 sid_string_dbg(user_sid
),
1181 &ldap_state
->domain_sid
)));
1184 if (asprintf(&temp
, "%i", rid
) < 0) {
1187 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1188 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1193 case SCHEMAVER_SAMBASAMACCOUNT
:
1194 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1195 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1196 sid_to_fstring(sid_string
, user_sid
));
1200 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1205 /* we don't need to store the primary group RID - so leaving it
1206 'free' to hang off the unix primary group makes life easier */
1208 if (need_update(sampass
, PDB_GROUPSID
)) {
1210 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1212 switch ( ldap_state
->schema_ver
) {
1213 case SCHEMAVER_SAMBAACCOUNT
:
1214 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1215 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1216 sid_string_dbg(group_sid
),
1218 &ldap_state
->domain_sid
)));
1222 if (asprintf(&temp
, "%i", rid
) < 0) {
1225 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1226 get_userattr_key2string(ldap_state
->schema_ver
,
1227 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1231 case SCHEMAVER_SAMBASAMACCOUNT
:
1232 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1233 get_userattr_key2string(ldap_state
->schema_ver
,
1234 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1238 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1244 /* displayName, cn, and gecos should all be the same
1245 * most easily accomplished by giving them the same OID
1246 * gecos isn't set here b/c it should be handled by the
1248 * We change displayName only and fall back to cn if
1249 * it does not exist.
1252 if (need_update(sampass
, PDB_FULLNAME
))
1253 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1254 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1255 pdb_get_fullname(sampass
));
1257 if (need_update(sampass
, PDB_ACCTDESC
))
1258 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1259 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1260 pdb_get_acct_desc(sampass
));
1262 if (need_update(sampass
, PDB_WORKSTATIONS
))
1263 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1264 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1265 pdb_get_workstations(sampass
));
1267 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1268 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1269 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1270 pdb_get_munged_dial(sampass
));
1272 if (need_update(sampass
, PDB_SMBHOME
))
1273 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1274 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1275 pdb_get_homedir(sampass
));
1277 if (need_update(sampass
, PDB_DRIVE
))
1278 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1279 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1280 pdb_get_dir_drive(sampass
));
1282 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1283 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1284 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1285 pdb_get_logon_script(sampass
));
1287 if (need_update(sampass
, PDB_PROFILE
))
1288 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1289 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1290 pdb_get_profile_path(sampass
));
1292 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1295 if (need_update(sampass
, PDB_LOGONTIME
))
1296 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1297 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1300 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1303 if (need_update(sampass
, PDB_LOGOFFTIME
))
1304 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1305 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1308 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1311 if (need_update(sampass
, PDB_KICKOFFTIME
))
1312 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1313 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1316 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1319 if (need_update(sampass
, PDB_CANCHANGETIME
))
1320 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1321 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1324 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1325 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1327 if (need_update(sampass
, PDB_LMPASSWD
)) {
1328 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1331 pdb_sethexpwd(pwstr
, lm_pw
,
1332 pdb_get_acct_ctrl(sampass
));
1333 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1334 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1337 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1338 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1342 if (need_update(sampass
, PDB_NTPASSWD
)) {
1343 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1346 pdb_sethexpwd(pwstr
, nt_pw
,
1347 pdb_get_acct_ctrl(sampass
));
1348 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1349 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1352 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1353 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1358 if (need_update(sampass
, PDB_PWHISTORY
)) {
1360 uint32_t pwHistLen
= 0;
1361 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1363 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1367 if (pwHistLen
== 0) {
1368 /* Remove any password history from the LDAP store. */
1369 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1373 uint32_t currHistLen
= 0;
1374 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1375 if (pwhist
!= NULL
) {
1376 /* We can only store (1024-1/64 password history entries. */
1377 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1378 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1379 /* Store the salt. */
1380 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1381 /* Followed by the md5 hash of salt + md4 hash */
1382 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1383 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1384 DEBUG(100, ("pwstr=%s\n", pwstr
));
1388 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1389 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1394 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1395 if (asprintf(&temp
, "%li",
1396 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1399 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1400 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1406 if (need_update(sampass
, PDB_HOURS
)) {
1407 const uint8
*hours
= pdb_get_hours(sampass
);
1410 pdb_sethexhours(hourstr
, hours
);
1411 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1414 get_userattr_key2string(ldap_state
->schema_ver
,
1415 LDAP_ATTR_LOGON_HOURS
),
1420 if (need_update(sampass
, PDB_ACCTCTRL
))
1421 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1422 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1423 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1425 /* password lockout cache:
1426 - If we are now autolocking or clearing, we write to ldap
1427 - If we are clearing, we delete the cache entry
1428 - If the count is > 0, we update the cache
1430 This even means when autolocking, we cache, just in case the
1431 update doesn't work, and we have to cache the autolock flag */
1433 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1434 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1435 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1436 time_t badtime
= pdb_get_bad_password_time(sampass
);
1438 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1440 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1441 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1443 if ((badcount
>= pol
) || (badcount
== 0)) {
1444 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1445 (unsigned int)badcount
, (unsigned int)badtime
));
1446 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1450 ldap_state
->smbldap_state
->ldap_struct
,
1452 get_userattr_key2string(
1453 ldap_state
->schema_ver
,
1454 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1458 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1462 ldap_state
->smbldap_state
->ldap_struct
,
1464 get_userattr_key2string(
1465 ldap_state
->schema_ver
,
1466 LDAP_ATTR_BAD_PASSWORD_TIME
),
1470 if (badcount
== 0) {
1471 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1472 login_cache_delentry(sampass
);
1474 struct login_cache cache_entry
;
1476 cache_entry
.entry_timestamp
= time(NULL
);
1477 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1478 cache_entry
.bad_password_count
= badcount
;
1479 cache_entry
.bad_password_time
= badtime
;
1481 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1482 login_cache_write(sampass
, &cache_entry
);
1489 /**********************************************************************
1490 End enumeration of the LDAP password list.
1491 *********************************************************************/
1493 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1495 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1496 if (ldap_state
->result
) {
1497 ldap_msgfree(ldap_state
->result
);
1498 ldap_state
->result
= NULL
;
1502 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1503 const char *new_attr
)
1507 if (new_attr
== NULL
) {
1511 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1515 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1517 SMB_ASSERT((*attr_list
) != NULL
);
1518 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1519 (*attr_list
)[i
+1] = NULL
;
1522 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1523 const char ***attr_list
)
1525 append_attr(mem_ctx
, attr_list
, "uidNumber");
1526 append_attr(mem_ctx
, attr_list
, "gidNumber");
1527 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1528 append_attr(mem_ctx
, attr_list
, "loginShell");
1529 append_attr(mem_ctx
, attr_list
, "gecos");
1532 /**********************************************************************
1533 Get struct samu entry from LDAP by username.
1534 *********************************************************************/
1536 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1538 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1539 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1540 LDAPMessage
*result
= NULL
;
1541 LDAPMessage
*entry
= NULL
;
1543 const char ** attr_list
;
1546 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1547 append_attr(user
, &attr_list
,
1548 get_userattr_key2string(ldap_state
->schema_ver
,
1549 LDAP_ATTR_MOD_TIMESTAMP
));
1550 ldapsam_add_unix_attributes(user
, &attr_list
);
1551 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1553 TALLOC_FREE( attr_list
);
1555 if ( rc
!= LDAP_SUCCESS
)
1556 return NT_STATUS_NO_SUCH_USER
;
1558 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1561 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1562 ldap_msgfree(result
);
1563 return NT_STATUS_NO_SUCH_USER
;
1564 } else if (count
> 1) {
1565 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1566 ldap_msgfree(result
);
1567 return NT_STATUS_NO_SUCH_USER
;
1570 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1572 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1573 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1574 ldap_msgfree(result
);
1575 return NT_STATUS_NO_SUCH_USER
;
1577 pdb_set_backend_private_data(user
, result
, NULL
,
1578 my_methods
, PDB_CHANGED
);
1579 talloc_autofree_ldapmsg(user
, result
);
1582 ldap_msgfree(result
);
1587 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1588 const struct dom_sid
*sid
, LDAPMessage
**result
)
1591 const char ** attr_list
;
1594 switch ( ldap_state
->schema_ver
) {
1595 case SCHEMAVER_SAMBASAMACCOUNT
: {
1596 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1597 if (tmp_ctx
== NULL
) {
1598 return LDAP_NO_MEMORY
;
1601 attr_list
= get_userattr_list(tmp_ctx
,
1602 ldap_state
->schema_ver
);
1603 append_attr(tmp_ctx
, &attr_list
,
1604 get_userattr_key2string(
1605 ldap_state
->schema_ver
,
1606 LDAP_ATTR_MOD_TIMESTAMP
));
1607 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1608 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1610 TALLOC_FREE(tmp_ctx
);
1612 if ( rc
!= LDAP_SUCCESS
)
1617 case SCHEMAVER_SAMBAACCOUNT
:
1618 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1622 attr_list
= get_userattr_list(NULL
,
1623 ldap_state
->schema_ver
);
1624 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1625 TALLOC_FREE( attr_list
);
1627 if ( rc
!= LDAP_SUCCESS
)
1634 /**********************************************************************
1635 Get struct samu entry from LDAP by SID.
1636 *********************************************************************/
1638 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1640 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1641 LDAPMessage
*result
= NULL
;
1642 LDAPMessage
*entry
= NULL
;
1646 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1648 if (rc
!= LDAP_SUCCESS
)
1649 return NT_STATUS_NO_SUCH_USER
;
1651 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1654 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1655 "count=%d\n", sid_string_dbg(sid
), count
));
1656 ldap_msgfree(result
);
1657 return NT_STATUS_NO_SUCH_USER
;
1658 } else if (count
> 1) {
1659 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1660 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1662 ldap_msgfree(result
);
1663 return NT_STATUS_NO_SUCH_USER
;
1666 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1668 ldap_msgfree(result
);
1669 return NT_STATUS_NO_SUCH_USER
;
1672 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1673 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1674 ldap_msgfree(result
);
1675 return NT_STATUS_NO_SUCH_USER
;
1678 pdb_set_backend_private_data(user
, result
, NULL
,
1679 my_methods
, PDB_CHANGED
);
1680 talloc_autofree_ldapmsg(user
, result
);
1681 return NT_STATUS_OK
;
1684 /********************************************************************
1685 Do the actual modification - also change a plaintext passord if
1687 **********************************************************************/
1689 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1690 struct samu
*newpwd
, char *dn
,
1691 LDAPMod
**mods
, int ldap_op
,
1692 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1694 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1697 if (!newpwd
|| !dn
) {
1698 return NT_STATUS_INVALID_PARAMETER
;
1701 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1702 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1703 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1704 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1707 char *retoid
= NULL
;
1708 struct berval
*retdata
= NULL
;
1709 char *utf8_password
;
1711 size_t converted_size
;
1714 if (!ldap_state
->is_nds_ldap
) {
1716 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1717 LDAP_EXOP_MODIFY_PASSWD
)) {
1718 DEBUG(2, ("ldap password change requested, but LDAP "
1719 "server does not support it -- ignoring\n"));
1720 return NT_STATUS_OK
;
1724 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1725 pdb_get_plaintext_passwd(newpwd
),
1728 return NT_STATUS_NO_MEMORY
;
1731 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1732 TALLOC_FREE(utf8_password
);
1733 return NT_STATUS_NO_MEMORY
;
1736 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1737 DEBUG(0,("ber_alloc_t returns NULL\n"));
1738 TALLOC_FREE(utf8_password
);
1739 TALLOC_FREE(utf8_dn
);
1740 return NT_STATUS_UNSUCCESSFUL
;
1743 if ((ber_printf (ber
, "{") < 0) ||
1744 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1746 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1749 TALLOC_FREE(utf8_dn
);
1750 TALLOC_FREE(utf8_password
);
1751 return NT_STATUS_UNSUCCESSFUL
;
1754 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1755 ret
= ber_printf(ber
, "ts}",
1756 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1759 ret
= ber_printf(ber
, "}");
1763 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1766 TALLOC_FREE(utf8_dn
);
1767 TALLOC_FREE(utf8_password
);
1768 return NT_STATUS_UNSUCCESSFUL
;
1771 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1772 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1774 TALLOC_FREE(utf8_dn
);
1775 TALLOC_FREE(utf8_password
);
1776 return NT_STATUS_UNSUCCESSFUL
;
1779 TALLOC_FREE(utf8_dn
);
1780 TALLOC_FREE(utf8_password
);
1783 if (!ldap_state
->is_nds_ldap
) {
1784 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1785 LDAP_EXOP_MODIFY_PASSWD
,
1786 bv
, NULL
, NULL
, &retoid
,
1789 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1790 pdb_get_plaintext_passwd(newpwd
));
1792 if (rc
!= LDAP_SUCCESS
) {
1793 char *ld_error
= NULL
;
1795 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1796 DEBUG(3, ("Could not set userPassword "
1797 "attribute due to an objectClass "
1798 "violation -- ignoring\n"));
1800 return NT_STATUS_OK
;
1803 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1805 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1806 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1807 SAFE_FREE(ld_error
);
1809 #if defined(LDAP_CONSTRAINT_VIOLATION)
1810 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1811 return NT_STATUS_PASSWORD_RESTRICTION
;
1813 return NT_STATUS_UNSUCCESSFUL
;
1815 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1816 #ifdef DEBUG_PASSWORD
1817 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1820 ber_bvfree(retdata
);
1822 ldap_memfree(retoid
);
1828 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1829 /* may be password change below however */
1833 if (ldap_state
->is_nds_ldap
) {
1834 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1838 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1842 rc
= smbldap_add(ldap_state
->smbldap_state
,
1845 case LDAP_MOD_REPLACE
:
1846 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1850 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1852 return NT_STATUS_INVALID_PARAMETER
;
1855 if (rc
!=LDAP_SUCCESS
) {
1856 return NT_STATUS_UNSUCCESSFUL
;
1860 return NT_STATUS_OK
;
1863 /**********************************************************************
1864 Delete entry from LDAP for username.
1865 *********************************************************************/
1867 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1868 struct samu
* sam_acct
)
1870 struct ldapsam_privates
*priv
=
1871 (struct ldapsam_privates
*)my_methods
->private_data
;
1874 LDAPMessage
*msg
, *entry
;
1875 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1876 const char **attr_list
;
1877 TALLOC_CTX
*mem_ctx
;
1880 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1881 return NT_STATUS_INVALID_PARAMETER
;
1884 sname
= pdb_get_username(sam_acct
);
1886 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1889 mem_ctx
= talloc_new(NULL
);
1890 if (mem_ctx
== NULL
) {
1891 DEBUG(0, ("talloc_new failed\n"));
1895 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1896 if (attr_list
== NULL
) {
1900 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1902 if ((rc
!= LDAP_SUCCESS
) ||
1903 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1904 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1905 DEBUG(5, ("Could not find user %s\n", sname
));
1906 result
= NT_STATUS_NO_SUCH_USER
;
1910 rc
= ldapsam_delete_entry(
1911 priv
, mem_ctx
, entry
,
1912 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1913 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1916 result
= (rc
== LDAP_SUCCESS
) ?
1917 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1920 TALLOC_FREE(mem_ctx
);
1924 /**********************************************************************
1926 *********************************************************************/
1928 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1930 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1931 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1934 LDAPMessage
*result
= NULL
;
1935 LDAPMessage
*entry
= NULL
;
1936 LDAPMod
**mods
= NULL
;
1937 const char **attr_list
;
1939 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1941 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1942 if (pdb_get_username(newpwd
) == NULL
) {
1943 return NT_STATUS_INVALID_PARAMETER
;
1945 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1946 TALLOC_FREE( attr_list
);
1947 if (rc
!= LDAP_SUCCESS
) {
1948 return NT_STATUS_UNSUCCESSFUL
;
1950 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1951 my_methods
, PDB_CHANGED
);
1952 talloc_autofree_ldapmsg(newpwd
, result
);
1955 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1956 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1957 return NT_STATUS_UNSUCCESSFUL
;
1960 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1961 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
1963 return NT_STATUS_UNSUCCESSFUL
;
1966 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1968 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1969 pdb_element_is_changed
)) {
1970 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1973 ldap_mods_free(mods
,True
);
1974 return NT_STATUS_UNSUCCESSFUL
;
1977 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1978 && (mods
== NULL
)) {
1979 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1980 pdb_get_username(newpwd
)));
1982 return NT_STATUS_OK
;
1985 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
1988 ldap_mods_free(mods
,True
);
1994 * We need to set the backend private data to NULL here. For example
1995 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1996 * same one, and with the explicit delete / add logic for attribute
1997 * values the second time we would use the wrong "old" value which
1998 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2000 * The existing LDAPMessage is still being auto-freed by the
2003 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2006 if (!NT_STATUS_IS_OK(ret
)) {
2010 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2011 pdb_get_username(newpwd
)));
2012 return NT_STATUS_OK
;
2015 /***************************************************************************
2016 Renames a struct samu
2017 - The "rename user script" has full responsibility for changing everything
2018 ***************************************************************************/
2020 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2021 TALLOC_CTX
*tmp_ctx
,
2023 uint32_t member_rid
);
2025 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2026 TALLOC_CTX
*mem_ctx
,
2028 struct dom_sid
**pp_sids
,
2030 uint32_t *p_num_groups
);
2032 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2033 struct samu
*old_acct
,
2034 const char *newname
)
2036 const char *oldname
;
2038 char *rename_script
= NULL
;
2039 fstring oldname_lower
, newname_lower
;
2042 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2043 return NT_STATUS_INVALID_PARAMETER
;
2046 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2047 return NT_STATUS_INVALID_PARAMETER
;
2050 oldname
= pdb_get_username(old_acct
);
2052 /* rename the posix user */
2053 rename_script
= talloc_strdup(talloc_tos(), lp_renameuser_script());
2054 if (rename_script
== NULL
) {
2055 return NT_STATUS_NO_MEMORY
;
2058 if (!(*rename_script
)) {
2059 TALLOC_FREE(rename_script
);
2060 return NT_STATUS_ACCESS_DENIED
;
2063 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2066 /* We have to allow the account name to end with a '$'.
2067 Also, follow the semantics in _samr_create_user() and lower case the
2068 posix name but preserve the case in passdb */
2070 fstrcpy( oldname_lower
, oldname
);
2071 strlower_m( oldname_lower
);
2072 fstrcpy( newname_lower
, newname
);
2073 strlower_m( newname_lower
);
2074 rename_script
= realloc_string_sub2(rename_script
,
2079 if (!rename_script
) {
2080 return NT_STATUS_NO_MEMORY
;
2082 rename_script
= realloc_string_sub2(rename_script
,
2087 rc
= smbrun(rename_script
, NULL
);
2089 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2090 rename_script
, rc
));
2092 TALLOC_FREE(rename_script
);
2095 smb_nscd_flush_user_cache();
2099 return NT_STATUS_UNSUCCESSFUL
;
2101 return NT_STATUS_OK
;
2104 /**********************************************************************
2105 Add struct samu to LDAP.
2106 *********************************************************************/
2108 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2110 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2111 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2113 LDAPMessage
*result
= NULL
;
2114 LDAPMessage
*entry
= NULL
;
2115 LDAPMod
**mods
= NULL
;
2116 int ldap_op
= LDAP_MOD_REPLACE
;
2117 uint32_t num_result
;
2118 const char **attr_list
;
2119 char *escape_user
= NULL
;
2120 const char *username
= pdb_get_username(newpwd
);
2121 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2122 char *filter
= NULL
;
2124 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2125 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2128 return NT_STATUS_NO_MEMORY
;
2131 if (!username
|| !*username
) {
2132 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2133 status
= NT_STATUS_INVALID_PARAMETER
;
2137 /* free this list after the second search or in case we exit on failure */
2138 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2140 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2142 if (rc
!= LDAP_SUCCESS
) {
2146 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2147 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2151 ldap_msgfree(result
);
2154 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2155 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2157 if (rc
== LDAP_SUCCESS
) {
2158 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2159 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2160 "already in the base, with samba "
2161 "attributes\n", sid_string_dbg(sid
)));
2164 ldap_msgfree(result
);
2169 /* does the entry already exist but without a samba attributes?
2170 we need to return the samba attributes here */
2172 escape_user
= escape_ldap_string(talloc_tos(), username
);
2173 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2175 status
= NT_STATUS_NO_MEMORY
;
2178 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2179 TALLOC_FREE(escape_user
);
2181 status
= NT_STATUS_NO_MEMORY
;
2185 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2186 filter
, attr_list
, &result
);
2187 if ( rc
!= LDAP_SUCCESS
) {
2191 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2193 if (num_result
> 1) {
2194 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2198 /* Check if we need to update an existing entry */
2199 if (num_result
== 1) {
2200 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2201 ldap_op
= LDAP_MOD_REPLACE
;
2202 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2203 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2205 status
= NT_STATUS_NO_MEMORY
;
2209 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2211 /* There might be a SID for this account already - say an idmap entry */
2213 filter
= talloc_asprintf(ctx
,
2214 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2215 get_userattr_key2string(ldap_state
->schema_ver
,
2216 LDAP_ATTR_USER_SID
),
2217 sid_string_talloc(ctx
, sid
),
2218 LDAP_OBJ_IDMAP_ENTRY
,
2219 LDAP_OBJ_SID_ENTRY
);
2221 status
= NT_STATUS_NO_MEMORY
;
2225 /* free old result before doing a new search */
2226 if (result
!= NULL
) {
2227 ldap_msgfree(result
);
2230 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2231 filter
, attr_list
, &result
);
2233 if ( rc
!= LDAP_SUCCESS
) {
2237 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2239 if (num_result
> 1) {
2240 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2244 /* Check if we need to update an existing entry */
2245 if (num_result
== 1) {
2247 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2248 ldap_op
= LDAP_MOD_REPLACE
;
2249 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2250 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2252 status
= NT_STATUS_NO_MEMORY
;
2258 if (num_result
== 0) {
2259 char *escape_username
;
2260 /* Check if we need to add an entry */
2261 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2262 ldap_op
= LDAP_MOD_ADD
;
2264 escape_username
= escape_rdn_val_string_alloc(username
);
2265 if (!escape_username
) {
2266 status
= NT_STATUS_NO_MEMORY
;
2270 if (username
[strlen(username
)-1] == '$') {
2271 dn
= talloc_asprintf(ctx
,
2274 lp_ldap_machine_suffix());
2276 dn
= talloc_asprintf(ctx
,
2279 lp_ldap_user_suffix());
2282 SAFE_FREE(escape_username
);
2284 status
= NT_STATUS_NO_MEMORY
;
2289 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2290 pdb_element_is_set_or_changed
)) {
2291 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2293 ldap_mods_free(mods
, true);
2299 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2302 switch ( ldap_state
->schema_ver
) {
2303 case SCHEMAVER_SAMBAACCOUNT
:
2304 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2306 case SCHEMAVER_SAMBASAMACCOUNT
:
2307 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2310 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2314 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2315 if (!NT_STATUS_IS_OK(ret
)) {
2316 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2317 pdb_get_username(newpwd
),dn
));
2318 ldap_mods_free(mods
, true);
2322 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2323 ldap_mods_free(mods
, true);
2325 status
= NT_STATUS_OK
;
2331 ldap_msgfree(result
);
2336 /**********************************************************************
2337 *********************************************************************/
2339 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2341 LDAPMessage
** result
)
2343 int scope
= LDAP_SCOPE_SUBTREE
;
2345 const char **attr_list
;
2347 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2348 rc
= smbldap_search(ldap_state
->smbldap_state
,
2349 lp_ldap_suffix (), scope
,
2350 filter
, attr_list
, 0, result
);
2351 TALLOC_FREE(attr_list
);
2356 /**********************************************************************
2357 *********************************************************************/
2359 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2360 GROUP_MAP
*map
, LDAPMessage
*entry
)
2363 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2365 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2366 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2367 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2372 temp
= smbldap_talloc_single_attribute(
2373 ldap_state
->smbldap_state
->ldap_struct
,
2375 get_attr_key2string(groupmap_attr_list
,
2376 LDAP_ATTR_GIDNUMBER
),
2379 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2380 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2384 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2386 map
->gid
= (gid_t
)atol(temp
);
2389 temp
= smbldap_talloc_single_attribute(
2390 ldap_state
->smbldap_state
->ldap_struct
,
2392 get_attr_key2string(groupmap_attr_list
,
2393 LDAP_ATTR_GROUP_SID
),
2396 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2397 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2402 if (!string_to_sid(&map
->sid
, temp
)) {
2403 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2409 temp
= smbldap_talloc_single_attribute(
2410 ldap_state
->smbldap_state
->ldap_struct
,
2412 get_attr_key2string(groupmap_attr_list
,
2413 LDAP_ATTR_GROUP_TYPE
),
2416 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2417 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2421 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2423 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2424 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2425 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2431 temp
= smbldap_talloc_single_attribute(
2432 ldap_state
->smbldap_state
->ldap_struct
,
2434 get_attr_key2string(groupmap_attr_list
,
2435 LDAP_ATTR_DISPLAY_NAME
),
2438 temp
= smbldap_talloc_single_attribute(
2439 ldap_state
->smbldap_state
->ldap_struct
,
2441 get_attr_key2string(groupmap_attr_list
,
2445 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2446 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2451 map
->nt_name
= talloc_strdup(map
, temp
);
2452 if (!map
->nt_name
) {
2458 temp
= smbldap_talloc_single_attribute(
2459 ldap_state
->smbldap_state
->ldap_struct
,
2461 get_attr_key2string(groupmap_attr_list
,
2465 temp
= talloc_strdup(ctx
, "");
2471 map
->comment
= talloc_strdup(map
, temp
);
2472 if (!map
->comment
) {
2477 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2478 idmap_cache_set_sid2gid(&map
->sid
, map
->gid
);
2485 /**********************************************************************
2486 *********************************************************************/
2488 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2492 struct ldapsam_privates
*ldap_state
=
2493 (struct ldapsam_privates
*)methods
->private_data
;
2494 LDAPMessage
*result
= NULL
;
2495 LDAPMessage
*entry
= NULL
;
2498 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2500 return NT_STATUS_NO_SUCH_GROUP
;
2503 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2506 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2508 ldap_msgfree(result
);
2509 return NT_STATUS_NO_SUCH_GROUP
;
2513 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2514 "count=%d\n", filter
, count
));
2515 ldap_msgfree(result
);
2516 return NT_STATUS_NO_SUCH_GROUP
;
2519 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2522 ldap_msgfree(result
);
2523 return NT_STATUS_UNSUCCESSFUL
;
2526 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2527 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2528 "group filter %s\n", filter
));
2529 ldap_msgfree(result
);
2530 return NT_STATUS_NO_SUCH_GROUP
;
2533 ldap_msgfree(result
);
2534 return NT_STATUS_OK
;
2537 /**********************************************************************
2538 *********************************************************************/
2540 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2543 char *filter
= NULL
;
2547 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2549 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2550 sid_to_fstring(tmp
, &sid
)) < 0) {
2551 return NT_STATUS_NO_MEMORY
;
2554 status
= ldapsam_getgroup(methods
, filter
, map
);
2559 /**********************************************************************
2560 *********************************************************************/
2562 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2565 char *filter
= NULL
;
2568 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2570 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2571 (unsigned long)gid
) < 0) {
2572 return NT_STATUS_NO_MEMORY
;
2575 status
= ldapsam_getgroup(methods
, filter
, map
);
2580 /**********************************************************************
2581 *********************************************************************/
2583 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2586 char *filter
= NULL
;
2587 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2591 return NT_STATUS_NO_MEMORY
;
2594 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2596 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2597 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2599 TALLOC_FREE(escape_name
);
2600 return NT_STATUS_NO_MEMORY
;
2603 TALLOC_FREE(escape_name
);
2604 status
= ldapsam_getgroup(methods
, filter
, map
);
2609 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2611 const struct dom_sid
*domain_sid
,
2617 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2618 str
, sizeof(str
)-1)) {
2619 DEBUG(10, ("Could not find sambaSID attribute\n"));
2623 if (!string_to_sid(&sid
, str
)) {
2624 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2628 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2629 DEBUG(10, ("SID %s is not in expected domain %s\n",
2630 str
, sid_string_dbg(domain_sid
)));
2634 if (!sid_peek_rid(&sid
, rid
)) {
2635 DEBUG(10, ("Could not peek into RID\n"));
2642 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2643 TALLOC_CTX
*mem_ctx
,
2644 const struct dom_sid
*group
,
2645 uint32_t **pp_member_rids
,
2646 size_t *p_num_members
)
2648 struct ldapsam_privates
*ldap_state
=
2649 (struct ldapsam_privates
*)methods
->private_data
;
2650 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2651 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2652 const char *sid_attrs
[] = { "sambaSID", NULL
};
2653 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2654 LDAPMessage
*result
= NULL
;
2657 char **values
= NULL
;
2662 *pp_member_rids
= NULL
;
2665 filter
= talloc_asprintf(mem_ctx
,
2666 "(&(objectClass=%s)"
2669 LDAP_OBJ_POSIXGROUP
,
2671 sid_string_talloc(mem_ctx
, group
));
2672 if (filter
== NULL
) {
2673 ret
= NT_STATUS_NO_MEMORY
;
2677 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2678 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2681 if (rc
!= LDAP_SUCCESS
)
2684 talloc_autofree_ldapmsg(mem_ctx
, result
);
2686 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2689 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2690 sid_string_dbg(group
)));
2691 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2696 ret
= NT_STATUS_NO_SUCH_GROUP
;
2700 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2704 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2706 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2707 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2711 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2713 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2715 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2716 if (filter
== NULL
) {
2717 ret
= NT_STATUS_NO_MEMORY
;
2721 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2722 char *escape_memberuid
;
2724 escape_memberuid
= escape_ldap_string(talloc_tos(),
2726 if (escape_memberuid
== NULL
) {
2727 ret
= NT_STATUS_NO_MEMORY
;
2731 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2732 TALLOC_FREE(escape_memberuid
);
2733 if (filter
== NULL
) {
2734 ret
= NT_STATUS_NO_MEMORY
;
2739 filter
= talloc_asprintf_append_buffer(filter
, "))");
2740 if (filter
== NULL
) {
2741 ret
= NT_STATUS_NO_MEMORY
;
2745 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2746 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2749 if (rc
!= LDAP_SUCCESS
)
2752 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2753 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2755 talloc_autofree_ldapmsg(mem_ctx
, result
);
2757 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2759 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2765 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2769 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2770 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2771 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2775 if (!string_to_sid(&sid
, sidstr
))
2778 if (!sid_check_is_in_our_domain(&sid
)) {
2779 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2780 "in our domain\n"));
2781 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2785 sid_peek_rid(&sid
, &rid
);
2787 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2789 ret
= NT_STATUS_NO_MEMORY
;
2795 filter
= talloc_asprintf(mem_ctx
,
2796 "(&(objectClass=%s)"
2798 LDAP_OBJ_SAMBASAMACCOUNT
,
2801 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2802 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2805 if (rc
!= LDAP_SUCCESS
)
2808 talloc_autofree_ldapmsg(mem_ctx
, result
);
2810 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2812 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2816 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2818 get_global_sam_sid(),
2820 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2821 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2825 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2827 ret
= NT_STATUS_NO_MEMORY
;
2837 ldap_value_free(values
);
2842 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2843 TALLOC_CTX
*mem_ctx
,
2845 struct dom_sid
**pp_sids
,
2847 uint32_t *p_num_groups
)
2849 struct ldapsam_privates
*ldap_state
=
2850 (struct ldapsam_privates
*)methods
->private_data
;
2851 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2853 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2856 LDAPMessage
*result
= NULL
;
2858 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2862 gid_t primary_gid
= -1;
2867 if (pdb_get_username(user
) == NULL
) {
2868 return NT_STATUS_INVALID_PARAMETER
;
2871 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2872 if (escape_name
== NULL
)
2873 return NT_STATUS_NO_MEMORY
;
2875 if (user
->unix_pw
) {
2876 primary_gid
= user
->unix_pw
->pw_gid
;
2878 /* retrieve the users primary gid */
2879 filter
= talloc_asprintf(mem_ctx
,
2880 "(&(objectClass=%s)(uid=%s))",
2881 LDAP_OBJ_SAMBASAMACCOUNT
,
2883 if (filter
== NULL
) {
2884 ret
= NT_STATUS_NO_MEMORY
;
2888 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2889 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2891 if (rc
!= LDAP_SUCCESS
)
2894 talloc_autofree_ldapmsg(mem_ctx
, result
);
2896 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2900 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2901 ret
= NT_STATUS_NO_SUCH_USER
;
2904 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2906 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2908 DEBUG (1, ("Unable to find the member's gid!\n"));
2909 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2912 primary_gid
= strtoul(gidstr
, NULL
, 10);
2915 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2916 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2921 filter
= talloc_asprintf(mem_ctx
,
2922 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2923 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2924 if (filter
== NULL
) {
2925 ret
= NT_STATUS_NO_MEMORY
;
2929 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2930 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2932 if (rc
!= LDAP_SUCCESS
)
2935 talloc_autofree_ldapmsg(mem_ctx
, result
);
2943 /* We need to add the primary group as the first gid/sid */
2945 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2946 ret
= NT_STATUS_NO_MEMORY
;
2950 /* This sid will be replaced later */
2952 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2954 if (!NT_STATUS_IS_OK(ret
)) {
2958 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2960 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2967 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2969 str
, sizeof(str
)-1))
2972 if (!string_to_sid(&sid
, str
))
2975 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2977 str
, sizeof(str
)-1))
2980 gid
= strtoul(str
, &end
, 10);
2982 if (PTR_DIFF(end
, str
) != strlen(str
))
2985 if (gid
== primary_gid
) {
2986 sid_copy(&(*pp_sids
)[0], &sid
);
2988 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2990 ret
= NT_STATUS_NO_MEMORY
;
2993 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2995 if (!NT_STATUS_IS_OK(ret
)) {
3001 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3002 DEBUG(3, ("primary group of [%s] not found\n",
3003 pdb_get_username(user
)));
3004 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
3008 *p_num_groups
= num_sids
;
3014 TALLOC_FREE(escape_name
);
3018 /**********************************************************************
3019 * Augment a posixGroup object with a sambaGroupMapping domgroup
3020 *********************************************************************/
3022 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3023 struct ldapsam_privates
*ldap_state
,
3026 const char *filter
, *dn
;
3027 LDAPMessage
*msg
, *entry
;
3031 filter
= talloc_asprintf(mem_ctx
,
3032 "(&(objectClass=%s)(gidNumber=%u))",
3033 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3034 if (filter
== NULL
) {
3035 return NT_STATUS_NO_MEMORY
;
3038 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3039 get_attr_list(mem_ctx
, groupmap_attr_list
),
3041 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3043 if ((rc
!= LDAP_SUCCESS
) ||
3044 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3045 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3046 return NT_STATUS_NO_SUCH_GROUP
;
3049 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3051 return NT_STATUS_NO_MEMORY
;
3055 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3057 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3058 sid_string_talloc(mem_ctx
, &map
->sid
));
3059 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3060 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3061 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3063 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3065 talloc_autofree_ldapmod(mem_ctx
, mods
);
3067 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3068 if (rc
!= LDAP_SUCCESS
) {
3069 return NT_STATUS_ACCESS_DENIED
;
3072 return NT_STATUS_OK
;
3075 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3078 struct ldapsam_privates
*ldap_state
=
3079 (struct ldapsam_privates
*)methods
->private_data
;
3080 LDAPMessage
*msg
= NULL
;
3081 LDAPMod
**mods
= NULL
;
3082 const char *attrs
[] = { NULL
};
3086 TALLOC_CTX
*mem_ctx
;
3093 mem_ctx
= talloc_new(NULL
);
3094 if (mem_ctx
== NULL
) {
3095 DEBUG(0, ("talloc_new failed\n"));
3096 return NT_STATUS_NO_MEMORY
;
3099 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3100 sid_string_talloc(mem_ctx
, &map
->sid
));
3101 if (filter
== NULL
) {
3102 result
= NT_STATUS_NO_MEMORY
;
3106 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3107 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3108 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3110 if ((rc
== LDAP_SUCCESS
) &&
3111 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3113 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3114 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3115 result
= NT_STATUS_GROUP_EXISTS
;
3119 switch (map
->sid_name_use
) {
3121 case SID_NAME_DOM_GRP
:
3122 /* To map a domain group we need to have a posix group
3124 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3128 case SID_NAME_ALIAS
:
3129 if (!sid_check_is_in_our_domain(&map
->sid
)
3130 && !sid_check_is_in_builtin(&map
->sid
) )
3132 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3133 sid_string_dbg(&map
->sid
)));
3134 result
= NT_STATUS_INVALID_PARAMETER
;
3140 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3141 sid_type_lookup(map
->sid_name_use
)));
3142 result
= NT_STATUS_INVALID_PARAMETER
;
3146 /* Domain groups have been mapped in a separate routine, we have to
3147 * create an alias now */
3149 if (map
->gid
== -1) {
3150 DEBUG(10, ("Refusing to map gid==-1\n"));
3151 result
= NT_STATUS_INVALID_PARAMETER
;
3155 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3156 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3157 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3158 result
= NT_STATUS_GROUP_EXISTS
;
3162 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3163 * the best we can get out of LDAP. */
3165 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3166 sid_string_talloc(mem_ctx
, &map
->sid
),
3167 lp_ldap_group_suffix());
3169 result
= NT_STATUS_NO_MEMORY
;
3175 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3176 LDAP_OBJ_SID_ENTRY
);
3177 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3179 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3180 sid_string_talloc(mem_ctx
, &map
->sid
));
3181 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3182 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3183 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3185 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3187 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3188 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3189 talloc_autofree_ldapmod(mem_ctx
, mods
);
3191 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3193 result
= (rc
== LDAP_SUCCESS
) ?
3194 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3197 TALLOC_FREE(mem_ctx
);
3201 /**********************************************************************
3202 * Update a group mapping entry. We're quite strict about what can be changed:
3203 * Only the description and displayname may be changed. It simply does not
3204 * make any sense to change the SID, gid or the type in a mapping.
3205 *********************************************************************/
3207 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3210 struct ldapsam_privates
*ldap_state
=
3211 (struct ldapsam_privates
*)methods
->private_data
;
3213 const char *filter
, *dn
;
3214 LDAPMessage
*msg
= NULL
;
3215 LDAPMessage
*entry
= NULL
;
3216 LDAPMod
**mods
= NULL
;
3217 TALLOC_CTX
*mem_ctx
;
3220 mem_ctx
= talloc_new(NULL
);
3221 if (mem_ctx
== NULL
) {
3222 DEBUG(0, ("talloc_new failed\n"));
3223 return NT_STATUS_NO_MEMORY
;
3226 /* Make 100% sure that sid, gid and type are not changed by looking up
3227 * exactly the values we're given in LDAP. */
3229 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3230 "(sambaSid=%s)(gidNumber=%u)"
3231 "(sambaGroupType=%d))",
3233 sid_string_talloc(mem_ctx
, &map
->sid
),
3234 (unsigned int)map
->gid
, map
->sid_name_use
);
3235 if (filter
== NULL
) {
3236 result
= NT_STATUS_NO_MEMORY
;
3240 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3241 get_attr_list(mem_ctx
, groupmap_attr_list
),
3243 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3245 if ((rc
!= LDAP_SUCCESS
) ||
3246 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3247 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3248 result
= NT_STATUS_NO_SUCH_GROUP
;
3252 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3255 result
= NT_STATUS_NO_MEMORY
;
3260 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3262 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3264 talloc_autofree_ldapmod(mem_ctx
, mods
);
3267 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3268 "nothing to do\n"));
3269 result
= NT_STATUS_OK
;
3273 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3275 if (rc
!= LDAP_SUCCESS
) {
3276 result
= NT_STATUS_ACCESS_DENIED
;
3280 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3281 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3283 result
= NT_STATUS_OK
;
3286 TALLOC_FREE(mem_ctx
);
3290 /**********************************************************************
3291 *********************************************************************/
3293 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3296 struct ldapsam_privates
*priv
=
3297 (struct ldapsam_privates
*)methods
->private_data
;
3298 LDAPMessage
*msg
, *entry
;
3301 TALLOC_CTX
*mem_ctx
;
3304 mem_ctx
= talloc_new(NULL
);
3305 if (mem_ctx
== NULL
) {
3306 DEBUG(0, ("talloc_new failed\n"));
3307 return NT_STATUS_NO_MEMORY
;
3310 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3311 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3312 sid_string_talloc(mem_ctx
, &sid
));
3313 if (filter
== NULL
) {
3314 result
= NT_STATUS_NO_MEMORY
;
3317 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3318 get_attr_list(mem_ctx
, groupmap_attr_list
),
3320 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3322 if ((rc
!= LDAP_SUCCESS
) ||
3323 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3324 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3325 result
= NT_STATUS_NO_SUCH_GROUP
;
3329 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3330 get_attr_list(mem_ctx
,
3331 groupmap_attr_list_to_delete
));
3333 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3334 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3335 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3336 const char *attrs
[] = { "sambaGroupType", "description",
3337 "displayName", "sambaSIDList",
3340 /* Second try. Don't delete the sambaSID attribute, this is
3341 for "old" entries that are tacked on a winbind
3344 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3345 LDAP_OBJ_GROUPMAP
, attrs
);
3348 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3349 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3350 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3351 const char *attrs
[] = { "sambaGroupType", "description",
3352 "displayName", "sambaSIDList",
3353 "gidNumber", NULL
};
3355 /* Third try. This is a post-3.0.21 alias (containing only
3356 * sambaSidEntry and sambaGroupMapping classes), we also have
3357 * to delete the gidNumber attribute, only the sambaSidEntry
3360 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3361 LDAP_OBJ_GROUPMAP
, attrs
);
3364 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3367 TALLOC_FREE(mem_ctx
);
3371 /**********************************************************************
3372 *********************************************************************/
3374 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3377 struct ldapsam_privates
*ldap_state
=
3378 (struct ldapsam_privates
*)my_methods
->private_data
;
3379 char *filter
= NULL
;
3381 const char **attr_list
;
3383 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3385 return NT_STATUS_NO_MEMORY
;
3387 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3388 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3389 LDAP_SCOPE_SUBTREE
, filter
,
3390 attr_list
, 0, &ldap_state
->result
);
3391 TALLOC_FREE(attr_list
);
3393 if (rc
!= LDAP_SUCCESS
) {
3394 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3395 ldap_err2string(rc
)));
3396 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3397 lp_ldap_suffix(), filter
));
3398 ldap_msgfree(ldap_state
->result
);
3399 ldap_state
->result
= NULL
;
3400 TALLOC_FREE(filter
);
3401 return NT_STATUS_UNSUCCESSFUL
;
3404 TALLOC_FREE(filter
);
3406 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3407 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3408 ldap_state
->result
)));
3411 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3412 ldap_state
->result
);
3413 ldap_state
->index
= 0;
3415 return NT_STATUS_OK
;
3418 /**********************************************************************
3419 *********************************************************************/
3421 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3423 ldapsam_endsampwent(my_methods
);
3426 /**********************************************************************
3427 *********************************************************************/
3429 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3432 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3433 struct ldapsam_privates
*ldap_state
=
3434 (struct ldapsam_privates
*)my_methods
->private_data
;
3438 if (!ldap_state
->entry
)
3441 ldap_state
->index
++;
3442 bret
= init_group_from_ldap(ldap_state
, map
,
3446 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3450 return NT_STATUS_OK
;
3453 /**********************************************************************
3454 *********************************************************************/
3456 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3457 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3458 GROUP_MAP
***pp_rmap
,
3459 size_t *p_num_entries
,
3462 GROUP_MAP
*map
= NULL
;
3468 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3469 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3471 return NT_STATUS_ACCESS_DENIED
;
3476 map
= talloc_zero(NULL
, GROUP_MAP
);
3478 return NT_STATUS_NO_MEMORY
;
3481 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3486 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3487 sid_name_use
!= map
->sid_name_use
) {
3488 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3489 "not of the requested type\n",
3493 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3494 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3495 "non mapped\n", map
->nt_name
));
3499 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3500 GROUP_MAP
*, entries
+ 1);
3502 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3503 "enlarge group map!\n"));
3504 return NT_STATUS_UNSUCCESSFUL
;
3507 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3512 ldapsam_endsamgrent(methods
);
3514 *p_num_entries
= entries
;
3516 return NT_STATUS_OK
;
3519 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3520 const struct dom_sid
*alias
,
3521 const struct dom_sid
*member
,
3524 struct ldapsam_privates
*ldap_state
=
3525 (struct ldapsam_privates
*)methods
->private_data
;
3527 LDAPMessage
*result
= NULL
;
3528 LDAPMessage
*entry
= NULL
;
3530 LDAPMod
**mods
= NULL
;
3532 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3535 char *filter
= NULL
;
3537 if (sid_check_is_in_builtin(alias
)) {
3538 type
= SID_NAME_ALIAS
;
3541 if (sid_check_is_in_our_domain(alias
)) {
3542 type
= SID_NAME_ALIAS
;
3545 if (type
== SID_NAME_USE_NONE
) {
3546 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3547 sid_string_dbg(alias
)));
3548 return NT_STATUS_NO_SUCH_ALIAS
;
3551 if (asprintf(&filter
,
3552 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3553 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3555 return NT_STATUS_NO_MEMORY
;
3558 if (ldapsam_search_one_group(ldap_state
, filter
,
3559 &result
) != LDAP_SUCCESS
) {
3561 return NT_STATUS_NO_SUCH_ALIAS
;
3564 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3568 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3569 ldap_msgfree(result
);
3571 return NT_STATUS_NO_SUCH_ALIAS
;
3575 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3576 "filter %s: count=%d\n", filter
, count
));
3577 ldap_msgfree(result
);
3579 return NT_STATUS_NO_SUCH_ALIAS
;
3584 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3588 ldap_msgfree(result
);
3589 return NT_STATUS_UNSUCCESSFUL
;
3592 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3594 ldap_msgfree(result
);
3595 return NT_STATUS_UNSUCCESSFUL
;
3598 smbldap_set_mod(&mods
, modop
,
3599 get_attr_key2string(groupmap_attr_list
,
3600 LDAP_ATTR_SID_LIST
),
3601 sid_to_fstring(tmp
, member
));
3603 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3605 ldap_mods_free(mods
, True
);
3606 ldap_msgfree(result
);
3609 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3610 return NT_STATUS_MEMBER_IN_ALIAS
;
3613 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3614 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3617 if (rc
!= LDAP_SUCCESS
) {
3618 return NT_STATUS_UNSUCCESSFUL
;
3621 return NT_STATUS_OK
;
3624 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3625 const struct dom_sid
*alias
,
3626 const struct dom_sid
*member
)
3628 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3631 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3632 const struct dom_sid
*alias
,
3633 const struct dom_sid
*member
)
3635 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3639 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3640 const struct dom_sid
*alias
,
3641 TALLOC_CTX
*mem_ctx
,
3642 struct dom_sid
**pp_members
,
3643 size_t *p_num_members
)
3645 struct ldapsam_privates
*ldap_state
=
3646 (struct ldapsam_privates
*)methods
->private_data
;
3647 LDAPMessage
*result
= NULL
;
3648 LDAPMessage
*entry
= NULL
;
3650 char **values
= NULL
;
3652 char *filter
= NULL
;
3653 uint32_t num_members
= 0;
3654 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3660 if (sid_check_is_in_builtin(alias
)) {
3661 type
= SID_NAME_ALIAS
;
3664 if (sid_check_is_in_our_domain(alias
)) {
3665 type
= SID_NAME_ALIAS
;
3668 if (type
== SID_NAME_USE_NONE
) {
3669 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3670 sid_string_dbg(alias
)));
3671 return NT_STATUS_NO_SUCH_ALIAS
;
3674 if (asprintf(&filter
,
3675 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3676 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3678 return NT_STATUS_NO_MEMORY
;
3681 if (ldapsam_search_one_group(ldap_state
, filter
,
3682 &result
) != LDAP_SUCCESS
) {
3684 return NT_STATUS_NO_SUCH_ALIAS
;
3687 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3691 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3692 ldap_msgfree(result
);
3694 return NT_STATUS_NO_SUCH_ALIAS
;
3698 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3699 "filter %s: count=%d\n", filter
, count
));
3700 ldap_msgfree(result
);
3702 return NT_STATUS_NO_SUCH_ALIAS
;
3707 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3711 ldap_msgfree(result
);
3712 return NT_STATUS_UNSUCCESSFUL
;
3715 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3717 get_attr_key2string(groupmap_attr_list
,
3718 LDAP_ATTR_SID_LIST
));
3720 if (values
== NULL
) {
3721 ldap_msgfree(result
);
3722 return NT_STATUS_OK
;
3725 count
= ldap_count_values(values
);
3727 for (i
=0; i
<count
; i
++) {
3728 struct dom_sid member
;
3731 if (!string_to_sid(&member
, values
[i
]))
3734 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3736 if (!NT_STATUS_IS_OK(status
)) {
3737 ldap_value_free(values
);
3738 ldap_msgfree(result
);
3743 *p_num_members
= num_members
;
3744 ldap_value_free(values
);
3745 ldap_msgfree(result
);
3747 return NT_STATUS_OK
;
3750 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3751 TALLOC_CTX
*mem_ctx
,
3752 const struct dom_sid
*domain_sid
,
3753 const struct dom_sid
*members
,
3755 uint32_t **pp_alias_rids
,
3756 size_t *p_num_alias_rids
)
3758 struct ldapsam_privates
*ldap_state
=
3759 (struct ldapsam_privates
*)methods
->private_data
;
3762 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3764 LDAPMessage
*result
= NULL
;
3765 LDAPMessage
*entry
= NULL
;
3769 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3770 bool is_builtin
= false;
3771 bool sid_added
= false;
3773 *pp_alias_rids
= NULL
;
3774 *p_num_alias_rids
= 0;
3776 if (sid_check_is_builtin(domain_sid
)) {
3778 type
= SID_NAME_ALIAS
;
3781 if (sid_check_is_domain(domain_sid
)) {
3782 type
= SID_NAME_ALIAS
;
3785 if (type
== SID_NAME_USE_NONE
) {
3786 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3787 sid_string_dbg(domain_sid
)));
3788 return NT_STATUS_UNSUCCESSFUL
;
3791 if (num_members
== 0) {
3792 return NT_STATUS_OK
;
3795 filter
= talloc_asprintf(mem_ctx
,
3796 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3797 LDAP_OBJ_GROUPMAP
, type
);
3799 for (i
=0; i
<num_members
; i
++)
3800 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3802 sid_string_talloc(mem_ctx
,
3805 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3807 if (filter
== NULL
) {
3808 return NT_STATUS_NO_MEMORY
;
3812 ldap_state
->search_cache
.filter
&&
3813 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3814 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3815 result
= ldap_state
->search_cache
.result
;
3816 ldap_state
->search_cache
.result
= NULL
;
3818 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3819 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3820 if (rc
!= LDAP_SUCCESS
) {
3821 return NT_STATUS_UNSUCCESSFUL
;
3823 talloc_autofree_ldapmsg(filter
, result
);
3826 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3828 for (entry
= ldap_first_entry(ldap_struct
, result
);
3830 entry
= ldap_next_entry(ldap_struct
, entry
))
3836 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3842 if (!string_to_sid(&sid
, sid_str
))
3845 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3850 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3851 p_num_alias_rids
)) {
3852 return NT_STATUS_NO_MEMORY
;
3856 if (!is_builtin
&& !sid_added
) {
3857 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3859 * Note: result is a talloc child of filter because of the
3860 * talloc_autofree_ldapmsg() usage
3862 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3863 ldap_state
->search_cache
.result
= result
;
3866 return NT_STATUS_OK
;
3869 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3870 enum pdb_policy_type type
,
3873 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3875 LDAPMod
**mods
= NULL
;
3876 fstring value_string
;
3877 const char *policy_attr
= NULL
;
3879 struct ldapsam_privates
*ldap_state
=
3880 (struct ldapsam_privates
*)methods
->private_data
;
3882 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3884 if (!ldap_state
->domain_dn
) {
3885 return NT_STATUS_INVALID_PARAMETER
;
3888 policy_attr
= get_account_policy_attr(type
);
3889 if (policy_attr
== NULL
) {
3890 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3895 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3897 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3899 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3902 ldap_mods_free(mods
, True
);
3904 if (rc
!= LDAP_SUCCESS
) {
3908 if (!cache_account_policy_set(type
, value
)) {
3909 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3910 "update local tdb cache\n"));
3914 return NT_STATUS_OK
;
3917 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3918 enum pdb_policy_type type
,
3921 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3925 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3926 enum pdb_policy_type type
,
3929 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3930 LDAPMessage
*result
= NULL
;
3931 LDAPMessage
*entry
= NULL
;
3936 const char *policy_attr
= NULL
;
3938 struct ldapsam_privates
*ldap_state
=
3939 (struct ldapsam_privates
*)methods
->private_data
;
3941 const char *attrs
[2];
3943 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3945 if (!ldap_state
->domain_dn
) {
3946 return NT_STATUS_INVALID_PARAMETER
;
3949 policy_attr
= get_account_policy_attr(type
);
3951 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3952 "policy index: %d\n", type
));
3956 attrs
[0] = policy_attr
;
3959 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3960 if (filter
== NULL
) {
3961 return NT_STATUS_NO_MEMORY
;
3963 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3964 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
3966 TALLOC_FREE(filter
);
3967 if (rc
!= LDAP_SUCCESS
) {
3971 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3976 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3977 if (entry
== NULL
) {
3981 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3986 *value
= (uint32_t)atol(vals
[0]);
3988 ntstatus
= NT_STATUS_OK
;
3992 ldap_value_free(vals
);
3993 ldap_msgfree(result
);
3998 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4000 - if user hasn't decided to use account policies inside LDAP just reuse the
4003 - if there is a valid cache entry, return that
4004 - if there is an LDAP entry, update cache and return
4005 - otherwise set to default, update cache and return
4009 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4010 enum pdb_policy_type type
,
4013 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4015 if (cache_account_policy_get(type
, value
)) {
4016 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4018 return NT_STATUS_OK
;
4021 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4023 if (NT_STATUS_IS_OK(ntstatus
)) {
4027 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4031 /* should we automagically migrate old tdb value here ? */
4032 if (account_policy_get(type
, value
))
4035 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4036 "default\n", type
));
4039 if (!account_policy_get_default(type
, value
)) {
4045 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4046 if (!NT_STATUS_IS_OK(ntstatus
)) {
4052 if (!cache_account_policy_set(type
, *value
)) {
4053 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4054 "tdb as a cache\n"));
4055 return NT_STATUS_UNSUCCESSFUL
;
4058 return NT_STATUS_OK
;
4061 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4062 const struct dom_sid
*domain_sid
,
4066 enum lsa_SidType
*attrs
)
4068 struct ldapsam_privates
*ldap_state
=
4069 (struct ldapsam_privates
*)methods
->private_data
;
4070 LDAPMessage
*msg
= NULL
;
4072 char *allsids
= NULL
;
4073 int i
, rc
, num_mapped
;
4074 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4075 TALLOC_CTX
*mem_ctx
;
4079 mem_ctx
= talloc_new(NULL
);
4080 if (mem_ctx
== NULL
) {
4081 DEBUG(0, ("talloc_new failed\n"));
4085 if (!sid_check_is_builtin(domain_sid
) &&
4086 !sid_check_is_domain(domain_sid
)) {
4087 result
= NT_STATUS_INVALID_PARAMETER
;
4091 if (num_rids
== 0) {
4092 result
= NT_STATUS_NONE_MAPPED
;
4096 for (i
=0; i
<num_rids
; i
++)
4097 attrs
[i
] = SID_NAME_UNKNOWN
;
4099 allsids
= talloc_strdup(mem_ctx
, "");
4100 if (allsids
== NULL
) {
4104 for (i
=0; i
<num_rids
; i
++) {
4106 sid_compose(&sid
, domain_sid
, rids
[i
]);
4107 allsids
= talloc_asprintf_append_buffer(
4108 allsids
, "(sambaSid=%s)",
4109 sid_string_talloc(mem_ctx
, &sid
));
4110 if (allsids
== NULL
) {
4115 /* First look for users */
4119 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4121 filter
= talloc_asprintf(
4122 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4123 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4125 if (filter
== NULL
) {
4129 rc
= smbldap_search(ldap_state
->smbldap_state
,
4130 lp_ldap_user_suffix(),
4131 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4133 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4136 if (rc
!= LDAP_SUCCESS
)
4139 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4142 for (entry
= ldap_first_entry(ld
, msg
);
4144 entry
= ldap_next_entry(ld
, entry
)) {
4149 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4151 DEBUG(2, ("Could not find sid from ldap entry\n"));
4155 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4158 DEBUG(2, ("Could not retrieve uid attribute\n"));
4162 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4163 if (rid
== rids
[rid_index
])
4167 if (rid_index
== num_rids
) {
4168 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4172 attrs
[rid_index
] = SID_NAME_USER
;
4173 names
[rid_index
] = name
;
4177 if (num_mapped
== num_rids
) {
4178 /* No need to look for groups anymore -- we're done */
4179 result
= NT_STATUS_OK
;
4183 /* Same game for groups */
4187 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4188 "sambaGroupType", NULL
};
4190 filter
= talloc_asprintf(
4191 mem_ctx
, "(&(objectClass=%s)(|%s))",
4192 LDAP_OBJ_GROUPMAP
, allsids
);
4193 if (filter
== NULL
) {
4197 rc
= smbldap_search(ldap_state
->smbldap_state
,
4199 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4201 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4204 if (rc
!= LDAP_SUCCESS
)
4207 /* ldap_struct might have changed due to a reconnect */
4209 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4211 /* For consistency checks, we already checked we're only domain or builtin */
4213 is_builtin
= sid_check_is_builtin(domain_sid
);
4215 for (entry
= ldap_first_entry(ld
, msg
);
4217 entry
= ldap_next_entry(ld
, entry
))
4222 enum lsa_SidType type
;
4223 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4225 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4228 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4233 type
= (enum lsa_SidType
)atol(attr
);
4235 /* Consistency checks */
4236 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4237 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4238 (type
!= SID_NAME_DOM_GRP
)))) {
4239 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4242 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4244 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4248 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4251 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4253 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4257 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4262 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4263 if (rid
== rids
[rid_index
])
4267 if (rid_index
== num_rids
) {
4268 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4272 attrs
[rid_index
] = type
;
4273 names
[rid_index
] = attr
;
4277 result
= NT_STATUS_NONE_MAPPED
;
4280 result
= (num_mapped
== num_rids
) ?
4281 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4283 TALLOC_FREE(mem_ctx
);
4287 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4289 char *filter
= NULL
;
4290 char *escaped
= NULL
;
4291 char *result
= NULL
;
4293 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4294 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4298 escaped
= escape_ldap_string(talloc_tos(), username
);
4299 if (escaped
== NULL
) goto done
;
4301 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4305 TALLOC_FREE(escaped
);
4310 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4314 const char **result
;
4316 va_start(ap
, mem_ctx
);
4317 while (va_arg(ap
, const char *) != NULL
)
4321 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4325 va_start(ap
, mem_ctx
);
4326 for (i
=0; i
<num
; i
++) {
4327 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4328 if (result
[i
] == NULL
) {
4329 talloc_free(result
);
4340 struct ldap_search_state
{
4341 struct smbldap_state
*connection
;
4343 uint32_t acct_flags
;
4344 uint16_t group_type
;
4351 void *pagedresults_cookie
;
4353 LDAPMessage
*entries
, *current_entry
;
4354 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4355 TALLOC_CTX
*mem_ctx
,
4356 LDAP
*ld
, LDAPMessage
*entry
,
4357 struct samr_displayentry
*result
);
4360 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4362 struct ldap_search_state
*state
=
4363 (struct ldap_search_state
*)search
->private_data
;
4365 int rc
= LDAP_OPERATIONS_ERROR
;
4367 state
->entries
= NULL
;
4369 if (state
->connection
->paged_results
) {
4370 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4371 state
->scope
, state
->filter
,
4372 state
->attrs
, state
->attrsonly
,
4373 lp_ldap_page_size(), &state
->entries
,
4374 &state
->pagedresults_cookie
);
4377 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4379 if (state
->entries
!= NULL
) {
4380 /* Left over from unsuccessful paged attempt */
4381 ldap_msgfree(state
->entries
);
4382 state
->entries
= NULL
;
4385 rc
= smbldap_search(state
->connection
, state
->base
,
4386 state
->scope
, state
->filter
, state
->attrs
,
4387 state
->attrsonly
, &state
->entries
);
4389 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4392 /* Ok, the server was lying. It told us it could do paged
4393 * searches when it could not. */
4394 state
->connection
->paged_results
= False
;
4397 ld
= state
->connection
->ldap_struct
;
4399 DEBUG(5, ("Don't have an LDAP connection right after a "
4403 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4408 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4410 struct ldap_search_state
*state
=
4411 (struct ldap_search_state
*)search
->private_data
;
4414 if (!state
->connection
->paged_results
) {
4415 /* There is no next page when there are no paged results */
4419 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4420 state
->scope
, state
->filter
, state
->attrs
,
4421 state
->attrsonly
, lp_ldap_page_size(),
4423 &state
->pagedresults_cookie
);
4425 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4428 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4430 if (state
->current_entry
== NULL
) {
4431 ldap_msgfree(state
->entries
);
4432 state
->entries
= NULL
;
4439 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4440 struct samr_displayentry
*entry
)
4442 struct ldap_search_state
*state
=
4443 (struct ldap_search_state
*)search
->private_data
;
4447 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4450 if ((state
->entries
== NULL
) &&
4451 !ldapsam_search_nextpage(search
))
4454 if (state
->current_entry
== NULL
) {
4458 result
= state
->ldap2displayentry(state
, search
,
4459 state
->connection
->ldap_struct
,
4460 state
->current_entry
, entry
);
4464 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4465 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4466 if (dn
!= NULL
) ldap_memfree(dn
);
4469 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4471 if (state
->current_entry
== NULL
) {
4472 ldap_msgfree(state
->entries
);
4473 state
->entries
= NULL
;
4476 if (!result
) goto retry
;
4481 static void ldapsam_search_end(struct pdb_search
*search
)
4483 struct ldap_search_state
*state
=
4484 (struct ldap_search_state
*)search
->private_data
;
4487 if (state
->pagedresults_cookie
== NULL
)
4490 if (state
->entries
!= NULL
)
4491 ldap_msgfree(state
->entries
);
4493 state
->entries
= NULL
;
4494 state
->current_entry
= NULL
;
4496 if (!state
->connection
->paged_results
)
4499 /* Tell the LDAP server we're not interested in the rest anymore. */
4501 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4502 state
->filter
, state
->attrs
,
4503 state
->attrsonly
, 0, &state
->entries
,
4504 &state
->pagedresults_cookie
);
4506 if (rc
!= LDAP_SUCCESS
)
4507 DEBUG(5, ("Could not end search properly\n"));
4512 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4513 TALLOC_CTX
*mem_ctx
,
4514 LDAP
*ld
, LDAPMessage
*entry
,
4515 struct samr_displayentry
*result
)
4518 size_t converted_size
;
4520 uint32_t acct_flags
;
4522 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4523 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4524 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4527 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4528 ldap_value_free(vals
);
4530 if ((state
->acct_flags
!= 0) &&
4531 ((state
->acct_flags
& acct_flags
) == 0))
4534 result
->acct_flags
= acct_flags
;
4535 result
->account_name
= "";
4536 result
->fullname
= "";
4537 result
->description
= "";
4539 vals
= ldap_get_values(ld
, entry
, "uid");
4540 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4541 DEBUG(5, ("\"uid\" not found\n"));
4544 if (!pull_utf8_talloc(mem_ctx
,
4545 discard_const_p(char *, &result
->account_name
),
4546 vals
[0], &converted_size
))
4548 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4552 ldap_value_free(vals
);
4554 vals
= ldap_get_values(ld
, entry
, "displayName");
4555 if ((vals
== NULL
) || (vals
[0] == NULL
))
4556 DEBUG(8, ("\"displayName\" not found\n"));
4557 else if (!pull_utf8_talloc(mem_ctx
,
4558 discard_const_p(char *, &result
->fullname
),
4559 vals
[0], &converted_size
))
4561 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4565 ldap_value_free(vals
);
4567 vals
= ldap_get_values(ld
, entry
, "description");
4568 if ((vals
== NULL
) || (vals
[0] == NULL
))
4569 DEBUG(8, ("\"description\" not found\n"));
4570 else if (!pull_utf8_talloc(mem_ctx
,
4571 discard_const_p(char *, &result
->description
),
4572 vals
[0], &converted_size
))
4574 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4578 ldap_value_free(vals
);
4580 if ((result
->account_name
== NULL
) ||
4581 (result
->fullname
== NULL
) ||
4582 (result
->description
== NULL
)) {
4583 DEBUG(0, ("talloc failed\n"));
4587 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4588 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4589 DEBUG(0, ("\"objectSid\" not found\n"));
4593 if (!string_to_sid(&sid
, vals
[0])) {
4594 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4595 ldap_value_free(vals
);
4598 ldap_value_free(vals
);
4600 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4601 DEBUG(0, ("sid %s does not belong to our domain\n",
4602 sid_string_dbg(&sid
)));
4610 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4611 struct pdb_search
*search
,
4612 uint32_t acct_flags
)
4614 struct ldapsam_privates
*ldap_state
=
4615 (struct ldapsam_privates
*)methods
->private_data
;
4616 struct ldap_search_state
*state
;
4618 state
= talloc(search
, struct ldap_search_state
);
4619 if (state
== NULL
) {
4620 DEBUG(0, ("talloc failed\n"));
4624 state
->connection
= ldap_state
->smbldap_state
;
4626 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4627 state
->base
= lp_ldap_user_suffix();
4628 else if ((acct_flags
!= 0) &&
4629 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4630 state
->base
= lp_ldap_machine_suffix();
4632 state
->base
= lp_ldap_suffix();
4634 state
->acct_flags
= acct_flags
;
4635 state
->base
= talloc_strdup(search
, state
->base
);
4636 state
->scope
= LDAP_SCOPE_SUBTREE
;
4637 state
->filter
= get_ldap_filter(search
, "*");
4638 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4639 "displayName", "description",
4640 "sambaAcctFlags", NULL
);
4641 state
->attrsonly
= 0;
4642 state
->pagedresults_cookie
= NULL
;
4643 state
->entries
= NULL
;
4644 state
->ldap2displayentry
= ldapuser2displayentry
;
4646 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4647 DEBUG(0, ("talloc failed\n"));
4651 search
->private_data
= state
;
4652 search
->next_entry
= ldapsam_search_next_entry
;
4653 search
->search_end
= ldapsam_search_end
;
4655 return ldapsam_search_firstpage(search
);
4658 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4659 TALLOC_CTX
*mem_ctx
,
4660 LDAP
*ld
, LDAPMessage
*entry
,
4661 struct samr_displayentry
*result
)
4664 size_t converted_size
;
4666 uint16_t group_type
;
4668 result
->account_name
= "";
4669 result
->fullname
= "";
4670 result
->description
= "";
4673 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4674 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4675 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4677 ldap_value_free(vals
);
4682 group_type
= atoi(vals
[0]);
4684 if ((state
->group_type
!= 0) &&
4685 ((state
->group_type
!= group_type
))) {
4686 ldap_value_free(vals
);
4690 ldap_value_free(vals
);
4692 /* display name is the NT group name */
4694 vals
= ldap_get_values(ld
, entry
, "displayName");
4695 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4696 DEBUG(8, ("\"displayName\" not found\n"));
4698 /* fallback to the 'cn' attribute */
4699 vals
= ldap_get_values(ld
, entry
, "cn");
4700 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4701 DEBUG(5, ("\"cn\" not found\n"));
4704 if (!pull_utf8_talloc(mem_ctx
,
4705 discard_const_p(char *,
4706 &result
->account_name
),
4707 vals
[0], &converted_size
))
4709 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4710 "failed: %s", strerror(errno
)));
4713 else if (!pull_utf8_talloc(mem_ctx
,
4714 discard_const_p(char *,
4715 &result
->account_name
),
4716 vals
[0], &converted_size
))
4718 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4722 ldap_value_free(vals
);
4724 vals
= ldap_get_values(ld
, entry
, "description");
4725 if ((vals
== NULL
) || (vals
[0] == NULL
))
4726 DEBUG(8, ("\"description\" not found\n"));
4727 else if (!pull_utf8_talloc(mem_ctx
,
4728 discard_const_p(char *, &result
->description
),
4729 vals
[0], &converted_size
))
4731 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4734 ldap_value_free(vals
);
4736 if ((result
->account_name
== NULL
) ||
4737 (result
->fullname
== NULL
) ||
4738 (result
->description
== NULL
)) {
4739 DEBUG(0, ("talloc failed\n"));
4743 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4744 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4745 DEBUG(0, ("\"objectSid\" not found\n"));
4747 ldap_value_free(vals
);
4752 if (!string_to_sid(&sid
, vals
[0])) {
4753 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4757 ldap_value_free(vals
);
4759 switch (group_type
) {
4760 case SID_NAME_DOM_GRP
:
4761 case SID_NAME_ALIAS
:
4763 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4764 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4766 DEBUG(0, ("%s is not in our domain\n",
4767 sid_string_dbg(&sid
)));
4773 DEBUG(0,("unknown group type: %d\n", group_type
));
4777 result
->acct_flags
= 0;
4782 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4783 struct pdb_search
*search
,
4784 const struct dom_sid
*sid
,
4785 enum lsa_SidType type
)
4787 struct ldapsam_privates
*ldap_state
=
4788 (struct ldapsam_privates
*)methods
->private_data
;
4789 struct ldap_search_state
*state
;
4792 state
= talloc(search
, struct ldap_search_state
);
4793 if (state
== NULL
) {
4794 DEBUG(0, ("talloc failed\n"));
4798 state
->connection
= ldap_state
->smbldap_state
;
4800 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4801 state
->connection
= ldap_state
->smbldap_state
;
4802 state
->scope
= LDAP_SCOPE_SUBTREE
;
4803 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4804 "(sambaGroupType=%d)(sambaSID=%s*))",
4806 type
, sid_to_fstring(tmp
, sid
));
4807 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4808 "displayName", "description",
4809 "sambaGroupType", NULL
);
4810 state
->attrsonly
= 0;
4811 state
->pagedresults_cookie
= NULL
;
4812 state
->entries
= NULL
;
4813 state
->group_type
= type
;
4814 state
->ldap2displayentry
= ldapgroup2displayentry
;
4816 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4817 DEBUG(0, ("talloc failed\n"));
4821 search
->private_data
= state
;
4822 search
->next_entry
= ldapsam_search_next_entry
;
4823 search
->search_end
= ldapsam_search_end
;
4825 return ldapsam_search_firstpage(search
);
4828 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4829 struct pdb_search
*search
)
4831 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4834 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4835 struct pdb_search
*search
,
4836 const struct dom_sid
*sid
)
4838 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4841 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4843 return PDB_CAP_STORE_RIDS
;
4846 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4849 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4851 LDAPMessage
*result
= NULL
;
4852 LDAPMessage
*entry
= NULL
;
4853 LDAPMod
**mods
= NULL
;
4857 uint32_t nextRid
= 0;
4860 TALLOC_CTX
*mem_ctx
;
4862 mem_ctx
= talloc_new(NULL
);
4863 if (mem_ctx
== NULL
) {
4864 DEBUG(0, ("talloc_new failed\n"));
4865 return NT_STATUS_NO_MEMORY
;
4868 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4869 get_global_sam_name(), False
);
4870 if (!NT_STATUS_IS_OK(status
)) {
4871 DEBUG(3, ("Could not get domain info: %s\n",
4872 nt_errstr(status
)));
4876 talloc_autofree_ldapmsg(mem_ctx
, result
);
4878 entry
= ldap_first_entry(priv2ld(priv
), result
);
4879 if (entry
== NULL
) {
4880 DEBUG(0, ("Could not get domain info entry\n"));
4881 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4885 /* Find the largest of the three attributes "sambaNextRid",
4886 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4887 concept of differentiating between user and group rids, and will
4888 use only "sambaNextRid" in the future. But for compatibility
4889 reasons I look if others have chosen different strategies -- VL */
4891 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4892 "sambaNextRid", mem_ctx
);
4893 if (value
!= NULL
) {
4894 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4895 nextRid
= MAX(nextRid
, tmp
);
4898 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4899 "sambaNextUserRid", mem_ctx
);
4900 if (value
!= NULL
) {
4901 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4902 nextRid
= MAX(nextRid
, tmp
);
4905 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4906 "sambaNextGroupRid", mem_ctx
);
4907 if (value
!= NULL
) {
4908 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4909 nextRid
= MAX(nextRid
, tmp
);
4913 nextRid
= BASE_RID
-1;
4918 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4919 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4920 talloc_autofree_ldapmod(mem_ctx
, mods
);
4922 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4923 status
= NT_STATUS_NO_MEMORY
;
4927 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4929 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4932 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4935 if (NT_STATUS_IS_OK(status
)) {
4939 TALLOC_FREE(mem_ctx
);
4943 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4947 for (i
=0; i
<10; i
++) {
4948 NTSTATUS result
= ldapsam_get_new_rid(
4949 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4950 if (NT_STATUS_IS_OK(result
)) {
4954 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4958 /* The ldap update failed (maybe a race condition), retry */
4961 /* Tried 10 times, fail. */
4962 return NT_STATUS_ACCESS_DENIED
;
4965 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
4967 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4968 return NT_STATUS_IS_OK(result
) ? True
: False
;
4971 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4972 const struct dom_sid
*sid
,
4973 uid_t
*uid
, gid_t
*gid
,
4974 enum lsa_SidType
*type
)
4976 struct ldapsam_privates
*priv
=
4977 (struct ldapsam_privates
*)methods
->private_data
;
4979 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4981 LDAPMessage
*result
= NULL
;
4982 LDAPMessage
*entry
= NULL
;
4987 TALLOC_CTX
*mem_ctx
;
4989 mem_ctx
= talloc_new(NULL
);
4990 if (mem_ctx
== NULL
) {
4991 DEBUG(0, ("talloc_new failed\n"));
4995 filter
= talloc_asprintf(mem_ctx
,
4997 "(|(objectClass=%s)(objectClass=%s)))",
4998 sid_string_talloc(mem_ctx
, sid
),
4999 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5000 if (filter
== NULL
) {
5001 DEBUG(5, ("talloc_asprintf failed\n"));
5005 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5007 if (rc
!= LDAP_SUCCESS
) {
5010 talloc_autofree_ldapmsg(mem_ctx
, result
);
5012 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5013 DEBUG(10, ("Got %d entries, expected one\n",
5014 ldap_count_entries(priv2ld(priv
), result
)));
5018 entry
= ldap_first_entry(priv2ld(priv
), result
);
5020 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5021 "sambaGroupType", mem_ctx
);
5023 if (value
!= NULL
) {
5024 const char *gid_str
;
5027 gid_str
= smbldap_talloc_single_attribute(
5028 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5029 if (gid_str
== NULL
) {
5030 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5031 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5036 *gid
= strtoul(gid_str
, NULL
, 10);
5037 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
5038 idmap_cache_set_sid2gid(sid
, *gid
);
5043 /* It must be a user */
5045 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5046 "uidNumber", mem_ctx
);
5047 if (value
== NULL
) {
5048 DEBUG(1, ("Could not find uidNumber in %s\n",
5049 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5053 *uid
= strtoul(value
, NULL
, 10);
5054 *type
= SID_NAME_USER
;
5055 idmap_cache_set_sid2uid(sid
, *uid
);
5059 TALLOC_FREE(mem_ctx
);
5064 * Find the SID for a uid.
5065 * This is shortcut is only used if ldapsam:trusted is set to true.
5067 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5068 struct dom_sid
*sid
)
5070 struct ldapsam_privates
*priv
=
5071 (struct ldapsam_privates
*)methods
->private_data
;
5073 const char *attrs
[] = { "sambaSID", NULL
};
5074 LDAPMessage
*result
= NULL
;
5075 LDAPMessage
*entry
= NULL
;
5077 char *user_sid_string
;
5078 struct dom_sid user_sid
;
5080 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5082 filter
= talloc_asprintf(tmp_ctx
,
5085 "(objectClass=%s))",
5087 LDAP_OBJ_POSIXACCOUNT
,
5088 LDAP_OBJ_SAMBASAMACCOUNT
);
5089 if (filter
== NULL
) {
5090 DEBUG(3, ("talloc_asprintf failed\n"));
5094 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5095 if (rc
!= LDAP_SUCCESS
) {
5098 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5100 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5101 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5102 ldap_count_entries(priv2ld(priv
), result
),
5103 (unsigned int)uid
));
5107 entry
= ldap_first_entry(priv2ld(priv
), result
);
5109 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5110 "sambaSID", tmp_ctx
);
5111 if (user_sid_string
== NULL
) {
5112 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5113 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5117 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5118 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5123 sid_copy(sid
, &user_sid
);
5125 idmap_cache_set_sid2uid(sid
, uid
);
5130 TALLOC_FREE(tmp_ctx
);
5135 * Find the SID for a gid.
5136 * This is shortcut is only used if ldapsam:trusted is set to true.
5138 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5139 struct dom_sid
*sid
)
5141 struct ldapsam_privates
*priv
=
5142 (struct ldapsam_privates
*)methods
->private_data
;
5144 const char *attrs
[] = { "sambaSID", NULL
};
5145 LDAPMessage
*result
= NULL
;
5146 LDAPMessage
*entry
= NULL
;
5148 char *group_sid_string
;
5149 struct dom_sid group_sid
;
5151 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5153 filter
= talloc_asprintf(tmp_ctx
,
5155 "(objectClass=%s))",
5158 if (filter
== NULL
) {
5159 DEBUG(3, ("talloc_asprintf failed\n"));
5163 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5164 if (rc
!= LDAP_SUCCESS
) {
5167 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5169 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5170 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5171 ldap_count_entries(priv2ld(priv
), result
),
5172 (unsigned int)gid
));
5176 entry
= ldap_first_entry(priv2ld(priv
), result
);
5178 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5179 "sambaSID", tmp_ctx
);
5180 if (group_sid_string
== NULL
) {
5181 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5182 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5186 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5187 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5192 sid_copy(sid
, &group_sid
);
5194 idmap_cache_set_sid2gid(sid
, gid
);
5199 TALLOC_FREE(tmp_ctx
);
5205 * The following functions are called only if
5206 * ldapsam:trusted and ldapsam:editposix are
5211 * ldapsam_create_user creates a new
5212 * posixAccount and sambaSamAccount object
5213 * in the ldap users subtree
5215 * The uid is allocated by winbindd.
5218 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5219 TALLOC_CTX
*tmp_ctx
, const char *name
,
5220 uint32_t acb_info
, uint32_t *rid
)
5222 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5223 LDAPMessage
*entry
= NULL
;
5224 LDAPMessage
*result
= NULL
;
5225 uint32_t num_result
;
5226 bool is_machine
= False
;
5227 bool add_posix
= False
;
5228 LDAPMod
**mods
= NULL
;
5236 const char *dn
= NULL
;
5237 struct dom_sid group_sid
;
5238 struct dom_sid user_sid
;
5244 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5245 acb_info
& ACB_WSTRUST
||
5246 acb_info
& ACB_SVRTRUST
||
5247 acb_info
& ACB_DOMTRUST
) {
5251 username
= escape_ldap_string(talloc_tos(), name
);
5252 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5253 username
, LDAP_OBJ_POSIXACCOUNT
);
5254 TALLOC_FREE(username
);
5256 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5257 if (rc
!= LDAP_SUCCESS
) {
5258 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5259 return NT_STATUS_ACCESS_DENIED
;
5261 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5263 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5265 if (num_result
> 1) {
5266 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5267 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5270 if (num_result
== 1) {
5272 /* check if it is just a posix account.
5273 * or if there is a sid attached to this entry
5276 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5278 return NT_STATUS_UNSUCCESSFUL
;
5281 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5283 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5284 return NT_STATUS_USER_EXISTS
;
5287 /* it is just a posix account, retrieve the dn for later use */
5288 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5290 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5291 return NT_STATUS_NO_MEMORY
;
5295 if (num_result
== 0) {
5299 /* Create the basic samu structure and generate the mods for the ldap commit */
5300 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5301 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5305 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5307 user
= samu_new(tmp_ctx
);
5309 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5310 return NT_STATUS_NO_MEMORY
;
5313 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5314 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5315 return NT_STATUS_UNSUCCESSFUL
;
5317 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5318 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5319 return NT_STATUS_UNSUCCESSFUL
;
5322 if (acb_info
& ACB_NORMAL
) {
5323 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5324 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5325 return NT_STATUS_UNSUCCESSFUL
;
5328 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5329 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5330 return NT_STATUS_UNSUCCESSFUL
;
5334 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5335 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5336 return NT_STATUS_UNSUCCESSFUL
;
5340 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5341 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5342 return NT_STATUS_UNSUCCESSFUL
;
5345 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
)) {
5346 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5347 return NT_STATUS_UNSUCCESSFUL
;
5350 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5351 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5353 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5358 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5360 /* retrieve the Domain Users group gid */
5361 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5362 !sid_to_gid(&group_sid
, &gid
)) {
5363 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5364 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5367 /* lets allocate a new userid for this user */
5368 if (!winbind_allocate_uid(&uid
)) {
5369 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5370 return NT_STATUS_UNSUCCESSFUL
;
5375 /* TODO: choose a more appropriate default for machines */
5376 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5377 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5379 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5380 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5382 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5383 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5385 escape_name
= escape_rdn_val_string_alloc(name
);
5387 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5388 return NT_STATUS_NO_MEMORY
;
5392 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5394 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5397 SAFE_FREE(escape_name
);
5399 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5400 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5401 return NT_STATUS_NO_MEMORY
;
5404 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5405 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5406 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5407 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5408 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5409 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5410 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5413 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5416 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5418 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5421 if (rc
!= LDAP_SUCCESS
) {
5422 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5423 return NT_STATUS_UNSUCCESSFUL
;
5426 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5428 flush_pwnam_cache();
5430 return NT_STATUS_OK
;
5433 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5435 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5436 LDAPMessage
*result
= NULL
;
5437 LDAPMessage
*entry
= NULL
;
5443 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5445 filter
= talloc_asprintf(tmp_ctx
,
5448 "(objectClass=%s))",
5449 pdb_get_username(sam_acct
),
5450 LDAP_OBJ_POSIXACCOUNT
,
5451 LDAP_OBJ_SAMBASAMACCOUNT
);
5452 if (filter
== NULL
) {
5453 return NT_STATUS_NO_MEMORY
;
5456 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5457 if (rc
!= LDAP_SUCCESS
) {
5458 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5459 return NT_STATUS_UNSUCCESSFUL
;
5461 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5463 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5465 if (num_result
== 0) {
5466 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5467 return NT_STATUS_NO_SUCH_USER
;
5470 if (num_result
> 1) {
5471 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5472 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5475 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5477 return NT_STATUS_UNSUCCESSFUL
;
5480 /* it is just a posix account, retrieve the dn for later use */
5481 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5483 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5484 return NT_STATUS_NO_MEMORY
;
5487 /* try to remove memberships first */
5490 struct dom_sid
*sids
= NULL
;
5492 uint32_t num_groups
= 0;
5494 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5496 status
= ldapsam_enum_group_memberships(my_methods
,
5502 if (!NT_STATUS_IS_OK(status
)) {
5506 for (i
=0; i
< num_groups
; i
++) {
5510 sid_peek_rid(&sids
[i
], &group_rid
);
5512 ldapsam_del_groupmem(my_methods
,
5521 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5522 if (rc
!= LDAP_SUCCESS
) {
5523 return NT_STATUS_UNSUCCESSFUL
;
5526 flush_pwnam_cache();
5528 return NT_STATUS_OK
;
5532 * ldapsam_create_group creates a new
5533 * posixGroup and sambaGroupMapping object
5534 * in the ldap groups subtree
5536 * The gid is allocated by winbindd.
5539 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5540 TALLOC_CTX
*tmp_ctx
,
5544 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5546 LDAPMessage
*entry
= NULL
;
5547 LDAPMessage
*result
= NULL
;
5548 uint32_t num_result
;
5549 bool is_new_entry
= False
;
5550 LDAPMod
**mods
= NULL
;
5556 const char *dn
= NULL
;
5557 struct dom_sid group_sid
;
5561 groupname
= escape_ldap_string(talloc_tos(), name
);
5562 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5563 groupname
, LDAP_OBJ_POSIXGROUP
);
5564 TALLOC_FREE(groupname
);
5566 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5567 if (rc
!= LDAP_SUCCESS
) {
5568 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5569 return NT_STATUS_UNSUCCESSFUL
;
5571 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5573 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5575 if (num_result
> 1) {
5576 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5577 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5580 if (num_result
== 1) {
5582 /* check if it is just a posix group.
5583 * or if there is a sid attached to this entry
5586 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5588 return NT_STATUS_UNSUCCESSFUL
;
5591 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5593 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5594 return NT_STATUS_GROUP_EXISTS
;
5597 /* it is just a posix group, retrieve the gid and the dn for later use */
5598 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5600 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5601 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5604 gid
= strtoul(tmp
, NULL
, 10);
5606 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5608 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5609 return NT_STATUS_NO_MEMORY
;
5613 if (num_result
== 0) {
5614 is_new_entry
= true;
5617 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5618 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5622 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5624 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5626 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5628 if (!groupsidstr
|| !grouptype
) {
5629 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5630 return NT_STATUS_NO_MEMORY
;
5633 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5634 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5635 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5636 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5641 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5643 /* lets allocate a new groupid for this group */
5644 if (!winbind_allocate_gid(&gid
)) {
5645 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5646 return NT_STATUS_UNSUCCESSFUL
;
5649 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5651 escape_name
= escape_rdn_val_string_alloc(name
);
5653 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5654 return NT_STATUS_NO_MEMORY
;
5657 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5659 SAFE_FREE(escape_name
);
5661 if (!gidstr
|| !dn
) {
5662 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5663 return NT_STATUS_NO_MEMORY
;
5666 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5667 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5668 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5671 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5674 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5676 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5677 /* This call may fail with rfc2307bis schema */
5678 /* Retry adding a structural class */
5679 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5680 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5684 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5687 if (rc
!= LDAP_SUCCESS
) {
5688 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5689 return NT_STATUS_UNSUCCESSFUL
;
5692 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5694 return NT_STATUS_OK
;
5697 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5699 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5700 LDAPMessage
*result
= NULL
;
5701 LDAPMessage
*entry
= NULL
;
5706 struct dom_sid group_sid
;
5709 /* get the group sid */
5710 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5712 filter
= talloc_asprintf(tmp_ctx
,
5715 "(objectClass=%s))",
5716 sid_string_talloc(tmp_ctx
, &group_sid
),
5717 LDAP_OBJ_POSIXGROUP
,
5719 if (filter
== NULL
) {
5720 return NT_STATUS_NO_MEMORY
;
5723 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5724 if (rc
!= LDAP_SUCCESS
) {
5725 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5726 return NT_STATUS_UNSUCCESSFUL
;
5728 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5730 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5732 if (num_result
== 0) {
5733 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5734 return NT_STATUS_NO_SUCH_GROUP
;
5737 if (num_result
> 1) {
5738 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5739 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5742 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5744 return NT_STATUS_UNSUCCESSFUL
;
5747 /* here it is, retrieve the dn for later use */
5748 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5750 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5751 return NT_STATUS_NO_MEMORY
;
5754 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5756 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5757 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5760 /* check no user have this group marked as primary group */
5761 filter
= talloc_asprintf(tmp_ctx
,
5764 "(objectClass=%s))",
5766 LDAP_OBJ_POSIXACCOUNT
,
5767 LDAP_OBJ_SAMBASAMACCOUNT
);
5769 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5770 if (rc
!= LDAP_SUCCESS
) {
5771 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5772 return NT_STATUS_UNSUCCESSFUL
;
5774 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5776 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5778 if (num_result
!= 0) {
5779 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5780 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5783 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5784 if (rc
!= LDAP_SUCCESS
) {
5785 return NT_STATUS_UNSUCCESSFUL
;
5788 return NT_STATUS_OK
;
5791 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5792 TALLOC_CTX
*tmp_ctx
,
5794 uint32_t member_rid
,
5797 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5798 LDAPMessage
*entry
= NULL
;
5799 LDAPMessage
*result
= NULL
;
5800 uint32_t num_result
;
5801 LDAPMod
**mods
= NULL
;
5804 const char *dn
= NULL
;
5805 struct dom_sid group_sid
;
5806 struct dom_sid member_sid
;
5811 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5813 case LDAP_MOD_DELETE
:
5814 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5817 return NT_STATUS_UNSUCCESSFUL
;
5820 /* get member sid */
5821 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5823 /* get the group sid */
5824 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5826 filter
= talloc_asprintf(tmp_ctx
,
5829 "(objectClass=%s))",
5830 sid_string_talloc(tmp_ctx
, &member_sid
),
5831 LDAP_OBJ_POSIXACCOUNT
,
5832 LDAP_OBJ_SAMBASAMACCOUNT
);
5833 if (filter
== NULL
) {
5834 return NT_STATUS_NO_MEMORY
;
5837 /* get the member uid */
5838 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5839 if (rc
!= LDAP_SUCCESS
) {
5840 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5841 return NT_STATUS_UNSUCCESSFUL
;
5843 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5845 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5847 if (num_result
== 0) {
5848 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5849 return NT_STATUS_NO_SUCH_MEMBER
;
5852 if (num_result
> 1) {
5853 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5854 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5857 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5859 return NT_STATUS_UNSUCCESSFUL
;
5862 if (modop
== LDAP_MOD_DELETE
) {
5863 /* check if we are trying to remove the member from his primary group */
5865 gid_t user_gid
, group_gid
;
5867 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5869 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5870 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5873 user_gid
= strtoul(gidstr
, NULL
, 10);
5875 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5876 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5877 return NT_STATUS_UNSUCCESSFUL
;
5880 if (user_gid
== group_gid
) {
5881 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5882 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5886 /* here it is, retrieve the uid for later use */
5887 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5889 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5890 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5893 filter
= talloc_asprintf(tmp_ctx
,
5896 "(objectClass=%s))",
5897 sid_string_talloc(tmp_ctx
, &group_sid
),
5898 LDAP_OBJ_POSIXGROUP
,
5902 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5903 if (rc
!= LDAP_SUCCESS
) {
5904 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5905 return NT_STATUS_UNSUCCESSFUL
;
5907 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5909 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5911 if (num_result
== 0) {
5912 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5913 return NT_STATUS_NO_SUCH_GROUP
;
5916 if (num_result
> 1) {
5917 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5918 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5921 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5923 return NT_STATUS_UNSUCCESSFUL
;
5926 /* here it is, retrieve the dn for later use */
5927 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5929 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5930 return NT_STATUS_NO_MEMORY
;
5933 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5935 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5937 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5938 if (rc
!= LDAP_SUCCESS
) {
5939 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5940 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5941 return NT_STATUS_MEMBER_IN_GROUP
;
5943 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5944 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5945 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5947 return NT_STATUS_UNSUCCESSFUL
;
5950 return NT_STATUS_OK
;
5953 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5954 TALLOC_CTX
*tmp_ctx
,
5956 uint32_t member_rid
)
5958 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5960 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5961 TALLOC_CTX
*tmp_ctx
,
5963 uint32_t member_rid
)
5965 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5968 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5969 TALLOC_CTX
*mem_ctx
,
5970 struct samu
*sampass
)
5972 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5973 LDAPMessage
*entry
= NULL
;
5974 LDAPMessage
*result
= NULL
;
5975 uint32_t num_result
;
5976 LDAPMod
**mods
= NULL
;
5978 char *escape_username
;
5980 const char *dn
= NULL
;
5984 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5986 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5987 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5988 return NT_STATUS_UNSUCCESSFUL
;
5990 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
5992 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5993 return NT_STATUS_NO_MEMORY
;
5996 escape_username
= escape_ldap_string(talloc_tos(),
5997 pdb_get_username(sampass
));
5998 if (escape_username
== NULL
) {
5999 return NT_STATUS_NO_MEMORY
;
6002 filter
= talloc_asprintf(mem_ctx
,
6005 "(objectClass=%s))",
6007 LDAP_OBJ_POSIXACCOUNT
,
6008 LDAP_OBJ_SAMBASAMACCOUNT
);
6010 TALLOC_FREE(escape_username
);
6012 if (filter
== NULL
) {
6013 return NT_STATUS_NO_MEMORY
;
6016 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6017 if (rc
!= LDAP_SUCCESS
) {
6018 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6019 return NT_STATUS_UNSUCCESSFUL
;
6021 talloc_autofree_ldapmsg(mem_ctx
, result
);
6023 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6025 if (num_result
== 0) {
6026 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6027 return NT_STATUS_NO_SUCH_USER
;
6030 if (num_result
> 1) {
6031 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6032 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6035 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6037 return NT_STATUS_UNSUCCESSFUL
;
6040 /* retrieve the dn for later use */
6041 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6043 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6044 return NT_STATUS_NO_MEMORY
;
6047 /* remove the old one, and add the new one, this way we do not risk races */
6048 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6051 return NT_STATUS_OK
;
6054 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6056 if (rc
!= LDAP_SUCCESS
) {
6057 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6058 pdb_get_username(sampass
), gidstr
));
6059 return NT_STATUS_UNSUCCESSFUL
;
6062 flush_pwnam_cache();
6064 return NT_STATUS_OK
;
6068 /**********************************************************************
6069 trusted domains functions
6070 *********************************************************************/
6072 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6075 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6076 ldap_state
->domain_dn
);
6079 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6080 TALLOC_CTX
*mem_ctx
,
6081 const char *domain
, LDAPMessage
**entry
)
6085 int scope
= LDAP_SCOPE_SUBTREE
;
6086 const char **attrs
= NULL
; /* NULL: get all attrs */
6087 int attrsonly
= 0; /* 0: return values too */
6088 LDAPMessage
*result
= NULL
;
6090 uint32_t num_result
;
6092 filter
= talloc_asprintf(talloc_tos(),
6093 "(&(objectClass=%s)(sambaDomainName=%s))",
6094 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6096 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6097 if (trusted_dn
== NULL
) {
6100 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6101 filter
, attrs
, attrsonly
, &result
);
6103 if (result
!= NULL
) {
6104 talloc_autofree_ldapmsg(mem_ctx
, result
);
6107 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6112 if (rc
!= LDAP_SUCCESS
) {
6116 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6118 if (num_result
> 1) {
6119 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6120 "%s object for domain '%s'?!\n",
6121 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6125 if (num_result
== 0) {
6126 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6127 "%s object for domain %s.\n",
6128 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6131 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6137 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6140 struct dom_sid
*sid
,
6141 time_t *pass_last_set_time
)
6143 struct ldapsam_privates
*ldap_state
=
6144 (struct ldapsam_privates
*)methods
->private_data
;
6145 LDAPMessage
*entry
= NULL
;
6147 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6149 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6158 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6159 entry
, "sambaClearTextPassword", talloc_tos());
6160 if (pwd_str
== NULL
) {
6163 /* trusteddom_pw routines do not use talloc yet... */
6164 *pwd
= SMB_STRDUP(pwd_str
);
6170 /* last change time */
6171 if (pass_last_set_time
!= NULL
) {
6173 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6174 entry
, "sambaPwdLastSet", talloc_tos());
6175 if (time_str
== NULL
) {
6178 *pass_last_set_time
= (time_t)atol(time_str
);
6184 struct dom_sid dom_sid
;
6185 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6188 if (sid_str
== NULL
) {
6191 if (!string_to_sid(&dom_sid
, sid_str
)) {
6194 sid_copy(sid
, &dom_sid
);
6200 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6203 const struct dom_sid
*sid
)
6205 struct ldapsam_privates
*ldap_state
=
6206 (struct ldapsam_privates
*)methods
->private_data
;
6207 LDAPMessage
*entry
= NULL
;
6208 LDAPMod
**mods
= NULL
;
6209 char *prev_pwd
= NULL
;
6210 char *trusted_dn
= NULL
;
6213 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6216 * get the current entry (if there is one) in order to put the
6217 * current password into the previous password attribute
6219 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6224 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6225 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6226 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6228 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6229 sid_string_tos(sid
));
6230 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6231 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6232 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6233 "sambaClearTextPassword", pwd
);
6235 if (entry
!= NULL
) {
6236 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6237 entry
, "sambaClearTextPassword", talloc_tos());
6238 if (prev_pwd
!= NULL
) {
6239 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6240 "sambaPreviousClearTextPassword",
6245 talloc_autofree_ldapmod(talloc_tos(), mods
);
6247 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6248 if (trusted_dn
== NULL
) {
6251 if (entry
== NULL
) {
6252 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6254 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6257 if (rc
!= LDAP_SUCCESS
) {
6258 DEBUG(1, ("error writing trusted domain password!\n"));
6265 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6269 struct ldapsam_privates
*ldap_state
=
6270 (struct ldapsam_privates
*)methods
->private_data
;
6271 LDAPMessage
*entry
= NULL
;
6272 const char *trusted_dn
;
6274 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6278 if (entry
== NULL
) {
6279 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6284 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6286 if (trusted_dn
== NULL
) {
6287 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6291 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6292 if (rc
!= LDAP_SUCCESS
) {
6299 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6300 TALLOC_CTX
*mem_ctx
,
6301 uint32_t *num_domains
,
6302 struct trustdom_info
***domains
)
6305 struct ldapsam_privates
*ldap_state
=
6306 (struct ldapsam_privates
*)methods
->private_data
;
6308 int scope
= LDAP_SCOPE_SUBTREE
;
6309 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6310 int attrsonly
= 0; /* 0: return values too */
6311 LDAPMessage
*result
= NULL
;
6312 LDAPMessage
*entry
= NULL
;
6314 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6315 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6317 rc
= smbldap_search(ldap_state
->smbldap_state
,
6318 ldap_state
->domain_dn
,
6325 if (result
!= NULL
) {
6326 talloc_autofree_ldapmsg(mem_ctx
, result
);
6329 if (rc
!= LDAP_SUCCESS
) {
6330 return NT_STATUS_UNSUCCESSFUL
;
6334 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6335 DEBUG(1, ("talloc failed\n"));
6336 return NT_STATUS_NO_MEMORY
;
6339 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6341 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6343 char *dom_name
, *dom_sid_str
;
6344 struct trustdom_info
*dom_info
;
6346 dom_info
= talloc(*domains
, struct trustdom_info
);
6347 if (dom_info
== NULL
) {
6348 DEBUG(1, ("talloc failed\n"));
6349 return NT_STATUS_NO_MEMORY
;
6352 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6356 if (dom_name
== NULL
) {
6357 DEBUG(1, ("talloc failed\n"));
6358 return NT_STATUS_NO_MEMORY
;
6360 dom_info
->name
= dom_name
;
6362 dom_sid_str
= smbldap_talloc_single_attribute(
6363 priv2ld(ldap_state
), entry
, "sambaSID",
6365 if (dom_sid_str
== NULL
) {
6366 DEBUG(1, ("talloc failed\n"));
6367 return NT_STATUS_NO_MEMORY
;
6369 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6370 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6372 return NT_STATUS_UNSUCCESSFUL
;
6375 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6376 domains
, num_domains
);
6378 if (*domains
== NULL
) {
6379 DEBUG(1, ("talloc failed\n"));
6380 return NT_STATUS_NO_MEMORY
;
6384 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6385 return NT_STATUS_OK
;
6389 /**********************************************************************
6391 *********************************************************************/
6393 static void free_private_data(void **vp
)
6395 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6397 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6399 if ((*ldap_state
)->result
!= NULL
) {
6400 ldap_msgfree((*ldap_state
)->result
);
6401 (*ldap_state
)->result
= NULL
;
6403 if ((*ldap_state
)->domain_dn
!= NULL
) {
6404 SAFE_FREE((*ldap_state
)->domain_dn
);
6409 /* No need to free any further, as it is talloc()ed */
6412 /*********************************************************************
6413 Intitalise the parts of the pdb_methods structure that are common to
6415 *********************************************************************/
6417 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6420 struct ldapsam_privates
*ldap_state
;
6421 char *bind_dn
= NULL
;
6422 char *bind_secret
= NULL
;
6424 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6428 (*pdb_method
)->name
= "ldapsam";
6430 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6431 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6432 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6433 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6434 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6435 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6437 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6438 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6439 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6440 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6441 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6442 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6443 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6445 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6446 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6448 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6450 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6451 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6453 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6454 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6455 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6456 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6458 /* TODO: Setup private data and free */
6460 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6461 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6462 return NT_STATUS_NO_MEMORY
;
6465 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6466 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6467 return NT_STATUS_NO_MEMORY
;
6470 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6471 location
, false, bind_dn
, bind_secret
,
6472 &ldap_state
->smbldap_state
);
6473 memset(bind_secret
, '\0', strlen(bind_secret
));
6474 SAFE_FREE(bind_secret
);
6476 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6480 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6481 return NT_STATUS_NO_MEMORY
;
6484 (*pdb_method
)->private_data
= ldap_state
;
6486 (*pdb_method
)->free_private_data
= free_private_data
;
6488 return NT_STATUS_OK
;
6491 /**********************************************************************
6492 Initialise the 'compat' mode for pdb_ldap
6493 *********************************************************************/
6495 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6498 struct ldapsam_privates
*ldap_state
;
6499 char *uri
= talloc_strdup( NULL
, location
);
6501 trim_char( uri
, '\"', '\"' );
6502 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6506 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6510 (*pdb_method
)->name
= "ldapsam_compat";
6512 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6513 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6515 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6517 return NT_STATUS_OK
;
6520 /**********************************************************************
6521 Initialise the normal mode for pdb_ldap
6522 *********************************************************************/
6524 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6527 struct ldapsam_privates
*ldap_state
= NULL
;
6528 uint32_t alg_rid_base
;
6529 char *alg_rid_base_string
= NULL
;
6530 LDAPMessage
*result
= NULL
;
6531 LDAPMessage
*entry
= NULL
;
6532 struct dom_sid ldap_domain_sid
;
6533 struct dom_sid secrets_domain_sid
;
6534 char *domain_sid_string
= NULL
;
6536 char *uri
= talloc_strdup( NULL
, location
);
6538 trim_char( uri
, '\"', '\"' );
6539 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6543 if (!NT_STATUS_IS_OK(nt_status
)) {
6547 (*pdb_method
)->name
= "ldapsam";
6549 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6550 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6551 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6552 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6553 (*pdb_method
)->search_users
= ldapsam_search_users
;
6554 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6555 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6557 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6558 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6559 (*pdb_method
)->enum_group_memberships
=
6560 ldapsam_enum_group_memberships
;
6561 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6562 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6563 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6564 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6566 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6567 (*pdb_method
)->create_user
= ldapsam_create_user
;
6568 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6569 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6570 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6571 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6572 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6573 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6577 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6578 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6580 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6582 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6584 ldap_state
->domain_name
, True
);
6586 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6587 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6588 "info, nor add one to the domain. "
6589 "We cannot work reliably without it.\n"));
6590 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6593 /* Given that the above might fail, everything below this must be
6596 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6599 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6601 ldap_msgfree(result
);
6602 return NT_STATUS_UNSUCCESSFUL
;
6605 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6607 ldap_msgfree(result
);
6608 return NT_STATUS_UNSUCCESSFUL
;
6611 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6614 domain_sid_string
= smbldap_talloc_single_attribute(
6615 ldap_state
->smbldap_state
->ldap_struct
,
6617 get_userattr_key2string(ldap_state
->schema_ver
,
6618 LDAP_ATTR_USER_SID
),
6621 if (domain_sid_string
) {
6623 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6624 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6625 "read as a valid SID\n", domain_sid_string
));
6626 ldap_msgfree(result
);
6627 TALLOC_FREE(domain_sid_string
);
6628 return NT_STATUS_INVALID_PARAMETER
;
6630 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6631 &secrets_domain_sid
);
6632 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6633 &ldap_domain_sid
)) {
6634 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6635 "%s based on pdb_ldap results %s -> %s\n",
6636 ldap_state
->domain_name
,
6637 sid_string_dbg(&secrets_domain_sid
),
6638 sid_string_dbg(&ldap_domain_sid
)));
6640 /* reset secrets.tdb sid */
6641 secrets_store_domain_sid(ldap_state
->domain_name
,
6643 DEBUG(1, ("New global sam SID: %s\n",
6644 sid_string_dbg(get_global_sam_sid())));
6646 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6647 TALLOC_FREE(domain_sid_string
);
6650 alg_rid_base_string
= smbldap_talloc_single_attribute(
6651 ldap_state
->smbldap_state
->ldap_struct
,
6653 get_attr_key2string( dominfo_attr_list
,
6654 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6656 if (alg_rid_base_string
) {
6657 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6658 if (alg_rid_base
!= algorithmic_rid_base()) {
6659 DEBUG(0, ("The value of 'algorithmic RID base' has "
6660 "changed since the LDAP\n"
6661 "database was initialised. Aborting. \n"));
6662 ldap_msgfree(result
);
6663 TALLOC_FREE(alg_rid_base_string
);
6664 return NT_STATUS_UNSUCCESSFUL
;
6666 TALLOC_FREE(alg_rid_base_string
);
6668 ldap_msgfree(result
);
6670 return NT_STATUS_OK
;
6673 NTSTATUS
pdb_ldap_init(void)
6676 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6679 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6682 /* Let pdb_nds register backends */
6687 return NT_STATUS_OK
;