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_first_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_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
844 if (user_dn
!= NULL
) {
845 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %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
)) {
851 TALLOC_FREE(user_dn
);
854 ZERO_STRUCT(smblmpwd
);
855 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
856 TALLOC_FREE(user_dn
);
859 ZERO_STRUCT(smbntpwd
);
860 use_samba_attrs
= False
;
863 TALLOC_FREE(user_dn
);
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", (long int)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", (long int)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", (long int)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", (long int)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", (long int)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 (long int)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", (long int)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
;
1705 if (!ldap_state
->is_nds_ldap
) {
1707 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1708 LDAP_EXOP_MODIFY_PASSWD
)) {
1709 DEBUG(2, ("ldap password change requested, but LDAP "
1710 "server does not support it -- ignoring\n"));
1711 return NT_STATUS_OK
;
1715 if (!push_utf8_allocate(&utf8_password
,
1716 pdb_get_plaintext_passwd(newpwd
),
1719 return NT_STATUS_NO_MEMORY
;
1722 if (!push_utf8_allocate(&utf8_dn
, dn
, &converted_size
)) {
1723 SAFE_FREE(utf8_password
);
1724 return NT_STATUS_NO_MEMORY
;
1727 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1728 DEBUG(0,("ber_alloc_t returns NULL\n"));
1729 SAFE_FREE(utf8_password
);
1731 return NT_STATUS_UNSUCCESSFUL
;
1734 if ((ber_printf (ber
, "{") < 0) ||
1735 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1737 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1741 SAFE_FREE(utf8_password
);
1742 return NT_STATUS_UNSUCCESSFUL
;
1745 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1746 ret
= ber_printf(ber
, "ts}",
1747 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1750 ret
= ber_printf(ber
, "}");
1754 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1758 SAFE_FREE(utf8_password
);
1759 return NT_STATUS_UNSUCCESSFUL
;
1762 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1763 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1766 SAFE_FREE(utf8_password
);
1767 return NT_STATUS_UNSUCCESSFUL
;
1771 SAFE_FREE(utf8_password
);
1774 if (!ldap_state
->is_nds_ldap
) {
1775 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1776 LDAP_EXOP_MODIFY_PASSWD
,
1777 bv
, NULL
, NULL
, &retoid
,
1780 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1781 pdb_get_plaintext_passwd(newpwd
));
1783 if (rc
!= LDAP_SUCCESS
) {
1784 char *ld_error
= NULL
;
1786 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1787 DEBUG(3, ("Could not set userPassword "
1788 "attribute due to an objectClass "
1789 "violation -- ignoring\n"));
1791 return NT_STATUS_OK
;
1794 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1796 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1797 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1798 SAFE_FREE(ld_error
);
1800 #if defined(LDAP_CONSTRAINT_VIOLATION)
1801 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1802 return NT_STATUS_PASSWORD_RESTRICTION
;
1804 return NT_STATUS_UNSUCCESSFUL
;
1806 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1807 #ifdef DEBUG_PASSWORD
1808 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1811 ber_bvfree(retdata
);
1813 ldap_memfree(retoid
);
1817 return NT_STATUS_OK
;
1820 /**********************************************************************
1821 Delete entry from LDAP for username.
1822 *********************************************************************/
1824 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1825 struct samu
* sam_acct
)
1827 struct ldapsam_privates
*priv
=
1828 (struct ldapsam_privates
*)my_methods
->private_data
;
1831 LDAPMessage
*msg
, *entry
;
1832 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1833 const char **attr_list
;
1834 TALLOC_CTX
*mem_ctx
;
1837 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1838 return NT_STATUS_INVALID_PARAMETER
;
1841 sname
= pdb_get_username(sam_acct
);
1843 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1846 mem_ctx
= talloc_new(NULL
);
1847 if (mem_ctx
== NULL
) {
1848 DEBUG(0, ("talloc_new failed\n"));
1852 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1853 if (attr_list
== NULL
) {
1857 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1859 if ((rc
!= LDAP_SUCCESS
) ||
1860 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1861 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1862 DEBUG(5, ("Could not find user %s\n", sname
));
1863 result
= NT_STATUS_NO_SUCH_USER
;
1867 rc
= ldapsam_delete_entry(
1868 priv
, mem_ctx
, entry
,
1869 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1870 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1873 result
= (rc
== LDAP_SUCCESS
) ?
1874 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1877 TALLOC_FREE(mem_ctx
);
1881 /**********************************************************************
1882 Helper function to determine for update_sam_account whether
1883 we need LDAP modification.
1884 *********************************************************************/
1886 static bool element_is_changed(const struct samu
*sampass
,
1887 enum pdb_elements element
)
1889 return IS_SAM_CHANGED(sampass
, element
);
1892 /**********************************************************************
1894 *********************************************************************/
1896 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1898 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1899 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1902 LDAPMessage
*result
= NULL
;
1903 LDAPMessage
*entry
= NULL
;
1904 LDAPMod
**mods
= NULL
;
1905 const char **attr_list
;
1907 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1909 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1910 if (pdb_get_username(newpwd
) == NULL
) {
1911 return NT_STATUS_INVALID_PARAMETER
;
1913 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1914 TALLOC_FREE( attr_list
);
1915 if (rc
!= LDAP_SUCCESS
) {
1916 return NT_STATUS_UNSUCCESSFUL
;
1918 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1919 my_methods
, PDB_CHANGED
);
1920 talloc_autofree_ldapmsg(newpwd
, result
);
1923 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1924 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1925 return NT_STATUS_UNSUCCESSFUL
;
1928 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1929 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
1931 return NT_STATUS_UNSUCCESSFUL
;
1934 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1936 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1937 element_is_changed
)) {
1938 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1941 ldap_mods_free(mods
,True
);
1942 return NT_STATUS_UNSUCCESSFUL
;
1945 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1946 && (mods
== NULL
)) {
1947 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1948 pdb_get_username(newpwd
)));
1950 return NT_STATUS_OK
;
1953 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1956 ldap_mods_free(mods
,True
);
1962 * We need to set the backend private data to NULL here. For example
1963 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1964 * same one, and with the explicit delete / add logic for attribute
1965 * values the second time we would use the wrong "old" value which
1966 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1968 * The existing LDAPMessage is still being auto-freed by the
1971 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
1974 if (!NT_STATUS_IS_OK(ret
)) {
1978 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1979 pdb_get_username(newpwd
)));
1980 return NT_STATUS_OK
;
1983 /***************************************************************************
1984 Renames a struct samu
1985 - The "rename user script" has full responsibility for changing everything
1986 ***************************************************************************/
1988 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
1989 struct samu
*old_acct
,
1990 const char *newname
)
1992 const char *oldname
;
1994 char *rename_script
= NULL
;
1995 fstring oldname_lower
, newname_lower
;
1998 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1999 return NT_STATUS_INVALID_PARAMETER
;
2002 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2003 return NT_STATUS_INVALID_PARAMETER
;
2006 oldname
= pdb_get_username(old_acct
);
2008 /* rename the posix user */
2009 rename_script
= SMB_STRDUP(lp_renameuser_script());
2010 if (rename_script
== NULL
) {
2011 return NT_STATUS_NO_MEMORY
;
2014 if (!(*rename_script
)) {
2015 SAFE_FREE(rename_script
);
2016 return NT_STATUS_ACCESS_DENIED
;
2019 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2022 /* We have to allow the account name to end with a '$'.
2023 Also, follow the semantics in _samr_create_user() and lower case the
2024 posix name but preserve the case in passdb */
2026 fstrcpy( oldname_lower
, oldname
);
2027 strlower_m( oldname_lower
);
2028 fstrcpy( newname_lower
, newname
);
2029 strlower_m( newname_lower
);
2030 rename_script
= realloc_string_sub2(rename_script
,
2035 if (!rename_script
) {
2036 return NT_STATUS_NO_MEMORY
;
2038 rename_script
= realloc_string_sub2(rename_script
,
2043 rc
= smbrun(rename_script
, NULL
);
2045 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2046 rename_script
, rc
));
2048 SAFE_FREE(rename_script
);
2051 smb_nscd_flush_user_cache();
2055 return NT_STATUS_UNSUCCESSFUL
;
2057 return NT_STATUS_OK
;
2060 /**********************************************************************
2061 Helper function to determine for update_sam_account whether
2062 we need LDAP modification.
2063 *********************************************************************/
2065 static bool element_is_set_or_changed(const struct samu
*sampass
,
2066 enum pdb_elements element
)
2068 return (IS_SAM_SET(sampass
, element
) ||
2069 IS_SAM_CHANGED(sampass
, element
));
2072 /**********************************************************************
2073 Add struct samu to LDAP.
2074 *********************************************************************/
2076 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2078 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2079 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2081 LDAPMessage
*result
= NULL
;
2082 LDAPMessage
*entry
= NULL
;
2083 LDAPMod
**mods
= NULL
;
2084 int ldap_op
= LDAP_MOD_REPLACE
;
2086 const char **attr_list
;
2087 char *escape_user
= NULL
;
2088 const char *username
= pdb_get_username(newpwd
);
2089 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
2090 char *filter
= NULL
;
2092 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2093 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2096 return NT_STATUS_NO_MEMORY
;
2099 if (!username
|| !*username
) {
2100 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2101 status
= NT_STATUS_INVALID_PARAMETER
;
2105 /* free this list after the second search or in case we exit on failure */
2106 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2108 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2110 if (rc
!= LDAP_SUCCESS
) {
2114 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2115 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2119 ldap_msgfree(result
);
2122 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2123 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2125 if (rc
== LDAP_SUCCESS
) {
2126 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2127 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2128 "already in the base, with samba "
2129 "attributes\n", sid_string_dbg(sid
)));
2132 ldap_msgfree(result
);
2137 /* does the entry already exist but without a samba attributes?
2138 we need to return the samba attributes here */
2140 escape_user
= escape_ldap_string_alloc( username
);
2141 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2143 status
= NT_STATUS_NO_MEMORY
;
2146 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2148 status
= NT_STATUS_NO_MEMORY
;
2151 SAFE_FREE(escape_user
);
2153 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2154 filter
, attr_list
, &result
);
2155 if ( rc
!= LDAP_SUCCESS
) {
2159 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2161 if (num_result
> 1) {
2162 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2166 /* Check if we need to update an existing entry */
2167 if (num_result
== 1) {
2168 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2169 ldap_op
= LDAP_MOD_REPLACE
;
2170 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2171 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2173 status
= NT_STATUS_NO_MEMORY
;
2177 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2179 /* There might be a SID for this account already - say an idmap entry */
2181 filter
= talloc_asprintf(ctx
,
2182 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2183 get_userattr_key2string(ldap_state
->schema_ver
,
2184 LDAP_ATTR_USER_SID
),
2185 sid_string_talloc(ctx
, sid
),
2186 LDAP_OBJ_IDMAP_ENTRY
,
2187 LDAP_OBJ_SID_ENTRY
);
2189 status
= NT_STATUS_NO_MEMORY
;
2193 /* free old result before doing a new search */
2194 if (result
!= NULL
) {
2195 ldap_msgfree(result
);
2198 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2199 filter
, attr_list
, &result
);
2201 if ( rc
!= LDAP_SUCCESS
) {
2205 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2207 if (num_result
> 1) {
2208 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2212 /* Check if we need to update an existing entry */
2213 if (num_result
== 1) {
2215 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2216 ldap_op
= LDAP_MOD_REPLACE
;
2217 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2218 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2220 status
= NT_STATUS_NO_MEMORY
;
2226 if (num_result
== 0) {
2227 char *escape_username
;
2228 /* Check if we need to add an entry */
2229 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2230 ldap_op
= LDAP_MOD_ADD
;
2232 escape_username
= escape_rdn_val_string_alloc(username
);
2233 if (!escape_username
) {
2234 status
= NT_STATUS_NO_MEMORY
;
2238 if (username
[strlen(username
)-1] == '$') {
2239 dn
= talloc_asprintf(ctx
,
2242 lp_ldap_machine_suffix());
2244 dn
= talloc_asprintf(ctx
,
2247 lp_ldap_user_suffix());
2250 SAFE_FREE(escape_username
);
2252 status
= NT_STATUS_NO_MEMORY
;
2257 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2258 element_is_set_or_changed
)) {
2259 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2261 ldap_mods_free(mods
, true);
2267 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2270 switch ( ldap_state
->schema_ver
) {
2271 case SCHEMAVER_SAMBAACCOUNT
:
2272 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2274 case SCHEMAVER_SAMBASAMACCOUNT
:
2275 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2278 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2282 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2283 if (!NT_STATUS_IS_OK(ret
)) {
2284 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2285 pdb_get_username(newpwd
),dn
));
2286 ldap_mods_free(mods
, true);
2290 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2291 ldap_mods_free(mods
, true);
2293 status
= NT_STATUS_OK
;
2298 SAFE_FREE(escape_user
);
2300 ldap_msgfree(result
);
2305 /**********************************************************************
2306 *********************************************************************/
2308 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2310 LDAPMessage
** result
)
2312 int scope
= LDAP_SCOPE_SUBTREE
;
2314 const char **attr_list
;
2316 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2317 rc
= smbldap_search(ldap_state
->smbldap_state
,
2318 lp_ldap_suffix (), scope
,
2319 filter
, attr_list
, 0, result
);
2320 TALLOC_FREE(attr_list
);
2325 /**********************************************************************
2326 *********************************************************************/
2328 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2329 GROUP_MAP
*map
, LDAPMessage
*entry
)
2332 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2334 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2335 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2336 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2341 temp
= smbldap_talloc_single_attribute(
2342 ldap_state
->smbldap_state
->ldap_struct
,
2344 get_attr_key2string(groupmap_attr_list
,
2345 LDAP_ATTR_GIDNUMBER
),
2348 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2349 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2353 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2355 map
->gid
= (gid_t
)atol(temp
);
2358 temp
= smbldap_talloc_single_attribute(
2359 ldap_state
->smbldap_state
->ldap_struct
,
2361 get_attr_key2string(groupmap_attr_list
,
2362 LDAP_ATTR_GROUP_SID
),
2365 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2366 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2371 if (!string_to_sid(&map
->sid
, temp
)) {
2372 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2378 temp
= smbldap_talloc_single_attribute(
2379 ldap_state
->smbldap_state
->ldap_struct
,
2381 get_attr_key2string(groupmap_attr_list
,
2382 LDAP_ATTR_GROUP_TYPE
),
2385 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2386 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2390 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2392 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2393 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2394 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2400 temp
= smbldap_talloc_single_attribute(
2401 ldap_state
->smbldap_state
->ldap_struct
,
2403 get_attr_key2string(groupmap_attr_list
,
2404 LDAP_ATTR_DISPLAY_NAME
),
2407 temp
= smbldap_talloc_single_attribute(
2408 ldap_state
->smbldap_state
->ldap_struct
,
2410 get_attr_key2string(groupmap_attr_list
,
2414 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2415 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2420 fstrcpy(map
->nt_name
, temp
);
2423 temp
= smbldap_talloc_single_attribute(
2424 ldap_state
->smbldap_state
->ldap_struct
,
2426 get_attr_key2string(groupmap_attr_list
,
2430 temp
= talloc_strdup(ctx
, "");
2436 fstrcpy(map
->comment
, temp
);
2438 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2439 store_gid_sid_cache(&map
->sid
, map
->gid
);
2446 /**********************************************************************
2447 *********************************************************************/
2449 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2453 struct ldapsam_privates
*ldap_state
=
2454 (struct ldapsam_privates
*)methods
->private_data
;
2455 LDAPMessage
*result
= NULL
;
2456 LDAPMessage
*entry
= NULL
;
2459 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2461 return NT_STATUS_NO_SUCH_GROUP
;
2464 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2467 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2469 ldap_msgfree(result
);
2470 return NT_STATUS_NO_SUCH_GROUP
;
2474 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2475 "count=%d\n", filter
, count
));
2476 ldap_msgfree(result
);
2477 return NT_STATUS_NO_SUCH_GROUP
;
2480 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2483 ldap_msgfree(result
);
2484 return NT_STATUS_UNSUCCESSFUL
;
2487 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2488 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2489 "group filter %s\n", filter
));
2490 ldap_msgfree(result
);
2491 return NT_STATUS_NO_SUCH_GROUP
;
2494 ldap_msgfree(result
);
2495 return NT_STATUS_OK
;
2498 /**********************************************************************
2499 *********************************************************************/
2501 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2504 char *filter
= NULL
;
2508 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2510 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2511 sid_to_fstring(tmp
, &sid
)) < 0) {
2512 return NT_STATUS_NO_MEMORY
;
2515 status
= ldapsam_getgroup(methods
, filter
, map
);
2520 /**********************************************************************
2521 *********************************************************************/
2523 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2526 char *filter
= NULL
;
2529 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2531 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2532 (unsigned long)gid
) < 0) {
2533 return NT_STATUS_NO_MEMORY
;
2536 status
= ldapsam_getgroup(methods
, filter
, map
);
2541 /**********************************************************************
2542 *********************************************************************/
2544 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2547 char *filter
= NULL
;
2548 char *escape_name
= escape_ldap_string_alloc(name
);
2552 return NT_STATUS_NO_MEMORY
;
2555 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2557 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2558 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2560 SAFE_FREE(escape_name
);
2561 return NT_STATUS_NO_MEMORY
;
2564 SAFE_FREE(escape_name
);
2565 status
= ldapsam_getgroup(methods
, filter
, map
);
2570 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2572 const DOM_SID
*domain_sid
,
2578 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2579 str
, sizeof(str
)-1)) {
2580 DEBUG(10, ("Could not find sambaSID attribute\n"));
2584 if (!string_to_sid(&sid
, str
)) {
2585 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2589 if (sid_compare_domain(&sid
, domain_sid
) != 0) {
2590 DEBUG(10, ("SID %s is not in expected domain %s\n",
2591 str
, sid_string_dbg(domain_sid
)));
2595 if (!sid_peek_rid(&sid
, rid
)) {
2596 DEBUG(10, ("Could not peek into RID\n"));
2603 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2604 TALLOC_CTX
*mem_ctx
,
2605 const DOM_SID
*group
,
2606 uint32
**pp_member_rids
,
2607 size_t *p_num_members
)
2609 struct ldapsam_privates
*ldap_state
=
2610 (struct ldapsam_privates
*)methods
->private_data
;
2611 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2612 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2613 const char *sid_attrs
[] = { "sambaSID", NULL
};
2614 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2615 LDAPMessage
*result
= NULL
;
2618 char **values
= NULL
;
2623 *pp_member_rids
= NULL
;
2626 filter
= talloc_asprintf(mem_ctx
,
2627 "(&(objectClass=%s)"
2630 LDAP_OBJ_POSIXGROUP
,
2632 sid_string_talloc(mem_ctx
, group
));
2633 if (filter
== NULL
) {
2634 ret
= NT_STATUS_NO_MEMORY
;
2638 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2639 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2642 if (rc
!= LDAP_SUCCESS
)
2645 talloc_autofree_ldapmsg(mem_ctx
, result
);
2647 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2650 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2651 sid_string_dbg(group
)));
2652 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2657 ret
= NT_STATUS_NO_SUCH_GROUP
;
2661 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2665 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2667 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2668 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2672 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2674 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2676 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2677 if (filter
== NULL
) {
2678 ret
= NT_STATUS_NO_MEMORY
;
2682 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2683 char *escape_memberuid
;
2685 escape_memberuid
= escape_ldap_string_alloc(*memberuid
);
2686 if (escape_memberuid
== NULL
) {
2687 ret
= NT_STATUS_NO_MEMORY
;
2691 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2692 if (filter
== NULL
) {
2693 SAFE_FREE(escape_memberuid
);
2694 ret
= NT_STATUS_NO_MEMORY
;
2698 SAFE_FREE(escape_memberuid
);
2701 filter
= talloc_asprintf_append_buffer(filter
, "))");
2702 if (filter
== NULL
) {
2703 ret
= NT_STATUS_NO_MEMORY
;
2707 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2708 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2711 if (rc
!= LDAP_SUCCESS
)
2714 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2715 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2717 talloc_autofree_ldapmsg(mem_ctx
, result
);
2719 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2721 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2727 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2731 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2732 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2733 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2737 if (!string_to_sid(&sid
, sidstr
))
2740 if (!sid_check_is_in_our_domain(&sid
)) {
2741 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2742 "in our domain\n"));
2743 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2747 sid_peek_rid(&sid
, &rid
);
2749 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2751 ret
= NT_STATUS_NO_MEMORY
;
2757 filter
= talloc_asprintf(mem_ctx
,
2758 "(&(objectClass=%s)"
2760 LDAP_OBJ_SAMBASAMACCOUNT
,
2763 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2764 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2767 if (rc
!= LDAP_SUCCESS
)
2770 talloc_autofree_ldapmsg(mem_ctx
, result
);
2772 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2774 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2778 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2780 get_global_sam_sid(),
2782 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2783 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2787 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2789 ret
= NT_STATUS_NO_MEMORY
;
2799 ldap_value_free(values
);
2804 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2805 TALLOC_CTX
*mem_ctx
,
2809 size_t *p_num_groups
)
2811 struct ldapsam_privates
*ldap_state
=
2812 (struct ldapsam_privates
*)methods
->private_data
;
2813 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2815 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2818 LDAPMessage
*result
= NULL
;
2820 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2821 size_t num_sids
, num_gids
;
2823 gid_t primary_gid
= -1;
2828 if (pdb_get_username(user
) == NULL
) {
2829 return NT_STATUS_INVALID_PARAMETER
;
2832 escape_name
= escape_ldap_string_alloc(pdb_get_username(user
));
2833 if (escape_name
== NULL
)
2834 return NT_STATUS_NO_MEMORY
;
2836 /* retrieve the users primary gid */
2837 filter
= talloc_asprintf(mem_ctx
,
2838 "(&(objectClass=%s)(uid=%s))",
2839 LDAP_OBJ_SAMBASAMACCOUNT
,
2841 if (filter
== NULL
) {
2842 ret
= NT_STATUS_NO_MEMORY
;
2846 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2847 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2849 if (rc
!= LDAP_SUCCESS
)
2852 talloc_autofree_ldapmsg(mem_ctx
, result
);
2854 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2858 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2859 ret
= NT_STATUS_NO_SUCH_USER
;
2862 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2864 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2866 DEBUG (1, ("Unable to find the member's gid!\n"));
2867 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2870 primary_gid
= strtoul(gidstr
, NULL
, 10);
2873 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2874 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2878 filter
= talloc_asprintf(mem_ctx
,
2879 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2880 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2881 if (filter
== NULL
) {
2882 ret
= NT_STATUS_NO_MEMORY
;
2886 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2887 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2889 if (rc
!= LDAP_SUCCESS
)
2892 talloc_autofree_ldapmsg(mem_ctx
, result
);
2900 /* We need to add the primary group as the first gid/sid */
2902 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2903 ret
= NT_STATUS_NO_MEMORY
;
2907 /* This sid will be replaced later */
2909 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2911 if (!NT_STATUS_IS_OK(ret
)) {
2915 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2917 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2924 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2926 str
, sizeof(str
)-1))
2929 if (!string_to_sid(&sid
, str
))
2932 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
2934 str
, sizeof(str
)-1))
2937 gid
= strtoul(str
, &end
, 10);
2939 if (PTR_DIFF(end
, str
) != strlen(str
))
2942 if (gid
== primary_gid
) {
2943 sid_copy(&(*pp_sids
)[0], &sid
);
2945 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
2947 ret
= NT_STATUS_NO_MEMORY
;
2950 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
2952 if (!NT_STATUS_IS_OK(ret
)) {
2958 if (sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
2959 DEBUG(3, ("primary group of [%s] not found\n",
2960 pdb_get_username(user
)));
2964 *p_num_groups
= num_sids
;
2970 SAFE_FREE(escape_name
);
2974 /**********************************************************************
2975 * Augment a posixGroup object with a sambaGroupMapping domgroup
2976 *********************************************************************/
2978 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
2979 struct ldapsam_privates
*ldap_state
,
2982 const char *filter
, *dn
;
2983 LDAPMessage
*msg
, *entry
;
2987 filter
= talloc_asprintf(mem_ctx
,
2988 "(&(objectClass=%s)(gidNumber=%u))",
2989 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
2990 if (filter
== NULL
) {
2991 return NT_STATUS_NO_MEMORY
;
2994 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
2995 get_attr_list(mem_ctx
, groupmap_attr_list
),
2997 talloc_autofree_ldapmsg(mem_ctx
, msg
);
2999 if ((rc
!= LDAP_SUCCESS
) ||
3000 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3001 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3002 return NT_STATUS_NO_SUCH_GROUP
;
3005 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3007 return NT_STATUS_NO_MEMORY
;
3011 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3013 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3014 sid_string_talloc(mem_ctx
, &map
->sid
));
3015 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3016 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3017 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3019 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3021 talloc_autofree_ldapmod(mem_ctx
, mods
);
3023 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3024 if (rc
!= LDAP_SUCCESS
) {
3025 return NT_STATUS_ACCESS_DENIED
;
3028 return NT_STATUS_OK
;
3031 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3034 struct ldapsam_privates
*ldap_state
=
3035 (struct ldapsam_privates
*)methods
->private_data
;
3036 LDAPMessage
*msg
= NULL
;
3037 LDAPMod
**mods
= NULL
;
3038 const char *attrs
[] = { NULL
};
3042 TALLOC_CTX
*mem_ctx
;
3049 mem_ctx
= talloc_new(NULL
);
3050 if (mem_ctx
== NULL
) {
3051 DEBUG(0, ("talloc_new failed\n"));
3052 return NT_STATUS_NO_MEMORY
;
3055 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3056 sid_string_talloc(mem_ctx
, &map
->sid
));
3057 if (filter
== NULL
) {
3058 result
= NT_STATUS_NO_MEMORY
;
3062 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3063 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3064 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3066 if ((rc
== LDAP_SUCCESS
) &&
3067 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3069 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3070 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3071 result
= NT_STATUS_GROUP_EXISTS
;
3075 switch (map
->sid_name_use
) {
3077 case SID_NAME_DOM_GRP
:
3078 /* To map a domain group we need to have a posix group
3080 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3084 case SID_NAME_ALIAS
:
3085 if (!sid_check_is_in_our_domain(&map
->sid
)
3086 && !sid_check_is_in_builtin(&map
->sid
) )
3088 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3089 sid_string_dbg(&map
->sid
)));
3090 result
= NT_STATUS_INVALID_PARAMETER
;
3096 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3097 sid_type_lookup(map
->sid_name_use
)));
3098 result
= NT_STATUS_INVALID_PARAMETER
;
3102 /* Domain groups have been mapped in a separate routine, we have to
3103 * create an alias now */
3105 if (map
->gid
== -1) {
3106 DEBUG(10, ("Refusing to map gid==-1\n"));
3107 result
= NT_STATUS_INVALID_PARAMETER
;
3111 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3112 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3113 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3114 result
= NT_STATUS_GROUP_EXISTS
;
3118 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3119 * the best we can get out of LDAP. */
3121 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3122 sid_string_talloc(mem_ctx
, &map
->sid
),
3123 lp_ldap_group_suffix());
3125 result
= NT_STATUS_NO_MEMORY
;
3131 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3132 LDAP_OBJ_SID_ENTRY
);
3133 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3135 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3136 sid_string_talloc(mem_ctx
, &map
->sid
));
3137 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3138 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3139 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3141 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3143 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3144 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3145 talloc_autofree_ldapmod(mem_ctx
, mods
);
3147 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3149 result
= (rc
== LDAP_SUCCESS
) ?
3150 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3153 TALLOC_FREE(mem_ctx
);
3157 /**********************************************************************
3158 * Update a group mapping entry. We're quite strict about what can be changed:
3159 * Only the description and displayname may be changed. It simply does not
3160 * make any sense to change the SID, gid or the type in a mapping.
3161 *********************************************************************/
3163 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3166 struct ldapsam_privates
*ldap_state
=
3167 (struct ldapsam_privates
*)methods
->private_data
;
3169 const char *filter
, *dn
;
3170 LDAPMessage
*msg
= NULL
;
3171 LDAPMessage
*entry
= NULL
;
3172 LDAPMod
**mods
= NULL
;
3173 TALLOC_CTX
*mem_ctx
;
3176 mem_ctx
= talloc_new(NULL
);
3177 if (mem_ctx
== NULL
) {
3178 DEBUG(0, ("talloc_new failed\n"));
3179 return NT_STATUS_NO_MEMORY
;
3182 /* Make 100% sure that sid, gid and type are not changed by looking up
3183 * exactly the values we're given in LDAP. */
3185 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3186 "(sambaSid=%s)(gidNumber=%u)"
3187 "(sambaGroupType=%d))",
3189 sid_string_talloc(mem_ctx
, &map
->sid
),
3190 (unsigned int)map
->gid
, map
->sid_name_use
);
3191 if (filter
== NULL
) {
3192 result
= NT_STATUS_NO_MEMORY
;
3196 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3197 get_attr_list(mem_ctx
, groupmap_attr_list
),
3199 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3201 if ((rc
!= LDAP_SUCCESS
) ||
3202 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3203 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3204 result
= NT_STATUS_NO_SUCH_GROUP
;
3208 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3211 result
= NT_STATUS_NO_MEMORY
;
3216 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3218 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3220 talloc_autofree_ldapmod(mem_ctx
, mods
);
3223 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3224 "nothing to do\n"));
3225 result
= NT_STATUS_OK
;
3229 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3231 if (rc
!= LDAP_SUCCESS
) {
3232 result
= NT_STATUS_ACCESS_DENIED
;
3236 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3237 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3239 result
= NT_STATUS_OK
;
3242 TALLOC_FREE(mem_ctx
);
3246 /**********************************************************************
3247 *********************************************************************/
3249 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3252 struct ldapsam_privates
*priv
=
3253 (struct ldapsam_privates
*)methods
->private_data
;
3254 LDAPMessage
*msg
, *entry
;
3257 TALLOC_CTX
*mem_ctx
;
3260 mem_ctx
= talloc_new(NULL
);
3261 if (mem_ctx
== NULL
) {
3262 DEBUG(0, ("talloc_new failed\n"));
3263 return NT_STATUS_NO_MEMORY
;
3266 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3267 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3268 sid_string_talloc(mem_ctx
, &sid
));
3269 if (filter
== NULL
) {
3270 result
= NT_STATUS_NO_MEMORY
;
3273 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3274 get_attr_list(mem_ctx
, groupmap_attr_list
),
3276 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3278 if ((rc
!= LDAP_SUCCESS
) ||
3279 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3280 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3281 result
= NT_STATUS_NO_SUCH_GROUP
;
3285 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3286 get_attr_list(mem_ctx
,
3287 groupmap_attr_list_to_delete
));
3289 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3290 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3291 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3292 const char *attrs
[] = { "sambaGroupType", "description",
3293 "displayName", "sambaSIDList",
3296 /* Second try. Don't delete the sambaSID attribute, this is
3297 for "old" entries that are tacked on a winbind
3300 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3301 LDAP_OBJ_GROUPMAP
, attrs
);
3304 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3305 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3306 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3307 const char *attrs
[] = { "sambaGroupType", "description",
3308 "displayName", "sambaSIDList",
3309 "gidNumber", NULL
};
3311 /* Third try. This is a post-3.0.21 alias (containing only
3312 * sambaSidEntry and sambaGroupMapping classes), we also have
3313 * to delete the gidNumber attribute, only the sambaSidEntry
3316 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3317 LDAP_OBJ_GROUPMAP
, attrs
);
3320 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3323 TALLOC_FREE(mem_ctx
);
3327 /**********************************************************************
3328 *********************************************************************/
3330 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3333 struct ldapsam_privates
*ldap_state
=
3334 (struct ldapsam_privates
*)my_methods
->private_data
;
3335 char *filter
= NULL
;
3337 const char **attr_list
;
3339 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3341 return NT_STATUS_NO_MEMORY
;
3343 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3344 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3345 LDAP_SCOPE_SUBTREE
, filter
,
3346 attr_list
, 0, &ldap_state
->result
);
3347 TALLOC_FREE(attr_list
);
3349 if (rc
!= LDAP_SUCCESS
) {
3350 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3351 ldap_err2string(rc
)));
3352 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3353 lp_ldap_suffix(), filter
));
3354 ldap_msgfree(ldap_state
->result
);
3355 ldap_state
->result
= NULL
;
3356 TALLOC_FREE(filter
);
3357 return NT_STATUS_UNSUCCESSFUL
;
3360 TALLOC_FREE(filter
);
3362 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3363 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3364 ldap_state
->result
)));
3367 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3368 ldap_state
->result
);
3369 ldap_state
->index
= 0;
3371 return NT_STATUS_OK
;
3374 /**********************************************************************
3375 *********************************************************************/
3377 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3379 ldapsam_endsampwent(my_methods
);
3382 /**********************************************************************
3383 *********************************************************************/
3385 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3388 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3389 struct ldapsam_privates
*ldap_state
=
3390 (struct ldapsam_privates
*)my_methods
->private_data
;
3394 if (!ldap_state
->entry
)
3397 ldap_state
->index
++;
3398 bret
= init_group_from_ldap(ldap_state
, map
,
3402 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3406 return NT_STATUS_OK
;
3409 /**********************************************************************
3410 *********************************************************************/
3412 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3413 const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
,
3414 GROUP_MAP
**pp_rmap
,
3415 size_t *p_num_entries
,
3424 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3425 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3427 return NT_STATUS_ACCESS_DENIED
;
3430 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3431 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3432 sid_name_use
!= map
.sid_name_use
) {
3433 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3434 "not of the requested type\n", map
.nt_name
));
3437 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3438 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3439 "non mapped\n", map
.nt_name
));
3443 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3445 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3446 "enlarge group map!\n"));
3447 return NT_STATUS_UNSUCCESSFUL
;
3450 (*pp_rmap
)[entries
] = map
;
3455 ldapsam_endsamgrent(methods
);
3457 *p_num_entries
= entries
;
3459 return NT_STATUS_OK
;
3462 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3463 const DOM_SID
*alias
,
3464 const DOM_SID
*member
,
3467 struct ldapsam_privates
*ldap_state
=
3468 (struct ldapsam_privates
*)methods
->private_data
;
3470 LDAPMessage
*result
= NULL
;
3471 LDAPMessage
*entry
= NULL
;
3473 LDAPMod
**mods
= NULL
;
3475 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3478 char *filter
= NULL
;
3480 if (sid_check_is_in_builtin(alias
)) {
3481 type
= SID_NAME_ALIAS
;
3484 if (sid_check_is_in_our_domain(alias
)) {
3485 type
= SID_NAME_ALIAS
;
3488 if (type
== SID_NAME_USE_NONE
) {
3489 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3490 sid_string_dbg(alias
)));
3491 return NT_STATUS_NO_SUCH_ALIAS
;
3494 if (asprintf(&filter
,
3495 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3496 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3498 return NT_STATUS_NO_MEMORY
;
3501 if (ldapsam_search_one_group(ldap_state
, filter
,
3502 &result
) != LDAP_SUCCESS
) {
3504 return NT_STATUS_NO_SUCH_ALIAS
;
3507 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3511 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3512 ldap_msgfree(result
);
3514 return NT_STATUS_NO_SUCH_ALIAS
;
3518 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3519 "filter %s: count=%d\n", filter
, count
));
3520 ldap_msgfree(result
);
3522 return NT_STATUS_NO_SUCH_ALIAS
;
3527 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3531 ldap_msgfree(result
);
3532 return NT_STATUS_UNSUCCESSFUL
;
3535 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3537 ldap_msgfree(result
);
3538 return NT_STATUS_UNSUCCESSFUL
;
3541 smbldap_set_mod(&mods
, modop
,
3542 get_attr_key2string(groupmap_attr_list
,
3543 LDAP_ATTR_SID_LIST
),
3544 sid_to_fstring(tmp
, member
));
3546 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3548 ldap_mods_free(mods
, True
);
3549 ldap_msgfree(result
);
3552 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3553 return NT_STATUS_MEMBER_IN_ALIAS
;
3556 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3557 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3560 if (rc
!= LDAP_SUCCESS
) {
3561 return NT_STATUS_UNSUCCESSFUL
;
3564 return NT_STATUS_OK
;
3567 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3568 const DOM_SID
*alias
,
3569 const DOM_SID
*member
)
3571 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3574 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3575 const DOM_SID
*alias
,
3576 const DOM_SID
*member
)
3578 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3582 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3583 const DOM_SID
*alias
,
3584 DOM_SID
**pp_members
,
3585 size_t *p_num_members
)
3587 struct ldapsam_privates
*ldap_state
=
3588 (struct ldapsam_privates
*)methods
->private_data
;
3589 LDAPMessage
*result
= NULL
;
3590 LDAPMessage
*entry
= NULL
;
3592 char **values
= NULL
;
3594 char *filter
= NULL
;
3595 size_t num_members
= 0;
3596 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3602 if (sid_check_is_in_builtin(alias
)) {
3603 type
= SID_NAME_ALIAS
;
3606 if (sid_check_is_in_our_domain(alias
)) {
3607 type
= SID_NAME_ALIAS
;
3610 if (type
== SID_NAME_USE_NONE
) {
3611 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3612 sid_string_dbg(alias
)));
3613 return NT_STATUS_NO_SUCH_ALIAS
;
3616 if (asprintf(&filter
,
3617 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3618 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3620 return NT_STATUS_NO_MEMORY
;
3623 if (ldapsam_search_one_group(ldap_state
, filter
,
3624 &result
) != LDAP_SUCCESS
) {
3626 return NT_STATUS_NO_SUCH_ALIAS
;
3629 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3633 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3634 ldap_msgfree(result
);
3636 return NT_STATUS_NO_SUCH_ALIAS
;
3640 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3641 "filter %s: count=%d\n", filter
, count
));
3642 ldap_msgfree(result
);
3644 return NT_STATUS_NO_SUCH_ALIAS
;
3649 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3653 ldap_msgfree(result
);
3654 return NT_STATUS_UNSUCCESSFUL
;
3657 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3659 get_attr_key2string(groupmap_attr_list
,
3660 LDAP_ATTR_SID_LIST
));
3662 if (values
== NULL
) {
3663 ldap_msgfree(result
);
3664 return NT_STATUS_OK
;
3667 count
= ldap_count_values(values
);
3669 for (i
=0; i
<count
; i
++) {
3673 if (!string_to_sid(&member
, values
[i
]))
3676 status
= add_sid_to_array(NULL
, &member
, pp_members
,
3678 if (!NT_STATUS_IS_OK(status
)) {
3679 ldap_value_free(values
);
3680 ldap_msgfree(result
);
3685 *p_num_members
= num_members
;
3686 ldap_value_free(values
);
3687 ldap_msgfree(result
);
3689 return NT_STATUS_OK
;
3692 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3693 TALLOC_CTX
*mem_ctx
,
3694 const DOM_SID
*domain_sid
,
3695 const DOM_SID
*members
,
3697 uint32
**pp_alias_rids
,
3698 size_t *p_num_alias_rids
)
3700 struct ldapsam_privates
*ldap_state
=
3701 (struct ldapsam_privates
*)methods
->private_data
;
3704 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3706 LDAPMessage
*result
= NULL
;
3707 LDAPMessage
*entry
= NULL
;
3711 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3713 if (sid_check_is_builtin(domain_sid
)) {
3714 type
= SID_NAME_ALIAS
;
3717 if (sid_check_is_domain(domain_sid
)) {
3718 type
= SID_NAME_ALIAS
;
3721 if (type
== SID_NAME_USE_NONE
) {
3722 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3723 sid_string_dbg(domain_sid
)));
3724 return NT_STATUS_UNSUCCESSFUL
;
3727 filter
= talloc_asprintf(mem_ctx
,
3728 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3729 LDAP_OBJ_GROUPMAP
, type
);
3731 for (i
=0; i
<num_members
; i
++)
3732 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3734 sid_string_talloc(mem_ctx
,
3737 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3739 if (filter
== NULL
) {
3740 return NT_STATUS_NO_MEMORY
;
3743 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3744 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3746 if (rc
!= LDAP_SUCCESS
)
3747 return NT_STATUS_UNSUCCESSFUL
;
3749 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3751 for (entry
= ldap_first_entry(ldap_struct
, result
);
3753 entry
= ldap_next_entry(ldap_struct
, entry
))
3759 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3765 if (!string_to_sid(&sid
, sid_str
))
3768 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3771 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3772 p_num_alias_rids
)) {
3773 ldap_msgfree(result
);
3774 return NT_STATUS_NO_MEMORY
;
3778 ldap_msgfree(result
);
3779 return NT_STATUS_OK
;
3782 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3786 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3788 LDAPMod
**mods
= NULL
;
3789 fstring value_string
;
3790 const char *policy_attr
= NULL
;
3792 struct ldapsam_privates
*ldap_state
=
3793 (struct ldapsam_privates
*)methods
->private_data
;
3795 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3797 if (!ldap_state
->domain_dn
) {
3798 return NT_STATUS_INVALID_PARAMETER
;
3801 policy_attr
= get_account_policy_attr(policy_index
);
3802 if (policy_attr
== NULL
) {
3803 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3808 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3810 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3812 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3815 ldap_mods_free(mods
, True
);
3817 if (rc
!= LDAP_SUCCESS
) {
3821 if (!cache_account_policy_set(policy_index
, value
)) {
3822 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3823 "update local tdb cache\n"));
3827 return NT_STATUS_OK
;
3830 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3831 int policy_index
, uint32 value
)
3833 return ldapsam_set_account_policy_in_ldap(methods
, policy_index
,
3837 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3841 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3842 LDAPMessage
*result
= NULL
;
3843 LDAPMessage
*entry
= NULL
;
3848 const char *policy_attr
= NULL
;
3850 struct ldapsam_privates
*ldap_state
=
3851 (struct ldapsam_privates
*)methods
->private_data
;
3853 const char *attrs
[2];
3855 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3857 if (!ldap_state
->domain_dn
) {
3858 return NT_STATUS_INVALID_PARAMETER
;
3861 policy_attr
= get_account_policy_attr(policy_index
);
3863 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3864 "policy index: %d\n", policy_index
));
3868 attrs
[0] = policy_attr
;
3871 filter
= talloc_asprintf(NULL
, "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3872 if (filter
== NULL
) {
3873 return NT_STATUS_NO_MEMORY
;
3875 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3876 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
3879 if (rc
!= LDAP_SUCCESS
) {
3883 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3888 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3889 if (entry
== NULL
) {
3893 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
3898 *value
= (uint32
)atol(vals
[0]);
3900 ntstatus
= NT_STATUS_OK
;
3904 ldap_value_free(vals
);
3905 ldap_msgfree(result
);
3910 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3912 - if user hasn't decided to use account policies inside LDAP just reuse the
3915 - if there is a valid cache entry, return that
3916 - if there is an LDAP entry, update cache and return
3917 - otherwise set to default, update cache and return
3921 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
3922 int policy_index
, uint32
*value
)
3924 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3926 if (cache_account_policy_get(policy_index
, value
)) {
3927 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3929 return NT_STATUS_OK
;
3932 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, policy_index
,
3934 if (NT_STATUS_IS_OK(ntstatus
)) {
3938 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3942 /* should we automagically migrate old tdb value here ? */
3943 if (account_policy_get(policy_index
, value
))
3946 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3947 "default\n", policy_index
));
3950 if (!account_policy_get_default(policy_index
, value
)) {
3956 ntstatus
= ldapsam_set_account_policy(methods
, policy_index
, *value
);
3957 if (!NT_STATUS_IS_OK(ntstatus
)) {
3963 if (!cache_account_policy_set(policy_index
, *value
)) {
3964 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3965 "tdb as a cache\n"));
3966 return NT_STATUS_UNSUCCESSFUL
;
3969 return NT_STATUS_OK
;
3972 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
3973 const DOM_SID
*domain_sid
,
3977 enum lsa_SidType
*attrs
)
3979 struct ldapsam_privates
*ldap_state
=
3980 (struct ldapsam_privates
*)methods
->private_data
;
3981 LDAPMessage
*msg
= NULL
;
3983 char *allsids
= NULL
;
3984 int i
, rc
, num_mapped
;
3985 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
3986 TALLOC_CTX
*mem_ctx
;
3990 mem_ctx
= talloc_new(NULL
);
3991 if (mem_ctx
== NULL
) {
3992 DEBUG(0, ("talloc_new failed\n"));
3996 if (!sid_check_is_builtin(domain_sid
) &&
3997 !sid_check_is_domain(domain_sid
)) {
3998 result
= NT_STATUS_INVALID_PARAMETER
;
4002 for (i
=0; i
<num_rids
; i
++)
4003 attrs
[i
] = SID_NAME_UNKNOWN
;
4005 allsids
= talloc_strdup(mem_ctx
, "");
4006 if (allsids
== NULL
) {
4010 for (i
=0; i
<num_rids
; i
++) {
4012 sid_compose(&sid
, domain_sid
, rids
[i
]);
4013 allsids
= talloc_asprintf_append_buffer(
4014 allsids
, "(sambaSid=%s)",
4015 sid_string_talloc(mem_ctx
, &sid
));
4016 if (allsids
== NULL
) {
4021 /* First look for users */
4025 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4027 filter
= talloc_asprintf(
4028 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4029 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4031 if (filter
== NULL
) {
4035 rc
= smbldap_search(ldap_state
->smbldap_state
,
4036 lp_ldap_user_suffix(),
4037 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4039 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4042 if (rc
!= LDAP_SUCCESS
)
4045 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4048 for (entry
= ldap_first_entry(ld
, msg
);
4050 entry
= ldap_next_entry(ld
, entry
)) {
4055 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4057 DEBUG(2, ("Could not find sid from ldap entry\n"));
4061 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4064 DEBUG(2, ("Could not retrieve uid attribute\n"));
4068 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4069 if (rid
== rids
[rid_index
])
4073 if (rid_index
== num_rids
) {
4074 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4078 attrs
[rid_index
] = SID_NAME_USER
;
4079 names
[rid_index
] = name
;
4083 if (num_mapped
== num_rids
) {
4084 /* No need to look for groups anymore -- we're done */
4085 result
= NT_STATUS_OK
;
4089 /* Same game for groups */
4093 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4094 "sambaGroupType", NULL
};
4096 filter
= talloc_asprintf(
4097 mem_ctx
, "(&(objectClass=%s)(|%s))",
4098 LDAP_OBJ_GROUPMAP
, allsids
);
4099 if (filter
== NULL
) {
4103 rc
= smbldap_search(ldap_state
->smbldap_state
,
4105 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4107 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4110 if (rc
!= LDAP_SUCCESS
)
4113 /* ldap_struct might have changed due to a reconnect */
4115 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4117 /* For consistency checks, we already checked we're only domain or builtin */
4119 is_builtin
= sid_check_is_builtin(domain_sid
);
4121 for (entry
= ldap_first_entry(ld
, msg
);
4123 entry
= ldap_next_entry(ld
, entry
))
4128 enum lsa_SidType type
;
4129 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4131 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4134 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4139 type
= (enum lsa_SidType
)atol(attr
);
4141 /* Consistency checks */
4142 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4143 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4144 (type
!= SID_NAME_DOM_GRP
)))) {
4145 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4148 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4150 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4154 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4157 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4159 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4163 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4168 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4169 if (rid
== rids
[rid_index
])
4173 if (rid_index
== num_rids
) {
4174 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4178 attrs
[rid_index
] = type
;
4179 names
[rid_index
] = attr
;
4183 result
= NT_STATUS_NONE_MAPPED
;
4186 result
= (num_mapped
== num_rids
) ?
4187 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4189 TALLOC_FREE(mem_ctx
);
4193 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4195 char *filter
= NULL
;
4196 char *escaped
= NULL
;
4197 char *result
= NULL
;
4199 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4200 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4204 escaped
= escape_ldap_string_alloc(username
);
4205 if (escaped
== NULL
) goto done
;
4207 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4216 const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4220 const char **result
;
4222 va_start(ap
, mem_ctx
);
4223 while (va_arg(ap
, const char *) != NULL
)
4227 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4231 va_start(ap
, mem_ctx
);
4232 for (i
=0; i
<num
; i
++) {
4233 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4234 if (result
[i
] == NULL
) {
4235 talloc_free(result
);
4246 struct ldap_search_state
{
4247 struct smbldap_state
*connection
;
4257 void *pagedresults_cookie
;
4259 LDAPMessage
*entries
, *current_entry
;
4260 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4261 TALLOC_CTX
*mem_ctx
,
4262 LDAP
*ld
, LDAPMessage
*entry
,
4263 struct samr_displayentry
*result
);
4266 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4268 struct ldap_search_state
*state
=
4269 (struct ldap_search_state
*)search
->private_data
;
4271 int rc
= LDAP_OPERATIONS_ERROR
;
4273 state
->entries
= NULL
;
4275 if (state
->connection
->paged_results
) {
4276 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4277 state
->scope
, state
->filter
,
4278 state
->attrs
, state
->attrsonly
,
4279 lp_ldap_page_size(), &state
->entries
,
4280 &state
->pagedresults_cookie
);
4283 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4285 if (state
->entries
!= NULL
) {
4286 /* Left over from unsuccessful paged attempt */
4287 ldap_msgfree(state
->entries
);
4288 state
->entries
= NULL
;
4291 rc
= smbldap_search(state
->connection
, state
->base
,
4292 state
->scope
, state
->filter
, state
->attrs
,
4293 state
->attrsonly
, &state
->entries
);
4295 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4298 /* Ok, the server was lying. It told us it could do paged
4299 * searches when it could not. */
4300 state
->connection
->paged_results
= False
;
4303 ld
= state
->connection
->ldap_struct
;
4305 DEBUG(5, ("Don't have an LDAP connection right after a "
4309 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4311 if (state
->current_entry
== NULL
) {
4312 ldap_msgfree(state
->entries
);
4313 state
->entries
= NULL
;
4319 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4321 struct ldap_search_state
*state
=
4322 (struct ldap_search_state
*)search
->private_data
;
4325 if (!state
->connection
->paged_results
) {
4326 /* There is no next page when there are no paged results */
4330 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4331 state
->scope
, state
->filter
, state
->attrs
,
4332 state
->attrsonly
, lp_ldap_page_size(),
4334 &state
->pagedresults_cookie
);
4336 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4339 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4341 if (state
->current_entry
== NULL
) {
4342 ldap_msgfree(state
->entries
);
4343 state
->entries
= NULL
;
4350 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4351 struct samr_displayentry
*entry
)
4353 struct ldap_search_state
*state
=
4354 (struct ldap_search_state
*)search
->private_data
;
4358 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4361 if ((state
->entries
== NULL
) &&
4362 !ldapsam_search_nextpage(search
))
4365 result
= state
->ldap2displayentry(state
, search
,
4366 state
->connection
->ldap_struct
,
4367 state
->current_entry
, entry
);
4371 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4372 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4373 if (dn
!= NULL
) ldap_memfree(dn
);
4376 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4378 if (state
->current_entry
== NULL
) {
4379 ldap_msgfree(state
->entries
);
4380 state
->entries
= NULL
;
4383 if (!result
) goto retry
;
4388 static void ldapsam_search_end(struct pdb_search
*search
)
4390 struct ldap_search_state
*state
=
4391 (struct ldap_search_state
*)search
->private_data
;
4394 if (state
->pagedresults_cookie
== NULL
)
4397 if (state
->entries
!= NULL
)
4398 ldap_msgfree(state
->entries
);
4400 state
->entries
= NULL
;
4401 state
->current_entry
= NULL
;
4403 if (!state
->connection
->paged_results
)
4406 /* Tell the LDAP server we're not interested in the rest anymore. */
4408 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4409 state
->filter
, state
->attrs
,
4410 state
->attrsonly
, 0, &state
->entries
,
4411 &state
->pagedresults_cookie
);
4413 if (rc
!= LDAP_SUCCESS
)
4414 DEBUG(5, ("Could not end search properly\n"));
4419 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4420 TALLOC_CTX
*mem_ctx
,
4421 LDAP
*ld
, LDAPMessage
*entry
,
4422 struct samr_displayentry
*result
)
4425 size_t converted_size
;
4429 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4430 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4431 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4434 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4435 ldap_value_free(vals
);
4437 if ((state
->acct_flags
!= 0) &&
4438 ((state
->acct_flags
& acct_flags
) == 0))
4441 result
->acct_flags
= acct_flags
;
4442 result
->account_name
= "";
4443 result
->fullname
= "";
4444 result
->description
= "";
4446 vals
= ldap_get_values(ld
, entry
, "uid");
4447 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4448 DEBUG(5, ("\"uid\" not found\n"));
4451 if (!pull_utf8_talloc(mem_ctx
,
4452 CONST_DISCARD(char **, &result
->account_name
),
4453 vals
[0], &converted_size
))
4455 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4459 ldap_value_free(vals
);
4461 vals
= ldap_get_values(ld
, entry
, "displayName");
4462 if ((vals
== NULL
) || (vals
[0] == NULL
))
4463 DEBUG(8, ("\"displayName\" not found\n"));
4464 else if (!pull_utf8_talloc(mem_ctx
,
4465 CONST_DISCARD(char **, &result
->fullname
),
4466 vals
[0], &converted_size
))
4468 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4472 ldap_value_free(vals
);
4474 vals
= ldap_get_values(ld
, entry
, "description");
4475 if ((vals
== NULL
) || (vals
[0] == NULL
))
4476 DEBUG(8, ("\"description\" not found\n"));
4477 else if (!pull_utf8_talloc(mem_ctx
,
4478 CONST_DISCARD(char **, &result
->description
),
4479 vals
[0], &converted_size
))
4481 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4485 ldap_value_free(vals
);
4487 if ((result
->account_name
== NULL
) ||
4488 (result
->fullname
== NULL
) ||
4489 (result
->description
== NULL
)) {
4490 DEBUG(0, ("talloc failed\n"));
4494 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4495 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4496 DEBUG(0, ("\"objectSid\" not found\n"));
4500 if (!string_to_sid(&sid
, vals
[0])) {
4501 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4502 ldap_value_free(vals
);
4505 ldap_value_free(vals
);
4507 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4508 DEBUG(0, ("sid %s does not belong to our domain\n",
4509 sid_string_dbg(&sid
)));
4517 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4518 struct pdb_search
*search
,
4521 struct ldapsam_privates
*ldap_state
=
4522 (struct ldapsam_privates
*)methods
->private_data
;
4523 struct ldap_search_state
*state
;
4525 state
= talloc(search
, struct ldap_search_state
);
4526 if (state
== NULL
) {
4527 DEBUG(0, ("talloc failed\n"));
4531 state
->connection
= ldap_state
->smbldap_state
;
4533 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4534 state
->base
= lp_ldap_user_suffix();
4535 else if ((acct_flags
!= 0) &&
4536 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4537 state
->base
= lp_ldap_machine_suffix();
4539 state
->base
= lp_ldap_suffix();
4541 state
->acct_flags
= acct_flags
;
4542 state
->base
= talloc_strdup(search
, state
->base
);
4543 state
->scope
= LDAP_SCOPE_SUBTREE
;
4544 state
->filter
= get_ldap_filter(search
, "*");
4545 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4546 "displayName", "description",
4547 "sambaAcctFlags", NULL
);
4548 state
->attrsonly
= 0;
4549 state
->pagedresults_cookie
= NULL
;
4550 state
->entries
= NULL
;
4551 state
->ldap2displayentry
= ldapuser2displayentry
;
4553 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4554 DEBUG(0, ("talloc failed\n"));
4558 search
->private_data
= state
;
4559 search
->next_entry
= ldapsam_search_next_entry
;
4560 search
->search_end
= ldapsam_search_end
;
4562 return ldapsam_search_firstpage(search
);
4565 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4566 TALLOC_CTX
*mem_ctx
,
4567 LDAP
*ld
, LDAPMessage
*entry
,
4568 struct samr_displayentry
*result
)
4571 size_t converted_size
;
4575 result
->account_name
= "";
4576 result
->fullname
= "";
4577 result
->description
= "";
4580 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4581 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4582 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4584 ldap_value_free(vals
);
4589 group_type
= atoi(vals
[0]);
4591 if ((state
->group_type
!= 0) &&
4592 ((state
->group_type
!= group_type
))) {
4593 ldap_value_free(vals
);
4597 ldap_value_free(vals
);
4599 /* display name is the NT group name */
4601 vals
= ldap_get_values(ld
, entry
, "displayName");
4602 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4603 DEBUG(8, ("\"displayName\" not found\n"));
4605 /* fallback to the 'cn' attribute */
4606 vals
= ldap_get_values(ld
, entry
, "cn");
4607 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4608 DEBUG(5, ("\"cn\" not found\n"));
4611 if (!pull_utf8_talloc(mem_ctx
,
4612 CONST_DISCARD(char **,
4613 &result
->account_name
),
4614 vals
[0], &converted_size
))
4616 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4617 "failed: %s", strerror(errno
)));
4620 else if (!pull_utf8_talloc(mem_ctx
,
4621 CONST_DISCARD(char **,
4622 &result
->account_name
),
4623 vals
[0], &converted_size
))
4625 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4629 ldap_value_free(vals
);
4631 vals
= ldap_get_values(ld
, entry
, "description");
4632 if ((vals
== NULL
) || (vals
[0] == NULL
))
4633 DEBUG(8, ("\"description\" not found\n"));
4634 else if (!pull_utf8_talloc(mem_ctx
,
4635 CONST_DISCARD(char **, &result
->description
),
4636 vals
[0], &converted_size
))
4638 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4641 ldap_value_free(vals
);
4643 if ((result
->account_name
== NULL
) ||
4644 (result
->fullname
== NULL
) ||
4645 (result
->description
== NULL
)) {
4646 DEBUG(0, ("talloc failed\n"));
4650 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4651 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4652 DEBUG(0, ("\"objectSid\" not found\n"));
4654 ldap_value_free(vals
);
4659 if (!string_to_sid(&sid
, vals
[0])) {
4660 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4664 ldap_value_free(vals
);
4666 switch (group_type
) {
4667 case SID_NAME_DOM_GRP
:
4668 case SID_NAME_ALIAS
:
4670 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4671 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4673 DEBUG(0, ("%s is not in our domain\n",
4674 sid_string_dbg(&sid
)));
4680 DEBUG(0,("unkown group type: %d\n", group_type
));
4684 result
->acct_flags
= 0;
4689 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4690 struct pdb_search
*search
,
4692 enum lsa_SidType type
)
4694 struct ldapsam_privates
*ldap_state
=
4695 (struct ldapsam_privates
*)methods
->private_data
;
4696 struct ldap_search_state
*state
;
4699 state
= talloc(search
, struct ldap_search_state
);
4700 if (state
== NULL
) {
4701 DEBUG(0, ("talloc failed\n"));
4705 state
->connection
= ldap_state
->smbldap_state
;
4707 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4708 state
->connection
= ldap_state
->smbldap_state
;
4709 state
->scope
= LDAP_SCOPE_SUBTREE
;
4710 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4711 "(sambaGroupType=%d)(sambaSID=%s*))",
4713 type
, sid_to_fstring(tmp
, sid
));
4714 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4715 "displayName", "description",
4716 "sambaGroupType", NULL
);
4717 state
->attrsonly
= 0;
4718 state
->pagedresults_cookie
= NULL
;
4719 state
->entries
= NULL
;
4720 state
->group_type
= type
;
4721 state
->ldap2displayentry
= ldapgroup2displayentry
;
4723 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4724 DEBUG(0, ("talloc failed\n"));
4728 search
->private_data
= state
;
4729 search
->next_entry
= ldapsam_search_next_entry
;
4730 search
->search_end
= ldapsam_search_end
;
4732 return ldapsam_search_firstpage(search
);
4735 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4736 struct pdb_search
*search
)
4738 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4741 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4742 struct pdb_search
*search
,
4745 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4748 static bool ldapsam_rid_algorithm(struct pdb_methods
*methods
)
4753 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4756 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4758 LDAPMessage
*result
= NULL
;
4759 LDAPMessage
*entry
= NULL
;
4760 LDAPMod
**mods
= NULL
;
4767 TALLOC_CTX
*mem_ctx
;
4769 mem_ctx
= talloc_new(NULL
);
4770 if (mem_ctx
== NULL
) {
4771 DEBUG(0, ("talloc_new failed\n"));
4772 return NT_STATUS_NO_MEMORY
;
4775 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4776 get_global_sam_name(), False
);
4777 if (!NT_STATUS_IS_OK(status
)) {
4778 DEBUG(3, ("Could not get domain info: %s\n",
4779 nt_errstr(status
)));
4783 talloc_autofree_ldapmsg(mem_ctx
, result
);
4785 entry
= ldap_first_entry(priv2ld(priv
), result
);
4786 if (entry
== NULL
) {
4787 DEBUG(0, ("Could not get domain info entry\n"));
4788 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4792 /* Find the largest of the three attributes "sambaNextRid",
4793 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4794 concept of differentiating between user and group rids, and will
4795 use only "sambaNextRid" in the future. But for compatibility
4796 reasons I look if others have chosen different strategies -- VL */
4798 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4799 "sambaNextRid", mem_ctx
);
4800 if (value
!= NULL
) {
4801 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4802 nextRid
= MAX(nextRid
, tmp
);
4805 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4806 "sambaNextUserRid", mem_ctx
);
4807 if (value
!= NULL
) {
4808 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4809 nextRid
= MAX(nextRid
, tmp
);
4812 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4813 "sambaNextGroupRid", mem_ctx
);
4814 if (value
!= NULL
) {
4815 uint32 tmp
= (uint32
)strtoul(value
, NULL
, 10);
4816 nextRid
= MAX(nextRid
, tmp
);
4820 nextRid
= BASE_RID
-1;
4825 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4826 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4827 talloc_autofree_ldapmod(mem_ctx
, mods
);
4829 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4830 status
= NT_STATUS_NO_MEMORY
;
4834 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4836 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4839 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4842 if (NT_STATUS_IS_OK(status
)) {
4846 TALLOC_FREE(mem_ctx
);
4850 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32
*rid
)
4854 for (i
=0; i
<10; i
++) {
4855 NTSTATUS result
= ldapsam_get_new_rid(
4856 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4857 if (NT_STATUS_IS_OK(result
)) {
4861 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4865 /* The ldap update failed (maybe a race condition), retry */
4868 /* Tried 10 times, fail. */
4869 return NT_STATUS_ACCESS_DENIED
;
4872 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32
*rid
)
4874 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4875 return NT_STATUS_IS_OK(result
) ? True
: False
;
4878 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4880 union unid_t
*id
, enum lsa_SidType
*type
)
4882 struct ldapsam_privates
*priv
=
4883 (struct ldapsam_privates
*)methods
->private_data
;
4885 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4887 LDAPMessage
*result
= NULL
;
4888 LDAPMessage
*entry
= NULL
;
4893 TALLOC_CTX
*mem_ctx
;
4895 mem_ctx
= talloc_new(NULL
);
4896 if (mem_ctx
== NULL
) {
4897 DEBUG(0, ("talloc_new failed\n"));
4901 filter
= talloc_asprintf(mem_ctx
,
4903 "(|(objectClass=%s)(objectClass=%s)))",
4904 sid_string_talloc(mem_ctx
, sid
),
4905 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
4906 if (filter
== NULL
) {
4907 DEBUG(5, ("talloc_asprintf failed\n"));
4911 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
4913 if (rc
!= LDAP_SUCCESS
) {
4916 talloc_autofree_ldapmsg(mem_ctx
, result
);
4918 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
4919 DEBUG(10, ("Got %d entries, expected one\n",
4920 ldap_count_entries(priv2ld(priv
), result
)));
4924 entry
= ldap_first_entry(priv2ld(priv
), result
);
4926 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4927 "sambaGroupType", mem_ctx
);
4929 if (value
!= NULL
) {
4930 const char *gid_str
;
4933 gid_str
= smbldap_talloc_single_attribute(
4934 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
4935 if (gid_str
== NULL
) {
4936 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4937 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
4942 id
->gid
= strtoul(gid_str
, NULL
, 10);
4943 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
4948 /* It must be a user */
4950 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4951 "uidNumber", mem_ctx
);
4952 if (value
== NULL
) {
4953 DEBUG(1, ("Could not find uidNumber in %s\n",
4954 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
4958 id
->uid
= strtoul(value
, NULL
, 10);
4959 *type
= SID_NAME_USER
;
4963 TALLOC_FREE(mem_ctx
);
4968 * The following functions is called only if
4969 * ldapsam:trusted and ldapsam:editposix are
4974 * ldapsam_create_user creates a new
4975 * posixAccount and sambaSamAccount object
4976 * in the ldap users subtree
4978 * The uid is allocated by winbindd.
4981 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
4982 TALLOC_CTX
*tmp_ctx
, const char *name
,
4983 uint32 acb_info
, uint32
*rid
)
4985 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
4986 LDAPMessage
*entry
= NULL
;
4987 LDAPMessage
*result
= NULL
;
4989 bool is_machine
= False
;
4990 bool add_posix
= False
;
4991 LDAPMod
**mods
= NULL
;
4999 const char *dn
= NULL
;
5007 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5008 acb_info
& ACB_WSTRUST
||
5009 acb_info
& ACB_SVRTRUST
||
5010 acb_info
& ACB_DOMTRUST
) {
5014 username
= escape_ldap_string_alloc(name
);
5015 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5016 username
, LDAP_OBJ_POSIXACCOUNT
);
5017 SAFE_FREE(username
);
5019 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5020 if (rc
!= LDAP_SUCCESS
) {
5021 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5022 return NT_STATUS_ACCESS_DENIED
;
5024 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5026 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5028 if (num_result
> 1) {
5029 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5030 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5033 if (num_result
== 1) {
5035 /* check if it is just a posix account.
5036 * or if there is a sid attached to this entry
5039 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5041 return NT_STATUS_UNSUCCESSFUL
;
5044 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5046 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5047 return NT_STATUS_USER_EXISTS
;
5050 /* it is just a posix account, retrieve the dn for later use */
5051 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5053 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5054 return NT_STATUS_NO_MEMORY
;
5058 if (num_result
== 0) {
5062 /* Create the basic samu structure and generate the mods for the ldap commit */
5063 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5064 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5068 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5070 user
= samu_new(tmp_ctx
);
5072 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5073 return NT_STATUS_NO_MEMORY
;
5076 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5077 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5078 return NT_STATUS_UNSUCCESSFUL
;
5080 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5081 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5082 return NT_STATUS_UNSUCCESSFUL
;
5085 if (acb_info
& ACB_NORMAL
) {
5086 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5087 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5088 return NT_STATUS_UNSUCCESSFUL
;
5091 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5092 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5093 return NT_STATUS_UNSUCCESSFUL
;
5097 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5098 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5099 return NT_STATUS_UNSUCCESSFUL
;
5103 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5104 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5105 return NT_STATUS_UNSUCCESSFUL
;
5108 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5109 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5110 return NT_STATUS_UNSUCCESSFUL
;
5113 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5114 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5116 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5121 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5123 /* retrieve the Domain Users group gid */
5124 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
) ||
5125 !sid_to_gid(&group_sid
, &gid
)) {
5126 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5127 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5130 /* lets allocate a new userid for this user */
5131 if (!winbind_allocate_uid(&uid
)) {
5132 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5133 return NT_STATUS_UNSUCCESSFUL
;
5138 /* TODO: choose a more appropriate default for machines */
5139 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5140 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5142 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5143 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5145 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5146 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5148 escape_name
= escape_rdn_val_string_alloc(name
);
5150 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5151 return NT_STATUS_NO_MEMORY
;
5155 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5157 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5160 SAFE_FREE(escape_name
);
5162 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5163 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5164 return NT_STATUS_NO_MEMORY
;
5167 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5168 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5169 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5170 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5171 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5172 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5173 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5176 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5179 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5181 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5184 if (rc
!= LDAP_SUCCESS
) {
5185 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5186 return NT_STATUS_UNSUCCESSFUL
;
5189 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5191 flush_pwnam_cache();
5193 return NT_STATUS_OK
;
5196 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5197 TALLOC_CTX
*tmp_ctx
,
5201 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
5202 TALLOC_CTX
*mem_ctx
,
5206 size_t *p_num_groups
);
5208 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5210 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5211 LDAPMessage
*result
= NULL
;
5212 LDAPMessage
*entry
= NULL
;
5218 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5220 filter
= talloc_asprintf(tmp_ctx
,
5223 "(objectClass=%s))",
5224 pdb_get_username(sam_acct
),
5225 LDAP_OBJ_POSIXACCOUNT
,
5226 LDAP_OBJ_SAMBASAMACCOUNT
);
5227 if (filter
== NULL
) {
5228 return NT_STATUS_NO_MEMORY
;
5231 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5232 if (rc
!= LDAP_SUCCESS
) {
5233 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5234 return NT_STATUS_UNSUCCESSFUL
;
5236 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5238 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5240 if (num_result
== 0) {
5241 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5242 return NT_STATUS_NO_SUCH_USER
;
5245 if (num_result
> 1) {
5246 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5247 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5250 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5252 return NT_STATUS_UNSUCCESSFUL
;
5255 /* it is just a posix account, retrieve the dn for later use */
5256 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5258 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5259 return NT_STATUS_NO_MEMORY
;
5262 /* try to remove memberships first */
5265 struct dom_sid
*sids
= NULL
;
5267 size_t num_groups
= 0;
5269 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5271 status
= ldapsam_enum_group_memberships(my_methods
,
5277 if (!NT_STATUS_IS_OK(status
)) {
5281 for (i
=0; i
< num_groups
; i
++) {
5285 sid_peek_rid(&sids
[i
], &group_rid
);
5287 ldapsam_del_groupmem(my_methods
,
5296 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5297 if (rc
!= LDAP_SUCCESS
) {
5298 return NT_STATUS_UNSUCCESSFUL
;
5301 flush_pwnam_cache();
5303 return NT_STATUS_OK
;
5307 * ldapsam_create_group creates a new
5308 * posixGroup and sambaGroupMapping object
5309 * in the ldap groups subtree
5311 * The gid is allocated by winbindd.
5314 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5315 TALLOC_CTX
*tmp_ctx
,
5319 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5321 LDAPMessage
*entry
= NULL
;
5322 LDAPMessage
*result
= NULL
;
5324 bool is_new_entry
= False
;
5325 LDAPMod
**mods
= NULL
;
5331 const char *dn
= NULL
;
5336 groupname
= escape_ldap_string_alloc(name
);
5337 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5338 groupname
, LDAP_OBJ_POSIXGROUP
);
5339 SAFE_FREE(groupname
);
5341 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5342 if (rc
!= LDAP_SUCCESS
) {
5343 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5344 return NT_STATUS_UNSUCCESSFUL
;
5346 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5348 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5350 if (num_result
> 1) {
5351 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5352 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5355 if (num_result
== 1) {
5357 /* check if it is just a posix group.
5358 * or if there is a sid attached to this entry
5361 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5363 return NT_STATUS_UNSUCCESSFUL
;
5366 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5368 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5369 return NT_STATUS_GROUP_EXISTS
;
5372 /* it is just a posix group, retrieve the gid and the dn for later use */
5373 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5375 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5376 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5379 gid
= strtoul(tmp
, NULL
, 10);
5381 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5383 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5384 return NT_STATUS_NO_MEMORY
;
5388 if (num_result
== 0) {
5391 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5393 is_new_entry
= True
;
5395 /* lets allocate a new groupid for this group */
5396 if (!winbind_allocate_gid(&gid
)) {
5397 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5398 return NT_STATUS_UNSUCCESSFUL
;
5401 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5403 escape_name
= escape_rdn_val_string_alloc(name
);
5405 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5406 return NT_STATUS_NO_MEMORY
;
5409 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5411 SAFE_FREE(escape_name
);
5413 if (!gidstr
|| !dn
) {
5414 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5415 return NT_STATUS_NO_MEMORY
;
5418 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5419 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5420 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5423 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5424 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5428 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5430 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5432 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5434 if (!groupsidstr
|| !grouptype
) {
5435 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5436 return NT_STATUS_NO_MEMORY
;
5439 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5440 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5441 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5442 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5443 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5446 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5448 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5449 /* This call may fail with rfc2307bis schema */
5450 /* Retry adding a structural class */
5451 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5452 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5456 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5459 if (rc
!= LDAP_SUCCESS
) {
5460 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5461 return NT_STATUS_UNSUCCESSFUL
;
5464 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5466 return NT_STATUS_OK
;
5469 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32 rid
)
5471 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5472 LDAPMessage
*result
= NULL
;
5473 LDAPMessage
*entry
= NULL
;
5481 /* get the group sid */
5482 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5484 filter
= talloc_asprintf(tmp_ctx
,
5487 "(objectClass=%s))",
5488 sid_string_talloc(tmp_ctx
, &group_sid
),
5489 LDAP_OBJ_POSIXGROUP
,
5491 if (filter
== NULL
) {
5492 return NT_STATUS_NO_MEMORY
;
5495 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5496 if (rc
!= LDAP_SUCCESS
) {
5497 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5498 return NT_STATUS_UNSUCCESSFUL
;
5500 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5502 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5504 if (num_result
== 0) {
5505 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5506 return NT_STATUS_NO_SUCH_GROUP
;
5509 if (num_result
> 1) {
5510 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5511 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5514 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5516 return NT_STATUS_UNSUCCESSFUL
;
5519 /* here it is, retrieve the dn for later use */
5520 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5522 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5523 return NT_STATUS_NO_MEMORY
;
5526 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5528 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5529 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5532 /* check no user have this group marked as primary group */
5533 filter
= talloc_asprintf(tmp_ctx
,
5536 "(objectClass=%s))",
5538 LDAP_OBJ_POSIXACCOUNT
,
5539 LDAP_OBJ_SAMBASAMACCOUNT
);
5541 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5542 if (rc
!= LDAP_SUCCESS
) {
5543 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5544 return NT_STATUS_UNSUCCESSFUL
;
5546 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5548 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5550 if (num_result
!= 0) {
5551 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5552 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5555 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5556 if (rc
!= LDAP_SUCCESS
) {
5557 return NT_STATUS_UNSUCCESSFUL
;
5560 return NT_STATUS_OK
;
5563 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5564 TALLOC_CTX
*tmp_ctx
,
5569 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5570 LDAPMessage
*entry
= NULL
;
5571 LDAPMessage
*result
= NULL
;
5573 LDAPMod
**mods
= NULL
;
5576 const char *dn
= NULL
;
5583 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5585 case LDAP_MOD_DELETE
:
5586 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5589 return NT_STATUS_UNSUCCESSFUL
;
5592 /* get member sid */
5593 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5595 /* get the group sid */
5596 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5598 filter
= talloc_asprintf(tmp_ctx
,
5601 "(objectClass=%s))",
5602 sid_string_talloc(tmp_ctx
, &member_sid
),
5603 LDAP_OBJ_POSIXACCOUNT
,
5604 LDAP_OBJ_SAMBASAMACCOUNT
);
5605 if (filter
== NULL
) {
5606 return NT_STATUS_NO_MEMORY
;
5609 /* get the member uid */
5610 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5611 if (rc
!= LDAP_SUCCESS
) {
5612 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5613 return NT_STATUS_UNSUCCESSFUL
;
5615 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5617 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5619 if (num_result
== 0) {
5620 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5621 return NT_STATUS_NO_SUCH_MEMBER
;
5624 if (num_result
> 1) {
5625 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5626 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5629 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5631 return NT_STATUS_UNSUCCESSFUL
;
5634 if (modop
== LDAP_MOD_DELETE
) {
5635 /* check if we are trying to remove the member from his primary group */
5637 gid_t user_gid
, group_gid
;
5639 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5641 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5642 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5645 user_gid
= strtoul(gidstr
, NULL
, 10);
5647 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5648 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5649 return NT_STATUS_UNSUCCESSFUL
;
5652 if (user_gid
== group_gid
) {
5653 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5654 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5658 /* here it is, retrieve the uid for later use */
5659 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5661 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5662 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5665 filter
= talloc_asprintf(tmp_ctx
,
5668 "(objectClass=%s))",
5669 sid_string_talloc(tmp_ctx
, &group_sid
),
5670 LDAP_OBJ_POSIXGROUP
,
5674 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5675 if (rc
!= LDAP_SUCCESS
) {
5676 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5677 return NT_STATUS_UNSUCCESSFUL
;
5679 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5681 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5683 if (num_result
== 0) {
5684 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5685 return NT_STATUS_NO_SUCH_GROUP
;
5688 if (num_result
> 1) {
5689 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5690 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5693 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5695 return NT_STATUS_UNSUCCESSFUL
;
5698 /* here it is, retrieve the dn for later use */
5699 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5701 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5702 return NT_STATUS_NO_MEMORY
;
5705 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5707 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5709 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5710 if (rc
!= LDAP_SUCCESS
) {
5711 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5712 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5713 return NT_STATUS_MEMBER_IN_GROUP
;
5715 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5716 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5717 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5719 return NT_STATUS_UNSUCCESSFUL
;
5722 return NT_STATUS_OK
;
5725 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5726 TALLOC_CTX
*tmp_ctx
,
5730 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5732 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5733 TALLOC_CTX
*tmp_ctx
,
5737 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5740 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5741 TALLOC_CTX
*mem_ctx
,
5742 struct samu
*sampass
)
5744 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5745 LDAPMessage
*entry
= NULL
;
5746 LDAPMessage
*result
= NULL
;
5748 LDAPMod
**mods
= NULL
;
5750 char *escape_username
;
5752 const char *dn
= NULL
;
5756 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
5758 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
5759 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5760 return NT_STATUS_UNSUCCESSFUL
;
5762 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
5764 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5765 return NT_STATUS_NO_MEMORY
;
5768 escape_username
= escape_ldap_string_alloc(pdb_get_username(sampass
));
5769 if (escape_username
== NULL
) {
5770 return NT_STATUS_NO_MEMORY
;
5773 filter
= talloc_asprintf(mem_ctx
,
5776 "(objectClass=%s))",
5778 LDAP_OBJ_POSIXACCOUNT
,
5779 LDAP_OBJ_SAMBASAMACCOUNT
);
5781 SAFE_FREE(escape_username
);
5783 if (filter
== NULL
) {
5784 return NT_STATUS_NO_MEMORY
;
5787 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5788 if (rc
!= LDAP_SUCCESS
) {
5789 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5790 return NT_STATUS_UNSUCCESSFUL
;
5792 talloc_autofree_ldapmsg(mem_ctx
, result
);
5794 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5796 if (num_result
== 0) {
5797 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5798 return NT_STATUS_NO_SUCH_USER
;
5801 if (num_result
> 1) {
5802 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
5803 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5806 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5808 return NT_STATUS_UNSUCCESSFUL
;
5811 /* retrieve the dn for later use */
5812 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
5814 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5815 return NT_STATUS_NO_MEMORY
;
5818 /* remove the old one, and add the new one, this way we do not risk races */
5819 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
5822 return NT_STATUS_OK
;
5825 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5827 if (rc
!= LDAP_SUCCESS
) {
5828 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5829 pdb_get_username(sampass
), gidstr
));
5830 return NT_STATUS_UNSUCCESSFUL
;
5833 flush_pwnam_cache();
5835 return NT_STATUS_OK
;
5839 /**********************************************************************
5840 trusted domains functions
5841 *********************************************************************/
5843 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
5846 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
5847 ldap_state
->domain_dn
);
5850 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
5851 TALLOC_CTX
*mem_ctx
,
5852 const char *domain
, LDAPMessage
**entry
)
5856 int scope
= LDAP_SCOPE_SUBTREE
;
5857 const char **attrs
= NULL
; /* NULL: get all attrs */
5858 int attrsonly
= 0; /* 0: return values too */
5859 LDAPMessage
*result
= NULL
;
5863 filter
= talloc_asprintf(talloc_tos(),
5864 "(&(objectClass=%s)(sambaDomainName=%s))",
5865 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
5867 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
5868 if (trusted_dn
== NULL
) {
5871 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
5872 filter
, attrs
, attrsonly
, &result
);
5874 if (result
!= NULL
) {
5875 talloc_autofree_ldapmsg(mem_ctx
, result
);
5878 if (rc
== LDAP_NO_SUCH_OBJECT
) {
5883 if (rc
!= LDAP_SUCCESS
) {
5887 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5889 if (num_result
> 1) {
5890 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5891 "%s object for domain '%s'?!\n",
5892 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
5896 if (num_result
== 0) {
5897 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5898 "%s object for domain %s.\n",
5899 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
5902 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5908 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
5912 time_t *pass_last_set_time
)
5914 struct ldapsam_privates
*ldap_state
=
5915 (struct ldapsam_privates
*)methods
->private_data
;
5916 LDAPMessage
*entry
= NULL
;
5918 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
5920 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
5929 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5930 entry
, "sambaClearTextPassword", talloc_tos());
5931 if (pwd_str
== NULL
) {
5934 /* trusteddom_pw routines do not use talloc yet... */
5935 *pwd
= SMB_STRDUP(pwd_str
);
5941 /* last change time */
5942 if (pass_last_set_time
!= NULL
) {
5944 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5945 entry
, "sambaPwdLastSet", talloc_tos());
5946 if (time_str
== NULL
) {
5949 *pass_last_set_time
= (time_t)atol(time_str
);
5956 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
5959 if (sid_str
== NULL
) {
5962 dom_sid
= string_sid_talloc(talloc_tos(), sid_str
);
5963 if (dom_sid
== NULL
) {
5966 sid_copy(sid
, dom_sid
);
5972 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
5977 struct ldapsam_privates
*ldap_state
=
5978 (struct ldapsam_privates
*)methods
->private_data
;
5979 LDAPMessage
*entry
= NULL
;
5980 LDAPMod
**mods
= NULL
;
5981 char *prev_pwd
= NULL
;
5982 char *trusted_dn
= NULL
;
5985 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
5988 * get the current entry (if there is one) in order to put the
5989 * current password into the previous password attribute
5991 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
5996 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
5997 LDAP_OBJ_TRUSTDOM_PASSWORD
);
5998 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6000 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6001 sid_string_tos(sid
));
6002 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6003 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6004 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6005 "sambaClearTextPassword", pwd
);
6007 talloc_autofree_ldapmod(talloc_tos(), mods
);
6009 if (entry
!= NULL
) {
6010 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6011 entry
, "sambaClearTextPassword", talloc_tos());
6012 if (prev_pwd
!= NULL
) {
6013 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6014 "sambaPreviousClearTextPassword",
6019 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6020 if (trusted_dn
== NULL
) {
6023 if (entry
== NULL
) {
6024 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6026 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6029 if (rc
!= LDAP_SUCCESS
) {
6030 DEBUG(1, ("error writing trusted domain password!\n"));
6037 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6041 struct ldapsam_privates
*ldap_state
=
6042 (struct ldapsam_privates
*)methods
->private_data
;
6043 LDAPMessage
*entry
= NULL
;
6044 const char *trusted_dn
;
6046 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6050 if (entry
== NULL
) {
6051 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6056 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6058 if (trusted_dn
== NULL
) {
6059 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6063 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6064 if (rc
!= LDAP_SUCCESS
) {
6071 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6072 TALLOC_CTX
*mem_ctx
,
6073 uint32
*num_domains
,
6074 struct trustdom_info
***domains
)
6077 struct ldapsam_privates
*ldap_state
=
6078 (struct ldapsam_privates
*)methods
->private_data
;
6080 int scope
= LDAP_SCOPE_SUBTREE
;
6081 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6082 int attrsonly
= 0; /* 0: return values too */
6083 LDAPMessage
*result
= NULL
;
6084 LDAPMessage
*entry
= NULL
;
6086 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6087 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6089 rc
= smbldap_search(ldap_state
->smbldap_state
,
6090 ldap_state
->domain_dn
,
6097 if (result
!= NULL
) {
6098 talloc_autofree_ldapmsg(mem_ctx
, result
);
6101 if (rc
!= LDAP_SUCCESS
) {
6102 return NT_STATUS_UNSUCCESSFUL
;
6106 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6107 DEBUG(1, ("talloc failed\n"));
6108 return NT_STATUS_NO_MEMORY
;
6111 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6113 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6115 char *dom_name
, *dom_sid_str
;
6116 struct trustdom_info
*dom_info
;
6118 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6119 if (dom_info
== NULL
) {
6120 DEBUG(1, ("talloc failed\n"));
6121 return NT_STATUS_NO_MEMORY
;
6124 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6128 if (dom_name
== NULL
) {
6129 DEBUG(1, ("talloc failed\n"));
6130 return NT_STATUS_NO_MEMORY
;
6132 dom_info
->name
= dom_name
;
6134 dom_sid_str
= smbldap_talloc_single_attribute(
6135 priv2ld(ldap_state
), entry
, "sambaSID",
6137 if (dom_sid_str
== NULL
) {
6138 DEBUG(1, ("talloc failed\n"));
6139 return NT_STATUS_NO_MEMORY
;
6141 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6142 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6144 return NT_STATUS_UNSUCCESSFUL
;
6147 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6148 domains
, num_domains
);
6150 if (*domains
== NULL
) {
6151 DEBUG(1, ("talloc failed\n"));
6152 return NT_STATUS_NO_MEMORY
;
6156 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6157 return NT_STATUS_OK
;
6161 /**********************************************************************
6163 *********************************************************************/
6165 static void free_private_data(void **vp
)
6167 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6169 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6171 if ((*ldap_state
)->result
!= NULL
) {
6172 ldap_msgfree((*ldap_state
)->result
);
6173 (*ldap_state
)->result
= NULL
;
6175 if ((*ldap_state
)->domain_dn
!= NULL
) {
6176 SAFE_FREE((*ldap_state
)->domain_dn
);
6181 /* No need to free any further, as it is talloc()ed */
6184 /*********************************************************************
6185 Intitalise the parts of the pdb_methods structure that are common to
6187 *********************************************************************/
6189 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6192 struct ldapsam_privates
*ldap_state
;
6194 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6198 (*pdb_method
)->name
= "ldapsam";
6200 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6201 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6202 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6203 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6204 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6205 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6207 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6208 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6209 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6210 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6211 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6212 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6213 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6215 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6216 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6218 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6220 (*pdb_method
)->rid_algorithm
= ldapsam_rid_algorithm
;
6221 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6223 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6224 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6225 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6226 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6228 /* TODO: Setup private data and free */
6230 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6231 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6232 return NT_STATUS_NO_MEMORY
;
6235 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6236 location
, &ldap_state
->smbldap_state
);
6238 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6242 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6243 return NT_STATUS_NO_MEMORY
;
6246 (*pdb_method
)->private_data
= ldap_state
;
6248 (*pdb_method
)->free_private_data
= free_private_data
;
6250 return NT_STATUS_OK
;
6253 /**********************************************************************
6254 Initialise the 'compat' mode for pdb_ldap
6255 *********************************************************************/
6257 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6260 struct ldapsam_privates
*ldap_state
;
6261 char *uri
= talloc_strdup( NULL
, location
);
6263 trim_char( uri
, '\"', '\"' );
6264 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6268 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6272 (*pdb_method
)->name
= "ldapsam_compat";
6274 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6275 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6277 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6279 return NT_STATUS_OK
;
6282 /**********************************************************************
6283 Initialise the normal mode for pdb_ldap
6284 *********************************************************************/
6286 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6289 struct ldapsam_privates
*ldap_state
= NULL
;
6290 uint32 alg_rid_base
;
6291 char *alg_rid_base_string
= NULL
;
6292 LDAPMessage
*result
= NULL
;
6293 LDAPMessage
*entry
= NULL
;
6294 DOM_SID ldap_domain_sid
;
6295 DOM_SID secrets_domain_sid
;
6296 char *domain_sid_string
= NULL
;
6298 char *uri
= talloc_strdup( NULL
, location
);
6300 trim_char( uri
, '\"', '\"' );
6301 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6306 if (!NT_STATUS_IS_OK(nt_status
)) {
6310 (*pdb_method
)->name
= "ldapsam";
6312 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6313 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6314 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6315 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6316 (*pdb_method
)->search_users
= ldapsam_search_users
;
6317 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6318 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6320 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6321 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6322 (*pdb_method
)->enum_group_memberships
=
6323 ldapsam_enum_group_memberships
;
6324 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6325 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6327 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6328 (*pdb_method
)->create_user
= ldapsam_create_user
;
6329 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6330 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6331 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6332 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6333 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6334 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6338 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6339 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6341 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6343 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6345 ldap_state
->domain_name
, True
);
6347 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6348 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6349 "info, nor add one to the domain\n"));
6350 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6351 "will be unable to allocate new users/groups, "
6352 "and will risk BDCs having inconsistant SIDs\n"));
6353 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6354 return NT_STATUS_OK
;
6357 /* Given that the above might fail, everything below this must be
6360 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6363 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6365 ldap_msgfree(result
);
6366 return NT_STATUS_UNSUCCESSFUL
;
6369 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6371 ldap_msgfree(result
);
6372 return NT_STATUS_UNSUCCESSFUL
;
6375 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6378 domain_sid_string
= smbldap_talloc_single_attribute(
6379 ldap_state
->smbldap_state
->ldap_struct
,
6381 get_userattr_key2string(ldap_state
->schema_ver
,
6382 LDAP_ATTR_USER_SID
),
6385 if (domain_sid_string
) {
6387 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6388 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6389 "read as a valid SID\n", domain_sid_string
));
6390 ldap_msgfree(result
);
6391 TALLOC_FREE(domain_sid_string
);
6392 return NT_STATUS_INVALID_PARAMETER
;
6394 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6395 &secrets_domain_sid
);
6396 if (!found_sid
|| !sid_equal(&secrets_domain_sid
,
6397 &ldap_domain_sid
)) {
6398 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6399 "%s based on pdb_ldap results %s -> %s\n",
6400 ldap_state
->domain_name
,
6401 sid_string_dbg(&secrets_domain_sid
),
6402 sid_string_dbg(&ldap_domain_sid
)));
6404 /* reset secrets.tdb sid */
6405 secrets_store_domain_sid(ldap_state
->domain_name
,
6407 DEBUG(1, ("New global sam SID: %s\n",
6408 sid_string_dbg(get_global_sam_sid())));
6410 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6411 TALLOC_FREE(domain_sid_string
);
6414 alg_rid_base_string
= smbldap_talloc_single_attribute(
6415 ldap_state
->smbldap_state
->ldap_struct
,
6417 get_attr_key2string( dominfo_attr_list
,
6418 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6420 if (alg_rid_base_string
) {
6421 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
6422 if (alg_rid_base
!= algorithmic_rid_base()) {
6423 DEBUG(0, ("The value of 'algorithmic RID base' has "
6424 "changed since the LDAP\n"
6425 "database was initialised. Aborting. \n"));
6426 ldap_msgfree(result
);
6427 TALLOC_FREE(alg_rid_base_string
);
6428 return NT_STATUS_UNSUCCESSFUL
;
6430 TALLOC_FREE(alg_rid_base_string
);
6432 ldap_msgfree(result
);
6434 return NT_STATUS_OK
;
6437 NTSTATUS
pdb_ldap_init(void)
6440 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6443 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6446 /* Let pdb_nds register backends */
6449 return NT_STATUS_OK
;