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
47 #include "../libcli/auth/libcli_auth.h"
49 #include "idmap_cache.h"
50 #include "../libcli/security/security.h"
53 #define DBGC_CLASS DBGC_PASSDB
59 * Work around versions of the LDAP client libs that don't have the OIDs
60 * defined, or have them defined under the old name.
61 * This functionality is really a factor of the server, not the client
65 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
66 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
67 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
68 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
71 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
72 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
73 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
77 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
78 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
79 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
80 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
86 /**********************************************************************
87 Simple helper function to make stuff better readable
88 **********************************************************************/
90 static LDAP
*priv2ld(struct ldapsam_privates
*priv
)
92 return priv
->smbldap_state
->ldap_struct
;
95 /**********************************************************************
96 Get the attribute name given a user schame version.
97 **********************************************************************/
99 static const char* get_userattr_key2string( int schema_ver
, int key
)
101 switch ( schema_ver
) {
102 case SCHEMAVER_SAMBAACCOUNT
:
103 return get_attr_key2string( attrib_map_v22
, key
);
105 case SCHEMAVER_SAMBASAMACCOUNT
:
106 return get_attr_key2string( attrib_map_v30
, key
);
109 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
115 /**********************************************************************
116 Return the list of attribute names given a user schema version.
117 **********************************************************************/
119 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
121 switch ( schema_ver
) {
122 case SCHEMAVER_SAMBAACCOUNT
:
123 return get_attr_list( mem_ctx
, attrib_map_v22
);
125 case SCHEMAVER_SAMBASAMACCOUNT
:
126 return get_attr_list( mem_ctx
, attrib_map_v30
);
128 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
135 /**************************************************************************
136 Return the list of attribute names to delete given a user schema version.
137 **************************************************************************/
139 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
142 switch ( schema_ver
) {
143 case SCHEMAVER_SAMBAACCOUNT
:
144 return get_attr_list( mem_ctx
,
145 attrib_map_to_delete_v22
);
147 case SCHEMAVER_SAMBASAMACCOUNT
:
148 return get_attr_list( mem_ctx
,
149 attrib_map_to_delete_v30
);
151 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
159 /*******************************************************************
160 Generate the LDAP search filter for the objectclass based on the
161 version of the schema we are using.
162 ******************************************************************/
164 static const char* get_objclass_filter( int schema_ver
)
166 fstring objclass_filter
;
169 switch( schema_ver
) {
170 case SCHEMAVER_SAMBAACCOUNT
:
171 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
173 case SCHEMAVER_SAMBASAMACCOUNT
:
174 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
177 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
178 objclass_filter
[0] = '\0';
182 result
= talloc_strdup(talloc_tos(), objclass_filter
);
183 SMB_ASSERT(result
!= NULL
);
187 /*****************************************************************
188 Scan a sequence number off OpenLDAP's syncrepl contextCSN
189 ******************************************************************/
191 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
193 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
194 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
195 LDAPMessage
*msg
= NULL
;
196 LDAPMessage
*entry
= NULL
;
198 char **values
= NULL
;
199 int rc
, num_result
, num_values
, rid
;
205 /* Unfortunatly there is no proper way to detect syncrepl-support in
206 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
207 * but do not show up in the root-DSE yet. Neither we can query the
208 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
209 * objectclass. Currently we require lp_ldap_suffix() to show up as
210 * namingContext. - Guenther
213 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
218 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
222 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
223 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
224 "as top-level namingContext\n", lp_ldap_suffix()));
228 mem_ctx
= talloc_init("ldapsam_get_seq_num");
231 return NT_STATUS_NO_MEMORY
;
233 if ((attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2)) == NULL
) {
234 ntstatus
= NT_STATUS_NO_MEMORY
;
238 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
239 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
242 /* consumer syncreplCookie: */
243 /* csn=20050126161620Z#0000001#00#00000 */
244 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
246 suffix
= talloc_asprintf(mem_ctx
,
247 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
249 ntstatus
= NT_STATUS_NO_MEMORY
;
254 /* provider contextCSN */
255 /* 20050126161620Z#000009#00#000000 */
256 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
258 suffix
= talloc_asprintf(mem_ctx
,
259 "cn=ldapsync,%s", lp_ldap_suffix());
262 ntstatus
= NT_STATUS_NO_MEMORY
;
267 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
268 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
270 if (rc
!= LDAP_SUCCESS
) {
274 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
275 if (num_result
!= 1) {
276 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
280 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
282 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
286 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
287 if (values
== NULL
) {
288 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
292 num_values
= ldap_count_values(values
);
293 if (num_values
== 0) {
294 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
299 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
300 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
305 if (!strncmp(p
, "csn=", strlen("csn=")))
308 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
310 *seq_num
= generalized_to_unix_time(p
);
312 /* very basic sanity check */
314 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
319 ntstatus
= NT_STATUS_OK
;
323 ldap_value_free(values
);
327 talloc_destroy(mem_ctx
);
332 /*******************************************************************
333 Run the search by name.
334 ******************************************************************/
336 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
338 LDAPMessage
** result
,
342 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
346 return LDAP_NO_MEMORY
;
350 * in the filter expression, replace %u with the real name
351 * so in ldap filter, %u MUST exist :-)
353 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
354 get_objclass_filter(ldap_state
->schema_ver
));
356 TALLOC_FREE(escape_user
);
357 return LDAP_NO_MEMORY
;
360 * have to use this here because $ is filtered out
364 filter
= talloc_all_string_sub(talloc_tos(),
365 filter
, "%u", escape_user
);
366 TALLOC_FREE(escape_user
);
368 return LDAP_NO_MEMORY
;
371 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
372 filter
, attr
, result
);
377 /*******************************************************************
378 Run the search by rid.
379 ******************************************************************/
381 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
382 uint32_t rid
, LDAPMessage
** result
,
388 filter
= talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid
,
389 get_objclass_filter(ldap_state
->schema_ver
));
391 return LDAP_NO_MEMORY
;
394 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
395 filter
, attr
, result
);
400 /*******************************************************************
401 Run the search by SID.
402 ******************************************************************/
404 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
405 const struct dom_sid
*sid
, LDAPMessage
** result
,
412 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
413 get_userattr_key2string(ldap_state
->schema_ver
,
415 sid_to_fstring(sid_string
, sid
),
416 get_objclass_filter(ldap_state
->schema_ver
));
418 return LDAP_NO_MEMORY
;
421 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
422 filter
, attr
, result
);
428 /*******************************************************************
429 Delete complete object or objectclass and attrs from
430 object found in search_result depending on lp_ldap_delete_dn
431 ******************************************************************/
433 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
436 const char *objectclass
,
439 LDAPMod
**mods
= NULL
;
442 BerElement
*ptr
= NULL
;
444 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
446 return LDAP_NO_MEMORY
;
449 if (lp_ldap_delete_dn()) {
450 return smbldap_delete(priv
->smbldap_state
, dn
);
453 /* Ok, delete only the SAM attributes */
455 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
457 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
460 /* We are only allowed to delete the attributes that
463 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
464 if (strequal(*attrib
, name
)) {
465 DEBUG(10, ("ldapsam_delete_entry: deleting "
466 "attribute %s\n", name
));
467 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
478 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
479 talloc_autofree_ldapmod(mem_ctx
, mods
);
481 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
484 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
489 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
490 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
496 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
497 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
507 /**********************************************************************
508 Initialize struct samu from an LDAP query.
509 (Based on init_sam_from_buffer in pdb_tdb.c)
510 *********************************************************************/
512 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
513 struct samu
* sampass
,
520 pass_can_change_time
,
521 pass_must_change_time
,
524 char *username
= NULL
,
530 *logon_script
= NULL
,
531 *profile_path
= NULL
,
533 *workstations
= NULL
,
536 uint8 smblmpwd
[LM_HASH_LEN
],
537 smbntpwd
[NT_HASH_LEN
];
538 bool use_samba_attrs
= True
;
539 uint32_t acct_ctrl
= 0;
541 uint16_t bad_password_count
= 0,
544 uint8 hours
[MAX_HOURS_LEN
];
546 struct login_cache cache_entry
;
548 bool expand_explicit
= lp_passdb_expand_explicit();
550 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
555 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
556 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
560 if (priv2ld(ldap_state
) == NULL
) {
561 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
562 "ldap_struct is NULL!\n"));
566 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
570 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
575 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
577 nt_username
= talloc_strdup(ctx
, username
);
582 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
587 pdb_set_username(sampass
, username
, PDB_SET
);
589 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
590 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
592 /* deal with different attributes between the schema first */
594 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
595 if ((temp
= smbldap_talloc_single_attribute(
596 ldap_state
->smbldap_state
->ldap_struct
,
598 get_userattr_key2string(ldap_state
->schema_ver
,
601 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
604 if ((temp
= smbldap_talloc_single_attribute(
605 ldap_state
->smbldap_state
->ldap_struct
,
607 get_userattr_key2string(ldap_state
->schema_ver
,
610 user_rid
= (uint32_t)atol(temp
);
611 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
615 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
616 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
617 get_userattr_key2string(ldap_state
->schema_ver
,
619 get_userattr_key2string(ldap_state
->schema_ver
,
625 temp
= smbldap_talloc_single_attribute(
626 ldap_state
->smbldap_state
->ldap_struct
,
628 get_userattr_key2string(ldap_state
->schema_ver
,
629 LDAP_ATTR_PWD_LAST_SET
),
632 pass_last_set_time
= (time_t) atol(temp
);
633 pdb_set_pass_last_set_time(sampass
,
634 pass_last_set_time
, PDB_SET
);
637 temp
= smbldap_talloc_single_attribute(
638 ldap_state
->smbldap_state
->ldap_struct
,
640 get_userattr_key2string(ldap_state
->schema_ver
,
641 LDAP_ATTR_LOGON_TIME
),
644 logon_time
= (time_t) atol(temp
);
645 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
648 temp
= smbldap_talloc_single_attribute(
649 ldap_state
->smbldap_state
->ldap_struct
,
651 get_userattr_key2string(ldap_state
->schema_ver
,
652 LDAP_ATTR_LOGOFF_TIME
),
655 logoff_time
= (time_t) atol(temp
);
656 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
659 temp
= smbldap_talloc_single_attribute(
660 ldap_state
->smbldap_state
->ldap_struct
,
662 get_userattr_key2string(ldap_state
->schema_ver
,
663 LDAP_ATTR_KICKOFF_TIME
),
666 kickoff_time
= (time_t) atol(temp
);
667 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
670 temp
= smbldap_talloc_single_attribute(
671 ldap_state
->smbldap_state
->ldap_struct
,
673 get_userattr_key2string(ldap_state
->schema_ver
,
674 LDAP_ATTR_PWD_CAN_CHANGE
),
677 pass_can_change_time
= (time_t) atol(temp
);
678 pdb_set_pass_can_change_time(sampass
,
679 pass_can_change_time
, PDB_SET
);
682 temp
= smbldap_talloc_single_attribute(
683 ldap_state
->smbldap_state
->ldap_struct
,
685 get_userattr_key2string(ldap_state
->schema_ver
,
686 LDAP_ATTR_PWD_MUST_CHANGE
),
689 pass_must_change_time
= (time_t) atol(temp
);
690 pdb_set_pass_must_change_time(sampass
,
691 pass_must_change_time
, PDB_SET
);
694 /* recommend that 'gecos' and 'displayName' should refer to the same
695 * attribute OID. userFullName depreciated, only used by Samba
696 * primary rules of LDAP: don't make a new attribute when one is already defined
697 * that fits your needs; using cn then displayName rather than 'userFullName'
700 fullname
= smbldap_talloc_single_attribute(
701 ldap_state
->smbldap_state
->ldap_struct
,
703 get_userattr_key2string(ldap_state
->schema_ver
,
704 LDAP_ATTR_DISPLAY_NAME
),
707 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
709 fullname
= smbldap_talloc_single_attribute(
710 ldap_state
->smbldap_state
->ldap_struct
,
712 get_userattr_key2string(ldap_state
->schema_ver
,
716 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
720 dir_drive
= smbldap_talloc_single_attribute(
721 ldap_state
->smbldap_state
->ldap_struct
,
723 get_userattr_key2string(ldap_state
->schema_ver
,
724 LDAP_ATTR_HOME_DRIVE
),
727 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
729 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
732 homedir
= smbldap_talloc_single_attribute(
733 ldap_state
->smbldap_state
->ldap_struct
,
735 get_userattr_key2string(ldap_state
->schema_ver
,
736 LDAP_ATTR_HOME_PATH
),
739 if (expand_explicit
) {
740 homedir
= talloc_sub_basic(ctx
,
748 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
750 pdb_set_homedir(sampass
,
751 talloc_sub_basic(ctx
, username
, domain
,
756 logon_script
= smbldap_talloc_single_attribute(
757 ldap_state
->smbldap_state
->ldap_struct
,
759 get_userattr_key2string(ldap_state
->schema_ver
,
760 LDAP_ATTR_LOGON_SCRIPT
),
763 if (expand_explicit
) {
764 logon_script
= talloc_sub_basic(ctx
,
772 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
774 pdb_set_logon_script(sampass
,
775 talloc_sub_basic(ctx
, username
, domain
,
780 profile_path
= smbldap_talloc_single_attribute(
781 ldap_state
->smbldap_state
->ldap_struct
,
783 get_userattr_key2string(ldap_state
->schema_ver
,
784 LDAP_ATTR_PROFILE_PATH
),
787 if (expand_explicit
) {
788 profile_path
= talloc_sub_basic(ctx
,
796 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
798 pdb_set_profile_path(sampass
,
799 talloc_sub_basic(ctx
, username
, domain
,
804 acct_desc
= smbldap_talloc_single_attribute(
805 ldap_state
->smbldap_state
->ldap_struct
,
807 get_userattr_key2string(ldap_state
->schema_ver
,
811 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
814 workstations
= smbldap_talloc_single_attribute(
815 ldap_state
->smbldap_state
->ldap_struct
,
817 get_userattr_key2string(ldap_state
->schema_ver
,
821 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
824 munged_dial
= smbldap_talloc_single_attribute(
825 ldap_state
->smbldap_state
->ldap_struct
,
827 get_userattr_key2string(ldap_state
->schema_ver
,
828 LDAP_ATTR_MUNGED_DIAL
),
831 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
834 /* FIXME: hours stuff should be cleaner */
838 memset(hours
, 0xff, hours_len
);
840 if (ldap_state
->is_nds_ldap
) {
843 char clear_text_pw
[512];
845 /* Make call to Novell eDirectory ldap extension to get clear text password.
846 NOTE: This will only work if we have an SSL connection to eDirectory. */
847 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
848 if (user_dn
!= NULL
) {
849 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
851 pwd_len
= sizeof(clear_text_pw
);
852 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
853 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
854 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
855 TALLOC_FREE(user_dn
);
858 ZERO_STRUCT(smblmpwd
);
859 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
860 TALLOC_FREE(user_dn
);
863 ZERO_STRUCT(smbntpwd
);
864 use_samba_attrs
= False
;
867 TALLOC_FREE(user_dn
);
870 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
874 if (use_samba_attrs
) {
875 temp
= smbldap_talloc_single_attribute(
876 ldap_state
->smbldap_state
->ldap_struct
,
878 get_userattr_key2string(ldap_state
->schema_ver
,
882 pdb_gethexpwd(temp
, smblmpwd
);
883 memset((char *)temp
, '\0', strlen(temp
)+1);
884 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
887 ZERO_STRUCT(smblmpwd
);
890 temp
= smbldap_talloc_single_attribute(
891 ldap_state
->smbldap_state
->ldap_struct
,
893 get_userattr_key2string(ldap_state
->schema_ver
,
897 pdb_gethexpwd(temp
, smbntpwd
);
898 memset((char *)temp
, '\0', strlen(temp
)+1);
899 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
902 ZERO_STRUCT(smbntpwd
);
908 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
910 uint8
*pwhist
= NULL
;
912 char *history_string
= TALLOC_ARRAY(ctx
, char,
913 MAX_PW_HISTORY_LEN
*64);
915 if (!history_string
) {
919 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
921 pwhist
= TALLOC_ARRAY(ctx
, uint8
,
922 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
923 if (pwhist
== NULL
) {
924 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
927 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
929 if (smbldap_get_single_attribute(
930 ldap_state
->smbldap_state
->ldap_struct
,
932 get_userattr_key2string(ldap_state
->schema_ver
,
933 LDAP_ATTR_PWD_HISTORY
),
935 MAX_PW_HISTORY_LEN
*64)) {
936 bool hex_failed
= false;
937 for (i
= 0; i
< pwHistLen
; i
++){
938 /* Get the 16 byte salt. */
939 if (!pdb_gethexpwd(&history_string
[i
*64],
940 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
944 /* Get the 16 byte MD5 hash of salt+passwd. */
945 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
946 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
947 PW_HISTORY_SALT_LEN
])) {
953 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
955 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
958 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
963 temp
= smbldap_talloc_single_attribute(
964 ldap_state
->smbldap_state
->ldap_struct
,
966 get_userattr_key2string(ldap_state
->schema_ver
,
970 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
972 if (acct_ctrl
== 0) {
973 acct_ctrl
|= ACB_NORMAL
;
976 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
978 acct_ctrl
|= ACB_NORMAL
;
981 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
982 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
984 temp
= smbldap_talloc_single_attribute(
985 ldap_state
->smbldap_state
->ldap_struct
,
987 get_userattr_key2string(ldap_state
->schema_ver
,
988 LDAP_ATTR_BAD_PASSWORD_COUNT
),
991 bad_password_count
= (uint32_t) atol(temp
);
992 pdb_set_bad_password_count(sampass
,
993 bad_password_count
, PDB_SET
);
996 temp
= smbldap_talloc_single_attribute(
997 ldap_state
->smbldap_state
->ldap_struct
,
999 get_userattr_key2string(ldap_state
->schema_ver
,
1000 LDAP_ATTR_BAD_PASSWORD_TIME
),
1003 bad_password_time
= (time_t) atol(temp
);
1004 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
1008 temp
= smbldap_talloc_single_attribute(
1009 ldap_state
->smbldap_state
->ldap_struct
,
1011 get_userattr_key2string(ldap_state
->schema_ver
,
1012 LDAP_ATTR_LOGON_COUNT
),
1015 logon_count
= (uint32_t) atol(temp
);
1016 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
1019 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1021 temp
= smbldap_talloc_single_attribute(
1022 ldap_state
->smbldap_state
->ldap_struct
,
1024 get_userattr_key2string(ldap_state
->schema_ver
,
1025 LDAP_ATTR_LOGON_HOURS
),
1028 pdb_gethexhours(temp
, hours
);
1029 memset((char *)temp
, '\0', strlen(temp
) +1);
1030 pdb_set_hours(sampass
, hours
, PDB_SET
);
1034 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1035 struct passwd unix_pw
;
1036 bool have_uid
= false;
1037 bool have_gid
= false;
1038 struct dom_sid mapped_gsid
;
1039 const struct dom_sid
*primary_gsid
;
1041 ZERO_STRUCT(unix_pw
);
1043 unix_pw
.pw_name
= username
;
1044 unix_pw
.pw_passwd
= discard_const_p(char, "x");
1046 temp
= smbldap_talloc_single_attribute(
1047 priv2ld(ldap_state
),
1052 /* We've got a uid, feed the cache */
1053 unix_pw
.pw_uid
= strtoul(temp
, NULL
, 10);
1056 temp
= smbldap_talloc_single_attribute(
1057 priv2ld(ldap_state
),
1062 /* We've got a uid, feed the cache */
1063 unix_pw
.pw_gid
= strtoul(temp
, NULL
, 10);
1066 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1067 priv2ld(ldap_state
),
1071 if (unix_pw
.pw_gecos
) {
1072 unix_pw
.pw_gecos
= fullname
;
1074 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1075 priv2ld(ldap_state
),
1079 if (unix_pw
.pw_dir
) {
1080 unix_pw
.pw_dir
= discard_const_p(char, "");
1082 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1083 priv2ld(ldap_state
),
1087 if (unix_pw
.pw_shell
) {
1088 unix_pw
.pw_shell
= discard_const_p(char, "");
1091 if (have_uid
&& have_gid
) {
1092 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1094 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1097 if (sampass
->unix_pw
== NULL
) {
1098 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1099 pdb_get_username(sampass
)));
1103 store_uid_sid_cache(pdb_get_user_sid(sampass
),
1104 sampass
->unix_pw
->pw_uid
);
1105 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass
),
1106 sampass
->unix_pw
->pw_uid
);
1108 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1109 primary_gsid
= pdb_get_group_sid(sampass
);
1110 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1111 store_gid_sid_cache(primary_gsid
,
1112 sampass
->unix_pw
->pw_gid
);
1113 idmap_cache_set_sid2gid(primary_gsid
,
1114 sampass
->unix_pw
->pw_gid
);
1118 /* check the timestamp of the cache vs ldap entry */
1119 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1125 /* see if we have newer updates */
1126 if (!login_cache_read(sampass
, &cache_entry
)) {
1127 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1128 (unsigned int)pdb_get_bad_password_count(sampass
),
1129 (unsigned int)pdb_get_bad_password_time(sampass
)));
1134 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1135 (unsigned int)ldap_entry_time
,
1136 (unsigned int)cache_entry
.entry_timestamp
,
1137 (unsigned int)cache_entry
.bad_password_time
));
1139 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1140 /* cache is older than directory , so
1141 we need to delete the entry but allow the
1142 fields to be written out */
1143 login_cache_delentry(sampass
);
1146 pdb_set_acct_ctrl(sampass
,
1147 pdb_get_acct_ctrl(sampass
) |
1148 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1150 pdb_set_bad_password_count(sampass
,
1151 cache_entry
.bad_password_count
,
1153 pdb_set_bad_password_time(sampass
,
1154 cache_entry
.bad_password_time
,
1166 /**********************************************************************
1167 Initialize the ldap db from a struct samu. Called on update.
1168 (Based on init_buffer_from_sam in pdb_tdb.c)
1169 *********************************************************************/
1171 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1172 LDAPMessage
*existing
,
1173 LDAPMod
*** mods
, struct samu
* sampass
,
1174 bool (*need_update
)(const struct samu
*,
1180 if (mods
== NULL
|| sampass
== NULL
) {
1181 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1188 * took out adding "objectclass: sambaAccount"
1189 * do this on a per-mod basis
1191 if (need_update(sampass
, PDB_USERNAME
)) {
1192 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1193 "uid", pdb_get_username(sampass
));
1194 if (ldap_state
->is_nds_ldap
) {
1195 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1196 "cn", pdb_get_username(sampass
));
1197 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1198 "sn", pdb_get_username(sampass
));
1202 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1204 /* only update the RID if we actually need to */
1205 if (need_update(sampass
, PDB_USERSID
)) {
1207 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1209 switch ( ldap_state
->schema_ver
) {
1210 case SCHEMAVER_SAMBAACCOUNT
:
1211 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1212 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1213 sid_string_dbg(user_sid
),
1215 &ldap_state
->domain_sid
)));
1218 if (asprintf(&temp
, "%i", rid
) < 0) {
1221 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1222 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1227 case SCHEMAVER_SAMBASAMACCOUNT
:
1228 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1229 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1230 sid_to_fstring(sid_string
, user_sid
));
1234 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1239 /* we don't need to store the primary group RID - so leaving it
1240 'free' to hang off the unix primary group makes life easier */
1242 if (need_update(sampass
, PDB_GROUPSID
)) {
1244 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1246 switch ( ldap_state
->schema_ver
) {
1247 case SCHEMAVER_SAMBAACCOUNT
:
1248 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1249 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1250 sid_string_dbg(group_sid
),
1252 &ldap_state
->domain_sid
)));
1256 if (asprintf(&temp
, "%i", rid
) < 0) {
1259 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1260 get_userattr_key2string(ldap_state
->schema_ver
,
1261 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1265 case SCHEMAVER_SAMBASAMACCOUNT
:
1266 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1267 get_userattr_key2string(ldap_state
->schema_ver
,
1268 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1272 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1278 /* displayName, cn, and gecos should all be the same
1279 * most easily accomplished by giving them the same OID
1280 * gecos isn't set here b/c it should be handled by the
1282 * We change displayName only and fall back to cn if
1283 * it does not exist.
1286 if (need_update(sampass
, PDB_FULLNAME
))
1287 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1288 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1289 pdb_get_fullname(sampass
));
1291 if (need_update(sampass
, PDB_ACCTDESC
))
1292 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1293 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1294 pdb_get_acct_desc(sampass
));
1296 if (need_update(sampass
, PDB_WORKSTATIONS
))
1297 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1298 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1299 pdb_get_workstations(sampass
));
1301 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1302 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1303 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1304 pdb_get_munged_dial(sampass
));
1306 if (need_update(sampass
, PDB_SMBHOME
))
1307 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1308 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1309 pdb_get_homedir(sampass
));
1311 if (need_update(sampass
, PDB_DRIVE
))
1312 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1313 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1314 pdb_get_dir_drive(sampass
));
1316 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1317 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1318 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1319 pdb_get_logon_script(sampass
));
1321 if (need_update(sampass
, PDB_PROFILE
))
1322 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1323 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1324 pdb_get_profile_path(sampass
));
1326 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1329 if (need_update(sampass
, PDB_LOGONTIME
))
1330 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1331 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1334 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1337 if (need_update(sampass
, PDB_LOGOFFTIME
))
1338 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1339 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1342 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1345 if (need_update(sampass
, PDB_KICKOFFTIME
))
1346 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1347 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1350 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1353 if (need_update(sampass
, PDB_CANCHANGETIME
))
1354 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1355 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1358 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_must_change_time(sampass
)) < 0) {
1361 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1362 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1363 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1366 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1367 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1369 if (need_update(sampass
, PDB_LMPASSWD
)) {
1370 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1373 pdb_sethexpwd(pwstr
, lm_pw
,
1374 pdb_get_acct_ctrl(sampass
));
1375 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1376 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1379 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1380 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1384 if (need_update(sampass
, PDB_NTPASSWD
)) {
1385 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1388 pdb_sethexpwd(pwstr
, nt_pw
,
1389 pdb_get_acct_ctrl(sampass
));
1390 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1391 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1394 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1395 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1400 if (need_update(sampass
, PDB_PWHISTORY
)) {
1402 uint32_t pwHistLen
= 0;
1403 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1405 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1409 if (pwHistLen
== 0) {
1410 /* Remove any password history from the LDAP store. */
1411 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1415 uint32_t currHistLen
= 0;
1416 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1417 if (pwhist
!= NULL
) {
1418 /* We can only store (1024-1/64 password history entries. */
1419 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1420 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1421 /* Store the salt. */
1422 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1423 /* Followed by the md5 hash of salt + md4 hash */
1424 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1425 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1426 DEBUG(100, ("pwstr=%s\n", pwstr
));
1430 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1431 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1436 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1437 if (asprintf(&temp
, "%li",
1438 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1441 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1442 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1448 if (need_update(sampass
, PDB_HOURS
)) {
1449 const uint8
*hours
= pdb_get_hours(sampass
);
1452 pdb_sethexhours(hourstr
, hours
);
1453 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1456 get_userattr_key2string(ldap_state
->schema_ver
,
1457 LDAP_ATTR_LOGON_HOURS
),
1462 if (need_update(sampass
, PDB_ACCTCTRL
))
1463 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1464 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1465 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1467 /* password lockout cache:
1468 - If we are now autolocking or clearing, we write to ldap
1469 - If we are clearing, we delete the cache entry
1470 - If the count is > 0, we update the cache
1472 This even means when autolocking, we cache, just in case the
1473 update doesn't work, and we have to cache the autolock flag */
1475 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1476 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1477 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1478 time_t badtime
= pdb_get_bad_password_time(sampass
);
1480 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1482 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1483 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1485 if ((badcount
>= pol
) || (badcount
== 0)) {
1486 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1487 (unsigned int)badcount
, (unsigned int)badtime
));
1488 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1492 ldap_state
->smbldap_state
->ldap_struct
,
1494 get_userattr_key2string(
1495 ldap_state
->schema_ver
,
1496 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1500 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1504 ldap_state
->smbldap_state
->ldap_struct
,
1506 get_userattr_key2string(
1507 ldap_state
->schema_ver
,
1508 LDAP_ATTR_BAD_PASSWORD_TIME
),
1512 if (badcount
== 0) {
1513 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1514 login_cache_delentry(sampass
);
1516 struct login_cache cache_entry
;
1518 cache_entry
.entry_timestamp
= time(NULL
);
1519 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1520 cache_entry
.bad_password_count
= badcount
;
1521 cache_entry
.bad_password_time
= badtime
;
1523 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1524 login_cache_write(sampass
, &cache_entry
);
1531 /**********************************************************************
1532 End enumeration of the LDAP password list.
1533 *********************************************************************/
1535 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1537 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1538 if (ldap_state
->result
) {
1539 ldap_msgfree(ldap_state
->result
);
1540 ldap_state
->result
= NULL
;
1544 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1545 const char *new_attr
)
1549 if (new_attr
== NULL
) {
1553 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1557 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1559 SMB_ASSERT((*attr_list
) != NULL
);
1560 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1561 (*attr_list
)[i
+1] = NULL
;
1564 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1565 const char ***attr_list
)
1567 append_attr(mem_ctx
, attr_list
, "uidNumber");
1568 append_attr(mem_ctx
, attr_list
, "gidNumber");
1569 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1570 append_attr(mem_ctx
, attr_list
, "loginShell");
1571 append_attr(mem_ctx
, attr_list
, "gecos");
1574 /**********************************************************************
1575 Get struct samu entry from LDAP by username.
1576 *********************************************************************/
1578 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1580 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1581 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1582 LDAPMessage
*result
= NULL
;
1583 LDAPMessage
*entry
= NULL
;
1585 const char ** attr_list
;
1588 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1589 append_attr(user
, &attr_list
,
1590 get_userattr_key2string(ldap_state
->schema_ver
,
1591 LDAP_ATTR_MOD_TIMESTAMP
));
1592 ldapsam_add_unix_attributes(user
, &attr_list
);
1593 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1595 TALLOC_FREE( attr_list
);
1597 if ( rc
!= LDAP_SUCCESS
)
1598 return NT_STATUS_NO_SUCH_USER
;
1600 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1603 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1604 ldap_msgfree(result
);
1605 return NT_STATUS_NO_SUCH_USER
;
1606 } else if (count
> 1) {
1607 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1608 ldap_msgfree(result
);
1609 return NT_STATUS_NO_SUCH_USER
;
1612 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1614 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1615 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1616 ldap_msgfree(result
);
1617 return NT_STATUS_NO_SUCH_USER
;
1619 pdb_set_backend_private_data(user
, result
, NULL
,
1620 my_methods
, PDB_CHANGED
);
1621 talloc_autofree_ldapmsg(user
, result
);
1624 ldap_msgfree(result
);
1629 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1630 const struct dom_sid
*sid
, LDAPMessage
**result
)
1633 const char ** attr_list
;
1636 switch ( ldap_state
->schema_ver
) {
1637 case SCHEMAVER_SAMBASAMACCOUNT
: {
1638 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1639 if (tmp_ctx
== NULL
) {
1640 return LDAP_NO_MEMORY
;
1643 attr_list
= get_userattr_list(tmp_ctx
,
1644 ldap_state
->schema_ver
);
1645 append_attr(tmp_ctx
, &attr_list
,
1646 get_userattr_key2string(
1647 ldap_state
->schema_ver
,
1648 LDAP_ATTR_MOD_TIMESTAMP
));
1649 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1650 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1652 TALLOC_FREE(tmp_ctx
);
1654 if ( rc
!= LDAP_SUCCESS
)
1659 case SCHEMAVER_SAMBAACCOUNT
:
1660 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1664 attr_list
= get_userattr_list(NULL
,
1665 ldap_state
->schema_ver
);
1666 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1667 TALLOC_FREE( attr_list
);
1669 if ( rc
!= LDAP_SUCCESS
)
1676 /**********************************************************************
1677 Get struct samu entry from LDAP by SID.
1678 *********************************************************************/
1680 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1682 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1683 LDAPMessage
*result
= NULL
;
1684 LDAPMessage
*entry
= NULL
;
1688 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1690 if (rc
!= LDAP_SUCCESS
)
1691 return NT_STATUS_NO_SUCH_USER
;
1693 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1696 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1697 "count=%d\n", sid_string_dbg(sid
), count
));
1698 ldap_msgfree(result
);
1699 return NT_STATUS_NO_SUCH_USER
;
1700 } else if (count
> 1) {
1701 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1702 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1704 ldap_msgfree(result
);
1705 return NT_STATUS_NO_SUCH_USER
;
1708 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1710 ldap_msgfree(result
);
1711 return NT_STATUS_NO_SUCH_USER
;
1714 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1715 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1716 ldap_msgfree(result
);
1717 return NT_STATUS_NO_SUCH_USER
;
1720 pdb_set_backend_private_data(user
, result
, NULL
,
1721 my_methods
, PDB_CHANGED
);
1722 talloc_autofree_ldapmsg(user
, result
);
1723 return NT_STATUS_OK
;
1726 /********************************************************************
1727 Do the actual modification - also change a plaintext passord if
1729 **********************************************************************/
1731 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1732 struct samu
*newpwd
, char *dn
,
1733 LDAPMod
**mods
, int ldap_op
,
1734 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1736 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1739 if (!newpwd
|| !dn
) {
1740 return NT_STATUS_INVALID_PARAMETER
;
1743 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1744 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1745 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1746 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1749 char *retoid
= NULL
;
1750 struct berval
*retdata
= NULL
;
1751 char *utf8_password
;
1753 size_t converted_size
;
1756 if (!ldap_state
->is_nds_ldap
) {
1758 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1759 LDAP_EXOP_MODIFY_PASSWD
)) {
1760 DEBUG(2, ("ldap password change requested, but LDAP "
1761 "server does not support it -- ignoring\n"));
1762 return NT_STATUS_OK
;
1766 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1767 pdb_get_plaintext_passwd(newpwd
),
1770 return NT_STATUS_NO_MEMORY
;
1773 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1774 TALLOC_FREE(utf8_password
);
1775 return NT_STATUS_NO_MEMORY
;
1778 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1779 DEBUG(0,("ber_alloc_t returns NULL\n"));
1780 TALLOC_FREE(utf8_password
);
1781 TALLOC_FREE(utf8_dn
);
1782 return NT_STATUS_UNSUCCESSFUL
;
1785 if ((ber_printf (ber
, "{") < 0) ||
1786 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1788 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1791 TALLOC_FREE(utf8_dn
);
1792 TALLOC_FREE(utf8_password
);
1793 return NT_STATUS_UNSUCCESSFUL
;
1796 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1797 ret
= ber_printf(ber
, "ts}",
1798 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1801 ret
= ber_printf(ber
, "}");
1805 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1808 TALLOC_FREE(utf8_dn
);
1809 TALLOC_FREE(utf8_password
);
1810 return NT_STATUS_UNSUCCESSFUL
;
1813 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1814 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1816 TALLOC_FREE(utf8_dn
);
1817 TALLOC_FREE(utf8_password
);
1818 return NT_STATUS_UNSUCCESSFUL
;
1821 TALLOC_FREE(utf8_dn
);
1822 TALLOC_FREE(utf8_password
);
1825 if (!ldap_state
->is_nds_ldap
) {
1826 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1827 LDAP_EXOP_MODIFY_PASSWD
,
1828 bv
, NULL
, NULL
, &retoid
,
1831 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1832 pdb_get_plaintext_passwd(newpwd
));
1834 if (rc
!= LDAP_SUCCESS
) {
1835 char *ld_error
= NULL
;
1837 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1838 DEBUG(3, ("Could not set userPassword "
1839 "attribute due to an objectClass "
1840 "violation -- ignoring\n"));
1842 return NT_STATUS_OK
;
1845 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1847 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1848 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1849 SAFE_FREE(ld_error
);
1851 #if defined(LDAP_CONSTRAINT_VIOLATION)
1852 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1853 return NT_STATUS_PASSWORD_RESTRICTION
;
1855 return NT_STATUS_UNSUCCESSFUL
;
1857 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1858 #ifdef DEBUG_PASSWORD
1859 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1862 ber_bvfree(retdata
);
1864 ldap_memfree(retoid
);
1870 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1871 /* may be password change below however */
1875 if (ldap_state
->is_nds_ldap
) {
1876 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1880 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1884 rc
= smbldap_add(ldap_state
->smbldap_state
,
1887 case LDAP_MOD_REPLACE
:
1888 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1892 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1894 return NT_STATUS_INVALID_PARAMETER
;
1897 if (rc
!=LDAP_SUCCESS
) {
1898 return NT_STATUS_UNSUCCESSFUL
;
1902 return NT_STATUS_OK
;
1905 /**********************************************************************
1906 Delete entry from LDAP for username.
1907 *********************************************************************/
1909 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1910 struct samu
* sam_acct
)
1912 struct ldapsam_privates
*priv
=
1913 (struct ldapsam_privates
*)my_methods
->private_data
;
1916 LDAPMessage
*msg
, *entry
;
1917 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1918 const char **attr_list
;
1919 TALLOC_CTX
*mem_ctx
;
1922 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1923 return NT_STATUS_INVALID_PARAMETER
;
1926 sname
= pdb_get_username(sam_acct
);
1928 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1931 mem_ctx
= talloc_new(NULL
);
1932 if (mem_ctx
== NULL
) {
1933 DEBUG(0, ("talloc_new failed\n"));
1937 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1938 if (attr_list
== NULL
) {
1942 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1944 if ((rc
!= LDAP_SUCCESS
) ||
1945 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1946 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1947 DEBUG(5, ("Could not find user %s\n", sname
));
1948 result
= NT_STATUS_NO_SUCH_USER
;
1952 rc
= ldapsam_delete_entry(
1953 priv
, mem_ctx
, entry
,
1954 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1955 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1958 result
= (rc
== LDAP_SUCCESS
) ?
1959 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1962 TALLOC_FREE(mem_ctx
);
1966 /**********************************************************************
1967 Helper function to determine for update_sam_account whether
1968 we need LDAP modification.
1969 *********************************************************************/
1971 static bool element_is_changed(const struct samu
*sampass
,
1972 enum pdb_elements element
)
1974 return IS_SAM_CHANGED(sampass
, element
);
1977 /**********************************************************************
1979 *********************************************************************/
1981 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1983 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1984 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1987 LDAPMessage
*result
= NULL
;
1988 LDAPMessage
*entry
= NULL
;
1989 LDAPMod
**mods
= NULL
;
1990 const char **attr_list
;
1992 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1994 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1995 if (pdb_get_username(newpwd
) == NULL
) {
1996 return NT_STATUS_INVALID_PARAMETER
;
1998 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1999 TALLOC_FREE( attr_list
);
2000 if (rc
!= LDAP_SUCCESS
) {
2001 return NT_STATUS_UNSUCCESSFUL
;
2003 pdb_set_backend_private_data(newpwd
, result
, NULL
,
2004 my_methods
, PDB_CHANGED
);
2005 talloc_autofree_ldapmsg(newpwd
, result
);
2008 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
2009 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
2010 return NT_STATUS_UNSUCCESSFUL
;
2013 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2014 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
2016 return NT_STATUS_UNSUCCESSFUL
;
2019 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
2021 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2022 element_is_changed
)) {
2023 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2026 ldap_mods_free(mods
,True
);
2027 return NT_STATUS_UNSUCCESSFUL
;
2030 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
2031 && (mods
== NULL
)) {
2032 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2033 pdb_get_username(newpwd
)));
2035 return NT_STATUS_OK
;
2038 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
2041 ldap_mods_free(mods
,True
);
2047 * We need to set the backend private data to NULL here. For example
2048 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2049 * same one, and with the explicit delete / add logic for attribute
2050 * values the second time we would use the wrong "old" value which
2051 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2053 * The existing LDAPMessage is still being auto-freed by the
2056 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2059 if (!NT_STATUS_IS_OK(ret
)) {
2063 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2064 pdb_get_username(newpwd
)));
2065 return NT_STATUS_OK
;
2068 /***************************************************************************
2069 Renames a struct samu
2070 - The "rename user script" has full responsibility for changing everything
2071 ***************************************************************************/
2073 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2074 TALLOC_CTX
*tmp_ctx
,
2076 uint32_t member_rid
);
2078 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2079 TALLOC_CTX
*mem_ctx
,
2081 struct dom_sid
**pp_sids
,
2083 size_t *p_num_groups
);
2085 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2086 struct samu
*old_acct
,
2087 const char *newname
)
2089 const char *oldname
;
2091 char *rename_script
= NULL
;
2092 fstring oldname_lower
, newname_lower
;
2095 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2096 return NT_STATUS_INVALID_PARAMETER
;
2099 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2100 return NT_STATUS_INVALID_PARAMETER
;
2103 oldname
= pdb_get_username(old_acct
);
2105 /* rename the posix user */
2106 rename_script
= SMB_STRDUP(lp_renameuser_script());
2107 if (rename_script
== NULL
) {
2108 return NT_STATUS_NO_MEMORY
;
2111 if (!(*rename_script
)) {
2112 SAFE_FREE(rename_script
);
2113 return NT_STATUS_ACCESS_DENIED
;
2116 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2119 /* We have to allow the account name to end with a '$'.
2120 Also, follow the semantics in _samr_create_user() and lower case the
2121 posix name but preserve the case in passdb */
2123 fstrcpy( oldname_lower
, oldname
);
2124 strlower_m( oldname_lower
);
2125 fstrcpy( newname_lower
, newname
);
2126 strlower_m( newname_lower
);
2127 rename_script
= realloc_string_sub2(rename_script
,
2132 if (!rename_script
) {
2133 return NT_STATUS_NO_MEMORY
;
2135 rename_script
= realloc_string_sub2(rename_script
,
2140 rc
= smbrun(rename_script
, NULL
);
2142 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2143 rename_script
, rc
));
2145 SAFE_FREE(rename_script
);
2148 smb_nscd_flush_user_cache();
2152 return NT_STATUS_UNSUCCESSFUL
;
2154 return NT_STATUS_OK
;
2157 /**********************************************************************
2158 Helper function to determine for update_sam_account whether
2159 we need LDAP modification.
2160 *********************************************************************/
2162 static bool element_is_set_or_changed(const struct samu
*sampass
,
2163 enum pdb_elements element
)
2165 return (IS_SAM_SET(sampass
, element
) ||
2166 IS_SAM_CHANGED(sampass
, element
));
2169 /**********************************************************************
2170 Add struct samu to LDAP.
2171 *********************************************************************/
2173 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2175 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2176 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2178 LDAPMessage
*result
= NULL
;
2179 LDAPMessage
*entry
= NULL
;
2180 LDAPMod
**mods
= NULL
;
2181 int ldap_op
= LDAP_MOD_REPLACE
;
2182 uint32_t num_result
;
2183 const char **attr_list
;
2184 char *escape_user
= NULL
;
2185 const char *username
= pdb_get_username(newpwd
);
2186 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2187 char *filter
= NULL
;
2189 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2190 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2193 return NT_STATUS_NO_MEMORY
;
2196 if (!username
|| !*username
) {
2197 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2198 status
= NT_STATUS_INVALID_PARAMETER
;
2202 /* free this list after the second search or in case we exit on failure */
2203 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2205 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2207 if (rc
!= LDAP_SUCCESS
) {
2211 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2212 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2216 ldap_msgfree(result
);
2219 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2220 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2222 if (rc
== LDAP_SUCCESS
) {
2223 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2224 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2225 "already in the base, with samba "
2226 "attributes\n", sid_string_dbg(sid
)));
2229 ldap_msgfree(result
);
2234 /* does the entry already exist but without a samba attributes?
2235 we need to return the samba attributes here */
2237 escape_user
= escape_ldap_string(talloc_tos(), username
);
2238 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2240 status
= NT_STATUS_NO_MEMORY
;
2243 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2244 TALLOC_FREE(escape_user
);
2246 status
= NT_STATUS_NO_MEMORY
;
2250 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2251 filter
, attr_list
, &result
);
2252 if ( rc
!= LDAP_SUCCESS
) {
2256 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2258 if (num_result
> 1) {
2259 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2263 /* Check if we need to update an existing entry */
2264 if (num_result
== 1) {
2265 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2266 ldap_op
= LDAP_MOD_REPLACE
;
2267 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2268 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2270 status
= NT_STATUS_NO_MEMORY
;
2274 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2276 /* There might be a SID for this account already - say an idmap entry */
2278 filter
= talloc_asprintf(ctx
,
2279 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2280 get_userattr_key2string(ldap_state
->schema_ver
,
2281 LDAP_ATTR_USER_SID
),
2282 sid_string_talloc(ctx
, sid
),
2283 LDAP_OBJ_IDMAP_ENTRY
,
2284 LDAP_OBJ_SID_ENTRY
);
2286 status
= NT_STATUS_NO_MEMORY
;
2290 /* free old result before doing a new search */
2291 if (result
!= NULL
) {
2292 ldap_msgfree(result
);
2295 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2296 filter
, attr_list
, &result
);
2298 if ( rc
!= LDAP_SUCCESS
) {
2302 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2304 if (num_result
> 1) {
2305 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2309 /* Check if we need to update an existing entry */
2310 if (num_result
== 1) {
2312 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2313 ldap_op
= LDAP_MOD_REPLACE
;
2314 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2315 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2317 status
= NT_STATUS_NO_MEMORY
;
2323 if (num_result
== 0) {
2324 char *escape_username
;
2325 /* Check if we need to add an entry */
2326 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2327 ldap_op
= LDAP_MOD_ADD
;
2329 escape_username
= escape_rdn_val_string_alloc(username
);
2330 if (!escape_username
) {
2331 status
= NT_STATUS_NO_MEMORY
;
2335 if (username
[strlen(username
)-1] == '$') {
2336 dn
= talloc_asprintf(ctx
,
2339 lp_ldap_machine_suffix());
2341 dn
= talloc_asprintf(ctx
,
2344 lp_ldap_user_suffix());
2347 SAFE_FREE(escape_username
);
2349 status
= NT_STATUS_NO_MEMORY
;
2354 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2355 element_is_set_or_changed
)) {
2356 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2358 ldap_mods_free(mods
, true);
2364 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2367 switch ( ldap_state
->schema_ver
) {
2368 case SCHEMAVER_SAMBAACCOUNT
:
2369 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2371 case SCHEMAVER_SAMBASAMACCOUNT
:
2372 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2375 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2379 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2380 if (!NT_STATUS_IS_OK(ret
)) {
2381 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2382 pdb_get_username(newpwd
),dn
));
2383 ldap_mods_free(mods
, true);
2387 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2388 ldap_mods_free(mods
, true);
2390 status
= NT_STATUS_OK
;
2396 ldap_msgfree(result
);
2401 /**********************************************************************
2402 *********************************************************************/
2404 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2406 LDAPMessage
** result
)
2408 int scope
= LDAP_SCOPE_SUBTREE
;
2410 const char **attr_list
;
2412 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2413 rc
= smbldap_search(ldap_state
->smbldap_state
,
2414 lp_ldap_suffix (), scope
,
2415 filter
, attr_list
, 0, result
);
2416 TALLOC_FREE(attr_list
);
2421 /**********************************************************************
2422 *********************************************************************/
2424 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2425 GROUP_MAP
*map
, LDAPMessage
*entry
)
2428 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2430 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2431 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2432 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2437 temp
= smbldap_talloc_single_attribute(
2438 ldap_state
->smbldap_state
->ldap_struct
,
2440 get_attr_key2string(groupmap_attr_list
,
2441 LDAP_ATTR_GIDNUMBER
),
2444 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2445 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2449 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2451 map
->gid
= (gid_t
)atol(temp
);
2454 temp
= smbldap_talloc_single_attribute(
2455 ldap_state
->smbldap_state
->ldap_struct
,
2457 get_attr_key2string(groupmap_attr_list
,
2458 LDAP_ATTR_GROUP_SID
),
2461 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2462 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2467 if (!string_to_sid(&map
->sid
, temp
)) {
2468 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2474 temp
= smbldap_talloc_single_attribute(
2475 ldap_state
->smbldap_state
->ldap_struct
,
2477 get_attr_key2string(groupmap_attr_list
,
2478 LDAP_ATTR_GROUP_TYPE
),
2481 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2482 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2486 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2488 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2489 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2490 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2496 temp
= smbldap_talloc_single_attribute(
2497 ldap_state
->smbldap_state
->ldap_struct
,
2499 get_attr_key2string(groupmap_attr_list
,
2500 LDAP_ATTR_DISPLAY_NAME
),
2503 temp
= smbldap_talloc_single_attribute(
2504 ldap_state
->smbldap_state
->ldap_struct
,
2506 get_attr_key2string(groupmap_attr_list
,
2510 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2511 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2516 fstrcpy(map
->nt_name
, temp
);
2519 temp
= smbldap_talloc_single_attribute(
2520 ldap_state
->smbldap_state
->ldap_struct
,
2522 get_attr_key2string(groupmap_attr_list
,
2526 temp
= talloc_strdup(ctx
, "");
2532 fstrcpy(map
->comment
, temp
);
2534 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2535 store_gid_sid_cache(&map
->sid
, map
->gid
);
2536 idmap_cache_set_sid2gid(&map
->sid
, map
->gid
);
2543 /**********************************************************************
2544 *********************************************************************/
2546 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2550 struct ldapsam_privates
*ldap_state
=
2551 (struct ldapsam_privates
*)methods
->private_data
;
2552 LDAPMessage
*result
= NULL
;
2553 LDAPMessage
*entry
= NULL
;
2556 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2558 return NT_STATUS_NO_SUCH_GROUP
;
2561 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2564 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2566 ldap_msgfree(result
);
2567 return NT_STATUS_NO_SUCH_GROUP
;
2571 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2572 "count=%d\n", filter
, count
));
2573 ldap_msgfree(result
);
2574 return NT_STATUS_NO_SUCH_GROUP
;
2577 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2580 ldap_msgfree(result
);
2581 return NT_STATUS_UNSUCCESSFUL
;
2584 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2585 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2586 "group filter %s\n", filter
));
2587 ldap_msgfree(result
);
2588 return NT_STATUS_NO_SUCH_GROUP
;
2591 ldap_msgfree(result
);
2592 return NT_STATUS_OK
;
2595 /**********************************************************************
2596 *********************************************************************/
2598 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2601 char *filter
= NULL
;
2605 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2607 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2608 sid_to_fstring(tmp
, &sid
)) < 0) {
2609 return NT_STATUS_NO_MEMORY
;
2612 status
= ldapsam_getgroup(methods
, filter
, map
);
2617 /**********************************************************************
2618 *********************************************************************/
2620 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2623 char *filter
= NULL
;
2626 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2628 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2629 (unsigned long)gid
) < 0) {
2630 return NT_STATUS_NO_MEMORY
;
2633 status
= ldapsam_getgroup(methods
, filter
, map
);
2638 /**********************************************************************
2639 *********************************************************************/
2641 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2644 char *filter
= NULL
;
2645 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2649 return NT_STATUS_NO_MEMORY
;
2652 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2654 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2655 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2657 TALLOC_FREE(escape_name
);
2658 return NT_STATUS_NO_MEMORY
;
2661 TALLOC_FREE(escape_name
);
2662 status
= ldapsam_getgroup(methods
, filter
, map
);
2667 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2669 const struct dom_sid
*domain_sid
,
2675 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2676 str
, sizeof(str
)-1)) {
2677 DEBUG(10, ("Could not find sambaSID attribute\n"));
2681 if (!string_to_sid(&sid
, str
)) {
2682 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2686 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2687 DEBUG(10, ("SID %s is not in expected domain %s\n",
2688 str
, sid_string_dbg(domain_sid
)));
2692 if (!sid_peek_rid(&sid
, rid
)) {
2693 DEBUG(10, ("Could not peek into RID\n"));
2700 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2701 TALLOC_CTX
*mem_ctx
,
2702 const struct dom_sid
*group
,
2703 uint32_t **pp_member_rids
,
2704 size_t *p_num_members
)
2706 struct ldapsam_privates
*ldap_state
=
2707 (struct ldapsam_privates
*)methods
->private_data
;
2708 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2709 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2710 const char *sid_attrs
[] = { "sambaSID", NULL
};
2711 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2712 LDAPMessage
*result
= NULL
;
2715 char **values
= NULL
;
2720 *pp_member_rids
= NULL
;
2723 filter
= talloc_asprintf(mem_ctx
,
2724 "(&(objectClass=%s)"
2727 LDAP_OBJ_POSIXGROUP
,
2729 sid_string_talloc(mem_ctx
, group
));
2730 if (filter
== NULL
) {
2731 ret
= NT_STATUS_NO_MEMORY
;
2735 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2736 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2739 if (rc
!= LDAP_SUCCESS
)
2742 talloc_autofree_ldapmsg(mem_ctx
, result
);
2744 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2747 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2748 sid_string_dbg(group
)));
2749 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2754 ret
= NT_STATUS_NO_SUCH_GROUP
;
2758 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2762 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2764 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2765 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2769 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2771 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2773 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2774 if (filter
== NULL
) {
2775 ret
= NT_STATUS_NO_MEMORY
;
2779 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2780 char *escape_memberuid
;
2782 escape_memberuid
= escape_ldap_string(talloc_tos(),
2784 if (escape_memberuid
== NULL
) {
2785 ret
= NT_STATUS_NO_MEMORY
;
2789 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2790 TALLOC_FREE(escape_memberuid
);
2791 if (filter
== NULL
) {
2792 ret
= NT_STATUS_NO_MEMORY
;
2797 filter
= talloc_asprintf_append_buffer(filter
, "))");
2798 if (filter
== NULL
) {
2799 ret
= NT_STATUS_NO_MEMORY
;
2803 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2804 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2807 if (rc
!= LDAP_SUCCESS
)
2810 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2811 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2813 talloc_autofree_ldapmsg(mem_ctx
, result
);
2815 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2817 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2823 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2827 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2828 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2829 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2833 if (!string_to_sid(&sid
, sidstr
))
2836 if (!sid_check_is_in_our_domain(&sid
)) {
2837 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2838 "in our domain\n"));
2839 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2843 sid_peek_rid(&sid
, &rid
);
2845 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2847 ret
= NT_STATUS_NO_MEMORY
;
2853 filter
= talloc_asprintf(mem_ctx
,
2854 "(&(objectClass=%s)"
2856 LDAP_OBJ_SAMBASAMACCOUNT
,
2859 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2860 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2863 if (rc
!= LDAP_SUCCESS
)
2866 talloc_autofree_ldapmsg(mem_ctx
, result
);
2868 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2870 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2874 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2876 get_global_sam_sid(),
2878 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2879 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2883 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2885 ret
= NT_STATUS_NO_MEMORY
;
2895 ldap_value_free(values
);
2900 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2901 TALLOC_CTX
*mem_ctx
,
2903 struct dom_sid
**pp_sids
,
2905 size_t *p_num_groups
)
2907 struct ldapsam_privates
*ldap_state
=
2908 (struct ldapsam_privates
*)methods
->private_data
;
2909 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2911 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2914 LDAPMessage
*result
= NULL
;
2916 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2920 gid_t primary_gid
= -1;
2925 if (pdb_get_username(user
) == NULL
) {
2926 return NT_STATUS_INVALID_PARAMETER
;
2929 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2930 if (escape_name
== NULL
)
2931 return NT_STATUS_NO_MEMORY
;
2933 if (user
->unix_pw
) {
2934 primary_gid
= user
->unix_pw
->pw_gid
;
2936 /* retrieve the users primary gid */
2937 filter
= talloc_asprintf(mem_ctx
,
2938 "(&(objectClass=%s)(uid=%s))",
2939 LDAP_OBJ_SAMBASAMACCOUNT
,
2941 if (filter
== NULL
) {
2942 ret
= NT_STATUS_NO_MEMORY
;
2946 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2947 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2949 if (rc
!= LDAP_SUCCESS
)
2952 talloc_autofree_ldapmsg(mem_ctx
, result
);
2954 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2958 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2959 ret
= NT_STATUS_NO_SUCH_USER
;
2962 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2964 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2966 DEBUG (1, ("Unable to find the member's gid!\n"));
2967 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2970 primary_gid
= strtoul(gidstr
, NULL
, 10);
2973 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2974 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2979 filter
= talloc_asprintf(mem_ctx
,
2980 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2981 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2982 if (filter
== NULL
) {
2983 ret
= NT_STATUS_NO_MEMORY
;
2987 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2988 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2990 if (rc
!= LDAP_SUCCESS
)
2993 talloc_autofree_ldapmsg(mem_ctx
, result
);
3001 /* We need to add the primary group as the first gid/sid */
3003 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
3004 ret
= NT_STATUS_NO_MEMORY
;
3008 /* This sid will be replaced later */
3010 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
3012 if (!NT_STATUS_IS_OK(ret
)) {
3016 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
3018 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
3025 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3027 str
, sizeof(str
)-1))
3030 if (!string_to_sid(&sid
, str
))
3033 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3035 str
, sizeof(str
)-1))
3038 gid
= strtoul(str
, &end
, 10);
3040 if (PTR_DIFF(end
, str
) != strlen(str
))
3043 if (gid
== primary_gid
) {
3044 sid_copy(&(*pp_sids
)[0], &sid
);
3046 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3048 ret
= NT_STATUS_NO_MEMORY
;
3051 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3053 if (!NT_STATUS_IS_OK(ret
)) {
3059 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3060 DEBUG(3, ("primary group of [%s] not found\n",
3061 pdb_get_username(user
)));
3065 *p_num_groups
= num_sids
;
3071 TALLOC_FREE(escape_name
);
3075 /**********************************************************************
3076 * Augment a posixGroup object with a sambaGroupMapping domgroup
3077 *********************************************************************/
3079 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3080 struct ldapsam_privates
*ldap_state
,
3083 const char *filter
, *dn
;
3084 LDAPMessage
*msg
, *entry
;
3088 filter
= talloc_asprintf(mem_ctx
,
3089 "(&(objectClass=%s)(gidNumber=%u))",
3090 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3091 if (filter
== NULL
) {
3092 return NT_STATUS_NO_MEMORY
;
3095 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3096 get_attr_list(mem_ctx
, groupmap_attr_list
),
3098 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3100 if ((rc
!= LDAP_SUCCESS
) ||
3101 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3102 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3103 return NT_STATUS_NO_SUCH_GROUP
;
3106 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3108 return NT_STATUS_NO_MEMORY
;
3112 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3114 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3115 sid_string_talloc(mem_ctx
, &map
->sid
));
3116 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3117 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3118 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3120 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3122 talloc_autofree_ldapmod(mem_ctx
, mods
);
3124 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3125 if (rc
!= LDAP_SUCCESS
) {
3126 return NT_STATUS_ACCESS_DENIED
;
3129 return NT_STATUS_OK
;
3132 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3135 struct ldapsam_privates
*ldap_state
=
3136 (struct ldapsam_privates
*)methods
->private_data
;
3137 LDAPMessage
*msg
= NULL
;
3138 LDAPMod
**mods
= NULL
;
3139 const char *attrs
[] = { NULL
};
3143 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 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3157 sid_string_talloc(mem_ctx
, &map
->sid
));
3158 if (filter
== NULL
) {
3159 result
= NT_STATUS_NO_MEMORY
;
3163 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3164 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3165 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3167 if ((rc
== LDAP_SUCCESS
) &&
3168 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3170 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3171 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3172 result
= NT_STATUS_GROUP_EXISTS
;
3176 switch (map
->sid_name_use
) {
3178 case SID_NAME_DOM_GRP
:
3179 /* To map a domain group we need to have a posix group
3181 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3185 case SID_NAME_ALIAS
:
3186 if (!sid_check_is_in_our_domain(&map
->sid
)
3187 && !sid_check_is_in_builtin(&map
->sid
) )
3189 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3190 sid_string_dbg(&map
->sid
)));
3191 result
= NT_STATUS_INVALID_PARAMETER
;
3197 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3198 sid_type_lookup(map
->sid_name_use
)));
3199 result
= NT_STATUS_INVALID_PARAMETER
;
3203 /* Domain groups have been mapped in a separate routine, we have to
3204 * create an alias now */
3206 if (map
->gid
== -1) {
3207 DEBUG(10, ("Refusing to map gid==-1\n"));
3208 result
= NT_STATUS_INVALID_PARAMETER
;
3212 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3213 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3214 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3215 result
= NT_STATUS_GROUP_EXISTS
;
3219 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3220 * the best we can get out of LDAP. */
3222 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3223 sid_string_talloc(mem_ctx
, &map
->sid
),
3224 lp_ldap_group_suffix());
3226 result
= NT_STATUS_NO_MEMORY
;
3232 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3233 LDAP_OBJ_SID_ENTRY
);
3234 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3236 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3237 sid_string_talloc(mem_ctx
, &map
->sid
));
3238 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3239 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3240 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3242 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3244 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3245 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3246 talloc_autofree_ldapmod(mem_ctx
, mods
);
3248 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3250 result
= (rc
== LDAP_SUCCESS
) ?
3251 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3254 TALLOC_FREE(mem_ctx
);
3258 /**********************************************************************
3259 * Update a group mapping entry. We're quite strict about what can be changed:
3260 * Only the description and displayname may be changed. It simply does not
3261 * make any sense to change the SID, gid or the type in a mapping.
3262 *********************************************************************/
3264 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3267 struct ldapsam_privates
*ldap_state
=
3268 (struct ldapsam_privates
*)methods
->private_data
;
3270 const char *filter
, *dn
;
3271 LDAPMessage
*msg
= NULL
;
3272 LDAPMessage
*entry
= NULL
;
3273 LDAPMod
**mods
= NULL
;
3274 TALLOC_CTX
*mem_ctx
;
3277 mem_ctx
= talloc_new(NULL
);
3278 if (mem_ctx
== NULL
) {
3279 DEBUG(0, ("talloc_new failed\n"));
3280 return NT_STATUS_NO_MEMORY
;
3283 /* Make 100% sure that sid, gid and type are not changed by looking up
3284 * exactly the values we're given in LDAP. */
3286 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3287 "(sambaSid=%s)(gidNumber=%u)"
3288 "(sambaGroupType=%d))",
3290 sid_string_talloc(mem_ctx
, &map
->sid
),
3291 (unsigned int)map
->gid
, map
->sid_name_use
);
3292 if (filter
== NULL
) {
3293 result
= NT_STATUS_NO_MEMORY
;
3297 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3298 get_attr_list(mem_ctx
, groupmap_attr_list
),
3300 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3302 if ((rc
!= LDAP_SUCCESS
) ||
3303 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3304 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3305 result
= NT_STATUS_NO_SUCH_GROUP
;
3309 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3312 result
= NT_STATUS_NO_MEMORY
;
3317 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3319 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3321 talloc_autofree_ldapmod(mem_ctx
, mods
);
3324 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3325 "nothing to do\n"));
3326 result
= NT_STATUS_OK
;
3330 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3332 if (rc
!= LDAP_SUCCESS
) {
3333 result
= NT_STATUS_ACCESS_DENIED
;
3337 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3338 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3340 result
= NT_STATUS_OK
;
3343 TALLOC_FREE(mem_ctx
);
3347 /**********************************************************************
3348 *********************************************************************/
3350 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3353 struct ldapsam_privates
*priv
=
3354 (struct ldapsam_privates
*)methods
->private_data
;
3355 LDAPMessage
*msg
, *entry
;
3358 TALLOC_CTX
*mem_ctx
;
3361 mem_ctx
= talloc_new(NULL
);
3362 if (mem_ctx
== NULL
) {
3363 DEBUG(0, ("talloc_new failed\n"));
3364 return NT_STATUS_NO_MEMORY
;
3367 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3368 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3369 sid_string_talloc(mem_ctx
, &sid
));
3370 if (filter
== NULL
) {
3371 result
= NT_STATUS_NO_MEMORY
;
3374 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3375 get_attr_list(mem_ctx
, groupmap_attr_list
),
3377 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3379 if ((rc
!= LDAP_SUCCESS
) ||
3380 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3381 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3382 result
= NT_STATUS_NO_SUCH_GROUP
;
3386 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3387 get_attr_list(mem_ctx
,
3388 groupmap_attr_list_to_delete
));
3390 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3391 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3392 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3393 const char *attrs
[] = { "sambaGroupType", "description",
3394 "displayName", "sambaSIDList",
3397 /* Second try. Don't delete the sambaSID attribute, this is
3398 for "old" entries that are tacked on a winbind
3401 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3402 LDAP_OBJ_GROUPMAP
, attrs
);
3405 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3406 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3407 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3408 const char *attrs
[] = { "sambaGroupType", "description",
3409 "displayName", "sambaSIDList",
3410 "gidNumber", NULL
};
3412 /* Third try. This is a post-3.0.21 alias (containing only
3413 * sambaSidEntry and sambaGroupMapping classes), we also have
3414 * to delete the gidNumber attribute, only the sambaSidEntry
3417 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3418 LDAP_OBJ_GROUPMAP
, attrs
);
3421 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3424 TALLOC_FREE(mem_ctx
);
3428 /**********************************************************************
3429 *********************************************************************/
3431 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3434 struct ldapsam_privates
*ldap_state
=
3435 (struct ldapsam_privates
*)my_methods
->private_data
;
3436 char *filter
= NULL
;
3438 const char **attr_list
;
3440 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3442 return NT_STATUS_NO_MEMORY
;
3444 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3445 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3446 LDAP_SCOPE_SUBTREE
, filter
,
3447 attr_list
, 0, &ldap_state
->result
);
3448 TALLOC_FREE(attr_list
);
3450 if (rc
!= LDAP_SUCCESS
) {
3451 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3452 ldap_err2string(rc
)));
3453 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3454 lp_ldap_suffix(), filter
));
3455 ldap_msgfree(ldap_state
->result
);
3456 ldap_state
->result
= NULL
;
3457 TALLOC_FREE(filter
);
3458 return NT_STATUS_UNSUCCESSFUL
;
3461 TALLOC_FREE(filter
);
3463 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3464 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3465 ldap_state
->result
)));
3468 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3469 ldap_state
->result
);
3470 ldap_state
->index
= 0;
3472 return NT_STATUS_OK
;
3475 /**********************************************************************
3476 *********************************************************************/
3478 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3480 ldapsam_endsampwent(my_methods
);
3483 /**********************************************************************
3484 *********************************************************************/
3486 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3489 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3490 struct ldapsam_privates
*ldap_state
=
3491 (struct ldapsam_privates
*)my_methods
->private_data
;
3495 if (!ldap_state
->entry
)
3498 ldap_state
->index
++;
3499 bret
= init_group_from_ldap(ldap_state
, map
,
3503 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3507 return NT_STATUS_OK
;
3510 /**********************************************************************
3511 *********************************************************************/
3513 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3514 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3515 GROUP_MAP
**pp_rmap
,
3516 size_t *p_num_entries
,
3525 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3526 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3528 return NT_STATUS_ACCESS_DENIED
;
3531 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3532 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3533 sid_name_use
!= map
.sid_name_use
) {
3534 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3535 "not of the requested type\n", map
.nt_name
));
3538 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3539 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3540 "non mapped\n", map
.nt_name
));
3544 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3546 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3547 "enlarge group map!\n"));
3548 return NT_STATUS_UNSUCCESSFUL
;
3551 (*pp_rmap
)[entries
] = map
;
3556 ldapsam_endsamgrent(methods
);
3558 *p_num_entries
= entries
;
3560 return NT_STATUS_OK
;
3563 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3564 const struct dom_sid
*alias
,
3565 const struct dom_sid
*member
,
3568 struct ldapsam_privates
*ldap_state
=
3569 (struct ldapsam_privates
*)methods
->private_data
;
3571 LDAPMessage
*result
= NULL
;
3572 LDAPMessage
*entry
= NULL
;
3574 LDAPMod
**mods
= NULL
;
3576 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3579 char *filter
= NULL
;
3581 if (sid_check_is_in_builtin(alias
)) {
3582 type
= SID_NAME_ALIAS
;
3585 if (sid_check_is_in_our_domain(alias
)) {
3586 type
= SID_NAME_ALIAS
;
3589 if (type
== SID_NAME_USE_NONE
) {
3590 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3591 sid_string_dbg(alias
)));
3592 return NT_STATUS_NO_SUCH_ALIAS
;
3595 if (asprintf(&filter
,
3596 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3597 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3599 return NT_STATUS_NO_MEMORY
;
3602 if (ldapsam_search_one_group(ldap_state
, filter
,
3603 &result
) != LDAP_SUCCESS
) {
3605 return NT_STATUS_NO_SUCH_ALIAS
;
3608 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3612 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3613 ldap_msgfree(result
);
3615 return NT_STATUS_NO_SUCH_ALIAS
;
3619 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3620 "filter %s: count=%d\n", filter
, count
));
3621 ldap_msgfree(result
);
3623 return NT_STATUS_NO_SUCH_ALIAS
;
3628 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3632 ldap_msgfree(result
);
3633 return NT_STATUS_UNSUCCESSFUL
;
3636 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3638 ldap_msgfree(result
);
3639 return NT_STATUS_UNSUCCESSFUL
;
3642 smbldap_set_mod(&mods
, modop
,
3643 get_attr_key2string(groupmap_attr_list
,
3644 LDAP_ATTR_SID_LIST
),
3645 sid_to_fstring(tmp
, member
));
3647 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3649 ldap_mods_free(mods
, True
);
3650 ldap_msgfree(result
);
3653 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3654 return NT_STATUS_MEMBER_IN_ALIAS
;
3657 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3658 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3661 if (rc
!= LDAP_SUCCESS
) {
3662 return NT_STATUS_UNSUCCESSFUL
;
3665 return NT_STATUS_OK
;
3668 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3669 const struct dom_sid
*alias
,
3670 const struct dom_sid
*member
)
3672 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3675 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3676 const struct dom_sid
*alias
,
3677 const struct dom_sid
*member
)
3679 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3683 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3684 const struct dom_sid
*alias
,
3685 TALLOC_CTX
*mem_ctx
,
3686 struct dom_sid
**pp_members
,
3687 size_t *p_num_members
)
3689 struct ldapsam_privates
*ldap_state
=
3690 (struct ldapsam_privates
*)methods
->private_data
;
3691 LDAPMessage
*result
= NULL
;
3692 LDAPMessage
*entry
= NULL
;
3694 char **values
= NULL
;
3696 char *filter
= NULL
;
3697 uint32_t num_members
= 0;
3698 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3704 if (sid_check_is_in_builtin(alias
)) {
3705 type
= SID_NAME_ALIAS
;
3708 if (sid_check_is_in_our_domain(alias
)) {
3709 type
= SID_NAME_ALIAS
;
3712 if (type
== SID_NAME_USE_NONE
) {
3713 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3714 sid_string_dbg(alias
)));
3715 return NT_STATUS_NO_SUCH_ALIAS
;
3718 if (asprintf(&filter
,
3719 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3720 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3722 return NT_STATUS_NO_MEMORY
;
3725 if (ldapsam_search_one_group(ldap_state
, filter
,
3726 &result
) != LDAP_SUCCESS
) {
3728 return NT_STATUS_NO_SUCH_ALIAS
;
3731 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3735 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3736 ldap_msgfree(result
);
3738 return NT_STATUS_NO_SUCH_ALIAS
;
3742 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3743 "filter %s: count=%d\n", filter
, count
));
3744 ldap_msgfree(result
);
3746 return NT_STATUS_NO_SUCH_ALIAS
;
3751 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3755 ldap_msgfree(result
);
3756 return NT_STATUS_UNSUCCESSFUL
;
3759 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3761 get_attr_key2string(groupmap_attr_list
,
3762 LDAP_ATTR_SID_LIST
));
3764 if (values
== NULL
) {
3765 ldap_msgfree(result
);
3766 return NT_STATUS_OK
;
3769 count
= ldap_count_values(values
);
3771 for (i
=0; i
<count
; i
++) {
3772 struct dom_sid member
;
3775 if (!string_to_sid(&member
, values
[i
]))
3778 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3780 if (!NT_STATUS_IS_OK(status
)) {
3781 ldap_value_free(values
);
3782 ldap_msgfree(result
);
3787 *p_num_members
= num_members
;
3788 ldap_value_free(values
);
3789 ldap_msgfree(result
);
3791 return NT_STATUS_OK
;
3794 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3795 TALLOC_CTX
*mem_ctx
,
3796 const struct dom_sid
*domain_sid
,
3797 const struct dom_sid
*members
,
3799 uint32_t **pp_alias_rids
,
3800 size_t *p_num_alias_rids
)
3802 struct ldapsam_privates
*ldap_state
=
3803 (struct ldapsam_privates
*)methods
->private_data
;
3806 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3808 LDAPMessage
*result
= NULL
;
3809 LDAPMessage
*entry
= NULL
;
3813 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3814 bool is_builtin
= false;
3815 bool sid_added
= false;
3817 *pp_alias_rids
= NULL
;
3818 *p_num_alias_rids
= 0;
3820 if (sid_check_is_builtin(domain_sid
)) {
3822 type
= SID_NAME_ALIAS
;
3825 if (sid_check_is_domain(domain_sid
)) {
3826 type
= SID_NAME_ALIAS
;
3829 if (type
== SID_NAME_USE_NONE
) {
3830 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3831 sid_string_dbg(domain_sid
)));
3832 return NT_STATUS_UNSUCCESSFUL
;
3835 if (num_members
== 0) {
3836 return NT_STATUS_OK
;
3839 filter
= talloc_asprintf(mem_ctx
,
3840 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3841 LDAP_OBJ_GROUPMAP
, type
);
3843 for (i
=0; i
<num_members
; i
++)
3844 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3846 sid_string_talloc(mem_ctx
,
3849 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3851 if (filter
== NULL
) {
3852 return NT_STATUS_NO_MEMORY
;
3856 ldap_state
->search_cache
.filter
&&
3857 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3858 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3859 result
= ldap_state
->search_cache
.result
;
3860 ldap_state
->search_cache
.result
= NULL
;
3862 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3863 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3864 if (rc
!= LDAP_SUCCESS
) {
3865 return NT_STATUS_UNSUCCESSFUL
;
3867 talloc_autofree_ldapmsg(filter
, result
);
3870 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3872 for (entry
= ldap_first_entry(ldap_struct
, result
);
3874 entry
= ldap_next_entry(ldap_struct
, entry
))
3880 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3886 if (!string_to_sid(&sid
, sid_str
))
3889 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3894 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3895 p_num_alias_rids
)) {
3896 return NT_STATUS_NO_MEMORY
;
3900 if (!is_builtin
&& !sid_added
) {
3901 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3903 * Note: result is a talloc child of filter because of the
3904 * talloc_autofree_ldapmsg() usage
3906 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3907 ldap_state
->search_cache
.result
= result
;
3910 return NT_STATUS_OK
;
3913 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3914 enum pdb_policy_type type
,
3917 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3919 LDAPMod
**mods
= NULL
;
3920 fstring value_string
;
3921 const char *policy_attr
= NULL
;
3923 struct ldapsam_privates
*ldap_state
=
3924 (struct ldapsam_privates
*)methods
->private_data
;
3926 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3928 if (!ldap_state
->domain_dn
) {
3929 return NT_STATUS_INVALID_PARAMETER
;
3932 policy_attr
= get_account_policy_attr(type
);
3933 if (policy_attr
== NULL
) {
3934 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3939 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3941 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3943 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3946 ldap_mods_free(mods
, True
);
3948 if (rc
!= LDAP_SUCCESS
) {
3952 if (!cache_account_policy_set(type
, value
)) {
3953 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3954 "update local tdb cache\n"));
3958 return NT_STATUS_OK
;
3961 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3962 enum pdb_policy_type type
,
3965 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3969 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3970 enum pdb_policy_type type
,
3973 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3974 LDAPMessage
*result
= NULL
;
3975 LDAPMessage
*entry
= NULL
;
3980 const char *policy_attr
= NULL
;
3982 struct ldapsam_privates
*ldap_state
=
3983 (struct ldapsam_privates
*)methods
->private_data
;
3985 const char *attrs
[2];
3987 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3989 if (!ldap_state
->domain_dn
) {
3990 return NT_STATUS_INVALID_PARAMETER
;
3993 policy_attr
= get_account_policy_attr(type
);
3995 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3996 "policy index: %d\n", type
));
4000 attrs
[0] = policy_attr
;
4003 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
4004 if (filter
== NULL
) {
4005 return NT_STATUS_NO_MEMORY
;
4007 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
4008 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
4010 TALLOC_FREE(filter
);
4011 if (rc
!= LDAP_SUCCESS
) {
4015 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4020 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4021 if (entry
== NULL
) {
4025 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4030 *value
= (uint32_t)atol(vals
[0]);
4032 ntstatus
= NT_STATUS_OK
;
4036 ldap_value_free(vals
);
4037 ldap_msgfree(result
);
4042 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4044 - if user hasn't decided to use account policies inside LDAP just reuse the
4047 - if there is a valid cache entry, return that
4048 - if there is an LDAP entry, update cache and return
4049 - otherwise set to default, update cache and return
4053 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4054 enum pdb_policy_type type
,
4057 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4059 if (cache_account_policy_get(type
, value
)) {
4060 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4062 return NT_STATUS_OK
;
4065 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4067 if (NT_STATUS_IS_OK(ntstatus
)) {
4071 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4075 /* should we automagically migrate old tdb value here ? */
4076 if (account_policy_get(type
, value
))
4079 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4080 "default\n", type
));
4083 if (!account_policy_get_default(type
, value
)) {
4089 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4090 if (!NT_STATUS_IS_OK(ntstatus
)) {
4096 if (!cache_account_policy_set(type
, *value
)) {
4097 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4098 "tdb as a cache\n"));
4099 return NT_STATUS_UNSUCCESSFUL
;
4102 return NT_STATUS_OK
;
4105 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4106 const struct dom_sid
*domain_sid
,
4110 enum lsa_SidType
*attrs
)
4112 struct ldapsam_privates
*ldap_state
=
4113 (struct ldapsam_privates
*)methods
->private_data
;
4114 LDAPMessage
*msg
= NULL
;
4116 char *allsids
= NULL
;
4117 int i
, rc
, num_mapped
;
4118 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4119 TALLOC_CTX
*mem_ctx
;
4123 mem_ctx
= talloc_new(NULL
);
4124 if (mem_ctx
== NULL
) {
4125 DEBUG(0, ("talloc_new failed\n"));
4129 if (!sid_check_is_builtin(domain_sid
) &&
4130 !sid_check_is_domain(domain_sid
)) {
4131 result
= NT_STATUS_INVALID_PARAMETER
;
4135 if (num_rids
== 0) {
4136 result
= NT_STATUS_NONE_MAPPED
;
4140 for (i
=0; i
<num_rids
; i
++)
4141 attrs
[i
] = SID_NAME_UNKNOWN
;
4143 allsids
= talloc_strdup(mem_ctx
, "");
4144 if (allsids
== NULL
) {
4148 for (i
=0; i
<num_rids
; i
++) {
4150 sid_compose(&sid
, domain_sid
, rids
[i
]);
4151 allsids
= talloc_asprintf_append_buffer(
4152 allsids
, "(sambaSid=%s)",
4153 sid_string_talloc(mem_ctx
, &sid
));
4154 if (allsids
== NULL
) {
4159 /* First look for users */
4163 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4165 filter
= talloc_asprintf(
4166 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4167 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4169 if (filter
== NULL
) {
4173 rc
= smbldap_search(ldap_state
->smbldap_state
,
4174 lp_ldap_user_suffix(),
4175 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4177 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4180 if (rc
!= LDAP_SUCCESS
)
4183 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4186 for (entry
= ldap_first_entry(ld
, msg
);
4188 entry
= ldap_next_entry(ld
, entry
)) {
4193 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4195 DEBUG(2, ("Could not find sid from ldap entry\n"));
4199 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4202 DEBUG(2, ("Could not retrieve uid attribute\n"));
4206 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4207 if (rid
== rids
[rid_index
])
4211 if (rid_index
== num_rids
) {
4212 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4216 attrs
[rid_index
] = SID_NAME_USER
;
4217 names
[rid_index
] = name
;
4221 if (num_mapped
== num_rids
) {
4222 /* No need to look for groups anymore -- we're done */
4223 result
= NT_STATUS_OK
;
4227 /* Same game for groups */
4231 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4232 "sambaGroupType", NULL
};
4234 filter
= talloc_asprintf(
4235 mem_ctx
, "(&(objectClass=%s)(|%s))",
4236 LDAP_OBJ_GROUPMAP
, allsids
);
4237 if (filter
== NULL
) {
4241 rc
= smbldap_search(ldap_state
->smbldap_state
,
4243 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4245 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4248 if (rc
!= LDAP_SUCCESS
)
4251 /* ldap_struct might have changed due to a reconnect */
4253 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4255 /* For consistency checks, we already checked we're only domain or builtin */
4257 is_builtin
= sid_check_is_builtin(domain_sid
);
4259 for (entry
= ldap_first_entry(ld
, msg
);
4261 entry
= ldap_next_entry(ld
, entry
))
4266 enum lsa_SidType type
;
4267 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4269 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4272 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4277 type
= (enum lsa_SidType
)atol(attr
);
4279 /* Consistency checks */
4280 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4281 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4282 (type
!= SID_NAME_DOM_GRP
)))) {
4283 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4286 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4288 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4292 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4295 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4297 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4301 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4306 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4307 if (rid
== rids
[rid_index
])
4311 if (rid_index
== num_rids
) {
4312 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4316 attrs
[rid_index
] = type
;
4317 names
[rid_index
] = attr
;
4321 result
= NT_STATUS_NONE_MAPPED
;
4324 result
= (num_mapped
== num_rids
) ?
4325 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4327 TALLOC_FREE(mem_ctx
);
4331 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4333 char *filter
= NULL
;
4334 char *escaped
= NULL
;
4335 char *result
= NULL
;
4337 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4338 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4342 escaped
= escape_ldap_string(talloc_tos(), username
);
4343 if (escaped
== NULL
) goto done
;
4345 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4349 TALLOC_FREE(escaped
);
4354 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4358 const char **result
;
4360 va_start(ap
, mem_ctx
);
4361 while (va_arg(ap
, const char *) != NULL
)
4365 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4369 va_start(ap
, mem_ctx
);
4370 for (i
=0; i
<num
; i
++) {
4371 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4372 if (result
[i
] == NULL
) {
4373 talloc_free(result
);
4384 struct ldap_search_state
{
4385 struct smbldap_state
*connection
;
4387 uint32_t acct_flags
;
4388 uint16_t group_type
;
4395 void *pagedresults_cookie
;
4397 LDAPMessage
*entries
, *current_entry
;
4398 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4399 TALLOC_CTX
*mem_ctx
,
4400 LDAP
*ld
, LDAPMessage
*entry
,
4401 struct samr_displayentry
*result
);
4404 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4406 struct ldap_search_state
*state
=
4407 (struct ldap_search_state
*)search
->private_data
;
4409 int rc
= LDAP_OPERATIONS_ERROR
;
4411 state
->entries
= NULL
;
4413 if (state
->connection
->paged_results
) {
4414 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4415 state
->scope
, state
->filter
,
4416 state
->attrs
, state
->attrsonly
,
4417 lp_ldap_page_size(), &state
->entries
,
4418 &state
->pagedresults_cookie
);
4421 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4423 if (state
->entries
!= NULL
) {
4424 /* Left over from unsuccessful paged attempt */
4425 ldap_msgfree(state
->entries
);
4426 state
->entries
= NULL
;
4429 rc
= smbldap_search(state
->connection
, state
->base
,
4430 state
->scope
, state
->filter
, state
->attrs
,
4431 state
->attrsonly
, &state
->entries
);
4433 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4436 /* Ok, the server was lying. It told us it could do paged
4437 * searches when it could not. */
4438 state
->connection
->paged_results
= False
;
4441 ld
= state
->connection
->ldap_struct
;
4443 DEBUG(5, ("Don't have an LDAP connection right after a "
4447 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4452 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4454 struct ldap_search_state
*state
=
4455 (struct ldap_search_state
*)search
->private_data
;
4458 if (!state
->connection
->paged_results
) {
4459 /* There is no next page when there are no paged results */
4463 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4464 state
->scope
, state
->filter
, state
->attrs
,
4465 state
->attrsonly
, lp_ldap_page_size(),
4467 &state
->pagedresults_cookie
);
4469 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4472 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4474 if (state
->current_entry
== NULL
) {
4475 ldap_msgfree(state
->entries
);
4476 state
->entries
= NULL
;
4483 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4484 struct samr_displayentry
*entry
)
4486 struct ldap_search_state
*state
=
4487 (struct ldap_search_state
*)search
->private_data
;
4491 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4494 if ((state
->entries
== NULL
) &&
4495 !ldapsam_search_nextpage(search
))
4498 if (state
->current_entry
== NULL
) {
4502 result
= state
->ldap2displayentry(state
, search
,
4503 state
->connection
->ldap_struct
,
4504 state
->current_entry
, entry
);
4508 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4509 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4510 if (dn
!= NULL
) ldap_memfree(dn
);
4513 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4515 if (state
->current_entry
== NULL
) {
4516 ldap_msgfree(state
->entries
);
4517 state
->entries
= NULL
;
4520 if (!result
) goto retry
;
4525 static void ldapsam_search_end(struct pdb_search
*search
)
4527 struct ldap_search_state
*state
=
4528 (struct ldap_search_state
*)search
->private_data
;
4531 if (state
->pagedresults_cookie
== NULL
)
4534 if (state
->entries
!= NULL
)
4535 ldap_msgfree(state
->entries
);
4537 state
->entries
= NULL
;
4538 state
->current_entry
= NULL
;
4540 if (!state
->connection
->paged_results
)
4543 /* Tell the LDAP server we're not interested in the rest anymore. */
4545 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4546 state
->filter
, state
->attrs
,
4547 state
->attrsonly
, 0, &state
->entries
,
4548 &state
->pagedresults_cookie
);
4550 if (rc
!= LDAP_SUCCESS
)
4551 DEBUG(5, ("Could not end search properly\n"));
4556 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4557 TALLOC_CTX
*mem_ctx
,
4558 LDAP
*ld
, LDAPMessage
*entry
,
4559 struct samr_displayentry
*result
)
4562 size_t converted_size
;
4564 uint32_t acct_flags
;
4566 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4567 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4568 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4571 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4572 ldap_value_free(vals
);
4574 if ((state
->acct_flags
!= 0) &&
4575 ((state
->acct_flags
& acct_flags
) == 0))
4578 result
->acct_flags
= acct_flags
;
4579 result
->account_name
= "";
4580 result
->fullname
= "";
4581 result
->description
= "";
4583 vals
= ldap_get_values(ld
, entry
, "uid");
4584 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4585 DEBUG(5, ("\"uid\" not found\n"));
4588 if (!pull_utf8_talloc(mem_ctx
,
4589 CONST_DISCARD(char **, &result
->account_name
),
4590 vals
[0], &converted_size
))
4592 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4596 ldap_value_free(vals
);
4598 vals
= ldap_get_values(ld
, entry
, "displayName");
4599 if ((vals
== NULL
) || (vals
[0] == NULL
))
4600 DEBUG(8, ("\"displayName\" not found\n"));
4601 else if (!pull_utf8_talloc(mem_ctx
,
4602 CONST_DISCARD(char **, &result
->fullname
),
4603 vals
[0], &converted_size
))
4605 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4609 ldap_value_free(vals
);
4611 vals
= ldap_get_values(ld
, entry
, "description");
4612 if ((vals
== NULL
) || (vals
[0] == NULL
))
4613 DEBUG(8, ("\"description\" not found\n"));
4614 else if (!pull_utf8_talloc(mem_ctx
,
4615 CONST_DISCARD(char **, &result
->description
),
4616 vals
[0], &converted_size
))
4618 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4622 ldap_value_free(vals
);
4624 if ((result
->account_name
== NULL
) ||
4625 (result
->fullname
== NULL
) ||
4626 (result
->description
== NULL
)) {
4627 DEBUG(0, ("talloc failed\n"));
4631 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4632 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4633 DEBUG(0, ("\"objectSid\" not found\n"));
4637 if (!string_to_sid(&sid
, vals
[0])) {
4638 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4639 ldap_value_free(vals
);
4642 ldap_value_free(vals
);
4644 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4645 DEBUG(0, ("sid %s does not belong to our domain\n",
4646 sid_string_dbg(&sid
)));
4654 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4655 struct pdb_search
*search
,
4656 uint32_t acct_flags
)
4658 struct ldapsam_privates
*ldap_state
=
4659 (struct ldapsam_privates
*)methods
->private_data
;
4660 struct ldap_search_state
*state
;
4662 state
= talloc(search
, struct ldap_search_state
);
4663 if (state
== NULL
) {
4664 DEBUG(0, ("talloc failed\n"));
4668 state
->connection
= ldap_state
->smbldap_state
;
4670 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4671 state
->base
= lp_ldap_user_suffix();
4672 else if ((acct_flags
!= 0) &&
4673 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4674 state
->base
= lp_ldap_machine_suffix();
4676 state
->base
= lp_ldap_suffix();
4678 state
->acct_flags
= acct_flags
;
4679 state
->base
= talloc_strdup(search
, state
->base
);
4680 state
->scope
= LDAP_SCOPE_SUBTREE
;
4681 state
->filter
= get_ldap_filter(search
, "*");
4682 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4683 "displayName", "description",
4684 "sambaAcctFlags", NULL
);
4685 state
->attrsonly
= 0;
4686 state
->pagedresults_cookie
= NULL
;
4687 state
->entries
= NULL
;
4688 state
->ldap2displayentry
= ldapuser2displayentry
;
4690 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4691 DEBUG(0, ("talloc failed\n"));
4695 search
->private_data
= state
;
4696 search
->next_entry
= ldapsam_search_next_entry
;
4697 search
->search_end
= ldapsam_search_end
;
4699 return ldapsam_search_firstpage(search
);
4702 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4703 TALLOC_CTX
*mem_ctx
,
4704 LDAP
*ld
, LDAPMessage
*entry
,
4705 struct samr_displayentry
*result
)
4708 size_t converted_size
;
4710 uint16_t group_type
;
4712 result
->account_name
= "";
4713 result
->fullname
= "";
4714 result
->description
= "";
4717 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4718 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4719 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4721 ldap_value_free(vals
);
4726 group_type
= atoi(vals
[0]);
4728 if ((state
->group_type
!= 0) &&
4729 ((state
->group_type
!= group_type
))) {
4730 ldap_value_free(vals
);
4734 ldap_value_free(vals
);
4736 /* display name is the NT group name */
4738 vals
= ldap_get_values(ld
, entry
, "displayName");
4739 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4740 DEBUG(8, ("\"displayName\" not found\n"));
4742 /* fallback to the 'cn' attribute */
4743 vals
= ldap_get_values(ld
, entry
, "cn");
4744 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4745 DEBUG(5, ("\"cn\" not found\n"));
4748 if (!pull_utf8_talloc(mem_ctx
,
4749 CONST_DISCARD(char **,
4750 &result
->account_name
),
4751 vals
[0], &converted_size
))
4753 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4754 "failed: %s", strerror(errno
)));
4757 else if (!pull_utf8_talloc(mem_ctx
,
4758 CONST_DISCARD(char **,
4759 &result
->account_name
),
4760 vals
[0], &converted_size
))
4762 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4766 ldap_value_free(vals
);
4768 vals
= ldap_get_values(ld
, entry
, "description");
4769 if ((vals
== NULL
) || (vals
[0] == NULL
))
4770 DEBUG(8, ("\"description\" not found\n"));
4771 else if (!pull_utf8_talloc(mem_ctx
,
4772 CONST_DISCARD(char **, &result
->description
),
4773 vals
[0], &converted_size
))
4775 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4778 ldap_value_free(vals
);
4780 if ((result
->account_name
== NULL
) ||
4781 (result
->fullname
== NULL
) ||
4782 (result
->description
== NULL
)) {
4783 DEBUG(0, ("talloc failed\n"));
4787 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4788 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4789 DEBUG(0, ("\"objectSid\" not found\n"));
4791 ldap_value_free(vals
);
4796 if (!string_to_sid(&sid
, vals
[0])) {
4797 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4801 ldap_value_free(vals
);
4803 switch (group_type
) {
4804 case SID_NAME_DOM_GRP
:
4805 case SID_NAME_ALIAS
:
4807 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4808 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4810 DEBUG(0, ("%s is not in our domain\n",
4811 sid_string_dbg(&sid
)));
4817 DEBUG(0,("unkown group type: %d\n", group_type
));
4821 result
->acct_flags
= 0;
4826 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4827 struct pdb_search
*search
,
4828 const struct dom_sid
*sid
,
4829 enum lsa_SidType type
)
4831 struct ldapsam_privates
*ldap_state
=
4832 (struct ldapsam_privates
*)methods
->private_data
;
4833 struct ldap_search_state
*state
;
4836 state
= talloc(search
, struct ldap_search_state
);
4837 if (state
== NULL
) {
4838 DEBUG(0, ("talloc failed\n"));
4842 state
->connection
= ldap_state
->smbldap_state
;
4844 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4845 state
->connection
= ldap_state
->smbldap_state
;
4846 state
->scope
= LDAP_SCOPE_SUBTREE
;
4847 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4848 "(sambaGroupType=%d)(sambaSID=%s*))",
4850 type
, sid_to_fstring(tmp
, sid
));
4851 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4852 "displayName", "description",
4853 "sambaGroupType", NULL
);
4854 state
->attrsonly
= 0;
4855 state
->pagedresults_cookie
= NULL
;
4856 state
->entries
= NULL
;
4857 state
->group_type
= type
;
4858 state
->ldap2displayentry
= ldapgroup2displayentry
;
4860 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4861 DEBUG(0, ("talloc failed\n"));
4865 search
->private_data
= state
;
4866 search
->next_entry
= ldapsam_search_next_entry
;
4867 search
->search_end
= ldapsam_search_end
;
4869 return ldapsam_search_firstpage(search
);
4872 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4873 struct pdb_search
*search
)
4875 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4878 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4879 struct pdb_search
*search
,
4880 const struct dom_sid
*sid
)
4882 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4885 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4887 return PDB_CAP_STORE_RIDS
;
4890 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4893 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4895 LDAPMessage
*result
= NULL
;
4896 LDAPMessage
*entry
= NULL
;
4897 LDAPMod
**mods
= NULL
;
4901 uint32_t nextRid
= 0;
4904 TALLOC_CTX
*mem_ctx
;
4906 mem_ctx
= talloc_new(NULL
);
4907 if (mem_ctx
== NULL
) {
4908 DEBUG(0, ("talloc_new failed\n"));
4909 return NT_STATUS_NO_MEMORY
;
4912 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4913 get_global_sam_name(), False
);
4914 if (!NT_STATUS_IS_OK(status
)) {
4915 DEBUG(3, ("Could not get domain info: %s\n",
4916 nt_errstr(status
)));
4920 talloc_autofree_ldapmsg(mem_ctx
, result
);
4922 entry
= ldap_first_entry(priv2ld(priv
), result
);
4923 if (entry
== NULL
) {
4924 DEBUG(0, ("Could not get domain info entry\n"));
4925 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4929 /* Find the largest of the three attributes "sambaNextRid",
4930 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4931 concept of differentiating between user and group rids, and will
4932 use only "sambaNextRid" in the future. But for compatibility
4933 reasons I look if others have chosen different strategies -- VL */
4935 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4936 "sambaNextRid", mem_ctx
);
4937 if (value
!= NULL
) {
4938 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4939 nextRid
= MAX(nextRid
, tmp
);
4942 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4943 "sambaNextUserRid", mem_ctx
);
4944 if (value
!= NULL
) {
4945 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4946 nextRid
= MAX(nextRid
, tmp
);
4949 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4950 "sambaNextGroupRid", mem_ctx
);
4951 if (value
!= NULL
) {
4952 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4953 nextRid
= MAX(nextRid
, tmp
);
4957 nextRid
= BASE_RID
-1;
4962 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4963 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4964 talloc_autofree_ldapmod(mem_ctx
, mods
);
4966 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4967 status
= NT_STATUS_NO_MEMORY
;
4971 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4973 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4976 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4979 if (NT_STATUS_IS_OK(status
)) {
4983 TALLOC_FREE(mem_ctx
);
4987 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4991 for (i
=0; i
<10; i
++) {
4992 NTSTATUS result
= ldapsam_get_new_rid(
4993 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4994 if (NT_STATUS_IS_OK(result
)) {
4998 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
5002 /* The ldap update failed (maybe a race condition), retry */
5005 /* Tried 10 times, fail. */
5006 return NT_STATUS_ACCESS_DENIED
;
5009 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
5011 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
5012 return NT_STATUS_IS_OK(result
) ? True
: False
;
5015 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
5016 const struct dom_sid
*sid
,
5017 union unid_t
*id
, enum lsa_SidType
*type
)
5019 struct ldapsam_privates
*priv
=
5020 (struct ldapsam_privates
*)methods
->private_data
;
5022 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5024 LDAPMessage
*result
= NULL
;
5025 LDAPMessage
*entry
= NULL
;
5030 TALLOC_CTX
*mem_ctx
;
5032 mem_ctx
= talloc_new(NULL
);
5033 if (mem_ctx
== NULL
) {
5034 DEBUG(0, ("talloc_new failed\n"));
5038 filter
= talloc_asprintf(mem_ctx
,
5040 "(|(objectClass=%s)(objectClass=%s)))",
5041 sid_string_talloc(mem_ctx
, sid
),
5042 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5043 if (filter
== NULL
) {
5044 DEBUG(5, ("talloc_asprintf failed\n"));
5048 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5050 if (rc
!= LDAP_SUCCESS
) {
5053 talloc_autofree_ldapmsg(mem_ctx
, result
);
5055 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5056 DEBUG(10, ("Got %d entries, expected one\n",
5057 ldap_count_entries(priv2ld(priv
), result
)));
5061 entry
= ldap_first_entry(priv2ld(priv
), result
);
5063 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5064 "sambaGroupType", mem_ctx
);
5066 if (value
!= NULL
) {
5067 const char *gid_str
;
5070 gid_str
= smbldap_talloc_single_attribute(
5071 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5072 if (gid_str
== NULL
) {
5073 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5074 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5079 id
->gid
= strtoul(gid_str
, NULL
, 10);
5080 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
5081 store_gid_sid_cache(sid
, id
->gid
);
5082 idmap_cache_set_sid2gid(sid
, id
->gid
);
5087 /* It must be a user */
5089 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5090 "uidNumber", mem_ctx
);
5091 if (value
== NULL
) {
5092 DEBUG(1, ("Could not find uidNumber in %s\n",
5093 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5097 id
->uid
= strtoul(value
, NULL
, 10);
5098 *type
= SID_NAME_USER
;
5099 store_uid_sid_cache(sid
, id
->uid
);
5100 idmap_cache_set_sid2uid(sid
, id
->uid
);
5104 TALLOC_FREE(mem_ctx
);
5109 * Find the SID for a uid.
5110 * This is shortcut is only used if ldapsam:trusted is set to true.
5112 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5113 struct dom_sid
*sid
)
5115 struct ldapsam_privates
*priv
=
5116 (struct ldapsam_privates
*)methods
->private_data
;
5118 const char *attrs
[] = { "sambaSID", NULL
};
5119 LDAPMessage
*result
= NULL
;
5120 LDAPMessage
*entry
= NULL
;
5122 char *user_sid_string
;
5123 struct dom_sid user_sid
;
5125 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5127 filter
= talloc_asprintf(tmp_ctx
,
5130 "(objectClass=%s))",
5132 LDAP_OBJ_POSIXACCOUNT
,
5133 LDAP_OBJ_SAMBASAMACCOUNT
);
5134 if (filter
== NULL
) {
5135 DEBUG(3, ("talloc_asprintf failed\n"));
5139 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5140 if (rc
!= LDAP_SUCCESS
) {
5143 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5145 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5146 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5147 ldap_count_entries(priv2ld(priv
), result
),
5148 (unsigned int)uid
));
5152 entry
= ldap_first_entry(priv2ld(priv
), result
);
5154 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5155 "sambaSID", tmp_ctx
);
5156 if (user_sid_string
== NULL
) {
5157 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5158 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5162 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5163 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5168 sid_copy(sid
, &user_sid
);
5170 store_uid_sid_cache(sid
, uid
);
5171 idmap_cache_set_sid2uid(sid
, uid
);
5176 TALLOC_FREE(tmp_ctx
);
5181 * Find the SID for a gid.
5182 * This is shortcut is only used if ldapsam:trusted is set to true.
5184 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5185 struct dom_sid
*sid
)
5187 struct ldapsam_privates
*priv
=
5188 (struct ldapsam_privates
*)methods
->private_data
;
5190 const char *attrs
[] = { "sambaSID", NULL
};
5191 LDAPMessage
*result
= NULL
;
5192 LDAPMessage
*entry
= NULL
;
5194 char *group_sid_string
;
5195 struct dom_sid group_sid
;
5197 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5199 filter
= talloc_asprintf(tmp_ctx
,
5201 "(objectClass=%s))",
5204 if (filter
== NULL
) {
5205 DEBUG(3, ("talloc_asprintf failed\n"));
5209 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5210 if (rc
!= LDAP_SUCCESS
) {
5213 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5215 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5216 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5217 ldap_count_entries(priv2ld(priv
), result
),
5218 (unsigned int)gid
));
5222 entry
= ldap_first_entry(priv2ld(priv
), result
);
5224 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5225 "sambaSID", tmp_ctx
);
5226 if (group_sid_string
== NULL
) {
5227 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5228 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5232 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5233 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5238 sid_copy(sid
, &group_sid
);
5240 store_gid_sid_cache(sid
, gid
);
5241 idmap_cache_set_sid2gid(sid
, gid
);
5246 TALLOC_FREE(tmp_ctx
);
5252 * The following functions are called only if
5253 * ldapsam:trusted and ldapsam:editposix are
5258 * ldapsam_create_user creates a new
5259 * posixAccount and sambaSamAccount object
5260 * in the ldap users subtree
5262 * The uid is allocated by winbindd.
5265 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5266 TALLOC_CTX
*tmp_ctx
, const char *name
,
5267 uint32_t acb_info
, uint32_t *rid
)
5269 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5270 LDAPMessage
*entry
= NULL
;
5271 LDAPMessage
*result
= NULL
;
5272 uint32_t num_result
;
5273 bool is_machine
= False
;
5274 bool add_posix
= False
;
5275 LDAPMod
**mods
= NULL
;
5283 const char *dn
= NULL
;
5284 struct dom_sid group_sid
;
5285 struct dom_sid user_sid
;
5291 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5292 acb_info
& ACB_WSTRUST
||
5293 acb_info
& ACB_SVRTRUST
||
5294 acb_info
& ACB_DOMTRUST
) {
5298 username
= escape_ldap_string(talloc_tos(), name
);
5299 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5300 username
, LDAP_OBJ_POSIXACCOUNT
);
5301 TALLOC_FREE(username
);
5303 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5304 if (rc
!= LDAP_SUCCESS
) {
5305 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5306 return NT_STATUS_ACCESS_DENIED
;
5308 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5310 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5312 if (num_result
> 1) {
5313 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5314 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5317 if (num_result
== 1) {
5319 /* check if it is just a posix account.
5320 * or if there is a sid attached to this entry
5323 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5325 return NT_STATUS_UNSUCCESSFUL
;
5328 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5330 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5331 return NT_STATUS_USER_EXISTS
;
5334 /* it is just a posix account, retrieve the dn for later use */
5335 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5337 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5338 return NT_STATUS_NO_MEMORY
;
5342 if (num_result
== 0) {
5346 /* Create the basic samu structure and generate the mods for the ldap commit */
5347 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5348 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5352 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5354 user
= samu_new(tmp_ctx
);
5356 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5357 return NT_STATUS_NO_MEMORY
;
5360 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5361 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5362 return NT_STATUS_UNSUCCESSFUL
;
5364 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5365 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5366 return NT_STATUS_UNSUCCESSFUL
;
5369 if (acb_info
& ACB_NORMAL
) {
5370 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5371 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5372 return NT_STATUS_UNSUCCESSFUL
;
5375 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5376 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5377 return NT_STATUS_UNSUCCESSFUL
;
5381 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5382 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5383 return NT_STATUS_UNSUCCESSFUL
;
5387 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5388 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5389 return NT_STATUS_UNSUCCESSFUL
;
5392 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5393 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5394 return NT_STATUS_UNSUCCESSFUL
;
5397 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5398 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5400 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5405 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5407 /* retrieve the Domain Users group gid */
5408 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5409 !sid_to_gid(&group_sid
, &gid
)) {
5410 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5411 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5414 /* lets allocate a new userid for this user */
5415 if (!winbind_allocate_uid(&uid
)) {
5416 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5417 return NT_STATUS_UNSUCCESSFUL
;
5422 /* TODO: choose a more appropriate default for machines */
5423 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5424 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5426 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5427 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5429 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5430 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5432 escape_name
= escape_rdn_val_string_alloc(name
);
5434 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5435 return NT_STATUS_NO_MEMORY
;
5439 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5441 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5444 SAFE_FREE(escape_name
);
5446 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5447 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5448 return NT_STATUS_NO_MEMORY
;
5451 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5452 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5453 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5454 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5455 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5456 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5457 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5460 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5463 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5465 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5468 if (rc
!= LDAP_SUCCESS
) {
5469 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5470 return NT_STATUS_UNSUCCESSFUL
;
5473 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5475 flush_pwnam_cache();
5477 return NT_STATUS_OK
;
5480 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5482 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5483 LDAPMessage
*result
= NULL
;
5484 LDAPMessage
*entry
= NULL
;
5490 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5492 filter
= talloc_asprintf(tmp_ctx
,
5495 "(objectClass=%s))",
5496 pdb_get_username(sam_acct
),
5497 LDAP_OBJ_POSIXACCOUNT
,
5498 LDAP_OBJ_SAMBASAMACCOUNT
);
5499 if (filter
== NULL
) {
5500 return NT_STATUS_NO_MEMORY
;
5503 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5504 if (rc
!= LDAP_SUCCESS
) {
5505 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5506 return NT_STATUS_UNSUCCESSFUL
;
5508 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5510 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5512 if (num_result
== 0) {
5513 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5514 return NT_STATUS_NO_SUCH_USER
;
5517 if (num_result
> 1) {
5518 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5519 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5522 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5524 return NT_STATUS_UNSUCCESSFUL
;
5527 /* it is just a posix account, retrieve the dn for later use */
5528 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5530 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5531 return NT_STATUS_NO_MEMORY
;
5534 /* try to remove memberships first */
5537 struct dom_sid
*sids
= NULL
;
5539 size_t num_groups
= 0;
5541 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5543 status
= ldapsam_enum_group_memberships(my_methods
,
5549 if (!NT_STATUS_IS_OK(status
)) {
5553 for (i
=0; i
< num_groups
; i
++) {
5557 sid_peek_rid(&sids
[i
], &group_rid
);
5559 ldapsam_del_groupmem(my_methods
,
5568 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5569 if (rc
!= LDAP_SUCCESS
) {
5570 return NT_STATUS_UNSUCCESSFUL
;
5573 flush_pwnam_cache();
5575 return NT_STATUS_OK
;
5579 * ldapsam_create_group creates a new
5580 * posixGroup and sambaGroupMapping object
5581 * in the ldap groups subtree
5583 * The gid is allocated by winbindd.
5586 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5587 TALLOC_CTX
*tmp_ctx
,
5591 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5593 LDAPMessage
*entry
= NULL
;
5594 LDAPMessage
*result
= NULL
;
5595 uint32_t num_result
;
5596 bool is_new_entry
= False
;
5597 LDAPMod
**mods
= NULL
;
5603 const char *dn
= NULL
;
5604 struct dom_sid group_sid
;
5608 groupname
= escape_ldap_string(talloc_tos(), name
);
5609 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5610 groupname
, LDAP_OBJ_POSIXGROUP
);
5611 TALLOC_FREE(groupname
);
5613 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5614 if (rc
!= LDAP_SUCCESS
) {
5615 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5616 return NT_STATUS_UNSUCCESSFUL
;
5618 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5620 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5622 if (num_result
> 1) {
5623 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5624 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5627 if (num_result
== 1) {
5629 /* check if it is just a posix group.
5630 * or if there is a sid attached to this entry
5633 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5635 return NT_STATUS_UNSUCCESSFUL
;
5638 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5640 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5641 return NT_STATUS_GROUP_EXISTS
;
5644 /* it is just a posix group, retrieve the gid and the dn for later use */
5645 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5647 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5648 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5651 gid
= strtoul(tmp
, NULL
, 10);
5653 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5655 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5656 return NT_STATUS_NO_MEMORY
;
5660 if (num_result
== 0) {
5661 is_new_entry
= true;
5664 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5665 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5669 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5671 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5673 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5675 if (!groupsidstr
|| !grouptype
) {
5676 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5677 return NT_STATUS_NO_MEMORY
;
5680 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5681 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5682 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5683 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5688 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5690 /* lets allocate a new groupid for this group */
5691 if (!winbind_allocate_gid(&gid
)) {
5692 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5693 return NT_STATUS_UNSUCCESSFUL
;
5696 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5698 escape_name
= escape_rdn_val_string_alloc(name
);
5700 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5701 return NT_STATUS_NO_MEMORY
;
5704 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5706 SAFE_FREE(escape_name
);
5708 if (!gidstr
|| !dn
) {
5709 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5710 return NT_STATUS_NO_MEMORY
;
5713 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5714 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5715 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5718 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5721 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5723 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5724 /* This call may fail with rfc2307bis schema */
5725 /* Retry adding a structural class */
5726 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5727 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5731 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5734 if (rc
!= LDAP_SUCCESS
) {
5735 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5736 return NT_STATUS_UNSUCCESSFUL
;
5739 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5741 return NT_STATUS_OK
;
5744 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5746 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5747 LDAPMessage
*result
= NULL
;
5748 LDAPMessage
*entry
= NULL
;
5753 struct dom_sid group_sid
;
5756 /* get the group sid */
5757 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5759 filter
= talloc_asprintf(tmp_ctx
,
5762 "(objectClass=%s))",
5763 sid_string_talloc(tmp_ctx
, &group_sid
),
5764 LDAP_OBJ_POSIXGROUP
,
5766 if (filter
== NULL
) {
5767 return NT_STATUS_NO_MEMORY
;
5770 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5771 if (rc
!= LDAP_SUCCESS
) {
5772 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5773 return NT_STATUS_UNSUCCESSFUL
;
5775 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5777 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5779 if (num_result
== 0) {
5780 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5781 return NT_STATUS_NO_SUCH_GROUP
;
5784 if (num_result
> 1) {
5785 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5786 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5789 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5791 return NT_STATUS_UNSUCCESSFUL
;
5794 /* here it is, retrieve the dn for later use */
5795 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5797 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5798 return NT_STATUS_NO_MEMORY
;
5801 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5803 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5804 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5807 /* check no user have this group marked as primary group */
5808 filter
= talloc_asprintf(tmp_ctx
,
5811 "(objectClass=%s))",
5813 LDAP_OBJ_POSIXACCOUNT
,
5814 LDAP_OBJ_SAMBASAMACCOUNT
);
5816 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5817 if (rc
!= LDAP_SUCCESS
) {
5818 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5819 return NT_STATUS_UNSUCCESSFUL
;
5821 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5823 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5825 if (num_result
!= 0) {
5826 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5827 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5830 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5831 if (rc
!= LDAP_SUCCESS
) {
5832 return NT_STATUS_UNSUCCESSFUL
;
5835 return NT_STATUS_OK
;
5838 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5839 TALLOC_CTX
*tmp_ctx
,
5841 uint32_t member_rid
,
5844 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5845 LDAPMessage
*entry
= NULL
;
5846 LDAPMessage
*result
= NULL
;
5847 uint32_t num_result
;
5848 LDAPMod
**mods
= NULL
;
5851 const char *dn
= NULL
;
5852 struct dom_sid group_sid
;
5853 struct dom_sid member_sid
;
5858 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5860 case LDAP_MOD_DELETE
:
5861 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5864 return NT_STATUS_UNSUCCESSFUL
;
5867 /* get member sid */
5868 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5870 /* get the group sid */
5871 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5873 filter
= talloc_asprintf(tmp_ctx
,
5876 "(objectClass=%s))",
5877 sid_string_talloc(tmp_ctx
, &member_sid
),
5878 LDAP_OBJ_POSIXACCOUNT
,
5879 LDAP_OBJ_SAMBASAMACCOUNT
);
5880 if (filter
== NULL
) {
5881 return NT_STATUS_NO_MEMORY
;
5884 /* get the member uid */
5885 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5886 if (rc
!= LDAP_SUCCESS
) {
5887 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5888 return NT_STATUS_UNSUCCESSFUL
;
5890 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5892 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5894 if (num_result
== 0) {
5895 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5896 return NT_STATUS_NO_SUCH_MEMBER
;
5899 if (num_result
> 1) {
5900 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5901 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5904 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5906 return NT_STATUS_UNSUCCESSFUL
;
5909 if (modop
== LDAP_MOD_DELETE
) {
5910 /* check if we are trying to remove the member from his primary group */
5912 gid_t user_gid
, group_gid
;
5914 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5916 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5917 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5920 user_gid
= strtoul(gidstr
, NULL
, 10);
5922 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5923 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5924 return NT_STATUS_UNSUCCESSFUL
;
5927 if (user_gid
== group_gid
) {
5928 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5929 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5933 /* here it is, retrieve the uid for later use */
5934 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5936 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5937 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5940 filter
= talloc_asprintf(tmp_ctx
,
5943 "(objectClass=%s))",
5944 sid_string_talloc(tmp_ctx
, &group_sid
),
5945 LDAP_OBJ_POSIXGROUP
,
5949 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5950 if (rc
!= LDAP_SUCCESS
) {
5951 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5952 return NT_STATUS_UNSUCCESSFUL
;
5954 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5956 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5958 if (num_result
== 0) {
5959 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5960 return NT_STATUS_NO_SUCH_GROUP
;
5963 if (num_result
> 1) {
5964 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5965 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5968 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5970 return NT_STATUS_UNSUCCESSFUL
;
5973 /* here it is, retrieve the dn for later use */
5974 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5976 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5977 return NT_STATUS_NO_MEMORY
;
5980 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5982 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5984 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5985 if (rc
!= LDAP_SUCCESS
) {
5986 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5987 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5988 return NT_STATUS_MEMBER_IN_GROUP
;
5990 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5991 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5992 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5994 return NT_STATUS_UNSUCCESSFUL
;
5997 return NT_STATUS_OK
;
6000 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
6001 TALLOC_CTX
*tmp_ctx
,
6003 uint32_t member_rid
)
6005 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
6007 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
6008 TALLOC_CTX
*tmp_ctx
,
6010 uint32_t member_rid
)
6012 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
6015 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
6016 TALLOC_CTX
*mem_ctx
,
6017 struct samu
*sampass
)
6019 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6020 LDAPMessage
*entry
= NULL
;
6021 LDAPMessage
*result
= NULL
;
6022 uint32_t num_result
;
6023 LDAPMod
**mods
= NULL
;
6025 char *escape_username
;
6027 const char *dn
= NULL
;
6031 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6033 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6034 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6035 return NT_STATUS_UNSUCCESSFUL
;
6037 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6039 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6040 return NT_STATUS_NO_MEMORY
;
6043 escape_username
= escape_ldap_string(talloc_tos(),
6044 pdb_get_username(sampass
));
6045 if (escape_username
== NULL
) {
6046 return NT_STATUS_NO_MEMORY
;
6049 filter
= talloc_asprintf(mem_ctx
,
6052 "(objectClass=%s))",
6054 LDAP_OBJ_POSIXACCOUNT
,
6055 LDAP_OBJ_SAMBASAMACCOUNT
);
6057 TALLOC_FREE(escape_username
);
6059 if (filter
== NULL
) {
6060 return NT_STATUS_NO_MEMORY
;
6063 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6064 if (rc
!= LDAP_SUCCESS
) {
6065 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6066 return NT_STATUS_UNSUCCESSFUL
;
6068 talloc_autofree_ldapmsg(mem_ctx
, result
);
6070 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6072 if (num_result
== 0) {
6073 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6074 return NT_STATUS_NO_SUCH_USER
;
6077 if (num_result
> 1) {
6078 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6079 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6082 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6084 return NT_STATUS_UNSUCCESSFUL
;
6087 /* retrieve the dn for later use */
6088 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6090 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6091 return NT_STATUS_NO_MEMORY
;
6094 /* remove the old one, and add the new one, this way we do not risk races */
6095 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6098 return NT_STATUS_OK
;
6101 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6103 if (rc
!= LDAP_SUCCESS
) {
6104 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6105 pdb_get_username(sampass
), gidstr
));
6106 return NT_STATUS_UNSUCCESSFUL
;
6109 flush_pwnam_cache();
6111 return NT_STATUS_OK
;
6115 /**********************************************************************
6116 trusted domains functions
6117 *********************************************************************/
6119 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6122 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6123 ldap_state
->domain_dn
);
6126 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6127 TALLOC_CTX
*mem_ctx
,
6128 const char *domain
, LDAPMessage
**entry
)
6132 int scope
= LDAP_SCOPE_SUBTREE
;
6133 const char **attrs
= NULL
; /* NULL: get all attrs */
6134 int attrsonly
= 0; /* 0: return values too */
6135 LDAPMessage
*result
= NULL
;
6137 uint32_t num_result
;
6139 filter
= talloc_asprintf(talloc_tos(),
6140 "(&(objectClass=%s)(sambaDomainName=%s))",
6141 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6143 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6144 if (trusted_dn
== NULL
) {
6147 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6148 filter
, attrs
, attrsonly
, &result
);
6150 if (result
!= NULL
) {
6151 talloc_autofree_ldapmsg(mem_ctx
, result
);
6154 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6159 if (rc
!= LDAP_SUCCESS
) {
6163 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6165 if (num_result
> 1) {
6166 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6167 "%s object for domain '%s'?!\n",
6168 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6172 if (num_result
== 0) {
6173 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6174 "%s object for domain %s.\n",
6175 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6178 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6184 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6187 struct dom_sid
*sid
,
6188 time_t *pass_last_set_time
)
6190 struct ldapsam_privates
*ldap_state
=
6191 (struct ldapsam_privates
*)methods
->private_data
;
6192 LDAPMessage
*entry
= NULL
;
6194 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6196 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6205 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6206 entry
, "sambaClearTextPassword", talloc_tos());
6207 if (pwd_str
== NULL
) {
6210 /* trusteddom_pw routines do not use talloc yet... */
6211 *pwd
= SMB_STRDUP(pwd_str
);
6217 /* last change time */
6218 if (pass_last_set_time
!= NULL
) {
6220 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6221 entry
, "sambaPwdLastSet", talloc_tos());
6222 if (time_str
== NULL
) {
6225 *pass_last_set_time
= (time_t)atol(time_str
);
6231 struct dom_sid dom_sid
;
6232 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6235 if (sid_str
== NULL
) {
6238 if (!string_to_sid(&dom_sid
, sid_str
)) {
6241 sid_copy(sid
, &dom_sid
);
6247 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6250 const struct dom_sid
*sid
)
6252 struct ldapsam_privates
*ldap_state
=
6253 (struct ldapsam_privates
*)methods
->private_data
;
6254 LDAPMessage
*entry
= NULL
;
6255 LDAPMod
**mods
= NULL
;
6256 char *prev_pwd
= NULL
;
6257 char *trusted_dn
= NULL
;
6260 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6263 * get the current entry (if there is one) in order to put the
6264 * current password into the previous password attribute
6266 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6271 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6272 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6273 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6275 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6276 sid_string_tos(sid
));
6277 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6278 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6279 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6280 "sambaClearTextPassword", pwd
);
6282 if (entry
!= NULL
) {
6283 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6284 entry
, "sambaClearTextPassword", talloc_tos());
6285 if (prev_pwd
!= NULL
) {
6286 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6287 "sambaPreviousClearTextPassword",
6292 talloc_autofree_ldapmod(talloc_tos(), mods
);
6294 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6295 if (trusted_dn
== NULL
) {
6298 if (entry
== NULL
) {
6299 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6301 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6304 if (rc
!= LDAP_SUCCESS
) {
6305 DEBUG(1, ("error writing trusted domain password!\n"));
6312 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6316 struct ldapsam_privates
*ldap_state
=
6317 (struct ldapsam_privates
*)methods
->private_data
;
6318 LDAPMessage
*entry
= NULL
;
6319 const char *trusted_dn
;
6321 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6325 if (entry
== NULL
) {
6326 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6331 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6333 if (trusted_dn
== NULL
) {
6334 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6338 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6339 if (rc
!= LDAP_SUCCESS
) {
6346 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6347 TALLOC_CTX
*mem_ctx
,
6348 uint32_t *num_domains
,
6349 struct trustdom_info
***domains
)
6352 struct ldapsam_privates
*ldap_state
=
6353 (struct ldapsam_privates
*)methods
->private_data
;
6355 int scope
= LDAP_SCOPE_SUBTREE
;
6356 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6357 int attrsonly
= 0; /* 0: return values too */
6358 LDAPMessage
*result
= NULL
;
6359 LDAPMessage
*entry
= NULL
;
6361 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6362 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6364 rc
= smbldap_search(ldap_state
->smbldap_state
,
6365 ldap_state
->domain_dn
,
6372 if (result
!= NULL
) {
6373 talloc_autofree_ldapmsg(mem_ctx
, result
);
6376 if (rc
!= LDAP_SUCCESS
) {
6377 return NT_STATUS_UNSUCCESSFUL
;
6381 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6382 DEBUG(1, ("talloc failed\n"));
6383 return NT_STATUS_NO_MEMORY
;
6386 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6388 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6390 char *dom_name
, *dom_sid_str
;
6391 struct trustdom_info
*dom_info
;
6393 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6394 if (dom_info
== NULL
) {
6395 DEBUG(1, ("talloc failed\n"));
6396 return NT_STATUS_NO_MEMORY
;
6399 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6403 if (dom_name
== NULL
) {
6404 DEBUG(1, ("talloc failed\n"));
6405 return NT_STATUS_NO_MEMORY
;
6407 dom_info
->name
= dom_name
;
6409 dom_sid_str
= smbldap_talloc_single_attribute(
6410 priv2ld(ldap_state
), entry
, "sambaSID",
6412 if (dom_sid_str
== NULL
) {
6413 DEBUG(1, ("talloc failed\n"));
6414 return NT_STATUS_NO_MEMORY
;
6416 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6417 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6419 return NT_STATUS_UNSUCCESSFUL
;
6422 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6423 domains
, num_domains
);
6425 if (*domains
== NULL
) {
6426 DEBUG(1, ("talloc failed\n"));
6427 return NT_STATUS_NO_MEMORY
;
6431 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6432 return NT_STATUS_OK
;
6436 /**********************************************************************
6438 *********************************************************************/
6440 static void free_private_data(void **vp
)
6442 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6444 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6446 if ((*ldap_state
)->result
!= NULL
) {
6447 ldap_msgfree((*ldap_state
)->result
);
6448 (*ldap_state
)->result
= NULL
;
6450 if ((*ldap_state
)->domain_dn
!= NULL
) {
6451 SAFE_FREE((*ldap_state
)->domain_dn
);
6456 /* No need to free any further, as it is talloc()ed */
6459 /*********************************************************************
6460 Intitalise the parts of the pdb_methods structure that are common to
6462 *********************************************************************/
6464 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6467 struct ldapsam_privates
*ldap_state
;
6469 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6473 (*pdb_method
)->name
= "ldapsam";
6475 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6476 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6477 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6478 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6479 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6480 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6482 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6483 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6484 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6485 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6486 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6487 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6488 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6490 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6491 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6493 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6495 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6496 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6498 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6499 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6500 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6501 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6503 /* TODO: Setup private data and free */
6505 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6506 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6507 return NT_STATUS_NO_MEMORY
;
6510 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6511 location
, &ldap_state
->smbldap_state
);
6513 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6517 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6518 return NT_STATUS_NO_MEMORY
;
6521 (*pdb_method
)->private_data
= ldap_state
;
6523 (*pdb_method
)->free_private_data
= free_private_data
;
6525 return NT_STATUS_OK
;
6528 /**********************************************************************
6529 Initialise the 'compat' mode for pdb_ldap
6530 *********************************************************************/
6532 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6535 struct ldapsam_privates
*ldap_state
;
6536 char *uri
= talloc_strdup( NULL
, location
);
6538 trim_char( uri
, '\"', '\"' );
6539 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6543 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6547 (*pdb_method
)->name
= "ldapsam_compat";
6549 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6550 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6552 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6554 return NT_STATUS_OK
;
6557 /**********************************************************************
6558 Initialise the normal mode for pdb_ldap
6559 *********************************************************************/
6561 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6564 struct ldapsam_privates
*ldap_state
= NULL
;
6565 uint32_t alg_rid_base
;
6566 char *alg_rid_base_string
= NULL
;
6567 LDAPMessage
*result
= NULL
;
6568 LDAPMessage
*entry
= NULL
;
6569 struct dom_sid ldap_domain_sid
;
6570 struct dom_sid secrets_domain_sid
;
6571 char *domain_sid_string
= NULL
;
6573 char *uri
= talloc_strdup( NULL
, location
);
6575 trim_char( uri
, '\"', '\"' );
6576 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6580 if (!NT_STATUS_IS_OK(nt_status
)) {
6584 (*pdb_method
)->name
= "ldapsam";
6586 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6587 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6588 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6589 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6590 (*pdb_method
)->search_users
= ldapsam_search_users
;
6591 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6592 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6594 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6595 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6596 (*pdb_method
)->enum_group_memberships
=
6597 ldapsam_enum_group_memberships
;
6598 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6599 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6600 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6601 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6603 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6604 (*pdb_method
)->create_user
= ldapsam_create_user
;
6605 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6606 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6607 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6608 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6609 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6610 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6614 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6615 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6617 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6619 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6621 ldap_state
->domain_name
, True
);
6623 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6624 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6625 "info, nor add one to the domain\n"));
6626 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6627 "will be unable to allocate new users/groups, "
6628 "and will risk BDCs having inconsistant SIDs\n"));
6629 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6630 return NT_STATUS_OK
;
6633 /* Given that the above might fail, everything below this must be
6636 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6639 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6641 ldap_msgfree(result
);
6642 return NT_STATUS_UNSUCCESSFUL
;
6645 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6647 ldap_msgfree(result
);
6648 return NT_STATUS_UNSUCCESSFUL
;
6651 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6654 domain_sid_string
= smbldap_talloc_single_attribute(
6655 ldap_state
->smbldap_state
->ldap_struct
,
6657 get_userattr_key2string(ldap_state
->schema_ver
,
6658 LDAP_ATTR_USER_SID
),
6661 if (domain_sid_string
) {
6663 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6664 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6665 "read as a valid SID\n", domain_sid_string
));
6666 ldap_msgfree(result
);
6667 TALLOC_FREE(domain_sid_string
);
6668 return NT_STATUS_INVALID_PARAMETER
;
6670 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6671 &secrets_domain_sid
);
6672 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6673 &ldap_domain_sid
)) {
6674 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6675 "%s based on pdb_ldap results %s -> %s\n",
6676 ldap_state
->domain_name
,
6677 sid_string_dbg(&secrets_domain_sid
),
6678 sid_string_dbg(&ldap_domain_sid
)));
6680 /* reset secrets.tdb sid */
6681 secrets_store_domain_sid(ldap_state
->domain_name
,
6683 DEBUG(1, ("New global sam SID: %s\n",
6684 sid_string_dbg(get_global_sam_sid())));
6686 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6687 TALLOC_FREE(domain_sid_string
);
6690 alg_rid_base_string
= smbldap_talloc_single_attribute(
6691 ldap_state
->smbldap_state
->ldap_struct
,
6693 get_attr_key2string( dominfo_attr_list
,
6694 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6696 if (alg_rid_base_string
) {
6697 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6698 if (alg_rid_base
!= algorithmic_rid_base()) {
6699 DEBUG(0, ("The value of 'algorithmic RID base' has "
6700 "changed since the LDAP\n"
6701 "database was initialised. Aborting. \n"));
6702 ldap_msgfree(result
);
6703 TALLOC_FREE(alg_rid_base_string
);
6704 return NT_STATUS_UNSUCCESSFUL
;
6706 TALLOC_FREE(alg_rid_base_string
);
6708 ldap_msgfree(result
);
6710 return NT_STATUS_OK
;
6713 NTSTATUS
pdb_ldap_init(void)
6716 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6719 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6722 /* Let pdb_nds register backends */
6725 return NT_STATUS_OK
;