2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
48 #include "../libcli/auth/libcli_auth.h"
50 #include "idmap_cache.h"
51 #include "../libcli/security/security.h"
52 #include "../lib/util/util_pw.h"
53 #include "lib/winbind_util.h"
54 #include "librpc/gen_ndr/idmap.h"
55 #include "lib/param/loadparm.h"
56 #include "lib/util_sid_passdb.h"
57 #include "lib/util/smb_strtox.h"
58 #include "lib/util/string_wrappers.h"
61 #define DBGC_CLASS DBGC_PASSDB
68 #include "passdb/pdb_ldap.h"
69 #include "passdb/pdb_nds.h"
70 #include "passdb/pdb_ldap_util.h"
71 #include "passdb/pdb_ldap_schema.h"
73 /**********************************************************************
74 Simple helper function to make stuff better readable
75 **********************************************************************/
77 LDAP
*priv2ld(struct ldapsam_privates
*priv
)
79 return smbldap_get_ldap(priv
->smbldap_state
);
82 /**********************************************************************
83 Get the attribute name given a user schame version.
84 **********************************************************************/
86 static const char* get_userattr_key2string( int schema_ver
, int key
)
88 switch ( schema_ver
) {
89 case SCHEMAVER_SAMBASAMACCOUNT
:
90 return get_attr_key2string( attrib_map_v30
, key
);
93 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
99 /**********************************************************************
100 Return the list of attribute names given a user schema version.
101 **********************************************************************/
103 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
105 switch ( schema_ver
) {
106 case SCHEMAVER_SAMBASAMACCOUNT
:
107 return get_attr_list( mem_ctx
, attrib_map_v30
);
109 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
116 /**************************************************************************
117 Return the list of attribute names to delete given a user schema version.
118 **************************************************************************/
120 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
123 switch ( schema_ver
) {
124 case SCHEMAVER_SAMBASAMACCOUNT
:
125 return get_attr_list( mem_ctx
,
126 attrib_map_to_delete_v30
);
128 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
136 /*******************************************************************
137 Generate the LDAP search filter for the objectclass based on the
138 version of the schema we are using.
139 ******************************************************************/
141 static const char* get_objclass_filter( int schema_ver
)
143 fstring objclass_filter
;
146 switch( schema_ver
) {
147 case SCHEMAVER_SAMBASAMACCOUNT
:
148 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
151 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
152 objclass_filter
[0] = '\0';
156 result
= talloc_strdup(talloc_tos(), objclass_filter
);
157 SMB_ASSERT(result
!= NULL
);
161 /*****************************************************************
162 Scan a sequence number off OpenLDAP's syncrepl contextCSN
163 ******************************************************************/
165 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
167 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
168 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
169 LDAPMessage
*msg
= NULL
;
170 LDAPMessage
*entry
= NULL
;
172 char **values
= NULL
;
173 int rc
, num_result
, num_values
, rid
;
179 /* Unfortunatly there is no proper way to detect syncrepl-support in
180 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
181 * but do not show up in the root-DSE yet. Neither we can query the
182 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
183 * objectclass. Currently we require lp_ldap_suffix() to show up as
184 * namingContext. - Guenther
187 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
192 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
196 if (!smbldap_has_naming_context(
197 smbldap_get_ldap(ldap_state
->smbldap_state
),
199 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
200 "as top-level namingContext\n", lp_ldap_suffix()));
204 mem_ctx
= talloc_init("ldapsam_get_seq_num");
207 return NT_STATUS_NO_MEMORY
;
209 if ((attrs
= talloc_array(mem_ctx
, const char *, 2)) == NULL
) {
210 ntstatus
= NT_STATUS_NO_MEMORY
;
214 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
215 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
218 /* consumer syncreplCookie: */
219 /* csn=20050126161620Z#0000001#00#00000 */
220 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
222 suffix
= talloc_asprintf(mem_ctx
,
223 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
225 ntstatus
= NT_STATUS_NO_MEMORY
;
230 /* provider contextCSN */
231 /* 20050126161620Z#000009#00#000000 */
232 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
234 suffix
= talloc_asprintf(mem_ctx
,
235 "cn=ldapsync,%s", lp_ldap_suffix());
238 ntstatus
= NT_STATUS_NO_MEMORY
;
243 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
244 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
246 if (rc
!= LDAP_SUCCESS
) {
250 num_result
= ldap_count_entries(
251 smbldap_get_ldap(ldap_state
->smbldap_state
), msg
);
252 if (num_result
!= 1) {
253 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
257 entry
= ldap_first_entry(
258 smbldap_get_ldap(ldap_state
->smbldap_state
), msg
);
260 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
264 values
= ldap_get_values(
265 smbldap_get_ldap(ldap_state
->smbldap_state
), entry
, attrs
[0]);
266 if (values
== NULL
) {
267 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
271 num_values
= ldap_count_values(values
);
272 if (num_values
== 0) {
273 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
278 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
279 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
284 if (!strncmp(p
, "csn=", strlen("csn=")))
287 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
289 *seq_num
= generalized_to_unix_time(p
);
291 /* very basic sanity check */
293 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
298 ntstatus
= NT_STATUS_OK
;
302 ldap_value_free(values
);
306 talloc_destroy(mem_ctx
);
311 /*******************************************************************
312 Run the search by name.
313 ******************************************************************/
315 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
317 LDAPMessage
** result
,
321 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
325 return LDAP_NO_MEMORY
;
329 * in the filter expression, replace %u with the real name
330 * so in ldap filter, %u MUST exist :-)
332 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
333 get_objclass_filter(ldap_state
->schema_ver
));
335 TALLOC_FREE(escape_user
);
336 return LDAP_NO_MEMORY
;
339 * have to use this here because $ is filtered out
343 filter
= talloc_all_string_sub(talloc_tos(),
344 filter
, "%u", escape_user
);
345 TALLOC_FREE(escape_user
);
347 return LDAP_NO_MEMORY
;
350 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
351 filter
, attr
, result
);
356 /*******************************************************************
357 Run the search by SID.
358 ******************************************************************/
360 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
361 const struct dom_sid
*sid
, LDAPMessage
** result
,
366 struct dom_sid_buf sid_string
;
368 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
369 get_userattr_key2string(ldap_state
->schema_ver
,
371 dom_sid_str_buf(sid
, &sid_string
),
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
);
384 /*******************************************************************
385 Delete complete object or objectclass and attrs from
386 object found in search_result depending on lp_ldap_delete_dn
387 ******************************************************************/
389 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
392 const char *objectclass
,
395 LDAPMod
**mods
= NULL
;
398 BerElement
*ptr
= NULL
;
400 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
402 return LDAP_NO_MEMORY
;
405 if (lp_ldap_delete_dn()) {
406 return smbldap_delete(priv
->smbldap_state
, dn
);
409 /* Ok, delete only the SAM attributes */
411 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
413 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
416 /* We are only allowed to delete the attributes that
419 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
420 if (strequal(*attrib
, name
)) {
421 DEBUG(10, ("ldapsam_delete_entry: deleting "
422 "attribute %s\n", name
));
423 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
434 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
435 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
437 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
440 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
445 temp
= smbldap_talloc_single_attribute(
446 smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
447 get_userattr_key2string(ldap_state
->schema_ver
,
448 LDAP_ATTR_MOD_TIMESTAMP
),
454 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
455 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
465 /**********************************************************************
466 Initialize struct samu from an LDAP query.
467 (Based on init_sam_from_buffer in pdb_tdb.c)
468 *********************************************************************/
470 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
471 struct samu
* sampass
,
478 pass_can_change_time
,
481 char *username
= NULL
,
487 *logon_script
= NULL
,
488 *profile_path
= NULL
,
490 *workstations
= NULL
,
493 uint8_t smblmpwd
[LM_HASH_LEN
],
494 smbntpwd
[NT_HASH_LEN
];
495 bool use_samba_attrs
= True
;
497 uint16_t bad_password_count
= 0,
500 uint8_t hours
[MAX_HOURS_LEN
];
502 struct login_cache cache_entry
;
504 bool expand_explicit
= lp_passdb_expand_explicit();
506 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
511 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
512 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
516 if (priv2ld(ldap_state
) == NULL
) {
517 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
518 "ldap_struct is NULL!\n"));
522 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
526 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
531 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
533 nt_username
= talloc_strdup(ctx
, username
);
538 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
543 pdb_set_username(sampass
, username
, PDB_SET
);
545 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
546 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
548 /* deal with different attributes between the schema first */
550 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
551 if ((temp
= smbldap_talloc_single_attribute(
552 smbldap_get_ldap(ldap_state
->smbldap_state
),
554 get_userattr_key2string(ldap_state
->schema_ver
,
557 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
560 if ((temp
= smbldap_talloc_single_attribute(
561 smbldap_get_ldap(ldap_state
->smbldap_state
),
563 get_userattr_key2string(ldap_state
->schema_ver
,
566 user_rid
= (uint32_t)atol(temp
);
567 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
571 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
572 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
573 get_userattr_key2string(ldap_state
->schema_ver
,
575 get_userattr_key2string(ldap_state
->schema_ver
,
581 temp
= smbldap_talloc_single_attribute(
582 smbldap_get_ldap(ldap_state
->smbldap_state
),
584 get_userattr_key2string(ldap_state
->schema_ver
,
585 LDAP_ATTR_PWD_LAST_SET
),
588 pass_last_set_time
= (time_t) atol(temp
);
589 pdb_set_pass_last_set_time(sampass
,
590 pass_last_set_time
, PDB_SET
);
593 temp
= smbldap_talloc_single_attribute(
594 smbldap_get_ldap(ldap_state
->smbldap_state
),
596 get_userattr_key2string(ldap_state
->schema_ver
,
597 LDAP_ATTR_LOGON_TIME
),
600 logon_time
= (time_t) atol(temp
);
601 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
604 temp
= smbldap_talloc_single_attribute(
605 smbldap_get_ldap(ldap_state
->smbldap_state
),
607 get_userattr_key2string(ldap_state
->schema_ver
,
608 LDAP_ATTR_LOGOFF_TIME
),
611 logoff_time
= (time_t) atol(temp
);
612 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
615 temp
= smbldap_talloc_single_attribute(
616 smbldap_get_ldap(ldap_state
->smbldap_state
),
618 get_userattr_key2string(ldap_state
->schema_ver
,
619 LDAP_ATTR_KICKOFF_TIME
),
622 kickoff_time
= (time_t) atol(temp
);
623 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
626 temp
= smbldap_talloc_single_attribute(
627 smbldap_get_ldap(ldap_state
->smbldap_state
),
629 get_userattr_key2string(ldap_state
->schema_ver
,
630 LDAP_ATTR_PWD_CAN_CHANGE
),
633 pass_can_change_time
= (time_t) atol(temp
);
634 pdb_set_pass_can_change_time(sampass
,
635 pass_can_change_time
, PDB_SET
);
638 /* recommend that 'gecos' and 'displayName' should refer to the same
639 * attribute OID. userFullName depreciated, only used by Samba
640 * primary rules of LDAP: don't make a new attribute when one is already defined
641 * that fits your needs; using cn then displayName rather than 'userFullName'
644 fullname
= smbldap_talloc_single_attribute(
645 smbldap_get_ldap(ldap_state
->smbldap_state
),
647 get_userattr_key2string(ldap_state
->schema_ver
,
648 LDAP_ATTR_DISPLAY_NAME
),
651 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
653 fullname
= smbldap_talloc_single_attribute(
654 smbldap_get_ldap(ldap_state
->smbldap_state
),
656 get_userattr_key2string(ldap_state
->schema_ver
,
660 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
664 dir_drive
= smbldap_talloc_single_attribute(
665 smbldap_get_ldap(ldap_state
->smbldap_state
),
667 get_userattr_key2string(ldap_state
->schema_ver
,
668 LDAP_ATTR_HOME_DRIVE
),
671 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
673 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
676 homedir
= smbldap_talloc_single_attribute(
677 smbldap_get_ldap(ldap_state
->smbldap_state
),
679 get_userattr_key2string(ldap_state
->schema_ver
,
680 LDAP_ATTR_HOME_PATH
),
683 if (expand_explicit
) {
684 homedir
= talloc_sub_basic(ctx
,
692 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
694 pdb_set_homedir(sampass
,
695 talloc_sub_basic(ctx
, username
, domain
,
700 logon_script
= smbldap_talloc_single_attribute(
701 smbldap_get_ldap(ldap_state
->smbldap_state
),
703 get_userattr_key2string(ldap_state
->schema_ver
,
704 LDAP_ATTR_LOGON_SCRIPT
),
707 if (expand_explicit
) {
708 logon_script
= talloc_sub_basic(ctx
,
716 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
718 pdb_set_logon_script(sampass
,
719 talloc_sub_basic(ctx
, username
, domain
,
724 profile_path
= smbldap_talloc_single_attribute(
725 smbldap_get_ldap(ldap_state
->smbldap_state
),
727 get_userattr_key2string(ldap_state
->schema_ver
,
728 LDAP_ATTR_PROFILE_PATH
),
731 if (expand_explicit
) {
732 profile_path
= talloc_sub_basic(ctx
,
740 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
742 pdb_set_profile_path(sampass
,
743 talloc_sub_basic(ctx
, username
, domain
,
748 acct_desc
= smbldap_talloc_single_attribute(
749 smbldap_get_ldap(ldap_state
->smbldap_state
),
751 get_userattr_key2string(ldap_state
->schema_ver
,
755 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
758 workstations
= smbldap_talloc_single_attribute(
759 smbldap_get_ldap(ldap_state
->smbldap_state
),
761 get_userattr_key2string(ldap_state
->schema_ver
,
765 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
768 munged_dial
= smbldap_talloc_single_attribute(
769 smbldap_get_ldap(ldap_state
->smbldap_state
),
771 get_userattr_key2string(ldap_state
->schema_ver
,
772 LDAP_ATTR_MUNGED_DIAL
),
775 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
778 /* FIXME: hours stuff should be cleaner */
782 memset(hours
, 0xff, hours_len
);
784 if (ldap_state
->is_nds_ldap
) {
787 char clear_text_pw
[512];
789 /* Make call to Novell eDirectory ldap extension to get clear text password.
790 NOTE: This will only work if we have an SSL connection to eDirectory. */
791 user_dn
= smbldap_talloc_dn(
792 ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
),
794 if (user_dn
!= NULL
) {
795 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
797 pwd_len
= sizeof(clear_text_pw
);
798 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
799 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
800 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
801 TALLOC_FREE(user_dn
);
804 ZERO_STRUCT(smblmpwd
);
805 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
806 TALLOC_FREE(user_dn
);
809 ZERO_STRUCT(smbntpwd
);
810 use_samba_attrs
= False
;
813 TALLOC_FREE(user_dn
);
816 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
820 if (use_samba_attrs
) {
821 temp
= smbldap_talloc_single_attribute(
822 smbldap_get_ldap(ldap_state
->smbldap_state
),
824 get_userattr_key2string(ldap_state
->schema_ver
,
828 pdb_gethexpwd(temp
, smblmpwd
);
829 memset((char *)temp
, '\0', strlen(temp
)+1);
830 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
833 ZERO_STRUCT(smblmpwd
);
836 temp
= smbldap_talloc_single_attribute(
837 smbldap_get_ldap(ldap_state
->smbldap_state
),
839 get_userattr_key2string(ldap_state
->schema_ver
,
843 pdb_gethexpwd(temp
, smbntpwd
);
844 memset((char *)temp
, '\0', strlen(temp
)+1);
845 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
848 ZERO_STRUCT(smbntpwd
);
854 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
856 uint8_t *pwhist
= NULL
;
858 char *history_string
= talloc_array(ctx
, char,
859 MAX_PW_HISTORY_LEN
*64);
861 if (!history_string
) {
865 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
867 pwhist
= talloc_zero_array(ctx
, uint8_t,
868 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
869 if (pwhist
== NULL
) {
870 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
874 if (smbldap_get_single_attribute(
875 smbldap_get_ldap(ldap_state
->smbldap_state
),
877 get_userattr_key2string(ldap_state
->schema_ver
,
878 LDAP_ATTR_PWD_HISTORY
),
880 MAX_PW_HISTORY_LEN
*64)) {
881 bool hex_failed
= false;
882 for (i
= 0; i
< pwHistLen
; i
++){
883 /* Get the 16 byte salt. */
884 if (!pdb_gethexpwd(&history_string
[i
*64],
885 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
889 /* Get the 16 byte MD5 hash of salt+passwd. */
890 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
891 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
892 PW_HISTORY_SALT_LEN
])) {
898 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
900 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
903 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
908 temp
= smbldap_talloc_single_attribute(
909 smbldap_get_ldap(ldap_state
->smbldap_state
),
911 get_userattr_key2string(ldap_state
->schema_ver
,
915 uint32_t acct_ctrl
= 0;
916 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
918 if (acct_ctrl
== 0) {
919 acct_ctrl
|= ACB_NORMAL
;
922 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
925 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
926 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
928 temp
= smbldap_talloc_single_attribute(
929 smbldap_get_ldap(ldap_state
->smbldap_state
),
931 get_userattr_key2string(ldap_state
->schema_ver
,
932 LDAP_ATTR_BAD_PASSWORD_COUNT
),
935 bad_password_count
= (uint32_t) atol(temp
);
936 pdb_set_bad_password_count(sampass
,
937 bad_password_count
, PDB_SET
);
940 temp
= smbldap_talloc_single_attribute(
941 smbldap_get_ldap(ldap_state
->smbldap_state
),
943 get_userattr_key2string(ldap_state
->schema_ver
,
944 LDAP_ATTR_BAD_PASSWORD_TIME
),
947 bad_password_time
= (time_t) atol(temp
);
948 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
952 temp
= smbldap_talloc_single_attribute(
953 smbldap_get_ldap(ldap_state
->smbldap_state
),
955 get_userattr_key2string(ldap_state
->schema_ver
,
956 LDAP_ATTR_LOGON_COUNT
),
959 logon_count
= (uint32_t) atol(temp
);
960 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
963 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
965 temp
= smbldap_talloc_single_attribute(
966 smbldap_get_ldap(ldap_state
->smbldap_state
),
968 get_userattr_key2string(ldap_state
->schema_ver
,
969 LDAP_ATTR_LOGON_HOURS
),
972 pdb_gethexhours(temp
, hours
);
973 memset((char *)temp
, '\0', strlen(temp
) +1);
974 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
978 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
979 struct passwd unix_pw
;
980 bool have_uid
= false;
981 bool have_gid
= false;
982 struct dom_sid mapped_gsid
;
983 const struct dom_sid
*primary_gsid
;
987 ZERO_STRUCT(unix_pw
);
989 unix_pw
.pw_name
= username
;
990 unix_pw
.pw_passwd
= discard_const_p(char, "x");
992 temp
= smbldap_talloc_single_attribute(
998 /* We've got a uid, feed the cache */
999 unix_pw
.pw_uid
= smb_strtoul(temp
,
1005 DBG_ERR("Failed to convert UID\n");
1010 temp
= smbldap_talloc_single_attribute(
1011 priv2ld(ldap_state
),
1016 /* We've got a uid, feed the cache */
1017 unix_pw
.pw_gid
= smb_strtoul(temp
,
1023 DBG_ERR("Failed to convert GID\n");
1028 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1029 priv2ld(ldap_state
),
1033 if (unix_pw
.pw_gecos
== NULL
) {
1034 unix_pw
.pw_gecos
= fullname
;
1036 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state
),
1041 if (unix_pw
.pw_dir
== NULL
) {
1042 unix_pw
.pw_dir
= discard_const_p(char, "");
1044 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1045 priv2ld(ldap_state
),
1049 if (unix_pw
.pw_shell
== NULL
) {
1050 unix_pw
.pw_shell
= discard_const_p(char, "");
1053 if (have_uid
&& have_gid
) {
1054 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1056 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1059 if (sampass
->unix_pw
== NULL
) {
1060 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1061 pdb_get_username(sampass
)));
1065 id
.id
= sampass
->unix_pw
->pw_uid
;
1066 id
.type
= ID_TYPE_UID
;
1068 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass
), &id
);
1070 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1071 primary_gsid
= pdb_get_group_sid(sampass
);
1072 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1073 id
.id
= sampass
->unix_pw
->pw_gid
;
1074 id
.type
= ID_TYPE_GID
;
1076 idmap_cache_set_sid2unixid(primary_gsid
, &id
);
1080 /* check the timestamp of the cache vs ldap entry */
1081 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1087 /* see if we have newer updates */
1088 if (!login_cache_read(sampass
, &cache_entry
)) {
1089 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1090 (unsigned int)pdb_get_bad_password_count(sampass
),
1091 (unsigned int)pdb_get_bad_password_time(sampass
)));
1096 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1097 (unsigned int)ldap_entry_time
,
1098 (unsigned int)cache_entry
.entry_timestamp
,
1099 (unsigned int)cache_entry
.bad_password_time
));
1101 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1102 /* cache is older than directory , so
1103 we need to delete the entry but allow the
1104 fields to be written out */
1105 login_cache_delentry(sampass
);
1108 pdb_set_acct_ctrl(sampass
,
1109 pdb_get_acct_ctrl(sampass
) |
1110 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1112 pdb_set_bad_password_count(sampass
,
1113 cache_entry
.bad_password_count
,
1115 pdb_set_bad_password_time(sampass
,
1116 cache_entry
.bad_password_time
,
1128 /**********************************************************************
1129 Initialize the ldap db from a struct samu. Called on update.
1130 (Based on init_buffer_from_sam in pdb_tdb.c)
1131 *********************************************************************/
1133 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1134 LDAPMessage
*existing
,
1135 LDAPMod
*** mods
, struct samu
* sampass
,
1136 bool (*need_update
)(const struct samu
*,
1141 if (mods
== NULL
|| sampass
== NULL
) {
1142 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1149 * took out adding "objectclass: sambaAccount"
1150 * do this on a per-mod basis
1152 if (need_update(sampass
, PDB_USERNAME
)) {
1153 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1155 "uid", pdb_get_username(sampass
));
1156 if (ldap_state
->is_nds_ldap
) {
1158 smbldap_get_ldap(ldap_state
->smbldap_state
),
1160 "cn", pdb_get_username(sampass
));
1162 smbldap_get_ldap(ldap_state
->smbldap_state
),
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
)) {
1172 struct dom_sid_buf sid_str
;
1173 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1175 switch ( ldap_state
->schema_ver
) {
1176 case SCHEMAVER_SAMBASAMACCOUNT
:
1179 ldap_state
->smbldap_state
),
1181 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1182 dom_sid_str_buf(user_sid
, &sid_str
));
1186 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1191 /* we don't need to store the primary group RID - so leaving it
1192 'free' to hang off the unix primary group makes life easier */
1194 if (need_update(sampass
, PDB_GROUPSID
)) {
1195 struct dom_sid_buf sid_str
;
1196 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1198 switch ( ldap_state
->schema_ver
) {
1199 case SCHEMAVER_SAMBASAMACCOUNT
:
1202 ldap_state
->smbldap_state
),
1204 get_userattr_key2string(ldap_state
->schema_ver
,
1205 LDAP_ATTR_PRIMARY_GROUP_SID
),
1206 dom_sid_str_buf(group_sid
, &sid_str
));
1210 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1216 /* displayName, cn, and gecos should all be the same
1217 * most easily accomplished by giving them the same OID
1218 * gecos isn't set here b/c it should be handled by the
1220 * We change displayName only and fall back to cn if
1221 * it does not exist.
1224 if (need_update(sampass
, PDB_FULLNAME
))
1225 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1227 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1228 pdb_get_fullname(sampass
));
1230 if (need_update(sampass
, PDB_ACCTDESC
))
1231 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1233 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1234 pdb_get_acct_desc(sampass
));
1236 if (need_update(sampass
, PDB_WORKSTATIONS
))
1237 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1239 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1240 pdb_get_workstations(sampass
));
1242 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1243 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1245 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1246 pdb_get_munged_dial(sampass
));
1248 if (need_update(sampass
, PDB_SMBHOME
))
1249 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1251 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1252 pdb_get_homedir(sampass
));
1254 if (need_update(sampass
, PDB_DRIVE
))
1255 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1257 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1258 pdb_get_dir_drive(sampass
));
1260 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1261 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1263 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1264 pdb_get_logon_script(sampass
));
1266 if (need_update(sampass
, PDB_PROFILE
))
1267 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1269 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1270 pdb_get_profile_path(sampass
));
1272 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1275 if (need_update(sampass
, PDB_LOGONTIME
))
1276 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1278 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1281 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1284 if (need_update(sampass
, PDB_LOGOFFTIME
))
1285 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1287 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1290 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1293 if (need_update(sampass
, PDB_KICKOFFTIME
))
1294 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1296 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1299 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1302 if (need_update(sampass
, PDB_CANCHANGETIME
))
1303 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1305 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1308 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1309 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1311 if (need_update(sampass
, PDB_LMPASSWD
)) {
1312 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1315 pdb_sethexpwd(pwstr
, lm_pw
,
1316 pdb_get_acct_ctrl(sampass
));
1319 ldap_state
->smbldap_state
),
1321 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1326 ldap_state
->smbldap_state
),
1328 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1332 if (need_update(sampass
, PDB_NTPASSWD
)) {
1333 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1336 pdb_sethexpwd(pwstr
, nt_pw
,
1337 pdb_get_acct_ctrl(sampass
));
1340 ldap_state
->smbldap_state
),
1342 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1347 ldap_state
->smbldap_state
),
1349 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1354 if (need_update(sampass
, PDB_PWHISTORY
)) {
1356 uint32_t pwHistLen
= 0;
1357 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1359 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1363 if (pwHistLen
== 0) {
1364 /* Remove any password history from the LDAP store. */
1365 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1369 uint32_t currHistLen
= 0;
1370 const uint8_t *pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1371 if (pwhist
!= NULL
) {
1372 /* We can only store (1024-1/64 password history entries. */
1373 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1374 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1375 /* Store the salt. */
1376 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1377 /* Followed by the md5 hash of salt + md4 hash */
1378 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1379 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1380 DEBUG(100, ("pwstr=%s\n", pwstr
));
1385 smbldap_get_ldap(ldap_state
->smbldap_state
),
1387 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1392 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1393 if (asprintf(&temp
, "%li",
1394 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1398 smbldap_get_ldap(ldap_state
->smbldap_state
),
1400 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1406 if (need_update(sampass
, PDB_HOURS
)) {
1407 const uint8_t *hours
= pdb_get_hours(sampass
);
1410 pdb_sethexhours(hourstr
, hours
);
1412 smbldap_get_ldap(ldap_state
->smbldap_state
),
1415 get_userattr_key2string(ldap_state
->schema_ver
,
1416 LDAP_ATTR_LOGON_HOURS
),
1421 if (need_update(sampass
, PDB_ACCTCTRL
))
1423 smbldap_get_ldap(ldap_state
->smbldap_state
),
1425 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1426 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1428 /* password lockout cache:
1429 - If we are now autolocking or clearing, we write to ldap
1430 - If we are clearing, we delete the cache entry
1431 - If the count is > 0, we update the cache
1433 This even means when autolocking, we cache, just in case the
1434 update doesn't work, and we have to cache the autolock flag */
1436 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1437 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1438 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1439 time_t badtime
= pdb_get_bad_password_time(sampass
);
1441 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1443 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1444 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1446 if ((badcount
>= pol
) || (badcount
== 0)) {
1447 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1448 (unsigned int)badcount
, (unsigned int)badtime
));
1449 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1453 smbldap_get_ldap(ldap_state
->smbldap_state
),
1455 get_userattr_key2string(
1456 ldap_state
->schema_ver
,
1457 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1461 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1465 smbldap_get_ldap(ldap_state
->smbldap_state
),
1467 get_userattr_key2string(
1468 ldap_state
->schema_ver
,
1469 LDAP_ATTR_BAD_PASSWORD_TIME
),
1473 if (badcount
== 0) {
1474 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1475 login_cache_delentry(sampass
);
1477 struct login_cache cache_entry
;
1479 cache_entry
.entry_timestamp
= time(NULL
);
1480 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1481 cache_entry
.bad_password_count
= badcount
;
1482 cache_entry
.bad_password_time
= badtime
;
1484 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1485 login_cache_write(sampass
, &cache_entry
);
1492 /**********************************************************************
1493 End enumeration of the LDAP password list.
1494 *********************************************************************/
1496 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1498 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1499 if (ldap_state
->result
) {
1500 ldap_msgfree(ldap_state
->result
);
1501 ldap_state
->result
= NULL
;
1505 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1506 const char *new_attr
)
1510 if (new_attr
== NULL
) {
1514 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1518 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1520 SMB_ASSERT((*attr_list
) != NULL
);
1521 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1522 (*attr_list
)[i
+1] = NULL
;
1525 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1526 const char ***attr_list
)
1528 append_attr(mem_ctx
, attr_list
, "uidNumber");
1529 append_attr(mem_ctx
, attr_list
, "gidNumber");
1530 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1531 append_attr(mem_ctx
, attr_list
, "loginShell");
1532 append_attr(mem_ctx
, attr_list
, "gecos");
1535 /**********************************************************************
1536 Get struct samu entry from LDAP by username.
1537 *********************************************************************/
1539 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1541 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1542 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1543 LDAPMessage
*result
= NULL
;
1544 LDAPMessage
*entry
= NULL
;
1546 const char ** attr_list
;
1549 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1550 append_attr(user
, &attr_list
,
1551 get_userattr_key2string(ldap_state
->schema_ver
,
1552 LDAP_ATTR_MOD_TIMESTAMP
));
1553 ldapsam_add_unix_attributes(user
, &attr_list
);
1554 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1556 TALLOC_FREE( attr_list
);
1558 if ( rc
!= LDAP_SUCCESS
)
1559 return NT_STATUS_NO_SUCH_USER
;
1561 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1565 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1566 ldap_msgfree(result
);
1567 return NT_STATUS_NO_SUCH_USER
;
1568 } else if (count
> 1) {
1569 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1570 ldap_msgfree(result
);
1571 return NT_STATUS_NO_SUCH_USER
;
1574 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1577 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1578 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1579 ldap_msgfree(result
);
1580 return NT_STATUS_NO_SUCH_USER
;
1582 pdb_set_backend_private_data(user
, result
, NULL
,
1583 my_methods
, PDB_CHANGED
);
1584 smbldap_talloc_autofree_ldapmsg(user
, result
);
1587 ldap_msgfree(result
);
1592 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1593 const struct dom_sid
*sid
, LDAPMessage
**result
)
1596 const char ** attr_list
;
1598 switch ( ldap_state
->schema_ver
) {
1599 case SCHEMAVER_SAMBASAMACCOUNT
: {
1600 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1601 if (tmp_ctx
== NULL
) {
1602 return LDAP_NO_MEMORY
;
1605 attr_list
= get_userattr_list(tmp_ctx
,
1606 ldap_state
->schema_ver
);
1607 append_attr(tmp_ctx
, &attr_list
,
1608 get_userattr_key2string(
1609 ldap_state
->schema_ver
,
1610 LDAP_ATTR_MOD_TIMESTAMP
));
1611 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1612 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1614 TALLOC_FREE(tmp_ctx
);
1616 if ( rc
!= LDAP_SUCCESS
)
1622 DEBUG(0,("Invalid schema version specified\n"));
1628 /**********************************************************************
1629 Get struct samu entry from LDAP by SID.
1630 *********************************************************************/
1632 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1634 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1635 LDAPMessage
*result
= NULL
;
1636 LDAPMessage
*entry
= NULL
;
1640 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1642 if (rc
!= LDAP_SUCCESS
)
1643 return NT_STATUS_NO_SUCH_USER
;
1645 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1649 struct dom_sid_buf buf
;
1650 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1652 dom_sid_str_buf(sid
, &buf
),
1654 ldap_msgfree(result
);
1655 return NT_STATUS_NO_SUCH_USER
;
1656 } else if (count
> 1) {
1657 struct dom_sid_buf buf
;
1658 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1659 "[%s]. Failing. count=%d\n",
1660 dom_sid_str_buf(sid
, &buf
),
1662 ldap_msgfree(result
);
1663 return NT_STATUS_NO_SUCH_USER
;
1666 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1669 ldap_msgfree(result
);
1670 return NT_STATUS_NO_SUCH_USER
;
1673 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1674 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1675 ldap_msgfree(result
);
1676 return NT_STATUS_NO_SUCH_USER
;
1679 pdb_set_backend_private_data(user
, result
, NULL
,
1680 my_methods
, PDB_CHANGED
);
1681 smbldap_talloc_autofree_ldapmsg(user
, result
);
1682 return NT_STATUS_OK
;
1685 /********************************************************************
1686 Do the actual modification - also change a plaintext passord if
1688 **********************************************************************/
1690 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1691 struct samu
*newpwd
, char *dn
,
1692 LDAPMod
**mods
, int ldap_op
,
1693 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1695 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1698 if (!newpwd
|| !dn
) {
1699 return NT_STATUS_INVALID_PARAMETER
;
1702 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1703 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1704 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1705 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1708 char *retoid
= NULL
;
1709 struct berval
*retdata
= NULL
;
1710 char *utf8_password
;
1712 size_t converted_size
;
1715 if (!ldap_state
->is_nds_ldap
) {
1717 if (!smbldap_has_extension(
1719 ldap_state
->smbldap_state
),
1720 LDAP_EXOP_MODIFY_PASSWD
)) {
1721 DEBUG(2, ("ldap password change requested, but LDAP "
1722 "server does not support it -- ignoring\n"));
1723 return NT_STATUS_OK
;
1727 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1728 pdb_get_plaintext_passwd(newpwd
),
1731 return NT_STATUS_NO_MEMORY
;
1734 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1735 TALLOC_FREE(utf8_password
);
1736 return NT_STATUS_NO_MEMORY
;
1739 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1740 DEBUG(0,("ber_alloc_t returns NULL\n"));
1741 TALLOC_FREE(utf8_password
);
1742 TALLOC_FREE(utf8_dn
);
1743 return NT_STATUS_UNSUCCESSFUL
;
1746 if ((ber_printf (ber
, "{") < 0) ||
1747 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1749 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1752 TALLOC_FREE(utf8_dn
);
1753 TALLOC_FREE(utf8_password
);
1754 return NT_STATUS_UNSUCCESSFUL
;
1757 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1758 ret
= ber_printf(ber
, "ts}",
1759 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1762 ret
= ber_printf(ber
, "}");
1766 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1769 TALLOC_FREE(utf8_dn
);
1770 TALLOC_FREE(utf8_password
);
1771 return NT_STATUS_UNSUCCESSFUL
;
1774 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1775 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1777 TALLOC_FREE(utf8_dn
);
1778 TALLOC_FREE(utf8_password
);
1779 return NT_STATUS_UNSUCCESSFUL
;
1782 TALLOC_FREE(utf8_dn
);
1783 TALLOC_FREE(utf8_password
);
1786 if (!ldap_state
->is_nds_ldap
) {
1787 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1788 LDAP_EXOP_MODIFY_PASSWD
,
1789 bv
, NULL
, NULL
, &retoid
,
1792 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1793 pdb_get_plaintext_passwd(newpwd
));
1795 if (rc
!= LDAP_SUCCESS
) {
1796 char *ld_error
= NULL
;
1798 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1799 DEBUG(3, ("Could not set userPassword "
1800 "attribute due to an objectClass "
1801 "violation -- ignoring\n"));
1803 return NT_STATUS_OK
;
1807 smbldap_get_ldap(ldap_state
->smbldap_state
),
1808 LDAP_OPT_ERROR_STRING
,
1810 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1811 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1812 SAFE_FREE(ld_error
);
1814 #if defined(LDAP_CONSTRAINT_VIOLATION)
1815 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1816 return NT_STATUS_PASSWORD_RESTRICTION
;
1818 return NT_STATUS_UNSUCCESSFUL
;
1820 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1821 #ifdef DEBUG_PASSWORD
1822 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1825 ber_bvfree(retdata
);
1827 ldap_memfree(retoid
);
1833 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1834 /* may be password change below however */
1838 if (ldap_state
->is_nds_ldap
) {
1839 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1843 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1847 rc
= smbldap_add(ldap_state
->smbldap_state
,
1850 case LDAP_MOD_REPLACE
:
1851 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1855 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1857 return NT_STATUS_INVALID_PARAMETER
;
1860 if (rc
!=LDAP_SUCCESS
) {
1861 return NT_STATUS_UNSUCCESSFUL
;
1865 return NT_STATUS_OK
;
1868 /**********************************************************************
1869 Delete entry from LDAP for username.
1870 *********************************************************************/
1872 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1873 struct samu
* sam_acct
)
1875 struct ldapsam_privates
*priv
=
1876 (struct ldapsam_privates
*)my_methods
->private_data
;
1879 LDAPMessage
*msg
, *entry
;
1880 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1881 const char **attr_list
;
1882 TALLOC_CTX
*mem_ctx
;
1885 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1886 return NT_STATUS_INVALID_PARAMETER
;
1889 sname
= pdb_get_username(sam_acct
);
1891 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1894 mem_ctx
= talloc_new(NULL
);
1895 if (mem_ctx
== NULL
) {
1896 DEBUG(0, ("talloc_new failed\n"));
1900 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1901 if (attr_list
== NULL
) {
1905 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1907 if ((rc
!= LDAP_SUCCESS
) ||
1908 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1909 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1910 DEBUG(5, ("Could not find user %s\n", sname
));
1911 result
= NT_STATUS_NO_SUCH_USER
;
1915 rc
= ldapsam_delete_entry(
1916 priv
, mem_ctx
, entry
,
1917 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1918 LDAP_OBJ_SAMBASAMACCOUNT
: 0,
1921 result
= (rc
== LDAP_SUCCESS
) ?
1922 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1925 TALLOC_FREE(mem_ctx
);
1929 /**********************************************************************
1931 *********************************************************************/
1933 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1936 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1939 LDAPMessage
*result
= NULL
;
1940 LDAPMessage
*entry
= NULL
;
1941 LDAPMod
**mods
= NULL
;
1942 const char **attr_list
;
1944 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1946 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1947 if (pdb_get_username(newpwd
) == NULL
) {
1948 return NT_STATUS_INVALID_PARAMETER
;
1950 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1951 TALLOC_FREE( attr_list
);
1952 if (rc
!= LDAP_SUCCESS
) {
1953 return NT_STATUS_UNSUCCESSFUL
;
1955 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1956 my_methods
, PDB_CHANGED
);
1957 smbldap_talloc_autofree_ldapmsg(newpwd
, result
);
1960 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1962 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1963 return NT_STATUS_UNSUCCESSFUL
;
1966 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1968 dn
= smbldap_talloc_dn(talloc_tos(),
1969 smbldap_get_ldap(ldap_state
->smbldap_state
),
1972 return NT_STATUS_UNSUCCESSFUL
;
1975 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1977 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1978 pdb_element_is_changed
)) {
1979 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1982 ldap_mods_free(mods
,True
);
1983 return NT_STATUS_UNSUCCESSFUL
;
1986 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1987 && (mods
== NULL
)) {
1988 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1989 pdb_get_username(newpwd
)));
1991 return NT_STATUS_OK
;
1994 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
1997 ldap_mods_free(mods
,True
);
2003 * We need to set the backend private data to NULL here. For example
2004 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2005 * same one, and with the explicit delete / add logic for attribute
2006 * values the second time we would use the wrong "old" value which
2007 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2009 * The existing LDAPMessage is still being auto-freed by the
2012 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2015 if (!NT_STATUS_IS_OK(ret
)) {
2019 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2020 pdb_get_username(newpwd
)));
2021 return NT_STATUS_OK
;
2024 /***************************************************************************
2025 Renames a struct samu
2026 - The "rename user script" has full responsibility for changing everything
2027 ***************************************************************************/
2029 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2030 TALLOC_CTX
*tmp_ctx
,
2032 uint32_t member_rid
);
2034 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2035 TALLOC_CTX
*mem_ctx
,
2037 struct dom_sid
**pp_sids
,
2039 uint32_t *p_num_groups
);
2041 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2042 struct samu
*old_acct
,
2043 const char *newname
)
2045 const struct loadparm_substitution
*lp_sub
=
2046 loadparm_s3_global_substitution();
2047 const char *oldname
;
2049 char *rename_script
= NULL
;
2050 fstring oldname_lower
, newname_lower
;
2053 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2054 return NT_STATUS_INVALID_PARAMETER
;
2057 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2058 return NT_STATUS_INVALID_PARAMETER
;
2061 oldname
= pdb_get_username(old_acct
);
2063 /* rename the posix user */
2064 rename_script
= lp_rename_user_script(talloc_tos(), lp_sub
);
2065 if (rename_script
== NULL
) {
2066 return NT_STATUS_NO_MEMORY
;
2069 if (!(*rename_script
)) {
2070 TALLOC_FREE(rename_script
);
2071 return NT_STATUS_ACCESS_DENIED
;
2074 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2077 /* We have to allow the account name to end with a '$'.
2078 Also, follow the semantics in _samr_create_user() and lower case the
2079 posix name but preserve the case in passdb */
2081 fstrcpy( oldname_lower
, oldname
);
2082 if (!strlower_m( oldname_lower
)) {
2083 return NT_STATUS_INVALID_PARAMETER
;
2085 fstrcpy( newname_lower
, newname
);
2086 if (!strlower_m( newname_lower
)) {
2087 return NT_STATUS_INVALID_PARAMETER
;
2090 rename_script
= realloc_string_sub2(rename_script
,
2095 if (!rename_script
) {
2096 return NT_STATUS_NO_MEMORY
;
2098 rename_script
= realloc_string_sub2(rename_script
,
2103 rc
= smbrun(rename_script
, NULL
, NULL
);
2105 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2106 rename_script
, rc
));
2108 TALLOC_FREE(rename_script
);
2111 smb_nscd_flush_user_cache();
2115 return NT_STATUS_UNSUCCESSFUL
;
2117 return NT_STATUS_OK
;
2120 /**********************************************************************
2121 Add struct samu to LDAP.
2122 *********************************************************************/
2124 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2126 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2128 LDAPMessage
*result
= NULL
;
2129 LDAPMessage
*entry
= NULL
;
2130 LDAPMod
**mods
= NULL
;
2131 int ldap_op
= LDAP_MOD_REPLACE
;
2132 uint32_t num_result
;
2133 const char **attr_list
;
2134 char *escape_user
= NULL
;
2135 const char *username
= pdb_get_username(newpwd
);
2136 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2137 char *filter
= NULL
;
2139 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2140 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2143 return NT_STATUS_NO_MEMORY
;
2146 if (!username
|| !*username
) {
2147 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2148 status
= NT_STATUS_INVALID_PARAMETER
;
2152 /* free this list after the second search or in case we exit on failure */
2153 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2155 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2157 if (rc
!= LDAP_SUCCESS
) {
2161 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
2163 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2167 ldap_msgfree(result
);
2170 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2171 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2173 if (rc
== LDAP_SUCCESS
) {
2174 if (ldap_count_entries(
2176 ldap_state
->smbldap_state
),
2178 struct dom_sid_buf buf
;
2179 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2180 "already in the base, with samba "
2182 dom_sid_str_buf(sid
, &buf
)));
2185 ldap_msgfree(result
);
2190 /* does the entry already exist but without a samba attributes?
2191 we need to return the samba attributes here */
2193 escape_user
= escape_ldap_string(talloc_tos(), username
);
2194 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2196 status
= NT_STATUS_NO_MEMORY
;
2199 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2200 TALLOC_FREE(escape_user
);
2202 status
= NT_STATUS_NO_MEMORY
;
2206 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2207 filter
, attr_list
, &result
);
2208 if ( rc
!= LDAP_SUCCESS
) {
2212 num_result
= ldap_count_entries(
2213 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2215 if (num_result
> 1) {
2216 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2220 /* Check if we need to update an existing entry */
2221 if (num_result
== 1) {
2222 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2223 ldap_op
= LDAP_MOD_REPLACE
;
2224 entry
= ldap_first_entry(
2225 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2226 dn
= smbldap_talloc_dn(
2227 ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
),
2230 status
= NT_STATUS_NO_MEMORY
;
2234 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2236 struct dom_sid_buf buf
;
2238 /* There might be a SID for this account already - say an idmap entry */
2240 filter
= talloc_asprintf(ctx
,
2241 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2242 get_userattr_key2string(ldap_state
->schema_ver
,
2243 LDAP_ATTR_USER_SID
),
2244 dom_sid_str_buf(sid
, &buf
),
2245 LDAP_OBJ_IDMAP_ENTRY
,
2246 LDAP_OBJ_SID_ENTRY
);
2248 status
= NT_STATUS_NO_MEMORY
;
2252 /* free old result before doing a new search */
2253 if (result
!= NULL
) {
2254 ldap_msgfree(result
);
2257 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2258 filter
, attr_list
, &result
);
2260 if ( rc
!= LDAP_SUCCESS
) {
2264 num_result
= ldap_count_entries(
2265 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2267 if (num_result
> 1) {
2268 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2272 /* Check if we need to update an existing entry */
2273 if (num_result
== 1) {
2275 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2276 ldap_op
= LDAP_MOD_REPLACE
;
2277 entry
= ldap_first_entry (
2278 smbldap_get_ldap(ldap_state
->smbldap_state
),
2280 dn
= smbldap_talloc_dn (
2282 smbldap_get_ldap(ldap_state
->smbldap_state
),
2285 status
= NT_STATUS_NO_MEMORY
;
2291 if (num_result
== 0) {
2292 char *escape_username
;
2293 /* Check if we need to add an entry */
2294 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2295 ldap_op
= LDAP_MOD_ADD
;
2297 escape_username
= escape_rdn_val_string_alloc(username
);
2298 if (!escape_username
) {
2299 status
= NT_STATUS_NO_MEMORY
;
2303 if (username
[strlen(username
)-1] == '$') {
2304 dn
= talloc_asprintf(ctx
,
2307 lp_ldap_machine_suffix(talloc_tos()));
2309 dn
= talloc_asprintf(ctx
,
2312 lp_ldap_user_suffix(talloc_tos()));
2315 SAFE_FREE(escape_username
);
2317 status
= NT_STATUS_NO_MEMORY
;
2322 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2323 pdb_element_is_set_or_changed
)) {
2324 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2326 ldap_mods_free(mods
, true);
2332 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2335 switch ( ldap_state
->schema_ver
) {
2336 case SCHEMAVER_SAMBASAMACCOUNT
:
2337 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2340 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2344 status
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2345 if (!NT_STATUS_IS_OK(status
)) {
2346 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2347 pdb_get_username(newpwd
),dn
));
2348 ldap_mods_free(mods
, true);
2352 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2353 ldap_mods_free(mods
, true);
2355 status
= NT_STATUS_OK
;
2361 ldap_msgfree(result
);
2366 /**********************************************************************
2367 *********************************************************************/
2369 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2371 LDAPMessage
** result
)
2373 int scope
= LDAP_SCOPE_SUBTREE
;
2375 const char **attr_list
;
2377 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2378 rc
= smbldap_search(ldap_state
->smbldap_state
,
2379 lp_ldap_suffix(), scope
,
2380 filter
, attr_list
, 0, result
);
2381 TALLOC_FREE(attr_list
);
2386 /**********************************************************************
2387 *********************************************************************/
2389 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2390 GROUP_MAP
*map
, LDAPMessage
*entry
)
2393 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2395 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2396 smbldap_get_ldap(ldap_state
->smbldap_state
) == NULL
) {
2397 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2402 temp
= smbldap_talloc_single_attribute(
2403 smbldap_get_ldap(ldap_state
->smbldap_state
),
2405 get_attr_key2string(groupmap_attr_list
,
2406 LDAP_ATTR_GIDNUMBER
),
2409 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2410 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2414 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2416 map
->gid
= (gid_t
)atol(temp
);
2419 temp
= smbldap_talloc_single_attribute(
2420 smbldap_get_ldap(ldap_state
->smbldap_state
),
2422 get_attr_key2string(groupmap_attr_list
,
2423 LDAP_ATTR_GROUP_SID
),
2426 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2427 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2432 if (!string_to_sid(&map
->sid
, temp
)) {
2433 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2439 temp
= smbldap_talloc_single_attribute(
2440 smbldap_get_ldap(ldap_state
->smbldap_state
),
2442 get_attr_key2string(groupmap_attr_list
,
2443 LDAP_ATTR_GROUP_TYPE
),
2446 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2447 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2451 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2453 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2454 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2455 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2461 temp
= smbldap_talloc_single_attribute(
2462 smbldap_get_ldap(ldap_state
->smbldap_state
),
2464 get_attr_key2string(groupmap_attr_list
,
2465 LDAP_ATTR_DISPLAY_NAME
),
2468 temp
= smbldap_talloc_single_attribute(
2469 smbldap_get_ldap(ldap_state
->smbldap_state
),
2471 get_attr_key2string(groupmap_attr_list
,
2475 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2476 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2481 map
->nt_name
= talloc_strdup(map
, temp
);
2482 if (!map
->nt_name
) {
2488 temp
= smbldap_talloc_single_attribute(
2489 smbldap_get_ldap(ldap_state
->smbldap_state
),
2491 get_attr_key2string(groupmap_attr_list
,
2495 temp
= talloc_strdup(ctx
, "");
2501 map
->comment
= talloc_strdup(map
, temp
);
2502 if (!map
->comment
) {
2507 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2510 id
.type
= ID_TYPE_GID
;
2512 idmap_cache_set_sid2unixid(&map
->sid
, &id
);
2519 /**********************************************************************
2520 *********************************************************************/
2522 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2526 struct ldapsam_privates
*ldap_state
=
2527 (struct ldapsam_privates
*)methods
->private_data
;
2528 LDAPMessage
*result
= NULL
;
2529 LDAPMessage
*entry
= NULL
;
2532 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2534 return NT_STATUS_NO_SUCH_GROUP
;
2537 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2540 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2542 ldap_msgfree(result
);
2543 return NT_STATUS_NO_SUCH_GROUP
;
2547 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2548 "count=%d\n", filter
, count
));
2549 ldap_msgfree(result
);
2550 return NT_STATUS_NO_SUCH_GROUP
;
2553 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2556 ldap_msgfree(result
);
2557 return NT_STATUS_UNSUCCESSFUL
;
2560 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2561 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2562 "group filter %s\n", filter
));
2563 ldap_msgfree(result
);
2564 return NT_STATUS_NO_SUCH_GROUP
;
2567 ldap_msgfree(result
);
2568 return NT_STATUS_OK
;
2571 /**********************************************************************
2572 *********************************************************************/
2574 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2577 char *filter
= NULL
;
2579 struct dom_sid_buf tmp
;
2581 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2583 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2584 dom_sid_str_buf(&sid
, &tmp
)) < 0) {
2585 return NT_STATUS_NO_MEMORY
;
2588 status
= ldapsam_getgroup(methods
, filter
, map
);
2593 /**********************************************************************
2594 *********************************************************************/
2596 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2599 char *filter
= NULL
;
2602 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2604 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2605 (unsigned long)gid
) < 0) {
2606 return NT_STATUS_NO_MEMORY
;
2609 status
= ldapsam_getgroup(methods
, filter
, map
);
2614 /**********************************************************************
2615 *********************************************************************/
2617 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2620 char *filter
= NULL
;
2621 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2625 return NT_STATUS_NO_MEMORY
;
2628 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2630 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2631 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2633 TALLOC_FREE(escape_name
);
2634 return NT_STATUS_NO_MEMORY
;
2637 TALLOC_FREE(escape_name
);
2638 status
= ldapsam_getgroup(methods
, filter
, map
);
2643 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2645 const struct dom_sid
*domain_sid
,
2651 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2652 str
, sizeof(str
)-1)) {
2653 DEBUG(10, ("Could not find sambaSID attribute\n"));
2657 if (!string_to_sid(&sid
, str
)) {
2658 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2662 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2663 struct dom_sid_buf buf
;
2664 DEBUG(10, ("SID %s is not in expected domain %s\n",
2666 dom_sid_str_buf(domain_sid
, &buf
)));
2670 if (!sid_peek_rid(&sid
, rid
)) {
2671 DEBUG(10, ("Could not peek into RID\n"));
2678 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2679 TALLOC_CTX
*mem_ctx
,
2680 const struct dom_sid
*group
,
2681 uint32_t **pp_member_rids
,
2682 size_t *p_num_members
)
2684 struct ldapsam_privates
*ldap_state
=
2685 (struct ldapsam_privates
*)methods
->private_data
;
2686 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2687 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2688 const char *sid_attrs
[] = { "sambaSID", NULL
};
2689 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2690 LDAPMessage
*result
= NULL
;
2693 char **values
= NULL
;
2697 struct dom_sid_buf buf
;
2699 *pp_member_rids
= NULL
;
2702 filter
= talloc_asprintf(mem_ctx
,
2703 "(&(objectClass=%s)"
2706 LDAP_OBJ_POSIXGROUP
,
2708 dom_sid_str_buf(group
, &buf
));
2709 if (filter
== NULL
) {
2710 ret
= NT_STATUS_NO_MEMORY
;
2714 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2715 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2718 if (rc
!= LDAP_SUCCESS
)
2721 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2723 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2726 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2727 dom_sid_str_buf(group
, &buf
)));
2728 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2733 ret
= NT_STATUS_NO_SUCH_GROUP
;
2737 entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2741 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2743 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2744 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2748 values
= ldap_get_values(smbldap_get_ldap(conn
), entry
, "memberUid");
2750 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2752 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2753 if (filter
== NULL
) {
2754 ret
= NT_STATUS_NO_MEMORY
;
2758 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2759 char *escape_memberuid
;
2761 escape_memberuid
= escape_ldap_string(talloc_tos(),
2763 if (escape_memberuid
== NULL
) {
2764 ret
= NT_STATUS_NO_MEMORY
;
2768 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2769 TALLOC_FREE(escape_memberuid
);
2770 if (filter
== NULL
) {
2771 ret
= NT_STATUS_NO_MEMORY
;
2776 filter
= talloc_asprintf_append_buffer(filter
, "))");
2777 if (filter
== NULL
) {
2778 ret
= NT_STATUS_NO_MEMORY
;
2782 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2783 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2786 if (rc
!= LDAP_SUCCESS
)
2789 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2790 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2792 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2794 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2796 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2802 sidstr
= smbldap_talloc_single_attribute(
2803 smbldap_get_ldap(conn
), entry
, "sambaSID",
2806 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2807 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2808 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2812 if (!string_to_sid(&sid
, sidstr
))
2815 if (!sid_check_is_in_our_sam(&sid
)) {
2816 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2817 "in our domain\n"));
2818 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2822 sid_peek_rid(&sid
, &rid
);
2824 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2826 ret
= NT_STATUS_NO_MEMORY
;
2832 filter
= talloc_asprintf(mem_ctx
,
2833 "(&(objectClass=%s)"
2835 LDAP_OBJ_SAMBASAMACCOUNT
,
2838 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2839 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2842 if (rc
!= LDAP_SUCCESS
)
2845 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2847 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2849 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2853 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn
),
2855 get_global_sam_sid(),
2857 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2858 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2862 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2864 ret
= NT_STATUS_NO_MEMORY
;
2874 ldap_value_free(values
);
2879 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2880 TALLOC_CTX
*mem_ctx
,
2882 struct dom_sid
**pp_sids
,
2884 uint32_t *p_num_groups
)
2886 struct ldapsam_privates
*ldap_state
=
2887 (struct ldapsam_privates
*)methods
->private_data
;
2888 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2890 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2893 LDAPMessage
*result
= NULL
;
2895 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2899 gid_t primary_gid
= -1;
2905 if (pdb_get_username(user
) == NULL
) {
2906 return NT_STATUS_INVALID_PARAMETER
;
2909 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2910 if (escape_name
== NULL
)
2911 return NT_STATUS_NO_MEMORY
;
2913 if (user
->unix_pw
) {
2914 primary_gid
= user
->unix_pw
->pw_gid
;
2916 /* retrieve the users primary gid */
2917 filter
= talloc_asprintf(mem_ctx
,
2918 "(&(objectClass=%s)(uid=%s))",
2919 LDAP_OBJ_SAMBASAMACCOUNT
,
2921 if (filter
== NULL
) {
2922 ret
= NT_STATUS_NO_MEMORY
;
2926 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2927 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2929 if (rc
!= LDAP_SUCCESS
)
2932 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2934 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2938 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2939 ret
= NT_STATUS_NO_SUCH_USER
;
2942 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2944 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2946 DEBUG (1, ("Unable to find the member's gid!\n"));
2947 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2950 primary_gid
= smb_strtoul(gidstr
,
2956 DBG_ERR("Failed to convert GID\n");
2961 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2962 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2967 filter
= talloc_asprintf(mem_ctx
,
2968 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2969 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2970 if (filter
== NULL
) {
2971 ret
= NT_STATUS_NO_MEMORY
;
2975 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2976 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2978 if (rc
!= LDAP_SUCCESS
)
2981 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2989 /* We need to add the primary group as the first gid/sid */
2991 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2992 ret
= NT_STATUS_NO_MEMORY
;
2996 /* This sid will be replaced later */
2998 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
3000 if (!NT_STATUS_IS_OK(ret
)) {
3004 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
3006 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
3012 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3014 str
, sizeof(str
)-1))
3017 if (!string_to_sid(&sid
, str
))
3020 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3022 str
, sizeof(str
)-1))
3025 gid
= smb_strtoul(str
, NULL
, 10, &error
, SMB_STR_FULL_STR_CONV
);
3031 if (gid
== primary_gid
) {
3032 sid_copy(&(*pp_sids
)[0], &sid
);
3034 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3036 ret
= NT_STATUS_NO_MEMORY
;
3039 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3041 if (!NT_STATUS_IS_OK(ret
)) {
3047 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3048 DEBUG(3, ("primary group of [%s] not found\n",
3049 pdb_get_username(user
)));
3050 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
3054 *p_num_groups
= num_sids
;
3060 TALLOC_FREE(escape_name
);
3064 /**********************************************************************
3065 * Augment a posixGroup object with a sambaGroupMapping domgroup
3066 *********************************************************************/
3068 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3069 struct ldapsam_privates
*ldap_state
,
3072 const char *filter
, *dn
;
3073 LDAPMessage
*msg
, *entry
;
3075 struct dom_sid_buf buf
;
3078 filter
= talloc_asprintf(mem_ctx
,
3079 "(&(objectClass=%s)(gidNumber=%u))",
3080 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3081 if (filter
== NULL
) {
3082 return NT_STATUS_NO_MEMORY
;
3085 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3086 get_attr_list(mem_ctx
, groupmap_attr_list
),
3088 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3090 if ((rc
!= LDAP_SUCCESS
) ||
3091 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3093 ((entry
= ldap_first_entry(
3094 smbldap_get_ldap(ldap_state
->smbldap_state
),
3096 return NT_STATUS_NO_SUCH_GROUP
;
3099 dn
= smbldap_talloc_dn(mem_ctx
,
3100 smbldap_get_ldap(ldap_state
->smbldap_state
),
3103 return NT_STATUS_NO_MEMORY
;
3107 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3109 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3111 dom_sid_str_buf(&map
->sid
, &buf
));
3112 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3113 &mods
, "sambaGroupType",
3114 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3115 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3116 &mods
, "displayName",
3118 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3119 &mods
, "description",
3121 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3123 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3124 if (rc
!= LDAP_SUCCESS
) {
3125 return NT_STATUS_ACCESS_DENIED
;
3128 return NT_STATUS_OK
;
3131 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3134 struct ldapsam_privates
*ldap_state
=
3135 (struct ldapsam_privates
*)methods
->private_data
;
3136 LDAPMessage
*msg
= NULL
;
3137 LDAPMod
**mods
= NULL
;
3138 const char *attrs
[] = { NULL
};
3142 TALLOC_CTX
*mem_ctx
;
3146 struct dom_sid_buf buf
;
3151 mem_ctx
= talloc_new(NULL
);
3152 if (mem_ctx
== NULL
) {
3153 DEBUG(0, ("talloc_new failed\n"));
3154 return NT_STATUS_NO_MEMORY
;
3157 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3158 dom_sid_str_buf(&map
->sid
, &buf
));
3159 if (filter
== NULL
) {
3160 result
= NT_STATUS_NO_MEMORY
;
3164 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3165 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3166 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3168 if ((rc
== LDAP_SUCCESS
) &&
3169 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3172 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3173 "group mapping entry\n",
3174 dom_sid_str_buf(&map
->sid
, &buf
)));
3175 result
= NT_STATUS_GROUP_EXISTS
;
3179 switch (map
->sid_name_use
) {
3181 case SID_NAME_DOM_GRP
:
3182 /* To map a domain group we need to have a posix group
3184 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3188 case SID_NAME_ALIAS
:
3189 if (!sid_check_is_in_our_sam(&map
->sid
)
3190 && !sid_check_is_in_builtin(&map
->sid
) )
3192 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3193 dom_sid_str_buf(&map
->sid
, &buf
)));
3194 result
= NT_STATUS_INVALID_PARAMETER
;
3200 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3201 sid_type_lookup(map
->sid_name_use
)));
3202 result
= NT_STATUS_INVALID_PARAMETER
;
3206 /* Domain groups have been mapped in a separate routine, we have to
3207 * create an alias now */
3209 if (map
->gid
== -1) {
3210 DEBUG(10, ("Refusing to map gid==-1\n"));
3211 result
= NT_STATUS_INVALID_PARAMETER
;
3216 id
.type
= ID_TYPE_GID
;
3218 if (pdb_id_to_sid(&id
, &sid
)) {
3219 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3221 (unsigned int)map
->gid
,
3222 dom_sid_str_buf(&sid
, &buf
)));
3223 result
= NT_STATUS_GROUP_EXISTS
;
3227 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3228 * the best we can get out of LDAP. */
3230 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3231 dom_sid_str_buf(&map
->sid
, &buf
),
3232 lp_ldap_group_suffix(talloc_tos()));
3234 result
= NT_STATUS_NO_MEMORY
;
3240 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3241 &mods
, "objectClass", LDAP_OBJ_SID_ENTRY
);
3242 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3243 &mods
, "objectClass", LDAP_OBJ_GROUPMAP
);
3244 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3246 dom_sid_str_buf(&map
->sid
, &buf
));
3247 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3248 &mods
, "sambaGroupType",
3249 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3250 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3251 &mods
, "displayName",
3253 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3254 &mods
, "description",
3256 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3258 talloc_asprintf(mem_ctx
, "%u",
3259 (unsigned int)map
->gid
));
3260 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3262 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3264 result
= (rc
== LDAP_SUCCESS
) ?
3265 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3268 TALLOC_FREE(mem_ctx
);
3272 /**********************************************************************
3273 * Update a group mapping entry. We're quite strict about what can be changed:
3274 * Only the description and displayname may be changed. It simply does not
3275 * make any sense to change the SID, gid or the type in a mapping.
3276 *********************************************************************/
3278 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3281 struct ldapsam_privates
*ldap_state
=
3282 (struct ldapsam_privates
*)methods
->private_data
;
3284 const char *filter
, *dn
;
3285 LDAPMessage
*msg
= NULL
;
3286 LDAPMessage
*entry
= NULL
;
3287 LDAPMod
**mods
= NULL
;
3288 TALLOC_CTX
*mem_ctx
;
3290 struct dom_sid_buf buf
;
3292 mem_ctx
= talloc_new(NULL
);
3293 if (mem_ctx
== NULL
) {
3294 DEBUG(0, ("talloc_new failed\n"));
3295 return NT_STATUS_NO_MEMORY
;
3298 /* Make 100% sure that sid, gid and type are not changed by looking up
3299 * exactly the values we're given in LDAP. */
3301 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3302 "(sambaSid=%s)(gidNumber=%u)"
3303 "(sambaGroupType=%d))",
3305 dom_sid_str_buf(&map
->sid
, &buf
),
3306 (unsigned int)map
->gid
, map
->sid_name_use
);
3307 if (filter
== NULL
) {
3308 result
= NT_STATUS_NO_MEMORY
;
3312 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3313 get_attr_list(mem_ctx
, groupmap_attr_list
),
3315 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3317 if ((rc
!= LDAP_SUCCESS
) ||
3318 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3320 ((entry
= ldap_first_entry(
3321 smbldap_get_ldap(ldap_state
->smbldap_state
),
3323 result
= NT_STATUS_NO_SUCH_GROUP
;
3327 dn
= smbldap_talloc_dn(
3328 mem_ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
), entry
);
3331 result
= NT_STATUS_NO_MEMORY
;
3336 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3337 &mods
, "displayName", map
->nt_name
);
3338 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3339 &mods
, "description", map
->comment
);
3340 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3343 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3344 "nothing to do\n"));
3345 result
= NT_STATUS_OK
;
3349 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3351 if (rc
!= LDAP_SUCCESS
) {
3352 result
= NT_STATUS_ACCESS_DENIED
;
3356 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3357 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3359 result
= NT_STATUS_OK
;
3362 TALLOC_FREE(mem_ctx
);
3366 /**********************************************************************
3367 *********************************************************************/
3369 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3372 struct ldapsam_privates
*priv
=
3373 (struct ldapsam_privates
*)methods
->private_data
;
3374 LDAPMessage
*msg
, *entry
;
3377 TALLOC_CTX
*mem_ctx
;
3379 struct dom_sid_buf buf
;
3381 mem_ctx
= talloc_new(NULL
);
3382 if (mem_ctx
== NULL
) {
3383 DEBUG(0, ("talloc_new failed\n"));
3384 return NT_STATUS_NO_MEMORY
;
3387 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3388 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3389 dom_sid_str_buf(&sid
, &buf
));
3390 if (filter
== NULL
) {
3391 result
= NT_STATUS_NO_MEMORY
;
3394 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3395 get_attr_list(mem_ctx
, groupmap_attr_list
),
3397 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3399 if ((rc
!= LDAP_SUCCESS
) ||
3400 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3401 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3402 result
= NT_STATUS_NO_SUCH_GROUP
;
3406 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3407 get_attr_list(mem_ctx
,
3408 groupmap_attr_list_to_delete
));
3410 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3411 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3412 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3413 const char *attrs
[] = { "sambaGroupType", "description",
3414 "displayName", "sambaSIDList",
3417 /* Second try. Don't delete the sambaSID attribute, this is
3418 for "old" entries that are tacked on a winbind
3421 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3422 LDAP_OBJ_GROUPMAP
, attrs
);
3425 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3426 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3427 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3428 const char *attrs
[] = { "sambaGroupType", "description",
3429 "displayName", "sambaSIDList",
3430 "gidNumber", NULL
};
3432 /* Third try. This is a post-3.0.21 alias (containing only
3433 * sambaSidEntry and sambaGroupMapping classes), we also have
3434 * to delete the gidNumber attribute, only the sambaSidEntry
3437 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3438 LDAP_OBJ_GROUPMAP
, attrs
);
3441 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3444 TALLOC_FREE(mem_ctx
);
3448 /**********************************************************************
3449 *********************************************************************/
3451 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3454 struct ldapsam_privates
*ldap_state
=
3455 (struct ldapsam_privates
*)my_methods
->private_data
;
3456 char *filter
= NULL
;
3458 const char **attr_list
;
3460 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3462 return NT_STATUS_NO_MEMORY
;
3464 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3465 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3466 LDAP_SCOPE_SUBTREE
, filter
,
3467 attr_list
, 0, &ldap_state
->result
);
3468 TALLOC_FREE(attr_list
);
3470 if (rc
!= LDAP_SUCCESS
) {
3471 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3472 ldap_err2string(rc
)));
3473 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3474 lp_ldap_suffix(), filter
));
3475 ldap_msgfree(ldap_state
->result
);
3476 ldap_state
->result
= NULL
;
3477 TALLOC_FREE(filter
);
3478 return NT_STATUS_UNSUCCESSFUL
;
3481 TALLOC_FREE(filter
);
3483 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3485 smbldap_get_ldap(ldap_state
->smbldap_state
),
3486 ldap_state
->result
)));
3489 ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3490 ldap_state
->result
);
3491 ldap_state
->index
= 0;
3493 return NT_STATUS_OK
;
3496 /**********************************************************************
3497 *********************************************************************/
3499 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3501 ldapsam_endsampwent(my_methods
);
3504 /**********************************************************************
3505 *********************************************************************/
3507 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3510 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3511 struct ldapsam_privates
*ldap_state
=
3512 (struct ldapsam_privates
*)my_methods
->private_data
;
3516 if (!ldap_state
->entry
)
3519 ldap_state
->index
++;
3520 bret
= init_group_from_ldap(ldap_state
, map
,
3523 ldap_state
->entry
= ldap_next_entry(
3524 smbldap_get_ldap(ldap_state
->smbldap_state
),
3528 return NT_STATUS_OK
;
3531 /**********************************************************************
3532 *********************************************************************/
3534 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3535 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3536 GROUP_MAP
***pp_rmap
,
3537 size_t *p_num_entries
,
3540 GROUP_MAP
*map
= NULL
;
3546 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3547 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3549 return NT_STATUS_ACCESS_DENIED
;
3554 map
= talloc_zero(NULL
, GROUP_MAP
);
3556 return NT_STATUS_NO_MEMORY
;
3559 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3564 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3565 sid_name_use
!= map
->sid_name_use
) {
3566 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3567 "not of the requested type\n",
3571 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3572 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3573 "non mapped\n", map
->nt_name
));
3577 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3578 GROUP_MAP
*, entries
+ 1);
3580 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3581 "enlarge group map!\n"));
3582 return NT_STATUS_UNSUCCESSFUL
;
3585 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3590 ldapsam_endsamgrent(methods
);
3592 *p_num_entries
= entries
;
3594 return NT_STATUS_OK
;
3597 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3598 const struct dom_sid
*alias
,
3599 const struct dom_sid
*member
,
3602 struct ldapsam_privates
*ldap_state
=
3603 (struct ldapsam_privates
*)methods
->private_data
;
3605 LDAPMessage
*result
= NULL
;
3606 LDAPMessage
*entry
= NULL
;
3608 LDAPMod
**mods
= NULL
;
3610 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3611 struct dom_sid_buf tmp
;
3613 char *filter
= NULL
;
3615 if (sid_check_is_in_builtin(alias
)) {
3616 type
= SID_NAME_ALIAS
;
3619 if (sid_check_is_in_our_sam(alias
)) {
3620 type
= SID_NAME_ALIAS
;
3623 if (type
== SID_NAME_USE_NONE
) {
3624 struct dom_sid_buf buf
;
3625 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3626 dom_sid_str_buf(alias
, &buf
)));
3627 return NT_STATUS_NO_SUCH_ALIAS
;
3630 if (asprintf(&filter
,
3631 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3633 dom_sid_str_buf(alias
, &tmp
),
3635 return NT_STATUS_NO_MEMORY
;
3638 if (ldapsam_search_one_group(ldap_state
, filter
,
3639 &result
) != LDAP_SUCCESS
) {
3641 return NT_STATUS_NO_SUCH_ALIAS
;
3644 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3648 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3649 ldap_msgfree(result
);
3651 return NT_STATUS_NO_SUCH_ALIAS
;
3655 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3656 "filter %s: count=%d\n", filter
, count
));
3657 ldap_msgfree(result
);
3659 return NT_STATUS_NO_SUCH_ALIAS
;
3664 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3668 ldap_msgfree(result
);
3669 return NT_STATUS_UNSUCCESSFUL
;
3672 dn
= smbldap_talloc_dn(talloc_tos(),
3673 smbldap_get_ldap(ldap_state
->smbldap_state
),
3676 ldap_msgfree(result
);
3677 return NT_STATUS_UNSUCCESSFUL
;
3680 smbldap_set_mod(&mods
, modop
,
3681 get_attr_key2string(groupmap_attr_list
,
3682 LDAP_ATTR_SID_LIST
),
3683 dom_sid_str_buf(member
, &tmp
));
3685 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3687 ldap_mods_free(mods
, True
);
3688 ldap_msgfree(result
);
3691 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3692 return NT_STATUS_MEMBER_IN_ALIAS
;
3695 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3696 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3699 if (rc
!= LDAP_SUCCESS
) {
3700 return NT_STATUS_UNSUCCESSFUL
;
3703 return NT_STATUS_OK
;
3706 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3707 const struct dom_sid
*alias
,
3708 const struct dom_sid
*member
)
3710 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3713 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3714 const struct dom_sid
*alias
,
3715 const struct dom_sid
*member
)
3717 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3721 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3722 const struct dom_sid
*alias
,
3723 TALLOC_CTX
*mem_ctx
,
3724 struct dom_sid
**pp_members
,
3725 size_t *p_num_members
)
3727 struct ldapsam_privates
*ldap_state
=
3728 (struct ldapsam_privates
*)methods
->private_data
;
3729 LDAPMessage
*result
= NULL
;
3730 LDAPMessage
*entry
= NULL
;
3732 char **values
= NULL
;
3734 char *filter
= NULL
;
3735 uint32_t num_members
= 0;
3736 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3737 struct dom_sid_buf tmp
;
3742 if (sid_check_is_in_builtin(alias
)) {
3743 type
= SID_NAME_ALIAS
;
3746 if (sid_check_is_in_our_sam(alias
)) {
3747 type
= SID_NAME_ALIAS
;
3750 if (type
== SID_NAME_USE_NONE
) {
3751 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3752 dom_sid_str_buf(alias
, &tmp
)));
3753 return NT_STATUS_NO_SUCH_ALIAS
;
3756 if (asprintf(&filter
,
3757 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3759 dom_sid_str_buf(alias
, &tmp
),
3761 return NT_STATUS_NO_MEMORY
;
3764 if (ldapsam_search_one_group(ldap_state
, filter
,
3765 &result
) != LDAP_SUCCESS
) {
3767 return NT_STATUS_NO_SUCH_ALIAS
;
3770 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3774 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3775 ldap_msgfree(result
);
3777 return NT_STATUS_NO_SUCH_ALIAS
;
3781 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3782 "filter %s: count=%d\n", filter
, count
));
3783 ldap_msgfree(result
);
3785 return NT_STATUS_NO_SUCH_ALIAS
;
3790 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3794 ldap_msgfree(result
);
3795 return NT_STATUS_UNSUCCESSFUL
;
3798 values
= ldap_get_values(smbldap_get_ldap(ldap_state
->smbldap_state
),
3800 get_attr_key2string(groupmap_attr_list
,
3801 LDAP_ATTR_SID_LIST
));
3803 if (values
== NULL
) {
3804 ldap_msgfree(result
);
3805 return NT_STATUS_OK
;
3808 count
= ldap_count_values(values
);
3810 for (i
=0; i
<count
; i
++) {
3811 struct dom_sid member
;
3814 if (!string_to_sid(&member
, values
[i
]))
3817 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3819 if (!NT_STATUS_IS_OK(status
)) {
3820 ldap_value_free(values
);
3821 ldap_msgfree(result
);
3826 *p_num_members
= num_members
;
3827 ldap_value_free(values
);
3828 ldap_msgfree(result
);
3830 return NT_STATUS_OK
;
3833 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3834 TALLOC_CTX
*mem_ctx
,
3835 const struct dom_sid
*domain_sid
,
3836 const struct dom_sid
*members
,
3838 uint32_t **pp_alias_rids
,
3839 size_t *p_num_alias_rids
)
3841 struct ldapsam_privates
*ldap_state
=
3842 (struct ldapsam_privates
*)methods
->private_data
;
3845 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3847 LDAPMessage
*result
= NULL
;
3848 LDAPMessage
*entry
= NULL
;
3852 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3853 bool is_builtin
= false;
3854 bool sid_added
= false;
3856 *pp_alias_rids
= NULL
;
3857 *p_num_alias_rids
= 0;
3859 if (sid_check_is_builtin(domain_sid
)) {
3861 type
= SID_NAME_ALIAS
;
3864 if (sid_check_is_our_sam(domain_sid
)) {
3865 type
= SID_NAME_ALIAS
;
3868 if (type
== SID_NAME_USE_NONE
) {
3869 struct dom_sid_buf buf
;
3870 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3871 dom_sid_str_buf(domain_sid
, &buf
)));
3872 return NT_STATUS_UNSUCCESSFUL
;
3875 if (num_members
== 0) {
3876 return NT_STATUS_OK
;
3879 filter
= talloc_asprintf(mem_ctx
,
3880 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3881 LDAP_OBJ_GROUPMAP
, type
);
3883 for (i
=0; i
<num_members
; i
++) {
3884 struct dom_sid_buf buf
;
3885 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3887 dom_sid_str_buf(&members
[i
], &buf
));
3890 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3892 if (filter
== NULL
) {
3893 return NT_STATUS_NO_MEMORY
;
3897 ldap_state
->search_cache
.filter
&&
3898 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3899 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3900 result
= ldap_state
->search_cache
.result
;
3901 ldap_state
->search_cache
.result
= NULL
;
3903 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3904 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3905 if (rc
!= LDAP_SUCCESS
) {
3906 return NT_STATUS_UNSUCCESSFUL
;
3908 smbldap_talloc_autofree_ldapmsg(filter
, result
);
3911 ldap_struct
= smbldap_get_ldap(ldap_state
->smbldap_state
);
3913 for (entry
= ldap_first_entry(ldap_struct
, result
);
3915 entry
= ldap_next_entry(ldap_struct
, entry
))
3921 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3927 if (!string_to_sid(&sid
, sid_str
))
3930 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3935 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3936 p_num_alias_rids
)) {
3937 return NT_STATUS_NO_MEMORY
;
3941 if (!is_builtin
&& !sid_added
) {
3942 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3944 * Note: result is a talloc child of filter because of the
3945 * smbldap_talloc_autofree_ldapmsg() usage
3947 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3948 ldap_state
->search_cache
.result
= result
;
3951 return NT_STATUS_OK
;
3954 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3955 enum pdb_policy_type type
,
3958 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3960 LDAPMod
**mods
= NULL
;
3961 fstring value_string
;
3962 const char *policy_attr
= NULL
;
3964 struct ldapsam_privates
*ldap_state
=
3965 (struct ldapsam_privates
*)methods
->private_data
;
3967 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3969 if (!ldap_state
->domain_dn
) {
3970 return NT_STATUS_INVALID_PARAMETER
;
3973 policy_attr
= get_account_policy_attr(type
);
3974 if (policy_attr
== NULL
) {
3975 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3980 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3982 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3984 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3987 ldap_mods_free(mods
, True
);
3989 if (rc
!= LDAP_SUCCESS
) {
3993 if (!cache_account_policy_set(type
, value
)) {
3994 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3995 "update local tdb cache\n"));
3999 return NT_STATUS_OK
;
4002 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
4003 enum pdb_policy_type type
,
4006 return ldapsam_set_account_policy_in_ldap(methods
, type
,
4010 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
4011 enum pdb_policy_type type
,
4014 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4015 LDAPMessage
*result
= NULL
;
4016 LDAPMessage
*entry
= NULL
;
4021 const char *policy_attr
= NULL
;
4023 struct ldapsam_privates
*ldap_state
=
4024 (struct ldapsam_privates
*)methods
->private_data
;
4026 const char *attrs
[2];
4028 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4030 if (!ldap_state
->domain_dn
) {
4031 return NT_STATUS_INVALID_PARAMETER
;
4034 policy_attr
= get_account_policy_attr(type
);
4036 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4037 "policy index: %d\n", type
));
4041 attrs
[0] = policy_attr
;
4044 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
4045 if (filter
== NULL
) {
4046 return NT_STATUS_NO_MEMORY
;
4048 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
4049 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
4051 TALLOC_FREE(filter
);
4052 if (rc
!= LDAP_SUCCESS
) {
4056 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4061 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4062 if (entry
== NULL
) {
4066 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4071 *value
= (uint32_t)atol(vals
[0]);
4073 ntstatus
= NT_STATUS_OK
;
4077 ldap_value_free(vals
);
4078 ldap_msgfree(result
);
4083 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4085 - if user hasn't decided to use account policies inside LDAP just reuse the
4088 - if there is a valid cache entry, return that
4089 - if there is an LDAP entry, update cache and return
4090 - otherwise set to default, update cache and return
4094 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4095 enum pdb_policy_type type
,
4100 if (cache_account_policy_get(type
, value
)) {
4101 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4103 return NT_STATUS_OK
;
4106 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4108 if (NT_STATUS_IS_OK(ntstatus
)) {
4112 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4116 /* should we automagically migrate old tdb value here ? */
4117 if (account_policy_get(type
, value
))
4120 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4121 "default\n", type
));
4124 if (!account_policy_get_default(type
, value
)) {
4130 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4131 if (!NT_STATUS_IS_OK(ntstatus
)) {
4137 if (!cache_account_policy_set(type
, *value
)) {
4138 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4139 "tdb as a cache\n"));
4140 return NT_STATUS_UNSUCCESSFUL
;
4143 return NT_STATUS_OK
;
4146 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4147 const struct dom_sid
*domain_sid
,
4151 enum lsa_SidType
*attrs
)
4153 struct ldapsam_privates
*ldap_state
=
4154 (struct ldapsam_privates
*)methods
->private_data
;
4155 LDAPMessage
*msg
= NULL
;
4157 char *allsids
= NULL
;
4158 size_t i
, num_mapped
;
4160 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4161 TALLOC_CTX
*mem_ctx
;
4165 mem_ctx
= talloc_new(NULL
);
4166 if (mem_ctx
== NULL
) {
4167 DEBUG(0, ("talloc_new failed\n"));
4171 if (!sid_check_is_builtin(domain_sid
) &&
4172 !sid_check_is_our_sam(domain_sid
)) {
4173 result
= NT_STATUS_INVALID_PARAMETER
;
4177 if (num_rids
== 0) {
4178 result
= NT_STATUS_NONE_MAPPED
;
4182 for (i
=0; i
<num_rids
; i
++)
4183 attrs
[i
] = SID_NAME_UNKNOWN
;
4185 allsids
= talloc_strdup(mem_ctx
, "");
4186 if (allsids
== NULL
) {
4190 for (i
=0; i
<num_rids
; i
++) {
4192 struct dom_sid_buf buf
;
4193 sid_compose(&sid
, domain_sid
, rids
[i
]);
4194 allsids
= talloc_asprintf_append_buffer(
4197 dom_sid_str_buf(&sid
, &buf
));
4198 if (allsids
== NULL
) {
4203 /* First look for users */
4207 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4209 filter
= talloc_asprintf(
4210 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4211 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4213 if (filter
== NULL
) {
4217 rc
= smbldap_search(ldap_state
->smbldap_state
,
4218 lp_ldap_user_suffix(talloc_tos()),
4219 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4221 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4224 if (rc
!= LDAP_SUCCESS
)
4227 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4230 for (entry
= ldap_first_entry(ld
, msg
);
4232 entry
= ldap_next_entry(ld
, entry
)) {
4237 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4239 DEBUG(2, ("Could not find sid from ldap entry\n"));
4243 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4246 DEBUG(2, ("Could not retrieve uid attribute\n"));
4250 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4251 if (rid
== rids
[rid_index
])
4255 if (rid_index
== num_rids
) {
4256 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4260 attrs
[rid_index
] = SID_NAME_USER
;
4261 names
[rid_index
] = name
;
4265 if (num_mapped
== num_rids
) {
4266 /* No need to look for groups anymore -- we're done */
4267 result
= NT_STATUS_OK
;
4271 /* Same game for groups */
4275 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4276 "sambaGroupType", NULL
};
4278 filter
= talloc_asprintf(
4279 mem_ctx
, "(&(objectClass=%s)(|%s))",
4280 LDAP_OBJ_GROUPMAP
, allsids
);
4281 if (filter
== NULL
) {
4285 rc
= smbldap_search(ldap_state
->smbldap_state
,
4287 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4289 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4292 if (rc
!= LDAP_SUCCESS
)
4295 /* ldap_struct might have changed due to a reconnect */
4297 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4299 /* For consistency checks, we already checked we're only domain or builtin */
4301 is_builtin
= sid_check_is_builtin(domain_sid
);
4303 for (entry
= ldap_first_entry(ld
, msg
);
4305 entry
= ldap_next_entry(ld
, entry
))
4310 enum lsa_SidType type
;
4311 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4313 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4316 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4321 type
= (enum lsa_SidType
)atol(attr
);
4323 /* Consistency checks */
4324 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4325 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4326 (type
!= SID_NAME_DOM_GRP
)))) {
4327 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4330 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4332 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4336 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4339 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4341 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4345 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4350 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4351 if (rid
== rids
[rid_index
])
4355 if (rid_index
== num_rids
) {
4356 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4360 attrs
[rid_index
] = type
;
4361 names
[rid_index
] = attr
;
4365 result
= NT_STATUS_NONE_MAPPED
;
4368 result
= (num_mapped
== num_rids
) ?
4369 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4371 TALLOC_FREE(mem_ctx
);
4375 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4377 char *filter
= NULL
;
4378 char *escaped
= NULL
;
4379 char *result
= NULL
;
4381 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4382 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4386 escaped
= escape_ldap_string(talloc_tos(), username
);
4387 if (escaped
== NULL
) goto done
;
4389 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4393 TALLOC_FREE(escaped
);
4398 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4402 const char **result
;
4404 va_start(ap
, mem_ctx
);
4405 while (va_arg(ap
, const char *) != NULL
)
4409 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4413 va_start(ap
, mem_ctx
);
4414 for (i
=0; i
<num
; i
++) {
4415 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4416 if (result
[i
] == NULL
) {
4417 talloc_free(result
);
4428 struct ldap_search_state
{
4429 struct smbldap_state
*connection
;
4431 uint32_t acct_flags
;
4432 uint16_t group_type
;
4439 void *pagedresults_cookie
;
4441 LDAPMessage
*entries
, *current_entry
;
4442 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4443 TALLOC_CTX
*mem_ctx
,
4444 LDAP
*ld
, LDAPMessage
*entry
,
4445 struct samr_displayentry
*result
);
4448 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4450 struct ldap_search_state
*state
=
4451 (struct ldap_search_state
*)search
->private_data
;
4453 int rc
= LDAP_OPERATIONS_ERROR
;
4455 state
->entries
= NULL
;
4457 if (smbldap_get_paged_results(state
->connection
)) {
4458 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4459 state
->scope
, state
->filter
,
4460 state
->attrs
, state
->attrsonly
,
4461 lp_ldap_page_size(), &state
->entries
,
4462 &state
->pagedresults_cookie
);
4465 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4467 if (state
->entries
!= NULL
) {
4468 /* Left over from unsuccessful paged attempt */
4469 ldap_msgfree(state
->entries
);
4470 state
->entries
= NULL
;
4473 rc
= smbldap_search(state
->connection
, state
->base
,
4474 state
->scope
, state
->filter
, state
->attrs
,
4475 state
->attrsonly
, &state
->entries
);
4477 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4480 /* Ok, the server was lying. It told us it could do paged
4481 * searches when it could not. */
4482 smbldap_set_paged_results(state
->connection
, false);
4485 ld
= smbldap_get_ldap(state
->connection
);
4487 DEBUG(5, ("Don't have an LDAP connection right after a "
4491 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4496 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4498 struct ldap_search_state
*state
=
4499 (struct ldap_search_state
*)search
->private_data
;
4502 if (!smbldap_get_paged_results(state
->connection
)) {
4503 /* There is no next page when there are no paged results */
4507 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4508 state
->scope
, state
->filter
, state
->attrs
,
4509 state
->attrsonly
, lp_ldap_page_size(),
4511 &state
->pagedresults_cookie
);
4513 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4516 state
->current_entry
= ldap_first_entry(
4517 smbldap_get_ldap(state
->connection
), state
->entries
);
4519 if (state
->current_entry
== NULL
) {
4520 ldap_msgfree(state
->entries
);
4521 state
->entries
= NULL
;
4528 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4529 struct samr_displayentry
*entry
)
4531 struct ldap_search_state
*state
=
4532 (struct ldap_search_state
*)search
->private_data
;
4536 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4539 if ((state
->entries
== NULL
) &&
4540 !ldapsam_search_nextpage(search
))
4543 if (state
->current_entry
== NULL
) {
4547 result
= state
->ldap2displayentry(state
, search
,
4548 smbldap_get_ldap(state
->connection
),
4549 state
->current_entry
, entry
);
4553 dn
= ldap_get_dn(smbldap_get_ldap(state
->connection
),
4554 state
->current_entry
);
4555 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4556 if (dn
!= NULL
) ldap_memfree(dn
);
4559 state
->current_entry
= ldap_next_entry(
4560 smbldap_get_ldap(state
->connection
), state
->current_entry
);
4562 if (state
->current_entry
== NULL
) {
4563 ldap_msgfree(state
->entries
);
4564 state
->entries
= NULL
;
4567 if (!result
) goto retry
;
4572 static void ldapsam_search_end(struct pdb_search
*search
)
4574 struct ldap_search_state
*state
=
4575 (struct ldap_search_state
*)search
->private_data
;
4578 if (state
->pagedresults_cookie
== NULL
)
4581 if (state
->entries
!= NULL
)
4582 ldap_msgfree(state
->entries
);
4584 state
->entries
= NULL
;
4585 state
->current_entry
= NULL
;
4587 if (!smbldap_get_paged_results(state
->connection
)) {
4591 /* Tell the LDAP server we're not interested in the rest anymore. */
4593 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4594 state
->filter
, state
->attrs
,
4595 state
->attrsonly
, 0, &state
->entries
,
4596 &state
->pagedresults_cookie
);
4598 if (rc
!= LDAP_SUCCESS
)
4599 DEBUG(5, ("Could not end search properly\n"));
4604 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4605 TALLOC_CTX
*mem_ctx
,
4606 LDAP
*ld
, LDAPMessage
*entry
,
4607 struct samr_displayentry
*result
)
4610 size_t converted_size
;
4612 uint32_t acct_flags
;
4614 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4615 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4616 acct_flags
= ACB_NORMAL
;
4618 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4619 ldap_value_free(vals
);
4622 if ((state
->acct_flags
!= 0) &&
4623 ((state
->acct_flags
& acct_flags
) == 0))
4626 result
->acct_flags
= acct_flags
;
4627 result
->account_name
= "";
4628 result
->fullname
= "";
4629 result
->description
= "";
4631 vals
= ldap_get_values(ld
, entry
, "uid");
4632 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4633 DEBUG(5, ("\"uid\" not found\n"));
4636 if (!pull_utf8_talloc(mem_ctx
,
4637 discard_const_p(char *, &result
->account_name
),
4638 vals
[0], &converted_size
))
4640 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4644 ldap_value_free(vals
);
4646 vals
= ldap_get_values(ld
, entry
, "displayName");
4647 if ((vals
== NULL
) || (vals
[0] == NULL
))
4648 DEBUG(8, ("\"displayName\" not found\n"));
4649 else if (!pull_utf8_talloc(mem_ctx
,
4650 discard_const_p(char *, &result
->fullname
),
4651 vals
[0], &converted_size
))
4653 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4657 ldap_value_free(vals
);
4659 vals
= ldap_get_values(ld
, entry
, "description");
4660 if ((vals
== NULL
) || (vals
[0] == NULL
))
4661 DEBUG(8, ("\"description\" not found\n"));
4662 else if (!pull_utf8_talloc(mem_ctx
,
4663 discard_const_p(char *, &result
->description
),
4664 vals
[0], &converted_size
))
4666 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4670 ldap_value_free(vals
);
4672 if ((result
->account_name
== NULL
) ||
4673 (result
->fullname
== NULL
) ||
4674 (result
->description
== NULL
)) {
4675 DEBUG(0, ("talloc failed\n"));
4679 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4680 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4681 DEBUG(0, ("\"objectSid\" not found\n"));
4685 if (!string_to_sid(&sid
, vals
[0])) {
4686 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4687 ldap_value_free(vals
);
4690 ldap_value_free(vals
);
4692 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4693 struct dom_sid_buf buf
;
4694 DEBUG(0, ("sid %s does not belong to our domain\n",
4695 dom_sid_str_buf(&sid
, &buf
)));
4703 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4704 struct pdb_search
*search
,
4705 uint32_t acct_flags
)
4707 struct ldapsam_privates
*ldap_state
=
4708 (struct ldapsam_privates
*)methods
->private_data
;
4709 struct ldap_search_state
*state
;
4711 state
= talloc(search
, struct ldap_search_state
);
4712 if (state
== NULL
) {
4713 DEBUG(0, ("talloc failed\n"));
4717 state
->connection
= ldap_state
->smbldap_state
;
4719 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4720 state
->base
= lp_ldap_user_suffix(talloc_tos());
4721 else if ((acct_flags
!= 0) &&
4722 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4723 state
->base
= lp_ldap_machine_suffix(talloc_tos());
4725 state
->base
= lp_ldap_suffix();
4727 state
->acct_flags
= acct_flags
;
4728 state
->base
= talloc_strdup(search
, state
->base
);
4729 state
->scope
= LDAP_SCOPE_SUBTREE
;
4730 state
->filter
= get_ldap_filter(search
, "*");
4731 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4732 "displayName", "description",
4733 "sambaAcctFlags", NULL
);
4734 state
->attrsonly
= 0;
4735 state
->pagedresults_cookie
= NULL
;
4736 state
->entries
= NULL
;
4737 state
->ldap2displayentry
= ldapuser2displayentry
;
4739 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4740 DEBUG(0, ("talloc failed\n"));
4744 search
->private_data
= state
;
4745 search
->next_entry
= ldapsam_search_next_entry
;
4746 search
->search_end
= ldapsam_search_end
;
4748 return ldapsam_search_firstpage(search
);
4751 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4752 TALLOC_CTX
*mem_ctx
,
4753 LDAP
*ld
, LDAPMessage
*entry
,
4754 struct samr_displayentry
*result
)
4757 size_t converted_size
;
4759 uint16_t group_type
;
4761 result
->account_name
= "";
4762 result
->fullname
= "";
4763 result
->description
= "";
4766 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4767 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4768 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4770 ldap_value_free(vals
);
4775 group_type
= atoi(vals
[0]);
4777 if ((state
->group_type
!= 0) &&
4778 ((state
->group_type
!= group_type
))) {
4779 ldap_value_free(vals
);
4783 ldap_value_free(vals
);
4785 /* display name is the NT group name */
4787 vals
= ldap_get_values(ld
, entry
, "displayName");
4788 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4789 DEBUG(8, ("\"displayName\" not found\n"));
4791 /* fallback to the 'cn' attribute */
4792 vals
= ldap_get_values(ld
, entry
, "cn");
4793 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4794 DEBUG(5, ("\"cn\" not found\n"));
4797 if (!pull_utf8_talloc(mem_ctx
,
4798 discard_const_p(char *,
4799 &result
->account_name
),
4800 vals
[0], &converted_size
))
4802 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4803 "failed: %s", strerror(errno
)));
4806 else if (!pull_utf8_talloc(mem_ctx
,
4807 discard_const_p(char *,
4808 &result
->account_name
),
4809 vals
[0], &converted_size
))
4811 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4815 ldap_value_free(vals
);
4817 vals
= ldap_get_values(ld
, entry
, "description");
4818 if ((vals
== NULL
) || (vals
[0] == NULL
))
4819 DEBUG(8, ("\"description\" not found\n"));
4820 else if (!pull_utf8_talloc(mem_ctx
,
4821 discard_const_p(char *, &result
->description
),
4822 vals
[0], &converted_size
))
4824 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4827 ldap_value_free(vals
);
4829 if ((result
->account_name
== NULL
) ||
4830 (result
->fullname
== NULL
) ||
4831 (result
->description
== NULL
)) {
4832 DEBUG(0, ("talloc failed\n"));
4836 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4837 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4838 DEBUG(0, ("\"objectSid\" not found\n"));
4840 ldap_value_free(vals
);
4845 if (!string_to_sid(&sid
, vals
[0])) {
4846 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4850 ldap_value_free(vals
);
4852 switch (group_type
) {
4853 case SID_NAME_DOM_GRP
:
4854 case SID_NAME_ALIAS
:
4856 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4857 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4859 struct dom_sid_buf buf
;
4860 DEBUG(0, ("%s is not in our domain\n",
4861 dom_sid_str_buf(&sid
, &buf
)));
4867 DEBUG(0,("unknown group type: %d\n", group_type
));
4871 result
->acct_flags
= 0;
4876 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4877 struct pdb_search
*search
,
4878 const struct dom_sid
*sid
,
4879 enum lsa_SidType type
)
4881 struct ldapsam_privates
*ldap_state
=
4882 (struct ldapsam_privates
*)methods
->private_data
;
4883 struct ldap_search_state
*state
;
4884 struct dom_sid_buf tmp
;
4886 state
= talloc(search
, struct ldap_search_state
);
4887 if (state
== NULL
) {
4888 DEBUG(0, ("talloc failed\n"));
4892 state
->connection
= ldap_state
->smbldap_state
;
4894 state
->base
= lp_ldap_suffix();
4895 state
->connection
= ldap_state
->smbldap_state
;
4896 state
->scope
= LDAP_SCOPE_SUBTREE
;
4897 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4898 "(sambaGroupType=%d)(sambaSID=%s*))",
4901 dom_sid_str_buf(sid
, &tmp
));
4902 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4903 "displayName", "description",
4904 "sambaGroupType", NULL
);
4905 state
->attrsonly
= 0;
4906 state
->pagedresults_cookie
= NULL
;
4907 state
->entries
= NULL
;
4908 state
->group_type
= type
;
4909 state
->ldap2displayentry
= ldapgroup2displayentry
;
4911 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4912 DEBUG(0, ("talloc failed\n"));
4916 search
->private_data
= state
;
4917 search
->next_entry
= ldapsam_search_next_entry
;
4918 search
->search_end
= ldapsam_search_end
;
4920 return ldapsam_search_firstpage(search
);
4923 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4924 struct pdb_search
*search
)
4926 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4929 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4930 struct pdb_search
*search
,
4931 const struct dom_sid
*sid
)
4933 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4936 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4938 return PDB_CAP_STORE_RIDS
;
4941 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4944 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4946 LDAPMessage
*result
= NULL
;
4947 LDAPMessage
*entry
= NULL
;
4948 LDAPMod
**mods
= NULL
;
4952 uint32_t nextRid
= 0;
4957 TALLOC_CTX
*mem_ctx
;
4959 mem_ctx
= talloc_new(NULL
);
4960 if (mem_ctx
== NULL
) {
4961 DEBUG(0, ("talloc_new failed\n"));
4962 return NT_STATUS_NO_MEMORY
;
4965 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4966 get_global_sam_name(), False
);
4967 if (!NT_STATUS_IS_OK(status
)) {
4968 DEBUG(3, ("Could not get domain info: %s\n",
4969 nt_errstr(status
)));
4973 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4975 entry
= ldap_first_entry(priv2ld(priv
), result
);
4976 if (entry
== NULL
) {
4977 DEBUG(0, ("Could not get domain info entry\n"));
4978 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4982 /* Find the largest of the three attributes "sambaNextRid",
4983 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4984 concept of differentiating between user and group rids, and will
4985 use only "sambaNextRid" in the future. But for compatibility
4986 reasons I look if others have chosen different strategies -- VL */
4988 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4989 "sambaNextRid", mem_ctx
);
4990 if (value
!= NULL
) {
4991 tmp
= (uint32_t)smb_strtoul(value
,
5000 nextRid
= MAX(nextRid
, tmp
);
5003 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5004 "sambaNextUserRid", mem_ctx
);
5005 if (value
!= NULL
) {
5006 tmp
= (uint32_t)smb_strtoul(value
,
5015 nextRid
= MAX(nextRid
, tmp
);
5018 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5019 "sambaNextGroupRid", mem_ctx
);
5020 if (value
!= NULL
) {
5021 tmp
= (uint32_t)smb_strtoul(value
,
5030 nextRid
= MAX(nextRid
, tmp
);
5034 nextRid
= BASE_RID
-1;
5039 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
5040 talloc_asprintf(mem_ctx
, "%d", nextRid
));
5041 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
5043 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
5044 status
= NT_STATUS_NO_MEMORY
;
5048 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
5050 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5053 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
5056 if (NT_STATUS_IS_OK(status
)) {
5060 TALLOC_FREE(mem_ctx
);
5064 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
5068 for (i
=0; i
<10; i
++) {
5069 NTSTATUS result
= ldapsam_get_new_rid(
5070 (struct ldapsam_privates
*)methods
->private_data
, rid
);
5071 if (NT_STATUS_IS_OK(result
)) {
5075 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
5079 /* The ldap update failed (maybe a race condition), retry */
5082 /* Tried 10 times, fail. */
5083 return NT_STATUS_ACCESS_DENIED
;
5086 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
5088 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
5089 return NT_STATUS_IS_OK(result
) ? True
: False
;
5092 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
5093 const struct dom_sid
*sid
,
5096 struct ldapsam_privates
*priv
=
5097 (struct ldapsam_privates
*)methods
->private_data
;
5100 struct dom_sid_buf buf
;
5101 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5103 LDAPMessage
*result
= NULL
;
5104 LDAPMessage
*entry
= NULL
;
5109 TALLOC_CTX
*mem_ctx
;
5111 ret
= pdb_sid_to_id_unix_users_and_groups(sid
, id
);
5116 mem_ctx
= talloc_new(NULL
);
5117 if (mem_ctx
== NULL
) {
5118 DEBUG(0, ("talloc_new failed\n"));
5122 filter
= talloc_asprintf(mem_ctx
,
5124 "(|(objectClass=%s)(objectClass=%s)))",
5125 dom_sid_str_buf(sid
, &buf
),
5126 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5127 if (filter
== NULL
) {
5128 DEBUG(5, ("talloc_asprintf failed\n"));
5132 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5134 if (rc
!= LDAP_SUCCESS
) {
5137 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
5139 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5140 DEBUG(10, ("Got %d entries, expected one\n",
5141 ldap_count_entries(priv2ld(priv
), result
)));
5145 entry
= ldap_first_entry(priv2ld(priv
), result
);
5147 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5148 "sambaGroupType", mem_ctx
);
5150 if (value
!= NULL
) {
5151 const char *gid_str
;
5154 gid_str
= smbldap_talloc_single_attribute(
5155 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5156 if (gid_str
== NULL
) {
5157 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5158 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5163 id
->id
= smb_strtoul(gid_str
,
5172 id
->type
= ID_TYPE_GID
;
5177 /* It must be a user */
5179 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5180 "uidNumber", mem_ctx
);
5181 if (value
== NULL
) {
5182 DEBUG(1, ("Could not find uidNumber in %s\n",
5183 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5187 id
->id
= smb_strtoul(value
, NULL
, 10, &error
, SMB_STR_STANDARD
);
5192 id
->type
= ID_TYPE_UID
;
5195 TALLOC_FREE(mem_ctx
);
5200 * Find the SID for a uid.
5201 * This is shortcut is only used if ldapsam:trusted is set to true.
5203 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5204 struct dom_sid
*sid
)
5206 struct ldapsam_privates
*priv
=
5207 (struct ldapsam_privates
*)methods
->private_data
;
5209 const char *attrs
[] = { "sambaSID", NULL
};
5210 LDAPMessage
*result
= NULL
;
5211 LDAPMessage
*entry
= NULL
;
5213 char *user_sid_string
;
5214 struct dom_sid user_sid
;
5216 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5218 filter
= talloc_asprintf(tmp_ctx
,
5221 "(objectClass=%s))",
5223 LDAP_OBJ_POSIXACCOUNT
,
5224 LDAP_OBJ_SAMBASAMACCOUNT
);
5225 if (filter
== NULL
) {
5226 DEBUG(3, ("talloc_asprintf failed\n"));
5230 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5231 if (rc
!= LDAP_SUCCESS
) {
5234 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5236 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5237 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5238 ldap_count_entries(priv2ld(priv
), result
),
5239 (unsigned int)uid
));
5243 entry
= ldap_first_entry(priv2ld(priv
), result
);
5245 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5246 "sambaSID", tmp_ctx
);
5247 if (user_sid_string
== NULL
) {
5248 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5249 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5253 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5254 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5259 sid_copy(sid
, &user_sid
);
5264 TALLOC_FREE(tmp_ctx
);
5269 * Find the SID for a gid.
5270 * This is shortcut is only used if ldapsam:trusted is set to true.
5272 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5273 struct dom_sid
*sid
)
5275 struct ldapsam_privates
*priv
=
5276 (struct ldapsam_privates
*)methods
->private_data
;
5278 const char *attrs
[] = { "sambaSID", NULL
};
5279 LDAPMessage
*result
= NULL
;
5280 LDAPMessage
*entry
= NULL
;
5282 char *group_sid_string
;
5283 struct dom_sid group_sid
;
5285 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5287 filter
= talloc_asprintf(tmp_ctx
,
5289 "(objectClass=%s))",
5292 if (filter
== NULL
) {
5293 DEBUG(3, ("talloc_asprintf failed\n"));
5297 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5298 if (rc
!= LDAP_SUCCESS
) {
5301 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5303 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5304 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5305 ldap_count_entries(priv2ld(priv
), result
),
5306 (unsigned int)gid
));
5310 entry
= ldap_first_entry(priv2ld(priv
), result
);
5312 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5313 "sambaSID", tmp_ctx
);
5314 if (group_sid_string
== NULL
) {
5315 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5316 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5320 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5321 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5326 sid_copy(sid
, &group_sid
);
5331 TALLOC_FREE(tmp_ctx
);
5335 static bool ldapsam_id_to_sid(struct pdb_methods
*methods
, struct unixid
*id
,
5336 struct dom_sid
*sid
)
5340 return ldapsam_uid_to_sid(methods
, id
->id
, sid
);
5343 return ldapsam_gid_to_sid(methods
, id
->id
, sid
);
5352 * The following functions are called only if
5353 * ldapsam:trusted and ldapsam:editposix are
5358 * ldapsam_create_user creates a new
5359 * posixAccount and sambaSamAccount object
5360 * in the ldap users subtree
5362 * The uid is allocated by winbindd.
5365 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5366 TALLOC_CTX
*tmp_ctx
, const char *name
,
5367 uint32_t acb_info
, uint32_t *rid
)
5369 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5370 LDAPMessage
*entry
= NULL
;
5371 LDAPMessage
*result
= NULL
;
5372 uint32_t num_result
;
5373 bool is_machine
= False
;
5374 bool add_posix
= False
;
5375 bool init_okay
= False
;
5376 LDAPMod
**mods
= NULL
;
5384 const char *dn
= NULL
;
5385 struct dom_sid group_sid
;
5386 struct dom_sid user_sid
;
5392 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5393 acb_info
& ACB_WSTRUST
||
5394 acb_info
& ACB_SVRTRUST
||
5395 acb_info
& ACB_DOMTRUST
) {
5399 username
= escape_ldap_string(talloc_tos(), name
);
5400 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5401 username
, LDAP_OBJ_POSIXACCOUNT
);
5402 TALLOC_FREE(username
);
5404 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5405 if (rc
!= LDAP_SUCCESS
) {
5406 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5407 return NT_STATUS_ACCESS_DENIED
;
5409 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5411 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5413 if (num_result
> 1) {
5414 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5415 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5418 if (num_result
== 1) {
5420 /* check if it is just a posix account.
5421 * or if there is a sid attached to this entry
5424 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5426 return NT_STATUS_UNSUCCESSFUL
;
5429 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5431 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5432 return NT_STATUS_USER_EXISTS
;
5435 /* it is just a posix account, retrieve the dn for later use */
5436 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5438 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5439 return NT_STATUS_NO_MEMORY
;
5443 if (num_result
== 0) {
5447 /* Create the basic samu structure and generate the mods for the ldap commit */
5448 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5449 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5453 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5455 user
= samu_new(tmp_ctx
);
5457 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5458 return NT_STATUS_NO_MEMORY
;
5461 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5462 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5463 return NT_STATUS_UNSUCCESSFUL
;
5465 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5466 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5467 return NT_STATUS_UNSUCCESSFUL
;
5470 if (acb_info
& ACB_NORMAL
) {
5471 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5472 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5473 return NT_STATUS_UNSUCCESSFUL
;
5476 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5477 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5478 return NT_STATUS_UNSUCCESSFUL
;
5482 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5483 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5484 return NT_STATUS_UNSUCCESSFUL
;
5488 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5489 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5490 return NT_STATUS_UNSUCCESSFUL
;
5493 init_okay
= init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
);
5496 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5497 ldap_mods_free(mods
, true);
5498 return NT_STATUS_UNSUCCESSFUL
;
5501 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5502 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5504 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5509 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5511 /* retrieve the Domain Users group gid */
5512 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5513 !sid_to_gid(&group_sid
, &gid
)) {
5514 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5515 ldap_mods_free(mods
, true);
5516 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5519 /* lets allocate a new userid for this user */
5520 if (!winbind_allocate_uid(&uid
)) {
5521 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5522 ldap_mods_free(mods
, true);
5523 return NT_STATUS_UNSUCCESSFUL
;
5528 /* TODO: choose a more appropriate default for machines */
5529 homedir
= talloc_sub_specified(tmp_ctx
,
5530 lp_template_homedir(),
5531 "SMB_workstations_home",
5533 ldap_state
->domain_name
,
5536 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5538 homedir
= talloc_sub_specified(tmp_ctx
,
5539 lp_template_homedir(),
5542 ldap_state
->domain_name
,
5545 shell
= talloc_sub_specified(tmp_ctx
,
5546 lp_template_shell(),
5549 ldap_state
->domain_name
,
5553 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5554 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5556 escape_name
= escape_rdn_val_string_alloc(name
);
5558 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5559 ldap_mods_free(mods
, true);
5560 return NT_STATUS_NO_MEMORY
;
5564 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix (talloc_tos()));
5566 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix (talloc_tos()));
5569 SAFE_FREE(escape_name
);
5571 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5572 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5573 ldap_mods_free(mods
, true);
5574 return NT_STATUS_NO_MEMORY
;
5577 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5578 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5579 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5580 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5581 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5582 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5583 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5587 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5589 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5592 ldap_mods_free(mods
, true);
5594 if (rc
!= LDAP_SUCCESS
) {
5595 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5596 return NT_STATUS_UNSUCCESSFUL
;
5599 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5601 flush_pwnam_cache();
5603 return NT_STATUS_OK
;
5606 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5608 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5609 LDAPMessage
*result
= NULL
;
5610 LDAPMessage
*entry
= NULL
;
5616 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5618 filter
= talloc_asprintf(tmp_ctx
,
5621 "(objectClass=%s))",
5622 pdb_get_username(sam_acct
),
5623 LDAP_OBJ_POSIXACCOUNT
,
5624 LDAP_OBJ_SAMBASAMACCOUNT
);
5625 if (filter
== NULL
) {
5626 return NT_STATUS_NO_MEMORY
;
5629 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5630 if (rc
!= LDAP_SUCCESS
) {
5631 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5632 return NT_STATUS_UNSUCCESSFUL
;
5634 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5636 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5638 if (num_result
== 0) {
5639 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5640 return NT_STATUS_NO_SUCH_USER
;
5643 if (num_result
> 1) {
5644 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5645 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5648 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5650 return NT_STATUS_UNSUCCESSFUL
;
5653 /* it is just a posix account, retrieve the dn for later use */
5654 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5656 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5657 return NT_STATUS_NO_MEMORY
;
5660 /* try to remove memberships first */
5663 struct dom_sid
*sids
= NULL
;
5665 uint32_t num_groups
= 0;
5667 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5669 status
= ldapsam_enum_group_memberships(my_methods
,
5675 if (!NT_STATUS_IS_OK(status
)) {
5679 for (i
=0; i
< num_groups
; i
++) {
5683 sid_peek_rid(&sids
[i
], &group_rid
);
5685 ldapsam_del_groupmem(my_methods
,
5694 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5695 if (rc
!= LDAP_SUCCESS
) {
5696 return NT_STATUS_UNSUCCESSFUL
;
5699 flush_pwnam_cache();
5701 return NT_STATUS_OK
;
5705 * ldapsam_create_group creates a new
5706 * posixGroup and sambaGroupMapping object
5707 * in the ldap groups subtree
5709 * The gid is allocated by winbindd.
5712 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5713 TALLOC_CTX
*tmp_ctx
,
5717 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5719 LDAPMessage
*entry
= NULL
;
5720 LDAPMessage
*result
= NULL
;
5721 uint32_t num_result
;
5722 bool is_new_entry
= False
;
5723 LDAPMod
**mods
= NULL
;
5728 const char *dn
= NULL
;
5729 struct dom_sid group_sid
;
5730 struct dom_sid_buf buf
;
5735 groupname
= escape_ldap_string(talloc_tos(), name
);
5736 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5737 groupname
, LDAP_OBJ_POSIXGROUP
);
5738 TALLOC_FREE(groupname
);
5740 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5741 if (rc
!= LDAP_SUCCESS
) {
5742 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5743 return NT_STATUS_UNSUCCESSFUL
;
5745 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5747 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5749 if (num_result
> 1) {
5750 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5751 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5754 if (num_result
== 1) {
5756 /* check if it is just a posix group.
5757 * or if there is a sid attached to this entry
5760 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5762 return NT_STATUS_UNSUCCESSFUL
;
5765 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5767 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5768 return NT_STATUS_GROUP_EXISTS
;
5771 /* it is just a posix group, retrieve the gid and the dn for later use */
5772 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5774 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5775 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5778 gid
= smb_strtoul(tmp
, NULL
, 10, &error
, SMB_STR_STANDARD
);
5780 DBG_ERR("Failed to convert gidNumber\n");
5781 return NT_STATUS_UNSUCCESSFUL
;
5784 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5786 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5787 return NT_STATUS_NO_MEMORY
;
5791 if (num_result
== 0) {
5792 is_new_entry
= true;
5795 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5796 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5800 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5802 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5805 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5806 return NT_STATUS_NO_MEMORY
;
5809 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5810 smbldap_set_mod(&mods
,
5813 dom_sid_str_buf(&group_sid
, &buf
));
5814 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5815 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5820 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5822 /* lets allocate a new groupid for this group */
5823 if (!winbind_allocate_gid(&gid
)) {
5824 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5825 return NT_STATUS_UNSUCCESSFUL
;
5828 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5830 escape_name
= escape_rdn_val_string_alloc(name
);
5832 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5833 return NT_STATUS_NO_MEMORY
;
5836 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix(talloc_tos()));
5838 SAFE_FREE(escape_name
);
5840 if (!gidstr
|| !dn
) {
5841 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5842 return NT_STATUS_NO_MEMORY
;
5845 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5846 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5847 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5850 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5853 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5855 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5856 /* This call may fail with rfc2307bis schema */
5857 /* Retry adding a structural class */
5858 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5859 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5863 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5866 if (rc
!= LDAP_SUCCESS
) {
5867 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5868 return NT_STATUS_UNSUCCESSFUL
;
5871 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5873 return NT_STATUS_OK
;
5876 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5878 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5879 LDAPMessage
*result
= NULL
;
5880 LDAPMessage
*entry
= NULL
;
5885 struct dom_sid group_sid
;
5886 struct dom_sid_buf buf
;
5889 /* get the group sid */
5890 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5892 filter
= talloc_asprintf(tmp_ctx
,
5895 "(objectClass=%s))",
5896 dom_sid_str_buf(&group_sid
, &buf
),
5897 LDAP_OBJ_POSIXGROUP
,
5899 if (filter
== NULL
) {
5900 return NT_STATUS_NO_MEMORY
;
5903 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5904 if (rc
!= LDAP_SUCCESS
) {
5905 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5906 return NT_STATUS_UNSUCCESSFUL
;
5908 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5910 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5912 if (num_result
== 0) {
5913 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5914 return NT_STATUS_NO_SUCH_GROUP
;
5917 if (num_result
> 1) {
5918 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5919 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5922 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5924 return NT_STATUS_UNSUCCESSFUL
;
5927 /* here it is, retrieve the dn for later use */
5928 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5930 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5931 return NT_STATUS_NO_MEMORY
;
5934 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5936 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5937 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5940 /* check no user have this group marked as primary group */
5941 filter
= talloc_asprintf(tmp_ctx
,
5944 "(objectClass=%s))",
5946 LDAP_OBJ_POSIXACCOUNT
,
5947 LDAP_OBJ_SAMBASAMACCOUNT
);
5949 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5950 if (rc
!= LDAP_SUCCESS
) {
5951 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5952 return NT_STATUS_UNSUCCESSFUL
;
5954 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5956 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5958 if (num_result
!= 0) {
5959 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5960 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5963 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5964 if (rc
!= LDAP_SUCCESS
) {
5965 return NT_STATUS_UNSUCCESSFUL
;
5968 return NT_STATUS_OK
;
5971 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5972 TALLOC_CTX
*tmp_ctx
,
5974 uint32_t member_rid
,
5977 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5978 LDAPMessage
*entry
= NULL
;
5979 LDAPMessage
*result
= NULL
;
5980 uint32_t num_result
;
5981 LDAPMod
**mods
= NULL
;
5984 const char *dn
= NULL
;
5985 struct dom_sid group_sid
;
5986 struct dom_sid member_sid
;
5987 struct dom_sid_buf buf
;
5993 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5995 case LDAP_MOD_DELETE
:
5996 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5999 return NT_STATUS_UNSUCCESSFUL
;
6002 /* get member sid */
6003 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
6005 /* get the group sid */
6006 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
6008 filter
= talloc_asprintf(tmp_ctx
,
6011 "(objectClass=%s))",
6012 dom_sid_str_buf(&member_sid
, &buf
),
6013 LDAP_OBJ_POSIXACCOUNT
,
6014 LDAP_OBJ_SAMBASAMACCOUNT
);
6015 if (filter
== NULL
) {
6016 return NT_STATUS_NO_MEMORY
;
6019 /* get the member uid */
6020 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6021 if (rc
!= LDAP_SUCCESS
) {
6022 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
6023 return NT_STATUS_UNSUCCESSFUL
;
6025 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
6027 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6029 if (num_result
== 0) {
6030 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6031 return NT_STATUS_NO_SUCH_MEMBER
;
6034 if (num_result
> 1) {
6035 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6036 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6039 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6041 return NT_STATUS_UNSUCCESSFUL
;
6044 if (modop
== LDAP_MOD_DELETE
) {
6045 /* check if we are trying to remove the member from his primary group */
6047 gid_t user_gid
, group_gid
;
6049 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
6051 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6052 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6055 user_gid
= smb_strtoul(gidstr
, NULL
, 10, &error
, SMB_STR_STANDARD
);
6057 DBG_ERR("Failed to convert user gid\n");
6058 return NT_STATUS_UNSUCCESSFUL
;
6061 if (!sid_to_gid(&group_sid
, &group_gid
)) {
6062 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6063 return NT_STATUS_UNSUCCESSFUL
;
6066 if (user_gid
== group_gid
) {
6067 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6068 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
6072 /* here it is, retrieve the uid for later use */
6073 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
6075 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6076 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6079 filter
= talloc_asprintf(tmp_ctx
,
6082 "(objectClass=%s))",
6083 dom_sid_str_buf(&group_sid
, &buf
),
6084 LDAP_OBJ_POSIXGROUP
,
6088 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6089 if (rc
!= LDAP_SUCCESS
) {
6090 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6091 return NT_STATUS_UNSUCCESSFUL
;
6093 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
6095 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6097 if (num_result
== 0) {
6098 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6099 return NT_STATUS_NO_SUCH_GROUP
;
6102 if (num_result
> 1) {
6103 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6104 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6107 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6109 return NT_STATUS_UNSUCCESSFUL
;
6112 /* here it is, retrieve the dn for later use */
6113 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
6115 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6116 return NT_STATUS_NO_MEMORY
;
6119 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
6121 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
6123 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6124 if (rc
!= LDAP_SUCCESS
) {
6125 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
6126 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6127 return NT_STATUS_MEMBER_IN_GROUP
;
6129 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
6130 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6131 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
6133 return NT_STATUS_UNSUCCESSFUL
;
6136 return NT_STATUS_OK
;
6139 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
6140 TALLOC_CTX
*tmp_ctx
,
6142 uint32_t member_rid
)
6144 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
6146 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
6147 TALLOC_CTX
*tmp_ctx
,
6149 uint32_t member_rid
)
6151 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
6154 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
6155 TALLOC_CTX
*mem_ctx
,
6156 struct samu
*sampass
)
6158 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6159 LDAPMessage
*entry
= NULL
;
6160 LDAPMessage
*result
= NULL
;
6161 uint32_t num_result
;
6162 LDAPMod
**mods
= NULL
;
6164 char *escape_username
;
6170 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6172 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6173 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6174 return NT_STATUS_UNSUCCESSFUL
;
6176 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6178 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6179 return NT_STATUS_NO_MEMORY
;
6182 escape_username
= escape_ldap_string(talloc_tos(),
6183 pdb_get_username(sampass
));
6184 if (escape_username
== NULL
) {
6185 return NT_STATUS_NO_MEMORY
;
6188 filter
= talloc_asprintf(mem_ctx
,
6191 "(objectClass=%s))",
6193 LDAP_OBJ_POSIXACCOUNT
,
6194 LDAP_OBJ_SAMBASAMACCOUNT
);
6196 TALLOC_FREE(escape_username
);
6198 if (filter
== NULL
) {
6199 return NT_STATUS_NO_MEMORY
;
6202 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6203 if (rc
!= LDAP_SUCCESS
) {
6204 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6205 return NT_STATUS_UNSUCCESSFUL
;
6207 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6209 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6211 if (num_result
== 0) {
6212 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6213 return NT_STATUS_NO_SUCH_USER
;
6216 if (num_result
> 1) {
6217 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6218 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6221 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6223 return NT_STATUS_UNSUCCESSFUL
;
6226 /* retrieve the dn for later use */
6227 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6229 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6230 return NT_STATUS_NO_MEMORY
;
6233 /* remove the old one, and add the new one, this way we do not risk races */
6234 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6238 return NT_STATUS_OK
;
6241 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6243 if (rc
!= LDAP_SUCCESS
) {
6244 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6245 pdb_get_username(sampass
), gidstr
));
6246 return NT_STATUS_UNSUCCESSFUL
;
6249 flush_pwnam_cache();
6251 return NT_STATUS_OK
;
6255 /**********************************************************************
6256 trusted domains functions
6257 *********************************************************************/
6259 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6262 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6263 ldap_state
->domain_dn
);
6266 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6267 TALLOC_CTX
*mem_ctx
,
6268 const char *domain
, LDAPMessage
**entry
)
6272 int scope
= LDAP_SCOPE_SUBTREE
;
6273 const char **attrs
= NULL
; /* NULL: get all attrs */
6274 int attrsonly
= 0; /* 0: return values too */
6275 LDAPMessage
*result
= NULL
;
6277 uint32_t num_result
;
6279 filter
= talloc_asprintf(talloc_tos(),
6280 "(&(objectClass=%s)(sambaDomainName=%s))",
6281 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6283 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6284 if (trusted_dn
== NULL
) {
6287 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6288 filter
, attrs
, attrsonly
, &result
);
6290 if (result
!= NULL
) {
6291 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6294 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6299 if (rc
!= LDAP_SUCCESS
) {
6303 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6305 if (num_result
> 1) {
6306 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6307 "%s object for domain '%s'?!\n",
6308 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6312 if (num_result
== 0) {
6313 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6314 "%s object for domain %s.\n",
6315 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6318 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6324 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6327 struct dom_sid
*sid
,
6328 time_t *pass_last_set_time
)
6330 struct ldapsam_privates
*ldap_state
=
6331 (struct ldapsam_privates
*)methods
->private_data
;
6332 LDAPMessage
*entry
= NULL
;
6334 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6336 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6345 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6346 entry
, "sambaClearTextPassword", talloc_tos());
6347 if (pwd_str
== NULL
) {
6350 /* trusteddom_pw routines do not use talloc yet... */
6351 *pwd
= SMB_STRDUP(pwd_str
);
6357 /* last change time */
6358 if (pass_last_set_time
!= NULL
) {
6360 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6361 entry
, "sambaPwdLastSet", talloc_tos());
6362 if (time_str
== NULL
) {
6365 *pass_last_set_time
= (time_t)atol(time_str
);
6371 struct dom_sid dom_sid
;
6372 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6375 if (sid_str
== NULL
) {
6378 if (!string_to_sid(&dom_sid
, sid_str
)) {
6381 sid_copy(sid
, &dom_sid
);
6387 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6390 const struct dom_sid
*sid
)
6392 struct ldapsam_privates
*ldap_state
=
6393 (struct ldapsam_privates
*)methods
->private_data
;
6394 LDAPMessage
*entry
= NULL
;
6395 LDAPMod
**mods
= NULL
;
6396 char *prev_pwd
= NULL
;
6397 char *trusted_dn
= NULL
;
6398 struct dom_sid_buf buf
;
6401 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6404 * get the current entry (if there is one) in order to put the
6405 * current password into the previous password attribute
6407 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6412 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6413 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6414 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6416 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6417 dom_sid_str_buf(sid
, &buf
));
6418 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6419 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6420 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6421 "sambaClearTextPassword", pwd
);
6423 if (entry
!= NULL
) {
6424 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6425 entry
, "sambaClearTextPassword", talloc_tos());
6426 if (prev_pwd
!= NULL
) {
6427 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6428 "sambaPreviousClearTextPassword",
6433 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods
);
6435 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6436 if (trusted_dn
== NULL
) {
6439 if (entry
== NULL
) {
6440 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6442 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6445 if (rc
!= LDAP_SUCCESS
) {
6446 DEBUG(1, ("error writing trusted domain password!\n"));
6453 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6457 struct ldapsam_privates
*ldap_state
=
6458 (struct ldapsam_privates
*)methods
->private_data
;
6459 LDAPMessage
*entry
= NULL
;
6460 const char *trusted_dn
;
6462 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6466 if (entry
== NULL
) {
6467 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6472 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6474 if (trusted_dn
== NULL
) {
6475 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6479 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6480 if (rc
!= LDAP_SUCCESS
) {
6487 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6488 TALLOC_CTX
*mem_ctx
,
6489 uint32_t *num_domains
,
6490 struct trustdom_info
***domains
)
6493 struct ldapsam_privates
*ldap_state
=
6494 (struct ldapsam_privates
*)methods
->private_data
;
6496 int scope
= LDAP_SCOPE_SUBTREE
;
6497 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6498 int attrsonly
= 0; /* 0: return values too */
6499 LDAPMessage
*result
= NULL
;
6500 LDAPMessage
*entry
= NULL
;
6502 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6503 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6505 rc
= smbldap_search(ldap_state
->smbldap_state
,
6506 ldap_state
->domain_dn
,
6513 if (result
!= NULL
) {
6514 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6517 if (rc
!= LDAP_SUCCESS
) {
6518 return NT_STATUS_UNSUCCESSFUL
;
6522 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6523 DEBUG(1, ("talloc failed\n"));
6524 return NT_STATUS_NO_MEMORY
;
6527 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6529 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6531 char *dom_name
, *dom_sid_str
;
6532 struct trustdom_info
*dom_info
;
6534 dom_info
= talloc(*domains
, struct trustdom_info
);
6535 if (dom_info
== NULL
) {
6536 DEBUG(1, ("talloc failed\n"));
6537 return NT_STATUS_NO_MEMORY
;
6540 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6544 if (dom_name
== NULL
) {
6545 DEBUG(1, ("talloc failed\n"));
6546 return NT_STATUS_NO_MEMORY
;
6548 dom_info
->name
= dom_name
;
6550 dom_sid_str
= smbldap_talloc_single_attribute(
6551 priv2ld(ldap_state
), entry
, "sambaSID",
6553 if (dom_sid_str
== NULL
) {
6554 DEBUG(1, ("talloc failed\n"));
6555 return NT_STATUS_NO_MEMORY
;
6557 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6558 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6560 return NT_STATUS_UNSUCCESSFUL
;
6563 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6564 domains
, num_domains
);
6566 if (*domains
== NULL
) {
6567 DEBUG(1, ("talloc failed\n"));
6568 return NT_STATUS_NO_MEMORY
;
6572 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6573 return NT_STATUS_OK
;
6577 /**********************************************************************
6579 *********************************************************************/
6581 static void free_private_data(void **vp
)
6583 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6585 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6587 if ((*ldap_state
)->result
!= NULL
) {
6588 ldap_msgfree((*ldap_state
)->result
);
6589 (*ldap_state
)->result
= NULL
;
6591 if ((*ldap_state
)->domain_dn
!= NULL
) {
6592 SAFE_FREE((*ldap_state
)->domain_dn
);
6597 /* No need to free any further, as it is talloc()ed */
6600 /*********************************************************************
6601 Intitalise the parts of the pdb_methods structure that are common to
6603 *********************************************************************/
6605 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6608 struct ldapsam_privates
*ldap_state
;
6609 char *bind_dn
= NULL
;
6610 char *bind_secret
= NULL
;
6612 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6616 (*pdb_method
)->name
= "ldapsam";
6618 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6619 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6620 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6621 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6622 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6623 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6625 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6626 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6627 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6628 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6629 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6630 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6631 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6633 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6634 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6636 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6638 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6639 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6641 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6642 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6643 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6644 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6646 /* TODO: Setup private data and free */
6648 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6649 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6650 return NT_STATUS_NO_MEMORY
;
6653 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6654 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6655 return NT_STATUS_NO_MEMORY
;
6658 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6659 location
, false, bind_dn
, bind_secret
,
6660 &ldap_state
->smbldap_state
);
6661 memset(bind_secret
, '\0', strlen(bind_secret
));
6662 SAFE_FREE(bind_secret
);
6664 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6668 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6669 return NT_STATUS_NO_MEMORY
;
6672 (*pdb_method
)->private_data
= ldap_state
;
6674 (*pdb_method
)->free_private_data
= free_private_data
;
6676 return NT_STATUS_OK
;
6679 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods
*m
)
6684 /**********************************************************************
6685 Initialise the normal mode for pdb_ldap
6686 *********************************************************************/
6688 NTSTATUS
pdb_ldapsam_init_common(struct pdb_methods
**pdb_method
,
6689 const char *location
)
6692 struct ldapsam_privates
*ldap_state
= NULL
;
6693 uint32_t alg_rid_base
;
6694 char *alg_rid_base_string
= NULL
;
6695 LDAPMessage
*result
= NULL
;
6696 LDAPMessage
*entry
= NULL
;
6697 struct dom_sid ldap_domain_sid
;
6698 struct dom_sid secrets_domain_sid
;
6699 char *domain_sid_string
= NULL
;
6701 char *uri
= talloc_strdup( NULL
, location
);
6703 trim_char( uri
, '\"', '\"' );
6704 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6708 if (!NT_STATUS_IS_OK(nt_status
)) {
6712 (*pdb_method
)->name
= "ldapsam";
6714 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6715 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6716 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6717 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6718 (*pdb_method
)->search_users
= ldapsam_search_users
;
6719 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6720 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6721 (*pdb_method
)->is_responsible_for_wellknown
=
6722 ldapsam_is_responsible_for_wellknown
;
6724 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6725 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6726 (*pdb_method
)->enum_group_memberships
=
6727 ldapsam_enum_group_memberships
;
6728 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6729 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6730 (*pdb_method
)->id_to_sid
= ldapsam_id_to_sid
;
6732 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6733 (*pdb_method
)->create_user
= ldapsam_create_user
;
6734 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6735 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6736 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6737 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6738 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6739 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6743 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6744 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6746 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6748 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6750 ldap_state
->domain_name
, True
);
6752 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6753 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6754 "info, nor add one to the domain. "
6755 "We cannot work reliably without it.\n"));
6756 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6759 /* Given that the above might fail, everything below this must be
6762 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
6765 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6767 ldap_msgfree(result
);
6768 return NT_STATUS_UNSUCCESSFUL
;
6771 dn
= smbldap_talloc_dn(talloc_tos(),
6772 smbldap_get_ldap(ldap_state
->smbldap_state
),
6775 ldap_msgfree(result
);
6776 return NT_STATUS_UNSUCCESSFUL
;
6779 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6782 domain_sid_string
= smbldap_talloc_single_attribute(
6783 smbldap_get_ldap(ldap_state
->smbldap_state
),
6785 get_userattr_key2string(ldap_state
->schema_ver
,
6786 LDAP_ATTR_USER_SID
),
6789 if (domain_sid_string
) {
6791 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6792 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6793 "read as a valid SID\n", domain_sid_string
));
6794 ldap_msgfree(result
);
6795 TALLOC_FREE(domain_sid_string
);
6796 return NT_STATUS_INVALID_PARAMETER
;
6798 found_sid
= PDB_secrets_fetch_domain_sid(ldap_state
->domain_name
,
6799 &secrets_domain_sid
);
6800 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6801 &ldap_domain_sid
)) {
6802 struct dom_sid_buf buf1
, buf2
;
6803 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6804 "%s based on pdb_ldap results %s -> %s\n",
6805 ldap_state
->domain_name
,
6806 dom_sid_str_buf(&secrets_domain_sid
, &buf1
),
6807 dom_sid_str_buf(&ldap_domain_sid
, &buf2
)));
6809 /* reset secrets.tdb sid */
6810 PDB_secrets_store_domain_sid(ldap_state
->domain_name
,
6812 DEBUG(1, ("New global sam SID: %s\n",
6813 dom_sid_str_buf(get_global_sam_sid(),
6816 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6817 TALLOC_FREE(domain_sid_string
);
6820 alg_rid_base_string
= smbldap_talloc_single_attribute(
6821 smbldap_get_ldap(ldap_state
->smbldap_state
),
6823 get_attr_key2string( dominfo_attr_list
,
6824 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6826 if (alg_rid_base_string
) {
6827 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6828 if (alg_rid_base
!= algorithmic_rid_base()) {
6829 DEBUG(0, ("The value of 'algorithmic RID base' has "
6830 "changed since the LDAP\n"
6831 "database was initialised. Aborting. \n"));
6832 ldap_msgfree(result
);
6833 TALLOC_FREE(alg_rid_base_string
);
6834 return NT_STATUS_UNSUCCESSFUL
;
6836 TALLOC_FREE(alg_rid_base_string
);
6838 ldap_msgfree(result
);
6840 return NT_STATUS_OK
;
6843 NTSTATUS
pdb_ldapsam_init(TALLOC_CTX
*ctx
)
6847 nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
,
6849 pdb_ldapsam_init_common
);
6850 if (!NT_STATUS_IS_OK(nt_status
)) {
6854 /* Let pdb_nds register backends */
6857 return NT_STATUS_OK
;