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
49 #define DBGC_CLASS DBGC_PASSDB
55 * Work around versions of the LDAP client libs that don't have the OIDs
56 * defined, or have them defined under the old name.
57 * This functionality is really a factor of the server, not the client
61 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
62 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
63 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
64 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
67 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
68 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
69 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
70 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
73 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
75 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
76 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
82 /**********************************************************************
83 Simple helper function to make stuff better readable
84 **********************************************************************/
86 static LDAP
*priv2ld(struct ldapsam_privates
*priv
)
88 return priv
->smbldap_state
->ldap_struct
;
91 /**********************************************************************
92 Get the attribute name given a user schame version.
93 **********************************************************************/
95 static const char* get_userattr_key2string( int schema_ver
, int key
)
97 switch ( schema_ver
) {
98 case SCHEMAVER_SAMBAACCOUNT
:
99 return get_attr_key2string( attrib_map_v22
, key
);
101 case SCHEMAVER_SAMBASAMACCOUNT
:
102 return get_attr_key2string( attrib_map_v30
, key
);
105 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
111 /**********************************************************************
112 Return the list of attribute names given a user schema version.
113 **********************************************************************/
115 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
117 switch ( schema_ver
) {
118 case SCHEMAVER_SAMBAACCOUNT
:
119 return get_attr_list( mem_ctx
, attrib_map_v22
);
121 case SCHEMAVER_SAMBASAMACCOUNT
:
122 return get_attr_list( mem_ctx
, attrib_map_v30
);
124 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
131 /**************************************************************************
132 Return the list of attribute names to delete given a user schema version.
133 **************************************************************************/
135 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
138 switch ( schema_ver
) {
139 case SCHEMAVER_SAMBAACCOUNT
:
140 return get_attr_list( mem_ctx
,
141 attrib_map_to_delete_v22
);
143 case SCHEMAVER_SAMBASAMACCOUNT
:
144 return get_attr_list( mem_ctx
,
145 attrib_map_to_delete_v30
);
147 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
155 /*******************************************************************
156 Generate the LDAP search filter for the objectclass based on the
157 version of the schema we are using.
158 ******************************************************************/
160 static const char* get_objclass_filter( int schema_ver
)
162 fstring objclass_filter
;
165 switch( schema_ver
) {
166 case SCHEMAVER_SAMBAACCOUNT
:
167 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
169 case SCHEMAVER_SAMBASAMACCOUNT
:
170 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
173 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
174 objclass_filter
[0] = '\0';
178 result
= talloc_strdup(talloc_tos(), objclass_filter
);
179 SMB_ASSERT(result
!= NULL
);
183 /*****************************************************************
184 Scan a sequence number off OpenLDAP's syncrepl contextCSN
185 ******************************************************************/
187 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
189 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
190 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
191 LDAPMessage
*msg
= NULL
;
192 LDAPMessage
*entry
= NULL
;
194 char **values
= NULL
;
195 int rc
, num_result
, num_values
, rid
;
201 /* Unfortunatly there is no proper way to detect syncrepl-support in
202 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
203 * but do not show up in the root-DSE yet. Neither we can query the
204 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
205 * objectclass. Currently we require lp_ldap_suffix() to show up as
206 * namingContext. - Guenther
209 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
214 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
218 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
219 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
220 "as top-level namingContext\n", lp_ldap_suffix()));
224 mem_ctx
= talloc_init("ldapsam_get_seq_num");
227 return NT_STATUS_NO_MEMORY
;
229 if ((attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2)) == NULL
) {
230 ntstatus
= NT_STATUS_NO_MEMORY
;
234 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
235 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
238 /* consumer syncreplCookie: */
239 /* csn=20050126161620Z#0000001#00#00000 */
240 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
242 suffix
= talloc_asprintf(mem_ctx
,
243 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
245 ntstatus
= NT_STATUS_NO_MEMORY
;
250 /* provider contextCSN */
251 /* 20050126161620Z#000009#00#000000 */
252 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
254 suffix
= talloc_asprintf(mem_ctx
,
255 "cn=ldapsync,%s", lp_ldap_suffix());
258 ntstatus
= NT_STATUS_NO_MEMORY
;
263 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
264 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
266 if (rc
!= LDAP_SUCCESS
) {
270 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
271 if (num_result
!= 1) {
272 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
276 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
278 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
282 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
283 if (values
== NULL
) {
284 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
288 num_values
= ldap_count_values(values
);
289 if (num_values
== 0) {
290 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
295 if (!next_token(&p
, tok
, "#", sizeof(tok
))) {
296 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
301 if (!strncmp(p
, "csn=", strlen("csn=")))
304 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
306 *seq_num
= generalized_to_unix_time(p
);
308 /* very basic sanity check */
310 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
315 ntstatus
= NT_STATUS_OK
;
319 ldap_value_free(values
);
323 talloc_destroy(mem_ctx
);
328 /*******************************************************************
329 Run the search by name.
330 ******************************************************************/
332 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
334 LDAPMessage
** result
,
338 char *escape_user
= escape_ldap_string_alloc(user
);
342 return LDAP_NO_MEMORY
;
346 * in the filter expression, replace %u with the real name
347 * so in ldap filter, %u MUST exist :-)
349 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
350 get_objclass_filter(ldap_state
->schema_ver
));
352 return LDAP_NO_MEMORY
;
355 * have to use this here because $ is filtered out
359 filter
= talloc_all_string_sub(talloc_tos(),
360 filter
, "%u", escape_user
);
362 return LDAP_NO_MEMORY
;
364 SAFE_FREE(escape_user
);
366 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
367 filter
, attr
, result
);
372 /*******************************************************************
373 Run the search by rid.
374 ******************************************************************/
376 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
377 uint32 rid
, LDAPMessage
** result
,
383 filter
= talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid
,
384 get_objclass_filter(ldap_state
->schema_ver
));
386 return LDAP_NO_MEMORY
;
389 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
390 filter
, attr
, result
);
395 /*******************************************************************
396 Run the search by SID.
397 ******************************************************************/
399 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
400 const DOM_SID
*sid
, LDAPMessage
** result
,
407 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
408 get_userattr_key2string(ldap_state
->schema_ver
,
410 sid_to_string(sid_string
, sid
),
411 get_objclass_filter(ldap_state
->schema_ver
));
413 return LDAP_NO_MEMORY
;
416 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
417 filter
, attr
, result
);
423 /*******************************************************************
424 Delete complete object or objectclass and attrs from
425 object found in search_result depending on lp_ldap_delete_dn
426 ******************************************************************/
428 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
431 const char *objectclass
,
434 LDAPMod
**mods
= NULL
;
437 BerElement
*ptr
= NULL
;
439 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
441 return LDAP_NO_MEMORY
;
444 if (lp_ldap_delete_dn()) {
445 return smbldap_delete(priv
->smbldap_state
, dn
);
448 /* Ok, delete only the SAM attributes */
450 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
452 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
455 /* We are only allowed to delete the attributes that
458 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
459 if (strequal(*attrib
, name
)) {
460 DEBUG(10, ("ldapsam_delete_entry: deleting "
461 "attribute %s\n", name
));
462 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
473 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
474 talloc_autofree_ldapmod(mem_ctx
, mods
);
476 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
479 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
484 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
485 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
491 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
492 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
502 /**********************************************************************
503 Initialize struct samu from an LDAP query.
504 (Based on init_sam_from_buffer in pdb_tdb.c)
505 *********************************************************************/
507 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
508 struct samu
* sampass
,
515 pass_can_change_time
,
516 pass_must_change_time
,
519 char *username
= NULL
,
525 *logon_script
= NULL
,
526 *profile_path
= NULL
,
528 *workstations
= NULL
,
531 uint8 smblmpwd
[LM_HASH_LEN
],
532 smbntpwd
[NT_HASH_LEN
];
533 bool use_samba_attrs
= True
;
534 uint32 acct_ctrl
= 0;
536 uint16 bad_password_count
= 0,
539 uint8 hours
[MAX_HOURS_LEN
];
541 LOGIN_CACHE
*cache_entry
= NULL
;
543 bool expand_explicit
= lp_passdb_expand_explicit();
545 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
550 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
551 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
555 if (priv2ld(ldap_state
) == NULL
) {
556 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
557 "ldap_struct is NULL!\n"));
561 if (!(username
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
565 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
570 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
572 nt_username
= talloc_strdup(ctx
, username
);
577 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
582 pdb_set_username(sampass
, username
, PDB_SET
);
584 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
585 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
587 /* deal with different attributes between the schema first */
589 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
590 if ((temp
= smbldap_talloc_single_attribute(
591 ldap_state
->smbldap_state
->ldap_struct
,
593 get_userattr_key2string(ldap_state
->schema_ver
,
596 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
599 if ((temp
= smbldap_talloc_single_attribute(
600 ldap_state
->smbldap_state
->ldap_struct
,
602 get_userattr_key2string(ldap_state
->schema_ver
,
605 user_rid
= (uint32
)atol(temp
);
606 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
610 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
611 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
612 get_userattr_key2string(ldap_state
->schema_ver
,
614 get_userattr_key2string(ldap_state
->schema_ver
,
620 temp
= smbldap_talloc_single_attribute(
621 ldap_state
->smbldap_state
->ldap_struct
,
623 get_userattr_key2string(ldap_state
->schema_ver
,
624 LDAP_ATTR_PWD_LAST_SET
),
627 pass_last_set_time
= (time_t) atol(temp
);
628 pdb_set_pass_last_set_time(sampass
,
629 pass_last_set_time
, PDB_SET
);
632 temp
= smbldap_talloc_single_attribute(
633 ldap_state
->smbldap_state
->ldap_struct
,
635 get_userattr_key2string(ldap_state
->schema_ver
,
636 LDAP_ATTR_LOGON_TIME
),
639 logon_time
= (time_t) atol(temp
);
640 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
643 temp
= smbldap_talloc_single_attribute(
644 ldap_state
->smbldap_state
->ldap_struct
,
646 get_userattr_key2string(ldap_state
->schema_ver
,
647 LDAP_ATTR_LOGOFF_TIME
),
650 logoff_time
= (time_t) atol(temp
);
651 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
654 temp
= smbldap_talloc_single_attribute(
655 ldap_state
->smbldap_state
->ldap_struct
,
657 get_userattr_key2string(ldap_state
->schema_ver
,
658 LDAP_ATTR_KICKOFF_TIME
),
661 kickoff_time
= (time_t) atol(temp
);
662 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
665 temp
= smbldap_talloc_single_attribute(
666 ldap_state
->smbldap_state
->ldap_struct
,
668 get_userattr_key2string(ldap_state
->schema_ver
,
669 LDAP_ATTR_PWD_CAN_CHANGE
),
672 pass_can_change_time
= (time_t) atol(temp
);
673 pdb_set_pass_can_change_time(sampass
,
674 pass_can_change_time
, PDB_SET
);
677 temp
= smbldap_talloc_single_attribute(
678 ldap_state
->smbldap_state
->ldap_struct
,
680 get_userattr_key2string(ldap_state
->schema_ver
,
681 LDAP_ATTR_PWD_MUST_CHANGE
),
684 pass_must_change_time
= (time_t) atol(temp
);
685 pdb_set_pass_must_change_time(sampass
,
686 pass_must_change_time
, PDB_SET
);
689 /* recommend that 'gecos' and 'displayName' should refer to the same
690 * attribute OID. userFullName depreciated, only used by Samba
691 * primary rules of LDAP: don't make a new attribute when one is already defined
692 * that fits your needs; using cn then displayName rather than 'userFullName'
695 fullname
= smbldap_talloc_single_attribute(
696 ldap_state
->smbldap_state
->ldap_struct
,
698 get_userattr_key2string(ldap_state
->schema_ver
,
699 LDAP_ATTR_DISPLAY_NAME
),
702 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
704 fullname
= smbldap_talloc_single_attribute(
705 ldap_state
->smbldap_state
->ldap_struct
,
707 get_userattr_key2string(ldap_state
->schema_ver
,
711 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
715 dir_drive
= smbldap_talloc_single_attribute(
716 ldap_state
->smbldap_state
->ldap_struct
,
718 get_userattr_key2string(ldap_state
->schema_ver
,
719 LDAP_ATTR_HOME_DRIVE
),
722 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
724 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
727 homedir
= smbldap_talloc_single_attribute(
728 ldap_state
->smbldap_state
->ldap_struct
,
730 get_userattr_key2string(ldap_state
->schema_ver
,
731 LDAP_ATTR_HOME_PATH
),
734 if (expand_explicit
) {
735 homedir
= talloc_sub_basic(ctx
,
743 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
745 pdb_set_homedir(sampass
,
746 talloc_sub_basic(ctx
, username
, domain
,
751 logon_script
= smbldap_talloc_single_attribute(
752 ldap_state
->smbldap_state
->ldap_struct
,
754 get_userattr_key2string(ldap_state
->schema_ver
,
755 LDAP_ATTR_LOGON_SCRIPT
),
758 if (expand_explicit
) {
759 logon_script
= talloc_sub_basic(ctx
,
767 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
769 pdb_set_logon_script(sampass
,
770 talloc_sub_basic(ctx
, username
, domain
,
775 profile_path
= smbldap_talloc_single_attribute(
776 ldap_state
->smbldap_state
->ldap_struct
,
778 get_userattr_key2string(ldap_state
->schema_ver
,
779 LDAP_ATTR_PROFILE_PATH
),
782 if (expand_explicit
) {
783 profile_path
= talloc_sub_basic(ctx
,
791 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
793 pdb_set_profile_path(sampass
,
794 talloc_sub_basic(ctx
, username
, domain
,
799 acct_desc
= smbldap_talloc_single_attribute(
800 ldap_state
->smbldap_state
->ldap_struct
,
802 get_userattr_key2string(ldap_state
->schema_ver
,
806 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
809 workstations
= smbldap_talloc_single_attribute(
810 ldap_state
->smbldap_state
->ldap_struct
,
812 get_userattr_key2string(ldap_state
->schema_ver
,
816 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
819 munged_dial
= smbldap_talloc_single_attribute(
820 ldap_state
->smbldap_state
->ldap_struct
,
822 get_userattr_key2string(ldap_state
->schema_ver
,
823 LDAP_ATTR_MUNGED_DIAL
),
826 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
829 /* FIXME: hours stuff should be cleaner */
833 memset(hours
, 0xff, hours_len
);
835 if (ldap_state
->is_nds_ldap
) {
838 char clear_text_pw
[512];
840 /* Make call to Novell eDirectory ldap extension to get clear text password.
841 NOTE: This will only work if we have an SSL connection to eDirectory. */
842 user_dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
843 if (user_dn
!= NULL
) {
844 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username
, user_dn
));
846 pwd_len
= sizeof(clear_text_pw
);
847 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
848 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
849 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
853 ZERO_STRUCT(smblmpwd
);
854 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
858 ZERO_STRUCT(smbntpwd
);
859 use_samba_attrs
= False
;
865 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
869 if (use_samba_attrs
) {
870 temp
= smbldap_talloc_single_attribute(
871 ldap_state
->smbldap_state
->ldap_struct
,
873 get_userattr_key2string(ldap_state
->schema_ver
,
877 pdb_gethexpwd(temp
, smblmpwd
);
878 memset((char *)temp
, '\0', strlen(temp
)+1);
879 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
882 ZERO_STRUCT(smblmpwd
);
885 temp
= smbldap_talloc_single_attribute(
886 ldap_state
->smbldap_state
->ldap_struct
,
888 get_userattr_key2string(ldap_state
->schema_ver
,
892 pdb_gethexpwd(temp
, smbntpwd
);
893 memset((char *)temp
, '\0', strlen(temp
)+1);
894 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
897 ZERO_STRUCT(smbntpwd
);
903 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
905 uint8
*pwhist
= NULL
;
907 char *history_string
= TALLOC_ARRAY(ctx
, char,
908 MAX_PW_HISTORY_LEN
*64);
910 if (!history_string
) {
914 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
916 if ((pwhist
= TALLOC_ARRAY(ctx
, uint8
,
917 pwHistLen
* PW_HISTORY_ENTRY_LEN
)) ==
919 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
922 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
924 if (smbldap_get_single_attribute(
925 ldap_state
->smbldap_state
->ldap_struct
,
927 get_userattr_key2string(ldap_state
->schema_ver
,
928 LDAP_ATTR_PWD_HISTORY
),
930 MAX_PW_HISTORY_LEN
*64)) {
931 bool hex_failed
= false;
932 for (i
= 0; i
< pwHistLen
; i
++){
933 /* Get the 16 byte salt. */
934 if (!pdb_gethexpwd(&history_string
[i
*64],
935 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
939 /* Get the 16 byte MD5 hash of salt+passwd. */
940 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
941 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
942 PW_HISTORY_SALT_LEN
])) {
948 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
950 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
953 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
958 temp
= smbldap_talloc_single_attribute(
959 ldap_state
->smbldap_state
->ldap_struct
,
961 get_userattr_key2string(ldap_state
->schema_ver
,
965 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
967 if (acct_ctrl
== 0) {
968 acct_ctrl
|= ACB_NORMAL
;
971 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
973 acct_ctrl
|= ACB_NORMAL
;
976 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
977 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
979 temp
= smbldap_talloc_single_attribute(
980 ldap_state
->smbldap_state
->ldap_struct
,
982 get_userattr_key2string(ldap_state
->schema_ver
,
983 LDAP_ATTR_BAD_PASSWORD_COUNT
),
986 bad_password_count
= (uint32
) atol(temp
);
987 pdb_set_bad_password_count(sampass
,
988 bad_password_count
, PDB_SET
);
991 temp
= smbldap_talloc_single_attribute(
992 ldap_state
->smbldap_state
->ldap_struct
,
994 get_userattr_key2string(ldap_state
->schema_ver
,
995 LDAP_ATTR_BAD_PASSWORD_TIME
),
998 bad_password_time
= (time_t) atol(temp
);
999 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
1003 temp
= smbldap_talloc_single_attribute(
1004 ldap_state
->smbldap_state
->ldap_struct
,
1006 get_userattr_key2string(ldap_state
->schema_ver
,
1007 LDAP_ATTR_LOGON_COUNT
),
1010 logon_count
= (uint32
) atol(temp
);
1011 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
1014 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1016 temp
= smbldap_talloc_single_attribute(
1017 ldap_state
->smbldap_state
->ldap_struct
,
1019 get_userattr_key2string(ldap_state
->schema_ver
,
1020 LDAP_ATTR_LOGON_HOURS
),
1023 pdb_gethexhours(temp
, hours
);
1024 memset((char *)temp
, '\0', strlen(temp
) +1);
1025 pdb_set_hours(sampass
, hours
, PDB_SET
);
1029 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1030 temp
= smbldap_talloc_single_attribute(
1031 priv2ld(ldap_state
),
1036 /* We've got a uid, feed the cache */
1037 uid_t uid
= strtoul(temp
, NULL
, 10);
1038 store_uid_sid_cache(pdb_get_user_sid(sampass
), uid
);
1042 /* check the timestamp of the cache vs ldap entry */
1043 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1049 /* see if we have newer updates */
1050 if (!(cache_entry
= login_cache_read(sampass
))) {
1051 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1052 (unsigned int)pdb_get_bad_password_count(sampass
),
1053 (unsigned int)pdb_get_bad_password_time(sampass
)));
1058 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1059 (unsigned int)ldap_entry_time
,
1060 (unsigned int)cache_entry
->entry_timestamp
,
1061 (unsigned int)cache_entry
->bad_password_time
));
1063 if (ldap_entry_time
> cache_entry
->entry_timestamp
) {
1064 /* cache is older than directory , so
1065 we need to delete the entry but allow the
1066 fields to be written out */
1067 login_cache_delentry(sampass
);
1070 pdb_set_acct_ctrl(sampass
,
1071 pdb_get_acct_ctrl(sampass
) |
1072 (cache_entry
->acct_ctrl
& ACB_AUTOLOCK
),
1074 pdb_set_bad_password_count(sampass
,
1075 cache_entry
->bad_password_count
,
1077 pdb_set_bad_password_time(sampass
,
1078 cache_entry
->bad_password_time
,
1087 SAFE_FREE(cache_entry
);
1091 /**********************************************************************
1092 Initialize the ldap db from a struct samu. Called on update.
1093 (Based on init_buffer_from_sam in pdb_tdb.c)
1094 *********************************************************************/
1096 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1097 LDAPMessage
*existing
,
1098 LDAPMod
*** mods
, struct samu
* sampass
,
1099 bool (*need_update
)(const struct samu
*,
1105 if (mods
== NULL
|| sampass
== NULL
) {
1106 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1113 * took out adding "objectclass: sambaAccount"
1114 * do this on a per-mod basis
1116 if (need_update(sampass
, PDB_USERNAME
)) {
1117 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1118 "uid", pdb_get_username(sampass
));
1119 if (ldap_state
->is_nds_ldap
) {
1120 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1121 "cn", pdb_get_username(sampass
));
1122 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1123 "sn", pdb_get_username(sampass
));
1127 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1129 /* only update the RID if we actually need to */
1130 if (need_update(sampass
, PDB_USERSID
)) {
1132 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
1134 switch ( ldap_state
->schema_ver
) {
1135 case SCHEMAVER_SAMBAACCOUNT
:
1136 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1137 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1138 sid_string_static(user_sid
),
1139 sid_string_static(&ldap_state
->domain_sid
)));
1142 if (asprintf(&temp
, "%i", rid
) < 0) {
1145 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1146 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1151 case SCHEMAVER_SAMBASAMACCOUNT
:
1152 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1153 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1154 sid_to_string(sid_string
, user_sid
));
1158 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1163 /* we don't need to store the primary group RID - so leaving it
1164 'free' to hang off the unix primary group makes life easier */
1166 if (need_update(sampass
, PDB_GROUPSID
)) {
1168 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
1170 switch ( ldap_state
->schema_ver
) {
1171 case SCHEMAVER_SAMBAACCOUNT
:
1172 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1173 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1174 sid_string_static(group_sid
),
1175 sid_string_static(&ldap_state
->domain_sid
)));
1179 if (asprintf(&temp
, "%i", rid
) < 0) {
1182 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1183 get_userattr_key2string(ldap_state
->schema_ver
,
1184 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1188 case SCHEMAVER_SAMBASAMACCOUNT
:
1189 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1190 get_userattr_key2string(ldap_state
->schema_ver
,
1191 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_string(sid_string
, group_sid
));
1195 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1201 /* displayName, cn, and gecos should all be the same
1202 * most easily accomplished by giving them the same OID
1203 * gecos isn't set here b/c it should be handled by the
1205 * We change displayName only and fall back to cn if
1206 * it does not exist.
1209 if (need_update(sampass
, PDB_FULLNAME
))
1210 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1211 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1212 pdb_get_fullname(sampass
));
1214 if (need_update(sampass
, PDB_ACCTDESC
))
1215 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1216 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1217 pdb_get_acct_desc(sampass
));
1219 if (need_update(sampass
, PDB_WORKSTATIONS
))
1220 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1221 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1222 pdb_get_workstations(sampass
));
1224 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1225 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1226 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1227 pdb_get_munged_dial(sampass
));
1229 if (need_update(sampass
, PDB_SMBHOME
))
1230 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1231 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1232 pdb_get_homedir(sampass
));
1234 if (need_update(sampass
, PDB_DRIVE
))
1235 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1236 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1237 pdb_get_dir_drive(sampass
));
1239 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1240 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1241 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1242 pdb_get_logon_script(sampass
));
1244 if (need_update(sampass
, PDB_PROFILE
))
1245 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1246 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1247 pdb_get_profile_path(sampass
));
1249 if (asprintf(&temp
, "%li", pdb_get_logon_time(sampass
)) < 0) {
1252 if (need_update(sampass
, PDB_LOGONTIME
))
1253 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1254 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1257 if (asprintf(&temp
, "%li", pdb_get_logoff_time(sampass
)) < 0) {
1260 if (need_update(sampass
, PDB_LOGOFFTIME
))
1261 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1262 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1265 if (asprintf(&temp
, "%li", pdb_get_kickoff_time(sampass
)) < 0) {
1268 if (need_update(sampass
, PDB_KICKOFFTIME
))
1269 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1270 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1273 if (asprintf(&temp
, "%li", pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1276 if (need_update(sampass
, PDB_CANCHANGETIME
))
1277 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1278 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1281 if (asprintf(&temp
, "%li", pdb_get_pass_must_change_time(sampass
)) < 0) {
1284 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1285 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1286 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1289 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1290 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1292 if (need_update(sampass
, PDB_LMPASSWD
)) {
1293 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1296 pdb_sethexpwd(pwstr
, lm_pw
,
1297 pdb_get_acct_ctrl(sampass
));
1298 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1299 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1302 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1303 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1307 if (need_update(sampass
, PDB_NTPASSWD
)) {
1308 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1311 pdb_sethexpwd(pwstr
, nt_pw
,
1312 pdb_get_acct_ctrl(sampass
));
1313 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1314 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1317 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1318 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1323 if (need_update(sampass
, PDB_PWHISTORY
)) {
1325 uint32 pwHistLen
= 0;
1326 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
1328 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1332 if (pwHistLen
== 0) {
1333 /* Remove any password history from the LDAP store. */
1334 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1338 uint32 currHistLen
= 0;
1339 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1340 if (pwhist
!= NULL
) {
1341 /* We can only store (1024-1/64 password history entries. */
1342 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1343 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1344 /* Store the salt. */
1345 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1346 /* Followed by the md5 hash of salt + md4 hash */
1347 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1348 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1349 DEBUG(100, ("pwstr=%s\n", pwstr
));
1353 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1354 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1359 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1360 if (asprintf(&temp
, "%li",
1361 pdb_get_pass_last_set_time(sampass
)) < 0) {
1364 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1365 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1371 if (need_update(sampass
, PDB_HOURS
)) {
1372 const uint8
*hours
= pdb_get_hours(sampass
);
1375 pdb_sethexhours(hourstr
, hours
);
1376 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1379 get_userattr_key2string(ldap_state
->schema_ver
,
1380 LDAP_ATTR_LOGON_HOURS
),
1385 if (need_update(sampass
, PDB_ACCTCTRL
))
1386 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1387 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1388 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1390 /* password lockout cache:
1391 - If we are now autolocking or clearing, we write to ldap
1392 - If we are clearing, we delete the cache entry
1393 - If the count is > 0, we update the cache
1395 This even means when autolocking, we cache, just in case the
1396 update doesn't work, and we have to cache the autolock flag */
1398 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1399 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1400 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1401 time_t badtime
= pdb_get_bad_password_time(sampass
);
1403 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, &pol
);
1405 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1406 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1408 if ((badcount
>= pol
) || (badcount
== 0)) {
1409 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1410 (unsigned int)badcount
, (unsigned int)badtime
));
1411 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1415 ldap_state
->smbldap_state
->ldap_struct
,
1417 get_userattr_key2string(
1418 ldap_state
->schema_ver
,
1419 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1423 if (asprintf(&temp
, "%li", badtime
) < 0) {
1427 ldap_state
->smbldap_state
->ldap_struct
,
1429 get_userattr_key2string(
1430 ldap_state
->schema_ver
,
1431 LDAP_ATTR_BAD_PASSWORD_TIME
),
1435 if (badcount
== 0) {
1436 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1437 login_cache_delentry(sampass
);
1439 LOGIN_CACHE cache_entry
;
1441 cache_entry
.entry_timestamp
= time(NULL
);
1442 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1443 cache_entry
.bad_password_count
= badcount
;
1444 cache_entry
.bad_password_time
= badtime
;
1446 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1447 login_cache_write(sampass
, cache_entry
);
1454 /**********************************************************************
1455 Connect to LDAP server for password enumeration.
1456 *********************************************************************/
1458 static NTSTATUS
ldapsam_setsampwent(struct pdb_methods
*my_methods
, bool update
, uint32 acb_mask
)
1460 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1462 char *filter
= NULL
;
1464 const char **attr_list
;
1465 bool machine_mask
= False
, user_mask
= False
;
1466 NTSTATUS status
= NT_STATUS_OK
;
1467 TALLOC_CTX
*ctx
= talloc_init("ldapsam_setsampwent");
1470 return NT_STATUS_NO_MEMORY
;
1472 filter
= talloc_asprintf(ctx
, "(&%s%s)", "(uid=%u)",
1473 get_objclass_filter(ldap_state
->schema_ver
));
1475 status
= NT_STATUS_NO_MEMORY
;
1479 filter
= talloc_all_string_sub(ctx
, filter
, "%u", "*");
1481 status
= NT_STATUS_NO_MEMORY
;
1485 machine_mask
= ((acb_mask
!= 0) && (acb_mask
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)));
1486 user_mask
= ((acb_mask
!= 0) && (acb_mask
& ACB_NORMAL
));
1489 suffix
= lp_ldap_machine_suffix();
1490 } else if (user_mask
) {
1491 suffix
= lp_ldap_user_suffix();
1493 suffix
= lp_ldap_suffix();
1496 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1499 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1500 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
, LDAP_SCOPE_SUBTREE
, filter
,
1501 attr_list
, 0, &ldap_state
->result
);
1502 TALLOC_FREE( attr_list
);
1504 if (rc
!= LDAP_SUCCESS
) {
1505 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc
)));
1506 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix
, filter
));
1507 ldap_msgfree(ldap_state
->result
);
1508 ldap_state
->result
= NULL
;
1509 status
= NT_STATUS_UNSUCCESSFUL
;
1513 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1514 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
1515 ldap_state
->result
), suffix
));
1517 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
1518 ldap_state
->result
);
1519 ldap_state
->index
= 0;
1527 /**********************************************************************
1528 End enumeration of the LDAP password list.
1529 *********************************************************************/
1531 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1533 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1534 if (ldap_state
->result
) {
1535 ldap_msgfree(ldap_state
->result
);
1536 ldap_state
->result
= NULL
;
1540 /**********************************************************************
1541 Get the next entry in the LDAP password database.
1542 *********************************************************************/
1544 static NTSTATUS
ldapsam_getsampwent(struct pdb_methods
*my_methods
,
1547 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1548 struct ldapsam_privates
*ldap_state
=
1549 (struct ldapsam_privates
*)my_methods
->private_data
;
1553 if (!ldap_state
->entry
)
1556 ldap_state
->index
++;
1557 bret
= init_sam_from_ldap(ldap_state
, user
, ldap_state
->entry
);
1559 ldap_state
->entry
= ldap_next_entry(priv2ld(ldap_state
),
1563 return NT_STATUS_OK
;
1566 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1567 const char *new_attr
)
1571 if (new_attr
== NULL
) {
1575 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1579 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1581 SMB_ASSERT((*attr_list
) != NULL
);
1582 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1583 (*attr_list
)[i
+1] = NULL
;
1586 /**********************************************************************
1587 Get struct samu entry from LDAP by username.
1588 *********************************************************************/
1590 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1592 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1593 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1594 LDAPMessage
*result
= NULL
;
1595 LDAPMessage
*entry
= NULL
;
1597 const char ** attr_list
;
1600 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1601 append_attr(user
, &attr_list
,
1602 get_userattr_key2string(ldap_state
->schema_ver
,
1603 LDAP_ATTR_MOD_TIMESTAMP
));
1604 append_attr(user
, &attr_list
, "uidNumber");
1605 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1607 TALLOC_FREE( attr_list
);
1609 if ( rc
!= LDAP_SUCCESS
)
1610 return NT_STATUS_NO_SUCH_USER
;
1612 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1615 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1616 ldap_msgfree(result
);
1617 return NT_STATUS_NO_SUCH_USER
;
1618 } else if (count
> 1) {
1619 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1620 ldap_msgfree(result
);
1621 return NT_STATUS_NO_SUCH_USER
;
1624 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1626 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1627 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1628 ldap_msgfree(result
);
1629 return NT_STATUS_NO_SUCH_USER
;
1631 pdb_set_backend_private_data(user
, result
, NULL
,
1632 my_methods
, PDB_CHANGED
);
1633 talloc_autofree_ldapmsg(user
, result
);
1636 ldap_msgfree(result
);
1641 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1642 const DOM_SID
*sid
, LDAPMessage
**result
)
1645 const char ** attr_list
;
1648 switch ( ldap_state
->schema_ver
) {
1649 case SCHEMAVER_SAMBASAMACCOUNT
: {
1650 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1651 if (tmp_ctx
== NULL
) {
1652 return LDAP_NO_MEMORY
;
1655 attr_list
= get_userattr_list(tmp_ctx
,
1656 ldap_state
->schema_ver
);
1657 append_attr(tmp_ctx
, &attr_list
,
1658 get_userattr_key2string(
1659 ldap_state
->schema_ver
,
1660 LDAP_ATTR_MOD_TIMESTAMP
));
1661 append_attr(tmp_ctx
, &attr_list
, "uidNumber");
1662 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1664 TALLOC_FREE(tmp_ctx
);
1666 if ( rc
!= LDAP_SUCCESS
)
1671 case SCHEMAVER_SAMBAACCOUNT
:
1672 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1676 attr_list
= get_userattr_list(NULL
,
1677 ldap_state
->schema_ver
);
1678 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1679 TALLOC_FREE( attr_list
);
1681 if ( rc
!= LDAP_SUCCESS
)
1688 /**********************************************************************
1689 Get struct samu entry from LDAP by SID.
1690 *********************************************************************/
1692 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1694 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1695 LDAPMessage
*result
= NULL
;
1696 LDAPMessage
*entry
= NULL
;
1701 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1703 if (rc
!= LDAP_SUCCESS
)
1704 return NT_STATUS_NO_SUCH_USER
;
1706 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1709 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string
, sid
),
1711 ldap_msgfree(result
);
1712 return NT_STATUS_NO_SUCH_USER
;
1713 } else if (count
> 1) {
1714 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string
, sid
),
1716 ldap_msgfree(result
);
1717 return NT_STATUS_NO_SUCH_USER
;
1720 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1722 ldap_msgfree(result
);
1723 return NT_STATUS_NO_SUCH_USER
;
1726 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1727 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1728 ldap_msgfree(result
);
1729 return NT_STATUS_NO_SUCH_USER
;
1732 pdb_set_backend_private_data(user
, result
, NULL
,
1733 my_methods
, PDB_CHANGED
);
1734 talloc_autofree_ldapmsg(user
, result
);
1735 return NT_STATUS_OK
;
1738 /********************************************************************
1739 Do the actual modification - also change a plaintext passord if
1741 **********************************************************************/
1743 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1744 struct samu
*newpwd
, char *dn
,
1745 LDAPMod
**mods
, int ldap_op
,
1746 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1748 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1751 if (!newpwd
|| !dn
) {
1752 return NT_STATUS_INVALID_PARAMETER
;
1756 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1757 /* may be password change below however */
1761 if (ldap_state
->is_nds_ldap
) {
1762 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1766 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1770 rc
= smbldap_add(ldap_state
->smbldap_state
,
1773 case LDAP_MOD_REPLACE
:
1774 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1778 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1780 return NT_STATUS_INVALID_PARAMETER
;
1783 if (rc
!=LDAP_SUCCESS
) {
1784 return NT_STATUS_UNSUCCESSFUL
;
1788 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1789 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1790 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1791 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1794 char *retoid
= NULL
;
1795 struct berval
*retdata
= NULL
;
1796 char *utf8_password
;
1799 if (!ldap_state
->is_nds_ldap
) {
1801 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1802 LDAP_EXOP_MODIFY_PASSWD
)) {
1803 DEBUG(2, ("ldap password change requested, but LDAP "
1804 "server does not support it -- ignoring\n"));
1805 return NT_STATUS_OK
;
1809 if (push_utf8_allocate(&utf8_password
, pdb_get_plaintext_passwd(newpwd
)) == (size_t)-1) {
1810 return NT_STATUS_NO_MEMORY
;
1813 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1814 SAFE_FREE(utf8_password
);
1815 return NT_STATUS_NO_MEMORY
;
1818 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1819 DEBUG(0,("ber_alloc_t returns NULL\n"));
1820 SAFE_FREE(utf8_password
);
1822 return NT_STATUS_UNSUCCESSFUL
;
1825 ber_printf (ber
, "{");
1826 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
, utf8_dn
);
1827 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
, utf8_password
);
1828 ber_printf (ber
, "n}");
1830 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1831 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1834 SAFE_FREE(utf8_password
);
1835 return NT_STATUS_UNSUCCESSFUL
;
1839 SAFE_FREE(utf8_password
);
1842 if (!ldap_state
->is_nds_ldap
) {
1843 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1844 LDAP_EXOP_MODIFY_PASSWD
,
1845 bv
, NULL
, NULL
, &retoid
,
1848 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1849 pdb_get_plaintext_passwd(newpwd
));
1851 if (rc
!= LDAP_SUCCESS
) {
1852 char *ld_error
= NULL
;
1854 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1855 DEBUG(3, ("Could not set userPassword "
1856 "attribute due to an objectClass "
1857 "violation -- ignoring\n"));
1859 return NT_STATUS_OK
;
1862 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1864 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1865 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1866 SAFE_FREE(ld_error
);
1868 return NT_STATUS_UNSUCCESSFUL
;
1870 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1871 #ifdef DEBUG_PASSWORD
1872 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1875 ber_bvfree(retdata
);
1877 ldap_memfree(retoid
);
1881 return NT_STATUS_OK
;
1884 /**********************************************************************
1885 Delete entry from LDAP for username.
1886 *********************************************************************/
1888 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1889 struct samu
* sam_acct
)
1891 struct ldapsam_privates
*priv
=
1892 (struct ldapsam_privates
*)my_methods
->private_data
;
1895 LDAPMessage
*msg
, *entry
;
1896 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1897 const char **attr_list
;
1898 TALLOC_CTX
*mem_ctx
;
1901 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1902 return NT_STATUS_INVALID_PARAMETER
;
1905 sname
= pdb_get_username(sam_acct
);
1907 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1910 mem_ctx
= talloc_new(NULL
);
1911 if (mem_ctx
== NULL
) {
1912 DEBUG(0, ("talloc_new failed\n"));
1916 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1917 if (attr_list
== NULL
) {
1921 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1923 if ((rc
!= LDAP_SUCCESS
) ||
1924 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1925 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1926 DEBUG(5, ("Could not find user %s\n", sname
));
1927 result
= NT_STATUS_NO_SUCH_USER
;
1931 rc
= ldapsam_delete_entry(
1932 priv
, mem_ctx
, entry
,
1933 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1934 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1937 result
= (rc
== LDAP_SUCCESS
) ?
1938 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1941 TALLOC_FREE(mem_ctx
);
1945 /**********************************************************************
1946 Helper function to determine for update_sam_account whether
1947 we need LDAP modification.
1948 *********************************************************************/
1950 static bool element_is_changed(const struct samu
*sampass
,
1951 enum pdb_elements element
)
1953 return IS_SAM_CHANGED(sampass
, element
);
1956 /**********************************************************************
1958 *********************************************************************/
1960 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1962 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1963 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1966 LDAPMessage
*result
= NULL
;
1967 LDAPMessage
*entry
= NULL
;
1968 LDAPMod
**mods
= NULL
;
1969 const char **attr_list
;
1971 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1973 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1974 if (pdb_get_username(newpwd
) == NULL
) {
1975 return NT_STATUS_INVALID_PARAMETER
;
1977 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1978 TALLOC_FREE( attr_list
);
1979 if (rc
!= LDAP_SUCCESS
) {
1980 return NT_STATUS_UNSUCCESSFUL
;
1982 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1983 my_methods
, PDB_CHANGED
);
1984 talloc_autofree_ldapmsg(newpwd
, result
);
1987 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1988 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1989 return NT_STATUS_UNSUCCESSFUL
;
1992 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1993 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1995 return NT_STATUS_UNSUCCESSFUL
;
1998 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
2000 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2001 element_is_changed
)) {
2002 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2005 ldap_mods_free(mods
,True
);
2006 return NT_STATUS_UNSUCCESSFUL
;
2010 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2011 pdb_get_username(newpwd
)));
2013 return NT_STATUS_OK
;
2016 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
2017 ldap_mods_free(mods
,True
);
2021 * We need to set the backend private data to NULL here. For example
2022 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2023 * same one, and with the explicit delete / add logic for attribute
2024 * values the second time we would use the wrong "old" value which
2025 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2027 * The existing LDAPMessage is still being auto-freed by the
2030 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2033 if (!NT_STATUS_IS_OK(ret
)) {
2037 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2038 pdb_get_username(newpwd
)));
2039 return NT_STATUS_OK
;
2042 /***************************************************************************
2043 Renames a struct samu
2044 - The "rename user script" has full responsibility for changing everything
2045 ***************************************************************************/
2047 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2048 struct samu
*old_acct
,
2049 const char *newname
)
2051 const char *oldname
;
2053 char *rename_script
= NULL
;
2054 fstring oldname_lower
, newname_lower
;
2057 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2058 return NT_STATUS_INVALID_PARAMETER
;
2061 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2062 return NT_STATUS_INVALID_PARAMETER
;
2065 oldname
= pdb_get_username(old_acct
);
2067 /* rename the posix user */
2068 rename_script
= SMB_STRDUP(lp_renameuser_script());
2069 if (rename_script
) {
2070 return NT_STATUS_NO_MEMORY
;
2073 if (!(*rename_script
)) {
2074 SAFE_FREE(rename_script
);
2075 return NT_STATUS_ACCESS_DENIED
;
2078 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2081 /* We have to allow the account name to end with a '$'.
2082 Also, follow the semantics in _samr_create_user() and lower case the
2083 posix name but preserve the case in passdb */
2085 fstrcpy( oldname_lower
, oldname
);
2086 strlower_m( oldname_lower
);
2087 fstrcpy( newname_lower
, newname
);
2088 strlower_m( newname_lower
);
2089 rename_script
= realloc_string_sub2(rename_script
,
2094 if (rename_script
) {
2095 return NT_STATUS_NO_MEMORY
;
2097 rename_script
= realloc_string_sub2(rename_script
,
2102 rc
= smbrun(rename_script
, NULL
);
2104 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2105 rename_script
, rc
));
2107 SAFE_FREE(rename_script
);
2110 smb_nscd_flush_user_cache();
2114 return NT_STATUS_UNSUCCESSFUL
;
2116 return NT_STATUS_OK
;
2119 /**********************************************************************
2120 Helper function to determine for update_sam_account whether
2121 we need LDAP modification.
2122 *********************************************************************/
2124 static bool element_is_set_or_changed(const struct samu
*sampass
,
2125 enum pdb_elements element
)
2127 return (IS_SAM_SET(sampass
, element
) ||
2128 IS_SAM_CHANGED(sampass
, element
));
2131 /**********************************************************************
2132 Add struct samu to LDAP.
2133 *********************************************************************/
2135 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2137 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2138 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2140 LDAPMessage
*result
= NULL
;
2141 LDAPMessage
*entry
= NULL
;
2142 LDAPMod
**mods
= NULL
;
2143 int ldap_op
= LDAP_MOD_REPLACE
;
2145 const char **attr_list
;
2146 char *escape_user
= NULL
;
2147 const char *username
= pdb_get_username(newpwd
);
2148 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
2149 char *filter
= NULL
;
2152 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2153 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2156 return NT_STATUS_NO_MEMORY
;
2159 if (!username
|| !*username
) {
2160 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2161 status
= NT_STATUS_INVALID_PARAMETER
;
2165 /* free this list after the second search or in case we exit on failure */
2166 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2168 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2170 if (rc
!= LDAP_SUCCESS
) {
2174 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2175 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2179 ldap_msgfree(result
);
2182 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2183 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2185 if (rc
== LDAP_SUCCESS
) {
2186 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2187 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
2188 sid_to_string(sid_string
, sid
)));
2191 ldap_msgfree(result
);
2196 /* does the entry already exist but without a samba attributes?
2197 we need to return the samba attributes here */
2199 escape_user
= escape_ldap_string_alloc( username
);
2200 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2202 status
= NT_STATUS_NO_MEMORY
;
2205 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2207 status
= NT_STATUS_NO_MEMORY
;
2210 SAFE_FREE(escape_user
);
2212 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2213 filter
, attr_list
, &result
);
2214 if ( rc
!= LDAP_SUCCESS
) {
2218 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2220 if (num_result
> 1) {
2221 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2225 /* Check if we need to update an existing entry */
2226 if (num_result
== 1) {
2229 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2230 ldap_op
= LDAP_MOD_REPLACE
;
2231 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2232 tmp
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
2236 dn
= talloc_asprintf(ctx
, "%s", tmp
);
2239 status
= NT_STATUS_NO_MEMORY
;
2243 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2245 /* There might be a SID for this account already - say an idmap entry */
2247 filter
= talloc_asprintf(ctx
,
2248 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2249 get_userattr_key2string(ldap_state
->schema_ver
,
2250 LDAP_ATTR_USER_SID
),
2251 sid_to_string(sid_string
, sid
),
2252 LDAP_OBJ_IDMAP_ENTRY
,
2253 LDAP_OBJ_SID_ENTRY
);
2255 status
= NT_STATUS_NO_MEMORY
;
2259 /* free old result before doing a new search */
2260 if (result
!= NULL
) {
2261 ldap_msgfree(result
);
2264 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2265 filter
, attr_list
, &result
);
2267 if ( rc
!= LDAP_SUCCESS
) {
2271 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2273 if (num_result
> 1) {
2274 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2278 /* Check if we need to update an existing entry */
2279 if (num_result
== 1) {
2282 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2283 ldap_op
= LDAP_MOD_REPLACE
;
2284 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2285 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
2289 dn
= talloc_asprintf(ctx
, "%s", tmp
);
2292 status
= NT_STATUS_NO_MEMORY
;
2298 if (num_result
== 0) {
2299 char *escape_username
;
2300 /* Check if we need to add an entry */
2301 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2302 ldap_op
= LDAP_MOD_ADD
;
2304 escape_username
= escape_rdn_val_string_alloc(username
);
2305 if (!escape_username
) {
2306 status
= NT_STATUS_NO_MEMORY
;
2310 if (username
[strlen(username
)-1] == '$') {
2311 dn
= talloc_asprintf(ctx
,
2314 lp_ldap_machine_suffix());
2316 dn
= talloc_asprintf(ctx
,
2319 lp_ldap_user_suffix());
2322 SAFE_FREE(escape_username
);
2324 status
= NT_STATUS_NO_MEMORY
;
2329 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2330 element_is_set_or_changed
)) {
2331 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2333 ldap_mods_free(mods
, true);
2339 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2342 switch ( ldap_state
->schema_ver
) {
2343 case SCHEMAVER_SAMBAACCOUNT
:
2344 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2346 case SCHEMAVER_SAMBASAMACCOUNT
:
2347 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2350 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2354 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2355 if (!NT_STATUS_IS_OK(ret
)) {
2356 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2357 pdb_get_username(newpwd
),dn
));
2358 ldap_mods_free(mods
, true);
2362 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2363 ldap_mods_free(mods
, true);
2365 status
= NT_STATUS_OK
;
2370 SAFE_FREE(escape_user
);
2372 ldap_msgfree(result
);
2377 /**********************************************************************
2378 *********************************************************************/
2380 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2382 LDAPMessage
** result
)
2384 int scope
= LDAP_SCOPE_SUBTREE
;
2386 const char **attr_list
;
2388 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2389 rc
= smbldap_search(ldap_state
->smbldap_state
,
2390 lp_ldap_group_suffix (), scope
,
2391 filter
, attr_list
, 0, result
);
2392 TALLOC_FREE(attr_list
);
2397 /**********************************************************************
2398 *********************************************************************/
2400 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2401 GROUP_MAP
*map
, LDAPMessage
*entry
)
2404 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2406 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2407 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2408 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2413 temp
= smbldap_talloc_single_attribute(
2414 ldap_state
->smbldap_state
->ldap_struct
,
2416 get_attr_key2string(groupmap_attr_list
,
2417 LDAP_ATTR_GIDNUMBER
),
2420 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2421 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2425 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2427 map
->gid
= (gid_t
)atol(temp
);
2430 temp
= smbldap_talloc_single_attribute(
2431 ldap_state
->smbldap_state
->ldap_struct
,
2433 get_attr_key2string(groupmap_attr_list
,
2434 LDAP_ATTR_GROUP_SID
),
2437 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2438 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2443 if (!string_to_sid(&map
->sid
, temp
)) {
2444 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2450 temp
= smbldap_talloc_single_attribute(
2451 ldap_state
->smbldap_state
->ldap_struct
,
2453 get_attr_key2string(groupmap_attr_list
,
2454 LDAP_ATTR_GROUP_TYPE
),
2457 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2458 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2462 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2464 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2465 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2466 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2472 temp
= smbldap_talloc_single_attribute(
2473 ldap_state
->smbldap_state
->ldap_struct
,
2475 get_attr_key2string(groupmap_attr_list
,
2476 LDAP_ATTR_DISPLAY_NAME
),
2479 temp
= smbldap_talloc_single_attribute(
2480 ldap_state
->smbldap_state
->ldap_struct
,
2482 get_attr_key2string(groupmap_attr_list
,
2486 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2487 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2492 fstrcpy(map
->nt_name
, temp
);
2495 temp
= smbldap_talloc_single_attribute(
2496 ldap_state
->smbldap_state
->ldap_struct
,
2498 get_attr_key2string(groupmap_attr_list
,
2502 temp
= talloc_strdup(ctx
, "");
2508 fstrcpy(map
->comment
, temp
);
2510 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2511 store_gid_sid_cache(&map
->sid
, map
->gid
);
2518 /**********************************************************************
2519 *********************************************************************/
2521 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2525 struct ldapsam_privates
*ldap_state
=
2526 (struct ldapsam_privates
*)methods
->private_data
;
2527 LDAPMessage
*result
= NULL
;
2528 LDAPMessage
*entry
= NULL
;
2531 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2533 return NT_STATUS_NO_SUCH_GROUP
;
2536 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2539 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2541 ldap_msgfree(result
);
2542 return NT_STATUS_NO_SUCH_GROUP
;
2546 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2547 "count=%d\n", filter
, count
));
2548 ldap_msgfree(result
);
2549 return NT_STATUS_NO_SUCH_GROUP
;
2552 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2555 ldap_msgfree(result
);
2556 return NT_STATUS_UNSUCCESSFUL
;
2559 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2560 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2561 "group filter %s\n", filter
));
2562 ldap_msgfree(result
);
2563 return NT_STATUS_NO_SUCH_GROUP
;
2566 ldap_msgfree(result
);
2567 return NT_STATUS_OK
;
2570 /**********************************************************************
2571 *********************************************************************/
2573 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2576 char *filter
= NULL
;
2579 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2581 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2582 sid_string_static(&sid
)) < 0) {
2583 return NT_STATUS_NO_MEMORY
;
2586 status
= ldapsam_getgroup(methods
, filter
, map
);
2591 /**********************************************************************
2592 *********************************************************************/
2594 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2597 char *filter
= NULL
;
2600 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2602 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2603 (unsigned long)gid
) < 0) {
2604 return NT_STATUS_NO_MEMORY
;
2607 status
= ldapsam_getgroup(methods
, filter
, map
);
2612 /**********************************************************************
2613 *********************************************************************/
2615 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2618 char *filter
= NULL
;
2619 char *escape_name
= escape_ldap_string_alloc(name
);
2623 return NT_STATUS_NO_MEMORY
;
2626 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2628 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2629 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2631 SAFE_FREE(escape_name
);
2632 return NT_STATUS_NO_MEMORY
;
2635 SAFE_FREE(escape_name
);
2636 status
= ldapsam_getgroup(methods
, filter
, map
);
2641 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2643 const DOM_SID
*domain_sid
,
2649 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2650 str
, sizeof(str
)-1)) {
2651 DEBUG(10, ("Could not find sambaSID attribute\n"));
2655 if (!string_to_sid(&sid
, str
)) {
2656 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2660 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2661 DEBUG(10, ("SID %s is not in expected domain %s\n",
2662 str
, sid_string_static(domain_sid
)));
2666 if (!sid_peek_rid(&sid
, rid
)) {
2667 DEBUG(10, ("Could not peek into RID\n"));
2674 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2675 TALLOC_CTX
*mem_ctx
,
2676 const DOM_SID
*group
,
2677 uint32
**pp_member_rids
,
2678 size_t *p_num_members
)
2680 struct ldapsam_privates
*ldap_state
=
2681 (struct ldapsam_privates
*)methods
->private_data
;
2682 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2683 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2684 const char *sid_attrs
[] = { "sambaSID", NULL
};
2685 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2686 LDAPMessage
*result
= NULL
;
2689 char **values
= NULL
;
2694 *pp_member_rids
= NULL
;
2697 filter
= talloc_asprintf(mem_ctx
,
2698 "(&(objectClass=%s)"
2701 LDAP_OBJ_POSIXGROUP
,
2703 sid_string_static(group
));
2704 if (filter
== NULL
) {
2705 ret
= NT_STATUS_NO_MEMORY
;
2709 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2710 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2713 if (rc
!= LDAP_SUCCESS
)
2716 talloc_autofree_ldapmsg(mem_ctx
, result
);
2718 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2721 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2722 sid_string_static(group
)));
2723 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2728 ret
= NT_STATUS_NO_SUCH_GROUP
;
2732 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2736 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2738 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2739 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2743 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2747 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2748 if (filter
== NULL
) {
2749 ret
= NT_STATUS_NO_MEMORY
;
2753 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2754 char *escape_memberuid
;
2756 escape_memberuid
= escape_ldap_string_alloc(*memberuid
);
2757 if (escape_memberuid
== NULL
) {
2758 ret
= NT_STATUS_NO_MEMORY
;
2762 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2763 if (filter
== NULL
) {
2764 SAFE_FREE(escape_memberuid
);
2765 ret
= NT_STATUS_NO_MEMORY
;
2769 SAFE_FREE(escape_memberuid
);
2772 filter
= talloc_asprintf_append_buffer(filter
, "))");
2773 if (filter
== NULL
) {
2774 ret
= NT_STATUS_NO_MEMORY
;
2778 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2779 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2782 if (rc
!= LDAP_SUCCESS
)
2785 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2786 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2788 talloc_autofree_ldapmsg(mem_ctx
, result
);
2790 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2792 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2798 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2802 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2803 "the sambaSID attribute\n"));
2804 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2808 if (!string_to_sid(&sid
, sidstr
))
2811 if (!sid_check_is_in_our_domain(&sid
)) {
2812 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2813 "in our domain\n"));
2814 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2818 sid_peek_rid(&sid
, &rid
);
2820 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2822 ret
= NT_STATUS_NO_MEMORY
;
2828 filter
= talloc_asprintf(mem_ctx
,
2829 "(&(objectClass=%s)"
2831 LDAP_OBJ_SAMBASAMACCOUNT
,
2834 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2835 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2838 if (rc
!= LDAP_SUCCESS
)
2841 talloc_autofree_ldapmsg(mem_ctx
, result
);
2843 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2845 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2849 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2851 get_global_sam_sid(),
2853 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2854 "the sambaSID attribute\n"));
2855 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2859 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2861 ret
= NT_STATUS_NO_MEMORY
;
2871 ldap_value_free(values
);
2876 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2877 TALLOC_CTX
*mem_ctx
,
2881 size_t *p_num_groups
)
2883 struct ldapsam_privates
*ldap_state
=
2884 (struct ldapsam_privates
*)methods
->private_data
;
2885 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2887 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2890 LDAPMessage
*result
= NULL
;
2892 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2893 size_t num_sids
, num_gids
;
2895 gid_t primary_gid
= -1;
2900 if (pdb_get_username(user
) == NULL
) {
2901 return NT_STATUS_INVALID_PARAMETER
;
2904 escape_name
= escape_ldap_string_alloc(pdb_get_username(user
));
2905 if (escape_name
== NULL
)
2906 return NT_STATUS_NO_MEMORY
;
2908 /* retrieve the users primary gid */
2909 filter
= talloc_asprintf(mem_ctx
,
2910 "(&(objectClass=%s)(uid=%s))",
2911 LDAP_OBJ_SAMBASAMACCOUNT
,
2913 if (filter
== NULL
) {
2914 ret
= NT_STATUS_NO_MEMORY
;
2918 rc
= smbldap_search(conn
, lp_ldap_user_suffix(),
2919 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2921 if (rc
!= LDAP_SUCCESS
)
2924 talloc_autofree_ldapmsg(mem_ctx
, result
);
2926 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2930 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2931 ret
= NT_STATUS_NO_SUCH_USER
;
2934 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2936 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2938 DEBUG (1, ("Unable to find the member's gid!\n"));
2939 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2942 primary_gid
= strtoul(gidstr
, NULL
, 10);
2945 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2946 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2950 filter
= talloc_asprintf(mem_ctx
,
2951 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
2952 LDAP_OBJ_POSIXGROUP
, escape_name
, primary_gid
);
2953 if (filter
== NULL
) {
2954 ret
= NT_STATUS_NO_MEMORY
;
2958 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2959 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2961 if (rc
!= LDAP_SUCCESS
)
2964 talloc_autofree_ldapmsg(mem_ctx
, result
);
2972 /* We need to add the primary group as the first gid/sid */
2974 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2975 ret
= NT_STATUS_NO_MEMORY
;
2979 /* This sid will be replaced later */
2981 if (!add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
, &num_sids
)) {
2982 ret
= NT_STATUS_NO_MEMORY
;
2986 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2988 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2995 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2997 str
, sizeof(str
)-1))
3000 if (!string_to_sid(&sid
, str
))
3003 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3005 str
, sizeof(str
)-1))
3008 gid
= strtoul(str
, &end
, 10);
3010 if (PTR_DIFF(end
, str
) != strlen(str
))
3013 if (gid
== primary_gid
) {
3014 sid_copy(&(*pp_sids
)[0], &sid
);
3016 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3018 ret
= NT_STATUS_NO_MEMORY
;
3021 if (!add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3023 ret
= NT_STATUS_NO_MEMORY
;
3029 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3030 DEBUG(3, ("primary group of [%s] not found\n",
3031 pdb_get_username(user
)));
3035 *p_num_groups
= num_sids
;
3041 SAFE_FREE(escape_name
);
3045 /**********************************************************************
3046 * Augment a posixGroup object with a sambaGroupMapping domgroup
3047 *********************************************************************/
3049 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3050 struct ldapsam_privates
*ldap_state
,
3053 const char *filter
, *dn
;
3054 LDAPMessage
*msg
, *entry
;
3058 filter
= talloc_asprintf(mem_ctx
,
3059 "(&(objectClass=posixGroup)(gidNumber=%u))",
3061 if (filter
== NULL
) {
3062 return NT_STATUS_NO_MEMORY
;
3065 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3066 get_attr_list(mem_ctx
, groupmap_attr_list
),
3068 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3070 if ((rc
!= LDAP_SUCCESS
) ||
3071 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3072 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3073 return NT_STATUS_NO_SUCH_GROUP
;
3076 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3078 return NT_STATUS_NO_MEMORY
;
3082 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3083 "sambaGroupMapping");
3084 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3085 sid_string_static(&map
->sid
));
3086 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3087 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3088 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3090 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3092 talloc_autofree_ldapmod(mem_ctx
, mods
);
3094 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3095 if (rc
!= LDAP_SUCCESS
) {
3096 return NT_STATUS_ACCESS_DENIED
;
3099 return NT_STATUS_OK
;
3102 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3105 struct ldapsam_privates
*ldap_state
=
3106 (struct ldapsam_privates
*)methods
->private_data
;
3107 LDAPMessage
*msg
= NULL
;
3108 LDAPMod
**mods
= NULL
;
3109 const char *attrs
[] = { NULL
};
3113 TALLOC_CTX
*mem_ctx
;
3120 mem_ctx
= talloc_new(NULL
);
3121 if (mem_ctx
== NULL
) {
3122 DEBUG(0, ("talloc_new failed\n"));
3123 return NT_STATUS_NO_MEMORY
;
3126 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3127 sid_string_static(&map
->sid
));
3128 if (filter
== NULL
) {
3129 result
= NT_STATUS_NO_MEMORY
;
3133 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3134 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3135 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3137 if ((rc
== LDAP_SUCCESS
) &&
3138 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3140 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3141 "group mapping entry\n",
3142 sid_string_static(&map
->sid
)));
3143 result
= NT_STATUS_GROUP_EXISTS
;
3147 switch (map
->sid_name_use
) {
3149 case SID_NAME_DOM_GRP
:
3150 /* To map a domain group we need to have a posix group
3152 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3156 case SID_NAME_ALIAS
:
3157 if (!sid_check_is_in_our_domain(&map
->sid
)
3158 && !sid_check_is_in_builtin(&map
->sid
) )
3160 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3161 sid_string_static(&map
->sid
)));
3162 result
= NT_STATUS_INVALID_PARAMETER
;
3168 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3169 sid_type_lookup(map
->sid_name_use
)));
3170 result
= NT_STATUS_INVALID_PARAMETER
;
3174 /* Domain groups have been mapped in a separate routine, we have to
3175 * create an alias now */
3177 if (map
->gid
== -1) {
3178 DEBUG(10, ("Refusing to map gid==-1\n"));
3179 result
= NT_STATUS_INVALID_PARAMETER
;
3183 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3184 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
3185 "add\n", map
->gid
, sid_string_static(&sid
)));
3186 result
= NT_STATUS_GROUP_EXISTS
;
3190 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3191 * the best we can get out of LDAP. */
3193 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3194 sid_string_static(&map
->sid
),
3195 lp_ldap_group_suffix());
3197 result
= NT_STATUS_NO_MEMORY
;
3203 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3205 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3206 "sambaGroupMapping");
3208 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3209 sid_string_static(&map
->sid
));
3210 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3211 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3212 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3214 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3216 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3217 talloc_asprintf(mem_ctx
, "%u", map
->gid
));
3218 talloc_autofree_ldapmod(mem_ctx
, mods
);
3220 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3222 result
= (rc
== LDAP_SUCCESS
) ?
3223 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3226 TALLOC_FREE(mem_ctx
);
3230 /**********************************************************************
3231 * Update a group mapping entry. We're quite strict about what can be changed:
3232 * Only the description and displayname may be changed. It simply does not
3233 * make any sense to change the SID, gid or the type in a mapping.
3234 *********************************************************************/
3236 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3239 struct ldapsam_privates
*ldap_state
=
3240 (struct ldapsam_privates
*)methods
->private_data
;
3242 const char *filter
, *dn
;
3243 LDAPMessage
*msg
= NULL
;
3244 LDAPMessage
*entry
= NULL
;
3245 LDAPMod
**mods
= NULL
;
3246 TALLOC_CTX
*mem_ctx
;
3249 mem_ctx
= talloc_new(NULL
);
3250 if (mem_ctx
== NULL
) {
3251 DEBUG(0, ("talloc_new failed\n"));
3252 return NT_STATUS_NO_MEMORY
;
3255 /* Make 100% sure that sid, gid and type are not changed by looking up
3256 * exactly the values we're given in LDAP. */
3258 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3259 "(sambaSid=%s)(gidNumber=%u)"
3260 "(sambaGroupType=%d))",
3262 sid_string_static(&map
->sid
), map
->gid
,
3264 if (filter
== NULL
) {
3265 result
= NT_STATUS_NO_MEMORY
;
3269 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3270 get_attr_list(mem_ctx
, groupmap_attr_list
),
3272 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3274 if ((rc
!= LDAP_SUCCESS
) ||
3275 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3276 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3277 result
= NT_STATUS_NO_SUCH_GROUP
;
3281 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3284 result
= NT_STATUS_NO_MEMORY
;
3289 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3291 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3293 talloc_autofree_ldapmod(mem_ctx
, mods
);
3296 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3297 "nothing to do\n"));
3298 result
= NT_STATUS_OK
;
3302 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3304 if (rc
!= LDAP_SUCCESS
) {
3305 result
= NT_STATUS_ACCESS_DENIED
;
3309 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3310 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3312 result
= NT_STATUS_OK
;
3315 TALLOC_FREE(mem_ctx
);
3319 /**********************************************************************
3320 *********************************************************************/
3322 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3325 struct ldapsam_privates
*priv
=
3326 (struct ldapsam_privates
*)methods
->private_data
;
3327 LDAPMessage
*msg
, *entry
;
3330 TALLOC_CTX
*mem_ctx
;
3333 mem_ctx
= talloc_new(NULL
);
3334 if (mem_ctx
== NULL
) {
3335 DEBUG(0, ("talloc_new failed\n"));
3336 return NT_STATUS_NO_MEMORY
;
3339 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3340 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3341 sid_string_static(&sid
));
3342 if (filter
== NULL
) {
3343 result
= NT_STATUS_NO_MEMORY
;
3346 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3347 get_attr_list(mem_ctx
, groupmap_attr_list
),
3349 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3351 if ((rc
!= LDAP_SUCCESS
) ||
3352 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3353 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3354 result
= NT_STATUS_NO_SUCH_GROUP
;
3358 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3359 get_attr_list(mem_ctx
,
3360 groupmap_attr_list_to_delete
));
3362 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3363 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3364 const char *attrs
[] = { "sambaGroupType", "description",
3365 "displayName", "sambaSIDList",
3368 /* Second try. Don't delete the sambaSID attribute, this is
3369 for "old" entries that are tacked on a winbind
3372 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3373 LDAP_OBJ_GROUPMAP
, attrs
);
3376 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3377 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3378 const char *attrs
[] = { "sambaGroupType", "description",
3379 "displayName", "sambaSIDList",
3380 "gidNumber", NULL
};
3382 /* Third try. This is a post-3.0.21 alias (containing only
3383 * sambaSidEntry and sambaGroupMapping classes), we also have
3384 * to delete the gidNumber attribute, only the sambaSidEntry
3387 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3388 LDAP_OBJ_GROUPMAP
, attrs
);
3391 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3394 TALLOC_FREE(mem_ctx
);
3398 /**********************************************************************
3399 *********************************************************************/
3401 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3404 struct ldapsam_privates
*ldap_state
=
3405 (struct ldapsam_privates
*)my_methods
->private_data
;
3408 const char **attr_list
;
3410 pstr_sprintf( filter
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3411 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3412 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3413 LDAP_SCOPE_SUBTREE
, filter
,
3414 attr_list
, 0, &ldap_state
->result
);
3415 TALLOC_FREE(attr_list
);
3417 if (rc
!= LDAP_SUCCESS
) {
3418 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3419 ldap_err2string(rc
)));
3420 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3421 lp_ldap_group_suffix(), filter
));
3422 ldap_msgfree(ldap_state
->result
);
3423 ldap_state
->result
= NULL
;
3424 return NT_STATUS_UNSUCCESSFUL
;
3427 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3428 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3429 ldap_state
->result
)));
3432 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3433 ldap_state
->result
);
3434 ldap_state
->index
= 0;
3436 return NT_STATUS_OK
;
3439 /**********************************************************************
3440 *********************************************************************/
3442 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3444 ldapsam_endsampwent(my_methods
);
3447 /**********************************************************************
3448 *********************************************************************/
3450 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3453 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3454 struct ldapsam_privates
*ldap_state
=
3455 (struct ldapsam_privates
*)my_methods
->private_data
;
3459 if (!ldap_state
->entry
)
3462 ldap_state
->index
++;
3463 bret
= init_group_from_ldap(ldap_state
, map
,
3467 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3471 return NT_STATUS_OK
;
3474 /**********************************************************************
3475 *********************************************************************/
3477 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3478 const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
,
3479 GROUP_MAP
**pp_rmap
,
3480 size_t *p_num_entries
,
3489 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3490 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3492 return NT_STATUS_ACCESS_DENIED
;
3495 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3496 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3497 sid_name_use
!= map
.sid_name_use
) {
3498 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3499 "not of the requested type\n", map
.nt_name
));
3502 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3503 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3504 "non mapped\n", map
.nt_name
));
3508 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3510 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3511 "enlarge group map!\n"));
3512 return NT_STATUS_UNSUCCESSFUL
;
3515 (*pp_rmap
)[entries
] = map
;
3520 ldapsam_endsamgrent(methods
);
3522 *p_num_entries
= entries
;
3524 return NT_STATUS_OK
;
3527 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3528 const DOM_SID
*alias
,
3529 const DOM_SID
*member
,
3532 struct ldapsam_privates
*ldap_state
=
3533 (struct ldapsam_privates
*)methods
->private_data
;
3535 LDAPMessage
*result
= NULL
;
3536 LDAPMessage
*entry
= NULL
;
3538 LDAPMod
**mods
= NULL
;
3540 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3542 char *filter
= NULL
;
3544 if (sid_check_is_in_builtin(alias
)) {
3545 type
= SID_NAME_ALIAS
;
3548 if (sid_check_is_in_our_domain(alias
)) {
3549 type
= SID_NAME_ALIAS
;
3552 if (type
== SID_NAME_USE_NONE
) {
3553 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3554 sid_string_static(alias
)));
3555 return NT_STATUS_NO_SUCH_ALIAS
;
3558 if (asprintf(&filter
,
3559 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3560 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3562 return NT_STATUS_NO_MEMORY
;
3565 if (ldapsam_search_one_group(ldap_state
, filter
,
3566 &result
) != LDAP_SUCCESS
) {
3568 return NT_STATUS_NO_SUCH_ALIAS
;
3571 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3575 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3576 ldap_msgfree(result
);
3578 return NT_STATUS_NO_SUCH_ALIAS
;
3582 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3583 "filter %s: count=%d\n", filter
, count
));
3584 ldap_msgfree(result
);
3586 return NT_STATUS_NO_SUCH_ALIAS
;
3591 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3595 ldap_msgfree(result
);
3596 return NT_STATUS_UNSUCCESSFUL
;
3599 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
3601 ldap_msgfree(result
);
3602 return NT_STATUS_UNSUCCESSFUL
;
3605 smbldap_set_mod(&mods
, modop
,
3606 get_attr_key2string(groupmap_attr_list
,
3607 LDAP_ATTR_SID_LIST
),
3608 sid_string_static(member
));
3610 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3612 ldap_mods_free(mods
, True
);
3613 ldap_msgfree(result
);
3616 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3617 return NT_STATUS_MEMBER_IN_ALIAS
;
3620 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3621 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3624 if (rc
!= LDAP_SUCCESS
) {
3625 return NT_STATUS_UNSUCCESSFUL
;
3628 return NT_STATUS_OK
;
3631 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3632 const DOM_SID
*alias
,
3633 const DOM_SID
*member
)
3635 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3638 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3639 const DOM_SID
*alias
,
3640 const DOM_SID
*member
)
3642 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3646 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3647 const DOM_SID
*alias
,
3648 DOM_SID
**pp_members
,
3649 size_t *p_num_members
)
3651 struct ldapsam_privates
*ldap_state
=
3652 (struct ldapsam_privates
*)methods
->private_data
;
3653 LDAPMessage
*result
= NULL
;
3654 LDAPMessage
*entry
= NULL
;
3656 char **values
= NULL
;
3658 char *filter
= NULL
;
3659 size_t num_members
= 0;
3660 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3665 if (sid_check_is_in_builtin(alias
)) {
3666 type
= SID_NAME_ALIAS
;
3669 if (sid_check_is_in_our_domain(alias
)) {
3670 type
= SID_NAME_ALIAS
;
3673 if (type
== SID_NAME_USE_NONE
) {
3674 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3675 sid_string_static(alias
)));
3676 return NT_STATUS_NO_SUCH_ALIAS
;
3679 if (asprintf(&filter
,
3680 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3681 LDAP_OBJ_GROUPMAP
, sid_string_static(alias
),
3683 return NT_STATUS_NO_MEMORY
;
3686 if (ldapsam_search_one_group(ldap_state
, filter
,
3687 &result
) != LDAP_SUCCESS
) {
3689 return NT_STATUS_NO_SUCH_ALIAS
;
3692 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3696 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3697 ldap_msgfree(result
);
3699 return NT_STATUS_NO_SUCH_ALIAS
;
3703 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3704 "filter %s: count=%d\n", filter
, count
));
3705 ldap_msgfree(result
);
3707 return NT_STATUS_NO_SUCH_ALIAS
;
3712 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3716 ldap_msgfree(result
);
3717 return NT_STATUS_UNSUCCESSFUL
;
3720 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3722 get_attr_key2string(groupmap_attr_list
,
3723 LDAP_ATTR_SID_LIST
));
3725 if (values
== NULL
) {
3726 ldap_msgfree(result
);
3727 return NT_STATUS_OK
;
3730 count
= ldap_count_values(values
);
3732 for (i
=0; i
<count
; i
++) {
3735 if (!string_to_sid(&member
, values
[i
]))
3738 if (!add_sid_to_array(NULL
, &member
, pp_members
, &num_members
)) {
3739 ldap_value_free(values
);
3740 ldap_msgfree(result
);
3741 return NT_STATUS_NO_MEMORY
;
3745 *p_num_members
= num_members
;
3746 ldap_value_free(values
);
3747 ldap_msgfree(result
);
3749 return NT_STATUS_OK
;
3752 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3753 TALLOC_CTX
*mem_ctx
,
3754 const DOM_SID
*domain_sid
,
3755 const DOM_SID
*members
,
3757 uint32
**pp_alias_rids
,
3758 size_t *p_num_alias_rids
)
3760 struct ldapsam_privates
*ldap_state
=
3761 (struct ldapsam_privates
*)methods
->private_data
;
3764 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3766 LDAPMessage
*result
= NULL
;
3767 LDAPMessage
*entry
= NULL
;
3771 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3773 if (sid_check_is_builtin(domain_sid
)) {
3774 type
= SID_NAME_ALIAS
;
3777 if (sid_check_is_domain(domain_sid
)) {
3778 type
= SID_NAME_ALIAS
;
3781 if (type
== SID_NAME_USE_NONE
) {
3782 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3783 sid_string_static(domain_sid
)));
3784 return NT_STATUS_UNSUCCESSFUL
;
3787 filter
= talloc_asprintf(mem_ctx
,
3788 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3789 LDAP_OBJ_GROUPMAP
, type
);
3791 for (i
=0; i
<num_members
; i
++)
3792 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3794 sid_string_static(&members
[i
]));
3796 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3798 if (filter
== NULL
) {
3799 return NT_STATUS_NO_MEMORY
;
3802 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3803 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3805 if (rc
!= LDAP_SUCCESS
)
3806 return NT_STATUS_UNSUCCESSFUL
;
3808 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3810 for (entry
= ldap_first_entry(ldap_struct
, result
);
3812 entry
= ldap_next_entry(ldap_struct
, entry
))
3818 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3824 if (!string_to_sid(&sid
, sid_str
))
3827 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3830 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3831 p_num_alias_rids
)) {
3832 ldap_msgfree(result
);
3833 return NT_STATUS_NO_MEMORY
;
3837 ldap_msgfree(result
);
3838 return NT_STATUS_OK
;
3841 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3845 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3847 LDAPMod
**mods
= NULL
;
3848 fstring value_string
;
3849 const char *policy_attr
= NULL
;
3851 struct ldapsam_privates
*ldap_state
=
3852 (struct ldapsam_privates
*)methods
->private_data
;
3854 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3856 if (!ldap_state
->domain_dn
) {
3857 return NT_STATUS_INVALID_PARAMETER
;
3860 policy_attr
= get_account_policy_attr(policy_index
);
3861 if (policy_attr
== NULL
) {
3862 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3867 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3869 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3871 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3874 ldap_mods_free(mods
, True
);
3876 if (rc
!= LDAP_SUCCESS
) {
3880 if (!cache_account_policy_set(policy_index
, value
)) {
3881 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3882 "update local tdb cache\n"));
3886 return NT_STATUS_OK
;
3889 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3890 int policy_index
, uint32 value
)
3892 return ldapsam_set_account_policy_in_ldap(methods
, policy_index
,
3896 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3900 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3901 LDAPMessage
*result
= NULL
;
3902 LDAPMessage
*entry
= NULL
;
3906 const char *policy_attr
= NULL
;
3908 struct ldapsam_privates
*ldap_state
=
3909 (struct ldapsam_privates
*)methods
->private_data
;
3911 const char *attrs
[2];
3913 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3915 if (!ldap_state
->domain_dn
) {
3916 return NT_STATUS_INVALID_PARAMETER
;
3919 policy_attr
= get_account_policy_attr(policy_index
);
3921 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3922 "policy index: %d\n", policy_index
));
3926 attrs
[0] = policy_attr
;
3929 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3930 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0,
3933 if (rc
!= LDAP_SUCCESS
) {
3937 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3942 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3943 if (entry
== NULL
) {
3947 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3952 *value
= (uint32
)atol(vals
[0]);
3954 ntstatus
= NT_STATUS_OK
;
3958 ldap_value_free(vals
);
3959 ldap_msgfree(result
);
3964 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3966 - if user hasn't decided to use account policies inside LDAP just reuse the
3969 - if there is a valid cache entry, return that
3970 - if there is an LDAP entry, update cache and return
3971 - otherwise set to default, update cache and return
3975 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3976 int policy_index
, uint32
*value
)
3978 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3980 if (cache_account_policy_get(policy_index
, value
)) {
3981 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3983 return NT_STATUS_OK
;
3986 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, policy_index
,
3988 if (NT_STATUS_IS_OK(ntstatus
)) {
3992 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3996 /* should we automagically migrate old tdb value here ? */
3997 if (account_policy_get(policy_index
, value
))
4000 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4001 "default\n", policy_index
));
4004 if (!account_policy_get_default(policy_index
, value
)) {
4010 ntstatus
= ldapsam_set_account_policy(methods
, policy_index
, *value
);
4011 if (!NT_STATUS_IS_OK(ntstatus
)) {
4017 if (!cache_account_policy_set(policy_index
, *value
)) {
4018 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4019 "tdb as a cache\n"));
4020 return NT_STATUS_UNSUCCESSFUL
;
4023 return NT_STATUS_OK
;
4026 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4027 const DOM_SID
*domain_sid
,
4031 enum lsa_SidType
*attrs
)
4033 struct ldapsam_privates
*ldap_state
=
4034 (struct ldapsam_privates
*)methods
->private_data
;
4035 LDAPMessage
*msg
= NULL
;
4037 char *allsids
= NULL
;
4038 int i
, rc
, num_mapped
;
4039 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4040 TALLOC_CTX
*mem_ctx
;
4044 mem_ctx
= talloc_new(NULL
);
4045 if (mem_ctx
== NULL
) {
4046 DEBUG(0, ("talloc_new failed\n"));
4050 if (!sid_check_is_builtin(domain_sid
) &&
4051 !sid_check_is_domain(domain_sid
)) {
4052 result
= NT_STATUS_INVALID_PARAMETER
;
4056 for (i
=0; i
<num_rids
; i
++)
4057 attrs
[i
] = SID_NAME_UNKNOWN
;
4059 allsids
= talloc_strdup(mem_ctx
, "");
4060 if (allsids
== NULL
) {
4064 for (i
=0; i
<num_rids
; i
++) {
4066 sid_compose(&sid
, domain_sid
, rids
[i
]);
4067 allsids
= talloc_asprintf_append_buffer(allsids
, "(sambaSid=%s)",
4068 sid_string_static(&sid
));
4069 if (allsids
== NULL
) {
4074 /* First look for users */
4078 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4080 filter
= talloc_asprintf(
4081 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4082 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4084 if (filter
== NULL
) {
4088 rc
= smbldap_search(ldap_state
->smbldap_state
,
4089 lp_ldap_user_suffix(),
4090 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4092 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4095 if (rc
!= LDAP_SUCCESS
)
4098 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4101 for (entry
= ldap_first_entry(ld
, msg
);
4103 entry
= ldap_next_entry(ld
, entry
)) {
4108 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4110 DEBUG(2, ("Could not find sid from ldap entry\n"));
4114 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4117 DEBUG(2, ("Could not retrieve uid attribute\n"));
4121 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4122 if (rid
== rids
[rid_index
])
4126 if (rid_index
== num_rids
) {
4127 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4131 attrs
[rid_index
] = SID_NAME_USER
;
4132 names
[rid_index
] = name
;
4136 if (num_mapped
== num_rids
) {
4137 /* No need to look for groups anymore -- we're done */
4138 result
= NT_STATUS_OK
;
4142 /* Same game for groups */
4146 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4147 "sambaGroupType", NULL
};
4149 filter
= talloc_asprintf(
4150 mem_ctx
, "(&(objectClass=%s)(|%s))",
4151 LDAP_OBJ_GROUPMAP
, allsids
);
4152 if (filter
== NULL
) {
4156 rc
= smbldap_search(ldap_state
->smbldap_state
,
4157 lp_ldap_group_suffix(),
4158 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4160 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4163 if (rc
!= LDAP_SUCCESS
)
4166 /* ldap_struct might have changed due to a reconnect */
4168 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4170 /* For consistency checks, we already checked we're only domain or builtin */
4172 is_builtin
= sid_check_is_builtin(domain_sid
);
4174 for (entry
= ldap_first_entry(ld
, msg
);
4176 entry
= ldap_next_entry(ld
, entry
))
4181 enum lsa_SidType type
;
4182 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4184 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4187 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4192 type
= (enum lsa_SidType
)atol(attr
);
4194 /* Consistency checks */
4195 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4196 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4197 (type
!= SID_NAME_DOM_GRP
)))) {
4198 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4201 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4203 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4207 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4210 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4212 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4216 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4221 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4222 if (rid
== rids
[rid_index
])
4226 if (rid_index
== num_rids
) {
4227 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4231 attrs
[rid_index
] = type
;
4232 names
[rid_index
] = attr
;
4236 result
= NT_STATUS_NONE_MAPPED
;
4239 result
= (num_mapped
== num_rids
) ?
4240 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4242 TALLOC_FREE(mem_ctx
);
4246 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4248 char *filter
= NULL
;
4249 char *escaped
= NULL
;
4250 char *result
= NULL
;
4252 asprintf(&filter
, "(&%s(objectclass=sambaSamAccount))",
4254 if (filter
== NULL
) goto done
;
4256 escaped
= escape_ldap_string_alloc(username
);
4257 if (escaped
== NULL
) goto done
;
4259 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4268 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4272 const char **result
;
4274 va_start(ap
, mem_ctx
);
4275 while (va_arg(ap
, const char *) != NULL
)
4279 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4283 va_start(ap
, mem_ctx
);
4284 for (i
=0; i
<num
; i
++) {
4285 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4286 if (result
[i
] == NULL
) {
4287 talloc_free(result
);
4297 struct ldap_search_state
{
4298 struct smbldap_state
*connection
;
4308 void *pagedresults_cookie
;
4310 LDAPMessage
*entries
, *current_entry
;
4311 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4312 TALLOC_CTX
*mem_ctx
,
4313 LDAP
*ld
, LDAPMessage
*entry
,
4314 struct samr_displayentry
*result
);
4317 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4319 struct ldap_search_state
*state
=
4320 (struct ldap_search_state
*)search
->private_data
;
4322 int rc
= LDAP_OPERATIONS_ERROR
;
4324 state
->entries
= NULL
;
4326 if (state
->connection
->paged_results
) {
4327 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4328 state
->scope
, state
->filter
,
4329 state
->attrs
, state
->attrsonly
,
4330 lp_ldap_page_size(), &state
->entries
,
4331 &state
->pagedresults_cookie
);
4334 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4336 if (state
->entries
!= NULL
) {
4337 /* Left over from unsuccessful paged attempt */
4338 ldap_msgfree(state
->entries
);
4339 state
->entries
= NULL
;
4342 rc
= smbldap_search(state
->connection
, state
->base
,
4343 state
->scope
, state
->filter
, state
->attrs
,
4344 state
->attrsonly
, &state
->entries
);
4346 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4349 /* Ok, the server was lying. It told us it could do paged
4350 * searches when it could not. */
4351 state
->connection
->paged_results
= False
;
4354 ld
= state
->connection
->ldap_struct
;
4356 DEBUG(5, ("Don't have an LDAP connection right after a "
4360 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4362 if (state
->current_entry
== NULL
) {
4363 ldap_msgfree(state
->entries
);
4364 state
->entries
= NULL
;
4370 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4372 struct ldap_search_state
*state
=
4373 (struct ldap_search_state
*)search
->private_data
;
4376 if (!state
->connection
->paged_results
) {
4377 /* There is no next page when there are no paged results */
4381 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4382 state
->scope
, state
->filter
, state
->attrs
,
4383 state
->attrsonly
, lp_ldap_page_size(),
4385 &state
->pagedresults_cookie
);
4387 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4390 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4392 if (state
->current_entry
== NULL
) {
4393 ldap_msgfree(state
->entries
);
4394 state
->entries
= NULL
;
4400 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4401 struct samr_displayentry
*entry
)
4403 struct ldap_search_state
*state
=
4404 (struct ldap_search_state
*)search
->private_data
;
4408 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4411 if ((state
->entries
== NULL
) &&
4412 !ldapsam_search_nextpage(search
))
4415 result
= state
->ldap2displayentry(state
, search
->mem_ctx
, state
->connection
->ldap_struct
,
4416 state
->current_entry
, entry
);
4420 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4421 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4422 if (dn
!= NULL
) ldap_memfree(dn
);
4425 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4427 if (state
->current_entry
== NULL
) {
4428 ldap_msgfree(state
->entries
);
4429 state
->entries
= NULL
;
4432 if (!result
) goto retry
;
4437 static void ldapsam_search_end(struct pdb_search
*search
)
4439 struct ldap_search_state
*state
=
4440 (struct ldap_search_state
*)search
->private_data
;
4443 if (state
->pagedresults_cookie
== NULL
)
4446 if (state
->entries
!= NULL
)
4447 ldap_msgfree(state
->entries
);
4449 state
->entries
= NULL
;
4450 state
->current_entry
= NULL
;
4452 if (!state
->connection
->paged_results
)
4455 /* Tell the LDAP server we're not interested in the rest anymore. */
4457 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4458 state
->filter
, state
->attrs
,
4459 state
->attrsonly
, 0, &state
->entries
,
4460 &state
->pagedresults_cookie
);
4462 if (rc
!= LDAP_SUCCESS
)
4463 DEBUG(5, ("Could not end search properly\n"));
4468 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4469 TALLOC_CTX
*mem_ctx
,
4470 LDAP
*ld
, LDAPMessage
*entry
,
4471 struct samr_displayentry
*result
)
4477 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4478 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4479 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4482 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4483 ldap_value_free(vals
);
4485 if ((state
->acct_flags
!= 0) &&
4486 ((state
->acct_flags
& acct_flags
) == 0))
4489 result
->acct_flags
= acct_flags
;
4490 result
->account_name
= "";
4491 result
->fullname
= "";
4492 result
->description
= "";
4494 vals
= ldap_get_values(ld
, entry
, "uid");
4495 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4496 DEBUG(5, ("\"uid\" not found\n"));
4499 pull_utf8_talloc(mem_ctx
,
4500 CONST_DISCARD(char **, &result
->account_name
),
4502 ldap_value_free(vals
);
4504 vals
= ldap_get_values(ld
, entry
, "displayName");
4505 if ((vals
== NULL
) || (vals
[0] == NULL
))
4506 DEBUG(8, ("\"displayName\" not found\n"));
4508 pull_utf8_talloc(mem_ctx
,
4509 CONST_DISCARD(char **, &result
->fullname
),
4511 ldap_value_free(vals
);
4513 vals
= ldap_get_values(ld
, entry
, "description");
4514 if ((vals
== NULL
) || (vals
[0] == NULL
))
4515 DEBUG(8, ("\"description\" not found\n"));
4517 pull_utf8_talloc(mem_ctx
,
4518 CONST_DISCARD(char **, &result
->description
),
4520 ldap_value_free(vals
);
4522 if ((result
->account_name
== NULL
) ||
4523 (result
->fullname
== NULL
) ||
4524 (result
->description
== NULL
)) {
4525 DEBUG(0, ("talloc failed\n"));
4529 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4530 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4531 DEBUG(0, ("\"objectSid\" not found\n"));
4535 if (!string_to_sid(&sid
, vals
[0])) {
4536 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4537 ldap_value_free(vals
);
4540 ldap_value_free(vals
);
4542 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4543 DEBUG(0, ("sid %s does not belong to our domain\n",
4544 sid_string_static(&sid
)));
4552 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4553 struct pdb_search
*search
,
4556 struct ldapsam_privates
*ldap_state
=
4557 (struct ldapsam_privates
*)methods
->private_data
;
4558 struct ldap_search_state
*state
;
4560 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4561 if (state
== NULL
) {
4562 DEBUG(0, ("talloc failed\n"));
4566 state
->connection
= ldap_state
->smbldap_state
;
4568 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4569 state
->base
= lp_ldap_user_suffix();
4570 else if ((acct_flags
!= 0) &&
4571 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4572 state
->base
= lp_ldap_machine_suffix();
4574 state
->base
= lp_ldap_suffix();
4576 state
->acct_flags
= acct_flags
;
4577 state
->base
= talloc_strdup(search
->mem_ctx
, state
->base
);
4578 state
->scope
= LDAP_SCOPE_SUBTREE
;
4579 state
->filter
= get_ldap_filter(search
->mem_ctx
, "*");
4580 state
->attrs
= talloc_attrs(search
->mem_ctx
, "uid", "sambaSid",
4581 "displayName", "description",
4582 "sambaAcctFlags", NULL
);
4583 state
->attrsonly
= 0;
4584 state
->pagedresults_cookie
= NULL
;
4585 state
->entries
= NULL
;
4586 state
->ldap2displayentry
= ldapuser2displayentry
;
4588 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4589 DEBUG(0, ("talloc failed\n"));
4593 search
->private_data
= state
;
4594 search
->next_entry
= ldapsam_search_next_entry
;
4595 search
->search_end
= ldapsam_search_end
;
4597 return ldapsam_search_firstpage(search
);
4600 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4601 TALLOC_CTX
*mem_ctx
,
4602 LDAP
*ld
, LDAPMessage
*entry
,
4603 struct samr_displayentry
*result
)
4609 result
->account_name
= "";
4610 result
->fullname
= "";
4611 result
->description
= "";
4614 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4615 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4616 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4618 ldap_value_free(vals
);
4623 group_type
= atoi(vals
[0]);
4625 if ((state
->group_type
!= 0) &&
4626 ((state
->group_type
!= group_type
))) {
4627 ldap_value_free(vals
);
4631 ldap_value_free(vals
);
4633 /* display name is the NT group name */
4635 vals
= ldap_get_values(ld
, entry
, "displayName");
4636 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4637 DEBUG(8, ("\"displayName\" not found\n"));
4639 /* fallback to the 'cn' attribute */
4640 vals
= ldap_get_values(ld
, entry
, "cn");
4641 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4642 DEBUG(5, ("\"cn\" not found\n"));
4645 pull_utf8_talloc(mem_ctx
,
4646 CONST_DISCARD(char **, &result
->account_name
),
4650 pull_utf8_talloc(mem_ctx
,
4651 CONST_DISCARD(char **, &result
->account_name
),
4655 ldap_value_free(vals
);
4657 vals
= ldap_get_values(ld
, entry
, "description");
4658 if ((vals
== NULL
) || (vals
[0] == NULL
))
4659 DEBUG(8, ("\"description\" not found\n"));
4661 pull_utf8_talloc(mem_ctx
,
4662 CONST_DISCARD(char **, &result
->description
),
4664 ldap_value_free(vals
);
4666 if ((result
->account_name
== NULL
) ||
4667 (result
->fullname
== NULL
) ||
4668 (result
->description
== NULL
)) {
4669 DEBUG(0, ("talloc failed\n"));
4673 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4674 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4675 DEBUG(0, ("\"objectSid\" not found\n"));
4677 ldap_value_free(vals
);
4682 if (!string_to_sid(&sid
, vals
[0])) {
4683 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4687 ldap_value_free(vals
);
4689 switch (group_type
) {
4690 case SID_NAME_DOM_GRP
:
4691 case SID_NAME_ALIAS
:
4693 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4694 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4696 DEBUG(0, ("%s is not in our domain\n",
4697 sid_string_static(&sid
)));
4703 DEBUG(0,("unkown group type: %d\n", group_type
));
4710 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4711 struct pdb_search
*search
,
4713 enum lsa_SidType type
)
4715 struct ldapsam_privates
*ldap_state
=
4716 (struct ldapsam_privates
*)methods
->private_data
;
4717 struct ldap_search_state
*state
;
4719 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4720 if (state
== NULL
) {
4721 DEBUG(0, ("talloc failed\n"));
4725 state
->connection
= ldap_state
->smbldap_state
;
4727 state
->base
= talloc_strdup(search
->mem_ctx
, lp_ldap_group_suffix());
4728 state
->connection
= ldap_state
->smbldap_state
;
4729 state
->scope
= LDAP_SCOPE_SUBTREE
;
4730 state
->filter
= talloc_asprintf(search
->mem_ctx
,
4731 "(&(objectclass=sambaGroupMapping)"
4732 "(sambaGroupType=%d)(sambaSID=%s*))",
4733 type
, sid_string_static(sid
));
4734 state
->attrs
= talloc_attrs(search
->mem_ctx
, "cn", "sambaSid",
4735 "displayName", "description",
4736 "sambaGroupType", NULL
);
4737 state
->attrsonly
= 0;
4738 state
->pagedresults_cookie
= NULL
;
4739 state
->entries
= NULL
;
4740 state
->group_type
= type
;
4741 state
->ldap2displayentry
= ldapgroup2displayentry
;
4743 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4744 DEBUG(0, ("talloc failed\n"));
4748 search
->private_data
= state
;
4749 search
->next_entry
= ldapsam_search_next_entry
;
4750 search
->search_end
= ldapsam_search_end
;
4752 return ldapsam_search_firstpage(search
);
4755 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4756 struct pdb_search
*search
)
4758 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4761 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4762 struct pdb_search
*search
,
4765 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4768 static bool ldapsam_rid_algorithm(struct pdb_methods
*methods
)
4773 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4776 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4778 LDAPMessage
*result
= NULL
;
4779 LDAPMessage
*entry
= NULL
;
4780 LDAPMod
**mods
= NULL
;
4787 TALLOC_CTX
*mem_ctx
;
4789 mem_ctx
= talloc_new(NULL
);
4790 if (mem_ctx
== NULL
) {
4791 DEBUG(0, ("talloc_new failed\n"));
4792 return NT_STATUS_NO_MEMORY
;
4795 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4796 get_global_sam_name(), False
);
4797 if (!NT_STATUS_IS_OK(status
)) {
4798 DEBUG(3, ("Could not get domain info: %s\n",
4799 nt_errstr(status
)));
4803 talloc_autofree_ldapmsg(mem_ctx
, result
);
4805 entry
= ldap_first_entry(priv2ld(priv
), result
);
4806 if (entry
== NULL
) {
4807 DEBUG(0, ("Could not get domain info entry\n"));
4808 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4812 /* Find the largest of the three attributes "sambaNextRid",
4813 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4814 concept of differentiating between user and group rids, and will
4815 use only "sambaNextRid" in the future. But for compatibility
4816 reasons I look if others have chosen different strategies -- VL */
4818 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4819 "sambaNextRid", mem_ctx
);
4820 if (value
!= NULL
) {
4821 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4822 nextRid
= MAX(nextRid
, tmp
);
4825 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4826 "sambaNextUserRid", mem_ctx
);
4827 if (value
!= NULL
) {
4828 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4829 nextRid
= MAX(nextRid
, tmp
);
4832 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4833 "sambaNextGroupRid", mem_ctx
);
4834 if (value
!= NULL
) {
4835 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4836 nextRid
= MAX(nextRid
, tmp
);
4840 nextRid
= BASE_RID
-1;
4845 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4846 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4847 talloc_autofree_ldapmod(mem_ctx
, mods
);
4849 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4850 status
= NT_STATUS_NO_MEMORY
;
4854 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4856 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4859 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4862 if (NT_STATUS_IS_OK(status
)) {
4866 TALLOC_FREE(mem_ctx
);
4870 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32
*rid
)
4874 for (i
=0; i
<10; i
++) {
4875 NTSTATUS result
= ldapsam_get_new_rid(
4876 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4877 if (NT_STATUS_IS_OK(result
)) {
4881 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4885 /* The ldap update failed (maybe a race condition), retry */
4888 /* Tried 10 times, fail. */
4889 return NT_STATUS_ACCESS_DENIED
;
4892 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
4894 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4895 return NT_STATUS_IS_OK(result
) ? True
: False
;
4898 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4900 union unid_t
*id
, enum lsa_SidType
*type
)
4902 struct ldapsam_privates
*priv
=
4903 (struct ldapsam_privates
*)methods
->private_data
;
4905 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4907 LDAPMessage
*result
= NULL
;
4908 LDAPMessage
*entry
= NULL
;
4913 TALLOC_CTX
*mem_ctx
;
4915 mem_ctx
= talloc_new(NULL
);
4916 if (mem_ctx
== NULL
) {
4917 DEBUG(0, ("talloc_new failed\n"));
4921 filter
= talloc_asprintf(mem_ctx
,
4923 "(|(objectClass=%s)(objectClass=%s)))",
4924 sid_string_static(sid
),
4925 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4926 if (filter
== NULL
) {
4927 DEBUG(5, ("talloc_asprintf failed\n"));
4931 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4933 if (rc
!= LDAP_SUCCESS
) {
4936 talloc_autofree_ldapmsg(mem_ctx
, result
);
4938 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4939 DEBUG(10, ("Got %d entries, expected one\n",
4940 ldap_count_entries(priv2ld(priv
), result
)));
4944 entry
= ldap_first_entry(priv2ld(priv
), result
);
4946 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4947 "sambaGroupType", mem_ctx
);
4949 if (value
!= NULL
) {
4950 const char *gid_str
;
4953 gid_str
= smbldap_talloc_single_attribute(
4954 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4955 if (gid_str
== NULL
) {
4956 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4957 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4962 id
->gid
= strtoul(gid_str
, NULL
, 10);
4963 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
4968 /* It must be a user */
4970 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4971 "uidNumber", mem_ctx
);
4972 if (value
== NULL
) {
4973 DEBUG(1, ("Could not find uidNumber in %s\n",
4974 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4978 id
->uid
= strtoul(value
, NULL
, 10);
4979 *type
= SID_NAME_USER
;
4983 TALLOC_FREE(mem_ctx
);
4988 * The following functions is called only if
4989 * ldapsam:trusted and ldapsam:editposix are
4994 * ldapsam_create_user creates a new
4995 * posixAccount and sambaSamAccount object
4996 * in the ldap users subtree
4998 * The uid is allocated by winbindd.
5001 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5002 TALLOC_CTX
*tmp_ctx
, const char *name
,
5003 uint32 acb_info
, uint32
*rid
)
5005 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5006 LDAPMessage
*entry
= NULL
;
5007 LDAPMessage
*result
= NULL
;
5009 bool is_machine
= False
;
5010 bool add_posix
= False
;
5011 LDAPMod
**mods
= NULL
;
5019 const char *dn
= NULL
;
5027 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5028 acb_info
& ACB_WSTRUST
||
5029 acb_info
& ACB_SVRTRUST
||
5030 acb_info
& ACB_DOMTRUST
) {
5034 username
= escape_ldap_string_alloc(name
);
5035 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5036 username
, LDAP_OBJ_POSIXACCOUNT
);
5037 SAFE_FREE(username
);
5039 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5040 if (rc
!= LDAP_SUCCESS
) {
5041 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5042 return NT_STATUS_UNSUCCESSFUL
;
5044 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5046 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5048 if (num_result
> 1) {
5049 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5050 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5053 if (num_result
== 1) {
5055 /* check if it is just a posix account.
5056 * or if there is a sid attached to this entry
5059 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5061 return NT_STATUS_UNSUCCESSFUL
;
5064 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5066 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5067 return NT_STATUS_USER_EXISTS
;
5070 /* it is just a posix account, retrieve the dn for later use */
5071 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5073 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5074 return NT_STATUS_NO_MEMORY
;
5078 if (num_result
== 0) {
5082 /* Create the basic samu structure and generate the mods for the ldap commit */
5083 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5084 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5088 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5090 user
= samu_new(tmp_ctx
);
5092 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5093 return NT_STATUS_NO_MEMORY
;
5096 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5097 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5098 return NT_STATUS_UNSUCCESSFUL
;
5100 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5101 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5102 return NT_STATUS_UNSUCCESSFUL
;
5105 if (acb_info
& ACB_NORMAL
) {
5106 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5107 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5108 return NT_STATUS_UNSUCCESSFUL
;
5111 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5112 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5113 return NT_STATUS_UNSUCCESSFUL
;
5117 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5118 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5119 return NT_STATUS_UNSUCCESSFUL
;
5123 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5124 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5125 return NT_STATUS_UNSUCCESSFUL
;
5128 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5129 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5130 return NT_STATUS_UNSUCCESSFUL
;
5133 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5134 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5136 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5141 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5143 /* retrieve the Domain Users group gid */
5144 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
5145 !sid_to_gid(&group_sid
, &gid
)) {
5146 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5147 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5150 /* lets allocate a new userid for this user */
5151 if (!winbind_allocate_uid(&uid
)) {
5152 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5153 return NT_STATUS_UNSUCCESSFUL
;
5158 /* TODO: choose a more appropriate default for machines */
5159 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5160 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5162 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5163 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5165 uidstr
= talloc_asprintf(tmp_ctx
, "%d", uid
);
5166 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
5168 escape_name
= escape_rdn_val_string_alloc(name
);
5170 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5171 return NT_STATUS_NO_MEMORY
;
5175 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5177 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5180 SAFE_FREE(escape_name
);
5182 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5183 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5184 return NT_STATUS_NO_MEMORY
;
5187 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5188 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5189 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5190 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5191 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5192 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5193 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5196 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5199 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5201 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5204 if (rc
!= LDAP_SUCCESS
) {
5205 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5206 return NT_STATUS_UNSUCCESSFUL
;
5209 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5211 flush_pwnam_cache();
5213 return NT_STATUS_OK
;
5216 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5218 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5219 LDAPMessage
*result
= NULL
;
5220 LDAPMessage
*entry
= NULL
;
5226 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5228 filter
= talloc_asprintf(tmp_ctx
,
5231 "(objectClass=%s))",
5232 pdb_get_username(sam_acct
),
5233 LDAP_OBJ_POSIXACCOUNT
,
5234 LDAP_OBJ_SAMBASAMACCOUNT
);
5235 if (filter
== NULL
) {
5236 return NT_STATUS_NO_MEMORY
;
5239 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5240 if (rc
!= LDAP_SUCCESS
) {
5241 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5242 return NT_STATUS_UNSUCCESSFUL
;
5244 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5246 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5248 if (num_result
== 0) {
5249 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5250 return NT_STATUS_NO_SUCH_USER
;
5253 if (num_result
> 1) {
5254 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5255 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5258 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5260 return NT_STATUS_UNSUCCESSFUL
;
5263 /* it is just a posix account, retrieve the dn for later use */
5264 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5266 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5267 return NT_STATUS_NO_MEMORY
;
5270 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5271 if (rc
!= LDAP_SUCCESS
) {
5272 return NT_STATUS_UNSUCCESSFUL
;
5275 flush_pwnam_cache();
5277 return NT_STATUS_OK
;
5281 * ldapsam_create_group creates a new
5282 * posixGroup and sambaGroupMapping object
5283 * in the ldap groups subtree
5285 * The gid is allocated by winbindd.
5288 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5289 TALLOC_CTX
*tmp_ctx
,
5293 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5295 LDAPMessage
*entry
= NULL
;
5296 LDAPMessage
*result
= NULL
;
5298 bool is_new_entry
= False
;
5299 LDAPMod
**mods
= NULL
;
5305 const char *dn
= NULL
;
5310 groupname
= escape_ldap_string_alloc(name
);
5311 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5312 groupname
, LDAP_OBJ_POSIXGROUP
);
5313 SAFE_FREE(groupname
);
5315 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5316 if (rc
!= LDAP_SUCCESS
) {
5317 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5318 return NT_STATUS_UNSUCCESSFUL
;
5320 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5322 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5324 if (num_result
> 1) {
5325 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5326 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5329 if (num_result
== 1) {
5331 /* check if it is just a posix group.
5332 * or if there is a sid attached to this entry
5335 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5337 return NT_STATUS_UNSUCCESSFUL
;
5340 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5342 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5343 return NT_STATUS_GROUP_EXISTS
;
5346 /* it is just a posix group, retrieve the gid and the dn for later use */
5347 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5349 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5350 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5353 gid
= strtoul(tmp
, NULL
, 10);
5355 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5357 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5358 return NT_STATUS_NO_MEMORY
;
5362 if (num_result
== 0) {
5365 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5367 is_new_entry
= True
;
5369 /* lets allocate a new groupid for this group */
5370 if (!winbind_allocate_gid(&gid
)) {
5371 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5372 return NT_STATUS_UNSUCCESSFUL
;
5375 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
5377 escape_name
= escape_rdn_val_string_alloc(name
);
5379 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5380 return NT_STATUS_NO_MEMORY
;
5383 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5385 SAFE_FREE(escape_name
);
5387 if (!gidstr
|| !dn
) {
5388 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5389 return NT_STATUS_NO_MEMORY
;
5392 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5393 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5394 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5397 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5398 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5402 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5404 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_static(&group_sid
));
5405 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5407 if (!groupsidstr
|| !grouptype
) {
5408 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5409 return NT_STATUS_NO_MEMORY
;
5412 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5413 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5414 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5415 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5416 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5419 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5421 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5422 /* This call may fail with rfc2307bis schema */
5423 /* Retry adding a structural class */
5424 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5425 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5429 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5432 if (rc
!= LDAP_SUCCESS
) {
5433 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5434 return NT_STATUS_UNSUCCESSFUL
;
5437 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5439 return NT_STATUS_OK
;
5442 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
5444 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5445 LDAPMessage
*result
= NULL
;
5446 LDAPMessage
*entry
= NULL
;
5454 /* get the group sid */
5455 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5457 filter
= talloc_asprintf(tmp_ctx
,
5460 "(objectClass=%s))",
5461 sid_string_static(&group_sid
),
5462 LDAP_OBJ_POSIXGROUP
,
5464 if (filter
== NULL
) {
5465 return NT_STATUS_NO_MEMORY
;
5468 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5469 if (rc
!= LDAP_SUCCESS
) {
5470 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5471 return NT_STATUS_UNSUCCESSFUL
;
5473 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5475 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5477 if (num_result
== 0) {
5478 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5479 return NT_STATUS_NO_SUCH_GROUP
;
5482 if (num_result
> 1) {
5483 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5484 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5487 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5489 return NT_STATUS_UNSUCCESSFUL
;
5492 /* here it is, retrieve the dn for later use */
5493 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5495 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5496 return NT_STATUS_NO_MEMORY
;
5499 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5501 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5502 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5505 /* check no user have this group marked as primary group */
5506 filter
= talloc_asprintf(tmp_ctx
,
5509 "(objectClass=%s))",
5511 LDAP_OBJ_POSIXACCOUNT
,
5512 LDAP_OBJ_SAMBASAMACCOUNT
);
5514 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5515 if (rc
!= LDAP_SUCCESS
) {
5516 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5517 return NT_STATUS_UNSUCCESSFUL
;
5519 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5521 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5523 if (num_result
!= 0) {
5524 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5525 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5528 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5529 if (rc
!= LDAP_SUCCESS
) {
5530 return NT_STATUS_UNSUCCESSFUL
;
5533 return NT_STATUS_OK
;
5536 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5537 TALLOC_CTX
*tmp_ctx
,
5542 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5543 LDAPMessage
*entry
= NULL
;
5544 LDAPMessage
*result
= NULL
;
5546 LDAPMod
**mods
= NULL
;
5549 const char *dn
= NULL
;
5556 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5558 case LDAP_MOD_DELETE
:
5559 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5562 return NT_STATUS_UNSUCCESSFUL
;
5565 /* get member sid */
5566 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5568 /* get the group sid */
5569 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5571 filter
= talloc_asprintf(tmp_ctx
,
5574 "(objectClass=%s))",
5575 sid_string_static(&member_sid
),
5576 LDAP_OBJ_POSIXACCOUNT
,
5577 LDAP_OBJ_SAMBASAMACCOUNT
);
5578 if (filter
== NULL
) {
5579 return NT_STATUS_NO_MEMORY
;
5582 /* get the member uid */
5583 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5584 if (rc
!= LDAP_SUCCESS
) {
5585 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5586 return NT_STATUS_UNSUCCESSFUL
;
5588 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5590 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5592 if (num_result
== 0) {
5593 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5594 return NT_STATUS_NO_SUCH_MEMBER
;
5597 if (num_result
> 1) {
5598 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5599 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5602 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5604 return NT_STATUS_UNSUCCESSFUL
;
5607 if (modop
== LDAP_MOD_DELETE
) {
5608 /* check if we are trying to remove the member from his primary group */
5610 gid_t user_gid
, group_gid
;
5612 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5614 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5615 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5618 user_gid
= strtoul(gidstr
, NULL
, 10);
5620 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5621 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5622 return NT_STATUS_UNSUCCESSFUL
;
5625 if (user_gid
== group_gid
) {
5626 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5627 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5631 /* here it is, retrieve the uid for later use */
5632 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5634 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5635 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5638 filter
= talloc_asprintf(tmp_ctx
,
5641 "(objectClass=%s))",
5642 sid_string_static(&group_sid
),
5643 LDAP_OBJ_POSIXGROUP
,
5647 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5648 if (rc
!= LDAP_SUCCESS
) {
5649 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5650 return NT_STATUS_UNSUCCESSFUL
;
5652 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5654 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5656 if (num_result
== 0) {
5657 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5658 return NT_STATUS_NO_SUCH_GROUP
;
5661 if (num_result
> 1) {
5662 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5663 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5666 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5668 return NT_STATUS_UNSUCCESSFUL
;
5671 /* here it is, retrieve the dn for later use */
5672 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5674 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5675 return NT_STATUS_NO_MEMORY
;
5678 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5680 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5682 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5683 if (rc
!= LDAP_SUCCESS
) {
5684 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5685 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5686 return NT_STATUS_MEMBER_IN_GROUP
;
5688 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5689 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5690 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5692 return NT_STATUS_UNSUCCESSFUL
;
5695 return NT_STATUS_OK
;
5698 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5699 TALLOC_CTX
*tmp_ctx
,
5703 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5705 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5706 TALLOC_CTX
*tmp_ctx
,
5710 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5713 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5714 TALLOC_CTX
*mem_ctx
,
5715 struct samu
*sampass
)
5717 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5718 LDAPMessage
*entry
= NULL
;
5719 LDAPMessage
*result
= NULL
;
5721 LDAPMod
**mods
= NULL
;
5723 char *escape_username
;
5725 const char *dn
= NULL
;
5729 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5731 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5732 DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
5733 return NT_STATUS_UNSUCCESSFUL
;
5735 gidstr
= talloc_asprintf(mem_ctx
, "%d", gid
);
5737 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5738 return NT_STATUS_NO_MEMORY
;
5741 escape_username
= escape_ldap_string_alloc(pdb_get_username(sampass
));
5742 if (escape_username
== NULL
) {
5743 return NT_STATUS_NO_MEMORY
;
5746 filter
= talloc_asprintf(mem_ctx
,
5749 "(objectClass=%s))",
5751 LDAP_OBJ_POSIXACCOUNT
,
5752 LDAP_OBJ_SAMBASAMACCOUNT
);
5754 SAFE_FREE(escape_username
);
5756 if (filter
== NULL
) {
5757 return NT_STATUS_NO_MEMORY
;
5760 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5761 if (rc
!= LDAP_SUCCESS
) {
5762 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5763 return NT_STATUS_UNSUCCESSFUL
;
5765 talloc_autofree_ldapmsg(mem_ctx
, result
);
5767 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5769 if (num_result
== 0) {
5770 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5771 return NT_STATUS_NO_SUCH_USER
;
5774 if (num_result
> 1) {
5775 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5776 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5779 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5781 return NT_STATUS_UNSUCCESSFUL
;
5784 /* retrieve the dn for later use */
5785 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5787 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5788 return NT_STATUS_NO_MEMORY
;
5791 /* remove the old one, and add the new one, this way we do not risk races */
5792 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5795 return NT_STATUS_OK
;
5798 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5800 if (rc
!= LDAP_SUCCESS
) {
5801 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5802 pdb_get_username(sampass
), gidstr
));
5803 return NT_STATUS_UNSUCCESSFUL
;
5806 flush_pwnam_cache();
5808 return NT_STATUS_OK
;
5812 /**********************************************************************
5813 trusted domains functions
5814 *********************************************************************/
5816 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
5819 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
5820 ldap_state
->domain_dn
);
5823 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
5824 const char *domain
, LDAPMessage
**entry
)
5828 int scope
= LDAP_SCOPE_SUBTREE
;
5829 const char **attrs
= NULL
; /* NULL: get all attrs */
5830 int attrsonly
= 0; /* 0: return values too */
5831 LDAPMessage
*result
= NULL
;
5835 filter
= talloc_asprintf(talloc_tos(),
5836 "(&(objectClass=%s)(sambaDomainName=%s))",
5837 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
5839 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5840 if (trusted_dn
== NULL
) {
5843 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
5844 filter
, attrs
, attrsonly
, &result
);
5846 if (rc
== LDAP_NO_SUCH_OBJECT
) {
5851 if (rc
!= LDAP_SUCCESS
) {
5855 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5857 if (num_result
> 1) {
5858 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5859 "sambaTrustedDomainPassword object for domain '%s'"
5864 if (num_result
== 0) {
5865 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5866 "sambaTrustedDomainPassword object for domain %s.\n",
5870 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5876 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
5880 time_t *pass_last_set_time
)
5882 struct ldapsam_privates
*ldap_state
=
5883 (struct ldapsam_privates
*)methods
->private_data
;
5884 LDAPMessage
*entry
= NULL
;
5886 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
5888 if (!get_trusteddom_pw_int(ldap_state
, domain
, &entry
) ||
5897 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5898 entry
, "sambaClearTextPassword", talloc_tos());
5899 if (pwd_str
== NULL
) {
5902 /* trusteddom_pw routines do not use talloc yet... */
5903 *pwd
= SMB_STRDUP(pwd_str
);
5909 /* last change time */
5910 if (pass_last_set_time
!= NULL
) {
5912 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5913 entry
, "sambaPwdLastSet", talloc_tos());
5914 if (time_str
== NULL
) {
5917 *pass_last_set_time
= (time_t)atol(time_str
);
5924 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5927 if (sid_str
== NULL
) {
5930 dom_sid
= string_sid_talloc(talloc_tos(), sid_str
);
5931 if (dom_sid
== NULL
) {
5934 sid_copy(sid
, dom_sid
);
5940 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
5945 struct ldapsam_privates
*ldap_state
=
5946 (struct ldapsam_privates
*)methods
->private_data
;
5947 LDAPMessage
*entry
= NULL
;
5948 LDAPMod
**mods
= NULL
;
5949 char *prev_pwd
= NULL
;
5950 char *trusted_dn
= NULL
;
5953 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
5956 * get the current entry (if there is one) in order to put the
5957 * current password into the previous password attribute
5959 if (!get_trusteddom_pw_int(ldap_state
, domain
, &entry
)) {
5964 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
5965 "sambaTrustedDomainPassword");
5966 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
5968 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
5969 sid_string_tos(sid
));
5970 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
5971 talloc_asprintf(talloc_tos(), "%li", time(NULL
)));
5972 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5973 "sambaClearTextPassword", pwd
);
5974 if (entry
!= NULL
) {
5975 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5976 entry
, "sambaClearTextPassword", talloc_tos());
5977 if (prev_pwd
!= NULL
) {
5978 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5979 "sambaPreviousClearTextPassword",
5984 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5985 if (trusted_dn
== NULL
) {
5988 if (entry
== NULL
) {
5989 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5991 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5994 if (rc
!= LDAP_SUCCESS
) {
5995 DEBUG(1, ("error writing trusted domain password!\n"));
6002 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6006 struct ldapsam_privates
*ldap_state
=
6007 (struct ldapsam_privates
*)methods
->private_data
;
6008 LDAPMessage
*entry
= NULL
;
6009 const char *trusted_dn
;
6011 if (!get_trusteddom_pw_int(ldap_state
, domain
, &entry
)) {
6015 if (entry
== NULL
) {
6016 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6021 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6023 if (trusted_dn
== NULL
) {
6024 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6028 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6029 if (rc
!= LDAP_SUCCESS
) {
6036 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6037 TALLOC_CTX
*mem_ctx
,
6038 uint32
*num_domains
,
6039 struct trustdom_info
***domains
)
6042 struct ldapsam_privates
*ldap_state
=
6043 (struct ldapsam_privates
*)methods
->private_data
;
6045 int scope
= LDAP_SCOPE_SUBTREE
;
6046 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6047 int attrsonly
= 0; /* 0: return values too */
6048 LDAPMessage
*result
= NULL
;
6049 LDAPMessage
*entry
= NULL
;
6051 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6052 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6054 rc
= smbldap_search(ldap_state
->smbldap_state
,
6055 ldap_state
->domain_dn
,
6062 if (rc
!= LDAP_SUCCESS
) {
6063 return NT_STATUS_UNSUCCESSFUL
;
6067 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6068 DEBUG(1, ("talloc failed\n"));
6069 return NT_STATUS_NO_MEMORY
;
6072 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6074 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6076 char *dom_name
, *dom_sid_str
;
6077 struct trustdom_info
*dom_info
;
6079 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6080 if (dom_info
== NULL
) {
6081 DEBUG(1, ("talloc failed\n"));
6082 return NT_STATUS_NO_MEMORY
;
6085 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6089 if (dom_name
== NULL
) {
6090 DEBUG(1, ("talloc failed\n"));
6091 return NT_STATUS_NO_MEMORY
;
6093 dom_info
->name
= dom_name
;
6095 dom_sid_str
= smbldap_talloc_single_attribute(
6096 priv2ld(ldap_state
), entry
, "sambaSID",
6098 if (dom_sid_str
== NULL
) {
6099 DEBUG(1, ("talloc failed\n"));
6100 return NT_STATUS_NO_MEMORY
;
6102 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6103 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6105 return NT_STATUS_UNSUCCESSFUL
;
6108 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6109 domains
, num_domains
);
6111 if (*domains
== NULL
) {
6112 DEBUG(1, ("talloc failed\n"));
6113 return NT_STATUS_NO_MEMORY
;
6117 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6118 return NT_STATUS_OK
;
6122 /**********************************************************************
6124 *********************************************************************/
6126 static void free_private_data(void **vp
)
6128 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6130 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6132 if ((*ldap_state
)->result
!= NULL
) {
6133 ldap_msgfree((*ldap_state
)->result
);
6134 (*ldap_state
)->result
= NULL
;
6136 if ((*ldap_state
)->domain_dn
!= NULL
) {
6137 SAFE_FREE((*ldap_state
)->domain_dn
);
6142 /* No need to free any further, as it is talloc()ed */
6145 /*********************************************************************
6146 Intitalise the parts of the pdb_methods structure that are common to
6148 *********************************************************************/
6150 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6153 struct ldapsam_privates
*ldap_state
;
6155 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6159 (*pdb_method
)->name
= "ldapsam";
6161 (*pdb_method
)->setsampwent
= ldapsam_setsampwent
;
6162 (*pdb_method
)->endsampwent
= ldapsam_endsampwent
;
6163 (*pdb_method
)->getsampwent
= ldapsam_getsampwent
;
6164 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6165 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6166 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6167 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6168 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6169 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6171 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6172 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6173 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6174 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6175 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6176 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6177 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6179 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6180 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6182 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6184 (*pdb_method
)->rid_algorithm
= ldapsam_rid_algorithm
;
6185 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6187 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6188 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6189 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6190 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6192 /* TODO: Setup private data and free */
6194 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6195 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6196 return NT_STATUS_NO_MEMORY
;
6199 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6200 location
, &ldap_state
->smbldap_state
);
6202 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6206 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6207 return NT_STATUS_NO_MEMORY
;
6210 (*pdb_method
)->private_data
= ldap_state
;
6212 (*pdb_method
)->free_private_data
= free_private_data
;
6214 return NT_STATUS_OK
;
6217 /**********************************************************************
6218 Initialise the 'compat' mode for pdb_ldap
6219 *********************************************************************/
6221 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6224 struct ldapsam_privates
*ldap_state
;
6225 char *uri
= talloc_strdup( NULL
, location
);
6227 trim_char( uri
, '\"', '\"' );
6228 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6232 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6236 (*pdb_method
)->name
= "ldapsam_compat";
6238 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6239 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6241 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6243 return NT_STATUS_OK
;
6246 /**********************************************************************
6247 Initialise the normal mode for pdb_ldap
6248 *********************************************************************/
6250 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6253 struct ldapsam_privates
*ldap_state
= NULL
;
6254 uint32 alg_rid_base
;
6255 char *alg_rid_base_string
= NULL
;
6256 LDAPMessage
*result
= NULL
;
6257 LDAPMessage
*entry
= NULL
;
6258 DOM_SID ldap_domain_sid
;
6259 DOM_SID secrets_domain_sid
;
6260 char *domain_sid_string
= NULL
;
6262 char *uri
= talloc_strdup( NULL
, location
);
6264 trim_char( uri
, '\"', '\"' );
6265 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6270 if (!NT_STATUS_IS_OK(nt_status
)) {
6274 (*pdb_method
)->name
= "ldapsam";
6276 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6277 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6278 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6279 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6280 (*pdb_method
)->search_users
= ldapsam_search_users
;
6281 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6282 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6284 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6285 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6286 (*pdb_method
)->enum_group_memberships
=
6287 ldapsam_enum_group_memberships
;
6288 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6289 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6291 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6292 (*pdb_method
)->create_user
= ldapsam_create_user
;
6293 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6294 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6295 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6296 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6297 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6298 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6302 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6303 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6305 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6307 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6309 ldap_state
->domain_name
, True
);
6311 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6312 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6313 "info, nor add one to the domain\n"));
6314 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6315 "will be unable to allocate new users/groups, "
6316 "and will risk BDCs having inconsistant SIDs\n"));
6317 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6318 return NT_STATUS_OK
;
6321 /* Given that the above might fail, everything below this must be
6324 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6327 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6329 ldap_msgfree(result
);
6330 return NT_STATUS_UNSUCCESSFUL
;
6333 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
6335 ldap_msgfree(result
);
6336 return NT_STATUS_UNSUCCESSFUL
;
6339 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6342 domain_sid_string
= smbldap_talloc_single_attribute(
6343 ldap_state
->smbldap_state
->ldap_struct
,
6345 get_userattr_key2string(ldap_state
->schema_ver
,
6346 LDAP_ATTR_USER_SID
),
6349 if (domain_sid_string
) {
6351 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6352 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6353 "read as a valid SID\n", domain_sid_string
));
6354 ldap_msgfree(result
);
6355 TALLOC_FREE(domain_sid_string
);
6356 return NT_STATUS_INVALID_PARAMETER
;
6358 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6359 &secrets_domain_sid
);
6360 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
6361 &ldap_domain_sid
)) {
6362 fstring new_sid_str
, old_sid_str
;
6363 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6364 "%s based on pdb_ldap results %s -> %s\n",
6365 ldap_state
->domain_name
,
6366 sid_to_string(old_sid_str
,
6367 &secrets_domain_sid
),
6368 sid_to_string(new_sid_str
,
6369 &ldap_domain_sid
)));
6371 /* reset secrets.tdb sid */
6372 secrets_store_domain_sid(ldap_state
->domain_name
,
6374 DEBUG(1, ("New global sam SID: %s\n",
6375 sid_to_string(new_sid_str
,
6376 get_global_sam_sid())));
6378 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6379 TALLOC_FREE(domain_sid_string
);
6382 alg_rid_base_string
= smbldap_talloc_single_attribute(
6383 ldap_state
->smbldap_state
->ldap_struct
,
6385 get_attr_key2string( dominfo_attr_list
,
6386 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6388 if (alg_rid_base_string
) {
6389 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
6390 if (alg_rid_base
!= algorithmic_rid_base()) {
6391 DEBUG(0, ("The value of 'algorithmic RID base' has "
6392 "changed since the LDAP\n"
6393 "database was initialised. Aborting. \n"));
6394 ldap_msgfree(result
);
6395 TALLOC_FREE(alg_rid_base_string
);
6396 return NT_STATUS_UNSUCCESSFUL
;
6398 TALLOC_FREE(alg_rid_base_string
);
6400 ldap_msgfree(result
);
6402 return NT_STATUS_OK
;
6405 NTSTATUS
pdb_ldap_init(void)
6408 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6411 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6414 /* Let pdb_nds register backends */
6417 return NT_STATUS_OK
;