2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
47 #include "../libcli/auth/libcli_auth.h"
49 #include "idmap_cache.h"
50 #include "../libcli/security/security.h"
53 #define DBGC_CLASS DBGC_PASSDB
61 /**********************************************************************
62 Simple helper function to make stuff better readable
63 **********************************************************************/
65 LDAP
*priv2ld(struct ldapsam_privates
*priv
)
67 return priv
->smbldap_state
->ldap_struct
;
70 /**********************************************************************
71 Get the attribute name given a user schame version.
72 **********************************************************************/
74 static const char* get_userattr_key2string( int schema_ver
, int key
)
76 switch ( schema_ver
) {
77 case SCHEMAVER_SAMBAACCOUNT
:
78 return get_attr_key2string( attrib_map_v22
, key
);
80 case SCHEMAVER_SAMBASAMACCOUNT
:
81 return get_attr_key2string( attrib_map_v30
, key
);
84 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
90 /**********************************************************************
91 Return the list of attribute names given a user schema version.
92 **********************************************************************/
94 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
96 switch ( schema_ver
) {
97 case SCHEMAVER_SAMBAACCOUNT
:
98 return get_attr_list( mem_ctx
, attrib_map_v22
);
100 case SCHEMAVER_SAMBASAMACCOUNT
:
101 return get_attr_list( mem_ctx
, attrib_map_v30
);
103 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
110 /**************************************************************************
111 Return the list of attribute names to delete given a user schema version.
112 **************************************************************************/
114 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
117 switch ( schema_ver
) {
118 case SCHEMAVER_SAMBAACCOUNT
:
119 return get_attr_list( mem_ctx
,
120 attrib_map_to_delete_v22
);
122 case SCHEMAVER_SAMBASAMACCOUNT
:
123 return get_attr_list( mem_ctx
,
124 attrib_map_to_delete_v30
);
126 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
134 /*******************************************************************
135 Generate the LDAP search filter for the objectclass based on the
136 version of the schema we are using.
137 ******************************************************************/
139 static const char* get_objclass_filter( int schema_ver
)
141 fstring objclass_filter
;
144 switch( schema_ver
) {
145 case SCHEMAVER_SAMBAACCOUNT
:
146 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
148 case SCHEMAVER_SAMBASAMACCOUNT
:
149 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
152 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
153 objclass_filter
[0] = '\0';
157 result
= talloc_strdup(talloc_tos(), objclass_filter
);
158 SMB_ASSERT(result
!= NULL
);
162 /*****************************************************************
163 Scan a sequence number off OpenLDAP's syncrepl contextCSN
164 ******************************************************************/
166 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
168 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
169 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
170 LDAPMessage
*msg
= NULL
;
171 LDAPMessage
*entry
= NULL
;
173 char **values
= NULL
;
174 int rc
, num_result
, num_values
, rid
;
180 /* Unfortunatly there is no proper way to detect syncrepl-support in
181 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
182 * but do not show up in the root-DSE yet. Neither we can query the
183 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
184 * objectclass. Currently we require lp_ldap_suffix() to show up as
185 * namingContext. - Guenther
188 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
193 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
197 if (!smbldap_has_naming_context(ldap_state
->smbldap_state
->ldap_struct
, lp_ldap_suffix())) {
198 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
199 "as top-level namingContext\n", lp_ldap_suffix()));
203 mem_ctx
= talloc_init("ldapsam_get_seq_num");
206 return NT_STATUS_NO_MEMORY
;
208 if ((attrs
= TALLOC_ARRAY(mem_ctx
, const char *, 2)) == NULL
) {
209 ntstatus
= NT_STATUS_NO_MEMORY
;
213 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
214 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
217 /* consumer syncreplCookie: */
218 /* csn=20050126161620Z#0000001#00#00000 */
219 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
221 suffix
= talloc_asprintf(mem_ctx
,
222 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
224 ntstatus
= NT_STATUS_NO_MEMORY
;
229 /* provider contextCSN */
230 /* 20050126161620Z#000009#00#000000 */
231 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
233 suffix
= talloc_asprintf(mem_ctx
,
234 "cn=ldapsync,%s", lp_ldap_suffix());
237 ntstatus
= NT_STATUS_NO_MEMORY
;
242 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
243 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
245 if (rc
!= LDAP_SUCCESS
) {
249 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
);
250 if (num_result
!= 1) {
251 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
255 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
);
257 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
261 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
, entry
, attrs
[0]);
262 if (values
== NULL
) {
263 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
267 num_values
= ldap_count_values(values
);
268 if (num_values
== 0) {
269 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
274 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
275 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
280 if (!strncmp(p
, "csn=", strlen("csn=")))
283 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
285 *seq_num
= generalized_to_unix_time(p
);
287 /* very basic sanity check */
289 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
294 ntstatus
= NT_STATUS_OK
;
298 ldap_value_free(values
);
302 talloc_destroy(mem_ctx
);
307 /*******************************************************************
308 Run the search by name.
309 ******************************************************************/
311 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
313 LDAPMessage
** result
,
317 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
321 return LDAP_NO_MEMORY
;
325 * in the filter expression, replace %u with the real name
326 * so in ldap filter, %u MUST exist :-)
328 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
329 get_objclass_filter(ldap_state
->schema_ver
));
331 TALLOC_FREE(escape_user
);
332 return LDAP_NO_MEMORY
;
335 * have to use this here because $ is filtered out
339 filter
= talloc_all_string_sub(talloc_tos(),
340 filter
, "%u", escape_user
);
341 TALLOC_FREE(escape_user
);
343 return LDAP_NO_MEMORY
;
346 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
347 filter
, attr
, result
);
352 /*******************************************************************
353 Run the search by rid.
354 ******************************************************************/
356 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
357 uint32_t rid
, LDAPMessage
** result
,
363 filter
= talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid
,
364 get_objclass_filter(ldap_state
->schema_ver
));
366 return LDAP_NO_MEMORY
;
369 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
370 filter
, attr
, result
);
375 /*******************************************************************
376 Run the search by SID.
377 ******************************************************************/
379 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
380 const struct dom_sid
*sid
, LDAPMessage
** result
,
387 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
388 get_userattr_key2string(ldap_state
->schema_ver
,
390 sid_to_fstring(sid_string
, sid
),
391 get_objclass_filter(ldap_state
->schema_ver
));
393 return LDAP_NO_MEMORY
;
396 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
397 filter
, attr
, result
);
403 /*******************************************************************
404 Delete complete object or objectclass and attrs from
405 object found in search_result depending on lp_ldap_delete_dn
406 ******************************************************************/
408 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
411 const char *objectclass
,
414 LDAPMod
**mods
= NULL
;
417 BerElement
*ptr
= NULL
;
419 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
421 return LDAP_NO_MEMORY
;
424 if (lp_ldap_delete_dn()) {
425 return smbldap_delete(priv
->smbldap_state
, dn
);
428 /* Ok, delete only the SAM attributes */
430 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
432 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
435 /* We are only allowed to delete the attributes that
438 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
439 if (strequal(*attrib
, name
)) {
440 DEBUG(10, ("ldapsam_delete_entry: deleting "
441 "attribute %s\n", name
));
442 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
453 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
454 talloc_autofree_ldapmod(mem_ctx
, mods
);
456 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
459 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
464 temp
= smbldap_talloc_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
465 get_userattr_key2string(ldap_state
->schema_ver
,LDAP_ATTR_MOD_TIMESTAMP
),
471 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
472 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
482 /**********************************************************************
483 Initialize struct samu from an LDAP query.
484 (Based on init_sam_from_buffer in pdb_tdb.c)
485 *********************************************************************/
487 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
488 struct samu
* sampass
,
495 pass_can_change_time
,
496 pass_must_change_time
,
499 char *username
= NULL
,
505 *logon_script
= NULL
,
506 *profile_path
= NULL
,
508 *workstations
= NULL
,
511 uint8 smblmpwd
[LM_HASH_LEN
],
512 smbntpwd
[NT_HASH_LEN
];
513 bool use_samba_attrs
= True
;
514 uint32_t acct_ctrl
= 0;
516 uint16_t bad_password_count
= 0,
519 uint8 hours
[MAX_HOURS_LEN
];
521 struct login_cache cache_entry
;
523 bool expand_explicit
= lp_passdb_expand_explicit();
525 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
530 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
531 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
535 if (priv2ld(ldap_state
) == NULL
) {
536 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
537 "ldap_struct is NULL!\n"));
541 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
545 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
550 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
552 nt_username
= talloc_strdup(ctx
, username
);
557 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
562 pdb_set_username(sampass
, username
, PDB_SET
);
564 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
565 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
567 /* deal with different attributes between the schema first */
569 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
570 if ((temp
= smbldap_talloc_single_attribute(
571 ldap_state
->smbldap_state
->ldap_struct
,
573 get_userattr_key2string(ldap_state
->schema_ver
,
576 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
579 if ((temp
= smbldap_talloc_single_attribute(
580 ldap_state
->smbldap_state
->ldap_struct
,
582 get_userattr_key2string(ldap_state
->schema_ver
,
585 user_rid
= (uint32_t)atol(temp
);
586 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
590 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
591 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
592 get_userattr_key2string(ldap_state
->schema_ver
,
594 get_userattr_key2string(ldap_state
->schema_ver
,
600 temp
= smbldap_talloc_single_attribute(
601 ldap_state
->smbldap_state
->ldap_struct
,
603 get_userattr_key2string(ldap_state
->schema_ver
,
604 LDAP_ATTR_PWD_LAST_SET
),
607 pass_last_set_time
= (time_t) atol(temp
);
608 pdb_set_pass_last_set_time(sampass
,
609 pass_last_set_time
, PDB_SET
);
612 temp
= smbldap_talloc_single_attribute(
613 ldap_state
->smbldap_state
->ldap_struct
,
615 get_userattr_key2string(ldap_state
->schema_ver
,
616 LDAP_ATTR_LOGON_TIME
),
619 logon_time
= (time_t) atol(temp
);
620 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
623 temp
= smbldap_talloc_single_attribute(
624 ldap_state
->smbldap_state
->ldap_struct
,
626 get_userattr_key2string(ldap_state
->schema_ver
,
627 LDAP_ATTR_LOGOFF_TIME
),
630 logoff_time
= (time_t) atol(temp
);
631 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
634 temp
= smbldap_talloc_single_attribute(
635 ldap_state
->smbldap_state
->ldap_struct
,
637 get_userattr_key2string(ldap_state
->schema_ver
,
638 LDAP_ATTR_KICKOFF_TIME
),
641 kickoff_time
= (time_t) atol(temp
);
642 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
645 temp
= smbldap_talloc_single_attribute(
646 ldap_state
->smbldap_state
->ldap_struct
,
648 get_userattr_key2string(ldap_state
->schema_ver
,
649 LDAP_ATTR_PWD_CAN_CHANGE
),
652 pass_can_change_time
= (time_t) atol(temp
);
653 pdb_set_pass_can_change_time(sampass
,
654 pass_can_change_time
, PDB_SET
);
657 temp
= smbldap_talloc_single_attribute(
658 ldap_state
->smbldap_state
->ldap_struct
,
660 get_userattr_key2string(ldap_state
->schema_ver
,
661 LDAP_ATTR_PWD_MUST_CHANGE
),
664 pass_must_change_time
= (time_t) atol(temp
);
665 pdb_set_pass_must_change_time(sampass
,
666 pass_must_change_time
, PDB_SET
);
669 /* recommend that 'gecos' and 'displayName' should refer to the same
670 * attribute OID. userFullName depreciated, only used by Samba
671 * primary rules of LDAP: don't make a new attribute when one is already defined
672 * that fits your needs; using cn then displayName rather than 'userFullName'
675 fullname
= smbldap_talloc_single_attribute(
676 ldap_state
->smbldap_state
->ldap_struct
,
678 get_userattr_key2string(ldap_state
->schema_ver
,
679 LDAP_ATTR_DISPLAY_NAME
),
682 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
684 fullname
= smbldap_talloc_single_attribute(
685 ldap_state
->smbldap_state
->ldap_struct
,
687 get_userattr_key2string(ldap_state
->schema_ver
,
691 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
695 dir_drive
= smbldap_talloc_single_attribute(
696 ldap_state
->smbldap_state
->ldap_struct
,
698 get_userattr_key2string(ldap_state
->schema_ver
,
699 LDAP_ATTR_HOME_DRIVE
),
702 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
704 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
707 homedir
= smbldap_talloc_single_attribute(
708 ldap_state
->smbldap_state
->ldap_struct
,
710 get_userattr_key2string(ldap_state
->schema_ver
,
711 LDAP_ATTR_HOME_PATH
),
714 if (expand_explicit
) {
715 homedir
= talloc_sub_basic(ctx
,
723 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
725 pdb_set_homedir(sampass
,
726 talloc_sub_basic(ctx
, username
, domain
,
731 logon_script
= smbldap_talloc_single_attribute(
732 ldap_state
->smbldap_state
->ldap_struct
,
734 get_userattr_key2string(ldap_state
->schema_ver
,
735 LDAP_ATTR_LOGON_SCRIPT
),
738 if (expand_explicit
) {
739 logon_script
= talloc_sub_basic(ctx
,
747 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
749 pdb_set_logon_script(sampass
,
750 talloc_sub_basic(ctx
, username
, domain
,
755 profile_path
= smbldap_talloc_single_attribute(
756 ldap_state
->smbldap_state
->ldap_struct
,
758 get_userattr_key2string(ldap_state
->schema_ver
,
759 LDAP_ATTR_PROFILE_PATH
),
762 if (expand_explicit
) {
763 profile_path
= talloc_sub_basic(ctx
,
771 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
773 pdb_set_profile_path(sampass
,
774 talloc_sub_basic(ctx
, username
, domain
,
779 acct_desc
= smbldap_talloc_single_attribute(
780 ldap_state
->smbldap_state
->ldap_struct
,
782 get_userattr_key2string(ldap_state
->schema_ver
,
786 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
789 workstations
= smbldap_talloc_single_attribute(
790 ldap_state
->smbldap_state
->ldap_struct
,
792 get_userattr_key2string(ldap_state
->schema_ver
,
796 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
799 munged_dial
= smbldap_talloc_single_attribute(
800 ldap_state
->smbldap_state
->ldap_struct
,
802 get_userattr_key2string(ldap_state
->schema_ver
,
803 LDAP_ATTR_MUNGED_DIAL
),
806 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
809 /* FIXME: hours stuff should be cleaner */
813 memset(hours
, 0xff, hours_len
);
815 if (ldap_state
->is_nds_ldap
) {
818 char clear_text_pw
[512];
820 /* Make call to Novell eDirectory ldap extension to get clear text password.
821 NOTE: This will only work if we have an SSL connection to eDirectory. */
822 user_dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
823 if (user_dn
!= NULL
) {
824 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
826 pwd_len
= sizeof(clear_text_pw
);
827 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
828 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
829 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
830 TALLOC_FREE(user_dn
);
833 ZERO_STRUCT(smblmpwd
);
834 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
835 TALLOC_FREE(user_dn
);
838 ZERO_STRUCT(smbntpwd
);
839 use_samba_attrs
= False
;
842 TALLOC_FREE(user_dn
);
845 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
849 if (use_samba_attrs
) {
850 temp
= smbldap_talloc_single_attribute(
851 ldap_state
->smbldap_state
->ldap_struct
,
853 get_userattr_key2string(ldap_state
->schema_ver
,
857 pdb_gethexpwd(temp
, smblmpwd
);
858 memset((char *)temp
, '\0', strlen(temp
)+1);
859 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
862 ZERO_STRUCT(smblmpwd
);
865 temp
= smbldap_talloc_single_attribute(
866 ldap_state
->smbldap_state
->ldap_struct
,
868 get_userattr_key2string(ldap_state
->schema_ver
,
872 pdb_gethexpwd(temp
, smbntpwd
);
873 memset((char *)temp
, '\0', strlen(temp
)+1);
874 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
877 ZERO_STRUCT(smbntpwd
);
883 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
885 uint8
*pwhist
= NULL
;
887 char *history_string
= TALLOC_ARRAY(ctx
, char,
888 MAX_PW_HISTORY_LEN
*64);
890 if (!history_string
) {
894 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
896 pwhist
= TALLOC_ARRAY(ctx
, uint8
,
897 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
898 if (pwhist
== NULL
) {
899 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
902 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
904 if (smbldap_get_single_attribute(
905 ldap_state
->smbldap_state
->ldap_struct
,
907 get_userattr_key2string(ldap_state
->schema_ver
,
908 LDAP_ATTR_PWD_HISTORY
),
910 MAX_PW_HISTORY_LEN
*64)) {
911 bool hex_failed
= false;
912 for (i
= 0; i
< pwHistLen
; i
++){
913 /* Get the 16 byte salt. */
914 if (!pdb_gethexpwd(&history_string
[i
*64],
915 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
919 /* Get the 16 byte MD5 hash of salt+passwd. */
920 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
921 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
922 PW_HISTORY_SALT_LEN
])) {
928 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
930 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
933 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
938 temp
= smbldap_talloc_single_attribute(
939 ldap_state
->smbldap_state
->ldap_struct
,
941 get_userattr_key2string(ldap_state
->schema_ver
,
945 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
947 if (acct_ctrl
== 0) {
948 acct_ctrl
|= ACB_NORMAL
;
951 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
953 acct_ctrl
|= ACB_NORMAL
;
956 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
957 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
959 temp
= smbldap_talloc_single_attribute(
960 ldap_state
->smbldap_state
->ldap_struct
,
962 get_userattr_key2string(ldap_state
->schema_ver
,
963 LDAP_ATTR_BAD_PASSWORD_COUNT
),
966 bad_password_count
= (uint32_t) atol(temp
);
967 pdb_set_bad_password_count(sampass
,
968 bad_password_count
, PDB_SET
);
971 temp
= smbldap_talloc_single_attribute(
972 ldap_state
->smbldap_state
->ldap_struct
,
974 get_userattr_key2string(ldap_state
->schema_ver
,
975 LDAP_ATTR_BAD_PASSWORD_TIME
),
978 bad_password_time
= (time_t) atol(temp
);
979 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
983 temp
= smbldap_talloc_single_attribute(
984 ldap_state
->smbldap_state
->ldap_struct
,
986 get_userattr_key2string(ldap_state
->schema_ver
,
987 LDAP_ATTR_LOGON_COUNT
),
990 logon_count
= (uint32_t) atol(temp
);
991 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
994 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
996 temp
= smbldap_talloc_single_attribute(
997 ldap_state
->smbldap_state
->ldap_struct
,
999 get_userattr_key2string(ldap_state
->schema_ver
,
1000 LDAP_ATTR_LOGON_HOURS
),
1003 pdb_gethexhours(temp
, hours
);
1004 memset((char *)temp
, '\0', strlen(temp
) +1);
1005 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
1009 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
1010 struct passwd unix_pw
;
1011 bool have_uid
= false;
1012 bool have_gid
= false;
1013 struct dom_sid mapped_gsid
;
1014 const struct dom_sid
*primary_gsid
;
1016 ZERO_STRUCT(unix_pw
);
1018 unix_pw
.pw_name
= username
;
1019 unix_pw
.pw_passwd
= discard_const_p(char, "x");
1021 temp
= smbldap_talloc_single_attribute(
1022 priv2ld(ldap_state
),
1027 /* We've got a uid, feed the cache */
1028 unix_pw
.pw_uid
= strtoul(temp
, NULL
, 10);
1031 temp
= smbldap_talloc_single_attribute(
1032 priv2ld(ldap_state
),
1037 /* We've got a uid, feed the cache */
1038 unix_pw
.pw_gid
= strtoul(temp
, NULL
, 10);
1041 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1042 priv2ld(ldap_state
),
1046 if (unix_pw
.pw_gecos
) {
1047 unix_pw
.pw_gecos
= fullname
;
1049 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1050 priv2ld(ldap_state
),
1054 if (unix_pw
.pw_dir
) {
1055 unix_pw
.pw_dir
= discard_const_p(char, "");
1057 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1058 priv2ld(ldap_state
),
1062 if (unix_pw
.pw_shell
) {
1063 unix_pw
.pw_shell
= discard_const_p(char, "");
1066 if (have_uid
&& have_gid
) {
1067 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1069 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1072 if (sampass
->unix_pw
== NULL
) {
1073 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1074 pdb_get_username(sampass
)));
1078 store_uid_sid_cache(pdb_get_user_sid(sampass
),
1079 sampass
->unix_pw
->pw_uid
);
1080 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass
),
1081 sampass
->unix_pw
->pw_uid
);
1083 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1084 primary_gsid
= pdb_get_group_sid(sampass
);
1085 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1086 store_gid_sid_cache(primary_gsid
,
1087 sampass
->unix_pw
->pw_gid
);
1088 idmap_cache_set_sid2gid(primary_gsid
,
1089 sampass
->unix_pw
->pw_gid
);
1093 /* check the timestamp of the cache vs ldap entry */
1094 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1100 /* see if we have newer updates */
1101 if (!login_cache_read(sampass
, &cache_entry
)) {
1102 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1103 (unsigned int)pdb_get_bad_password_count(sampass
),
1104 (unsigned int)pdb_get_bad_password_time(sampass
)));
1109 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1110 (unsigned int)ldap_entry_time
,
1111 (unsigned int)cache_entry
.entry_timestamp
,
1112 (unsigned int)cache_entry
.bad_password_time
));
1114 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1115 /* cache is older than directory , so
1116 we need to delete the entry but allow the
1117 fields to be written out */
1118 login_cache_delentry(sampass
);
1121 pdb_set_acct_ctrl(sampass
,
1122 pdb_get_acct_ctrl(sampass
) |
1123 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1125 pdb_set_bad_password_count(sampass
,
1126 cache_entry
.bad_password_count
,
1128 pdb_set_bad_password_time(sampass
,
1129 cache_entry
.bad_password_time
,
1141 /**********************************************************************
1142 Initialize the ldap db from a struct samu. Called on update.
1143 (Based on init_buffer_from_sam in pdb_tdb.c)
1144 *********************************************************************/
1146 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1147 LDAPMessage
*existing
,
1148 LDAPMod
*** mods
, struct samu
* sampass
,
1149 bool (*need_update
)(const struct samu
*,
1155 if (mods
== NULL
|| sampass
== NULL
) {
1156 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1163 * took out adding "objectclass: sambaAccount"
1164 * do this on a per-mod basis
1166 if (need_update(sampass
, PDB_USERNAME
)) {
1167 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1168 "uid", pdb_get_username(sampass
));
1169 if (ldap_state
->is_nds_ldap
) {
1170 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1171 "cn", pdb_get_username(sampass
));
1172 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1173 "sn", pdb_get_username(sampass
));
1177 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1179 /* only update the RID if we actually need to */
1180 if (need_update(sampass
, PDB_USERSID
)) {
1182 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1184 switch ( ldap_state
->schema_ver
) {
1185 case SCHEMAVER_SAMBAACCOUNT
:
1186 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
1187 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1188 sid_string_dbg(user_sid
),
1190 &ldap_state
->domain_sid
)));
1193 if (asprintf(&temp
, "%i", rid
) < 0) {
1196 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1197 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
1202 case SCHEMAVER_SAMBASAMACCOUNT
:
1203 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1204 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1205 sid_to_fstring(sid_string
, user_sid
));
1209 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1214 /* we don't need to store the primary group RID - so leaving it
1215 'free' to hang off the unix primary group makes life easier */
1217 if (need_update(sampass
, PDB_GROUPSID
)) {
1219 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1221 switch ( ldap_state
->schema_ver
) {
1222 case SCHEMAVER_SAMBAACCOUNT
:
1223 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
1224 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1225 sid_string_dbg(group_sid
),
1227 &ldap_state
->domain_sid
)));
1231 if (asprintf(&temp
, "%i", rid
) < 0) {
1234 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1235 get_userattr_key2string(ldap_state
->schema_ver
,
1236 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
1240 case SCHEMAVER_SAMBASAMACCOUNT
:
1241 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1242 get_userattr_key2string(ldap_state
->schema_ver
,
1243 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_fstring(sid_string
, group_sid
));
1247 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1253 /* displayName, cn, and gecos should all be the same
1254 * most easily accomplished by giving them the same OID
1255 * gecos isn't set here b/c it should be handled by the
1257 * We change displayName only and fall back to cn if
1258 * it does not exist.
1261 if (need_update(sampass
, PDB_FULLNAME
))
1262 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1263 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1264 pdb_get_fullname(sampass
));
1266 if (need_update(sampass
, PDB_ACCTDESC
))
1267 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1268 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1269 pdb_get_acct_desc(sampass
));
1271 if (need_update(sampass
, PDB_WORKSTATIONS
))
1272 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1273 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1274 pdb_get_workstations(sampass
));
1276 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1277 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1278 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1279 pdb_get_munged_dial(sampass
));
1281 if (need_update(sampass
, PDB_SMBHOME
))
1282 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1283 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1284 pdb_get_homedir(sampass
));
1286 if (need_update(sampass
, PDB_DRIVE
))
1287 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1288 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1289 pdb_get_dir_drive(sampass
));
1291 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1292 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1293 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1294 pdb_get_logon_script(sampass
));
1296 if (need_update(sampass
, PDB_PROFILE
))
1297 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1298 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1299 pdb_get_profile_path(sampass
));
1301 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1304 if (need_update(sampass
, PDB_LOGONTIME
))
1305 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1306 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1309 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1312 if (need_update(sampass
, PDB_LOGOFFTIME
))
1313 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1314 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1317 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1320 if (need_update(sampass
, PDB_KICKOFFTIME
))
1321 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1322 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1325 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1328 if (need_update(sampass
, PDB_CANCHANGETIME
))
1329 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1330 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1333 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_must_change_time(sampass
)) < 0) {
1336 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
1337 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1338 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
1341 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1342 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1344 if (need_update(sampass
, PDB_LMPASSWD
)) {
1345 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1348 pdb_sethexpwd(pwstr
, lm_pw
,
1349 pdb_get_acct_ctrl(sampass
));
1350 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1351 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1354 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1355 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1359 if (need_update(sampass
, PDB_NTPASSWD
)) {
1360 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1363 pdb_sethexpwd(pwstr
, nt_pw
,
1364 pdb_get_acct_ctrl(sampass
));
1365 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1366 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1369 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1370 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1375 if (need_update(sampass
, PDB_PWHISTORY
)) {
1377 uint32_t pwHistLen
= 0;
1378 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1380 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1384 if (pwHistLen
== 0) {
1385 /* Remove any password history from the LDAP store. */
1386 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1390 uint32_t currHistLen
= 0;
1391 const uint8
*pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1392 if (pwhist
!= NULL
) {
1393 /* We can only store (1024-1/64 password history entries. */
1394 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1395 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1396 /* Store the salt. */
1397 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1398 /* Followed by the md5 hash of salt + md4 hash */
1399 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1400 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1401 DEBUG(100, ("pwstr=%s\n", pwstr
));
1405 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1406 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1411 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1412 if (asprintf(&temp
, "%li",
1413 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1416 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1417 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1423 if (need_update(sampass
, PDB_HOURS
)) {
1424 const uint8
*hours
= pdb_get_hours(sampass
);
1427 pdb_sethexhours(hourstr
, hours
);
1428 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
,
1431 get_userattr_key2string(ldap_state
->schema_ver
,
1432 LDAP_ATTR_LOGON_HOURS
),
1437 if (need_update(sampass
, PDB_ACCTCTRL
))
1438 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
1439 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1440 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1442 /* password lockout cache:
1443 - If we are now autolocking or clearing, we write to ldap
1444 - If we are clearing, we delete the cache entry
1445 - If the count is > 0, we update the cache
1447 This even means when autolocking, we cache, just in case the
1448 update doesn't work, and we have to cache the autolock flag */
1450 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1451 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1452 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1453 time_t badtime
= pdb_get_bad_password_time(sampass
);
1455 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1457 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1458 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1460 if ((badcount
>= pol
) || (badcount
== 0)) {
1461 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1462 (unsigned int)badcount
, (unsigned int)badtime
));
1463 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1467 ldap_state
->smbldap_state
->ldap_struct
,
1469 get_userattr_key2string(
1470 ldap_state
->schema_ver
,
1471 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1475 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1479 ldap_state
->smbldap_state
->ldap_struct
,
1481 get_userattr_key2string(
1482 ldap_state
->schema_ver
,
1483 LDAP_ATTR_BAD_PASSWORD_TIME
),
1487 if (badcount
== 0) {
1488 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1489 login_cache_delentry(sampass
);
1491 struct login_cache cache_entry
;
1493 cache_entry
.entry_timestamp
= time(NULL
);
1494 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1495 cache_entry
.bad_password_count
= badcount
;
1496 cache_entry
.bad_password_time
= badtime
;
1498 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1499 login_cache_write(sampass
, &cache_entry
);
1506 /**********************************************************************
1507 End enumeration of the LDAP password list.
1508 *********************************************************************/
1510 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1512 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1513 if (ldap_state
->result
) {
1514 ldap_msgfree(ldap_state
->result
);
1515 ldap_state
->result
= NULL
;
1519 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1520 const char *new_attr
)
1524 if (new_attr
== NULL
) {
1528 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1532 (*attr_list
) = TALLOC_REALLOC_ARRAY(mem_ctx
, (*attr_list
),
1534 SMB_ASSERT((*attr_list
) != NULL
);
1535 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1536 (*attr_list
)[i
+1] = NULL
;
1539 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1540 const char ***attr_list
)
1542 append_attr(mem_ctx
, attr_list
, "uidNumber");
1543 append_attr(mem_ctx
, attr_list
, "gidNumber");
1544 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1545 append_attr(mem_ctx
, attr_list
, "loginShell");
1546 append_attr(mem_ctx
, attr_list
, "gecos");
1549 /**********************************************************************
1550 Get struct samu entry from LDAP by username.
1551 *********************************************************************/
1553 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1555 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1556 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1557 LDAPMessage
*result
= NULL
;
1558 LDAPMessage
*entry
= NULL
;
1560 const char ** attr_list
;
1563 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1564 append_attr(user
, &attr_list
,
1565 get_userattr_key2string(ldap_state
->schema_ver
,
1566 LDAP_ATTR_MOD_TIMESTAMP
));
1567 ldapsam_add_unix_attributes(user
, &attr_list
);
1568 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1570 TALLOC_FREE( attr_list
);
1572 if ( rc
!= LDAP_SUCCESS
)
1573 return NT_STATUS_NO_SUCH_USER
;
1575 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1578 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1579 ldap_msgfree(result
);
1580 return NT_STATUS_NO_SUCH_USER
;
1581 } else if (count
> 1) {
1582 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1583 ldap_msgfree(result
);
1584 return NT_STATUS_NO_SUCH_USER
;
1587 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1589 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1590 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1591 ldap_msgfree(result
);
1592 return NT_STATUS_NO_SUCH_USER
;
1594 pdb_set_backend_private_data(user
, result
, NULL
,
1595 my_methods
, PDB_CHANGED
);
1596 talloc_autofree_ldapmsg(user
, result
);
1599 ldap_msgfree(result
);
1604 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1605 const struct dom_sid
*sid
, LDAPMessage
**result
)
1608 const char ** attr_list
;
1611 switch ( ldap_state
->schema_ver
) {
1612 case SCHEMAVER_SAMBASAMACCOUNT
: {
1613 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1614 if (tmp_ctx
== NULL
) {
1615 return LDAP_NO_MEMORY
;
1618 attr_list
= get_userattr_list(tmp_ctx
,
1619 ldap_state
->schema_ver
);
1620 append_attr(tmp_ctx
, &attr_list
,
1621 get_userattr_key2string(
1622 ldap_state
->schema_ver
,
1623 LDAP_ATTR_MOD_TIMESTAMP
));
1624 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1625 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1627 TALLOC_FREE(tmp_ctx
);
1629 if ( rc
!= LDAP_SUCCESS
)
1634 case SCHEMAVER_SAMBAACCOUNT
:
1635 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1639 attr_list
= get_userattr_list(NULL
,
1640 ldap_state
->schema_ver
);
1641 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1642 TALLOC_FREE( attr_list
);
1644 if ( rc
!= LDAP_SUCCESS
)
1651 /**********************************************************************
1652 Get struct samu entry from LDAP by SID.
1653 *********************************************************************/
1655 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1657 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1658 LDAPMessage
*result
= NULL
;
1659 LDAPMessage
*entry
= NULL
;
1663 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1665 if (rc
!= LDAP_SUCCESS
)
1666 return NT_STATUS_NO_SUCH_USER
;
1668 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1671 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1672 "count=%d\n", sid_string_dbg(sid
), count
));
1673 ldap_msgfree(result
);
1674 return NT_STATUS_NO_SUCH_USER
;
1675 } else if (count
> 1) {
1676 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1677 "[%s]. Failing. count=%d\n", sid_string_dbg(sid
),
1679 ldap_msgfree(result
);
1680 return NT_STATUS_NO_SUCH_USER
;
1683 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1685 ldap_msgfree(result
);
1686 return NT_STATUS_NO_SUCH_USER
;
1689 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1690 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1691 ldap_msgfree(result
);
1692 return NT_STATUS_NO_SUCH_USER
;
1695 pdb_set_backend_private_data(user
, result
, NULL
,
1696 my_methods
, PDB_CHANGED
);
1697 talloc_autofree_ldapmsg(user
, result
);
1698 return NT_STATUS_OK
;
1701 /********************************************************************
1702 Do the actual modification - also change a plaintext passord if
1704 **********************************************************************/
1706 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1707 struct samu
*newpwd
, char *dn
,
1708 LDAPMod
**mods
, int ldap_op
,
1709 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1711 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1714 if (!newpwd
|| !dn
) {
1715 return NT_STATUS_INVALID_PARAMETER
;
1718 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1719 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1720 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1721 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1724 char *retoid
= NULL
;
1725 struct berval
*retdata
= NULL
;
1726 char *utf8_password
;
1728 size_t converted_size
;
1731 if (!ldap_state
->is_nds_ldap
) {
1733 if (!smbldap_has_extension(ldap_state
->smbldap_state
->ldap_struct
,
1734 LDAP_EXOP_MODIFY_PASSWD
)) {
1735 DEBUG(2, ("ldap password change requested, but LDAP "
1736 "server does not support it -- ignoring\n"));
1737 return NT_STATUS_OK
;
1741 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1742 pdb_get_plaintext_passwd(newpwd
),
1745 return NT_STATUS_NO_MEMORY
;
1748 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1749 TALLOC_FREE(utf8_password
);
1750 return NT_STATUS_NO_MEMORY
;
1753 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1754 DEBUG(0,("ber_alloc_t returns NULL\n"));
1755 TALLOC_FREE(utf8_password
);
1756 TALLOC_FREE(utf8_dn
);
1757 return NT_STATUS_UNSUCCESSFUL
;
1760 if ((ber_printf (ber
, "{") < 0) ||
1761 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1763 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1766 TALLOC_FREE(utf8_dn
);
1767 TALLOC_FREE(utf8_password
);
1768 return NT_STATUS_UNSUCCESSFUL
;
1771 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1772 ret
= ber_printf(ber
, "ts}",
1773 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1776 ret
= ber_printf(ber
, "}");
1780 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1783 TALLOC_FREE(utf8_dn
);
1784 TALLOC_FREE(utf8_password
);
1785 return NT_STATUS_UNSUCCESSFUL
;
1788 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1789 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1791 TALLOC_FREE(utf8_dn
);
1792 TALLOC_FREE(utf8_password
);
1793 return NT_STATUS_UNSUCCESSFUL
;
1796 TALLOC_FREE(utf8_dn
);
1797 TALLOC_FREE(utf8_password
);
1800 if (!ldap_state
->is_nds_ldap
) {
1801 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1802 LDAP_EXOP_MODIFY_PASSWD
,
1803 bv
, NULL
, NULL
, &retoid
,
1806 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1807 pdb_get_plaintext_passwd(newpwd
));
1809 if (rc
!= LDAP_SUCCESS
) {
1810 char *ld_error
= NULL
;
1812 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1813 DEBUG(3, ("Could not set userPassword "
1814 "attribute due to an objectClass "
1815 "violation -- ignoring\n"));
1817 return NT_STATUS_OK
;
1820 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1822 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1823 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1824 SAFE_FREE(ld_error
);
1826 #if defined(LDAP_CONSTRAINT_VIOLATION)
1827 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1828 return NT_STATUS_PASSWORD_RESTRICTION
;
1830 return NT_STATUS_UNSUCCESSFUL
;
1832 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1833 #ifdef DEBUG_PASSWORD
1834 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1837 ber_bvfree(retdata
);
1839 ldap_memfree(retoid
);
1845 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1846 /* may be password change below however */
1850 if (ldap_state
->is_nds_ldap
) {
1851 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1855 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1859 rc
= smbldap_add(ldap_state
->smbldap_state
,
1862 case LDAP_MOD_REPLACE
:
1863 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1867 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1869 return NT_STATUS_INVALID_PARAMETER
;
1872 if (rc
!=LDAP_SUCCESS
) {
1873 return NT_STATUS_UNSUCCESSFUL
;
1877 return NT_STATUS_OK
;
1880 /**********************************************************************
1881 Delete entry from LDAP for username.
1882 *********************************************************************/
1884 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1885 struct samu
* sam_acct
)
1887 struct ldapsam_privates
*priv
=
1888 (struct ldapsam_privates
*)my_methods
->private_data
;
1891 LDAPMessage
*msg
, *entry
;
1892 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1893 const char **attr_list
;
1894 TALLOC_CTX
*mem_ctx
;
1897 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1898 return NT_STATUS_INVALID_PARAMETER
;
1901 sname
= pdb_get_username(sam_acct
);
1903 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1906 mem_ctx
= talloc_new(NULL
);
1907 if (mem_ctx
== NULL
) {
1908 DEBUG(0, ("talloc_new failed\n"));
1912 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1913 if (attr_list
== NULL
) {
1917 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1919 if ((rc
!= LDAP_SUCCESS
) ||
1920 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1921 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1922 DEBUG(5, ("Could not find user %s\n", sname
));
1923 result
= NT_STATUS_NO_SUCH_USER
;
1927 rc
= ldapsam_delete_entry(
1928 priv
, mem_ctx
, entry
,
1929 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1930 LDAP_OBJ_SAMBASAMACCOUNT
: LDAP_OBJ_SAMBAACCOUNT
,
1933 result
= (rc
== LDAP_SUCCESS
) ?
1934 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1937 TALLOC_FREE(mem_ctx
);
1941 /**********************************************************************
1942 Helper function to determine for update_sam_account whether
1943 we need LDAP modification.
1944 *********************************************************************/
1946 static bool element_is_changed(const struct samu
*sampass
,
1947 enum pdb_elements element
)
1949 return IS_SAM_CHANGED(sampass
, element
);
1952 /**********************************************************************
1954 *********************************************************************/
1956 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1958 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1959 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1962 LDAPMessage
*result
= NULL
;
1963 LDAPMessage
*entry
= NULL
;
1964 LDAPMod
**mods
= NULL
;
1965 const char **attr_list
;
1967 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1969 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1970 if (pdb_get_username(newpwd
) == NULL
) {
1971 return NT_STATUS_INVALID_PARAMETER
;
1973 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1974 TALLOC_FREE( attr_list
);
1975 if (rc
!= LDAP_SUCCESS
) {
1976 return NT_STATUS_UNSUCCESSFUL
;
1978 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1979 my_methods
, PDB_CHANGED
);
1980 talloc_autofree_ldapmsg(newpwd
, result
);
1983 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1984 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1985 return NT_STATUS_UNSUCCESSFUL
;
1988 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1989 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
1991 return NT_STATUS_UNSUCCESSFUL
;
1994 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1996 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1997 element_is_changed
)) {
1998 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2001 ldap_mods_free(mods
,True
);
2002 return NT_STATUS_UNSUCCESSFUL
;
2005 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
2006 && (mods
== NULL
)) {
2007 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2008 pdb_get_username(newpwd
)));
2010 return NT_STATUS_OK
;
2013 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
2016 ldap_mods_free(mods
,True
);
2022 * We need to set the backend private data to NULL here. For example
2023 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2024 * same one, and with the explicit delete / add logic for attribute
2025 * values the second time we would use the wrong "old" value which
2026 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2028 * The existing LDAPMessage is still being auto-freed by the
2031 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2034 if (!NT_STATUS_IS_OK(ret
)) {
2038 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2039 pdb_get_username(newpwd
)));
2040 return NT_STATUS_OK
;
2043 /***************************************************************************
2044 Renames a struct samu
2045 - The "rename user script" has full responsibility for changing everything
2046 ***************************************************************************/
2048 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2049 TALLOC_CTX
*tmp_ctx
,
2051 uint32_t member_rid
);
2053 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2054 TALLOC_CTX
*mem_ctx
,
2056 struct dom_sid
**pp_sids
,
2058 uint32_t *p_num_groups
);
2060 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2061 struct samu
*old_acct
,
2062 const char *newname
)
2064 const char *oldname
;
2066 char *rename_script
= NULL
;
2067 fstring oldname_lower
, newname_lower
;
2070 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2071 return NT_STATUS_INVALID_PARAMETER
;
2074 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2075 return NT_STATUS_INVALID_PARAMETER
;
2078 oldname
= pdb_get_username(old_acct
);
2080 /* rename the posix user */
2081 rename_script
= SMB_STRDUP(lp_renameuser_script());
2082 if (rename_script
== NULL
) {
2083 return NT_STATUS_NO_MEMORY
;
2086 if (!(*rename_script
)) {
2087 SAFE_FREE(rename_script
);
2088 return NT_STATUS_ACCESS_DENIED
;
2091 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2094 /* We have to allow the account name to end with a '$'.
2095 Also, follow the semantics in _samr_create_user() and lower case the
2096 posix name but preserve the case in passdb */
2098 fstrcpy( oldname_lower
, oldname
);
2099 strlower_m( oldname_lower
);
2100 fstrcpy( newname_lower
, newname
);
2101 strlower_m( newname_lower
);
2102 rename_script
= realloc_string_sub2(rename_script
,
2107 if (!rename_script
) {
2108 return NT_STATUS_NO_MEMORY
;
2110 rename_script
= realloc_string_sub2(rename_script
,
2115 rc
= smbrun(rename_script
, NULL
);
2117 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2118 rename_script
, rc
));
2120 SAFE_FREE(rename_script
);
2123 smb_nscd_flush_user_cache();
2127 return NT_STATUS_UNSUCCESSFUL
;
2129 return NT_STATUS_OK
;
2132 /**********************************************************************
2133 Helper function to determine for update_sam_account whether
2134 we need LDAP modification.
2135 *********************************************************************/
2137 static bool element_is_set_or_changed(const struct samu
*sampass
,
2138 enum pdb_elements element
)
2140 return (IS_SAM_SET(sampass
, element
) ||
2141 IS_SAM_CHANGED(sampass
, element
));
2144 /**********************************************************************
2145 Add struct samu to LDAP.
2146 *********************************************************************/
2148 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2150 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2151 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2153 LDAPMessage
*result
= NULL
;
2154 LDAPMessage
*entry
= NULL
;
2155 LDAPMod
**mods
= NULL
;
2156 int ldap_op
= LDAP_MOD_REPLACE
;
2157 uint32_t num_result
;
2158 const char **attr_list
;
2159 char *escape_user
= NULL
;
2160 const char *username
= pdb_get_username(newpwd
);
2161 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2162 char *filter
= NULL
;
2164 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2165 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2168 return NT_STATUS_NO_MEMORY
;
2171 if (!username
|| !*username
) {
2172 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2173 status
= NT_STATUS_INVALID_PARAMETER
;
2177 /* free this list after the second search or in case we exit on failure */
2178 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2180 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2182 if (rc
!= LDAP_SUCCESS
) {
2186 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2187 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2191 ldap_msgfree(result
);
2194 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2195 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2197 if (rc
== LDAP_SUCCESS
) {
2198 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
2199 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2200 "already in the base, with samba "
2201 "attributes\n", sid_string_dbg(sid
)));
2204 ldap_msgfree(result
);
2209 /* does the entry already exist but without a samba attributes?
2210 we need to return the samba attributes here */
2212 escape_user
= escape_ldap_string(talloc_tos(), username
);
2213 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2215 status
= NT_STATUS_NO_MEMORY
;
2218 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2219 TALLOC_FREE(escape_user
);
2221 status
= NT_STATUS_NO_MEMORY
;
2225 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2226 filter
, attr_list
, &result
);
2227 if ( rc
!= LDAP_SUCCESS
) {
2231 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2233 if (num_result
> 1) {
2234 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2238 /* Check if we need to update an existing entry */
2239 if (num_result
== 1) {
2240 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2241 ldap_op
= LDAP_MOD_REPLACE
;
2242 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2243 dn
= smbldap_talloc_dn(ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2245 status
= NT_STATUS_NO_MEMORY
;
2249 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2251 /* There might be a SID for this account already - say an idmap entry */
2253 filter
= talloc_asprintf(ctx
,
2254 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2255 get_userattr_key2string(ldap_state
->schema_ver
,
2256 LDAP_ATTR_USER_SID
),
2257 sid_string_talloc(ctx
, sid
),
2258 LDAP_OBJ_IDMAP_ENTRY
,
2259 LDAP_OBJ_SID_ENTRY
);
2261 status
= NT_STATUS_NO_MEMORY
;
2265 /* free old result before doing a new search */
2266 if (result
!= NULL
) {
2267 ldap_msgfree(result
);
2270 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2271 filter
, attr_list
, &result
);
2273 if ( rc
!= LDAP_SUCCESS
) {
2277 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
2279 if (num_result
> 1) {
2280 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2284 /* Check if we need to update an existing entry */
2285 if (num_result
== 1) {
2287 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2288 ldap_op
= LDAP_MOD_REPLACE
;
2289 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
2290 dn
= smbldap_talloc_dn (ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
2292 status
= NT_STATUS_NO_MEMORY
;
2298 if (num_result
== 0) {
2299 char *escape_username
;
2300 /* Check if we need to add an entry */
2301 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2302 ldap_op
= LDAP_MOD_ADD
;
2304 escape_username
= escape_rdn_val_string_alloc(username
);
2305 if (!escape_username
) {
2306 status
= NT_STATUS_NO_MEMORY
;
2310 if (username
[strlen(username
)-1] == '$') {
2311 dn
= talloc_asprintf(ctx
,
2314 lp_ldap_machine_suffix());
2316 dn
= talloc_asprintf(ctx
,
2319 lp_ldap_user_suffix());
2322 SAFE_FREE(escape_username
);
2324 status
= NT_STATUS_NO_MEMORY
;
2329 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2330 element_is_set_or_changed
)) {
2331 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2333 ldap_mods_free(mods
, true);
2339 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2342 switch ( ldap_state
->schema_ver
) {
2343 case SCHEMAVER_SAMBAACCOUNT
:
2344 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
2346 case SCHEMAVER_SAMBASAMACCOUNT
:
2347 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2350 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2354 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
2355 if (!NT_STATUS_IS_OK(ret
)) {
2356 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2357 pdb_get_username(newpwd
),dn
));
2358 ldap_mods_free(mods
, true);
2362 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2363 ldap_mods_free(mods
, true);
2365 status
= NT_STATUS_OK
;
2371 ldap_msgfree(result
);
2376 /**********************************************************************
2377 *********************************************************************/
2379 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2381 LDAPMessage
** result
)
2383 int scope
= LDAP_SCOPE_SUBTREE
;
2385 const char **attr_list
;
2387 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2388 rc
= smbldap_search(ldap_state
->smbldap_state
,
2389 lp_ldap_suffix (), scope
,
2390 filter
, attr_list
, 0, result
);
2391 TALLOC_FREE(attr_list
);
2396 /**********************************************************************
2397 *********************************************************************/
2399 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2400 GROUP_MAP
*map
, LDAPMessage
*entry
)
2403 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2405 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2406 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
2407 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2412 temp
= smbldap_talloc_single_attribute(
2413 ldap_state
->smbldap_state
->ldap_struct
,
2415 get_attr_key2string(groupmap_attr_list
,
2416 LDAP_ATTR_GIDNUMBER
),
2419 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2420 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2424 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2426 map
->gid
= (gid_t
)atol(temp
);
2429 temp
= smbldap_talloc_single_attribute(
2430 ldap_state
->smbldap_state
->ldap_struct
,
2432 get_attr_key2string(groupmap_attr_list
,
2433 LDAP_ATTR_GROUP_SID
),
2436 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2437 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2442 if (!string_to_sid(&map
->sid
, temp
)) {
2443 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2449 temp
= smbldap_talloc_single_attribute(
2450 ldap_state
->smbldap_state
->ldap_struct
,
2452 get_attr_key2string(groupmap_attr_list
,
2453 LDAP_ATTR_GROUP_TYPE
),
2456 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2457 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2461 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2463 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2464 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2465 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2471 temp
= smbldap_talloc_single_attribute(
2472 ldap_state
->smbldap_state
->ldap_struct
,
2474 get_attr_key2string(groupmap_attr_list
,
2475 LDAP_ATTR_DISPLAY_NAME
),
2478 temp
= smbldap_talloc_single_attribute(
2479 ldap_state
->smbldap_state
->ldap_struct
,
2481 get_attr_key2string(groupmap_attr_list
,
2485 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2486 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2491 fstrcpy(map
->nt_name
, temp
);
2494 temp
= smbldap_talloc_single_attribute(
2495 ldap_state
->smbldap_state
->ldap_struct
,
2497 get_attr_key2string(groupmap_attr_list
,
2501 temp
= talloc_strdup(ctx
, "");
2507 fstrcpy(map
->comment
, temp
);
2509 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2510 store_gid_sid_cache(&map
->sid
, map
->gid
);
2511 idmap_cache_set_sid2gid(&map
->sid
, map
->gid
);
2518 /**********************************************************************
2519 *********************************************************************/
2521 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2525 struct ldapsam_privates
*ldap_state
=
2526 (struct ldapsam_privates
*)methods
->private_data
;
2527 LDAPMessage
*result
= NULL
;
2528 LDAPMessage
*entry
= NULL
;
2531 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2533 return NT_STATUS_NO_SUCH_GROUP
;
2536 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2539 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2541 ldap_msgfree(result
);
2542 return NT_STATUS_NO_SUCH_GROUP
;
2546 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2547 "count=%d\n", filter
, count
));
2548 ldap_msgfree(result
);
2549 return NT_STATUS_NO_SUCH_GROUP
;
2552 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2555 ldap_msgfree(result
);
2556 return NT_STATUS_UNSUCCESSFUL
;
2559 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2560 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2561 "group filter %s\n", filter
));
2562 ldap_msgfree(result
);
2563 return NT_STATUS_NO_SUCH_GROUP
;
2566 ldap_msgfree(result
);
2567 return NT_STATUS_OK
;
2570 /**********************************************************************
2571 *********************************************************************/
2573 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2576 char *filter
= NULL
;
2580 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2582 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2583 sid_to_fstring(tmp
, &sid
)) < 0) {
2584 return NT_STATUS_NO_MEMORY
;
2587 status
= ldapsam_getgroup(methods
, filter
, map
);
2592 /**********************************************************************
2593 *********************************************************************/
2595 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2598 char *filter
= NULL
;
2601 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2603 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2604 (unsigned long)gid
) < 0) {
2605 return NT_STATUS_NO_MEMORY
;
2608 status
= ldapsam_getgroup(methods
, filter
, map
);
2613 /**********************************************************************
2614 *********************************************************************/
2616 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2619 char *filter
= NULL
;
2620 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2624 return NT_STATUS_NO_MEMORY
;
2627 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2629 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2630 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2632 TALLOC_FREE(escape_name
);
2633 return NT_STATUS_NO_MEMORY
;
2636 TALLOC_FREE(escape_name
);
2637 status
= ldapsam_getgroup(methods
, filter
, map
);
2642 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2644 const struct dom_sid
*domain_sid
,
2650 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2651 str
, sizeof(str
)-1)) {
2652 DEBUG(10, ("Could not find sambaSID attribute\n"));
2656 if (!string_to_sid(&sid
, str
)) {
2657 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2661 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2662 DEBUG(10, ("SID %s is not in expected domain %s\n",
2663 str
, sid_string_dbg(domain_sid
)));
2667 if (!sid_peek_rid(&sid
, rid
)) {
2668 DEBUG(10, ("Could not peek into RID\n"));
2675 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2676 TALLOC_CTX
*mem_ctx
,
2677 const struct dom_sid
*group
,
2678 uint32_t **pp_member_rids
,
2679 size_t *p_num_members
)
2681 struct ldapsam_privates
*ldap_state
=
2682 (struct ldapsam_privates
*)methods
->private_data
;
2683 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2684 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2685 const char *sid_attrs
[] = { "sambaSID", NULL
};
2686 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2687 LDAPMessage
*result
= NULL
;
2690 char **values
= NULL
;
2695 *pp_member_rids
= NULL
;
2698 filter
= talloc_asprintf(mem_ctx
,
2699 "(&(objectClass=%s)"
2702 LDAP_OBJ_POSIXGROUP
,
2704 sid_string_talloc(mem_ctx
, group
));
2705 if (filter
== NULL
) {
2706 ret
= NT_STATUS_NO_MEMORY
;
2710 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2711 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2714 if (rc
!= LDAP_SUCCESS
)
2717 talloc_autofree_ldapmsg(mem_ctx
, result
);
2719 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2722 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2723 sid_string_dbg(group
)));
2724 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2729 ret
= NT_STATUS_NO_SUCH_GROUP
;
2733 entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2737 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2739 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2740 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2744 values
= ldap_get_values(conn
->ldap_struct
, entry
, "memberUid");
2746 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2748 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2749 if (filter
== NULL
) {
2750 ret
= NT_STATUS_NO_MEMORY
;
2754 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2755 char *escape_memberuid
;
2757 escape_memberuid
= escape_ldap_string(talloc_tos(),
2759 if (escape_memberuid
== NULL
) {
2760 ret
= NT_STATUS_NO_MEMORY
;
2764 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2765 TALLOC_FREE(escape_memberuid
);
2766 if (filter
== NULL
) {
2767 ret
= NT_STATUS_NO_MEMORY
;
2772 filter
= talloc_asprintf_append_buffer(filter
, "))");
2773 if (filter
== NULL
) {
2774 ret
= NT_STATUS_NO_MEMORY
;
2778 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2779 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2782 if (rc
!= LDAP_SUCCESS
)
2785 count
= ldap_count_entries(conn
->ldap_struct
, result
);
2786 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2788 talloc_autofree_ldapmsg(mem_ctx
, result
);
2790 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2792 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2798 sidstr
= smbldap_talloc_single_attribute(conn
->ldap_struct
,
2802 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2803 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2804 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2808 if (!string_to_sid(&sid
, sidstr
))
2811 if (!sid_check_is_in_our_domain(&sid
)) {
2812 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2813 "in our domain\n"));
2814 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2818 sid_peek_rid(&sid
, &rid
);
2820 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2822 ret
= NT_STATUS_NO_MEMORY
;
2828 filter
= talloc_asprintf(mem_ctx
,
2829 "(&(objectClass=%s)"
2831 LDAP_OBJ_SAMBASAMACCOUNT
,
2834 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2835 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2838 if (rc
!= LDAP_SUCCESS
)
2841 talloc_autofree_ldapmsg(mem_ctx
, result
);
2843 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2845 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
2849 if (!ldapsam_extract_rid_from_entry(conn
->ldap_struct
,
2851 get_global_sam_sid(),
2853 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2854 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2858 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2860 ret
= NT_STATUS_NO_MEMORY
;
2870 ldap_value_free(values
);
2875 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2876 TALLOC_CTX
*mem_ctx
,
2878 struct dom_sid
**pp_sids
,
2880 uint32_t *p_num_groups
)
2882 struct ldapsam_privates
*ldap_state
=
2883 (struct ldapsam_privates
*)methods
->private_data
;
2884 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2886 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2889 LDAPMessage
*result
= NULL
;
2891 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2895 gid_t primary_gid
= -1;
2900 if (pdb_get_username(user
) == NULL
) {
2901 return NT_STATUS_INVALID_PARAMETER
;
2904 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2905 if (escape_name
== NULL
)
2906 return NT_STATUS_NO_MEMORY
;
2908 if (user
->unix_pw
) {
2909 primary_gid
= user
->unix_pw
->pw_gid
;
2911 /* retrieve the users primary gid */
2912 filter
= talloc_asprintf(mem_ctx
,
2913 "(&(objectClass=%s)(uid=%s))",
2914 LDAP_OBJ_SAMBASAMACCOUNT
,
2916 if (filter
== NULL
) {
2917 ret
= NT_STATUS_NO_MEMORY
;
2921 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2922 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2924 if (rc
!= LDAP_SUCCESS
)
2927 talloc_autofree_ldapmsg(mem_ctx
, result
);
2929 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2933 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2934 ret
= NT_STATUS_NO_SUCH_USER
;
2937 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2939 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2941 DEBUG (1, ("Unable to find the member's gid!\n"));
2942 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2945 primary_gid
= strtoul(gidstr
, NULL
, 10);
2948 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2949 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2954 filter
= talloc_asprintf(mem_ctx
,
2955 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2956 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2957 if (filter
== NULL
) {
2958 ret
= NT_STATUS_NO_MEMORY
;
2962 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2963 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2965 if (rc
!= LDAP_SUCCESS
)
2968 talloc_autofree_ldapmsg(mem_ctx
, result
);
2976 /* We need to add the primary group as the first gid/sid */
2978 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2979 ret
= NT_STATUS_NO_MEMORY
;
2983 /* This sid will be replaced later */
2985 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2987 if (!NT_STATUS_IS_OK(ret
)) {
2991 for (entry
= ldap_first_entry(conn
->ldap_struct
, result
);
2993 entry
= ldap_next_entry(conn
->ldap_struct
, entry
))
3000 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3002 str
, sizeof(str
)-1))
3005 if (!string_to_sid(&sid
, str
))
3008 if (!smbldap_get_single_attribute(conn
->ldap_struct
,
3010 str
, sizeof(str
)-1))
3013 gid
= strtoul(str
, &end
, 10);
3015 if (PTR_DIFF(end
, str
) != strlen(str
))
3018 if (gid
== primary_gid
) {
3019 sid_copy(&(*pp_sids
)[0], &sid
);
3021 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3023 ret
= NT_STATUS_NO_MEMORY
;
3026 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3028 if (!NT_STATUS_IS_OK(ret
)) {
3034 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3035 DEBUG(3, ("primary group of [%s] not found\n",
3036 pdb_get_username(user
)));
3040 *p_num_groups
= num_sids
;
3046 TALLOC_FREE(escape_name
);
3050 /**********************************************************************
3051 * Augment a posixGroup object with a sambaGroupMapping domgroup
3052 *********************************************************************/
3054 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3055 struct ldapsam_privates
*ldap_state
,
3058 const char *filter
, *dn
;
3059 LDAPMessage
*msg
, *entry
;
3063 filter
= talloc_asprintf(mem_ctx
,
3064 "(&(objectClass=%s)(gidNumber=%u))",
3065 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3066 if (filter
== NULL
) {
3067 return NT_STATUS_NO_MEMORY
;
3070 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3071 get_attr_list(mem_ctx
, groupmap_attr_list
),
3073 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3075 if ((rc
!= LDAP_SUCCESS
) ||
3076 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3077 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3078 return NT_STATUS_NO_SUCH_GROUP
;
3081 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3083 return NT_STATUS_NO_MEMORY
;
3087 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3089 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaSid",
3090 sid_string_talloc(mem_ctx
, &map
->sid
));
3091 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "sambaGroupType",
3092 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3093 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3095 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3097 talloc_autofree_ldapmod(mem_ctx
, mods
);
3099 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3100 if (rc
!= LDAP_SUCCESS
) {
3101 return NT_STATUS_ACCESS_DENIED
;
3104 return NT_STATUS_OK
;
3107 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3110 struct ldapsam_privates
*ldap_state
=
3111 (struct ldapsam_privates
*)methods
->private_data
;
3112 LDAPMessage
*msg
= NULL
;
3113 LDAPMod
**mods
= NULL
;
3114 const char *attrs
[] = { NULL
};
3118 TALLOC_CTX
*mem_ctx
;
3125 mem_ctx
= talloc_new(NULL
);
3126 if (mem_ctx
== NULL
) {
3127 DEBUG(0, ("talloc_new failed\n"));
3128 return NT_STATUS_NO_MEMORY
;
3131 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3132 sid_string_talloc(mem_ctx
, &map
->sid
));
3133 if (filter
== NULL
) {
3134 result
= NT_STATUS_NO_MEMORY
;
3138 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3139 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3140 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3142 if ((rc
== LDAP_SUCCESS
) &&
3143 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) > 0)) {
3145 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3146 "group mapping entry\n", sid_string_dbg(&map
->sid
)));
3147 result
= NT_STATUS_GROUP_EXISTS
;
3151 switch (map
->sid_name_use
) {
3153 case SID_NAME_DOM_GRP
:
3154 /* To map a domain group we need to have a posix group
3156 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3160 case SID_NAME_ALIAS
:
3161 if (!sid_check_is_in_our_domain(&map
->sid
)
3162 && !sid_check_is_in_builtin(&map
->sid
) )
3164 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3165 sid_string_dbg(&map
->sid
)));
3166 result
= NT_STATUS_INVALID_PARAMETER
;
3172 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3173 sid_type_lookup(map
->sid_name_use
)));
3174 result
= NT_STATUS_INVALID_PARAMETER
;
3178 /* Domain groups have been mapped in a separate routine, we have to
3179 * create an alias now */
3181 if (map
->gid
== -1) {
3182 DEBUG(10, ("Refusing to map gid==-1\n"));
3183 result
= NT_STATUS_INVALID_PARAMETER
;
3187 if (pdb_gid_to_sid(map
->gid
, &sid
)) {
3188 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3189 "add\n", (unsigned int)map
->gid
, sid_string_dbg(&sid
)));
3190 result
= NT_STATUS_GROUP_EXISTS
;
3194 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3195 * the best we can get out of LDAP. */
3197 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3198 sid_string_talloc(mem_ctx
, &map
->sid
),
3199 lp_ldap_group_suffix());
3201 result
= NT_STATUS_NO_MEMORY
;
3207 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3208 LDAP_OBJ_SID_ENTRY
);
3209 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "objectClass",
3211 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaSid",
3212 sid_string_talloc(mem_ctx
, &map
->sid
));
3213 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "sambaGroupType",
3214 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3215 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "displayName",
3217 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "description",
3219 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, NULL
, &mods
, "gidNumber",
3220 talloc_asprintf(mem_ctx
, "%u", (unsigned int)map
->gid
));
3221 talloc_autofree_ldapmod(mem_ctx
, mods
);
3223 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3225 result
= (rc
== LDAP_SUCCESS
) ?
3226 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3229 TALLOC_FREE(mem_ctx
);
3233 /**********************************************************************
3234 * Update a group mapping entry. We're quite strict about what can be changed:
3235 * Only the description and displayname may be changed. It simply does not
3236 * make any sense to change the SID, gid or the type in a mapping.
3237 *********************************************************************/
3239 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3242 struct ldapsam_privates
*ldap_state
=
3243 (struct ldapsam_privates
*)methods
->private_data
;
3245 const char *filter
, *dn
;
3246 LDAPMessage
*msg
= NULL
;
3247 LDAPMessage
*entry
= NULL
;
3248 LDAPMod
**mods
= NULL
;
3249 TALLOC_CTX
*mem_ctx
;
3252 mem_ctx
= talloc_new(NULL
);
3253 if (mem_ctx
== NULL
) {
3254 DEBUG(0, ("talloc_new failed\n"));
3255 return NT_STATUS_NO_MEMORY
;
3258 /* Make 100% sure that sid, gid and type are not changed by looking up
3259 * exactly the values we're given in LDAP. */
3261 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3262 "(sambaSid=%s)(gidNumber=%u)"
3263 "(sambaGroupType=%d))",
3265 sid_string_talloc(mem_ctx
, &map
->sid
),
3266 (unsigned int)map
->gid
, map
->sid_name_use
);
3267 if (filter
== NULL
) {
3268 result
= NT_STATUS_NO_MEMORY
;
3272 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3273 get_attr_list(mem_ctx
, groupmap_attr_list
),
3275 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3277 if ((rc
!= LDAP_SUCCESS
) ||
3278 (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, msg
) != 1) ||
3279 ((entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, msg
)) == NULL
)) {
3280 result
= NT_STATUS_NO_SUCH_GROUP
;
3284 dn
= smbldap_talloc_dn(mem_ctx
, ldap_state
->smbldap_state
->ldap_struct
, entry
);
3287 result
= NT_STATUS_NO_MEMORY
;
3292 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "displayName",
3294 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, entry
, &mods
, "description",
3296 talloc_autofree_ldapmod(mem_ctx
, mods
);
3299 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3300 "nothing to do\n"));
3301 result
= NT_STATUS_OK
;
3305 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3307 if (rc
!= LDAP_SUCCESS
) {
3308 result
= NT_STATUS_ACCESS_DENIED
;
3312 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3313 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3315 result
= NT_STATUS_OK
;
3318 TALLOC_FREE(mem_ctx
);
3322 /**********************************************************************
3323 *********************************************************************/
3325 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3328 struct ldapsam_privates
*priv
=
3329 (struct ldapsam_privates
*)methods
->private_data
;
3330 LDAPMessage
*msg
, *entry
;
3333 TALLOC_CTX
*mem_ctx
;
3336 mem_ctx
= talloc_new(NULL
);
3337 if (mem_ctx
== NULL
) {
3338 DEBUG(0, ("talloc_new failed\n"));
3339 return NT_STATUS_NO_MEMORY
;
3342 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3343 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3344 sid_string_talloc(mem_ctx
, &sid
));
3345 if (filter
== NULL
) {
3346 result
= NT_STATUS_NO_MEMORY
;
3349 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3350 get_attr_list(mem_ctx
, groupmap_attr_list
),
3352 talloc_autofree_ldapmsg(mem_ctx
, msg
);
3354 if ((rc
!= LDAP_SUCCESS
) ||
3355 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3356 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3357 result
= NT_STATUS_NO_SUCH_GROUP
;
3361 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3362 get_attr_list(mem_ctx
,
3363 groupmap_attr_list_to_delete
));
3365 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3366 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3367 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3368 const char *attrs
[] = { "sambaGroupType", "description",
3369 "displayName", "sambaSIDList",
3372 /* Second try. Don't delete the sambaSID attribute, this is
3373 for "old" entries that are tacked on a winbind
3376 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3377 LDAP_OBJ_GROUPMAP
, attrs
);
3380 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3381 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3382 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3383 const char *attrs
[] = { "sambaGroupType", "description",
3384 "displayName", "sambaSIDList",
3385 "gidNumber", NULL
};
3387 /* Third try. This is a post-3.0.21 alias (containing only
3388 * sambaSidEntry and sambaGroupMapping classes), we also have
3389 * to delete the gidNumber attribute, only the sambaSidEntry
3392 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3393 LDAP_OBJ_GROUPMAP
, attrs
);
3396 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3399 TALLOC_FREE(mem_ctx
);
3403 /**********************************************************************
3404 *********************************************************************/
3406 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3409 struct ldapsam_privates
*ldap_state
=
3410 (struct ldapsam_privates
*)my_methods
->private_data
;
3411 char *filter
= NULL
;
3413 const char **attr_list
;
3415 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3417 return NT_STATUS_NO_MEMORY
;
3419 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3420 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3421 LDAP_SCOPE_SUBTREE
, filter
,
3422 attr_list
, 0, &ldap_state
->result
);
3423 TALLOC_FREE(attr_list
);
3425 if (rc
!= LDAP_SUCCESS
) {
3426 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3427 ldap_err2string(rc
)));
3428 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3429 lp_ldap_suffix(), filter
));
3430 ldap_msgfree(ldap_state
->result
);
3431 ldap_state
->result
= NULL
;
3432 TALLOC_FREE(filter
);
3433 return NT_STATUS_UNSUCCESSFUL
;
3436 TALLOC_FREE(filter
);
3438 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3439 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3440 ldap_state
->result
)));
3443 ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3444 ldap_state
->result
);
3445 ldap_state
->index
= 0;
3447 return NT_STATUS_OK
;
3450 /**********************************************************************
3451 *********************************************************************/
3453 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3455 ldapsam_endsampwent(my_methods
);
3458 /**********************************************************************
3459 *********************************************************************/
3461 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3464 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3465 struct ldapsam_privates
*ldap_state
=
3466 (struct ldapsam_privates
*)my_methods
->private_data
;
3470 if (!ldap_state
->entry
)
3473 ldap_state
->index
++;
3474 bret
= init_group_from_ldap(ldap_state
, map
,
3478 ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
3482 return NT_STATUS_OK
;
3485 /**********************************************************************
3486 *********************************************************************/
3488 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3489 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3490 GROUP_MAP
**pp_rmap
,
3491 size_t *p_num_entries
,
3500 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3501 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3503 return NT_STATUS_ACCESS_DENIED
;
3506 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
3507 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3508 sid_name_use
!= map
.sid_name_use
) {
3509 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3510 "not of the requested type\n", map
.nt_name
));
3513 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
3514 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3515 "non mapped\n", map
.nt_name
));
3519 (*pp_rmap
)=SMB_REALLOC_ARRAY((*pp_rmap
), GROUP_MAP
, entries
+1);
3521 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3522 "enlarge group map!\n"));
3523 return NT_STATUS_UNSUCCESSFUL
;
3526 (*pp_rmap
)[entries
] = map
;
3531 ldapsam_endsamgrent(methods
);
3533 *p_num_entries
= entries
;
3535 return NT_STATUS_OK
;
3538 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3539 const struct dom_sid
*alias
,
3540 const struct dom_sid
*member
,
3543 struct ldapsam_privates
*ldap_state
=
3544 (struct ldapsam_privates
*)methods
->private_data
;
3546 LDAPMessage
*result
= NULL
;
3547 LDAPMessage
*entry
= NULL
;
3549 LDAPMod
**mods
= NULL
;
3551 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3554 char *filter
= NULL
;
3556 if (sid_check_is_in_builtin(alias
)) {
3557 type
= SID_NAME_ALIAS
;
3560 if (sid_check_is_in_our_domain(alias
)) {
3561 type
= SID_NAME_ALIAS
;
3564 if (type
== SID_NAME_USE_NONE
) {
3565 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3566 sid_string_dbg(alias
)));
3567 return NT_STATUS_NO_SUCH_ALIAS
;
3570 if (asprintf(&filter
,
3571 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3572 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3574 return NT_STATUS_NO_MEMORY
;
3577 if (ldapsam_search_one_group(ldap_state
, filter
,
3578 &result
) != LDAP_SUCCESS
) {
3580 return NT_STATUS_NO_SUCH_ALIAS
;
3583 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3587 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3588 ldap_msgfree(result
);
3590 return NT_STATUS_NO_SUCH_ALIAS
;
3594 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3595 "filter %s: count=%d\n", filter
, count
));
3596 ldap_msgfree(result
);
3598 return NT_STATUS_NO_SUCH_ALIAS
;
3603 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3607 ldap_msgfree(result
);
3608 return NT_STATUS_UNSUCCESSFUL
;
3611 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
3613 ldap_msgfree(result
);
3614 return NT_STATUS_UNSUCCESSFUL
;
3617 smbldap_set_mod(&mods
, modop
,
3618 get_attr_key2string(groupmap_attr_list
,
3619 LDAP_ATTR_SID_LIST
),
3620 sid_to_fstring(tmp
, member
));
3622 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3624 ldap_mods_free(mods
, True
);
3625 ldap_msgfree(result
);
3628 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3629 return NT_STATUS_MEMBER_IN_ALIAS
;
3632 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3633 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3636 if (rc
!= LDAP_SUCCESS
) {
3637 return NT_STATUS_UNSUCCESSFUL
;
3640 return NT_STATUS_OK
;
3643 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3644 const struct dom_sid
*alias
,
3645 const struct dom_sid
*member
)
3647 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3650 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3651 const struct dom_sid
*alias
,
3652 const struct dom_sid
*member
)
3654 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3658 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3659 const struct dom_sid
*alias
,
3660 TALLOC_CTX
*mem_ctx
,
3661 struct dom_sid
**pp_members
,
3662 size_t *p_num_members
)
3664 struct ldapsam_privates
*ldap_state
=
3665 (struct ldapsam_privates
*)methods
->private_data
;
3666 LDAPMessage
*result
= NULL
;
3667 LDAPMessage
*entry
= NULL
;
3669 char **values
= NULL
;
3671 char *filter
= NULL
;
3672 uint32_t num_members
= 0;
3673 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3679 if (sid_check_is_in_builtin(alias
)) {
3680 type
= SID_NAME_ALIAS
;
3683 if (sid_check_is_in_our_domain(alias
)) {
3684 type
= SID_NAME_ALIAS
;
3687 if (type
== SID_NAME_USE_NONE
) {
3688 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3689 sid_string_dbg(alias
)));
3690 return NT_STATUS_NO_SUCH_ALIAS
;
3693 if (asprintf(&filter
,
3694 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3695 LDAP_OBJ_GROUPMAP
, sid_to_fstring(tmp
, alias
),
3697 return NT_STATUS_NO_MEMORY
;
3700 if (ldapsam_search_one_group(ldap_state
, filter
,
3701 &result
) != LDAP_SUCCESS
) {
3703 return NT_STATUS_NO_SUCH_ALIAS
;
3706 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
3710 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3711 ldap_msgfree(result
);
3713 return NT_STATUS_NO_SUCH_ALIAS
;
3717 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3718 "filter %s: count=%d\n", filter
, count
));
3719 ldap_msgfree(result
);
3721 return NT_STATUS_NO_SUCH_ALIAS
;
3726 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
3730 ldap_msgfree(result
);
3731 return NT_STATUS_UNSUCCESSFUL
;
3734 values
= ldap_get_values(ldap_state
->smbldap_state
->ldap_struct
,
3736 get_attr_key2string(groupmap_attr_list
,
3737 LDAP_ATTR_SID_LIST
));
3739 if (values
== NULL
) {
3740 ldap_msgfree(result
);
3741 return NT_STATUS_OK
;
3744 count
= ldap_count_values(values
);
3746 for (i
=0; i
<count
; i
++) {
3747 struct dom_sid member
;
3750 if (!string_to_sid(&member
, values
[i
]))
3753 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3755 if (!NT_STATUS_IS_OK(status
)) {
3756 ldap_value_free(values
);
3757 ldap_msgfree(result
);
3762 *p_num_members
= num_members
;
3763 ldap_value_free(values
);
3764 ldap_msgfree(result
);
3766 return NT_STATUS_OK
;
3769 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3770 TALLOC_CTX
*mem_ctx
,
3771 const struct dom_sid
*domain_sid
,
3772 const struct dom_sid
*members
,
3774 uint32_t **pp_alias_rids
,
3775 size_t *p_num_alias_rids
)
3777 struct ldapsam_privates
*ldap_state
=
3778 (struct ldapsam_privates
*)methods
->private_data
;
3781 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3783 LDAPMessage
*result
= NULL
;
3784 LDAPMessage
*entry
= NULL
;
3788 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3789 bool is_builtin
= false;
3790 bool sid_added
= false;
3792 *pp_alias_rids
= NULL
;
3793 *p_num_alias_rids
= 0;
3795 if (sid_check_is_builtin(domain_sid
)) {
3797 type
= SID_NAME_ALIAS
;
3800 if (sid_check_is_domain(domain_sid
)) {
3801 type
= SID_NAME_ALIAS
;
3804 if (type
== SID_NAME_USE_NONE
) {
3805 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3806 sid_string_dbg(domain_sid
)));
3807 return NT_STATUS_UNSUCCESSFUL
;
3810 if (num_members
== 0) {
3811 return NT_STATUS_OK
;
3814 filter
= talloc_asprintf(mem_ctx
,
3815 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3816 LDAP_OBJ_GROUPMAP
, type
);
3818 for (i
=0; i
<num_members
; i
++)
3819 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3821 sid_string_talloc(mem_ctx
,
3824 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3826 if (filter
== NULL
) {
3827 return NT_STATUS_NO_MEMORY
;
3831 ldap_state
->search_cache
.filter
&&
3832 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3833 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3834 result
= ldap_state
->search_cache
.result
;
3835 ldap_state
->search_cache
.result
= NULL
;
3837 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3838 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3839 if (rc
!= LDAP_SUCCESS
) {
3840 return NT_STATUS_UNSUCCESSFUL
;
3842 talloc_autofree_ldapmsg(filter
, result
);
3845 ldap_struct
= ldap_state
->smbldap_state
->ldap_struct
;
3847 for (entry
= ldap_first_entry(ldap_struct
, result
);
3849 entry
= ldap_next_entry(ldap_struct
, entry
))
3855 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3861 if (!string_to_sid(&sid
, sid_str
))
3864 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3869 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3870 p_num_alias_rids
)) {
3871 return NT_STATUS_NO_MEMORY
;
3875 if (!is_builtin
&& !sid_added
) {
3876 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3878 * Note: result is a talloc child of filter because of the
3879 * talloc_autofree_ldapmsg() usage
3881 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3882 ldap_state
->search_cache
.result
= result
;
3885 return NT_STATUS_OK
;
3888 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3889 enum pdb_policy_type type
,
3892 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3894 LDAPMod
**mods
= NULL
;
3895 fstring value_string
;
3896 const char *policy_attr
= NULL
;
3898 struct ldapsam_privates
*ldap_state
=
3899 (struct ldapsam_privates
*)methods
->private_data
;
3901 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3903 if (!ldap_state
->domain_dn
) {
3904 return NT_STATUS_INVALID_PARAMETER
;
3907 policy_attr
= get_account_policy_attr(type
);
3908 if (policy_attr
== NULL
) {
3909 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3914 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3916 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3918 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3921 ldap_mods_free(mods
, True
);
3923 if (rc
!= LDAP_SUCCESS
) {
3927 if (!cache_account_policy_set(type
, value
)) {
3928 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3929 "update local tdb cache\n"));
3933 return NT_STATUS_OK
;
3936 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3937 enum pdb_policy_type type
,
3940 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3944 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3945 enum pdb_policy_type type
,
3948 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3949 LDAPMessage
*result
= NULL
;
3950 LDAPMessage
*entry
= NULL
;
3955 const char *policy_attr
= NULL
;
3957 struct ldapsam_privates
*ldap_state
=
3958 (struct ldapsam_privates
*)methods
->private_data
;
3960 const char *attrs
[2];
3962 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3964 if (!ldap_state
->domain_dn
) {
3965 return NT_STATUS_INVALID_PARAMETER
;
3968 policy_attr
= get_account_policy_attr(type
);
3970 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3971 "policy index: %d\n", type
));
3975 attrs
[0] = policy_attr
;
3978 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
3979 if (filter
== NULL
) {
3980 return NT_STATUS_NO_MEMORY
;
3982 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3983 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
3985 TALLOC_FREE(filter
);
3986 if (rc
!= LDAP_SUCCESS
) {
3990 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
3995 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
3996 if (entry
== NULL
) {
4000 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4005 *value
= (uint32_t)atol(vals
[0]);
4007 ntstatus
= NT_STATUS_OK
;
4011 ldap_value_free(vals
);
4012 ldap_msgfree(result
);
4017 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4019 - if user hasn't decided to use account policies inside LDAP just reuse the
4022 - if there is a valid cache entry, return that
4023 - if there is an LDAP entry, update cache and return
4024 - otherwise set to default, update cache and return
4028 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4029 enum pdb_policy_type type
,
4032 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4034 if (cache_account_policy_get(type
, value
)) {
4035 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4037 return NT_STATUS_OK
;
4040 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4042 if (NT_STATUS_IS_OK(ntstatus
)) {
4046 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4050 /* should we automagically migrate old tdb value here ? */
4051 if (account_policy_get(type
, value
))
4054 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4055 "default\n", type
));
4058 if (!account_policy_get_default(type
, value
)) {
4064 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4065 if (!NT_STATUS_IS_OK(ntstatus
)) {
4071 if (!cache_account_policy_set(type
, *value
)) {
4072 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4073 "tdb as a cache\n"));
4074 return NT_STATUS_UNSUCCESSFUL
;
4077 return NT_STATUS_OK
;
4080 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4081 const struct dom_sid
*domain_sid
,
4085 enum lsa_SidType
*attrs
)
4087 struct ldapsam_privates
*ldap_state
=
4088 (struct ldapsam_privates
*)methods
->private_data
;
4089 LDAPMessage
*msg
= NULL
;
4091 char *allsids
= NULL
;
4092 int i
, rc
, num_mapped
;
4093 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4094 TALLOC_CTX
*mem_ctx
;
4098 mem_ctx
= talloc_new(NULL
);
4099 if (mem_ctx
== NULL
) {
4100 DEBUG(0, ("talloc_new failed\n"));
4104 if (!sid_check_is_builtin(domain_sid
) &&
4105 !sid_check_is_domain(domain_sid
)) {
4106 result
= NT_STATUS_INVALID_PARAMETER
;
4110 if (num_rids
== 0) {
4111 result
= NT_STATUS_NONE_MAPPED
;
4115 for (i
=0; i
<num_rids
; i
++)
4116 attrs
[i
] = SID_NAME_UNKNOWN
;
4118 allsids
= talloc_strdup(mem_ctx
, "");
4119 if (allsids
== NULL
) {
4123 for (i
=0; i
<num_rids
; i
++) {
4125 sid_compose(&sid
, domain_sid
, rids
[i
]);
4126 allsids
= talloc_asprintf_append_buffer(
4127 allsids
, "(sambaSid=%s)",
4128 sid_string_talloc(mem_ctx
, &sid
));
4129 if (allsids
== NULL
) {
4134 /* First look for users */
4138 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4140 filter
= talloc_asprintf(
4141 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4142 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4144 if (filter
== NULL
) {
4148 rc
= smbldap_search(ldap_state
->smbldap_state
,
4149 lp_ldap_user_suffix(),
4150 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4152 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4155 if (rc
!= LDAP_SUCCESS
)
4158 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4161 for (entry
= ldap_first_entry(ld
, msg
);
4163 entry
= ldap_next_entry(ld
, entry
)) {
4168 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4170 DEBUG(2, ("Could not find sid from ldap entry\n"));
4174 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4177 DEBUG(2, ("Could not retrieve uid attribute\n"));
4181 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4182 if (rid
== rids
[rid_index
])
4186 if (rid_index
== num_rids
) {
4187 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4191 attrs
[rid_index
] = SID_NAME_USER
;
4192 names
[rid_index
] = name
;
4196 if (num_mapped
== num_rids
) {
4197 /* No need to look for groups anymore -- we're done */
4198 result
= NT_STATUS_OK
;
4202 /* Same game for groups */
4206 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4207 "sambaGroupType", NULL
};
4209 filter
= talloc_asprintf(
4210 mem_ctx
, "(&(objectClass=%s)(|%s))",
4211 LDAP_OBJ_GROUPMAP
, allsids
);
4212 if (filter
== NULL
) {
4216 rc
= smbldap_search(ldap_state
->smbldap_state
,
4218 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4220 talloc_autofree_ldapmsg(mem_ctx
, msg
);
4223 if (rc
!= LDAP_SUCCESS
)
4226 /* ldap_struct might have changed due to a reconnect */
4228 ld
= ldap_state
->smbldap_state
->ldap_struct
;
4230 /* For consistency checks, we already checked we're only domain or builtin */
4232 is_builtin
= sid_check_is_builtin(domain_sid
);
4234 for (entry
= ldap_first_entry(ld
, msg
);
4236 entry
= ldap_next_entry(ld
, entry
))
4241 enum lsa_SidType type
;
4242 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4244 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4247 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4252 type
= (enum lsa_SidType
)atol(attr
);
4254 /* Consistency checks */
4255 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4256 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4257 (type
!= SID_NAME_DOM_GRP
)))) {
4258 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4261 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4263 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4267 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4270 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4272 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4276 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4281 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4282 if (rid
== rids
[rid_index
])
4286 if (rid_index
== num_rids
) {
4287 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4291 attrs
[rid_index
] = type
;
4292 names
[rid_index
] = attr
;
4296 result
= NT_STATUS_NONE_MAPPED
;
4299 result
= (num_mapped
== num_rids
) ?
4300 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4302 TALLOC_FREE(mem_ctx
);
4306 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4308 char *filter
= NULL
;
4309 char *escaped
= NULL
;
4310 char *result
= NULL
;
4312 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4313 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4317 escaped
= escape_ldap_string(talloc_tos(), username
);
4318 if (escaped
== NULL
) goto done
;
4320 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4324 TALLOC_FREE(escaped
);
4329 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4333 const char **result
;
4335 va_start(ap
, mem_ctx
);
4336 while (va_arg(ap
, const char *) != NULL
)
4340 if ((result
= TALLOC_ARRAY(mem_ctx
, const char *, num
+1)) == NULL
) {
4344 va_start(ap
, mem_ctx
);
4345 for (i
=0; i
<num
; i
++) {
4346 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4347 if (result
[i
] == NULL
) {
4348 talloc_free(result
);
4359 struct ldap_search_state
{
4360 struct smbldap_state
*connection
;
4362 uint32_t acct_flags
;
4363 uint16_t group_type
;
4370 void *pagedresults_cookie
;
4372 LDAPMessage
*entries
, *current_entry
;
4373 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4374 TALLOC_CTX
*mem_ctx
,
4375 LDAP
*ld
, LDAPMessage
*entry
,
4376 struct samr_displayentry
*result
);
4379 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4381 struct ldap_search_state
*state
=
4382 (struct ldap_search_state
*)search
->private_data
;
4384 int rc
= LDAP_OPERATIONS_ERROR
;
4386 state
->entries
= NULL
;
4388 if (state
->connection
->paged_results
) {
4389 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4390 state
->scope
, state
->filter
,
4391 state
->attrs
, state
->attrsonly
,
4392 lp_ldap_page_size(), &state
->entries
,
4393 &state
->pagedresults_cookie
);
4396 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4398 if (state
->entries
!= NULL
) {
4399 /* Left over from unsuccessful paged attempt */
4400 ldap_msgfree(state
->entries
);
4401 state
->entries
= NULL
;
4404 rc
= smbldap_search(state
->connection
, state
->base
,
4405 state
->scope
, state
->filter
, state
->attrs
,
4406 state
->attrsonly
, &state
->entries
);
4408 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4411 /* Ok, the server was lying. It told us it could do paged
4412 * searches when it could not. */
4413 state
->connection
->paged_results
= False
;
4416 ld
= state
->connection
->ldap_struct
;
4418 DEBUG(5, ("Don't have an LDAP connection right after a "
4422 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4427 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4429 struct ldap_search_state
*state
=
4430 (struct ldap_search_state
*)search
->private_data
;
4433 if (!state
->connection
->paged_results
) {
4434 /* There is no next page when there are no paged results */
4438 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4439 state
->scope
, state
->filter
, state
->attrs
,
4440 state
->attrsonly
, lp_ldap_page_size(),
4442 &state
->pagedresults_cookie
);
4444 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4447 state
->current_entry
= ldap_first_entry(state
->connection
->ldap_struct
, state
->entries
);
4449 if (state
->current_entry
== NULL
) {
4450 ldap_msgfree(state
->entries
);
4451 state
->entries
= NULL
;
4458 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4459 struct samr_displayentry
*entry
)
4461 struct ldap_search_state
*state
=
4462 (struct ldap_search_state
*)search
->private_data
;
4466 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4469 if ((state
->entries
== NULL
) &&
4470 !ldapsam_search_nextpage(search
))
4473 if (state
->current_entry
== NULL
) {
4477 result
= state
->ldap2displayentry(state
, search
,
4478 state
->connection
->ldap_struct
,
4479 state
->current_entry
, entry
);
4483 dn
= ldap_get_dn(state
->connection
->ldap_struct
, state
->current_entry
);
4484 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4485 if (dn
!= NULL
) ldap_memfree(dn
);
4488 state
->current_entry
= ldap_next_entry(state
->connection
->ldap_struct
, state
->current_entry
);
4490 if (state
->current_entry
== NULL
) {
4491 ldap_msgfree(state
->entries
);
4492 state
->entries
= NULL
;
4495 if (!result
) goto retry
;
4500 static void ldapsam_search_end(struct pdb_search
*search
)
4502 struct ldap_search_state
*state
=
4503 (struct ldap_search_state
*)search
->private_data
;
4506 if (state
->pagedresults_cookie
== NULL
)
4509 if (state
->entries
!= NULL
)
4510 ldap_msgfree(state
->entries
);
4512 state
->entries
= NULL
;
4513 state
->current_entry
= NULL
;
4515 if (!state
->connection
->paged_results
)
4518 /* Tell the LDAP server we're not interested in the rest anymore. */
4520 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4521 state
->filter
, state
->attrs
,
4522 state
->attrsonly
, 0, &state
->entries
,
4523 &state
->pagedresults_cookie
);
4525 if (rc
!= LDAP_SUCCESS
)
4526 DEBUG(5, ("Could not end search properly\n"));
4531 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4532 TALLOC_CTX
*mem_ctx
,
4533 LDAP
*ld
, LDAPMessage
*entry
,
4534 struct samr_displayentry
*result
)
4537 size_t converted_size
;
4539 uint32_t acct_flags
;
4541 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4542 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4543 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4546 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4547 ldap_value_free(vals
);
4549 if ((state
->acct_flags
!= 0) &&
4550 ((state
->acct_flags
& acct_flags
) == 0))
4553 result
->acct_flags
= acct_flags
;
4554 result
->account_name
= "";
4555 result
->fullname
= "";
4556 result
->description
= "";
4558 vals
= ldap_get_values(ld
, entry
, "uid");
4559 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4560 DEBUG(5, ("\"uid\" not found\n"));
4563 if (!pull_utf8_talloc(mem_ctx
,
4564 CONST_DISCARD(char **, &result
->account_name
),
4565 vals
[0], &converted_size
))
4567 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4571 ldap_value_free(vals
);
4573 vals
= ldap_get_values(ld
, entry
, "displayName");
4574 if ((vals
== NULL
) || (vals
[0] == NULL
))
4575 DEBUG(8, ("\"displayName\" not found\n"));
4576 else if (!pull_utf8_talloc(mem_ctx
,
4577 CONST_DISCARD(char **, &result
->fullname
),
4578 vals
[0], &converted_size
))
4580 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4584 ldap_value_free(vals
);
4586 vals
= ldap_get_values(ld
, entry
, "description");
4587 if ((vals
== NULL
) || (vals
[0] == NULL
))
4588 DEBUG(8, ("\"description\" not found\n"));
4589 else if (!pull_utf8_talloc(mem_ctx
,
4590 CONST_DISCARD(char **, &result
->description
),
4591 vals
[0], &converted_size
))
4593 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4597 ldap_value_free(vals
);
4599 if ((result
->account_name
== NULL
) ||
4600 (result
->fullname
== NULL
) ||
4601 (result
->description
== NULL
)) {
4602 DEBUG(0, ("talloc failed\n"));
4606 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4607 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4608 DEBUG(0, ("\"objectSid\" not found\n"));
4612 if (!string_to_sid(&sid
, vals
[0])) {
4613 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4614 ldap_value_free(vals
);
4617 ldap_value_free(vals
);
4619 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4620 DEBUG(0, ("sid %s does not belong to our domain\n",
4621 sid_string_dbg(&sid
)));
4629 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4630 struct pdb_search
*search
,
4631 uint32_t acct_flags
)
4633 struct ldapsam_privates
*ldap_state
=
4634 (struct ldapsam_privates
*)methods
->private_data
;
4635 struct ldap_search_state
*state
;
4637 state
= talloc(search
, struct ldap_search_state
);
4638 if (state
== NULL
) {
4639 DEBUG(0, ("talloc failed\n"));
4643 state
->connection
= ldap_state
->smbldap_state
;
4645 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4646 state
->base
= lp_ldap_user_suffix();
4647 else if ((acct_flags
!= 0) &&
4648 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4649 state
->base
= lp_ldap_machine_suffix();
4651 state
->base
= lp_ldap_suffix();
4653 state
->acct_flags
= acct_flags
;
4654 state
->base
= talloc_strdup(search
, state
->base
);
4655 state
->scope
= LDAP_SCOPE_SUBTREE
;
4656 state
->filter
= get_ldap_filter(search
, "*");
4657 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4658 "displayName", "description",
4659 "sambaAcctFlags", NULL
);
4660 state
->attrsonly
= 0;
4661 state
->pagedresults_cookie
= NULL
;
4662 state
->entries
= NULL
;
4663 state
->ldap2displayentry
= ldapuser2displayentry
;
4665 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4666 DEBUG(0, ("talloc failed\n"));
4670 search
->private_data
= state
;
4671 search
->next_entry
= ldapsam_search_next_entry
;
4672 search
->search_end
= ldapsam_search_end
;
4674 return ldapsam_search_firstpage(search
);
4677 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4678 TALLOC_CTX
*mem_ctx
,
4679 LDAP
*ld
, LDAPMessage
*entry
,
4680 struct samr_displayentry
*result
)
4683 size_t converted_size
;
4685 uint16_t group_type
;
4687 result
->account_name
= "";
4688 result
->fullname
= "";
4689 result
->description
= "";
4692 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4693 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4694 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4696 ldap_value_free(vals
);
4701 group_type
= atoi(vals
[0]);
4703 if ((state
->group_type
!= 0) &&
4704 ((state
->group_type
!= group_type
))) {
4705 ldap_value_free(vals
);
4709 ldap_value_free(vals
);
4711 /* display name is the NT group name */
4713 vals
= ldap_get_values(ld
, entry
, "displayName");
4714 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4715 DEBUG(8, ("\"displayName\" not found\n"));
4717 /* fallback to the 'cn' attribute */
4718 vals
= ldap_get_values(ld
, entry
, "cn");
4719 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4720 DEBUG(5, ("\"cn\" not found\n"));
4723 if (!pull_utf8_talloc(mem_ctx
,
4724 CONST_DISCARD(char **,
4725 &result
->account_name
),
4726 vals
[0], &converted_size
))
4728 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4729 "failed: %s", strerror(errno
)));
4732 else if (!pull_utf8_talloc(mem_ctx
,
4733 CONST_DISCARD(char **,
4734 &result
->account_name
),
4735 vals
[0], &converted_size
))
4737 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4741 ldap_value_free(vals
);
4743 vals
= ldap_get_values(ld
, entry
, "description");
4744 if ((vals
== NULL
) || (vals
[0] == NULL
))
4745 DEBUG(8, ("\"description\" not found\n"));
4746 else if (!pull_utf8_talloc(mem_ctx
,
4747 CONST_DISCARD(char **, &result
->description
),
4748 vals
[0], &converted_size
))
4750 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4753 ldap_value_free(vals
);
4755 if ((result
->account_name
== NULL
) ||
4756 (result
->fullname
== NULL
) ||
4757 (result
->description
== NULL
)) {
4758 DEBUG(0, ("talloc failed\n"));
4762 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4763 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4764 DEBUG(0, ("\"objectSid\" not found\n"));
4766 ldap_value_free(vals
);
4771 if (!string_to_sid(&sid
, vals
[0])) {
4772 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4776 ldap_value_free(vals
);
4778 switch (group_type
) {
4779 case SID_NAME_DOM_GRP
:
4780 case SID_NAME_ALIAS
:
4782 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4783 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4785 DEBUG(0, ("%s is not in our domain\n",
4786 sid_string_dbg(&sid
)));
4792 DEBUG(0,("unknown group type: %d\n", group_type
));
4796 result
->acct_flags
= 0;
4801 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4802 struct pdb_search
*search
,
4803 const struct dom_sid
*sid
,
4804 enum lsa_SidType type
)
4806 struct ldapsam_privates
*ldap_state
=
4807 (struct ldapsam_privates
*)methods
->private_data
;
4808 struct ldap_search_state
*state
;
4811 state
= talloc(search
, struct ldap_search_state
);
4812 if (state
== NULL
) {
4813 DEBUG(0, ("talloc failed\n"));
4817 state
->connection
= ldap_state
->smbldap_state
;
4819 state
->base
= talloc_strdup(search
, lp_ldap_suffix());
4820 state
->connection
= ldap_state
->smbldap_state
;
4821 state
->scope
= LDAP_SCOPE_SUBTREE
;
4822 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4823 "(sambaGroupType=%d)(sambaSID=%s*))",
4825 type
, sid_to_fstring(tmp
, sid
));
4826 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4827 "displayName", "description",
4828 "sambaGroupType", NULL
);
4829 state
->attrsonly
= 0;
4830 state
->pagedresults_cookie
= NULL
;
4831 state
->entries
= NULL
;
4832 state
->group_type
= type
;
4833 state
->ldap2displayentry
= ldapgroup2displayentry
;
4835 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4836 DEBUG(0, ("talloc failed\n"));
4840 search
->private_data
= state
;
4841 search
->next_entry
= ldapsam_search_next_entry
;
4842 search
->search_end
= ldapsam_search_end
;
4844 return ldapsam_search_firstpage(search
);
4847 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4848 struct pdb_search
*search
)
4850 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4853 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4854 struct pdb_search
*search
,
4855 const struct dom_sid
*sid
)
4857 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4860 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4862 return PDB_CAP_STORE_RIDS
;
4865 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4868 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4870 LDAPMessage
*result
= NULL
;
4871 LDAPMessage
*entry
= NULL
;
4872 LDAPMod
**mods
= NULL
;
4876 uint32_t nextRid
= 0;
4879 TALLOC_CTX
*mem_ctx
;
4881 mem_ctx
= talloc_new(NULL
);
4882 if (mem_ctx
== NULL
) {
4883 DEBUG(0, ("talloc_new failed\n"));
4884 return NT_STATUS_NO_MEMORY
;
4887 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4888 get_global_sam_name(), False
);
4889 if (!NT_STATUS_IS_OK(status
)) {
4890 DEBUG(3, ("Could not get domain info: %s\n",
4891 nt_errstr(status
)));
4895 talloc_autofree_ldapmsg(mem_ctx
, result
);
4897 entry
= ldap_first_entry(priv2ld(priv
), result
);
4898 if (entry
== NULL
) {
4899 DEBUG(0, ("Could not get domain info entry\n"));
4900 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4904 /* Find the largest of the three attributes "sambaNextRid",
4905 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4906 concept of differentiating between user and group rids, and will
4907 use only "sambaNextRid" in the future. But for compatibility
4908 reasons I look if others have chosen different strategies -- VL */
4910 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4911 "sambaNextRid", mem_ctx
);
4912 if (value
!= NULL
) {
4913 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4914 nextRid
= MAX(nextRid
, tmp
);
4917 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4918 "sambaNextUserRid", mem_ctx
);
4919 if (value
!= NULL
) {
4920 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4921 nextRid
= MAX(nextRid
, tmp
);
4924 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4925 "sambaNextGroupRid", mem_ctx
);
4926 if (value
!= NULL
) {
4927 uint32_t tmp
= (uint32_t)strtoul(value
, NULL
, 10);
4928 nextRid
= MAX(nextRid
, tmp
);
4932 nextRid
= BASE_RID
-1;
4937 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
4938 talloc_asprintf(mem_ctx
, "%d", nextRid
));
4939 talloc_autofree_ldapmod(mem_ctx
, mods
);
4941 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
4942 status
= NT_STATUS_NO_MEMORY
;
4946 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
4948 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4951 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
4954 if (NT_STATUS_IS_OK(status
)) {
4958 TALLOC_FREE(mem_ctx
);
4962 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
4966 for (i
=0; i
<10; i
++) {
4967 NTSTATUS result
= ldapsam_get_new_rid(
4968 (struct ldapsam_privates
*)methods
->private_data
, rid
);
4969 if (NT_STATUS_IS_OK(result
)) {
4973 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
4977 /* The ldap update failed (maybe a race condition), retry */
4980 /* Tried 10 times, fail. */
4981 return NT_STATUS_ACCESS_DENIED
;
4984 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
4986 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
4987 return NT_STATUS_IS_OK(result
) ? True
: False
;
4990 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
4991 const struct dom_sid
*sid
,
4992 union unid_t
*id
, enum lsa_SidType
*type
)
4994 struct ldapsam_privates
*priv
=
4995 (struct ldapsam_privates
*)methods
->private_data
;
4997 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
4999 LDAPMessage
*result
= NULL
;
5000 LDAPMessage
*entry
= NULL
;
5005 TALLOC_CTX
*mem_ctx
;
5007 mem_ctx
= talloc_new(NULL
);
5008 if (mem_ctx
== NULL
) {
5009 DEBUG(0, ("talloc_new failed\n"));
5013 filter
= talloc_asprintf(mem_ctx
,
5015 "(|(objectClass=%s)(objectClass=%s)))",
5016 sid_string_talloc(mem_ctx
, sid
),
5017 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5018 if (filter
== NULL
) {
5019 DEBUG(5, ("talloc_asprintf failed\n"));
5023 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5025 if (rc
!= LDAP_SUCCESS
) {
5028 talloc_autofree_ldapmsg(mem_ctx
, result
);
5030 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5031 DEBUG(10, ("Got %d entries, expected one\n",
5032 ldap_count_entries(priv2ld(priv
), result
)));
5036 entry
= ldap_first_entry(priv2ld(priv
), result
);
5038 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5039 "sambaGroupType", mem_ctx
);
5041 if (value
!= NULL
) {
5042 const char *gid_str
;
5045 gid_str
= smbldap_talloc_single_attribute(
5046 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5047 if (gid_str
== NULL
) {
5048 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5049 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5054 id
->gid
= strtoul(gid_str
, NULL
, 10);
5055 *type
= (enum lsa_SidType
)strtoul(value
, NULL
, 10);
5056 store_gid_sid_cache(sid
, id
->gid
);
5057 idmap_cache_set_sid2gid(sid
, id
->gid
);
5062 /* It must be a user */
5064 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5065 "uidNumber", mem_ctx
);
5066 if (value
== NULL
) {
5067 DEBUG(1, ("Could not find uidNumber in %s\n",
5068 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5072 id
->uid
= strtoul(value
, NULL
, 10);
5073 *type
= SID_NAME_USER
;
5074 store_uid_sid_cache(sid
, id
->uid
);
5075 idmap_cache_set_sid2uid(sid
, id
->uid
);
5079 TALLOC_FREE(mem_ctx
);
5084 * Find the SID for a uid.
5085 * This is shortcut is only used if ldapsam:trusted is set to true.
5087 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5088 struct dom_sid
*sid
)
5090 struct ldapsam_privates
*priv
=
5091 (struct ldapsam_privates
*)methods
->private_data
;
5093 const char *attrs
[] = { "sambaSID", NULL
};
5094 LDAPMessage
*result
= NULL
;
5095 LDAPMessage
*entry
= NULL
;
5097 char *user_sid_string
;
5098 struct dom_sid user_sid
;
5100 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5102 filter
= talloc_asprintf(tmp_ctx
,
5105 "(objectClass=%s))",
5107 LDAP_OBJ_POSIXACCOUNT
,
5108 LDAP_OBJ_SAMBASAMACCOUNT
);
5109 if (filter
== NULL
) {
5110 DEBUG(3, ("talloc_asprintf failed\n"));
5114 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5115 if (rc
!= LDAP_SUCCESS
) {
5118 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5120 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5121 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5122 ldap_count_entries(priv2ld(priv
), result
),
5123 (unsigned int)uid
));
5127 entry
= ldap_first_entry(priv2ld(priv
), result
);
5129 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5130 "sambaSID", tmp_ctx
);
5131 if (user_sid_string
== NULL
) {
5132 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5133 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5137 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5138 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5143 sid_copy(sid
, &user_sid
);
5145 store_uid_sid_cache(sid
, uid
);
5146 idmap_cache_set_sid2uid(sid
, uid
);
5151 TALLOC_FREE(tmp_ctx
);
5156 * Find the SID for a gid.
5157 * This is shortcut is only used if ldapsam:trusted is set to true.
5159 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5160 struct dom_sid
*sid
)
5162 struct ldapsam_privates
*priv
=
5163 (struct ldapsam_privates
*)methods
->private_data
;
5165 const char *attrs
[] = { "sambaSID", NULL
};
5166 LDAPMessage
*result
= NULL
;
5167 LDAPMessage
*entry
= NULL
;
5169 char *group_sid_string
;
5170 struct dom_sid group_sid
;
5172 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5174 filter
= talloc_asprintf(tmp_ctx
,
5176 "(objectClass=%s))",
5179 if (filter
== NULL
) {
5180 DEBUG(3, ("talloc_asprintf failed\n"));
5184 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5185 if (rc
!= LDAP_SUCCESS
) {
5188 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5190 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5191 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5192 ldap_count_entries(priv2ld(priv
), result
),
5193 (unsigned int)gid
));
5197 entry
= ldap_first_entry(priv2ld(priv
), result
);
5199 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5200 "sambaSID", tmp_ctx
);
5201 if (group_sid_string
== NULL
) {
5202 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5203 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5207 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5208 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5213 sid_copy(sid
, &group_sid
);
5215 store_gid_sid_cache(sid
, gid
);
5216 idmap_cache_set_sid2gid(sid
, gid
);
5221 TALLOC_FREE(tmp_ctx
);
5227 * The following functions are called only if
5228 * ldapsam:trusted and ldapsam:editposix are
5233 * ldapsam_create_user creates a new
5234 * posixAccount and sambaSamAccount object
5235 * in the ldap users subtree
5237 * The uid is allocated by winbindd.
5240 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5241 TALLOC_CTX
*tmp_ctx
, const char *name
,
5242 uint32_t acb_info
, uint32_t *rid
)
5244 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5245 LDAPMessage
*entry
= NULL
;
5246 LDAPMessage
*result
= NULL
;
5247 uint32_t num_result
;
5248 bool is_machine
= False
;
5249 bool add_posix
= False
;
5250 LDAPMod
**mods
= NULL
;
5258 const char *dn
= NULL
;
5259 struct dom_sid group_sid
;
5260 struct dom_sid user_sid
;
5266 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5267 acb_info
& ACB_WSTRUST
||
5268 acb_info
& ACB_SVRTRUST
||
5269 acb_info
& ACB_DOMTRUST
) {
5273 username
= escape_ldap_string(talloc_tos(), name
);
5274 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5275 username
, LDAP_OBJ_POSIXACCOUNT
);
5276 TALLOC_FREE(username
);
5278 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5279 if (rc
!= LDAP_SUCCESS
) {
5280 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5281 return NT_STATUS_ACCESS_DENIED
;
5283 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5285 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5287 if (num_result
> 1) {
5288 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5289 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5292 if (num_result
== 1) {
5294 /* check if it is just a posix account.
5295 * or if there is a sid attached to this entry
5298 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5300 return NT_STATUS_UNSUCCESSFUL
;
5303 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5305 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5306 return NT_STATUS_USER_EXISTS
;
5309 /* it is just a posix account, retrieve the dn for later use */
5310 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5312 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5313 return NT_STATUS_NO_MEMORY
;
5317 if (num_result
== 0) {
5321 /* Create the basic samu structure and generate the mods for the ldap commit */
5322 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5323 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5327 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5329 user
= samu_new(tmp_ctx
);
5331 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5332 return NT_STATUS_NO_MEMORY
;
5335 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5336 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5337 return NT_STATUS_UNSUCCESSFUL
;
5339 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5340 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5341 return NT_STATUS_UNSUCCESSFUL
;
5344 if (acb_info
& ACB_NORMAL
) {
5345 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5346 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5347 return NT_STATUS_UNSUCCESSFUL
;
5350 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5351 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5352 return NT_STATUS_UNSUCCESSFUL
;
5356 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5357 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5358 return NT_STATUS_UNSUCCESSFUL
;
5362 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5363 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5364 return NT_STATUS_UNSUCCESSFUL
;
5367 if (!init_ldap_from_sam(ldap_state
, NULL
, &mods
, user
, element_is_set_or_changed
)) {
5368 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5369 return NT_STATUS_UNSUCCESSFUL
;
5372 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5373 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5375 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5380 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5382 /* retrieve the Domain Users group gid */
5383 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5384 !sid_to_gid(&group_sid
, &gid
)) {
5385 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5386 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5389 /* lets allocate a new userid for this user */
5390 if (!winbind_allocate_uid(&uid
)) {
5391 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5392 return NT_STATUS_UNSUCCESSFUL
;
5397 /* TODO: choose a more appropriate default for machines */
5398 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), "SMB_workstations_home", ldap_state
->domain_name
, uid
, gid
);
5399 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5401 homedir
= talloc_sub_specified(tmp_ctx
, lp_template_homedir(), name
, ldap_state
->domain_name
, uid
, gid
);
5402 shell
= talloc_sub_specified(tmp_ctx
, lp_template_shell(), name
, ldap_state
->domain_name
, uid
, gid
);
5404 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5405 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5407 escape_name
= escape_rdn_val_string_alloc(name
);
5409 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5410 return NT_STATUS_NO_MEMORY
;
5414 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix ());
5416 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix ());
5419 SAFE_FREE(escape_name
);
5421 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5422 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5423 return NT_STATUS_NO_MEMORY
;
5426 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5427 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5428 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5429 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5430 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5431 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5432 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5435 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5438 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5440 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5443 if (rc
!= LDAP_SUCCESS
) {
5444 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5445 return NT_STATUS_UNSUCCESSFUL
;
5448 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5450 flush_pwnam_cache();
5452 return NT_STATUS_OK
;
5455 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5457 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5458 LDAPMessage
*result
= NULL
;
5459 LDAPMessage
*entry
= NULL
;
5465 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5467 filter
= talloc_asprintf(tmp_ctx
,
5470 "(objectClass=%s))",
5471 pdb_get_username(sam_acct
),
5472 LDAP_OBJ_POSIXACCOUNT
,
5473 LDAP_OBJ_SAMBASAMACCOUNT
);
5474 if (filter
== NULL
) {
5475 return NT_STATUS_NO_MEMORY
;
5478 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5479 if (rc
!= LDAP_SUCCESS
) {
5480 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5481 return NT_STATUS_UNSUCCESSFUL
;
5483 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5485 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5487 if (num_result
== 0) {
5488 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5489 return NT_STATUS_NO_SUCH_USER
;
5492 if (num_result
> 1) {
5493 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5494 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5497 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5499 return NT_STATUS_UNSUCCESSFUL
;
5502 /* it is just a posix account, retrieve the dn for later use */
5503 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5505 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5506 return NT_STATUS_NO_MEMORY
;
5509 /* try to remove memberships first */
5512 struct dom_sid
*sids
= NULL
;
5514 uint32_t num_groups
= 0;
5516 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5518 status
= ldapsam_enum_group_memberships(my_methods
,
5524 if (!NT_STATUS_IS_OK(status
)) {
5528 for (i
=0; i
< num_groups
; i
++) {
5532 sid_peek_rid(&sids
[i
], &group_rid
);
5534 ldapsam_del_groupmem(my_methods
,
5543 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5544 if (rc
!= LDAP_SUCCESS
) {
5545 return NT_STATUS_UNSUCCESSFUL
;
5548 flush_pwnam_cache();
5550 return NT_STATUS_OK
;
5554 * ldapsam_create_group creates a new
5555 * posixGroup and sambaGroupMapping object
5556 * in the ldap groups subtree
5558 * The gid is allocated by winbindd.
5561 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5562 TALLOC_CTX
*tmp_ctx
,
5566 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5568 LDAPMessage
*entry
= NULL
;
5569 LDAPMessage
*result
= NULL
;
5570 uint32_t num_result
;
5571 bool is_new_entry
= False
;
5572 LDAPMod
**mods
= NULL
;
5578 const char *dn
= NULL
;
5579 struct dom_sid group_sid
;
5583 groupname
= escape_ldap_string(talloc_tos(), name
);
5584 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5585 groupname
, LDAP_OBJ_POSIXGROUP
);
5586 TALLOC_FREE(groupname
);
5588 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5589 if (rc
!= LDAP_SUCCESS
) {
5590 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5591 return NT_STATUS_UNSUCCESSFUL
;
5593 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5595 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5597 if (num_result
> 1) {
5598 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5599 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5602 if (num_result
== 1) {
5604 /* check if it is just a posix group.
5605 * or if there is a sid attached to this entry
5608 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5610 return NT_STATUS_UNSUCCESSFUL
;
5613 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5615 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5616 return NT_STATUS_GROUP_EXISTS
;
5619 /* it is just a posix group, retrieve the gid and the dn for later use */
5620 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5622 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5623 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5626 gid
= strtoul(tmp
, NULL
, 10);
5628 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5630 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5631 return NT_STATUS_NO_MEMORY
;
5635 if (num_result
== 0) {
5636 is_new_entry
= true;
5639 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5640 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5644 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5646 groupsidstr
= talloc_strdup(tmp_ctx
, sid_string_talloc(tmp_ctx
,
5648 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5650 if (!groupsidstr
|| !grouptype
) {
5651 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5652 return NT_STATUS_NO_MEMORY
;
5655 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5656 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid", groupsidstr
);
5657 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5658 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5663 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5665 /* lets allocate a new groupid for this group */
5666 if (!winbind_allocate_gid(&gid
)) {
5667 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5668 return NT_STATUS_UNSUCCESSFUL
;
5671 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5673 escape_name
= escape_rdn_val_string_alloc(name
);
5675 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5676 return NT_STATUS_NO_MEMORY
;
5679 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix());
5681 SAFE_FREE(escape_name
);
5683 if (!gidstr
|| !dn
) {
5684 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5685 return NT_STATUS_NO_MEMORY
;
5688 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5689 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5690 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5693 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5696 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5698 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5699 /* This call may fail with rfc2307bis schema */
5700 /* Retry adding a structural class */
5701 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5702 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5706 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5709 if (rc
!= LDAP_SUCCESS
) {
5710 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5711 return NT_STATUS_UNSUCCESSFUL
;
5714 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5716 return NT_STATUS_OK
;
5719 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5721 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5722 LDAPMessage
*result
= NULL
;
5723 LDAPMessage
*entry
= NULL
;
5728 struct dom_sid group_sid
;
5731 /* get the group sid */
5732 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5734 filter
= talloc_asprintf(tmp_ctx
,
5737 "(objectClass=%s))",
5738 sid_string_talloc(tmp_ctx
, &group_sid
),
5739 LDAP_OBJ_POSIXGROUP
,
5741 if (filter
== NULL
) {
5742 return NT_STATUS_NO_MEMORY
;
5745 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5746 if (rc
!= LDAP_SUCCESS
) {
5747 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5748 return NT_STATUS_UNSUCCESSFUL
;
5750 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5752 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5754 if (num_result
== 0) {
5755 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5756 return NT_STATUS_NO_SUCH_GROUP
;
5759 if (num_result
> 1) {
5760 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5761 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5764 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5766 return NT_STATUS_UNSUCCESSFUL
;
5769 /* here it is, retrieve the dn for later use */
5770 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5772 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5773 return NT_STATUS_NO_MEMORY
;
5776 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5778 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5779 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5782 /* check no user have this group marked as primary group */
5783 filter
= talloc_asprintf(tmp_ctx
,
5786 "(objectClass=%s))",
5788 LDAP_OBJ_POSIXACCOUNT
,
5789 LDAP_OBJ_SAMBASAMACCOUNT
);
5791 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5792 if (rc
!= LDAP_SUCCESS
) {
5793 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5794 return NT_STATUS_UNSUCCESSFUL
;
5796 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5798 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5800 if (num_result
!= 0) {
5801 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5802 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5805 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5806 if (rc
!= LDAP_SUCCESS
) {
5807 return NT_STATUS_UNSUCCESSFUL
;
5810 return NT_STATUS_OK
;
5813 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5814 TALLOC_CTX
*tmp_ctx
,
5816 uint32_t member_rid
,
5819 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5820 LDAPMessage
*entry
= NULL
;
5821 LDAPMessage
*result
= NULL
;
5822 uint32_t num_result
;
5823 LDAPMod
**mods
= NULL
;
5826 const char *dn
= NULL
;
5827 struct dom_sid group_sid
;
5828 struct dom_sid member_sid
;
5833 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5835 case LDAP_MOD_DELETE
:
5836 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5839 return NT_STATUS_UNSUCCESSFUL
;
5842 /* get member sid */
5843 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5845 /* get the group sid */
5846 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5848 filter
= talloc_asprintf(tmp_ctx
,
5851 "(objectClass=%s))",
5852 sid_string_talloc(tmp_ctx
, &member_sid
),
5853 LDAP_OBJ_POSIXACCOUNT
,
5854 LDAP_OBJ_SAMBASAMACCOUNT
);
5855 if (filter
== NULL
) {
5856 return NT_STATUS_NO_MEMORY
;
5859 /* get the member uid */
5860 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5861 if (rc
!= LDAP_SUCCESS
) {
5862 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5863 return NT_STATUS_UNSUCCESSFUL
;
5865 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5867 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5869 if (num_result
== 0) {
5870 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5871 return NT_STATUS_NO_SUCH_MEMBER
;
5874 if (num_result
> 1) {
5875 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5876 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5879 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5881 return NT_STATUS_UNSUCCESSFUL
;
5884 if (modop
== LDAP_MOD_DELETE
) {
5885 /* check if we are trying to remove the member from his primary group */
5887 gid_t user_gid
, group_gid
;
5889 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5891 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5892 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5895 user_gid
= strtoul(gidstr
, NULL
, 10);
5897 if (!sid_to_gid(&group_sid
, &group_gid
)) {
5898 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5899 return NT_STATUS_UNSUCCESSFUL
;
5902 if (user_gid
== group_gid
) {
5903 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5904 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5908 /* here it is, retrieve the uid for later use */
5909 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
5911 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5912 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5915 filter
= talloc_asprintf(tmp_ctx
,
5918 "(objectClass=%s))",
5919 sid_string_talloc(tmp_ctx
, &group_sid
),
5920 LDAP_OBJ_POSIXGROUP
,
5924 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5925 if (rc
!= LDAP_SUCCESS
) {
5926 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5927 return NT_STATUS_UNSUCCESSFUL
;
5929 talloc_autofree_ldapmsg(tmp_ctx
, result
);
5931 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5933 if (num_result
== 0) {
5934 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5935 return NT_STATUS_NO_SUCH_GROUP
;
5938 if (num_result
> 1) {
5939 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5940 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5943 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5945 return NT_STATUS_UNSUCCESSFUL
;
5948 /* here it is, retrieve the dn for later use */
5949 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5951 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5952 return NT_STATUS_NO_MEMORY
;
5955 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
5957 talloc_autofree_ldapmod(tmp_ctx
, mods
);
5959 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5960 if (rc
!= LDAP_SUCCESS
) {
5961 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
5962 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5963 return NT_STATUS_MEMBER_IN_GROUP
;
5965 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
5966 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5967 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
5969 return NT_STATUS_UNSUCCESSFUL
;
5972 return NT_STATUS_OK
;
5975 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
5976 TALLOC_CTX
*tmp_ctx
,
5978 uint32_t member_rid
)
5980 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
5982 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
5983 TALLOC_CTX
*tmp_ctx
,
5985 uint32_t member_rid
)
5987 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
5990 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
5991 TALLOC_CTX
*mem_ctx
,
5992 struct samu
*sampass
)
5994 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5995 LDAPMessage
*entry
= NULL
;
5996 LDAPMessage
*result
= NULL
;
5997 uint32_t num_result
;
5998 LDAPMod
**mods
= NULL
;
6000 char *escape_username
;
6002 const char *dn
= NULL
;
6006 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6008 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6009 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6010 return NT_STATUS_UNSUCCESSFUL
;
6012 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6014 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6015 return NT_STATUS_NO_MEMORY
;
6018 escape_username
= escape_ldap_string(talloc_tos(),
6019 pdb_get_username(sampass
));
6020 if (escape_username
== NULL
) {
6021 return NT_STATUS_NO_MEMORY
;
6024 filter
= talloc_asprintf(mem_ctx
,
6027 "(objectClass=%s))",
6029 LDAP_OBJ_POSIXACCOUNT
,
6030 LDAP_OBJ_SAMBASAMACCOUNT
);
6032 TALLOC_FREE(escape_username
);
6034 if (filter
== NULL
) {
6035 return NT_STATUS_NO_MEMORY
;
6038 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6039 if (rc
!= LDAP_SUCCESS
) {
6040 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6041 return NT_STATUS_UNSUCCESSFUL
;
6043 talloc_autofree_ldapmsg(mem_ctx
, result
);
6045 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6047 if (num_result
== 0) {
6048 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6049 return NT_STATUS_NO_SUCH_USER
;
6052 if (num_result
> 1) {
6053 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6054 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6057 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6059 return NT_STATUS_UNSUCCESSFUL
;
6062 /* retrieve the dn for later use */
6063 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6065 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6066 return NT_STATUS_NO_MEMORY
;
6069 /* remove the old one, and add the new one, this way we do not risk races */
6070 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6073 return NT_STATUS_OK
;
6076 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6078 if (rc
!= LDAP_SUCCESS
) {
6079 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6080 pdb_get_username(sampass
), gidstr
));
6081 return NT_STATUS_UNSUCCESSFUL
;
6084 flush_pwnam_cache();
6086 return NT_STATUS_OK
;
6090 /**********************************************************************
6091 trusted domains functions
6092 *********************************************************************/
6094 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6097 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6098 ldap_state
->domain_dn
);
6101 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6102 TALLOC_CTX
*mem_ctx
,
6103 const char *domain
, LDAPMessage
**entry
)
6107 int scope
= LDAP_SCOPE_SUBTREE
;
6108 const char **attrs
= NULL
; /* NULL: get all attrs */
6109 int attrsonly
= 0; /* 0: return values too */
6110 LDAPMessage
*result
= NULL
;
6112 uint32_t num_result
;
6114 filter
= talloc_asprintf(talloc_tos(),
6115 "(&(objectClass=%s)(sambaDomainName=%s))",
6116 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6118 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6119 if (trusted_dn
== NULL
) {
6122 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6123 filter
, attrs
, attrsonly
, &result
);
6125 if (result
!= NULL
) {
6126 talloc_autofree_ldapmsg(mem_ctx
, result
);
6129 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6134 if (rc
!= LDAP_SUCCESS
) {
6138 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6140 if (num_result
> 1) {
6141 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6142 "%s object for domain '%s'?!\n",
6143 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6147 if (num_result
== 0) {
6148 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6149 "%s object for domain %s.\n",
6150 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6153 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6159 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6162 struct dom_sid
*sid
,
6163 time_t *pass_last_set_time
)
6165 struct ldapsam_privates
*ldap_state
=
6166 (struct ldapsam_privates
*)methods
->private_data
;
6167 LDAPMessage
*entry
= NULL
;
6169 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6171 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6180 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6181 entry
, "sambaClearTextPassword", talloc_tos());
6182 if (pwd_str
== NULL
) {
6185 /* trusteddom_pw routines do not use talloc yet... */
6186 *pwd
= SMB_STRDUP(pwd_str
);
6192 /* last change time */
6193 if (pass_last_set_time
!= NULL
) {
6195 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6196 entry
, "sambaPwdLastSet", talloc_tos());
6197 if (time_str
== NULL
) {
6200 *pass_last_set_time
= (time_t)atol(time_str
);
6206 struct dom_sid dom_sid
;
6207 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6210 if (sid_str
== NULL
) {
6213 if (!string_to_sid(&dom_sid
, sid_str
)) {
6216 sid_copy(sid
, &dom_sid
);
6222 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6225 const struct dom_sid
*sid
)
6227 struct ldapsam_privates
*ldap_state
=
6228 (struct ldapsam_privates
*)methods
->private_data
;
6229 LDAPMessage
*entry
= NULL
;
6230 LDAPMod
**mods
= NULL
;
6231 char *prev_pwd
= NULL
;
6232 char *trusted_dn
= NULL
;
6235 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6238 * get the current entry (if there is one) in order to put the
6239 * current password into the previous password attribute
6241 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6246 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6247 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6248 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6250 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6251 sid_string_tos(sid
));
6252 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6253 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6254 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6255 "sambaClearTextPassword", pwd
);
6257 if (entry
!= NULL
) {
6258 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6259 entry
, "sambaClearTextPassword", talloc_tos());
6260 if (prev_pwd
!= NULL
) {
6261 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6262 "sambaPreviousClearTextPassword",
6267 talloc_autofree_ldapmod(talloc_tos(), mods
);
6269 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6270 if (trusted_dn
== NULL
) {
6273 if (entry
== NULL
) {
6274 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6276 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6279 if (rc
!= LDAP_SUCCESS
) {
6280 DEBUG(1, ("error writing trusted domain password!\n"));
6287 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6291 struct ldapsam_privates
*ldap_state
=
6292 (struct ldapsam_privates
*)methods
->private_data
;
6293 LDAPMessage
*entry
= NULL
;
6294 const char *trusted_dn
;
6296 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6300 if (entry
== NULL
) {
6301 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6306 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6308 if (trusted_dn
== NULL
) {
6309 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6313 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6314 if (rc
!= LDAP_SUCCESS
) {
6321 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6322 TALLOC_CTX
*mem_ctx
,
6323 uint32_t *num_domains
,
6324 struct trustdom_info
***domains
)
6327 struct ldapsam_privates
*ldap_state
=
6328 (struct ldapsam_privates
*)methods
->private_data
;
6330 int scope
= LDAP_SCOPE_SUBTREE
;
6331 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6332 int attrsonly
= 0; /* 0: return values too */
6333 LDAPMessage
*result
= NULL
;
6334 LDAPMessage
*entry
= NULL
;
6336 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6337 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6339 rc
= smbldap_search(ldap_state
->smbldap_state
,
6340 ldap_state
->domain_dn
,
6347 if (result
!= NULL
) {
6348 talloc_autofree_ldapmsg(mem_ctx
, result
);
6351 if (rc
!= LDAP_SUCCESS
) {
6352 return NT_STATUS_UNSUCCESSFUL
;
6356 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
6357 DEBUG(1, ("talloc failed\n"));
6358 return NT_STATUS_NO_MEMORY
;
6361 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6363 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6365 char *dom_name
, *dom_sid_str
;
6366 struct trustdom_info
*dom_info
;
6368 dom_info
= TALLOC_P(*domains
, struct trustdom_info
);
6369 if (dom_info
== NULL
) {
6370 DEBUG(1, ("talloc failed\n"));
6371 return NT_STATUS_NO_MEMORY
;
6374 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6378 if (dom_name
== NULL
) {
6379 DEBUG(1, ("talloc failed\n"));
6380 return NT_STATUS_NO_MEMORY
;
6382 dom_info
->name
= dom_name
;
6384 dom_sid_str
= smbldap_talloc_single_attribute(
6385 priv2ld(ldap_state
), entry
, "sambaSID",
6387 if (dom_sid_str
== NULL
) {
6388 DEBUG(1, ("talloc failed\n"));
6389 return NT_STATUS_NO_MEMORY
;
6391 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6392 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6394 return NT_STATUS_UNSUCCESSFUL
;
6397 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6398 domains
, num_domains
);
6400 if (*domains
== NULL
) {
6401 DEBUG(1, ("talloc failed\n"));
6402 return NT_STATUS_NO_MEMORY
;
6406 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6407 return NT_STATUS_OK
;
6411 /**********************************************************************
6413 *********************************************************************/
6415 static void free_private_data(void **vp
)
6417 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6419 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6421 if ((*ldap_state
)->result
!= NULL
) {
6422 ldap_msgfree((*ldap_state
)->result
);
6423 (*ldap_state
)->result
= NULL
;
6425 if ((*ldap_state
)->domain_dn
!= NULL
) {
6426 SAFE_FREE((*ldap_state
)->domain_dn
);
6431 /* No need to free any further, as it is talloc()ed */
6434 /*********************************************************************
6435 Intitalise the parts of the pdb_methods structure that are common to
6437 *********************************************************************/
6439 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6442 struct ldapsam_privates
*ldap_state
;
6444 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6448 (*pdb_method
)->name
= "ldapsam";
6450 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6451 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6452 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6453 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6454 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6455 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6457 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6458 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6459 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6460 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6461 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6462 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6463 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6465 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6466 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6468 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6470 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6471 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6473 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6474 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6475 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6476 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6478 /* TODO: Setup private data and free */
6480 if ( !(ldap_state
= TALLOC_ZERO_P(*pdb_method
, struct ldapsam_privates
)) ) {
6481 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6482 return NT_STATUS_NO_MEMORY
;
6485 nt_status
= smbldap_init(*pdb_method
, pdb_get_event_context(),
6486 location
, &ldap_state
->smbldap_state
);
6488 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6492 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6493 return NT_STATUS_NO_MEMORY
;
6496 (*pdb_method
)->private_data
= ldap_state
;
6498 (*pdb_method
)->free_private_data
= free_private_data
;
6500 return NT_STATUS_OK
;
6503 /**********************************************************************
6504 Initialise the 'compat' mode for pdb_ldap
6505 *********************************************************************/
6507 NTSTATUS
pdb_init_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
6510 struct ldapsam_privates
*ldap_state
;
6511 char *uri
= talloc_strdup( NULL
, location
);
6513 trim_char( uri
, '\"', '\"' );
6514 nt_status
= pdb_init_ldapsam_common( pdb_method
, uri
);
6518 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6522 (*pdb_method
)->name
= "ldapsam_compat";
6524 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6525 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
6527 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6529 return NT_STATUS_OK
;
6532 /**********************************************************************
6533 Initialise the normal mode for pdb_ldap
6534 *********************************************************************/
6536 NTSTATUS
pdb_init_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
6539 struct ldapsam_privates
*ldap_state
= NULL
;
6540 uint32_t alg_rid_base
;
6541 char *alg_rid_base_string
= NULL
;
6542 LDAPMessage
*result
= NULL
;
6543 LDAPMessage
*entry
= NULL
;
6544 struct dom_sid ldap_domain_sid
;
6545 struct dom_sid secrets_domain_sid
;
6546 char *domain_sid_string
= NULL
;
6548 char *uri
= talloc_strdup( NULL
, location
);
6550 trim_char( uri
, '\"', '\"' );
6551 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6555 if (!NT_STATUS_IS_OK(nt_status
)) {
6559 (*pdb_method
)->name
= "ldapsam";
6561 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6562 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6563 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6564 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6565 (*pdb_method
)->search_users
= ldapsam_search_users
;
6566 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6567 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6569 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6570 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6571 (*pdb_method
)->enum_group_memberships
=
6572 ldapsam_enum_group_memberships
;
6573 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6574 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6575 (*pdb_method
)->uid_to_sid
= ldapsam_uid_to_sid
;
6576 (*pdb_method
)->gid_to_sid
= ldapsam_gid_to_sid
;
6578 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6579 (*pdb_method
)->create_user
= ldapsam_create_user
;
6580 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6581 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6582 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6583 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6584 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6585 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6589 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6590 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6592 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6594 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6596 ldap_state
->domain_name
, True
);
6598 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6599 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6600 "info, nor add one to the domain\n"));
6601 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6602 "will be unable to allocate new users/groups, "
6603 "and will risk BDCs having inconsistent SIDs\n"));
6604 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
6605 return NT_STATUS_OK
;
6608 /* Given that the above might fail, everything below this must be
6611 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
6614 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6616 ldap_msgfree(result
);
6617 return NT_STATUS_UNSUCCESSFUL
;
6620 dn
= smbldap_talloc_dn(talloc_tos(), ldap_state
->smbldap_state
->ldap_struct
, entry
);
6622 ldap_msgfree(result
);
6623 return NT_STATUS_UNSUCCESSFUL
;
6626 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6629 domain_sid_string
= smbldap_talloc_single_attribute(
6630 ldap_state
->smbldap_state
->ldap_struct
,
6632 get_userattr_key2string(ldap_state
->schema_ver
,
6633 LDAP_ATTR_USER_SID
),
6636 if (domain_sid_string
) {
6638 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6639 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6640 "read as a valid SID\n", domain_sid_string
));
6641 ldap_msgfree(result
);
6642 TALLOC_FREE(domain_sid_string
);
6643 return NT_STATUS_INVALID_PARAMETER
;
6645 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
,
6646 &secrets_domain_sid
);
6647 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6648 &ldap_domain_sid
)) {
6649 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6650 "%s based on pdb_ldap results %s -> %s\n",
6651 ldap_state
->domain_name
,
6652 sid_string_dbg(&secrets_domain_sid
),
6653 sid_string_dbg(&ldap_domain_sid
)));
6655 /* reset secrets.tdb sid */
6656 secrets_store_domain_sid(ldap_state
->domain_name
,
6658 DEBUG(1, ("New global sam SID: %s\n",
6659 sid_string_dbg(get_global_sam_sid())));
6661 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6662 TALLOC_FREE(domain_sid_string
);
6665 alg_rid_base_string
= smbldap_talloc_single_attribute(
6666 ldap_state
->smbldap_state
->ldap_struct
,
6668 get_attr_key2string( dominfo_attr_list
,
6669 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6671 if (alg_rid_base_string
) {
6672 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6673 if (alg_rid_base
!= algorithmic_rid_base()) {
6674 DEBUG(0, ("The value of 'algorithmic RID base' has "
6675 "changed since the LDAP\n"
6676 "database was initialised. Aborting. \n"));
6677 ldap_msgfree(result
);
6678 TALLOC_FREE(alg_rid_base_string
);
6679 return NT_STATUS_UNSUCCESSFUL
;
6681 TALLOC_FREE(alg_rid_base_string
);
6683 ldap_msgfree(result
);
6685 return NT_STATUS_OK
;
6688 NTSTATUS
pdb_ldap_init(void)
6691 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
6694 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
6697 /* Let pdb_nds register backends */
6702 return NT_STATUS_OK
;