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"
58 #define DBGC_CLASS DBGC_PASSDB
65 #include "passdb/pdb_ldap.h"
66 #include "passdb/pdb_nds.h"
67 #include "passdb/pdb_ipa.h"
68 #include "passdb/pdb_ldap_util.h"
69 #include "passdb/pdb_ldap_schema.h"
71 /**********************************************************************
72 Simple helper function to make stuff better readable
73 **********************************************************************/
75 LDAP
*priv2ld(struct ldapsam_privates
*priv
)
77 return priv
->smbldap_state
->ldap_struct
;
80 /**********************************************************************
81 Get the attribute name given a user schame version.
82 **********************************************************************/
84 static const char* get_userattr_key2string( int schema_ver
, int key
)
86 switch ( schema_ver
) {
87 case SCHEMAVER_SAMBASAMACCOUNT
:
88 return get_attr_key2string( attrib_map_v30
, key
);
91 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
97 /**********************************************************************
98 Return the list of attribute names given a user schema version.
99 **********************************************************************/
101 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
103 switch ( schema_ver
) {
104 case SCHEMAVER_SAMBASAMACCOUNT
:
105 return get_attr_list( mem_ctx
, attrib_map_v30
);
107 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
114 /**************************************************************************
115 Return the list of attribute names to delete given a user schema version.
116 **************************************************************************/
118 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
121 switch ( schema_ver
) {
122 case SCHEMAVER_SAMBASAMACCOUNT
:
123 return get_attr_list( mem_ctx
,
124 attrib_map_to_delete_v30
);
126 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
134 /*******************************************************************
135 Generate the LDAP search filter for the objectclass based on the
136 version of the schema we are using.
137 ******************************************************************/
139 static const char* get_objclass_filter( int schema_ver
)
141 fstring objclass_filter
;
144 switch( schema_ver
) {
145 case SCHEMAVER_SAMBASAMACCOUNT
:
146 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
149 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
150 objclass_filter
[0] = '\0';
154 result
= talloc_strdup(talloc_tos(), objclass_filter
);
155 SMB_ASSERT(result
!= NULL
);
159 /*****************************************************************
160 Scan a sequence number off OpenLDAP's syncrepl contextCSN
161 ******************************************************************/
163 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
165 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
166 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
167 LDAPMessage
*msg
= NULL
;
168 LDAPMessage
*entry
= NULL
;
170 char **values
= NULL
;
171 int rc
, num_result
, num_values
, rid
;
177 /* Unfortunatly there is no proper way to detect syncrepl-support in
178 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
179 * but do not show up in the root-DSE yet. Neither we can query the
180 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
181 * objectclass. Currently we require lp_ldap_suffix() to show up as
182 * namingContext. - Guenther
185 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
190 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
194 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix(talloc_tos()))) {
195 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
196 "as top-level namingContext\n", lp_ldap_suffix(talloc_tos())));
200 mem_ctx
= talloc_init("ldapsam_get_seq_num");
203 return NT_STATUS_NO_MEMORY
;
205 if ((attrs
= talloc_array(mem_ctx
, const char *, 2)) == NULL
) {
206 ntstatus
= NT_STATUS_NO_MEMORY
;
210 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
211 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
214 /* consumer syncreplCookie: */
215 /* csn=20050126161620Z#0000001#00#00000 */
216 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
218 suffix
= talloc_asprintf(mem_ctx
,
219 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix(talloc_tos()));
221 ntstatus
= NT_STATUS_NO_MEMORY
;
226 /* provider contextCSN */
227 /* 20050126161620Z#000009#00#000000 */
228 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
230 suffix
= talloc_asprintf(mem_ctx
,
231 "cn=ldapsync,%s", lp_ldap_suffix(talloc_tos()));
234 ntstatus
= NT_STATUS_NO_MEMORY
;
239 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
240 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
242 if (rc
!= LDAP_SUCCESS
) {
246 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
247 if (num_result
!= 1) {
248 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
252 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
254 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
258 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
259 if (values
== NULL
) {
260 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
264 num_values
= ldap_count_values(values
);
265 if (num_values
== 0) {
266 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
271 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
272 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
277 if (!strncmp(p
, "csn=", strlen("csn=")))
280 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
282 *seq_num
= generalized_to_unix_time(p
);
284 /* very basic sanity check */
286 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
291 ntstatus
= NT_STATUS_OK
;
295 ldap_value_free(values
);
299 talloc_destroy(mem_ctx
);
304 /*******************************************************************
305 Run the search by name.
306 ******************************************************************/
308 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
310 LDAPMessage
** result
,
314 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
318 return LDAP_NO_MEMORY
;
322 * in the filter expression, replace %u with the real name
323 * so in ldap filter, %u MUST exist :-)
325 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
326 get_objclass_filter(ldap_state
->schema_ver
));
328 TALLOC_FREE(escape_user
);
329 return LDAP_NO_MEMORY
;
332 * have to use this here because $ is filtered out
336 filter
= talloc_all_string_sub(talloc_tos(),
337 filter
, "%u", escape_user
);
338 TALLOC_FREE(escape_user
);
340 return LDAP_NO_MEMORY
;
343 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
344 filter
, attr
, result
);
349 /*******************************************************************
350 Run the search by SID.
351 ******************************************************************/
353 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
354 const struct dom_sid
*sid
, LDAPMessage
** result
,
361 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
362 get_userattr_key2string(ldap_state
->schema_ver
,
364 sid_to_fstring(sid_string
, sid
),
365 get_objclass_filter(ldap_state
->schema_ver
));
367 return LDAP_NO_MEMORY
;
370 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
371 filter
, attr
, result
);
377 /*******************************************************************
378 Delete complete object or objectclass and attrs from
379 object found in search_result depending on lp_ldap_delete_dn
380 ******************************************************************/
382 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
385 const char *objectclass
,
388 LDAPMod
**mods
= NULL
;
391 BerElement
*ptr
= NULL
;
393 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
395 return LDAP_NO_MEMORY
;
398 if (lp_ldap_delete_dn()) {
399 return smbldap_delete(priv
->smbldap_state
, dn
);
402 /* Ok, delete only the SAM attributes */
404 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
406 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
409 /* We are only allowed to delete the attributes that
412 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
413 if (strequal(*attrib
, name
)) {
414 DEBUG(10, ("ldapsam_delete_entry: deleting "
415 "attribute %s\n", name
));
416 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
427 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
428 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
430 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
433 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
438 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
439 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
445 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
446 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
456 /**********************************************************************
457 Initialize struct samu from an LDAP query.
458 (Based on init_sam_from_buffer in pdb_tdb.c)
459 *********************************************************************/
461 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
462 struct samu
* sampass
,
469 pass_can_change_time
,
472 char *username
= NULL
,
478 *logon_script
= NULL
,
479 *profile_path
= NULL
,
481 *workstations
= NULL
,
484 uint8 smblmpwd
[LM_HASH_LEN
],
485 smbntpwd
[NT_HASH_LEN
];
486 bool use_samba_attrs
= True
;
487 uint32_t acct_ctrl
= 0;
489 uint16_t bad_password_count
= 0,
492 uint8 hours
[MAX_HOURS_LEN
];
494 struct login_cache cache_entry
;
496 bool expand_explicit
= lp_passdb_expand_explicit();
498 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
503 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
504 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
508 if (priv2ld(ldap_state
) == NULL
) {
509 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
510 "ldap_struct is NULL!\n"));
514 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
518 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
523 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
525 nt_username
= talloc_strdup(ctx
, username
);
530 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
535 pdb_set_username(sampass
, username
, PDB_SET
);
537 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
538 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
540 /* deal with different attributes between the schema first */
542 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
543 if ((temp
= smbldap_talloc_single_attribute(
544 ldap_state
->smbldap_state
->ldap_struct
,
546 get_userattr_key2string(ldap_state
->schema_ver
,
549 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
552 if ((temp
= smbldap_talloc_single_attribute(
553 ldap_state
->smbldap_state
->ldap_struct
,
555 get_userattr_key2string(ldap_state
->schema_ver
,
558 user_rid
= (uint32_t)atol(temp
);
559 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
563 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
564 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
565 get_userattr_key2string(ldap_state
->schema_ver
,
567 get_userattr_key2string(ldap_state
->schema_ver
,
573 temp
= smbldap_talloc_single_attribute(
574 ldap_state
->smbldap_state
->ldap_struct
,
576 get_userattr_key2string(ldap_state
->schema_ver
,
577 LDAP_ATTR_PWD_LAST_SET
),
580 pass_last_set_time
= (time_t) atol(temp
);
581 pdb_set_pass_last_set_time(sampass
,
582 pass_last_set_time
, PDB_SET
);
585 temp
= smbldap_talloc_single_attribute(
586 ldap_state
->smbldap_state
->ldap_struct
,
588 get_userattr_key2string(ldap_state
->schema_ver
,
589 LDAP_ATTR_LOGON_TIME
),
592 logon_time
= (time_t) atol(temp
);
593 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
596 temp
= smbldap_talloc_single_attribute(
597 ldap_state
->smbldap_state
->ldap_struct
,
599 get_userattr_key2string(ldap_state
->schema_ver
,
600 LDAP_ATTR_LOGOFF_TIME
),
603 logoff_time
= (time_t) atol(temp
);
604 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
607 temp
= smbldap_talloc_single_attribute(
608 ldap_state
->smbldap_state
->ldap_struct
,
610 get_userattr_key2string(ldap_state
->schema_ver
,
611 LDAP_ATTR_KICKOFF_TIME
),
614 kickoff_time
= (time_t) atol(temp
);
615 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
618 temp
= smbldap_talloc_single_attribute(
619 ldap_state
->smbldap_state
->ldap_struct
,
621 get_userattr_key2string(ldap_state
->schema_ver
,
622 LDAP_ATTR_PWD_CAN_CHANGE
),
625 pass_can_change_time
= (time_t) atol(temp
);
626 pdb_set_pass_can_change_time(sampass
,
627 pass_can_change_time
, PDB_SET
);
630 /* recommend that 'gecos' and 'displayName' should refer to the same
631 * attribute OID. userFullName depreciated, only used by Samba
632 * primary rules of LDAP: don't make a new attribute when one is already defined
633 * that fits your needs; using cn then displayName rather than 'userFullName'
636 fullname
= smbldap_talloc_single_attribute(
637 ldap_state
->smbldap_state
->ldap_struct
,
639 get_userattr_key2string(ldap_state
->schema_ver
,
640 LDAP_ATTR_DISPLAY_NAME
),
643 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
645 fullname
= smbldap_talloc_single_attribute(
646 ldap_state
->smbldap_state
->ldap_struct
,
648 get_userattr_key2string(ldap_state
->schema_ver
,
652 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
656 dir_drive
= smbldap_talloc_single_attribute(
657 ldap_state
->smbldap_state
->ldap_struct
,
659 get_userattr_key2string(ldap_state
->schema_ver
,
660 LDAP_ATTR_HOME_DRIVE
),
663 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
665 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
668 homedir
= smbldap_talloc_single_attribute(
669 ldap_state
->smbldap_state
->ldap_struct
,
671 get_userattr_key2string(ldap_state
->schema_ver
,
672 LDAP_ATTR_HOME_PATH
),
675 if (expand_explicit
) {
676 homedir
= talloc_sub_basic(ctx
,
684 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
686 pdb_set_homedir(sampass
,
687 talloc_sub_basic(ctx
, username
, domain
,
692 logon_script
= smbldap_talloc_single_attribute(
693 ldap_state
->smbldap_state
->ldap_struct
,
695 get_userattr_key2string(ldap_state
->schema_ver
,
696 LDAP_ATTR_LOGON_SCRIPT
),
699 if (expand_explicit
) {
700 logon_script
= talloc_sub_basic(ctx
,
708 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
710 pdb_set_logon_script(sampass
,
711 talloc_sub_basic(ctx
, username
, domain
,
716 profile_path
= smbldap_talloc_single_attribute(
717 ldap_state
->smbldap_state
->ldap_struct
,
719 get_userattr_key2string(ldap_state
->schema_ver
,
720 LDAP_ATTR_PROFILE_PATH
),
723 if (expand_explicit
) {
724 profile_path
= talloc_sub_basic(ctx
,
732 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
734 pdb_set_profile_path(sampass
,
735 talloc_sub_basic(ctx
, username
, domain
,
740 acct_desc
= smbldap_talloc_single_attribute(
741 ldap_state
->smbldap_state
->ldap_struct
,
743 get_userattr_key2string(ldap_state
->schema_ver
,
747 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
750 workstations
= smbldap_talloc_single_attribute(
751 ldap_state
->smbldap_state
->ldap_struct
,
753 get_userattr_key2string(ldap_state
->schema_ver
,
757 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
760 munged_dial
= smbldap_talloc_single_attribute(
761 ldap_state
->smbldap_state
->ldap_struct
,
763 get_userattr_key2string(ldap_state
->schema_ver
,
764 LDAP_ATTR_MUNGED_DIAL
),
767 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
770 /* FIXME: hours stuff should be cleaner */
774 memset(hours
, 0xff, hours_len
);
776 if (ldap_state
->is_nds_ldap
) {
779 char clear_text_pw
[512];
781 /* Make call to Novell eDirectory ldap extension to get clear text password.
782 NOTE: This will only work if we have an SSL connection to eDirectory. */
783 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
784 if (user_dn
!= NULL
) {
785 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
787 pwd_len
= sizeof(clear_text_pw
);
788 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
789 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
790 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
791 TALLOC_FREE(user_dn
);
794 ZERO_STRUCT(smblmpwd
);
795 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
796 TALLOC_FREE(user_dn
);
799 ZERO_STRUCT(smbntpwd
);
800 use_samba_attrs
= False
;
803 TALLOC_FREE(user_dn
);
806 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
810 if (use_samba_attrs
) {
811 temp
= smbldap_talloc_single_attribute(
812 ldap_state
->smbldap_state
->ldap_struct
,
814 get_userattr_key2string(ldap_state
->schema_ver
,
818 pdb_gethexpwd(temp
, smblmpwd
);
819 memset((char *)temp
, '\0', strlen(temp
)+1);
820 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
823 ZERO_STRUCT(smblmpwd
);
826 temp
= smbldap_talloc_single_attribute(
827 ldap_state
->smbldap_state
->ldap_struct
,
829 get_userattr_key2string(ldap_state
->schema_ver
,
833 pdb_gethexpwd(temp
, smbntpwd
);
834 memset((char *)temp
, '\0', strlen(temp
)+1);
835 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
838 ZERO_STRUCT(smbntpwd
);
844 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
846 uint8
*pwhist
= NULL
;
848 char *history_string
= talloc_array(ctx
, char,
849 MAX_PW_HISTORY_LEN
*64);
851 if (!history_string
) {
855 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
857 pwhist
= talloc_array(ctx
, uint8
,
858 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
859 if (pwhist
== NULL
) {
860 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
863 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
865 if (smbldap_get_single_attribute(
866 ldap_state
->smbldap_state
->ldap_struct
,
868 get_userattr_key2string(ldap_state
->schema_ver
,
869 LDAP_ATTR_PWD_HISTORY
),
871 MAX_PW_HISTORY_LEN
*64)) {
872 bool hex_failed
= false;
873 for (i
= 0; i
< pwHistLen
; i
++){
874 /* Get the 16 byte salt. */
875 if (!pdb_gethexpwd(&history_string
[i
*64],
876 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
880 /* Get the 16 byte MD5 hash of salt+passwd. */
881 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
882 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
883 PW_HISTORY_SALT_LEN
])) {
889 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
891 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
894 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
899 temp
= smbldap_talloc_single_attribute(
900 ldap_state
->smbldap_state
->ldap_struct
,
902 get_userattr_key2string(ldap_state
->schema_ver
,
906 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
908 if (acct_ctrl
== 0) {
909 acct_ctrl
|= ACB_NORMAL
;
912 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
914 acct_ctrl
|= ACB_NORMAL
;
917 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
918 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
920 temp
= smbldap_talloc_single_attribute(
921 ldap_state
->smbldap_state
->ldap_struct
,
923 get_userattr_key2string(ldap_state
->schema_ver
,
924 LDAP_ATTR_BAD_PASSWORD_COUNT
),
927 bad_password_count
= (uint32_t) atol(temp
);
928 pdb_set_bad_password_count(sampass
,
929 bad_password_count
, PDB_SET
);
932 temp
= smbldap_talloc_single_attribute(
933 ldap_state
->smbldap_state
->ldap_struct
,
935 get_userattr_key2string(ldap_state
->schema_ver
,
936 LDAP_ATTR_BAD_PASSWORD_TIME
),
939 bad_password_time
= (time_t) atol(temp
);
940 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
944 temp
= smbldap_talloc_single_attribute(
945 ldap_state
->smbldap_state
->ldap_struct
,
947 get_userattr_key2string(ldap_state
->schema_ver
,
948 LDAP_ATTR_LOGON_COUNT
),
951 logon_count
= (uint32_t) atol(temp
);
952 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
955 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
957 temp
= smbldap_talloc_single_attribute(
958 ldap_state
->smbldap_state
->ldap_struct
,
960 get_userattr_key2string(ldap_state
->schema_ver
,
961 LDAP_ATTR_LOGON_HOURS
),
964 pdb_gethexhours(temp
, hours
);
965 memset((char *)temp
, '\0', strlen(temp
) +1);
966 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
970 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
971 struct passwd unix_pw
;
972 bool have_uid
= false;
973 bool have_gid
= false;
974 struct dom_sid mapped_gsid
;
975 const struct dom_sid
*primary_gsid
;
978 ZERO_STRUCT(unix_pw
);
980 unix_pw
.pw_name
= username
;
981 unix_pw
.pw_passwd
= discard_const_p(char, "x");
983 temp
= smbldap_talloc_single_attribute(
989 /* We've got a uid, feed the cache */
990 unix_pw
.pw_uid
= strtoul(temp
, NULL
, 10);
993 temp
= smbldap_talloc_single_attribute(
999 /* We've got a uid, feed the cache */
1000 unix_pw
.pw_gid
= strtoul(temp
, NULL
, 10);
1003 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1004 priv2ld(ldap_state
),
1008 if (unix_pw
.pw_gecos
) {
1009 unix_pw
.pw_gecos
= fullname
;
1011 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1012 priv2ld(ldap_state
),
1016 if (unix_pw
.pw_dir
) {
1017 unix_pw
.pw_dir
= discard_const_p(char, "");
1019 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1020 priv2ld(ldap_state
),
1024 if (unix_pw
.pw_shell
) {
1025 unix_pw
.pw_shell
= discard_const_p(char, "");
1028 if (have_uid
&& have_gid
) {
1029 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1031 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1034 if (sampass
->unix_pw
== NULL
) {
1035 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1036 pdb_get_username(sampass
)));
1040 id
.id
= sampass
->unix_pw
->pw_uid
;
1041 id
.type
= ID_TYPE_UID
;
1043 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass
), &id
);
1045 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1046 primary_gsid
= pdb_get_group_sid(sampass
);
1047 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1048 id
.id
= sampass
->unix_pw
->pw_gid
;
1049 id
.type
= ID_TYPE_GID
;
1051 idmap_cache_set_sid2unixid(primary_gsid
, &id
);
1055 /* check the timestamp of the cache vs ldap entry */
1056 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1062 /* see if we have newer updates */
1063 if (!login_cache_read(sampass
, &cache_entry
)) {
1064 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1065 (unsigned int)pdb_get_bad_password_count(sampass
),
1066 (unsigned int)pdb_get_bad_password_time(sampass
)));
1071 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1072 (unsigned int)ldap_entry_time
,
1073 (unsigned int)cache_entry
.entry_timestamp
,
1074 (unsigned int)cache_entry
.bad_password_time
));
1076 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1077 /* cache is older than directory , so
1078 we need to delete the entry but allow the
1079 fields to be written out */
1080 login_cache_delentry(sampass
);
1083 pdb_set_acct_ctrl(sampass
,
1084 pdb_get_acct_ctrl(sampass
) |
1085 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1087 pdb_set_bad_password_count(sampass
,
1088 cache_entry
.bad_password_count
,
1090 pdb_set_bad_password_time(sampass
,
1091 cache_entry
.bad_password_time
,
1103 /**********************************************************************
1104 Initialize the ldap db from a struct samu. Called on update.
1105 (Based on init_buffer_from_sam in pdb_tdb.c)
1106 *********************************************************************/
1108 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1109 LDAPMessage
*existing
,
1110 LDAPMod
*** mods
, struct samu
* sampass
,
1111 bool (*need_update
)(const struct samu
*,
1116 if (mods
== NULL
|| sampass
== NULL
) {
1117 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1124 * took out adding "objectclass: sambaAccount"
1125 * do this on a per-mod basis
1127 if (need_update(sampass
, PDB_USERNAME
)) {
1128 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1129 "uid", pdb_get_username(sampass
));
1130 if (ldap_state
->is_nds_ldap
) {
1131 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1132 "cn", pdb_get_username(sampass
));
1133 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1134 "sn", pdb_get_username(sampass
));
1138 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1140 /* only update the RID if we actually need to */
1141 if (need_update(sampass
, PDB_USERSID
)) {
1143 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1145 switch ( ldap_state
->schema_ver
) {
1146 case SCHEMAVER_SAMBASAMACCOUNT
:
1147 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1148 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1149 sid_to_fstring(sid_string
, user_sid
));
1153 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1158 /* we don't need to store the primary group RID - so leaving it
1159 'free' to hang off the unix primary group makes life easier */
1161 if (need_update(sampass
, PDB_GROUPSID
)) {
1163 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1165 switch ( ldap_state
->schema_ver
) {
1166 case SCHEMAVER_SAMBASAMACCOUNT
:
1167 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1168 get_userattr_key2string(ldap_state
->schema_ver
,
1169 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1173 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1179 /* displayName, cn, and gecos should all be the same
1180 * most easily accomplished by giving them the same OID
1181 * gecos isn't set here b/c it should be handled by the
1183 * We change displayName only and fall back to cn if
1184 * it does not exist.
1187 if (need_update(sampass
, PDB_FULLNAME
))
1188 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1189 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1190 pdb_get_fullname(sampass
));
1192 if (need_update(sampass
, PDB_ACCTDESC
))
1193 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1194 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1195 pdb_get_acct_desc(sampass
));
1197 if (need_update(sampass
, PDB_WORKSTATIONS
))
1198 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1199 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1200 pdb_get_workstations(sampass
));
1202 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1203 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1204 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1205 pdb_get_munged_dial(sampass
));
1207 if (need_update(sampass
, PDB_SMBHOME
))
1208 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1209 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1210 pdb_get_homedir(sampass
));
1212 if (need_update(sampass
, PDB_DRIVE
))
1213 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1214 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1215 pdb_get_dir_drive(sampass
));
1217 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1218 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1219 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1220 pdb_get_logon_script(sampass
));
1222 if (need_update(sampass
, PDB_PROFILE
))
1223 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1224 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1225 pdb_get_profile_path(sampass
));
1227 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1230 if (need_update(sampass
, PDB_LOGONTIME
))
1231 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1232 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1235 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1238 if (need_update(sampass
, PDB_LOGOFFTIME
))
1239 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1240 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1243 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1246 if (need_update(sampass
, PDB_KICKOFFTIME
))
1247 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1248 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1251 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1254 if (need_update(sampass
, PDB_CANCHANGETIME
))
1255 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1256 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1259 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1260 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1262 if (need_update(sampass
, PDB_LMPASSWD
)) {
1263 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1266 pdb_sethexpwd(pwstr
, lm_pw
,
1267 pdb_get_acct_ctrl(sampass
));
1268 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1269 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1272 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1273 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1277 if (need_update(sampass
, PDB_NTPASSWD
)) {
1278 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1281 pdb_sethexpwd(pwstr
, nt_pw
,
1282 pdb_get_acct_ctrl(sampass
));
1283 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1284 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1287 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1288 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1293 if (need_update(sampass
, PDB_PWHISTORY
)) {
1295 uint32_t pwHistLen
= 0;
1296 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1298 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1302 if (pwHistLen
== 0) {
1303 /* Remove any password history from the LDAP store. */
1304 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1308 uint32_t currHistLen
= 0;
1309 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1310 if (pwhist
!= NULL
) {
1311 /* We can only store (1024-1/64 password history entries. */
1312 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1313 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1314 /* Store the salt. */
1315 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1316 /* Followed by the md5 hash of salt + md4 hash */
1317 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1318 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1319 DEBUG(100, ("pwstr=%s\n", pwstr
));
1323 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1324 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1329 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1330 if (asprintf(&temp
, "%li",
1331 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1334 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1335 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1341 if (need_update(sampass
, PDB_HOURS
)) {
1342 const uint8
*hours
= pdb_get_hours(sampass
);
1345 pdb_sethexhours(hourstr
, hours
);
1346 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1349 get_userattr_key2string(ldap_state
->schema_ver
,
1350 LDAP_ATTR_LOGON_HOURS
),
1355 if (need_update(sampass
, PDB_ACCTCTRL
))
1356 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1357 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1358 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1360 /* password lockout cache:
1361 - If we are now autolocking or clearing, we write to ldap
1362 - If we are clearing, we delete the cache entry
1363 - If the count is > 0, we update the cache
1365 This even means when autolocking, we cache, just in case the
1366 update doesn't work, and we have to cache the autolock flag */
1368 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1369 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1370 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1371 time_t badtime
= pdb_get_bad_password_time(sampass
);
1373 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1375 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1376 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1378 if ((badcount
>= pol
) || (badcount
== 0)) {
1379 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1380 (unsigned int)badcount
, (unsigned int)badtime
));
1381 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1385 ldap_state
->smbldap_state
->ldap_struct
,
1387 get_userattr_key2string(
1388 ldap_state
->schema_ver
,
1389 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1393 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1397 ldap_state
->smbldap_state
->ldap_struct
,
1399 get_userattr_key2string(
1400 ldap_state
->schema_ver
,
1401 LDAP_ATTR_BAD_PASSWORD_TIME
),
1405 if (badcount
== 0) {
1406 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1407 login_cache_delentry(sampass
);
1409 struct login_cache cache_entry
;
1411 cache_entry
.entry_timestamp
= time(NULL
);
1412 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1413 cache_entry
.bad_password_count
= badcount
;
1414 cache_entry
.bad_password_time
= badtime
;
1416 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1417 login_cache_write(sampass
, &cache_entry
);
1424 /**********************************************************************
1425 End enumeration of the LDAP password list.
1426 *********************************************************************/
1428 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1430 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1431 if (ldap_state
->result
) {
1432 ldap_msgfree(ldap_state
->result
);
1433 ldap_state
->result
= NULL
;
1437 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1438 const char *new_attr
)
1442 if (new_attr
== NULL
) {
1446 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1450 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1452 SMB_ASSERT((*attr_list
) != NULL
);
1453 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1454 (*attr_list
)[i
+1] = NULL
;
1457 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1458 const char ***attr_list
)
1460 append_attr(mem_ctx
, attr_list
, "uidNumber");
1461 append_attr(mem_ctx
, attr_list
, "gidNumber");
1462 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1463 append_attr(mem_ctx
, attr_list
, "loginShell");
1464 append_attr(mem_ctx
, attr_list
, "gecos");
1467 /**********************************************************************
1468 Get struct samu entry from LDAP by username.
1469 *********************************************************************/
1471 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1473 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1474 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1475 LDAPMessage
*result
= NULL
;
1476 LDAPMessage
*entry
= NULL
;
1478 const char ** attr_list
;
1481 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1482 append_attr(user
, &attr_list
,
1483 get_userattr_key2string(ldap_state
->schema_ver
,
1484 LDAP_ATTR_MOD_TIMESTAMP
));
1485 ldapsam_add_unix_attributes(user
, &attr_list
);
1486 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1488 TALLOC_FREE( attr_list
);
1490 if ( rc
!= LDAP_SUCCESS
)
1491 return NT_STATUS_NO_SUCH_USER
;
1493 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1496 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1497 ldap_msgfree(result
);
1498 return NT_STATUS_NO_SUCH_USER
;
1499 } else if (count
> 1) {
1500 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1501 ldap_msgfree(result
);
1502 return NT_STATUS_NO_SUCH_USER
;
1505 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1507 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1508 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1509 ldap_msgfree(result
);
1510 return NT_STATUS_NO_SUCH_USER
;
1512 pdb_set_backend_private_data(user
, result
, NULL
,
1513 my_methods
, PDB_CHANGED
);
1514 smbldap_talloc_autofree_ldapmsg(user
, result
);
1517 ldap_msgfree(result
);
1522 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1523 const struct dom_sid
*sid
, LDAPMessage
**result
)
1526 const char ** attr_list
;
1528 switch ( ldap_state
->schema_ver
) {
1529 case SCHEMAVER_SAMBASAMACCOUNT
: {
1530 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1531 if (tmp_ctx
== NULL
) {
1532 return LDAP_NO_MEMORY
;
1535 attr_list
= get_userattr_list(tmp_ctx
,
1536 ldap_state
->schema_ver
);
1537 append_attr(tmp_ctx
, &attr_list
,
1538 get_userattr_key2string(
1539 ldap_state
->schema_ver
,
1540 LDAP_ATTR_MOD_TIMESTAMP
));
1541 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1542 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1544 TALLOC_FREE(tmp_ctx
);
1546 if ( rc
!= LDAP_SUCCESS
)
1552 DEBUG(0,("Invalid schema version specified\n"));
1558 /**********************************************************************
1559 Get struct samu entry from LDAP by SID.
1560 *********************************************************************/
1562 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1564 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1565 LDAPMessage
*result
= NULL
;
1566 LDAPMessage
*entry
= NULL
;
1570 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1572 if (rc
!= LDAP_SUCCESS
)
1573 return NT_STATUS_NO_SUCH_USER
;
1575 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1578 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1579 "count=%d\n", sid_string_dbg(sid
), count
));
1580 ldap_msgfree(result
);
1581 return NT_STATUS_NO_SUCH_USER
;
1582 } else if (count
> 1) {
1583 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1584 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1586 ldap_msgfree(result
);
1587 return NT_STATUS_NO_SUCH_USER
;
1590 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1592 ldap_msgfree(result
);
1593 return NT_STATUS_NO_SUCH_USER
;
1596 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1597 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1598 ldap_msgfree(result
);
1599 return NT_STATUS_NO_SUCH_USER
;
1602 pdb_set_backend_private_data(user
, result
, NULL
,
1603 my_methods
, PDB_CHANGED
);
1604 smbldap_talloc_autofree_ldapmsg(user
, result
);
1605 return NT_STATUS_OK
;
1608 /********************************************************************
1609 Do the actual modification - also change a plaintext passord if
1611 **********************************************************************/
1613 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1614 struct samu
*newpwd
, char *dn
,
1615 LDAPMod
**mods
, int ldap_op
,
1616 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1618 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1621 if (!newpwd
|| !dn
) {
1622 return NT_STATUS_INVALID_PARAMETER
;
1625 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1626 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1627 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1628 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1631 char *retoid
= NULL
;
1632 struct berval
*retdata
= NULL
;
1633 char *utf8_password
;
1635 size_t converted_size
;
1638 if (!ldap_state
->is_nds_ldap
) {
1640 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1641 LDAP_EXOP_MODIFY_PASSWD
)) {
1642 DEBUG(2, ("ldap password change requested, but LDAP "
1643 "server does not support it -- ignoring\n"));
1644 return NT_STATUS_OK
;
1648 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1649 pdb_get_plaintext_passwd(newpwd
),
1652 return NT_STATUS_NO_MEMORY
;
1655 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1656 TALLOC_FREE(utf8_password
);
1657 return NT_STATUS_NO_MEMORY
;
1660 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1661 DEBUG(0,("ber_alloc_t returns NULL\n"));
1662 TALLOC_FREE(utf8_password
);
1663 TALLOC_FREE(utf8_dn
);
1664 return NT_STATUS_UNSUCCESSFUL
;
1667 if ((ber_printf (ber
, "{") < 0) ||
1668 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1670 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1673 TALLOC_FREE(utf8_dn
);
1674 TALLOC_FREE(utf8_password
);
1675 return NT_STATUS_UNSUCCESSFUL
;
1678 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1679 ret
= ber_printf(ber
, "ts}",
1680 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1683 ret
= ber_printf(ber
, "}");
1687 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1690 TALLOC_FREE(utf8_dn
);
1691 TALLOC_FREE(utf8_password
);
1692 return NT_STATUS_UNSUCCESSFUL
;
1695 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1696 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1698 TALLOC_FREE(utf8_dn
);
1699 TALLOC_FREE(utf8_password
);
1700 return NT_STATUS_UNSUCCESSFUL
;
1703 TALLOC_FREE(utf8_dn
);
1704 TALLOC_FREE(utf8_password
);
1707 if (!ldap_state
->is_nds_ldap
) {
1708 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1709 LDAP_EXOP_MODIFY_PASSWD
,
1710 bv
, NULL
, NULL
, &retoid
,
1713 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1714 pdb_get_plaintext_passwd(newpwd
));
1716 if (rc
!= LDAP_SUCCESS
) {
1717 char *ld_error
= NULL
;
1719 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1720 DEBUG(3, ("Could not set userPassword "
1721 "attribute due to an objectClass "
1722 "violation -- ignoring\n"));
1724 return NT_STATUS_OK
;
1727 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1729 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1730 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1731 SAFE_FREE(ld_error
);
1733 #if defined(LDAP_CONSTRAINT_VIOLATION)
1734 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1735 return NT_STATUS_PASSWORD_RESTRICTION
;
1737 return NT_STATUS_UNSUCCESSFUL
;
1739 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1740 #ifdef DEBUG_PASSWORD
1741 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1744 ber_bvfree(retdata
);
1746 ldap_memfree(retoid
);
1752 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1753 /* may be password change below however */
1757 if (ldap_state
->is_nds_ldap
) {
1758 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1762 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1766 rc
= smbldap_add(ldap_state
->smbldap_state
,
1769 case LDAP_MOD_REPLACE
:
1770 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1774 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1776 return NT_STATUS_INVALID_PARAMETER
;
1779 if (rc
!=LDAP_SUCCESS
) {
1780 return NT_STATUS_UNSUCCESSFUL
;
1784 return NT_STATUS_OK
;
1787 /**********************************************************************
1788 Delete entry from LDAP for username.
1789 *********************************************************************/
1791 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1792 struct samu
* sam_acct
)
1794 struct ldapsam_privates
*priv
=
1795 (struct ldapsam_privates
*)my_methods
->private_data
;
1798 LDAPMessage
*msg
, *entry
;
1799 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1800 const char **attr_list
;
1801 TALLOC_CTX
*mem_ctx
;
1804 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1805 return NT_STATUS_INVALID_PARAMETER
;
1808 sname
= pdb_get_username(sam_acct
);
1810 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1813 mem_ctx
= talloc_new(NULL
);
1814 if (mem_ctx
== NULL
) {
1815 DEBUG(0, ("talloc_new failed\n"));
1819 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1820 if (attr_list
== NULL
) {
1824 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1826 if ((rc
!= LDAP_SUCCESS
) ||
1827 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1828 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1829 DEBUG(5, ("Could not find user %s\n", sname
));
1830 result
= NT_STATUS_NO_SUCH_USER
;
1834 rc
= ldapsam_delete_entry(
1835 priv
, mem_ctx
, entry
,
1836 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1837 LDAP_OBJ_SAMBASAMACCOUNT
: 0,
1840 result
= (rc
== LDAP_SUCCESS
) ?
1841 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1844 TALLOC_FREE(mem_ctx
);
1848 /**********************************************************************
1850 *********************************************************************/
1852 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1854 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1855 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1858 LDAPMessage
*result
= NULL
;
1859 LDAPMessage
*entry
= NULL
;
1860 LDAPMod
**mods
= NULL
;
1861 const char **attr_list
;
1863 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1865 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1866 if (pdb_get_username(newpwd
) == NULL
) {
1867 return NT_STATUS_INVALID_PARAMETER
;
1869 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1870 TALLOC_FREE( attr_list
);
1871 if (rc
!= LDAP_SUCCESS
) {
1872 return NT_STATUS_UNSUCCESSFUL
;
1874 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1875 my_methods
, PDB_CHANGED
);
1876 smbldap_talloc_autofree_ldapmsg(newpwd
, result
);
1879 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1880 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1881 return NT_STATUS_UNSUCCESSFUL
;
1884 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1885 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
1887 return NT_STATUS_UNSUCCESSFUL
;
1890 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1892 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1893 pdb_element_is_changed
)) {
1894 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1897 ldap_mods_free(mods
,True
);
1898 return NT_STATUS_UNSUCCESSFUL
;
1901 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1902 && (mods
== NULL
)) {
1903 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1904 pdb_get_username(newpwd
)));
1906 return NT_STATUS_OK
;
1909 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
1912 ldap_mods_free(mods
,True
);
1918 * We need to set the backend private data to NULL here. For example
1919 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1920 * same one, and with the explicit delete / add logic for attribute
1921 * values the second time we would use the wrong "old" value which
1922 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1924 * The existing LDAPMessage is still being auto-freed by the
1927 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
1930 if (!NT_STATUS_IS_OK(ret
)) {
1934 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1935 pdb_get_username(newpwd
)));
1936 return NT_STATUS_OK
;
1939 /***************************************************************************
1940 Renames a struct samu
1941 - The "rename user script" has full responsibility for changing everything
1942 ***************************************************************************/
1944 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
1945 TALLOC_CTX
*tmp_ctx
,
1947 uint32_t member_rid
);
1949 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
1950 TALLOC_CTX
*mem_ctx
,
1952 struct dom_sid
**pp_sids
,
1954 uint32_t *p_num_groups
);
1956 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1957 struct samu
*old_acct
,
1958 const char *newname
)
1960 const char *oldname
;
1962 char *rename_script
= NULL
;
1963 fstring oldname_lower
, newname_lower
;
1966 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1967 return NT_STATUS_INVALID_PARAMETER
;
1970 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1971 return NT_STATUS_INVALID_PARAMETER
;
1974 oldname
= pdb_get_username(old_acct
);
1976 /* rename the posix user */
1977 rename_script
= lp_renameuser_script(talloc_tos());
1978 if (rename_script
== NULL
) {
1979 return NT_STATUS_NO_MEMORY
;
1982 if (!(*rename_script
)) {
1983 TALLOC_FREE(rename_script
);
1984 return NT_STATUS_ACCESS_DENIED
;
1987 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1990 /* We have to allow the account name to end with a '$'.
1991 Also, follow the semantics in _samr_create_user() and lower case the
1992 posix name but preserve the case in passdb */
1994 fstrcpy( oldname_lower
, oldname
);
1995 if (!strlower_m( oldname_lower
)) {
1996 return NT_STATUS_INVALID_PARAMETER
;
1998 fstrcpy( newname_lower
, newname
);
1999 if (!strlower_m( newname_lower
)) {
2000 return NT_STATUS_INVALID_PARAMETER
;
2003 rename_script
= realloc_string_sub2(rename_script
,
2008 if (!rename_script
) {
2009 return NT_STATUS_NO_MEMORY
;
2011 rename_script
= realloc_string_sub2(rename_script
,
2016 rc
= smbrun(rename_script
, NULL
);
2018 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2019 rename_script
, rc
));
2021 TALLOC_FREE(rename_script
);
2024 smb_nscd_flush_user_cache();
2028 return NT_STATUS_UNSUCCESSFUL
;
2030 return NT_STATUS_OK
;
2033 /**********************************************************************
2034 Add struct samu to LDAP.
2035 *********************************************************************/
2037 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2039 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2040 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2042 LDAPMessage
*result
= NULL
;
2043 LDAPMessage
*entry
= NULL
;
2044 LDAPMod
**mods
= NULL
;
2045 int ldap_op
= LDAP_MOD_REPLACE
;
2046 uint32_t num_result
;
2047 const char **attr_list
;
2048 char *escape_user
= NULL
;
2049 const char *username
= pdb_get_username(newpwd
);
2050 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2051 char *filter
= NULL
;
2053 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2054 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2057 return NT_STATUS_NO_MEMORY
;
2060 if (!username
|| !*username
) {
2061 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2062 status
= NT_STATUS_INVALID_PARAMETER
;
2066 /* free this list after the second search or in case we exit on failure */
2067 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2069 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2071 if (rc
!= LDAP_SUCCESS
) {
2075 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2076 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2080 ldap_msgfree(result
);
2083 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2084 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2086 if (rc
== LDAP_SUCCESS
) {
2087 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2088 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2089 "already in the base, with samba "
2090 "attributes\n", sid_string_dbg(sid
)));
2093 ldap_msgfree(result
);
2098 /* does the entry already exist but without a samba attributes?
2099 we need to return the samba attributes here */
2101 escape_user
= escape_ldap_string(talloc_tos(), username
);
2102 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2104 status
= NT_STATUS_NO_MEMORY
;
2107 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2108 TALLOC_FREE(escape_user
);
2110 status
= NT_STATUS_NO_MEMORY
;
2114 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2115 filter
, attr_list
, &result
);
2116 if ( rc
!= LDAP_SUCCESS
) {
2120 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2122 if (num_result
> 1) {
2123 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2127 /* Check if we need to update an existing entry */
2128 if (num_result
== 1) {
2129 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2130 ldap_op
= LDAP_MOD_REPLACE
;
2131 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2132 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2134 status
= NT_STATUS_NO_MEMORY
;
2138 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2140 /* There might be a SID for this account already - say an idmap entry */
2142 filter
= talloc_asprintf(ctx
,
2143 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2144 get_userattr_key2string(ldap_state
->schema_ver
,
2145 LDAP_ATTR_USER_SID
),
2146 sid_string_talloc(ctx
, sid
),
2147 LDAP_OBJ_IDMAP_ENTRY
,
2148 LDAP_OBJ_SID_ENTRY
);
2150 status
= NT_STATUS_NO_MEMORY
;
2154 /* free old result before doing a new search */
2155 if (result
!= NULL
) {
2156 ldap_msgfree(result
);
2159 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2160 filter
, attr_list
, &result
);
2162 if ( rc
!= LDAP_SUCCESS
) {
2166 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2168 if (num_result
> 1) {
2169 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2173 /* Check if we need to update an existing entry */
2174 if (num_result
== 1) {
2176 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2177 ldap_op
= LDAP_MOD_REPLACE
;
2178 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2179 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2181 status
= NT_STATUS_NO_MEMORY
;
2187 if (num_result
== 0) {
2188 char *escape_username
;
2189 /* Check if we need to add an entry */
2190 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2191 ldap_op
= LDAP_MOD_ADD
;
2193 escape_username
= escape_rdn_val_string_alloc(username
);
2194 if (!escape_username
) {
2195 status
= NT_STATUS_NO_MEMORY
;
2199 if (username
[strlen(username
)-1] == '$') {
2200 dn
= talloc_asprintf(ctx
,
2203 lp_ldap_machine_suffix(talloc_tos()));
2205 dn
= talloc_asprintf(ctx
,
2208 lp_ldap_user_suffix(talloc_tos()));
2211 SAFE_FREE(escape_username
);
2213 status
= NT_STATUS_NO_MEMORY
;
2218 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2219 pdb_element_is_set_or_changed
)) {
2220 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2222 ldap_mods_free(mods
, true);
2228 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2231 switch ( ldap_state
->schema_ver
) {
2232 case SCHEMAVER_SAMBASAMACCOUNT
:
2233 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2236 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2240 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2241 if (!NT_STATUS_IS_OK(ret
)) {
2242 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2243 pdb_get_username(newpwd
),dn
));
2244 ldap_mods_free(mods
, true);
2248 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2249 ldap_mods_free(mods
, true);
2251 status
= NT_STATUS_OK
;
2257 ldap_msgfree(result
);
2262 /**********************************************************************
2263 *********************************************************************/
2265 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2267 LDAPMessage
** result
)
2269 int scope
= LDAP_SCOPE_SUBTREE
;
2271 const char **attr_list
;
2273 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2274 rc
= smbldap_search(ldap_state
->smbldap_state
,
2275 lp_ldap_suffix (talloc_tos()), scope
,
2276 filter
, attr_list
, 0, result
);
2277 TALLOC_FREE(attr_list
);
2282 /**********************************************************************
2283 *********************************************************************/
2285 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2286 GROUP_MAP
*map
, LDAPMessage
*entry
)
2289 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2291 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2292 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2293 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2298 temp
= smbldap_talloc_single_attribute(
2299 ldap_state
->smbldap_state
->ldap_struct
,
2301 get_attr_key2string(groupmap_attr_list
,
2302 LDAP_ATTR_GIDNUMBER
),
2305 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2306 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2310 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2312 map
->gid
= (gid_t
)atol(temp
);
2315 temp
= smbldap_talloc_single_attribute(
2316 ldap_state
->smbldap_state
->ldap_struct
,
2318 get_attr_key2string(groupmap_attr_list
,
2319 LDAP_ATTR_GROUP_SID
),
2322 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2323 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2328 if (!string_to_sid(&map
->sid
, temp
)) {
2329 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2335 temp
= smbldap_talloc_single_attribute(
2336 ldap_state
->smbldap_state
->ldap_struct
,
2338 get_attr_key2string(groupmap_attr_list
,
2339 LDAP_ATTR_GROUP_TYPE
),
2342 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2343 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2347 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2349 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2350 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2351 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2357 temp
= smbldap_talloc_single_attribute(
2358 ldap_state
->smbldap_state
->ldap_struct
,
2360 get_attr_key2string(groupmap_attr_list
,
2361 LDAP_ATTR_DISPLAY_NAME
),
2364 temp
= smbldap_talloc_single_attribute(
2365 ldap_state
->smbldap_state
->ldap_struct
,
2367 get_attr_key2string(groupmap_attr_list
,
2371 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2372 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2377 map
->nt_name
= talloc_strdup(map
, temp
);
2378 if (!map
->nt_name
) {
2384 temp
= smbldap_talloc_single_attribute(
2385 ldap_state
->smbldap_state
->ldap_struct
,
2387 get_attr_key2string(groupmap_attr_list
,
2391 temp
= talloc_strdup(ctx
, "");
2397 map
->comment
= talloc_strdup(map
, temp
);
2398 if (!map
->comment
) {
2403 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2406 id
.type
= ID_TYPE_GID
;
2408 idmap_cache_set_sid2unixid(&map
->sid
, &id
);
2415 /**********************************************************************
2416 *********************************************************************/
2418 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2422 struct ldapsam_privates
*ldap_state
=
2423 (struct ldapsam_privates
*)methods
->private_data
;
2424 LDAPMessage
*result
= NULL
;
2425 LDAPMessage
*entry
= NULL
;
2428 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2430 return NT_STATUS_NO_SUCH_GROUP
;
2433 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2436 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2438 ldap_msgfree(result
);
2439 return NT_STATUS_NO_SUCH_GROUP
;
2443 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2444 "count=%d\n", filter
, count
));
2445 ldap_msgfree(result
);
2446 return NT_STATUS_NO_SUCH_GROUP
;
2449 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2452 ldap_msgfree(result
);
2453 return NT_STATUS_UNSUCCESSFUL
;
2456 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2457 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2458 "group filter %s\n", filter
));
2459 ldap_msgfree(result
);
2460 return NT_STATUS_NO_SUCH_GROUP
;
2463 ldap_msgfree(result
);
2464 return NT_STATUS_OK
;
2467 /**********************************************************************
2468 *********************************************************************/
2470 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2473 char *filter
= NULL
;
2477 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2479 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2480 sid_to_fstring(tmp
, &sid
)) < 0) {
2481 return NT_STATUS_NO_MEMORY
;
2484 status
= ldapsam_getgroup(methods
, filter
, map
);
2489 /**********************************************************************
2490 *********************************************************************/
2492 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2495 char *filter
= NULL
;
2498 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2500 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2501 (unsigned long)gid
) < 0) {
2502 return NT_STATUS_NO_MEMORY
;
2505 status
= ldapsam_getgroup(methods
, filter
, map
);
2510 /**********************************************************************
2511 *********************************************************************/
2513 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2516 char *filter
= NULL
;
2517 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2521 return NT_STATUS_NO_MEMORY
;
2524 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2526 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2527 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2529 TALLOC_FREE(escape_name
);
2530 return NT_STATUS_NO_MEMORY
;
2533 TALLOC_FREE(escape_name
);
2534 status
= ldapsam_getgroup(methods
, filter
, map
);
2539 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2541 const struct dom_sid
*domain_sid
,
2547 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2548 str
, sizeof(str
)-1)) {
2549 DEBUG(10, ("Could not find sambaSID attribute\n"));
2553 if (!string_to_sid(&sid
, str
)) {
2554 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2558 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2559 DEBUG(10, ("SID %s is not in expected domain %s\n",
2560 str
, sid_string_dbg(domain_sid
)));
2564 if (!sid_peek_rid(&sid
, rid
)) {
2565 DEBUG(10, ("Could not peek into RID\n"));
2572 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2573 TALLOC_CTX
*mem_ctx
,
2574 const struct dom_sid
*group
,
2575 uint32_t **pp_member_rids
,
2576 size_t *p_num_members
)
2578 struct ldapsam_privates
*ldap_state
=
2579 (struct ldapsam_privates
*)methods
->private_data
;
2580 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2581 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2582 const char *sid_attrs
[] = { "sambaSID", NULL
};
2583 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2584 LDAPMessage
*result
= NULL
;
2587 char **values
= NULL
;
2592 *pp_member_rids
= NULL
;
2595 filter
= talloc_asprintf(mem_ctx
,
2596 "(&(objectClass=%s)"
2599 LDAP_OBJ_POSIXGROUP
,
2601 sid_string_talloc(mem_ctx
, group
));
2602 if (filter
== NULL
) {
2603 ret
= NT_STATUS_NO_MEMORY
;
2607 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2608 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2611 if (rc
!= LDAP_SUCCESS
)
2614 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2616 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2619 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2620 sid_string_dbg(group
)));
2621 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2626 ret
= NT_STATUS_NO_SUCH_GROUP
;
2630 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2634 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2636 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2637 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2641 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2643 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2645 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2646 if (filter
== NULL
) {
2647 ret
= NT_STATUS_NO_MEMORY
;
2651 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2652 char *escape_memberuid
;
2654 escape_memberuid
= escape_ldap_string(talloc_tos(),
2656 if (escape_memberuid
== NULL
) {
2657 ret
= NT_STATUS_NO_MEMORY
;
2661 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2662 TALLOC_FREE(escape_memberuid
);
2663 if (filter
== NULL
) {
2664 ret
= NT_STATUS_NO_MEMORY
;
2669 filter
= talloc_asprintf_append_buffer(filter
, "))");
2670 if (filter
== NULL
) {
2671 ret
= NT_STATUS_NO_MEMORY
;
2675 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2676 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2679 if (rc
!= LDAP_SUCCESS
)
2682 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2683 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2685 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2687 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2689 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2695 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2699 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2700 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2701 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2705 if (!string_to_sid(&sid
, sidstr
))
2708 if (!sid_check_is_in_our_sam(&sid
)) {
2709 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2710 "in our domain\n"));
2711 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2715 sid_peek_rid(&sid
, &rid
);
2717 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2719 ret
= NT_STATUS_NO_MEMORY
;
2725 filter
= talloc_asprintf(mem_ctx
,
2726 "(&(objectClass=%s)"
2728 LDAP_OBJ_SAMBASAMACCOUNT
,
2731 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2732 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2735 if (rc
!= LDAP_SUCCESS
)
2738 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2740 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2742 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2746 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2748 get_global_sam_sid(),
2750 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2751 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2755 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2757 ret
= NT_STATUS_NO_MEMORY
;
2767 ldap_value_free(values
);
2772 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2773 TALLOC_CTX
*mem_ctx
,
2775 struct dom_sid
**pp_sids
,
2777 uint32_t *p_num_groups
)
2779 struct ldapsam_privates
*ldap_state
=
2780 (struct ldapsam_privates
*)methods
->private_data
;
2781 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2783 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2786 LDAPMessage
*result
= NULL
;
2788 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2792 gid_t primary_gid
= -1;
2797 if (pdb_get_username(user
) == NULL
) {
2798 return NT_STATUS_INVALID_PARAMETER
;
2801 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2802 if (escape_name
== NULL
)
2803 return NT_STATUS_NO_MEMORY
;
2805 if (user
->unix_pw
) {
2806 primary_gid
= user
->unix_pw
->pw_gid
;
2808 /* retrieve the users primary gid */
2809 filter
= talloc_asprintf(mem_ctx
,
2810 "(&(objectClass=%s)(uid=%s))",
2811 LDAP_OBJ_SAMBASAMACCOUNT
,
2813 if (filter
== NULL
) {
2814 ret
= NT_STATUS_NO_MEMORY
;
2818 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2819 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2821 if (rc
!= LDAP_SUCCESS
)
2824 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2826 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2830 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2831 ret
= NT_STATUS_NO_SUCH_USER
;
2834 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2836 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2838 DEBUG (1, ("Unable to find the member's gid!\n"));
2839 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2842 primary_gid
= strtoul(gidstr
, NULL
, 10);
2845 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2846 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2851 filter
= talloc_asprintf(mem_ctx
,
2852 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2853 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2854 if (filter
== NULL
) {
2855 ret
= NT_STATUS_NO_MEMORY
;
2859 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2860 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2862 if (rc
!= LDAP_SUCCESS
)
2865 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2873 /* We need to add the primary group as the first gid/sid */
2875 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2876 ret
= NT_STATUS_NO_MEMORY
;
2880 /* This sid will be replaced later */
2882 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2884 if (!NT_STATUS_IS_OK(ret
)) {
2888 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2890 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2897 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2899 str
, sizeof(str
)-1))
2902 if (!string_to_sid(&sid
, str
))
2905 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2907 str
, sizeof(str
)-1))
2910 gid
= strtoul(str
, &end
, 10);
2912 if (PTR_DIFF(end
, str
) != strlen(str
))
2915 if (gid
== primary_gid
) {
2916 sid_copy(&(*pp_sids
)[0], &sid
);
2918 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2920 ret
= NT_STATUS_NO_MEMORY
;
2923 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2925 if (!NT_STATUS_IS_OK(ret
)) {
2931 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2932 DEBUG(3, ("primary group of [%s] not found\n",
2933 pdb_get_username(user
)));
2934 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2938 *p_num_groups
= num_sids
;
2944 TALLOC_FREE(escape_name
);
2948 /**********************************************************************
2949 * Augment a posixGroup object with a sambaGroupMapping domgroup
2950 *********************************************************************/
2952 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2953 struct ldapsam_privates
*ldap_state
,
2956 const char *filter
, *dn
;
2957 LDAPMessage
*msg
, *entry
;
2961 filter
= talloc_asprintf(mem_ctx
,
2962 "(&(objectClass=%s)(gidNumber=%u))",
2963 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
2964 if (filter
== NULL
) {
2965 return NT_STATUS_NO_MEMORY
;
2968 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2969 get_attr_list(mem_ctx
, groupmap_attr_list
),
2971 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
2973 if ((rc
!= LDAP_SUCCESS
) ||
2974 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2975 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2976 return NT_STATUS_NO_SUCH_GROUP
;
2979 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2981 return NT_STATUS_NO_MEMORY
;
2985 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
2987 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
2988 sid_string_talloc(mem_ctx
, &map
->sid
));
2989 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
2990 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
2991 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
2993 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
2995 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
2997 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2998 if (rc
!= LDAP_SUCCESS
) {
2999 return NT_STATUS_ACCESS_DENIED
;
3002 return NT_STATUS_OK
;
3005 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3008 struct ldapsam_privates
*ldap_state
=
3009 (struct ldapsam_privates
*)methods
->private_data
;
3010 LDAPMessage
*msg
= NULL
;
3011 LDAPMod
**mods
= NULL
;
3012 const char *attrs
[] = { NULL
};
3016 TALLOC_CTX
*mem_ctx
;
3023 mem_ctx
= talloc_new(NULL
);
3024 if (mem_ctx
== NULL
) {
3025 DEBUG(0, ("talloc_new failed\n"));
3026 return NT_STATUS_NO_MEMORY
;
3029 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3030 sid_string_talloc(mem_ctx
, &map
->sid
));
3031 if (filter
== NULL
) {
3032 result
= NT_STATUS_NO_MEMORY
;
3036 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3037 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3038 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3040 if ((rc
== LDAP_SUCCESS
) &&
3041 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3043 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3044 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3045 result
= NT_STATUS_GROUP_EXISTS
;
3049 switch (map
->sid_name_use
) {
3051 case SID_NAME_DOM_GRP
:
3052 /* To map a domain group we need to have a posix group
3054 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3058 case SID_NAME_ALIAS
:
3059 if (!sid_check_is_in_our_sam(&map
->sid
)
3060 && !sid_check_is_in_builtin(&map
->sid
) )
3062 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3063 sid_string_dbg(&map
->sid
)));
3064 result
= NT_STATUS_INVALID_PARAMETER
;
3070 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3071 sid_type_lookup(map
->sid_name_use
)));
3072 result
= NT_STATUS_INVALID_PARAMETER
;
3076 /* Domain groups have been mapped in a separate routine, we have to
3077 * create an alias now */
3079 if (map
->gid
== -1) {
3080 DEBUG(10, ("Refusing to map gid==-1\n"));
3081 result
= NT_STATUS_INVALID_PARAMETER
;
3085 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3086 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3087 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3088 result
= NT_STATUS_GROUP_EXISTS
;
3092 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3093 * the best we can get out of LDAP. */
3095 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3096 sid_string_talloc(mem_ctx
, &map
->sid
),
3097 lp_ldap_group_suffix(talloc_tos()));
3099 result
= NT_STATUS_NO_MEMORY
;
3105 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3106 LDAP_OBJ_SID_ENTRY
);
3107 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3109 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3110 sid_string_talloc(mem_ctx
, &map
->sid
));
3111 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3112 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3113 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3115 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3117 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3118 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3119 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3121 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3123 result
= (rc
== LDAP_SUCCESS
) ?
3124 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3127 TALLOC_FREE(mem_ctx
);
3131 /**********************************************************************
3132 * Update a group mapping entry. We're quite strict about what can be changed:
3133 * Only the description and displayname may be changed. It simply does not
3134 * make any sense to change the SID, gid or the type in a mapping.
3135 *********************************************************************/
3137 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3140 struct ldapsam_privates
*ldap_state
=
3141 (struct ldapsam_privates
*)methods
->private_data
;
3143 const char *filter
, *dn
;
3144 LDAPMessage
*msg
= NULL
;
3145 LDAPMessage
*entry
= NULL
;
3146 LDAPMod
**mods
= NULL
;
3147 TALLOC_CTX
*mem_ctx
;
3150 mem_ctx
= talloc_new(NULL
);
3151 if (mem_ctx
== NULL
) {
3152 DEBUG(0, ("talloc_new failed\n"));
3153 return NT_STATUS_NO_MEMORY
;
3156 /* Make 100% sure that sid, gid and type are not changed by looking up
3157 * exactly the values we're given in LDAP. */
3159 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3160 "(sambaSid=%s)(gidNumber=%u)"
3161 "(sambaGroupType=%d))",
3163 sid_string_talloc(mem_ctx
, &map
->sid
),
3164 (unsigned int)map
->gid
, map
->sid_name_use
);
3165 if (filter
== NULL
) {
3166 result
= NT_STATUS_NO_MEMORY
;
3170 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3171 get_attr_list(mem_ctx
, groupmap_attr_list
),
3173 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3175 if ((rc
!= LDAP_SUCCESS
) ||
3176 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3177 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3178 result
= NT_STATUS_NO_SUCH_GROUP
;
3182 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3185 result
= NT_STATUS_NO_MEMORY
;
3190 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3192 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3194 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3197 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3198 "nothing to do\n"));
3199 result
= NT_STATUS_OK
;
3203 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3205 if (rc
!= LDAP_SUCCESS
) {
3206 result
= NT_STATUS_ACCESS_DENIED
;
3210 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3211 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3213 result
= NT_STATUS_OK
;
3216 TALLOC_FREE(mem_ctx
);
3220 /**********************************************************************
3221 *********************************************************************/
3223 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3226 struct ldapsam_privates
*priv
=
3227 (struct ldapsam_privates
*)methods
->private_data
;
3228 LDAPMessage
*msg
, *entry
;
3231 TALLOC_CTX
*mem_ctx
;
3234 mem_ctx
= talloc_new(NULL
);
3235 if (mem_ctx
== NULL
) {
3236 DEBUG(0, ("talloc_new failed\n"));
3237 return NT_STATUS_NO_MEMORY
;
3240 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3241 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3242 sid_string_talloc(mem_ctx
, &sid
));
3243 if (filter
== NULL
) {
3244 result
= NT_STATUS_NO_MEMORY
;
3247 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3248 get_attr_list(mem_ctx
, groupmap_attr_list
),
3250 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3252 if ((rc
!= LDAP_SUCCESS
) ||
3253 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3254 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3255 result
= NT_STATUS_NO_SUCH_GROUP
;
3259 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3260 get_attr_list(mem_ctx
,
3261 groupmap_attr_list_to_delete
));
3263 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3264 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3265 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3266 const char *attrs
[] = { "sambaGroupType", "description",
3267 "displayName", "sambaSIDList",
3270 /* Second try. Don't delete the sambaSID attribute, this is
3271 for "old" entries that are tacked on a winbind
3274 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3275 LDAP_OBJ_GROUPMAP
, attrs
);
3278 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3279 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3280 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3281 const char *attrs
[] = { "sambaGroupType", "description",
3282 "displayName", "sambaSIDList",
3283 "gidNumber", NULL
};
3285 /* Third try. This is a post-3.0.21 alias (containing only
3286 * sambaSidEntry and sambaGroupMapping classes), we also have
3287 * to delete the gidNumber attribute, only the sambaSidEntry
3290 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3291 LDAP_OBJ_GROUPMAP
, attrs
);
3294 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3297 TALLOC_FREE(mem_ctx
);
3301 /**********************************************************************
3302 *********************************************************************/
3304 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3307 struct ldapsam_privates
*ldap_state
=
3308 (struct ldapsam_privates
*)my_methods
->private_data
;
3309 char *filter
= NULL
;
3311 const char **attr_list
;
3313 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3315 return NT_STATUS_NO_MEMORY
;
3317 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3318 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3319 LDAP_SCOPE_SUBTREE
, filter
,
3320 attr_list
, 0, &ldap_state
->result
);
3321 TALLOC_FREE(attr_list
);
3323 if (rc
!= LDAP_SUCCESS
) {
3324 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3325 ldap_err2string(rc
)));
3326 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3327 lp_ldap_suffix(talloc_tos()), filter
));
3328 ldap_msgfree(ldap_state
->result
);
3329 ldap_state
->result
= NULL
;
3330 TALLOC_FREE(filter
);
3331 return NT_STATUS_UNSUCCESSFUL
;
3334 TALLOC_FREE(filter
);
3336 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3337 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3338 ldap_state
->result
)));
3341 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3342 ldap_state
->result
);
3343 ldap_state
->index
= 0;
3345 return NT_STATUS_OK
;
3348 /**********************************************************************
3349 *********************************************************************/
3351 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3353 ldapsam_endsampwent(my_methods
);
3356 /**********************************************************************
3357 *********************************************************************/
3359 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3362 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3363 struct ldapsam_privates
*ldap_state
=
3364 (struct ldapsam_privates
*)my_methods
->private_data
;
3368 if (!ldap_state
->entry
)
3371 ldap_state
->index
++;
3372 bret
= init_group_from_ldap(ldap_state
, map
,
3376 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3380 return NT_STATUS_OK
;
3383 /**********************************************************************
3384 *********************************************************************/
3386 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3387 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3388 GROUP_MAP
***pp_rmap
,
3389 size_t *p_num_entries
,
3392 GROUP_MAP
*map
= NULL
;
3398 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3399 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3401 return NT_STATUS_ACCESS_DENIED
;
3406 map
= talloc_zero(NULL
, GROUP_MAP
);
3408 return NT_STATUS_NO_MEMORY
;
3411 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3416 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3417 sid_name_use
!= map
->sid_name_use
) {
3418 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3419 "not of the requested type\n",
3423 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3424 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3425 "non mapped\n", map
->nt_name
));
3429 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3430 GROUP_MAP
*, entries
+ 1);
3432 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3433 "enlarge group map!\n"));
3434 return NT_STATUS_UNSUCCESSFUL
;
3437 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3442 ldapsam_endsamgrent(methods
);
3444 *p_num_entries
= entries
;
3446 return NT_STATUS_OK
;
3449 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3450 const struct dom_sid
*alias
,
3451 const struct dom_sid
*member
,
3454 struct ldapsam_privates
*ldap_state
=
3455 (struct ldapsam_privates
*)methods
->private_data
;
3457 LDAPMessage
*result
= NULL
;
3458 LDAPMessage
*entry
= NULL
;
3460 LDAPMod
**mods
= NULL
;
3462 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3465 char *filter
= NULL
;
3467 if (sid_check_is_in_builtin(alias
)) {
3468 type
= SID_NAME_ALIAS
;
3471 if (sid_check_is_in_our_sam(alias
)) {
3472 type
= SID_NAME_ALIAS
;
3475 if (type
== SID_NAME_USE_NONE
) {
3476 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3477 sid_string_dbg(alias
)));
3478 return NT_STATUS_NO_SUCH_ALIAS
;
3481 if (asprintf(&filter
,
3482 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3483 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3485 return NT_STATUS_NO_MEMORY
;
3488 if (ldapsam_search_one_group(ldap_state
, filter
,
3489 &result
) != LDAP_SUCCESS
) {
3491 return NT_STATUS_NO_SUCH_ALIAS
;
3494 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3498 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3499 ldap_msgfree(result
);
3501 return NT_STATUS_NO_SUCH_ALIAS
;
3505 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3506 "filter %s: count=%d\n", filter
, count
));
3507 ldap_msgfree(result
);
3509 return NT_STATUS_NO_SUCH_ALIAS
;
3514 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3518 ldap_msgfree(result
);
3519 return NT_STATUS_UNSUCCESSFUL
;
3522 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3524 ldap_msgfree(result
);
3525 return NT_STATUS_UNSUCCESSFUL
;
3528 smbldap_set_mod(&mods
, modop
,
3529 get_attr_key2string(groupmap_attr_list
,
3530 LDAP_ATTR_SID_LIST
),
3531 sid_to_fstring(tmp
, member
));
3533 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3535 ldap_mods_free(mods
, True
);
3536 ldap_msgfree(result
);
3539 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3540 return NT_STATUS_MEMBER_IN_ALIAS
;
3543 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3544 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3547 if (rc
!= LDAP_SUCCESS
) {
3548 return NT_STATUS_UNSUCCESSFUL
;
3551 return NT_STATUS_OK
;
3554 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3555 const struct dom_sid
*alias
,
3556 const struct dom_sid
*member
)
3558 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3561 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3562 const struct dom_sid
*alias
,
3563 const struct dom_sid
*member
)
3565 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3569 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3570 const struct dom_sid
*alias
,
3571 TALLOC_CTX
*mem_ctx
,
3572 struct dom_sid
**pp_members
,
3573 size_t *p_num_members
)
3575 struct ldapsam_privates
*ldap_state
=
3576 (struct ldapsam_privates
*)methods
->private_data
;
3577 LDAPMessage
*result
= NULL
;
3578 LDAPMessage
*entry
= NULL
;
3580 char **values
= NULL
;
3582 char *filter
= NULL
;
3583 uint32_t num_members
= 0;
3584 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3590 if (sid_check_is_in_builtin(alias
)) {
3591 type
= SID_NAME_ALIAS
;
3594 if (sid_check_is_in_our_sam(alias
)) {
3595 type
= SID_NAME_ALIAS
;
3598 if (type
== SID_NAME_USE_NONE
) {
3599 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3600 sid_string_dbg(alias
)));
3601 return NT_STATUS_NO_SUCH_ALIAS
;
3604 if (asprintf(&filter
,
3605 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3606 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3608 return NT_STATUS_NO_MEMORY
;
3611 if (ldapsam_search_one_group(ldap_state
, filter
,
3612 &result
) != LDAP_SUCCESS
) {
3614 return NT_STATUS_NO_SUCH_ALIAS
;
3617 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3621 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3622 ldap_msgfree(result
);
3624 return NT_STATUS_NO_SUCH_ALIAS
;
3628 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3629 "filter %s: count=%d\n", filter
, count
));
3630 ldap_msgfree(result
);
3632 return NT_STATUS_NO_SUCH_ALIAS
;
3637 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3641 ldap_msgfree(result
);
3642 return NT_STATUS_UNSUCCESSFUL
;
3645 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3647 get_attr_key2string(groupmap_attr_list
,
3648 LDAP_ATTR_SID_LIST
));
3650 if (values
== NULL
) {
3651 ldap_msgfree(result
);
3652 return NT_STATUS_OK
;
3655 count
= ldap_count_values(values
);
3657 for (i
=0; i
<count
; i
++) {
3658 struct dom_sid member
;
3661 if (!string_to_sid(&member
, values
[i
]))
3664 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3666 if (!NT_STATUS_IS_OK(status
)) {
3667 ldap_value_free(values
);
3668 ldap_msgfree(result
);
3673 *p_num_members
= num_members
;
3674 ldap_value_free(values
);
3675 ldap_msgfree(result
);
3677 return NT_STATUS_OK
;
3680 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3681 TALLOC_CTX
*mem_ctx
,
3682 const struct dom_sid
*domain_sid
,
3683 const struct dom_sid
*members
,
3685 uint32_t **pp_alias_rids
,
3686 size_t *p_num_alias_rids
)
3688 struct ldapsam_privates
*ldap_state
=
3689 (struct ldapsam_privates
*)methods
->private_data
;
3692 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3694 LDAPMessage
*result
= NULL
;
3695 LDAPMessage
*entry
= NULL
;
3699 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3700 bool is_builtin
= false;
3701 bool sid_added
= false;
3703 *pp_alias_rids
= NULL
;
3704 *p_num_alias_rids
= 0;
3706 if (sid_check_is_builtin(domain_sid
)) {
3708 type
= SID_NAME_ALIAS
;
3711 if (sid_check_is_our_sam(domain_sid
)) {
3712 type
= SID_NAME_ALIAS
;
3715 if (type
== SID_NAME_USE_NONE
) {
3716 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3717 sid_string_dbg(domain_sid
)));
3718 return NT_STATUS_UNSUCCESSFUL
;
3721 if (num_members
== 0) {
3722 return NT_STATUS_OK
;
3725 filter
= talloc_asprintf(mem_ctx
,
3726 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3727 LDAP_OBJ_GROUPMAP
, type
);
3729 for (i
=0; i
<num_members
; i
++)
3730 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3732 sid_string_talloc(mem_ctx
,
3735 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3737 if (filter
== NULL
) {
3738 return NT_STATUS_NO_MEMORY
;
3742 ldap_state
->search_cache
.filter
&&
3743 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3744 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3745 result
= ldap_state
->search_cache
.result
;
3746 ldap_state
->search_cache
.result
= NULL
;
3748 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3749 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3750 if (rc
!= LDAP_SUCCESS
) {
3751 return NT_STATUS_UNSUCCESSFUL
;
3753 smbldap_talloc_autofree_ldapmsg(filter
, result
);
3756 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3758 for (entry
= ldap_first_entry(ldap_struct
, result
);
3760 entry
= ldap_next_entry(ldap_struct
, entry
))
3766 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3772 if (!string_to_sid(&sid
, sid_str
))
3775 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3780 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3781 p_num_alias_rids
)) {
3782 return NT_STATUS_NO_MEMORY
;
3786 if (!is_builtin
&& !sid_added
) {
3787 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3789 * Note: result is a talloc child of filter because of the
3790 * smbldap_talloc_autofree_ldapmsg() usage
3792 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3793 ldap_state
->search_cache
.result
= result
;
3796 return NT_STATUS_OK
;
3799 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3800 enum pdb_policy_type type
,
3803 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3805 LDAPMod
**mods
= NULL
;
3806 fstring value_string
;
3807 const char *policy_attr
= NULL
;
3809 struct ldapsam_privates
*ldap_state
=
3810 (struct ldapsam_privates
*)methods
->private_data
;
3812 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3814 if (!ldap_state
->domain_dn
) {
3815 return NT_STATUS_INVALID_PARAMETER
;
3818 policy_attr
= get_account_policy_attr(type
);
3819 if (policy_attr
== NULL
) {
3820 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3825 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3827 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3829 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3832 ldap_mods_free(mods
, True
);
3834 if (rc
!= LDAP_SUCCESS
) {
3838 if (!cache_account_policy_set(type
, value
)) {
3839 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3840 "update local tdb cache\n"));
3844 return NT_STATUS_OK
;
3847 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3848 enum pdb_policy_type type
,
3851 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3855 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3856 enum pdb_policy_type type
,
3859 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3860 LDAPMessage
*result
= NULL
;
3861 LDAPMessage
*entry
= NULL
;
3866 const char *policy_attr
= NULL
;
3868 struct ldapsam_privates
*ldap_state
=
3869 (struct ldapsam_privates
*)methods
->private_data
;
3871 const char *attrs
[2];
3873 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3875 if (!ldap_state
->domain_dn
) {
3876 return NT_STATUS_INVALID_PARAMETER
;
3879 policy_attr
= get_account_policy_attr(type
);
3881 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3882 "policy index: %d\n", type
));
3886 attrs
[0] = policy_attr
;
3889 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3890 if (filter
== NULL
) {
3891 return NT_STATUS_NO_MEMORY
;
3893 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3894 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
3896 TALLOC_FREE(filter
);
3897 if (rc
!= LDAP_SUCCESS
) {
3901 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3906 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3907 if (entry
== NULL
) {
3911 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3916 *value
= (uint32_t)atol(vals
[0]);
3918 ntstatus
= NT_STATUS_OK
;
3922 ldap_value_free(vals
);
3923 ldap_msgfree(result
);
3928 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3930 - if user hasn't decided to use account policies inside LDAP just reuse the
3933 - if there is a valid cache entry, return that
3934 - if there is an LDAP entry, update cache and return
3935 - otherwise set to default, update cache and return
3939 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3940 enum pdb_policy_type type
,
3943 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3945 if (cache_account_policy_get(type
, value
)) {
3946 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3948 return NT_STATUS_OK
;
3951 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
3953 if (NT_STATUS_IS_OK(ntstatus
)) {
3957 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3961 /* should we automagically migrate old tdb value here ? */
3962 if (account_policy_get(type
, value
))
3965 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3966 "default\n", type
));
3969 if (!account_policy_get_default(type
, value
)) {
3975 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
3976 if (!NT_STATUS_IS_OK(ntstatus
)) {
3982 if (!cache_account_policy_set(type
, *value
)) {
3983 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3984 "tdb as a cache\n"));
3985 return NT_STATUS_UNSUCCESSFUL
;
3988 return NT_STATUS_OK
;
3991 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3992 const struct dom_sid
*domain_sid
,
3996 enum lsa_SidType
*attrs
)
3998 struct ldapsam_privates
*ldap_state
=
3999 (struct ldapsam_privates
*)methods
->private_data
;
4000 LDAPMessage
*msg
= NULL
;
4002 char *allsids
= NULL
;
4003 int i
, rc
, num_mapped
;
4004 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4005 TALLOC_CTX
*mem_ctx
;
4009 mem_ctx
= talloc_new(NULL
);
4010 if (mem_ctx
== NULL
) {
4011 DEBUG(0, ("talloc_new failed\n"));
4015 if (!sid_check_is_builtin(domain_sid
) &&
4016 !sid_check_is_our_sam(domain_sid
)) {
4017 result
= NT_STATUS_INVALID_PARAMETER
;
4021 if (num_rids
== 0) {
4022 result
= NT_STATUS_NONE_MAPPED
;
4026 for (i
=0; i
<num_rids
; i
++)
4027 attrs
[i
] = SID_NAME_UNKNOWN
;
4029 allsids
= talloc_strdup(mem_ctx
, "");
4030 if (allsids
== NULL
) {
4034 for (i
=0; i
<num_rids
; i
++) {
4036 sid_compose(&sid
, domain_sid
, rids
[i
]);
4037 allsids
= talloc_asprintf_append_buffer(
4038 allsids
, "(sambaSid=%s)",
4039 sid_string_talloc(mem_ctx
, &sid
));
4040 if (allsids
== NULL
) {
4045 /* First look for users */
4049 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4051 filter
= talloc_asprintf(
4052 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4053 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4055 if (filter
== NULL
) {
4059 rc
= smbldap_search(ldap_state
->smbldap_state
,
4060 lp_ldap_user_suffix(talloc_tos()),
4061 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4063 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4066 if (rc
!= LDAP_SUCCESS
)
4069 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4072 for (entry
= ldap_first_entry(ld
, msg
);
4074 entry
= ldap_next_entry(ld
, entry
)) {
4079 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4081 DEBUG(2, ("Could not find sid from ldap entry\n"));
4085 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4088 DEBUG(2, ("Could not retrieve uid attribute\n"));
4092 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4093 if (rid
== rids
[rid_index
])
4097 if (rid_index
== num_rids
) {
4098 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4102 attrs
[rid_index
] = SID_NAME_USER
;
4103 names
[rid_index
] = name
;
4107 if (num_mapped
== num_rids
) {
4108 /* No need to look for groups anymore -- we're done */
4109 result
= NT_STATUS_OK
;
4113 /* Same game for groups */
4117 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4118 "sambaGroupType", NULL
};
4120 filter
= talloc_asprintf(
4121 mem_ctx
, "(&(objectClass=%s)(|%s))",
4122 LDAP_OBJ_GROUPMAP
, allsids
);
4123 if (filter
== NULL
) {
4127 rc
= smbldap_search(ldap_state
->smbldap_state
,
4128 lp_ldap_suffix(talloc_tos()),
4129 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4131 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4134 if (rc
!= LDAP_SUCCESS
)
4137 /* ldap_struct might have changed due to a reconnect */
4139 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4141 /* For consistency checks, we already checked we're only domain or builtin */
4143 is_builtin
= sid_check_is_builtin(domain_sid
);
4145 for (entry
= ldap_first_entry(ld
, msg
);
4147 entry
= ldap_next_entry(ld
, entry
))
4152 enum lsa_SidType type
;
4153 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4155 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4158 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4163 type
= (enum lsa_SidType
)atol(attr
);
4165 /* Consistency checks */
4166 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4167 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4168 (type
!= SID_NAME_DOM_GRP
)))) {
4169 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4172 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4174 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4178 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4181 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4183 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4187 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4192 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4193 if (rid
== rids
[rid_index
])
4197 if (rid_index
== num_rids
) {
4198 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4202 attrs
[rid_index
] = type
;
4203 names
[rid_index
] = attr
;
4207 result
= NT_STATUS_NONE_MAPPED
;
4210 result
= (num_mapped
== num_rids
) ?
4211 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4213 TALLOC_FREE(mem_ctx
);
4217 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4219 char *filter
= NULL
;
4220 char *escaped
= NULL
;
4221 char *result
= NULL
;
4223 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4224 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4228 escaped
= escape_ldap_string(talloc_tos(), username
);
4229 if (escaped
== NULL
) goto done
;
4231 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4235 TALLOC_FREE(escaped
);
4240 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4244 const char **result
;
4246 va_start(ap
, mem_ctx
);
4247 while (va_arg(ap
, const char *) != NULL
)
4251 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4255 va_start(ap
, mem_ctx
);
4256 for (i
=0; i
<num
; i
++) {
4257 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4258 if (result
[i
] == NULL
) {
4259 talloc_free(result
);
4270 struct ldap_search_state
{
4271 struct smbldap_state
*connection
;
4273 uint32_t acct_flags
;
4274 uint16_t group_type
;
4281 void *pagedresults_cookie
;
4283 LDAPMessage
*entries
, *current_entry
;
4284 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4285 TALLOC_CTX
*mem_ctx
,
4286 LDAP
*ld
, LDAPMessage
*entry
,
4287 struct samr_displayentry
*result
);
4290 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4292 struct ldap_search_state
*state
=
4293 (struct ldap_search_state
*)search
->private_data
;
4295 int rc
= LDAP_OPERATIONS_ERROR
;
4297 state
->entries
= NULL
;
4299 if (state
->connection
->paged_results
) {
4300 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4301 state
->scope
, state
->filter
,
4302 state
->attrs
, state
->attrsonly
,
4303 lp_ldap_page_size(), &state
->entries
,
4304 &state
->pagedresults_cookie
);
4307 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4309 if (state
->entries
!= NULL
) {
4310 /* Left over from unsuccessful paged attempt */
4311 ldap_msgfree(state
->entries
);
4312 state
->entries
= NULL
;
4315 rc
= smbldap_search(state
->connection
, state
->base
,
4316 state
->scope
, state
->filter
, state
->attrs
,
4317 state
->attrsonly
, &state
->entries
);
4319 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4322 /* Ok, the server was lying. It told us it could do paged
4323 * searches when it could not. */
4324 state
->connection
->paged_results
= False
;
4327 ld
= state
->connection
->ldap_struct
;
4329 DEBUG(5, ("Don't have an LDAP connection right after a "
4333 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4338 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4340 struct ldap_search_state
*state
=
4341 (struct ldap_search_state
*)search
->private_data
;
4344 if (!state
->connection
->paged_results
) {
4345 /* There is no next page when there are no paged results */
4349 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4350 state
->scope
, state
->filter
, state
->attrs
,
4351 state
->attrsonly
, lp_ldap_page_size(),
4353 &state
->pagedresults_cookie
);
4355 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4358 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4360 if (state
->current_entry
== NULL
) {
4361 ldap_msgfree(state
->entries
);
4362 state
->entries
= NULL
;
4369 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4370 struct samr_displayentry
*entry
)
4372 struct ldap_search_state
*state
=
4373 (struct ldap_search_state
*)search
->private_data
;
4377 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4380 if ((state
->entries
== NULL
) &&
4381 !ldapsam_search_nextpage(search
))
4384 if (state
->current_entry
== NULL
) {
4388 result
= state
->ldap2displayentry(state
, search
,
4389 state
->connection
->ldap_struct
,
4390 state
->current_entry
, entry
);
4394 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4395 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4396 if (dn
!= NULL
) ldap_memfree(dn
);
4399 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4401 if (state
->current_entry
== NULL
) {
4402 ldap_msgfree(state
->entries
);
4403 state
->entries
= NULL
;
4406 if (!result
) goto retry
;
4411 static void ldapsam_search_end(struct pdb_search
*search
)
4413 struct ldap_search_state
*state
=
4414 (struct ldap_search_state
*)search
->private_data
;
4417 if (state
->pagedresults_cookie
== NULL
)
4420 if (state
->entries
!= NULL
)
4421 ldap_msgfree(state
->entries
);
4423 state
->entries
= NULL
;
4424 state
->current_entry
= NULL
;
4426 if (!state
->connection
->paged_results
)
4429 /* Tell the LDAP server we're not interested in the rest anymore. */
4431 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4432 state
->filter
, state
->attrs
,
4433 state
->attrsonly
, 0, &state
->entries
,
4434 &state
->pagedresults_cookie
);
4436 if (rc
!= LDAP_SUCCESS
)
4437 DEBUG(5, ("Could not end search properly\n"));
4442 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4443 TALLOC_CTX
*mem_ctx
,
4444 LDAP
*ld
, LDAPMessage
*entry
,
4445 struct samr_displayentry
*result
)
4448 size_t converted_size
;
4450 uint32_t acct_flags
;
4452 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4453 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4454 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4457 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4458 ldap_value_free(vals
);
4460 if ((state
->acct_flags
!= 0) &&
4461 ((state
->acct_flags
& acct_flags
) == 0))
4464 result
->acct_flags
= acct_flags
;
4465 result
->account_name
= "";
4466 result
->fullname
= "";
4467 result
->description
= "";
4469 vals
= ldap_get_values(ld
, entry
, "uid");
4470 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4471 DEBUG(5, ("\"uid\" not found\n"));
4474 if (!pull_utf8_talloc(mem_ctx
,
4475 discard_const_p(char *, &result
->account_name
),
4476 vals
[0], &converted_size
))
4478 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4482 ldap_value_free(vals
);
4484 vals
= ldap_get_values(ld
, entry
, "displayName");
4485 if ((vals
== NULL
) || (vals
[0] == NULL
))
4486 DEBUG(8, ("\"displayName\" not found\n"));
4487 else if (!pull_utf8_talloc(mem_ctx
,
4488 discard_const_p(char *, &result
->fullname
),
4489 vals
[0], &converted_size
))
4491 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4495 ldap_value_free(vals
);
4497 vals
= ldap_get_values(ld
, entry
, "description");
4498 if ((vals
== NULL
) || (vals
[0] == NULL
))
4499 DEBUG(8, ("\"description\" not found\n"));
4500 else if (!pull_utf8_talloc(mem_ctx
,
4501 discard_const_p(char *, &result
->description
),
4502 vals
[0], &converted_size
))
4504 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4508 ldap_value_free(vals
);
4510 if ((result
->account_name
== NULL
) ||
4511 (result
->fullname
== NULL
) ||
4512 (result
->description
== NULL
)) {
4513 DEBUG(0, ("talloc failed\n"));
4517 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4518 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4519 DEBUG(0, ("\"objectSid\" not found\n"));
4523 if (!string_to_sid(&sid
, vals
[0])) {
4524 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4525 ldap_value_free(vals
);
4528 ldap_value_free(vals
);
4530 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4531 DEBUG(0, ("sid %s does not belong to our domain\n",
4532 sid_string_dbg(&sid
)));
4540 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4541 struct pdb_search
*search
,
4542 uint32_t acct_flags
)
4544 struct ldapsam_privates
*ldap_state
=
4545 (struct ldapsam_privates
*)methods
->private_data
;
4546 struct ldap_search_state
*state
;
4548 state
= talloc(search
, struct ldap_search_state
);
4549 if (state
== NULL
) {
4550 DEBUG(0, ("talloc failed\n"));
4554 state
->connection
= ldap_state
->smbldap_state
;
4556 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4557 state
->base
= lp_ldap_user_suffix(talloc_tos());
4558 else if ((acct_flags
!= 0) &&
4559 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4560 state
->base
= lp_ldap_machine_suffix(talloc_tos());
4562 state
->base
= lp_ldap_suffix(talloc_tos());
4564 state
->acct_flags
= acct_flags
;
4565 state
->base
= talloc_strdup(search
, state
->base
);
4566 state
->scope
= LDAP_SCOPE_SUBTREE
;
4567 state
->filter
= get_ldap_filter(search
, "*");
4568 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4569 "displayName", "description",
4570 "sambaAcctFlags", NULL
);
4571 state
->attrsonly
= 0;
4572 state
->pagedresults_cookie
= NULL
;
4573 state
->entries
= NULL
;
4574 state
->ldap2displayentry
= ldapuser2displayentry
;
4576 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4577 DEBUG(0, ("talloc failed\n"));
4581 search
->private_data
= state
;
4582 search
->next_entry
= ldapsam_search_next_entry
;
4583 search
->search_end
= ldapsam_search_end
;
4585 return ldapsam_search_firstpage(search
);
4588 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4589 TALLOC_CTX
*mem_ctx
,
4590 LDAP
*ld
, LDAPMessage
*entry
,
4591 struct samr_displayentry
*result
)
4594 size_t converted_size
;
4596 uint16_t group_type
;
4598 result
->account_name
= "";
4599 result
->fullname
= "";
4600 result
->description
= "";
4603 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4604 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4605 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4607 ldap_value_free(vals
);
4612 group_type
= atoi(vals
[0]);
4614 if ((state
->group_type
!= 0) &&
4615 ((state
->group_type
!= group_type
))) {
4616 ldap_value_free(vals
);
4620 ldap_value_free(vals
);
4622 /* display name is the NT group name */
4624 vals
= ldap_get_values(ld
, entry
, "displayName");
4625 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4626 DEBUG(8, ("\"displayName\" not found\n"));
4628 /* fallback to the 'cn' attribute */
4629 vals
= ldap_get_values(ld
, entry
, "cn");
4630 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4631 DEBUG(5, ("\"cn\" not found\n"));
4634 if (!pull_utf8_talloc(mem_ctx
,
4635 discard_const_p(char *,
4636 &result
->account_name
),
4637 vals
[0], &converted_size
))
4639 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4640 "failed: %s", strerror(errno
)));
4643 else if (!pull_utf8_talloc(mem_ctx
,
4644 discard_const_p(char *,
4645 &result
->account_name
),
4646 vals
[0], &converted_size
))
4648 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4652 ldap_value_free(vals
);
4654 vals
= ldap_get_values(ld
, entry
, "description");
4655 if ((vals
== NULL
) || (vals
[0] == NULL
))
4656 DEBUG(8, ("\"description\" not found\n"));
4657 else if (!pull_utf8_talloc(mem_ctx
,
4658 discard_const_p(char *, &result
->description
),
4659 vals
[0], &converted_size
))
4661 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4664 ldap_value_free(vals
);
4666 if ((result
->account_name
== NULL
) ||
4667 (result
->fullname
== NULL
) ||
4668 (result
->description
== NULL
)) {
4669 DEBUG(0, ("talloc failed\n"));
4673 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4674 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4675 DEBUG(0, ("\"objectSid\" not found\n"));
4677 ldap_value_free(vals
);
4682 if (!string_to_sid(&sid
, vals
[0])) {
4683 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4687 ldap_value_free(vals
);
4689 switch (group_type
) {
4690 case SID_NAME_DOM_GRP
:
4691 case SID_NAME_ALIAS
:
4693 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4694 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4696 DEBUG(0, ("%s is not in our domain\n",
4697 sid_string_dbg(&sid
)));
4703 DEBUG(0,("unknown group type: %d\n", group_type
));
4707 result
->acct_flags
= 0;
4712 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4713 struct pdb_search
*search
,
4714 const struct dom_sid
*sid
,
4715 enum lsa_SidType type
)
4717 struct ldapsam_privates
*ldap_state
=
4718 (struct ldapsam_privates
*)methods
->private_data
;
4719 struct ldap_search_state
*state
;
4722 state
= talloc(search
, struct ldap_search_state
);
4723 if (state
== NULL
) {
4724 DEBUG(0, ("talloc failed\n"));
4728 state
->connection
= ldap_state
->smbldap_state
;
4730 state
->base
= lp_ldap_suffix(search
);
4731 state
->connection
= ldap_state
->smbldap_state
;
4732 state
->scope
= LDAP_SCOPE_SUBTREE
;
4733 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4734 "(sambaGroupType=%d)(sambaSID=%s*))",
4736 type
, sid_to_fstring(tmp
, sid
));
4737 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4738 "displayName", "description",
4739 "sambaGroupType", NULL
);
4740 state
->attrsonly
= 0;
4741 state
->pagedresults_cookie
= NULL
;
4742 state
->entries
= NULL
;
4743 state
->group_type
= type
;
4744 state
->ldap2displayentry
= ldapgroup2displayentry
;
4746 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4747 DEBUG(0, ("talloc failed\n"));
4751 search
->private_data
= state
;
4752 search
->next_entry
= ldapsam_search_next_entry
;
4753 search
->search_end
= ldapsam_search_end
;
4755 return ldapsam_search_firstpage(search
);
4758 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4759 struct pdb_search
*search
)
4761 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4764 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4765 struct pdb_search
*search
,
4766 const struct dom_sid
*sid
)
4768 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4771 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4773 return PDB_CAP_STORE_RIDS
;
4776 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4779 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4781 LDAPMessage
*result
= NULL
;
4782 LDAPMessage
*entry
= NULL
;
4783 LDAPMod
**mods
= NULL
;
4787 uint32_t nextRid
= 0;
4790 TALLOC_CTX
*mem_ctx
;
4792 mem_ctx
= talloc_new(NULL
);
4793 if (mem_ctx
== NULL
) {
4794 DEBUG(0, ("talloc_new failed\n"));
4795 return NT_STATUS_NO_MEMORY
;
4798 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4799 get_global_sam_name(), False
);
4800 if (!NT_STATUS_IS_OK(status
)) {
4801 DEBUG(3, ("Could not get domain info: %s\n",
4802 nt_errstr(status
)));
4806 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4808 entry
= ldap_first_entry(priv2ld(priv
), result
);
4809 if (entry
== NULL
) {
4810 DEBUG(0, ("Could not get domain info entry\n"));
4811 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4815 /* Find the largest of the three attributes "sambaNextRid",
4816 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4817 concept of differentiating between user and group rids, and will
4818 use only "sambaNextRid" in the future. But for compatibility
4819 reasons I look if others have chosen different strategies -- VL */
4821 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4822 "sambaNextRid", mem_ctx
);
4823 if (value
!= NULL
) {
4824 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4825 nextRid
= MAX(nextRid
, tmp
);
4828 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4829 "sambaNextUserRid", mem_ctx
);
4830 if (value
!= NULL
) {
4831 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4832 nextRid
= MAX(nextRid
, tmp
);
4835 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4836 "sambaNextGroupRid", mem_ctx
);
4837 if (value
!= NULL
) {
4838 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4839 nextRid
= MAX(nextRid
, tmp
);
4843 nextRid
= BASE_RID
-1;
4848 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4849 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4850 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
4852 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4853 status
= NT_STATUS_NO_MEMORY
;
4857 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4859 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4862 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4865 if (NT_STATUS_IS_OK(status
)) {
4869 TALLOC_FREE(mem_ctx
);
4873 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4877 for (i
=0; i
<10; i
++) {
4878 NTSTATUS result
= ldapsam_get_new_rid(
4879 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4880 if (NT_STATUS_IS_OK(result
)) {
4884 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4888 /* The ldap update failed (maybe a race condition), retry */
4891 /* Tried 10 times, fail. */
4892 return NT_STATUS_ACCESS_DENIED
;
4895 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
4897 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4898 return NT_STATUS_IS_OK(result
) ? True
: False
;
4901 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4902 const struct dom_sid
*sid
,
4905 struct ldapsam_privates
*priv
=
4906 (struct ldapsam_privates
*)methods
->private_data
;
4908 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4910 LDAPMessage
*result
= NULL
;
4911 LDAPMessage
*entry
= NULL
;
4916 TALLOC_CTX
*mem_ctx
;
4918 if (!sid_check_is_in_our_sam(sid
)) {
4923 mem_ctx
= talloc_new(NULL
);
4924 if (mem_ctx
== NULL
) {
4925 DEBUG(0, ("talloc_new failed\n"));
4929 filter
= talloc_asprintf(mem_ctx
,
4931 "(|(objectClass=%s)(objectClass=%s)))",
4932 sid_string_talloc(mem_ctx
, sid
),
4933 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4934 if (filter
== NULL
) {
4935 DEBUG(5, ("talloc_asprintf failed\n"));
4939 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4941 if (rc
!= LDAP_SUCCESS
) {
4944 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4946 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4947 DEBUG(10, ("Got %d entries, expected one\n",
4948 ldap_count_entries(priv2ld(priv
), result
)));
4952 entry
= ldap_first_entry(priv2ld(priv
), result
);
4954 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4955 "sambaGroupType", mem_ctx
);
4957 if (value
!= NULL
) {
4958 const char *gid_str
;
4961 gid_str
= smbldap_talloc_single_attribute(
4962 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4963 if (gid_str
== NULL
) {
4964 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4965 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4970 id
->id
= strtoul(gid_str
, NULL
, 10);
4971 id
->type
= ID_TYPE_GID
;
4972 idmap_cache_set_sid2unixid(sid
, id
);
4977 /* It must be a user */
4979 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4980 "uidNumber", mem_ctx
);
4981 if (value
== NULL
) {
4982 DEBUG(1, ("Could not find uidNumber in %s\n",
4983 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4987 id
->id
= strtoul(value
, NULL
, 10);
4988 id
->type
= ID_TYPE_UID
;
4989 idmap_cache_set_sid2unixid(sid
, id
);
4993 TALLOC_FREE(mem_ctx
);
4998 * Find the SID for a uid.
4999 * This is shortcut is only used if ldapsam:trusted is set to true.
5001 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5002 struct dom_sid
*sid
)
5004 struct ldapsam_privates
*priv
=
5005 (struct ldapsam_privates
*)methods
->private_data
;
5007 const char *attrs
[] = { "sambaSID", NULL
};
5008 LDAPMessage
*result
= NULL
;
5009 LDAPMessage
*entry
= NULL
;
5011 char *user_sid_string
;
5012 struct dom_sid user_sid
;
5014 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5017 filter
= talloc_asprintf(tmp_ctx
,
5020 "(objectClass=%s))",
5022 LDAP_OBJ_POSIXACCOUNT
,
5023 LDAP_OBJ_SAMBASAMACCOUNT
);
5024 if (filter
== NULL
) {
5025 DEBUG(3, ("talloc_asprintf failed\n"));
5029 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5030 if (rc
!= LDAP_SUCCESS
) {
5033 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5035 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5036 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5037 ldap_count_entries(priv2ld(priv
), result
),
5038 (unsigned int)uid
));
5042 entry
= ldap_first_entry(priv2ld(priv
), result
);
5044 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5045 "sambaSID", tmp_ctx
);
5046 if (user_sid_string
== NULL
) {
5047 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5048 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5052 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5053 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5058 sid_copy(sid
, &user_sid
);
5061 id
.type
= ID_TYPE_UID
;
5063 idmap_cache_set_sid2unixid(sid
, &id
);
5068 TALLOC_FREE(tmp_ctx
);
5073 * Find the SID for a gid.
5074 * This is shortcut is only used if ldapsam:trusted is set to true.
5076 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5077 struct dom_sid
*sid
)
5079 struct ldapsam_privates
*priv
=
5080 (struct ldapsam_privates
*)methods
->private_data
;
5082 const char *attrs
[] = { "sambaSID", NULL
};
5083 LDAPMessage
*result
= NULL
;
5084 LDAPMessage
*entry
= NULL
;
5086 char *group_sid_string
;
5087 struct dom_sid group_sid
;
5089 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5092 filter
= talloc_asprintf(tmp_ctx
,
5094 "(objectClass=%s))",
5097 if (filter
== NULL
) {
5098 DEBUG(3, ("talloc_asprintf failed\n"));
5102 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5103 if (rc
!= LDAP_SUCCESS
) {
5106 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5108 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5109 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5110 ldap_count_entries(priv2ld(priv
), result
),
5111 (unsigned int)gid
));
5115 entry
= ldap_first_entry(priv2ld(priv
), result
);
5117 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5118 "sambaSID", tmp_ctx
);
5119 if (group_sid_string
== NULL
) {
5120 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5121 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5125 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5126 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5131 sid_copy(sid
, &group_sid
);
5134 id
.type
= ID_TYPE_GID
;
5136 idmap_cache_set_sid2unixid(sid
, &id
);
5141 TALLOC_FREE(tmp_ctx
);
5147 * The following functions are called only if
5148 * ldapsam:trusted and ldapsam:editposix are
5153 * ldapsam_create_user creates a new
5154 * posixAccount and sambaSamAccount object
5155 * in the ldap users subtree
5157 * The uid is allocated by winbindd.
5160 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5161 TALLOC_CTX
*tmp_ctx
, const char *name
,
5162 uint32_t acb_info
, uint32_t *rid
)
5164 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5165 LDAPMessage
*entry
= NULL
;
5166 LDAPMessage
*result
= NULL
;
5167 uint32_t num_result
;
5168 bool is_machine
= False
;
5169 bool add_posix
= False
;
5170 LDAPMod
**mods
= NULL
;
5178 const char *dn
= NULL
;
5179 struct dom_sid group_sid
;
5180 struct dom_sid user_sid
;
5186 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5187 acb_info
& ACB_WSTRUST
||
5188 acb_info
& ACB_SVRTRUST
||
5189 acb_info
& ACB_DOMTRUST
) {
5193 username
= escape_ldap_string(talloc_tos(), name
);
5194 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5195 username
, LDAP_OBJ_POSIXACCOUNT
);
5196 TALLOC_FREE(username
);
5198 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5199 if (rc
!= LDAP_SUCCESS
) {
5200 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5201 return NT_STATUS_ACCESS_DENIED
;
5203 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5205 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5207 if (num_result
> 1) {
5208 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5209 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5212 if (num_result
== 1) {
5214 /* check if it is just a posix account.
5215 * or if there is a sid attached to this entry
5218 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5220 return NT_STATUS_UNSUCCESSFUL
;
5223 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5225 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5226 return NT_STATUS_USER_EXISTS
;
5229 /* it is just a posix account, retrieve the dn for later use */
5230 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5232 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5233 return NT_STATUS_NO_MEMORY
;
5237 if (num_result
== 0) {
5241 /* Create the basic samu structure and generate the mods for the ldap commit */
5242 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5243 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5247 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5249 user
= samu_new(tmp_ctx
);
5251 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5252 return NT_STATUS_NO_MEMORY
;
5255 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5256 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5257 return NT_STATUS_UNSUCCESSFUL
;
5259 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5260 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5261 return NT_STATUS_UNSUCCESSFUL
;
5264 if (acb_info
& ACB_NORMAL
) {
5265 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5266 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5267 return NT_STATUS_UNSUCCESSFUL
;
5270 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5271 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5272 return NT_STATUS_UNSUCCESSFUL
;
5276 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5277 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5278 return NT_STATUS_UNSUCCESSFUL
;
5282 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5283 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5284 return NT_STATUS_UNSUCCESSFUL
;
5287 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
)) {
5288 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5289 return NT_STATUS_UNSUCCESSFUL
;
5292 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5293 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5295 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5300 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5302 /* retrieve the Domain Users group gid */
5303 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5304 !sid_to_gid(&group_sid
, &gid
)) {
5305 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5306 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5309 /* lets allocate a new userid for this user */
5310 if (!winbind_allocate_uid(&uid
)) {
5311 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5312 return NT_STATUS_UNSUCCESSFUL
;
5317 /* TODO: choose a more appropriate default for machines */
5318 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5319 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5321 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5322 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5324 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5325 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5327 escape_name
= escape_rdn_val_string_alloc(name
);
5329 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5330 return NT_STATUS_NO_MEMORY
;
5334 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix (talloc_tos()));
5336 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix (talloc_tos()));
5339 SAFE_FREE(escape_name
);
5341 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5342 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5343 return NT_STATUS_NO_MEMORY
;
5346 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5347 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5348 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5349 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5350 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5351 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5352 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5355 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5358 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5360 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5363 if (rc
!= LDAP_SUCCESS
) {
5364 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5365 return NT_STATUS_UNSUCCESSFUL
;
5368 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5370 flush_pwnam_cache();
5372 return NT_STATUS_OK
;
5375 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5377 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5378 LDAPMessage
*result
= NULL
;
5379 LDAPMessage
*entry
= NULL
;
5385 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5387 filter
= talloc_asprintf(tmp_ctx
,
5390 "(objectClass=%s))",
5391 pdb_get_username(sam_acct
),
5392 LDAP_OBJ_POSIXACCOUNT
,
5393 LDAP_OBJ_SAMBASAMACCOUNT
);
5394 if (filter
== NULL
) {
5395 return NT_STATUS_NO_MEMORY
;
5398 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5399 if (rc
!= LDAP_SUCCESS
) {
5400 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5401 return NT_STATUS_UNSUCCESSFUL
;
5403 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5405 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5407 if (num_result
== 0) {
5408 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5409 return NT_STATUS_NO_SUCH_USER
;
5412 if (num_result
> 1) {
5413 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5414 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5417 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5419 return NT_STATUS_UNSUCCESSFUL
;
5422 /* it is just a posix account, retrieve the dn for later use */
5423 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5425 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5426 return NT_STATUS_NO_MEMORY
;
5429 /* try to remove memberships first */
5432 struct dom_sid
*sids
= NULL
;
5434 uint32_t num_groups
= 0;
5436 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5438 status
= ldapsam_enum_group_memberships(my_methods
,
5444 if (!NT_STATUS_IS_OK(status
)) {
5448 for (i
=0; i
< num_groups
; i
++) {
5452 sid_peek_rid(&sids
[i
], &group_rid
);
5454 ldapsam_del_groupmem(my_methods
,
5463 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5464 if (rc
!= LDAP_SUCCESS
) {
5465 return NT_STATUS_UNSUCCESSFUL
;
5468 flush_pwnam_cache();
5470 return NT_STATUS_OK
;
5474 * ldapsam_create_group creates a new
5475 * posixGroup and sambaGroupMapping object
5476 * in the ldap groups subtree
5478 * The gid is allocated by winbindd.
5481 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5482 TALLOC_CTX
*tmp_ctx
,
5486 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5488 LDAPMessage
*entry
= NULL
;
5489 LDAPMessage
*result
= NULL
;
5490 uint32_t num_result
;
5491 bool is_new_entry
= False
;
5492 LDAPMod
**mods
= NULL
;
5498 const char *dn
= NULL
;
5499 struct dom_sid group_sid
;
5503 groupname
= escape_ldap_string(talloc_tos(), name
);
5504 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5505 groupname
, LDAP_OBJ_POSIXGROUP
);
5506 TALLOC_FREE(groupname
);
5508 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5509 if (rc
!= LDAP_SUCCESS
) {
5510 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5511 return NT_STATUS_UNSUCCESSFUL
;
5513 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5515 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5517 if (num_result
> 1) {
5518 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5519 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5522 if (num_result
== 1) {
5524 /* check if it is just a posix group.
5525 * or if there is a sid attached to this entry
5528 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5530 return NT_STATUS_UNSUCCESSFUL
;
5533 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5535 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5536 return NT_STATUS_GROUP_EXISTS
;
5539 /* it is just a posix group, retrieve the gid and the dn for later use */
5540 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5542 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5543 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5546 gid
= strtoul(tmp
, NULL
, 10);
5548 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5550 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5551 return NT_STATUS_NO_MEMORY
;
5555 if (num_result
== 0) {
5556 is_new_entry
= true;
5559 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5560 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5564 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5566 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5568 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5570 if (!groupsidstr
|| !grouptype
) {
5571 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5572 return NT_STATUS_NO_MEMORY
;
5575 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5576 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5577 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5578 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5583 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5585 /* lets allocate a new groupid for this group */
5586 if (!winbind_allocate_gid(&gid
)) {
5587 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5588 return NT_STATUS_UNSUCCESSFUL
;
5591 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5593 escape_name
= escape_rdn_val_string_alloc(name
);
5595 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5596 return NT_STATUS_NO_MEMORY
;
5599 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix(talloc_tos()));
5601 SAFE_FREE(escape_name
);
5603 if (!gidstr
|| !dn
) {
5604 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5605 return NT_STATUS_NO_MEMORY
;
5608 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5609 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5610 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5613 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5616 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5618 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5619 /* This call may fail with rfc2307bis schema */
5620 /* Retry adding a structural class */
5621 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5622 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5626 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5629 if (rc
!= LDAP_SUCCESS
) {
5630 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5631 return NT_STATUS_UNSUCCESSFUL
;
5634 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5636 return NT_STATUS_OK
;
5639 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5641 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5642 LDAPMessage
*result
= NULL
;
5643 LDAPMessage
*entry
= NULL
;
5648 struct dom_sid group_sid
;
5651 /* get the group sid */
5652 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5654 filter
= talloc_asprintf(tmp_ctx
,
5657 "(objectClass=%s))",
5658 sid_string_talloc(tmp_ctx
, &group_sid
),
5659 LDAP_OBJ_POSIXGROUP
,
5661 if (filter
== NULL
) {
5662 return NT_STATUS_NO_MEMORY
;
5665 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5666 if (rc
!= LDAP_SUCCESS
) {
5667 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5668 return NT_STATUS_UNSUCCESSFUL
;
5670 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5672 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5674 if (num_result
== 0) {
5675 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5676 return NT_STATUS_NO_SUCH_GROUP
;
5679 if (num_result
> 1) {
5680 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5681 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5684 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5686 return NT_STATUS_UNSUCCESSFUL
;
5689 /* here it is, retrieve the dn for later use */
5690 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5692 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5693 return NT_STATUS_NO_MEMORY
;
5696 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5698 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5699 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5702 /* check no user have this group marked as primary group */
5703 filter
= talloc_asprintf(tmp_ctx
,
5706 "(objectClass=%s))",
5708 LDAP_OBJ_POSIXACCOUNT
,
5709 LDAP_OBJ_SAMBASAMACCOUNT
);
5711 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5712 if (rc
!= LDAP_SUCCESS
) {
5713 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5714 return NT_STATUS_UNSUCCESSFUL
;
5716 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5718 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5720 if (num_result
!= 0) {
5721 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5722 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5725 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5726 if (rc
!= LDAP_SUCCESS
) {
5727 return NT_STATUS_UNSUCCESSFUL
;
5730 return NT_STATUS_OK
;
5733 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5734 TALLOC_CTX
*tmp_ctx
,
5736 uint32_t member_rid
,
5739 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5740 LDAPMessage
*entry
= NULL
;
5741 LDAPMessage
*result
= NULL
;
5742 uint32_t num_result
;
5743 LDAPMod
**mods
= NULL
;
5746 const char *dn
= NULL
;
5747 struct dom_sid group_sid
;
5748 struct dom_sid member_sid
;
5753 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5755 case LDAP_MOD_DELETE
:
5756 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5759 return NT_STATUS_UNSUCCESSFUL
;
5762 /* get member sid */
5763 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5765 /* get the group sid */
5766 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5768 filter
= talloc_asprintf(tmp_ctx
,
5771 "(objectClass=%s))",
5772 sid_string_talloc(tmp_ctx
, &member_sid
),
5773 LDAP_OBJ_POSIXACCOUNT
,
5774 LDAP_OBJ_SAMBASAMACCOUNT
);
5775 if (filter
== NULL
) {
5776 return NT_STATUS_NO_MEMORY
;
5779 /* get the member uid */
5780 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5781 if (rc
!= LDAP_SUCCESS
) {
5782 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5783 return NT_STATUS_UNSUCCESSFUL
;
5785 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5787 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5789 if (num_result
== 0) {
5790 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5791 return NT_STATUS_NO_SUCH_MEMBER
;
5794 if (num_result
> 1) {
5795 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5796 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5799 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5801 return NT_STATUS_UNSUCCESSFUL
;
5804 if (modop
== LDAP_MOD_DELETE
) {
5805 /* check if we are trying to remove the member from his primary group */
5807 gid_t user_gid
, group_gid
;
5809 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5811 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5812 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5815 user_gid
= strtoul(gidstr
, NULL
, 10);
5817 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5818 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5819 return NT_STATUS_UNSUCCESSFUL
;
5822 if (user_gid
== group_gid
) {
5823 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5824 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5828 /* here it is, retrieve the uid for later use */
5829 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5831 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5832 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5835 filter
= talloc_asprintf(tmp_ctx
,
5838 "(objectClass=%s))",
5839 sid_string_talloc(tmp_ctx
, &group_sid
),
5840 LDAP_OBJ_POSIXGROUP
,
5844 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5845 if (rc
!= LDAP_SUCCESS
) {
5846 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5847 return NT_STATUS_UNSUCCESSFUL
;
5849 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5851 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5853 if (num_result
== 0) {
5854 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5855 return NT_STATUS_NO_SUCH_GROUP
;
5858 if (num_result
> 1) {
5859 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5860 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5863 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5865 return NT_STATUS_UNSUCCESSFUL
;
5868 /* here it is, retrieve the dn for later use */
5869 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5871 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5872 return NT_STATUS_NO_MEMORY
;
5875 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5877 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5879 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5880 if (rc
!= LDAP_SUCCESS
) {
5881 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5882 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5883 return NT_STATUS_MEMBER_IN_GROUP
;
5885 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5886 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5887 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5889 return NT_STATUS_UNSUCCESSFUL
;
5892 return NT_STATUS_OK
;
5895 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5896 TALLOC_CTX
*tmp_ctx
,
5898 uint32_t member_rid
)
5900 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5902 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5903 TALLOC_CTX
*tmp_ctx
,
5905 uint32_t member_rid
)
5907 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5910 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5911 TALLOC_CTX
*mem_ctx
,
5912 struct samu
*sampass
)
5914 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5915 LDAPMessage
*entry
= NULL
;
5916 LDAPMessage
*result
= NULL
;
5917 uint32_t num_result
;
5918 LDAPMod
**mods
= NULL
;
5920 char *escape_username
;
5922 const char *dn
= NULL
;
5926 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5928 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5929 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5930 return NT_STATUS_UNSUCCESSFUL
;
5932 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
5934 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5935 return NT_STATUS_NO_MEMORY
;
5938 escape_username
= escape_ldap_string(talloc_tos(),
5939 pdb_get_username(sampass
));
5940 if (escape_username
== NULL
) {
5941 return NT_STATUS_NO_MEMORY
;
5944 filter
= talloc_asprintf(mem_ctx
,
5947 "(objectClass=%s))",
5949 LDAP_OBJ_POSIXACCOUNT
,
5950 LDAP_OBJ_SAMBASAMACCOUNT
);
5952 TALLOC_FREE(escape_username
);
5954 if (filter
== NULL
) {
5955 return NT_STATUS_NO_MEMORY
;
5958 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5959 if (rc
!= LDAP_SUCCESS
) {
5960 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5961 return NT_STATUS_UNSUCCESSFUL
;
5963 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
5965 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5967 if (num_result
== 0) {
5968 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5969 return NT_STATUS_NO_SUCH_USER
;
5972 if (num_result
> 1) {
5973 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5974 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5977 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5979 return NT_STATUS_UNSUCCESSFUL
;
5982 /* retrieve the dn for later use */
5983 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5985 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5986 return NT_STATUS_NO_MEMORY
;
5989 /* remove the old one, and add the new one, this way we do not risk races */
5990 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5993 return NT_STATUS_OK
;
5996 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5998 if (rc
!= LDAP_SUCCESS
) {
5999 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6000 pdb_get_username(sampass
), gidstr
));
6001 return NT_STATUS_UNSUCCESSFUL
;
6004 flush_pwnam_cache();
6006 return NT_STATUS_OK
;
6010 /**********************************************************************
6011 trusted domains functions
6012 *********************************************************************/
6014 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6017 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6018 ldap_state
->domain_dn
);
6021 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6022 TALLOC_CTX
*mem_ctx
,
6023 const char *domain
, LDAPMessage
**entry
)
6027 int scope
= LDAP_SCOPE_SUBTREE
;
6028 const char **attrs
= NULL
; /* NULL: get all attrs */
6029 int attrsonly
= 0; /* 0: return values too */
6030 LDAPMessage
*result
= NULL
;
6032 uint32_t num_result
;
6034 filter
= talloc_asprintf(talloc_tos(),
6035 "(&(objectClass=%s)(sambaDomainName=%s))",
6036 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6038 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6039 if (trusted_dn
== NULL
) {
6042 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6043 filter
, attrs
, attrsonly
, &result
);
6045 if (result
!= NULL
) {
6046 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6049 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6054 if (rc
!= LDAP_SUCCESS
) {
6058 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6060 if (num_result
> 1) {
6061 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6062 "%s object for domain '%s'?!\n",
6063 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6067 if (num_result
== 0) {
6068 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6069 "%s object for domain %s.\n",
6070 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6073 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6079 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6082 struct dom_sid
*sid
,
6083 time_t *pass_last_set_time
)
6085 struct ldapsam_privates
*ldap_state
=
6086 (struct ldapsam_privates
*)methods
->private_data
;
6087 LDAPMessage
*entry
= NULL
;
6089 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6091 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6100 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6101 entry
, "sambaClearTextPassword", talloc_tos());
6102 if (pwd_str
== NULL
) {
6105 /* trusteddom_pw routines do not use talloc yet... */
6106 *pwd
= SMB_STRDUP(pwd_str
);
6112 /* last change time */
6113 if (pass_last_set_time
!= NULL
) {
6115 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6116 entry
, "sambaPwdLastSet", talloc_tos());
6117 if (time_str
== NULL
) {
6120 *pass_last_set_time
= (time_t)atol(time_str
);
6126 struct dom_sid dom_sid
;
6127 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6130 if (sid_str
== NULL
) {
6133 if (!string_to_sid(&dom_sid
, sid_str
)) {
6136 sid_copy(sid
, &dom_sid
);
6142 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6145 const struct dom_sid
*sid
)
6147 struct ldapsam_privates
*ldap_state
=
6148 (struct ldapsam_privates
*)methods
->private_data
;
6149 LDAPMessage
*entry
= NULL
;
6150 LDAPMod
**mods
= NULL
;
6151 char *prev_pwd
= NULL
;
6152 char *trusted_dn
= NULL
;
6155 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6158 * get the current entry (if there is one) in order to put the
6159 * current password into the previous password attribute
6161 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6166 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6167 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6168 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6170 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6171 sid_string_tos(sid
));
6172 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6173 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6174 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6175 "sambaClearTextPassword", pwd
);
6177 if (entry
!= NULL
) {
6178 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6179 entry
, "sambaClearTextPassword", talloc_tos());
6180 if (prev_pwd
!= NULL
) {
6181 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6182 "sambaPreviousClearTextPassword",
6187 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods
);
6189 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6190 if (trusted_dn
== NULL
) {
6193 if (entry
== NULL
) {
6194 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6196 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6199 if (rc
!= LDAP_SUCCESS
) {
6200 DEBUG(1, ("error writing trusted domain password!\n"));
6207 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6211 struct ldapsam_privates
*ldap_state
=
6212 (struct ldapsam_privates
*)methods
->private_data
;
6213 LDAPMessage
*entry
= NULL
;
6214 const char *trusted_dn
;
6216 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6220 if (entry
== NULL
) {
6221 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6226 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6228 if (trusted_dn
== NULL
) {
6229 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6233 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6234 if (rc
!= LDAP_SUCCESS
) {
6241 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6242 TALLOC_CTX
*mem_ctx
,
6243 uint32_t *num_domains
,
6244 struct trustdom_info
***domains
)
6247 struct ldapsam_privates
*ldap_state
=
6248 (struct ldapsam_privates
*)methods
->private_data
;
6250 int scope
= LDAP_SCOPE_SUBTREE
;
6251 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6252 int attrsonly
= 0; /* 0: return values too */
6253 LDAPMessage
*result
= NULL
;
6254 LDAPMessage
*entry
= NULL
;
6256 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6257 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6259 rc
= smbldap_search(ldap_state
->smbldap_state
,
6260 ldap_state
->domain_dn
,
6267 if (result
!= NULL
) {
6268 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6271 if (rc
!= LDAP_SUCCESS
) {
6272 return NT_STATUS_UNSUCCESSFUL
;
6276 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6277 DEBUG(1, ("talloc failed\n"));
6278 return NT_STATUS_NO_MEMORY
;
6281 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6283 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6285 char *dom_name
, *dom_sid_str
;
6286 struct trustdom_info
*dom_info
;
6288 dom_info
= talloc(*domains
, struct trustdom_info
);
6289 if (dom_info
== NULL
) {
6290 DEBUG(1, ("talloc failed\n"));
6291 return NT_STATUS_NO_MEMORY
;
6294 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6298 if (dom_name
== NULL
) {
6299 DEBUG(1, ("talloc failed\n"));
6300 return NT_STATUS_NO_MEMORY
;
6302 dom_info
->name
= dom_name
;
6304 dom_sid_str
= smbldap_talloc_single_attribute(
6305 priv2ld(ldap_state
), entry
, "sambaSID",
6307 if (dom_sid_str
== NULL
) {
6308 DEBUG(1, ("talloc failed\n"));
6309 return NT_STATUS_NO_MEMORY
;
6311 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6312 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6314 return NT_STATUS_UNSUCCESSFUL
;
6317 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6318 domains
, num_domains
);
6320 if (*domains
== NULL
) {
6321 DEBUG(1, ("talloc failed\n"));
6322 return NT_STATUS_NO_MEMORY
;
6326 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6327 return NT_STATUS_OK
;
6331 /**********************************************************************
6333 *********************************************************************/
6335 static void free_private_data(void **vp
)
6337 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6339 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6341 if ((*ldap_state
)->result
!= NULL
) {
6342 ldap_msgfree((*ldap_state
)->result
);
6343 (*ldap_state
)->result
= NULL
;
6345 if ((*ldap_state
)->domain_dn
!= NULL
) {
6346 SAFE_FREE((*ldap_state
)->domain_dn
);
6351 /* No need to free any further, as it is talloc()ed */
6354 /*********************************************************************
6355 Intitalise the parts of the pdb_methods structure that are common to
6357 *********************************************************************/
6359 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6362 struct ldapsam_privates
*ldap_state
;
6363 char *bind_dn
= NULL
;
6364 char *bind_secret
= NULL
;
6366 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6370 (*pdb_method
)->name
= "ldapsam";
6372 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6373 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6374 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6375 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6376 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6377 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6379 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6380 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6381 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6382 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6383 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6384 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6385 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6387 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6388 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6390 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6392 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6393 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6395 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6396 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6397 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6398 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6400 /* TODO: Setup private data and free */
6402 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6403 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6404 return NT_STATUS_NO_MEMORY
;
6407 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6408 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6409 return NT_STATUS_NO_MEMORY
;
6412 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6413 location
, false, bind_dn
, bind_secret
,
6414 &ldap_state
->smbldap_state
);
6415 memset(bind_secret
, '\0', strlen(bind_secret
));
6416 SAFE_FREE(bind_secret
);
6418 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6422 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6423 return NT_STATUS_NO_MEMORY
;
6426 (*pdb_method
)->private_data
= ldap_state
;
6428 (*pdb_method
)->free_private_data
= free_private_data
;
6430 return NT_STATUS_OK
;
6433 /**********************************************************************
6434 Initialise the normal mode for pdb_ldap
6435 *********************************************************************/
6437 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6440 struct ldapsam_privates
*ldap_state
= NULL
;
6441 uint32_t alg_rid_base
;
6442 char *alg_rid_base_string
= NULL
;
6443 LDAPMessage
*result
= NULL
;
6444 LDAPMessage
*entry
= NULL
;
6445 struct dom_sid ldap_domain_sid
;
6446 struct dom_sid secrets_domain_sid
;
6447 char *domain_sid_string
= NULL
;
6449 char *uri
= talloc_strdup( NULL
, location
);
6451 trim_char( uri
, '\"', '\"' );
6452 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6456 if (!NT_STATUS_IS_OK(nt_status
)) {
6460 (*pdb_method
)->name
= "ldapsam";
6462 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6463 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6464 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6465 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6466 (*pdb_method
)->search_users
= ldapsam_search_users
;
6467 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6468 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6470 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6471 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6472 (*pdb_method
)->enum_group_memberships
=
6473 ldapsam_enum_group_memberships
;
6474 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6475 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6476 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6477 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6479 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6480 (*pdb_method
)->create_user
= ldapsam_create_user
;
6481 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6482 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6483 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6484 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6485 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6486 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6490 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6491 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6493 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6495 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6497 ldap_state
->domain_name
, True
);
6499 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6500 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6501 "info, nor add one to the domain. "
6502 "We cannot work reliably without it.\n"));
6503 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6506 /* Given that the above might fail, everything below this must be
6509 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6512 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6514 ldap_msgfree(result
);
6515 return NT_STATUS_UNSUCCESSFUL
;
6518 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6520 ldap_msgfree(result
);
6521 return NT_STATUS_UNSUCCESSFUL
;
6524 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6527 domain_sid_string
= smbldap_talloc_single_attribute(
6528 ldap_state
->smbldap_state
->ldap_struct
,
6530 get_userattr_key2string(ldap_state
->schema_ver
,
6531 LDAP_ATTR_USER_SID
),
6534 if (domain_sid_string
) {
6536 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6537 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6538 "read as a valid SID\n", domain_sid_string
));
6539 ldap_msgfree(result
);
6540 TALLOC_FREE(domain_sid_string
);
6541 return NT_STATUS_INVALID_PARAMETER
;
6543 found_sid
= PDB_secrets_fetch_domain_sid(ldap_state
->domain_name
,
6544 &secrets_domain_sid
);
6545 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6546 &ldap_domain_sid
)) {
6547 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6548 "%s based on pdb_ldap results %s -> %s\n",
6549 ldap_state
->domain_name
,
6550 sid_string_dbg(&secrets_domain_sid
),
6551 sid_string_dbg(&ldap_domain_sid
)));
6553 /* reset secrets.tdb sid */
6554 PDB_secrets_store_domain_sid(ldap_state
->domain_name
,
6556 DEBUG(1, ("New global sam SID: %s\n",
6557 sid_string_dbg(get_global_sam_sid())));
6559 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6560 TALLOC_FREE(domain_sid_string
);
6563 alg_rid_base_string
= smbldap_talloc_single_attribute(
6564 ldap_state
->smbldap_state
->ldap_struct
,
6566 get_attr_key2string( dominfo_attr_list
,
6567 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6569 if (alg_rid_base_string
) {
6570 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6571 if (alg_rid_base
!= algorithmic_rid_base()) {
6572 DEBUG(0, ("The value of 'algorithmic RID base' has "
6573 "changed since the LDAP\n"
6574 "database was initialised. Aborting. \n"));
6575 ldap_msgfree(result
);
6576 TALLOC_FREE(alg_rid_base_string
);
6577 return NT_STATUS_UNSUCCESSFUL
;
6579 TALLOC_FREE(alg_rid_base_string
);
6581 ldap_msgfree(result
);
6583 return NT_STATUS_OK
;
6586 NTSTATUS
pdb_ldap_init(void)
6589 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6592 /* Let pdb_nds register backends */
6597 return NT_STATUS_OK
;