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
48 #include "../libcli/auth/libcli_auth.h"
50 #include "idmap_cache.h"
51 #include "../libcli/security/security.h"
52 #include "../lib/util/util_pw.h"
53 #include "lib/winbind_util.h"
54 #include "librpc/gen_ndr/idmap.h"
55 #include "lib/param/loadparm.h"
56 #include "lib/util_sid_passdb.h"
59 #define DBGC_CLASS DBGC_PASSDB
66 #include "passdb/pdb_ldap.h"
67 #include "passdb/pdb_nds.h"
68 #include "passdb/pdb_ldap_util.h"
69 #include "passdb/pdb_ldap_schema.h"
71 /**********************************************************************
72 Simple helper function to make stuff better readable
73 **********************************************************************/
75 LDAP
*priv2ld(struct ldapsam_privates
*priv
)
77 return smbldap_get_ldap(priv
->smbldap_state
);
80 /**********************************************************************
81 Get the attribute name given a user schame version.
82 **********************************************************************/
84 static const char* get_userattr_key2string( int schema_ver
, int key
)
86 switch ( schema_ver
) {
87 case SCHEMAVER_SAMBASAMACCOUNT
:
88 return get_attr_key2string( attrib_map_v30
, key
);
91 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
97 /**********************************************************************
98 Return the list of attribute names given a user schema version.
99 **********************************************************************/
101 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
103 switch ( schema_ver
) {
104 case SCHEMAVER_SAMBASAMACCOUNT
:
105 return get_attr_list( mem_ctx
, attrib_map_v30
);
107 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
114 /**************************************************************************
115 Return the list of attribute names to delete given a user schema version.
116 **************************************************************************/
118 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
121 switch ( schema_ver
) {
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_SAMBASAMACCOUNT
:
146 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
149 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
150 objclass_filter
[0] = '\0';
154 result
= talloc_strdup(talloc_tos(), objclass_filter
);
155 SMB_ASSERT(result
!= NULL
);
159 /*****************************************************************
160 Scan a sequence number off OpenLDAP's syncrepl contextCSN
161 ******************************************************************/
163 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
165 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
166 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
167 LDAPMessage
*msg
= NULL
;
168 LDAPMessage
*entry
= NULL
;
170 char **values
= NULL
;
171 int rc
, num_result
, num_values
, rid
;
177 /* Unfortunatly there is no proper way to detect syncrepl-support in
178 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
179 * but do not show up in the root-DSE yet. Neither we can query the
180 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
181 * objectclass. Currently we require lp_ldap_suffix() to show up as
182 * namingContext. - Guenther
185 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
190 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
194 if (!smbldap_has_naming_context(
195 smbldap_get_ldap(ldap_state
->smbldap_state
),
196 lp_ldap_suffix(talloc_tos()))) {
197 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
198 "as top-level namingContext\n", lp_ldap_suffix(talloc_tos())));
202 mem_ctx
= talloc_init("ldapsam_get_seq_num");
205 return NT_STATUS_NO_MEMORY
;
207 if ((attrs
= talloc_array(mem_ctx
, const char *, 2)) == NULL
) {
208 ntstatus
= NT_STATUS_NO_MEMORY
;
212 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
213 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
216 /* consumer syncreplCookie: */
217 /* csn=20050126161620Z#0000001#00#00000 */
218 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
220 suffix
= talloc_asprintf(mem_ctx
,
221 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix(talloc_tos()));
223 ntstatus
= NT_STATUS_NO_MEMORY
;
228 /* provider contextCSN */
229 /* 20050126161620Z#000009#00#000000 */
230 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
232 suffix
= talloc_asprintf(mem_ctx
,
233 "cn=ldapsync,%s", lp_ldap_suffix(talloc_tos()));
236 ntstatus
= NT_STATUS_NO_MEMORY
;
241 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
242 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
244 if (rc
!= LDAP_SUCCESS
) {
248 num_result
= ldap_count_entries(
249 smbldap_get_ldap(ldap_state
->smbldap_state
), 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(
256 smbldap_get_ldap(ldap_state
->smbldap_state
), msg
);
258 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
262 values
= ldap_get_values(
263 smbldap_get_ldap(ldap_state
->smbldap_state
), entry
, attrs
[0]);
264 if (values
== NULL
) {
265 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
269 num_values
= ldap_count_values(values
);
270 if (num_values
== 0) {
271 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
276 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
277 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
282 if (!strncmp(p
, "csn=", strlen("csn=")))
285 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
287 *seq_num
= generalized_to_unix_time(p
);
289 /* very basic sanity check */
291 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
296 ntstatus
= NT_STATUS_OK
;
300 ldap_value_free(values
);
304 talloc_destroy(mem_ctx
);
309 /*******************************************************************
310 Run the search by name.
311 ******************************************************************/
313 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
315 LDAPMessage
** result
,
319 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
323 return LDAP_NO_MEMORY
;
327 * in the filter expression, replace %u with the real name
328 * so in ldap filter, %u MUST exist :-)
330 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
331 get_objclass_filter(ldap_state
->schema_ver
));
333 TALLOC_FREE(escape_user
);
334 return LDAP_NO_MEMORY
;
337 * have to use this here because $ is filtered out
341 filter
= talloc_all_string_sub(talloc_tos(),
342 filter
, "%u", escape_user
);
343 TALLOC_FREE(escape_user
);
345 return LDAP_NO_MEMORY
;
348 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
349 filter
, attr
, result
);
354 /*******************************************************************
355 Run the search by SID.
356 ******************************************************************/
358 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
359 const struct dom_sid
*sid
, LDAPMessage
** result
,
364 struct dom_sid_buf sid_string
;
366 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
367 get_userattr_key2string(ldap_state
->schema_ver
,
369 dom_sid_str_buf(sid
, &sid_string
),
370 get_objclass_filter(ldap_state
->schema_ver
));
372 return LDAP_NO_MEMORY
;
375 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
376 filter
, attr
, result
);
382 /*******************************************************************
383 Delete complete object or objectclass and attrs from
384 object found in search_result depending on lp_ldap_delete_dn
385 ******************************************************************/
387 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
390 const char *objectclass
,
393 LDAPMod
**mods
= NULL
;
396 BerElement
*ptr
= NULL
;
398 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
400 return LDAP_NO_MEMORY
;
403 if (lp_ldap_delete_dn()) {
404 return smbldap_delete(priv
->smbldap_state
, dn
);
407 /* Ok, delete only the SAM attributes */
409 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
411 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
414 /* We are only allowed to delete the attributes that
417 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
418 if (strequal(*attrib
, name
)) {
419 DEBUG(10, ("ldapsam_delete_entry: deleting "
420 "attribute %s\n", name
));
421 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
432 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
433 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
435 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
438 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
443 temp
= smbldap_talloc_single_attribute(
444 smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
445 get_userattr_key2string(ldap_state
->schema_ver
,
446 LDAP_ATTR_MOD_TIMESTAMP
),
452 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
453 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
463 /**********************************************************************
464 Initialize struct samu from an LDAP query.
465 (Based on init_sam_from_buffer in pdb_tdb.c)
466 *********************************************************************/
468 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
469 struct samu
* sampass
,
476 pass_can_change_time
,
479 char *username
= NULL
,
485 *logon_script
= NULL
,
486 *profile_path
= NULL
,
488 *workstations
= NULL
,
491 uint8_t smblmpwd
[LM_HASH_LEN
],
492 smbntpwd
[NT_HASH_LEN
];
493 bool use_samba_attrs
= True
;
494 uint32_t acct_ctrl
= 0;
496 uint16_t bad_password_count
= 0,
499 uint8_t hours
[MAX_HOURS_LEN
];
501 struct login_cache cache_entry
;
503 bool expand_explicit
= lp_passdb_expand_explicit();
505 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
510 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
511 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
515 if (priv2ld(ldap_state
) == NULL
) {
516 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
517 "ldap_struct is NULL!\n"));
521 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
525 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
530 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
532 nt_username
= talloc_strdup(ctx
, username
);
537 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
542 pdb_set_username(sampass
, username
, PDB_SET
);
544 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
545 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
547 /* deal with different attributes between the schema first */
549 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
550 if ((temp
= smbldap_talloc_single_attribute(
551 smbldap_get_ldap(ldap_state
->smbldap_state
),
553 get_userattr_key2string(ldap_state
->schema_ver
,
556 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
559 if ((temp
= smbldap_talloc_single_attribute(
560 smbldap_get_ldap(ldap_state
->smbldap_state
),
562 get_userattr_key2string(ldap_state
->schema_ver
,
565 user_rid
= (uint32_t)atol(temp
);
566 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
570 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
571 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
572 get_userattr_key2string(ldap_state
->schema_ver
,
574 get_userattr_key2string(ldap_state
->schema_ver
,
580 temp
= smbldap_talloc_single_attribute(
581 smbldap_get_ldap(ldap_state
->smbldap_state
),
583 get_userattr_key2string(ldap_state
->schema_ver
,
584 LDAP_ATTR_PWD_LAST_SET
),
587 pass_last_set_time
= (time_t) atol(temp
);
588 pdb_set_pass_last_set_time(sampass
,
589 pass_last_set_time
, PDB_SET
);
592 temp
= smbldap_talloc_single_attribute(
593 smbldap_get_ldap(ldap_state
->smbldap_state
),
595 get_userattr_key2string(ldap_state
->schema_ver
,
596 LDAP_ATTR_LOGON_TIME
),
599 logon_time
= (time_t) atol(temp
);
600 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
603 temp
= smbldap_talloc_single_attribute(
604 smbldap_get_ldap(ldap_state
->smbldap_state
),
606 get_userattr_key2string(ldap_state
->schema_ver
,
607 LDAP_ATTR_LOGOFF_TIME
),
610 logoff_time
= (time_t) atol(temp
);
611 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
614 temp
= smbldap_talloc_single_attribute(
615 smbldap_get_ldap(ldap_state
->smbldap_state
),
617 get_userattr_key2string(ldap_state
->schema_ver
,
618 LDAP_ATTR_KICKOFF_TIME
),
621 kickoff_time
= (time_t) atol(temp
);
622 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
625 temp
= smbldap_talloc_single_attribute(
626 smbldap_get_ldap(ldap_state
->smbldap_state
),
628 get_userattr_key2string(ldap_state
->schema_ver
,
629 LDAP_ATTR_PWD_CAN_CHANGE
),
632 pass_can_change_time
= (time_t) atol(temp
);
633 pdb_set_pass_can_change_time(sampass
,
634 pass_can_change_time
, PDB_SET
);
637 /* recommend that 'gecos' and 'displayName' should refer to the same
638 * attribute OID. userFullName depreciated, only used by Samba
639 * primary rules of LDAP: don't make a new attribute when one is already defined
640 * that fits your needs; using cn then displayName rather than 'userFullName'
643 fullname
= smbldap_talloc_single_attribute(
644 smbldap_get_ldap(ldap_state
->smbldap_state
),
646 get_userattr_key2string(ldap_state
->schema_ver
,
647 LDAP_ATTR_DISPLAY_NAME
),
650 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
652 fullname
= smbldap_talloc_single_attribute(
653 smbldap_get_ldap(ldap_state
->smbldap_state
),
655 get_userattr_key2string(ldap_state
->schema_ver
,
659 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
663 dir_drive
= smbldap_talloc_single_attribute(
664 smbldap_get_ldap(ldap_state
->smbldap_state
),
666 get_userattr_key2string(ldap_state
->schema_ver
,
667 LDAP_ATTR_HOME_DRIVE
),
670 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
672 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
675 homedir
= smbldap_talloc_single_attribute(
676 smbldap_get_ldap(ldap_state
->smbldap_state
),
678 get_userattr_key2string(ldap_state
->schema_ver
,
679 LDAP_ATTR_HOME_PATH
),
682 if (expand_explicit
) {
683 homedir
= talloc_sub_basic(ctx
,
691 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
693 pdb_set_homedir(sampass
,
694 talloc_sub_basic(ctx
, username
, domain
,
699 logon_script
= smbldap_talloc_single_attribute(
700 smbldap_get_ldap(ldap_state
->smbldap_state
),
702 get_userattr_key2string(ldap_state
->schema_ver
,
703 LDAP_ATTR_LOGON_SCRIPT
),
706 if (expand_explicit
) {
707 logon_script
= talloc_sub_basic(ctx
,
715 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
717 pdb_set_logon_script(sampass
,
718 talloc_sub_basic(ctx
, username
, domain
,
723 profile_path
= smbldap_talloc_single_attribute(
724 smbldap_get_ldap(ldap_state
->smbldap_state
),
726 get_userattr_key2string(ldap_state
->schema_ver
,
727 LDAP_ATTR_PROFILE_PATH
),
730 if (expand_explicit
) {
731 profile_path
= talloc_sub_basic(ctx
,
739 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
741 pdb_set_profile_path(sampass
,
742 talloc_sub_basic(ctx
, username
, domain
,
747 acct_desc
= smbldap_talloc_single_attribute(
748 smbldap_get_ldap(ldap_state
->smbldap_state
),
750 get_userattr_key2string(ldap_state
->schema_ver
,
754 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
757 workstations
= smbldap_talloc_single_attribute(
758 smbldap_get_ldap(ldap_state
->smbldap_state
),
760 get_userattr_key2string(ldap_state
->schema_ver
,
764 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
767 munged_dial
= smbldap_talloc_single_attribute(
768 smbldap_get_ldap(ldap_state
->smbldap_state
),
770 get_userattr_key2string(ldap_state
->schema_ver
,
771 LDAP_ATTR_MUNGED_DIAL
),
774 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
777 /* FIXME: hours stuff should be cleaner */
781 memset(hours
, 0xff, hours_len
);
783 if (ldap_state
->is_nds_ldap
) {
786 char clear_text_pw
[512];
788 /* Make call to Novell eDirectory ldap extension to get clear text password.
789 NOTE: This will only work if we have an SSL connection to eDirectory. */
790 user_dn
= smbldap_talloc_dn(
791 ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
),
793 if (user_dn
!= NULL
) {
794 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
796 pwd_len
= sizeof(clear_text_pw
);
797 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
798 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
799 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
800 TALLOC_FREE(user_dn
);
803 ZERO_STRUCT(smblmpwd
);
804 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
805 TALLOC_FREE(user_dn
);
808 ZERO_STRUCT(smbntpwd
);
809 use_samba_attrs
= False
;
812 TALLOC_FREE(user_dn
);
815 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
819 if (use_samba_attrs
) {
820 temp
= smbldap_talloc_single_attribute(
821 smbldap_get_ldap(ldap_state
->smbldap_state
),
823 get_userattr_key2string(ldap_state
->schema_ver
,
827 pdb_gethexpwd(temp
, smblmpwd
);
828 memset((char *)temp
, '\0', strlen(temp
)+1);
829 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
832 ZERO_STRUCT(smblmpwd
);
835 temp
= smbldap_talloc_single_attribute(
836 smbldap_get_ldap(ldap_state
->smbldap_state
),
838 get_userattr_key2string(ldap_state
->schema_ver
,
842 pdb_gethexpwd(temp
, smbntpwd
);
843 memset((char *)temp
, '\0', strlen(temp
)+1);
844 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
847 ZERO_STRUCT(smbntpwd
);
853 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
855 uint8_t *pwhist
= NULL
;
857 char *history_string
= talloc_array(ctx
, char,
858 MAX_PW_HISTORY_LEN
*64);
860 if (!history_string
) {
864 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
866 pwhist
= talloc_zero_array(ctx
, uint8_t,
867 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
868 if (pwhist
== NULL
) {
869 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
873 if (smbldap_get_single_attribute(
874 smbldap_get_ldap(ldap_state
->smbldap_state
),
876 get_userattr_key2string(ldap_state
->schema_ver
,
877 LDAP_ATTR_PWD_HISTORY
),
879 MAX_PW_HISTORY_LEN
*64)) {
880 bool hex_failed
= false;
881 for (i
= 0; i
< pwHistLen
; i
++){
882 /* Get the 16 byte salt. */
883 if (!pdb_gethexpwd(&history_string
[i
*64],
884 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
888 /* Get the 16 byte MD5 hash of salt+passwd. */
889 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
890 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
891 PW_HISTORY_SALT_LEN
])) {
897 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
899 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
902 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
907 temp
= smbldap_talloc_single_attribute(
908 smbldap_get_ldap(ldap_state
->smbldap_state
),
910 get_userattr_key2string(ldap_state
->schema_ver
,
914 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
916 if (acct_ctrl
== 0) {
917 acct_ctrl
|= ACB_NORMAL
;
920 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
922 acct_ctrl
|= ACB_NORMAL
;
925 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
926 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
928 temp
= smbldap_talloc_single_attribute(
929 smbldap_get_ldap(ldap_state
->smbldap_state
),
931 get_userattr_key2string(ldap_state
->schema_ver
,
932 LDAP_ATTR_BAD_PASSWORD_COUNT
),
935 bad_password_count
= (uint32_t) atol(temp
);
936 pdb_set_bad_password_count(sampass
,
937 bad_password_count
, PDB_SET
);
940 temp
= smbldap_talloc_single_attribute(
941 smbldap_get_ldap(ldap_state
->smbldap_state
),
943 get_userattr_key2string(ldap_state
->schema_ver
,
944 LDAP_ATTR_BAD_PASSWORD_TIME
),
947 bad_password_time
= (time_t) atol(temp
);
948 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
952 temp
= smbldap_talloc_single_attribute(
953 smbldap_get_ldap(ldap_state
->smbldap_state
),
955 get_userattr_key2string(ldap_state
->schema_ver
,
956 LDAP_ATTR_LOGON_COUNT
),
959 logon_count
= (uint32_t) atol(temp
);
960 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
963 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
965 temp
= smbldap_talloc_single_attribute(
966 smbldap_get_ldap(ldap_state
->smbldap_state
),
968 get_userattr_key2string(ldap_state
->schema_ver
,
969 LDAP_ATTR_LOGON_HOURS
),
972 pdb_gethexhours(temp
, hours
);
973 memset((char *)temp
, '\0', strlen(temp
) +1);
974 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
978 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
979 struct passwd unix_pw
;
980 bool have_uid
= false;
981 bool have_gid
= false;
982 struct dom_sid mapped_gsid
;
983 const struct dom_sid
*primary_gsid
;
987 ZERO_STRUCT(unix_pw
);
989 unix_pw
.pw_name
= username
;
990 unix_pw
.pw_passwd
= discard_const_p(char, "x");
992 temp
= smbldap_talloc_single_attribute(
998 /* We've got a uid, feed the cache */
999 unix_pw
.pw_uid
= smb_strtoul(temp
,
1005 DBG_ERR("Failed to convert UID\n");
1010 temp
= smbldap_talloc_single_attribute(
1011 priv2ld(ldap_state
),
1016 /* We've got a uid, feed the cache */
1017 unix_pw
.pw_gid
= smb_strtoul(temp
,
1023 DBG_ERR("Failed to convert GID\n");
1028 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1029 priv2ld(ldap_state
),
1033 if (unix_pw
.pw_gecos
== NULL
) {
1034 unix_pw
.pw_gecos
= fullname
;
1036 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state
),
1041 if (unix_pw
.pw_dir
== NULL
) {
1042 unix_pw
.pw_dir
= discard_const_p(char, "");
1044 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1045 priv2ld(ldap_state
),
1049 if (unix_pw
.pw_shell
== NULL
) {
1050 unix_pw
.pw_shell
= discard_const_p(char, "");
1053 if (have_uid
&& have_gid
) {
1054 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1056 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1059 if (sampass
->unix_pw
== NULL
) {
1060 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1061 pdb_get_username(sampass
)));
1065 id
.id
= sampass
->unix_pw
->pw_uid
;
1066 id
.type
= ID_TYPE_UID
;
1068 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass
), &id
);
1070 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1071 primary_gsid
= pdb_get_group_sid(sampass
);
1072 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1073 id
.id
= sampass
->unix_pw
->pw_gid
;
1074 id
.type
= ID_TYPE_GID
;
1076 idmap_cache_set_sid2unixid(primary_gsid
, &id
);
1080 /* check the timestamp of the cache vs ldap entry */
1081 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1087 /* see if we have newer updates */
1088 if (!login_cache_read(sampass
, &cache_entry
)) {
1089 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1090 (unsigned int)pdb_get_bad_password_count(sampass
),
1091 (unsigned int)pdb_get_bad_password_time(sampass
)));
1096 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1097 (unsigned int)ldap_entry_time
,
1098 (unsigned int)cache_entry
.entry_timestamp
,
1099 (unsigned int)cache_entry
.bad_password_time
));
1101 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1102 /* cache is older than directory , so
1103 we need to delete the entry but allow the
1104 fields to be written out */
1105 login_cache_delentry(sampass
);
1108 pdb_set_acct_ctrl(sampass
,
1109 pdb_get_acct_ctrl(sampass
) |
1110 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1112 pdb_set_bad_password_count(sampass
,
1113 cache_entry
.bad_password_count
,
1115 pdb_set_bad_password_time(sampass
,
1116 cache_entry
.bad_password_time
,
1128 /**********************************************************************
1129 Initialize the ldap db from a struct samu. Called on update.
1130 (Based on init_buffer_from_sam in pdb_tdb.c)
1131 *********************************************************************/
1133 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1134 LDAPMessage
*existing
,
1135 LDAPMod
*** mods
, struct samu
* sampass
,
1136 bool (*need_update
)(const struct samu
*,
1141 if (mods
== NULL
|| sampass
== NULL
) {
1142 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1149 * took out adding "objectclass: sambaAccount"
1150 * do this on a per-mod basis
1152 if (need_update(sampass
, PDB_USERNAME
)) {
1153 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1155 "uid", pdb_get_username(sampass
));
1156 if (ldap_state
->is_nds_ldap
) {
1158 smbldap_get_ldap(ldap_state
->smbldap_state
),
1160 "cn", pdb_get_username(sampass
));
1162 smbldap_get_ldap(ldap_state
->smbldap_state
),
1164 "sn", pdb_get_username(sampass
));
1168 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1170 /* only update the RID if we actually need to */
1171 if (need_update(sampass
, PDB_USERSID
)) {
1172 struct dom_sid_buf sid_str
;
1173 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1175 switch ( ldap_state
->schema_ver
) {
1176 case SCHEMAVER_SAMBASAMACCOUNT
:
1179 ldap_state
->smbldap_state
),
1181 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1182 dom_sid_str_buf(user_sid
, &sid_str
));
1186 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1191 /* we don't need to store the primary group RID - so leaving it
1192 'free' to hang off the unix primary group makes life easier */
1194 if (need_update(sampass
, PDB_GROUPSID
)) {
1195 struct dom_sid_buf sid_str
;
1196 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1198 switch ( ldap_state
->schema_ver
) {
1199 case SCHEMAVER_SAMBASAMACCOUNT
:
1202 ldap_state
->smbldap_state
),
1204 get_userattr_key2string(ldap_state
->schema_ver
,
1205 LDAP_ATTR_PRIMARY_GROUP_SID
),
1206 dom_sid_str_buf(group_sid
, &sid_str
));
1210 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1216 /* displayName, cn, and gecos should all be the same
1217 * most easily accomplished by giving them the same OID
1218 * gecos isn't set here b/c it should be handled by the
1220 * We change displayName only and fall back to cn if
1221 * it does not exist.
1224 if (need_update(sampass
, PDB_FULLNAME
))
1225 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1227 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1228 pdb_get_fullname(sampass
));
1230 if (need_update(sampass
, PDB_ACCTDESC
))
1231 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1233 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1234 pdb_get_acct_desc(sampass
));
1236 if (need_update(sampass
, PDB_WORKSTATIONS
))
1237 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1239 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1240 pdb_get_workstations(sampass
));
1242 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1243 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1245 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1246 pdb_get_munged_dial(sampass
));
1248 if (need_update(sampass
, PDB_SMBHOME
))
1249 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1251 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1252 pdb_get_homedir(sampass
));
1254 if (need_update(sampass
, PDB_DRIVE
))
1255 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1257 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1258 pdb_get_dir_drive(sampass
));
1260 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1261 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1263 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1264 pdb_get_logon_script(sampass
));
1266 if (need_update(sampass
, PDB_PROFILE
))
1267 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1269 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1270 pdb_get_profile_path(sampass
));
1272 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1275 if (need_update(sampass
, PDB_LOGONTIME
))
1276 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1278 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1281 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1284 if (need_update(sampass
, PDB_LOGOFFTIME
))
1285 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1287 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1290 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1293 if (need_update(sampass
, PDB_KICKOFFTIME
))
1294 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1296 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1299 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1302 if (need_update(sampass
, PDB_CANCHANGETIME
))
1303 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1305 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1308 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1309 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1311 if (need_update(sampass
, PDB_LMPASSWD
)) {
1312 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1315 pdb_sethexpwd(pwstr
, lm_pw
,
1316 pdb_get_acct_ctrl(sampass
));
1319 ldap_state
->smbldap_state
),
1321 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1326 ldap_state
->smbldap_state
),
1328 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1332 if (need_update(sampass
, PDB_NTPASSWD
)) {
1333 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1336 pdb_sethexpwd(pwstr
, nt_pw
,
1337 pdb_get_acct_ctrl(sampass
));
1340 ldap_state
->smbldap_state
),
1342 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1347 ldap_state
->smbldap_state
),
1349 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1354 if (need_update(sampass
, PDB_PWHISTORY
)) {
1356 uint32_t pwHistLen
= 0;
1357 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1359 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1363 if (pwHistLen
== 0) {
1364 /* Remove any password history from the LDAP store. */
1365 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1369 uint32_t currHistLen
= 0;
1370 const uint8_t *pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1371 if (pwhist
!= NULL
) {
1372 /* We can only store (1024-1/64 password history entries. */
1373 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1374 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1375 /* Store the salt. */
1376 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1377 /* Followed by the md5 hash of salt + md4 hash */
1378 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1379 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1380 DEBUG(100, ("pwstr=%s\n", pwstr
));
1385 smbldap_get_ldap(ldap_state
->smbldap_state
),
1387 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1392 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1393 if (asprintf(&temp
, "%li",
1394 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1398 smbldap_get_ldap(ldap_state
->smbldap_state
),
1400 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1406 if (need_update(sampass
, PDB_HOURS
)) {
1407 const uint8_t *hours
= pdb_get_hours(sampass
);
1410 pdb_sethexhours(hourstr
, hours
);
1412 smbldap_get_ldap(ldap_state
->smbldap_state
),
1415 get_userattr_key2string(ldap_state
->schema_ver
,
1416 LDAP_ATTR_LOGON_HOURS
),
1421 if (need_update(sampass
, PDB_ACCTCTRL
))
1423 smbldap_get_ldap(ldap_state
->smbldap_state
),
1425 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1426 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1428 /* password lockout cache:
1429 - If we are now autolocking or clearing, we write to ldap
1430 - If we are clearing, we delete the cache entry
1431 - If the count is > 0, we update the cache
1433 This even means when autolocking, we cache, just in case the
1434 update doesn't work, and we have to cache the autolock flag */
1436 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1437 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1438 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1439 time_t badtime
= pdb_get_bad_password_time(sampass
);
1441 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1443 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1444 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1446 if ((badcount
>= pol
) || (badcount
== 0)) {
1447 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1448 (unsigned int)badcount
, (unsigned int)badtime
));
1449 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1453 smbldap_get_ldap(ldap_state
->smbldap_state
),
1455 get_userattr_key2string(
1456 ldap_state
->schema_ver
,
1457 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1461 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1465 smbldap_get_ldap(ldap_state
->smbldap_state
),
1467 get_userattr_key2string(
1468 ldap_state
->schema_ver
,
1469 LDAP_ATTR_BAD_PASSWORD_TIME
),
1473 if (badcount
== 0) {
1474 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1475 login_cache_delentry(sampass
);
1477 struct login_cache cache_entry
;
1479 cache_entry
.entry_timestamp
= time(NULL
);
1480 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1481 cache_entry
.bad_password_count
= badcount
;
1482 cache_entry
.bad_password_time
= badtime
;
1484 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1485 login_cache_write(sampass
, &cache_entry
);
1492 /**********************************************************************
1493 End enumeration of the LDAP password list.
1494 *********************************************************************/
1496 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1498 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1499 if (ldap_state
->result
) {
1500 ldap_msgfree(ldap_state
->result
);
1501 ldap_state
->result
= NULL
;
1505 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1506 const char *new_attr
)
1510 if (new_attr
== NULL
) {
1514 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1518 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1520 SMB_ASSERT((*attr_list
) != NULL
);
1521 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1522 (*attr_list
)[i
+1] = NULL
;
1525 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1526 const char ***attr_list
)
1528 append_attr(mem_ctx
, attr_list
, "uidNumber");
1529 append_attr(mem_ctx
, attr_list
, "gidNumber");
1530 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1531 append_attr(mem_ctx
, attr_list
, "loginShell");
1532 append_attr(mem_ctx
, attr_list
, "gecos");
1535 /**********************************************************************
1536 Get struct samu entry from LDAP by username.
1537 *********************************************************************/
1539 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1541 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1542 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1543 LDAPMessage
*result
= NULL
;
1544 LDAPMessage
*entry
= NULL
;
1546 const char ** attr_list
;
1549 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1550 append_attr(user
, &attr_list
,
1551 get_userattr_key2string(ldap_state
->schema_ver
,
1552 LDAP_ATTR_MOD_TIMESTAMP
));
1553 ldapsam_add_unix_attributes(user
, &attr_list
);
1554 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1556 TALLOC_FREE( attr_list
);
1558 if ( rc
!= LDAP_SUCCESS
)
1559 return NT_STATUS_NO_SUCH_USER
;
1561 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1565 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1566 ldap_msgfree(result
);
1567 return NT_STATUS_NO_SUCH_USER
;
1568 } else if (count
> 1) {
1569 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1570 ldap_msgfree(result
);
1571 return NT_STATUS_NO_SUCH_USER
;
1574 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1577 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1578 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1579 ldap_msgfree(result
);
1580 return NT_STATUS_NO_SUCH_USER
;
1582 pdb_set_backend_private_data(user
, result
, NULL
,
1583 my_methods
, PDB_CHANGED
);
1584 smbldap_talloc_autofree_ldapmsg(user
, result
);
1587 ldap_msgfree(result
);
1592 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1593 const struct dom_sid
*sid
, LDAPMessage
**result
)
1596 const char ** attr_list
;
1598 switch ( ldap_state
->schema_ver
) {
1599 case SCHEMAVER_SAMBASAMACCOUNT
: {
1600 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1601 if (tmp_ctx
== NULL
) {
1602 return LDAP_NO_MEMORY
;
1605 attr_list
= get_userattr_list(tmp_ctx
,
1606 ldap_state
->schema_ver
);
1607 append_attr(tmp_ctx
, &attr_list
,
1608 get_userattr_key2string(
1609 ldap_state
->schema_ver
,
1610 LDAP_ATTR_MOD_TIMESTAMP
));
1611 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1612 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1614 TALLOC_FREE(tmp_ctx
);
1616 if ( rc
!= LDAP_SUCCESS
)
1622 DEBUG(0,("Invalid schema version specified\n"));
1628 /**********************************************************************
1629 Get struct samu entry from LDAP by SID.
1630 *********************************************************************/
1632 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1634 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1635 LDAPMessage
*result
= NULL
;
1636 LDAPMessage
*entry
= NULL
;
1640 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1642 if (rc
!= LDAP_SUCCESS
)
1643 return NT_STATUS_NO_SUCH_USER
;
1645 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1649 struct dom_sid_buf buf
;
1650 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1652 dom_sid_str_buf(sid
, &buf
),
1654 ldap_msgfree(result
);
1655 return NT_STATUS_NO_SUCH_USER
;
1656 } else if (count
> 1) {
1657 struct dom_sid_buf buf
;
1658 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1659 "[%s]. Failing. count=%d\n",
1660 dom_sid_str_buf(sid
, &buf
),
1662 ldap_msgfree(result
);
1663 return NT_STATUS_NO_SUCH_USER
;
1666 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1669 ldap_msgfree(result
);
1670 return NT_STATUS_NO_SUCH_USER
;
1673 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1674 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1675 ldap_msgfree(result
);
1676 return NT_STATUS_NO_SUCH_USER
;
1679 pdb_set_backend_private_data(user
, result
, NULL
,
1680 my_methods
, PDB_CHANGED
);
1681 smbldap_talloc_autofree_ldapmsg(user
, result
);
1682 return NT_STATUS_OK
;
1685 /********************************************************************
1686 Do the actual modification - also change a plaintext passord if
1688 **********************************************************************/
1690 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1691 struct samu
*newpwd
, char *dn
,
1692 LDAPMod
**mods
, int ldap_op
,
1693 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1695 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1698 if (!newpwd
|| !dn
) {
1699 return NT_STATUS_INVALID_PARAMETER
;
1702 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1703 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1704 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1705 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1708 char *retoid
= NULL
;
1709 struct berval
*retdata
= NULL
;
1710 char *utf8_password
;
1712 size_t converted_size
;
1715 if (!ldap_state
->is_nds_ldap
) {
1717 if (!smbldap_has_extension(
1719 ldap_state
->smbldap_state
),
1720 LDAP_EXOP_MODIFY_PASSWD
)) {
1721 DEBUG(2, ("ldap password change requested, but LDAP "
1722 "server does not support it -- ignoring\n"));
1723 return NT_STATUS_OK
;
1727 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1728 pdb_get_plaintext_passwd(newpwd
),
1731 return NT_STATUS_NO_MEMORY
;
1734 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1735 TALLOC_FREE(utf8_password
);
1736 return NT_STATUS_NO_MEMORY
;
1739 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1740 DEBUG(0,("ber_alloc_t returns NULL\n"));
1741 TALLOC_FREE(utf8_password
);
1742 TALLOC_FREE(utf8_dn
);
1743 return NT_STATUS_UNSUCCESSFUL
;
1746 if ((ber_printf (ber
, "{") < 0) ||
1747 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1749 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1752 TALLOC_FREE(utf8_dn
);
1753 TALLOC_FREE(utf8_password
);
1754 return NT_STATUS_UNSUCCESSFUL
;
1757 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1758 ret
= ber_printf(ber
, "ts}",
1759 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1762 ret
= ber_printf(ber
, "}");
1766 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1769 TALLOC_FREE(utf8_dn
);
1770 TALLOC_FREE(utf8_password
);
1771 return NT_STATUS_UNSUCCESSFUL
;
1774 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1775 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1777 TALLOC_FREE(utf8_dn
);
1778 TALLOC_FREE(utf8_password
);
1779 return NT_STATUS_UNSUCCESSFUL
;
1782 TALLOC_FREE(utf8_dn
);
1783 TALLOC_FREE(utf8_password
);
1786 if (!ldap_state
->is_nds_ldap
) {
1787 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1788 LDAP_EXOP_MODIFY_PASSWD
,
1789 bv
, NULL
, NULL
, &retoid
,
1792 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1793 pdb_get_plaintext_passwd(newpwd
));
1795 if (rc
!= LDAP_SUCCESS
) {
1796 char *ld_error
= NULL
;
1798 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1799 DEBUG(3, ("Could not set userPassword "
1800 "attribute due to an objectClass "
1801 "violation -- ignoring\n"));
1803 return NT_STATUS_OK
;
1807 smbldap_get_ldap(ldap_state
->smbldap_state
),
1808 LDAP_OPT_ERROR_STRING
,
1810 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1811 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1812 SAFE_FREE(ld_error
);
1814 #if defined(LDAP_CONSTRAINT_VIOLATION)
1815 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1816 return NT_STATUS_PASSWORD_RESTRICTION
;
1818 return NT_STATUS_UNSUCCESSFUL
;
1820 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1821 #ifdef DEBUG_PASSWORD
1822 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1825 ber_bvfree(retdata
);
1827 ldap_memfree(retoid
);
1833 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1834 /* may be password change below however */
1838 if (ldap_state
->is_nds_ldap
) {
1839 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1843 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1847 rc
= smbldap_add(ldap_state
->smbldap_state
,
1850 case LDAP_MOD_REPLACE
:
1851 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1855 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1857 return NT_STATUS_INVALID_PARAMETER
;
1860 if (rc
!=LDAP_SUCCESS
) {
1861 return NT_STATUS_UNSUCCESSFUL
;
1865 return NT_STATUS_OK
;
1868 /**********************************************************************
1869 Delete entry from LDAP for username.
1870 *********************************************************************/
1872 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1873 struct samu
* sam_acct
)
1875 struct ldapsam_privates
*priv
=
1876 (struct ldapsam_privates
*)my_methods
->private_data
;
1879 LDAPMessage
*msg
, *entry
;
1880 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1881 const char **attr_list
;
1882 TALLOC_CTX
*mem_ctx
;
1885 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1886 return NT_STATUS_INVALID_PARAMETER
;
1889 sname
= pdb_get_username(sam_acct
);
1891 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1894 mem_ctx
= talloc_new(NULL
);
1895 if (mem_ctx
== NULL
) {
1896 DEBUG(0, ("talloc_new failed\n"));
1900 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1901 if (attr_list
== NULL
) {
1905 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1907 if ((rc
!= LDAP_SUCCESS
) ||
1908 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1909 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1910 DEBUG(5, ("Could not find user %s\n", sname
));
1911 result
= NT_STATUS_NO_SUCH_USER
;
1915 rc
= ldapsam_delete_entry(
1916 priv
, mem_ctx
, entry
,
1917 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1918 LDAP_OBJ_SAMBASAMACCOUNT
: 0,
1921 result
= (rc
== LDAP_SUCCESS
) ?
1922 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1925 TALLOC_FREE(mem_ctx
);
1929 /**********************************************************************
1931 *********************************************************************/
1933 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1935 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1936 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1939 LDAPMessage
*result
= NULL
;
1940 LDAPMessage
*entry
= NULL
;
1941 LDAPMod
**mods
= NULL
;
1942 const char **attr_list
;
1944 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1946 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1947 if (pdb_get_username(newpwd
) == NULL
) {
1948 return NT_STATUS_INVALID_PARAMETER
;
1950 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1951 TALLOC_FREE( attr_list
);
1952 if (rc
!= LDAP_SUCCESS
) {
1953 return NT_STATUS_UNSUCCESSFUL
;
1955 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1956 my_methods
, PDB_CHANGED
);
1957 smbldap_talloc_autofree_ldapmsg(newpwd
, result
);
1960 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1962 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1963 return NT_STATUS_UNSUCCESSFUL
;
1966 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1968 dn
= smbldap_talloc_dn(talloc_tos(),
1969 smbldap_get_ldap(ldap_state
->smbldap_state
),
1972 return NT_STATUS_UNSUCCESSFUL
;
1975 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1977 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1978 pdb_element_is_changed
)) {
1979 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1982 ldap_mods_free(mods
,True
);
1983 return NT_STATUS_UNSUCCESSFUL
;
1986 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1987 && (mods
== NULL
)) {
1988 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1989 pdb_get_username(newpwd
)));
1991 return NT_STATUS_OK
;
1994 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
1997 ldap_mods_free(mods
,True
);
2003 * We need to set the backend private data to NULL here. For example
2004 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2005 * same one, and with the explicit delete / add logic for attribute
2006 * values the second time we would use the wrong "old" value which
2007 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2009 * The existing LDAPMessage is still being auto-freed by the
2012 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2015 if (!NT_STATUS_IS_OK(ret
)) {
2019 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2020 pdb_get_username(newpwd
)));
2021 return NT_STATUS_OK
;
2024 /***************************************************************************
2025 Renames a struct samu
2026 - The "rename user script" has full responsibility for changing everything
2027 ***************************************************************************/
2029 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2030 TALLOC_CTX
*tmp_ctx
,
2032 uint32_t member_rid
);
2034 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2035 TALLOC_CTX
*mem_ctx
,
2037 struct dom_sid
**pp_sids
,
2039 uint32_t *p_num_groups
);
2041 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2042 struct samu
*old_acct
,
2043 const char *newname
)
2045 const char *oldname
;
2047 char *rename_script
= NULL
;
2048 fstring oldname_lower
, newname_lower
;
2051 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2052 return NT_STATUS_INVALID_PARAMETER
;
2055 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2056 return NT_STATUS_INVALID_PARAMETER
;
2059 oldname
= pdb_get_username(old_acct
);
2061 /* rename the posix user */
2062 rename_script
= lp_rename_user_script(talloc_tos());
2063 if (rename_script
== NULL
) {
2064 return NT_STATUS_NO_MEMORY
;
2067 if (!(*rename_script
)) {
2068 TALLOC_FREE(rename_script
);
2069 return NT_STATUS_ACCESS_DENIED
;
2072 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2075 /* We have to allow the account name to end with a '$'.
2076 Also, follow the semantics in _samr_create_user() and lower case the
2077 posix name but preserve the case in passdb */
2079 fstrcpy( oldname_lower
, oldname
);
2080 if (!strlower_m( oldname_lower
)) {
2081 return NT_STATUS_INVALID_PARAMETER
;
2083 fstrcpy( newname_lower
, newname
);
2084 if (!strlower_m( newname_lower
)) {
2085 return NT_STATUS_INVALID_PARAMETER
;
2088 rename_script
= realloc_string_sub2(rename_script
,
2093 if (!rename_script
) {
2094 return NT_STATUS_NO_MEMORY
;
2096 rename_script
= realloc_string_sub2(rename_script
,
2101 rc
= smbrun(rename_script
, NULL
, NULL
);
2103 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2104 rename_script
, rc
));
2106 TALLOC_FREE(rename_script
);
2109 smb_nscd_flush_user_cache();
2113 return NT_STATUS_UNSUCCESSFUL
;
2115 return NT_STATUS_OK
;
2118 /**********************************************************************
2119 Add struct samu to LDAP.
2120 *********************************************************************/
2122 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2124 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2125 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2127 LDAPMessage
*result
= NULL
;
2128 LDAPMessage
*entry
= NULL
;
2129 LDAPMod
**mods
= NULL
;
2130 int ldap_op
= LDAP_MOD_REPLACE
;
2131 uint32_t num_result
;
2132 const char **attr_list
;
2133 char *escape_user
= NULL
;
2134 const char *username
= pdb_get_username(newpwd
);
2135 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2136 char *filter
= NULL
;
2138 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2139 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2142 return NT_STATUS_NO_MEMORY
;
2145 if (!username
|| !*username
) {
2146 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2147 status
= NT_STATUS_INVALID_PARAMETER
;
2151 /* free this list after the second search or in case we exit on failure */
2152 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2154 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2156 if (rc
!= LDAP_SUCCESS
) {
2160 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
2162 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2166 ldap_msgfree(result
);
2169 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2170 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2172 if (rc
== LDAP_SUCCESS
) {
2173 if (ldap_count_entries(
2175 ldap_state
->smbldap_state
),
2177 struct dom_sid_buf buf
;
2178 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2179 "already in the base, with samba "
2181 dom_sid_str_buf(sid
, &buf
)));
2184 ldap_msgfree(result
);
2189 /* does the entry already exist but without a samba attributes?
2190 we need to return the samba attributes here */
2192 escape_user
= escape_ldap_string(talloc_tos(), username
);
2193 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2195 status
= NT_STATUS_NO_MEMORY
;
2198 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2199 TALLOC_FREE(escape_user
);
2201 status
= NT_STATUS_NO_MEMORY
;
2205 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2206 filter
, attr_list
, &result
);
2207 if ( rc
!= LDAP_SUCCESS
) {
2211 num_result
= ldap_count_entries(
2212 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2214 if (num_result
> 1) {
2215 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2219 /* Check if we need to update an existing entry */
2220 if (num_result
== 1) {
2221 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2222 ldap_op
= LDAP_MOD_REPLACE
;
2223 entry
= ldap_first_entry(
2224 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2225 dn
= smbldap_talloc_dn(
2226 ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
),
2229 status
= NT_STATUS_NO_MEMORY
;
2233 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2235 struct dom_sid_buf buf
;
2237 /* There might be a SID for this account already - say an idmap entry */
2239 filter
= talloc_asprintf(ctx
,
2240 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2241 get_userattr_key2string(ldap_state
->schema_ver
,
2242 LDAP_ATTR_USER_SID
),
2243 dom_sid_str_buf(sid
, &buf
),
2244 LDAP_OBJ_IDMAP_ENTRY
,
2245 LDAP_OBJ_SID_ENTRY
);
2247 status
= NT_STATUS_NO_MEMORY
;
2251 /* free old result before doing a new search */
2252 if (result
!= NULL
) {
2253 ldap_msgfree(result
);
2256 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2257 filter
, attr_list
, &result
);
2259 if ( rc
!= LDAP_SUCCESS
) {
2263 num_result
= ldap_count_entries(
2264 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2266 if (num_result
> 1) {
2267 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2271 /* Check if we need to update an existing entry */
2272 if (num_result
== 1) {
2274 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2275 ldap_op
= LDAP_MOD_REPLACE
;
2276 entry
= ldap_first_entry (
2277 smbldap_get_ldap(ldap_state
->smbldap_state
),
2279 dn
= smbldap_talloc_dn (
2281 smbldap_get_ldap(ldap_state
->smbldap_state
),
2284 status
= NT_STATUS_NO_MEMORY
;
2290 if (num_result
== 0) {
2291 char *escape_username
;
2292 /* Check if we need to add an entry */
2293 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2294 ldap_op
= LDAP_MOD_ADD
;
2296 escape_username
= escape_rdn_val_string_alloc(username
);
2297 if (!escape_username
) {
2298 status
= NT_STATUS_NO_MEMORY
;
2302 if (username
[strlen(username
)-1] == '$') {
2303 dn
= talloc_asprintf(ctx
,
2306 lp_ldap_machine_suffix(talloc_tos()));
2308 dn
= talloc_asprintf(ctx
,
2311 lp_ldap_user_suffix(talloc_tos()));
2314 SAFE_FREE(escape_username
);
2316 status
= NT_STATUS_NO_MEMORY
;
2321 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2322 pdb_element_is_set_or_changed
)) {
2323 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2325 ldap_mods_free(mods
, true);
2331 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2334 switch ( ldap_state
->schema_ver
) {
2335 case SCHEMAVER_SAMBASAMACCOUNT
:
2336 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2339 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2343 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2344 if (!NT_STATUS_IS_OK(ret
)) {
2345 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2346 pdb_get_username(newpwd
),dn
));
2347 ldap_mods_free(mods
, true);
2351 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2352 ldap_mods_free(mods
, true);
2354 status
= NT_STATUS_OK
;
2360 ldap_msgfree(result
);
2365 /**********************************************************************
2366 *********************************************************************/
2368 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2370 LDAPMessage
** result
)
2372 int scope
= LDAP_SCOPE_SUBTREE
;
2374 const char **attr_list
;
2376 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2377 rc
= smbldap_search(ldap_state
->smbldap_state
,
2378 lp_ldap_suffix (talloc_tos()), scope
,
2379 filter
, attr_list
, 0, result
);
2380 TALLOC_FREE(attr_list
);
2385 /**********************************************************************
2386 *********************************************************************/
2388 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2389 GROUP_MAP
*map
, LDAPMessage
*entry
)
2392 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2394 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2395 smbldap_get_ldap(ldap_state
->smbldap_state
) == NULL
) {
2396 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2401 temp
= smbldap_talloc_single_attribute(
2402 smbldap_get_ldap(ldap_state
->smbldap_state
),
2404 get_attr_key2string(groupmap_attr_list
,
2405 LDAP_ATTR_GIDNUMBER
),
2408 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2409 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2413 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2415 map
->gid
= (gid_t
)atol(temp
);
2418 temp
= smbldap_talloc_single_attribute(
2419 smbldap_get_ldap(ldap_state
->smbldap_state
),
2421 get_attr_key2string(groupmap_attr_list
,
2422 LDAP_ATTR_GROUP_SID
),
2425 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2426 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2431 if (!string_to_sid(&map
->sid
, temp
)) {
2432 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2438 temp
= smbldap_talloc_single_attribute(
2439 smbldap_get_ldap(ldap_state
->smbldap_state
),
2441 get_attr_key2string(groupmap_attr_list
,
2442 LDAP_ATTR_GROUP_TYPE
),
2445 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2446 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2450 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2452 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2453 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2454 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2460 temp
= smbldap_talloc_single_attribute(
2461 smbldap_get_ldap(ldap_state
->smbldap_state
),
2463 get_attr_key2string(groupmap_attr_list
,
2464 LDAP_ATTR_DISPLAY_NAME
),
2467 temp
= smbldap_talloc_single_attribute(
2468 smbldap_get_ldap(ldap_state
->smbldap_state
),
2470 get_attr_key2string(groupmap_attr_list
,
2474 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2475 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2480 map
->nt_name
= talloc_strdup(map
, temp
);
2481 if (!map
->nt_name
) {
2487 temp
= smbldap_talloc_single_attribute(
2488 smbldap_get_ldap(ldap_state
->smbldap_state
),
2490 get_attr_key2string(groupmap_attr_list
,
2494 temp
= talloc_strdup(ctx
, "");
2500 map
->comment
= talloc_strdup(map
, temp
);
2501 if (!map
->comment
) {
2506 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2509 id
.type
= ID_TYPE_GID
;
2511 idmap_cache_set_sid2unixid(&map
->sid
, &id
);
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
;
2578 struct dom_sid_buf tmp
;
2580 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2582 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2583 dom_sid_str_buf(&sid
, &tmp
)) < 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 struct dom_sid_buf buf
;
2663 DEBUG(10, ("SID %s is not in expected domain %s\n",
2665 dom_sid_str_buf(domain_sid
, &buf
)));
2669 if (!sid_peek_rid(&sid
, rid
)) {
2670 DEBUG(10, ("Could not peek into RID\n"));
2677 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2678 TALLOC_CTX
*mem_ctx
,
2679 const struct dom_sid
*group
,
2680 uint32_t **pp_member_rids
,
2681 size_t *p_num_members
)
2683 struct ldapsam_privates
*ldap_state
=
2684 (struct ldapsam_privates
*)methods
->private_data
;
2685 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2686 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2687 const char *sid_attrs
[] = { "sambaSID", NULL
};
2688 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2689 LDAPMessage
*result
= NULL
;
2692 char **values
= NULL
;
2696 struct dom_sid_buf buf
;
2698 *pp_member_rids
= NULL
;
2701 filter
= talloc_asprintf(mem_ctx
,
2702 "(&(objectClass=%s)"
2705 LDAP_OBJ_POSIXGROUP
,
2707 dom_sid_str_buf(group
, &buf
));
2708 if (filter
== NULL
) {
2709 ret
= NT_STATUS_NO_MEMORY
;
2713 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2714 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2717 if (rc
!= LDAP_SUCCESS
)
2720 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2722 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2725 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2726 dom_sid_str_buf(group
, &buf
)));
2727 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2732 ret
= NT_STATUS_NO_SUCH_GROUP
;
2736 entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2740 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2742 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2743 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2747 values
= ldap_get_values(smbldap_get_ldap(conn
), entry
, "memberUid");
2749 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2751 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2752 if (filter
== NULL
) {
2753 ret
= NT_STATUS_NO_MEMORY
;
2757 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2758 char *escape_memberuid
;
2760 escape_memberuid
= escape_ldap_string(talloc_tos(),
2762 if (escape_memberuid
== NULL
) {
2763 ret
= NT_STATUS_NO_MEMORY
;
2767 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2768 TALLOC_FREE(escape_memberuid
);
2769 if (filter
== NULL
) {
2770 ret
= NT_STATUS_NO_MEMORY
;
2775 filter
= talloc_asprintf_append_buffer(filter
, "))");
2776 if (filter
== NULL
) {
2777 ret
= NT_STATUS_NO_MEMORY
;
2781 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2782 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2785 if (rc
!= LDAP_SUCCESS
)
2788 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2789 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2791 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2793 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2795 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2801 sidstr
= smbldap_talloc_single_attribute(
2802 smbldap_get_ldap(conn
), entry
, "sambaSID",
2805 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2806 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2807 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2811 if (!string_to_sid(&sid
, sidstr
))
2814 if (!sid_check_is_in_our_sam(&sid
)) {
2815 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2816 "in our domain\n"));
2817 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2821 sid_peek_rid(&sid
, &rid
);
2823 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2825 ret
= NT_STATUS_NO_MEMORY
;
2831 filter
= talloc_asprintf(mem_ctx
,
2832 "(&(objectClass=%s)"
2834 LDAP_OBJ_SAMBASAMACCOUNT
,
2837 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2838 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2841 if (rc
!= LDAP_SUCCESS
)
2844 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2846 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2848 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2852 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn
),
2854 get_global_sam_sid(),
2856 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2857 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2861 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2863 ret
= NT_STATUS_NO_MEMORY
;
2873 ldap_value_free(values
);
2878 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2879 TALLOC_CTX
*mem_ctx
,
2881 struct dom_sid
**pp_sids
,
2883 uint32_t *p_num_groups
)
2885 struct ldapsam_privates
*ldap_state
=
2886 (struct ldapsam_privates
*)methods
->private_data
;
2887 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2889 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2892 LDAPMessage
*result
= NULL
;
2894 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2898 gid_t primary_gid
= -1;
2904 if (pdb_get_username(user
) == NULL
) {
2905 return NT_STATUS_INVALID_PARAMETER
;
2908 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2909 if (escape_name
== NULL
)
2910 return NT_STATUS_NO_MEMORY
;
2912 if (user
->unix_pw
) {
2913 primary_gid
= user
->unix_pw
->pw_gid
;
2915 /* retrieve the users primary gid */
2916 filter
= talloc_asprintf(mem_ctx
,
2917 "(&(objectClass=%s)(uid=%s))",
2918 LDAP_OBJ_SAMBASAMACCOUNT
,
2920 if (filter
== NULL
) {
2921 ret
= NT_STATUS_NO_MEMORY
;
2925 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2926 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2928 if (rc
!= LDAP_SUCCESS
)
2931 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2933 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2937 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2938 ret
= NT_STATUS_NO_SUCH_USER
;
2941 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2943 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2945 DEBUG (1, ("Unable to find the member's gid!\n"));
2946 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2949 primary_gid
= smb_strtoul(gidstr
,
2955 DBG_ERR("Failed to convert GID\n");
2960 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2961 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2966 filter
= talloc_asprintf(mem_ctx
,
2967 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2968 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2969 if (filter
== NULL
) {
2970 ret
= NT_STATUS_NO_MEMORY
;
2974 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2975 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2977 if (rc
!= LDAP_SUCCESS
)
2980 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2988 /* We need to add the primary group as the first gid/sid */
2990 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2991 ret
= NT_STATUS_NO_MEMORY
;
2995 /* This sid will be replaced later */
2997 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2999 if (!NT_STATUS_IS_OK(ret
)) {
3003 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
3005 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
3011 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3013 str
, sizeof(str
)-1))
3016 if (!string_to_sid(&sid
, str
))
3019 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3021 str
, sizeof(str
)-1))
3024 gid
= smb_strtoul(str
, NULL
, 10, &error
, SMB_STR_FULL_STR_CONV
);
3030 if (gid
== primary_gid
) {
3031 sid_copy(&(*pp_sids
)[0], &sid
);
3033 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3035 ret
= NT_STATUS_NO_MEMORY
;
3038 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3040 if (!NT_STATUS_IS_OK(ret
)) {
3046 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3047 DEBUG(3, ("primary group of [%s] not found\n",
3048 pdb_get_username(user
)));
3049 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
3053 *p_num_groups
= num_sids
;
3059 TALLOC_FREE(escape_name
);
3063 /**********************************************************************
3064 * Augment a posixGroup object with a sambaGroupMapping domgroup
3065 *********************************************************************/
3067 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3068 struct ldapsam_privates
*ldap_state
,
3071 const char *filter
, *dn
;
3072 LDAPMessage
*msg
, *entry
;
3074 struct dom_sid_buf buf
;
3077 filter
= talloc_asprintf(mem_ctx
,
3078 "(&(objectClass=%s)(gidNumber=%u))",
3079 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3080 if (filter
== NULL
) {
3081 return NT_STATUS_NO_MEMORY
;
3084 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3085 get_attr_list(mem_ctx
, groupmap_attr_list
),
3087 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3089 if ((rc
!= LDAP_SUCCESS
) ||
3090 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3092 ((entry
= ldap_first_entry(
3093 smbldap_get_ldap(ldap_state
->smbldap_state
),
3095 return NT_STATUS_NO_SUCH_GROUP
;
3098 dn
= smbldap_talloc_dn(mem_ctx
,
3099 smbldap_get_ldap(ldap_state
->smbldap_state
),
3102 return NT_STATUS_NO_MEMORY
;
3106 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3108 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3110 dom_sid_str_buf(&map
->sid
, &buf
));
3111 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3112 &mods
, "sambaGroupType",
3113 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3114 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3115 &mods
, "displayName",
3117 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3118 &mods
, "description",
3120 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3122 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3123 if (rc
!= LDAP_SUCCESS
) {
3124 return NT_STATUS_ACCESS_DENIED
;
3127 return NT_STATUS_OK
;
3130 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3133 struct ldapsam_privates
*ldap_state
=
3134 (struct ldapsam_privates
*)methods
->private_data
;
3135 LDAPMessage
*msg
= NULL
;
3136 LDAPMod
**mods
= NULL
;
3137 const char *attrs
[] = { NULL
};
3141 TALLOC_CTX
*mem_ctx
;
3145 struct dom_sid_buf buf
;
3150 mem_ctx
= talloc_new(NULL
);
3151 if (mem_ctx
== NULL
) {
3152 DEBUG(0, ("talloc_new failed\n"));
3153 return NT_STATUS_NO_MEMORY
;
3156 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3157 dom_sid_str_buf(&map
->sid
, &buf
));
3158 if (filter
== NULL
) {
3159 result
= NT_STATUS_NO_MEMORY
;
3163 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3164 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3165 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3167 if ((rc
== LDAP_SUCCESS
) &&
3168 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3171 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3172 "group mapping entry\n",
3173 dom_sid_str_buf(&map
->sid
, &buf
)));
3174 result
= NT_STATUS_GROUP_EXISTS
;
3178 switch (map
->sid_name_use
) {
3180 case SID_NAME_DOM_GRP
:
3181 /* To map a domain group we need to have a posix group
3183 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3187 case SID_NAME_ALIAS
:
3188 if (!sid_check_is_in_our_sam(&map
->sid
)
3189 && !sid_check_is_in_builtin(&map
->sid
) )
3191 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3192 dom_sid_str_buf(&map
->sid
, &buf
)));
3193 result
= NT_STATUS_INVALID_PARAMETER
;
3199 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3200 sid_type_lookup(map
->sid_name_use
)));
3201 result
= NT_STATUS_INVALID_PARAMETER
;
3205 /* Domain groups have been mapped in a separate routine, we have to
3206 * create an alias now */
3208 if (map
->gid
== -1) {
3209 DEBUG(10, ("Refusing to map gid==-1\n"));
3210 result
= NT_STATUS_INVALID_PARAMETER
;
3215 id
.type
= ID_TYPE_GID
;
3217 if (pdb_id_to_sid(&id
, &sid
)) {
3218 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3220 (unsigned int)map
->gid
,
3221 dom_sid_str_buf(&sid
, &buf
)));
3222 result
= NT_STATUS_GROUP_EXISTS
;
3226 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3227 * the best we can get out of LDAP. */
3229 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3230 dom_sid_str_buf(&map
->sid
, &buf
),
3231 lp_ldap_group_suffix(talloc_tos()));
3233 result
= NT_STATUS_NO_MEMORY
;
3239 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3240 &mods
, "objectClass", LDAP_OBJ_SID_ENTRY
);
3241 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3242 &mods
, "objectClass", LDAP_OBJ_GROUPMAP
);
3243 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3245 dom_sid_str_buf(&map
->sid
, &buf
));
3246 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3247 &mods
, "sambaGroupType",
3248 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3249 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3250 &mods
, "displayName",
3252 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3253 &mods
, "description",
3255 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3257 talloc_asprintf(mem_ctx
, "%u",
3258 (unsigned int)map
->gid
));
3259 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3261 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3263 result
= (rc
== LDAP_SUCCESS
) ?
3264 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3267 TALLOC_FREE(mem_ctx
);
3271 /**********************************************************************
3272 * Update a group mapping entry. We're quite strict about what can be changed:
3273 * Only the description and displayname may be changed. It simply does not
3274 * make any sense to change the SID, gid or the type in a mapping.
3275 *********************************************************************/
3277 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3280 struct ldapsam_privates
*ldap_state
=
3281 (struct ldapsam_privates
*)methods
->private_data
;
3283 const char *filter
, *dn
;
3284 LDAPMessage
*msg
= NULL
;
3285 LDAPMessage
*entry
= NULL
;
3286 LDAPMod
**mods
= NULL
;
3287 TALLOC_CTX
*mem_ctx
;
3289 struct dom_sid_buf buf
;
3291 mem_ctx
= talloc_new(NULL
);
3292 if (mem_ctx
== NULL
) {
3293 DEBUG(0, ("talloc_new failed\n"));
3294 return NT_STATUS_NO_MEMORY
;
3297 /* Make 100% sure that sid, gid and type are not changed by looking up
3298 * exactly the values we're given in LDAP. */
3300 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3301 "(sambaSid=%s)(gidNumber=%u)"
3302 "(sambaGroupType=%d))",
3304 dom_sid_str_buf(&map
->sid
, &buf
),
3305 (unsigned int)map
->gid
, map
->sid_name_use
);
3306 if (filter
== NULL
) {
3307 result
= NT_STATUS_NO_MEMORY
;
3311 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3312 get_attr_list(mem_ctx
, groupmap_attr_list
),
3314 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3316 if ((rc
!= LDAP_SUCCESS
) ||
3317 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3319 ((entry
= ldap_first_entry(
3320 smbldap_get_ldap(ldap_state
->smbldap_state
),
3322 result
= NT_STATUS_NO_SUCH_GROUP
;
3326 dn
= smbldap_talloc_dn(
3327 mem_ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
), entry
);
3330 result
= NT_STATUS_NO_MEMORY
;
3335 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3336 &mods
, "displayName", map
->nt_name
);
3337 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3338 &mods
, "description", map
->comment
);
3339 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3342 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3343 "nothing to do\n"));
3344 result
= NT_STATUS_OK
;
3348 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3350 if (rc
!= LDAP_SUCCESS
) {
3351 result
= NT_STATUS_ACCESS_DENIED
;
3355 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3356 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3358 result
= NT_STATUS_OK
;
3361 TALLOC_FREE(mem_ctx
);
3365 /**********************************************************************
3366 *********************************************************************/
3368 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3371 struct ldapsam_privates
*priv
=
3372 (struct ldapsam_privates
*)methods
->private_data
;
3373 LDAPMessage
*msg
, *entry
;
3376 TALLOC_CTX
*mem_ctx
;
3378 struct dom_sid_buf buf
;
3380 mem_ctx
= talloc_new(NULL
);
3381 if (mem_ctx
== NULL
) {
3382 DEBUG(0, ("talloc_new failed\n"));
3383 return NT_STATUS_NO_MEMORY
;
3386 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3387 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3388 dom_sid_str_buf(&sid
, &buf
));
3389 if (filter
== NULL
) {
3390 result
= NT_STATUS_NO_MEMORY
;
3393 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3394 get_attr_list(mem_ctx
, groupmap_attr_list
),
3396 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3398 if ((rc
!= LDAP_SUCCESS
) ||
3399 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3400 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3401 result
= NT_STATUS_NO_SUCH_GROUP
;
3405 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3406 get_attr_list(mem_ctx
,
3407 groupmap_attr_list_to_delete
));
3409 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3410 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3411 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3412 const char *attrs
[] = { "sambaGroupType", "description",
3413 "displayName", "sambaSIDList",
3416 /* Second try. Don't delete the sambaSID attribute, this is
3417 for "old" entries that are tacked on a winbind
3420 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3421 LDAP_OBJ_GROUPMAP
, attrs
);
3424 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3425 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3426 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3427 const char *attrs
[] = { "sambaGroupType", "description",
3428 "displayName", "sambaSIDList",
3429 "gidNumber", NULL
};
3431 /* Third try. This is a post-3.0.21 alias (containing only
3432 * sambaSidEntry and sambaGroupMapping classes), we also have
3433 * to delete the gidNumber attribute, only the sambaSidEntry
3436 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3437 LDAP_OBJ_GROUPMAP
, attrs
);
3440 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3443 TALLOC_FREE(mem_ctx
);
3447 /**********************************************************************
3448 *********************************************************************/
3450 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3453 struct ldapsam_privates
*ldap_state
=
3454 (struct ldapsam_privates
*)my_methods
->private_data
;
3455 char *filter
= NULL
;
3457 const char **attr_list
;
3459 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3461 return NT_STATUS_NO_MEMORY
;
3463 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3464 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3465 LDAP_SCOPE_SUBTREE
, filter
,
3466 attr_list
, 0, &ldap_state
->result
);
3467 TALLOC_FREE(attr_list
);
3469 if (rc
!= LDAP_SUCCESS
) {
3470 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3471 ldap_err2string(rc
)));
3472 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3473 lp_ldap_suffix(talloc_tos()), filter
));
3474 ldap_msgfree(ldap_state
->result
);
3475 ldap_state
->result
= NULL
;
3476 TALLOC_FREE(filter
);
3477 return NT_STATUS_UNSUCCESSFUL
;
3480 TALLOC_FREE(filter
);
3482 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3484 smbldap_get_ldap(ldap_state
->smbldap_state
),
3485 ldap_state
->result
)));
3488 ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3489 ldap_state
->result
);
3490 ldap_state
->index
= 0;
3492 return NT_STATUS_OK
;
3495 /**********************************************************************
3496 *********************************************************************/
3498 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3500 ldapsam_endsampwent(my_methods
);
3503 /**********************************************************************
3504 *********************************************************************/
3506 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3509 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3510 struct ldapsam_privates
*ldap_state
=
3511 (struct ldapsam_privates
*)my_methods
->private_data
;
3515 if (!ldap_state
->entry
)
3518 ldap_state
->index
++;
3519 bret
= init_group_from_ldap(ldap_state
, map
,
3522 ldap_state
->entry
= ldap_next_entry(
3523 smbldap_get_ldap(ldap_state
->smbldap_state
),
3527 return NT_STATUS_OK
;
3530 /**********************************************************************
3531 *********************************************************************/
3533 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3534 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3535 GROUP_MAP
***pp_rmap
,
3536 size_t *p_num_entries
,
3539 GROUP_MAP
*map
= NULL
;
3545 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3546 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3548 return NT_STATUS_ACCESS_DENIED
;
3553 map
= talloc_zero(NULL
, GROUP_MAP
);
3555 return NT_STATUS_NO_MEMORY
;
3558 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3563 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3564 sid_name_use
!= map
->sid_name_use
) {
3565 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3566 "not of the requested type\n",
3570 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3571 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3572 "non mapped\n", map
->nt_name
));
3576 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3577 GROUP_MAP
*, entries
+ 1);
3579 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3580 "enlarge group map!\n"));
3581 return NT_STATUS_UNSUCCESSFUL
;
3584 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3589 ldapsam_endsamgrent(methods
);
3591 *p_num_entries
= entries
;
3593 return NT_STATUS_OK
;
3596 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3597 const struct dom_sid
*alias
,
3598 const struct dom_sid
*member
,
3601 struct ldapsam_privates
*ldap_state
=
3602 (struct ldapsam_privates
*)methods
->private_data
;
3604 LDAPMessage
*result
= NULL
;
3605 LDAPMessage
*entry
= NULL
;
3607 LDAPMod
**mods
= NULL
;
3609 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3610 struct dom_sid_buf tmp
;
3612 char *filter
= NULL
;
3614 if (sid_check_is_in_builtin(alias
)) {
3615 type
= SID_NAME_ALIAS
;
3618 if (sid_check_is_in_our_sam(alias
)) {
3619 type
= SID_NAME_ALIAS
;
3622 if (type
== SID_NAME_USE_NONE
) {
3623 struct dom_sid_buf buf
;
3624 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3625 dom_sid_str_buf(alias
, &buf
)));
3626 return NT_STATUS_NO_SUCH_ALIAS
;
3629 if (asprintf(&filter
,
3630 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3632 dom_sid_str_buf(alias
, &tmp
),
3634 return NT_STATUS_NO_MEMORY
;
3637 if (ldapsam_search_one_group(ldap_state
, filter
,
3638 &result
) != LDAP_SUCCESS
) {
3640 return NT_STATUS_NO_SUCH_ALIAS
;
3643 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3647 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3648 ldap_msgfree(result
);
3650 return NT_STATUS_NO_SUCH_ALIAS
;
3654 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3655 "filter %s: count=%d\n", filter
, count
));
3656 ldap_msgfree(result
);
3658 return NT_STATUS_NO_SUCH_ALIAS
;
3663 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3667 ldap_msgfree(result
);
3668 return NT_STATUS_UNSUCCESSFUL
;
3671 dn
= smbldap_talloc_dn(talloc_tos(),
3672 smbldap_get_ldap(ldap_state
->smbldap_state
),
3675 ldap_msgfree(result
);
3676 return NT_STATUS_UNSUCCESSFUL
;
3679 smbldap_set_mod(&mods
, modop
,
3680 get_attr_key2string(groupmap_attr_list
,
3681 LDAP_ATTR_SID_LIST
),
3682 dom_sid_str_buf(member
, &tmp
));
3684 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3686 ldap_mods_free(mods
, True
);
3687 ldap_msgfree(result
);
3690 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3691 return NT_STATUS_MEMBER_IN_ALIAS
;
3694 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3695 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3698 if (rc
!= LDAP_SUCCESS
) {
3699 return NT_STATUS_UNSUCCESSFUL
;
3702 return NT_STATUS_OK
;
3705 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3706 const struct dom_sid
*alias
,
3707 const struct dom_sid
*member
)
3709 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3712 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3713 const struct dom_sid
*alias
,
3714 const struct dom_sid
*member
)
3716 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3720 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3721 const struct dom_sid
*alias
,
3722 TALLOC_CTX
*mem_ctx
,
3723 struct dom_sid
**pp_members
,
3724 size_t *p_num_members
)
3726 struct ldapsam_privates
*ldap_state
=
3727 (struct ldapsam_privates
*)methods
->private_data
;
3728 LDAPMessage
*result
= NULL
;
3729 LDAPMessage
*entry
= NULL
;
3731 char **values
= NULL
;
3733 char *filter
= NULL
;
3734 uint32_t num_members
= 0;
3735 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3736 struct dom_sid_buf tmp
;
3741 if (sid_check_is_in_builtin(alias
)) {
3742 type
= SID_NAME_ALIAS
;
3745 if (sid_check_is_in_our_sam(alias
)) {
3746 type
= SID_NAME_ALIAS
;
3749 if (type
== SID_NAME_USE_NONE
) {
3750 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3751 dom_sid_str_buf(alias
, &tmp
)));
3752 return NT_STATUS_NO_SUCH_ALIAS
;
3755 if (asprintf(&filter
,
3756 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3758 dom_sid_str_buf(alias
, &tmp
),
3760 return NT_STATUS_NO_MEMORY
;
3763 if (ldapsam_search_one_group(ldap_state
, filter
,
3764 &result
) != LDAP_SUCCESS
) {
3766 return NT_STATUS_NO_SUCH_ALIAS
;
3769 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3773 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3774 ldap_msgfree(result
);
3776 return NT_STATUS_NO_SUCH_ALIAS
;
3780 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3781 "filter %s: count=%d\n", filter
, count
));
3782 ldap_msgfree(result
);
3784 return NT_STATUS_NO_SUCH_ALIAS
;
3789 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3793 ldap_msgfree(result
);
3794 return NT_STATUS_UNSUCCESSFUL
;
3797 values
= ldap_get_values(smbldap_get_ldap(ldap_state
->smbldap_state
),
3799 get_attr_key2string(groupmap_attr_list
,
3800 LDAP_ATTR_SID_LIST
));
3802 if (values
== NULL
) {
3803 ldap_msgfree(result
);
3804 return NT_STATUS_OK
;
3807 count
= ldap_count_values(values
);
3809 for (i
=0; i
<count
; i
++) {
3810 struct dom_sid member
;
3813 if (!string_to_sid(&member
, values
[i
]))
3816 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3818 if (!NT_STATUS_IS_OK(status
)) {
3819 ldap_value_free(values
);
3820 ldap_msgfree(result
);
3825 *p_num_members
= num_members
;
3826 ldap_value_free(values
);
3827 ldap_msgfree(result
);
3829 return NT_STATUS_OK
;
3832 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3833 TALLOC_CTX
*mem_ctx
,
3834 const struct dom_sid
*domain_sid
,
3835 const struct dom_sid
*members
,
3837 uint32_t **pp_alias_rids
,
3838 size_t *p_num_alias_rids
)
3840 struct ldapsam_privates
*ldap_state
=
3841 (struct ldapsam_privates
*)methods
->private_data
;
3844 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3846 LDAPMessage
*result
= NULL
;
3847 LDAPMessage
*entry
= NULL
;
3851 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3852 bool is_builtin
= false;
3853 bool sid_added
= false;
3855 *pp_alias_rids
= NULL
;
3856 *p_num_alias_rids
= 0;
3858 if (sid_check_is_builtin(domain_sid
)) {
3860 type
= SID_NAME_ALIAS
;
3863 if (sid_check_is_our_sam(domain_sid
)) {
3864 type
= SID_NAME_ALIAS
;
3867 if (type
== SID_NAME_USE_NONE
) {
3868 struct dom_sid_buf buf
;
3869 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3870 dom_sid_str_buf(domain_sid
, &buf
)));
3871 return NT_STATUS_UNSUCCESSFUL
;
3874 if (num_members
== 0) {
3875 return NT_STATUS_OK
;
3878 filter
= talloc_asprintf(mem_ctx
,
3879 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3880 LDAP_OBJ_GROUPMAP
, type
);
3882 for (i
=0; i
<num_members
; i
++) {
3883 struct dom_sid_buf buf
;
3884 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3886 dom_sid_str_buf(&members
[i
], &buf
));
3889 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3891 if (filter
== NULL
) {
3892 return NT_STATUS_NO_MEMORY
;
3896 ldap_state
->search_cache
.filter
&&
3897 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3898 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3899 result
= ldap_state
->search_cache
.result
;
3900 ldap_state
->search_cache
.result
= NULL
;
3902 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3903 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3904 if (rc
!= LDAP_SUCCESS
) {
3905 return NT_STATUS_UNSUCCESSFUL
;
3907 smbldap_talloc_autofree_ldapmsg(filter
, result
);
3910 ldap_struct
= smbldap_get_ldap(ldap_state
->smbldap_state
);
3912 for (entry
= ldap_first_entry(ldap_struct
, result
);
3914 entry
= ldap_next_entry(ldap_struct
, entry
))
3920 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3926 if (!string_to_sid(&sid
, sid_str
))
3929 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3934 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3935 p_num_alias_rids
)) {
3936 return NT_STATUS_NO_MEMORY
;
3940 if (!is_builtin
&& !sid_added
) {
3941 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3943 * Note: result is a talloc child of filter because of the
3944 * smbldap_talloc_autofree_ldapmsg() usage
3946 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3947 ldap_state
->search_cache
.result
= result
;
3950 return NT_STATUS_OK
;
3953 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3954 enum pdb_policy_type type
,
3957 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3959 LDAPMod
**mods
= NULL
;
3960 fstring value_string
;
3961 const char *policy_attr
= NULL
;
3963 struct ldapsam_privates
*ldap_state
=
3964 (struct ldapsam_privates
*)methods
->private_data
;
3966 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3968 if (!ldap_state
->domain_dn
) {
3969 return NT_STATUS_INVALID_PARAMETER
;
3972 policy_attr
= get_account_policy_attr(type
);
3973 if (policy_attr
== NULL
) {
3974 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3979 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3981 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3983 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3986 ldap_mods_free(mods
, True
);
3988 if (rc
!= LDAP_SUCCESS
) {
3992 if (!cache_account_policy_set(type
, value
)) {
3993 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3994 "update local tdb cache\n"));
3998 return NT_STATUS_OK
;
4001 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
4002 enum pdb_policy_type type
,
4005 return ldapsam_set_account_policy_in_ldap(methods
, type
,
4009 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
4010 enum pdb_policy_type type
,
4013 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4014 LDAPMessage
*result
= NULL
;
4015 LDAPMessage
*entry
= NULL
;
4020 const char *policy_attr
= NULL
;
4022 struct ldapsam_privates
*ldap_state
=
4023 (struct ldapsam_privates
*)methods
->private_data
;
4025 const char *attrs
[2];
4027 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4029 if (!ldap_state
->domain_dn
) {
4030 return NT_STATUS_INVALID_PARAMETER
;
4033 policy_attr
= get_account_policy_attr(type
);
4035 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4036 "policy index: %d\n", type
));
4040 attrs
[0] = policy_attr
;
4043 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
4044 if (filter
== NULL
) {
4045 return NT_STATUS_NO_MEMORY
;
4047 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
4048 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
4050 TALLOC_FREE(filter
);
4051 if (rc
!= LDAP_SUCCESS
) {
4055 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4060 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4061 if (entry
== NULL
) {
4065 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4070 *value
= (uint32_t)atol(vals
[0]);
4072 ntstatus
= NT_STATUS_OK
;
4076 ldap_value_free(vals
);
4077 ldap_msgfree(result
);
4082 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4084 - if user hasn't decided to use account policies inside LDAP just reuse the
4087 - if there is a valid cache entry, return that
4088 - if there is an LDAP entry, update cache and return
4089 - otherwise set to default, update cache and return
4093 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4094 enum pdb_policy_type type
,
4097 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4099 if (cache_account_policy_get(type
, value
)) {
4100 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4102 return NT_STATUS_OK
;
4105 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4107 if (NT_STATUS_IS_OK(ntstatus
)) {
4111 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4115 /* should we automagically migrate old tdb value here ? */
4116 if (account_policy_get(type
, value
))
4119 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4120 "default\n", type
));
4123 if (!account_policy_get_default(type
, value
)) {
4129 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4130 if (!NT_STATUS_IS_OK(ntstatus
)) {
4136 if (!cache_account_policy_set(type
, *value
)) {
4137 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4138 "tdb as a cache\n"));
4139 return NT_STATUS_UNSUCCESSFUL
;
4142 return NT_STATUS_OK
;
4145 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4146 const struct dom_sid
*domain_sid
,
4150 enum lsa_SidType
*attrs
)
4152 struct ldapsam_privates
*ldap_state
=
4153 (struct ldapsam_privates
*)methods
->private_data
;
4154 LDAPMessage
*msg
= NULL
;
4156 char *allsids
= NULL
;
4157 size_t i
, num_mapped
;
4159 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4160 TALLOC_CTX
*mem_ctx
;
4164 mem_ctx
= talloc_new(NULL
);
4165 if (mem_ctx
== NULL
) {
4166 DEBUG(0, ("talloc_new failed\n"));
4170 if (!sid_check_is_builtin(domain_sid
) &&
4171 !sid_check_is_our_sam(domain_sid
)) {
4172 result
= NT_STATUS_INVALID_PARAMETER
;
4176 if (num_rids
== 0) {
4177 result
= NT_STATUS_NONE_MAPPED
;
4181 for (i
=0; i
<num_rids
; i
++)
4182 attrs
[i
] = SID_NAME_UNKNOWN
;
4184 allsids
= talloc_strdup(mem_ctx
, "");
4185 if (allsids
== NULL
) {
4189 for (i
=0; i
<num_rids
; i
++) {
4191 struct dom_sid_buf buf
;
4192 sid_compose(&sid
, domain_sid
, rids
[i
]);
4193 allsids
= talloc_asprintf_append_buffer(
4196 dom_sid_str_buf(&sid
, &buf
));
4197 if (allsids
== NULL
) {
4202 /* First look for users */
4206 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4208 filter
= talloc_asprintf(
4209 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4210 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4212 if (filter
== NULL
) {
4216 rc
= smbldap_search(ldap_state
->smbldap_state
,
4217 lp_ldap_user_suffix(talloc_tos()),
4218 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4220 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4223 if (rc
!= LDAP_SUCCESS
)
4226 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4229 for (entry
= ldap_first_entry(ld
, msg
);
4231 entry
= ldap_next_entry(ld
, entry
)) {
4236 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4238 DEBUG(2, ("Could not find sid from ldap entry\n"));
4242 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4245 DEBUG(2, ("Could not retrieve uid attribute\n"));
4249 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4250 if (rid
== rids
[rid_index
])
4254 if (rid_index
== num_rids
) {
4255 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4259 attrs
[rid_index
] = SID_NAME_USER
;
4260 names
[rid_index
] = name
;
4264 if (num_mapped
== num_rids
) {
4265 /* No need to look for groups anymore -- we're done */
4266 result
= NT_STATUS_OK
;
4270 /* Same game for groups */
4274 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4275 "sambaGroupType", NULL
};
4277 filter
= talloc_asprintf(
4278 mem_ctx
, "(&(objectClass=%s)(|%s))",
4279 LDAP_OBJ_GROUPMAP
, allsids
);
4280 if (filter
== NULL
) {
4284 rc
= smbldap_search(ldap_state
->smbldap_state
,
4285 lp_ldap_suffix(talloc_tos()),
4286 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4288 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4291 if (rc
!= LDAP_SUCCESS
)
4294 /* ldap_struct might have changed due to a reconnect */
4296 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4298 /* For consistency checks, we already checked we're only domain or builtin */
4300 is_builtin
= sid_check_is_builtin(domain_sid
);
4302 for (entry
= ldap_first_entry(ld
, msg
);
4304 entry
= ldap_next_entry(ld
, entry
))
4309 enum lsa_SidType type
;
4310 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4312 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4315 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4320 type
= (enum lsa_SidType
)atol(attr
);
4322 /* Consistency checks */
4323 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4324 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4325 (type
!= SID_NAME_DOM_GRP
)))) {
4326 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4329 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4331 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4335 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4338 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4340 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4344 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4349 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4350 if (rid
== rids
[rid_index
])
4354 if (rid_index
== num_rids
) {
4355 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4359 attrs
[rid_index
] = type
;
4360 names
[rid_index
] = attr
;
4364 result
= NT_STATUS_NONE_MAPPED
;
4367 result
= (num_mapped
== num_rids
) ?
4368 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4370 TALLOC_FREE(mem_ctx
);
4374 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4376 char *filter
= NULL
;
4377 char *escaped
= NULL
;
4378 char *result
= NULL
;
4380 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4381 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4385 escaped
= escape_ldap_string(talloc_tos(), username
);
4386 if (escaped
== NULL
) goto done
;
4388 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4392 TALLOC_FREE(escaped
);
4397 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4401 const char **result
;
4403 va_start(ap
, mem_ctx
);
4404 while (va_arg(ap
, const char *) != NULL
)
4408 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4412 va_start(ap
, mem_ctx
);
4413 for (i
=0; i
<num
; i
++) {
4414 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4415 if (result
[i
] == NULL
) {
4416 talloc_free(result
);
4427 struct ldap_search_state
{
4428 struct smbldap_state
*connection
;
4430 uint32_t acct_flags
;
4431 uint16_t group_type
;
4438 void *pagedresults_cookie
;
4440 LDAPMessage
*entries
, *current_entry
;
4441 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4442 TALLOC_CTX
*mem_ctx
,
4443 LDAP
*ld
, LDAPMessage
*entry
,
4444 struct samr_displayentry
*result
);
4447 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4449 struct ldap_search_state
*state
=
4450 (struct ldap_search_state
*)search
->private_data
;
4452 int rc
= LDAP_OPERATIONS_ERROR
;
4454 state
->entries
= NULL
;
4456 if (smbldap_get_paged_results(state
->connection
)) {
4457 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4458 state
->scope
, state
->filter
,
4459 state
->attrs
, state
->attrsonly
,
4460 lp_ldap_page_size(), &state
->entries
,
4461 &state
->pagedresults_cookie
);
4464 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4466 if (state
->entries
!= NULL
) {
4467 /* Left over from unsuccessful paged attempt */
4468 ldap_msgfree(state
->entries
);
4469 state
->entries
= NULL
;
4472 rc
= smbldap_search(state
->connection
, state
->base
,
4473 state
->scope
, state
->filter
, state
->attrs
,
4474 state
->attrsonly
, &state
->entries
);
4476 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4479 /* Ok, the server was lying. It told us it could do paged
4480 * searches when it could not. */
4481 smbldap_set_paged_results(state
->connection
, false);
4484 ld
= smbldap_get_ldap(state
->connection
);
4486 DEBUG(5, ("Don't have an LDAP connection right after a "
4490 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4495 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4497 struct ldap_search_state
*state
=
4498 (struct ldap_search_state
*)search
->private_data
;
4501 if (!smbldap_get_paged_results(state
->connection
)) {
4502 /* There is no next page when there are no paged results */
4506 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4507 state
->scope
, state
->filter
, state
->attrs
,
4508 state
->attrsonly
, lp_ldap_page_size(),
4510 &state
->pagedresults_cookie
);
4512 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4515 state
->current_entry
= ldap_first_entry(
4516 smbldap_get_ldap(state
->connection
), state
->entries
);
4518 if (state
->current_entry
== NULL
) {
4519 ldap_msgfree(state
->entries
);
4520 state
->entries
= NULL
;
4527 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4528 struct samr_displayentry
*entry
)
4530 struct ldap_search_state
*state
=
4531 (struct ldap_search_state
*)search
->private_data
;
4535 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4538 if ((state
->entries
== NULL
) &&
4539 !ldapsam_search_nextpage(search
))
4542 if (state
->current_entry
== NULL
) {
4546 result
= state
->ldap2displayentry(state
, search
,
4547 smbldap_get_ldap(state
->connection
),
4548 state
->current_entry
, entry
);
4552 dn
= ldap_get_dn(smbldap_get_ldap(state
->connection
),
4553 state
->current_entry
);
4554 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4555 if (dn
!= NULL
) ldap_memfree(dn
);
4558 state
->current_entry
= ldap_next_entry(
4559 smbldap_get_ldap(state
->connection
), state
->current_entry
);
4561 if (state
->current_entry
== NULL
) {
4562 ldap_msgfree(state
->entries
);
4563 state
->entries
= NULL
;
4566 if (!result
) goto retry
;
4571 static void ldapsam_search_end(struct pdb_search
*search
)
4573 struct ldap_search_state
*state
=
4574 (struct ldap_search_state
*)search
->private_data
;
4577 if (state
->pagedresults_cookie
== NULL
)
4580 if (state
->entries
!= NULL
)
4581 ldap_msgfree(state
->entries
);
4583 state
->entries
= NULL
;
4584 state
->current_entry
= NULL
;
4586 if (!smbldap_get_paged_results(state
->connection
)) {
4590 /* Tell the LDAP server we're not interested in the rest anymore. */
4592 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4593 state
->filter
, state
->attrs
,
4594 state
->attrsonly
, 0, &state
->entries
,
4595 &state
->pagedresults_cookie
);
4597 if (rc
!= LDAP_SUCCESS
)
4598 DEBUG(5, ("Could not end search properly\n"));
4603 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4604 TALLOC_CTX
*mem_ctx
,
4605 LDAP
*ld
, LDAPMessage
*entry
,
4606 struct samr_displayentry
*result
)
4609 size_t converted_size
;
4611 uint32_t acct_flags
;
4613 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4614 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4615 acct_flags
= ACB_NORMAL
;
4617 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4618 ldap_value_free(vals
);
4621 if ((state
->acct_flags
!= 0) &&
4622 ((state
->acct_flags
& acct_flags
) == 0))
4625 result
->acct_flags
= acct_flags
;
4626 result
->account_name
= "";
4627 result
->fullname
= "";
4628 result
->description
= "";
4630 vals
= ldap_get_values(ld
, entry
, "uid");
4631 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4632 DEBUG(5, ("\"uid\" not found\n"));
4635 if (!pull_utf8_talloc(mem_ctx
,
4636 discard_const_p(char *, &result
->account_name
),
4637 vals
[0], &converted_size
))
4639 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4643 ldap_value_free(vals
);
4645 vals
= ldap_get_values(ld
, entry
, "displayName");
4646 if ((vals
== NULL
) || (vals
[0] == NULL
))
4647 DEBUG(8, ("\"displayName\" not found\n"));
4648 else if (!pull_utf8_talloc(mem_ctx
,
4649 discard_const_p(char *, &result
->fullname
),
4650 vals
[0], &converted_size
))
4652 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4656 ldap_value_free(vals
);
4658 vals
= ldap_get_values(ld
, entry
, "description");
4659 if ((vals
== NULL
) || (vals
[0] == NULL
))
4660 DEBUG(8, ("\"description\" not found\n"));
4661 else if (!pull_utf8_talloc(mem_ctx
,
4662 discard_const_p(char *, &result
->description
),
4663 vals
[0], &converted_size
))
4665 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4669 ldap_value_free(vals
);
4671 if ((result
->account_name
== NULL
) ||
4672 (result
->fullname
== NULL
) ||
4673 (result
->description
== NULL
)) {
4674 DEBUG(0, ("talloc failed\n"));
4678 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4679 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4680 DEBUG(0, ("\"objectSid\" not found\n"));
4684 if (!string_to_sid(&sid
, vals
[0])) {
4685 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4686 ldap_value_free(vals
);
4689 ldap_value_free(vals
);
4691 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4692 struct dom_sid_buf buf
;
4693 DEBUG(0, ("sid %s does not belong to our domain\n",
4694 dom_sid_str_buf(&sid
, &buf
)));
4702 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4703 struct pdb_search
*search
,
4704 uint32_t acct_flags
)
4706 struct ldapsam_privates
*ldap_state
=
4707 (struct ldapsam_privates
*)methods
->private_data
;
4708 struct ldap_search_state
*state
;
4710 state
= talloc(search
, struct ldap_search_state
);
4711 if (state
== NULL
) {
4712 DEBUG(0, ("talloc failed\n"));
4716 state
->connection
= ldap_state
->smbldap_state
;
4718 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4719 state
->base
= lp_ldap_user_suffix(talloc_tos());
4720 else if ((acct_flags
!= 0) &&
4721 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4722 state
->base
= lp_ldap_machine_suffix(talloc_tos());
4724 state
->base
= lp_ldap_suffix(talloc_tos());
4726 state
->acct_flags
= acct_flags
;
4727 state
->base
= talloc_strdup(search
, state
->base
);
4728 state
->scope
= LDAP_SCOPE_SUBTREE
;
4729 state
->filter
= get_ldap_filter(search
, "*");
4730 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4731 "displayName", "description",
4732 "sambaAcctFlags", NULL
);
4733 state
->attrsonly
= 0;
4734 state
->pagedresults_cookie
= NULL
;
4735 state
->entries
= NULL
;
4736 state
->ldap2displayentry
= ldapuser2displayentry
;
4738 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4739 DEBUG(0, ("talloc failed\n"));
4743 search
->private_data
= state
;
4744 search
->next_entry
= ldapsam_search_next_entry
;
4745 search
->search_end
= ldapsam_search_end
;
4747 return ldapsam_search_firstpage(search
);
4750 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4751 TALLOC_CTX
*mem_ctx
,
4752 LDAP
*ld
, LDAPMessage
*entry
,
4753 struct samr_displayentry
*result
)
4756 size_t converted_size
;
4758 uint16_t group_type
;
4760 result
->account_name
= "";
4761 result
->fullname
= "";
4762 result
->description
= "";
4765 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4766 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4767 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4769 ldap_value_free(vals
);
4774 group_type
= atoi(vals
[0]);
4776 if ((state
->group_type
!= 0) &&
4777 ((state
->group_type
!= group_type
))) {
4778 ldap_value_free(vals
);
4782 ldap_value_free(vals
);
4784 /* display name is the NT group name */
4786 vals
= ldap_get_values(ld
, entry
, "displayName");
4787 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4788 DEBUG(8, ("\"displayName\" not found\n"));
4790 /* fallback to the 'cn' attribute */
4791 vals
= ldap_get_values(ld
, entry
, "cn");
4792 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4793 DEBUG(5, ("\"cn\" not found\n"));
4796 if (!pull_utf8_talloc(mem_ctx
,
4797 discard_const_p(char *,
4798 &result
->account_name
),
4799 vals
[0], &converted_size
))
4801 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4802 "failed: %s", strerror(errno
)));
4805 else if (!pull_utf8_talloc(mem_ctx
,
4806 discard_const_p(char *,
4807 &result
->account_name
),
4808 vals
[0], &converted_size
))
4810 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4814 ldap_value_free(vals
);
4816 vals
= ldap_get_values(ld
, entry
, "description");
4817 if ((vals
== NULL
) || (vals
[0] == NULL
))
4818 DEBUG(8, ("\"description\" not found\n"));
4819 else if (!pull_utf8_talloc(mem_ctx
,
4820 discard_const_p(char *, &result
->description
),
4821 vals
[0], &converted_size
))
4823 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4826 ldap_value_free(vals
);
4828 if ((result
->account_name
== NULL
) ||
4829 (result
->fullname
== NULL
) ||
4830 (result
->description
== NULL
)) {
4831 DEBUG(0, ("talloc failed\n"));
4835 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4836 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4837 DEBUG(0, ("\"objectSid\" not found\n"));
4839 ldap_value_free(vals
);
4844 if (!string_to_sid(&sid
, vals
[0])) {
4845 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4849 ldap_value_free(vals
);
4851 switch (group_type
) {
4852 case SID_NAME_DOM_GRP
:
4853 case SID_NAME_ALIAS
:
4855 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4856 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4858 struct dom_sid_buf buf
;
4859 DEBUG(0, ("%s is not in our domain\n",
4860 dom_sid_str_buf(&sid
, &buf
)));
4866 DEBUG(0,("unknown group type: %d\n", group_type
));
4870 result
->acct_flags
= 0;
4875 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4876 struct pdb_search
*search
,
4877 const struct dom_sid
*sid
,
4878 enum lsa_SidType type
)
4880 struct ldapsam_privates
*ldap_state
=
4881 (struct ldapsam_privates
*)methods
->private_data
;
4882 struct ldap_search_state
*state
;
4883 struct dom_sid_buf tmp
;
4885 state
= talloc(search
, struct ldap_search_state
);
4886 if (state
== NULL
) {
4887 DEBUG(0, ("talloc failed\n"));
4891 state
->connection
= ldap_state
->smbldap_state
;
4893 state
->base
= lp_ldap_suffix(search
);
4894 state
->connection
= ldap_state
->smbldap_state
;
4895 state
->scope
= LDAP_SCOPE_SUBTREE
;
4896 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4897 "(sambaGroupType=%d)(sambaSID=%s*))",
4900 dom_sid_str_buf(sid
, &tmp
));
4901 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4902 "displayName", "description",
4903 "sambaGroupType", NULL
);
4904 state
->attrsonly
= 0;
4905 state
->pagedresults_cookie
= NULL
;
4906 state
->entries
= NULL
;
4907 state
->group_type
= type
;
4908 state
->ldap2displayentry
= ldapgroup2displayentry
;
4910 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4911 DEBUG(0, ("talloc failed\n"));
4915 search
->private_data
= state
;
4916 search
->next_entry
= ldapsam_search_next_entry
;
4917 search
->search_end
= ldapsam_search_end
;
4919 return ldapsam_search_firstpage(search
);
4922 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4923 struct pdb_search
*search
)
4925 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4928 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4929 struct pdb_search
*search
,
4930 const struct dom_sid
*sid
)
4932 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4935 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4937 return PDB_CAP_STORE_RIDS
;
4940 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4943 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4945 LDAPMessage
*result
= NULL
;
4946 LDAPMessage
*entry
= NULL
;
4947 LDAPMod
**mods
= NULL
;
4951 uint32_t nextRid
= 0;
4956 TALLOC_CTX
*mem_ctx
;
4958 mem_ctx
= talloc_new(NULL
);
4959 if (mem_ctx
== NULL
) {
4960 DEBUG(0, ("talloc_new failed\n"));
4961 return NT_STATUS_NO_MEMORY
;
4964 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4965 get_global_sam_name(), False
);
4966 if (!NT_STATUS_IS_OK(status
)) {
4967 DEBUG(3, ("Could not get domain info: %s\n",
4968 nt_errstr(status
)));
4972 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4974 entry
= ldap_first_entry(priv2ld(priv
), result
);
4975 if (entry
== NULL
) {
4976 DEBUG(0, ("Could not get domain info entry\n"));
4977 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4981 /* Find the largest of the three attributes "sambaNextRid",
4982 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4983 concept of differentiating between user and group rids, and will
4984 use only "sambaNextRid" in the future. But for compatibility
4985 reasons I look if others have chosen different strategies -- VL */
4987 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4988 "sambaNextRid", mem_ctx
);
4989 if (value
!= NULL
) {
4990 tmp
= (uint32_t)smb_strtoul(value
,
4999 nextRid
= MAX(nextRid
, tmp
);
5002 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5003 "sambaNextUserRid", mem_ctx
);
5004 if (value
!= NULL
) {
5005 tmp
= (uint32_t)smb_strtoul(value
,
5014 nextRid
= MAX(nextRid
, tmp
);
5017 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5018 "sambaNextGroupRid", mem_ctx
);
5019 if (value
!= NULL
) {
5020 tmp
= (uint32_t)smb_strtoul(value
,
5029 nextRid
= MAX(nextRid
, tmp
);
5033 nextRid
= BASE_RID
-1;
5038 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
5039 talloc_asprintf(mem_ctx
, "%d", nextRid
));
5040 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
5042 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
5043 status
= NT_STATUS_NO_MEMORY
;
5047 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
5049 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5052 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
5055 if (NT_STATUS_IS_OK(status
)) {
5059 TALLOC_FREE(mem_ctx
);
5063 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
5067 for (i
=0; i
<10; i
++) {
5068 NTSTATUS result
= ldapsam_get_new_rid(
5069 (struct ldapsam_privates
*)methods
->private_data
, rid
);
5070 if (NT_STATUS_IS_OK(result
)) {
5074 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
5078 /* The ldap update failed (maybe a race condition), retry */
5081 /* Tried 10 times, fail. */
5082 return NT_STATUS_ACCESS_DENIED
;
5085 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
5087 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
5088 return NT_STATUS_IS_OK(result
) ? True
: False
;
5091 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
5092 const struct dom_sid
*sid
,
5095 struct ldapsam_privates
*priv
=
5096 (struct ldapsam_privates
*)methods
->private_data
;
5099 struct dom_sid_buf buf
;
5100 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5102 LDAPMessage
*result
= NULL
;
5103 LDAPMessage
*entry
= NULL
;
5108 TALLOC_CTX
*mem_ctx
;
5110 ret
= pdb_sid_to_id_unix_users_and_groups(sid
, id
);
5115 mem_ctx
= talloc_new(NULL
);
5116 if (mem_ctx
== NULL
) {
5117 DEBUG(0, ("talloc_new failed\n"));
5121 filter
= talloc_asprintf(mem_ctx
,
5123 "(|(objectClass=%s)(objectClass=%s)))",
5124 dom_sid_str_buf(sid
, &buf
),
5125 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5126 if (filter
== NULL
) {
5127 DEBUG(5, ("talloc_asprintf failed\n"));
5131 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5133 if (rc
!= LDAP_SUCCESS
) {
5136 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
5138 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5139 DEBUG(10, ("Got %d entries, expected one\n",
5140 ldap_count_entries(priv2ld(priv
), result
)));
5144 entry
= ldap_first_entry(priv2ld(priv
), result
);
5146 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5147 "sambaGroupType", mem_ctx
);
5149 if (value
!= NULL
) {
5150 const char *gid_str
;
5153 gid_str
= smbldap_talloc_single_attribute(
5154 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5155 if (gid_str
== NULL
) {
5156 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5157 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5162 id
->id
= smb_strtoul(gid_str
,
5171 id
->type
= ID_TYPE_GID
;
5176 /* It must be a user */
5178 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5179 "uidNumber", mem_ctx
);
5180 if (value
== NULL
) {
5181 DEBUG(1, ("Could not find uidNumber in %s\n",
5182 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5186 id
->id
= smb_strtoul(value
, NULL
, 10, &error
, SMB_STR_STANDARD
);
5191 id
->type
= ID_TYPE_UID
;
5194 TALLOC_FREE(mem_ctx
);
5199 * Find the SID for a uid.
5200 * This is shortcut is only used if ldapsam:trusted is set to true.
5202 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5203 struct dom_sid
*sid
)
5205 struct ldapsam_privates
*priv
=
5206 (struct ldapsam_privates
*)methods
->private_data
;
5208 const char *attrs
[] = { "sambaSID", NULL
};
5209 LDAPMessage
*result
= NULL
;
5210 LDAPMessage
*entry
= NULL
;
5212 char *user_sid_string
;
5213 struct dom_sid user_sid
;
5215 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5217 filter
= talloc_asprintf(tmp_ctx
,
5220 "(objectClass=%s))",
5222 LDAP_OBJ_POSIXACCOUNT
,
5223 LDAP_OBJ_SAMBASAMACCOUNT
);
5224 if (filter
== NULL
) {
5225 DEBUG(3, ("talloc_asprintf failed\n"));
5229 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5230 if (rc
!= LDAP_SUCCESS
) {
5233 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5235 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5236 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5237 ldap_count_entries(priv2ld(priv
), result
),
5238 (unsigned int)uid
));
5242 entry
= ldap_first_entry(priv2ld(priv
), result
);
5244 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5245 "sambaSID", tmp_ctx
);
5246 if (user_sid_string
== NULL
) {
5247 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5248 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5252 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5253 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5258 sid_copy(sid
, &user_sid
);
5263 TALLOC_FREE(tmp_ctx
);
5268 * Find the SID for a gid.
5269 * This is shortcut is only used if ldapsam:trusted is set to true.
5271 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5272 struct dom_sid
*sid
)
5274 struct ldapsam_privates
*priv
=
5275 (struct ldapsam_privates
*)methods
->private_data
;
5277 const char *attrs
[] = { "sambaSID", NULL
};
5278 LDAPMessage
*result
= NULL
;
5279 LDAPMessage
*entry
= NULL
;
5281 char *group_sid_string
;
5282 struct dom_sid group_sid
;
5284 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5286 filter
= talloc_asprintf(tmp_ctx
,
5288 "(objectClass=%s))",
5291 if (filter
== NULL
) {
5292 DEBUG(3, ("talloc_asprintf failed\n"));
5296 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5297 if (rc
!= LDAP_SUCCESS
) {
5300 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5302 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5303 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5304 ldap_count_entries(priv2ld(priv
), result
),
5305 (unsigned int)gid
));
5309 entry
= ldap_first_entry(priv2ld(priv
), result
);
5311 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5312 "sambaSID", tmp_ctx
);
5313 if (group_sid_string
== NULL
) {
5314 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5315 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5319 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5320 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5325 sid_copy(sid
, &group_sid
);
5330 TALLOC_FREE(tmp_ctx
);
5334 static bool ldapsam_id_to_sid(struct pdb_methods
*methods
, struct unixid
*id
,
5335 struct dom_sid
*sid
)
5339 return ldapsam_uid_to_sid(methods
, id
->id
, sid
);
5342 return ldapsam_gid_to_sid(methods
, id
->id
, sid
);
5351 * The following functions are called only if
5352 * ldapsam:trusted and ldapsam:editposix are
5357 * ldapsam_create_user creates a new
5358 * posixAccount and sambaSamAccount object
5359 * in the ldap users subtree
5361 * The uid is allocated by winbindd.
5364 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5365 TALLOC_CTX
*tmp_ctx
, const char *name
,
5366 uint32_t acb_info
, uint32_t *rid
)
5368 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5369 LDAPMessage
*entry
= NULL
;
5370 LDAPMessage
*result
= NULL
;
5371 uint32_t num_result
;
5372 bool is_machine
= False
;
5373 bool add_posix
= False
;
5374 bool init_okay
= False
;
5375 LDAPMod
**mods
= NULL
;
5383 const char *dn
= NULL
;
5384 struct dom_sid group_sid
;
5385 struct dom_sid user_sid
;
5391 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5392 acb_info
& ACB_WSTRUST
||
5393 acb_info
& ACB_SVRTRUST
||
5394 acb_info
& ACB_DOMTRUST
) {
5398 username
= escape_ldap_string(talloc_tos(), name
);
5399 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5400 username
, LDAP_OBJ_POSIXACCOUNT
);
5401 TALLOC_FREE(username
);
5403 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5404 if (rc
!= LDAP_SUCCESS
) {
5405 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5406 return NT_STATUS_ACCESS_DENIED
;
5408 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5410 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5412 if (num_result
> 1) {
5413 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5414 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5417 if (num_result
== 1) {
5419 /* check if it is just a posix account.
5420 * or if there is a sid attached to this entry
5423 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5425 return NT_STATUS_UNSUCCESSFUL
;
5428 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5430 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5431 return NT_STATUS_USER_EXISTS
;
5434 /* it is just a posix account, retrieve the dn for later use */
5435 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5437 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5438 return NT_STATUS_NO_MEMORY
;
5442 if (num_result
== 0) {
5446 /* Create the basic samu structure and generate the mods for the ldap commit */
5447 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5448 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5452 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5454 user
= samu_new(tmp_ctx
);
5456 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5457 return NT_STATUS_NO_MEMORY
;
5460 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5461 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5462 return NT_STATUS_UNSUCCESSFUL
;
5464 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5465 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5466 return NT_STATUS_UNSUCCESSFUL
;
5469 if (acb_info
& ACB_NORMAL
) {
5470 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5471 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5472 return NT_STATUS_UNSUCCESSFUL
;
5475 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5476 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5477 return NT_STATUS_UNSUCCESSFUL
;
5481 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5482 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5483 return NT_STATUS_UNSUCCESSFUL
;
5487 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5488 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5489 return NT_STATUS_UNSUCCESSFUL
;
5492 init_okay
= init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
);
5495 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5496 ldap_mods_free(mods
, true);
5497 return NT_STATUS_UNSUCCESSFUL
;
5500 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5501 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5503 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5508 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5510 /* retrieve the Domain Users group gid */
5511 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5512 !sid_to_gid(&group_sid
, &gid
)) {
5513 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5514 ldap_mods_free(mods
, true);
5515 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5518 /* lets allocate a new userid for this user */
5519 if (!winbind_allocate_uid(&uid
)) {
5520 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5521 ldap_mods_free(mods
, true);
5522 return NT_STATUS_UNSUCCESSFUL
;
5527 /* TODO: choose a more appropriate default for machines */
5528 homedir
= talloc_sub_specified(tmp_ctx
,
5529 lp_template_homedir(),
5530 "SMB_workstations_home",
5532 ldap_state
->domain_name
,
5535 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5537 homedir
= talloc_sub_specified(tmp_ctx
,
5538 lp_template_homedir(),
5541 ldap_state
->domain_name
,
5544 shell
= talloc_sub_specified(tmp_ctx
,
5545 lp_template_shell(),
5548 ldap_state
->domain_name
,
5552 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5553 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5555 escape_name
= escape_rdn_val_string_alloc(name
);
5557 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5558 ldap_mods_free(mods
, true);
5559 return NT_STATUS_NO_MEMORY
;
5563 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix (talloc_tos()));
5565 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix (talloc_tos()));
5568 SAFE_FREE(escape_name
);
5570 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5571 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5572 ldap_mods_free(mods
, true);
5573 return NT_STATUS_NO_MEMORY
;
5576 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5577 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5578 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5579 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5580 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5581 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5582 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5586 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5588 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5591 ldap_mods_free(mods
, true);
5593 if (rc
!= LDAP_SUCCESS
) {
5594 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5595 return NT_STATUS_UNSUCCESSFUL
;
5598 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5600 flush_pwnam_cache();
5602 return NT_STATUS_OK
;
5605 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5607 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5608 LDAPMessage
*result
= NULL
;
5609 LDAPMessage
*entry
= NULL
;
5615 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5617 filter
= talloc_asprintf(tmp_ctx
,
5620 "(objectClass=%s))",
5621 pdb_get_username(sam_acct
),
5622 LDAP_OBJ_POSIXACCOUNT
,
5623 LDAP_OBJ_SAMBASAMACCOUNT
);
5624 if (filter
== NULL
) {
5625 return NT_STATUS_NO_MEMORY
;
5628 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5629 if (rc
!= LDAP_SUCCESS
) {
5630 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5631 return NT_STATUS_UNSUCCESSFUL
;
5633 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5635 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5637 if (num_result
== 0) {
5638 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5639 return NT_STATUS_NO_SUCH_USER
;
5642 if (num_result
> 1) {
5643 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5644 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5647 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5649 return NT_STATUS_UNSUCCESSFUL
;
5652 /* it is just a posix account, retrieve the dn for later use */
5653 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5655 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5656 return NT_STATUS_NO_MEMORY
;
5659 /* try to remove memberships first */
5662 struct dom_sid
*sids
= NULL
;
5664 uint32_t num_groups
= 0;
5666 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5668 status
= ldapsam_enum_group_memberships(my_methods
,
5674 if (!NT_STATUS_IS_OK(status
)) {
5678 for (i
=0; i
< num_groups
; i
++) {
5682 sid_peek_rid(&sids
[i
], &group_rid
);
5684 ldapsam_del_groupmem(my_methods
,
5693 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5694 if (rc
!= LDAP_SUCCESS
) {
5695 return NT_STATUS_UNSUCCESSFUL
;
5698 flush_pwnam_cache();
5700 return NT_STATUS_OK
;
5704 * ldapsam_create_group creates a new
5705 * posixGroup and sambaGroupMapping object
5706 * in the ldap groups subtree
5708 * The gid is allocated by winbindd.
5711 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5712 TALLOC_CTX
*tmp_ctx
,
5716 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5718 LDAPMessage
*entry
= NULL
;
5719 LDAPMessage
*result
= NULL
;
5720 uint32_t num_result
;
5721 bool is_new_entry
= False
;
5722 LDAPMod
**mods
= NULL
;
5727 const char *dn
= NULL
;
5728 struct dom_sid group_sid
;
5729 struct dom_sid_buf buf
;
5734 groupname
= escape_ldap_string(talloc_tos(), name
);
5735 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5736 groupname
, LDAP_OBJ_POSIXGROUP
);
5737 TALLOC_FREE(groupname
);
5739 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5740 if (rc
!= LDAP_SUCCESS
) {
5741 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5742 return NT_STATUS_UNSUCCESSFUL
;
5744 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5746 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5748 if (num_result
> 1) {
5749 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5750 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5753 if (num_result
== 1) {
5755 /* check if it is just a posix group.
5756 * or if there is a sid attached to this entry
5759 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5761 return NT_STATUS_UNSUCCESSFUL
;
5764 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5766 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5767 return NT_STATUS_GROUP_EXISTS
;
5770 /* it is just a posix group, retrieve the gid and the dn for later use */
5771 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5773 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5774 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5777 gid
= smb_strtoul(tmp
, NULL
, 10, &error
, SMB_STR_STANDARD
);
5779 DBG_ERR("Failed to convert gidNumber\n");
5780 return NT_STATUS_UNSUCCESSFUL
;
5783 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5785 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5786 return NT_STATUS_NO_MEMORY
;
5790 if (num_result
== 0) {
5791 is_new_entry
= true;
5794 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5795 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5799 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5801 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5804 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5805 return NT_STATUS_NO_MEMORY
;
5808 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5809 smbldap_set_mod(&mods
,
5812 dom_sid_str_buf(&group_sid
, &buf
));
5813 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5814 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5819 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5821 /* lets allocate a new groupid for this group */
5822 if (!winbind_allocate_gid(&gid
)) {
5823 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5824 return NT_STATUS_UNSUCCESSFUL
;
5827 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5829 escape_name
= escape_rdn_val_string_alloc(name
);
5831 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5832 return NT_STATUS_NO_MEMORY
;
5835 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix(talloc_tos()));
5837 SAFE_FREE(escape_name
);
5839 if (!gidstr
|| !dn
) {
5840 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5841 return NT_STATUS_NO_MEMORY
;
5844 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5845 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5846 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5849 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5852 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5854 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5855 /* This call may fail with rfc2307bis schema */
5856 /* Retry adding a structural class */
5857 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5858 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5862 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5865 if (rc
!= LDAP_SUCCESS
) {
5866 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5867 return NT_STATUS_UNSUCCESSFUL
;
5870 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5872 return NT_STATUS_OK
;
5875 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5877 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5878 LDAPMessage
*result
= NULL
;
5879 LDAPMessage
*entry
= NULL
;
5884 struct dom_sid group_sid
;
5885 struct dom_sid_buf buf
;
5888 /* get the group sid */
5889 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5891 filter
= talloc_asprintf(tmp_ctx
,
5894 "(objectClass=%s))",
5895 dom_sid_str_buf(&group_sid
, &buf
),
5896 LDAP_OBJ_POSIXGROUP
,
5898 if (filter
== NULL
) {
5899 return NT_STATUS_NO_MEMORY
;
5902 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5903 if (rc
!= LDAP_SUCCESS
) {
5904 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5905 return NT_STATUS_UNSUCCESSFUL
;
5907 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5909 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5911 if (num_result
== 0) {
5912 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5913 return NT_STATUS_NO_SUCH_GROUP
;
5916 if (num_result
> 1) {
5917 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5918 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5921 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5923 return NT_STATUS_UNSUCCESSFUL
;
5926 /* here it is, retrieve the dn for later use */
5927 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5929 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5930 return NT_STATUS_NO_MEMORY
;
5933 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5935 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5936 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5939 /* check no user have this group marked as primary group */
5940 filter
= talloc_asprintf(tmp_ctx
,
5943 "(objectClass=%s))",
5945 LDAP_OBJ_POSIXACCOUNT
,
5946 LDAP_OBJ_SAMBASAMACCOUNT
);
5948 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5949 if (rc
!= LDAP_SUCCESS
) {
5950 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5951 return NT_STATUS_UNSUCCESSFUL
;
5953 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5955 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5957 if (num_result
!= 0) {
5958 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5959 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5962 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5963 if (rc
!= LDAP_SUCCESS
) {
5964 return NT_STATUS_UNSUCCESSFUL
;
5967 return NT_STATUS_OK
;
5970 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5971 TALLOC_CTX
*tmp_ctx
,
5973 uint32_t member_rid
,
5976 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5977 LDAPMessage
*entry
= NULL
;
5978 LDAPMessage
*result
= NULL
;
5979 uint32_t num_result
;
5980 LDAPMod
**mods
= NULL
;
5983 const char *dn
= NULL
;
5984 struct dom_sid group_sid
;
5985 struct dom_sid member_sid
;
5986 struct dom_sid_buf buf
;
5992 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5994 case LDAP_MOD_DELETE
:
5995 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5998 return NT_STATUS_UNSUCCESSFUL
;
6001 /* get member sid */
6002 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
6004 /* get the group sid */
6005 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
6007 filter
= talloc_asprintf(tmp_ctx
,
6010 "(objectClass=%s))",
6011 dom_sid_str_buf(&member_sid
, &buf
),
6012 LDAP_OBJ_POSIXACCOUNT
,
6013 LDAP_OBJ_SAMBASAMACCOUNT
);
6014 if (filter
== NULL
) {
6015 return NT_STATUS_NO_MEMORY
;
6018 /* get the member uid */
6019 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6020 if (rc
!= LDAP_SUCCESS
) {
6021 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
6022 return NT_STATUS_UNSUCCESSFUL
;
6024 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
6026 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6028 if (num_result
== 0) {
6029 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6030 return NT_STATUS_NO_SUCH_MEMBER
;
6033 if (num_result
> 1) {
6034 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6035 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6038 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6040 return NT_STATUS_UNSUCCESSFUL
;
6043 if (modop
== LDAP_MOD_DELETE
) {
6044 /* check if we are trying to remove the member from his primary group */
6046 gid_t user_gid
, group_gid
;
6048 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
6050 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6051 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6054 user_gid
= smb_strtoul(gidstr
, NULL
, 10, &error
, SMB_STR_STANDARD
);
6056 DBG_ERR("Failed to convert user gid\n");
6057 return NT_STATUS_UNSUCCESSFUL
;
6060 if (!sid_to_gid(&group_sid
, &group_gid
)) {
6061 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6062 return NT_STATUS_UNSUCCESSFUL
;
6065 if (user_gid
== group_gid
) {
6066 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6067 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
6071 /* here it is, retrieve the uid for later use */
6072 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
6074 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6075 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6078 filter
= talloc_asprintf(tmp_ctx
,
6081 "(objectClass=%s))",
6082 dom_sid_str_buf(&group_sid
, &buf
),
6083 LDAP_OBJ_POSIXGROUP
,
6087 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6088 if (rc
!= LDAP_SUCCESS
) {
6089 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6090 return NT_STATUS_UNSUCCESSFUL
;
6092 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
6094 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6096 if (num_result
== 0) {
6097 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6098 return NT_STATUS_NO_SUCH_GROUP
;
6101 if (num_result
> 1) {
6102 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6103 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6106 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6108 return NT_STATUS_UNSUCCESSFUL
;
6111 /* here it is, retrieve the dn for later use */
6112 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
6114 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6115 return NT_STATUS_NO_MEMORY
;
6118 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
6120 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
6122 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6123 if (rc
!= LDAP_SUCCESS
) {
6124 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
6125 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6126 return NT_STATUS_MEMBER_IN_GROUP
;
6128 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
6129 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6130 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
6132 return NT_STATUS_UNSUCCESSFUL
;
6135 return NT_STATUS_OK
;
6138 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
6139 TALLOC_CTX
*tmp_ctx
,
6141 uint32_t member_rid
)
6143 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
6145 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
6146 TALLOC_CTX
*tmp_ctx
,
6148 uint32_t member_rid
)
6150 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
6153 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
6154 TALLOC_CTX
*mem_ctx
,
6155 struct samu
*sampass
)
6157 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6158 LDAPMessage
*entry
= NULL
;
6159 LDAPMessage
*result
= NULL
;
6160 uint32_t num_result
;
6161 LDAPMod
**mods
= NULL
;
6163 char *escape_username
;
6169 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6171 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6172 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6173 return NT_STATUS_UNSUCCESSFUL
;
6175 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6177 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6178 return NT_STATUS_NO_MEMORY
;
6181 escape_username
= escape_ldap_string(talloc_tos(),
6182 pdb_get_username(sampass
));
6183 if (escape_username
== NULL
) {
6184 return NT_STATUS_NO_MEMORY
;
6187 filter
= talloc_asprintf(mem_ctx
,
6190 "(objectClass=%s))",
6192 LDAP_OBJ_POSIXACCOUNT
,
6193 LDAP_OBJ_SAMBASAMACCOUNT
);
6195 TALLOC_FREE(escape_username
);
6197 if (filter
== NULL
) {
6198 return NT_STATUS_NO_MEMORY
;
6201 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6202 if (rc
!= LDAP_SUCCESS
) {
6203 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6204 return NT_STATUS_UNSUCCESSFUL
;
6206 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6208 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6210 if (num_result
== 0) {
6211 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6212 return NT_STATUS_NO_SUCH_USER
;
6215 if (num_result
> 1) {
6216 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6217 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6220 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6222 return NT_STATUS_UNSUCCESSFUL
;
6225 /* retrieve the dn for later use */
6226 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6228 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6229 return NT_STATUS_NO_MEMORY
;
6232 /* remove the old one, and add the new one, this way we do not risk races */
6233 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6237 return NT_STATUS_OK
;
6240 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6242 if (rc
!= LDAP_SUCCESS
) {
6243 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6244 pdb_get_username(sampass
), gidstr
));
6245 return NT_STATUS_UNSUCCESSFUL
;
6248 flush_pwnam_cache();
6250 return NT_STATUS_OK
;
6254 /**********************************************************************
6255 trusted domains functions
6256 *********************************************************************/
6258 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6261 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6262 ldap_state
->domain_dn
);
6265 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6266 TALLOC_CTX
*mem_ctx
,
6267 const char *domain
, LDAPMessage
**entry
)
6271 int scope
= LDAP_SCOPE_SUBTREE
;
6272 const char **attrs
= NULL
; /* NULL: get all attrs */
6273 int attrsonly
= 0; /* 0: return values too */
6274 LDAPMessage
*result
= NULL
;
6276 uint32_t num_result
;
6278 filter
= talloc_asprintf(talloc_tos(),
6279 "(&(objectClass=%s)(sambaDomainName=%s))",
6280 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6282 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6283 if (trusted_dn
== NULL
) {
6286 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6287 filter
, attrs
, attrsonly
, &result
);
6289 if (result
!= NULL
) {
6290 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6293 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6298 if (rc
!= LDAP_SUCCESS
) {
6302 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6304 if (num_result
> 1) {
6305 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6306 "%s object for domain '%s'?!\n",
6307 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6311 if (num_result
== 0) {
6312 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6313 "%s object for domain %s.\n",
6314 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6317 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6323 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6326 struct dom_sid
*sid
,
6327 time_t *pass_last_set_time
)
6329 struct ldapsam_privates
*ldap_state
=
6330 (struct ldapsam_privates
*)methods
->private_data
;
6331 LDAPMessage
*entry
= NULL
;
6333 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6335 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6344 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6345 entry
, "sambaClearTextPassword", talloc_tos());
6346 if (pwd_str
== NULL
) {
6349 /* trusteddom_pw routines do not use talloc yet... */
6350 *pwd
= SMB_STRDUP(pwd_str
);
6356 /* last change time */
6357 if (pass_last_set_time
!= NULL
) {
6359 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6360 entry
, "sambaPwdLastSet", talloc_tos());
6361 if (time_str
== NULL
) {
6364 *pass_last_set_time
= (time_t)atol(time_str
);
6370 struct dom_sid dom_sid
;
6371 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6374 if (sid_str
== NULL
) {
6377 if (!string_to_sid(&dom_sid
, sid_str
)) {
6380 sid_copy(sid
, &dom_sid
);
6386 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6389 const struct dom_sid
*sid
)
6391 struct ldapsam_privates
*ldap_state
=
6392 (struct ldapsam_privates
*)methods
->private_data
;
6393 LDAPMessage
*entry
= NULL
;
6394 LDAPMod
**mods
= NULL
;
6395 char *prev_pwd
= NULL
;
6396 char *trusted_dn
= NULL
;
6397 struct dom_sid_buf buf
;
6400 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6403 * get the current entry (if there is one) in order to put the
6404 * current password into the previous password attribute
6406 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6411 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6412 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6413 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6415 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6416 dom_sid_str_buf(sid
, &buf
));
6417 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6418 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6419 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6420 "sambaClearTextPassword", pwd
);
6422 if (entry
!= NULL
) {
6423 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6424 entry
, "sambaClearTextPassword", talloc_tos());
6425 if (prev_pwd
!= NULL
) {
6426 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6427 "sambaPreviousClearTextPassword",
6432 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods
);
6434 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6435 if (trusted_dn
== NULL
) {
6438 if (entry
== NULL
) {
6439 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6441 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6444 if (rc
!= LDAP_SUCCESS
) {
6445 DEBUG(1, ("error writing trusted domain password!\n"));
6452 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6456 struct ldapsam_privates
*ldap_state
=
6457 (struct ldapsam_privates
*)methods
->private_data
;
6458 LDAPMessage
*entry
= NULL
;
6459 const char *trusted_dn
;
6461 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6465 if (entry
== NULL
) {
6466 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6471 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6473 if (trusted_dn
== NULL
) {
6474 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6478 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6479 if (rc
!= LDAP_SUCCESS
) {
6486 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6487 TALLOC_CTX
*mem_ctx
,
6488 uint32_t *num_domains
,
6489 struct trustdom_info
***domains
)
6492 struct ldapsam_privates
*ldap_state
=
6493 (struct ldapsam_privates
*)methods
->private_data
;
6495 int scope
= LDAP_SCOPE_SUBTREE
;
6496 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6497 int attrsonly
= 0; /* 0: return values too */
6498 LDAPMessage
*result
= NULL
;
6499 LDAPMessage
*entry
= NULL
;
6501 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6502 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6504 rc
= smbldap_search(ldap_state
->smbldap_state
,
6505 ldap_state
->domain_dn
,
6512 if (result
!= NULL
) {
6513 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6516 if (rc
!= LDAP_SUCCESS
) {
6517 return NT_STATUS_UNSUCCESSFUL
;
6521 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6522 DEBUG(1, ("talloc failed\n"));
6523 return NT_STATUS_NO_MEMORY
;
6526 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6528 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6530 char *dom_name
, *dom_sid_str
;
6531 struct trustdom_info
*dom_info
;
6533 dom_info
= talloc(*domains
, struct trustdom_info
);
6534 if (dom_info
== NULL
) {
6535 DEBUG(1, ("talloc failed\n"));
6536 return NT_STATUS_NO_MEMORY
;
6539 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6543 if (dom_name
== NULL
) {
6544 DEBUG(1, ("talloc failed\n"));
6545 return NT_STATUS_NO_MEMORY
;
6547 dom_info
->name
= dom_name
;
6549 dom_sid_str
= smbldap_talloc_single_attribute(
6550 priv2ld(ldap_state
), entry
, "sambaSID",
6552 if (dom_sid_str
== NULL
) {
6553 DEBUG(1, ("talloc failed\n"));
6554 return NT_STATUS_NO_MEMORY
;
6556 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6557 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6559 return NT_STATUS_UNSUCCESSFUL
;
6562 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6563 domains
, num_domains
);
6565 if (*domains
== NULL
) {
6566 DEBUG(1, ("talloc failed\n"));
6567 return NT_STATUS_NO_MEMORY
;
6571 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6572 return NT_STATUS_OK
;
6576 /**********************************************************************
6578 *********************************************************************/
6580 static void free_private_data(void **vp
)
6582 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6584 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6586 if ((*ldap_state
)->result
!= NULL
) {
6587 ldap_msgfree((*ldap_state
)->result
);
6588 (*ldap_state
)->result
= NULL
;
6590 if ((*ldap_state
)->domain_dn
!= NULL
) {
6591 SAFE_FREE((*ldap_state
)->domain_dn
);
6596 /* No need to free any further, as it is talloc()ed */
6599 /*********************************************************************
6600 Intitalise the parts of the pdb_methods structure that are common to
6602 *********************************************************************/
6604 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6607 struct ldapsam_privates
*ldap_state
;
6608 char *bind_dn
= NULL
;
6609 char *bind_secret
= NULL
;
6611 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6615 (*pdb_method
)->name
= "ldapsam";
6617 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6618 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6619 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6620 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6621 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6622 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6624 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6625 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6626 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6627 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6628 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6629 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6630 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6632 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6633 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6635 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6637 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6638 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6640 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6641 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6642 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6643 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6645 /* TODO: Setup private data and free */
6647 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6648 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6649 return NT_STATUS_NO_MEMORY
;
6652 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6653 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6654 return NT_STATUS_NO_MEMORY
;
6657 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6658 location
, false, bind_dn
, bind_secret
,
6659 &ldap_state
->smbldap_state
);
6660 memset(bind_secret
, '\0', strlen(bind_secret
));
6661 SAFE_FREE(bind_secret
);
6663 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6667 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6668 return NT_STATUS_NO_MEMORY
;
6671 (*pdb_method
)->private_data
= ldap_state
;
6673 (*pdb_method
)->free_private_data
= free_private_data
;
6675 return NT_STATUS_OK
;
6678 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods
*m
)
6683 /**********************************************************************
6684 Initialise the normal mode for pdb_ldap
6685 *********************************************************************/
6687 NTSTATUS
pdb_ldapsam_init_common(struct pdb_methods
**pdb_method
,
6688 const char *location
)
6691 struct ldapsam_privates
*ldap_state
= NULL
;
6692 uint32_t alg_rid_base
;
6693 char *alg_rid_base_string
= NULL
;
6694 LDAPMessage
*result
= NULL
;
6695 LDAPMessage
*entry
= NULL
;
6696 struct dom_sid ldap_domain_sid
;
6697 struct dom_sid secrets_domain_sid
;
6698 char *domain_sid_string
= NULL
;
6700 char *uri
= talloc_strdup( NULL
, location
);
6702 trim_char( uri
, '\"', '\"' );
6703 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6707 if (!NT_STATUS_IS_OK(nt_status
)) {
6711 (*pdb_method
)->name
= "ldapsam";
6713 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6714 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6715 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6716 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6717 (*pdb_method
)->search_users
= ldapsam_search_users
;
6718 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6719 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6720 (*pdb_method
)->is_responsible_for_wellknown
=
6721 ldapsam_is_responsible_for_wellknown
;
6723 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6724 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6725 (*pdb_method
)->enum_group_memberships
=
6726 ldapsam_enum_group_memberships
;
6727 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6728 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6729 (*pdb_method
)->id_to_sid
= ldapsam_id_to_sid
;
6731 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6732 (*pdb_method
)->create_user
= ldapsam_create_user
;
6733 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6734 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6735 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6736 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6737 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6738 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6742 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6743 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6745 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6747 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6749 ldap_state
->domain_name
, True
);
6751 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6752 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6753 "info, nor add one to the domain. "
6754 "We cannot work reliably without it.\n"));
6755 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6758 /* Given that the above might fail, everything below this must be
6761 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
6764 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6766 ldap_msgfree(result
);
6767 return NT_STATUS_UNSUCCESSFUL
;
6770 dn
= smbldap_talloc_dn(talloc_tos(),
6771 smbldap_get_ldap(ldap_state
->smbldap_state
),
6774 ldap_msgfree(result
);
6775 return NT_STATUS_UNSUCCESSFUL
;
6778 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6781 domain_sid_string
= smbldap_talloc_single_attribute(
6782 smbldap_get_ldap(ldap_state
->smbldap_state
),
6784 get_userattr_key2string(ldap_state
->schema_ver
,
6785 LDAP_ATTR_USER_SID
),
6788 if (domain_sid_string
) {
6790 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6791 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6792 "read as a valid SID\n", domain_sid_string
));
6793 ldap_msgfree(result
);
6794 TALLOC_FREE(domain_sid_string
);
6795 return NT_STATUS_INVALID_PARAMETER
;
6797 found_sid
= PDB_secrets_fetch_domain_sid(ldap_state
->domain_name
,
6798 &secrets_domain_sid
);
6799 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6800 &ldap_domain_sid
)) {
6801 struct dom_sid_buf buf1
, buf2
;
6802 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6803 "%s based on pdb_ldap results %s -> %s\n",
6804 ldap_state
->domain_name
,
6805 dom_sid_str_buf(&secrets_domain_sid
, &buf1
),
6806 dom_sid_str_buf(&ldap_domain_sid
, &buf2
)));
6808 /* reset secrets.tdb sid */
6809 PDB_secrets_store_domain_sid(ldap_state
->domain_name
,
6811 DEBUG(1, ("New global sam SID: %s\n",
6812 dom_sid_str_buf(get_global_sam_sid(),
6815 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6816 TALLOC_FREE(domain_sid_string
);
6819 alg_rid_base_string
= smbldap_talloc_single_attribute(
6820 smbldap_get_ldap(ldap_state
->smbldap_state
),
6822 get_attr_key2string( dominfo_attr_list
,
6823 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6825 if (alg_rid_base_string
) {
6826 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6827 if (alg_rid_base
!= algorithmic_rid_base()) {
6828 DEBUG(0, ("The value of 'algorithmic RID base' has "
6829 "changed since the LDAP\n"
6830 "database was initialised. Aborting. \n"));
6831 ldap_msgfree(result
);
6832 TALLOC_FREE(alg_rid_base_string
);
6833 return NT_STATUS_UNSUCCESSFUL
;
6835 TALLOC_FREE(alg_rid_base_string
);
6837 ldap_msgfree(result
);
6839 return NT_STATUS_OK
;
6842 NTSTATUS
pdb_ldapsam_init(TALLOC_CTX
*ctx
)
6846 nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
,
6848 pdb_ldapsam_init_common
);
6849 if (!NT_STATUS_IS_OK(nt_status
)) {
6853 /* Let pdb_nds register backends */
6856 return NT_STATUS_OK
;