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"
57 #include "lib/util/smb_strtox.h"
58 #include "lib/util/string_wrappers.h"
59 #include "source3/lib/substitute.h"
62 #define DBGC_CLASS DBGC_PASSDB
69 #include "passdb/pdb_ldap.h"
70 #include "passdb/pdb_nds.h"
71 #include "passdb/pdb_ldap_util.h"
72 #include "passdb/pdb_ldap_schema.h"
74 /**********************************************************************
75 Simple helper function to make stuff better readable
76 **********************************************************************/
78 LDAP
*priv2ld(struct ldapsam_privates
*priv
)
80 return smbldap_get_ldap(priv
->smbldap_state
);
83 /**********************************************************************
84 Get the attribute name given a user schame version.
85 **********************************************************************/
87 static const char* get_userattr_key2string( int schema_ver
, int key
)
89 switch ( schema_ver
) {
90 case SCHEMAVER_SAMBASAMACCOUNT
:
91 return get_attr_key2string( attrib_map_v30
, key
);
94 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
100 /**********************************************************************
101 Return the list of attribute names given a user schema version.
102 **********************************************************************/
104 const char** get_userattr_list( TALLOC_CTX
*mem_ctx
, int schema_ver
)
106 switch ( schema_ver
) {
107 case SCHEMAVER_SAMBASAMACCOUNT
:
108 return get_attr_list( mem_ctx
, attrib_map_v30
);
110 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
117 /**************************************************************************
118 Return the list of attribute names to delete given a user schema version.
119 **************************************************************************/
121 static const char** get_userattr_delete_list( TALLOC_CTX
*mem_ctx
,
124 switch ( schema_ver
) {
125 case SCHEMAVER_SAMBASAMACCOUNT
:
126 return get_attr_list( mem_ctx
,
127 attrib_map_to_delete_v30
);
129 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
137 /*******************************************************************
138 Generate the LDAP search filter for the objectclass based on the
139 version of the schema we are using.
140 ******************************************************************/
142 static const char* get_objclass_filter( int schema_ver
)
144 fstring objclass_filter
;
147 switch( schema_ver
) {
148 case SCHEMAVER_SAMBASAMACCOUNT
:
149 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
152 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
153 objclass_filter
[0] = '\0';
157 result
= talloc_strdup(talloc_tos(), objclass_filter
);
158 SMB_ASSERT(result
!= NULL
);
162 /*****************************************************************
163 Scan a sequence number off OpenLDAP's syncrepl contextCSN
164 ******************************************************************/
166 static NTSTATUS
ldapsam_get_seq_num(struct pdb_methods
*my_methods
, time_t *seq_num
)
168 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
169 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
170 LDAPMessage
*msg
= NULL
;
171 LDAPMessage
*entry
= NULL
;
173 char **values
= NULL
;
174 int rc
, num_result
, num_values
, rid
;
180 /* Unfortunatly there is no proper way to detect syncrepl-support in
181 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
182 * but do not show up in the root-DSE yet. Neither we can query the
183 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
184 * objectclass. Currently we require lp_ldap_suffix() to show up as
185 * namingContext. - Guenther
188 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False
)) {
193 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
197 if (!smbldap_has_naming_context(
198 smbldap_get_ldap(ldap_state
->smbldap_state
),
200 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
201 "as top-level namingContext\n", lp_ldap_suffix()));
205 mem_ctx
= talloc_init("ldapsam_get_seq_num");
208 return NT_STATUS_NO_MEMORY
;
210 if ((attrs
= talloc_array(mem_ctx
, const char *, 2)) == NULL
) {
211 ntstatus
= NT_STATUS_NO_MEMORY
;
215 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
216 rid
= lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
219 /* consumer syncreplCookie: */
220 /* csn=20050126161620Z#0000001#00#00000 */
221 attrs
[0] = talloc_strdup(mem_ctx
, "syncreplCookie");
223 suffix
= talloc_asprintf(mem_ctx
,
224 "cn=syncrepl%d,%s", rid
, lp_ldap_suffix());
226 ntstatus
= NT_STATUS_NO_MEMORY
;
231 /* provider contextCSN */
232 /* 20050126161620Z#000009#00#000000 */
233 attrs
[0] = talloc_strdup(mem_ctx
, "contextCSN");
235 suffix
= talloc_asprintf(mem_ctx
,
236 "cn=ldapsync,%s", lp_ldap_suffix());
239 ntstatus
= NT_STATUS_NO_MEMORY
;
244 rc
= smbldap_search(ldap_state
->smbldap_state
, suffix
,
245 LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, 0, &msg
);
247 if (rc
!= LDAP_SUCCESS
) {
251 num_result
= ldap_count_entries(
252 smbldap_get_ldap(ldap_state
->smbldap_state
), msg
);
253 if (num_result
!= 1) {
254 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result
));
258 entry
= ldap_first_entry(
259 smbldap_get_ldap(ldap_state
->smbldap_state
), msg
);
261 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
265 values
= ldap_get_values(
266 smbldap_get_ldap(ldap_state
->smbldap_state
), entry
, attrs
[0]);
267 if (values
== NULL
) {
268 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
272 num_values
= ldap_count_values(values
);
273 if (num_values
== 0) {
274 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
279 if (!next_token_talloc(mem_ctx
, &p
, &tok
, "#")) {
280 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
285 if (!strncmp(p
, "csn=", strlen("csn=")))
288 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs
[0], p
));
290 *seq_num
= generalized_to_unix_time(p
);
292 /* very basic sanity check */
294 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
299 ntstatus
= NT_STATUS_OK
;
303 ldap_value_free(values
);
307 talloc_destroy(mem_ctx
);
312 /*******************************************************************
313 Run the search by name.
314 ******************************************************************/
316 int ldapsam_search_suffix_by_name(struct ldapsam_privates
*ldap_state
,
318 LDAPMessage
** result
,
322 char *escape_user
= escape_ldap_string(talloc_tos(), user
);
326 return LDAP_NO_MEMORY
;
330 * in the filter expression, replace %u with the real name
331 * so in ldap filter, %u MUST exist :-)
333 filter
= talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
334 get_objclass_filter(ldap_state
->schema_ver
));
336 TALLOC_FREE(escape_user
);
337 return LDAP_NO_MEMORY
;
340 * have to use this here because $ is filtered out
344 filter
= talloc_all_string_sub(talloc_tos(),
345 filter
, "%u", escape_user
);
346 TALLOC_FREE(escape_user
);
348 return LDAP_NO_MEMORY
;
351 ret
= smbldap_search_suffix(ldap_state
->smbldap_state
,
352 filter
, attr
, result
);
357 /*******************************************************************
358 Run the search by SID.
359 ******************************************************************/
361 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
362 const struct dom_sid
*sid
, LDAPMessage
** result
,
367 struct dom_sid_buf sid_string
;
369 filter
= talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
370 get_userattr_key2string(ldap_state
->schema_ver
,
372 dom_sid_str_buf(sid
, &sid_string
),
373 get_objclass_filter(ldap_state
->schema_ver
));
375 return LDAP_NO_MEMORY
;
378 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
379 filter
, attr
, result
);
385 /*******************************************************************
386 Delete complete object or objectclass and attrs from
387 object found in search_result depending on lp_ldap_delete_dn
388 ******************************************************************/
390 static int ldapsam_delete_entry(struct ldapsam_privates
*priv
,
393 const char *objectclass
,
396 LDAPMod
**mods
= NULL
;
399 BerElement
*ptr
= NULL
;
401 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
);
403 return LDAP_NO_MEMORY
;
406 if (lp_ldap_delete_dn()) {
407 return smbldap_delete(priv
->smbldap_state
, dn
);
410 /* Ok, delete only the SAM attributes */
412 for (name
= ldap_first_attribute(priv2ld(priv
), entry
, &ptr
);
414 name
= ldap_next_attribute(priv2ld(priv
), entry
, ptr
)) {
417 /* We are only allowed to delete the attributes that
420 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
421 if (strequal(*attrib
, name
)) {
422 DEBUG(10, ("ldapsam_delete_entry: deleting "
423 "attribute %s\n", name
));
424 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
,
435 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
436 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
438 return smbldap_modify(priv
->smbldap_state
, dn
, mods
);
441 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates
*ldap_state
, LDAPMessage
* entry
)
446 temp
= smbldap_talloc_single_attribute(
447 smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
448 get_userattr_key2string(ldap_state
->schema_ver
,
449 LDAP_ATTR_MOD_TIMESTAMP
),
455 if ( !strptime(temp
, "%Y%m%d%H%M%SZ", &tm
)) {
456 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
466 /**********************************************************************
467 Initialize struct samu from an LDAP query.
468 (Based on init_sam_from_buffer in pdb_tdb.c)
469 *********************************************************************/
471 static bool init_sam_from_ldap(struct ldapsam_privates
*ldap_state
,
472 struct samu
* sampass
,
479 pass_can_change_time
,
482 char *username
= NULL
,
488 *logon_script
= NULL
,
489 *profile_path
= NULL
,
491 *workstations
= NULL
,
494 uint8_t smblmpwd
[LM_HASH_LEN
],
495 smbntpwd
[NT_HASH_LEN
];
496 bool use_samba_attrs
= True
;
498 uint16_t bad_password_count
= 0,
501 uint8_t hours
[MAX_HOURS_LEN
];
503 struct login_cache cache_entry
;
505 bool expand_explicit
= lp_passdb_expand_explicit();
507 TALLOC_CTX
*ctx
= talloc_init("init_sam_from_ldap");
512 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
513 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
517 if (priv2ld(ldap_state
) == NULL
) {
518 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
519 "ldap_struct is NULL!\n"));
523 if (!(username
= smbldap_talloc_first_attribute(priv2ld(ldap_state
),
527 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
532 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
534 nt_username
= talloc_strdup(ctx
, username
);
539 domain
= talloc_strdup(ctx
, ldap_state
->domain_name
);
544 pdb_set_username(sampass
, username
, PDB_SET
);
546 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
547 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
549 /* deal with different attributes between the schema first */
551 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
552 if ((temp
= smbldap_talloc_single_attribute(
553 smbldap_get_ldap(ldap_state
->smbldap_state
),
555 get_userattr_key2string(ldap_state
->schema_ver
,
558 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
561 if ((temp
= smbldap_talloc_single_attribute(
562 smbldap_get_ldap(ldap_state
->smbldap_state
),
564 get_userattr_key2string(ldap_state
->schema_ver
,
567 user_rid
= (uint32_t)atol(temp
);
568 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
572 if (IS_SAM_DEFAULT(sampass
, PDB_USERSID
)) {
573 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
574 get_userattr_key2string(ldap_state
->schema_ver
,
576 get_userattr_key2string(ldap_state
->schema_ver
,
582 temp
= smbldap_talloc_single_attribute(
583 smbldap_get_ldap(ldap_state
->smbldap_state
),
585 get_userattr_key2string(ldap_state
->schema_ver
,
586 LDAP_ATTR_PWD_LAST_SET
),
589 pass_last_set_time
= (time_t) atol(temp
);
590 pdb_set_pass_last_set_time(sampass
,
591 pass_last_set_time
, PDB_SET
);
594 temp
= smbldap_talloc_single_attribute(
595 smbldap_get_ldap(ldap_state
->smbldap_state
),
597 get_userattr_key2string(ldap_state
->schema_ver
,
598 LDAP_ATTR_LOGON_TIME
),
601 logon_time
= (time_t) atol(temp
);
602 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
605 temp
= smbldap_talloc_single_attribute(
606 smbldap_get_ldap(ldap_state
->smbldap_state
),
608 get_userattr_key2string(ldap_state
->schema_ver
,
609 LDAP_ATTR_LOGOFF_TIME
),
612 logoff_time
= (time_t) atol(temp
);
613 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
616 temp
= smbldap_talloc_single_attribute(
617 smbldap_get_ldap(ldap_state
->smbldap_state
),
619 get_userattr_key2string(ldap_state
->schema_ver
,
620 LDAP_ATTR_KICKOFF_TIME
),
623 kickoff_time
= (time_t) atol(temp
);
624 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
627 temp
= smbldap_talloc_single_attribute(
628 smbldap_get_ldap(ldap_state
->smbldap_state
),
630 get_userattr_key2string(ldap_state
->schema_ver
,
631 LDAP_ATTR_PWD_CAN_CHANGE
),
634 pass_can_change_time
= (time_t) atol(temp
);
635 pdb_set_pass_can_change_time(sampass
,
636 pass_can_change_time
, PDB_SET
);
639 /* recommend that 'gecos' and 'displayName' should refer to the same
640 * attribute OID. userFullName depreciated, only used by Samba
641 * primary rules of LDAP: don't make a new attribute when one is already defined
642 * that fits your needs; using cn then displayName rather than 'userFullName'
645 fullname
= smbldap_talloc_single_attribute(
646 smbldap_get_ldap(ldap_state
->smbldap_state
),
648 get_userattr_key2string(ldap_state
->schema_ver
,
649 LDAP_ATTR_DISPLAY_NAME
),
652 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
654 fullname
= smbldap_talloc_single_attribute(
655 smbldap_get_ldap(ldap_state
->smbldap_state
),
657 get_userattr_key2string(ldap_state
->schema_ver
,
661 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
665 dir_drive
= smbldap_talloc_single_attribute(
666 smbldap_get_ldap(ldap_state
->smbldap_state
),
668 get_userattr_key2string(ldap_state
->schema_ver
,
669 LDAP_ATTR_HOME_DRIVE
),
672 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
674 pdb_set_dir_drive( sampass
, lp_logon_drive(), PDB_DEFAULT
);
677 homedir
= smbldap_talloc_single_attribute(
678 smbldap_get_ldap(ldap_state
->smbldap_state
),
680 get_userattr_key2string(ldap_state
->schema_ver
,
681 LDAP_ATTR_HOME_PATH
),
684 if (expand_explicit
) {
685 homedir
= talloc_sub_basic(ctx
,
693 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
695 pdb_set_homedir(sampass
,
696 talloc_sub_basic(ctx
, username
, domain
,
701 logon_script
= smbldap_talloc_single_attribute(
702 smbldap_get_ldap(ldap_state
->smbldap_state
),
704 get_userattr_key2string(ldap_state
->schema_ver
,
705 LDAP_ATTR_LOGON_SCRIPT
),
708 if (expand_explicit
) {
709 logon_script
= talloc_sub_basic(ctx
,
717 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
719 pdb_set_logon_script(sampass
,
720 talloc_sub_basic(ctx
, username
, domain
,
725 profile_path
= smbldap_talloc_single_attribute(
726 smbldap_get_ldap(ldap_state
->smbldap_state
),
728 get_userattr_key2string(ldap_state
->schema_ver
,
729 LDAP_ATTR_PROFILE_PATH
),
732 if (expand_explicit
) {
733 profile_path
= talloc_sub_basic(ctx
,
741 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
743 pdb_set_profile_path(sampass
,
744 talloc_sub_basic(ctx
, username
, domain
,
749 acct_desc
= smbldap_talloc_single_attribute(
750 smbldap_get_ldap(ldap_state
->smbldap_state
),
752 get_userattr_key2string(ldap_state
->schema_ver
,
756 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
759 workstations
= smbldap_talloc_single_attribute(
760 smbldap_get_ldap(ldap_state
->smbldap_state
),
762 get_userattr_key2string(ldap_state
->schema_ver
,
766 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
769 munged_dial
= smbldap_talloc_single_attribute(
770 smbldap_get_ldap(ldap_state
->smbldap_state
),
772 get_userattr_key2string(ldap_state
->schema_ver
,
773 LDAP_ATTR_MUNGED_DIAL
),
776 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
779 /* FIXME: hours stuff should be cleaner */
783 memset(hours
, 0xff, hours_len
);
785 if (ldap_state
->is_nds_ldap
) {
788 char clear_text_pw
[512];
790 /* Make call to Novell eDirectory ldap extension to get clear text password.
791 NOTE: This will only work if we have an SSL connection to eDirectory. */
792 user_dn
= smbldap_talloc_dn(
793 ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
),
795 if (user_dn
!= NULL
) {
796 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username
, user_dn
));
798 pwd_len
= sizeof(clear_text_pw
);
799 if (pdb_nds_get_password(ldap_state
->smbldap_state
, user_dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
800 nt_lm_owf_gen(clear_text_pw
, smbntpwd
, smblmpwd
);
801 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
802 TALLOC_FREE(user_dn
);
805 ZERO_STRUCT(smblmpwd
);
806 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
807 TALLOC_FREE(user_dn
);
810 ZERO_STRUCT(smbntpwd
);
811 use_samba_attrs
= False
;
814 TALLOC_FREE(user_dn
);
817 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username
));
821 if (use_samba_attrs
) {
822 temp
= smbldap_talloc_single_attribute(
823 smbldap_get_ldap(ldap_state
->smbldap_state
),
825 get_userattr_key2string(ldap_state
->schema_ver
,
829 pdb_gethexpwd(temp
, smblmpwd
);
830 memset((char *)temp
, '\0', strlen(temp
)+1);
831 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
)) {
834 ZERO_STRUCT(smblmpwd
);
837 temp
= smbldap_talloc_single_attribute(
838 smbldap_get_ldap(ldap_state
->smbldap_state
),
840 get_userattr_key2string(ldap_state
->schema_ver
,
844 pdb_gethexpwd(temp
, smbntpwd
);
845 memset((char *)temp
, '\0', strlen(temp
)+1);
846 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
)) {
849 ZERO_STRUCT(smbntpwd
);
855 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
857 uint8_t *pwhist
= NULL
;
859 char *history_string
= talloc_array(ctx
, char,
860 MAX_PW_HISTORY_LEN
*64);
862 if (!history_string
) {
866 pwHistLen
= MIN(pwHistLen
, MAX_PW_HISTORY_LEN
);
868 pwhist
= talloc_zero_array(ctx
, uint8_t,
869 pwHistLen
* PW_HISTORY_ENTRY_LEN
);
870 if (pwhist
== NULL
) {
871 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
875 if (smbldap_get_single_attribute(
876 smbldap_get_ldap(ldap_state
->smbldap_state
),
878 get_userattr_key2string(ldap_state
->schema_ver
,
879 LDAP_ATTR_PWD_HISTORY
),
881 MAX_PW_HISTORY_LEN
*64)) {
882 bool hex_failed
= false;
883 for (i
= 0; i
< pwHistLen
; i
++){
884 /* Get the 16 byte salt. */
885 if (!pdb_gethexpwd(&history_string
[i
*64],
886 &pwhist
[i
*PW_HISTORY_ENTRY_LEN
])) {
890 /* Get the 16 byte MD5 hash of salt+passwd. */
891 if (!pdb_gethexpwd(&history_string
[(i
*64)+32],
892 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+
893 PW_HISTORY_SALT_LEN
])) {
899 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
901 memset(pwhist
, '\0', pwHistLen
* PW_HISTORY_ENTRY_LEN
);
904 if (!pdb_set_pw_history(sampass
, pwhist
, pwHistLen
, PDB_SET
)){
909 temp
= smbldap_talloc_single_attribute(
910 smbldap_get_ldap(ldap_state
->smbldap_state
),
912 get_userattr_key2string(ldap_state
->schema_ver
,
916 uint32_t acct_ctrl
= 0;
917 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
919 if (acct_ctrl
== 0) {
920 acct_ctrl
|= ACB_NORMAL
;
923 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
926 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
927 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
929 temp
= smbldap_talloc_single_attribute(
930 smbldap_get_ldap(ldap_state
->smbldap_state
),
932 get_userattr_key2string(ldap_state
->schema_ver
,
933 LDAP_ATTR_BAD_PASSWORD_COUNT
),
936 bad_password_count
= (uint32_t) atol(temp
);
937 pdb_set_bad_password_count(sampass
,
938 bad_password_count
, PDB_SET
);
941 temp
= smbldap_talloc_single_attribute(
942 smbldap_get_ldap(ldap_state
->smbldap_state
),
944 get_userattr_key2string(ldap_state
->schema_ver
,
945 LDAP_ATTR_BAD_PASSWORD_TIME
),
948 bad_password_time
= (time_t) atol(temp
);
949 pdb_set_bad_password_time(sampass
, bad_password_time
, PDB_SET
);
953 temp
= smbldap_talloc_single_attribute(
954 smbldap_get_ldap(ldap_state
->smbldap_state
),
956 get_userattr_key2string(ldap_state
->schema_ver
,
957 LDAP_ATTR_LOGON_COUNT
),
960 logon_count
= (uint32_t) atol(temp
);
961 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
964 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
966 temp
= smbldap_talloc_single_attribute(
967 smbldap_get_ldap(ldap_state
->smbldap_state
),
969 get_userattr_key2string(ldap_state
->schema_ver
,
970 LDAP_ATTR_LOGON_HOURS
),
973 pdb_gethexhours(temp
, hours
);
974 memset((char *)temp
, '\0', strlen(temp
) +1);
975 pdb_set_hours(sampass
, hours
, hours_len
, PDB_SET
);
979 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
980 struct passwd unix_pw
;
981 bool have_uid
= false;
982 bool have_gid
= false;
983 struct dom_sid mapped_gsid
;
984 const struct dom_sid
*primary_gsid
;
988 ZERO_STRUCT(unix_pw
);
990 unix_pw
.pw_name
= username
;
991 unix_pw
.pw_passwd
= discard_const_p(char, "x");
993 temp
= smbldap_talloc_single_attribute(
999 /* We've got a uid, feed the cache */
1000 unix_pw
.pw_uid
= smb_strtoul(temp
,
1006 DBG_ERR("Failed to convert UID\n");
1011 temp
= smbldap_talloc_single_attribute(
1012 priv2ld(ldap_state
),
1017 /* We've got a uid, feed the cache */
1018 unix_pw
.pw_gid
= smb_strtoul(temp
,
1024 DBG_ERR("Failed to convert GID\n");
1029 unix_pw
.pw_gecos
= smbldap_talloc_single_attribute(
1030 priv2ld(ldap_state
),
1034 if (unix_pw
.pw_gecos
== NULL
) {
1035 unix_pw
.pw_gecos
= fullname
;
1037 unix_pw
.pw_dir
= smbldap_talloc_single_attribute(
1038 priv2ld(ldap_state
),
1042 if (unix_pw
.pw_dir
== NULL
) {
1043 unix_pw
.pw_dir
= discard_const_p(char, "");
1045 unix_pw
.pw_shell
= smbldap_talloc_single_attribute(
1046 priv2ld(ldap_state
),
1050 if (unix_pw
.pw_shell
== NULL
) {
1051 unix_pw
.pw_shell
= discard_const_p(char, "");
1054 if (have_uid
&& have_gid
) {
1055 sampass
->unix_pw
= tcopy_passwd(sampass
, &unix_pw
);
1057 sampass
->unix_pw
= Get_Pwnam_alloc(sampass
, unix_pw
.pw_name
);
1060 if (sampass
->unix_pw
== NULL
) {
1061 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1062 pdb_get_username(sampass
)));
1066 id
.id
= sampass
->unix_pw
->pw_uid
;
1067 id
.type
= ID_TYPE_UID
;
1069 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass
), &id
);
1071 gid_to_sid(&mapped_gsid
, sampass
->unix_pw
->pw_gid
);
1072 primary_gsid
= pdb_get_group_sid(sampass
);
1073 if (primary_gsid
&& dom_sid_equal(primary_gsid
, &mapped_gsid
)) {
1074 id
.id
= sampass
->unix_pw
->pw_gid
;
1075 id
.type
= ID_TYPE_GID
;
1077 idmap_cache_set_sid2unixid(primary_gsid
, &id
);
1081 /* check the timestamp of the cache vs ldap entry */
1082 if (!(ldap_entry_time
= ldapsam_get_entry_timestamp(ldap_state
,
1088 /* see if we have newer updates */
1089 if (!login_cache_read(sampass
, &cache_entry
)) {
1090 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1091 (unsigned int)pdb_get_bad_password_count(sampass
),
1092 (unsigned int)pdb_get_bad_password_time(sampass
)));
1097 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1098 (unsigned int)ldap_entry_time
,
1099 (unsigned int)cache_entry
.entry_timestamp
,
1100 (unsigned int)cache_entry
.bad_password_time
));
1102 if (ldap_entry_time
> cache_entry
.entry_timestamp
) {
1103 /* cache is older than directory , so
1104 we need to delete the entry but allow the
1105 fields to be written out */
1106 login_cache_delentry(sampass
);
1109 pdb_set_acct_ctrl(sampass
,
1110 pdb_get_acct_ctrl(sampass
) |
1111 (cache_entry
.acct_ctrl
& ACB_AUTOLOCK
),
1113 pdb_set_bad_password_count(sampass
,
1114 cache_entry
.bad_password_count
,
1116 pdb_set_bad_password_time(sampass
,
1117 cache_entry
.bad_password_time
,
1129 /**********************************************************************
1130 Initialize the ldap db from a struct samu. Called on update.
1131 (Based on init_buffer_from_sam in pdb_tdb.c)
1132 *********************************************************************/
1134 static bool init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
1135 LDAPMessage
*existing
,
1136 LDAPMod
*** mods
, struct samu
* sampass
,
1137 bool (*need_update
)(const struct samu
*,
1142 if (mods
== NULL
|| sampass
== NULL
) {
1143 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1150 * took out adding "objectclass: sambaAccount"
1151 * do this on a per-mod basis
1153 if (need_update(sampass
, PDB_USERNAME
)) {
1154 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1156 "uid", pdb_get_username(sampass
));
1157 if (ldap_state
->is_nds_ldap
) {
1159 smbldap_get_ldap(ldap_state
->smbldap_state
),
1161 "cn", pdb_get_username(sampass
));
1163 smbldap_get_ldap(ldap_state
->smbldap_state
),
1165 "sn", pdb_get_username(sampass
));
1169 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
1171 /* only update the RID if we actually need to */
1172 if (need_update(sampass
, PDB_USERSID
)) {
1173 struct dom_sid_buf sid_str
;
1174 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
1176 switch ( ldap_state
->schema_ver
) {
1177 case SCHEMAVER_SAMBASAMACCOUNT
:
1180 ldap_state
->smbldap_state
),
1182 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1183 dom_sid_str_buf(user_sid
, &sid_str
));
1187 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1192 /* we don't need to store the primary group RID - so leaving it
1193 'free' to hang off the unix primary group makes life easier */
1195 if (need_update(sampass
, PDB_GROUPSID
)) {
1196 struct dom_sid_buf sid_str
;
1197 const struct dom_sid
*group_sid
= pdb_get_group_sid(sampass
);
1199 switch ( ldap_state
->schema_ver
) {
1200 case SCHEMAVER_SAMBASAMACCOUNT
:
1203 ldap_state
->smbldap_state
),
1205 get_userattr_key2string(ldap_state
->schema_ver
,
1206 LDAP_ATTR_PRIMARY_GROUP_SID
),
1207 dom_sid_str_buf(group_sid
, &sid_str
));
1211 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1217 /* displayName, cn, and gecos should all be the same
1218 * most easily accomplished by giving them the same OID
1219 * gecos isn't set here b/c it should be handled by the
1221 * We change displayName only and fall back to cn if
1222 * it does not exist.
1225 if (need_update(sampass
, PDB_FULLNAME
))
1226 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1228 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
1229 pdb_get_fullname(sampass
));
1231 if (need_update(sampass
, PDB_ACCTDESC
))
1232 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1234 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
1235 pdb_get_acct_desc(sampass
));
1237 if (need_update(sampass
, PDB_WORKSTATIONS
))
1238 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1240 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
1241 pdb_get_workstations(sampass
));
1243 if (need_update(sampass
, PDB_MUNGEDDIAL
))
1244 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1246 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
1247 pdb_get_munged_dial(sampass
));
1249 if (need_update(sampass
, PDB_SMBHOME
))
1250 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1252 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
1253 pdb_get_homedir(sampass
));
1255 if (need_update(sampass
, PDB_DRIVE
))
1256 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1258 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
1259 pdb_get_dir_drive(sampass
));
1261 if (need_update(sampass
, PDB_LOGONSCRIPT
))
1262 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1264 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
1265 pdb_get_logon_script(sampass
));
1267 if (need_update(sampass
, PDB_PROFILE
))
1268 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1270 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
1271 pdb_get_profile_path(sampass
));
1273 if (asprintf(&temp
, "%li", (long int)pdb_get_logon_time(sampass
)) < 0) {
1276 if (need_update(sampass
, PDB_LOGONTIME
))
1277 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1279 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
1282 if (asprintf(&temp
, "%li", (long int)pdb_get_logoff_time(sampass
)) < 0) {
1285 if (need_update(sampass
, PDB_LOGOFFTIME
))
1286 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1288 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
1291 if (asprintf(&temp
, "%li", (long int)pdb_get_kickoff_time(sampass
)) < 0) {
1294 if (need_update(sampass
, PDB_KICKOFFTIME
))
1295 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1297 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
1300 if (asprintf(&temp
, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass
)) < 0) {
1303 if (need_update(sampass
, PDB_CANCHANGETIME
))
1304 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
),
1306 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
1309 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
1310 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
1312 if (need_update(sampass
, PDB_LMPASSWD
)) {
1313 const uchar
*lm_pw
= pdb_get_lanman_passwd(sampass
);
1316 pdb_sethexpwd(pwstr
, lm_pw
,
1317 pdb_get_acct_ctrl(sampass
));
1320 ldap_state
->smbldap_state
),
1322 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1327 ldap_state
->smbldap_state
),
1329 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
1333 if (need_update(sampass
, PDB_NTPASSWD
)) {
1334 const uchar
*nt_pw
= pdb_get_nt_passwd(sampass
);
1337 pdb_sethexpwd(pwstr
, nt_pw
,
1338 pdb_get_acct_ctrl(sampass
));
1341 ldap_state
->smbldap_state
),
1343 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1348 ldap_state
->smbldap_state
),
1350 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
1355 if (need_update(sampass
, PDB_PWHISTORY
)) {
1357 uint32_t pwHistLen
= 0;
1358 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
, &pwHistLen
);
1360 pwstr
= SMB_MALLOC_ARRAY(char, 1024);
1364 if (pwHistLen
== 0) {
1365 /* Remove any password history from the LDAP store. */
1366 memset(pwstr
, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1370 uint32_t currHistLen
= 0;
1371 const uint8_t *pwhist
= pdb_get_pw_history(sampass
, &currHistLen
);
1372 if (pwhist
!= NULL
) {
1373 /* We can only store (1024-1/64 password history entries. */
1374 pwHistLen
= MIN(pwHistLen
, ((1024-1)/64));
1375 for (i
=0; i
< pwHistLen
&& i
< currHistLen
; i
++) {
1376 /* Store the salt. */
1377 pdb_sethexpwd(&pwstr
[i
*64], &pwhist
[i
*PW_HISTORY_ENTRY_LEN
], 0);
1378 /* Followed by the md5 hash of salt + md4 hash */
1379 pdb_sethexpwd(&pwstr
[(i
*64)+32],
1380 &pwhist
[(i
*PW_HISTORY_ENTRY_LEN
)+PW_HISTORY_SALT_LEN
], 0);
1381 DEBUG(100, ("pwstr=%s\n", pwstr
));
1386 smbldap_get_ldap(ldap_state
->smbldap_state
),
1388 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_HISTORY
),
1393 if (need_update(sampass
, PDB_PASSLASTSET
)) {
1394 if (asprintf(&temp
, "%li",
1395 (long int)pdb_get_pass_last_set_time(sampass
)) < 0) {
1399 smbldap_get_ldap(ldap_state
->smbldap_state
),
1401 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
1407 if (need_update(sampass
, PDB_HOURS
)) {
1408 const uint8_t *hours
= pdb_get_hours(sampass
);
1411 pdb_sethexhours(hourstr
, hours
);
1413 smbldap_get_ldap(ldap_state
->smbldap_state
),
1416 get_userattr_key2string(ldap_state
->schema_ver
,
1417 LDAP_ATTR_LOGON_HOURS
),
1422 if (need_update(sampass
, PDB_ACCTCTRL
))
1424 smbldap_get_ldap(ldap_state
->smbldap_state
),
1426 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
1427 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1429 /* password lockout cache:
1430 - If we are now autolocking or clearing, we write to ldap
1431 - If we are clearing, we delete the cache entry
1432 - If the count is > 0, we update the cache
1434 This even means when autolocking, we cache, just in case the
1435 update doesn't work, and we have to cache the autolock flag */
1437 if (need_update(sampass
, PDB_BAD_PASSWORD_COUNT
)) /* &&
1438 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1439 uint16_t badcount
= pdb_get_bad_password_count(sampass
);
1440 time_t badtime
= pdb_get_bad_password_time(sampass
);
1442 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT
, &pol
);
1444 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1445 (unsigned int)pol
, (unsigned int)badcount
, (unsigned int)badtime
));
1447 if ((badcount
>= pol
) || (badcount
== 0)) {
1448 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1449 (unsigned int)badcount
, (unsigned int)badtime
));
1450 if (asprintf(&temp
, "%li", (long)badcount
) < 0) {
1454 smbldap_get_ldap(ldap_state
->smbldap_state
),
1456 get_userattr_key2string(
1457 ldap_state
->schema_ver
,
1458 LDAP_ATTR_BAD_PASSWORD_COUNT
),
1462 if (asprintf(&temp
, "%li", (long int)badtime
) < 0) {
1466 smbldap_get_ldap(ldap_state
->smbldap_state
),
1468 get_userattr_key2string(
1469 ldap_state
->schema_ver
,
1470 LDAP_ATTR_BAD_PASSWORD_TIME
),
1474 if (badcount
== 0) {
1475 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass
)));
1476 login_cache_delentry(sampass
);
1478 struct login_cache cache_entry
;
1480 cache_entry
.entry_timestamp
= time(NULL
);
1481 cache_entry
.acct_ctrl
= pdb_get_acct_ctrl(sampass
);
1482 cache_entry
.bad_password_count
= badcount
;
1483 cache_entry
.bad_password_time
= badtime
;
1485 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1486 login_cache_write(sampass
, &cache_entry
);
1493 /**********************************************************************
1494 End enumeration of the LDAP password list.
1495 *********************************************************************/
1497 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
1499 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1500 if (ldap_state
->result
) {
1501 ldap_msgfree(ldap_state
->result
);
1502 ldap_state
->result
= NULL
;
1506 static void append_attr(TALLOC_CTX
*mem_ctx
, const char ***attr_list
,
1507 const char *new_attr
)
1511 if (new_attr
== NULL
) {
1515 for (i
=0; (*attr_list
)[i
] != NULL
; i
++) {
1519 (*attr_list
) = talloc_realloc(mem_ctx
, (*attr_list
),
1521 SMB_ASSERT((*attr_list
) != NULL
);
1522 (*attr_list
)[i
] = talloc_strdup((*attr_list
), new_attr
);
1523 (*attr_list
)[i
+1] = NULL
;
1526 static void ldapsam_add_unix_attributes(TALLOC_CTX
*mem_ctx
,
1527 const char ***attr_list
)
1529 append_attr(mem_ctx
, attr_list
, "uidNumber");
1530 append_attr(mem_ctx
, attr_list
, "gidNumber");
1531 append_attr(mem_ctx
, attr_list
, "homeDirectory");
1532 append_attr(mem_ctx
, attr_list
, "loginShell");
1533 append_attr(mem_ctx
, attr_list
, "gecos");
1536 /**********************************************************************
1537 Get struct samu entry from LDAP by username.
1538 *********************************************************************/
1540 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, struct samu
*user
, const char *sname
)
1542 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1543 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1544 LDAPMessage
*result
= NULL
;
1545 LDAPMessage
*entry
= NULL
;
1547 const char ** attr_list
;
1550 attr_list
= get_userattr_list( user
, ldap_state
->schema_ver
);
1551 append_attr(user
, &attr_list
,
1552 get_userattr_key2string(ldap_state
->schema_ver
,
1553 LDAP_ATTR_MOD_TIMESTAMP
));
1554 ldapsam_add_unix_attributes(user
, &attr_list
);
1555 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
,
1557 TALLOC_FREE( attr_list
);
1559 if ( rc
!= LDAP_SUCCESS
)
1560 return NT_STATUS_NO_SUCH_USER
;
1562 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1566 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1567 ldap_msgfree(result
);
1568 return NT_STATUS_NO_SUCH_USER
;
1569 } else if (count
> 1) {
1570 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1571 ldap_msgfree(result
);
1572 return NT_STATUS_NO_SUCH_USER
;
1575 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1578 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1579 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1580 ldap_msgfree(result
);
1581 return NT_STATUS_NO_SUCH_USER
;
1583 pdb_set_backend_private_data(user
, result
, NULL
,
1584 my_methods
, PDB_CHANGED
);
1585 smbldap_talloc_autofree_ldapmsg(user
, result
);
1588 ldap_msgfree(result
);
1593 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1594 const struct dom_sid
*sid
, LDAPMessage
**result
)
1597 const char ** attr_list
;
1599 switch ( ldap_state
->schema_ver
) {
1600 case SCHEMAVER_SAMBASAMACCOUNT
: {
1601 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1602 if (tmp_ctx
== NULL
) {
1603 return LDAP_NO_MEMORY
;
1606 attr_list
= get_userattr_list(tmp_ctx
,
1607 ldap_state
->schema_ver
);
1608 append_attr(tmp_ctx
, &attr_list
,
1609 get_userattr_key2string(
1610 ldap_state
->schema_ver
,
1611 LDAP_ATTR_MOD_TIMESTAMP
));
1612 ldapsam_add_unix_attributes(tmp_ctx
, &attr_list
);
1613 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
,
1615 TALLOC_FREE(tmp_ctx
);
1617 if ( rc
!= LDAP_SUCCESS
)
1623 DEBUG(0,("Invalid schema version specified\n"));
1629 /**********************************************************************
1630 Get struct samu entry from LDAP by SID.
1631 *********************************************************************/
1633 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1635 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1636 LDAPMessage
*result
= NULL
;
1637 LDAPMessage
*entry
= NULL
;
1641 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1643 if (rc
!= LDAP_SUCCESS
)
1644 return NT_STATUS_NO_SUCH_USER
;
1646 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1650 struct dom_sid_buf buf
;
1651 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1653 dom_sid_str_buf(sid
, &buf
),
1655 ldap_msgfree(result
);
1656 return NT_STATUS_NO_SUCH_USER
;
1657 } else if (count
> 1) {
1658 struct dom_sid_buf buf
;
1659 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1660 "[%s]. Failing. count=%d\n",
1661 dom_sid_str_buf(sid
, &buf
),
1663 ldap_msgfree(result
);
1664 return NT_STATUS_NO_SUCH_USER
;
1667 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1670 ldap_msgfree(result
);
1671 return NT_STATUS_NO_SUCH_USER
;
1674 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1675 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1676 ldap_msgfree(result
);
1677 return NT_STATUS_NO_SUCH_USER
;
1680 pdb_set_backend_private_data(user
, result
, NULL
,
1681 my_methods
, PDB_CHANGED
);
1682 smbldap_talloc_autofree_ldapmsg(user
, result
);
1683 return NT_STATUS_OK
;
1686 /********************************************************************
1687 Do the actual modification - also change a plaintext passord if
1689 **********************************************************************/
1691 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1692 struct samu
*newpwd
, char *dn
,
1693 LDAPMod
**mods
, int ldap_op
,
1694 bool (*need_update
)(const struct samu
*, enum pdb_elements
))
1696 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1699 if (!newpwd
|| !dn
) {
1700 return NT_STATUS_INVALID_PARAMETER
;
1703 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1704 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1705 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1706 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1709 char *retoid
= NULL
;
1710 struct berval
*retdata
= NULL
;
1711 char *utf8_password
;
1713 size_t converted_size
;
1716 if (!ldap_state
->is_nds_ldap
) {
1718 if (!smbldap_has_extension(
1720 ldap_state
->smbldap_state
),
1721 LDAP_EXOP_MODIFY_PASSWD
)) {
1722 DEBUG(2, ("ldap password change requested, but LDAP "
1723 "server does not support it -- ignoring\n"));
1724 return NT_STATUS_OK
;
1728 if (!push_utf8_talloc(talloc_tos(), &utf8_password
,
1729 pdb_get_plaintext_passwd(newpwd
),
1732 return NT_STATUS_NO_MEMORY
;
1735 if (!push_utf8_talloc(talloc_tos(), &utf8_dn
, dn
, &converted_size
)) {
1736 TALLOC_FREE(utf8_password
);
1737 return NT_STATUS_NO_MEMORY
;
1740 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1741 DEBUG(0,("ber_alloc_t returns NULL\n"));
1742 TALLOC_FREE(utf8_password
);
1743 TALLOC_FREE(utf8_dn
);
1744 return NT_STATUS_UNSUCCESSFUL
;
1747 if ((ber_printf (ber
, "{") < 0) ||
1748 (ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
,
1750 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1753 TALLOC_FREE(utf8_dn
);
1754 TALLOC_FREE(utf8_password
);
1755 return NT_STATUS_UNSUCCESSFUL
;
1758 if ((utf8_password
!= NULL
) && (*utf8_password
!= '\0')) {
1759 ret
= ber_printf(ber
, "ts}",
1760 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
,
1763 ret
= ber_printf(ber
, "}");
1767 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1770 TALLOC_FREE(utf8_dn
);
1771 TALLOC_FREE(utf8_password
);
1772 return NT_STATUS_UNSUCCESSFUL
;
1775 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1776 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1778 TALLOC_FREE(utf8_dn
);
1779 TALLOC_FREE(utf8_password
);
1780 return NT_STATUS_UNSUCCESSFUL
;
1783 TALLOC_FREE(utf8_dn
);
1784 TALLOC_FREE(utf8_password
);
1787 if (!ldap_state
->is_nds_ldap
) {
1788 rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1789 LDAP_EXOP_MODIFY_PASSWD
,
1790 bv
, NULL
, NULL
, &retoid
,
1793 rc
= pdb_nds_set_password(ldap_state
->smbldap_state
, dn
,
1794 pdb_get_plaintext_passwd(newpwd
));
1796 if (rc
!= LDAP_SUCCESS
) {
1797 char *ld_error
= NULL
;
1799 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
1800 DEBUG(3, ("Could not set userPassword "
1801 "attribute due to an objectClass "
1802 "violation -- ignoring\n"));
1804 return NT_STATUS_OK
;
1808 smbldap_get_ldap(ldap_state
->smbldap_state
),
1809 LDAP_OPT_ERROR_STRING
,
1811 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1812 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1813 SAFE_FREE(ld_error
);
1815 #if defined(LDAP_CONSTRAINT_VIOLATION)
1816 if (rc
== LDAP_CONSTRAINT_VIOLATION
)
1817 return NT_STATUS_PASSWORD_RESTRICTION
;
1819 return NT_STATUS_UNSUCCESSFUL
;
1821 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1822 #ifdef DEBUG_PASSWORD
1823 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1826 ber_bvfree(retdata
);
1828 ldap_memfree(retoid
);
1834 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1835 /* may be password change below however */
1839 if (ldap_state
->is_nds_ldap
) {
1840 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1844 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1848 rc
= smbldap_add(ldap_state
->smbldap_state
,
1851 case LDAP_MOD_REPLACE
:
1852 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1856 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1858 return NT_STATUS_INVALID_PARAMETER
;
1861 if (rc
!=LDAP_SUCCESS
) {
1862 return NT_STATUS_UNSUCCESSFUL
;
1866 return NT_STATUS_OK
;
1869 /**********************************************************************
1870 Delete entry from LDAP for username.
1871 *********************************************************************/
1873 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
,
1874 struct samu
* sam_acct
)
1876 struct ldapsam_privates
*priv
=
1877 (struct ldapsam_privates
*)my_methods
->private_data
;
1880 LDAPMessage
*msg
, *entry
;
1881 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1882 const char **attr_list
;
1883 TALLOC_CTX
*mem_ctx
;
1886 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1887 return NT_STATUS_INVALID_PARAMETER
;
1890 sname
= pdb_get_username(sam_acct
);
1892 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1895 mem_ctx
= talloc_new(NULL
);
1896 if (mem_ctx
== NULL
) {
1897 DEBUG(0, ("talloc_new failed\n"));
1901 attr_list
= get_userattr_delete_list(mem_ctx
, priv
->schema_ver
);
1902 if (attr_list
== NULL
) {
1906 rc
= ldapsam_search_suffix_by_name(priv
, sname
, &msg
, attr_list
);
1908 if ((rc
!= LDAP_SUCCESS
) ||
1909 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
1910 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
1911 DEBUG(5, ("Could not find user %s\n", sname
));
1912 result
= NT_STATUS_NO_SUCH_USER
;
1916 rc
= ldapsam_delete_entry(
1917 priv
, mem_ctx
, entry
,
1918 priv
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
?
1919 LDAP_OBJ_SAMBASAMACCOUNT
: 0,
1922 result
= (rc
== LDAP_SUCCESS
) ?
1923 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1926 TALLOC_FREE(mem_ctx
);
1930 /**********************************************************************
1932 *********************************************************************/
1934 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
1937 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1940 LDAPMessage
*result
= NULL
;
1941 LDAPMessage
*entry
= NULL
;
1942 LDAPMod
**mods
= NULL
;
1943 const char **attr_list
;
1945 result
= (LDAPMessage
*)pdb_get_backend_private_data(newpwd
, my_methods
);
1947 attr_list
= get_userattr_list(NULL
, ldap_state
->schema_ver
);
1948 if (pdb_get_username(newpwd
) == NULL
) {
1949 return NT_STATUS_INVALID_PARAMETER
;
1951 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1952 TALLOC_FREE( attr_list
);
1953 if (rc
!= LDAP_SUCCESS
) {
1954 return NT_STATUS_UNSUCCESSFUL
;
1956 pdb_set_backend_private_data(newpwd
, result
, NULL
,
1957 my_methods
, PDB_CHANGED
);
1958 smbldap_talloc_autofree_ldapmsg(newpwd
, result
);
1961 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
1963 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1964 return NT_STATUS_UNSUCCESSFUL
;
1967 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
1969 dn
= smbldap_talloc_dn(talloc_tos(),
1970 smbldap_get_ldap(ldap_state
->smbldap_state
),
1973 return NT_STATUS_UNSUCCESSFUL
;
1976 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1978 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1979 pdb_element_is_changed
)) {
1980 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1983 ldap_mods_free(mods
,True
);
1984 return NT_STATUS_UNSUCCESSFUL
;
1987 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY
)
1988 && (mods
== NULL
)) {
1989 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1990 pdb_get_username(newpwd
)));
1992 return NT_STATUS_OK
;
1995 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, pdb_element_is_changed
);
1998 ldap_mods_free(mods
,True
);
2004 * We need to set the backend private data to NULL here. For example
2005 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2006 * same one, and with the explicit delete / add logic for attribute
2007 * values the second time we would use the wrong "old" value which
2008 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2010 * The existing LDAPMessage is still being auto-freed by the
2013 pdb_set_backend_private_data(newpwd
, NULL
, NULL
, my_methods
,
2016 if (!NT_STATUS_IS_OK(ret
)) {
2020 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2021 pdb_get_username(newpwd
)));
2022 return NT_STATUS_OK
;
2025 /***************************************************************************
2026 Renames a struct samu
2027 - The "rename user script" has full responsibility for changing everything
2028 ***************************************************************************/
2030 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
2031 TALLOC_CTX
*tmp_ctx
,
2033 uint32_t member_rid
);
2035 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2036 TALLOC_CTX
*mem_ctx
,
2038 struct dom_sid
**pp_sids
,
2040 uint32_t *p_num_groups
);
2042 static NTSTATUS
ldapsam_rename_sam_account(struct pdb_methods
*my_methods
,
2043 struct samu
*old_acct
,
2044 const char *newname
)
2046 const struct loadparm_substitution
*lp_sub
=
2047 loadparm_s3_global_substitution();
2048 const char *oldname
;
2050 char *rename_script
= NULL
;
2051 fstring oldname_lower
, newname_lower
;
2054 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2055 return NT_STATUS_INVALID_PARAMETER
;
2058 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2059 return NT_STATUS_INVALID_PARAMETER
;
2062 oldname
= pdb_get_username(old_acct
);
2064 /* rename the posix user */
2065 rename_script
= lp_rename_user_script(talloc_tos(), lp_sub
);
2066 if (rename_script
== NULL
) {
2067 return NT_STATUS_NO_MEMORY
;
2070 if (!(*rename_script
)) {
2071 TALLOC_FREE(rename_script
);
2072 return NT_STATUS_ACCESS_DENIED
;
2075 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2078 /* We have to allow the account name to end with a '$'.
2079 Also, follow the semantics in _samr_create_user() and lower case the
2080 posix name but preserve the case in passdb */
2082 fstrcpy( oldname_lower
, oldname
);
2083 if (!strlower_m( oldname_lower
)) {
2084 return NT_STATUS_INVALID_PARAMETER
;
2086 fstrcpy( newname_lower
, newname
);
2087 if (!strlower_m( newname_lower
)) {
2088 return NT_STATUS_INVALID_PARAMETER
;
2091 rename_script
= realloc_string_sub2(rename_script
,
2096 if (!rename_script
) {
2097 return NT_STATUS_NO_MEMORY
;
2099 rename_script
= realloc_string_sub2(rename_script
,
2104 rc
= smbrun(rename_script
, NULL
, NULL
);
2106 DEBUG(rc
? 0 : 3,("Running the command `%s' gave %d\n",
2107 rename_script
, rc
));
2109 TALLOC_FREE(rename_script
);
2112 smb_nscd_flush_user_cache();
2116 return NT_STATUS_UNSUCCESSFUL
;
2118 return NT_STATUS_OK
;
2121 /**********************************************************************
2122 Add struct samu to LDAP.
2123 *********************************************************************/
2125 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, struct samu
* newpwd
)
2127 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2129 LDAPMessage
*result
= NULL
;
2130 LDAPMessage
*entry
= NULL
;
2131 LDAPMod
**mods
= NULL
;
2132 int ldap_op
= LDAP_MOD_REPLACE
;
2133 uint32_t num_result
;
2134 const char **attr_list
;
2135 char *escape_user
= NULL
;
2136 const char *username
= pdb_get_username(newpwd
);
2137 const struct dom_sid
*sid
= pdb_get_user_sid(newpwd
);
2138 char *filter
= NULL
;
2140 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2141 TALLOC_CTX
*ctx
= talloc_init("ldapsam_add_sam_account");
2144 return NT_STATUS_NO_MEMORY
;
2147 if (!username
|| !*username
) {
2148 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2149 status
= NT_STATUS_INVALID_PARAMETER
;
2153 /* free this list after the second search or in case we exit on failure */
2154 attr_list
= get_userattr_list(ctx
, ldap_state
->schema_ver
);
2156 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
2158 if (rc
!= LDAP_SUCCESS
) {
2162 if (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
2164 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2168 ldap_msgfree(result
);
2171 if (pdb_element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
2172 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
2174 if (rc
== LDAP_SUCCESS
) {
2175 if (ldap_count_entries(
2177 ldap_state
->smbldap_state
),
2179 struct dom_sid_buf buf
;
2180 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2181 "already in the base, with samba "
2183 dom_sid_str_buf(sid
, &buf
)));
2186 ldap_msgfree(result
);
2191 /* does the entry already exist but without a samba attributes?
2192 we need to return the samba attributes here */
2194 escape_user
= escape_ldap_string(talloc_tos(), username
);
2195 filter
= talloc_strdup(attr_list
, "(uid=%u)");
2197 status
= NT_STATUS_NO_MEMORY
;
2200 filter
= talloc_all_string_sub(attr_list
, filter
, "%u", escape_user
);
2201 TALLOC_FREE(escape_user
);
2203 status
= NT_STATUS_NO_MEMORY
;
2207 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2208 filter
, attr_list
, &result
);
2209 if ( rc
!= LDAP_SUCCESS
) {
2213 num_result
= ldap_count_entries(
2214 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2216 if (num_result
> 1) {
2217 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2221 /* Check if we need to update an existing entry */
2222 if (num_result
== 1) {
2223 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2224 ldap_op
= LDAP_MOD_REPLACE
;
2225 entry
= ldap_first_entry(
2226 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2227 dn
= smbldap_talloc_dn(
2228 ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
),
2231 status
= NT_STATUS_NO_MEMORY
;
2235 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
2237 struct dom_sid_buf buf
;
2239 /* There might be a SID for this account already - say an idmap entry */
2241 filter
= talloc_asprintf(ctx
,
2242 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2243 get_userattr_key2string(ldap_state
->schema_ver
,
2244 LDAP_ATTR_USER_SID
),
2245 dom_sid_str_buf(sid
, &buf
),
2246 LDAP_OBJ_IDMAP_ENTRY
,
2247 LDAP_OBJ_SID_ENTRY
);
2249 status
= NT_STATUS_NO_MEMORY
;
2253 /* free old result before doing a new search */
2254 if (result
!= NULL
) {
2255 ldap_msgfree(result
);
2258 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
2259 filter
, attr_list
, &result
);
2261 if ( rc
!= LDAP_SUCCESS
) {
2265 num_result
= ldap_count_entries(
2266 smbldap_get_ldap(ldap_state
->smbldap_state
), result
);
2268 if (num_result
> 1) {
2269 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2273 /* Check if we need to update an existing entry */
2274 if (num_result
== 1) {
2276 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2277 ldap_op
= LDAP_MOD_REPLACE
;
2278 entry
= ldap_first_entry (
2279 smbldap_get_ldap(ldap_state
->smbldap_state
),
2281 dn
= smbldap_talloc_dn (
2283 smbldap_get_ldap(ldap_state
->smbldap_state
),
2286 status
= NT_STATUS_NO_MEMORY
;
2292 if (num_result
== 0) {
2293 char *escape_username
;
2294 /* Check if we need to add an entry */
2295 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2296 ldap_op
= LDAP_MOD_ADD
;
2298 escape_username
= escape_rdn_val_string_alloc(username
);
2299 if (!escape_username
) {
2300 status
= NT_STATUS_NO_MEMORY
;
2304 if (username
[strlen(username
)-1] == '$') {
2305 dn
= talloc_asprintf(ctx
,
2308 lp_ldap_machine_suffix(talloc_tos()));
2310 dn
= talloc_asprintf(ctx
,
2313 lp_ldap_user_suffix(talloc_tos()));
2316 SAFE_FREE(escape_username
);
2318 status
= NT_STATUS_NO_MEMORY
;
2323 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
2324 pdb_element_is_set_or_changed
)) {
2325 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2327 ldap_mods_free(mods
, true);
2333 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
2336 switch ( ldap_state
->schema_ver
) {
2337 case SCHEMAVER_SAMBASAMACCOUNT
:
2338 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
2341 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2345 status
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, pdb_element_is_set_or_changed
);
2346 if (!NT_STATUS_IS_OK(status
)) {
2347 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2348 pdb_get_username(newpwd
),dn
));
2349 ldap_mods_free(mods
, true);
2353 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
2354 ldap_mods_free(mods
, true);
2356 status
= NT_STATUS_OK
;
2362 ldap_msgfree(result
);
2367 /**********************************************************************
2368 *********************************************************************/
2370 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
2372 LDAPMessage
** result
)
2374 int scope
= LDAP_SCOPE_SUBTREE
;
2376 const char **attr_list
;
2378 attr_list
= get_attr_list(NULL
, groupmap_attr_list
);
2379 rc
= smbldap_search(ldap_state
->smbldap_state
,
2380 lp_ldap_suffix(), scope
,
2381 filter
, attr_list
, 0, result
);
2382 TALLOC_FREE(attr_list
);
2387 /**********************************************************************
2388 *********************************************************************/
2390 static bool init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
2391 GROUP_MAP
*map
, LDAPMessage
*entry
)
2394 TALLOC_CTX
*ctx
= talloc_init("init_group_from_ldap");
2396 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
2397 smbldap_get_ldap(ldap_state
->smbldap_state
) == NULL
) {
2398 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2403 temp
= smbldap_talloc_single_attribute(
2404 smbldap_get_ldap(ldap_state
->smbldap_state
),
2406 get_attr_key2string(groupmap_attr_list
,
2407 LDAP_ATTR_GIDNUMBER
),
2410 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2411 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
2415 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
2417 map
->gid
= (gid_t
)atol(temp
);
2420 temp
= smbldap_talloc_single_attribute(
2421 smbldap_get_ldap(ldap_state
->smbldap_state
),
2423 get_attr_key2string(groupmap_attr_list
,
2424 LDAP_ATTR_GROUP_SID
),
2427 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2428 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
2433 if (!string_to_sid(&map
->sid
, temp
)) {
2434 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
2440 temp
= smbldap_talloc_single_attribute(
2441 smbldap_get_ldap(ldap_state
->smbldap_state
),
2443 get_attr_key2string(groupmap_attr_list
,
2444 LDAP_ATTR_GROUP_TYPE
),
2447 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2448 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
2452 map
->sid_name_use
= (enum lsa_SidType
)atol(temp
);
2454 if ((map
->sid_name_use
< SID_NAME_USER
) ||
2455 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
2456 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
2462 temp
= smbldap_talloc_single_attribute(
2463 smbldap_get_ldap(ldap_state
->smbldap_state
),
2465 get_attr_key2string(groupmap_attr_list
,
2466 LDAP_ATTR_DISPLAY_NAME
),
2469 temp
= smbldap_talloc_single_attribute(
2470 smbldap_get_ldap(ldap_state
->smbldap_state
),
2472 get_attr_key2string(groupmap_attr_list
,
2476 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2477 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
2482 map
->nt_name
= talloc_strdup(map
, temp
);
2483 if (!map
->nt_name
) {
2489 temp
= smbldap_talloc_single_attribute(
2490 smbldap_get_ldap(ldap_state
->smbldap_state
),
2492 get_attr_key2string(groupmap_attr_list
,
2496 temp
= talloc_strdup(ctx
, "");
2502 map
->comment
= talloc_strdup(map
, temp
);
2503 if (!map
->comment
) {
2508 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2511 id
.type
= ID_TYPE_GID
;
2513 idmap_cache_set_sid2unixid(&map
->sid
, &id
);
2520 /**********************************************************************
2521 *********************************************************************/
2523 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
2527 struct ldapsam_privates
*ldap_state
=
2528 (struct ldapsam_privates
*)methods
->private_data
;
2529 LDAPMessage
*result
= NULL
;
2530 LDAPMessage
*entry
= NULL
;
2533 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
2535 return NT_STATUS_NO_SUCH_GROUP
;
2538 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2541 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2543 ldap_msgfree(result
);
2544 return NT_STATUS_NO_SUCH_GROUP
;
2548 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2549 "count=%d\n", filter
, count
));
2550 ldap_msgfree(result
);
2551 return NT_STATUS_NO_SUCH_GROUP
;
2554 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2557 ldap_msgfree(result
);
2558 return NT_STATUS_UNSUCCESSFUL
;
2561 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
2562 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2563 "group filter %s\n", filter
));
2564 ldap_msgfree(result
);
2565 return NT_STATUS_NO_SUCH_GROUP
;
2568 ldap_msgfree(result
);
2569 return NT_STATUS_OK
;
2572 /**********************************************************************
2573 *********************************************************************/
2575 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2578 char *filter
= NULL
;
2580 struct dom_sid_buf tmp
;
2582 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%s))",
2584 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
2585 dom_sid_str_buf(&sid
, &tmp
)) < 0) {
2586 return NT_STATUS_NO_MEMORY
;
2589 status
= ldapsam_getgroup(methods
, filter
, map
);
2594 /**********************************************************************
2595 *********************************************************************/
2597 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2600 char *filter
= NULL
;
2603 if (asprintf(&filter
, "(&(objectClass=%s)(%s=%lu))",
2605 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
2606 (unsigned long)gid
) < 0) {
2607 return NT_STATUS_NO_MEMORY
;
2610 status
= ldapsam_getgroup(methods
, filter
, map
);
2615 /**********************************************************************
2616 *********************************************************************/
2618 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
2621 char *filter
= NULL
;
2622 char *escape_name
= escape_ldap_string(talloc_tos(), name
);
2626 return NT_STATUS_NO_MEMORY
;
2629 if (asprintf(&filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2631 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
2632 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
),
2634 TALLOC_FREE(escape_name
);
2635 return NT_STATUS_NO_MEMORY
;
2638 TALLOC_FREE(escape_name
);
2639 status
= ldapsam_getgroup(methods
, filter
, map
);
2644 static bool ldapsam_extract_rid_from_entry(LDAP
*ldap_struct
,
2646 const struct dom_sid
*domain_sid
,
2652 if (!smbldap_get_single_attribute(ldap_struct
, entry
, "sambaSID",
2653 str
, sizeof(str
)-1)) {
2654 DEBUG(10, ("Could not find sambaSID attribute\n"));
2658 if (!string_to_sid(&sid
, str
)) {
2659 DEBUG(10, ("Could not convert string %s to sid\n", str
));
2663 if (dom_sid_compare_domain(&sid
, domain_sid
) != 0) {
2664 struct dom_sid_buf buf
;
2665 DEBUG(10, ("SID %s is not in expected domain %s\n",
2667 dom_sid_str_buf(domain_sid
, &buf
)));
2671 if (!sid_peek_rid(&sid
, rid
)) {
2672 DEBUG(10, ("Could not peek into RID\n"));
2679 static NTSTATUS
ldapsam_enum_group_members(struct pdb_methods
*methods
,
2680 TALLOC_CTX
*mem_ctx
,
2681 const struct dom_sid
*group
,
2682 uint32_t **pp_member_rids
,
2683 size_t *p_num_members
)
2685 struct ldapsam_privates
*ldap_state
=
2686 (struct ldapsam_privates
*)methods
->private_data
;
2687 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2688 const char *id_attrs
[] = { "memberUid", "gidNumber", NULL
};
2689 const char *sid_attrs
[] = { "sambaSID", NULL
};
2690 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2691 LDAPMessage
*result
= NULL
;
2694 char **values
= NULL
;
2698 struct dom_sid_buf buf
;
2700 *pp_member_rids
= NULL
;
2703 filter
= talloc_asprintf(mem_ctx
,
2704 "(&(objectClass=%s)"
2707 LDAP_OBJ_POSIXGROUP
,
2709 dom_sid_str_buf(group
, &buf
));
2710 if (filter
== NULL
) {
2711 ret
= NT_STATUS_NO_MEMORY
;
2715 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2716 LDAP_SCOPE_SUBTREE
, filter
, id_attrs
, 0,
2719 if (rc
!= LDAP_SUCCESS
)
2722 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2724 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2727 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2728 dom_sid_str_buf(group
, &buf
)));
2729 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2734 ret
= NT_STATUS_NO_SUCH_GROUP
;
2738 entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2742 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2744 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2745 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2749 values
= ldap_get_values(smbldap_get_ldap(conn
), entry
, "memberUid");
2751 if ((values
!= NULL
) && (values
[0] != NULL
)) {
2753 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT
);
2754 if (filter
== NULL
) {
2755 ret
= NT_STATUS_NO_MEMORY
;
2759 for (memberuid
= values
; *memberuid
!= NULL
; memberuid
+= 1) {
2760 char *escape_memberuid
;
2762 escape_memberuid
= escape_ldap_string(talloc_tos(),
2764 if (escape_memberuid
== NULL
) {
2765 ret
= NT_STATUS_NO_MEMORY
;
2769 filter
= talloc_asprintf_append_buffer(filter
, "(uid=%s)", escape_memberuid
);
2770 TALLOC_FREE(escape_memberuid
);
2771 if (filter
== NULL
) {
2772 ret
= NT_STATUS_NO_MEMORY
;
2777 filter
= talloc_asprintf_append_buffer(filter
, "))");
2778 if (filter
== NULL
) {
2779 ret
= NT_STATUS_NO_MEMORY
;
2783 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2784 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2787 if (rc
!= LDAP_SUCCESS
)
2790 count
= ldap_count_entries(smbldap_get_ldap(conn
), result
);
2791 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count
));
2793 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2795 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2797 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2803 sidstr
= smbldap_talloc_single_attribute(
2804 smbldap_get_ldap(conn
), entry
, "sambaSID",
2807 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2808 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2809 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2813 if (!string_to_sid(&sid
, sidstr
))
2816 if (!sid_check_is_in_our_sam(&sid
)) {
2817 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2818 "in our domain\n"));
2819 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2823 sid_peek_rid(&sid
, &rid
);
2825 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2827 ret
= NT_STATUS_NO_MEMORY
;
2833 filter
= talloc_asprintf(mem_ctx
,
2834 "(&(objectClass=%s)"
2836 LDAP_OBJ_SAMBASAMACCOUNT
,
2839 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2840 LDAP_SCOPE_SUBTREE
, filter
, sid_attrs
, 0,
2843 if (rc
!= LDAP_SUCCESS
)
2846 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2848 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
2850 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
2854 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn
),
2856 get_global_sam_sid(),
2858 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT
));
2859 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2863 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_member_rids
,
2865 ret
= NT_STATUS_NO_MEMORY
;
2875 ldap_value_free(values
);
2880 static NTSTATUS
ldapsam_enum_group_memberships(struct pdb_methods
*methods
,
2881 TALLOC_CTX
*mem_ctx
,
2883 struct dom_sid
**pp_sids
,
2885 uint32_t *p_num_groups
)
2887 struct ldapsam_privates
*ldap_state
=
2888 (struct ldapsam_privates
*)methods
->private_data
;
2889 struct smbldap_state
*conn
= ldap_state
->smbldap_state
;
2891 const char *attrs
[] = { "gidNumber", "sambaSID", NULL
};
2894 LDAPMessage
*result
= NULL
;
2896 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2900 gid_t primary_gid
= -1;
2906 if (pdb_get_username(user
) == NULL
) {
2907 return NT_STATUS_INVALID_PARAMETER
;
2910 escape_name
= escape_ldap_string(talloc_tos(), pdb_get_username(user
));
2911 if (escape_name
== NULL
)
2912 return NT_STATUS_NO_MEMORY
;
2914 if (user
->unix_pw
) {
2915 primary_gid
= user
->unix_pw
->pw_gid
;
2917 /* retrieve the users primary gid */
2918 filter
= talloc_asprintf(mem_ctx
,
2919 "(&(objectClass=%s)(uid=%s))",
2920 LDAP_OBJ_SAMBASAMACCOUNT
,
2922 if (filter
== NULL
) {
2923 ret
= NT_STATUS_NO_MEMORY
;
2927 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2928 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2930 if (rc
!= LDAP_SUCCESS
)
2933 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2935 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
2939 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user
)));
2940 ret
= NT_STATUS_NO_SUCH_USER
;
2943 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
2945 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", mem_ctx
);
2947 DEBUG (1, ("Unable to find the member's gid!\n"));
2948 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2951 primary_gid
= smb_strtoul(gidstr
,
2957 DBG_ERR("Failed to convert GID\n");
2962 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2963 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2968 filter
= talloc_asprintf(mem_ctx
,
2969 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2970 LDAP_OBJ_POSIXGROUP
, escape_name
, (unsigned int)primary_gid
);
2971 if (filter
== NULL
) {
2972 ret
= NT_STATUS_NO_MEMORY
;
2976 rc
= smbldap_search(conn
, lp_ldap_suffix(),
2977 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
2979 if (rc
!= LDAP_SUCCESS
)
2982 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
2990 /* We need to add the primary group as the first gid/sid */
2992 if (!add_gid_to_array_unique(mem_ctx
, primary_gid
, pp_gids
, &num_gids
)) {
2993 ret
= NT_STATUS_NO_MEMORY
;
2997 /* This sid will be replaced later */
2999 ret
= add_sid_to_array_unique(mem_ctx
, &global_sid_NULL
, pp_sids
,
3001 if (!NT_STATUS_IS_OK(ret
)) {
3005 for (entry
= ldap_first_entry(smbldap_get_ldap(conn
), result
);
3007 entry
= ldap_next_entry(smbldap_get_ldap(conn
), entry
))
3013 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3015 str
, sizeof(str
)-1))
3018 if (!string_to_sid(&sid
, str
))
3021 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn
),
3023 str
, sizeof(str
)-1))
3026 gid
= smb_strtoul(str
, NULL
, 10, &error
, SMB_STR_FULL_STR_CONV
);
3032 if (gid
== primary_gid
) {
3033 sid_copy(&(*pp_sids
)[0], &sid
);
3035 if (!add_gid_to_array_unique(mem_ctx
, gid
, pp_gids
,
3037 ret
= NT_STATUS_NO_MEMORY
;
3040 ret
= add_sid_to_array_unique(mem_ctx
, &sid
, pp_sids
,
3042 if (!NT_STATUS_IS_OK(ret
)) {
3048 if (dom_sid_compare(&global_sid_NULL
, &(*pp_sids
)[0]) == 0) {
3049 DEBUG(3, ("primary group of [%s] not found\n",
3050 pdb_get_username(user
)));
3051 ret
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
3055 *p_num_groups
= num_sids
;
3061 TALLOC_FREE(escape_name
);
3065 /**********************************************************************
3066 * Augment a posixGroup object with a sambaGroupMapping domgroup
3067 *********************************************************************/
3069 static NTSTATUS
ldapsam_map_posixgroup(TALLOC_CTX
*mem_ctx
,
3070 struct ldapsam_privates
*ldap_state
,
3073 const char *filter
, *dn
;
3074 LDAPMessage
*msg
, *entry
;
3076 struct dom_sid_buf buf
;
3079 filter
= talloc_asprintf(mem_ctx
,
3080 "(&(objectClass=%s)(gidNumber=%u))",
3081 LDAP_OBJ_POSIXGROUP
, (unsigned int)map
->gid
);
3082 if (filter
== NULL
) {
3083 return NT_STATUS_NO_MEMORY
;
3086 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3087 get_attr_list(mem_ctx
, groupmap_attr_list
),
3089 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3091 if ((rc
!= LDAP_SUCCESS
) ||
3092 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3094 ((entry
= ldap_first_entry(
3095 smbldap_get_ldap(ldap_state
->smbldap_state
),
3097 return NT_STATUS_NO_SUCH_GROUP
;
3100 dn
= smbldap_talloc_dn(mem_ctx
,
3101 smbldap_get_ldap(ldap_state
->smbldap_state
),
3104 return NT_STATUS_NO_MEMORY
;
3108 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass",
3110 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3112 dom_sid_str_buf(&map
->sid
, &buf
));
3113 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3114 &mods
, "sambaGroupType",
3115 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3116 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3117 &mods
, "displayName",
3119 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3120 &mods
, "description",
3122 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3124 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3125 if (rc
!= LDAP_SUCCESS
) {
3126 return NT_STATUS_ACCESS_DENIED
;
3129 return NT_STATUS_OK
;
3132 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
3135 struct ldapsam_privates
*ldap_state
=
3136 (struct ldapsam_privates
*)methods
->private_data
;
3137 LDAPMessage
*msg
= NULL
;
3138 LDAPMod
**mods
= NULL
;
3139 const char *attrs
[] = { NULL
};
3143 TALLOC_CTX
*mem_ctx
;
3147 struct dom_sid_buf buf
;
3152 mem_ctx
= talloc_new(NULL
);
3153 if (mem_ctx
== NULL
) {
3154 DEBUG(0, ("talloc_new failed\n"));
3155 return NT_STATUS_NO_MEMORY
;
3158 filter
= talloc_asprintf(mem_ctx
, "(sambaSid=%s)",
3159 dom_sid_str_buf(&map
->sid
, &buf
));
3160 if (filter
== NULL
) {
3161 result
= NT_STATUS_NO_MEMORY
;
3165 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3166 LDAP_SCOPE_SUBTREE
, filter
, attrs
, True
, &msg
);
3167 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3169 if ((rc
== LDAP_SUCCESS
) &&
3170 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3173 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3174 "group mapping entry\n",
3175 dom_sid_str_buf(&map
->sid
, &buf
)));
3176 result
= NT_STATUS_GROUP_EXISTS
;
3180 switch (map
->sid_name_use
) {
3182 case SID_NAME_DOM_GRP
:
3183 /* To map a domain group we need to have a posix group
3185 result
= ldapsam_map_posixgroup(mem_ctx
, ldap_state
, map
);
3189 case SID_NAME_ALIAS
:
3190 if (!sid_check_is_in_our_sam(&map
->sid
)
3191 && !sid_check_is_in_builtin(&map
->sid
) )
3193 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3194 dom_sid_str_buf(&map
->sid
, &buf
)));
3195 result
= NT_STATUS_INVALID_PARAMETER
;
3201 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3202 sid_type_lookup(map
->sid_name_use
)));
3203 result
= NT_STATUS_INVALID_PARAMETER
;
3207 /* Domain groups have been mapped in a separate routine, we have to
3208 * create an alias now */
3210 if (map
->gid
== -1) {
3211 DEBUG(10, ("Refusing to map gid==-1\n"));
3212 result
= NT_STATUS_INVALID_PARAMETER
;
3217 id
.type
= ID_TYPE_GID
;
3219 if (pdb_id_to_sid(&id
, &sid
)) {
3220 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3222 (unsigned int)map
->gid
,
3223 dom_sid_str_buf(&sid
, &buf
)));
3224 result
= NT_STATUS_GROUP_EXISTS
;
3228 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3229 * the best we can get out of LDAP. */
3231 dn
= talloc_asprintf(mem_ctx
, "sambaSid=%s,%s",
3232 dom_sid_str_buf(&map
->sid
, &buf
),
3233 lp_ldap_group_suffix(talloc_tos()));
3235 result
= NT_STATUS_NO_MEMORY
;
3241 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3242 &mods
, "objectClass", LDAP_OBJ_SID_ENTRY
);
3243 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3244 &mods
, "objectClass", LDAP_OBJ_GROUPMAP
);
3245 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3247 dom_sid_str_buf(&map
->sid
, &buf
));
3248 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3249 &mods
, "sambaGroupType",
3250 talloc_asprintf(mem_ctx
, "%d", map
->sid_name_use
));
3251 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3252 &mods
, "displayName",
3254 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3255 &mods
, "description",
3257 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), NULL
,
3259 talloc_asprintf(mem_ctx
, "%u",
3260 (unsigned int)map
->gid
));
3261 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3263 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
3265 result
= (rc
== LDAP_SUCCESS
) ?
3266 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
3269 TALLOC_FREE(mem_ctx
);
3273 /**********************************************************************
3274 * Update a group mapping entry. We're quite strict about what can be changed:
3275 * Only the description and displayname may be changed. It simply does not
3276 * make any sense to change the SID, gid or the type in a mapping.
3277 *********************************************************************/
3279 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
3282 struct ldapsam_privates
*ldap_state
=
3283 (struct ldapsam_privates
*)methods
->private_data
;
3285 const char *filter
, *dn
;
3286 LDAPMessage
*msg
= NULL
;
3287 LDAPMessage
*entry
= NULL
;
3288 LDAPMod
**mods
= NULL
;
3289 TALLOC_CTX
*mem_ctx
;
3291 struct dom_sid_buf buf
;
3293 mem_ctx
= talloc_new(NULL
);
3294 if (mem_ctx
== NULL
) {
3295 DEBUG(0, ("talloc_new failed\n"));
3296 return NT_STATUS_NO_MEMORY
;
3299 /* Make 100% sure that sid, gid and type are not changed by looking up
3300 * exactly the values we're given in LDAP. */
3302 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)"
3303 "(sambaSid=%s)(gidNumber=%u)"
3304 "(sambaGroupType=%d))",
3306 dom_sid_str_buf(&map
->sid
, &buf
),
3307 (unsigned int)map
->gid
, map
->sid_name_use
);
3308 if (filter
== NULL
) {
3309 result
= NT_STATUS_NO_MEMORY
;
3313 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
3314 get_attr_list(mem_ctx
, groupmap_attr_list
),
3316 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3318 if ((rc
!= LDAP_SUCCESS
) ||
3319 (ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3321 ((entry
= ldap_first_entry(
3322 smbldap_get_ldap(ldap_state
->smbldap_state
),
3324 result
= NT_STATUS_NO_SUCH_GROUP
;
3328 dn
= smbldap_talloc_dn(
3329 mem_ctx
, smbldap_get_ldap(ldap_state
->smbldap_state
), entry
);
3332 result
= NT_STATUS_NO_MEMORY
;
3337 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3338 &mods
, "displayName", map
->nt_name
);
3339 smbldap_make_mod(smbldap_get_ldap(ldap_state
->smbldap_state
), entry
,
3340 &mods
, "description", map
->comment
);
3341 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
3344 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3345 "nothing to do\n"));
3346 result
= NT_STATUS_OK
;
3350 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3352 if (rc
!= LDAP_SUCCESS
) {
3353 result
= NT_STATUS_ACCESS_DENIED
;
3357 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3358 "group %lu in LDAP\n", (unsigned long)map
->gid
));
3360 result
= NT_STATUS_OK
;
3363 TALLOC_FREE(mem_ctx
);
3367 /**********************************************************************
3368 *********************************************************************/
3370 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
3373 struct ldapsam_privates
*priv
=
3374 (struct ldapsam_privates
*)methods
->private_data
;
3375 LDAPMessage
*msg
, *entry
;
3378 TALLOC_CTX
*mem_ctx
;
3380 struct dom_sid_buf buf
;
3382 mem_ctx
= talloc_new(NULL
);
3383 if (mem_ctx
== NULL
) {
3384 DEBUG(0, ("talloc_new failed\n"));
3385 return NT_STATUS_NO_MEMORY
;
3388 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=%s)(%s=%s))",
3389 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
,
3390 dom_sid_str_buf(&sid
, &buf
));
3391 if (filter
== NULL
) {
3392 result
= NT_STATUS_NO_MEMORY
;
3395 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
3396 get_attr_list(mem_ctx
, groupmap_attr_list
),
3398 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
3400 if ((rc
!= LDAP_SUCCESS
) ||
3401 (ldap_count_entries(priv2ld(priv
), msg
) != 1) ||
3402 ((entry
= ldap_first_entry(priv2ld(priv
), msg
)) == NULL
)) {
3403 result
= NT_STATUS_NO_SUCH_GROUP
;
3407 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
, LDAP_OBJ_GROUPMAP
,
3408 get_attr_list(mem_ctx
,
3409 groupmap_attr_list_to_delete
));
3411 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3412 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3413 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3414 const char *attrs
[] = { "sambaGroupType", "description",
3415 "displayName", "sambaSIDList",
3418 /* Second try. Don't delete the sambaSID attribute, this is
3419 for "old" entries that are tacked on a winbind
3422 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3423 LDAP_OBJ_GROUPMAP
, attrs
);
3426 if ((rc
== LDAP_NAMING_VIOLATION
) ||
3427 (rc
== LDAP_NOT_ALLOWED_ON_RDN
) ||
3428 (rc
== LDAP_OBJECT_CLASS_VIOLATION
)) {
3429 const char *attrs
[] = { "sambaGroupType", "description",
3430 "displayName", "sambaSIDList",
3431 "gidNumber", NULL
};
3433 /* Third try. This is a post-3.0.21 alias (containing only
3434 * sambaSidEntry and sambaGroupMapping classes), we also have
3435 * to delete the gidNumber attribute, only the sambaSidEntry
3438 rc
= ldapsam_delete_entry(priv
, mem_ctx
, entry
,
3439 LDAP_OBJ_GROUPMAP
, attrs
);
3442 result
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3445 TALLOC_FREE(mem_ctx
);
3449 /**********************************************************************
3450 *********************************************************************/
3452 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
,
3455 struct ldapsam_privates
*ldap_state
=
3456 (struct ldapsam_privates
*)my_methods
->private_data
;
3457 char *filter
= NULL
;
3459 const char **attr_list
;
3461 filter
= talloc_asprintf(NULL
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
3463 return NT_STATUS_NO_MEMORY
;
3465 attr_list
= get_attr_list( NULL
, groupmap_attr_list
);
3466 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3467 LDAP_SCOPE_SUBTREE
, filter
,
3468 attr_list
, 0, &ldap_state
->result
);
3469 TALLOC_FREE(attr_list
);
3471 if (rc
!= LDAP_SUCCESS
) {
3472 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3473 ldap_err2string(rc
)));
3474 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3475 lp_ldap_suffix(), filter
));
3476 ldap_msgfree(ldap_state
->result
);
3477 ldap_state
->result
= NULL
;
3478 TALLOC_FREE(filter
);
3479 return NT_STATUS_UNSUCCESSFUL
;
3482 TALLOC_FREE(filter
);
3484 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3486 smbldap_get_ldap(ldap_state
->smbldap_state
),
3487 ldap_state
->result
)));
3490 ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3491 ldap_state
->result
);
3492 ldap_state
->index
= 0;
3494 return NT_STATUS_OK
;
3497 /**********************************************************************
3498 *********************************************************************/
3500 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
3502 ldapsam_endsampwent(my_methods
);
3505 /**********************************************************************
3506 *********************************************************************/
3508 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
3511 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
3512 struct ldapsam_privates
*ldap_state
=
3513 (struct ldapsam_privates
*)my_methods
->private_data
;
3517 if (!ldap_state
->entry
)
3520 ldap_state
->index
++;
3521 bret
= init_group_from_ldap(ldap_state
, map
,
3524 ldap_state
->entry
= ldap_next_entry(
3525 smbldap_get_ldap(ldap_state
->smbldap_state
),
3529 return NT_STATUS_OK
;
3532 /**********************************************************************
3533 *********************************************************************/
3535 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
3536 const struct dom_sid
*domsid
, enum lsa_SidType sid_name_use
,
3537 GROUP_MAP
***pp_rmap
,
3538 size_t *p_num_entries
,
3541 GROUP_MAP
*map
= NULL
;
3547 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
3548 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3550 return NT_STATUS_ACCESS_DENIED
;
3555 map
= talloc_zero(NULL
, GROUP_MAP
);
3557 return NT_STATUS_NO_MEMORY
;
3560 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, map
))) {
3565 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
3566 sid_name_use
!= map
->sid_name_use
) {
3567 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3568 "not of the requested type\n",
3572 if (unix_only
== ENUM_ONLY_MAPPED
&& map
->gid
== -1) {
3573 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3574 "non mapped\n", map
->nt_name
));
3578 *pp_rmap
= talloc_realloc(NULL
, *pp_rmap
,
3579 GROUP_MAP
*, entries
+ 1);
3581 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3582 "enlarge group map!\n"));
3583 return NT_STATUS_UNSUCCESSFUL
;
3586 (*pp_rmap
)[entries
] = talloc_move((*pp_rmap
), &map
);
3591 ldapsam_endsamgrent(methods
);
3593 *p_num_entries
= entries
;
3595 return NT_STATUS_OK
;
3598 static NTSTATUS
ldapsam_modify_aliasmem(struct pdb_methods
*methods
,
3599 const struct dom_sid
*alias
,
3600 const struct dom_sid
*member
,
3603 struct ldapsam_privates
*ldap_state
=
3604 (struct ldapsam_privates
*)methods
->private_data
;
3606 LDAPMessage
*result
= NULL
;
3607 LDAPMessage
*entry
= NULL
;
3609 LDAPMod
**mods
= NULL
;
3611 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3612 struct dom_sid_buf tmp
;
3614 char *filter
= NULL
;
3616 if (sid_check_is_in_builtin(alias
)) {
3617 type
= SID_NAME_ALIAS
;
3620 if (sid_check_is_in_our_sam(alias
)) {
3621 type
= SID_NAME_ALIAS
;
3624 if (type
== SID_NAME_USE_NONE
) {
3625 struct dom_sid_buf buf
;
3626 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3627 dom_sid_str_buf(alias
, &buf
)));
3628 return NT_STATUS_NO_SUCH_ALIAS
;
3631 if (asprintf(&filter
,
3632 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3634 dom_sid_str_buf(alias
, &tmp
),
3636 return NT_STATUS_NO_MEMORY
;
3639 if (ldapsam_search_one_group(ldap_state
, filter
,
3640 &result
) != LDAP_SUCCESS
) {
3642 return NT_STATUS_NO_SUCH_ALIAS
;
3645 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3649 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3650 ldap_msgfree(result
);
3652 return NT_STATUS_NO_SUCH_ALIAS
;
3656 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3657 "filter %s: count=%d\n", filter
, count
));
3658 ldap_msgfree(result
);
3660 return NT_STATUS_NO_SUCH_ALIAS
;
3665 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3669 ldap_msgfree(result
);
3670 return NT_STATUS_UNSUCCESSFUL
;
3673 dn
= smbldap_talloc_dn(talloc_tos(),
3674 smbldap_get_ldap(ldap_state
->smbldap_state
),
3677 ldap_msgfree(result
);
3678 return NT_STATUS_UNSUCCESSFUL
;
3681 smbldap_set_mod(&mods
, modop
,
3682 get_attr_key2string(groupmap_attr_list
,
3683 LDAP_ATTR_SID_LIST
),
3684 dom_sid_str_buf(member
, &tmp
));
3686 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
3688 ldap_mods_free(mods
, True
);
3689 ldap_msgfree(result
);
3692 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
) {
3693 return NT_STATUS_MEMBER_IN_ALIAS
;
3696 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
) {
3697 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
3700 if (rc
!= LDAP_SUCCESS
) {
3701 return NT_STATUS_UNSUCCESSFUL
;
3704 return NT_STATUS_OK
;
3707 static NTSTATUS
ldapsam_add_aliasmem(struct pdb_methods
*methods
,
3708 const struct dom_sid
*alias
,
3709 const struct dom_sid
*member
)
3711 return ldapsam_modify_aliasmem(methods
, alias
, member
, LDAP_MOD_ADD
);
3714 static NTSTATUS
ldapsam_del_aliasmem(struct pdb_methods
*methods
,
3715 const struct dom_sid
*alias
,
3716 const struct dom_sid
*member
)
3718 return ldapsam_modify_aliasmem(methods
, alias
, member
,
3722 static NTSTATUS
ldapsam_enum_aliasmem(struct pdb_methods
*methods
,
3723 const struct dom_sid
*alias
,
3724 TALLOC_CTX
*mem_ctx
,
3725 struct dom_sid
**pp_members
,
3726 size_t *p_num_members
)
3728 struct ldapsam_privates
*ldap_state
=
3729 (struct ldapsam_privates
*)methods
->private_data
;
3730 LDAPMessage
*result
= NULL
;
3731 LDAPMessage
*entry
= NULL
;
3733 char **values
= NULL
;
3735 char *filter
= NULL
;
3736 uint32_t num_members
= 0;
3737 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3738 struct dom_sid_buf tmp
;
3743 if (sid_check_is_in_builtin(alias
)) {
3744 type
= SID_NAME_ALIAS
;
3747 if (sid_check_is_in_our_sam(alias
)) {
3748 type
= SID_NAME_ALIAS
;
3751 if (type
== SID_NAME_USE_NONE
) {
3752 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3753 dom_sid_str_buf(alias
, &tmp
)));
3754 return NT_STATUS_NO_SUCH_ALIAS
;
3757 if (asprintf(&filter
,
3758 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3760 dom_sid_str_buf(alias
, &tmp
),
3762 return NT_STATUS_NO_MEMORY
;
3765 if (ldapsam_search_one_group(ldap_state
, filter
,
3766 &result
) != LDAP_SUCCESS
) {
3768 return NT_STATUS_NO_SUCH_ALIAS
;
3771 count
= ldap_count_entries(smbldap_get_ldap(ldap_state
->smbldap_state
),
3775 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3776 ldap_msgfree(result
);
3778 return NT_STATUS_NO_SUCH_ALIAS
;
3782 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3783 "filter %s: count=%d\n", filter
, count
));
3784 ldap_msgfree(result
);
3786 return NT_STATUS_NO_SUCH_ALIAS
;
3791 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
3795 ldap_msgfree(result
);
3796 return NT_STATUS_UNSUCCESSFUL
;
3799 values
= ldap_get_values(smbldap_get_ldap(ldap_state
->smbldap_state
),
3801 get_attr_key2string(groupmap_attr_list
,
3802 LDAP_ATTR_SID_LIST
));
3804 if (values
== NULL
) {
3805 ldap_msgfree(result
);
3806 return NT_STATUS_OK
;
3809 count
= ldap_count_values(values
);
3811 for (i
=0; i
<count
; i
++) {
3812 struct dom_sid member
;
3815 if (!string_to_sid(&member
, values
[i
]))
3818 status
= add_sid_to_array(mem_ctx
, &member
, pp_members
,
3820 if (!NT_STATUS_IS_OK(status
)) {
3821 ldap_value_free(values
);
3822 ldap_msgfree(result
);
3827 *p_num_members
= num_members
;
3828 ldap_value_free(values
);
3829 ldap_msgfree(result
);
3831 return NT_STATUS_OK
;
3834 static NTSTATUS
ldapsam_alias_memberships(struct pdb_methods
*methods
,
3835 TALLOC_CTX
*mem_ctx
,
3836 const struct dom_sid
*domain_sid
,
3837 const struct dom_sid
*members
,
3839 uint32_t **pp_alias_rids
,
3840 size_t *p_num_alias_rids
)
3842 struct ldapsam_privates
*ldap_state
=
3843 (struct ldapsam_privates
*)methods
->private_data
;
3846 const char *attrs
[] = { LDAP_ATTRIBUTE_SID
, NULL
};
3848 LDAPMessage
*result
= NULL
;
3849 LDAPMessage
*entry
= NULL
;
3853 enum lsa_SidType type
= SID_NAME_USE_NONE
;
3854 bool is_builtin
= false;
3855 bool sid_added
= false;
3857 *pp_alias_rids
= NULL
;
3858 *p_num_alias_rids
= 0;
3860 if (sid_check_is_builtin(domain_sid
)) {
3862 type
= SID_NAME_ALIAS
;
3865 if (sid_check_is_our_sam(domain_sid
)) {
3866 type
= SID_NAME_ALIAS
;
3869 if (type
== SID_NAME_USE_NONE
) {
3870 struct dom_sid_buf buf
;
3871 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3872 dom_sid_str_buf(domain_sid
, &buf
)));
3873 return NT_STATUS_UNSUCCESSFUL
;
3876 if (num_members
== 0) {
3877 return NT_STATUS_OK
;
3880 filter
= talloc_asprintf(mem_ctx
,
3881 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3882 LDAP_OBJ_GROUPMAP
, type
);
3884 for (i
=0; i
<num_members
; i
++) {
3885 struct dom_sid_buf buf
;
3886 filter
= talloc_asprintf(mem_ctx
, "%s(sambaSIDList=%s)",
3888 dom_sid_str_buf(&members
[i
], &buf
));
3891 filter
= talloc_asprintf(mem_ctx
, "%s))", filter
);
3893 if (filter
== NULL
) {
3894 return NT_STATUS_NO_MEMORY
;
3898 ldap_state
->search_cache
.filter
&&
3899 strcmp(ldap_state
->search_cache
.filter
, filter
) == 0) {
3900 filter
= talloc_move(filter
, &ldap_state
->search_cache
.filter
);
3901 result
= ldap_state
->search_cache
.result
;
3902 ldap_state
->search_cache
.result
= NULL
;
3904 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_suffix(),
3905 LDAP_SCOPE_SUBTREE
, filter
, attrs
, 0, &result
);
3906 if (rc
!= LDAP_SUCCESS
) {
3907 return NT_STATUS_UNSUCCESSFUL
;
3909 smbldap_talloc_autofree_ldapmsg(filter
, result
);
3912 ldap_struct
= smbldap_get_ldap(ldap_state
->smbldap_state
);
3914 for (entry
= ldap_first_entry(ldap_struct
, result
);
3916 entry
= ldap_next_entry(ldap_struct
, entry
))
3922 if (!smbldap_get_single_attribute(ldap_struct
, entry
,
3928 if (!string_to_sid(&sid
, sid_str
))
3931 if (!sid_peek_check_rid(domain_sid
, &sid
, &rid
))
3936 if (!add_rid_to_array_unique(mem_ctx
, rid
, pp_alias_rids
,
3937 p_num_alias_rids
)) {
3938 return NT_STATUS_NO_MEMORY
;
3942 if (!is_builtin
&& !sid_added
) {
3943 TALLOC_FREE(ldap_state
->search_cache
.filter
);
3945 * Note: result is a talloc child of filter because of the
3946 * smbldap_talloc_autofree_ldapmsg() usage
3948 ldap_state
->search_cache
.filter
= talloc_move(ldap_state
, &filter
);
3949 ldap_state
->search_cache
.result
= result
;
3952 return NT_STATUS_OK
;
3955 static NTSTATUS
ldapsam_set_account_policy_in_ldap(struct pdb_methods
*methods
,
3956 enum pdb_policy_type type
,
3959 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
3961 LDAPMod
**mods
= NULL
;
3962 fstring value_string
;
3963 const char *policy_attr
= NULL
;
3965 struct ldapsam_privates
*ldap_state
=
3966 (struct ldapsam_privates
*)methods
->private_data
;
3968 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3970 if (!ldap_state
->domain_dn
) {
3971 return NT_STATUS_INVALID_PARAMETER
;
3974 policy_attr
= get_account_policy_attr(type
);
3975 if (policy_attr
== NULL
) {
3976 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3981 slprintf(value_string
, sizeof(value_string
) - 1, "%i", value
);
3983 smbldap_set_mod(&mods
, LDAP_MOD_REPLACE
, policy_attr
, value_string
);
3985 rc
= smbldap_modify(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
3988 ldap_mods_free(mods
, True
);
3990 if (rc
!= LDAP_SUCCESS
) {
3994 if (!cache_account_policy_set(type
, value
)) {
3995 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3996 "update local tdb cache\n"));
4000 return NT_STATUS_OK
;
4003 static NTSTATUS
ldapsam_set_account_policy(struct pdb_methods
*methods
,
4004 enum pdb_policy_type type
,
4007 return ldapsam_set_account_policy_in_ldap(methods
, type
,
4011 static NTSTATUS
ldapsam_get_account_policy_from_ldap(struct pdb_methods
*methods
,
4012 enum pdb_policy_type type
,
4015 NTSTATUS ntstatus
= NT_STATUS_UNSUCCESSFUL
;
4016 LDAPMessage
*result
= NULL
;
4017 LDAPMessage
*entry
= NULL
;
4022 const char *policy_attr
= NULL
;
4024 struct ldapsam_privates
*ldap_state
=
4025 (struct ldapsam_privates
*)methods
->private_data
;
4027 const char *attrs
[2];
4029 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4031 if (!ldap_state
->domain_dn
) {
4032 return NT_STATUS_INVALID_PARAMETER
;
4035 policy_attr
= get_account_policy_attr(type
);
4037 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4038 "policy index: %d\n", type
));
4042 attrs
[0] = policy_attr
;
4045 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO
);
4046 if (filter
== NULL
) {
4047 return NT_STATUS_NO_MEMORY
;
4049 rc
= smbldap_search(ldap_state
->smbldap_state
, ldap_state
->domain_dn
,
4050 LDAP_SCOPE_BASE
, filter
, attrs
, 0,
4052 TALLOC_FREE(filter
);
4053 if (rc
!= LDAP_SUCCESS
) {
4057 count
= ldap_count_entries(priv2ld(ldap_state
), result
);
4062 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
4063 if (entry
== NULL
) {
4067 vals
= ldap_get_values(priv2ld(ldap_state
), entry
, policy_attr
);
4072 *value
= (uint32_t)atol(vals
[0]);
4074 ntstatus
= NT_STATUS_OK
;
4078 ldap_value_free(vals
);
4079 ldap_msgfree(result
);
4084 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4086 - if user hasn't decided to use account policies inside LDAP just reuse the
4089 - if there is a valid cache entry, return that
4090 - if there is an LDAP entry, update cache and return
4091 - otherwise set to default, update cache and return
4095 static NTSTATUS
ldapsam_get_account_policy(struct pdb_methods
*methods
,
4096 enum pdb_policy_type type
,
4101 if (cache_account_policy_get(type
, value
)) {
4102 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4104 return NT_STATUS_OK
;
4107 ntstatus
= ldapsam_get_account_policy_from_ldap(methods
, type
,
4109 if (NT_STATUS_IS_OK(ntstatus
)) {
4113 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4117 /* should we automagically migrate old tdb value here ? */
4118 if (account_policy_get(type
, value
))
4121 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4122 "default\n", type
));
4125 if (!account_policy_get_default(type
, value
)) {
4131 ntstatus
= ldapsam_set_account_policy(methods
, type
, *value
);
4132 if (!NT_STATUS_IS_OK(ntstatus
)) {
4138 if (!cache_account_policy_set(type
, *value
)) {
4139 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4140 "tdb as a cache\n"));
4141 return NT_STATUS_UNSUCCESSFUL
;
4144 return NT_STATUS_OK
;
4147 static NTSTATUS
ldapsam_lookup_rids(struct pdb_methods
*methods
,
4148 const struct dom_sid
*domain_sid
,
4152 enum lsa_SidType
*attrs
)
4154 struct ldapsam_privates
*ldap_state
=
4155 (struct ldapsam_privates
*)methods
->private_data
;
4156 LDAPMessage
*msg
= NULL
;
4158 char *allsids
= NULL
;
4159 size_t i
, num_mapped
;
4161 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
4162 TALLOC_CTX
*mem_ctx
;
4166 mem_ctx
= talloc_new(NULL
);
4167 if (mem_ctx
== NULL
) {
4168 DEBUG(0, ("talloc_new failed\n"));
4172 if (!sid_check_is_builtin(domain_sid
) &&
4173 !sid_check_is_our_sam(domain_sid
)) {
4174 result
= NT_STATUS_INVALID_PARAMETER
;
4178 if (num_rids
== 0) {
4179 result
= NT_STATUS_NONE_MAPPED
;
4183 for (i
=0; i
<num_rids
; i
++)
4184 attrs
[i
] = SID_NAME_UNKNOWN
;
4186 allsids
= talloc_strdup(mem_ctx
, "");
4187 if (allsids
== NULL
) {
4191 for (i
=0; i
<num_rids
; i
++) {
4193 struct dom_sid_buf buf
;
4194 sid_compose(&sid
, domain_sid
, rids
[i
]);
4195 allsids
= talloc_asprintf_append_buffer(
4198 dom_sid_str_buf(&sid
, &buf
));
4199 if (allsids
== NULL
) {
4204 /* First look for users */
4208 const char *ldap_attrs
[] = { "uid", "sambaSid", NULL
};
4210 filter
= talloc_asprintf(
4211 mem_ctx
, ("(&(objectClass=%s)(|%s))"),
4212 LDAP_OBJ_SAMBASAMACCOUNT
, allsids
);
4214 if (filter
== NULL
) {
4218 rc
= smbldap_search(ldap_state
->smbldap_state
,
4219 lp_ldap_user_suffix(talloc_tos()),
4220 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4222 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4225 if (rc
!= LDAP_SUCCESS
)
4228 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4231 for (entry
= ldap_first_entry(ld
, msg
);
4233 entry
= ldap_next_entry(ld
, entry
)) {
4238 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4240 DEBUG(2, ("Could not find sid from ldap entry\n"));
4244 name
= smbldap_talloc_single_attribute(ld
, entry
, "uid",
4247 DEBUG(2, ("Could not retrieve uid attribute\n"));
4251 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4252 if (rid
== rids
[rid_index
])
4256 if (rid_index
== num_rids
) {
4257 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4261 attrs
[rid_index
] = SID_NAME_USER
;
4262 names
[rid_index
] = name
;
4266 if (num_mapped
== num_rids
) {
4267 /* No need to look for groups anymore -- we're done */
4268 result
= NT_STATUS_OK
;
4272 /* Same game for groups */
4276 const char *ldap_attrs
[] = { "cn", "displayName", "sambaSid",
4277 "sambaGroupType", NULL
};
4279 filter
= talloc_asprintf(
4280 mem_ctx
, "(&(objectClass=%s)(|%s))",
4281 LDAP_OBJ_GROUPMAP
, allsids
);
4282 if (filter
== NULL
) {
4286 rc
= smbldap_search(ldap_state
->smbldap_state
,
4288 LDAP_SCOPE_SUBTREE
, filter
, ldap_attrs
, 0,
4290 smbldap_talloc_autofree_ldapmsg(mem_ctx
, msg
);
4293 if (rc
!= LDAP_SUCCESS
)
4296 /* ldap_struct might have changed due to a reconnect */
4298 ld
= smbldap_get_ldap(ldap_state
->smbldap_state
);
4300 /* For consistency checks, we already checked we're only domain or builtin */
4302 is_builtin
= sid_check_is_builtin(domain_sid
);
4304 for (entry
= ldap_first_entry(ld
, msg
);
4306 entry
= ldap_next_entry(ld
, entry
))
4311 enum lsa_SidType type
;
4312 const char *dn
= smbldap_talloc_dn(mem_ctx
, ld
, entry
);
4314 attr
= smbldap_talloc_single_attribute(ld
, entry
, "sambaGroupType",
4317 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4322 type
= (enum lsa_SidType
)atol(attr
);
4324 /* Consistency checks */
4325 if ((is_builtin
&& (type
!= SID_NAME_ALIAS
)) ||
4326 (!is_builtin
&& ((type
!= SID_NAME_ALIAS
) &&
4327 (type
!= SID_NAME_DOM_GRP
)))) {
4328 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn
));
4331 if (!ldapsam_extract_rid_from_entry(ld
, entry
, domain_sid
,
4333 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn
));
4337 attr
= smbldap_talloc_single_attribute(ld
, entry
, "displayName", names
);
4340 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4342 attr
= smbldap_talloc_single_attribute(ld
, entry
, "cn", names
);
4346 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4351 for (rid_index
= 0; rid_index
< num_rids
; rid_index
++) {
4352 if (rid
== rids
[rid_index
])
4356 if (rid_index
== num_rids
) {
4357 DEBUG(2, ("Got a RID not asked for: %d\n", rid
));
4361 attrs
[rid_index
] = type
;
4362 names
[rid_index
] = attr
;
4366 result
= NT_STATUS_NONE_MAPPED
;
4369 result
= (num_mapped
== num_rids
) ?
4370 NT_STATUS_OK
: STATUS_SOME_UNMAPPED
;
4372 TALLOC_FREE(mem_ctx
);
4376 static char *get_ldap_filter(TALLOC_CTX
*mem_ctx
, const char *username
)
4378 char *filter
= NULL
;
4379 char *escaped
= NULL
;
4380 char *result
= NULL
;
4382 if (asprintf(&filter
, "(&%s(objectclass=%s))",
4383 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT
) < 0) {
4387 escaped
= escape_ldap_string(talloc_tos(), username
);
4388 if (escaped
== NULL
) goto done
;
4390 result
= talloc_string_sub(mem_ctx
, filter
, "%u", username
);
4394 TALLOC_FREE(escaped
);
4399 static const char **talloc_attrs(TALLOC_CTX
*mem_ctx
, ...)
4403 const char **result
;
4405 va_start(ap
, mem_ctx
);
4406 while (va_arg(ap
, const char *) != NULL
)
4410 if ((result
= talloc_array(mem_ctx
, const char *, num
+1)) == NULL
) {
4414 va_start(ap
, mem_ctx
);
4415 for (i
=0; i
<num
; i
++) {
4416 result
[i
] = talloc_strdup(result
, va_arg(ap
, const char*));
4417 if (result
[i
] == NULL
) {
4418 talloc_free(result
);
4429 struct ldap_search_state
{
4430 struct smbldap_state
*connection
;
4432 uint32_t acct_flags
;
4433 uint16_t group_type
;
4440 void *pagedresults_cookie
;
4442 LDAPMessage
*entries
, *current_entry
;
4443 bool (*ldap2displayentry
)(struct ldap_search_state
*state
,
4444 TALLOC_CTX
*mem_ctx
,
4445 LDAP
*ld
, LDAPMessage
*entry
,
4446 struct samr_displayentry
*result
);
4449 static bool ldapsam_search_firstpage(struct pdb_search
*search
)
4451 struct ldap_search_state
*state
=
4452 (struct ldap_search_state
*)search
->private_data
;
4454 int rc
= LDAP_OPERATIONS_ERROR
;
4456 state
->entries
= NULL
;
4458 if (smbldap_get_paged_results(state
->connection
)) {
4459 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4460 state
->scope
, state
->filter
,
4461 state
->attrs
, state
->attrsonly
,
4462 lp_ldap_page_size(), &state
->entries
,
4463 &state
->pagedresults_cookie
);
4466 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
)) {
4468 if (state
->entries
!= NULL
) {
4469 /* Left over from unsuccessful paged attempt */
4470 ldap_msgfree(state
->entries
);
4471 state
->entries
= NULL
;
4474 rc
= smbldap_search(state
->connection
, state
->base
,
4475 state
->scope
, state
->filter
, state
->attrs
,
4476 state
->attrsonly
, &state
->entries
);
4478 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4481 /* Ok, the server was lying. It told us it could do paged
4482 * searches when it could not. */
4483 smbldap_set_paged_results(state
->connection
, false);
4486 ld
= smbldap_get_ldap(state
->connection
);
4488 DEBUG(5, ("Don't have an LDAP connection right after a "
4492 state
->current_entry
= ldap_first_entry(ld
, state
->entries
);
4497 static bool ldapsam_search_nextpage(struct pdb_search
*search
)
4499 struct ldap_search_state
*state
=
4500 (struct ldap_search_state
*)search
->private_data
;
4503 if (!smbldap_get_paged_results(state
->connection
)) {
4504 /* There is no next page when there are no paged results */
4508 rc
= smbldap_search_paged(state
->connection
, state
->base
,
4509 state
->scope
, state
->filter
, state
->attrs
,
4510 state
->attrsonly
, lp_ldap_page_size(),
4512 &state
->pagedresults_cookie
);
4514 if ((rc
!= LDAP_SUCCESS
) || (state
->entries
== NULL
))
4517 state
->current_entry
= ldap_first_entry(
4518 smbldap_get_ldap(state
->connection
), state
->entries
);
4520 if (state
->current_entry
== NULL
) {
4521 ldap_msgfree(state
->entries
);
4522 state
->entries
= NULL
;
4529 static bool ldapsam_search_next_entry(struct pdb_search
*search
,
4530 struct samr_displayentry
*entry
)
4532 struct ldap_search_state
*state
=
4533 (struct ldap_search_state
*)search
->private_data
;
4537 if ((state
->entries
== NULL
) && (state
->pagedresults_cookie
== NULL
))
4540 if ((state
->entries
== NULL
) &&
4541 !ldapsam_search_nextpage(search
))
4544 if (state
->current_entry
== NULL
) {
4548 result
= state
->ldap2displayentry(state
, search
,
4549 smbldap_get_ldap(state
->connection
),
4550 state
->current_entry
, entry
);
4554 dn
= ldap_get_dn(smbldap_get_ldap(state
->connection
),
4555 state
->current_entry
);
4556 DEBUG(5, ("Skipping entry %s\n", dn
!= NULL
? dn
: "<NULL>"));
4557 if (dn
!= NULL
) ldap_memfree(dn
);
4560 state
->current_entry
= ldap_next_entry(
4561 smbldap_get_ldap(state
->connection
), state
->current_entry
);
4563 if (state
->current_entry
== NULL
) {
4564 ldap_msgfree(state
->entries
);
4565 state
->entries
= NULL
;
4568 if (!result
) goto retry
;
4573 static void ldapsam_search_end(struct pdb_search
*search
)
4575 struct ldap_search_state
*state
=
4576 (struct ldap_search_state
*)search
->private_data
;
4579 if (state
->pagedresults_cookie
== NULL
)
4582 if (state
->entries
!= NULL
)
4583 ldap_msgfree(state
->entries
);
4585 state
->entries
= NULL
;
4586 state
->current_entry
= NULL
;
4588 if (!smbldap_get_paged_results(state
->connection
)) {
4592 /* Tell the LDAP server we're not interested in the rest anymore. */
4594 rc
= smbldap_search_paged(state
->connection
, state
->base
, state
->scope
,
4595 state
->filter
, state
->attrs
,
4596 state
->attrsonly
, 0, &state
->entries
,
4597 &state
->pagedresults_cookie
);
4599 if (rc
!= LDAP_SUCCESS
)
4600 DEBUG(5, ("Could not end search properly\n"));
4605 static bool ldapuser2displayentry(struct ldap_search_state
*state
,
4606 TALLOC_CTX
*mem_ctx
,
4607 LDAP
*ld
, LDAPMessage
*entry
,
4608 struct samr_displayentry
*result
)
4611 size_t converted_size
;
4613 uint32_t acct_flags
;
4615 vals
= ldap_get_values(ld
, entry
, "sambaAcctFlags");
4616 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4617 acct_flags
= ACB_NORMAL
;
4619 acct_flags
= pdb_decode_acct_ctrl(vals
[0]);
4620 ldap_value_free(vals
);
4623 if ((state
->acct_flags
!= 0) &&
4624 ((state
->acct_flags
& acct_flags
) == 0))
4627 result
->acct_flags
= acct_flags
;
4628 result
->account_name
= "";
4629 result
->fullname
= "";
4630 result
->description
= "";
4632 vals
= ldap_get_values(ld
, entry
, "uid");
4633 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4634 DEBUG(5, ("\"uid\" not found\n"));
4637 if (!pull_utf8_talloc(mem_ctx
,
4638 discard_const_p(char *, &result
->account_name
),
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
, "displayName");
4648 if ((vals
== NULL
) || (vals
[0] == NULL
))
4649 DEBUG(8, ("\"displayName\" not found\n"));
4650 else if (!pull_utf8_talloc(mem_ctx
,
4651 discard_const_p(char *, &result
->fullname
),
4652 vals
[0], &converted_size
))
4654 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4658 ldap_value_free(vals
);
4660 vals
= ldap_get_values(ld
, entry
, "description");
4661 if ((vals
== NULL
) || (vals
[0] == NULL
))
4662 DEBUG(8, ("\"description\" not found\n"));
4663 else if (!pull_utf8_talloc(mem_ctx
,
4664 discard_const_p(char *, &result
->description
),
4665 vals
[0], &converted_size
))
4667 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4671 ldap_value_free(vals
);
4673 if ((result
->account_name
== NULL
) ||
4674 (result
->fullname
== NULL
) ||
4675 (result
->description
== NULL
)) {
4676 DEBUG(0, ("talloc failed\n"));
4680 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4681 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4682 DEBUG(0, ("\"objectSid\" not found\n"));
4686 if (!string_to_sid(&sid
, vals
[0])) {
4687 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4688 ldap_value_free(vals
);
4691 ldap_value_free(vals
);
4693 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)) {
4694 struct dom_sid_buf buf
;
4695 DEBUG(0, ("sid %s does not belong to our domain\n",
4696 dom_sid_str_buf(&sid
, &buf
)));
4704 static bool ldapsam_search_users(struct pdb_methods
*methods
,
4705 struct pdb_search
*search
,
4706 uint32_t acct_flags
)
4708 struct ldapsam_privates
*ldap_state
=
4709 (struct ldapsam_privates
*)methods
->private_data
;
4710 struct ldap_search_state
*state
;
4712 state
= talloc(search
, struct ldap_search_state
);
4713 if (state
== NULL
) {
4714 DEBUG(0, ("talloc failed\n"));
4718 state
->connection
= ldap_state
->smbldap_state
;
4720 if ((acct_flags
!= 0) && ((acct_flags
& ACB_NORMAL
) != 0))
4721 state
->base
= lp_ldap_user_suffix(talloc_tos());
4722 else if ((acct_flags
!= 0) &&
4723 ((acct_flags
& (ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) != 0))
4724 state
->base
= lp_ldap_machine_suffix(talloc_tos());
4726 state
->base
= lp_ldap_suffix();
4728 state
->acct_flags
= acct_flags
;
4729 state
->base
= talloc_strdup(search
, state
->base
);
4730 state
->scope
= LDAP_SCOPE_SUBTREE
;
4731 state
->filter
= get_ldap_filter(search
, "*");
4732 state
->attrs
= talloc_attrs(search
, "uid", "sambaSid",
4733 "displayName", "description",
4734 "sambaAcctFlags", NULL
);
4735 state
->attrsonly
= 0;
4736 state
->pagedresults_cookie
= NULL
;
4737 state
->entries
= NULL
;
4738 state
->ldap2displayentry
= ldapuser2displayentry
;
4740 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4741 DEBUG(0, ("talloc failed\n"));
4745 search
->private_data
= state
;
4746 search
->next_entry
= ldapsam_search_next_entry
;
4747 search
->search_end
= ldapsam_search_end
;
4749 return ldapsam_search_firstpage(search
);
4752 static bool ldapgroup2displayentry(struct ldap_search_state
*state
,
4753 TALLOC_CTX
*mem_ctx
,
4754 LDAP
*ld
, LDAPMessage
*entry
,
4755 struct samr_displayentry
*result
)
4758 size_t converted_size
;
4760 uint16_t group_type
;
4762 result
->account_name
= "";
4763 result
->fullname
= "";
4764 result
->description
= "";
4767 vals
= ldap_get_values(ld
, entry
, "sambaGroupType");
4768 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4769 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4771 ldap_value_free(vals
);
4776 group_type
= atoi(vals
[0]);
4778 if ((state
->group_type
!= 0) &&
4779 ((state
->group_type
!= group_type
))) {
4780 ldap_value_free(vals
);
4784 ldap_value_free(vals
);
4786 /* display name is the NT group name */
4788 vals
= ldap_get_values(ld
, entry
, "displayName");
4789 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4790 DEBUG(8, ("\"displayName\" not found\n"));
4792 /* fallback to the 'cn' attribute */
4793 vals
= ldap_get_values(ld
, entry
, "cn");
4794 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4795 DEBUG(5, ("\"cn\" not found\n"));
4798 if (!pull_utf8_talloc(mem_ctx
,
4799 discard_const_p(char *,
4800 &result
->account_name
),
4801 vals
[0], &converted_size
))
4803 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4804 "failed: %s", strerror(errno
)));
4807 else if (!pull_utf8_talloc(mem_ctx
,
4808 discard_const_p(char *,
4809 &result
->account_name
),
4810 vals
[0], &converted_size
))
4812 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4816 ldap_value_free(vals
);
4818 vals
= ldap_get_values(ld
, entry
, "description");
4819 if ((vals
== NULL
) || (vals
[0] == NULL
))
4820 DEBUG(8, ("\"description\" not found\n"));
4821 else if (!pull_utf8_talloc(mem_ctx
,
4822 discard_const_p(char *, &result
->description
),
4823 vals
[0], &converted_size
))
4825 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4828 ldap_value_free(vals
);
4830 if ((result
->account_name
== NULL
) ||
4831 (result
->fullname
== NULL
) ||
4832 (result
->description
== NULL
)) {
4833 DEBUG(0, ("talloc failed\n"));
4837 vals
= ldap_get_values(ld
, entry
, "sambaSid");
4838 if ((vals
== NULL
) || (vals
[0] == NULL
)) {
4839 DEBUG(0, ("\"objectSid\" not found\n"));
4841 ldap_value_free(vals
);
4846 if (!string_to_sid(&sid
, vals
[0])) {
4847 DEBUG(0, ("Could not convert %s to SID\n", vals
[0]));
4851 ldap_value_free(vals
);
4853 switch (group_type
) {
4854 case SID_NAME_DOM_GRP
:
4855 case SID_NAME_ALIAS
:
4857 if (!sid_peek_check_rid(get_global_sam_sid(), &sid
, &result
->rid
)
4858 && !sid_peek_check_rid(&global_sid_Builtin
, &sid
, &result
->rid
))
4860 struct dom_sid_buf buf
;
4861 DEBUG(0, ("%s is not in our domain\n",
4862 dom_sid_str_buf(&sid
, &buf
)));
4868 DEBUG(0,("unknown group type: %d\n", group_type
));
4872 result
->acct_flags
= 0;
4877 static bool ldapsam_search_grouptype(struct pdb_methods
*methods
,
4878 struct pdb_search
*search
,
4879 const struct dom_sid
*sid
,
4880 enum lsa_SidType type
)
4882 struct ldapsam_privates
*ldap_state
=
4883 (struct ldapsam_privates
*)methods
->private_data
;
4884 struct ldap_search_state
*state
;
4885 struct dom_sid_buf tmp
;
4887 state
= talloc(search
, struct ldap_search_state
);
4888 if (state
== NULL
) {
4889 DEBUG(0, ("talloc failed\n"));
4893 state
->connection
= ldap_state
->smbldap_state
;
4895 state
->base
= lp_ldap_suffix();
4896 state
->connection
= ldap_state
->smbldap_state
;
4897 state
->scope
= LDAP_SCOPE_SUBTREE
;
4898 state
->filter
= talloc_asprintf(search
, "(&(objectclass=%s)"
4899 "(sambaGroupType=%d)(sambaSID=%s*))",
4902 dom_sid_str_buf(sid
, &tmp
));
4903 state
->attrs
= talloc_attrs(search
, "cn", "sambaSid",
4904 "displayName", "description",
4905 "sambaGroupType", NULL
);
4906 state
->attrsonly
= 0;
4907 state
->pagedresults_cookie
= NULL
;
4908 state
->entries
= NULL
;
4909 state
->group_type
= type
;
4910 state
->ldap2displayentry
= ldapgroup2displayentry
;
4912 if ((state
->filter
== NULL
) || (state
->attrs
== NULL
)) {
4913 DEBUG(0, ("talloc failed\n"));
4917 search
->private_data
= state
;
4918 search
->next_entry
= ldapsam_search_next_entry
;
4919 search
->search_end
= ldapsam_search_end
;
4921 return ldapsam_search_firstpage(search
);
4924 static bool ldapsam_search_groups(struct pdb_methods
*methods
,
4925 struct pdb_search
*search
)
4927 return ldapsam_search_grouptype(methods
, search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
4930 static bool ldapsam_search_aliases(struct pdb_methods
*methods
,
4931 struct pdb_search
*search
,
4932 const struct dom_sid
*sid
)
4934 return ldapsam_search_grouptype(methods
, search
, sid
, SID_NAME_ALIAS
);
4937 static uint32_t ldapsam_capabilities(struct pdb_methods
*methods
)
4939 return PDB_CAP_STORE_RIDS
;
4942 static NTSTATUS
ldapsam_get_new_rid(struct ldapsam_privates
*priv
,
4945 struct smbldap_state
*smbldap_state
= priv
->smbldap_state
;
4947 LDAPMessage
*result
= NULL
;
4948 LDAPMessage
*entry
= NULL
;
4949 LDAPMod
**mods
= NULL
;
4953 uint32_t nextRid
= 0;
4958 TALLOC_CTX
*mem_ctx
;
4960 mem_ctx
= talloc_new(NULL
);
4961 if (mem_ctx
== NULL
) {
4962 DEBUG(0, ("talloc_new failed\n"));
4963 return NT_STATUS_NO_MEMORY
;
4966 status
= smbldap_search_domain_info(smbldap_state
, &result
,
4967 get_global_sam_name(), False
);
4968 if (!NT_STATUS_IS_OK(status
)) {
4969 DEBUG(3, ("Could not get domain info: %s\n",
4970 nt_errstr(status
)));
4974 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
4976 entry
= ldap_first_entry(priv2ld(priv
), result
);
4977 if (entry
== NULL
) {
4978 DEBUG(0, ("Could not get domain info entry\n"));
4979 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
4983 /* Find the largest of the three attributes "sambaNextRid",
4984 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4985 concept of differentiating between user and group rids, and will
4986 use only "sambaNextRid" in the future. But for compatibility
4987 reasons I look if others have chosen different strategies -- VL */
4989 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
4990 "sambaNextRid", mem_ctx
);
4991 if (value
!= NULL
) {
4992 tmp
= (uint32_t)smb_strtoul(value
,
5001 nextRid
= MAX(nextRid
, tmp
);
5004 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5005 "sambaNextUserRid", mem_ctx
);
5006 if (value
!= NULL
) {
5007 tmp
= (uint32_t)smb_strtoul(value
,
5016 nextRid
= MAX(nextRid
, tmp
);
5019 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5020 "sambaNextGroupRid", mem_ctx
);
5021 if (value
!= NULL
) {
5022 tmp
= (uint32_t)smb_strtoul(value
,
5031 nextRid
= MAX(nextRid
, tmp
);
5035 nextRid
= BASE_RID
-1;
5040 smbldap_make_mod(priv2ld(priv
), entry
, &mods
, "sambaNextRid",
5041 talloc_asprintf(mem_ctx
, "%d", nextRid
));
5042 smbldap_talloc_autofree_ldapmod(mem_ctx
, mods
);
5044 if ((dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)) == NULL
) {
5045 status
= NT_STATUS_NO_MEMORY
;
5049 rc
= smbldap_modify(smbldap_state
, dn
, mods
);
5051 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5054 status
= (rc
== LDAP_SUCCESS
) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
5057 if (NT_STATUS_IS_OK(status
)) {
5061 TALLOC_FREE(mem_ctx
);
5065 static NTSTATUS
ldapsam_new_rid_internal(struct pdb_methods
*methods
, uint32_t *rid
)
5069 for (i
=0; i
<10; i
++) {
5070 NTSTATUS result
= ldapsam_get_new_rid(
5071 (struct ldapsam_privates
*)methods
->private_data
, rid
);
5072 if (NT_STATUS_IS_OK(result
)) {
5076 if (!NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
5080 /* The ldap update failed (maybe a race condition), retry */
5083 /* Tried 10 times, fail. */
5084 return NT_STATUS_ACCESS_DENIED
;
5087 static bool ldapsam_new_rid(struct pdb_methods
*methods
, uint32_t *rid
)
5089 NTSTATUS result
= ldapsam_new_rid_internal(methods
, rid
);
5090 return NT_STATUS_IS_OK(result
) ? True
: False
;
5093 static bool ldapsam_sid_to_id(struct pdb_methods
*methods
,
5094 const struct dom_sid
*sid
,
5097 struct ldapsam_privates
*priv
=
5098 (struct ldapsam_privates
*)methods
->private_data
;
5101 struct dom_sid_buf buf
;
5102 const char *attrs
[] = { "sambaGroupType", "gidNumber", "uidNumber",
5104 LDAPMessage
*result
= NULL
;
5105 LDAPMessage
*entry
= NULL
;
5110 TALLOC_CTX
*mem_ctx
;
5112 ret
= pdb_sid_to_id_unix_users_and_groups(sid
, id
);
5117 mem_ctx
= talloc_new(NULL
);
5118 if (mem_ctx
== NULL
) {
5119 DEBUG(0, ("talloc_new failed\n"));
5123 filter
= talloc_asprintf(mem_ctx
,
5125 "(|(objectClass=%s)(objectClass=%s)))",
5126 dom_sid_str_buf(sid
, &buf
),
5127 LDAP_OBJ_GROUPMAP
, LDAP_OBJ_SAMBASAMACCOUNT
);
5128 if (filter
== NULL
) {
5129 DEBUG(5, ("talloc_asprintf failed\n"));
5133 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
,
5135 if (rc
!= LDAP_SUCCESS
) {
5138 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
5140 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5141 DEBUG(10, ("Got %d entries, expected one\n",
5142 ldap_count_entries(priv2ld(priv
), result
)));
5146 entry
= ldap_first_entry(priv2ld(priv
), result
);
5148 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5149 "sambaGroupType", mem_ctx
);
5151 if (value
!= NULL
) {
5152 const char *gid_str
;
5155 gid_str
= smbldap_talloc_single_attribute(
5156 priv2ld(priv
), entry
, "gidNumber", mem_ctx
);
5157 if (gid_str
== NULL
) {
5158 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5159 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
),
5164 id
->id
= smb_strtoul(gid_str
,
5173 id
->type
= ID_TYPE_GID
;
5178 /* It must be a user */
5180 value
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5181 "uidNumber", mem_ctx
);
5182 if (value
== NULL
) {
5183 DEBUG(1, ("Could not find uidNumber in %s\n",
5184 smbldap_talloc_dn(mem_ctx
, priv2ld(priv
), entry
)));
5188 id
->id
= smb_strtoul(value
, NULL
, 10, &error
, SMB_STR_STANDARD
);
5193 id
->type
= ID_TYPE_UID
;
5196 TALLOC_FREE(mem_ctx
);
5201 * Find the SID for a uid.
5202 * This is shortcut is only used if ldapsam:trusted is set to true.
5204 static bool ldapsam_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
5205 struct dom_sid
*sid
)
5207 struct ldapsam_privates
*priv
=
5208 (struct ldapsam_privates
*)methods
->private_data
;
5210 const char *attrs
[] = { "sambaSID", NULL
};
5211 LDAPMessage
*result
= NULL
;
5212 LDAPMessage
*entry
= NULL
;
5214 char *user_sid_string
;
5215 struct dom_sid user_sid
;
5217 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5219 filter
= talloc_asprintf(tmp_ctx
,
5222 "(objectClass=%s))",
5224 LDAP_OBJ_POSIXACCOUNT
,
5225 LDAP_OBJ_SAMBASAMACCOUNT
);
5226 if (filter
== NULL
) {
5227 DEBUG(3, ("talloc_asprintf failed\n"));
5231 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5232 if (rc
!= LDAP_SUCCESS
) {
5235 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5237 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5238 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5239 ldap_count_entries(priv2ld(priv
), result
),
5240 (unsigned int)uid
));
5244 entry
= ldap_first_entry(priv2ld(priv
), result
);
5246 user_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5247 "sambaSID", tmp_ctx
);
5248 if (user_sid_string
== NULL
) {
5249 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5250 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5254 if (!string_to_sid(&user_sid
, user_sid_string
)) {
5255 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5260 sid_copy(sid
, &user_sid
);
5265 TALLOC_FREE(tmp_ctx
);
5270 * Find the SID for a gid.
5271 * This is shortcut is only used if ldapsam:trusted is set to true.
5273 static bool ldapsam_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
5274 struct dom_sid
*sid
)
5276 struct ldapsam_privates
*priv
=
5277 (struct ldapsam_privates
*)methods
->private_data
;
5279 const char *attrs
[] = { "sambaSID", NULL
};
5280 LDAPMessage
*result
= NULL
;
5281 LDAPMessage
*entry
= NULL
;
5283 char *group_sid_string
;
5284 struct dom_sid group_sid
;
5286 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
5288 filter
= talloc_asprintf(tmp_ctx
,
5290 "(objectClass=%s))",
5293 if (filter
== NULL
) {
5294 DEBUG(3, ("talloc_asprintf failed\n"));
5298 rc
= smbldap_search_suffix(priv
->smbldap_state
, filter
, attrs
, &result
);
5299 if (rc
!= LDAP_SUCCESS
) {
5302 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5304 if (ldap_count_entries(priv2ld(priv
), result
) != 1) {
5305 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5306 ldap_count_entries(priv2ld(priv
), result
),
5307 (unsigned int)gid
));
5311 entry
= ldap_first_entry(priv2ld(priv
), result
);
5313 group_sid_string
= smbldap_talloc_single_attribute(priv2ld(priv
), entry
,
5314 "sambaSID", tmp_ctx
);
5315 if (group_sid_string
== NULL
) {
5316 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5317 smbldap_talloc_dn(tmp_ctx
, priv2ld(priv
), entry
)));
5321 if (!string_to_sid(&group_sid
, group_sid_string
)) {
5322 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5327 sid_copy(sid
, &group_sid
);
5332 TALLOC_FREE(tmp_ctx
);
5336 static bool ldapsam_id_to_sid(struct pdb_methods
*methods
, struct unixid
*id
,
5337 struct dom_sid
*sid
)
5341 return ldapsam_uid_to_sid(methods
, id
->id
, sid
);
5344 return ldapsam_gid_to_sid(methods
, id
->id
, sid
);
5353 * The following functions are called only if
5354 * ldapsam:trusted and ldapsam:editposix are
5359 * ldapsam_create_user creates a new
5360 * posixAccount and sambaSamAccount object
5361 * in the ldap users subtree
5363 * The uid is allocated by winbindd.
5366 static NTSTATUS
ldapsam_create_user(struct pdb_methods
*my_methods
,
5367 TALLOC_CTX
*tmp_ctx
, const char *name
,
5368 uint32_t acb_info
, uint32_t *rid
)
5370 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5371 LDAPMessage
*entry
= NULL
;
5372 LDAPMessage
*result
= NULL
;
5373 uint32_t num_result
;
5374 bool is_machine
= False
;
5375 bool add_posix
= False
;
5376 bool init_okay
= False
;
5377 LDAPMod
**mods
= NULL
;
5385 const char *dn
= NULL
;
5386 struct dom_sid group_sid
;
5387 struct dom_sid user_sid
;
5393 if (((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] == '$') ||
5394 acb_info
& ACB_WSTRUST
||
5395 acb_info
& ACB_SVRTRUST
||
5396 acb_info
& ACB_DOMTRUST
) {
5400 username
= escape_ldap_string(talloc_tos(), name
);
5401 filter
= talloc_asprintf(tmp_ctx
, "(&(uid=%s)(objectClass=%s))",
5402 username
, LDAP_OBJ_POSIXACCOUNT
);
5403 TALLOC_FREE(username
);
5405 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5406 if (rc
!= LDAP_SUCCESS
) {
5407 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5408 return NT_STATUS_ACCESS_DENIED
;
5410 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5412 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5414 if (num_result
> 1) {
5415 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name
));
5416 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5419 if (num_result
== 1) {
5421 /* check if it is just a posix account.
5422 * or if there is a sid attached to this entry
5425 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5427 return NT_STATUS_UNSUCCESSFUL
;
5430 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5432 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name
));
5433 return NT_STATUS_USER_EXISTS
;
5436 /* it is just a posix account, retrieve the dn for later use */
5437 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5439 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5440 return NT_STATUS_NO_MEMORY
;
5444 if (num_result
== 0) {
5448 /* Create the basic samu structure and generate the mods for the ldap commit */
5449 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5450 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5454 sid_compose(&user_sid
, get_global_sam_sid(), *rid
);
5456 user
= samu_new(tmp_ctx
);
5458 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5459 return NT_STATUS_NO_MEMORY
;
5462 if (!pdb_set_username(user
, name
, PDB_SET
)) {
5463 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5464 return NT_STATUS_UNSUCCESSFUL
;
5466 if (!pdb_set_domain(user
, get_global_sam_name(), PDB_SET
)) {
5467 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5468 return NT_STATUS_UNSUCCESSFUL
;
5471 if (acb_info
& ACB_NORMAL
) {
5472 if (!pdb_set_acct_ctrl(user
, ACB_WSTRUST
, PDB_SET
)) {
5473 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5474 return NT_STATUS_UNSUCCESSFUL
;
5477 if (!pdb_set_acct_ctrl(user
, acb_info
, PDB_SET
)) {
5478 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5479 return NT_STATUS_UNSUCCESSFUL
;
5483 if (!pdb_set_acct_ctrl(user
, ACB_NORMAL
| ACB_DISABLED
, PDB_SET
)) {
5484 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5485 return NT_STATUS_UNSUCCESSFUL
;
5489 if (!pdb_set_user_sid(user
, &user_sid
, PDB_SET
)) {
5490 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5491 return NT_STATUS_UNSUCCESSFUL
;
5494 init_okay
= init_ldap_from_sam(ldap_state
, entry
, &mods
, user
, pdb_element_is_set_or_changed
);
5497 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5498 ldap_mods_free(mods
, true);
5499 return NT_STATUS_UNSUCCESSFUL
;
5502 if (ldap_state
->schema_ver
!= SCHEMAVER_SAMBASAMACCOUNT
) {
5503 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5505 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
5510 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5512 /* retrieve the Domain Users group gid */
5513 if (!sid_compose(&group_sid
, get_global_sam_sid(), DOMAIN_RID_USERS
) ||
5514 !sid_to_gid(&group_sid
, &gid
)) {
5515 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5516 ldap_mods_free(mods
, true);
5517 return NT_STATUS_INVALID_PRIMARY_GROUP
;
5520 /* lets allocate a new userid for this user */
5521 if (!winbind_allocate_uid(&uid
)) {
5522 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5523 ldap_mods_free(mods
, true);
5524 return NT_STATUS_UNSUCCESSFUL
;
5529 /* TODO: choose a more appropriate default for machines */
5530 homedir
= talloc_sub_specified(tmp_ctx
,
5531 lp_template_homedir(),
5532 "SMB_workstations_home",
5534 ldap_state
->domain_name
,
5537 shell
= talloc_strdup(tmp_ctx
, "/bin/false");
5539 homedir
= talloc_sub_specified(tmp_ctx
,
5540 lp_template_homedir(),
5543 ldap_state
->domain_name
,
5546 shell
= talloc_sub_specified(tmp_ctx
,
5547 lp_template_shell(),
5550 ldap_state
->domain_name
,
5554 uidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)uid
);
5555 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5557 escape_name
= escape_rdn_val_string_alloc(name
);
5559 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5560 ldap_mods_free(mods
, true);
5561 return NT_STATUS_NO_MEMORY
;
5565 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_machine_suffix (talloc_tos()));
5567 dn
= talloc_asprintf(tmp_ctx
, "uid=%s,%s", escape_name
, lp_ldap_user_suffix (talloc_tos()));
5570 SAFE_FREE(escape_name
);
5572 if (!homedir
|| !shell
|| !uidstr
|| !gidstr
|| !dn
) {
5573 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5574 ldap_mods_free(mods
, true);
5575 return NT_STATUS_NO_MEMORY
;
5578 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
5579 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
5580 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5581 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
5582 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5583 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", homedir
);
5584 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
5588 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5590 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5593 ldap_mods_free(mods
, true);
5595 if (rc
!= LDAP_SUCCESS
) {
5596 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name
,dn
));
5597 return NT_STATUS_UNSUCCESSFUL
;
5600 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name
));
5602 flush_pwnam_cache();
5604 return NT_STATUS_OK
;
5607 static NTSTATUS
ldapsam_delete_user(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, struct samu
*sam_acct
)
5609 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5610 LDAPMessage
*result
= NULL
;
5611 LDAPMessage
*entry
= NULL
;
5617 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct
)));
5619 filter
= talloc_asprintf(tmp_ctx
,
5622 "(objectClass=%s))",
5623 pdb_get_username(sam_acct
),
5624 LDAP_OBJ_POSIXACCOUNT
,
5625 LDAP_OBJ_SAMBASAMACCOUNT
);
5626 if (filter
== NULL
) {
5627 return NT_STATUS_NO_MEMORY
;
5630 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5631 if (rc
!= LDAP_SUCCESS
) {
5632 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5633 return NT_STATUS_UNSUCCESSFUL
;
5635 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5637 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5639 if (num_result
== 0) {
5640 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5641 return NT_STATUS_NO_SUCH_USER
;
5644 if (num_result
> 1) {
5645 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct
)));
5646 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5649 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5651 return NT_STATUS_UNSUCCESSFUL
;
5654 /* it is just a posix account, retrieve the dn for later use */
5655 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5657 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5658 return NT_STATUS_NO_MEMORY
;
5661 /* try to remove memberships first */
5664 struct dom_sid
*sids
= NULL
;
5666 uint32_t num_groups
= 0;
5668 uint32_t user_rid
= pdb_get_user_rid(sam_acct
);
5670 status
= ldapsam_enum_group_memberships(my_methods
,
5676 if (!NT_STATUS_IS_OK(status
)) {
5680 for (i
=0; i
< num_groups
; i
++) {
5684 sid_peek_rid(&sids
[i
], &group_rid
);
5686 ldapsam_del_groupmem(my_methods
,
5695 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5696 if (rc
!= LDAP_SUCCESS
) {
5697 return NT_STATUS_UNSUCCESSFUL
;
5700 flush_pwnam_cache();
5702 return NT_STATUS_OK
;
5706 * ldapsam_create_group creates a new
5707 * posixGroup and sambaGroupMapping object
5708 * in the ldap groups subtree
5710 * The gid is allocated by winbindd.
5713 static NTSTATUS
ldapsam_create_dom_group(struct pdb_methods
*my_methods
,
5714 TALLOC_CTX
*tmp_ctx
,
5718 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5720 LDAPMessage
*entry
= NULL
;
5721 LDAPMessage
*result
= NULL
;
5722 uint32_t num_result
;
5723 bool is_new_entry
= False
;
5724 LDAPMod
**mods
= NULL
;
5729 const char *dn
= NULL
;
5730 struct dom_sid group_sid
;
5731 struct dom_sid_buf buf
;
5736 groupname
= escape_ldap_string(talloc_tos(), name
);
5737 filter
= talloc_asprintf(tmp_ctx
, "(&(cn=%s)(objectClass=%s))",
5738 groupname
, LDAP_OBJ_POSIXGROUP
);
5739 TALLOC_FREE(groupname
);
5741 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5742 if (rc
!= LDAP_SUCCESS
) {
5743 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5744 return NT_STATUS_UNSUCCESSFUL
;
5746 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5748 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5750 if (num_result
> 1) {
5751 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name
));
5752 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5755 if (num_result
== 1) {
5757 /* check if it is just a posix group.
5758 * or if there is a sid attached to this entry
5761 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5763 return NT_STATUS_UNSUCCESSFUL
;
5766 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "sambaSID", tmp_ctx
);
5768 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name
));
5769 return NT_STATUS_GROUP_EXISTS
;
5772 /* it is just a posix group, retrieve the gid and the dn for later use */
5773 tmp
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5775 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name
));
5776 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5779 gid
= smb_strtoul(tmp
, NULL
, 10, &error
, SMB_STR_STANDARD
);
5781 DBG_ERR("Failed to convert gidNumber\n");
5782 return NT_STATUS_UNSUCCESSFUL
;
5785 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5787 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5788 return NT_STATUS_NO_MEMORY
;
5792 if (num_result
== 0) {
5793 is_new_entry
= true;
5796 if (!NT_STATUS_IS_OK((ret
= ldapsam_new_rid_internal(my_methods
, rid
)))) {
5797 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5801 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
5803 grouptype
= talloc_asprintf(tmp_ctx
, "%d", SID_NAME_DOM_GRP
);
5806 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5807 return NT_STATUS_NO_MEMORY
;
5810 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
5811 smbldap_set_mod(&mods
,
5814 dom_sid_str_buf(&group_sid
, &buf
));
5815 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", grouptype
);
5816 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
5821 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5823 /* lets allocate a new groupid for this group */
5824 if (!winbind_allocate_gid(&gid
)) {
5825 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5826 return NT_STATUS_UNSUCCESSFUL
;
5829 gidstr
= talloc_asprintf(tmp_ctx
, "%u", (unsigned int)gid
);
5831 escape_name
= escape_rdn_val_string_alloc(name
);
5833 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5834 return NT_STATUS_NO_MEMORY
;
5837 dn
= talloc_asprintf(tmp_ctx
, "cn=%s,%s", escape_name
, lp_ldap_group_suffix(talloc_tos()));
5839 SAFE_FREE(escape_name
);
5841 if (!gidstr
|| !dn
) {
5842 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5843 return NT_STATUS_NO_MEMORY
;
5846 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_POSIXGROUP
);
5847 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
5848 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
5851 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
5854 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5856 if (rc
== LDAP_OBJECT_CLASS_VIOLATION
) {
5857 /* This call may fail with rfc2307bis schema */
5858 /* Retry adding a structural class */
5859 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "????");
5860 rc
= smbldap_add(ldap_state
->smbldap_state
, dn
, mods
);
5864 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
5867 if (rc
!= LDAP_SUCCESS
) {
5868 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name
,dn
));
5869 return NT_STATUS_UNSUCCESSFUL
;
5872 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name
));
5874 return NT_STATUS_OK
;
5877 static NTSTATUS
ldapsam_delete_dom_group(struct pdb_methods
*my_methods
, TALLOC_CTX
*tmp_ctx
, uint32_t rid
)
5879 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5880 LDAPMessage
*result
= NULL
;
5881 LDAPMessage
*entry
= NULL
;
5886 struct dom_sid group_sid
;
5887 struct dom_sid_buf buf
;
5890 /* get the group sid */
5891 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
5893 filter
= talloc_asprintf(tmp_ctx
,
5896 "(objectClass=%s))",
5897 dom_sid_str_buf(&group_sid
, &buf
),
5898 LDAP_OBJ_POSIXGROUP
,
5900 if (filter
== NULL
) {
5901 return NT_STATUS_NO_MEMORY
;
5904 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5905 if (rc
!= LDAP_SUCCESS
) {
5906 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5907 return NT_STATUS_UNSUCCESSFUL
;
5909 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5911 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5913 if (num_result
== 0) {
5914 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5915 return NT_STATUS_NO_SUCH_GROUP
;
5918 if (num_result
> 1) {
5919 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5920 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5923 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
5925 return NT_STATUS_UNSUCCESSFUL
;
5928 /* here it is, retrieve the dn for later use */
5929 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
5931 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5932 return NT_STATUS_NO_MEMORY
;
5935 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
5937 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5938 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
5941 /* check no user have this group marked as primary group */
5942 filter
= talloc_asprintf(tmp_ctx
,
5945 "(objectClass=%s))",
5947 LDAP_OBJ_POSIXACCOUNT
,
5948 LDAP_OBJ_SAMBASAMACCOUNT
);
5950 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
5951 if (rc
!= LDAP_SUCCESS
) {
5952 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5953 return NT_STATUS_UNSUCCESSFUL
;
5955 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
5957 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
5959 if (num_result
!= 0) {
5960 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result
));
5961 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
5964 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
5965 if (rc
!= LDAP_SUCCESS
) {
5966 return NT_STATUS_UNSUCCESSFUL
;
5969 return NT_STATUS_OK
;
5972 static NTSTATUS
ldapsam_change_groupmem(struct pdb_methods
*my_methods
,
5973 TALLOC_CTX
*tmp_ctx
,
5975 uint32_t member_rid
,
5978 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
5979 LDAPMessage
*entry
= NULL
;
5980 LDAPMessage
*result
= NULL
;
5981 uint32_t num_result
;
5982 LDAPMod
**mods
= NULL
;
5985 const char *dn
= NULL
;
5986 struct dom_sid group_sid
;
5987 struct dom_sid member_sid
;
5988 struct dom_sid_buf buf
;
5994 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid
, group_rid
));
5996 case LDAP_MOD_DELETE
:
5997 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid
, group_rid
));
6000 return NT_STATUS_UNSUCCESSFUL
;
6003 /* get member sid */
6004 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
6006 /* get the group sid */
6007 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
6009 filter
= talloc_asprintf(tmp_ctx
,
6012 "(objectClass=%s))",
6013 dom_sid_str_buf(&member_sid
, &buf
),
6014 LDAP_OBJ_POSIXACCOUNT
,
6015 LDAP_OBJ_SAMBASAMACCOUNT
);
6016 if (filter
== NULL
) {
6017 return NT_STATUS_NO_MEMORY
;
6020 /* get the member uid */
6021 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6022 if (rc
!= LDAP_SUCCESS
) {
6023 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
6024 return NT_STATUS_UNSUCCESSFUL
;
6026 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
6028 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6030 if (num_result
== 0) {
6031 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6032 return NT_STATUS_NO_SUCH_MEMBER
;
6035 if (num_result
> 1) {
6036 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6037 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6040 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6042 return NT_STATUS_UNSUCCESSFUL
;
6045 if (modop
== LDAP_MOD_DELETE
) {
6046 /* check if we are trying to remove the member from his primary group */
6048 gid_t user_gid
, group_gid
;
6050 gidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "gidNumber", tmp_ctx
);
6052 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6053 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6056 user_gid
= smb_strtoul(gidstr
, NULL
, 10, &error
, SMB_STR_STANDARD
);
6058 DBG_ERR("Failed to convert user gid\n");
6059 return NT_STATUS_UNSUCCESSFUL
;
6062 if (!sid_to_gid(&group_sid
, &group_gid
)) {
6063 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6064 return NT_STATUS_UNSUCCESSFUL
;
6067 if (user_gid
== group_gid
) {
6068 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6069 return NT_STATUS_MEMBERS_PRIMARY_GROUP
;
6073 /* here it is, retrieve the uid for later use */
6074 uidstr
= smbldap_talloc_single_attribute(priv2ld(ldap_state
), entry
, "uid", tmp_ctx
);
6076 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6077 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6080 filter
= talloc_asprintf(tmp_ctx
,
6083 "(objectClass=%s))",
6084 dom_sid_str_buf(&group_sid
, &buf
),
6085 LDAP_OBJ_POSIXGROUP
,
6089 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6090 if (rc
!= LDAP_SUCCESS
) {
6091 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6092 return NT_STATUS_UNSUCCESSFUL
;
6094 smbldap_talloc_autofree_ldapmsg(tmp_ctx
, result
);
6096 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6098 if (num_result
== 0) {
6099 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6100 return NT_STATUS_NO_SUCH_GROUP
;
6103 if (num_result
> 1) {
6104 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6105 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6108 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6110 return NT_STATUS_UNSUCCESSFUL
;
6113 /* here it is, retrieve the dn for later use */
6114 dn
= smbldap_talloc_dn(tmp_ctx
, priv2ld(ldap_state
), entry
);
6116 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6117 return NT_STATUS_NO_MEMORY
;
6120 smbldap_set_mod(&mods
, modop
, "memberUid", uidstr
);
6122 smbldap_talloc_autofree_ldapmod(tmp_ctx
, mods
);
6124 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6125 if (rc
!= LDAP_SUCCESS
) {
6126 if (rc
== LDAP_TYPE_OR_VALUE_EXISTS
&& modop
== LDAP_MOD_ADD
) {
6127 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6128 return NT_STATUS_MEMBER_IN_GROUP
;
6130 if (rc
== LDAP_NO_SUCH_ATTRIBUTE
&& modop
== LDAP_MOD_DELETE
) {
6131 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6132 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
6134 return NT_STATUS_UNSUCCESSFUL
;
6137 return NT_STATUS_OK
;
6140 static NTSTATUS
ldapsam_add_groupmem(struct pdb_methods
*my_methods
,
6141 TALLOC_CTX
*tmp_ctx
,
6143 uint32_t member_rid
)
6145 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_ADD
);
6147 static NTSTATUS
ldapsam_del_groupmem(struct pdb_methods
*my_methods
,
6148 TALLOC_CTX
*tmp_ctx
,
6150 uint32_t member_rid
)
6152 return ldapsam_change_groupmem(my_methods
, tmp_ctx
, group_rid
, member_rid
, LDAP_MOD_DELETE
);
6155 static NTSTATUS
ldapsam_set_primary_group(struct pdb_methods
*my_methods
,
6156 TALLOC_CTX
*mem_ctx
,
6157 struct samu
*sampass
)
6159 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
6160 LDAPMessage
*entry
= NULL
;
6161 LDAPMessage
*result
= NULL
;
6162 uint32_t num_result
;
6163 LDAPMod
**mods
= NULL
;
6165 char *escape_username
;
6171 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass
)));
6173 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
)) {
6174 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6175 return NT_STATUS_UNSUCCESSFUL
;
6177 gidstr
= talloc_asprintf(mem_ctx
, "%u", (unsigned int)gid
);
6179 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6180 return NT_STATUS_NO_MEMORY
;
6183 escape_username
= escape_ldap_string(talloc_tos(),
6184 pdb_get_username(sampass
));
6185 if (escape_username
== NULL
) {
6186 return NT_STATUS_NO_MEMORY
;
6189 filter
= talloc_asprintf(mem_ctx
,
6192 "(objectClass=%s))",
6194 LDAP_OBJ_POSIXACCOUNT
,
6195 LDAP_OBJ_SAMBASAMACCOUNT
);
6197 TALLOC_FREE(escape_username
);
6199 if (filter
== NULL
) {
6200 return NT_STATUS_NO_MEMORY
;
6203 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, NULL
, &result
);
6204 if (rc
!= LDAP_SUCCESS
) {
6205 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6206 return NT_STATUS_UNSUCCESSFUL
;
6208 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6210 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6212 if (num_result
== 0) {
6213 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6214 return NT_STATUS_NO_SUCH_USER
;
6217 if (num_result
> 1) {
6218 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass
)));
6219 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
6222 entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6224 return NT_STATUS_UNSUCCESSFUL
;
6227 /* retrieve the dn for later use */
6228 dn
= smbldap_talloc_dn(mem_ctx
, priv2ld(ldap_state
), entry
);
6230 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6231 return NT_STATUS_NO_MEMORY
;
6234 /* remove the old one, and add the new one, this way we do not risk races */
6235 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "gidNumber", gidstr
);
6239 return NT_STATUS_OK
;
6242 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
6244 if (rc
!= LDAP_SUCCESS
) {
6245 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6246 pdb_get_username(sampass
), gidstr
));
6247 return NT_STATUS_UNSUCCESSFUL
;
6250 flush_pwnam_cache();
6252 return NT_STATUS_OK
;
6256 /**********************************************************************
6257 trusted domains functions
6258 *********************************************************************/
6260 static char *trusteddom_dn(struct ldapsam_privates
*ldap_state
,
6263 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain
,
6264 ldap_state
->domain_dn
);
6267 static bool get_trusteddom_pw_int(struct ldapsam_privates
*ldap_state
,
6268 TALLOC_CTX
*mem_ctx
,
6269 const char *domain
, LDAPMessage
**entry
)
6273 int scope
= LDAP_SCOPE_SUBTREE
;
6274 const char **attrs
= NULL
; /* NULL: get all attrs */
6275 int attrsonly
= 0; /* 0: return values too */
6276 LDAPMessage
*result
= NULL
;
6278 uint32_t num_result
;
6280 filter
= talloc_asprintf(talloc_tos(),
6281 "(&(objectClass=%s)(sambaDomainName=%s))",
6282 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
);
6284 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6285 if (trusted_dn
== NULL
) {
6288 rc
= smbldap_search(ldap_state
->smbldap_state
, trusted_dn
, scope
,
6289 filter
, attrs
, attrsonly
, &result
);
6291 if (result
!= NULL
) {
6292 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6295 if (rc
== LDAP_NO_SUCH_OBJECT
) {
6300 if (rc
!= LDAP_SUCCESS
) {
6304 num_result
= ldap_count_entries(priv2ld(ldap_state
), result
);
6306 if (num_result
> 1) {
6307 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6308 "%s object for domain '%s'?!\n",
6309 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6313 if (num_result
== 0) {
6314 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6315 "%s object for domain %s.\n",
6316 LDAP_OBJ_TRUSTDOM_PASSWORD
, domain
));
6319 *entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6325 static bool ldapsam_get_trusteddom_pw(struct pdb_methods
*methods
,
6328 struct dom_sid
*sid
,
6329 time_t *pass_last_set_time
)
6331 struct ldapsam_privates
*ldap_state
=
6332 (struct ldapsam_privates
*)methods
->private_data
;
6333 LDAPMessage
*entry
= NULL
;
6335 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain
));
6337 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
) ||
6346 pwd_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6347 entry
, "sambaClearTextPassword", talloc_tos());
6348 if (pwd_str
== NULL
) {
6351 /* trusteddom_pw routines do not use talloc yet... */
6352 *pwd
= SMB_STRDUP(pwd_str
);
6358 /* last change time */
6359 if (pass_last_set_time
!= NULL
) {
6361 time_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6362 entry
, "sambaPwdLastSet", talloc_tos());
6363 if (time_str
== NULL
) {
6366 *pass_last_set_time
= (time_t)atol(time_str
);
6372 struct dom_sid dom_sid
;
6373 sid_str
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6376 if (sid_str
== NULL
) {
6379 if (!string_to_sid(&dom_sid
, sid_str
)) {
6382 sid_copy(sid
, &dom_sid
);
6388 static bool ldapsam_set_trusteddom_pw(struct pdb_methods
*methods
,
6391 const struct dom_sid
*sid
)
6393 struct ldapsam_privates
*ldap_state
=
6394 (struct ldapsam_privates
*)methods
->private_data
;
6395 LDAPMessage
*entry
= NULL
;
6396 LDAPMod
**mods
= NULL
;
6397 char *prev_pwd
= NULL
;
6398 char *trusted_dn
= NULL
;
6399 struct dom_sid_buf buf
;
6402 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain
));
6405 * get the current entry (if there is one) in order to put the
6406 * current password into the previous password attribute
6408 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6413 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "objectClass",
6414 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6415 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaDomainName",
6417 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaSID",
6418 dom_sid_str_buf(sid
, &buf
));
6419 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
, "sambaPwdLastSet",
6420 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL
)));
6421 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6422 "sambaClearTextPassword", pwd
);
6424 if (entry
!= NULL
) {
6425 prev_pwd
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6426 entry
, "sambaClearTextPassword", talloc_tos());
6427 if (prev_pwd
!= NULL
) {
6428 smbldap_make_mod(priv2ld(ldap_state
), entry
, &mods
,
6429 "sambaPreviousClearTextPassword",
6434 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods
);
6436 trusted_dn
= trusteddom_dn(ldap_state
, domain
);
6437 if (trusted_dn
== NULL
) {
6440 if (entry
== NULL
) {
6441 rc
= smbldap_add(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6443 rc
= smbldap_modify(ldap_state
->smbldap_state
, trusted_dn
, mods
);
6446 if (rc
!= LDAP_SUCCESS
) {
6447 DEBUG(1, ("error writing trusted domain password!\n"));
6454 static bool ldapsam_del_trusteddom_pw(struct pdb_methods
*methods
,
6458 struct ldapsam_privates
*ldap_state
=
6459 (struct ldapsam_privates
*)methods
->private_data
;
6460 LDAPMessage
*entry
= NULL
;
6461 const char *trusted_dn
;
6463 if (!get_trusteddom_pw_int(ldap_state
, talloc_tos(), domain
, &entry
)) {
6467 if (entry
== NULL
) {
6468 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6473 trusted_dn
= smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state
),
6475 if (trusted_dn
== NULL
) {
6476 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6480 rc
= smbldap_delete(ldap_state
->smbldap_state
, trusted_dn
);
6481 if (rc
!= LDAP_SUCCESS
) {
6488 static NTSTATUS
ldapsam_enum_trusteddoms(struct pdb_methods
*methods
,
6489 TALLOC_CTX
*mem_ctx
,
6490 uint32_t *num_domains
,
6491 struct trustdom_info
***domains
)
6494 struct ldapsam_privates
*ldap_state
=
6495 (struct ldapsam_privates
*)methods
->private_data
;
6497 int scope
= LDAP_SCOPE_SUBTREE
;
6498 const char *attrs
[] = { "sambaDomainName", "sambaSID", NULL
};
6499 int attrsonly
= 0; /* 0: return values too */
6500 LDAPMessage
*result
= NULL
;
6501 LDAPMessage
*entry
= NULL
;
6503 filter
= talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6504 LDAP_OBJ_TRUSTDOM_PASSWORD
);
6506 rc
= smbldap_search(ldap_state
->smbldap_state
,
6507 ldap_state
->domain_dn
,
6514 if (result
!= NULL
) {
6515 smbldap_talloc_autofree_ldapmsg(mem_ctx
, result
);
6518 if (rc
!= LDAP_SUCCESS
) {
6519 return NT_STATUS_UNSUCCESSFUL
;
6523 if (!(*domains
= talloc_array(mem_ctx
, struct trustdom_info
*, 1))) {
6524 DEBUG(1, ("talloc failed\n"));
6525 return NT_STATUS_NO_MEMORY
;
6528 for (entry
= ldap_first_entry(priv2ld(ldap_state
), result
);
6530 entry
= ldap_next_entry(priv2ld(ldap_state
), entry
))
6532 char *dom_name
, *dom_sid_str
;
6533 struct trustdom_info
*dom_info
;
6535 dom_info
= talloc(*domains
, struct trustdom_info
);
6536 if (dom_info
== NULL
) {
6537 DEBUG(1, ("talloc failed\n"));
6538 return NT_STATUS_NO_MEMORY
;
6541 dom_name
= smbldap_talloc_single_attribute(priv2ld(ldap_state
),
6545 if (dom_name
== NULL
) {
6546 DEBUG(1, ("talloc failed\n"));
6547 return NT_STATUS_NO_MEMORY
;
6549 dom_info
->name
= dom_name
;
6551 dom_sid_str
= smbldap_talloc_single_attribute(
6552 priv2ld(ldap_state
), entry
, "sambaSID",
6554 if (dom_sid_str
== NULL
) {
6555 DEBUG(1, ("talloc failed\n"));
6556 return NT_STATUS_NO_MEMORY
;
6558 if (!string_to_sid(&dom_info
->sid
, dom_sid_str
)) {
6559 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6561 return NT_STATUS_UNSUCCESSFUL
;
6564 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
6565 domains
, num_domains
);
6567 if (*domains
== NULL
) {
6568 DEBUG(1, ("talloc failed\n"));
6569 return NT_STATUS_NO_MEMORY
;
6573 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains
));
6574 return NT_STATUS_OK
;
6578 /**********************************************************************
6580 *********************************************************************/
6582 static void free_private_data(void **vp
)
6584 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
6586 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
6588 if ((*ldap_state
)->result
!= NULL
) {
6589 ldap_msgfree((*ldap_state
)->result
);
6590 (*ldap_state
)->result
= NULL
;
6592 if ((*ldap_state
)->domain_dn
!= NULL
) {
6593 SAFE_FREE((*ldap_state
)->domain_dn
);
6598 /* No need to free any further, as it is talloc()ed */
6601 /*********************************************************************
6602 Intitalise the parts of the pdb_methods structure that are common to
6604 *********************************************************************/
6606 static NTSTATUS
pdb_init_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
6609 struct ldapsam_privates
*ldap_state
;
6610 char *bind_dn
= NULL
;
6611 char *bind_secret
= NULL
;
6613 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
6617 (*pdb_method
)->name
= "ldapsam";
6619 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
6620 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
6621 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
6622 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
6623 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
6624 (*pdb_method
)->rename_sam_account
= ldapsam_rename_sam_account
;
6626 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
6627 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
6628 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
6629 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
6630 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
6631 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
6632 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
6634 (*pdb_method
)->get_account_policy
= ldapsam_get_account_policy
;
6635 (*pdb_method
)->set_account_policy
= ldapsam_set_account_policy
;
6637 (*pdb_method
)->get_seq_num
= ldapsam_get_seq_num
;
6639 (*pdb_method
)->capabilities
= ldapsam_capabilities
;
6640 (*pdb_method
)->new_rid
= ldapsam_new_rid
;
6642 (*pdb_method
)->get_trusteddom_pw
= ldapsam_get_trusteddom_pw
;
6643 (*pdb_method
)->set_trusteddom_pw
= ldapsam_set_trusteddom_pw
;
6644 (*pdb_method
)->del_trusteddom_pw
= ldapsam_del_trusteddom_pw
;
6645 (*pdb_method
)->enum_trusteddoms
= ldapsam_enum_trusteddoms
;
6647 /* TODO: Setup private data and free */
6649 if ( !(ldap_state
= talloc_zero(*pdb_method
, struct ldapsam_privates
)) ) {
6650 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6651 return NT_STATUS_NO_MEMORY
;
6654 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
6655 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6656 return NT_STATUS_NO_MEMORY
;
6659 nt_status
= smbldap_init(*pdb_method
, pdb_get_tevent_context(),
6660 location
, false, bind_dn
, bind_secret
,
6661 &ldap_state
->smbldap_state
);
6662 memset(bind_secret
, '\0', strlen(bind_secret
));
6663 SAFE_FREE(bind_secret
);
6665 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6669 if ( !(ldap_state
->domain_name
= talloc_strdup(*pdb_method
, get_global_sam_name()) ) ) {
6670 return NT_STATUS_NO_MEMORY
;
6673 (*pdb_method
)->private_data
= ldap_state
;
6675 (*pdb_method
)->free_private_data
= free_private_data
;
6677 return NT_STATUS_OK
;
6680 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods
*m
)
6685 /**********************************************************************
6686 Initialise the normal mode for pdb_ldap
6687 *********************************************************************/
6689 NTSTATUS
pdb_ldapsam_init_common(struct pdb_methods
**pdb_method
,
6690 const char *location
)
6693 struct ldapsam_privates
*ldap_state
= NULL
;
6694 uint32_t alg_rid_base
;
6695 char *alg_rid_base_string
= NULL
;
6696 LDAPMessage
*result
= NULL
;
6697 LDAPMessage
*entry
= NULL
;
6698 struct dom_sid ldap_domain_sid
;
6699 struct dom_sid secrets_domain_sid
;
6700 char *domain_sid_string
= NULL
;
6702 char *uri
= talloc_strdup( NULL
, location
);
6704 trim_char( uri
, '\"', '\"' );
6705 nt_status
= pdb_init_ldapsam_common(pdb_method
, uri
);
6709 if (!NT_STATUS_IS_OK(nt_status
)) {
6713 (*pdb_method
)->name
= "ldapsam";
6715 (*pdb_method
)->add_aliasmem
= ldapsam_add_aliasmem
;
6716 (*pdb_method
)->del_aliasmem
= ldapsam_del_aliasmem
;
6717 (*pdb_method
)->enum_aliasmem
= ldapsam_enum_aliasmem
;
6718 (*pdb_method
)->enum_alias_memberships
= ldapsam_alias_memberships
;
6719 (*pdb_method
)->search_users
= ldapsam_search_users
;
6720 (*pdb_method
)->search_groups
= ldapsam_search_groups
;
6721 (*pdb_method
)->search_aliases
= ldapsam_search_aliases
;
6722 (*pdb_method
)->is_responsible_for_wellknown
=
6723 ldapsam_is_responsible_for_wellknown
;
6725 if (lp_parm_bool(-1, "ldapsam", "trusted", False
)) {
6726 (*pdb_method
)->enum_group_members
= ldapsam_enum_group_members
;
6727 (*pdb_method
)->enum_group_memberships
=
6728 ldapsam_enum_group_memberships
;
6729 (*pdb_method
)->lookup_rids
= ldapsam_lookup_rids
;
6730 (*pdb_method
)->sid_to_id
= ldapsam_sid_to_id
;
6731 (*pdb_method
)->id_to_sid
= ldapsam_id_to_sid
;
6733 if (lp_parm_bool(-1, "ldapsam", "editposix", False
)) {
6734 (*pdb_method
)->create_user
= ldapsam_create_user
;
6735 (*pdb_method
)->delete_user
= ldapsam_delete_user
;
6736 (*pdb_method
)->create_dom_group
= ldapsam_create_dom_group
;
6737 (*pdb_method
)->delete_dom_group
= ldapsam_delete_dom_group
;
6738 (*pdb_method
)->add_groupmem
= ldapsam_add_groupmem
;
6739 (*pdb_method
)->del_groupmem
= ldapsam_del_groupmem
;
6740 (*pdb_method
)->set_unix_primary_group
= ldapsam_set_primary_group
;
6744 ldap_state
= (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
6745 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
6747 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6749 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
,
6751 ldap_state
->domain_name
, True
);
6753 if ( !NT_STATUS_IS_OK(nt_status
) ) {
6754 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6755 "info, nor add one to the domain. "
6756 "We cannot work reliably without it.\n"));
6757 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
6760 /* Given that the above might fail, everything below this must be
6763 entry
= ldap_first_entry(smbldap_get_ldap(ldap_state
->smbldap_state
),
6766 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6768 ldap_msgfree(result
);
6769 return NT_STATUS_UNSUCCESSFUL
;
6772 dn
= smbldap_talloc_dn(talloc_tos(),
6773 smbldap_get_ldap(ldap_state
->smbldap_state
),
6776 ldap_msgfree(result
);
6777 return NT_STATUS_UNSUCCESSFUL
;
6780 ldap_state
->domain_dn
= smb_xstrdup(dn
);
6783 domain_sid_string
= smbldap_talloc_single_attribute(
6784 smbldap_get_ldap(ldap_state
->smbldap_state
),
6786 get_userattr_key2string(ldap_state
->schema_ver
,
6787 LDAP_ATTR_USER_SID
),
6790 if (domain_sid_string
) {
6792 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
6793 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6794 "read as a valid SID\n", domain_sid_string
));
6795 ldap_msgfree(result
);
6796 TALLOC_FREE(domain_sid_string
);
6797 return NT_STATUS_INVALID_PARAMETER
;
6799 found_sid
= PDB_secrets_fetch_domain_sid(ldap_state
->domain_name
,
6800 &secrets_domain_sid
);
6801 if (!found_sid
|| !dom_sid_equal(&secrets_domain_sid
,
6802 &ldap_domain_sid
)) {
6803 struct dom_sid_buf buf1
, buf2
;
6804 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6805 "%s based on pdb_ldap results %s -> %s\n",
6806 ldap_state
->domain_name
,
6807 dom_sid_str_buf(&secrets_domain_sid
, &buf1
),
6808 dom_sid_str_buf(&ldap_domain_sid
, &buf2
)));
6810 /* reset secrets.tdb sid */
6811 PDB_secrets_store_domain_sid(ldap_state
->domain_name
,
6813 DEBUG(1, ("New global sam SID: %s\n",
6814 dom_sid_str_buf(get_global_sam_sid(),
6817 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
6818 TALLOC_FREE(domain_sid_string
);
6821 alg_rid_base_string
= smbldap_talloc_single_attribute(
6822 smbldap_get_ldap(ldap_state
->smbldap_state
),
6824 get_attr_key2string( dominfo_attr_list
,
6825 LDAP_ATTR_ALGORITHMIC_RID_BASE
),
6827 if (alg_rid_base_string
) {
6828 alg_rid_base
= (uint32_t)atol(alg_rid_base_string
);
6829 if (alg_rid_base
!= algorithmic_rid_base()) {
6830 DEBUG(0, ("The value of 'algorithmic RID base' has "
6831 "changed since the LDAP\n"
6832 "database was initialised. Aborting. \n"));
6833 ldap_msgfree(result
);
6834 TALLOC_FREE(alg_rid_base_string
);
6835 return NT_STATUS_UNSUCCESSFUL
;
6837 TALLOC_FREE(alg_rid_base_string
);
6839 ldap_msgfree(result
);
6841 return NT_STATUS_OK
;
6844 NTSTATUS
pdb_ldapsam_init(TALLOC_CTX
*ctx
)
6848 nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
,
6850 pdb_ldapsam_init_common
);
6851 if (!NT_STATUS_IS_OK(nt_status
)) {
6855 /* Let pdb_nds register backends */
6858 return NT_STATUS_OK
;