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"
50 #define DBGC_CLASS DBGC_PASSDB
56 * Work around versions of the LDAP client libs that don't have the OIDs
57 * defined, or have them defined under the old name.
58 * This functionality is really a factor of the server, not the client
62 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
63 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
64 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
65 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
68 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
69 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
70 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
71 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
74 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
75 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
76 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
77 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
83 /**********************************************************************
84 Simple helper function to make stuff better readable
85 **********************************************************************/
87 static LDAP
*priv2ld(struct ldapsam_privates
*priv
)
89 return priv
->smbldap_state
->ldap_struct
;
92 /**********************************************************************
93 Get the attribute name given a user schame version.
94 **********************************************************************/
96 static const char* get_userattr_key2string( int schema_ver
, int key
)
98 switch ( schema_ver
) {
99 case SCHEMAVER_SAMBAACCOUNT
:
100 return get_attr_key2string( attrib_map_v22
, key
);
102 case SCHEMAVER_SAMBASAMACCOUNT
:
103 return get_attr_key2string( attrib_map_v30
, key
);
106 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
112 /**********************************************************************
113 Return the list of attribute names given a user schema version.
114 **********************************************************************/
116 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
118 switch ( schema_ver
) {
119 case SCHEMAVER_SAMBAACCOUNT
:
120 return get_attr_list( mem_ctx
, attrib_map_v22
);
122 case SCHEMAVER_SAMBASAMACCOUNT
:
123 return get_attr_list( mem_ctx
, attrib_map_v30
);
125 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
132 /**************************************************************************
133 Return the list of attribute names to delete given a user schema version.
134 **************************************************************************/
136 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
139 switch ( schema_ver
) {
140 case SCHEMAVER_SAMBAACCOUNT
:
141 return get_attr_list( mem_ctx
,
142 attrib_map_to_delete_v22
);
144 case SCHEMAVER_SAMBASAMACCOUNT
:
145 return get_attr_list( mem_ctx
,
146 attrib_map_to_delete_v30
);
148 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
156 /*******************************************************************
157 Generate the LDAP search filter for the objectclass based on the
158 version of the schema we are using.
159 ******************************************************************/
161 static const char* get_objclass_filter( int schema_ver
)
163 fstring objclass_filter
;
166 switch( schema_ver
) {
167 case SCHEMAVER_SAMBAACCOUNT
:
168 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
170 case SCHEMAVER_SAMBASAMACCOUNT
:
171 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
174 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
175 objclass_filter
[0] = '\0';
179 result
= talloc_strdup(talloc_tos(), objclass_filter
);
180 SMB_ASSERT(result
!= NULL
);
184 /*****************************************************************
185 Scan a sequence number off OpenLDAP's syncrepl contextCSN
186 ******************************************************************/
188 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
190 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
191 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
192 LDAPMessage
*msg
= NULL
;
193 LDAPMessage
*entry
= NULL
;
195 char **values
= NULL
;
196 int rc
, num_result
, num_values
, rid
;
202 /* Unfortunatly there is no proper way to detect syncrepl-support in
203 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
204 * but do not show up in the root-DSE yet. Neither we can query the
205 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
206 * objectclass. Currently we require lp_ldap_suffix() to show up as
207 * namingContext. - Guenther
210 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
215 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
219 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
220 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
221 "as top-level namingContext\n", lp_ldap_suffix()));
225 mem_ctx
= talloc_init("ldapsam_get_seq_num");
228 return NT_STATUS_NO_MEMORY
;
230 if ((attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2)) == NULL
) {
231 ntstatus
= NT_STATUS_NO_MEMORY
;
235 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
236 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
239 /* consumer syncreplCookie: */
240 /* csn=20050126161620Z#0000001#00#00000 */
241 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
243 suffix
= talloc_asprintf(mem_ctx
,
244 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
246 ntstatus
= NT_STATUS_NO_MEMORY
;
251 /* provider contextCSN */
252 /* 20050126161620Z#000009#00#000000 */
253 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
255 suffix
= talloc_asprintf(mem_ctx
,
256 "cn=ldapsync,%s", lp_ldap_suffix());
259 ntstatus
= NT_STATUS_NO_MEMORY
;
264 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
265 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
267 if (rc
!= LDAP_SUCCESS
) {
271 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
272 if (num_result
!= 1) {
273 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
277 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
279 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
283 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
284 if (values
== NULL
) {
285 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
289 num_values
= ldap_count_values(values
);
290 if (num_values
== 0) {
291 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
296 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
297 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
302 if (!strncmp(p
, "csn=", strlen("csn=")))
305 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
307 *seq_num
= generalized_to_unix_time(p
);
309 /* very basic sanity check */
311 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
316 ntstatus
= NT_STATUS_OK
;
320 ldap_value_free(values
);
324 talloc_destroy(mem_ctx
);
329 /*******************************************************************
330 Run the search by name.
331 ******************************************************************/
333 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
335 LDAPMessage
** result
,
339 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
343 return LDAP_NO_MEMORY
;
347 * in the filter expression, replace %u with the real name
348 * so in ldap filter, %u MUST exist :-)
350 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
351 get_objclass_filter(ldap_state
->schema_ver
));
353 TALLOC_FREE(escape_user
);
354 return LDAP_NO_MEMORY
;
357 * have to use this here because $ is filtered out
361 filter
= talloc_all_string_sub(talloc_tos(),
362 filter
, "%u", escape_user
);
363 TALLOC_FREE(escape_user
);
365 return LDAP_NO_MEMORY
;
368 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
369 filter
, attr
, result
);
374 /*******************************************************************
375 Run the search by rid.
376 ******************************************************************/
378 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
379 uint32 rid
, LDAPMessage
** result
,
385 filter
= talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid
,
386 get_objclass_filter(ldap_state
->schema_ver
));
388 return LDAP_NO_MEMORY
;
391 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
392 filter
, attr
, result
);
397 /*******************************************************************
398 Run the search by SID.
399 ******************************************************************/
401 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
402 const DOM_SID
*sid
, LDAPMessage
** result
,
409 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
410 get_userattr_key2string(ldap_state
->schema_ver
,
412 sid_to_fstring(sid_string
, sid
),
413 get_objclass_filter(ldap_state
->schema_ver
));
415 return LDAP_NO_MEMORY
;
418 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
419 filter
, attr
, result
);
425 /*******************************************************************
426 Delete complete object or objectclass and attrs from
427 object found in search_result depending on lp_ldap_delete_dn
428 ******************************************************************/
430 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
433 const char *objectclass
,
436 LDAPMod
**mods
= NULL
;
439 BerElement
*ptr
= NULL
;
441 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
443 return LDAP_NO_MEMORY
;
446 if (lp_ldap_delete_dn()) {
447 return smbldap_delete(priv
->smbldap_state
, dn
);
450 /* Ok, delete only the SAM attributes */
452 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
454 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
457 /* We are only allowed to delete the attributes that
460 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
461 if (strequal(*attrib
, name
)) {
462 DEBUG(10, ("ldapsam_delete_entry: deleting "
463 "attribute %s\n", name
));
464 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
475 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
476 talloc_autofree_ldapmod(mem_ctx
, mods
);
478 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
481 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
486 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
487 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
493 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
494 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
504 /**********************************************************************
505 Initialize struct samu from an LDAP query.
506 (Based on init_sam_from_buffer in pdb_tdb.c)
507 *********************************************************************/
509 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
510 struct samu
* sampass
,
517 pass_can_change_time
,
518 pass_must_change_time
,
521 char *username
= NULL
,
527 *logon_script
= NULL
,
528 *profile_path
= NULL
,
530 *workstations
= NULL
,
533 uint8 smblmpwd
[LM_HASH_LEN
],
534 smbntpwd
[NT_HASH_LEN
];
535 bool use_samba_attrs
= True
;
536 uint32 acct_ctrl
= 0;
538 uint16 bad_password_count
= 0,
541 uint8 hours
[MAX_HOURS_LEN
];
543 LOGIN_CACHE
*cache_entry
= NULL
;
545 bool expand_explicit
= lp_passdb_expand_explicit();
547 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
552 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
553 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
557 if (priv2ld(ldap_state
) == NULL
) {
558 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
559 "ldap_struct is NULL!\n"));
563 if (!(username
= smbldap_talloc_smallest_attribute(priv2ld(ldap_state
),
567 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
572 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
574 nt_username
= talloc_strdup(ctx
, username
);
579 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
584 pdb_set_username(sampass
, username
, PDB_SET
);
586 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
587 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
589 /* deal with different attributes between the schema first */
591 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
592 if ((temp
= smbldap_talloc_single_attribute(
593 ldap_state
->smbldap_state
->ldap_struct
,
595 get_userattr_key2string(ldap_state
->schema_ver
,
598 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
601 if ((temp
= smbldap_talloc_single_attribute(
602 ldap_state
->smbldap_state
->ldap_struct
,
604 get_userattr_key2string(ldap_state
->schema_ver
,
607 user_rid
= (uint32
)atol(temp
);
608 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
612 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
613 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
614 get_userattr_key2string(ldap_state
->schema_ver
,
616 get_userattr_key2string(ldap_state
->schema_ver
,
622 temp
= smbldap_talloc_single_attribute(
623 ldap_state
->smbldap_state
->ldap_struct
,
625 get_userattr_key2string(ldap_state
->schema_ver
,
626 LDAP_ATTR_PWD_LAST_SET
),
629 pass_last_set_time
= (time_t) atol(temp
);
630 pdb_set_pass_last_set_time(sampass
,
631 pass_last_set_time
, PDB_SET
);
634 temp
= smbldap_talloc_single_attribute(
635 ldap_state
->smbldap_state
->ldap_struct
,
637 get_userattr_key2string(ldap_state
->schema_ver
,
638 LDAP_ATTR_LOGON_TIME
),
641 logon_time
= (time_t) atol(temp
);
642 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
645 temp
= smbldap_talloc_single_attribute(
646 ldap_state
->smbldap_state
->ldap_struct
,
648 get_userattr_key2string(ldap_state
->schema_ver
,
649 LDAP_ATTR_LOGOFF_TIME
),
652 logoff_time
= (time_t) atol(temp
);
653 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
656 temp
= smbldap_talloc_single_attribute(
657 ldap_state
->smbldap_state
->ldap_struct
,
659 get_userattr_key2string(ldap_state
->schema_ver
,
660 LDAP_ATTR_KICKOFF_TIME
),
663 kickoff_time
= (time_t) atol(temp
);
664 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
667 temp
= smbldap_talloc_single_attribute(
668 ldap_state
->smbldap_state
->ldap_struct
,
670 get_userattr_key2string(ldap_state
->schema_ver
,
671 LDAP_ATTR_PWD_CAN_CHANGE
),
674 pass_can_change_time
= (time_t) atol(temp
);
675 pdb_set_pass_can_change_time(sampass
,
676 pass_can_change_time
, PDB_SET
);
679 temp
= smbldap_talloc_single_attribute(
680 ldap_state
->smbldap_state
->ldap_struct
,
682 get_userattr_key2string(ldap_state
->schema_ver
,
683 LDAP_ATTR_PWD_MUST_CHANGE
),
686 pass_must_change_time
= (time_t) atol(temp
);
687 pdb_set_pass_must_change_time(sampass
,
688 pass_must_change_time
, PDB_SET
);
691 /* recommend that 'gecos' and 'displayName' should refer to the same
692 * attribute OID. userFullName depreciated, only used by Samba
693 * primary rules of LDAP: don't make a new attribute when one is already defined
694 * that fits your needs; using cn then displayName rather than 'userFullName'
697 fullname
= smbldap_talloc_single_attribute(
698 ldap_state
->smbldap_state
->ldap_struct
,
700 get_userattr_key2string(ldap_state
->schema_ver
,
701 LDAP_ATTR_DISPLAY_NAME
),
704 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
706 fullname
= smbldap_talloc_single_attribute(
707 ldap_state
->smbldap_state
->ldap_struct
,
709 get_userattr_key2string(ldap_state
->schema_ver
,
713 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
717 dir_drive
= smbldap_talloc_single_attribute(
718 ldap_state
->smbldap_state
->ldap_struct
,
720 get_userattr_key2string(ldap_state
->schema_ver
,
721 LDAP_ATTR_HOME_DRIVE
),
724 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
726 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
729 homedir
= smbldap_talloc_single_attribute(
730 ldap_state
->smbldap_state
->ldap_struct
,
732 get_userattr_key2string(ldap_state
->schema_ver
,
733 LDAP_ATTR_HOME_PATH
),
736 if (expand_explicit
) {
737 homedir
= talloc_sub_basic(ctx
,
745 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
747 pdb_set_homedir(sampass
,
748 talloc_sub_basic(ctx
, username
, domain
,
753 logon_script
= smbldap_talloc_single_attribute(
754 ldap_state
->smbldap_state
->ldap_struct
,
756 get_userattr_key2string(ldap_state
->schema_ver
,
757 LDAP_ATTR_LOGON_SCRIPT
),
760 if (expand_explicit
) {
761 logon_script
= talloc_sub_basic(ctx
,
769 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
771 pdb_set_logon_script(sampass
,
772 talloc_sub_basic(ctx
, username
, domain
,
777 profile_path
= smbldap_talloc_single_attribute(
778 ldap_state
->smbldap_state
->ldap_struct
,
780 get_userattr_key2string(ldap_state
->schema_ver
,
781 LDAP_ATTR_PROFILE_PATH
),
784 if (expand_explicit
) {
785 profile_path
= talloc_sub_basic(ctx
,
793 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
795 pdb_set_profile_path(sampass
,
796 talloc_sub_basic(ctx
, username
, domain
,
801 acct_desc
= smbldap_talloc_single_attribute(
802 ldap_state
->smbldap_state
->ldap_struct
,
804 get_userattr_key2string(ldap_state
->schema_ver
,
808 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
811 workstations
= smbldap_talloc_single_attribute(
812 ldap_state
->smbldap_state
->ldap_struct
,
814 get_userattr_key2string(ldap_state
->schema_ver
,
818 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
821 munged_dial
= smbldap_talloc_single_attribute(
822 ldap_state
->smbldap_state
->ldap_struct
,
824 get_userattr_key2string(ldap_state
->schema_ver
,
825 LDAP_ATTR_MUNGED_DIAL
),
828 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
831 /* FIXME: hours stuff should be cleaner */
835 memset(hours
, 0xff, hours_len
);
837 if (ldap_state
->is_nds_ldap
) {
840 char clear_text_pw
[512];
842 /* Make call to Novell eDirectory ldap extension to get clear text password.
843 NOTE: This will only work if we have an SSL connection to eDirectory. */
844 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
845 if (user_dn
!= NULL
) {
846 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
848 pwd_len
= sizeof(clear_text_pw
);
849 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
850 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
851 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
852 TALLOC_FREE(user_dn
);
855 ZERO_STRUCT(smblmpwd
);
856 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
857 TALLOC_FREE(user_dn
);
860 ZERO_STRUCT(smbntpwd
);
861 use_samba_attrs
= False
;
864 TALLOC_FREE(user_dn
);
867 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
871 if (use_samba_attrs
) {
872 temp
= smbldap_talloc_single_attribute(
873 ldap_state
->smbldap_state
->ldap_struct
,
875 get_userattr_key2string(ldap_state
->schema_ver
,
879 pdb_gethexpwd(temp
, smblmpwd
);
880 memset((char *)temp
, '\0', strlen(temp
)+1);
881 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
884 ZERO_STRUCT(smblmpwd
);
887 temp
= smbldap_talloc_single_attribute(
888 ldap_state
->smbldap_state
->ldap_struct
,
890 get_userattr_key2string(ldap_state
->schema_ver
,
894 pdb_gethexpwd(temp
, smbntpwd
);
895 memset((char *)temp
, '\0', strlen(temp
)+1);
896 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
899 ZERO_STRUCT(smbntpwd
);
905 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
907 uint8
*pwhist
= NULL
;
909 char *history_string
= TALLOC_ARRAY(ctx
, char,
910 MAX_PW_HISTORY_LEN
*64);
912 if (!history_string
) {
916 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
918 if ((pwhist
= TALLOC_ARRAY(ctx
, uint8
,
919 pwHistLen
* PW_HISTORY_ENTRY_LEN
)) ==
921 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
924 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
926 if (smbldap_get_single_attribute(
927 ldap_state
->smbldap_state
->ldap_struct
,
929 get_userattr_key2string(ldap_state
->schema_ver
,
930 LDAP_ATTR_PWD_HISTORY
),
932 MAX_PW_HISTORY_LEN
*64)) {
933 bool hex_failed
= false;
934 for (i
= 0; i
< pwHistLen
; i
++){
935 /* Get the 16 byte salt. */
936 if (!pdb_gethexpwd(&history_string
[i
*64],
937 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
941 /* Get the 16 byte MD5 hash of salt+passwd. */
942 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
943 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
944 PW_HISTORY_SALT_LEN
])) {
950 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
952 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
955 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
960 temp
= smbldap_talloc_single_attribute(
961 ldap_state
->smbldap_state
->ldap_struct
,
963 get_userattr_key2string(ldap_state
->schema_ver
,
967 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
969 if (acct_ctrl
== 0) {
970 acct_ctrl
|= ACB_NORMAL
;
973 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
975 acct_ctrl
|= ACB_NORMAL
;
978 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
979 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
981 temp
= smbldap_talloc_single_attribute(
982 ldap_state
->smbldap_state
->ldap_struct
,
984 get_userattr_key2string(ldap_state
->schema_ver
,
985 LDAP_ATTR_BAD_PASSWORD_COUNT
),
988 bad_password_count
= (uint32
) atol(temp
);
989 pdb_set_bad_password_count(sampass
,
990 bad_password_count
, PDB_SET
);
993 temp
= smbldap_talloc_single_attribute(
994 ldap_state
->smbldap_state
->ldap_struct
,
996 get_userattr_key2string(ldap_state
->schema_ver
,
997 LDAP_ATTR_BAD_PASSWORD_TIME
),
1000 bad_password_time
= (time_t) atol(temp
);
1001 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
1005 temp
= smbldap_talloc_single_attribute(
1006 ldap_state
->smbldap_state
->ldap_struct
,
1008 get_userattr_key2string(ldap_state
->schema_ver
,
1009 LDAP_ATTR_LOGON_COUNT
),
1012 logon_count
= (uint32
) atol(temp
);
1013 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
1016 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1018 temp
= smbldap_talloc_single_attribute(
1019 ldap_state
->smbldap_state
->ldap_struct
,
1021 get_userattr_key2string(ldap_state
->schema_ver
,
1022 LDAP_ATTR_LOGON_HOURS
),
1025 pdb_gethexhours(temp
, hours
);
1026 memset((char *)temp
, '\0', strlen(temp
) +1);
1027 pdb_set_hours(sampass
, hours
, PDB_SET
);
1031 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1032 temp
= smbldap_talloc_single_attribute(
1033 priv2ld(ldap_state
),
1038 /* We've got a uid, feed the cache */
1039 uid_t uid
= strtoul(temp
, NULL
, 10);
1040 store_uid_sid_cache(pdb_get_user_sid(sampass
), uid
);
1044 /* check the timestamp of the cache vs ldap entry */
1045 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1051 /* see if we have newer updates */
1052 if (!(cache_entry
= login_cache_read(sampass
))) {
1053 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1054 (unsigned int)pdb_get_bad_password_count(sampass
),
1055 (unsigned int)pdb_get_bad_password_time(sampass
)));
1060 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1061 (unsigned int)ldap_entry_time
,
1062 (unsigned int)cache_entry
->entry_timestamp
,
1063 (unsigned int)cache_entry
->bad_password_time
));
1065 if (ldap_entry_time
> cache_entry
->entry_timestamp
) {
1066 /* cache is older than directory , so
1067 we need to delete the entry but allow the
1068 fields to be written out */
1069 login_cache_delentry(sampass
);
1072 pdb_set_acct_ctrl(sampass
,
1073 pdb_get_acct_ctrl(sampass
) |
1074 (cache_entry
->acct_ctrl
& ACB_AUTOLOCK
),
1076 pdb_set_bad_password_count(sampass
,
1077 cache_entry
->bad_password_count
,
1079 pdb_set_bad_password_time(sampass
,
1080 cache_entry
->bad_password_time
,
1089 SAFE_FREE(cache_entry
);
1093 /**********************************************************************
1094 Initialize the ldap db from a struct samu. Called on update.
1095 (Based on init_buffer_from_sam in pdb_tdb.c)
1096 *********************************************************************/
1098 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1099 LDAPMessage
*existing
,
1100 LDAPMod
*** mods
, struct samu
* sampass
,
1101 bool (*need_update
)(const struct samu
*,
1107 if (mods
== NULL
|| sampass
== NULL
) {
1108 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1115 * took out adding "objectclass: sambaAccount"
1116 * do this on a per-mod basis
1118 if (need_update(sampass
, PDB_USERNAME
)) {
1119 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1120 "uid", pdb_get_username(sampass
));
1121 if (ldap_state
->is_nds_ldap
) {
1122 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1123 "cn", pdb_get_username(sampass
));
1124 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1125 "sn", pdb_get_username(sampass
));
1129 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1131 /* only update the RID if we actually need to */
1132 if (need_update(sampass
, PDB_USERSID
)) {
1134 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
1136 switch ( ldap_state
->schema_ver
) {
1137 case SCHEMAVER_SAMBAACCOUNT
:
1138 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1139 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1140 sid_string_dbg(user_sid
),
1142 &ldap_state
->domain_sid
)));
1145 if (asprintf(&temp
, "%i", rid
) < 0) {
1148 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1149 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1154 case SCHEMAVER_SAMBASAMACCOUNT
:
1155 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1156 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1157 sid_to_fstring(sid_string
, user_sid
));
1161 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1166 /* we don't need to store the primary group RID - so leaving it
1167 'free' to hang off the unix primary group makes life easier */
1169 if (need_update(sampass
, PDB_GROUPSID
)) {
1171 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
1173 switch ( ldap_state
->schema_ver
) {
1174 case SCHEMAVER_SAMBAACCOUNT
:
1175 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1176 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1177 sid_string_dbg(group_sid
),
1179 &ldap_state
->domain_sid
)));
1183 if (asprintf(&temp
, "%i", rid
) < 0) {
1186 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1187 get_userattr_key2string(ldap_state
->schema_ver
,
1188 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1192 case SCHEMAVER_SAMBASAMACCOUNT
:
1193 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1194 get_userattr_key2string(ldap_state
->schema_ver
,
1195 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1199 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1205 /* displayName, cn, and gecos should all be the same
1206 * most easily accomplished by giving them the same OID
1207 * gecos isn't set here b/c it should be handled by the
1209 * We change displayName only and fall back to cn if
1210 * it does not exist.
1213 if (need_update(sampass
, PDB_FULLNAME
))
1214 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1215 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1216 pdb_get_fullname(sampass
));
1218 if (need_update(sampass
, PDB_ACCTDESC
))
1219 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1220 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1221 pdb_get_acct_desc(sampass
));
1223 if (need_update(sampass
, PDB_WORKSTATIONS
))
1224 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1225 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1226 pdb_get_workstations(sampass
));
1228 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1229 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1230 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1231 pdb_get_munged_dial(sampass
));
1233 if (need_update(sampass
, PDB_SMBHOME
))
1234 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1235 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1236 pdb_get_homedir(sampass
));
1238 if (need_update(sampass
, PDB_DRIVE
))
1239 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1240 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1241 pdb_get_dir_drive(sampass
));
1243 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1244 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1245 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1246 pdb_get_logon_script(sampass
));
1248 if (need_update(sampass
, PDB_PROFILE
))
1249 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1250 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1251 pdb_get_profile_path(sampass
));
1253 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1256 if (need_update(sampass
, PDB_LOGONTIME
))
1257 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1258 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1261 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1264 if (need_update(sampass
, PDB_LOGOFFTIME
))
1265 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1266 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1269 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1272 if (need_update(sampass
, PDB_KICKOFFTIME
))
1273 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1274 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1277 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1280 if (need_update(sampass
, PDB_CANCHANGETIME
))
1281 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1282 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1285 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_must_change_time(sampass
)) < 0) {
1288 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1289 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1290 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1293 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1294 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1296 if (need_update(sampass
, PDB_LMPASSWD
)) {
1297 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1300 pdb_sethexpwd(pwstr
, lm_pw
,
1301 pdb_get_acct_ctrl(sampass
));
1302 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1303 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1306 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1307 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1311 if (need_update(sampass
, PDB_NTPASSWD
)) {
1312 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1315 pdb_sethexpwd(pwstr
, nt_pw
,
1316 pdb_get_acct_ctrl(sampass
));
1317 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1318 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1321 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1322 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1327 if (need_update(sampass
, PDB_PWHISTORY
)) {
1329 uint32 pwHistLen
= 0;
1330 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1332 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1336 if (pwHistLen
== 0) {
1337 /* Remove any password history from the LDAP store. */
1338 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1342 uint32 currHistLen
= 0;
1343 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1344 if (pwhist
!= NULL
) {
1345 /* We can only store (1024-1/64 password history entries. */
1346 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1347 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1348 /* Store the salt. */
1349 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1350 /* Followed by the md5 hash of salt + md4 hash */
1351 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1352 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1353 DEBUG(100, ("pwstr=%s\n", pwstr
));
1357 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1358 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1363 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1364 if (asprintf(&temp
, "%li",
1365 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1368 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1369 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1375 if (need_update(sampass
, PDB_HOURS
)) {
1376 const uint8
*hours
= pdb_get_hours(sampass
);
1379 pdb_sethexhours(hourstr
, hours
);
1380 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1383 get_userattr_key2string(ldap_state
->schema_ver
,
1384 LDAP_ATTR_LOGON_HOURS
),
1389 if (need_update(sampass
, PDB_ACCTCTRL
))
1390 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1391 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1392 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1394 /* password lockout cache:
1395 - If we are now autolocking or clearing, we write to ldap
1396 - If we are clearing, we delete the cache entry
1397 - If the count is > 0, we update the cache
1399 This even means when autolocking, we cache, just in case the
1400 update doesn't work, and we have to cache the autolock flag */
1402 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1403 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1404 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1405 time_t badtime
= pdb_get_bad_password_time(sampass
);
1407 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1409 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1410 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1412 if ((badcount
>= pol
) || (badcount
== 0)) {
1413 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1414 (unsigned int)badcount
, (unsigned int)badtime
));
1415 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1419 ldap_state
->smbldap_state
->ldap_struct
,
1421 get_userattr_key2string(
1422 ldap_state
->schema_ver
,
1423 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1427 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1431 ldap_state
->smbldap_state
->ldap_struct
,
1433 get_userattr_key2string(
1434 ldap_state
->schema_ver
,
1435 LDAP_ATTR_BAD_PASSWORD_TIME
),
1439 if (badcount
== 0) {
1440 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1441 login_cache_delentry(sampass
);
1443 LOGIN_CACHE cache_entry
;
1445 cache_entry
.entry_timestamp
= time(NULL
);
1446 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1447 cache_entry
.bad_password_count
= badcount
;
1448 cache_entry
.bad_password_time
= badtime
;
1450 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1451 login_cache_write(sampass
, cache_entry
);
1458 /**********************************************************************
1459 End enumeration of the LDAP password list.
1460 *********************************************************************/
1462 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1464 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1465 if (ldap_state
->result
) {
1466 ldap_msgfree(ldap_state
->result
);
1467 ldap_state
->result
= NULL
;
1471 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1472 const char *new_attr
)
1476 if (new_attr
== NULL
) {
1480 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1484 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1486 SMB_ASSERT((*attr_list
) != NULL
);
1487 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1488 (*attr_list
)[i
+1] = NULL
;
1491 /**********************************************************************
1492 Get struct samu entry from LDAP by username.
1493 *********************************************************************/
1495 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1497 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1498 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1499 LDAPMessage
*result
= NULL
;
1500 LDAPMessage
*entry
= NULL
;
1502 const char ** attr_list
;
1505 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1506 append_attr(user
, &attr_list
,
1507 get_userattr_key2string(ldap_state
->schema_ver
,
1508 LDAP_ATTR_MOD_TIMESTAMP
));
1509 append_attr(user
, &attr_list
, "uidNumber");
1510 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1512 TALLOC_FREE( attr_list
);
1514 if ( rc
!= LDAP_SUCCESS
)
1515 return NT_STATUS_NO_SUCH_USER
;
1517 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1520 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1521 ldap_msgfree(result
);
1522 return NT_STATUS_NO_SUCH_USER
;
1523 } else if (count
> 1) {
1524 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1525 ldap_msgfree(result
);
1526 return NT_STATUS_NO_SUCH_USER
;
1529 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1531 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1532 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1533 ldap_msgfree(result
);
1534 return NT_STATUS_NO_SUCH_USER
;
1536 pdb_set_backend_private_data(user
, result
, NULL
,
1537 my_methods
, PDB_CHANGED
);
1538 talloc_autofree_ldapmsg(user
, result
);
1541 ldap_msgfree(result
);
1546 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1547 const DOM_SID
*sid
, LDAPMessage
**result
)
1550 const char ** attr_list
;
1553 switch ( ldap_state
->schema_ver
) {
1554 case SCHEMAVER_SAMBASAMACCOUNT
: {
1555 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1556 if (tmp_ctx
== NULL
) {
1557 return LDAP_NO_MEMORY
;
1560 attr_list
= get_userattr_list(tmp_ctx
,
1561 ldap_state
->schema_ver
);
1562 append_attr(tmp_ctx
, &attr_list
,
1563 get_userattr_key2string(
1564 ldap_state
->schema_ver
,
1565 LDAP_ATTR_MOD_TIMESTAMP
));
1566 append_attr(tmp_ctx
, &attr_list
, "uidNumber");
1567 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1569 TALLOC_FREE(tmp_ctx
);
1571 if ( rc
!= LDAP_SUCCESS
)
1576 case SCHEMAVER_SAMBAACCOUNT
:
1577 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1581 attr_list
= get_userattr_list(NULL
,
1582 ldap_state
->schema_ver
);
1583 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1584 TALLOC_FREE( attr_list
);
1586 if ( rc
!= LDAP_SUCCESS
)
1593 /**********************************************************************
1594 Get struct samu entry from LDAP by SID.
1595 *********************************************************************/
1597 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1599 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1600 LDAPMessage
*result
= NULL
;
1601 LDAPMessage
*entry
= NULL
;
1605 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1607 if (rc
!= LDAP_SUCCESS
)
1608 return NT_STATUS_NO_SUCH_USER
;
1610 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1613 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1614 "count=%d\n", sid_string_dbg(sid
), count
));
1615 ldap_msgfree(result
);
1616 return NT_STATUS_NO_SUCH_USER
;
1617 } else if (count
> 1) {
1618 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1619 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1621 ldap_msgfree(result
);
1622 return NT_STATUS_NO_SUCH_USER
;
1625 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1627 ldap_msgfree(result
);
1628 return NT_STATUS_NO_SUCH_USER
;
1631 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1632 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1633 ldap_msgfree(result
);
1634 return NT_STATUS_NO_SUCH_USER
;
1637 pdb_set_backend_private_data(user
, result
, NULL
,
1638 my_methods
, PDB_CHANGED
);
1639 talloc_autofree_ldapmsg(user
, result
);
1640 return NT_STATUS_OK
;
1643 /********************************************************************
1644 Do the actual modification - also change a plaintext passord if
1646 **********************************************************************/
1648 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1649 struct samu
*newpwd
, char *dn
,
1650 LDAPMod
**mods
, int ldap_op
,
1651 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1653 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1656 if (!newpwd
|| !dn
) {
1657 return NT_STATUS_INVALID_PARAMETER
;
1661 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1662 /* may be password change below however */
1666 if (ldap_state
->is_nds_ldap
) {
1667 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1671 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1675 rc
= smbldap_add(ldap_state
->smbldap_state
,
1678 case LDAP_MOD_REPLACE
:
1679 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1683 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1685 return NT_STATUS_INVALID_PARAMETER
;
1688 if (rc
!=LDAP_SUCCESS
) {
1689 return NT_STATUS_UNSUCCESSFUL
;
1693 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1694 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1695 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1696 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1699 char *retoid
= NULL
;
1700 struct berval
*retdata
= NULL
;
1701 char *utf8_password
;
1703 size_t converted_size
;
1706 if (!ldap_state
->is_nds_ldap
) {
1708 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1709 LDAP_EXOP_MODIFY_PASSWD
)) {
1710 DEBUG(2, ("ldap password change requested, but LDAP "
1711 "server does not support it -- ignoring\n"));
1712 return NT_STATUS_OK
;
1716 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1717 pdb_get_plaintext_passwd(newpwd
),
1720 return NT_STATUS_NO_MEMORY
;
1723 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1724 TALLOC_FREE(utf8_password
);
1725 return NT_STATUS_NO_MEMORY
;
1728 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1729 DEBUG(0,("ber_alloc_t returns NULL\n"));
1730 TALLOC_FREE(utf8_password
);
1731 TALLOC_FREE(utf8_dn
);
1732 return NT_STATUS_UNSUCCESSFUL
;
1735 if ((ber_printf (ber
, "{") < 0) ||
1736 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1738 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1741 TALLOC_FREE(utf8_dn
);
1742 TALLOC_FREE(utf8_password
);
1743 return NT_STATUS_UNSUCCESSFUL
;
1746 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1747 ret
= ber_printf(ber
, "ts}",
1748 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1751 ret
= ber_printf(ber
, "}");
1755 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1758 TALLOC_FREE(utf8_dn
);
1759 TALLOC_FREE(utf8_password
);
1760 return NT_STATUS_UNSUCCESSFUL
;
1763 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1764 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1766 TALLOC_FREE(utf8_dn
);
1767 TALLOC_FREE(utf8_password
);
1768 return NT_STATUS_UNSUCCESSFUL
;
1771 TALLOC_FREE(utf8_dn
);
1772 TALLOC_FREE(utf8_password
);
1775 if (!ldap_state
->is_nds_ldap
) {
1776 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1777 LDAP_EXOP_MODIFY_PASSWD
,
1778 bv
, NULL
, NULL
, &retoid
,
1781 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1782 pdb_get_plaintext_passwd(newpwd
));
1784 if (rc
!= LDAP_SUCCESS
) {
1785 char *ld_error
= NULL
;
1787 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1788 DEBUG(3, ("Could not set userPassword "
1789 "attribute due to an objectClass "
1790 "violation -- ignoring\n"));
1792 return NT_STATUS_OK
;
1795 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1797 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1798 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1799 SAFE_FREE(ld_error
);
1801 #if defined(LDAP_CONSTRAINT_VIOLATION)
1802 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1803 return NT_STATUS_PASSWORD_RESTRICTION
;
1805 return NT_STATUS_UNSUCCESSFUL
;
1807 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1808 #ifdef DEBUG_PASSWORD
1809 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1812 ber_bvfree(retdata
);
1814 ldap_memfree(retoid
);
1818 return NT_STATUS_OK
;
1821 /**********************************************************************
1822 Delete entry from LDAP for username.
1823 *********************************************************************/
1825 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1826 struct samu
* sam_acct
)
1828 struct ldapsam_privates
*priv
=
1829 (struct ldapsam_privates
*)my_methods
->private_data
;
1832 LDAPMessage
*msg
, *entry
;
1833 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1834 const char **attr_list
;
1835 TALLOC_CTX
*mem_ctx
;
1838 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1839 return NT_STATUS_INVALID_PARAMETER
;
1842 sname
= pdb_get_username(sam_acct
);
1844 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1847 mem_ctx
= talloc_new(NULL
);
1848 if (mem_ctx
== NULL
) {
1849 DEBUG(0, ("talloc_new failed\n"));
1853 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1854 if (attr_list
== NULL
) {
1858 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1860 if ((rc
!= LDAP_SUCCESS
) ||
1861 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1862 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1863 DEBUG(5, ("Could not find user %s\n", sname
));
1864 result
= NT_STATUS_NO_SUCH_USER
;
1868 rc
= ldapsam_delete_entry(
1869 priv
, mem_ctx
, entry
,
1870 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1871 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1874 result
= (rc
== LDAP_SUCCESS
) ?
1875 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1878 TALLOC_FREE(mem_ctx
);
1882 /**********************************************************************
1883 Helper function to determine for update_sam_account whether
1884 we need LDAP modification.
1885 *********************************************************************/
1887 static bool element_is_changed(const struct samu
*sampass
,
1888 enum pdb_elements element
)
1890 return IS_SAM_CHANGED(sampass
, element
);
1893 /**********************************************************************
1895 *********************************************************************/
1897 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1899 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1900 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1903 LDAPMessage
*result
= NULL
;
1904 LDAPMessage
*entry
= NULL
;
1905 LDAPMod
**mods
= NULL
;
1906 const char **attr_list
;
1908 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1910 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1911 if (pdb_get_username(newpwd
) == NULL
) {
1912 return NT_STATUS_INVALID_PARAMETER
;
1914 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1915 TALLOC_FREE( attr_list
);
1916 if (rc
!= LDAP_SUCCESS
) {
1917 return NT_STATUS_UNSUCCESSFUL
;
1919 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1920 my_methods
, PDB_CHANGED
);
1921 talloc_autofree_ldapmsg(newpwd
, result
);
1924 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1925 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1926 return NT_STATUS_UNSUCCESSFUL
;
1929 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1930 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
1932 return NT_STATUS_UNSUCCESSFUL
;
1935 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1937 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1938 element_is_changed
)) {
1939 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1942 ldap_mods_free(mods
,True
);
1943 return NT_STATUS_UNSUCCESSFUL
;
1946 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1947 && (mods
== NULL
)) {
1948 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1949 pdb_get_username(newpwd
)));
1951 return NT_STATUS_OK
;
1954 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1957 ldap_mods_free(mods
,True
);
1963 * We need to set the backend private data to NULL here. For example
1964 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1965 * same one, and with the explicit delete / add logic for attribute
1966 * values the second time we would use the wrong "old" value which
1967 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1969 * The existing LDAPMessage is still being auto-freed by the
1972 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
1975 if (!NT_STATUS_IS_OK(ret
)) {
1979 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1980 pdb_get_username(newpwd
)));
1981 return NT_STATUS_OK
;
1984 /***************************************************************************
1985 Renames a struct samu
1986 - The "rename user script" has full responsibility for changing everything
1987 ***************************************************************************/
1989 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1990 struct samu
*old_acct
,
1991 const char *newname
)
1993 const char *oldname
;
1995 char *rename_script
= NULL
;
1996 fstring oldname_lower
, newname_lower
;
1999 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2000 return NT_STATUS_INVALID_PARAMETER
;
2003 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2004 return NT_STATUS_INVALID_PARAMETER
;
2007 oldname
= pdb_get_username(old_acct
);
2009 /* rename the posix user */
2010 rename_script
= SMB_STRDUP(lp_renameuser_script());
2011 if (rename_script
== NULL
) {
2012 return NT_STATUS_NO_MEMORY
;
2015 if (!(*rename_script
)) {
2016 SAFE_FREE(rename_script
);
2017 return NT_STATUS_ACCESS_DENIED
;
2020 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2023 /* We have to allow the account name to end with a '$'.
2024 Also, follow the semantics in _samr_create_user() and lower case the
2025 posix name but preserve the case in passdb */
2027 fstrcpy( oldname_lower
, oldname
);
2028 strlower_m( oldname_lower
);
2029 fstrcpy( newname_lower
, newname
);
2030 strlower_m( newname_lower
);
2031 rename_script
= realloc_string_sub2(rename_script
,
2036 if (!rename_script
) {
2037 return NT_STATUS_NO_MEMORY
;
2039 rename_script
= realloc_string_sub2(rename_script
,
2044 rc
= smbrun(rename_script
, NULL
);
2046 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2047 rename_script
, rc
));
2049 SAFE_FREE(rename_script
);
2052 smb_nscd_flush_user_cache();
2056 return NT_STATUS_UNSUCCESSFUL
;
2058 return NT_STATUS_OK
;
2061 /**********************************************************************
2062 Helper function to determine for update_sam_account whether
2063 we need LDAP modification.
2064 *********************************************************************/
2066 static bool element_is_set_or_changed(const struct samu
*sampass
,
2067 enum pdb_elements element
)
2069 return (IS_SAM_SET(sampass
, element
) ||
2070 IS_SAM_CHANGED(sampass
, element
));
2073 /**********************************************************************
2074 Add struct samu to LDAP.
2075 *********************************************************************/
2077 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2079 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2080 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2082 LDAPMessage
*result
= NULL
;
2083 LDAPMessage
*entry
= NULL
;
2084 LDAPMod
**mods
= NULL
;
2085 int ldap_op
= LDAP_MOD_REPLACE
;
2087 const char **attr_list
;
2088 char *escape_user
= NULL
;
2089 const char *username
= pdb_get_username(newpwd
);
2090 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
2091 char *filter
= NULL
;
2093 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2094 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2097 return NT_STATUS_NO_MEMORY
;
2100 if (!username
|| !*username
) {
2101 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2102 status
= NT_STATUS_INVALID_PARAMETER
;
2106 /* free this list after the second search or in case we exit on failure */
2107 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2109 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2111 if (rc
!= LDAP_SUCCESS
) {
2115 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2116 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2120 ldap_msgfree(result
);
2123 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2124 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2126 if (rc
== LDAP_SUCCESS
) {
2127 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2128 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2129 "already in the base, with samba "
2130 "attributes\n", sid_string_dbg(sid
)));
2133 ldap_msgfree(result
);
2138 /* does the entry already exist but without a samba attributes?
2139 we need to return the samba attributes here */
2141 escape_user
= escape_ldap_string(talloc_tos(), username
);
2142 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2144 status
= NT_STATUS_NO_MEMORY
;
2147 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2148 TALLOC_FREE(escape_user
);
2150 status
= NT_STATUS_NO_MEMORY
;
2154 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2155 filter
, attr_list
, &result
);
2156 if ( rc
!= LDAP_SUCCESS
) {
2160 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2162 if (num_result
> 1) {
2163 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2167 /* Check if we need to update an existing entry */
2168 if (num_result
== 1) {
2169 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2170 ldap_op
= LDAP_MOD_REPLACE
;
2171 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2172 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2174 status
= NT_STATUS_NO_MEMORY
;
2178 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2180 /* There might be a SID for this account already - say an idmap entry */
2182 filter
= talloc_asprintf(ctx
,
2183 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2184 get_userattr_key2string(ldap_state
->schema_ver
,
2185 LDAP_ATTR_USER_SID
),
2186 sid_string_talloc(ctx
, sid
),
2187 LDAP_OBJ_IDMAP_ENTRY
,
2188 LDAP_OBJ_SID_ENTRY
);
2190 status
= NT_STATUS_NO_MEMORY
;
2194 /* free old result before doing a new search */
2195 if (result
!= NULL
) {
2196 ldap_msgfree(result
);
2199 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2200 filter
, attr_list
, &result
);
2202 if ( rc
!= LDAP_SUCCESS
) {
2206 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2208 if (num_result
> 1) {
2209 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2213 /* Check if we need to update an existing entry */
2214 if (num_result
== 1) {
2216 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2217 ldap_op
= LDAP_MOD_REPLACE
;
2218 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2219 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2221 status
= NT_STATUS_NO_MEMORY
;
2227 if (num_result
== 0) {
2228 char *escape_username
;
2229 /* Check if we need to add an entry */
2230 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2231 ldap_op
= LDAP_MOD_ADD
;
2233 escape_username
= escape_rdn_val_string_alloc(username
);
2234 if (!escape_username
) {
2235 status
= NT_STATUS_NO_MEMORY
;
2239 if (username
[strlen(username
)-1] == '$') {
2240 dn
= talloc_asprintf(ctx
,
2243 lp_ldap_machine_suffix());
2245 dn
= talloc_asprintf(ctx
,
2248 lp_ldap_user_suffix());
2251 SAFE_FREE(escape_username
);
2253 status
= NT_STATUS_NO_MEMORY
;
2258 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2259 element_is_set_or_changed
)) {
2260 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2262 ldap_mods_free(mods
, true);
2268 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2271 switch ( ldap_state
->schema_ver
) {
2272 case SCHEMAVER_SAMBAACCOUNT
:
2273 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2275 case SCHEMAVER_SAMBASAMACCOUNT
:
2276 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2279 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2283 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2284 if (!NT_STATUS_IS_OK(ret
)) {
2285 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2286 pdb_get_username(newpwd
),dn
));
2287 ldap_mods_free(mods
, true);
2291 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2292 ldap_mods_free(mods
, true);
2294 status
= NT_STATUS_OK
;
2300 ldap_msgfree(result
);
2305 /**********************************************************************
2306 *********************************************************************/
2308 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2310 LDAPMessage
** result
)
2312 int scope
= LDAP_SCOPE_SUBTREE
;
2314 const char **attr_list
;
2316 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2317 rc
= smbldap_search(ldap_state
->smbldap_state
,
2318 lp_ldap_suffix (), scope
,
2319 filter
, attr_list
, 0, result
);
2320 TALLOC_FREE(attr_list
);
2325 /**********************************************************************
2326 *********************************************************************/
2328 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2329 GROUP_MAP
*map
, LDAPMessage
*entry
)
2332 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2334 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2335 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2336 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2341 temp
= smbldap_talloc_single_attribute(
2342 ldap_state
->smbldap_state
->ldap_struct
,
2344 get_attr_key2string(groupmap_attr_list
,
2345 LDAP_ATTR_GIDNUMBER
),
2348 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2349 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2353 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2355 map
->gid
= (gid_t
)atol(temp
);
2358 temp
= smbldap_talloc_single_attribute(
2359 ldap_state
->smbldap_state
->ldap_struct
,
2361 get_attr_key2string(groupmap_attr_list
,
2362 LDAP_ATTR_GROUP_SID
),
2365 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2366 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2371 if (!string_to_sid(&map
->sid
, temp
)) {
2372 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2378 temp
= smbldap_talloc_single_attribute(
2379 ldap_state
->smbldap_state
->ldap_struct
,
2381 get_attr_key2string(groupmap_attr_list
,
2382 LDAP_ATTR_GROUP_TYPE
),
2385 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2386 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2390 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2392 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2393 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2394 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2400 temp
= smbldap_talloc_single_attribute(
2401 ldap_state
->smbldap_state
->ldap_struct
,
2403 get_attr_key2string(groupmap_attr_list
,
2404 LDAP_ATTR_DISPLAY_NAME
),
2407 temp
= smbldap_talloc_single_attribute(
2408 ldap_state
->smbldap_state
->ldap_struct
,
2410 get_attr_key2string(groupmap_attr_list
,
2414 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2415 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2420 fstrcpy(map
->nt_name
, temp
);
2423 temp
= smbldap_talloc_single_attribute(
2424 ldap_state
->smbldap_state
->ldap_struct
,
2426 get_attr_key2string(groupmap_attr_list
,
2430 temp
= talloc_strdup(ctx
, "");
2436 fstrcpy(map
->comment
, temp
);
2438 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2439 store_gid_sid_cache(&map
->sid
, map
->gid
);
2446 /**********************************************************************
2447 *********************************************************************/
2449 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2453 struct ldapsam_privates
*ldap_state
=
2454 (struct ldapsam_privates
*)methods
->private_data
;
2455 LDAPMessage
*result
= NULL
;
2456 LDAPMessage
*entry
= NULL
;
2459 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2461 return NT_STATUS_NO_SUCH_GROUP
;
2464 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2467 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2469 ldap_msgfree(result
);
2470 return NT_STATUS_NO_SUCH_GROUP
;
2474 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2475 "count=%d\n", filter
, count
));
2476 ldap_msgfree(result
);
2477 return NT_STATUS_NO_SUCH_GROUP
;
2480 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2483 ldap_msgfree(result
);
2484 return NT_STATUS_UNSUCCESSFUL
;
2487 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2488 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2489 "group filter %s\n", filter
));
2490 ldap_msgfree(result
);
2491 return NT_STATUS_NO_SUCH_GROUP
;
2494 ldap_msgfree(result
);
2495 return NT_STATUS_OK
;
2498 /**********************************************************************
2499 *********************************************************************/
2501 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2504 char *filter
= NULL
;
2508 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2510 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2511 sid_to_fstring(tmp
, &sid
)) < 0) {
2512 return NT_STATUS_NO_MEMORY
;
2515 status
= ldapsam_getgroup(methods
, filter
, map
);
2520 /**********************************************************************
2521 *********************************************************************/
2523 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2526 char *filter
= NULL
;
2529 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2531 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2532 (unsigned long)gid
) < 0) {
2533 return NT_STATUS_NO_MEMORY
;
2536 status
= ldapsam_getgroup(methods
, filter
, map
);
2541 /**********************************************************************
2542 *********************************************************************/
2544 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2547 char *filter
= NULL
;
2548 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2552 return NT_STATUS_NO_MEMORY
;
2555 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2557 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2558 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2560 TALLOC_FREE(escape_name
);
2561 return NT_STATUS_NO_MEMORY
;
2564 TALLOC_FREE(escape_name
);
2565 status
= ldapsam_getgroup(methods
, filter
, map
);
2570 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2572 const DOM_SID
*domain_sid
,
2578 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2579 str
, sizeof(str
)-1)) {
2580 DEBUG(10, ("Could not find sambaSID attribute\n"));
2584 if (!string_to_sid(&sid
, str
)) {
2585 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2589 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2590 DEBUG(10, ("SID %s is not in expected domain %s\n",
2591 str
, sid_string_dbg(domain_sid
)));
2595 if (!sid_peek_rid(&sid
, rid
)) {
2596 DEBUG(10, ("Could not peek into RID\n"));
2603 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2604 TALLOC_CTX
*mem_ctx
,
2605 const DOM_SID
*group
,
2606 uint32
**pp_member_rids
,
2607 size_t *p_num_members
)
2609 struct ldapsam_privates
*ldap_state
=
2610 (struct ldapsam_privates
*)methods
->private_data
;
2611 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2612 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2613 const char *sid_attrs
[] = { "sambaSID", NULL
};
2614 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2615 LDAPMessage
*result
= NULL
;
2618 char **values
= NULL
;
2623 *pp_member_rids
= NULL
;
2626 filter
= talloc_asprintf(mem_ctx
,
2627 "(&(objectClass=%s)"
2630 LDAP_OBJ_POSIXGROUP
,
2632 sid_string_talloc(mem_ctx
, group
));
2633 if (filter
== NULL
) {
2634 ret
= NT_STATUS_NO_MEMORY
;
2638 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2639 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2642 if (rc
!= LDAP_SUCCESS
)
2645 talloc_autofree_ldapmsg(mem_ctx
, result
);
2647 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2650 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2651 sid_string_dbg(group
)));
2652 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2657 ret
= NT_STATUS_NO_SUCH_GROUP
;
2661 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2665 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2667 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2668 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2672 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2676 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2677 if (filter
== NULL
) {
2678 ret
= NT_STATUS_NO_MEMORY
;
2682 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2683 char *escape_memberuid
;
2685 escape_memberuid
= escape_ldap_string(talloc_tos(),
2687 if (escape_memberuid
== NULL
) {
2688 ret
= NT_STATUS_NO_MEMORY
;
2692 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2693 TALLOC_FREE(escape_memberuid
);
2694 if (filter
== NULL
) {
2695 ret
= NT_STATUS_NO_MEMORY
;
2700 filter
= talloc_asprintf_append_buffer(filter
, "))");
2701 if (filter
== NULL
) {
2702 ret
= NT_STATUS_NO_MEMORY
;
2706 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2707 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2710 if (rc
!= LDAP_SUCCESS
)
2713 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2714 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2716 talloc_autofree_ldapmsg(mem_ctx
, result
);
2718 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2720 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2726 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2730 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2731 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2732 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2736 if (!string_to_sid(&sid
, sidstr
))
2739 if (!sid_check_is_in_our_domain(&sid
)) {
2740 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2741 "in our domain\n"));
2742 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2746 sid_peek_rid(&sid
, &rid
);
2748 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2750 ret
= NT_STATUS_NO_MEMORY
;
2756 filter
= talloc_asprintf(mem_ctx
,
2757 "(&(objectClass=%s)"
2759 LDAP_OBJ_SAMBASAMACCOUNT
,
2762 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2763 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2766 if (rc
!= LDAP_SUCCESS
)
2769 talloc_autofree_ldapmsg(mem_ctx
, result
);
2771 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2773 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2777 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2779 get_global_sam_sid(),
2781 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2782 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2786 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2788 ret
= NT_STATUS_NO_MEMORY
;
2798 ldap_value_free(values
);
2803 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2804 TALLOC_CTX
*mem_ctx
,
2808 size_t *p_num_groups
)
2810 struct ldapsam_privates
*ldap_state
=
2811 (struct ldapsam_privates
*)methods
->private_data
;
2812 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2814 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2817 LDAPMessage
*result
= NULL
;
2819 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2820 size_t num_sids
, num_gids
;
2822 gid_t primary_gid
= -1;
2827 if (pdb_get_username(user
) == NULL
) {
2828 return NT_STATUS_INVALID_PARAMETER
;
2831 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2832 if (escape_name
== NULL
)
2833 return NT_STATUS_NO_MEMORY
;
2835 /* retrieve the users primary gid */
2836 filter
= talloc_asprintf(mem_ctx
,
2837 "(&(objectClass=%s)(uid=%s))",
2838 LDAP_OBJ_SAMBASAMACCOUNT
,
2840 if (filter
== NULL
) {
2841 ret
= NT_STATUS_NO_MEMORY
;
2845 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2846 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2848 if (rc
!= LDAP_SUCCESS
)
2851 talloc_autofree_ldapmsg(mem_ctx
, result
);
2853 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2857 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2858 ret
= NT_STATUS_NO_SUCH_USER
;
2861 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2863 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2865 DEBUG (1, ("Unable to find the member's gid!\n"));
2866 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2869 primary_gid
= strtoul(gidstr
, NULL
, 10);
2872 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2873 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2877 filter
= talloc_asprintf(mem_ctx
,
2878 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2879 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2880 if (filter
== NULL
) {
2881 ret
= NT_STATUS_NO_MEMORY
;
2885 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2886 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2888 if (rc
!= LDAP_SUCCESS
)
2891 talloc_autofree_ldapmsg(mem_ctx
, result
);
2899 /* We need to add the primary group as the first gid/sid */
2901 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2902 ret
= NT_STATUS_NO_MEMORY
;
2906 /* This sid will be replaced later */
2908 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2910 if (!NT_STATUS_IS_OK(ret
)) {
2914 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2916 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2923 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2925 str
, sizeof(str
)-1))
2928 if (!string_to_sid(&sid
, str
))
2931 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2933 str
, sizeof(str
)-1))
2936 gid
= strtoul(str
, &end
, 10);
2938 if (PTR_DIFF(end
, str
) != strlen(str
))
2941 if (gid
== primary_gid
) {
2942 sid_copy(&(*pp_sids
)[0], &sid
);
2944 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2946 ret
= NT_STATUS_NO_MEMORY
;
2949 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2951 if (!NT_STATUS_IS_OK(ret
)) {
2957 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2958 DEBUG(3, ("primary group of [%s] not found\n",
2959 pdb_get_username(user
)));
2963 *p_num_groups
= num_sids
;
2969 TALLOC_FREE(escape_name
);
2973 /**********************************************************************
2974 * Augment a posixGroup object with a sambaGroupMapping domgroup
2975 *********************************************************************/
2977 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2978 struct ldapsam_privates
*ldap_state
,
2981 const char *filter
, *dn
;
2982 LDAPMessage
*msg
, *entry
;
2986 filter
= talloc_asprintf(mem_ctx
,
2987 "(&(objectClass=%s)(gidNumber=%u))",
2988 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
2989 if (filter
== NULL
) {
2990 return NT_STATUS_NO_MEMORY
;
2993 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2994 get_attr_list(mem_ctx
, groupmap_attr_list
),
2996 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2998 if ((rc
!= LDAP_SUCCESS
) ||
2999 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3000 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3001 return NT_STATUS_NO_SUCH_GROUP
;
3004 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3006 return NT_STATUS_NO_MEMORY
;
3010 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3012 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3013 sid_string_talloc(mem_ctx
, &map
->sid
));
3014 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3015 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3016 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3018 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3020 talloc_autofree_ldapmod(mem_ctx
, mods
);
3022 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3023 if (rc
!= LDAP_SUCCESS
) {
3024 return NT_STATUS_ACCESS_DENIED
;
3027 return NT_STATUS_OK
;
3030 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3033 struct ldapsam_privates
*ldap_state
=
3034 (struct ldapsam_privates
*)methods
->private_data
;
3035 LDAPMessage
*msg
= NULL
;
3036 LDAPMod
**mods
= NULL
;
3037 const char *attrs
[] = { NULL
};
3041 TALLOC_CTX
*mem_ctx
;
3048 mem_ctx
= talloc_new(NULL
);
3049 if (mem_ctx
== NULL
) {
3050 DEBUG(0, ("talloc_new failed\n"));
3051 return NT_STATUS_NO_MEMORY
;
3054 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3055 sid_string_talloc(mem_ctx
, &map
->sid
));
3056 if (filter
== NULL
) {
3057 result
= NT_STATUS_NO_MEMORY
;
3061 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3062 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3063 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3065 if ((rc
== LDAP_SUCCESS
) &&
3066 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3068 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3069 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3070 result
= NT_STATUS_GROUP_EXISTS
;
3074 switch (map
->sid_name_use
) {
3076 case SID_NAME_DOM_GRP
:
3077 /* To map a domain group we need to have a posix group
3079 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3083 case SID_NAME_ALIAS
:
3084 if (!sid_check_is_in_our_domain(&map
->sid
)
3085 && !sid_check_is_in_builtin(&map
->sid
) )
3087 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3088 sid_string_dbg(&map
->sid
)));
3089 result
= NT_STATUS_INVALID_PARAMETER
;
3095 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3096 sid_type_lookup(map
->sid_name_use
)));
3097 result
= NT_STATUS_INVALID_PARAMETER
;
3101 /* Domain groups have been mapped in a separate routine, we have to
3102 * create an alias now */
3104 if (map
->gid
== -1) {
3105 DEBUG(10, ("Refusing to map gid==-1\n"));
3106 result
= NT_STATUS_INVALID_PARAMETER
;
3110 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3111 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3112 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3113 result
= NT_STATUS_GROUP_EXISTS
;
3117 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3118 * the best we can get out of LDAP. */
3120 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3121 sid_string_talloc(mem_ctx
, &map
->sid
),
3122 lp_ldap_group_suffix());
3124 result
= NT_STATUS_NO_MEMORY
;
3130 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3131 LDAP_OBJ_SID_ENTRY
);
3132 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3134 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3135 sid_string_talloc(mem_ctx
, &map
->sid
));
3136 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3137 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3138 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3140 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3142 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3143 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3144 talloc_autofree_ldapmod(mem_ctx
, mods
);
3146 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3148 result
= (rc
== LDAP_SUCCESS
) ?
3149 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3152 TALLOC_FREE(mem_ctx
);
3156 /**********************************************************************
3157 * Update a group mapping entry. We're quite strict about what can be changed:
3158 * Only the description and displayname may be changed. It simply does not
3159 * make any sense to change the SID, gid or the type in a mapping.
3160 *********************************************************************/
3162 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3165 struct ldapsam_privates
*ldap_state
=
3166 (struct ldapsam_privates
*)methods
->private_data
;
3168 const char *filter
, *dn
;
3169 LDAPMessage
*msg
= NULL
;
3170 LDAPMessage
*entry
= NULL
;
3171 LDAPMod
**mods
= NULL
;
3172 TALLOC_CTX
*mem_ctx
;
3175 mem_ctx
= talloc_new(NULL
);
3176 if (mem_ctx
== NULL
) {
3177 DEBUG(0, ("talloc_new failed\n"));
3178 return NT_STATUS_NO_MEMORY
;
3181 /* Make 100% sure that sid, gid and type are not changed by looking up
3182 * exactly the values we're given in LDAP. */
3184 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3185 "(sambaSid=%s)(gidNumber=%u)"
3186 "(sambaGroupType=%d))",
3188 sid_string_talloc(mem_ctx
, &map
->sid
),
3189 (unsigned int)map
->gid
, map
->sid_name_use
);
3190 if (filter
== NULL
) {
3191 result
= NT_STATUS_NO_MEMORY
;
3195 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3196 get_attr_list(mem_ctx
, groupmap_attr_list
),
3198 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3200 if ((rc
!= LDAP_SUCCESS
) ||
3201 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3202 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3203 result
= NT_STATUS_NO_SUCH_GROUP
;
3207 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3210 result
= NT_STATUS_NO_MEMORY
;
3215 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3217 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3219 talloc_autofree_ldapmod(mem_ctx
, mods
);
3222 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3223 "nothing to do\n"));
3224 result
= NT_STATUS_OK
;
3228 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3230 if (rc
!= LDAP_SUCCESS
) {
3231 result
= NT_STATUS_ACCESS_DENIED
;
3235 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3236 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3238 result
= NT_STATUS_OK
;
3241 TALLOC_FREE(mem_ctx
);
3245 /**********************************************************************
3246 *********************************************************************/
3248 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3251 struct ldapsam_privates
*priv
=
3252 (struct ldapsam_privates
*)methods
->private_data
;
3253 LDAPMessage
*msg
, *entry
;
3256 TALLOC_CTX
*mem_ctx
;
3259 mem_ctx
= talloc_new(NULL
);
3260 if (mem_ctx
== NULL
) {
3261 DEBUG(0, ("talloc_new failed\n"));
3262 return NT_STATUS_NO_MEMORY
;
3265 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3266 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3267 sid_string_talloc(mem_ctx
, &sid
));
3268 if (filter
== NULL
) {
3269 result
= NT_STATUS_NO_MEMORY
;
3272 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3273 get_attr_list(mem_ctx
, groupmap_attr_list
),
3275 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3277 if ((rc
!= LDAP_SUCCESS
) ||
3278 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3279 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3280 result
= NT_STATUS_NO_SUCH_GROUP
;
3284 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3285 get_attr_list(mem_ctx
,
3286 groupmap_attr_list_to_delete
));
3288 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3289 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3290 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3291 const char *attrs
[] = { "sambaGroupType", "description",
3292 "displayName", "sambaSIDList",
3295 /* Second try. Don't delete the sambaSID attribute, this is
3296 for "old" entries that are tacked on a winbind
3299 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3300 LDAP_OBJ_GROUPMAP
, attrs
);
3303 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3304 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3305 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3306 const char *attrs
[] = { "sambaGroupType", "description",
3307 "displayName", "sambaSIDList",
3308 "gidNumber", NULL
};
3310 /* Third try. This is a post-3.0.21 alias (containing only
3311 * sambaSidEntry and sambaGroupMapping classes), we also have
3312 * to delete the gidNumber attribute, only the sambaSidEntry
3315 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3316 LDAP_OBJ_GROUPMAP
, attrs
);
3319 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3322 TALLOC_FREE(mem_ctx
);
3326 /**********************************************************************
3327 *********************************************************************/
3329 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3332 struct ldapsam_privates
*ldap_state
=
3333 (struct ldapsam_privates
*)my_methods
->private_data
;
3334 char *filter
= NULL
;
3336 const char **attr_list
;
3338 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3340 return NT_STATUS_NO_MEMORY
;
3342 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3343 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3344 LDAP_SCOPE_SUBTREE
, filter
,
3345 attr_list
, 0, &ldap_state
->result
);
3346 TALLOC_FREE(attr_list
);
3348 if (rc
!= LDAP_SUCCESS
) {
3349 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3350 ldap_err2string(rc
)));
3351 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3352 lp_ldap_suffix(), filter
));
3353 ldap_msgfree(ldap_state
->result
);
3354 ldap_state
->result
= NULL
;
3355 TALLOC_FREE(filter
);
3356 return NT_STATUS_UNSUCCESSFUL
;
3359 TALLOC_FREE(filter
);
3361 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3362 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3363 ldap_state
->result
)));
3366 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3367 ldap_state
->result
);
3368 ldap_state
->index
= 0;
3370 return NT_STATUS_OK
;
3373 /**********************************************************************
3374 *********************************************************************/
3376 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3378 ldapsam_endsampwent(my_methods
);
3381 /**********************************************************************
3382 *********************************************************************/
3384 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3387 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3388 struct ldapsam_privates
*ldap_state
=
3389 (struct ldapsam_privates
*)my_methods
->private_data
;
3393 if (!ldap_state
->entry
)
3396 ldap_state
->index
++;
3397 bret
= init_group_from_ldap(ldap_state
, map
,
3401 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3405 return NT_STATUS_OK
;
3408 /**********************************************************************
3409 *********************************************************************/
3411 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3412 const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
,
3413 GROUP_MAP
**pp_rmap
,
3414 size_t *p_num_entries
,
3423 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3424 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3426 return NT_STATUS_ACCESS_DENIED
;
3429 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3430 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3431 sid_name_use
!= map
.sid_name_use
) {
3432 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3433 "not of the requested type\n", map
.nt_name
));
3436 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3437 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3438 "non mapped\n", map
.nt_name
));
3442 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3444 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3445 "enlarge group map!\n"));
3446 return NT_STATUS_UNSUCCESSFUL
;
3449 (*pp_rmap
)[entries
] = map
;
3454 ldapsam_endsamgrent(methods
);
3456 *p_num_entries
= entries
;
3458 return NT_STATUS_OK
;
3461 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3462 const DOM_SID
*alias
,
3463 const DOM_SID
*member
,
3466 struct ldapsam_privates
*ldap_state
=
3467 (struct ldapsam_privates
*)methods
->private_data
;
3469 LDAPMessage
*result
= NULL
;
3470 LDAPMessage
*entry
= NULL
;
3472 LDAPMod
**mods
= NULL
;
3474 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3477 char *filter
= NULL
;
3479 if (sid_check_is_in_builtin(alias
)) {
3480 type
= SID_NAME_ALIAS
;
3483 if (sid_check_is_in_our_domain(alias
)) {
3484 type
= SID_NAME_ALIAS
;
3487 if (type
== SID_NAME_USE_NONE
) {
3488 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3489 sid_string_dbg(alias
)));
3490 return NT_STATUS_NO_SUCH_ALIAS
;
3493 if (asprintf(&filter
,
3494 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3495 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3497 return NT_STATUS_NO_MEMORY
;
3500 if (ldapsam_search_one_group(ldap_state
, filter
,
3501 &result
) != LDAP_SUCCESS
) {
3503 return NT_STATUS_NO_SUCH_ALIAS
;
3506 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3510 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3511 ldap_msgfree(result
);
3513 return NT_STATUS_NO_SUCH_ALIAS
;
3517 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3518 "filter %s: count=%d\n", filter
, count
));
3519 ldap_msgfree(result
);
3521 return NT_STATUS_NO_SUCH_ALIAS
;
3526 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3530 ldap_msgfree(result
);
3531 return NT_STATUS_UNSUCCESSFUL
;
3534 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3536 ldap_msgfree(result
);
3537 return NT_STATUS_UNSUCCESSFUL
;
3540 smbldap_set_mod(&mods
, modop
,
3541 get_attr_key2string(groupmap_attr_list
,
3542 LDAP_ATTR_SID_LIST
),
3543 sid_to_fstring(tmp
, member
));
3545 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3547 ldap_mods_free(mods
, True
);
3548 ldap_msgfree(result
);
3551 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3552 return NT_STATUS_MEMBER_IN_ALIAS
;
3555 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3556 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3559 if (rc
!= LDAP_SUCCESS
) {
3560 return NT_STATUS_UNSUCCESSFUL
;
3563 return NT_STATUS_OK
;
3566 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3567 const DOM_SID
*alias
,
3568 const DOM_SID
*member
)
3570 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3573 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3574 const DOM_SID
*alias
,
3575 const DOM_SID
*member
)
3577 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3581 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3582 const DOM_SID
*alias
,
3583 TALLOC_CTX
*mem_ctx
,
3584 DOM_SID
**pp_members
,
3585 size_t *p_num_members
)
3587 struct ldapsam_privates
*ldap_state
=
3588 (struct ldapsam_privates
*)methods
->private_data
;
3589 LDAPMessage
*result
= NULL
;
3590 LDAPMessage
*entry
= NULL
;
3592 char **values
= NULL
;
3594 char *filter
= NULL
;
3595 size_t num_members
= 0;
3596 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3602 if (sid_check_is_in_builtin(alias
)) {
3603 type
= SID_NAME_ALIAS
;
3606 if (sid_check_is_in_our_domain(alias
)) {
3607 type
= SID_NAME_ALIAS
;
3610 if (type
== SID_NAME_USE_NONE
) {
3611 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3612 sid_string_dbg(alias
)));
3613 return NT_STATUS_NO_SUCH_ALIAS
;
3616 if (asprintf(&filter
,
3617 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3618 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3620 return NT_STATUS_NO_MEMORY
;
3623 if (ldapsam_search_one_group(ldap_state
, filter
,
3624 &result
) != LDAP_SUCCESS
) {
3626 return NT_STATUS_NO_SUCH_ALIAS
;
3629 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3633 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3634 ldap_msgfree(result
);
3636 return NT_STATUS_NO_SUCH_ALIAS
;
3640 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3641 "filter %s: count=%d\n", filter
, count
));
3642 ldap_msgfree(result
);
3644 return NT_STATUS_NO_SUCH_ALIAS
;
3649 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3653 ldap_msgfree(result
);
3654 return NT_STATUS_UNSUCCESSFUL
;
3657 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3659 get_attr_key2string(groupmap_attr_list
,
3660 LDAP_ATTR_SID_LIST
));
3662 if (values
== NULL
) {
3663 ldap_msgfree(result
);
3664 return NT_STATUS_OK
;
3667 count
= ldap_count_values(values
);
3669 for (i
=0; i
<count
; i
++) {
3673 if (!string_to_sid(&member
, values
[i
]))
3676 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3678 if (!NT_STATUS_IS_OK(status
)) {
3679 ldap_value_free(values
);
3680 ldap_msgfree(result
);
3685 *p_num_members
= num_members
;
3686 ldap_value_free(values
);
3687 ldap_msgfree(result
);
3689 return NT_STATUS_OK
;
3692 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3693 TALLOC_CTX
*mem_ctx
,
3694 const DOM_SID
*domain_sid
,
3695 const DOM_SID
*members
,
3697 uint32
**pp_alias_rids
,
3698 size_t *p_num_alias_rids
)
3700 struct ldapsam_privates
*ldap_state
=
3701 (struct ldapsam_privates
*)methods
->private_data
;
3704 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3706 LDAPMessage
*result
= NULL
;
3707 LDAPMessage
*entry
= NULL
;
3711 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3713 *pp_alias_rids
= NULL
;
3714 *p_num_alias_rids
= 0;
3716 if (sid_check_is_builtin(domain_sid
)) {
3717 type
= SID_NAME_ALIAS
;
3720 if (sid_check_is_domain(domain_sid
)) {
3721 type
= SID_NAME_ALIAS
;
3724 if (type
== SID_NAME_USE_NONE
) {
3725 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3726 sid_string_dbg(domain_sid
)));
3727 return NT_STATUS_UNSUCCESSFUL
;
3730 if (num_members
== 0) {
3731 return NT_STATUS_OK
;
3734 filter
= talloc_asprintf(mem_ctx
,
3735 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3736 LDAP_OBJ_GROUPMAP
, type
);
3738 for (i
=0; i
<num_members
; i
++)
3739 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3741 sid_string_talloc(mem_ctx
,
3744 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3746 if (filter
== NULL
) {
3747 return NT_STATUS_NO_MEMORY
;
3750 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3751 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3753 if (rc
!= LDAP_SUCCESS
)
3754 return NT_STATUS_UNSUCCESSFUL
;
3756 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3758 for (entry
= ldap_first_entry(ldap_struct
, result
);
3760 entry
= ldap_next_entry(ldap_struct
, entry
))
3766 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3772 if (!string_to_sid(&sid
, sid_str
))
3775 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3778 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3779 p_num_alias_rids
)) {
3780 ldap_msgfree(result
);
3781 return NT_STATUS_NO_MEMORY
;
3785 ldap_msgfree(result
);
3786 return NT_STATUS_OK
;
3789 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3790 enum pdb_policy_type type
,
3793 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3795 LDAPMod
**mods
= NULL
;
3796 fstring value_string
;
3797 const char *policy_attr
= NULL
;
3799 struct ldapsam_privates
*ldap_state
=
3800 (struct ldapsam_privates
*)methods
->private_data
;
3802 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3804 if (!ldap_state
->domain_dn
) {
3805 return NT_STATUS_INVALID_PARAMETER
;
3808 policy_attr
= get_account_policy_attr(type
);
3809 if (policy_attr
== NULL
) {
3810 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3815 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3817 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3819 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3822 ldap_mods_free(mods
, True
);
3824 if (rc
!= LDAP_SUCCESS
) {
3828 if (!cache_account_policy_set(type
, value
)) {
3829 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3830 "update local tdb cache\n"));
3834 return NT_STATUS_OK
;
3837 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3838 enum pdb_policy_type type
,
3841 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3845 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3846 enum pdb_policy_type type
,
3849 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3850 LDAPMessage
*result
= NULL
;
3851 LDAPMessage
*entry
= NULL
;
3855 const char *policy_attr
= NULL
;
3857 struct ldapsam_privates
*ldap_state
=
3858 (struct ldapsam_privates
*)methods
->private_data
;
3860 const char *attrs
[2];
3862 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3864 if (!ldap_state
->domain_dn
) {
3865 return NT_STATUS_INVALID_PARAMETER
;
3868 policy_attr
= get_account_policy_attr(type
);
3870 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3871 "policy index: %d\n", type
));
3875 attrs
[0] = policy_attr
;
3878 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3879 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0,
3882 if (rc
!= LDAP_SUCCESS
) {
3886 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3891 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3892 if (entry
== NULL
) {
3896 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3901 *value
= (uint32
)atol(vals
[0]);
3903 ntstatus
= NT_STATUS_OK
;
3907 ldap_value_free(vals
);
3908 ldap_msgfree(result
);
3913 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3915 - if user hasn't decided to use account policies inside LDAP just reuse the
3918 - if there is a valid cache entry, return that
3919 - if there is an LDAP entry, update cache and return
3920 - otherwise set to default, update cache and return
3924 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3925 enum pdb_policy_type type
,
3928 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3930 if (cache_account_policy_get(type
, value
)) {
3931 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3933 return NT_STATUS_OK
;
3936 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
3938 if (NT_STATUS_IS_OK(ntstatus
)) {
3942 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3946 /* should we automagically migrate old tdb value here ? */
3947 if (account_policy_get(type
, value
))
3950 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3951 "default\n", type
));
3954 if (!account_policy_get_default(type
, value
)) {
3960 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
3961 if (!NT_STATUS_IS_OK(ntstatus
)) {
3967 if (!cache_account_policy_set(type
, *value
)) {
3968 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3969 "tdb as a cache\n"));
3970 return NT_STATUS_UNSUCCESSFUL
;
3973 return NT_STATUS_OK
;
3976 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3977 const DOM_SID
*domain_sid
,
3981 enum lsa_SidType
*attrs
)
3983 struct ldapsam_privates
*ldap_state
=
3984 (struct ldapsam_privates
*)methods
->private_data
;
3985 LDAPMessage
*msg
= NULL
;
3987 char *allsids
= NULL
;
3988 int i
, rc
, num_mapped
;
3989 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
3990 TALLOC_CTX
*mem_ctx
;
3994 mem_ctx
= talloc_new(NULL
);
3995 if (mem_ctx
== NULL
) {
3996 DEBUG(0, ("talloc_new failed\n"));
4000 if (!sid_check_is_builtin(domain_sid
) &&
4001 !sid_check_is_domain(domain_sid
)) {
4002 result
= NT_STATUS_INVALID_PARAMETER
;
4006 if (num_rids
== 0) {
4007 result
= NT_STATUS_NONE_MAPPED
;
4011 for (i
=0; i
<num_rids
; i
++)
4012 attrs
[i
] = SID_NAME_UNKNOWN
;
4014 allsids
= talloc_strdup(mem_ctx
, "");
4015 if (allsids
== NULL
) {
4019 for (i
=0; i
<num_rids
; i
++) {
4021 sid_compose(&sid
, domain_sid
, rids
[i
]);
4022 allsids
= talloc_asprintf_append_buffer(
4023 allsids
, "(sambaSid=%s)",
4024 sid_string_talloc(mem_ctx
, &sid
));
4025 if (allsids
== NULL
) {
4030 /* First look for users */
4034 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4036 filter
= talloc_asprintf(
4037 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4038 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4040 if (filter
== NULL
) {
4044 rc
= smbldap_search(ldap_state
->smbldap_state
,
4045 lp_ldap_user_suffix(),
4046 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4048 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4051 if (rc
!= LDAP_SUCCESS
)
4054 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4057 for (entry
= ldap_first_entry(ld
, msg
);
4059 entry
= ldap_next_entry(ld
, entry
)) {
4064 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4066 DEBUG(2, ("Could not find sid from ldap entry\n"));
4070 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4073 DEBUG(2, ("Could not retrieve uid attribute\n"));
4077 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4078 if (rid
== rids
[rid_index
])
4082 if (rid_index
== num_rids
) {
4083 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4087 attrs
[rid_index
] = SID_NAME_USER
;
4088 names
[rid_index
] = name
;
4092 if (num_mapped
== num_rids
) {
4093 /* No need to look for groups anymore -- we're done */
4094 result
= NT_STATUS_OK
;
4098 /* Same game for groups */
4102 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4103 "sambaGroupType", NULL
};
4105 filter
= talloc_asprintf(
4106 mem_ctx
, "(&(objectClass=%s)(|%s))",
4107 LDAP_OBJ_GROUPMAP
, allsids
);
4108 if (filter
== NULL
) {
4112 rc
= smbldap_search(ldap_state
->smbldap_state
,
4114 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4116 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4119 if (rc
!= LDAP_SUCCESS
)
4122 /* ldap_struct might have changed due to a reconnect */
4124 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4126 /* For consistency checks, we already checked we're only domain or builtin */
4128 is_builtin
= sid_check_is_builtin(domain_sid
);
4130 for (entry
= ldap_first_entry(ld
, msg
);
4132 entry
= ldap_next_entry(ld
, entry
))
4137 enum lsa_SidType type
;
4138 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4140 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4143 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4148 type
= (enum lsa_SidType
)atol(attr
);
4150 /* Consistency checks */
4151 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4152 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4153 (type
!= SID_NAME_DOM_GRP
)))) {
4154 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4157 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4159 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4163 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4166 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4168 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4172 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4177 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4178 if (rid
== rids
[rid_index
])
4182 if (rid_index
== num_rids
) {
4183 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4187 attrs
[rid_index
] = type
;
4188 names
[rid_index
] = attr
;
4192 result
= NT_STATUS_NONE_MAPPED
;
4195 result
= (num_mapped
== num_rids
) ?
4196 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4198 TALLOC_FREE(mem_ctx
);
4202 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4204 char *filter
= NULL
;
4205 char *escaped
= NULL
;
4206 char *result
= NULL
;
4208 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4209 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4213 escaped
= escape_ldap_string(talloc_tos(), username
);
4214 if (escaped
== NULL
) goto done
;
4216 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4220 TALLOC_FREE(escaped
);
4225 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4229 const char **result
;
4231 va_start(ap
, mem_ctx
);
4232 while (va_arg(ap
, const char *) != NULL
)
4236 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4240 va_start(ap
, mem_ctx
);
4241 for (i
=0; i
<num
; i
++) {
4242 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4243 if (result
[i
] == NULL
) {
4244 talloc_free(result
);
4255 struct ldap_search_state
{
4256 struct smbldap_state
*connection
;
4266 void *pagedresults_cookie
;
4268 LDAPMessage
*entries
, *current_entry
;
4269 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4270 TALLOC_CTX
*mem_ctx
,
4271 LDAP
*ld
, LDAPMessage
*entry
,
4272 struct samr_displayentry
*result
);
4275 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4277 struct ldap_search_state
*state
=
4278 (struct ldap_search_state
*)search
->private_data
;
4280 int rc
= LDAP_OPERATIONS_ERROR
;
4282 state
->entries
= NULL
;
4284 if (state
->connection
->paged_results
) {
4285 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4286 state
->scope
, state
->filter
,
4287 state
->attrs
, state
->attrsonly
,
4288 lp_ldap_page_size(), &state
->entries
,
4289 &state
->pagedresults_cookie
);
4292 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4294 if (state
->entries
!= NULL
) {
4295 /* Left over from unsuccessful paged attempt */
4296 ldap_msgfree(state
->entries
);
4297 state
->entries
= NULL
;
4300 rc
= smbldap_search(state
->connection
, state
->base
,
4301 state
->scope
, state
->filter
, state
->attrs
,
4302 state
->attrsonly
, &state
->entries
);
4304 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4307 /* Ok, the server was lying. It told us it could do paged
4308 * searches when it could not. */
4309 state
->connection
->paged_results
= False
;
4312 ld
= state
->connection
->ldap_struct
;
4314 DEBUG(5, ("Don't have an LDAP connection right after a "
4318 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4320 if (state
->current_entry
== NULL
) {
4321 ldap_msgfree(state
->entries
);
4322 state
->entries
= NULL
;
4328 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4330 struct ldap_search_state
*state
=
4331 (struct ldap_search_state
*)search
->private_data
;
4334 if (!state
->connection
->paged_results
) {
4335 /* There is no next page when there are no paged results */
4339 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4340 state
->scope
, state
->filter
, state
->attrs
,
4341 state
->attrsonly
, lp_ldap_page_size(),
4343 &state
->pagedresults_cookie
);
4345 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4348 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4350 if (state
->current_entry
== NULL
) {
4351 ldap_msgfree(state
->entries
);
4352 state
->entries
= NULL
;
4358 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4359 struct samr_displayentry
*entry
)
4361 struct ldap_search_state
*state
=
4362 (struct ldap_search_state
*)search
->private_data
;
4366 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4369 if ((state
->entries
== NULL
) &&
4370 !ldapsam_search_nextpage(search
))
4373 result
= state
->ldap2displayentry(state
, search
,
4374 state
->connection
->ldap_struct
,
4375 state
->current_entry
, entry
);
4379 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4380 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4381 if (dn
!= NULL
) ldap_memfree(dn
);
4384 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4386 if (state
->current_entry
== NULL
) {
4387 ldap_msgfree(state
->entries
);
4388 state
->entries
= NULL
;
4391 if (!result
) goto retry
;
4396 static void ldapsam_search_end(struct pdb_search
*search
)
4398 struct ldap_search_state
*state
=
4399 (struct ldap_search_state
*)search
->private_data
;
4402 if (state
->pagedresults_cookie
== NULL
)
4405 if (state
->entries
!= NULL
)
4406 ldap_msgfree(state
->entries
);
4408 state
->entries
= NULL
;
4409 state
->current_entry
= NULL
;
4411 if (!state
->connection
->paged_results
)
4414 /* Tell the LDAP server we're not interested in the rest anymore. */
4416 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4417 state
->filter
, state
->attrs
,
4418 state
->attrsonly
, 0, &state
->entries
,
4419 &state
->pagedresults_cookie
);
4421 if (rc
!= LDAP_SUCCESS
)
4422 DEBUG(5, ("Could not end search properly\n"));
4427 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4428 TALLOC_CTX
*mem_ctx
,
4429 LDAP
*ld
, LDAPMessage
*entry
,
4430 struct samr_displayentry
*result
)
4433 size_t converted_size
;
4437 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4438 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4439 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4442 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4443 ldap_value_free(vals
);
4445 if ((state
->acct_flags
!= 0) &&
4446 ((state
->acct_flags
& acct_flags
) == 0))
4449 result
->acct_flags
= acct_flags
;
4450 result
->account_name
= "";
4451 result
->fullname
= "";
4452 result
->description
= "";
4454 vals
= ldap_get_values(ld
, entry
, "uid");
4455 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4456 DEBUG(5, ("\"uid\" not found\n"));
4459 if (!pull_utf8_talloc(mem_ctx
,
4460 CONST_DISCARD(char **, &result
->account_name
),
4461 vals
[0], &converted_size
))
4463 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4467 ldap_value_free(vals
);
4469 vals
= ldap_get_values(ld
, entry
, "displayName");
4470 if ((vals
== NULL
) || (vals
[0] == NULL
))
4471 DEBUG(8, ("\"displayName\" not found\n"));
4472 else if (!pull_utf8_talloc(mem_ctx
,
4473 CONST_DISCARD(char **, &result
->fullname
),
4474 vals
[0], &converted_size
))
4476 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4480 ldap_value_free(vals
);
4482 vals
= ldap_get_values(ld
, entry
, "description");
4483 if ((vals
== NULL
) || (vals
[0] == NULL
))
4484 DEBUG(8, ("\"description\" not found\n"));
4485 else if (!pull_utf8_talloc(mem_ctx
,
4486 CONST_DISCARD(char **, &result
->description
),
4487 vals
[0], &converted_size
))
4489 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4493 ldap_value_free(vals
);
4495 if ((result
->account_name
== NULL
) ||
4496 (result
->fullname
== NULL
) ||
4497 (result
->description
== NULL
)) {
4498 DEBUG(0, ("talloc failed\n"));
4502 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4503 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4504 DEBUG(0, ("\"objectSid\" not found\n"));
4508 if (!string_to_sid(&sid
, vals
[0])) {
4509 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4510 ldap_value_free(vals
);
4513 ldap_value_free(vals
);
4515 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4516 DEBUG(0, ("sid %s does not belong to our domain\n",
4517 sid_string_dbg(&sid
)));
4525 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4526 struct pdb_search
*search
,
4529 struct ldapsam_privates
*ldap_state
=
4530 (struct ldapsam_privates
*)methods
->private_data
;
4531 struct ldap_search_state
*state
;
4533 state
= talloc(search
, struct ldap_search_state
);
4534 if (state
== NULL
) {
4535 DEBUG(0, ("talloc failed\n"));
4539 state
->connection
= ldap_state
->smbldap_state
;
4541 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4542 state
->base
= lp_ldap_user_suffix();
4543 else if ((acct_flags
!= 0) &&
4544 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4545 state
->base
= lp_ldap_machine_suffix();
4547 state
->base
= lp_ldap_suffix();
4549 state
->acct_flags
= acct_flags
;
4550 state
->base
= talloc_strdup(search
, state
->base
);
4551 state
->scope
= LDAP_SCOPE_SUBTREE
;
4552 state
->filter
= get_ldap_filter(search
, "*");
4553 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4554 "displayName", "description",
4555 "sambaAcctFlags", NULL
);
4556 state
->attrsonly
= 0;
4557 state
->pagedresults_cookie
= NULL
;
4558 state
->entries
= NULL
;
4559 state
->ldap2displayentry
= ldapuser2displayentry
;
4561 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4562 DEBUG(0, ("talloc failed\n"));
4566 search
->private_data
= state
;
4567 search
->next_entry
= ldapsam_search_next_entry
;
4568 search
->search_end
= ldapsam_search_end
;
4570 return ldapsam_search_firstpage(search
);
4573 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4574 TALLOC_CTX
*mem_ctx
,
4575 LDAP
*ld
, LDAPMessage
*entry
,
4576 struct samr_displayentry
*result
)
4579 size_t converted_size
;
4583 result
->account_name
= "";
4584 result
->fullname
= "";
4585 result
->description
= "";
4588 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4589 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4590 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4592 ldap_value_free(vals
);
4597 group_type
= atoi(vals
[0]);
4599 if ((state
->group_type
!= 0) &&
4600 ((state
->group_type
!= group_type
))) {
4601 ldap_value_free(vals
);
4605 ldap_value_free(vals
);
4607 /* display name is the NT group name */
4609 vals
= ldap_get_values(ld
, entry
, "displayName");
4610 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4611 DEBUG(8, ("\"displayName\" not found\n"));
4613 /* fallback to the 'cn' attribute */
4614 vals
= ldap_get_values(ld
, entry
, "cn");
4615 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4616 DEBUG(5, ("\"cn\" not found\n"));
4619 if (!pull_utf8_talloc(mem_ctx
,
4620 CONST_DISCARD(char **,
4621 &result
->account_name
),
4622 vals
[0], &converted_size
))
4624 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4625 "failed: %s", strerror(errno
)));
4628 else if (!pull_utf8_talloc(mem_ctx
,
4629 CONST_DISCARD(char **,
4630 &result
->account_name
),
4631 vals
[0], &converted_size
))
4633 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4637 ldap_value_free(vals
);
4639 vals
= ldap_get_values(ld
, entry
, "description");
4640 if ((vals
== NULL
) || (vals
[0] == NULL
))
4641 DEBUG(8, ("\"description\" not found\n"));
4642 else if (!pull_utf8_talloc(mem_ctx
,
4643 CONST_DISCARD(char **, &result
->description
),
4644 vals
[0], &converted_size
))
4646 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4649 ldap_value_free(vals
);
4651 if ((result
->account_name
== NULL
) ||
4652 (result
->fullname
== NULL
) ||
4653 (result
->description
== NULL
)) {
4654 DEBUG(0, ("talloc failed\n"));
4658 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4659 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4660 DEBUG(0, ("\"objectSid\" not found\n"));
4662 ldap_value_free(vals
);
4667 if (!string_to_sid(&sid
, vals
[0])) {
4668 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4672 ldap_value_free(vals
);
4674 switch (group_type
) {
4675 case SID_NAME_DOM_GRP
:
4676 case SID_NAME_ALIAS
:
4678 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4679 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4681 DEBUG(0, ("%s is not in our domain\n",
4682 sid_string_dbg(&sid
)));
4688 DEBUG(0,("unkown group type: %d\n", group_type
));
4692 result
->acct_flags
= 0;
4697 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4698 struct pdb_search
*search
,
4700 enum lsa_SidType type
)
4702 struct ldapsam_privates
*ldap_state
=
4703 (struct ldapsam_privates
*)methods
->private_data
;
4704 struct ldap_search_state
*state
;
4707 state
= talloc(search
, struct ldap_search_state
);
4708 if (state
== NULL
) {
4709 DEBUG(0, ("talloc failed\n"));
4713 state
->connection
= ldap_state
->smbldap_state
;
4715 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4716 state
->connection
= ldap_state
->smbldap_state
;
4717 state
->scope
= LDAP_SCOPE_SUBTREE
;
4718 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4719 "(sambaGroupType=%d)(sambaSID=%s*))",
4721 type
, sid_to_fstring(tmp
, sid
));
4722 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4723 "displayName", "description",
4724 "sambaGroupType", NULL
);
4725 state
->attrsonly
= 0;
4726 state
->pagedresults_cookie
= NULL
;
4727 state
->entries
= NULL
;
4728 state
->group_type
= type
;
4729 state
->ldap2displayentry
= ldapgroup2displayentry
;
4731 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4732 DEBUG(0, ("talloc failed\n"));
4736 search
->private_data
= state
;
4737 search
->next_entry
= ldapsam_search_next_entry
;
4738 search
->search_end
= ldapsam_search_end
;
4740 return ldapsam_search_firstpage(search
);
4743 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4744 struct pdb_search
*search
)
4746 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4749 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4750 struct pdb_search
*search
,
4753 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4756 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4758 return PDB_CAP_STORE_RIDS
;
4761 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4764 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4766 LDAPMessage
*result
= NULL
;
4767 LDAPMessage
*entry
= NULL
;
4768 LDAPMod
**mods
= NULL
;
4775 TALLOC_CTX
*mem_ctx
;
4777 mem_ctx
= talloc_new(NULL
);
4778 if (mem_ctx
== NULL
) {
4779 DEBUG(0, ("talloc_new failed\n"));
4780 return NT_STATUS_NO_MEMORY
;
4783 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4784 get_global_sam_name(), False
);
4785 if (!NT_STATUS_IS_OK(status
)) {
4786 DEBUG(3, ("Could not get domain info: %s\n",
4787 nt_errstr(status
)));
4791 talloc_autofree_ldapmsg(mem_ctx
, result
);
4793 entry
= ldap_first_entry(priv2ld(priv
), result
);
4794 if (entry
== NULL
) {
4795 DEBUG(0, ("Could not get domain info entry\n"));
4796 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4800 /* Find the largest of the three attributes "sambaNextRid",
4801 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4802 concept of differentiating between user and group rids, and will
4803 use only "sambaNextRid" in the future. But for compatibility
4804 reasons I look if others have chosen different strategies -- VL */
4806 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4807 "sambaNextRid", mem_ctx
);
4808 if (value
!= NULL
) {
4809 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4810 nextRid
= MAX(nextRid
, tmp
);
4813 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4814 "sambaNextUserRid", mem_ctx
);
4815 if (value
!= NULL
) {
4816 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4817 nextRid
= MAX(nextRid
, tmp
);
4820 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4821 "sambaNextGroupRid", mem_ctx
);
4822 if (value
!= NULL
) {
4823 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4824 nextRid
= MAX(nextRid
, tmp
);
4828 nextRid
= BASE_RID
-1;
4833 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4834 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4835 talloc_autofree_ldapmod(mem_ctx
, mods
);
4837 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4838 status
= NT_STATUS_NO_MEMORY
;
4842 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4844 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4847 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4850 if (NT_STATUS_IS_OK(status
)) {
4854 TALLOC_FREE(mem_ctx
);
4858 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32
*rid
)
4862 for (i
=0; i
<10; i
++) {
4863 NTSTATUS result
= ldapsam_get_new_rid(
4864 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4865 if (NT_STATUS_IS_OK(result
)) {
4869 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4873 /* The ldap update failed (maybe a race condition), retry */
4876 /* Tried 10 times, fail. */
4877 return NT_STATUS_ACCESS_DENIED
;
4880 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
4882 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4883 return NT_STATUS_IS_OK(result
) ? True
: False
;
4886 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4888 union unid_t
*id
, enum lsa_SidType
*type
)
4890 struct ldapsam_privates
*priv
=
4891 (struct ldapsam_privates
*)methods
->private_data
;
4893 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4895 LDAPMessage
*result
= NULL
;
4896 LDAPMessage
*entry
= NULL
;
4901 TALLOC_CTX
*mem_ctx
;
4903 mem_ctx
= talloc_new(NULL
);
4904 if (mem_ctx
== NULL
) {
4905 DEBUG(0, ("talloc_new failed\n"));
4909 filter
= talloc_asprintf(mem_ctx
,
4911 "(|(objectClass=%s)(objectClass=%s)))",
4912 sid_string_talloc(mem_ctx
, sid
),
4913 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4914 if (filter
== NULL
) {
4915 DEBUG(5, ("talloc_asprintf failed\n"));
4919 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4921 if (rc
!= LDAP_SUCCESS
) {
4924 talloc_autofree_ldapmsg(mem_ctx
, result
);
4926 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4927 DEBUG(10, ("Got %d entries, expected one\n",
4928 ldap_count_entries(priv2ld(priv
), result
)));
4932 entry
= ldap_first_entry(priv2ld(priv
), result
);
4934 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4935 "sambaGroupType", mem_ctx
);
4937 if (value
!= NULL
) {
4938 const char *gid_str
;
4941 gid_str
= smbldap_talloc_single_attribute(
4942 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4943 if (gid_str
== NULL
) {
4944 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4945 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4950 id
->gid
= strtoul(gid_str
, NULL
, 10);
4951 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
4956 /* It must be a user */
4958 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4959 "uidNumber", mem_ctx
);
4960 if (value
== NULL
) {
4961 DEBUG(1, ("Could not find uidNumber in %s\n",
4962 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4966 id
->uid
= strtoul(value
, NULL
, 10);
4967 *type
= SID_NAME_USER
;
4971 TALLOC_FREE(mem_ctx
);
4976 * The following functions is called only if
4977 * ldapsam:trusted and ldapsam:editposix are
4982 * ldapsam_create_user creates a new
4983 * posixAccount and sambaSamAccount object
4984 * in the ldap users subtree
4986 * The uid is allocated by winbindd.
4989 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
4990 TALLOC_CTX
*tmp_ctx
, const char *name
,
4991 uint32 acb_info
, uint32
*rid
)
4993 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4994 LDAPMessage
*entry
= NULL
;
4995 LDAPMessage
*result
= NULL
;
4997 bool is_machine
= False
;
4998 bool add_posix
= False
;
4999 LDAPMod
**mods
= NULL
;
5007 const char *dn
= NULL
;
5015 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5016 acb_info
& ACB_WSTRUST
||
5017 acb_info
& ACB_SVRTRUST
||
5018 acb_info
& ACB_DOMTRUST
) {
5022 username
= escape_ldap_string(talloc_tos(), name
);
5023 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5024 username
, LDAP_OBJ_POSIXACCOUNT
);
5025 TALLOC_FREE(username
);
5027 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5028 if (rc
!= LDAP_SUCCESS
) {
5029 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5030 return NT_STATUS_ACCESS_DENIED
;
5032 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5034 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5036 if (num_result
> 1) {
5037 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5038 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5041 if (num_result
== 1) {
5043 /* check if it is just a posix account.
5044 * or if there is a sid attached to this entry
5047 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5049 return NT_STATUS_UNSUCCESSFUL
;
5052 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5054 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5055 return NT_STATUS_USER_EXISTS
;
5058 /* it is just a posix account, retrieve the dn for later use */
5059 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5061 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5062 return NT_STATUS_NO_MEMORY
;
5066 if (num_result
== 0) {
5070 /* Create the basic samu structure and generate the mods for the ldap commit */
5071 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5072 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5076 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5078 user
= samu_new(tmp_ctx
);
5080 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5081 return NT_STATUS_NO_MEMORY
;
5084 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5085 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5086 return NT_STATUS_UNSUCCESSFUL
;
5088 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5089 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5090 return NT_STATUS_UNSUCCESSFUL
;
5093 if (acb_info
& ACB_NORMAL
) {
5094 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5095 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5096 return NT_STATUS_UNSUCCESSFUL
;
5099 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5100 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5101 return NT_STATUS_UNSUCCESSFUL
;
5105 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5106 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5107 return NT_STATUS_UNSUCCESSFUL
;
5111 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5112 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5113 return NT_STATUS_UNSUCCESSFUL
;
5116 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5117 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5118 return NT_STATUS_UNSUCCESSFUL
;
5121 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5122 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5124 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5129 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5131 /* retrieve the Domain Users group gid */
5132 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
5133 !sid_to_gid(&group_sid
, &gid
)) {
5134 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5135 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5138 /* lets allocate a new userid for this user */
5139 if (!winbind_allocate_uid(&uid
)) {
5140 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5141 return NT_STATUS_UNSUCCESSFUL
;
5146 /* TODO: choose a more appropriate default for machines */
5147 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5148 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5150 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5151 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5153 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5154 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5156 escape_name
= escape_rdn_val_string_alloc(name
);
5158 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5159 return NT_STATUS_NO_MEMORY
;
5163 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5165 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5168 SAFE_FREE(escape_name
);
5170 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5171 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5172 return NT_STATUS_NO_MEMORY
;
5175 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5176 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5177 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5178 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5179 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5180 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5181 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5184 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5187 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5189 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5192 if (rc
!= LDAP_SUCCESS
) {
5193 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5194 return NT_STATUS_UNSUCCESSFUL
;
5197 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5199 flush_pwnam_cache();
5201 return NT_STATUS_OK
;
5204 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5206 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5207 LDAPMessage
*result
= NULL
;
5208 LDAPMessage
*entry
= NULL
;
5214 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5216 filter
= talloc_asprintf(tmp_ctx
,
5219 "(objectClass=%s))",
5220 pdb_get_username(sam_acct
),
5221 LDAP_OBJ_POSIXACCOUNT
,
5222 LDAP_OBJ_SAMBASAMACCOUNT
);
5223 if (filter
== NULL
) {
5224 return NT_STATUS_NO_MEMORY
;
5227 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5228 if (rc
!= LDAP_SUCCESS
) {
5229 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5230 return NT_STATUS_UNSUCCESSFUL
;
5232 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5234 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5236 if (num_result
== 0) {
5237 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5238 return NT_STATUS_NO_SUCH_USER
;
5241 if (num_result
> 1) {
5242 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5243 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5246 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5248 return NT_STATUS_UNSUCCESSFUL
;
5251 /* it is just a posix account, retrieve the dn for later use */
5252 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5254 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5255 return NT_STATUS_NO_MEMORY
;
5258 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5259 if (rc
!= LDAP_SUCCESS
) {
5260 return NT_STATUS_UNSUCCESSFUL
;
5263 flush_pwnam_cache();
5265 return NT_STATUS_OK
;
5269 * ldapsam_create_group creates a new
5270 * posixGroup and sambaGroupMapping object
5271 * in the ldap groups subtree
5273 * The gid is allocated by winbindd.
5276 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5277 TALLOC_CTX
*tmp_ctx
,
5281 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5283 LDAPMessage
*entry
= NULL
;
5284 LDAPMessage
*result
= NULL
;
5286 bool is_new_entry
= False
;
5287 LDAPMod
**mods
= NULL
;
5293 const char *dn
= NULL
;
5298 groupname
= escape_ldap_string(talloc_tos(), name
);
5299 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5300 groupname
, LDAP_OBJ_POSIXGROUP
);
5301 TALLOC_FREE(groupname
);
5303 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5304 if (rc
!= LDAP_SUCCESS
) {
5305 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5306 return NT_STATUS_UNSUCCESSFUL
;
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_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5314 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5317 if (num_result
== 1) {
5319 /* check if it is just a posix group.
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_group: The group [%s] already exist!\n", name
));
5331 return NT_STATUS_GROUP_EXISTS
;
5334 /* it is just a posix group, retrieve the gid and the dn for later use */
5335 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5337 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5338 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5341 gid
= strtoul(tmp
, NULL
, 10);
5343 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5345 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5346 return NT_STATUS_NO_MEMORY
;
5350 if (num_result
== 0) {
5353 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5355 is_new_entry
= True
;
5357 /* lets allocate a new groupid for this group */
5358 if (!winbind_allocate_gid(&gid
)) {
5359 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5360 return NT_STATUS_UNSUCCESSFUL
;
5363 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5365 escape_name
= escape_rdn_val_string_alloc(name
);
5367 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5368 return NT_STATUS_NO_MEMORY
;
5371 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5373 SAFE_FREE(escape_name
);
5375 if (!gidstr
|| !dn
) {
5376 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5377 return NT_STATUS_NO_MEMORY
;
5380 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5381 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5382 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5385 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5386 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5390 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5392 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5394 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5396 if (!groupsidstr
|| !grouptype
) {
5397 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5398 return NT_STATUS_NO_MEMORY
;
5401 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5402 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5403 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5404 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5405 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5408 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5410 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5411 /* This call may fail with rfc2307bis schema */
5412 /* Retry adding a structural class */
5413 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5414 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5418 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5421 if (rc
!= LDAP_SUCCESS
) {
5422 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5423 return NT_STATUS_UNSUCCESSFUL
;
5426 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5428 return NT_STATUS_OK
;
5431 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
5433 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5434 LDAPMessage
*result
= NULL
;
5435 LDAPMessage
*entry
= NULL
;
5443 /* get the group sid */
5444 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5446 filter
= talloc_asprintf(tmp_ctx
,
5449 "(objectClass=%s))",
5450 sid_string_talloc(tmp_ctx
, &group_sid
),
5451 LDAP_OBJ_POSIXGROUP
,
5453 if (filter
== NULL
) {
5454 return NT_STATUS_NO_MEMORY
;
5457 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5458 if (rc
!= LDAP_SUCCESS
) {
5459 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5460 return NT_STATUS_UNSUCCESSFUL
;
5462 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5464 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5466 if (num_result
== 0) {
5467 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5468 return NT_STATUS_NO_SUCH_GROUP
;
5471 if (num_result
> 1) {
5472 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5473 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5476 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5478 return NT_STATUS_UNSUCCESSFUL
;
5481 /* here it is, retrieve the dn for later use */
5482 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5484 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5485 return NT_STATUS_NO_MEMORY
;
5488 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5490 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5491 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5494 /* check no user have this group marked as primary group */
5495 filter
= talloc_asprintf(tmp_ctx
,
5498 "(objectClass=%s))",
5500 LDAP_OBJ_POSIXACCOUNT
,
5501 LDAP_OBJ_SAMBASAMACCOUNT
);
5503 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5504 if (rc
!= LDAP_SUCCESS
) {
5505 DEBUG(1,("ldapsam_delete_dom_group: accounts 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(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5514 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5517 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5518 if (rc
!= LDAP_SUCCESS
) {
5519 return NT_STATUS_UNSUCCESSFUL
;
5522 return NT_STATUS_OK
;
5525 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5526 TALLOC_CTX
*tmp_ctx
,
5531 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5532 LDAPMessage
*entry
= NULL
;
5533 LDAPMessage
*result
= NULL
;
5535 LDAPMod
**mods
= NULL
;
5538 const char *dn
= NULL
;
5545 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5547 case LDAP_MOD_DELETE
:
5548 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5551 return NT_STATUS_UNSUCCESSFUL
;
5554 /* get member sid */
5555 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5557 /* get the group sid */
5558 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5560 filter
= talloc_asprintf(tmp_ctx
,
5563 "(objectClass=%s))",
5564 sid_string_talloc(tmp_ctx
, &member_sid
),
5565 LDAP_OBJ_POSIXACCOUNT
,
5566 LDAP_OBJ_SAMBASAMACCOUNT
);
5567 if (filter
== NULL
) {
5568 return NT_STATUS_NO_MEMORY
;
5571 /* get the member uid */
5572 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5573 if (rc
!= LDAP_SUCCESS
) {
5574 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5575 return NT_STATUS_UNSUCCESSFUL
;
5577 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5579 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5581 if (num_result
== 0) {
5582 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5583 return NT_STATUS_NO_SUCH_MEMBER
;
5586 if (num_result
> 1) {
5587 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5588 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5591 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5593 return NT_STATUS_UNSUCCESSFUL
;
5596 if (modop
== LDAP_MOD_DELETE
) {
5597 /* check if we are trying to remove the member from his primary group */
5599 gid_t user_gid
, group_gid
;
5601 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5603 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5604 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5607 user_gid
= strtoul(gidstr
, NULL
, 10);
5609 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5610 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5611 return NT_STATUS_UNSUCCESSFUL
;
5614 if (user_gid
== group_gid
) {
5615 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5616 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5620 /* here it is, retrieve the uid for later use */
5621 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5623 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5624 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5627 filter
= talloc_asprintf(tmp_ctx
,
5630 "(objectClass=%s))",
5631 sid_string_talloc(tmp_ctx
, &group_sid
),
5632 LDAP_OBJ_POSIXGROUP
,
5636 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5637 if (rc
!= LDAP_SUCCESS
) {
5638 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5639 return NT_STATUS_UNSUCCESSFUL
;
5641 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5643 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5645 if (num_result
== 0) {
5646 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5647 return NT_STATUS_NO_SUCH_GROUP
;
5650 if (num_result
> 1) {
5651 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5652 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5655 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5657 return NT_STATUS_UNSUCCESSFUL
;
5660 /* here it is, retrieve the dn for later use */
5661 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5663 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5664 return NT_STATUS_NO_MEMORY
;
5667 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5669 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5671 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5672 if (rc
!= LDAP_SUCCESS
) {
5673 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5674 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5675 return NT_STATUS_MEMBER_IN_GROUP
;
5677 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5678 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5679 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5681 return NT_STATUS_UNSUCCESSFUL
;
5684 return NT_STATUS_OK
;
5687 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5688 TALLOC_CTX
*tmp_ctx
,
5692 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5694 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5695 TALLOC_CTX
*tmp_ctx
,
5699 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5702 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5703 TALLOC_CTX
*mem_ctx
,
5704 struct samu
*sampass
)
5706 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5707 LDAPMessage
*entry
= NULL
;
5708 LDAPMessage
*result
= NULL
;
5710 LDAPMod
**mods
= NULL
;
5712 char *escape_username
;
5714 const char *dn
= NULL
;
5718 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5720 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5721 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5722 return NT_STATUS_UNSUCCESSFUL
;
5724 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
5726 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5727 return NT_STATUS_NO_MEMORY
;
5730 escape_username
= escape_ldap_string(talloc_tos(),
5731 pdb_get_username(sampass
));
5732 if (escape_username
== NULL
) {
5733 return NT_STATUS_NO_MEMORY
;
5736 filter
= talloc_asprintf(mem_ctx
,
5739 "(objectClass=%s))",
5741 LDAP_OBJ_POSIXACCOUNT
,
5742 LDAP_OBJ_SAMBASAMACCOUNT
);
5744 TALLOC_FREE(escape_username
);
5746 if (filter
== NULL
) {
5747 return NT_STATUS_NO_MEMORY
;
5750 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5751 if (rc
!= LDAP_SUCCESS
) {
5752 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5753 return NT_STATUS_UNSUCCESSFUL
;
5755 talloc_autofree_ldapmsg(mem_ctx
, result
);
5757 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5759 if (num_result
== 0) {
5760 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5761 return NT_STATUS_NO_SUCH_USER
;
5764 if (num_result
> 1) {
5765 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5766 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5769 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5771 return NT_STATUS_UNSUCCESSFUL
;
5774 /* retrieve the dn for later use */
5775 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5777 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5778 return NT_STATUS_NO_MEMORY
;
5781 /* remove the old one, and add the new one, this way we do not risk races */
5782 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5785 return NT_STATUS_OK
;
5788 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5790 if (rc
!= LDAP_SUCCESS
) {
5791 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5792 pdb_get_username(sampass
), gidstr
));
5793 return NT_STATUS_UNSUCCESSFUL
;
5796 flush_pwnam_cache();
5798 return NT_STATUS_OK
;
5802 /**********************************************************************
5803 trusted domains functions
5804 *********************************************************************/
5806 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
5809 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
5810 ldap_state
->domain_dn
);
5813 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
5814 TALLOC_CTX
*mem_ctx
,
5815 const char *domain
, LDAPMessage
**entry
)
5819 int scope
= LDAP_SCOPE_SUBTREE
;
5820 const char **attrs
= NULL
; /* NULL: get all attrs */
5821 int attrsonly
= 0; /* 0: return values too */
5822 LDAPMessage
*result
= NULL
;
5826 filter
= talloc_asprintf(talloc_tos(),
5827 "(&(objectClass=%s)(sambaDomainName=%s))",
5828 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
5830 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5831 if (trusted_dn
== NULL
) {
5834 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
5835 filter
, attrs
, attrsonly
, &result
);
5837 if (result
!= NULL
) {
5838 talloc_autofree_ldapmsg(mem_ctx
, result
);
5841 if (rc
== LDAP_NO_SUCH_OBJECT
) {
5846 if (rc
!= LDAP_SUCCESS
) {
5850 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5852 if (num_result
> 1) {
5853 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5854 "%s object for domain '%s'?!\n",
5855 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
5859 if (num_result
== 0) {
5860 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5861 "%s object for domain %s.\n",
5862 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
5865 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5871 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
5875 time_t *pass_last_set_time
)
5877 struct ldapsam_privates
*ldap_state
=
5878 (struct ldapsam_privates
*)methods
->private_data
;
5879 LDAPMessage
*entry
= NULL
;
5881 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
5883 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
5892 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5893 entry
, "sambaClearTextPassword", talloc_tos());
5894 if (pwd_str
== NULL
) {
5897 /* trusteddom_pw routines do not use talloc yet... */
5898 *pwd
= SMB_STRDUP(pwd_str
);
5904 /* last change time */
5905 if (pass_last_set_time
!= NULL
) {
5907 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5908 entry
, "sambaPwdLastSet", talloc_tos());
5909 if (time_str
== NULL
) {
5912 *pass_last_set_time
= (time_t)atol(time_str
);
5919 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5922 if (sid_str
== NULL
) {
5925 dom_sid
= string_sid_talloc(talloc_tos(), sid_str
);
5926 if (dom_sid
== NULL
) {
5929 sid_copy(sid
, dom_sid
);
5935 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
5940 struct ldapsam_privates
*ldap_state
=
5941 (struct ldapsam_privates
*)methods
->private_data
;
5942 LDAPMessage
*entry
= NULL
;
5943 LDAPMod
**mods
= NULL
;
5944 char *prev_pwd
= NULL
;
5945 char *trusted_dn
= NULL
;
5948 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
5951 * get the current entry (if there is one) in order to put the
5952 * current password into the previous password attribute
5954 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
5959 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
5960 LDAP_OBJ_TRUSTDOM_PASSWORD
);
5961 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
5963 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
5964 sid_string_tos(sid
));
5965 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
5966 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
5967 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5968 "sambaClearTextPassword", pwd
);
5970 talloc_autofree_ldapmod(talloc_tos(), mods
);
5972 if (entry
!= NULL
) {
5973 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5974 entry
, "sambaClearTextPassword", talloc_tos());
5975 if (prev_pwd
!= NULL
) {
5976 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5977 "sambaPreviousClearTextPassword",
5982 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5983 if (trusted_dn
== NULL
) {
5986 if (entry
== NULL
) {
5987 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5989 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5992 if (rc
!= LDAP_SUCCESS
) {
5993 DEBUG(1, ("error writing trusted domain password!\n"));
6000 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6004 struct ldapsam_privates
*ldap_state
=
6005 (struct ldapsam_privates
*)methods
->private_data
;
6006 LDAPMessage
*entry
= NULL
;
6007 const char *trusted_dn
;
6009 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6013 if (entry
== NULL
) {
6014 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6019 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6021 if (trusted_dn
== NULL
) {
6022 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6026 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6027 if (rc
!= LDAP_SUCCESS
) {
6034 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6035 TALLOC_CTX
*mem_ctx
,
6036 uint32
*num_domains
,
6037 struct trustdom_info
***domains
)
6040 struct ldapsam_privates
*ldap_state
=
6041 (struct ldapsam_privates
*)methods
->private_data
;
6043 int scope
= LDAP_SCOPE_SUBTREE
;
6044 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6045 int attrsonly
= 0; /* 0: return values too */
6046 LDAPMessage
*result
= NULL
;
6047 LDAPMessage
*entry
= NULL
;
6049 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6050 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6052 rc
= smbldap_search(ldap_state
->smbldap_state
,
6053 ldap_state
->domain_dn
,
6060 if (result
!= NULL
) {
6061 talloc_autofree_ldapmsg(mem_ctx
, result
);
6064 if (rc
!= LDAP_SUCCESS
) {
6065 return NT_STATUS_UNSUCCESSFUL
;
6069 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6070 DEBUG(1, ("talloc failed\n"));
6071 return NT_STATUS_NO_MEMORY
;
6074 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6076 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6078 char *dom_name
, *dom_sid_str
;
6079 struct trustdom_info
*dom_info
;
6081 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6082 if (dom_info
== NULL
) {
6083 DEBUG(1, ("talloc failed\n"));
6084 return NT_STATUS_NO_MEMORY
;
6087 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6091 if (dom_name
== NULL
) {
6092 DEBUG(1, ("talloc failed\n"));
6093 return NT_STATUS_NO_MEMORY
;
6095 dom_info
->name
= dom_name
;
6097 dom_sid_str
= smbldap_talloc_single_attribute(
6098 priv2ld(ldap_state
), entry
, "sambaSID",
6100 if (dom_sid_str
== NULL
) {
6101 DEBUG(1, ("talloc failed\n"));
6102 return NT_STATUS_NO_MEMORY
;
6104 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6105 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6107 return NT_STATUS_UNSUCCESSFUL
;
6110 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6111 domains
, num_domains
);
6113 if (*domains
== NULL
) {
6114 DEBUG(1, ("talloc failed\n"));
6115 return NT_STATUS_NO_MEMORY
;
6119 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6120 return NT_STATUS_OK
;
6124 /**********************************************************************
6126 *********************************************************************/
6128 static void free_private_data(void **vp
)
6130 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6132 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6134 if ((*ldap_state
)->result
!= NULL
) {
6135 ldap_msgfree((*ldap_state
)->result
);
6136 (*ldap_state
)->result
= NULL
;
6138 if ((*ldap_state
)->domain_dn
!= NULL
) {
6139 SAFE_FREE((*ldap_state
)->domain_dn
);
6144 /* No need to free any further, as it is talloc()ed */
6147 /*********************************************************************
6148 Intitalise the parts of the pdb_methods structure that are common to
6150 *********************************************************************/
6152 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6155 struct ldapsam_privates
*ldap_state
;
6157 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6161 (*pdb_method
)->name
= "ldapsam";
6163 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6164 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6165 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6166 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6167 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6168 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6170 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6171 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6172 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6173 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6174 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6175 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6176 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6178 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6179 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6181 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6183 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6184 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6186 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6187 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6188 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6189 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6191 /* TODO: Setup private data and free */
6193 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6194 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6195 return NT_STATUS_NO_MEMORY
;
6198 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6199 location
, &ldap_state
->smbldap_state
);
6201 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6205 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6206 return NT_STATUS_NO_MEMORY
;
6209 (*pdb_method
)->private_data
= ldap_state
;
6211 (*pdb_method
)->free_private_data
= free_private_data
;
6213 return NT_STATUS_OK
;
6216 /**********************************************************************
6217 Initialise the 'compat' mode for pdb_ldap
6218 *********************************************************************/
6220 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6223 struct ldapsam_privates
*ldap_state
;
6224 char *uri
= talloc_strdup( NULL
, location
);
6226 trim_char( uri
, '\"', '\"' );
6227 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6231 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6235 (*pdb_method
)->name
= "ldapsam_compat";
6237 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6238 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6240 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6242 return NT_STATUS_OK
;
6245 /**********************************************************************
6246 Initialise the normal mode for pdb_ldap
6247 *********************************************************************/
6249 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6252 struct ldapsam_privates
*ldap_state
= NULL
;
6253 uint32 alg_rid_base
;
6254 char *alg_rid_base_string
= NULL
;
6255 LDAPMessage
*result
= NULL
;
6256 LDAPMessage
*entry
= NULL
;
6257 DOM_SID ldap_domain_sid
;
6258 DOM_SID secrets_domain_sid
;
6259 char *domain_sid_string
= NULL
;
6261 char *uri
= talloc_strdup( NULL
, location
);
6263 trim_char( uri
, '\"', '\"' );
6264 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6269 if (!NT_STATUS_IS_OK(nt_status
)) {
6273 (*pdb_method
)->name
= "ldapsam";
6275 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6276 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6277 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6278 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6279 (*pdb_method
)->search_users
= ldapsam_search_users
;
6280 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6281 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6283 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6284 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6285 (*pdb_method
)->enum_group_memberships
=
6286 ldapsam_enum_group_memberships
;
6287 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6288 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6290 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6291 (*pdb_method
)->create_user
= ldapsam_create_user
;
6292 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6293 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6294 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6295 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6296 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6297 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6301 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6302 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6304 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6306 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6308 ldap_state
->domain_name
, True
);
6310 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6311 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6312 "info, nor add one to the domain\n"));
6313 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6314 "will be unable to allocate new users/groups, "
6315 "and will risk BDCs having inconsistant SIDs\n"));
6316 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6317 return NT_STATUS_OK
;
6320 /* Given that the above might fail, everything below this must be
6323 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6326 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6328 ldap_msgfree(result
);
6329 return NT_STATUS_UNSUCCESSFUL
;
6332 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6334 ldap_msgfree(result
);
6335 return NT_STATUS_UNSUCCESSFUL
;
6338 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6341 domain_sid_string
= smbldap_talloc_single_attribute(
6342 ldap_state
->smbldap_state
->ldap_struct
,
6344 get_userattr_key2string(ldap_state
->schema_ver
,
6345 LDAP_ATTR_USER_SID
),
6348 if (domain_sid_string
) {
6350 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6351 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6352 "read as a valid SID\n", domain_sid_string
));
6353 ldap_msgfree(result
);
6354 TALLOC_FREE(domain_sid_string
);
6355 return NT_STATUS_INVALID_PARAMETER
;
6357 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6358 &secrets_domain_sid
);
6359 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
6360 &ldap_domain_sid
)) {
6361 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6362 "%s based on pdb_ldap results %s -> %s\n",
6363 ldap_state
->domain_name
,
6364 sid_string_dbg(&secrets_domain_sid
),
6365 sid_string_dbg(&ldap_domain_sid
)));
6367 /* reset secrets.tdb sid */
6368 secrets_store_domain_sid(ldap_state
->domain_name
,
6370 DEBUG(1, ("New global sam SID: %s\n",
6371 sid_string_dbg(get_global_sam_sid())));
6373 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6374 TALLOC_FREE(domain_sid_string
);
6377 alg_rid_base_string
= smbldap_talloc_single_attribute(
6378 ldap_state
->smbldap_state
->ldap_struct
,
6380 get_attr_key2string( dominfo_attr_list
,
6381 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6383 if (alg_rid_base_string
) {
6384 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
6385 if (alg_rid_base
!= algorithmic_rid_base()) {
6386 DEBUG(0, ("The value of 'algorithmic RID base' has "
6387 "changed since the LDAP\n"
6388 "database was initialised. Aborting. \n"));
6389 ldap_msgfree(result
);
6390 TALLOC_FREE(alg_rid_base_string
);
6391 return NT_STATUS_UNSUCCESSFUL
;
6393 TALLOC_FREE(alg_rid_base_string
);
6395 ldap_msgfree(result
);
6397 return NT_STATUS_OK
;
6400 NTSTATUS
pdb_ldap_init(void)
6403 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6406 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6409 /* Let pdb_nds register backends */
6412 return NT_STATUS_OK
;