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_talloc(mem_ctx
, &p
, &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 SAFE_FREE(escape_user
);
353 return LDAP_NO_MEMORY
;
356 * have to use this here because $ is filtered out
360 filter
= talloc_all_string_sub(talloc_tos(),
361 filter
, "%u", escape_user
);
362 SAFE_FREE(escape_user
);
364 return LDAP_NO_MEMORY
;
367 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
368 filter
, attr
, result
);
373 /*******************************************************************
374 Run the search by rid.
375 ******************************************************************/
377 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
378 uint32 rid
, LDAPMessage
** result
,
384 filter
= talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid
,
385 get_objclass_filter(ldap_state
->schema_ver
));
387 return LDAP_NO_MEMORY
;
390 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
391 filter
, attr
, result
);
396 /*******************************************************************
397 Run the search by SID.
398 ******************************************************************/
400 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
401 const DOM_SID
*sid
, LDAPMessage
** result
,
408 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
409 get_userattr_key2string(ldap_state
->schema_ver
,
411 sid_to_fstring(sid_string
, sid
),
412 get_objclass_filter(ldap_state
->schema_ver
));
414 return LDAP_NO_MEMORY
;
417 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
418 filter
, attr
, result
);
424 /*******************************************************************
425 Delete complete object or objectclass and attrs from
426 object found in search_result depending on lp_ldap_delete_dn
427 ******************************************************************/
429 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
432 const char *objectclass
,
435 LDAPMod
**mods
= NULL
;
438 BerElement
*ptr
= NULL
;
440 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
442 return LDAP_NO_MEMORY
;
445 if (lp_ldap_delete_dn()) {
446 return smbldap_delete(priv
->smbldap_state
, dn
);
449 /* Ok, delete only the SAM attributes */
451 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
453 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
456 /* We are only allowed to delete the attributes that
459 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
460 if (strequal(*attrib
, name
)) {
461 DEBUG(10, ("ldapsam_delete_entry: deleting "
462 "attribute %s\n", name
));
463 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
474 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
475 talloc_autofree_ldapmod(mem_ctx
, mods
);
477 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
480 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
485 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
486 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
492 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
493 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
503 /**********************************************************************
504 Initialize struct samu from an LDAP query.
505 (Based on init_sam_from_buffer in pdb_tdb.c)
506 *********************************************************************/
508 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
509 struct samu
* sampass
,
516 pass_can_change_time
,
517 pass_must_change_time
,
520 char *username
= NULL
,
526 *logon_script
= NULL
,
527 *profile_path
= NULL
,
529 *workstations
= NULL
,
532 uint8 smblmpwd
[LM_HASH_LEN
],
533 smbntpwd
[NT_HASH_LEN
];
534 bool use_samba_attrs
= True
;
535 uint32 acct_ctrl
= 0;
537 uint16 bad_password_count
= 0,
540 uint8 hours
[MAX_HOURS_LEN
];
542 LOGIN_CACHE
*cache_entry
= NULL
;
544 bool expand_explicit
= lp_passdb_expand_explicit();
546 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
551 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
552 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
556 if (priv2ld(ldap_state
) == NULL
) {
557 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
558 "ldap_struct is NULL!\n"));
562 if (!(username
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
566 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
571 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
573 nt_username
= talloc_strdup(ctx
, username
);
578 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
583 pdb_set_username(sampass
, username
, PDB_SET
);
585 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
586 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
588 /* deal with different attributes between the schema first */
590 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
591 if ((temp
= smbldap_talloc_single_attribute(
592 ldap_state
->smbldap_state
->ldap_struct
,
594 get_userattr_key2string(ldap_state
->schema_ver
,
597 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
600 if ((temp
= smbldap_talloc_single_attribute(
601 ldap_state
->smbldap_state
->ldap_struct
,
603 get_userattr_key2string(ldap_state
->schema_ver
,
606 user_rid
= (uint32
)atol(temp
);
607 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
611 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
612 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
613 get_userattr_key2string(ldap_state
->schema_ver
,
615 get_userattr_key2string(ldap_state
->schema_ver
,
621 temp
= smbldap_talloc_single_attribute(
622 ldap_state
->smbldap_state
->ldap_struct
,
624 get_userattr_key2string(ldap_state
->schema_ver
,
625 LDAP_ATTR_PWD_LAST_SET
),
628 pass_last_set_time
= (time_t) atol(temp
);
629 pdb_set_pass_last_set_time(sampass
,
630 pass_last_set_time
, PDB_SET
);
633 temp
= smbldap_talloc_single_attribute(
634 ldap_state
->smbldap_state
->ldap_struct
,
636 get_userattr_key2string(ldap_state
->schema_ver
,
637 LDAP_ATTR_LOGON_TIME
),
640 logon_time
= (time_t) atol(temp
);
641 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
644 temp
= smbldap_talloc_single_attribute(
645 ldap_state
->smbldap_state
->ldap_struct
,
647 get_userattr_key2string(ldap_state
->schema_ver
,
648 LDAP_ATTR_LOGOFF_TIME
),
651 logoff_time
= (time_t) atol(temp
);
652 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
655 temp
= smbldap_talloc_single_attribute(
656 ldap_state
->smbldap_state
->ldap_struct
,
658 get_userattr_key2string(ldap_state
->schema_ver
,
659 LDAP_ATTR_KICKOFF_TIME
),
662 kickoff_time
= (time_t) atol(temp
);
663 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
666 temp
= smbldap_talloc_single_attribute(
667 ldap_state
->smbldap_state
->ldap_struct
,
669 get_userattr_key2string(ldap_state
->schema_ver
,
670 LDAP_ATTR_PWD_CAN_CHANGE
),
673 pass_can_change_time
= (time_t) atol(temp
);
674 pdb_set_pass_can_change_time(sampass
,
675 pass_can_change_time
, PDB_SET
);
678 temp
= smbldap_talloc_single_attribute(
679 ldap_state
->smbldap_state
->ldap_struct
,
681 get_userattr_key2string(ldap_state
->schema_ver
,
682 LDAP_ATTR_PWD_MUST_CHANGE
),
685 pass_must_change_time
= (time_t) atol(temp
);
686 pdb_set_pass_must_change_time(sampass
,
687 pass_must_change_time
, PDB_SET
);
690 /* recommend that 'gecos' and 'displayName' should refer to the same
691 * attribute OID. userFullName depreciated, only used by Samba
692 * primary rules of LDAP: don't make a new attribute when one is already defined
693 * that fits your needs; using cn then displayName rather than 'userFullName'
696 fullname
= smbldap_talloc_single_attribute(
697 ldap_state
->smbldap_state
->ldap_struct
,
699 get_userattr_key2string(ldap_state
->schema_ver
,
700 LDAP_ATTR_DISPLAY_NAME
),
703 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
705 fullname
= smbldap_talloc_single_attribute(
706 ldap_state
->smbldap_state
->ldap_struct
,
708 get_userattr_key2string(ldap_state
->schema_ver
,
712 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
716 dir_drive
= smbldap_talloc_single_attribute(
717 ldap_state
->smbldap_state
->ldap_struct
,
719 get_userattr_key2string(ldap_state
->schema_ver
,
720 LDAP_ATTR_HOME_DRIVE
),
723 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
725 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
728 homedir
= smbldap_talloc_single_attribute(
729 ldap_state
->smbldap_state
->ldap_struct
,
731 get_userattr_key2string(ldap_state
->schema_ver
,
732 LDAP_ATTR_HOME_PATH
),
735 if (expand_explicit
) {
736 homedir
= talloc_sub_basic(ctx
,
744 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
746 pdb_set_homedir(sampass
,
747 talloc_sub_basic(ctx
, username
, domain
,
752 logon_script
= smbldap_talloc_single_attribute(
753 ldap_state
->smbldap_state
->ldap_struct
,
755 get_userattr_key2string(ldap_state
->schema_ver
,
756 LDAP_ATTR_LOGON_SCRIPT
),
759 if (expand_explicit
) {
760 logon_script
= talloc_sub_basic(ctx
,
768 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
770 pdb_set_logon_script(sampass
,
771 talloc_sub_basic(ctx
, username
, domain
,
776 profile_path
= smbldap_talloc_single_attribute(
777 ldap_state
->smbldap_state
->ldap_struct
,
779 get_userattr_key2string(ldap_state
->schema_ver
,
780 LDAP_ATTR_PROFILE_PATH
),
783 if (expand_explicit
) {
784 profile_path
= talloc_sub_basic(ctx
,
792 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
794 pdb_set_profile_path(sampass
,
795 talloc_sub_basic(ctx
, username
, domain
,
800 acct_desc
= smbldap_talloc_single_attribute(
801 ldap_state
->smbldap_state
->ldap_struct
,
803 get_userattr_key2string(ldap_state
->schema_ver
,
807 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
810 workstations
= smbldap_talloc_single_attribute(
811 ldap_state
->smbldap_state
->ldap_struct
,
813 get_userattr_key2string(ldap_state
->schema_ver
,
817 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
820 munged_dial
= smbldap_talloc_single_attribute(
821 ldap_state
->smbldap_state
->ldap_struct
,
823 get_userattr_key2string(ldap_state
->schema_ver
,
824 LDAP_ATTR_MUNGED_DIAL
),
827 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
830 /* FIXME: hours stuff should be cleaner */
834 memset(hours
, 0xff, hours_len
);
836 if (ldap_state
->is_nds_ldap
) {
839 char clear_text_pw
[512];
841 /* Make call to Novell eDirectory ldap extension to get clear text password.
842 NOTE: This will only work if we have an SSL connection to eDirectory. */
843 user_dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
844 if (user_dn
!= NULL
) {
845 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username
, user_dn
));
847 pwd_len
= sizeof(clear_text_pw
);
848 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
849 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
850 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
854 ZERO_STRUCT(smblmpwd
);
855 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
859 ZERO_STRUCT(smbntpwd
);
860 use_samba_attrs
= False
;
866 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
870 if (use_samba_attrs
) {
871 temp
= smbldap_talloc_single_attribute(
872 ldap_state
->smbldap_state
->ldap_struct
,
874 get_userattr_key2string(ldap_state
->schema_ver
,
878 pdb_gethexpwd(temp
, smblmpwd
);
879 memset((char *)temp
, '\0', strlen(temp
)+1);
880 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
883 ZERO_STRUCT(smblmpwd
);
886 temp
= smbldap_talloc_single_attribute(
887 ldap_state
->smbldap_state
->ldap_struct
,
889 get_userattr_key2string(ldap_state
->schema_ver
,
893 pdb_gethexpwd(temp
, smbntpwd
);
894 memset((char *)temp
, '\0', strlen(temp
)+1);
895 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
898 ZERO_STRUCT(smbntpwd
);
904 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
906 uint8
*pwhist
= NULL
;
908 char *history_string
= TALLOC_ARRAY(ctx
, char,
909 MAX_PW_HISTORY_LEN
*64);
911 if (!history_string
) {
915 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
917 if ((pwhist
= TALLOC_ARRAY(ctx
, uint8
,
918 pwHistLen
* PW_HISTORY_ENTRY_LEN
)) ==
920 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
923 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
925 if (smbldap_get_single_attribute(
926 ldap_state
->smbldap_state
->ldap_struct
,
928 get_userattr_key2string(ldap_state
->schema_ver
,
929 LDAP_ATTR_PWD_HISTORY
),
931 MAX_PW_HISTORY_LEN
*64)) {
932 bool hex_failed
= false;
933 for (i
= 0; i
< pwHistLen
; i
++){
934 /* Get the 16 byte salt. */
935 if (!pdb_gethexpwd(&history_string
[i
*64],
936 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
940 /* Get the 16 byte MD5 hash of salt+passwd. */
941 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
942 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
943 PW_HISTORY_SALT_LEN
])) {
949 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
951 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
954 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
959 temp
= smbldap_talloc_single_attribute(
960 ldap_state
->smbldap_state
->ldap_struct
,
962 get_userattr_key2string(ldap_state
->schema_ver
,
966 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
968 if (acct_ctrl
== 0) {
969 acct_ctrl
|= ACB_NORMAL
;
972 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
974 acct_ctrl
|= ACB_NORMAL
;
977 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
978 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
980 temp
= smbldap_talloc_single_attribute(
981 ldap_state
->smbldap_state
->ldap_struct
,
983 get_userattr_key2string(ldap_state
->schema_ver
,
984 LDAP_ATTR_BAD_PASSWORD_COUNT
),
987 bad_password_count
= (uint32
) atol(temp
);
988 pdb_set_bad_password_count(sampass
,
989 bad_password_count
, PDB_SET
);
992 temp
= smbldap_talloc_single_attribute(
993 ldap_state
->smbldap_state
->ldap_struct
,
995 get_userattr_key2string(ldap_state
->schema_ver
,
996 LDAP_ATTR_BAD_PASSWORD_TIME
),
999 bad_password_time
= (time_t) atol(temp
);
1000 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
1004 temp
= smbldap_talloc_single_attribute(
1005 ldap_state
->smbldap_state
->ldap_struct
,
1007 get_userattr_key2string(ldap_state
->schema_ver
,
1008 LDAP_ATTR_LOGON_COUNT
),
1011 logon_count
= (uint32
) atol(temp
);
1012 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
1015 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1017 temp
= smbldap_talloc_single_attribute(
1018 ldap_state
->smbldap_state
->ldap_struct
,
1020 get_userattr_key2string(ldap_state
->schema_ver
,
1021 LDAP_ATTR_LOGON_HOURS
),
1024 pdb_gethexhours(temp
, hours
);
1025 memset((char *)temp
, '\0', strlen(temp
) +1);
1026 pdb_set_hours(sampass
, hours
, PDB_SET
);
1030 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1031 temp
= smbldap_talloc_single_attribute(
1032 priv2ld(ldap_state
),
1037 /* We've got a uid, feed the cache */
1038 uid_t uid
= strtoul(temp
, NULL
, 10);
1039 store_uid_sid_cache(pdb_get_user_sid(sampass
), uid
);
1043 /* check the timestamp of the cache vs ldap entry */
1044 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1050 /* see if we have newer updates */
1051 if (!(cache_entry
= login_cache_read(sampass
))) {
1052 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1053 (unsigned int)pdb_get_bad_password_count(sampass
),
1054 (unsigned int)pdb_get_bad_password_time(sampass
)));
1059 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1060 (unsigned int)ldap_entry_time
,
1061 (unsigned int)cache_entry
->entry_timestamp
,
1062 (unsigned int)cache_entry
->bad_password_time
));
1064 if (ldap_entry_time
> cache_entry
->entry_timestamp
) {
1065 /* cache is older than directory , so
1066 we need to delete the entry but allow the
1067 fields to be written out */
1068 login_cache_delentry(sampass
);
1071 pdb_set_acct_ctrl(sampass
,
1072 pdb_get_acct_ctrl(sampass
) |
1073 (cache_entry
->acct_ctrl
& ACB_AUTOLOCK
),
1075 pdb_set_bad_password_count(sampass
,
1076 cache_entry
->bad_password_count
,
1078 pdb_set_bad_password_time(sampass
,
1079 cache_entry
->bad_password_time
,
1088 SAFE_FREE(cache_entry
);
1092 /**********************************************************************
1093 Initialize the ldap db from a struct samu. Called on update.
1094 (Based on init_buffer_from_sam in pdb_tdb.c)
1095 *********************************************************************/
1097 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1098 LDAPMessage
*existing
,
1099 LDAPMod
*** mods
, struct samu
* sampass
,
1100 bool (*need_update
)(const struct samu
*,
1106 if (mods
== NULL
|| sampass
== NULL
) {
1107 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1114 * took out adding "objectclass: sambaAccount"
1115 * do this on a per-mod basis
1117 if (need_update(sampass
, PDB_USERNAME
)) {
1118 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1119 "uid", pdb_get_username(sampass
));
1120 if (ldap_state
->is_nds_ldap
) {
1121 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1122 "cn", pdb_get_username(sampass
));
1123 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1124 "sn", pdb_get_username(sampass
));
1128 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1130 /* only update the RID if we actually need to */
1131 if (need_update(sampass
, PDB_USERSID
)) {
1133 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
1135 switch ( ldap_state
->schema_ver
) {
1136 case SCHEMAVER_SAMBAACCOUNT
:
1137 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1138 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1139 sid_string_dbg(user_sid
),
1141 &ldap_state
->domain_sid
)));
1144 if (asprintf(&temp
, "%i", rid
) < 0) {
1147 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1148 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1153 case SCHEMAVER_SAMBASAMACCOUNT
:
1154 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1155 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1156 sid_to_fstring(sid_string
, user_sid
));
1160 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1165 /* we don't need to store the primary group RID - so leaving it
1166 'free' to hang off the unix primary group makes life easier */
1168 if (need_update(sampass
, PDB_GROUPSID
)) {
1170 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
1172 switch ( ldap_state
->schema_ver
) {
1173 case SCHEMAVER_SAMBAACCOUNT
:
1174 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1175 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1176 sid_string_dbg(group_sid
),
1178 &ldap_state
->domain_sid
)));
1182 if (asprintf(&temp
, "%i", rid
) < 0) {
1185 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1186 get_userattr_key2string(ldap_state
->schema_ver
,
1187 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1191 case SCHEMAVER_SAMBASAMACCOUNT
:
1192 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1193 get_userattr_key2string(ldap_state
->schema_ver
,
1194 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1198 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1204 /* displayName, cn, and gecos should all be the same
1205 * most easily accomplished by giving them the same OID
1206 * gecos isn't set here b/c it should be handled by the
1208 * We change displayName only and fall back to cn if
1209 * it does not exist.
1212 if (need_update(sampass
, PDB_FULLNAME
))
1213 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1214 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1215 pdb_get_fullname(sampass
));
1217 if (need_update(sampass
, PDB_ACCTDESC
))
1218 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1219 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1220 pdb_get_acct_desc(sampass
));
1222 if (need_update(sampass
, PDB_WORKSTATIONS
))
1223 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1224 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1225 pdb_get_workstations(sampass
));
1227 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1228 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1229 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1230 pdb_get_munged_dial(sampass
));
1232 if (need_update(sampass
, PDB_SMBHOME
))
1233 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1234 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1235 pdb_get_homedir(sampass
));
1237 if (need_update(sampass
, PDB_DRIVE
))
1238 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1239 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1240 pdb_get_dir_drive(sampass
));
1242 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1243 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1244 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1245 pdb_get_logon_script(sampass
));
1247 if (need_update(sampass
, PDB_PROFILE
))
1248 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1249 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1250 pdb_get_profile_path(sampass
));
1252 if (asprintf(&temp
, "%li", pdb_get_logon_time(sampass
)) < 0) {
1255 if (need_update(sampass
, PDB_LOGONTIME
))
1256 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1257 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1260 if (asprintf(&temp
, "%li", pdb_get_logoff_time(sampass
)) < 0) {
1263 if (need_update(sampass
, PDB_LOGOFFTIME
))
1264 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1265 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1268 if (asprintf(&temp
, "%li", pdb_get_kickoff_time(sampass
)) < 0) {
1271 if (need_update(sampass
, PDB_KICKOFFTIME
))
1272 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1273 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1276 if (asprintf(&temp
, "%li", pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1279 if (need_update(sampass
, PDB_CANCHANGETIME
))
1280 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1281 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1284 if (asprintf(&temp
, "%li", pdb_get_pass_must_change_time(sampass
)) < 0) {
1287 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1288 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1289 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1292 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1293 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1295 if (need_update(sampass
, PDB_LMPASSWD
)) {
1296 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1299 pdb_sethexpwd(pwstr
, lm_pw
,
1300 pdb_get_acct_ctrl(sampass
));
1301 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1302 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1305 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1306 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1310 if (need_update(sampass
, PDB_NTPASSWD
)) {
1311 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1314 pdb_sethexpwd(pwstr
, nt_pw
,
1315 pdb_get_acct_ctrl(sampass
));
1316 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1317 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1320 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1321 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1326 if (need_update(sampass
, PDB_PWHISTORY
)) {
1328 uint32 pwHistLen
= 0;
1329 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &pwHistLen
);
1331 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1335 if (pwHistLen
== 0) {
1336 /* Remove any password history from the LDAP store. */
1337 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1341 uint32 currHistLen
= 0;
1342 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1343 if (pwhist
!= NULL
) {
1344 /* We can only store (1024-1/64 password history entries. */
1345 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1346 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1347 /* Store the salt. */
1348 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1349 /* Followed by the md5 hash of salt + md4 hash */
1350 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1351 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1352 DEBUG(100, ("pwstr=%s\n", pwstr
));
1356 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1357 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1362 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1363 if (asprintf(&temp
, "%li",
1364 pdb_get_pass_last_set_time(sampass
)) < 0) {
1367 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1368 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1374 if (need_update(sampass
, PDB_HOURS
)) {
1375 const uint8
*hours
= pdb_get_hours(sampass
);
1378 pdb_sethexhours(hourstr
, hours
);
1379 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1382 get_userattr_key2string(ldap_state
->schema_ver
,
1383 LDAP_ATTR_LOGON_HOURS
),
1388 if (need_update(sampass
, PDB_ACCTCTRL
))
1389 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1390 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1391 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1393 /* password lockout cache:
1394 - If we are now autolocking or clearing, we write to ldap
1395 - If we are clearing, we delete the cache entry
1396 - If the count is > 0, we update the cache
1398 This even means when autolocking, we cache, just in case the
1399 update doesn't work, and we have to cache the autolock flag */
1401 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1402 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1403 uint16 badcount
= pdb_get_bad_password_count(sampass
);
1404 time_t badtime
= pdb_get_bad_password_time(sampass
);
1406 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, &pol
);
1408 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1409 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1411 if ((badcount
>= pol
) || (badcount
== 0)) {
1412 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1413 (unsigned int)badcount
, (unsigned int)badtime
));
1414 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1418 ldap_state
->smbldap_state
->ldap_struct
,
1420 get_userattr_key2string(
1421 ldap_state
->schema_ver
,
1422 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1426 if (asprintf(&temp
, "%li", badtime
) < 0) {
1430 ldap_state
->smbldap_state
->ldap_struct
,
1432 get_userattr_key2string(
1433 ldap_state
->schema_ver
,
1434 LDAP_ATTR_BAD_PASSWORD_TIME
),
1438 if (badcount
== 0) {
1439 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1440 login_cache_delentry(sampass
);
1442 LOGIN_CACHE cache_entry
;
1444 cache_entry
.entry_timestamp
= time(NULL
);
1445 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1446 cache_entry
.bad_password_count
= badcount
;
1447 cache_entry
.bad_password_time
= badtime
;
1449 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1450 login_cache_write(sampass
, cache_entry
);
1457 /**********************************************************************
1458 End enumeration of the LDAP password list.
1459 *********************************************************************/
1461 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1463 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1464 if (ldap_state
->result
) {
1465 ldap_msgfree(ldap_state
->result
);
1466 ldap_state
->result
= NULL
;
1470 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1471 const char *new_attr
)
1475 if (new_attr
== NULL
) {
1479 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1483 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1485 SMB_ASSERT((*attr_list
) != NULL
);
1486 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1487 (*attr_list
)[i
+1] = NULL
;
1490 /**********************************************************************
1491 Get struct samu entry from LDAP by username.
1492 *********************************************************************/
1494 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1496 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1497 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1498 LDAPMessage
*result
= NULL
;
1499 LDAPMessage
*entry
= NULL
;
1501 const char ** attr_list
;
1504 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1505 append_attr(user
, &attr_list
,
1506 get_userattr_key2string(ldap_state
->schema_ver
,
1507 LDAP_ATTR_MOD_TIMESTAMP
));
1508 append_attr(user
, &attr_list
, "uidNumber");
1509 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1511 TALLOC_FREE( attr_list
);
1513 if ( rc
!= LDAP_SUCCESS
)
1514 return NT_STATUS_NO_SUCH_USER
;
1516 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1519 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1520 ldap_msgfree(result
);
1521 return NT_STATUS_NO_SUCH_USER
;
1522 } else if (count
> 1) {
1523 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1524 ldap_msgfree(result
);
1525 return NT_STATUS_NO_SUCH_USER
;
1528 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1530 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1531 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1532 ldap_msgfree(result
);
1533 return NT_STATUS_NO_SUCH_USER
;
1535 pdb_set_backend_private_data(user
, result
, NULL
,
1536 my_methods
, PDB_CHANGED
);
1537 talloc_autofree_ldapmsg(user
, result
);
1540 ldap_msgfree(result
);
1545 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1546 const DOM_SID
*sid
, LDAPMessage
**result
)
1549 const char ** attr_list
;
1552 switch ( ldap_state
->schema_ver
) {
1553 case SCHEMAVER_SAMBASAMACCOUNT
: {
1554 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1555 if (tmp_ctx
== NULL
) {
1556 return LDAP_NO_MEMORY
;
1559 attr_list
= get_userattr_list(tmp_ctx
,
1560 ldap_state
->schema_ver
);
1561 append_attr(tmp_ctx
, &attr_list
,
1562 get_userattr_key2string(
1563 ldap_state
->schema_ver
,
1564 LDAP_ATTR_MOD_TIMESTAMP
));
1565 append_attr(tmp_ctx
, &attr_list
, "uidNumber");
1566 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1568 TALLOC_FREE(tmp_ctx
);
1570 if ( rc
!= LDAP_SUCCESS
)
1575 case SCHEMAVER_SAMBAACCOUNT
:
1576 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1580 attr_list
= get_userattr_list(NULL
,
1581 ldap_state
->schema_ver
);
1582 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1583 TALLOC_FREE( attr_list
);
1585 if ( rc
!= LDAP_SUCCESS
)
1592 /**********************************************************************
1593 Get struct samu entry from LDAP by SID.
1594 *********************************************************************/
1596 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1598 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1599 LDAPMessage
*result
= NULL
;
1600 LDAPMessage
*entry
= NULL
;
1604 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1606 if (rc
!= LDAP_SUCCESS
)
1607 return NT_STATUS_NO_SUCH_USER
;
1609 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1612 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1613 "count=%d\n", sid_string_dbg(sid
), count
));
1614 ldap_msgfree(result
);
1615 return NT_STATUS_NO_SUCH_USER
;
1616 } else if (count
> 1) {
1617 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1618 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1620 ldap_msgfree(result
);
1621 return NT_STATUS_NO_SUCH_USER
;
1624 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1626 ldap_msgfree(result
);
1627 return NT_STATUS_NO_SUCH_USER
;
1630 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1631 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1632 ldap_msgfree(result
);
1633 return NT_STATUS_NO_SUCH_USER
;
1636 pdb_set_backend_private_data(user
, result
, NULL
,
1637 my_methods
, PDB_CHANGED
);
1638 talloc_autofree_ldapmsg(user
, result
);
1639 return NT_STATUS_OK
;
1642 /********************************************************************
1643 Do the actual modification - also change a plaintext passord if
1645 **********************************************************************/
1647 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1648 struct samu
*newpwd
, char *dn
,
1649 LDAPMod
**mods
, int ldap_op
,
1650 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1652 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1655 if (!newpwd
|| !dn
) {
1656 return NT_STATUS_INVALID_PARAMETER
;
1660 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1661 /* may be password change below however */
1665 if (ldap_state
->is_nds_ldap
) {
1666 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1670 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1674 rc
= smbldap_add(ldap_state
->smbldap_state
,
1677 case LDAP_MOD_REPLACE
:
1678 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1682 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1684 return NT_STATUS_INVALID_PARAMETER
;
1687 if (rc
!=LDAP_SUCCESS
) {
1688 return NT_STATUS_UNSUCCESSFUL
;
1692 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1693 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1694 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1695 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1698 char *retoid
= NULL
;
1699 struct berval
*retdata
= NULL
;
1700 char *utf8_password
;
1702 size_t converted_size
;
1704 if (!ldap_state
->is_nds_ldap
) {
1706 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1707 LDAP_EXOP_MODIFY_PASSWD
)) {
1708 DEBUG(2, ("ldap password change requested, but LDAP "
1709 "server does not support it -- ignoring\n"));
1710 return NT_STATUS_OK
;
1714 if (!push_utf8_allocate(&utf8_password
,
1715 pdb_get_plaintext_passwd(newpwd
),
1718 return NT_STATUS_NO_MEMORY
;
1721 if (!push_utf8_allocate(&utf8_dn
, dn
, &converted_size
)) {
1722 SAFE_FREE(utf8_password
);
1723 return NT_STATUS_NO_MEMORY
;
1726 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1727 DEBUG(0,("ber_alloc_t returns NULL\n"));
1728 SAFE_FREE(utf8_password
);
1730 return NT_STATUS_UNSUCCESSFUL
;
1733 if ((ber_printf (ber
, "{") < 0) ||
1734 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
, utf8_dn
) < 0) ||
1735 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
, utf8_password
) < 0) ||
1736 (ber_printf (ber
, "n}") < 0)) {
1737 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a value <0\n"));
1740 SAFE_FREE(utf8_password
);
1741 return NT_STATUS_UNSUCCESSFUL
;
1744 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1745 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1748 SAFE_FREE(utf8_password
);
1749 return NT_STATUS_UNSUCCESSFUL
;
1753 SAFE_FREE(utf8_password
);
1756 if (!ldap_state
->is_nds_ldap
) {
1757 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1758 LDAP_EXOP_MODIFY_PASSWD
,
1759 bv
, NULL
, NULL
, &retoid
,
1762 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1763 pdb_get_plaintext_passwd(newpwd
));
1765 if (rc
!= LDAP_SUCCESS
) {
1766 char *ld_error
= NULL
;
1768 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1769 DEBUG(3, ("Could not set userPassword "
1770 "attribute due to an objectClass "
1771 "violation -- ignoring\n"));
1773 return NT_STATUS_OK
;
1776 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1778 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1779 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1780 SAFE_FREE(ld_error
);
1782 #if defined(LDAP_CONSTRAINT_VIOLATION)
1783 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1784 return NT_STATUS_PASSWORD_RESTRICTION
;
1786 return NT_STATUS_UNSUCCESSFUL
;
1788 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1789 #ifdef DEBUG_PASSWORD
1790 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1793 ber_bvfree(retdata
);
1795 ldap_memfree(retoid
);
1799 return NT_STATUS_OK
;
1802 /**********************************************************************
1803 Delete entry from LDAP for username.
1804 *********************************************************************/
1806 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1807 struct samu
* sam_acct
)
1809 struct ldapsam_privates
*priv
=
1810 (struct ldapsam_privates
*)my_methods
->private_data
;
1813 LDAPMessage
*msg
, *entry
;
1814 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1815 const char **attr_list
;
1816 TALLOC_CTX
*mem_ctx
;
1819 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1820 return NT_STATUS_INVALID_PARAMETER
;
1823 sname
= pdb_get_username(sam_acct
);
1825 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1828 mem_ctx
= talloc_new(NULL
);
1829 if (mem_ctx
== NULL
) {
1830 DEBUG(0, ("talloc_new failed\n"));
1834 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1835 if (attr_list
== NULL
) {
1839 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1841 if ((rc
!= LDAP_SUCCESS
) ||
1842 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1843 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1844 DEBUG(5, ("Could not find user %s\n", sname
));
1845 result
= NT_STATUS_NO_SUCH_USER
;
1849 rc
= ldapsam_delete_entry(
1850 priv
, mem_ctx
, entry
,
1851 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1852 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1855 result
= (rc
== LDAP_SUCCESS
) ?
1856 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1859 TALLOC_FREE(mem_ctx
);
1863 /**********************************************************************
1864 Helper function to determine for update_sam_account whether
1865 we need LDAP modification.
1866 *********************************************************************/
1868 static bool element_is_changed(const struct samu
*sampass
,
1869 enum pdb_elements element
)
1871 return IS_SAM_CHANGED(sampass
, element
);
1874 /**********************************************************************
1876 *********************************************************************/
1878 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1880 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1881 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1884 LDAPMessage
*result
= NULL
;
1885 LDAPMessage
*entry
= NULL
;
1886 LDAPMod
**mods
= NULL
;
1887 const char **attr_list
;
1889 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1891 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1892 if (pdb_get_username(newpwd
) == NULL
) {
1893 return NT_STATUS_INVALID_PARAMETER
;
1895 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1896 TALLOC_FREE( attr_list
);
1897 if (rc
!= LDAP_SUCCESS
) {
1898 return NT_STATUS_UNSUCCESSFUL
;
1900 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1901 my_methods
, PDB_CHANGED
);
1902 talloc_autofree_ldapmsg(newpwd
, result
);
1905 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1906 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1907 return NT_STATUS_UNSUCCESSFUL
;
1910 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1911 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1913 return NT_STATUS_UNSUCCESSFUL
;
1916 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1918 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1919 element_is_changed
)) {
1920 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1923 ldap_mods_free(mods
,True
);
1924 return NT_STATUS_UNSUCCESSFUL
;
1927 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1928 && (mods
== NULL
)) {
1929 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1930 pdb_get_username(newpwd
)));
1932 return NT_STATUS_OK
;
1935 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1938 ldap_mods_free(mods
,True
);
1944 * We need to set the backend private data to NULL here. For example
1945 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1946 * same one, and with the explicit delete / add logic for attribute
1947 * values the second time we would use the wrong "old" value which
1948 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1950 * The existing LDAPMessage is still being auto-freed by the
1953 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
1956 if (!NT_STATUS_IS_OK(ret
)) {
1960 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1961 pdb_get_username(newpwd
)));
1962 return NT_STATUS_OK
;
1965 /***************************************************************************
1966 Renames a struct samu
1967 - The "rename user script" has full responsibility for changing everything
1968 ***************************************************************************/
1970 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1971 struct samu
*old_acct
,
1972 const char *newname
)
1974 const char *oldname
;
1976 char *rename_script
= NULL
;
1977 fstring oldname_lower
, newname_lower
;
1980 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1981 return NT_STATUS_INVALID_PARAMETER
;
1984 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1985 return NT_STATUS_INVALID_PARAMETER
;
1988 oldname
= pdb_get_username(old_acct
);
1990 /* rename the posix user */
1991 rename_script
= SMB_STRDUP(lp_renameuser_script());
1992 if (rename_script
== NULL
) {
1993 return NT_STATUS_NO_MEMORY
;
1996 if (!(*rename_script
)) {
1997 SAFE_FREE(rename_script
);
1998 return NT_STATUS_ACCESS_DENIED
;
2001 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2004 /* We have to allow the account name to end with a '$'.
2005 Also, follow the semantics in _samr_create_user() and lower case the
2006 posix name but preserve the case in passdb */
2008 fstrcpy( oldname_lower
, oldname
);
2009 strlower_m( oldname_lower
);
2010 fstrcpy( newname_lower
, newname
);
2011 strlower_m( newname_lower
);
2012 rename_script
= realloc_string_sub2(rename_script
,
2017 if (!rename_script
) {
2018 return NT_STATUS_NO_MEMORY
;
2020 rename_script
= realloc_string_sub2(rename_script
,
2025 rc
= smbrun(rename_script
, NULL
);
2027 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2028 rename_script
, rc
));
2030 SAFE_FREE(rename_script
);
2033 smb_nscd_flush_user_cache();
2037 return NT_STATUS_UNSUCCESSFUL
;
2039 return NT_STATUS_OK
;
2042 /**********************************************************************
2043 Helper function to determine for update_sam_account whether
2044 we need LDAP modification.
2045 *********************************************************************/
2047 static bool element_is_set_or_changed(const struct samu
*sampass
,
2048 enum pdb_elements element
)
2050 return (IS_SAM_SET(sampass
, element
) ||
2051 IS_SAM_CHANGED(sampass
, element
));
2054 /**********************************************************************
2055 Add struct samu to LDAP.
2056 *********************************************************************/
2058 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2060 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2061 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2063 LDAPMessage
*result
= NULL
;
2064 LDAPMessage
*entry
= NULL
;
2065 LDAPMod
**mods
= NULL
;
2066 int ldap_op
= LDAP_MOD_REPLACE
;
2068 const char **attr_list
;
2069 char *escape_user
= NULL
;
2070 const char *username
= pdb_get_username(newpwd
);
2071 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
2072 char *filter
= NULL
;
2074 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2075 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2078 return NT_STATUS_NO_MEMORY
;
2081 if (!username
|| !*username
) {
2082 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2083 status
= NT_STATUS_INVALID_PARAMETER
;
2087 /* free this list after the second search or in case we exit on failure */
2088 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2090 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2092 if (rc
!= LDAP_SUCCESS
) {
2096 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2097 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2101 ldap_msgfree(result
);
2104 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2105 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2107 if (rc
== LDAP_SUCCESS
) {
2108 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2109 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2110 "already in the base, with samba "
2111 "attributes\n", sid_string_dbg(sid
)));
2114 ldap_msgfree(result
);
2119 /* does the entry already exist but without a samba attributes?
2120 we need to return the samba attributes here */
2122 escape_user
= escape_ldap_string_alloc( username
);
2123 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2125 status
= NT_STATUS_NO_MEMORY
;
2128 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2130 status
= NT_STATUS_NO_MEMORY
;
2133 SAFE_FREE(escape_user
);
2135 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2136 filter
, attr_list
, &result
);
2137 if ( rc
!= LDAP_SUCCESS
) {
2141 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2143 if (num_result
> 1) {
2144 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2148 /* Check if we need to update an existing entry */
2149 if (num_result
== 1) {
2152 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2153 ldap_op
= LDAP_MOD_REPLACE
;
2154 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2155 tmp
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
2159 dn
= talloc_asprintf(ctx
, "%s", tmp
);
2162 status
= NT_STATUS_NO_MEMORY
;
2166 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2168 /* There might be a SID for this account already - say an idmap entry */
2170 filter
= talloc_asprintf(ctx
,
2171 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2172 get_userattr_key2string(ldap_state
->schema_ver
,
2173 LDAP_ATTR_USER_SID
),
2174 sid_string_talloc(ctx
, sid
),
2175 LDAP_OBJ_IDMAP_ENTRY
,
2176 LDAP_OBJ_SID_ENTRY
);
2178 status
= NT_STATUS_NO_MEMORY
;
2182 /* free old result before doing a new search */
2183 if (result
!= NULL
) {
2184 ldap_msgfree(result
);
2187 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2188 filter
, attr_list
, &result
);
2190 if ( rc
!= LDAP_SUCCESS
) {
2194 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2196 if (num_result
> 1) {
2197 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2201 /* Check if we need to update an existing entry */
2202 if (num_result
== 1) {
2205 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2206 ldap_op
= LDAP_MOD_REPLACE
;
2207 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2208 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
2212 dn
= talloc_asprintf(ctx
, "%s", tmp
);
2215 status
= NT_STATUS_NO_MEMORY
;
2221 if (num_result
== 0) {
2222 char *escape_username
;
2223 /* Check if we need to add an entry */
2224 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2225 ldap_op
= LDAP_MOD_ADD
;
2227 escape_username
= escape_rdn_val_string_alloc(username
);
2228 if (!escape_username
) {
2229 status
= NT_STATUS_NO_MEMORY
;
2233 if (username
[strlen(username
)-1] == '$') {
2234 dn
= talloc_asprintf(ctx
,
2237 lp_ldap_machine_suffix());
2239 dn
= talloc_asprintf(ctx
,
2242 lp_ldap_user_suffix());
2245 SAFE_FREE(escape_username
);
2247 status
= NT_STATUS_NO_MEMORY
;
2252 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2253 element_is_set_or_changed
)) {
2254 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2256 ldap_mods_free(mods
, true);
2262 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2265 switch ( ldap_state
->schema_ver
) {
2266 case SCHEMAVER_SAMBAACCOUNT
:
2267 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2269 case SCHEMAVER_SAMBASAMACCOUNT
:
2270 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2273 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2277 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2278 if (!NT_STATUS_IS_OK(ret
)) {
2279 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2280 pdb_get_username(newpwd
),dn
));
2281 ldap_mods_free(mods
, true);
2285 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2286 ldap_mods_free(mods
, true);
2288 status
= NT_STATUS_OK
;
2293 SAFE_FREE(escape_user
);
2295 ldap_msgfree(result
);
2300 /**********************************************************************
2301 *********************************************************************/
2303 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2305 LDAPMessage
** result
)
2307 int scope
= LDAP_SCOPE_SUBTREE
;
2309 const char **attr_list
;
2311 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2312 rc
= smbldap_search(ldap_state
->smbldap_state
,
2313 lp_ldap_group_suffix (), scope
,
2314 filter
, attr_list
, 0, result
);
2315 TALLOC_FREE(attr_list
);
2320 /**********************************************************************
2321 *********************************************************************/
2323 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2324 GROUP_MAP
*map
, LDAPMessage
*entry
)
2327 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2329 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2330 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2331 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2336 temp
= smbldap_talloc_single_attribute(
2337 ldap_state
->smbldap_state
->ldap_struct
,
2339 get_attr_key2string(groupmap_attr_list
,
2340 LDAP_ATTR_GIDNUMBER
),
2343 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2344 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2348 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2350 map
->gid
= (gid_t
)atol(temp
);
2353 temp
= smbldap_talloc_single_attribute(
2354 ldap_state
->smbldap_state
->ldap_struct
,
2356 get_attr_key2string(groupmap_attr_list
,
2357 LDAP_ATTR_GROUP_SID
),
2360 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2361 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2366 if (!string_to_sid(&map
->sid
, temp
)) {
2367 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2373 temp
= smbldap_talloc_single_attribute(
2374 ldap_state
->smbldap_state
->ldap_struct
,
2376 get_attr_key2string(groupmap_attr_list
,
2377 LDAP_ATTR_GROUP_TYPE
),
2380 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2381 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2385 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2387 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2388 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2389 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2395 temp
= smbldap_talloc_single_attribute(
2396 ldap_state
->smbldap_state
->ldap_struct
,
2398 get_attr_key2string(groupmap_attr_list
,
2399 LDAP_ATTR_DISPLAY_NAME
),
2402 temp
= smbldap_talloc_single_attribute(
2403 ldap_state
->smbldap_state
->ldap_struct
,
2405 get_attr_key2string(groupmap_attr_list
,
2409 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2410 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2415 fstrcpy(map
->nt_name
, temp
);
2418 temp
= smbldap_talloc_single_attribute(
2419 ldap_state
->smbldap_state
->ldap_struct
,
2421 get_attr_key2string(groupmap_attr_list
,
2425 temp
= talloc_strdup(ctx
, "");
2431 fstrcpy(map
->comment
, temp
);
2433 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2434 store_gid_sid_cache(&map
->sid
, map
->gid
);
2441 /**********************************************************************
2442 *********************************************************************/
2444 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2448 struct ldapsam_privates
*ldap_state
=
2449 (struct ldapsam_privates
*)methods
->private_data
;
2450 LDAPMessage
*result
= NULL
;
2451 LDAPMessage
*entry
= NULL
;
2454 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2456 return NT_STATUS_NO_SUCH_GROUP
;
2459 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2462 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2464 ldap_msgfree(result
);
2465 return NT_STATUS_NO_SUCH_GROUP
;
2469 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2470 "count=%d\n", filter
, count
));
2471 ldap_msgfree(result
);
2472 return NT_STATUS_NO_SUCH_GROUP
;
2475 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2478 ldap_msgfree(result
);
2479 return NT_STATUS_UNSUCCESSFUL
;
2482 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2483 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2484 "group filter %s\n", filter
));
2485 ldap_msgfree(result
);
2486 return NT_STATUS_NO_SUCH_GROUP
;
2489 ldap_msgfree(result
);
2490 return NT_STATUS_OK
;
2493 /**********************************************************************
2494 *********************************************************************/
2496 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2499 char *filter
= NULL
;
2503 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2505 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2506 sid_to_fstring(tmp
, &sid
)) < 0) {
2507 return NT_STATUS_NO_MEMORY
;
2510 status
= ldapsam_getgroup(methods
, filter
, map
);
2515 /**********************************************************************
2516 *********************************************************************/
2518 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2521 char *filter
= NULL
;
2524 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2526 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2527 (unsigned long)gid
) < 0) {
2528 return NT_STATUS_NO_MEMORY
;
2531 status
= ldapsam_getgroup(methods
, filter
, map
);
2536 /**********************************************************************
2537 *********************************************************************/
2539 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2542 char *filter
= NULL
;
2543 char *escape_name
= escape_ldap_string_alloc(name
);
2547 return NT_STATUS_NO_MEMORY
;
2550 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2552 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2553 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2555 SAFE_FREE(escape_name
);
2556 return NT_STATUS_NO_MEMORY
;
2559 SAFE_FREE(escape_name
);
2560 status
= ldapsam_getgroup(methods
, filter
, map
);
2565 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2567 const DOM_SID
*domain_sid
,
2573 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2574 str
, sizeof(str
)-1)) {
2575 DEBUG(10, ("Could not find sambaSID attribute\n"));
2579 if (!string_to_sid(&sid
, str
)) {
2580 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2584 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2585 DEBUG(10, ("SID %s is not in expected domain %s\n",
2586 str
, sid_string_dbg(domain_sid
)));
2590 if (!sid_peek_rid(&sid
, rid
)) {
2591 DEBUG(10, ("Could not peek into RID\n"));
2598 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2599 TALLOC_CTX
*mem_ctx
,
2600 const DOM_SID
*group
,
2601 uint32
**pp_member_rids
,
2602 size_t *p_num_members
)
2604 struct ldapsam_privates
*ldap_state
=
2605 (struct ldapsam_privates
*)methods
->private_data
;
2606 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2607 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2608 const char *sid_attrs
[] = { "sambaSID", NULL
};
2609 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2610 LDAPMessage
*result
= NULL
;
2613 char **values
= NULL
;
2618 *pp_member_rids
= NULL
;
2621 filter
= talloc_asprintf(mem_ctx
,
2622 "(&(objectClass=%s)"
2625 LDAP_OBJ_POSIXGROUP
,
2627 sid_string_talloc(mem_ctx
, group
));
2628 if (filter
== NULL
) {
2629 ret
= NT_STATUS_NO_MEMORY
;
2633 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2634 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2637 if (rc
!= LDAP_SUCCESS
)
2640 talloc_autofree_ldapmsg(mem_ctx
, result
);
2642 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2645 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2646 sid_string_dbg(group
)));
2647 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2652 ret
= NT_STATUS_NO_SUCH_GROUP
;
2656 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2660 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2662 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2663 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2667 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2671 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2672 if (filter
== NULL
) {
2673 ret
= NT_STATUS_NO_MEMORY
;
2677 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2678 char *escape_memberuid
;
2680 escape_memberuid
= escape_ldap_string_alloc(*memberuid
);
2681 if (escape_memberuid
== NULL
) {
2682 ret
= NT_STATUS_NO_MEMORY
;
2686 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2687 if (filter
== NULL
) {
2688 SAFE_FREE(escape_memberuid
);
2689 ret
= NT_STATUS_NO_MEMORY
;
2693 SAFE_FREE(escape_memberuid
);
2696 filter
= talloc_asprintf_append_buffer(filter
, "))");
2697 if (filter
== NULL
) {
2698 ret
= NT_STATUS_NO_MEMORY
;
2702 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2703 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2706 if (rc
!= LDAP_SUCCESS
)
2709 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2710 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2712 talloc_autofree_ldapmsg(mem_ctx
, result
);
2714 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2716 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2722 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2726 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2727 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2728 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2732 if (!string_to_sid(&sid
, sidstr
))
2735 if (!sid_check_is_in_our_domain(&sid
)) {
2736 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2737 "in our domain\n"));
2738 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2742 sid_peek_rid(&sid
, &rid
);
2744 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2746 ret
= NT_STATUS_NO_MEMORY
;
2752 filter
= talloc_asprintf(mem_ctx
,
2753 "(&(objectClass=%s)"
2755 LDAP_OBJ_SAMBASAMACCOUNT
,
2758 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2759 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2762 if (rc
!= LDAP_SUCCESS
)
2765 talloc_autofree_ldapmsg(mem_ctx
, result
);
2767 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2769 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2773 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2775 get_global_sam_sid(),
2777 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2778 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2782 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2784 ret
= NT_STATUS_NO_MEMORY
;
2794 ldap_value_free(values
);
2799 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2800 TALLOC_CTX
*mem_ctx
,
2804 size_t *p_num_groups
)
2806 struct ldapsam_privates
*ldap_state
=
2807 (struct ldapsam_privates
*)methods
->private_data
;
2808 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2810 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2813 LDAPMessage
*result
= NULL
;
2815 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2816 size_t num_sids
, num_gids
;
2818 gid_t primary_gid
= -1;
2823 if (pdb_get_username(user
) == NULL
) {
2824 return NT_STATUS_INVALID_PARAMETER
;
2827 escape_name
= escape_ldap_string_alloc(pdb_get_username(user
));
2828 if (escape_name
== NULL
)
2829 return NT_STATUS_NO_MEMORY
;
2831 /* retrieve the users primary gid */
2832 filter
= talloc_asprintf(mem_ctx
,
2833 "(&(objectClass=%s)(uid=%s))",
2834 LDAP_OBJ_SAMBASAMACCOUNT
,
2836 if (filter
== NULL
) {
2837 ret
= NT_STATUS_NO_MEMORY
;
2841 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2842 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2844 if (rc
!= LDAP_SUCCESS
)
2847 talloc_autofree_ldapmsg(mem_ctx
, result
);
2849 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2853 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2854 ret
= NT_STATUS_NO_SUCH_USER
;
2857 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2859 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2861 DEBUG (1, ("Unable to find the member's gid!\n"));
2862 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2865 primary_gid
= strtoul(gidstr
, NULL
, 10);
2868 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2869 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2873 filter
= talloc_asprintf(mem_ctx
,
2874 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
2875 LDAP_OBJ_POSIXGROUP
, escape_name
, primary_gid
);
2876 if (filter
== NULL
) {
2877 ret
= NT_STATUS_NO_MEMORY
;
2881 rc
= smbldap_search(conn
, lp_ldap_group_suffix(),
2882 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2884 if (rc
!= LDAP_SUCCESS
)
2887 talloc_autofree_ldapmsg(mem_ctx
, result
);
2895 /* We need to add the primary group as the first gid/sid */
2897 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2898 ret
= NT_STATUS_NO_MEMORY
;
2902 /* This sid will be replaced later */
2904 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2906 if (!NT_STATUS_IS_OK(ret
)) {
2910 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2912 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2919 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2921 str
, sizeof(str
)-1))
2924 if (!string_to_sid(&sid
, str
))
2927 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2929 str
, sizeof(str
)-1))
2932 gid
= strtoul(str
, &end
, 10);
2934 if (PTR_DIFF(end
, str
) != strlen(str
))
2937 if (gid
== primary_gid
) {
2938 sid_copy(&(*pp_sids
)[0], &sid
);
2940 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2942 ret
= NT_STATUS_NO_MEMORY
;
2945 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2947 if (!NT_STATUS_IS_OK(ret
)) {
2953 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2954 DEBUG(3, ("primary group of [%s] not found\n",
2955 pdb_get_username(user
)));
2959 *p_num_groups
= num_sids
;
2965 SAFE_FREE(escape_name
);
2969 /**********************************************************************
2970 * Augment a posixGroup object with a sambaGroupMapping domgroup
2971 *********************************************************************/
2973 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2974 struct ldapsam_privates
*ldap_state
,
2977 const char *filter
, *dn
;
2978 LDAPMessage
*msg
, *entry
;
2982 filter
= talloc_asprintf(mem_ctx
,
2983 "(&(objectClass=%s)(gidNumber=%u))",
2984 LDAP_OBJ_POSIXGROUP
, map
->gid
);
2985 if (filter
== NULL
) {
2986 return NT_STATUS_NO_MEMORY
;
2989 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2990 get_attr_list(mem_ctx
, groupmap_attr_list
),
2992 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2994 if ((rc
!= LDAP_SUCCESS
) ||
2995 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
2996 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
2997 return NT_STATUS_NO_SUCH_GROUP
;
3000 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3002 return NT_STATUS_NO_MEMORY
;
3006 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3008 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3009 sid_string_talloc(mem_ctx
, &map
->sid
));
3010 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3011 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3012 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3014 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3016 talloc_autofree_ldapmod(mem_ctx
, mods
);
3018 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3019 if (rc
!= LDAP_SUCCESS
) {
3020 return NT_STATUS_ACCESS_DENIED
;
3023 return NT_STATUS_OK
;
3026 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3029 struct ldapsam_privates
*ldap_state
=
3030 (struct ldapsam_privates
*)methods
->private_data
;
3031 LDAPMessage
*msg
= NULL
;
3032 LDAPMod
**mods
= NULL
;
3033 const char *attrs
[] = { NULL
};
3037 TALLOC_CTX
*mem_ctx
;
3044 mem_ctx
= talloc_new(NULL
);
3045 if (mem_ctx
== NULL
) {
3046 DEBUG(0, ("talloc_new failed\n"));
3047 return NT_STATUS_NO_MEMORY
;
3050 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3051 sid_string_talloc(mem_ctx
, &map
->sid
));
3052 if (filter
== NULL
) {
3053 result
= NT_STATUS_NO_MEMORY
;
3057 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3058 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3059 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3061 if ((rc
== LDAP_SUCCESS
) &&
3062 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3064 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3065 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3066 result
= NT_STATUS_GROUP_EXISTS
;
3070 switch (map
->sid_name_use
) {
3072 case SID_NAME_DOM_GRP
:
3073 /* To map a domain group we need to have a posix group
3075 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3079 case SID_NAME_ALIAS
:
3080 if (!sid_check_is_in_our_domain(&map
->sid
)
3081 && !sid_check_is_in_builtin(&map
->sid
) )
3083 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3084 sid_string_dbg(&map
->sid
)));
3085 result
= NT_STATUS_INVALID_PARAMETER
;
3091 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3092 sid_type_lookup(map
->sid_name_use
)));
3093 result
= NT_STATUS_INVALID_PARAMETER
;
3097 /* Domain groups have been mapped in a separate routine, we have to
3098 * create an alias now */
3100 if (map
->gid
== -1) {
3101 DEBUG(10, ("Refusing to map gid==-1\n"));
3102 result
= NT_STATUS_INVALID_PARAMETER
;
3106 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3107 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
3108 "add\n", map
->gid
, sid_string_dbg(&sid
)));
3109 result
= NT_STATUS_GROUP_EXISTS
;
3113 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3114 * the best we can get out of LDAP. */
3116 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3117 sid_string_talloc(mem_ctx
, &map
->sid
),
3118 lp_ldap_group_suffix());
3120 result
= NT_STATUS_NO_MEMORY
;
3126 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3127 LDAP_OBJ_SID_ENTRY
);
3128 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3130 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3131 sid_string_talloc(mem_ctx
, &map
->sid
));
3132 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3133 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3134 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3136 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3138 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3139 talloc_asprintf(mem_ctx
, "%u", map
->gid
));
3140 talloc_autofree_ldapmod(mem_ctx
, mods
);
3142 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3144 result
= (rc
== LDAP_SUCCESS
) ?
3145 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3148 TALLOC_FREE(mem_ctx
);
3152 /**********************************************************************
3153 * Update a group mapping entry. We're quite strict about what can be changed:
3154 * Only the description and displayname may be changed. It simply does not
3155 * make any sense to change the SID, gid or the type in a mapping.
3156 *********************************************************************/
3158 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3161 struct ldapsam_privates
*ldap_state
=
3162 (struct ldapsam_privates
*)methods
->private_data
;
3164 const char *filter
, *dn
;
3165 LDAPMessage
*msg
= NULL
;
3166 LDAPMessage
*entry
= NULL
;
3167 LDAPMod
**mods
= NULL
;
3168 TALLOC_CTX
*mem_ctx
;
3171 mem_ctx
= talloc_new(NULL
);
3172 if (mem_ctx
== NULL
) {
3173 DEBUG(0, ("talloc_new failed\n"));
3174 return NT_STATUS_NO_MEMORY
;
3177 /* Make 100% sure that sid, gid and type are not changed by looking up
3178 * exactly the values we're given in LDAP. */
3180 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3181 "(sambaSid=%s)(gidNumber=%u)"
3182 "(sambaGroupType=%d))",
3184 sid_string_talloc(mem_ctx
, &map
->sid
),
3185 map
->gid
, map
->sid_name_use
);
3186 if (filter
== NULL
) {
3187 result
= NT_STATUS_NO_MEMORY
;
3191 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3192 get_attr_list(mem_ctx
, groupmap_attr_list
),
3194 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3196 if ((rc
!= LDAP_SUCCESS
) ||
3197 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3198 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3199 result
= NT_STATUS_NO_SUCH_GROUP
;
3203 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3206 result
= NT_STATUS_NO_MEMORY
;
3211 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3213 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3215 talloc_autofree_ldapmod(mem_ctx
, mods
);
3218 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3219 "nothing to do\n"));
3220 result
= NT_STATUS_OK
;
3224 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3226 if (rc
!= LDAP_SUCCESS
) {
3227 result
= NT_STATUS_ACCESS_DENIED
;
3231 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3232 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3234 result
= NT_STATUS_OK
;
3237 TALLOC_FREE(mem_ctx
);
3241 /**********************************************************************
3242 *********************************************************************/
3244 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3247 struct ldapsam_privates
*priv
=
3248 (struct ldapsam_privates
*)methods
->private_data
;
3249 LDAPMessage
*msg
, *entry
;
3252 TALLOC_CTX
*mem_ctx
;
3255 mem_ctx
= talloc_new(NULL
);
3256 if (mem_ctx
== NULL
) {
3257 DEBUG(0, ("talloc_new failed\n"));
3258 return NT_STATUS_NO_MEMORY
;
3261 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3262 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3263 sid_string_talloc(mem_ctx
, &sid
));
3264 if (filter
== NULL
) {
3265 result
= NT_STATUS_NO_MEMORY
;
3268 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3269 get_attr_list(mem_ctx
, groupmap_attr_list
),
3271 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3273 if ((rc
!= LDAP_SUCCESS
) ||
3274 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3275 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3276 result
= NT_STATUS_NO_SUCH_GROUP
;
3280 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3281 get_attr_list(mem_ctx
,
3282 groupmap_attr_list_to_delete
));
3284 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3285 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3286 const char *attrs
[] = { "sambaGroupType", "description",
3287 "displayName", "sambaSIDList",
3290 /* Second try. Don't delete the sambaSID attribute, this is
3291 for "old" entries that are tacked on a winbind
3294 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3295 LDAP_OBJ_GROUPMAP
, attrs
);
3298 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3299 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3300 const char *attrs
[] = { "sambaGroupType", "description",
3301 "displayName", "sambaSIDList",
3302 "gidNumber", NULL
};
3304 /* Third try. This is a post-3.0.21 alias (containing only
3305 * sambaSidEntry and sambaGroupMapping classes), we also have
3306 * to delete the gidNumber attribute, only the sambaSidEntry
3309 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3310 LDAP_OBJ_GROUPMAP
, attrs
);
3313 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3316 TALLOC_FREE(mem_ctx
);
3320 /**********************************************************************
3321 *********************************************************************/
3323 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3326 struct ldapsam_privates
*ldap_state
=
3327 (struct ldapsam_privates
*)my_methods
->private_data
;
3328 char *filter
= NULL
;
3330 const char **attr_list
;
3332 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3334 return NT_STATUS_NO_MEMORY
;
3336 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3337 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3338 LDAP_SCOPE_SUBTREE
, filter
,
3339 attr_list
, 0, &ldap_state
->result
);
3340 TALLOC_FREE(attr_list
);
3342 if (rc
!= LDAP_SUCCESS
) {
3343 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3344 ldap_err2string(rc
)));
3345 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3346 lp_ldap_group_suffix(), filter
));
3347 ldap_msgfree(ldap_state
->result
);
3348 ldap_state
->result
= NULL
;
3349 TALLOC_FREE(filter
);
3350 return NT_STATUS_UNSUCCESSFUL
;
3353 TALLOC_FREE(filter
);
3355 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3356 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3357 ldap_state
->result
)));
3360 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3361 ldap_state
->result
);
3362 ldap_state
->index
= 0;
3364 return NT_STATUS_OK
;
3367 /**********************************************************************
3368 *********************************************************************/
3370 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3372 ldapsam_endsampwent(my_methods
);
3375 /**********************************************************************
3376 *********************************************************************/
3378 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3381 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3382 struct ldapsam_privates
*ldap_state
=
3383 (struct ldapsam_privates
*)my_methods
->private_data
;
3387 if (!ldap_state
->entry
)
3390 ldap_state
->index
++;
3391 bret
= init_group_from_ldap(ldap_state
, map
,
3395 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3399 return NT_STATUS_OK
;
3402 /**********************************************************************
3403 *********************************************************************/
3405 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3406 const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
,
3407 GROUP_MAP
**pp_rmap
,
3408 size_t *p_num_entries
,
3417 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3418 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3420 return NT_STATUS_ACCESS_DENIED
;
3423 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3424 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3425 sid_name_use
!= map
.sid_name_use
) {
3426 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3427 "not of the requested type\n", map
.nt_name
));
3430 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3431 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3432 "non mapped\n", map
.nt_name
));
3436 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3438 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3439 "enlarge group map!\n"));
3440 return NT_STATUS_UNSUCCESSFUL
;
3443 (*pp_rmap
)[entries
] = map
;
3448 ldapsam_endsamgrent(methods
);
3450 *p_num_entries
= entries
;
3452 return NT_STATUS_OK
;
3455 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3456 const DOM_SID
*alias
,
3457 const DOM_SID
*member
,
3460 struct ldapsam_privates
*ldap_state
=
3461 (struct ldapsam_privates
*)methods
->private_data
;
3463 LDAPMessage
*result
= NULL
;
3464 LDAPMessage
*entry
= NULL
;
3466 LDAPMod
**mods
= NULL
;
3468 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3471 char *filter
= NULL
;
3473 if (sid_check_is_in_builtin(alias
)) {
3474 type
= SID_NAME_ALIAS
;
3477 if (sid_check_is_in_our_domain(alias
)) {
3478 type
= SID_NAME_ALIAS
;
3481 if (type
== SID_NAME_USE_NONE
) {
3482 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3483 sid_string_dbg(alias
)));
3484 return NT_STATUS_NO_SUCH_ALIAS
;
3487 if (asprintf(&filter
,
3488 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3489 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3491 return NT_STATUS_NO_MEMORY
;
3494 if (ldapsam_search_one_group(ldap_state
, filter
,
3495 &result
) != LDAP_SUCCESS
) {
3497 return NT_STATUS_NO_SUCH_ALIAS
;
3500 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3504 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3505 ldap_msgfree(result
);
3507 return NT_STATUS_NO_SUCH_ALIAS
;
3511 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3512 "filter %s: count=%d\n", filter
, count
));
3513 ldap_msgfree(result
);
3515 return NT_STATUS_NO_SUCH_ALIAS
;
3520 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3524 ldap_msgfree(result
);
3525 return NT_STATUS_UNSUCCESSFUL
;
3528 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
3530 ldap_msgfree(result
);
3531 return NT_STATUS_UNSUCCESSFUL
;
3534 smbldap_set_mod(&mods
, modop
,
3535 get_attr_key2string(groupmap_attr_list
,
3536 LDAP_ATTR_SID_LIST
),
3537 sid_to_fstring(tmp
, member
));
3539 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3541 ldap_mods_free(mods
, True
);
3542 ldap_msgfree(result
);
3545 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3546 return NT_STATUS_MEMBER_IN_ALIAS
;
3549 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3550 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3553 if (rc
!= LDAP_SUCCESS
) {
3554 return NT_STATUS_UNSUCCESSFUL
;
3557 return NT_STATUS_OK
;
3560 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3561 const DOM_SID
*alias
,
3562 const DOM_SID
*member
)
3564 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3567 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3568 const DOM_SID
*alias
,
3569 const DOM_SID
*member
)
3571 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3575 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3576 const DOM_SID
*alias
,
3577 DOM_SID
**pp_members
,
3578 size_t *p_num_members
)
3580 struct ldapsam_privates
*ldap_state
=
3581 (struct ldapsam_privates
*)methods
->private_data
;
3582 LDAPMessage
*result
= NULL
;
3583 LDAPMessage
*entry
= NULL
;
3585 char **values
= NULL
;
3587 char *filter
= NULL
;
3588 size_t num_members
= 0;
3589 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3595 if (sid_check_is_in_builtin(alias
)) {
3596 type
= SID_NAME_ALIAS
;
3599 if (sid_check_is_in_our_domain(alias
)) {
3600 type
= SID_NAME_ALIAS
;
3603 if (type
== SID_NAME_USE_NONE
) {
3604 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3605 sid_string_dbg(alias
)));
3606 return NT_STATUS_NO_SUCH_ALIAS
;
3609 if (asprintf(&filter
,
3610 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3611 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3613 return NT_STATUS_NO_MEMORY
;
3616 if (ldapsam_search_one_group(ldap_state
, filter
,
3617 &result
) != LDAP_SUCCESS
) {
3619 return NT_STATUS_NO_SUCH_ALIAS
;
3622 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3626 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3627 ldap_msgfree(result
);
3629 return NT_STATUS_NO_SUCH_ALIAS
;
3633 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3634 "filter %s: count=%d\n", filter
, count
));
3635 ldap_msgfree(result
);
3637 return NT_STATUS_NO_SUCH_ALIAS
;
3642 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3646 ldap_msgfree(result
);
3647 return NT_STATUS_UNSUCCESSFUL
;
3650 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3652 get_attr_key2string(groupmap_attr_list
,
3653 LDAP_ATTR_SID_LIST
));
3655 if (values
== NULL
) {
3656 ldap_msgfree(result
);
3657 return NT_STATUS_OK
;
3660 count
= ldap_count_values(values
);
3662 for (i
=0; i
<count
; i
++) {
3666 if (!string_to_sid(&member
, values
[i
]))
3669 status
= add_sid_to_array(NULL
, &member
, pp_members
,
3671 if (!NT_STATUS_IS_OK(status
)) {
3672 ldap_value_free(values
);
3673 ldap_msgfree(result
);
3678 *p_num_members
= num_members
;
3679 ldap_value_free(values
);
3680 ldap_msgfree(result
);
3682 return NT_STATUS_OK
;
3685 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3686 TALLOC_CTX
*mem_ctx
,
3687 const DOM_SID
*domain_sid
,
3688 const DOM_SID
*members
,
3690 uint32
**pp_alias_rids
,
3691 size_t *p_num_alias_rids
)
3693 struct ldapsam_privates
*ldap_state
=
3694 (struct ldapsam_privates
*)methods
->private_data
;
3697 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3699 LDAPMessage
*result
= NULL
;
3700 LDAPMessage
*entry
= NULL
;
3704 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3706 if (sid_check_is_builtin(domain_sid
)) {
3707 type
= SID_NAME_ALIAS
;
3710 if (sid_check_is_domain(domain_sid
)) {
3711 type
= SID_NAME_ALIAS
;
3714 if (type
== SID_NAME_USE_NONE
) {
3715 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3716 sid_string_dbg(domain_sid
)));
3717 return NT_STATUS_UNSUCCESSFUL
;
3720 filter
= talloc_asprintf(mem_ctx
,
3721 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3722 LDAP_OBJ_GROUPMAP
, type
);
3724 for (i
=0; i
<num_members
; i
++)
3725 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3727 sid_string_talloc(mem_ctx
,
3730 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3732 if (filter
== NULL
) {
3733 return NT_STATUS_NO_MEMORY
;
3736 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
3737 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3739 if (rc
!= LDAP_SUCCESS
)
3740 return NT_STATUS_UNSUCCESSFUL
;
3742 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3744 for (entry
= ldap_first_entry(ldap_struct
, result
);
3746 entry
= ldap_next_entry(ldap_struct
, entry
))
3752 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3758 if (!string_to_sid(&sid
, sid_str
))
3761 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3764 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3765 p_num_alias_rids
)) {
3766 ldap_msgfree(result
);
3767 return NT_STATUS_NO_MEMORY
;
3771 ldap_msgfree(result
);
3772 return NT_STATUS_OK
;
3775 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3779 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3781 LDAPMod
**mods
= NULL
;
3782 fstring value_string
;
3783 const char *policy_attr
= NULL
;
3785 struct ldapsam_privates
*ldap_state
=
3786 (struct ldapsam_privates
*)methods
->private_data
;
3788 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3790 if (!ldap_state
->domain_dn
) {
3791 return NT_STATUS_INVALID_PARAMETER
;
3794 policy_attr
= get_account_policy_attr(policy_index
);
3795 if (policy_attr
== NULL
) {
3796 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3801 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3803 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3805 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3808 ldap_mods_free(mods
, True
);
3810 if (rc
!= LDAP_SUCCESS
) {
3814 if (!cache_account_policy_set(policy_index
, value
)) {
3815 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3816 "update local tdb cache\n"));
3820 return NT_STATUS_OK
;
3823 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3824 int policy_index
, uint32 value
)
3826 return ldapsam_set_account_policy_in_ldap(methods
, policy_index
,
3830 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3834 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3835 LDAPMessage
*result
= NULL
;
3836 LDAPMessage
*entry
= NULL
;
3840 const char *policy_attr
= NULL
;
3842 struct ldapsam_privates
*ldap_state
=
3843 (struct ldapsam_privates
*)methods
->private_data
;
3845 const char *attrs
[2];
3847 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3849 if (!ldap_state
->domain_dn
) {
3850 return NT_STATUS_INVALID_PARAMETER
;
3853 policy_attr
= get_account_policy_attr(policy_index
);
3855 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3856 "policy index: %d\n", policy_index
));
3860 attrs
[0] = policy_attr
;
3863 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3864 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0,
3867 if (rc
!= LDAP_SUCCESS
) {
3871 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3876 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3877 if (entry
== NULL
) {
3881 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3886 *value
= (uint32
)atol(vals
[0]);
3888 ntstatus
= NT_STATUS_OK
;
3892 ldap_value_free(vals
);
3893 ldap_msgfree(result
);
3898 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3900 - if user hasn't decided to use account policies inside LDAP just reuse the
3903 - if there is a valid cache entry, return that
3904 - if there is an LDAP entry, update cache and return
3905 - otherwise set to default, update cache and return
3909 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3910 int policy_index
, uint32
*value
)
3912 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3914 if (cache_account_policy_get(policy_index
, value
)) {
3915 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3917 return NT_STATUS_OK
;
3920 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, policy_index
,
3922 if (NT_STATUS_IS_OK(ntstatus
)) {
3926 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3930 /* should we automagically migrate old tdb value here ? */
3931 if (account_policy_get(policy_index
, value
))
3934 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3935 "default\n", policy_index
));
3938 if (!account_policy_get_default(policy_index
, value
)) {
3944 ntstatus
= ldapsam_set_account_policy(methods
, policy_index
, *value
);
3945 if (!NT_STATUS_IS_OK(ntstatus
)) {
3951 if (!cache_account_policy_set(policy_index
, *value
)) {
3952 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3953 "tdb as a cache\n"));
3954 return NT_STATUS_UNSUCCESSFUL
;
3957 return NT_STATUS_OK
;
3960 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3961 const DOM_SID
*domain_sid
,
3965 enum lsa_SidType
*attrs
)
3967 struct ldapsam_privates
*ldap_state
=
3968 (struct ldapsam_privates
*)methods
->private_data
;
3969 LDAPMessage
*msg
= NULL
;
3971 char *allsids
= NULL
;
3972 int i
, rc
, num_mapped
;
3973 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
3974 TALLOC_CTX
*mem_ctx
;
3978 mem_ctx
= talloc_new(NULL
);
3979 if (mem_ctx
== NULL
) {
3980 DEBUG(0, ("talloc_new failed\n"));
3984 if (!sid_check_is_builtin(domain_sid
) &&
3985 !sid_check_is_domain(domain_sid
)) {
3986 result
= NT_STATUS_INVALID_PARAMETER
;
3990 for (i
=0; i
<num_rids
; i
++)
3991 attrs
[i
] = SID_NAME_UNKNOWN
;
3993 allsids
= talloc_strdup(mem_ctx
, "");
3994 if (allsids
== NULL
) {
3998 for (i
=0; i
<num_rids
; i
++) {
4000 sid_compose(&sid
, domain_sid
, rids
[i
]);
4001 allsids
= talloc_asprintf_append_buffer(
4002 allsids
, "(sambaSid=%s)",
4003 sid_string_talloc(mem_ctx
, &sid
));
4004 if (allsids
== NULL
) {
4009 /* First look for users */
4013 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4015 filter
= talloc_asprintf(
4016 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4017 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4019 if (filter
== NULL
) {
4023 rc
= smbldap_search(ldap_state
->smbldap_state
,
4024 lp_ldap_user_suffix(),
4025 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4027 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4030 if (rc
!= LDAP_SUCCESS
)
4033 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4036 for (entry
= ldap_first_entry(ld
, msg
);
4038 entry
= ldap_next_entry(ld
, entry
)) {
4043 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4045 DEBUG(2, ("Could not find sid from ldap entry\n"));
4049 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4052 DEBUG(2, ("Could not retrieve uid attribute\n"));
4056 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4057 if (rid
== rids
[rid_index
])
4061 if (rid_index
== num_rids
) {
4062 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4066 attrs
[rid_index
] = SID_NAME_USER
;
4067 names
[rid_index
] = name
;
4071 if (num_mapped
== num_rids
) {
4072 /* No need to look for groups anymore -- we're done */
4073 result
= NT_STATUS_OK
;
4077 /* Same game for groups */
4081 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4082 "sambaGroupType", NULL
};
4084 filter
= talloc_asprintf(
4085 mem_ctx
, "(&(objectClass=%s)(|%s))",
4086 LDAP_OBJ_GROUPMAP
, allsids
);
4087 if (filter
== NULL
) {
4091 rc
= smbldap_search(ldap_state
->smbldap_state
,
4092 lp_ldap_group_suffix(),
4093 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4095 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4098 if (rc
!= LDAP_SUCCESS
)
4101 /* ldap_struct might have changed due to a reconnect */
4103 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4105 /* For consistency checks, we already checked we're only domain or builtin */
4107 is_builtin
= sid_check_is_builtin(domain_sid
);
4109 for (entry
= ldap_first_entry(ld
, msg
);
4111 entry
= ldap_next_entry(ld
, entry
))
4116 enum lsa_SidType type
;
4117 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4119 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4122 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4127 type
= (enum lsa_SidType
)atol(attr
);
4129 /* Consistency checks */
4130 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4131 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4132 (type
!= SID_NAME_DOM_GRP
)))) {
4133 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4136 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4138 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4142 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4145 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4147 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4151 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4156 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4157 if (rid
== rids
[rid_index
])
4161 if (rid_index
== num_rids
) {
4162 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4166 attrs
[rid_index
] = type
;
4167 names
[rid_index
] = attr
;
4171 result
= NT_STATUS_NONE_MAPPED
;
4174 result
= (num_mapped
== num_rids
) ?
4175 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4177 TALLOC_FREE(mem_ctx
);
4181 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4183 char *filter
= NULL
;
4184 char *escaped
= NULL
;
4185 char *result
= NULL
;
4187 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4188 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4192 escaped
= escape_ldap_string_alloc(username
);
4193 if (escaped
== NULL
) goto done
;
4195 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4204 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4208 const char **result
;
4210 va_start(ap
, mem_ctx
);
4211 while (va_arg(ap
, const char *) != NULL
)
4215 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4219 va_start(ap
, mem_ctx
);
4220 for (i
=0; i
<num
; i
++) {
4221 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4222 if (result
[i
] == NULL
) {
4223 talloc_free(result
);
4233 struct ldap_search_state
{
4234 struct smbldap_state
*connection
;
4244 void *pagedresults_cookie
;
4246 LDAPMessage
*entries
, *current_entry
;
4247 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4248 TALLOC_CTX
*mem_ctx
,
4249 LDAP
*ld
, LDAPMessage
*entry
,
4250 struct samr_displayentry
*result
);
4253 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4255 struct ldap_search_state
*state
=
4256 (struct ldap_search_state
*)search
->private_data
;
4258 int rc
= LDAP_OPERATIONS_ERROR
;
4260 state
->entries
= NULL
;
4262 if (state
->connection
->paged_results
) {
4263 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4264 state
->scope
, state
->filter
,
4265 state
->attrs
, state
->attrsonly
,
4266 lp_ldap_page_size(), &state
->entries
,
4267 &state
->pagedresults_cookie
);
4270 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4272 if (state
->entries
!= NULL
) {
4273 /* Left over from unsuccessful paged attempt */
4274 ldap_msgfree(state
->entries
);
4275 state
->entries
= NULL
;
4278 rc
= smbldap_search(state
->connection
, state
->base
,
4279 state
->scope
, state
->filter
, state
->attrs
,
4280 state
->attrsonly
, &state
->entries
);
4282 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4285 /* Ok, the server was lying. It told us it could do paged
4286 * searches when it could not. */
4287 state
->connection
->paged_results
= False
;
4290 ld
= state
->connection
->ldap_struct
;
4292 DEBUG(5, ("Don't have an LDAP connection right after a "
4296 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4298 if (state
->current_entry
== NULL
) {
4299 ldap_msgfree(state
->entries
);
4300 state
->entries
= NULL
;
4306 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4308 struct ldap_search_state
*state
=
4309 (struct ldap_search_state
*)search
->private_data
;
4312 if (!state
->connection
->paged_results
) {
4313 /* There is no next page when there are no paged results */
4317 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4318 state
->scope
, state
->filter
, state
->attrs
,
4319 state
->attrsonly
, lp_ldap_page_size(),
4321 &state
->pagedresults_cookie
);
4323 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4326 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4328 if (state
->current_entry
== NULL
) {
4329 ldap_msgfree(state
->entries
);
4330 state
->entries
= NULL
;
4336 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4337 struct samr_displayentry
*entry
)
4339 struct ldap_search_state
*state
=
4340 (struct ldap_search_state
*)search
->private_data
;
4344 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4347 if ((state
->entries
== NULL
) &&
4348 !ldapsam_search_nextpage(search
))
4351 result
= state
->ldap2displayentry(state
, search
->mem_ctx
, state
->connection
->ldap_struct
,
4352 state
->current_entry
, entry
);
4356 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4357 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4358 if (dn
!= NULL
) ldap_memfree(dn
);
4361 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4363 if (state
->current_entry
== NULL
) {
4364 ldap_msgfree(state
->entries
);
4365 state
->entries
= NULL
;
4368 if (!result
) goto retry
;
4373 static void ldapsam_search_end(struct pdb_search
*search
)
4375 struct ldap_search_state
*state
=
4376 (struct ldap_search_state
*)search
->private_data
;
4379 if (state
->pagedresults_cookie
== NULL
)
4382 if (state
->entries
!= NULL
)
4383 ldap_msgfree(state
->entries
);
4385 state
->entries
= NULL
;
4386 state
->current_entry
= NULL
;
4388 if (!state
->connection
->paged_results
)
4391 /* Tell the LDAP server we're not interested in the rest anymore. */
4393 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4394 state
->filter
, state
->attrs
,
4395 state
->attrsonly
, 0, &state
->entries
,
4396 &state
->pagedresults_cookie
);
4398 if (rc
!= LDAP_SUCCESS
)
4399 DEBUG(5, ("Could not end search properly\n"));
4404 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4405 TALLOC_CTX
*mem_ctx
,
4406 LDAP
*ld
, LDAPMessage
*entry
,
4407 struct samr_displayentry
*result
)
4410 size_t converted_size
;
4414 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4415 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4416 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4419 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4420 ldap_value_free(vals
);
4422 if ((state
->acct_flags
!= 0) &&
4423 ((state
->acct_flags
& acct_flags
) == 0))
4426 result
->acct_flags
= acct_flags
;
4427 result
->account_name
= "";
4428 result
->fullname
= "";
4429 result
->description
= "";
4431 vals
= ldap_get_values(ld
, entry
, "uid");
4432 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4433 DEBUG(5, ("\"uid\" not found\n"));
4436 if (!pull_utf8_talloc(mem_ctx
,
4437 CONST_DISCARD(char **, &result
->account_name
),
4438 vals
[0], &converted_size
))
4440 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4444 ldap_value_free(vals
);
4446 vals
= ldap_get_values(ld
, entry
, "displayName");
4447 if ((vals
== NULL
) || (vals
[0] == NULL
))
4448 DEBUG(8, ("\"displayName\" not found\n"));
4449 else if (!pull_utf8_talloc(mem_ctx
,
4450 CONST_DISCARD(char **, &result
->fullname
),
4451 vals
[0], &converted_size
))
4453 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4457 ldap_value_free(vals
);
4459 vals
= ldap_get_values(ld
, entry
, "description");
4460 if ((vals
== NULL
) || (vals
[0] == NULL
))
4461 DEBUG(8, ("\"description\" not found\n"));
4462 else if (!pull_utf8_talloc(mem_ctx
,
4463 CONST_DISCARD(char **, &result
->description
),
4464 vals
[0], &converted_size
))
4466 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4470 ldap_value_free(vals
);
4472 if ((result
->account_name
== NULL
) ||
4473 (result
->fullname
== NULL
) ||
4474 (result
->description
== NULL
)) {
4475 DEBUG(0, ("talloc failed\n"));
4479 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4480 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4481 DEBUG(0, ("\"objectSid\" not found\n"));
4485 if (!string_to_sid(&sid
, vals
[0])) {
4486 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4487 ldap_value_free(vals
);
4490 ldap_value_free(vals
);
4492 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4493 DEBUG(0, ("sid %s does not belong to our domain\n",
4494 sid_string_dbg(&sid
)));
4502 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4503 struct pdb_search
*search
,
4506 struct ldapsam_privates
*ldap_state
=
4507 (struct ldapsam_privates
*)methods
->private_data
;
4508 struct ldap_search_state
*state
;
4510 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4511 if (state
== NULL
) {
4512 DEBUG(0, ("talloc failed\n"));
4516 state
->connection
= ldap_state
->smbldap_state
;
4518 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4519 state
->base
= lp_ldap_user_suffix();
4520 else if ((acct_flags
!= 0) &&
4521 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4522 state
->base
= lp_ldap_machine_suffix();
4524 state
->base
= lp_ldap_suffix();
4526 state
->acct_flags
= acct_flags
;
4527 state
->base
= talloc_strdup(search
->mem_ctx
, state
->base
);
4528 state
->scope
= LDAP_SCOPE_SUBTREE
;
4529 state
->filter
= get_ldap_filter(search
->mem_ctx
, "*");
4530 state
->attrs
= talloc_attrs(search
->mem_ctx
, "uid", "sambaSid",
4531 "displayName", "description",
4532 "sambaAcctFlags", NULL
);
4533 state
->attrsonly
= 0;
4534 state
->pagedresults_cookie
= NULL
;
4535 state
->entries
= NULL
;
4536 state
->ldap2displayentry
= ldapuser2displayentry
;
4538 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4539 DEBUG(0, ("talloc failed\n"));
4543 search
->private_data
= state
;
4544 search
->next_entry
= ldapsam_search_next_entry
;
4545 search
->search_end
= ldapsam_search_end
;
4547 return ldapsam_search_firstpage(search
);
4550 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4551 TALLOC_CTX
*mem_ctx
,
4552 LDAP
*ld
, LDAPMessage
*entry
,
4553 struct samr_displayentry
*result
)
4556 size_t converted_size
;
4560 result
->account_name
= "";
4561 result
->fullname
= "";
4562 result
->description
= "";
4565 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4566 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4567 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4569 ldap_value_free(vals
);
4574 group_type
= atoi(vals
[0]);
4576 if ((state
->group_type
!= 0) &&
4577 ((state
->group_type
!= group_type
))) {
4578 ldap_value_free(vals
);
4582 ldap_value_free(vals
);
4584 /* display name is the NT group name */
4586 vals
= ldap_get_values(ld
, entry
, "displayName");
4587 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4588 DEBUG(8, ("\"displayName\" not found\n"));
4590 /* fallback to the 'cn' attribute */
4591 vals
= ldap_get_values(ld
, entry
, "cn");
4592 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4593 DEBUG(5, ("\"cn\" not found\n"));
4596 if (!pull_utf8_talloc(mem_ctx
,
4597 CONST_DISCARD(char **,
4598 &result
->account_name
),
4599 vals
[0], &converted_size
))
4601 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4602 "failed: %s", strerror(errno
)));
4605 else if (!pull_utf8_talloc(mem_ctx
,
4606 CONST_DISCARD(char **,
4607 &result
->account_name
),
4608 vals
[0], &converted_size
))
4610 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4614 ldap_value_free(vals
);
4616 vals
= ldap_get_values(ld
, entry
, "description");
4617 if ((vals
== NULL
) || (vals
[0] == NULL
))
4618 DEBUG(8, ("\"description\" not found\n"));
4619 else if (!pull_utf8_talloc(mem_ctx
,
4620 CONST_DISCARD(char **, &result
->description
),
4621 vals
[0], &converted_size
))
4623 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4626 ldap_value_free(vals
);
4628 if ((result
->account_name
== NULL
) ||
4629 (result
->fullname
== NULL
) ||
4630 (result
->description
== NULL
)) {
4631 DEBUG(0, ("talloc failed\n"));
4635 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4636 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4637 DEBUG(0, ("\"objectSid\" not found\n"));
4639 ldap_value_free(vals
);
4644 if (!string_to_sid(&sid
, vals
[0])) {
4645 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4649 ldap_value_free(vals
);
4651 switch (group_type
) {
4652 case SID_NAME_DOM_GRP
:
4653 case SID_NAME_ALIAS
:
4655 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4656 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4658 DEBUG(0, ("%s is not in our domain\n",
4659 sid_string_dbg(&sid
)));
4665 DEBUG(0,("unkown group type: %d\n", group_type
));
4669 result
->acct_flags
= 0;
4674 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4675 struct pdb_search
*search
,
4677 enum lsa_SidType type
)
4679 struct ldapsam_privates
*ldap_state
=
4680 (struct ldapsam_privates
*)methods
->private_data
;
4681 struct ldap_search_state
*state
;
4684 state
= TALLOC_P(search
->mem_ctx
, struct ldap_search_state
);
4685 if (state
== NULL
) {
4686 DEBUG(0, ("talloc failed\n"));
4690 state
->connection
= ldap_state
->smbldap_state
;
4692 state
->base
= talloc_strdup(search
->mem_ctx
, lp_ldap_group_suffix());
4693 state
->connection
= ldap_state
->smbldap_state
;
4694 state
->scope
= LDAP_SCOPE_SUBTREE
;
4695 state
->filter
= talloc_asprintf(search
->mem_ctx
,
4696 "(&(objectclass=%s)"
4697 "(sambaGroupType=%d)(sambaSID=%s*))",
4699 type
, sid_to_fstring(tmp
, sid
));
4700 state
->attrs
= talloc_attrs(search
->mem_ctx
, "cn", "sambaSid",
4701 "displayName", "description",
4702 "sambaGroupType", NULL
);
4703 state
->attrsonly
= 0;
4704 state
->pagedresults_cookie
= NULL
;
4705 state
->entries
= NULL
;
4706 state
->group_type
= type
;
4707 state
->ldap2displayentry
= ldapgroup2displayentry
;
4709 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4710 DEBUG(0, ("talloc failed\n"));
4714 search
->private_data
= state
;
4715 search
->next_entry
= ldapsam_search_next_entry
;
4716 search
->search_end
= ldapsam_search_end
;
4718 return ldapsam_search_firstpage(search
);
4721 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4722 struct pdb_search
*search
)
4724 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4727 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4728 struct pdb_search
*search
,
4731 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4734 static bool ldapsam_rid_algorithm(struct pdb_methods
*methods
)
4739 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4742 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4744 LDAPMessage
*result
= NULL
;
4745 LDAPMessage
*entry
= NULL
;
4746 LDAPMod
**mods
= NULL
;
4753 TALLOC_CTX
*mem_ctx
;
4755 mem_ctx
= talloc_new(NULL
);
4756 if (mem_ctx
== NULL
) {
4757 DEBUG(0, ("talloc_new failed\n"));
4758 return NT_STATUS_NO_MEMORY
;
4761 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4762 get_global_sam_name(), False
);
4763 if (!NT_STATUS_IS_OK(status
)) {
4764 DEBUG(3, ("Could not get domain info: %s\n",
4765 nt_errstr(status
)));
4769 talloc_autofree_ldapmsg(mem_ctx
, result
);
4771 entry
= ldap_first_entry(priv2ld(priv
), result
);
4772 if (entry
== NULL
) {
4773 DEBUG(0, ("Could not get domain info entry\n"));
4774 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4778 /* Find the largest of the three attributes "sambaNextRid",
4779 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4780 concept of differentiating between user and group rids, and will
4781 use only "sambaNextRid" in the future. But for compatibility
4782 reasons I look if others have chosen different strategies -- VL */
4784 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4785 "sambaNextRid", mem_ctx
);
4786 if (value
!= NULL
) {
4787 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4788 nextRid
= MAX(nextRid
, tmp
);
4791 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4792 "sambaNextUserRid", mem_ctx
);
4793 if (value
!= NULL
) {
4794 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4795 nextRid
= MAX(nextRid
, tmp
);
4798 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4799 "sambaNextGroupRid", mem_ctx
);
4800 if (value
!= NULL
) {
4801 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4802 nextRid
= MAX(nextRid
, tmp
);
4806 nextRid
= BASE_RID
-1;
4811 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4812 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4813 talloc_autofree_ldapmod(mem_ctx
, mods
);
4815 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4816 status
= NT_STATUS_NO_MEMORY
;
4820 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4822 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4825 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4828 if (NT_STATUS_IS_OK(status
)) {
4832 TALLOC_FREE(mem_ctx
);
4836 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32
*rid
)
4840 for (i
=0; i
<10; i
++) {
4841 NTSTATUS result
= ldapsam_get_new_rid(
4842 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4843 if (NT_STATUS_IS_OK(result
)) {
4847 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4851 /* The ldap update failed (maybe a race condition), retry */
4854 /* Tried 10 times, fail. */
4855 return NT_STATUS_ACCESS_DENIED
;
4858 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
4860 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4861 return NT_STATUS_IS_OK(result
) ? True
: False
;
4864 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4866 union unid_t
*id
, enum lsa_SidType
*type
)
4868 struct ldapsam_privates
*priv
=
4869 (struct ldapsam_privates
*)methods
->private_data
;
4871 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4873 LDAPMessage
*result
= NULL
;
4874 LDAPMessage
*entry
= NULL
;
4879 TALLOC_CTX
*mem_ctx
;
4881 mem_ctx
= talloc_new(NULL
);
4882 if (mem_ctx
== NULL
) {
4883 DEBUG(0, ("talloc_new failed\n"));
4887 filter
= talloc_asprintf(mem_ctx
,
4889 "(|(objectClass=%s)(objectClass=%s)))",
4890 sid_string_talloc(mem_ctx
, sid
),
4891 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4892 if (filter
== NULL
) {
4893 DEBUG(5, ("talloc_asprintf failed\n"));
4897 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4899 if (rc
!= LDAP_SUCCESS
) {
4902 talloc_autofree_ldapmsg(mem_ctx
, result
);
4904 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4905 DEBUG(10, ("Got %d entries, expected one\n",
4906 ldap_count_entries(priv2ld(priv
), result
)));
4910 entry
= ldap_first_entry(priv2ld(priv
), result
);
4912 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4913 "sambaGroupType", mem_ctx
);
4915 if (value
!= NULL
) {
4916 const char *gid_str
;
4919 gid_str
= smbldap_talloc_single_attribute(
4920 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4921 if (gid_str
== NULL
) {
4922 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4923 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4928 id
->gid
= strtoul(gid_str
, NULL
, 10);
4929 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
4934 /* It must be a user */
4936 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4937 "uidNumber", mem_ctx
);
4938 if (value
== NULL
) {
4939 DEBUG(1, ("Could not find uidNumber in %s\n",
4940 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4944 id
->uid
= strtoul(value
, NULL
, 10);
4945 *type
= SID_NAME_USER
;
4949 TALLOC_FREE(mem_ctx
);
4954 * The following functions is called only if
4955 * ldapsam:trusted and ldapsam:editposix are
4960 * ldapsam_create_user creates a new
4961 * posixAccount and sambaSamAccount object
4962 * in the ldap users subtree
4964 * The uid is allocated by winbindd.
4967 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
4968 TALLOC_CTX
*tmp_ctx
, const char *name
,
4969 uint32 acb_info
, uint32
*rid
)
4971 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4972 LDAPMessage
*entry
= NULL
;
4973 LDAPMessage
*result
= NULL
;
4975 bool is_machine
= False
;
4976 bool add_posix
= False
;
4977 LDAPMod
**mods
= NULL
;
4985 const char *dn
= NULL
;
4993 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
4994 acb_info
& ACB_WSTRUST
||
4995 acb_info
& ACB_SVRTRUST
||
4996 acb_info
& ACB_DOMTRUST
) {
5000 username
= escape_ldap_string_alloc(name
);
5001 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5002 username
, LDAP_OBJ_POSIXACCOUNT
);
5003 SAFE_FREE(username
);
5005 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5006 if (rc
!= LDAP_SUCCESS
) {
5007 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5008 return NT_STATUS_ACCESS_DENIED
;
5010 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5012 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5014 if (num_result
> 1) {
5015 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5016 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5019 if (num_result
== 1) {
5021 /* check if it is just a posix account.
5022 * or if there is a sid attached to this entry
5025 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5027 return NT_STATUS_UNSUCCESSFUL
;
5030 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5032 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5033 return NT_STATUS_USER_EXISTS
;
5036 /* it is just a posix account, retrieve the dn for later use */
5037 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5039 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5040 return NT_STATUS_NO_MEMORY
;
5044 if (num_result
== 0) {
5048 /* Create the basic samu structure and generate the mods for the ldap commit */
5049 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5050 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5054 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5056 user
= samu_new(tmp_ctx
);
5058 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5059 return NT_STATUS_NO_MEMORY
;
5062 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5063 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5064 return NT_STATUS_UNSUCCESSFUL
;
5066 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5067 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5068 return NT_STATUS_UNSUCCESSFUL
;
5071 if (acb_info
& ACB_NORMAL
) {
5072 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5073 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5074 return NT_STATUS_UNSUCCESSFUL
;
5077 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5078 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5079 return NT_STATUS_UNSUCCESSFUL
;
5083 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5084 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5085 return NT_STATUS_UNSUCCESSFUL
;
5089 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5090 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5091 return NT_STATUS_UNSUCCESSFUL
;
5094 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5095 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5096 return NT_STATUS_UNSUCCESSFUL
;
5099 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5100 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5102 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5107 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5109 /* retrieve the Domain Users group gid */
5110 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
5111 !sid_to_gid(&group_sid
, &gid
)) {
5112 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5113 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5116 /* lets allocate a new userid for this user */
5117 if (!winbind_allocate_uid(&uid
)) {
5118 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5119 return NT_STATUS_UNSUCCESSFUL
;
5124 /* TODO: choose a more appropriate default for machines */
5125 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5126 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5128 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5129 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5131 uidstr
= talloc_asprintf(tmp_ctx
, "%d", uid
);
5132 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
5134 escape_name
= escape_rdn_val_string_alloc(name
);
5136 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5137 return NT_STATUS_NO_MEMORY
;
5141 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5143 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5146 SAFE_FREE(escape_name
);
5148 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5149 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5150 return NT_STATUS_NO_MEMORY
;
5153 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5154 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5155 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5156 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5157 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5158 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5159 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5162 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5165 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5167 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5170 if (rc
!= LDAP_SUCCESS
) {
5171 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5172 return NT_STATUS_UNSUCCESSFUL
;
5175 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5177 flush_pwnam_cache();
5179 return NT_STATUS_OK
;
5182 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5184 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5185 LDAPMessage
*result
= NULL
;
5186 LDAPMessage
*entry
= NULL
;
5192 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5194 filter
= talloc_asprintf(tmp_ctx
,
5197 "(objectClass=%s))",
5198 pdb_get_username(sam_acct
),
5199 LDAP_OBJ_POSIXACCOUNT
,
5200 LDAP_OBJ_SAMBASAMACCOUNT
);
5201 if (filter
== NULL
) {
5202 return NT_STATUS_NO_MEMORY
;
5205 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5206 if (rc
!= LDAP_SUCCESS
) {
5207 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5208 return NT_STATUS_UNSUCCESSFUL
;
5210 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5212 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5214 if (num_result
== 0) {
5215 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5216 return NT_STATUS_NO_SUCH_USER
;
5219 if (num_result
> 1) {
5220 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5221 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5224 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5226 return NT_STATUS_UNSUCCESSFUL
;
5229 /* it is just a posix account, retrieve the dn for later use */
5230 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5232 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5233 return NT_STATUS_NO_MEMORY
;
5236 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5237 if (rc
!= LDAP_SUCCESS
) {
5238 return NT_STATUS_UNSUCCESSFUL
;
5241 flush_pwnam_cache();
5243 return NT_STATUS_OK
;
5247 * ldapsam_create_group creates a new
5248 * posixGroup and sambaGroupMapping object
5249 * in the ldap groups subtree
5251 * The gid is allocated by winbindd.
5254 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5255 TALLOC_CTX
*tmp_ctx
,
5259 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5261 LDAPMessage
*entry
= NULL
;
5262 LDAPMessage
*result
= NULL
;
5264 bool is_new_entry
= False
;
5265 LDAPMod
**mods
= NULL
;
5271 const char *dn
= NULL
;
5276 groupname
= escape_ldap_string_alloc(name
);
5277 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5278 groupname
, LDAP_OBJ_POSIXGROUP
);
5279 SAFE_FREE(groupname
);
5281 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5282 if (rc
!= LDAP_SUCCESS
) {
5283 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5284 return NT_STATUS_UNSUCCESSFUL
;
5286 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5288 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5290 if (num_result
> 1) {
5291 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5292 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5295 if (num_result
== 1) {
5297 /* check if it is just a posix group.
5298 * or if there is a sid attached to this entry
5301 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5303 return NT_STATUS_UNSUCCESSFUL
;
5306 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5308 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5309 return NT_STATUS_GROUP_EXISTS
;
5312 /* it is just a posix group, retrieve the gid and the dn for later use */
5313 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5315 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5316 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5319 gid
= strtoul(tmp
, NULL
, 10);
5321 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5323 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5324 return NT_STATUS_NO_MEMORY
;
5328 if (num_result
== 0) {
5331 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5333 is_new_entry
= True
;
5335 /* lets allocate a new groupid for this group */
5336 if (!winbind_allocate_gid(&gid
)) {
5337 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5338 return NT_STATUS_UNSUCCESSFUL
;
5341 gidstr
= talloc_asprintf(tmp_ctx
, "%d", gid
);
5343 escape_name
= escape_rdn_val_string_alloc(name
);
5345 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5346 return NT_STATUS_NO_MEMORY
;
5349 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5351 SAFE_FREE(escape_name
);
5353 if (!gidstr
|| !dn
) {
5354 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5355 return NT_STATUS_NO_MEMORY
;
5358 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5359 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5360 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5363 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5364 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5368 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5370 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5372 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5374 if (!groupsidstr
|| !grouptype
) {
5375 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5376 return NT_STATUS_NO_MEMORY
;
5379 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5380 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5381 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5382 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5383 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5386 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5388 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5389 /* This call may fail with rfc2307bis schema */
5390 /* Retry adding a structural class */
5391 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5392 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5396 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5399 if (rc
!= LDAP_SUCCESS
) {
5400 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5401 return NT_STATUS_UNSUCCESSFUL
;
5404 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5406 return NT_STATUS_OK
;
5409 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
5411 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5412 LDAPMessage
*result
= NULL
;
5413 LDAPMessage
*entry
= NULL
;
5421 /* get the group sid */
5422 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5424 filter
= talloc_asprintf(tmp_ctx
,
5427 "(objectClass=%s))",
5428 sid_string_talloc(tmp_ctx
, &group_sid
),
5429 LDAP_OBJ_POSIXGROUP
,
5431 if (filter
== NULL
) {
5432 return NT_STATUS_NO_MEMORY
;
5435 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5436 if (rc
!= LDAP_SUCCESS
) {
5437 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5438 return NT_STATUS_UNSUCCESSFUL
;
5440 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5442 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5444 if (num_result
== 0) {
5445 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5446 return NT_STATUS_NO_SUCH_GROUP
;
5449 if (num_result
> 1) {
5450 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5451 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5454 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5456 return NT_STATUS_UNSUCCESSFUL
;
5459 /* here it is, retrieve the dn for later use */
5460 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5462 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5463 return NT_STATUS_NO_MEMORY
;
5466 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5468 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5469 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5472 /* check no user have this group marked as primary group */
5473 filter
= talloc_asprintf(tmp_ctx
,
5476 "(objectClass=%s))",
5478 LDAP_OBJ_POSIXACCOUNT
,
5479 LDAP_OBJ_SAMBASAMACCOUNT
);
5481 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5482 if (rc
!= LDAP_SUCCESS
) {
5483 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5484 return NT_STATUS_UNSUCCESSFUL
;
5486 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5488 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5490 if (num_result
!= 0) {
5491 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5492 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5495 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5496 if (rc
!= LDAP_SUCCESS
) {
5497 return NT_STATUS_UNSUCCESSFUL
;
5500 return NT_STATUS_OK
;
5503 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5504 TALLOC_CTX
*tmp_ctx
,
5509 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5510 LDAPMessage
*entry
= NULL
;
5511 LDAPMessage
*result
= NULL
;
5513 LDAPMod
**mods
= NULL
;
5516 const char *dn
= NULL
;
5523 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5525 case LDAP_MOD_DELETE
:
5526 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5529 return NT_STATUS_UNSUCCESSFUL
;
5532 /* get member sid */
5533 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5535 /* get the group sid */
5536 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5538 filter
= talloc_asprintf(tmp_ctx
,
5541 "(objectClass=%s))",
5542 sid_string_talloc(tmp_ctx
, &member_sid
),
5543 LDAP_OBJ_POSIXACCOUNT
,
5544 LDAP_OBJ_SAMBASAMACCOUNT
);
5545 if (filter
== NULL
) {
5546 return NT_STATUS_NO_MEMORY
;
5549 /* get the member uid */
5550 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5551 if (rc
!= LDAP_SUCCESS
) {
5552 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5553 return NT_STATUS_UNSUCCESSFUL
;
5555 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5557 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5559 if (num_result
== 0) {
5560 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5561 return NT_STATUS_NO_SUCH_MEMBER
;
5564 if (num_result
> 1) {
5565 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5566 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5569 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5571 return NT_STATUS_UNSUCCESSFUL
;
5574 if (modop
== LDAP_MOD_DELETE
) {
5575 /* check if we are trying to remove the member from his primary group */
5577 gid_t user_gid
, group_gid
;
5579 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5581 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5582 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5585 user_gid
= strtoul(gidstr
, NULL
, 10);
5587 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5588 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5589 return NT_STATUS_UNSUCCESSFUL
;
5592 if (user_gid
== group_gid
) {
5593 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5594 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5598 /* here it is, retrieve the uid for later use */
5599 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5601 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5602 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5605 filter
= talloc_asprintf(tmp_ctx
,
5608 "(objectClass=%s))",
5609 sid_string_talloc(tmp_ctx
, &group_sid
),
5610 LDAP_OBJ_POSIXGROUP
,
5614 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5615 if (rc
!= LDAP_SUCCESS
) {
5616 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5617 return NT_STATUS_UNSUCCESSFUL
;
5619 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5621 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5623 if (num_result
== 0) {
5624 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5625 return NT_STATUS_NO_SUCH_GROUP
;
5628 if (num_result
> 1) {
5629 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5630 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5633 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5635 return NT_STATUS_UNSUCCESSFUL
;
5638 /* here it is, retrieve the dn for later use */
5639 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5641 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5642 return NT_STATUS_NO_MEMORY
;
5645 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5647 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5649 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5650 if (rc
!= LDAP_SUCCESS
) {
5651 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5652 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5653 return NT_STATUS_MEMBER_IN_GROUP
;
5655 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5656 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5657 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5659 return NT_STATUS_UNSUCCESSFUL
;
5662 return NT_STATUS_OK
;
5665 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5666 TALLOC_CTX
*tmp_ctx
,
5670 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5672 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5673 TALLOC_CTX
*tmp_ctx
,
5677 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5680 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5681 TALLOC_CTX
*mem_ctx
,
5682 struct samu
*sampass
)
5684 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5685 LDAPMessage
*entry
= NULL
;
5686 LDAPMessage
*result
= NULL
;
5688 LDAPMod
**mods
= NULL
;
5690 char *escape_username
;
5692 const char *dn
= NULL
;
5696 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5698 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5699 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5700 return NT_STATUS_UNSUCCESSFUL
;
5702 gidstr
= talloc_asprintf(mem_ctx
, "%d", gid
);
5704 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5705 return NT_STATUS_NO_MEMORY
;
5708 escape_username
= escape_ldap_string_alloc(pdb_get_username(sampass
));
5709 if (escape_username
== NULL
) {
5710 return NT_STATUS_NO_MEMORY
;
5713 filter
= talloc_asprintf(mem_ctx
,
5716 "(objectClass=%s))",
5718 LDAP_OBJ_POSIXACCOUNT
,
5719 LDAP_OBJ_SAMBASAMACCOUNT
);
5721 SAFE_FREE(escape_username
);
5723 if (filter
== NULL
) {
5724 return NT_STATUS_NO_MEMORY
;
5727 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5728 if (rc
!= LDAP_SUCCESS
) {
5729 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5730 return NT_STATUS_UNSUCCESSFUL
;
5732 talloc_autofree_ldapmsg(mem_ctx
, result
);
5734 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5736 if (num_result
== 0) {
5737 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5738 return NT_STATUS_NO_SUCH_USER
;
5741 if (num_result
> 1) {
5742 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5743 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5746 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5748 return NT_STATUS_UNSUCCESSFUL
;
5751 /* retrieve the dn for later use */
5752 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5754 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5755 return NT_STATUS_NO_MEMORY
;
5758 /* remove the old one, and add the new one, this way we do not risk races */
5759 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5762 return NT_STATUS_OK
;
5765 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5767 if (rc
!= LDAP_SUCCESS
) {
5768 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5769 pdb_get_username(sampass
), gidstr
));
5770 return NT_STATUS_UNSUCCESSFUL
;
5773 flush_pwnam_cache();
5775 return NT_STATUS_OK
;
5779 /**********************************************************************
5780 trusted domains functions
5781 *********************************************************************/
5783 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
5786 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
5787 ldap_state
->domain_dn
);
5790 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
5791 TALLOC_CTX
*mem_ctx
,
5792 const char *domain
, LDAPMessage
**entry
)
5796 int scope
= LDAP_SCOPE_SUBTREE
;
5797 const char **attrs
= NULL
; /* NULL: get all attrs */
5798 int attrsonly
= 0; /* 0: return values too */
5799 LDAPMessage
*result
= NULL
;
5803 filter
= talloc_asprintf(talloc_tos(),
5804 "(&(objectClass=%s)(sambaDomainName=%s))",
5805 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
5807 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5808 if (trusted_dn
== NULL
) {
5811 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
5812 filter
, attrs
, attrsonly
, &result
);
5814 if (result
!= NULL
) {
5815 talloc_autofree_ldapmsg(mem_ctx
, result
);
5818 if (rc
== LDAP_NO_SUCH_OBJECT
) {
5823 if (rc
!= LDAP_SUCCESS
) {
5827 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5829 if (num_result
> 1) {
5830 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5831 "%s object for domain '%s'?!\n",
5832 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
5836 if (num_result
== 0) {
5837 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5838 "%s object for domain %s.\n",
5839 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
5842 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5848 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
5852 time_t *pass_last_set_time
)
5854 struct ldapsam_privates
*ldap_state
=
5855 (struct ldapsam_privates
*)methods
->private_data
;
5856 LDAPMessage
*entry
= NULL
;
5858 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
5860 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
5869 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5870 entry
, "sambaClearTextPassword", talloc_tos());
5871 if (pwd_str
== NULL
) {
5874 /* trusteddom_pw routines do not use talloc yet... */
5875 *pwd
= SMB_STRDUP(pwd_str
);
5881 /* last change time */
5882 if (pass_last_set_time
!= NULL
) {
5884 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5885 entry
, "sambaPwdLastSet", talloc_tos());
5886 if (time_str
== NULL
) {
5889 *pass_last_set_time
= (time_t)atol(time_str
);
5896 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5899 if (sid_str
== NULL
) {
5902 dom_sid
= string_sid_talloc(talloc_tos(), sid_str
);
5903 if (dom_sid
== NULL
) {
5906 sid_copy(sid
, dom_sid
);
5912 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
5917 struct ldapsam_privates
*ldap_state
=
5918 (struct ldapsam_privates
*)methods
->private_data
;
5919 LDAPMessage
*entry
= NULL
;
5920 LDAPMod
**mods
= NULL
;
5921 char *prev_pwd
= NULL
;
5922 char *trusted_dn
= NULL
;
5925 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
5928 * get the current entry (if there is one) in order to put the
5929 * current password into the previous password attribute
5931 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
5936 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
5937 LDAP_OBJ_TRUSTDOM_PASSWORD
);
5938 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
5940 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
5941 sid_string_tos(sid
));
5942 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
5943 talloc_asprintf(talloc_tos(), "%li", time(NULL
)));
5944 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5945 "sambaClearTextPassword", pwd
);
5947 talloc_autofree_ldapmod(talloc_tos(), mods
);
5949 if (entry
!= NULL
) {
5950 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5951 entry
, "sambaClearTextPassword", talloc_tos());
5952 if (prev_pwd
!= NULL
) {
5953 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
5954 "sambaPreviousClearTextPassword",
5959 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5960 if (trusted_dn
== NULL
) {
5963 if (entry
== NULL
) {
5964 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5966 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
5969 if (rc
!= LDAP_SUCCESS
) {
5970 DEBUG(1, ("error writing trusted domain password!\n"));
5977 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
5981 struct ldapsam_privates
*ldap_state
=
5982 (struct ldapsam_privates
*)methods
->private_data
;
5983 LDAPMessage
*entry
= NULL
;
5984 const char *trusted_dn
;
5986 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
5990 if (entry
== NULL
) {
5991 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
5996 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
5998 if (trusted_dn
== NULL
) {
5999 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6003 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6004 if (rc
!= LDAP_SUCCESS
) {
6011 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6012 TALLOC_CTX
*mem_ctx
,
6013 uint32
*num_domains
,
6014 struct trustdom_info
***domains
)
6017 struct ldapsam_privates
*ldap_state
=
6018 (struct ldapsam_privates
*)methods
->private_data
;
6020 int scope
= LDAP_SCOPE_SUBTREE
;
6021 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6022 int attrsonly
= 0; /* 0: return values too */
6023 LDAPMessage
*result
= NULL
;
6024 LDAPMessage
*entry
= NULL
;
6026 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6027 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6029 rc
= smbldap_search(ldap_state
->smbldap_state
,
6030 ldap_state
->domain_dn
,
6037 if (result
!= NULL
) {
6038 talloc_autofree_ldapmsg(mem_ctx
, result
);
6041 if (rc
!= LDAP_SUCCESS
) {
6042 return NT_STATUS_UNSUCCESSFUL
;
6046 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6047 DEBUG(1, ("talloc failed\n"));
6048 return NT_STATUS_NO_MEMORY
;
6051 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6053 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6055 char *dom_name
, *dom_sid_str
;
6056 struct trustdom_info
*dom_info
;
6058 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6059 if (dom_info
== NULL
) {
6060 DEBUG(1, ("talloc failed\n"));
6061 return NT_STATUS_NO_MEMORY
;
6064 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6068 if (dom_name
== NULL
) {
6069 DEBUG(1, ("talloc failed\n"));
6070 return NT_STATUS_NO_MEMORY
;
6072 dom_info
->name
= dom_name
;
6074 dom_sid_str
= smbldap_talloc_single_attribute(
6075 priv2ld(ldap_state
), entry
, "sambaSID",
6077 if (dom_sid_str
== NULL
) {
6078 DEBUG(1, ("talloc failed\n"));
6079 return NT_STATUS_NO_MEMORY
;
6081 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6082 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6084 return NT_STATUS_UNSUCCESSFUL
;
6087 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6088 domains
, num_domains
);
6090 if (*domains
== NULL
) {
6091 DEBUG(1, ("talloc failed\n"));
6092 return NT_STATUS_NO_MEMORY
;
6096 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6097 return NT_STATUS_OK
;
6101 /**********************************************************************
6103 *********************************************************************/
6105 static void free_private_data(void **vp
)
6107 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6109 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6111 if ((*ldap_state
)->result
!= NULL
) {
6112 ldap_msgfree((*ldap_state
)->result
);
6113 (*ldap_state
)->result
= NULL
;
6115 if ((*ldap_state
)->domain_dn
!= NULL
) {
6116 SAFE_FREE((*ldap_state
)->domain_dn
);
6121 /* No need to free any further, as it is talloc()ed */
6124 /*********************************************************************
6125 Intitalise the parts of the pdb_methods structure that are common to
6127 *********************************************************************/
6129 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6132 struct ldapsam_privates
*ldap_state
;
6134 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6138 (*pdb_method
)->name
= "ldapsam";
6140 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6141 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6142 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6143 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6144 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6145 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6147 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6148 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6149 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6150 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6151 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6152 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6153 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6155 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6156 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6158 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6160 (*pdb_method
)->rid_algorithm
= ldapsam_rid_algorithm
;
6161 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6163 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6164 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6165 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6166 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6168 /* TODO: Setup private data and free */
6170 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6171 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6172 return NT_STATUS_NO_MEMORY
;
6175 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6176 location
, &ldap_state
->smbldap_state
);
6178 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6182 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6183 return NT_STATUS_NO_MEMORY
;
6186 (*pdb_method
)->private_data
= ldap_state
;
6188 (*pdb_method
)->free_private_data
= free_private_data
;
6190 return NT_STATUS_OK
;
6193 /**********************************************************************
6194 Initialise the 'compat' mode for pdb_ldap
6195 *********************************************************************/
6197 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6200 struct ldapsam_privates
*ldap_state
;
6201 char *uri
= talloc_strdup( NULL
, location
);
6203 trim_char( uri
, '\"', '\"' );
6204 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6208 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6212 (*pdb_method
)->name
= "ldapsam_compat";
6214 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6215 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6217 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6219 return NT_STATUS_OK
;
6222 /**********************************************************************
6223 Initialise the normal mode for pdb_ldap
6224 *********************************************************************/
6226 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6229 struct ldapsam_privates
*ldap_state
= NULL
;
6230 uint32 alg_rid_base
;
6231 char *alg_rid_base_string
= NULL
;
6232 LDAPMessage
*result
= NULL
;
6233 LDAPMessage
*entry
= NULL
;
6234 DOM_SID ldap_domain_sid
;
6235 DOM_SID secrets_domain_sid
;
6236 char *domain_sid_string
= NULL
;
6238 char *uri
= talloc_strdup( NULL
, location
);
6240 trim_char( uri
, '\"', '\"' );
6241 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6246 if (!NT_STATUS_IS_OK(nt_status
)) {
6250 (*pdb_method
)->name
= "ldapsam";
6252 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6253 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6254 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6255 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6256 (*pdb_method
)->search_users
= ldapsam_search_users
;
6257 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6258 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6260 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6261 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6262 (*pdb_method
)->enum_group_memberships
=
6263 ldapsam_enum_group_memberships
;
6264 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6265 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6267 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6268 (*pdb_method
)->create_user
= ldapsam_create_user
;
6269 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6270 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6271 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6272 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6273 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6274 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6278 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6279 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6281 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6283 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6285 ldap_state
->domain_name
, True
);
6287 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6288 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6289 "info, nor add one to the domain\n"));
6290 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6291 "will be unable to allocate new users/groups, "
6292 "and will risk BDCs having inconsistant SIDs\n"));
6293 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6294 return NT_STATUS_OK
;
6297 /* Given that the above might fail, everything below this must be
6300 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6303 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6305 ldap_msgfree(result
);
6306 return NT_STATUS_UNSUCCESSFUL
;
6309 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
6311 ldap_msgfree(result
);
6312 return NT_STATUS_UNSUCCESSFUL
;
6315 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6318 domain_sid_string
= smbldap_talloc_single_attribute(
6319 ldap_state
->smbldap_state
->ldap_struct
,
6321 get_userattr_key2string(ldap_state
->schema_ver
,
6322 LDAP_ATTR_USER_SID
),
6325 if (domain_sid_string
) {
6327 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6328 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6329 "read as a valid SID\n", domain_sid_string
));
6330 ldap_msgfree(result
);
6331 TALLOC_FREE(domain_sid_string
);
6332 return NT_STATUS_INVALID_PARAMETER
;
6334 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6335 &secrets_domain_sid
);
6336 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
6337 &ldap_domain_sid
)) {
6338 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6339 "%s based on pdb_ldap results %s -> %s\n",
6340 ldap_state
->domain_name
,
6341 sid_string_dbg(&secrets_domain_sid
),
6342 sid_string_dbg(&ldap_domain_sid
)));
6344 /* reset secrets.tdb sid */
6345 secrets_store_domain_sid(ldap_state
->domain_name
,
6347 DEBUG(1, ("New global sam SID: %s\n",
6348 sid_string_dbg(get_global_sam_sid())));
6350 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6351 TALLOC_FREE(domain_sid_string
);
6354 alg_rid_base_string
= smbldap_talloc_single_attribute(
6355 ldap_state
->smbldap_state
->ldap_struct
,
6357 get_attr_key2string( dominfo_attr_list
,
6358 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6360 if (alg_rid_base_string
) {
6361 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
6362 if (alg_rid_base
!= algorithmic_rid_base()) {
6363 DEBUG(0, ("The value of 'algorithmic RID base' has "
6364 "changed since the LDAP\n"
6365 "database was initialised. Aborting. \n"));
6366 ldap_msgfree(result
);
6367 TALLOC_FREE(alg_rid_base_string
);
6368 return NT_STATUS_UNSUCCESSFUL
;
6370 TALLOC_FREE(alg_rid_base_string
);
6372 ldap_msgfree(result
);
6374 return NT_STATUS_OK
;
6377 NTSTATUS
pdb_ldap_init(void)
6380 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6383 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6386 /* Let pdb_nds register backends */
6389 return NT_STATUS_OK
;