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
= strtoul_err(temp
, NULL
, 10, &error
);
1001 DBG_ERR("Failed to convert UID\n");
1006 temp
= smbldap_talloc_single_attribute(
1007 priv2ld(ldap_state
),
1012 /* We've got a uid, feed the cache */
1013 unix_pw
.pw_gid
= strtoul_err(temp
, NULL
, 10, &error
);
1015 DBG_ERR("Failed to convert GID\n");
1020 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1021 priv2ld(ldap_state
),
1025 if (unix_pw
.pw_gecos
== NULL
) {
1026 unix_pw
.pw_gecos
= fullname
;
1028 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1029 priv2ld(ldap_state
),
1033 if (unix_pw
.pw_dir
== NULL
) {
1034 unix_pw
.pw_dir
= discard_const_p(char, "");
1036 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state
),
1041 if (unix_pw
.pw_shell
== NULL
) {
1042 unix_pw
.pw_shell
= discard_const_p(char, "");
1045 if (have_uid
&& have_gid
) {
1046 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1048 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1051 if (sampass
->unix_pw
== NULL
) {
1052 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1053 pdb_get_username(sampass
)));
1057 id
.id
= sampass
->unix_pw
->pw_uid
;
1058 id
.type
= ID_TYPE_UID
;
1060 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass
), &id
);
1062 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1063 primary_gsid
= pdb_get_group_sid(sampass
);
1064 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1065 id
.id
= sampass
->unix_pw
->pw_gid
;
1066 id
.type
= ID_TYPE_GID
;
1068 idmap_cache_set_sid2unixid(primary_gsid
, &id
);
1072 /* check the timestamp of the cache vs ldap entry */
1073 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1079 /* see if we have newer updates */
1080 if (!login_cache_read(sampass
, &cache_entry
)) {
1081 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1082 (unsigned int)pdb_get_bad_password_count(sampass
),
1083 (unsigned int)pdb_get_bad_password_time(sampass
)));
1088 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1089 (unsigned int)ldap_entry_time
,
1090 (unsigned int)cache_entry
.entry_timestamp
,
1091 (unsigned int)cache_entry
.bad_password_time
));
1093 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1094 /* cache is older than directory , so
1095 we need to delete the entry but allow the
1096 fields to be written out */
1097 login_cache_delentry(sampass
);
1100 pdb_set_acct_ctrl(sampass
,
1101 pdb_get_acct_ctrl(sampass
) |
1102 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1104 pdb_set_bad_password_count(sampass
,
1105 cache_entry
.bad_password_count
,
1107 pdb_set_bad_password_time(sampass
,
1108 cache_entry
.bad_password_time
,
1120 /**********************************************************************
1121 Initialize the ldap db from a struct samu. Called on update.
1122 (Based on init_buffer_from_sam in pdb_tdb.c)
1123 *********************************************************************/
1125 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1126 LDAPMessage
*existing
,
1127 LDAPMod
*** mods
, struct samu
* sampass
,
1128 bool (*need_update
)(const struct samu
*,
1133 if (mods
== NULL
|| sampass
== NULL
) {
1134 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1141 * took out adding "objectclass: sambaAccount"
1142 * do this on a per-mod basis
1144 if (need_update(sampass
, PDB_USERNAME
)) {
1145 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1147 "uid", pdb_get_username(sampass
));
1148 if (ldap_state
->is_nds_ldap
) {
1150 smbldap_get_ldap(ldap_state
->smbldap_state
),
1152 "cn", pdb_get_username(sampass
));
1154 smbldap_get_ldap(ldap_state
->smbldap_state
),
1156 "sn", pdb_get_username(sampass
));
1160 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1162 /* only update the RID if we actually need to */
1163 if (need_update(sampass
, PDB_USERSID
)) {
1164 struct dom_sid_buf sid_str
;
1165 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1167 switch ( ldap_state
->schema_ver
) {
1168 case SCHEMAVER_SAMBASAMACCOUNT
:
1171 ldap_state
->smbldap_state
),
1173 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1174 dom_sid_str_buf(user_sid
, &sid_str
));
1178 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1183 /* we don't need to store the primary group RID - so leaving it
1184 'free' to hang off the unix primary group makes life easier */
1186 if (need_update(sampass
, PDB_GROUPSID
)) {
1187 struct dom_sid_buf sid_str
;
1188 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1190 switch ( ldap_state
->schema_ver
) {
1191 case SCHEMAVER_SAMBASAMACCOUNT
:
1194 ldap_state
->smbldap_state
),
1196 get_userattr_key2string(ldap_state
->schema_ver
,
1197 LDAP_ATTR_PRIMARY_GROUP_SID
),
1198 dom_sid_str_buf(group_sid
, &sid_str
));
1202 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1208 /* displayName, cn, and gecos should all be the same
1209 * most easily accomplished by giving them the same OID
1210 * gecos isn't set here b/c it should be handled by the
1212 * We change displayName only and fall back to cn if
1213 * it does not exist.
1216 if (need_update(sampass
, PDB_FULLNAME
))
1217 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1219 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1220 pdb_get_fullname(sampass
));
1222 if (need_update(sampass
, PDB_ACCTDESC
))
1223 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1225 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1226 pdb_get_acct_desc(sampass
));
1228 if (need_update(sampass
, PDB_WORKSTATIONS
))
1229 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1231 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1232 pdb_get_workstations(sampass
));
1234 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1235 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1237 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1238 pdb_get_munged_dial(sampass
));
1240 if (need_update(sampass
, PDB_SMBHOME
))
1241 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1243 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1244 pdb_get_homedir(sampass
));
1246 if (need_update(sampass
, PDB_DRIVE
))
1247 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1249 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1250 pdb_get_dir_drive(sampass
));
1252 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1253 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1255 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1256 pdb_get_logon_script(sampass
));
1258 if (need_update(sampass
, PDB_PROFILE
))
1259 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1261 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1262 pdb_get_profile_path(sampass
));
1264 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1267 if (need_update(sampass
, PDB_LOGONTIME
))
1268 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1270 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1273 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1276 if (need_update(sampass
, PDB_LOGOFFTIME
))
1277 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1279 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1282 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1285 if (need_update(sampass
, PDB_KICKOFFTIME
))
1286 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1288 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1291 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1294 if (need_update(sampass
, PDB_CANCHANGETIME
))
1295 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1297 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1300 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1301 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1303 if (need_update(sampass
, PDB_LMPASSWD
)) {
1304 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1307 pdb_sethexpwd(pwstr
, lm_pw
,
1308 pdb_get_acct_ctrl(sampass
));
1311 ldap_state
->smbldap_state
),
1313 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1318 ldap_state
->smbldap_state
),
1320 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1324 if (need_update(sampass
, PDB_NTPASSWD
)) {
1325 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1328 pdb_sethexpwd(pwstr
, nt_pw
,
1329 pdb_get_acct_ctrl(sampass
));
1332 ldap_state
->smbldap_state
),
1334 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1339 ldap_state
->smbldap_state
),
1341 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1346 if (need_update(sampass
, PDB_PWHISTORY
)) {
1348 uint32_t pwHistLen
= 0;
1349 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1351 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1355 if (pwHistLen
== 0) {
1356 /* Remove any password history from the LDAP store. */
1357 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1361 uint32_t currHistLen
= 0;
1362 const uint8_t *pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1363 if (pwhist
!= NULL
) {
1364 /* We can only store (1024-1/64 password history entries. */
1365 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1366 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1367 /* Store the salt. */
1368 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1369 /* Followed by the md5 hash of salt + md4 hash */
1370 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1371 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1372 DEBUG(100, ("pwstr=%s\n", pwstr
));
1377 smbldap_get_ldap(ldap_state
->smbldap_state
),
1379 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1384 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1385 if (asprintf(&temp
, "%li",
1386 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1390 smbldap_get_ldap(ldap_state
->smbldap_state
),
1392 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1398 if (need_update(sampass
, PDB_HOURS
)) {
1399 const uint8_t *hours
= pdb_get_hours(sampass
);
1402 pdb_sethexhours(hourstr
, hours
);
1404 smbldap_get_ldap(ldap_state
->smbldap_state
),
1407 get_userattr_key2string(ldap_state
->schema_ver
,
1408 LDAP_ATTR_LOGON_HOURS
),
1413 if (need_update(sampass
, PDB_ACCTCTRL
))
1415 smbldap_get_ldap(ldap_state
->smbldap_state
),
1417 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1418 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1420 /* password lockout cache:
1421 - If we are now autolocking or clearing, we write to ldap
1422 - If we are clearing, we delete the cache entry
1423 - If the count is > 0, we update the cache
1425 This even means when autolocking, we cache, just in case the
1426 update doesn't work, and we have to cache the autolock flag */
1428 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1429 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1430 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1431 time_t badtime
= pdb_get_bad_password_time(sampass
);
1433 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1435 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1436 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1438 if ((badcount
>= pol
) || (badcount
== 0)) {
1439 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1440 (unsigned int)badcount
, (unsigned int)badtime
));
1441 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1445 smbldap_get_ldap(ldap_state
->smbldap_state
),
1447 get_userattr_key2string(
1448 ldap_state
->schema_ver
,
1449 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1453 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1457 smbldap_get_ldap(ldap_state
->smbldap_state
),
1459 get_userattr_key2string(
1460 ldap_state
->schema_ver
,
1461 LDAP_ATTR_BAD_PASSWORD_TIME
),
1465 if (badcount
== 0) {
1466 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1467 login_cache_delentry(sampass
);
1469 struct login_cache cache_entry
;
1471 cache_entry
.entry_timestamp
= time(NULL
);
1472 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1473 cache_entry
.bad_password_count
= badcount
;
1474 cache_entry
.bad_password_time
= badtime
;
1476 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1477 login_cache_write(sampass
, &cache_entry
);
1484 /**********************************************************************
1485 End enumeration of the LDAP password list.
1486 *********************************************************************/
1488 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1490 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1491 if (ldap_state
->result
) {
1492 ldap_msgfree(ldap_state
->result
);
1493 ldap_state
->result
= NULL
;
1497 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1498 const char *new_attr
)
1502 if (new_attr
== NULL
) {
1506 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1510 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1512 SMB_ASSERT((*attr_list
) != NULL
);
1513 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1514 (*attr_list
)[i
+1] = NULL
;
1517 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1518 const char ***attr_list
)
1520 append_attr(mem_ctx
, attr_list
, "uidNumber");
1521 append_attr(mem_ctx
, attr_list
, "gidNumber");
1522 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1523 append_attr(mem_ctx
, attr_list
, "loginShell");
1524 append_attr(mem_ctx
, attr_list
, "gecos");
1527 /**********************************************************************
1528 Get struct samu entry from LDAP by username.
1529 *********************************************************************/
1531 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1533 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1534 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1535 LDAPMessage
*result
= NULL
;
1536 LDAPMessage
*entry
= NULL
;
1538 const char ** attr_list
;
1541 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1542 append_attr(user
, &attr_list
,
1543 get_userattr_key2string(ldap_state
->schema_ver
,
1544 LDAP_ATTR_MOD_TIMESTAMP
));
1545 ldapsam_add_unix_attributes(user
, &attr_list
);
1546 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1548 TALLOC_FREE( attr_list
);
1550 if ( rc
!= LDAP_SUCCESS
)
1551 return NT_STATUS_NO_SUCH_USER
;
1553 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1557 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1558 ldap_msgfree(result
);
1559 return NT_STATUS_NO_SUCH_USER
;
1560 } else if (count
> 1) {
1561 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1562 ldap_msgfree(result
);
1563 return NT_STATUS_NO_SUCH_USER
;
1566 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1569 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1570 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1571 ldap_msgfree(result
);
1572 return NT_STATUS_NO_SUCH_USER
;
1574 pdb_set_backend_private_data(user
, result
, NULL
,
1575 my_methods
, PDB_CHANGED
);
1576 smbldap_talloc_autofree_ldapmsg(user
, result
);
1579 ldap_msgfree(result
);
1584 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1585 const struct dom_sid
*sid
, LDAPMessage
**result
)
1588 const char ** attr_list
;
1590 switch ( ldap_state
->schema_ver
) {
1591 case SCHEMAVER_SAMBASAMACCOUNT
: {
1592 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1593 if (tmp_ctx
== NULL
) {
1594 return LDAP_NO_MEMORY
;
1597 attr_list
= get_userattr_list(tmp_ctx
,
1598 ldap_state
->schema_ver
);
1599 append_attr(tmp_ctx
, &attr_list
,
1600 get_userattr_key2string(
1601 ldap_state
->schema_ver
,
1602 LDAP_ATTR_MOD_TIMESTAMP
));
1603 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1604 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1606 TALLOC_FREE(tmp_ctx
);
1608 if ( rc
!= LDAP_SUCCESS
)
1614 DEBUG(0,("Invalid schema version specified\n"));
1620 /**********************************************************************
1621 Get struct samu entry from LDAP by SID.
1622 *********************************************************************/
1624 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1626 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1627 LDAPMessage
*result
= NULL
;
1628 LDAPMessage
*entry
= NULL
;
1632 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1634 if (rc
!= LDAP_SUCCESS
)
1635 return NT_STATUS_NO_SUCH_USER
;
1637 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1641 struct dom_sid_buf buf
;
1642 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1644 dom_sid_str_buf(sid
, &buf
),
1646 ldap_msgfree(result
);
1647 return NT_STATUS_NO_SUCH_USER
;
1648 } else if (count
> 1) {
1649 struct dom_sid_buf buf
;
1650 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1651 "[%s]. Failing. count=%d\n",
1652 dom_sid_str_buf(sid
, &buf
),
1654 ldap_msgfree(result
);
1655 return NT_STATUS_NO_SUCH_USER
;
1658 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1661 ldap_msgfree(result
);
1662 return NT_STATUS_NO_SUCH_USER
;
1665 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1666 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1667 ldap_msgfree(result
);
1668 return NT_STATUS_NO_SUCH_USER
;
1671 pdb_set_backend_private_data(user
, result
, NULL
,
1672 my_methods
, PDB_CHANGED
);
1673 smbldap_talloc_autofree_ldapmsg(user
, result
);
1674 return NT_STATUS_OK
;
1677 /********************************************************************
1678 Do the actual modification - also change a plaintext passord if
1680 **********************************************************************/
1682 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1683 struct samu
*newpwd
, char *dn
,
1684 LDAPMod
**mods
, int ldap_op
,
1685 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1687 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1690 if (!newpwd
|| !dn
) {
1691 return NT_STATUS_INVALID_PARAMETER
;
1694 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1695 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1696 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1697 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1700 char *retoid
= NULL
;
1701 struct berval
*retdata
= NULL
;
1702 char *utf8_password
;
1704 size_t converted_size
;
1707 if (!ldap_state
->is_nds_ldap
) {
1709 if (!smbldap_has_extension(
1711 ldap_state
->smbldap_state
),
1712 LDAP_EXOP_MODIFY_PASSWD
)) {
1713 DEBUG(2, ("ldap password change requested, but LDAP "
1714 "server does not support it -- ignoring\n"));
1715 return NT_STATUS_OK
;
1719 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1720 pdb_get_plaintext_passwd(newpwd
),
1723 return NT_STATUS_NO_MEMORY
;
1726 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1727 TALLOC_FREE(utf8_password
);
1728 return NT_STATUS_NO_MEMORY
;
1731 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1732 DEBUG(0,("ber_alloc_t returns NULL\n"));
1733 TALLOC_FREE(utf8_password
);
1734 TALLOC_FREE(utf8_dn
);
1735 return NT_STATUS_UNSUCCESSFUL
;
1738 if ((ber_printf (ber
, "{") < 0) ||
1739 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1741 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1744 TALLOC_FREE(utf8_dn
);
1745 TALLOC_FREE(utf8_password
);
1746 return NT_STATUS_UNSUCCESSFUL
;
1749 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1750 ret
= ber_printf(ber
, "ts}",
1751 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1754 ret
= ber_printf(ber
, "}");
1758 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1761 TALLOC_FREE(utf8_dn
);
1762 TALLOC_FREE(utf8_password
);
1763 return NT_STATUS_UNSUCCESSFUL
;
1766 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1767 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1769 TALLOC_FREE(utf8_dn
);
1770 TALLOC_FREE(utf8_password
);
1771 return NT_STATUS_UNSUCCESSFUL
;
1774 TALLOC_FREE(utf8_dn
);
1775 TALLOC_FREE(utf8_password
);
1778 if (!ldap_state
->is_nds_ldap
) {
1779 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1780 LDAP_EXOP_MODIFY_PASSWD
,
1781 bv
, NULL
, NULL
, &retoid
,
1784 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1785 pdb_get_plaintext_passwd(newpwd
));
1787 if (rc
!= LDAP_SUCCESS
) {
1788 char *ld_error
= NULL
;
1790 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1791 DEBUG(3, ("Could not set userPassword "
1792 "attribute due to an objectClass "
1793 "violation -- ignoring\n"));
1795 return NT_STATUS_OK
;
1799 smbldap_get_ldap(ldap_state
->smbldap_state
),
1800 LDAP_OPT_ERROR_STRING
,
1802 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1803 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1804 SAFE_FREE(ld_error
);
1806 #if defined(LDAP_CONSTRAINT_VIOLATION)
1807 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1808 return NT_STATUS_PASSWORD_RESTRICTION
;
1810 return NT_STATUS_UNSUCCESSFUL
;
1812 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1813 #ifdef DEBUG_PASSWORD
1814 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1817 ber_bvfree(retdata
);
1819 ldap_memfree(retoid
);
1825 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1826 /* may be password change below however */
1830 if (ldap_state
->is_nds_ldap
) {
1831 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1835 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1839 rc
= smbldap_add(ldap_state
->smbldap_state
,
1842 case LDAP_MOD_REPLACE
:
1843 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1847 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1849 return NT_STATUS_INVALID_PARAMETER
;
1852 if (rc
!=LDAP_SUCCESS
) {
1853 return NT_STATUS_UNSUCCESSFUL
;
1857 return NT_STATUS_OK
;
1860 /**********************************************************************
1861 Delete entry from LDAP for username.
1862 *********************************************************************/
1864 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1865 struct samu
* sam_acct
)
1867 struct ldapsam_privates
*priv
=
1868 (struct ldapsam_privates
*)my_methods
->private_data
;
1871 LDAPMessage
*msg
, *entry
;
1872 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1873 const char **attr_list
;
1874 TALLOC_CTX
*mem_ctx
;
1877 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1878 return NT_STATUS_INVALID_PARAMETER
;
1881 sname
= pdb_get_username(sam_acct
);
1883 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1886 mem_ctx
= talloc_new(NULL
);
1887 if (mem_ctx
== NULL
) {
1888 DEBUG(0, ("talloc_new failed\n"));
1892 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1893 if (attr_list
== NULL
) {
1897 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1899 if ((rc
!= LDAP_SUCCESS
) ||
1900 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1901 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1902 DEBUG(5, ("Could not find user %s\n", sname
));
1903 result
= NT_STATUS_NO_SUCH_USER
;
1907 rc
= ldapsam_delete_entry(
1908 priv
, mem_ctx
, entry
,
1909 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1910 LDAP_OBJ_SAMBASAMACCOUNT
: 0,
1913 result
= (rc
== LDAP_SUCCESS
) ?
1914 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1917 TALLOC_FREE(mem_ctx
);
1921 /**********************************************************************
1923 *********************************************************************/
1925 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1927 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1928 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1931 LDAPMessage
*result
= NULL
;
1932 LDAPMessage
*entry
= NULL
;
1933 LDAPMod
**mods
= NULL
;
1934 const char **attr_list
;
1936 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1938 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1939 if (pdb_get_username(newpwd
) == NULL
) {
1940 return NT_STATUS_INVALID_PARAMETER
;
1942 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1943 TALLOC_FREE( attr_list
);
1944 if (rc
!= LDAP_SUCCESS
) {
1945 return NT_STATUS_UNSUCCESSFUL
;
1947 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1948 my_methods
, PDB_CHANGED
);
1949 smbldap_talloc_autofree_ldapmsg(newpwd
, result
);
1952 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1954 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1955 return NT_STATUS_UNSUCCESSFUL
;
1958 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1960 dn
= smbldap_talloc_dn(talloc_tos(),
1961 smbldap_get_ldap(ldap_state
->smbldap_state
),
1964 return NT_STATUS_UNSUCCESSFUL
;
1967 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1969 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1970 pdb_element_is_changed
)) {
1971 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1974 ldap_mods_free(mods
,True
);
1975 return NT_STATUS_UNSUCCESSFUL
;
1978 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1979 && (mods
== NULL
)) {
1980 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1981 pdb_get_username(newpwd
)));
1983 return NT_STATUS_OK
;
1986 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
1989 ldap_mods_free(mods
,True
);
1995 * We need to set the backend private data to NULL here. For example
1996 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1997 * same one, and with the explicit delete / add logic for attribute
1998 * values the second time we would use the wrong "old" value which
1999 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2001 * The existing LDAPMessage is still being auto-freed by the
2004 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2007 if (!NT_STATUS_IS_OK(ret
)) {
2011 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2012 pdb_get_username(newpwd
)));
2013 return NT_STATUS_OK
;
2016 /***************************************************************************
2017 Renames a struct samu
2018 - The "rename user script" has full responsibility for changing everything
2019 ***************************************************************************/
2021 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2022 TALLOC_CTX
*tmp_ctx
,
2024 uint32_t member_rid
);
2026 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2027 TALLOC_CTX
*mem_ctx
,
2029 struct dom_sid
**pp_sids
,
2031 uint32_t *p_num_groups
);
2033 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2034 struct samu
*old_acct
,
2035 const char *newname
)
2037 const char *oldname
;
2039 char *rename_script
= NULL
;
2040 fstring oldname_lower
, newname_lower
;
2043 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2044 return NT_STATUS_INVALID_PARAMETER
;
2047 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2048 return NT_STATUS_INVALID_PARAMETER
;
2051 oldname
= pdb_get_username(old_acct
);
2053 /* rename the posix user */
2054 rename_script
= lp_rename_user_script(talloc_tos());
2055 if (rename_script
== NULL
) {
2056 return NT_STATUS_NO_MEMORY
;
2059 if (!(*rename_script
)) {
2060 TALLOC_FREE(rename_script
);
2061 return NT_STATUS_ACCESS_DENIED
;
2064 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2067 /* We have to allow the account name to end with a '$'.
2068 Also, follow the semantics in _samr_create_user() and lower case the
2069 posix name but preserve the case in passdb */
2071 fstrcpy( oldname_lower
, oldname
);
2072 if (!strlower_m( oldname_lower
)) {
2073 return NT_STATUS_INVALID_PARAMETER
;
2075 fstrcpy( newname_lower
, newname
);
2076 if (!strlower_m( newname_lower
)) {
2077 return NT_STATUS_INVALID_PARAMETER
;
2080 rename_script
= realloc_string_sub2(rename_script
,
2085 if (!rename_script
) {
2086 return NT_STATUS_NO_MEMORY
;
2088 rename_script
= realloc_string_sub2(rename_script
,
2093 rc
= smbrun(rename_script
, NULL
, NULL
);
2095 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2096 rename_script
, rc
));
2098 TALLOC_FREE(rename_script
);
2101 smb_nscd_flush_user_cache();
2105 return NT_STATUS_UNSUCCESSFUL
;
2107 return NT_STATUS_OK
;
2110 /**********************************************************************
2111 Add struct samu to LDAP.
2112 *********************************************************************/
2114 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2116 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2117 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2119 LDAPMessage
*result
= NULL
;
2120 LDAPMessage
*entry
= NULL
;
2121 LDAPMod
**mods
= NULL
;
2122 int ldap_op
= LDAP_MOD_REPLACE
;
2123 uint32_t num_result
;
2124 const char **attr_list
;
2125 char *escape_user
= NULL
;
2126 const char *username
= pdb_get_username(newpwd
);
2127 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2128 char *filter
= NULL
;
2130 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2131 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2134 return NT_STATUS_NO_MEMORY
;
2137 if (!username
|| !*username
) {
2138 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2139 status
= NT_STATUS_INVALID_PARAMETER
;
2143 /* free this list after the second search or in case we exit on failure */
2144 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2146 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2148 if (rc
!= LDAP_SUCCESS
) {
2152 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
2154 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2158 ldap_msgfree(result
);
2161 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2162 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2164 if (rc
== LDAP_SUCCESS
) {
2165 if (ldap_count_entries(
2167 ldap_state
->smbldap_state
),
2169 struct dom_sid_buf buf
;
2170 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2171 "already in the base, with samba "
2173 dom_sid_str_buf(sid
, &buf
)));
2176 ldap_msgfree(result
);
2181 /* does the entry already exist but without a samba attributes?
2182 we need to return the samba attributes here */
2184 escape_user
= escape_ldap_string(talloc_tos(), username
);
2185 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2187 status
= NT_STATUS_NO_MEMORY
;
2190 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2191 TALLOC_FREE(escape_user
);
2193 status
= NT_STATUS_NO_MEMORY
;
2197 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2198 filter
, attr_list
, &result
);
2199 if ( rc
!= LDAP_SUCCESS
) {
2203 num_result
= ldap_count_entries(
2204 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2206 if (num_result
> 1) {
2207 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2211 /* Check if we need to update an existing entry */
2212 if (num_result
== 1) {
2213 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2214 ldap_op
= LDAP_MOD_REPLACE
;
2215 entry
= ldap_first_entry(
2216 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2217 dn
= smbldap_talloc_dn(
2218 ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
),
2221 status
= NT_STATUS_NO_MEMORY
;
2225 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2227 struct dom_sid_buf buf
;
2229 /* There might be a SID for this account already - say an idmap entry */
2231 filter
= talloc_asprintf(ctx
,
2232 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2233 get_userattr_key2string(ldap_state
->schema_ver
,
2234 LDAP_ATTR_USER_SID
),
2235 dom_sid_str_buf(sid
, &buf
),
2236 LDAP_OBJ_IDMAP_ENTRY
,
2237 LDAP_OBJ_SID_ENTRY
);
2239 status
= NT_STATUS_NO_MEMORY
;
2243 /* free old result before doing a new search */
2244 if (result
!= NULL
) {
2245 ldap_msgfree(result
);
2248 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2249 filter
, attr_list
, &result
);
2251 if ( rc
!= LDAP_SUCCESS
) {
2255 num_result
= ldap_count_entries(
2256 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2258 if (num_result
> 1) {
2259 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2263 /* Check if we need to update an existing entry */
2264 if (num_result
== 1) {
2266 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2267 ldap_op
= LDAP_MOD_REPLACE
;
2268 entry
= ldap_first_entry (
2269 smbldap_get_ldap(ldap_state
->smbldap_state
),
2271 dn
= smbldap_talloc_dn (
2273 smbldap_get_ldap(ldap_state
->smbldap_state
),
2276 status
= NT_STATUS_NO_MEMORY
;
2282 if (num_result
== 0) {
2283 char *escape_username
;
2284 /* Check if we need to add an entry */
2285 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2286 ldap_op
= LDAP_MOD_ADD
;
2288 escape_username
= escape_rdn_val_string_alloc(username
);
2289 if (!escape_username
) {
2290 status
= NT_STATUS_NO_MEMORY
;
2294 if (username
[strlen(username
)-1] == '$') {
2295 dn
= talloc_asprintf(ctx
,
2298 lp_ldap_machine_suffix(talloc_tos()));
2300 dn
= talloc_asprintf(ctx
,
2303 lp_ldap_user_suffix(talloc_tos()));
2306 SAFE_FREE(escape_username
);
2308 status
= NT_STATUS_NO_MEMORY
;
2313 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2314 pdb_element_is_set_or_changed
)) {
2315 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2317 ldap_mods_free(mods
, true);
2323 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2326 switch ( ldap_state
->schema_ver
) {
2327 case SCHEMAVER_SAMBASAMACCOUNT
:
2328 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2331 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2335 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2336 if (!NT_STATUS_IS_OK(ret
)) {
2337 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2338 pdb_get_username(newpwd
),dn
));
2339 ldap_mods_free(mods
, true);
2343 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2344 ldap_mods_free(mods
, true);
2346 status
= NT_STATUS_OK
;
2352 ldap_msgfree(result
);
2357 /**********************************************************************
2358 *********************************************************************/
2360 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2362 LDAPMessage
** result
)
2364 int scope
= LDAP_SCOPE_SUBTREE
;
2366 const char **attr_list
;
2368 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2369 rc
= smbldap_search(ldap_state
->smbldap_state
,
2370 lp_ldap_suffix (talloc_tos()), scope
,
2371 filter
, attr_list
, 0, result
);
2372 TALLOC_FREE(attr_list
);
2377 /**********************************************************************
2378 *********************************************************************/
2380 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2381 GROUP_MAP
*map
, LDAPMessage
*entry
)
2384 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2386 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2387 smbldap_get_ldap(ldap_state
->smbldap_state
) == NULL
) {
2388 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2393 temp
= smbldap_talloc_single_attribute(
2394 smbldap_get_ldap(ldap_state
->smbldap_state
),
2396 get_attr_key2string(groupmap_attr_list
,
2397 LDAP_ATTR_GIDNUMBER
),
2400 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2401 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2405 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2407 map
->gid
= (gid_t
)atol(temp
);
2410 temp
= smbldap_talloc_single_attribute(
2411 smbldap_get_ldap(ldap_state
->smbldap_state
),
2413 get_attr_key2string(groupmap_attr_list
,
2414 LDAP_ATTR_GROUP_SID
),
2417 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2418 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2423 if (!string_to_sid(&map
->sid
, temp
)) {
2424 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2430 temp
= smbldap_talloc_single_attribute(
2431 smbldap_get_ldap(ldap_state
->smbldap_state
),
2433 get_attr_key2string(groupmap_attr_list
,
2434 LDAP_ATTR_GROUP_TYPE
),
2437 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2438 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2442 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2444 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2445 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2446 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2452 temp
= smbldap_talloc_single_attribute(
2453 smbldap_get_ldap(ldap_state
->smbldap_state
),
2455 get_attr_key2string(groupmap_attr_list
,
2456 LDAP_ATTR_DISPLAY_NAME
),
2459 temp
= smbldap_talloc_single_attribute(
2460 smbldap_get_ldap(ldap_state
->smbldap_state
),
2462 get_attr_key2string(groupmap_attr_list
,
2466 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2467 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2472 map
->nt_name
= talloc_strdup(map
, temp
);
2473 if (!map
->nt_name
) {
2479 temp
= smbldap_talloc_single_attribute(
2480 smbldap_get_ldap(ldap_state
->smbldap_state
),
2482 get_attr_key2string(groupmap_attr_list
,
2486 temp
= talloc_strdup(ctx
, "");
2492 map
->comment
= talloc_strdup(map
, temp
);
2493 if (!map
->comment
) {
2498 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2501 id
.type
= ID_TYPE_GID
;
2503 idmap_cache_set_sid2unixid(&map
->sid
, &id
);
2510 /**********************************************************************
2511 *********************************************************************/
2513 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2517 struct ldapsam_privates
*ldap_state
=
2518 (struct ldapsam_privates
*)methods
->private_data
;
2519 LDAPMessage
*result
= NULL
;
2520 LDAPMessage
*entry
= NULL
;
2523 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2525 return NT_STATUS_NO_SUCH_GROUP
;
2528 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2531 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2533 ldap_msgfree(result
);
2534 return NT_STATUS_NO_SUCH_GROUP
;
2538 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2539 "count=%d\n", filter
, count
));
2540 ldap_msgfree(result
);
2541 return NT_STATUS_NO_SUCH_GROUP
;
2544 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2547 ldap_msgfree(result
);
2548 return NT_STATUS_UNSUCCESSFUL
;
2551 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2552 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2553 "group filter %s\n", filter
));
2554 ldap_msgfree(result
);
2555 return NT_STATUS_NO_SUCH_GROUP
;
2558 ldap_msgfree(result
);
2559 return NT_STATUS_OK
;
2562 /**********************************************************************
2563 *********************************************************************/
2565 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2568 char *filter
= NULL
;
2570 struct dom_sid_buf tmp
;
2572 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2574 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2575 dom_sid_str_buf(&sid
, &tmp
)) < 0) {
2576 return NT_STATUS_NO_MEMORY
;
2579 status
= ldapsam_getgroup(methods
, filter
, map
);
2584 /**********************************************************************
2585 *********************************************************************/
2587 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2590 char *filter
= NULL
;
2593 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2595 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2596 (unsigned long)gid
) < 0) {
2597 return NT_STATUS_NO_MEMORY
;
2600 status
= ldapsam_getgroup(methods
, filter
, map
);
2605 /**********************************************************************
2606 *********************************************************************/
2608 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2611 char *filter
= NULL
;
2612 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2616 return NT_STATUS_NO_MEMORY
;
2619 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2621 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2622 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2624 TALLOC_FREE(escape_name
);
2625 return NT_STATUS_NO_MEMORY
;
2628 TALLOC_FREE(escape_name
);
2629 status
= ldapsam_getgroup(methods
, filter
, map
);
2634 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2636 const struct dom_sid
*domain_sid
,
2642 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2643 str
, sizeof(str
)-1)) {
2644 DEBUG(10, ("Could not find sambaSID attribute\n"));
2648 if (!string_to_sid(&sid
, str
)) {
2649 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2653 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2654 struct dom_sid_buf buf
;
2655 DEBUG(10, ("SID %s is not in expected domain %s\n",
2657 dom_sid_str_buf(domain_sid
, &buf
)));
2661 if (!sid_peek_rid(&sid
, rid
)) {
2662 DEBUG(10, ("Could not peek into RID\n"));
2669 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2670 TALLOC_CTX
*mem_ctx
,
2671 const struct dom_sid
*group
,
2672 uint32_t **pp_member_rids
,
2673 size_t *p_num_members
)
2675 struct ldapsam_privates
*ldap_state
=
2676 (struct ldapsam_privates
*)methods
->private_data
;
2677 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2678 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2679 const char *sid_attrs
[] = { "sambaSID", NULL
};
2680 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2681 LDAPMessage
*result
= NULL
;
2684 char **values
= NULL
;
2688 struct dom_sid_buf buf
;
2690 *pp_member_rids
= NULL
;
2693 filter
= talloc_asprintf(mem_ctx
,
2694 "(&(objectClass=%s)"
2697 LDAP_OBJ_POSIXGROUP
,
2699 dom_sid_str_buf(group
, &buf
));
2700 if (filter
== NULL
) {
2701 ret
= NT_STATUS_NO_MEMORY
;
2705 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2706 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2709 if (rc
!= LDAP_SUCCESS
)
2712 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2714 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2717 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2718 dom_sid_str_buf(group
, &buf
)));
2719 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2724 ret
= NT_STATUS_NO_SUCH_GROUP
;
2728 entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2732 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2734 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2735 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2739 values
= ldap_get_values(smbldap_get_ldap(conn
), entry
, "memberUid");
2741 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2743 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2744 if (filter
== NULL
) {
2745 ret
= NT_STATUS_NO_MEMORY
;
2749 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2750 char *escape_memberuid
;
2752 escape_memberuid
= escape_ldap_string(talloc_tos(),
2754 if (escape_memberuid
== NULL
) {
2755 ret
= NT_STATUS_NO_MEMORY
;
2759 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2760 TALLOC_FREE(escape_memberuid
);
2761 if (filter
== NULL
) {
2762 ret
= NT_STATUS_NO_MEMORY
;
2767 filter
= talloc_asprintf_append_buffer(filter
, "))");
2768 if (filter
== NULL
) {
2769 ret
= NT_STATUS_NO_MEMORY
;
2773 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2774 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2777 if (rc
!= LDAP_SUCCESS
)
2780 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2781 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2783 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2785 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2787 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2793 sidstr
= smbldap_talloc_single_attribute(
2794 smbldap_get_ldap(conn
), entry
, "sambaSID",
2797 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2798 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2799 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2803 if (!string_to_sid(&sid
, sidstr
))
2806 if (!sid_check_is_in_our_sam(&sid
)) {
2807 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2808 "in our domain\n"));
2809 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2813 sid_peek_rid(&sid
, &rid
);
2815 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2817 ret
= NT_STATUS_NO_MEMORY
;
2823 filter
= talloc_asprintf(mem_ctx
,
2824 "(&(objectClass=%s)"
2826 LDAP_OBJ_SAMBASAMACCOUNT
,
2829 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2830 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2833 if (rc
!= LDAP_SUCCESS
)
2836 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2838 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2840 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2844 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn
),
2846 get_global_sam_sid(),
2848 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2849 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2853 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2855 ret
= NT_STATUS_NO_MEMORY
;
2865 ldap_value_free(values
);
2870 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2871 TALLOC_CTX
*mem_ctx
,
2873 struct dom_sid
**pp_sids
,
2875 uint32_t *p_num_groups
)
2877 struct ldapsam_privates
*ldap_state
=
2878 (struct ldapsam_privates
*)methods
->private_data
;
2879 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2881 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2884 LDAPMessage
*result
= NULL
;
2886 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2890 gid_t primary_gid
= -1;
2896 if (pdb_get_username(user
) == NULL
) {
2897 return NT_STATUS_INVALID_PARAMETER
;
2900 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2901 if (escape_name
== NULL
)
2902 return NT_STATUS_NO_MEMORY
;
2904 if (user
->unix_pw
) {
2905 primary_gid
= user
->unix_pw
->pw_gid
;
2907 /* retrieve the users primary gid */
2908 filter
= talloc_asprintf(mem_ctx
,
2909 "(&(objectClass=%s)(uid=%s))",
2910 LDAP_OBJ_SAMBASAMACCOUNT
,
2912 if (filter
== NULL
) {
2913 ret
= NT_STATUS_NO_MEMORY
;
2917 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2918 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2920 if (rc
!= LDAP_SUCCESS
)
2923 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2925 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2929 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2930 ret
= NT_STATUS_NO_SUCH_USER
;
2933 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2935 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2937 DEBUG (1, ("Unable to find the member's gid!\n"));
2938 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2941 primary_gid
= strtoul_err(gidstr
, NULL
, 10, &error
);
2943 DBG_ERR("Failed to convert GID\n");
2948 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2949 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2954 filter
= talloc_asprintf(mem_ctx
,
2955 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2956 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2957 if (filter
== NULL
) {
2958 ret
= NT_STATUS_NO_MEMORY
;
2962 rc
= smbldap_search(conn
, lp_ldap_suffix(talloc_tos()),
2963 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2965 if (rc
!= LDAP_SUCCESS
)
2968 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2976 /* We need to add the primary group as the first gid/sid */
2978 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2979 ret
= NT_STATUS_NO_MEMORY
;
2983 /* This sid will be replaced later */
2985 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
2987 if (!NT_STATUS_IS_OK(ret
)) {
2991 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2993 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
3000 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3002 str
, sizeof(str
)-1))
3005 if (!string_to_sid(&sid
, str
))
3008 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3010 str
, sizeof(str
)-1))
3013 gid
= strtoul_err(str
, &end
, 10, &error
);
3015 if ((PTR_DIFF(end
, str
) != strlen(str
)) || (error
!= 0)) {
3019 if (gid
== primary_gid
) {
3020 sid_copy(&(*pp_sids
)[0], &sid
);
3022 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3024 ret
= NT_STATUS_NO_MEMORY
;
3027 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3029 if (!NT_STATUS_IS_OK(ret
)) {
3035 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3036 DEBUG(3, ("primary group of [%s] not found\n",
3037 pdb_get_username(user
)));
3038 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
3042 *p_num_groups
= num_sids
;
3048 TALLOC_FREE(escape_name
);
3052 /**********************************************************************
3053 * Augment a posixGroup object with a sambaGroupMapping domgroup
3054 *********************************************************************/
3056 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3057 struct ldapsam_privates
*ldap_state
,
3060 const char *filter
, *dn
;
3061 LDAPMessage
*msg
, *entry
;
3063 struct dom_sid_buf buf
;
3066 filter
= talloc_asprintf(mem_ctx
,
3067 "(&(objectClass=%s)(gidNumber=%u))",
3068 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3069 if (filter
== NULL
) {
3070 return NT_STATUS_NO_MEMORY
;
3073 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3074 get_attr_list(mem_ctx
, groupmap_attr_list
),
3076 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3078 if ((rc
!= LDAP_SUCCESS
) ||
3079 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3081 ((entry
= ldap_first_entry(
3082 smbldap_get_ldap(ldap_state
->smbldap_state
),
3084 return NT_STATUS_NO_SUCH_GROUP
;
3087 dn
= smbldap_talloc_dn(mem_ctx
,
3088 smbldap_get_ldap(ldap_state
->smbldap_state
),
3091 return NT_STATUS_NO_MEMORY
;
3095 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3097 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3099 dom_sid_str_buf(&map
->sid
, &buf
));
3100 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3101 &mods
, "sambaGroupType",
3102 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3103 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3104 &mods
, "displayName",
3106 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3107 &mods
, "description",
3109 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3111 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3112 if (rc
!= LDAP_SUCCESS
) {
3113 return NT_STATUS_ACCESS_DENIED
;
3116 return NT_STATUS_OK
;
3119 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3122 struct ldapsam_privates
*ldap_state
=
3123 (struct ldapsam_privates
*)methods
->private_data
;
3124 LDAPMessage
*msg
= NULL
;
3125 LDAPMod
**mods
= NULL
;
3126 const char *attrs
[] = { NULL
};
3130 TALLOC_CTX
*mem_ctx
;
3134 struct dom_sid_buf buf
;
3139 mem_ctx
= talloc_new(NULL
);
3140 if (mem_ctx
== NULL
) {
3141 DEBUG(0, ("talloc_new failed\n"));
3142 return NT_STATUS_NO_MEMORY
;
3145 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3146 dom_sid_str_buf(&map
->sid
, &buf
));
3147 if (filter
== NULL
) {
3148 result
= NT_STATUS_NO_MEMORY
;
3152 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3153 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3154 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3156 if ((rc
== LDAP_SUCCESS
) &&
3157 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3160 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3161 "group mapping entry\n",
3162 dom_sid_str_buf(&map
->sid
, &buf
)));
3163 result
= NT_STATUS_GROUP_EXISTS
;
3167 switch (map
->sid_name_use
) {
3169 case SID_NAME_DOM_GRP
:
3170 /* To map a domain group we need to have a posix group
3172 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3176 case SID_NAME_ALIAS
:
3177 if (!sid_check_is_in_our_sam(&map
->sid
)
3178 && !sid_check_is_in_builtin(&map
->sid
) )
3180 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3181 dom_sid_str_buf(&map
->sid
, &buf
)));
3182 result
= NT_STATUS_INVALID_PARAMETER
;
3188 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3189 sid_type_lookup(map
->sid_name_use
)));
3190 result
= NT_STATUS_INVALID_PARAMETER
;
3194 /* Domain groups have been mapped in a separate routine, we have to
3195 * create an alias now */
3197 if (map
->gid
== -1) {
3198 DEBUG(10, ("Refusing to map gid==-1\n"));
3199 result
= NT_STATUS_INVALID_PARAMETER
;
3204 id
.type
= ID_TYPE_GID
;
3206 if (pdb_id_to_sid(&id
, &sid
)) {
3207 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3209 (unsigned int)map
->gid
,
3210 dom_sid_str_buf(&sid
, &buf
)));
3211 result
= NT_STATUS_GROUP_EXISTS
;
3215 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3216 * the best we can get out of LDAP. */
3218 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3219 dom_sid_str_buf(&map
->sid
, &buf
),
3220 lp_ldap_group_suffix(talloc_tos()));
3222 result
= NT_STATUS_NO_MEMORY
;
3228 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3229 &mods
, "objectClass", LDAP_OBJ_SID_ENTRY
);
3230 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3231 &mods
, "objectClass", LDAP_OBJ_GROUPMAP
);
3232 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3234 dom_sid_str_buf(&map
->sid
, &buf
));
3235 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3236 &mods
, "sambaGroupType",
3237 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3238 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3239 &mods
, "displayName",
3241 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3242 &mods
, "description",
3244 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3246 talloc_asprintf(mem_ctx
, "%u",
3247 (unsigned int)map
->gid
));
3248 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3250 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3252 result
= (rc
== LDAP_SUCCESS
) ?
3253 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3256 TALLOC_FREE(mem_ctx
);
3260 /**********************************************************************
3261 * Update a group mapping entry. We're quite strict about what can be changed:
3262 * Only the description and displayname may be changed. It simply does not
3263 * make any sense to change the SID, gid or the type in a mapping.
3264 *********************************************************************/
3266 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3269 struct ldapsam_privates
*ldap_state
=
3270 (struct ldapsam_privates
*)methods
->private_data
;
3272 const char *filter
, *dn
;
3273 LDAPMessage
*msg
= NULL
;
3274 LDAPMessage
*entry
= NULL
;
3275 LDAPMod
**mods
= NULL
;
3276 TALLOC_CTX
*mem_ctx
;
3278 struct dom_sid_buf buf
;
3280 mem_ctx
= talloc_new(NULL
);
3281 if (mem_ctx
== NULL
) {
3282 DEBUG(0, ("talloc_new failed\n"));
3283 return NT_STATUS_NO_MEMORY
;
3286 /* Make 100% sure that sid, gid and type are not changed by looking up
3287 * exactly the values we're given in LDAP. */
3289 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3290 "(sambaSid=%s)(gidNumber=%u)"
3291 "(sambaGroupType=%d))",
3293 dom_sid_str_buf(&map
->sid
, &buf
),
3294 (unsigned int)map
->gid
, map
->sid_name_use
);
3295 if (filter
== NULL
) {
3296 result
= NT_STATUS_NO_MEMORY
;
3300 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3301 get_attr_list(mem_ctx
, groupmap_attr_list
),
3303 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3305 if ((rc
!= LDAP_SUCCESS
) ||
3306 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3308 ((entry
= ldap_first_entry(
3309 smbldap_get_ldap(ldap_state
->smbldap_state
),
3311 result
= NT_STATUS_NO_SUCH_GROUP
;
3315 dn
= smbldap_talloc_dn(
3316 mem_ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
), entry
);
3319 result
= NT_STATUS_NO_MEMORY
;
3324 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3325 &mods
, "displayName", map
->nt_name
);
3326 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3327 &mods
, "description", map
->comment
);
3328 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3331 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3332 "nothing to do\n"));
3333 result
= NT_STATUS_OK
;
3337 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3339 if (rc
!= LDAP_SUCCESS
) {
3340 result
= NT_STATUS_ACCESS_DENIED
;
3344 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3345 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3347 result
= NT_STATUS_OK
;
3350 TALLOC_FREE(mem_ctx
);
3354 /**********************************************************************
3355 *********************************************************************/
3357 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3360 struct ldapsam_privates
*priv
=
3361 (struct ldapsam_privates
*)methods
->private_data
;
3362 LDAPMessage
*msg
, *entry
;
3365 TALLOC_CTX
*mem_ctx
;
3367 struct dom_sid_buf buf
;
3369 mem_ctx
= talloc_new(NULL
);
3370 if (mem_ctx
== NULL
) {
3371 DEBUG(0, ("talloc_new failed\n"));
3372 return NT_STATUS_NO_MEMORY
;
3375 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3376 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3377 dom_sid_str_buf(&sid
, &buf
));
3378 if (filter
== NULL
) {
3379 result
= NT_STATUS_NO_MEMORY
;
3382 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3383 get_attr_list(mem_ctx
, groupmap_attr_list
),
3385 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3387 if ((rc
!= LDAP_SUCCESS
) ||
3388 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3389 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3390 result
= NT_STATUS_NO_SUCH_GROUP
;
3394 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3395 get_attr_list(mem_ctx
,
3396 groupmap_attr_list_to_delete
));
3398 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3399 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3400 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3401 const char *attrs
[] = { "sambaGroupType", "description",
3402 "displayName", "sambaSIDList",
3405 /* Second try. Don't delete the sambaSID attribute, this is
3406 for "old" entries that are tacked on a winbind
3409 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3410 LDAP_OBJ_GROUPMAP
, attrs
);
3413 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3414 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3415 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3416 const char *attrs
[] = { "sambaGroupType", "description",
3417 "displayName", "sambaSIDList",
3418 "gidNumber", NULL
};
3420 /* Third try. This is a post-3.0.21 alias (containing only
3421 * sambaSidEntry and sambaGroupMapping classes), we also have
3422 * to delete the gidNumber attribute, only the sambaSidEntry
3425 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3426 LDAP_OBJ_GROUPMAP
, attrs
);
3429 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3432 TALLOC_FREE(mem_ctx
);
3436 /**********************************************************************
3437 *********************************************************************/
3439 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3442 struct ldapsam_privates
*ldap_state
=
3443 (struct ldapsam_privates
*)my_methods
->private_data
;
3444 char *filter
= NULL
;
3446 const char **attr_list
;
3448 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3450 return NT_STATUS_NO_MEMORY
;
3452 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3453 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3454 LDAP_SCOPE_SUBTREE
, filter
,
3455 attr_list
, 0, &ldap_state
->result
);
3456 TALLOC_FREE(attr_list
);
3458 if (rc
!= LDAP_SUCCESS
) {
3459 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3460 ldap_err2string(rc
)));
3461 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3462 lp_ldap_suffix(talloc_tos()), filter
));
3463 ldap_msgfree(ldap_state
->result
);
3464 ldap_state
->result
= NULL
;
3465 TALLOC_FREE(filter
);
3466 return NT_STATUS_UNSUCCESSFUL
;
3469 TALLOC_FREE(filter
);
3471 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3473 smbldap_get_ldap(ldap_state
->smbldap_state
),
3474 ldap_state
->result
)));
3477 ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3478 ldap_state
->result
);
3479 ldap_state
->index
= 0;
3481 return NT_STATUS_OK
;
3484 /**********************************************************************
3485 *********************************************************************/
3487 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3489 ldapsam_endsampwent(my_methods
);
3492 /**********************************************************************
3493 *********************************************************************/
3495 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3498 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3499 struct ldapsam_privates
*ldap_state
=
3500 (struct ldapsam_privates
*)my_methods
->private_data
;
3504 if (!ldap_state
->entry
)
3507 ldap_state
->index
++;
3508 bret
= init_group_from_ldap(ldap_state
, map
,
3511 ldap_state
->entry
= ldap_next_entry(
3512 smbldap_get_ldap(ldap_state
->smbldap_state
),
3516 return NT_STATUS_OK
;
3519 /**********************************************************************
3520 *********************************************************************/
3522 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3523 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3524 GROUP_MAP
***pp_rmap
,
3525 size_t *p_num_entries
,
3528 GROUP_MAP
*map
= NULL
;
3534 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3535 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3537 return NT_STATUS_ACCESS_DENIED
;
3542 map
= talloc_zero(NULL
, GROUP_MAP
);
3544 return NT_STATUS_NO_MEMORY
;
3547 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3552 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3553 sid_name_use
!= map
->sid_name_use
) {
3554 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3555 "not of the requested type\n",
3559 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3560 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3561 "non mapped\n", map
->nt_name
));
3565 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3566 GROUP_MAP
*, entries
+ 1);
3568 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3569 "enlarge group map!\n"));
3570 return NT_STATUS_UNSUCCESSFUL
;
3573 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3578 ldapsam_endsamgrent(methods
);
3580 *p_num_entries
= entries
;
3582 return NT_STATUS_OK
;
3585 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3586 const struct dom_sid
*alias
,
3587 const struct dom_sid
*member
,
3590 struct ldapsam_privates
*ldap_state
=
3591 (struct ldapsam_privates
*)methods
->private_data
;
3593 LDAPMessage
*result
= NULL
;
3594 LDAPMessage
*entry
= NULL
;
3596 LDAPMod
**mods
= NULL
;
3598 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3599 struct dom_sid_buf tmp
;
3601 char *filter
= NULL
;
3603 if (sid_check_is_in_builtin(alias
)) {
3604 type
= SID_NAME_ALIAS
;
3607 if (sid_check_is_in_our_sam(alias
)) {
3608 type
= SID_NAME_ALIAS
;
3611 if (type
== SID_NAME_USE_NONE
) {
3612 struct dom_sid_buf buf
;
3613 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3614 dom_sid_str_buf(alias
, &buf
)));
3615 return NT_STATUS_NO_SUCH_ALIAS
;
3618 if (asprintf(&filter
,
3619 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3621 dom_sid_str_buf(alias
, &tmp
),
3623 return NT_STATUS_NO_MEMORY
;
3626 if (ldapsam_search_one_group(ldap_state
, filter
,
3627 &result
) != LDAP_SUCCESS
) {
3629 return NT_STATUS_NO_SUCH_ALIAS
;
3632 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3636 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3637 ldap_msgfree(result
);
3639 return NT_STATUS_NO_SUCH_ALIAS
;
3643 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3644 "filter %s: count=%d\n", filter
, count
));
3645 ldap_msgfree(result
);
3647 return NT_STATUS_NO_SUCH_ALIAS
;
3652 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3656 ldap_msgfree(result
);
3657 return NT_STATUS_UNSUCCESSFUL
;
3660 dn
= smbldap_talloc_dn(talloc_tos(),
3661 smbldap_get_ldap(ldap_state
->smbldap_state
),
3664 ldap_msgfree(result
);
3665 return NT_STATUS_UNSUCCESSFUL
;
3668 smbldap_set_mod(&mods
, modop
,
3669 get_attr_key2string(groupmap_attr_list
,
3670 LDAP_ATTR_SID_LIST
),
3671 dom_sid_str_buf(member
, &tmp
));
3673 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3675 ldap_mods_free(mods
, True
);
3676 ldap_msgfree(result
);
3679 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3680 return NT_STATUS_MEMBER_IN_ALIAS
;
3683 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3684 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3687 if (rc
!= LDAP_SUCCESS
) {
3688 return NT_STATUS_UNSUCCESSFUL
;
3691 return NT_STATUS_OK
;
3694 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3695 const struct dom_sid
*alias
,
3696 const struct dom_sid
*member
)
3698 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3701 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3702 const struct dom_sid
*alias
,
3703 const struct dom_sid
*member
)
3705 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3709 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3710 const struct dom_sid
*alias
,
3711 TALLOC_CTX
*mem_ctx
,
3712 struct dom_sid
**pp_members
,
3713 size_t *p_num_members
)
3715 struct ldapsam_privates
*ldap_state
=
3716 (struct ldapsam_privates
*)methods
->private_data
;
3717 LDAPMessage
*result
= NULL
;
3718 LDAPMessage
*entry
= NULL
;
3720 char **values
= NULL
;
3722 char *filter
= NULL
;
3723 uint32_t num_members
= 0;
3724 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3725 struct dom_sid_buf tmp
;
3730 if (sid_check_is_in_builtin(alias
)) {
3731 type
= SID_NAME_ALIAS
;
3734 if (sid_check_is_in_our_sam(alias
)) {
3735 type
= SID_NAME_ALIAS
;
3738 if (type
== SID_NAME_USE_NONE
) {
3739 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3740 dom_sid_str_buf(alias
, &tmp
)));
3741 return NT_STATUS_NO_SUCH_ALIAS
;
3744 if (asprintf(&filter
,
3745 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3747 dom_sid_str_buf(alias
, &tmp
),
3749 return NT_STATUS_NO_MEMORY
;
3752 if (ldapsam_search_one_group(ldap_state
, filter
,
3753 &result
) != LDAP_SUCCESS
) {
3755 return NT_STATUS_NO_SUCH_ALIAS
;
3758 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3762 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3763 ldap_msgfree(result
);
3765 return NT_STATUS_NO_SUCH_ALIAS
;
3769 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3770 "filter %s: count=%d\n", filter
, count
));
3771 ldap_msgfree(result
);
3773 return NT_STATUS_NO_SUCH_ALIAS
;
3778 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3782 ldap_msgfree(result
);
3783 return NT_STATUS_UNSUCCESSFUL
;
3786 values
= ldap_get_values(smbldap_get_ldap(ldap_state
->smbldap_state
),
3788 get_attr_key2string(groupmap_attr_list
,
3789 LDAP_ATTR_SID_LIST
));
3791 if (values
== NULL
) {
3792 ldap_msgfree(result
);
3793 return NT_STATUS_OK
;
3796 count
= ldap_count_values(values
);
3798 for (i
=0; i
<count
; i
++) {
3799 struct dom_sid member
;
3802 if (!string_to_sid(&member
, values
[i
]))
3805 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3807 if (!NT_STATUS_IS_OK(status
)) {
3808 ldap_value_free(values
);
3809 ldap_msgfree(result
);
3814 *p_num_members
= num_members
;
3815 ldap_value_free(values
);
3816 ldap_msgfree(result
);
3818 return NT_STATUS_OK
;
3821 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3822 TALLOC_CTX
*mem_ctx
,
3823 const struct dom_sid
*domain_sid
,
3824 const struct dom_sid
*members
,
3826 uint32_t **pp_alias_rids
,
3827 size_t *p_num_alias_rids
)
3829 struct ldapsam_privates
*ldap_state
=
3830 (struct ldapsam_privates
*)methods
->private_data
;
3833 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3835 LDAPMessage
*result
= NULL
;
3836 LDAPMessage
*entry
= NULL
;
3840 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3841 bool is_builtin
= false;
3842 bool sid_added
= false;
3844 *pp_alias_rids
= NULL
;
3845 *p_num_alias_rids
= 0;
3847 if (sid_check_is_builtin(domain_sid
)) {
3849 type
= SID_NAME_ALIAS
;
3852 if (sid_check_is_our_sam(domain_sid
)) {
3853 type
= SID_NAME_ALIAS
;
3856 if (type
== SID_NAME_USE_NONE
) {
3857 struct dom_sid_buf buf
;
3858 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3859 dom_sid_str_buf(domain_sid
, &buf
)));
3860 return NT_STATUS_UNSUCCESSFUL
;
3863 if (num_members
== 0) {
3864 return NT_STATUS_OK
;
3867 filter
= talloc_asprintf(mem_ctx
,
3868 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3869 LDAP_OBJ_GROUPMAP
, type
);
3871 for (i
=0; i
<num_members
; i
++) {
3872 struct dom_sid_buf buf
;
3873 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3875 dom_sid_str_buf(&members
[i
], &buf
));
3878 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3880 if (filter
== NULL
) {
3881 return NT_STATUS_NO_MEMORY
;
3885 ldap_state
->search_cache
.filter
&&
3886 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3887 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3888 result
= ldap_state
->search_cache
.result
;
3889 ldap_state
->search_cache
.result
= NULL
;
3891 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(talloc_tos()),
3892 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3893 if (rc
!= LDAP_SUCCESS
) {
3894 return NT_STATUS_UNSUCCESSFUL
;
3896 smbldap_talloc_autofree_ldapmsg(filter
, result
);
3899 ldap_struct
= smbldap_get_ldap(ldap_state
->smbldap_state
);
3901 for (entry
= ldap_first_entry(ldap_struct
, result
);
3903 entry
= ldap_next_entry(ldap_struct
, entry
))
3909 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3915 if (!string_to_sid(&sid
, sid_str
))
3918 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3923 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3924 p_num_alias_rids
)) {
3925 return NT_STATUS_NO_MEMORY
;
3929 if (!is_builtin
&& !sid_added
) {
3930 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3932 * Note: result is a talloc child of filter because of the
3933 * smbldap_talloc_autofree_ldapmsg() usage
3935 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3936 ldap_state
->search_cache
.result
= result
;
3939 return NT_STATUS_OK
;
3942 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3943 enum pdb_policy_type type
,
3946 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3948 LDAPMod
**mods
= NULL
;
3949 fstring value_string
;
3950 const char *policy_attr
= NULL
;
3952 struct ldapsam_privates
*ldap_state
=
3953 (struct ldapsam_privates
*)methods
->private_data
;
3955 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3957 if (!ldap_state
->domain_dn
) {
3958 return NT_STATUS_INVALID_PARAMETER
;
3961 policy_attr
= get_account_policy_attr(type
);
3962 if (policy_attr
== NULL
) {
3963 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3968 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3970 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3972 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3975 ldap_mods_free(mods
, True
);
3977 if (rc
!= LDAP_SUCCESS
) {
3981 if (!cache_account_policy_set(type
, value
)) {
3982 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3983 "update local tdb cache\n"));
3987 return NT_STATUS_OK
;
3990 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
3991 enum pdb_policy_type type
,
3994 return ldapsam_set_account_policy_in_ldap(methods
, type
,
3998 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
3999 enum pdb_policy_type type
,
4002 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4003 LDAPMessage
*result
= NULL
;
4004 LDAPMessage
*entry
= NULL
;
4009 const char *policy_attr
= NULL
;
4011 struct ldapsam_privates
*ldap_state
=
4012 (struct ldapsam_privates
*)methods
->private_data
;
4014 const char *attrs
[2];
4016 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4018 if (!ldap_state
->domain_dn
) {
4019 return NT_STATUS_INVALID_PARAMETER
;
4022 policy_attr
= get_account_policy_attr(type
);
4024 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4025 "policy index: %d\n", type
));
4029 attrs
[0] = policy_attr
;
4032 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
4033 if (filter
== NULL
) {
4034 return NT_STATUS_NO_MEMORY
;
4036 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
4037 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
4039 TALLOC_FREE(filter
);
4040 if (rc
!= LDAP_SUCCESS
) {
4044 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4049 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4050 if (entry
== NULL
) {
4054 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4059 *value
= (uint32_t)atol(vals
[0]);
4061 ntstatus
= NT_STATUS_OK
;
4065 ldap_value_free(vals
);
4066 ldap_msgfree(result
);
4071 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4073 - if user hasn't decided to use account policies inside LDAP just reuse the
4076 - if there is a valid cache entry, return that
4077 - if there is an LDAP entry, update cache and return
4078 - otherwise set to default, update cache and return
4082 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4083 enum pdb_policy_type type
,
4086 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4088 if (cache_account_policy_get(type
, value
)) {
4089 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4091 return NT_STATUS_OK
;
4094 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4096 if (NT_STATUS_IS_OK(ntstatus
)) {
4100 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4104 /* should we automagically migrate old tdb value here ? */
4105 if (account_policy_get(type
, value
))
4108 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4109 "default\n", type
));
4112 if (!account_policy_get_default(type
, value
)) {
4118 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4119 if (!NT_STATUS_IS_OK(ntstatus
)) {
4125 if (!cache_account_policy_set(type
, *value
)) {
4126 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4127 "tdb as a cache\n"));
4128 return NT_STATUS_UNSUCCESSFUL
;
4131 return NT_STATUS_OK
;
4134 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4135 const struct dom_sid
*domain_sid
,
4139 enum lsa_SidType
*attrs
)
4141 struct ldapsam_privates
*ldap_state
=
4142 (struct ldapsam_privates
*)methods
->private_data
;
4143 LDAPMessage
*msg
= NULL
;
4145 char *allsids
= NULL
;
4146 size_t i
, num_mapped
;
4148 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4149 TALLOC_CTX
*mem_ctx
;
4153 mem_ctx
= talloc_new(NULL
);
4154 if (mem_ctx
== NULL
) {
4155 DEBUG(0, ("talloc_new failed\n"));
4159 if (!sid_check_is_builtin(domain_sid
) &&
4160 !sid_check_is_our_sam(domain_sid
)) {
4161 result
= NT_STATUS_INVALID_PARAMETER
;
4165 if (num_rids
== 0) {
4166 result
= NT_STATUS_NONE_MAPPED
;
4170 for (i
=0; i
<num_rids
; i
++)
4171 attrs
[i
] = SID_NAME_UNKNOWN
;
4173 allsids
= talloc_strdup(mem_ctx
, "");
4174 if (allsids
== NULL
) {
4178 for (i
=0; i
<num_rids
; i
++) {
4180 struct dom_sid_buf buf
;
4181 sid_compose(&sid
, domain_sid
, rids
[i
]);
4182 allsids
= talloc_asprintf_append_buffer(
4185 dom_sid_str_buf(&sid
, &buf
));
4186 if (allsids
== NULL
) {
4191 /* First look for users */
4195 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4197 filter
= talloc_asprintf(
4198 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4199 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4201 if (filter
== NULL
) {
4205 rc
= smbldap_search(ldap_state
->smbldap_state
,
4206 lp_ldap_user_suffix(talloc_tos()),
4207 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4209 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4212 if (rc
!= LDAP_SUCCESS
)
4215 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4218 for (entry
= ldap_first_entry(ld
, msg
);
4220 entry
= ldap_next_entry(ld
, entry
)) {
4225 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4227 DEBUG(2, ("Could not find sid from ldap entry\n"));
4231 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4234 DEBUG(2, ("Could not retrieve uid attribute\n"));
4238 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4239 if (rid
== rids
[rid_index
])
4243 if (rid_index
== num_rids
) {
4244 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4248 attrs
[rid_index
] = SID_NAME_USER
;
4249 names
[rid_index
] = name
;
4253 if (num_mapped
== num_rids
) {
4254 /* No need to look for groups anymore -- we're done */
4255 result
= NT_STATUS_OK
;
4259 /* Same game for groups */
4263 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4264 "sambaGroupType", NULL
};
4266 filter
= talloc_asprintf(
4267 mem_ctx
, "(&(objectClass=%s)(|%s))",
4268 LDAP_OBJ_GROUPMAP
, allsids
);
4269 if (filter
== NULL
) {
4273 rc
= smbldap_search(ldap_state
->smbldap_state
,
4274 lp_ldap_suffix(talloc_tos()),
4275 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4277 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4280 if (rc
!= LDAP_SUCCESS
)
4283 /* ldap_struct might have changed due to a reconnect */
4285 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4287 /* For consistency checks, we already checked we're only domain or builtin */
4289 is_builtin
= sid_check_is_builtin(domain_sid
);
4291 for (entry
= ldap_first_entry(ld
, msg
);
4293 entry
= ldap_next_entry(ld
, entry
))
4298 enum lsa_SidType type
;
4299 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4301 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4304 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4309 type
= (enum lsa_SidType
)atol(attr
);
4311 /* Consistency checks */
4312 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4313 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4314 (type
!= SID_NAME_DOM_GRP
)))) {
4315 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4318 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4320 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4324 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4327 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4329 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4333 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4338 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4339 if (rid
== rids
[rid_index
])
4343 if (rid_index
== num_rids
) {
4344 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4348 attrs
[rid_index
] = type
;
4349 names
[rid_index
] = attr
;
4353 result
= NT_STATUS_NONE_MAPPED
;
4356 result
= (num_mapped
== num_rids
) ?
4357 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4359 TALLOC_FREE(mem_ctx
);
4363 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4365 char *filter
= NULL
;
4366 char *escaped
= NULL
;
4367 char *result
= NULL
;
4369 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4370 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4374 escaped
= escape_ldap_string(talloc_tos(), username
);
4375 if (escaped
== NULL
) goto done
;
4377 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4381 TALLOC_FREE(escaped
);
4386 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4390 const char **result
;
4392 va_start(ap
, mem_ctx
);
4393 while (va_arg(ap
, const char *) != NULL
)
4397 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4401 va_start(ap
, mem_ctx
);
4402 for (i
=0; i
<num
; i
++) {
4403 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4404 if (result
[i
] == NULL
) {
4405 talloc_free(result
);
4416 struct ldap_search_state
{
4417 struct smbldap_state
*connection
;
4419 uint32_t acct_flags
;
4420 uint16_t group_type
;
4427 void *pagedresults_cookie
;
4429 LDAPMessage
*entries
, *current_entry
;
4430 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4431 TALLOC_CTX
*mem_ctx
,
4432 LDAP
*ld
, LDAPMessage
*entry
,
4433 struct samr_displayentry
*result
);
4436 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4438 struct ldap_search_state
*state
=
4439 (struct ldap_search_state
*)search
->private_data
;
4441 int rc
= LDAP_OPERATIONS_ERROR
;
4443 state
->entries
= NULL
;
4445 if (smbldap_get_paged_results(state
->connection
)) {
4446 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4447 state
->scope
, state
->filter
,
4448 state
->attrs
, state
->attrsonly
,
4449 lp_ldap_page_size(), &state
->entries
,
4450 &state
->pagedresults_cookie
);
4453 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4455 if (state
->entries
!= NULL
) {
4456 /* Left over from unsuccessful paged attempt */
4457 ldap_msgfree(state
->entries
);
4458 state
->entries
= NULL
;
4461 rc
= smbldap_search(state
->connection
, state
->base
,
4462 state
->scope
, state
->filter
, state
->attrs
,
4463 state
->attrsonly
, &state
->entries
);
4465 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4468 /* Ok, the server was lying. It told us it could do paged
4469 * searches when it could not. */
4470 smbldap_set_paged_results(state
->connection
, false);
4473 ld
= smbldap_get_ldap(state
->connection
);
4475 DEBUG(5, ("Don't have an LDAP connection right after a "
4479 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4484 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4486 struct ldap_search_state
*state
=
4487 (struct ldap_search_state
*)search
->private_data
;
4490 if (!smbldap_get_paged_results(state
->connection
)) {
4491 /* There is no next page when there are no paged results */
4495 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4496 state
->scope
, state
->filter
, state
->attrs
,
4497 state
->attrsonly
, lp_ldap_page_size(),
4499 &state
->pagedresults_cookie
);
4501 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4504 state
->current_entry
= ldap_first_entry(
4505 smbldap_get_ldap(state
->connection
), state
->entries
);
4507 if (state
->current_entry
== NULL
) {
4508 ldap_msgfree(state
->entries
);
4509 state
->entries
= NULL
;
4516 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4517 struct samr_displayentry
*entry
)
4519 struct ldap_search_state
*state
=
4520 (struct ldap_search_state
*)search
->private_data
;
4524 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4527 if ((state
->entries
== NULL
) &&
4528 !ldapsam_search_nextpage(search
))
4531 if (state
->current_entry
== NULL
) {
4535 result
= state
->ldap2displayentry(state
, search
,
4536 smbldap_get_ldap(state
->connection
),
4537 state
->current_entry
, entry
);
4541 dn
= ldap_get_dn(smbldap_get_ldap(state
->connection
),
4542 state
->current_entry
);
4543 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4544 if (dn
!= NULL
) ldap_memfree(dn
);
4547 state
->current_entry
= ldap_next_entry(
4548 smbldap_get_ldap(state
->connection
), state
->current_entry
);
4550 if (state
->current_entry
== NULL
) {
4551 ldap_msgfree(state
->entries
);
4552 state
->entries
= NULL
;
4555 if (!result
) goto retry
;
4560 static void ldapsam_search_end(struct pdb_search
*search
)
4562 struct ldap_search_state
*state
=
4563 (struct ldap_search_state
*)search
->private_data
;
4566 if (state
->pagedresults_cookie
== NULL
)
4569 if (state
->entries
!= NULL
)
4570 ldap_msgfree(state
->entries
);
4572 state
->entries
= NULL
;
4573 state
->current_entry
= NULL
;
4575 if (!smbldap_get_paged_results(state
->connection
)) {
4579 /* Tell the LDAP server we're not interested in the rest anymore. */
4581 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4582 state
->filter
, state
->attrs
,
4583 state
->attrsonly
, 0, &state
->entries
,
4584 &state
->pagedresults_cookie
);
4586 if (rc
!= LDAP_SUCCESS
)
4587 DEBUG(5, ("Could not end search properly\n"));
4592 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4593 TALLOC_CTX
*mem_ctx
,
4594 LDAP
*ld
, LDAPMessage
*entry
,
4595 struct samr_displayentry
*result
)
4598 size_t converted_size
;
4600 uint32_t acct_flags
;
4602 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4603 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4604 acct_flags
= ACB_NORMAL
;
4606 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4607 ldap_value_free(vals
);
4610 if ((state
->acct_flags
!= 0) &&
4611 ((state
->acct_flags
& acct_flags
) == 0))
4614 result
->acct_flags
= acct_flags
;
4615 result
->account_name
= "";
4616 result
->fullname
= "";
4617 result
->description
= "";
4619 vals
= ldap_get_values(ld
, entry
, "uid");
4620 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4621 DEBUG(5, ("\"uid\" not found\n"));
4624 if (!pull_utf8_talloc(mem_ctx
,
4625 discard_const_p(char *, &result
->account_name
),
4626 vals
[0], &converted_size
))
4628 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4632 ldap_value_free(vals
);
4634 vals
= ldap_get_values(ld
, entry
, "displayName");
4635 if ((vals
== NULL
) || (vals
[0] == NULL
))
4636 DEBUG(8, ("\"displayName\" not found\n"));
4637 else if (!pull_utf8_talloc(mem_ctx
,
4638 discard_const_p(char *, &result
->fullname
),
4639 vals
[0], &converted_size
))
4641 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4645 ldap_value_free(vals
);
4647 vals
= ldap_get_values(ld
, entry
, "description");
4648 if ((vals
== NULL
) || (vals
[0] == NULL
))
4649 DEBUG(8, ("\"description\" not found\n"));
4650 else if (!pull_utf8_talloc(mem_ctx
,
4651 discard_const_p(char *, &result
->description
),
4652 vals
[0], &converted_size
))
4654 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4658 ldap_value_free(vals
);
4660 if ((result
->account_name
== NULL
) ||
4661 (result
->fullname
== NULL
) ||
4662 (result
->description
== NULL
)) {
4663 DEBUG(0, ("talloc failed\n"));
4667 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4668 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4669 DEBUG(0, ("\"objectSid\" not found\n"));
4673 if (!string_to_sid(&sid
, vals
[0])) {
4674 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4675 ldap_value_free(vals
);
4678 ldap_value_free(vals
);
4680 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4681 struct dom_sid_buf buf
;
4682 DEBUG(0, ("sid %s does not belong to our domain\n",
4683 dom_sid_str_buf(&sid
, &buf
)));
4691 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4692 struct pdb_search
*search
,
4693 uint32_t acct_flags
)
4695 struct ldapsam_privates
*ldap_state
=
4696 (struct ldapsam_privates
*)methods
->private_data
;
4697 struct ldap_search_state
*state
;
4699 state
= talloc(search
, struct ldap_search_state
);
4700 if (state
== NULL
) {
4701 DEBUG(0, ("talloc failed\n"));
4705 state
->connection
= ldap_state
->smbldap_state
;
4707 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4708 state
->base
= lp_ldap_user_suffix(talloc_tos());
4709 else if ((acct_flags
!= 0) &&
4710 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4711 state
->base
= lp_ldap_machine_suffix(talloc_tos());
4713 state
->base
= lp_ldap_suffix(talloc_tos());
4715 state
->acct_flags
= acct_flags
;
4716 state
->base
= talloc_strdup(search
, state
->base
);
4717 state
->scope
= LDAP_SCOPE_SUBTREE
;
4718 state
->filter
= get_ldap_filter(search
, "*");
4719 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4720 "displayName", "description",
4721 "sambaAcctFlags", NULL
);
4722 state
->attrsonly
= 0;
4723 state
->pagedresults_cookie
= NULL
;
4724 state
->entries
= NULL
;
4725 state
->ldap2displayentry
= ldapuser2displayentry
;
4727 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4728 DEBUG(0, ("talloc failed\n"));
4732 search
->private_data
= state
;
4733 search
->next_entry
= ldapsam_search_next_entry
;
4734 search
->search_end
= ldapsam_search_end
;
4736 return ldapsam_search_firstpage(search
);
4739 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4740 TALLOC_CTX
*mem_ctx
,
4741 LDAP
*ld
, LDAPMessage
*entry
,
4742 struct samr_displayentry
*result
)
4745 size_t converted_size
;
4747 uint16_t group_type
;
4749 result
->account_name
= "";
4750 result
->fullname
= "";
4751 result
->description
= "";
4754 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4755 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4756 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4758 ldap_value_free(vals
);
4763 group_type
= atoi(vals
[0]);
4765 if ((state
->group_type
!= 0) &&
4766 ((state
->group_type
!= group_type
))) {
4767 ldap_value_free(vals
);
4771 ldap_value_free(vals
);
4773 /* display name is the NT group name */
4775 vals
= ldap_get_values(ld
, entry
, "displayName");
4776 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4777 DEBUG(8, ("\"displayName\" not found\n"));
4779 /* fallback to the 'cn' attribute */
4780 vals
= ldap_get_values(ld
, entry
, "cn");
4781 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4782 DEBUG(5, ("\"cn\" not found\n"));
4785 if (!pull_utf8_talloc(mem_ctx
,
4786 discard_const_p(char *,
4787 &result
->account_name
),
4788 vals
[0], &converted_size
))
4790 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4791 "failed: %s", strerror(errno
)));
4794 else if (!pull_utf8_talloc(mem_ctx
,
4795 discard_const_p(char *,
4796 &result
->account_name
),
4797 vals
[0], &converted_size
))
4799 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4803 ldap_value_free(vals
);
4805 vals
= ldap_get_values(ld
, entry
, "description");
4806 if ((vals
== NULL
) || (vals
[0] == NULL
))
4807 DEBUG(8, ("\"description\" not found\n"));
4808 else if (!pull_utf8_talloc(mem_ctx
,
4809 discard_const_p(char *, &result
->description
),
4810 vals
[0], &converted_size
))
4812 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4815 ldap_value_free(vals
);
4817 if ((result
->account_name
== NULL
) ||
4818 (result
->fullname
== NULL
) ||
4819 (result
->description
== NULL
)) {
4820 DEBUG(0, ("talloc failed\n"));
4824 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4825 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4826 DEBUG(0, ("\"objectSid\" not found\n"));
4828 ldap_value_free(vals
);
4833 if (!string_to_sid(&sid
, vals
[0])) {
4834 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4838 ldap_value_free(vals
);
4840 switch (group_type
) {
4841 case SID_NAME_DOM_GRP
:
4842 case SID_NAME_ALIAS
:
4844 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4845 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4847 struct dom_sid_buf buf
;
4848 DEBUG(0, ("%s is not in our domain\n",
4849 dom_sid_str_buf(&sid
, &buf
)));
4855 DEBUG(0,("unknown group type: %d\n", group_type
));
4859 result
->acct_flags
= 0;
4864 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4865 struct pdb_search
*search
,
4866 const struct dom_sid
*sid
,
4867 enum lsa_SidType type
)
4869 struct ldapsam_privates
*ldap_state
=
4870 (struct ldapsam_privates
*)methods
->private_data
;
4871 struct ldap_search_state
*state
;
4872 struct dom_sid_buf tmp
;
4874 state
= talloc(search
, struct ldap_search_state
);
4875 if (state
== NULL
) {
4876 DEBUG(0, ("talloc failed\n"));
4880 state
->connection
= ldap_state
->smbldap_state
;
4882 state
->base
= lp_ldap_suffix(search
);
4883 state
->connection
= ldap_state
->smbldap_state
;
4884 state
->scope
= LDAP_SCOPE_SUBTREE
;
4885 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4886 "(sambaGroupType=%d)(sambaSID=%s*))",
4889 dom_sid_str_buf(sid
, &tmp
));
4890 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4891 "displayName", "description",
4892 "sambaGroupType", NULL
);
4893 state
->attrsonly
= 0;
4894 state
->pagedresults_cookie
= NULL
;
4895 state
->entries
= NULL
;
4896 state
->group_type
= type
;
4897 state
->ldap2displayentry
= ldapgroup2displayentry
;
4899 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4900 DEBUG(0, ("talloc failed\n"));
4904 search
->private_data
= state
;
4905 search
->next_entry
= ldapsam_search_next_entry
;
4906 search
->search_end
= ldapsam_search_end
;
4908 return ldapsam_search_firstpage(search
);
4911 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4912 struct pdb_search
*search
)
4914 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4917 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4918 struct pdb_search
*search
,
4919 const struct dom_sid
*sid
)
4921 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4924 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4926 return PDB_CAP_STORE_RIDS
;
4929 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4932 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4934 LDAPMessage
*result
= NULL
;
4935 LDAPMessage
*entry
= NULL
;
4936 LDAPMod
**mods
= NULL
;
4940 uint32_t nextRid
= 0;
4945 TALLOC_CTX
*mem_ctx
;
4947 mem_ctx
= talloc_new(NULL
);
4948 if (mem_ctx
== NULL
) {
4949 DEBUG(0, ("talloc_new failed\n"));
4950 return NT_STATUS_NO_MEMORY
;
4953 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4954 get_global_sam_name(), False
);
4955 if (!NT_STATUS_IS_OK(status
)) {
4956 DEBUG(3, ("Could not get domain info: %s\n",
4957 nt_errstr(status
)));
4961 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4963 entry
= ldap_first_entry(priv2ld(priv
), result
);
4964 if (entry
== NULL
) {
4965 DEBUG(0, ("Could not get domain info entry\n"));
4966 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4970 /* Find the largest of the three attributes "sambaNextRid",
4971 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4972 concept of differentiating between user and group rids, and will
4973 use only "sambaNextRid" in the future. But for compatibility
4974 reasons I look if others have chosen different strategies -- VL */
4976 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4977 "sambaNextRid", mem_ctx
);
4978 if (value
!= NULL
) {
4979 tmp
= (uint32_t)strtoul_err(value
, NULL
, 10, &error
);
4984 nextRid
= MAX(nextRid
, tmp
);
4987 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4988 "sambaNextUserRid", mem_ctx
);
4989 if (value
!= NULL
) {
4990 tmp
= (uint32_t)strtoul_err(value
, NULL
, 10, &error
);
4995 nextRid
= MAX(nextRid
, tmp
);
4998 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4999 "sambaNextGroupRid", mem_ctx
);
5000 if (value
!= NULL
) {
5001 tmp
= (uint32_t)strtoul_err(value
, NULL
, 10, &error
);
5006 nextRid
= MAX(nextRid
, tmp
);
5010 nextRid
= BASE_RID
-1;
5015 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
5016 talloc_asprintf(mem_ctx
, "%d", nextRid
));
5017 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
5019 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
5020 status
= NT_STATUS_NO_MEMORY
;
5024 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
5026 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5029 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
5032 if (NT_STATUS_IS_OK(status
)) {
5036 TALLOC_FREE(mem_ctx
);
5040 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
5044 for (i
=0; i
<10; i
++) {
5045 NTSTATUS result
= ldapsam_get_new_rid(
5046 (struct ldapsam_privates
*)methods
->private_data
, rid
);
5047 if (NT_STATUS_IS_OK(result
)) {
5051 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
5055 /* The ldap update failed (maybe a race condition), retry */
5058 /* Tried 10 times, fail. */
5059 return NT_STATUS_ACCESS_DENIED
;
5062 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
5064 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
5065 return NT_STATUS_IS_OK(result
) ? True
: False
;
5068 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
5069 const struct dom_sid
*sid
,
5072 struct ldapsam_privates
*priv
=
5073 (struct ldapsam_privates
*)methods
->private_data
;
5076 struct dom_sid_buf buf
;
5077 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5079 LDAPMessage
*result
= NULL
;
5080 LDAPMessage
*entry
= NULL
;
5085 TALLOC_CTX
*mem_ctx
;
5087 ret
= pdb_sid_to_id_unix_users_and_groups(sid
, id
);
5092 mem_ctx
= talloc_new(NULL
);
5093 if (mem_ctx
== NULL
) {
5094 DEBUG(0, ("talloc_new failed\n"));
5098 filter
= talloc_asprintf(mem_ctx
,
5100 "(|(objectClass=%s)(objectClass=%s)))",
5101 dom_sid_str_buf(sid
, &buf
),
5102 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5103 if (filter
== NULL
) {
5104 DEBUG(5, ("talloc_asprintf failed\n"));
5108 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5110 if (rc
!= LDAP_SUCCESS
) {
5113 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
5115 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5116 DEBUG(10, ("Got %d entries, expected one\n",
5117 ldap_count_entries(priv2ld(priv
), result
)));
5121 entry
= ldap_first_entry(priv2ld(priv
), result
);
5123 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5124 "sambaGroupType", mem_ctx
);
5126 if (value
!= NULL
) {
5127 const char *gid_str
;
5130 gid_str
= smbldap_talloc_single_attribute(
5131 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5132 if (gid_str
== NULL
) {
5133 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5134 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5139 id
->id
= strtoul_err(gid_str
, NULL
, 10, &error
);
5144 id
->type
= ID_TYPE_GID
;
5149 /* It must be a user */
5151 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5152 "uidNumber", mem_ctx
);
5153 if (value
== NULL
) {
5154 DEBUG(1, ("Could not find uidNumber in %s\n",
5155 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5159 id
->id
= strtoul_err(value
, NULL
, 10, &error
);
5164 id
->type
= ID_TYPE_UID
;
5167 TALLOC_FREE(mem_ctx
);
5172 * Find the SID for a uid.
5173 * This is shortcut is only used if ldapsam:trusted is set to true.
5175 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5176 struct dom_sid
*sid
)
5178 struct ldapsam_privates
*priv
=
5179 (struct ldapsam_privates
*)methods
->private_data
;
5181 const char *attrs
[] = { "sambaSID", NULL
};
5182 LDAPMessage
*result
= NULL
;
5183 LDAPMessage
*entry
= NULL
;
5185 char *user_sid_string
;
5186 struct dom_sid user_sid
;
5188 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5190 filter
= talloc_asprintf(tmp_ctx
,
5193 "(objectClass=%s))",
5195 LDAP_OBJ_POSIXACCOUNT
,
5196 LDAP_OBJ_SAMBASAMACCOUNT
);
5197 if (filter
== NULL
) {
5198 DEBUG(3, ("talloc_asprintf failed\n"));
5202 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5203 if (rc
!= LDAP_SUCCESS
) {
5206 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5208 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5209 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5210 ldap_count_entries(priv2ld(priv
), result
),
5211 (unsigned int)uid
));
5215 entry
= ldap_first_entry(priv2ld(priv
), result
);
5217 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5218 "sambaSID", tmp_ctx
);
5219 if (user_sid_string
== NULL
) {
5220 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5221 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5225 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5226 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5231 sid_copy(sid
, &user_sid
);
5236 TALLOC_FREE(tmp_ctx
);
5241 * Find the SID for a gid.
5242 * This is shortcut is only used if ldapsam:trusted is set to true.
5244 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5245 struct dom_sid
*sid
)
5247 struct ldapsam_privates
*priv
=
5248 (struct ldapsam_privates
*)methods
->private_data
;
5250 const char *attrs
[] = { "sambaSID", NULL
};
5251 LDAPMessage
*result
= NULL
;
5252 LDAPMessage
*entry
= NULL
;
5254 char *group_sid_string
;
5255 struct dom_sid group_sid
;
5257 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5259 filter
= talloc_asprintf(tmp_ctx
,
5261 "(objectClass=%s))",
5264 if (filter
== NULL
) {
5265 DEBUG(3, ("talloc_asprintf failed\n"));
5269 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5270 if (rc
!= LDAP_SUCCESS
) {
5273 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5275 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5276 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5277 ldap_count_entries(priv2ld(priv
), result
),
5278 (unsigned int)gid
));
5282 entry
= ldap_first_entry(priv2ld(priv
), result
);
5284 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5285 "sambaSID", tmp_ctx
);
5286 if (group_sid_string
== NULL
) {
5287 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5288 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5292 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5293 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5298 sid_copy(sid
, &group_sid
);
5303 TALLOC_FREE(tmp_ctx
);
5307 static bool ldapsam_id_to_sid(struct pdb_methods
*methods
, struct unixid
*id
,
5308 struct dom_sid
*sid
)
5312 return ldapsam_uid_to_sid(methods
, id
->id
, sid
);
5315 return ldapsam_gid_to_sid(methods
, id
->id
, sid
);
5324 * The following functions are called only if
5325 * ldapsam:trusted and ldapsam:editposix are
5330 * ldapsam_create_user creates a new
5331 * posixAccount and sambaSamAccount object
5332 * in the ldap users subtree
5334 * The uid is allocated by winbindd.
5337 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5338 TALLOC_CTX
*tmp_ctx
, const char *name
,
5339 uint32_t acb_info
, uint32_t *rid
)
5341 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5342 LDAPMessage
*entry
= NULL
;
5343 LDAPMessage
*result
= NULL
;
5344 uint32_t num_result
;
5345 bool is_machine
= False
;
5346 bool add_posix
= False
;
5347 bool init_okay
= False
;
5348 LDAPMod
**mods
= NULL
;
5356 const char *dn
= NULL
;
5357 struct dom_sid group_sid
;
5358 struct dom_sid user_sid
;
5364 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5365 acb_info
& ACB_WSTRUST
||
5366 acb_info
& ACB_SVRTRUST
||
5367 acb_info
& ACB_DOMTRUST
) {
5371 username
= escape_ldap_string(talloc_tos(), name
);
5372 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5373 username
, LDAP_OBJ_POSIXACCOUNT
);
5374 TALLOC_FREE(username
);
5376 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5377 if (rc
!= LDAP_SUCCESS
) {
5378 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5379 return NT_STATUS_ACCESS_DENIED
;
5381 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5383 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5385 if (num_result
> 1) {
5386 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5387 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5390 if (num_result
== 1) {
5392 /* check if it is just a posix account.
5393 * or if there is a sid attached to this entry
5396 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5398 return NT_STATUS_UNSUCCESSFUL
;
5401 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5403 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5404 return NT_STATUS_USER_EXISTS
;
5407 /* it is just a posix account, retrieve the dn for later use */
5408 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5410 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5411 return NT_STATUS_NO_MEMORY
;
5415 if (num_result
== 0) {
5419 /* Create the basic samu structure and generate the mods for the ldap commit */
5420 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5421 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5425 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5427 user
= samu_new(tmp_ctx
);
5429 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5430 return NT_STATUS_NO_MEMORY
;
5433 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5434 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5435 return NT_STATUS_UNSUCCESSFUL
;
5437 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5438 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5439 return NT_STATUS_UNSUCCESSFUL
;
5442 if (acb_info
& ACB_NORMAL
) {
5443 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5444 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5445 return NT_STATUS_UNSUCCESSFUL
;
5448 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5449 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5450 return NT_STATUS_UNSUCCESSFUL
;
5454 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5455 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5456 return NT_STATUS_UNSUCCESSFUL
;
5460 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5461 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5462 return NT_STATUS_UNSUCCESSFUL
;
5465 init_okay
= init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
);
5468 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5469 ldap_mods_free(mods
, true);
5470 return NT_STATUS_UNSUCCESSFUL
;
5473 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5474 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5476 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5481 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5483 /* retrieve the Domain Users group gid */
5484 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5485 !sid_to_gid(&group_sid
, &gid
)) {
5486 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5487 ldap_mods_free(mods
, true);
5488 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5491 /* lets allocate a new userid for this user */
5492 if (!winbind_allocate_uid(&uid
)) {
5493 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5494 ldap_mods_free(mods
, true);
5495 return NT_STATUS_UNSUCCESSFUL
;
5500 /* TODO: choose a more appropriate default for machines */
5501 homedir
= talloc_sub_specified(tmp_ctx
,
5502 lp_template_homedir(),
5503 "SMB_workstations_home",
5505 ldap_state
->domain_name
,
5508 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5510 homedir
= talloc_sub_specified(tmp_ctx
,
5511 lp_template_homedir(),
5514 ldap_state
->domain_name
,
5517 shell
= talloc_sub_specified(tmp_ctx
,
5518 lp_template_shell(),
5521 ldap_state
->domain_name
,
5525 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5526 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5528 escape_name
= escape_rdn_val_string_alloc(name
);
5530 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5531 ldap_mods_free(mods
, true);
5532 return NT_STATUS_NO_MEMORY
;
5536 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix (talloc_tos()));
5538 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix (talloc_tos()));
5541 SAFE_FREE(escape_name
);
5543 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5544 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5545 ldap_mods_free(mods
, true);
5546 return NT_STATUS_NO_MEMORY
;
5549 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5550 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5551 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5552 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5553 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5554 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5555 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5559 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5561 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5564 ldap_mods_free(mods
, true);
5566 if (rc
!= LDAP_SUCCESS
) {
5567 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5568 return NT_STATUS_UNSUCCESSFUL
;
5571 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5573 flush_pwnam_cache();
5575 return NT_STATUS_OK
;
5578 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5580 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5581 LDAPMessage
*result
= NULL
;
5582 LDAPMessage
*entry
= NULL
;
5588 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5590 filter
= talloc_asprintf(tmp_ctx
,
5593 "(objectClass=%s))",
5594 pdb_get_username(sam_acct
),
5595 LDAP_OBJ_POSIXACCOUNT
,
5596 LDAP_OBJ_SAMBASAMACCOUNT
);
5597 if (filter
== NULL
) {
5598 return NT_STATUS_NO_MEMORY
;
5601 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5602 if (rc
!= LDAP_SUCCESS
) {
5603 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5604 return NT_STATUS_UNSUCCESSFUL
;
5606 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5608 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5610 if (num_result
== 0) {
5611 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5612 return NT_STATUS_NO_SUCH_USER
;
5615 if (num_result
> 1) {
5616 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5617 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5620 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5622 return NT_STATUS_UNSUCCESSFUL
;
5625 /* it is just a posix account, retrieve the dn for later use */
5626 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5628 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5629 return NT_STATUS_NO_MEMORY
;
5632 /* try to remove memberships first */
5635 struct dom_sid
*sids
= NULL
;
5637 uint32_t num_groups
= 0;
5639 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5641 status
= ldapsam_enum_group_memberships(my_methods
,
5647 if (!NT_STATUS_IS_OK(status
)) {
5651 for (i
=0; i
< num_groups
; i
++) {
5655 sid_peek_rid(&sids
[i
], &group_rid
);
5657 ldapsam_del_groupmem(my_methods
,
5666 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5667 if (rc
!= LDAP_SUCCESS
) {
5668 return NT_STATUS_UNSUCCESSFUL
;
5671 flush_pwnam_cache();
5673 return NT_STATUS_OK
;
5677 * ldapsam_create_group creates a new
5678 * posixGroup and sambaGroupMapping object
5679 * in the ldap groups subtree
5681 * The gid is allocated by winbindd.
5684 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5685 TALLOC_CTX
*tmp_ctx
,
5689 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5691 LDAPMessage
*entry
= NULL
;
5692 LDAPMessage
*result
= NULL
;
5693 uint32_t num_result
;
5694 bool is_new_entry
= False
;
5695 LDAPMod
**mods
= NULL
;
5700 const char *dn
= NULL
;
5701 struct dom_sid group_sid
;
5702 struct dom_sid_buf buf
;
5707 groupname
= escape_ldap_string(talloc_tos(), name
);
5708 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5709 groupname
, LDAP_OBJ_POSIXGROUP
);
5710 TALLOC_FREE(groupname
);
5712 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5713 if (rc
!= LDAP_SUCCESS
) {
5714 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5715 return NT_STATUS_UNSUCCESSFUL
;
5717 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5719 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5721 if (num_result
> 1) {
5722 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5723 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5726 if (num_result
== 1) {
5728 /* check if it is just a posix group.
5729 * or if there is a sid attached to this entry
5732 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5734 return NT_STATUS_UNSUCCESSFUL
;
5737 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5739 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5740 return NT_STATUS_GROUP_EXISTS
;
5743 /* it is just a posix group, retrieve the gid and the dn for later use */
5744 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5746 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5747 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5750 gid
= strtoul_err(tmp
, NULL
, 10, &error
);
5752 DBG_ERR("Failed to convert gidNumber\n");
5753 return NT_STATUS_UNSUCCESSFUL
;
5756 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5758 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5759 return NT_STATUS_NO_MEMORY
;
5763 if (num_result
== 0) {
5764 is_new_entry
= true;
5767 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5768 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5772 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5774 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5777 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5778 return NT_STATUS_NO_MEMORY
;
5781 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5782 smbldap_set_mod(&mods
,
5785 dom_sid_str_buf(&group_sid
, &buf
));
5786 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5787 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5792 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5794 /* lets allocate a new groupid for this group */
5795 if (!winbind_allocate_gid(&gid
)) {
5796 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5797 return NT_STATUS_UNSUCCESSFUL
;
5800 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5802 escape_name
= escape_rdn_val_string_alloc(name
);
5804 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5805 return NT_STATUS_NO_MEMORY
;
5808 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix(talloc_tos()));
5810 SAFE_FREE(escape_name
);
5812 if (!gidstr
|| !dn
) {
5813 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5814 return NT_STATUS_NO_MEMORY
;
5817 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5818 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5819 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5822 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5825 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5827 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5828 /* This call may fail with rfc2307bis schema */
5829 /* Retry adding a structural class */
5830 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5831 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5835 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5838 if (rc
!= LDAP_SUCCESS
) {
5839 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5840 return NT_STATUS_UNSUCCESSFUL
;
5843 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5845 return NT_STATUS_OK
;
5848 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5850 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5851 LDAPMessage
*result
= NULL
;
5852 LDAPMessage
*entry
= NULL
;
5857 struct dom_sid group_sid
;
5858 struct dom_sid_buf buf
;
5861 /* get the group sid */
5862 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5864 filter
= talloc_asprintf(tmp_ctx
,
5867 "(objectClass=%s))",
5868 dom_sid_str_buf(&group_sid
, &buf
),
5869 LDAP_OBJ_POSIXGROUP
,
5871 if (filter
== NULL
) {
5872 return NT_STATUS_NO_MEMORY
;
5875 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5876 if (rc
!= LDAP_SUCCESS
) {
5877 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5878 return NT_STATUS_UNSUCCESSFUL
;
5880 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5882 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5884 if (num_result
== 0) {
5885 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5886 return NT_STATUS_NO_SUCH_GROUP
;
5889 if (num_result
> 1) {
5890 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5891 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5894 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5896 return NT_STATUS_UNSUCCESSFUL
;
5899 /* here it is, retrieve the dn for later use */
5900 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5902 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5903 return NT_STATUS_NO_MEMORY
;
5906 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5908 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5909 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5912 /* check no user have this group marked as primary group */
5913 filter
= talloc_asprintf(tmp_ctx
,
5916 "(objectClass=%s))",
5918 LDAP_OBJ_POSIXACCOUNT
,
5919 LDAP_OBJ_SAMBASAMACCOUNT
);
5921 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5922 if (rc
!= LDAP_SUCCESS
) {
5923 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5924 return NT_STATUS_UNSUCCESSFUL
;
5926 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5928 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5930 if (num_result
!= 0) {
5931 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5932 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5935 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5936 if (rc
!= LDAP_SUCCESS
) {
5937 return NT_STATUS_UNSUCCESSFUL
;
5940 return NT_STATUS_OK
;
5943 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5944 TALLOC_CTX
*tmp_ctx
,
5946 uint32_t member_rid
,
5949 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5950 LDAPMessage
*entry
= NULL
;
5951 LDAPMessage
*result
= NULL
;
5952 uint32_t num_result
;
5953 LDAPMod
**mods
= NULL
;
5956 const char *dn
= NULL
;
5957 struct dom_sid group_sid
;
5958 struct dom_sid member_sid
;
5959 struct dom_sid_buf buf
;
5965 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5967 case LDAP_MOD_DELETE
:
5968 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
5971 return NT_STATUS_UNSUCCESSFUL
;
5974 /* get member sid */
5975 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
5977 /* get the group sid */
5978 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
5980 filter
= talloc_asprintf(tmp_ctx
,
5983 "(objectClass=%s))",
5984 dom_sid_str_buf(&member_sid
, &buf
),
5985 LDAP_OBJ_POSIXACCOUNT
,
5986 LDAP_OBJ_SAMBASAMACCOUNT
);
5987 if (filter
== NULL
) {
5988 return NT_STATUS_NO_MEMORY
;
5991 /* get the member uid */
5992 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5993 if (rc
!= LDAP_SUCCESS
) {
5994 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5995 return NT_STATUS_UNSUCCESSFUL
;
5997 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5999 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6001 if (num_result
== 0) {
6002 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6003 return NT_STATUS_NO_SUCH_MEMBER
;
6006 if (num_result
> 1) {
6007 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6008 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6011 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6013 return NT_STATUS_UNSUCCESSFUL
;
6016 if (modop
== LDAP_MOD_DELETE
) {
6017 /* check if we are trying to remove the member from his primary group */
6019 gid_t user_gid
, group_gid
;
6021 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
6023 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6024 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6027 user_gid
= strtoul_err(gidstr
, NULL
, 10, &error
);
6029 DBG_ERR("Failed to convert user gid\n");
6030 return NT_STATUS_UNSUCCESSFUL
;
6033 if (!sid_to_gid(&group_sid
, &group_gid
)) {
6034 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6035 return NT_STATUS_UNSUCCESSFUL
;
6038 if (user_gid
== group_gid
) {
6039 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6040 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
6044 /* here it is, retrieve the uid for later use */
6045 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
6047 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6048 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6051 filter
= talloc_asprintf(tmp_ctx
,
6054 "(objectClass=%s))",
6055 dom_sid_str_buf(&group_sid
, &buf
),
6056 LDAP_OBJ_POSIXGROUP
,
6060 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6061 if (rc
!= LDAP_SUCCESS
) {
6062 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6063 return NT_STATUS_UNSUCCESSFUL
;
6065 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
6067 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6069 if (num_result
== 0) {
6070 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6071 return NT_STATUS_NO_SUCH_GROUP
;
6074 if (num_result
> 1) {
6075 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6076 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6079 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6081 return NT_STATUS_UNSUCCESSFUL
;
6084 /* here it is, retrieve the dn for later use */
6085 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
6087 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6088 return NT_STATUS_NO_MEMORY
;
6091 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
6093 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
6095 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6096 if (rc
!= LDAP_SUCCESS
) {
6097 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
6098 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6099 return NT_STATUS_MEMBER_IN_GROUP
;
6101 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
6102 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6103 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
6105 return NT_STATUS_UNSUCCESSFUL
;
6108 return NT_STATUS_OK
;
6111 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
6112 TALLOC_CTX
*tmp_ctx
,
6114 uint32_t member_rid
)
6116 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
6118 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
6119 TALLOC_CTX
*tmp_ctx
,
6121 uint32_t member_rid
)
6123 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
6126 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
6127 TALLOC_CTX
*mem_ctx
,
6128 struct samu
*sampass
)
6130 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6131 LDAPMessage
*entry
= NULL
;
6132 LDAPMessage
*result
= NULL
;
6133 uint32_t num_result
;
6134 LDAPMod
**mods
= NULL
;
6136 char *escape_username
;
6142 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6144 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6145 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6146 return NT_STATUS_UNSUCCESSFUL
;
6148 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6150 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6151 return NT_STATUS_NO_MEMORY
;
6154 escape_username
= escape_ldap_string(talloc_tos(),
6155 pdb_get_username(sampass
));
6156 if (escape_username
== NULL
) {
6157 return NT_STATUS_NO_MEMORY
;
6160 filter
= talloc_asprintf(mem_ctx
,
6163 "(objectClass=%s))",
6165 LDAP_OBJ_POSIXACCOUNT
,
6166 LDAP_OBJ_SAMBASAMACCOUNT
);
6168 TALLOC_FREE(escape_username
);
6170 if (filter
== NULL
) {
6171 return NT_STATUS_NO_MEMORY
;
6174 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6175 if (rc
!= LDAP_SUCCESS
) {
6176 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6177 return NT_STATUS_UNSUCCESSFUL
;
6179 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6181 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6183 if (num_result
== 0) {
6184 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6185 return NT_STATUS_NO_SUCH_USER
;
6188 if (num_result
> 1) {
6189 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6190 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6193 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6195 return NT_STATUS_UNSUCCESSFUL
;
6198 /* retrieve the dn for later use */
6199 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6201 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6202 return NT_STATUS_NO_MEMORY
;
6205 /* remove the old one, and add the new one, this way we do not risk races */
6206 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6210 return NT_STATUS_OK
;
6213 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6215 if (rc
!= LDAP_SUCCESS
) {
6216 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6217 pdb_get_username(sampass
), gidstr
));
6218 return NT_STATUS_UNSUCCESSFUL
;
6221 flush_pwnam_cache();
6223 return NT_STATUS_OK
;
6227 /**********************************************************************
6228 trusted domains functions
6229 *********************************************************************/
6231 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6234 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6235 ldap_state
->domain_dn
);
6238 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6239 TALLOC_CTX
*mem_ctx
,
6240 const char *domain
, LDAPMessage
**entry
)
6244 int scope
= LDAP_SCOPE_SUBTREE
;
6245 const char **attrs
= NULL
; /* NULL: get all attrs */
6246 int attrsonly
= 0; /* 0: return values too */
6247 LDAPMessage
*result
= NULL
;
6249 uint32_t num_result
;
6251 filter
= talloc_asprintf(talloc_tos(),
6252 "(&(objectClass=%s)(sambaDomainName=%s))",
6253 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6255 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6256 if (trusted_dn
== NULL
) {
6259 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6260 filter
, attrs
, attrsonly
, &result
);
6262 if (result
!= NULL
) {
6263 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6266 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6271 if (rc
!= LDAP_SUCCESS
) {
6275 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6277 if (num_result
> 1) {
6278 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6279 "%s object for domain '%s'?!\n",
6280 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6284 if (num_result
== 0) {
6285 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6286 "%s object for domain %s.\n",
6287 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6290 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6296 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6299 struct dom_sid
*sid
,
6300 time_t *pass_last_set_time
)
6302 struct ldapsam_privates
*ldap_state
=
6303 (struct ldapsam_privates
*)methods
->private_data
;
6304 LDAPMessage
*entry
= NULL
;
6306 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6308 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6317 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6318 entry
, "sambaClearTextPassword", talloc_tos());
6319 if (pwd_str
== NULL
) {
6322 /* trusteddom_pw routines do not use talloc yet... */
6323 *pwd
= SMB_STRDUP(pwd_str
);
6329 /* last change time */
6330 if (pass_last_set_time
!= NULL
) {
6332 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6333 entry
, "sambaPwdLastSet", talloc_tos());
6334 if (time_str
== NULL
) {
6337 *pass_last_set_time
= (time_t)atol(time_str
);
6343 struct dom_sid dom_sid
;
6344 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6347 if (sid_str
== NULL
) {
6350 if (!string_to_sid(&dom_sid
, sid_str
)) {
6353 sid_copy(sid
, &dom_sid
);
6359 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6362 const struct dom_sid
*sid
)
6364 struct ldapsam_privates
*ldap_state
=
6365 (struct ldapsam_privates
*)methods
->private_data
;
6366 LDAPMessage
*entry
= NULL
;
6367 LDAPMod
**mods
= NULL
;
6368 char *prev_pwd
= NULL
;
6369 char *trusted_dn
= NULL
;
6370 struct dom_sid_buf buf
;
6373 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6376 * get the current entry (if there is one) in order to put the
6377 * current password into the previous password attribute
6379 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6384 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6385 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6386 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6388 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6389 dom_sid_str_buf(sid
, &buf
));
6390 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6391 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6392 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6393 "sambaClearTextPassword", pwd
);
6395 if (entry
!= NULL
) {
6396 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6397 entry
, "sambaClearTextPassword", talloc_tos());
6398 if (prev_pwd
!= NULL
) {
6399 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6400 "sambaPreviousClearTextPassword",
6405 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods
);
6407 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6408 if (trusted_dn
== NULL
) {
6411 if (entry
== NULL
) {
6412 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6414 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6417 if (rc
!= LDAP_SUCCESS
) {
6418 DEBUG(1, ("error writing trusted domain password!\n"));
6425 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6429 struct ldapsam_privates
*ldap_state
=
6430 (struct ldapsam_privates
*)methods
->private_data
;
6431 LDAPMessage
*entry
= NULL
;
6432 const char *trusted_dn
;
6434 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6438 if (entry
== NULL
) {
6439 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6444 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6446 if (trusted_dn
== NULL
) {
6447 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6451 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6452 if (rc
!= LDAP_SUCCESS
) {
6459 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6460 TALLOC_CTX
*mem_ctx
,
6461 uint32_t *num_domains
,
6462 struct trustdom_info
***domains
)
6465 struct ldapsam_privates
*ldap_state
=
6466 (struct ldapsam_privates
*)methods
->private_data
;
6468 int scope
= LDAP_SCOPE_SUBTREE
;
6469 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6470 int attrsonly
= 0; /* 0: return values too */
6471 LDAPMessage
*result
= NULL
;
6472 LDAPMessage
*entry
= NULL
;
6474 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6475 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6477 rc
= smbldap_search(ldap_state
->smbldap_state
,
6478 ldap_state
->domain_dn
,
6485 if (result
!= NULL
) {
6486 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6489 if (rc
!= LDAP_SUCCESS
) {
6490 return NT_STATUS_UNSUCCESSFUL
;
6494 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6495 DEBUG(1, ("talloc failed\n"));
6496 return NT_STATUS_NO_MEMORY
;
6499 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6501 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6503 char *dom_name
, *dom_sid_str
;
6504 struct trustdom_info
*dom_info
;
6506 dom_info
= talloc(*domains
, struct trustdom_info
);
6507 if (dom_info
== NULL
) {
6508 DEBUG(1, ("talloc failed\n"));
6509 return NT_STATUS_NO_MEMORY
;
6512 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6516 if (dom_name
== NULL
) {
6517 DEBUG(1, ("talloc failed\n"));
6518 return NT_STATUS_NO_MEMORY
;
6520 dom_info
->name
= dom_name
;
6522 dom_sid_str
= smbldap_talloc_single_attribute(
6523 priv2ld(ldap_state
), entry
, "sambaSID",
6525 if (dom_sid_str
== NULL
) {
6526 DEBUG(1, ("talloc failed\n"));
6527 return NT_STATUS_NO_MEMORY
;
6529 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6530 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6532 return NT_STATUS_UNSUCCESSFUL
;
6535 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6536 domains
, num_domains
);
6538 if (*domains
== NULL
) {
6539 DEBUG(1, ("talloc failed\n"));
6540 return NT_STATUS_NO_MEMORY
;
6544 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6545 return NT_STATUS_OK
;
6549 /**********************************************************************
6551 *********************************************************************/
6553 static void free_private_data(void **vp
)
6555 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6557 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6559 if ((*ldap_state
)->result
!= NULL
) {
6560 ldap_msgfree((*ldap_state
)->result
);
6561 (*ldap_state
)->result
= NULL
;
6563 if ((*ldap_state
)->domain_dn
!= NULL
) {
6564 SAFE_FREE((*ldap_state
)->domain_dn
);
6569 /* No need to free any further, as it is talloc()ed */
6572 /*********************************************************************
6573 Intitalise the parts of the pdb_methods structure that are common to
6575 *********************************************************************/
6577 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6580 struct ldapsam_privates
*ldap_state
;
6581 char *bind_dn
= NULL
;
6582 char *bind_secret
= NULL
;
6584 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6588 (*pdb_method
)->name
= "ldapsam";
6590 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6591 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6592 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6593 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6594 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6595 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6597 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6598 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6599 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6600 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6601 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6602 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6603 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6605 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6606 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6608 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6610 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6611 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6613 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6614 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6615 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6616 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6618 /* TODO: Setup private data and free */
6620 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6621 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6622 return NT_STATUS_NO_MEMORY
;
6625 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6626 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6627 return NT_STATUS_NO_MEMORY
;
6630 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6631 location
, false, bind_dn
, bind_secret
,
6632 &ldap_state
->smbldap_state
);
6633 memset(bind_secret
, '\0', strlen(bind_secret
));
6634 SAFE_FREE(bind_secret
);
6636 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6640 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6641 return NT_STATUS_NO_MEMORY
;
6644 (*pdb_method
)->private_data
= ldap_state
;
6646 (*pdb_method
)->free_private_data
= free_private_data
;
6648 return NT_STATUS_OK
;
6651 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods
*m
)
6656 /**********************************************************************
6657 Initialise the normal mode for pdb_ldap
6658 *********************************************************************/
6660 NTSTATUS
pdb_ldapsam_init_common(struct pdb_methods
**pdb_method
,
6661 const char *location
)
6664 struct ldapsam_privates
*ldap_state
= NULL
;
6665 uint32_t alg_rid_base
;
6666 char *alg_rid_base_string
= NULL
;
6667 LDAPMessage
*result
= NULL
;
6668 LDAPMessage
*entry
= NULL
;
6669 struct dom_sid ldap_domain_sid
;
6670 struct dom_sid secrets_domain_sid
;
6671 char *domain_sid_string
= NULL
;
6673 char *uri
= talloc_strdup( NULL
, location
);
6675 trim_char( uri
, '\"', '\"' );
6676 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6680 if (!NT_STATUS_IS_OK(nt_status
)) {
6684 (*pdb_method
)->name
= "ldapsam";
6686 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6687 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6688 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6689 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6690 (*pdb_method
)->search_users
= ldapsam_search_users
;
6691 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6692 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6693 (*pdb_method
)->is_responsible_for_wellknown
=
6694 ldapsam_is_responsible_for_wellknown
;
6696 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6697 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6698 (*pdb_method
)->enum_group_memberships
=
6699 ldapsam_enum_group_memberships
;
6700 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6701 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6702 (*pdb_method
)->id_to_sid
= ldapsam_id_to_sid
;
6704 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6705 (*pdb_method
)->create_user
= ldapsam_create_user
;
6706 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6707 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6708 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6709 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6710 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6711 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6715 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6716 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6718 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6720 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6722 ldap_state
->domain_name
, True
);
6724 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6725 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6726 "info, nor add one to the domain. "
6727 "We cannot work reliably without it.\n"));
6728 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6731 /* Given that the above might fail, everything below this must be
6734 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
6737 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6739 ldap_msgfree(result
);
6740 return NT_STATUS_UNSUCCESSFUL
;
6743 dn
= smbldap_talloc_dn(talloc_tos(),
6744 smbldap_get_ldap(ldap_state
->smbldap_state
),
6747 ldap_msgfree(result
);
6748 return NT_STATUS_UNSUCCESSFUL
;
6751 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6754 domain_sid_string
= smbldap_talloc_single_attribute(
6755 smbldap_get_ldap(ldap_state
->smbldap_state
),
6757 get_userattr_key2string(ldap_state
->schema_ver
,
6758 LDAP_ATTR_USER_SID
),
6761 if (domain_sid_string
) {
6763 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6764 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6765 "read as a valid SID\n", domain_sid_string
));
6766 ldap_msgfree(result
);
6767 TALLOC_FREE(domain_sid_string
);
6768 return NT_STATUS_INVALID_PARAMETER
;
6770 found_sid
= PDB_secrets_fetch_domain_sid(ldap_state
->domain_name
,
6771 &secrets_domain_sid
);
6772 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6773 &ldap_domain_sid
)) {
6774 struct dom_sid_buf buf1
, buf2
;
6775 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6776 "%s based on pdb_ldap results %s -> %s\n",
6777 ldap_state
->domain_name
,
6778 dom_sid_str_buf(&secrets_domain_sid
, &buf1
),
6779 dom_sid_str_buf(&ldap_domain_sid
, &buf2
)));
6781 /* reset secrets.tdb sid */
6782 PDB_secrets_store_domain_sid(ldap_state
->domain_name
,
6784 DEBUG(1, ("New global sam SID: %s\n",
6785 dom_sid_str_buf(get_global_sam_sid(),
6788 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6789 TALLOC_FREE(domain_sid_string
);
6792 alg_rid_base_string
= smbldap_talloc_single_attribute(
6793 smbldap_get_ldap(ldap_state
->smbldap_state
),
6795 get_attr_key2string( dominfo_attr_list
,
6796 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6798 if (alg_rid_base_string
) {
6799 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6800 if (alg_rid_base
!= algorithmic_rid_base()) {
6801 DEBUG(0, ("The value of 'algorithmic RID base' has "
6802 "changed since the LDAP\n"
6803 "database was initialised. Aborting. \n"));
6804 ldap_msgfree(result
);
6805 TALLOC_FREE(alg_rid_base_string
);
6806 return NT_STATUS_UNSUCCESSFUL
;
6808 TALLOC_FREE(alg_rid_base_string
);
6810 ldap_msgfree(result
);
6812 return NT_STATUS_OK
;
6815 NTSTATUS
pdb_ldapsam_init(TALLOC_CTX
*ctx
)
6819 nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
,
6821 pdb_ldapsam_init_common
);
6822 if (!NT_STATUS_IS_OK(nt_status
)) {
6826 /* Let pdb_nds register backends */
6829 return NT_STATUS_OK
;