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"
57 #define DBGC_CLASS DBGC_PASSDB
64 #include "passdb/pdb_ldap.h"
65 #include "passdb/pdb_nds.h"
66 #include "passdb/pdb_ipa.h"
67 #include "passdb/pdb_ldap_util.h"
68 #include "passdb/pdb_ldap_schema.h"
70 /**********************************************************************
71 Simple helper function to make stuff better readable
72 **********************************************************************/
74 LDAP *priv2ld(struct ldapsam_privates *priv)
76 return priv->smbldap_state->ldap_struct;
79 /**********************************************************************
80 Get the attribute name given a user schame version.
81 **********************************************************************/
83 static const char* get_userattr_key2string( int schema_ver, int key )
85 switch ( schema_ver ) {
86 case SCHEMAVER_SAMBAACCOUNT:
87 return get_attr_key2string( attrib_map_v22, key );
89 case SCHEMAVER_SAMBASAMACCOUNT:
90 return get_attr_key2string( attrib_map_v30, key );
93 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
99 /**********************************************************************
100 Return the list of attribute names given a user schema version.
101 **********************************************************************/
103 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
105 switch ( schema_ver ) {
106 case SCHEMAVER_SAMBAACCOUNT:
107 return get_attr_list( mem_ctx, attrib_map_v22 );
109 case SCHEMAVER_SAMBASAMACCOUNT:
110 return get_attr_list( mem_ctx, attrib_map_v30 );
112 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
119 /**************************************************************************
120 Return the list of attribute names to delete given a user schema version.
121 **************************************************************************/
123 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
126 switch ( schema_ver ) {
127 case SCHEMAVER_SAMBAACCOUNT:
128 return get_attr_list( mem_ctx,
129 attrib_map_to_delete_v22 );
131 case SCHEMAVER_SAMBASAMACCOUNT:
132 return get_attr_list( mem_ctx,
133 attrib_map_to_delete_v30 );
135 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
143 /*******************************************************************
144 Generate the LDAP search filter for the objectclass based on the
145 version of the schema we are using.
146 ******************************************************************/
148 static const char* get_objclass_filter( int schema_ver )
150 fstring objclass_filter;
153 switch( schema_ver ) {
154 case SCHEMAVER_SAMBAACCOUNT:
155 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
157 case SCHEMAVER_SAMBASAMACCOUNT:
158 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
161 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
162 objclass_filter[0] = '\0';
166 result = talloc_strdup(talloc_tos(), objclass_filter);
167 SMB_ASSERT(result != NULL);
171 /*****************************************************************
172 Scan a sequence number off OpenLDAP's syncrepl contextCSN
173 ******************************************************************/
175 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
177 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
178 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
179 LDAPMessage *msg = NULL;
180 LDAPMessage *entry = NULL;
182 char **values = NULL;
183 int rc, num_result, num_values, rid;
189 /* Unfortunatly there is no proper way to detect syncrepl-support in
190 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
191 * but do not show up in the root-DSE yet. Neither we can query the
192 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
193 * objectclass. Currently we require lp_ldap_suffix() to show up as
194 * namingContext. - Guenther
197 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
202 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
206 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
207 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
208 "as top-level namingContext\n", lp_ldap_suffix()));
212 mem_ctx = talloc_init("ldapsam_get_seq_num");
215 return NT_STATUS_NO_MEMORY;
217 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
218 ntstatus = NT_STATUS_NO_MEMORY;
222 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
223 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
226 /* consumer syncreplCookie: */
227 /* csn=20050126161620Z#0000001#00#00000 */
228 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
230 suffix = talloc_asprintf(mem_ctx,
231 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
233 ntstatus = NT_STATUS_NO_MEMORY;
238 /* provider contextCSN */
239 /* 20050126161620Z#000009#00#000000 */
240 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
242 suffix = talloc_asprintf(mem_ctx,
243 "cn=ldapsync,%s", lp_ldap_suffix());
246 ntstatus = NT_STATUS_NO_MEMORY;
251 rc = smbldap_search(ldap_state->smbldap_state, suffix,
252 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
254 if (rc != LDAP_SUCCESS) {
258 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
259 if (num_result != 1) {
260 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
264 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
266 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
270 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
271 if (values == NULL) {
272 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
276 num_values = ldap_count_values(values);
277 if (num_values == 0) {
278 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
283 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
284 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
289 if (!strncmp(p, "csn=", strlen("csn=")))
292 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
294 *seq_num = generalized_to_unix_time(p);
296 /* very basic sanity check */
298 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
303 ntstatus = NT_STATUS_OK;
307 ldap_value_free(values);
311 talloc_destroy(mem_ctx);
316 /*******************************************************************
317 Run the search by name.
318 ******************************************************************/
320 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
322 LDAPMessage ** result,
326 char *escape_user = escape_ldap_string(talloc_tos(), user);
330 return LDAP_NO_MEMORY;
334 * in the filter expression, replace %u with the real name
335 * so in ldap filter, %u MUST exist :-)
337 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
338 get_objclass_filter(ldap_state->schema_ver));
340 TALLOC_FREE(escape_user);
341 return LDAP_NO_MEMORY;
344 * have to use this here because $ is filtered out
348 filter = talloc_all_string_sub(talloc_tos(),
349 filter, "%u", escape_user);
350 TALLOC_FREE(escape_user);
352 return LDAP_NO_MEMORY;
355 ret = smbldap_search_suffix(ldap_state->smbldap_state,
356 filter, attr, result);
361 /*******************************************************************
362 Run the search by rid.
363 ******************************************************************/
365 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
366 uint32_t rid, LDAPMessage ** result,
372 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
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);
384 /*******************************************************************
385 Run the search by SID.
386 ******************************************************************/
388 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
389 const struct dom_sid *sid, LDAPMessage ** result,
396 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
397 get_userattr_key2string(ldap_state->schema_ver,
399 sid_to_fstring(sid_string, sid),
400 get_objclass_filter(ldap_state->schema_ver));
402 return LDAP_NO_MEMORY;
405 rc = smbldap_search_suffix(ldap_state->smbldap_state,
406 filter, attr, result);
412 /*******************************************************************
413 Delete complete object or objectclass and attrs from
414 object found in search_result depending on lp_ldap_delete_dn
415 ******************************************************************/
417 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
420 const char *objectclass,
423 LDAPMod **mods = NULL;
426 BerElement *ptr = NULL;
428 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
430 return LDAP_NO_MEMORY;
433 if (lp_ldap_delete_dn()) {
434 return smbldap_delete(priv->smbldap_state, dn);
437 /* Ok, delete only the SAM attributes */
439 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
441 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
444 /* We are only allowed to delete the attributes that
447 for (attrib = attrs; *attrib != NULL; attrib++) {
448 if (strequal(*attrib, name)) {
449 DEBUG(10, ("ldapsam_delete_entry: deleting "
450 "attribute %s\n", name));
451 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
462 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
463 talloc_autofree_ldapmod(mem_ctx, mods);
465 return smbldap_modify(priv->smbldap_state, dn, mods);
468 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
473 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
474 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
480 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
481 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
491 /**********************************************************************
492 Initialize struct samu from an LDAP query.
493 (Based on init_sam_from_buffer in pdb_tdb.c)
494 *********************************************************************/
496 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
497 struct samu * sampass,
504 pass_can_change_time,
507 char *username = NULL,
513 *logon_script = NULL,
514 *profile_path = NULL,
516 *workstations = NULL,
519 uint8 smblmpwd[LM_HASH_LEN],
520 smbntpwd[NT_HASH_LEN];
521 bool use_samba_attrs = True;
522 uint32_t acct_ctrl = 0;
524 uint16_t bad_password_count = 0,
527 uint8 hours[MAX_HOURS_LEN];
529 struct login_cache cache_entry;
531 bool expand_explicit = lp_passdb_expand_explicit();
533 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
538 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
539 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
543 if (priv2ld(ldap_state) == NULL) {
544 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
545 "ldap_struct is NULL!\n"));
549 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
553 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
558 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
560 nt_username = talloc_strdup(ctx, username);
565 domain = talloc_strdup(ctx, ldap_state->domain_name);
570 pdb_set_username(sampass, username, PDB_SET);
572 pdb_set_domain(sampass, domain, PDB_DEFAULT);
573 pdb_set_nt_username(sampass, nt_username, PDB_SET);
575 /* deal with different attributes between the schema first */
577 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
578 if ((temp = smbldap_talloc_single_attribute(
579 ldap_state->smbldap_state->ldap_struct,
581 get_userattr_key2string(ldap_state->schema_ver,
584 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
587 if ((temp = smbldap_talloc_single_attribute(
588 ldap_state->smbldap_state->ldap_struct,
590 get_userattr_key2string(ldap_state->schema_ver,
593 user_rid = (uint32_t)atol(temp);
594 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
598 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
599 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
600 get_userattr_key2string(ldap_state->schema_ver,
602 get_userattr_key2string(ldap_state->schema_ver,
608 temp = smbldap_talloc_single_attribute(
609 ldap_state->smbldap_state->ldap_struct,
611 get_userattr_key2string(ldap_state->schema_ver,
612 LDAP_ATTR_PWD_LAST_SET),
615 pass_last_set_time = (time_t) atol(temp);
616 pdb_set_pass_last_set_time(sampass,
617 pass_last_set_time, PDB_SET);
620 temp = smbldap_talloc_single_attribute(
621 ldap_state->smbldap_state->ldap_struct,
623 get_userattr_key2string(ldap_state->schema_ver,
624 LDAP_ATTR_LOGON_TIME),
627 logon_time = (time_t) atol(temp);
628 pdb_set_logon_time(sampass, logon_time, PDB_SET);
631 temp = smbldap_talloc_single_attribute(
632 ldap_state->smbldap_state->ldap_struct,
634 get_userattr_key2string(ldap_state->schema_ver,
635 LDAP_ATTR_LOGOFF_TIME),
638 logoff_time = (time_t) atol(temp);
639 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
642 temp = smbldap_talloc_single_attribute(
643 ldap_state->smbldap_state->ldap_struct,
645 get_userattr_key2string(ldap_state->schema_ver,
646 LDAP_ATTR_KICKOFF_TIME),
649 kickoff_time = (time_t) atol(temp);
650 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
653 temp = smbldap_talloc_single_attribute(
654 ldap_state->smbldap_state->ldap_struct,
656 get_userattr_key2string(ldap_state->schema_ver,
657 LDAP_ATTR_PWD_CAN_CHANGE),
660 pass_can_change_time = (time_t) atol(temp);
661 pdb_set_pass_can_change_time(sampass,
662 pass_can_change_time, PDB_SET);
665 /* recommend that 'gecos' and 'displayName' should refer to the same
666 * attribute OID. userFullName depreciated, only used by Samba
667 * primary rules of LDAP: don't make a new attribute when one is already defined
668 * that fits your needs; using cn then displayName rather than 'userFullName'
671 fullname = smbldap_talloc_single_attribute(
672 ldap_state->smbldap_state->ldap_struct,
674 get_userattr_key2string(ldap_state->schema_ver,
675 LDAP_ATTR_DISPLAY_NAME),
678 pdb_set_fullname(sampass, fullname, PDB_SET);
680 fullname = smbldap_talloc_single_attribute(
681 ldap_state->smbldap_state->ldap_struct,
683 get_userattr_key2string(ldap_state->schema_ver,
687 pdb_set_fullname(sampass, fullname, PDB_SET);
691 dir_drive = smbldap_talloc_single_attribute(
692 ldap_state->smbldap_state->ldap_struct,
694 get_userattr_key2string(ldap_state->schema_ver,
695 LDAP_ATTR_HOME_DRIVE),
698 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
700 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
703 homedir = smbldap_talloc_single_attribute(
704 ldap_state->smbldap_state->ldap_struct,
706 get_userattr_key2string(ldap_state->schema_ver,
707 LDAP_ATTR_HOME_PATH),
710 if (expand_explicit) {
711 homedir = talloc_sub_basic(ctx,
719 pdb_set_homedir(sampass, homedir, PDB_SET);
721 pdb_set_homedir(sampass,
722 talloc_sub_basic(ctx, username, domain,
727 logon_script = smbldap_talloc_single_attribute(
728 ldap_state->smbldap_state->ldap_struct,
730 get_userattr_key2string(ldap_state->schema_ver,
731 LDAP_ATTR_LOGON_SCRIPT),
734 if (expand_explicit) {
735 logon_script = talloc_sub_basic(ctx,
743 pdb_set_logon_script(sampass, logon_script, PDB_SET);
745 pdb_set_logon_script(sampass,
746 talloc_sub_basic(ctx, username, domain,
751 profile_path = smbldap_talloc_single_attribute(
752 ldap_state->smbldap_state->ldap_struct,
754 get_userattr_key2string(ldap_state->schema_ver,
755 LDAP_ATTR_PROFILE_PATH),
758 if (expand_explicit) {
759 profile_path = talloc_sub_basic(ctx,
767 pdb_set_profile_path(sampass, profile_path, PDB_SET);
769 pdb_set_profile_path(sampass,
770 talloc_sub_basic(ctx, username, domain,
775 acct_desc = smbldap_talloc_single_attribute(
776 ldap_state->smbldap_state->ldap_struct,
778 get_userattr_key2string(ldap_state->schema_ver,
782 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
785 workstations = smbldap_talloc_single_attribute(
786 ldap_state->smbldap_state->ldap_struct,
788 get_userattr_key2string(ldap_state->schema_ver,
792 pdb_set_workstations(sampass, workstations, PDB_SET);
795 munged_dial = smbldap_talloc_single_attribute(
796 ldap_state->smbldap_state->ldap_struct,
798 get_userattr_key2string(ldap_state->schema_ver,
799 LDAP_ATTR_MUNGED_DIAL),
802 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
805 /* FIXME: hours stuff should be cleaner */
809 memset(hours, 0xff, hours_len);
811 if (ldap_state->is_nds_ldap) {
814 char clear_text_pw[512];
816 /* Make call to Novell eDirectory ldap extension to get clear text password.
817 NOTE: This will only work if we have an SSL connection to eDirectory. */
818 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
819 if (user_dn != NULL) {
820 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
822 pwd_len = sizeof(clear_text_pw);
823 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
824 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
825 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
826 TALLOC_FREE(user_dn);
829 ZERO_STRUCT(smblmpwd);
830 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
831 TALLOC_FREE(user_dn);
834 ZERO_STRUCT(smbntpwd);
835 use_samba_attrs = False;
838 TALLOC_FREE(user_dn);
841 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
845 if (use_samba_attrs) {
846 temp = smbldap_talloc_single_attribute(
847 ldap_state->smbldap_state->ldap_struct,
849 get_userattr_key2string(ldap_state->schema_ver,
853 pdb_gethexpwd(temp, smblmpwd);
854 memset((char *)temp, '\0', strlen(temp)+1);
855 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
858 ZERO_STRUCT(smblmpwd);
861 temp = smbldap_talloc_single_attribute(
862 ldap_state->smbldap_state->ldap_struct,
864 get_userattr_key2string(ldap_state->schema_ver,
868 pdb_gethexpwd(temp, smbntpwd);
869 memset((char *)temp, '\0', strlen(temp)+1);
870 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
873 ZERO_STRUCT(smbntpwd);
879 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
881 uint8 *pwhist = NULL;
883 char *history_string = talloc_array(ctx, char,
884 MAX_PW_HISTORY_LEN*64);
886 if (!history_string) {
890 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
892 pwhist = talloc_array(ctx, uint8,
893 pwHistLen * PW_HISTORY_ENTRY_LEN);
894 if (pwhist == NULL) {
895 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
898 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
900 if (smbldap_get_single_attribute(
901 ldap_state->smbldap_state->ldap_struct,
903 get_userattr_key2string(ldap_state->schema_ver,
904 LDAP_ATTR_PWD_HISTORY),
906 MAX_PW_HISTORY_LEN*64)) {
907 bool hex_failed = false;
908 for (i = 0; i < pwHistLen; i++){
909 /* Get the 16 byte salt. */
910 if (!pdb_gethexpwd(&history_string[i*64],
911 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
915 /* Get the 16 byte MD5 hash of salt+passwd. */
916 if (!pdb_gethexpwd(&history_string[(i*64)+32],
917 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
918 PW_HISTORY_SALT_LEN])) {
924 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
926 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
929 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
934 temp = smbldap_talloc_single_attribute(
935 ldap_state->smbldap_state->ldap_struct,
937 get_userattr_key2string(ldap_state->schema_ver,
941 acct_ctrl = pdb_decode_acct_ctrl(temp);
943 if (acct_ctrl == 0) {
944 acct_ctrl |= ACB_NORMAL;
947 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
949 acct_ctrl |= ACB_NORMAL;
952 pdb_set_hours_len(sampass, hours_len, PDB_SET);
953 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
955 temp = smbldap_talloc_single_attribute(
956 ldap_state->smbldap_state->ldap_struct,
958 get_userattr_key2string(ldap_state->schema_ver,
959 LDAP_ATTR_BAD_PASSWORD_COUNT),
962 bad_password_count = (uint32_t) atol(temp);
963 pdb_set_bad_password_count(sampass,
964 bad_password_count, PDB_SET);
967 temp = smbldap_talloc_single_attribute(
968 ldap_state->smbldap_state->ldap_struct,
970 get_userattr_key2string(ldap_state->schema_ver,
971 LDAP_ATTR_BAD_PASSWORD_TIME),
974 bad_password_time = (time_t) atol(temp);
975 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
979 temp = smbldap_talloc_single_attribute(
980 ldap_state->smbldap_state->ldap_struct,
982 get_userattr_key2string(ldap_state->schema_ver,
983 LDAP_ATTR_LOGON_COUNT),
986 logon_count = (uint32_t) atol(temp);
987 pdb_set_logon_count(sampass, logon_count, PDB_SET);
990 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
992 temp = smbldap_talloc_single_attribute(
993 ldap_state->smbldap_state->ldap_struct,
995 get_userattr_key2string(ldap_state->schema_ver,
996 LDAP_ATTR_LOGON_HOURS),
999 pdb_gethexhours(temp, hours);
1000 memset((char *)temp, '\0', strlen(temp) +1);
1001 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
1005 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1006 struct passwd unix_pw;
1007 bool have_uid = false;
1008 bool have_gid = false;
1009 struct dom_sid mapped_gsid;
1010 const struct dom_sid *primary_gsid;
1012 ZERO_STRUCT(unix_pw);
1014 unix_pw.pw_name = username;
1015 unix_pw.pw_passwd = discard_const_p(char, "x");
1017 temp = smbldap_talloc_single_attribute(
1018 priv2ld(ldap_state),
1023 /* We've got a uid, feed the cache */
1024 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1027 temp = smbldap_talloc_single_attribute(
1028 priv2ld(ldap_state),
1033 /* We've got a uid, feed the cache */
1034 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1037 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1038 priv2ld(ldap_state),
1042 if (unix_pw.pw_gecos) {
1043 unix_pw.pw_gecos = fullname;
1045 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1046 priv2ld(ldap_state),
1050 if (unix_pw.pw_dir) {
1051 unix_pw.pw_dir = discard_const_p(char, "");
1053 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1054 priv2ld(ldap_state),
1058 if (unix_pw.pw_shell) {
1059 unix_pw.pw_shell = discard_const_p(char, "");
1062 if (have_uid && have_gid) {
1063 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1065 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1068 if (sampass->unix_pw == NULL) {
1069 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1070 pdb_get_username(sampass)));
1074 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
1075 sampass->unix_pw->pw_uid);
1077 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1078 primary_gsid = pdb_get_group_sid(sampass);
1079 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1080 idmap_cache_set_sid2gid(primary_gsid,
1081 sampass->unix_pw->pw_gid);
1085 /* check the timestamp of the cache vs ldap entry */
1086 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1092 /* see if we have newer updates */
1093 if (!login_cache_read(sampass, &cache_entry)) {
1094 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1095 (unsigned int)pdb_get_bad_password_count(sampass),
1096 (unsigned int)pdb_get_bad_password_time(sampass)));
1101 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1102 (unsigned int)ldap_entry_time,
1103 (unsigned int)cache_entry.entry_timestamp,
1104 (unsigned int)cache_entry.bad_password_time));
1106 if (ldap_entry_time > cache_entry.entry_timestamp) {
1107 /* cache is older than directory , so
1108 we need to delete the entry but allow the
1109 fields to be written out */
1110 login_cache_delentry(sampass);
1113 pdb_set_acct_ctrl(sampass,
1114 pdb_get_acct_ctrl(sampass) |
1115 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1117 pdb_set_bad_password_count(sampass,
1118 cache_entry.bad_password_count,
1120 pdb_set_bad_password_time(sampass,
1121 cache_entry.bad_password_time,
1133 /**********************************************************************
1134 Initialize the ldap db from a struct samu. Called on update.
1135 (Based on init_buffer_from_sam in pdb_tdb.c)
1136 *********************************************************************/
1138 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1139 LDAPMessage *existing,
1140 LDAPMod *** mods, struct samu * sampass,
1141 bool (*need_update)(const struct samu *,
1147 if (mods == NULL || sampass == NULL) {
1148 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1155 * took out adding "objectclass: sambaAccount"
1156 * do this on a per-mod basis
1158 if (need_update(sampass, PDB_USERNAME)) {
1159 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1160 "uid", pdb_get_username(sampass));
1161 if (ldap_state->is_nds_ldap) {
1162 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1163 "cn", pdb_get_username(sampass));
1164 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
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)) {
1174 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1176 switch ( ldap_state->schema_ver ) {
1177 case SCHEMAVER_SAMBAACCOUNT:
1178 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1179 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1180 sid_string_dbg(user_sid),
1182 &ldap_state->domain_sid)));
1185 if (asprintf(&temp, "%i", rid) < 0) {
1188 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1189 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1194 case SCHEMAVER_SAMBASAMACCOUNT:
1195 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1196 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1197 sid_to_fstring(sid_string, user_sid));
1201 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1206 /* we don't need to store the primary group RID - so leaving it
1207 'free' to hang off the unix primary group makes life easier */
1209 if (need_update(sampass, PDB_GROUPSID)) {
1211 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1213 switch ( ldap_state->schema_ver ) {
1214 case SCHEMAVER_SAMBAACCOUNT:
1215 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1216 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1217 sid_string_dbg(group_sid),
1219 &ldap_state->domain_sid)));
1223 if (asprintf(&temp, "%i", rid) < 0) {
1226 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1227 get_userattr_key2string(ldap_state->schema_ver,
1228 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1232 case SCHEMAVER_SAMBASAMACCOUNT:
1233 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1234 get_userattr_key2string(ldap_state->schema_ver,
1235 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1239 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1245 /* displayName, cn, and gecos should all be the same
1246 * most easily accomplished by giving them the same OID
1247 * gecos isn't set here b/c it should be handled by the
1249 * We change displayName only and fall back to cn if
1250 * it does not exist.
1253 if (need_update(sampass, PDB_FULLNAME))
1254 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1255 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1256 pdb_get_fullname(sampass));
1258 if (need_update(sampass, PDB_ACCTDESC))
1259 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1260 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1261 pdb_get_acct_desc(sampass));
1263 if (need_update(sampass, PDB_WORKSTATIONS))
1264 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1265 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1266 pdb_get_workstations(sampass));
1268 if (need_update(sampass, PDB_MUNGEDDIAL))
1269 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1270 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1271 pdb_get_munged_dial(sampass));
1273 if (need_update(sampass, PDB_SMBHOME))
1274 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1275 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1276 pdb_get_homedir(sampass));
1278 if (need_update(sampass, PDB_DRIVE))
1279 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1280 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1281 pdb_get_dir_drive(sampass));
1283 if (need_update(sampass, PDB_LOGONSCRIPT))
1284 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1285 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1286 pdb_get_logon_script(sampass));
1288 if (need_update(sampass, PDB_PROFILE))
1289 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1290 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1291 pdb_get_profile_path(sampass));
1293 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1296 if (need_update(sampass, PDB_LOGONTIME))
1297 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1298 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1301 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1304 if (need_update(sampass, PDB_LOGOFFTIME))
1305 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1306 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1309 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1312 if (need_update(sampass, PDB_KICKOFFTIME))
1313 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1314 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1317 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1320 if (need_update(sampass, PDB_CANCHANGETIME))
1321 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1322 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1325 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1326 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1328 if (need_update(sampass, PDB_LMPASSWD)) {
1329 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1332 pdb_sethexpwd(pwstr, lm_pw,
1333 pdb_get_acct_ctrl(sampass));
1334 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1335 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1338 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1339 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1343 if (need_update(sampass, PDB_NTPASSWD)) {
1344 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1347 pdb_sethexpwd(pwstr, nt_pw,
1348 pdb_get_acct_ctrl(sampass));
1349 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1350 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1353 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1354 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1359 if (need_update(sampass, PDB_PWHISTORY)) {
1361 uint32_t pwHistLen = 0;
1362 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1364 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1368 if (pwHistLen == 0) {
1369 /* Remove any password history from the LDAP store. */
1370 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1374 uint32_t currHistLen = 0;
1375 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1376 if (pwhist != NULL) {
1377 /* We can only store (1024-1/64 password history entries. */
1378 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1379 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1380 /* Store the salt. */
1381 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1382 /* Followed by the md5 hash of salt + md4 hash */
1383 pdb_sethexpwd(&pwstr[(i*64)+32],
1384 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1385 DEBUG(100, ("pwstr=%s\n", pwstr));
1389 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1390 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1395 if (need_update(sampass, PDB_PASSLASTSET)) {
1396 if (asprintf(&temp, "%li",
1397 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1400 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1401 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1407 if (need_update(sampass, PDB_HOURS)) {
1408 const uint8 *hours = pdb_get_hours(sampass);
1411 pdb_sethexhours(hourstr, hours);
1412 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1415 get_userattr_key2string(ldap_state->schema_ver,
1416 LDAP_ATTR_LOGON_HOURS),
1421 if (need_update(sampass, PDB_ACCTCTRL))
1422 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1423 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1424 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1426 /* password lockout cache:
1427 - If we are now autolocking or clearing, we write to ldap
1428 - If we are clearing, we delete the cache entry
1429 - If the count is > 0, we update the cache
1431 This even means when autolocking, we cache, just in case the
1432 update doesn't work, and we have to cache the autolock flag */
1434 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1435 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1436 uint16_t badcount = pdb_get_bad_password_count(sampass);
1437 time_t badtime = pdb_get_bad_password_time(sampass);
1439 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1441 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1442 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1444 if ((badcount >= pol) || (badcount == 0)) {
1445 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1446 (unsigned int)badcount, (unsigned int)badtime));
1447 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1451 ldap_state->smbldap_state->ldap_struct,
1453 get_userattr_key2string(
1454 ldap_state->schema_ver,
1455 LDAP_ATTR_BAD_PASSWORD_COUNT),
1459 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1463 ldap_state->smbldap_state->ldap_struct,
1465 get_userattr_key2string(
1466 ldap_state->schema_ver,
1467 LDAP_ATTR_BAD_PASSWORD_TIME),
1471 if (badcount == 0) {
1472 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1473 login_cache_delentry(sampass);
1475 struct login_cache cache_entry;
1477 cache_entry.entry_timestamp = time(NULL);
1478 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1479 cache_entry.bad_password_count = badcount;
1480 cache_entry.bad_password_time = badtime;
1482 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1483 login_cache_write(sampass, &cache_entry);
1490 /**********************************************************************
1491 End enumeration of the LDAP password list.
1492 *********************************************************************/
1494 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1496 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1497 if (ldap_state->result) {
1498 ldap_msgfree(ldap_state->result);
1499 ldap_state->result = NULL;
1503 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1504 const char *new_attr)
1508 if (new_attr == NULL) {
1512 for (i=0; (*attr_list)[i] != NULL; i++) {
1516 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1518 SMB_ASSERT((*attr_list) != NULL);
1519 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1520 (*attr_list)[i+1] = NULL;
1523 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1524 const char ***attr_list)
1526 append_attr(mem_ctx, attr_list, "uidNumber");
1527 append_attr(mem_ctx, attr_list, "gidNumber");
1528 append_attr(mem_ctx, attr_list, "homeDirectory");
1529 append_attr(mem_ctx, attr_list, "loginShell");
1530 append_attr(mem_ctx, attr_list, "gecos");
1533 /**********************************************************************
1534 Get struct samu entry from LDAP by username.
1535 *********************************************************************/
1537 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1539 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1540 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1541 LDAPMessage *result = NULL;
1542 LDAPMessage *entry = NULL;
1544 const char ** attr_list;
1547 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1548 append_attr(user, &attr_list,
1549 get_userattr_key2string(ldap_state->schema_ver,
1550 LDAP_ATTR_MOD_TIMESTAMP));
1551 ldapsam_add_unix_attributes(user, &attr_list);
1552 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1554 TALLOC_FREE( attr_list );
1556 if ( rc != LDAP_SUCCESS )
1557 return NT_STATUS_NO_SUCH_USER;
1559 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1562 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1563 ldap_msgfree(result);
1564 return NT_STATUS_NO_SUCH_USER;
1565 } else if (count > 1) {
1566 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1567 ldap_msgfree(result);
1568 return NT_STATUS_NO_SUCH_USER;
1571 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1573 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1574 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1575 ldap_msgfree(result);
1576 return NT_STATUS_NO_SUCH_USER;
1578 pdb_set_backend_private_data(user, result, NULL,
1579 my_methods, PDB_CHANGED);
1580 talloc_autofree_ldapmsg(user, result);
1583 ldap_msgfree(result);
1588 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1589 const struct dom_sid *sid, LDAPMessage **result)
1592 const char ** attr_list;
1595 switch ( ldap_state->schema_ver ) {
1596 case SCHEMAVER_SAMBASAMACCOUNT: {
1597 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1598 if (tmp_ctx == NULL) {
1599 return LDAP_NO_MEMORY;
1602 attr_list = get_userattr_list(tmp_ctx,
1603 ldap_state->schema_ver);
1604 append_attr(tmp_ctx, &attr_list,
1605 get_userattr_key2string(
1606 ldap_state->schema_ver,
1607 LDAP_ATTR_MOD_TIMESTAMP));
1608 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1609 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1611 TALLOC_FREE(tmp_ctx);
1613 if ( rc != LDAP_SUCCESS )
1618 case SCHEMAVER_SAMBAACCOUNT:
1619 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1623 attr_list = get_userattr_list(NULL,
1624 ldap_state->schema_ver);
1625 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1626 TALLOC_FREE( attr_list );
1628 if ( rc != LDAP_SUCCESS )
1635 /**********************************************************************
1636 Get struct samu entry from LDAP by SID.
1637 *********************************************************************/
1639 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1641 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1642 LDAPMessage *result = NULL;
1643 LDAPMessage *entry = NULL;
1647 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1649 if (rc != LDAP_SUCCESS)
1650 return NT_STATUS_NO_SUCH_USER;
1652 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1655 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1656 "count=%d\n", sid_string_dbg(sid), count));
1657 ldap_msgfree(result);
1658 return NT_STATUS_NO_SUCH_USER;
1659 } else if (count > 1) {
1660 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1661 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1663 ldap_msgfree(result);
1664 return NT_STATUS_NO_SUCH_USER;
1667 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1669 ldap_msgfree(result);
1670 return NT_STATUS_NO_SUCH_USER;
1673 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1674 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1675 ldap_msgfree(result);
1676 return NT_STATUS_NO_SUCH_USER;
1679 pdb_set_backend_private_data(user, result, NULL,
1680 my_methods, PDB_CHANGED);
1681 talloc_autofree_ldapmsg(user, result);
1682 return NT_STATUS_OK;
1685 /********************************************************************
1686 Do the actual modification - also change a plaintext passord if
1688 **********************************************************************/
1690 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1691 struct samu *newpwd, char *dn,
1692 LDAPMod **mods, int ldap_op,
1693 bool (*need_update)(const struct samu *, enum pdb_elements))
1695 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1698 if (!newpwd || !dn) {
1699 return NT_STATUS_INVALID_PARAMETER;
1702 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1703 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1704 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1705 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1708 char *retoid = NULL;
1709 struct berval *retdata = NULL;
1710 char *utf8_password;
1712 size_t converted_size;
1715 if (!ldap_state->is_nds_ldap) {
1717 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1718 LDAP_EXOP_MODIFY_PASSWD)) {
1719 DEBUG(2, ("ldap password change requested, but LDAP "
1720 "server does not support it -- ignoring\n"));
1721 return NT_STATUS_OK;
1725 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1726 pdb_get_plaintext_passwd(newpwd),
1729 return NT_STATUS_NO_MEMORY;
1732 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1733 TALLOC_FREE(utf8_password);
1734 return NT_STATUS_NO_MEMORY;
1737 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1738 DEBUG(0,("ber_alloc_t returns NULL\n"));
1739 TALLOC_FREE(utf8_password);
1740 TALLOC_FREE(utf8_dn);
1741 return NT_STATUS_UNSUCCESSFUL;
1744 if ((ber_printf (ber, "{") < 0) ||
1745 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1747 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1750 TALLOC_FREE(utf8_dn);
1751 TALLOC_FREE(utf8_password);
1752 return NT_STATUS_UNSUCCESSFUL;
1755 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1756 ret = ber_printf(ber, "ts}",
1757 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1760 ret = ber_printf(ber, "}");
1764 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1767 TALLOC_FREE(utf8_dn);
1768 TALLOC_FREE(utf8_password);
1769 return NT_STATUS_UNSUCCESSFUL;
1772 if ((rc = ber_flatten (ber, &bv))<0) {
1773 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1775 TALLOC_FREE(utf8_dn);
1776 TALLOC_FREE(utf8_password);
1777 return NT_STATUS_UNSUCCESSFUL;
1780 TALLOC_FREE(utf8_dn);
1781 TALLOC_FREE(utf8_password);
1784 if (!ldap_state->is_nds_ldap) {
1785 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1786 LDAP_EXOP_MODIFY_PASSWD,
1787 bv, NULL, NULL, &retoid,
1790 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1791 pdb_get_plaintext_passwd(newpwd));
1793 if (rc != LDAP_SUCCESS) {
1794 char *ld_error = NULL;
1796 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1797 DEBUG(3, ("Could not set userPassword "
1798 "attribute due to an objectClass "
1799 "violation -- ignoring\n"));
1801 return NT_STATUS_OK;
1804 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1806 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1807 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1808 SAFE_FREE(ld_error);
1810 #if defined(LDAP_CONSTRAINT_VIOLATION)
1811 if (rc == LDAP_CONSTRAINT_VIOLATION)
1812 return NT_STATUS_PASSWORD_RESTRICTION;
1814 return NT_STATUS_UNSUCCESSFUL;
1816 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1817 #ifdef DEBUG_PASSWORD
1818 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1821 ber_bvfree(retdata);
1823 ldap_memfree(retoid);
1829 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1830 /* may be password change below however */
1834 if (ldap_state->is_nds_ldap) {
1835 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1839 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1843 rc = smbldap_add(ldap_state->smbldap_state,
1846 case LDAP_MOD_REPLACE:
1847 rc = smbldap_modify(ldap_state->smbldap_state,
1851 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1853 return NT_STATUS_INVALID_PARAMETER;
1856 if (rc!=LDAP_SUCCESS) {
1857 return NT_STATUS_UNSUCCESSFUL;
1861 return NT_STATUS_OK;
1864 /**********************************************************************
1865 Delete entry from LDAP for username.
1866 *********************************************************************/
1868 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1869 struct samu * sam_acct)
1871 struct ldapsam_privates *priv =
1872 (struct ldapsam_privates *)my_methods->private_data;
1875 LDAPMessage *msg, *entry;
1876 NTSTATUS result = NT_STATUS_NO_MEMORY;
1877 const char **attr_list;
1878 TALLOC_CTX *mem_ctx;
1881 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1882 return NT_STATUS_INVALID_PARAMETER;
1885 sname = pdb_get_username(sam_acct);
1887 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1890 mem_ctx = talloc_new(NULL);
1891 if (mem_ctx == NULL) {
1892 DEBUG(0, ("talloc_new failed\n"));
1896 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1897 if (attr_list == NULL) {
1901 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1903 if ((rc != LDAP_SUCCESS) ||
1904 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1905 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1906 DEBUG(5, ("Could not find user %s\n", sname));
1907 result = NT_STATUS_NO_SUCH_USER;
1911 rc = ldapsam_delete_entry(
1912 priv, mem_ctx, entry,
1913 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1914 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1917 result = (rc == LDAP_SUCCESS) ?
1918 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1921 TALLOC_FREE(mem_ctx);
1925 /**********************************************************************
1927 *********************************************************************/
1929 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1931 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1932 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1935 LDAPMessage *result = NULL;
1936 LDAPMessage *entry = NULL;
1937 LDAPMod **mods = NULL;
1938 const char **attr_list;
1940 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1942 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1943 if (pdb_get_username(newpwd) == NULL) {
1944 return NT_STATUS_INVALID_PARAMETER;
1946 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1947 TALLOC_FREE( attr_list );
1948 if (rc != LDAP_SUCCESS) {
1949 return NT_STATUS_UNSUCCESSFUL;
1951 pdb_set_backend_private_data(newpwd, result, NULL,
1952 my_methods, PDB_CHANGED);
1953 talloc_autofree_ldapmsg(newpwd, result);
1956 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1957 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1958 return NT_STATUS_UNSUCCESSFUL;
1961 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1962 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1964 return NT_STATUS_UNSUCCESSFUL;
1967 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1969 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1970 pdb_element_is_changed)) {
1971 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1974 ldap_mods_free(mods,True);
1975 return NT_STATUS_UNSUCCESSFUL;
1978 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1979 && (mods == NULL)) {
1980 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1981 pdb_get_username(newpwd)));
1983 return NT_STATUS_OK;
1986 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1989 ldap_mods_free(mods,True);
1995 * We need to set the backend private data to NULL here. For example
1996 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1997 * same one, and with the explicit delete / add logic for attribute
1998 * values the second time we would use the wrong "old" value which
1999 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2001 * The existing LDAPMessage is still being auto-freed by the
2004 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2007 if (!NT_STATUS_IS_OK(ret)) {
2011 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2012 pdb_get_username(newpwd)));
2013 return NT_STATUS_OK;
2016 /***************************************************************************
2017 Renames a struct samu
2018 - The "rename user script" has full responsibility for changing everything
2019 ***************************************************************************/
2021 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2022 TALLOC_CTX *tmp_ctx,
2024 uint32_t member_rid);
2026 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2027 TALLOC_CTX *mem_ctx,
2029 struct dom_sid **pp_sids,
2031 uint32_t *p_num_groups);
2033 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2034 struct samu *old_acct,
2035 const char *newname)
2037 const char *oldname;
2039 char *rename_script = NULL;
2040 fstring oldname_lower, newname_lower;
2043 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2044 return NT_STATUS_INVALID_PARAMETER;
2047 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2048 return NT_STATUS_INVALID_PARAMETER;
2051 oldname = pdb_get_username(old_acct);
2053 /* rename the posix user */
2054 rename_script = talloc_strdup(talloc_tos(), lp_renameuser_script());
2055 if (rename_script == NULL) {
2056 return NT_STATUS_NO_MEMORY;
2059 if (!(*rename_script)) {
2060 TALLOC_FREE(rename_script);
2061 return NT_STATUS_ACCESS_DENIED;
2064 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2067 /* We have to allow the account name to end with a '$'.
2068 Also, follow the semantics in _samr_create_user() and lower case the
2069 posix name but preserve the case in passdb */
2071 fstrcpy( oldname_lower, oldname );
2072 strlower_m( oldname_lower );
2073 fstrcpy( newname_lower, newname );
2074 strlower_m( newname_lower );
2075 rename_script = realloc_string_sub2(rename_script,
2080 if (!rename_script) {
2081 return NT_STATUS_NO_MEMORY;
2083 rename_script = realloc_string_sub2(rename_script,
2088 rc = smbrun(rename_script, NULL);
2090 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2091 rename_script, rc));
2093 TALLOC_FREE(rename_script);
2096 smb_nscd_flush_user_cache();
2100 return NT_STATUS_UNSUCCESSFUL;
2102 return NT_STATUS_OK;
2105 /**********************************************************************
2106 Add struct samu to LDAP.
2107 *********************************************************************/
2109 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2111 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2112 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2114 LDAPMessage *result = NULL;
2115 LDAPMessage *entry = NULL;
2116 LDAPMod **mods = NULL;
2117 int ldap_op = LDAP_MOD_REPLACE;
2118 uint32_t num_result;
2119 const char **attr_list;
2120 char *escape_user = NULL;
2121 const char *username = pdb_get_username(newpwd);
2122 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2123 char *filter = NULL;
2125 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2126 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2129 return NT_STATUS_NO_MEMORY;
2132 if (!username || !*username) {
2133 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2134 status = NT_STATUS_INVALID_PARAMETER;
2138 /* free this list after the second search or in case we exit on failure */
2139 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2141 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2143 if (rc != LDAP_SUCCESS) {
2147 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2148 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2152 ldap_msgfree(result);
2155 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2156 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2158 if (rc == LDAP_SUCCESS) {
2159 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2160 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2161 "already in the base, with samba "
2162 "attributes\n", sid_string_dbg(sid)));
2165 ldap_msgfree(result);
2170 /* does the entry already exist but without a samba attributes?
2171 we need to return the samba attributes here */
2173 escape_user = escape_ldap_string(talloc_tos(), username);
2174 filter = talloc_strdup(attr_list, "(uid=%u)");
2176 status = NT_STATUS_NO_MEMORY;
2179 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2180 TALLOC_FREE(escape_user);
2182 status = NT_STATUS_NO_MEMORY;
2186 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2187 filter, attr_list, &result);
2188 if ( rc != LDAP_SUCCESS ) {
2192 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2194 if (num_result > 1) {
2195 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2199 /* Check if we need to update an existing entry */
2200 if (num_result == 1) {
2201 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2202 ldap_op = LDAP_MOD_REPLACE;
2203 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2204 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2206 status = NT_STATUS_NO_MEMORY;
2210 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2212 /* There might be a SID for this account already - say an idmap entry */
2214 filter = talloc_asprintf(ctx,
2215 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2216 get_userattr_key2string(ldap_state->schema_ver,
2217 LDAP_ATTR_USER_SID),
2218 sid_string_talloc(ctx, sid),
2219 LDAP_OBJ_IDMAP_ENTRY,
2220 LDAP_OBJ_SID_ENTRY);
2222 status = NT_STATUS_NO_MEMORY;
2226 /* free old result before doing a new search */
2227 if (result != NULL) {
2228 ldap_msgfree(result);
2231 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2232 filter, attr_list, &result);
2234 if ( rc != LDAP_SUCCESS ) {
2238 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2240 if (num_result > 1) {
2241 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2245 /* Check if we need to update an existing entry */
2246 if (num_result == 1) {
2248 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2249 ldap_op = LDAP_MOD_REPLACE;
2250 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2251 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2253 status = NT_STATUS_NO_MEMORY;
2259 if (num_result == 0) {
2260 char *escape_username;
2261 /* Check if we need to add an entry */
2262 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2263 ldap_op = LDAP_MOD_ADD;
2265 escape_username = escape_rdn_val_string_alloc(username);
2266 if (!escape_username) {
2267 status = NT_STATUS_NO_MEMORY;
2271 if (username[strlen(username)-1] == '$') {
2272 dn = talloc_asprintf(ctx,
2275 lp_ldap_machine_suffix());
2277 dn = talloc_asprintf(ctx,
2280 lp_ldap_user_suffix());
2283 SAFE_FREE(escape_username);
2285 status = NT_STATUS_NO_MEMORY;
2290 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2291 pdb_element_is_set_or_changed)) {
2292 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2294 ldap_mods_free(mods, true);
2300 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2303 switch ( ldap_state->schema_ver ) {
2304 case SCHEMAVER_SAMBAACCOUNT:
2305 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2307 case SCHEMAVER_SAMBASAMACCOUNT:
2308 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2311 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2315 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2316 if (!NT_STATUS_IS_OK(ret)) {
2317 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2318 pdb_get_username(newpwd),dn));
2319 ldap_mods_free(mods, true);
2323 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2324 ldap_mods_free(mods, true);
2326 status = NT_STATUS_OK;
2332 ldap_msgfree(result);
2337 /**********************************************************************
2338 *********************************************************************/
2340 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2342 LDAPMessage ** result)
2344 int scope = LDAP_SCOPE_SUBTREE;
2346 const char **attr_list;
2348 attr_list = get_attr_list(NULL, groupmap_attr_list);
2349 rc = smbldap_search(ldap_state->smbldap_state,
2350 lp_ldap_suffix (), scope,
2351 filter, attr_list, 0, result);
2352 TALLOC_FREE(attr_list);
2357 /**********************************************************************
2358 *********************************************************************/
2360 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2361 GROUP_MAP *map, LDAPMessage *entry)
2364 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2366 if (ldap_state == NULL || map == NULL || entry == NULL ||
2367 ldap_state->smbldap_state->ldap_struct == NULL) {
2368 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2373 temp = smbldap_talloc_single_attribute(
2374 ldap_state->smbldap_state->ldap_struct,
2376 get_attr_key2string(groupmap_attr_list,
2377 LDAP_ATTR_GIDNUMBER),
2380 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2381 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2385 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2387 map->gid = (gid_t)atol(temp);
2390 temp = smbldap_talloc_single_attribute(
2391 ldap_state->smbldap_state->ldap_struct,
2393 get_attr_key2string(groupmap_attr_list,
2394 LDAP_ATTR_GROUP_SID),
2397 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2398 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2403 if (!string_to_sid(&map->sid, temp)) {
2404 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2410 temp = smbldap_talloc_single_attribute(
2411 ldap_state->smbldap_state->ldap_struct,
2413 get_attr_key2string(groupmap_attr_list,
2414 LDAP_ATTR_GROUP_TYPE),
2417 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2418 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2422 map->sid_name_use = (enum lsa_SidType)atol(temp);
2424 if ((map->sid_name_use < SID_NAME_USER) ||
2425 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2426 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2432 temp = smbldap_talloc_single_attribute(
2433 ldap_state->smbldap_state->ldap_struct,
2435 get_attr_key2string(groupmap_attr_list,
2436 LDAP_ATTR_DISPLAY_NAME),
2439 temp = smbldap_talloc_single_attribute(
2440 ldap_state->smbldap_state->ldap_struct,
2442 get_attr_key2string(groupmap_attr_list,
2446 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2447 for gidNumber(%lu)\n",(unsigned long)map->gid));
2452 map->nt_name = talloc_strdup(map, temp);
2453 if (!map->nt_name) {
2459 temp = smbldap_talloc_single_attribute(
2460 ldap_state->smbldap_state->ldap_struct,
2462 get_attr_key2string(groupmap_attr_list,
2466 temp = talloc_strdup(ctx, "");
2472 map->comment = talloc_strdup(map, temp);
2473 if (!map->comment) {
2478 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2479 idmap_cache_set_sid2gid(&map->sid, map->gid);
2486 /**********************************************************************
2487 *********************************************************************/
2489 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2493 struct ldapsam_privates *ldap_state =
2494 (struct ldapsam_privates *)methods->private_data;
2495 LDAPMessage *result = NULL;
2496 LDAPMessage *entry = NULL;
2499 if (ldapsam_search_one_group(ldap_state, filter, &result)
2501 return NT_STATUS_NO_SUCH_GROUP;
2504 count = ldap_count_entries(priv2ld(ldap_state), result);
2507 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2509 ldap_msgfree(result);
2510 return NT_STATUS_NO_SUCH_GROUP;
2514 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2515 "count=%d\n", filter, count));
2516 ldap_msgfree(result);
2517 return NT_STATUS_NO_SUCH_GROUP;
2520 entry = ldap_first_entry(priv2ld(ldap_state), result);
2523 ldap_msgfree(result);
2524 return NT_STATUS_UNSUCCESSFUL;
2527 if (!init_group_from_ldap(ldap_state, map, entry)) {
2528 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2529 "group filter %s\n", filter));
2530 ldap_msgfree(result);
2531 return NT_STATUS_NO_SUCH_GROUP;
2534 ldap_msgfree(result);
2535 return NT_STATUS_OK;
2538 /**********************************************************************
2539 *********************************************************************/
2541 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2544 char *filter = NULL;
2548 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2550 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2551 sid_to_fstring(tmp, &sid)) < 0) {
2552 return NT_STATUS_NO_MEMORY;
2555 status = ldapsam_getgroup(methods, filter, map);
2560 /**********************************************************************
2561 *********************************************************************/
2563 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2566 char *filter = NULL;
2569 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2571 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2572 (unsigned long)gid) < 0) {
2573 return NT_STATUS_NO_MEMORY;
2576 status = ldapsam_getgroup(methods, filter, map);
2581 /**********************************************************************
2582 *********************************************************************/
2584 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2587 char *filter = NULL;
2588 char *escape_name = escape_ldap_string(talloc_tos(), name);
2592 return NT_STATUS_NO_MEMORY;
2595 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2597 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2598 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2600 TALLOC_FREE(escape_name);
2601 return NT_STATUS_NO_MEMORY;
2604 TALLOC_FREE(escape_name);
2605 status = ldapsam_getgroup(methods, filter, map);
2610 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2612 const struct dom_sid *domain_sid,
2618 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2619 str, sizeof(str)-1)) {
2620 DEBUG(10, ("Could not find sambaSID attribute\n"));
2624 if (!string_to_sid(&sid, str)) {
2625 DEBUG(10, ("Could not convert string %s to sid\n", str));
2629 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2630 DEBUG(10, ("SID %s is not in expected domain %s\n",
2631 str, sid_string_dbg(domain_sid)));
2635 if (!sid_peek_rid(&sid, rid)) {
2636 DEBUG(10, ("Could not peek into RID\n"));
2643 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2644 TALLOC_CTX *mem_ctx,
2645 const struct dom_sid *group,
2646 uint32_t **pp_member_rids,
2647 size_t *p_num_members)
2649 struct ldapsam_privates *ldap_state =
2650 (struct ldapsam_privates *)methods->private_data;
2651 struct smbldap_state *conn = ldap_state->smbldap_state;
2652 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2653 const char *sid_attrs[] = { "sambaSID", NULL };
2654 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2655 LDAPMessage *result = NULL;
2658 char **values = NULL;
2663 *pp_member_rids = NULL;
2666 filter = talloc_asprintf(mem_ctx,
2667 "(&(objectClass=%s)"
2670 LDAP_OBJ_POSIXGROUP,
2672 sid_string_talloc(mem_ctx, group));
2673 if (filter == NULL) {
2674 ret = NT_STATUS_NO_MEMORY;
2678 rc = smbldap_search(conn, lp_ldap_suffix(),
2679 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2682 if (rc != LDAP_SUCCESS)
2685 talloc_autofree_ldapmsg(mem_ctx, result);
2687 count = ldap_count_entries(conn->ldap_struct, result);
2690 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2691 sid_string_dbg(group)));
2692 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2697 ret = NT_STATUS_NO_SUCH_GROUP;
2701 entry = ldap_first_entry(conn->ldap_struct, result);
2705 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2707 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2708 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2712 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2714 if ((values != NULL) && (values[0] != NULL)) {
2716 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2717 if (filter == NULL) {
2718 ret = NT_STATUS_NO_MEMORY;
2722 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2723 char *escape_memberuid;
2725 escape_memberuid = escape_ldap_string(talloc_tos(),
2727 if (escape_memberuid == NULL) {
2728 ret = NT_STATUS_NO_MEMORY;
2732 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2733 TALLOC_FREE(escape_memberuid);
2734 if (filter == NULL) {
2735 ret = NT_STATUS_NO_MEMORY;
2740 filter = talloc_asprintf_append_buffer(filter, "))");
2741 if (filter == NULL) {
2742 ret = NT_STATUS_NO_MEMORY;
2746 rc = smbldap_search(conn, lp_ldap_suffix(),
2747 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2750 if (rc != LDAP_SUCCESS)
2753 count = ldap_count_entries(conn->ldap_struct, result);
2754 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2756 talloc_autofree_ldapmsg(mem_ctx, result);
2758 for (entry = ldap_first_entry(conn->ldap_struct, result);
2760 entry = ldap_next_entry(conn->ldap_struct, entry))
2766 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2770 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2771 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2772 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2776 if (!string_to_sid(&sid, sidstr))
2779 if (!sid_check_is_in_our_domain(&sid)) {
2780 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2781 "in our domain\n"));
2782 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2786 sid_peek_rid(&sid, &rid);
2788 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2790 ret = NT_STATUS_NO_MEMORY;
2796 filter = talloc_asprintf(mem_ctx,
2797 "(&(objectClass=%s)"
2799 LDAP_OBJ_SAMBASAMACCOUNT,
2802 rc = smbldap_search(conn, lp_ldap_suffix(),
2803 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2806 if (rc != LDAP_SUCCESS)
2809 talloc_autofree_ldapmsg(mem_ctx, result);
2811 for (entry = ldap_first_entry(conn->ldap_struct, result);
2813 entry = ldap_next_entry(conn->ldap_struct, entry))
2817 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2819 get_global_sam_sid(),
2821 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2822 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2826 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2828 ret = NT_STATUS_NO_MEMORY;
2838 ldap_value_free(values);
2843 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2844 TALLOC_CTX *mem_ctx,
2846 struct dom_sid **pp_sids,
2848 uint32_t *p_num_groups)
2850 struct ldapsam_privates *ldap_state =
2851 (struct ldapsam_privates *)methods->private_data;
2852 struct smbldap_state *conn = ldap_state->smbldap_state;
2854 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2857 LDAPMessage *result = NULL;
2859 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2863 gid_t primary_gid = -1;
2868 if (pdb_get_username(user) == NULL) {
2869 return NT_STATUS_INVALID_PARAMETER;
2872 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2873 if (escape_name == NULL)
2874 return NT_STATUS_NO_MEMORY;
2876 if (user->unix_pw) {
2877 primary_gid = user->unix_pw->pw_gid;
2879 /* retrieve the users primary gid */
2880 filter = talloc_asprintf(mem_ctx,
2881 "(&(objectClass=%s)(uid=%s))",
2882 LDAP_OBJ_SAMBASAMACCOUNT,
2884 if (filter == NULL) {
2885 ret = NT_STATUS_NO_MEMORY;
2889 rc = smbldap_search(conn, lp_ldap_suffix(),
2890 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2892 if (rc != LDAP_SUCCESS)
2895 talloc_autofree_ldapmsg(mem_ctx, result);
2897 count = ldap_count_entries(priv2ld(ldap_state), result);
2901 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2902 ret = NT_STATUS_NO_SUCH_USER;
2905 entry = ldap_first_entry(priv2ld(ldap_state), result);
2907 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2909 DEBUG (1, ("Unable to find the member's gid!\n"));
2910 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2913 primary_gid = strtoul(gidstr, NULL, 10);
2916 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2917 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2922 filter = talloc_asprintf(mem_ctx,
2923 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2924 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2925 if (filter == NULL) {
2926 ret = NT_STATUS_NO_MEMORY;
2930 rc = smbldap_search(conn, lp_ldap_suffix(),
2931 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2933 if (rc != LDAP_SUCCESS)
2936 talloc_autofree_ldapmsg(mem_ctx, result);
2944 /* We need to add the primary group as the first gid/sid */
2946 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2947 ret = NT_STATUS_NO_MEMORY;
2951 /* This sid will be replaced later */
2953 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2955 if (!NT_STATUS_IS_OK(ret)) {
2959 for (entry = ldap_first_entry(conn->ldap_struct, result);
2961 entry = ldap_next_entry(conn->ldap_struct, entry))
2968 if (!smbldap_get_single_attribute(conn->ldap_struct,
2970 str, sizeof(str)-1))
2973 if (!string_to_sid(&sid, str))
2976 if (!smbldap_get_single_attribute(conn->ldap_struct,
2978 str, sizeof(str)-1))
2981 gid = strtoul(str, &end, 10);
2983 if (PTR_DIFF(end, str) != strlen(str))
2986 if (gid == primary_gid) {
2987 sid_copy(&(*pp_sids)[0], &sid);
2989 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2991 ret = NT_STATUS_NO_MEMORY;
2994 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2996 if (!NT_STATUS_IS_OK(ret)) {
3002 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3003 DEBUG(3, ("primary group of [%s] not found\n",
3004 pdb_get_username(user)));
3005 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3009 *p_num_groups = num_sids;
3015 TALLOC_FREE(escape_name);
3019 /**********************************************************************
3020 * Augment a posixGroup object with a sambaGroupMapping domgroup
3021 *********************************************************************/
3023 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3024 struct ldapsam_privates *ldap_state,
3027 const char *filter, *dn;
3028 LDAPMessage *msg, *entry;
3032 filter = talloc_asprintf(mem_ctx,
3033 "(&(objectClass=%s)(gidNumber=%u))",
3034 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3035 if (filter == NULL) {
3036 return NT_STATUS_NO_MEMORY;
3039 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3040 get_attr_list(mem_ctx, groupmap_attr_list),
3042 talloc_autofree_ldapmsg(mem_ctx, msg);
3044 if ((rc != LDAP_SUCCESS) ||
3045 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3046 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3047 return NT_STATUS_NO_SUCH_GROUP;
3050 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3052 return NT_STATUS_NO_MEMORY;
3056 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3058 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3059 sid_string_talloc(mem_ctx, &map->sid));
3060 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3061 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3062 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3064 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3066 talloc_autofree_ldapmod(mem_ctx, mods);
3068 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3069 if (rc != LDAP_SUCCESS) {
3070 return NT_STATUS_ACCESS_DENIED;
3073 return NT_STATUS_OK;
3076 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3079 struct ldapsam_privates *ldap_state =
3080 (struct ldapsam_privates *)methods->private_data;
3081 LDAPMessage *msg = NULL;
3082 LDAPMod **mods = NULL;
3083 const char *attrs[] = { NULL };
3087 TALLOC_CTX *mem_ctx;
3094 mem_ctx = talloc_new(NULL);
3095 if (mem_ctx == NULL) {
3096 DEBUG(0, ("talloc_new failed\n"));
3097 return NT_STATUS_NO_MEMORY;
3100 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3101 sid_string_talloc(mem_ctx, &map->sid));
3102 if (filter == NULL) {
3103 result = NT_STATUS_NO_MEMORY;
3107 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3108 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3109 talloc_autofree_ldapmsg(mem_ctx, msg);
3111 if ((rc == LDAP_SUCCESS) &&
3112 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3114 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3115 "group mapping entry\n", sid_string_dbg(&map->sid)));
3116 result = NT_STATUS_GROUP_EXISTS;
3120 switch (map->sid_name_use) {
3122 case SID_NAME_DOM_GRP:
3123 /* To map a domain group we need to have a posix group
3125 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3129 case SID_NAME_ALIAS:
3130 if (!sid_check_is_in_our_domain(&map->sid)
3131 && !sid_check_is_in_builtin(&map->sid) )
3133 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3134 sid_string_dbg(&map->sid)));
3135 result = NT_STATUS_INVALID_PARAMETER;
3141 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3142 sid_type_lookup(map->sid_name_use)));
3143 result = NT_STATUS_INVALID_PARAMETER;
3147 /* Domain groups have been mapped in a separate routine, we have to
3148 * create an alias now */
3150 if (map->gid == -1) {
3151 DEBUG(10, ("Refusing to map gid==-1\n"));
3152 result = NT_STATUS_INVALID_PARAMETER;
3156 if (pdb_gid_to_sid(map->gid, &sid)) {
3157 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3158 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3159 result = NT_STATUS_GROUP_EXISTS;
3163 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3164 * the best we can get out of LDAP. */
3166 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3167 sid_string_talloc(mem_ctx, &map->sid),
3168 lp_ldap_group_suffix());
3170 result = NT_STATUS_NO_MEMORY;
3176 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3177 LDAP_OBJ_SID_ENTRY);
3178 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3180 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3181 sid_string_talloc(mem_ctx, &map->sid));
3182 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3183 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3184 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3186 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3188 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3189 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3190 talloc_autofree_ldapmod(mem_ctx, mods);
3192 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3194 result = (rc == LDAP_SUCCESS) ?
3195 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3198 TALLOC_FREE(mem_ctx);
3202 /**********************************************************************
3203 * Update a group mapping entry. We're quite strict about what can be changed:
3204 * Only the description and displayname may be changed. It simply does not
3205 * make any sense to change the SID, gid or the type in a mapping.
3206 *********************************************************************/
3208 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3211 struct ldapsam_privates *ldap_state =
3212 (struct ldapsam_privates *)methods->private_data;
3214 const char *filter, *dn;
3215 LDAPMessage *msg = NULL;
3216 LDAPMessage *entry = NULL;
3217 LDAPMod **mods = NULL;
3218 TALLOC_CTX *mem_ctx;
3221 mem_ctx = talloc_new(NULL);
3222 if (mem_ctx == NULL) {
3223 DEBUG(0, ("talloc_new failed\n"));
3224 return NT_STATUS_NO_MEMORY;
3227 /* Make 100% sure that sid, gid and type are not changed by looking up
3228 * exactly the values we're given in LDAP. */
3230 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3231 "(sambaSid=%s)(gidNumber=%u)"
3232 "(sambaGroupType=%d))",
3234 sid_string_talloc(mem_ctx, &map->sid),
3235 (unsigned int)map->gid, map->sid_name_use);
3236 if (filter == NULL) {
3237 result = NT_STATUS_NO_MEMORY;
3241 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3242 get_attr_list(mem_ctx, groupmap_attr_list),
3244 talloc_autofree_ldapmsg(mem_ctx, msg);
3246 if ((rc != LDAP_SUCCESS) ||
3247 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3248 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3249 result = NT_STATUS_NO_SUCH_GROUP;
3253 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3256 result = NT_STATUS_NO_MEMORY;
3261 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3263 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3265 talloc_autofree_ldapmod(mem_ctx, mods);
3268 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3269 "nothing to do\n"));
3270 result = NT_STATUS_OK;
3274 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3276 if (rc != LDAP_SUCCESS) {
3277 result = NT_STATUS_ACCESS_DENIED;
3281 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3282 "group %lu in LDAP\n", (unsigned long)map->gid));
3284 result = NT_STATUS_OK;
3287 TALLOC_FREE(mem_ctx);
3291 /**********************************************************************
3292 *********************************************************************/
3294 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3297 struct ldapsam_privates *priv =
3298 (struct ldapsam_privates *)methods->private_data;
3299 LDAPMessage *msg, *entry;
3302 TALLOC_CTX *mem_ctx;
3305 mem_ctx = talloc_new(NULL);
3306 if (mem_ctx == NULL) {
3307 DEBUG(0, ("talloc_new failed\n"));
3308 return NT_STATUS_NO_MEMORY;
3311 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3312 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3313 sid_string_talloc(mem_ctx, &sid));
3314 if (filter == NULL) {
3315 result = NT_STATUS_NO_MEMORY;
3318 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3319 get_attr_list(mem_ctx, groupmap_attr_list),
3321 talloc_autofree_ldapmsg(mem_ctx, msg);
3323 if ((rc != LDAP_SUCCESS) ||
3324 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3325 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3326 result = NT_STATUS_NO_SUCH_GROUP;
3330 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3331 get_attr_list(mem_ctx,
3332 groupmap_attr_list_to_delete));
3334 if ((rc == LDAP_NAMING_VIOLATION) ||
3335 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3336 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3337 const char *attrs[] = { "sambaGroupType", "description",
3338 "displayName", "sambaSIDList",
3341 /* Second try. Don't delete the sambaSID attribute, this is
3342 for "old" entries that are tacked on a winbind
3345 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3346 LDAP_OBJ_GROUPMAP, attrs);
3349 if ((rc == LDAP_NAMING_VIOLATION) ||
3350 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3351 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3352 const char *attrs[] = { "sambaGroupType", "description",
3353 "displayName", "sambaSIDList",
3354 "gidNumber", NULL };
3356 /* Third try. This is a post-3.0.21 alias (containing only
3357 * sambaSidEntry and sambaGroupMapping classes), we also have
3358 * to delete the gidNumber attribute, only the sambaSidEntry
3361 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3362 LDAP_OBJ_GROUPMAP, attrs);
3365 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3368 TALLOC_FREE(mem_ctx);
3372 /**********************************************************************
3373 *********************************************************************/
3375 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3378 struct ldapsam_privates *ldap_state =
3379 (struct ldapsam_privates *)my_methods->private_data;
3380 char *filter = NULL;
3382 const char **attr_list;
3384 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3386 return NT_STATUS_NO_MEMORY;
3388 attr_list = get_attr_list( NULL, groupmap_attr_list );
3389 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3390 LDAP_SCOPE_SUBTREE, filter,
3391 attr_list, 0, &ldap_state->result);
3392 TALLOC_FREE(attr_list);
3394 if (rc != LDAP_SUCCESS) {
3395 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3396 ldap_err2string(rc)));
3397 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3398 lp_ldap_suffix(), filter));
3399 ldap_msgfree(ldap_state->result);
3400 ldap_state->result = NULL;
3401 TALLOC_FREE(filter);
3402 return NT_STATUS_UNSUCCESSFUL;
3405 TALLOC_FREE(filter);
3407 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3408 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3409 ldap_state->result)));
3412 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3413 ldap_state->result);
3414 ldap_state->index = 0;
3416 return NT_STATUS_OK;
3419 /**********************************************************************
3420 *********************************************************************/
3422 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3424 ldapsam_endsampwent(my_methods);
3427 /**********************************************************************
3428 *********************************************************************/
3430 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3433 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3434 struct ldapsam_privates *ldap_state =
3435 (struct ldapsam_privates *)my_methods->private_data;
3439 if (!ldap_state->entry)
3442 ldap_state->index++;
3443 bret = init_group_from_ldap(ldap_state, map,
3447 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3451 return NT_STATUS_OK;
3454 /**********************************************************************
3455 *********************************************************************/
3457 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3458 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3459 GROUP_MAP ***pp_rmap,
3460 size_t *p_num_entries,
3463 GROUP_MAP *map = NULL;
3469 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3470 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3472 return NT_STATUS_ACCESS_DENIED;
3477 map = talloc_zero(NULL, GROUP_MAP);
3479 return NT_STATUS_NO_MEMORY;
3482 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3487 if (sid_name_use != SID_NAME_UNKNOWN &&
3488 sid_name_use != map->sid_name_use) {
3489 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3490 "not of the requested type\n",
3494 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3495 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3496 "non mapped\n", map->nt_name));
3500 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3501 GROUP_MAP *, entries + 1);
3503 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3504 "enlarge group map!\n"));
3505 return NT_STATUS_UNSUCCESSFUL;
3508 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3513 ldapsam_endsamgrent(methods);
3515 *p_num_entries = entries;
3517 return NT_STATUS_OK;
3520 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3521 const struct dom_sid *alias,
3522 const struct dom_sid *member,
3525 struct ldapsam_privates *ldap_state =
3526 (struct ldapsam_privates *)methods->private_data;
3528 LDAPMessage *result = NULL;
3529 LDAPMessage *entry = NULL;
3531 LDAPMod **mods = NULL;
3533 enum lsa_SidType type = SID_NAME_USE_NONE;
3536 char *filter = NULL;
3538 if (sid_check_is_in_builtin(alias)) {
3539 type = SID_NAME_ALIAS;
3542 if (sid_check_is_in_our_domain(alias)) {
3543 type = SID_NAME_ALIAS;
3546 if (type == SID_NAME_USE_NONE) {
3547 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3548 sid_string_dbg(alias)));
3549 return NT_STATUS_NO_SUCH_ALIAS;
3552 if (asprintf(&filter,
3553 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3554 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3556 return NT_STATUS_NO_MEMORY;
3559 if (ldapsam_search_one_group(ldap_state, filter,
3560 &result) != LDAP_SUCCESS) {
3562 return NT_STATUS_NO_SUCH_ALIAS;
3565 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3569 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3570 ldap_msgfree(result);
3572 return NT_STATUS_NO_SUCH_ALIAS;
3576 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3577 "filter %s: count=%d\n", filter, count));
3578 ldap_msgfree(result);
3580 return NT_STATUS_NO_SUCH_ALIAS;
3585 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3589 ldap_msgfree(result);
3590 return NT_STATUS_UNSUCCESSFUL;
3593 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3595 ldap_msgfree(result);
3596 return NT_STATUS_UNSUCCESSFUL;
3599 smbldap_set_mod(&mods, modop,
3600 get_attr_key2string(groupmap_attr_list,
3601 LDAP_ATTR_SID_LIST),
3602 sid_to_fstring(tmp, member));
3604 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3606 ldap_mods_free(mods, True);
3607 ldap_msgfree(result);
3610 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3611 return NT_STATUS_MEMBER_IN_ALIAS;
3614 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3615 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3618 if (rc != LDAP_SUCCESS) {
3619 return NT_STATUS_UNSUCCESSFUL;
3622 return NT_STATUS_OK;
3625 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3626 const struct dom_sid *alias,
3627 const struct dom_sid *member)
3629 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3632 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3633 const struct dom_sid *alias,
3634 const struct dom_sid *member)
3636 return ldapsam_modify_aliasmem(methods, alias, member,
3640 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3641 const struct dom_sid *alias,
3642 TALLOC_CTX *mem_ctx,
3643 struct dom_sid **pp_members,
3644 size_t *p_num_members)
3646 struct ldapsam_privates *ldap_state =
3647 (struct ldapsam_privates *)methods->private_data;
3648 LDAPMessage *result = NULL;
3649 LDAPMessage *entry = NULL;
3651 char **values = NULL;
3653 char *filter = NULL;
3654 uint32_t num_members = 0;
3655 enum lsa_SidType type = SID_NAME_USE_NONE;
3661 if (sid_check_is_in_builtin(alias)) {
3662 type = SID_NAME_ALIAS;
3665 if (sid_check_is_in_our_domain(alias)) {
3666 type = SID_NAME_ALIAS;
3669 if (type == SID_NAME_USE_NONE) {
3670 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3671 sid_string_dbg(alias)));
3672 return NT_STATUS_NO_SUCH_ALIAS;
3675 if (asprintf(&filter,
3676 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3677 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3679 return NT_STATUS_NO_MEMORY;
3682 if (ldapsam_search_one_group(ldap_state, filter,
3683 &result) != LDAP_SUCCESS) {
3685 return NT_STATUS_NO_SUCH_ALIAS;
3688 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3692 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3693 ldap_msgfree(result);
3695 return NT_STATUS_NO_SUCH_ALIAS;
3699 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3700 "filter %s: count=%d\n", filter, count));
3701 ldap_msgfree(result);
3703 return NT_STATUS_NO_SUCH_ALIAS;
3708 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3712 ldap_msgfree(result);
3713 return NT_STATUS_UNSUCCESSFUL;
3716 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3718 get_attr_key2string(groupmap_attr_list,
3719 LDAP_ATTR_SID_LIST));
3721 if (values == NULL) {
3722 ldap_msgfree(result);
3723 return NT_STATUS_OK;
3726 count = ldap_count_values(values);
3728 for (i=0; i<count; i++) {
3729 struct dom_sid member;
3732 if (!string_to_sid(&member, values[i]))
3735 status = add_sid_to_array(mem_ctx, &member, pp_members,
3737 if (!NT_STATUS_IS_OK(status)) {
3738 ldap_value_free(values);
3739 ldap_msgfree(result);
3744 *p_num_members = num_members;
3745 ldap_value_free(values);
3746 ldap_msgfree(result);
3748 return NT_STATUS_OK;
3751 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3752 TALLOC_CTX *mem_ctx,
3753 const struct dom_sid *domain_sid,
3754 const struct dom_sid *members,
3756 uint32_t **pp_alias_rids,
3757 size_t *p_num_alias_rids)
3759 struct ldapsam_privates *ldap_state =
3760 (struct ldapsam_privates *)methods->private_data;
3763 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3765 LDAPMessage *result = NULL;
3766 LDAPMessage *entry = NULL;
3770 enum lsa_SidType type = SID_NAME_USE_NONE;
3771 bool is_builtin = false;
3772 bool sid_added = false;
3774 *pp_alias_rids = NULL;
3775 *p_num_alias_rids = 0;
3777 if (sid_check_is_builtin(domain_sid)) {
3779 type = SID_NAME_ALIAS;
3782 if (sid_check_is_domain(domain_sid)) {
3783 type = SID_NAME_ALIAS;
3786 if (type == SID_NAME_USE_NONE) {
3787 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3788 sid_string_dbg(domain_sid)));
3789 return NT_STATUS_UNSUCCESSFUL;
3792 if (num_members == 0) {
3793 return NT_STATUS_OK;
3796 filter = talloc_asprintf(mem_ctx,
3797 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3798 LDAP_OBJ_GROUPMAP, type);
3800 for (i=0; i<num_members; i++)
3801 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3803 sid_string_talloc(mem_ctx,
3806 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3808 if (filter == NULL) {
3809 return NT_STATUS_NO_MEMORY;
3813 ldap_state->search_cache.filter &&
3814 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3815 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3816 result = ldap_state->search_cache.result;
3817 ldap_state->search_cache.result = NULL;
3819 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3820 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3821 if (rc != LDAP_SUCCESS) {
3822 return NT_STATUS_UNSUCCESSFUL;
3824 talloc_autofree_ldapmsg(filter, result);
3827 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3829 for (entry = ldap_first_entry(ldap_struct, result);
3831 entry = ldap_next_entry(ldap_struct, entry))
3837 if (!smbldap_get_single_attribute(ldap_struct, entry,
3843 if (!string_to_sid(&sid, sid_str))
3846 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3851 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3852 p_num_alias_rids)) {
3853 return NT_STATUS_NO_MEMORY;
3857 if (!is_builtin && !sid_added) {
3858 TALLOC_FREE(ldap_state->search_cache.filter);
3860 * Note: result is a talloc child of filter because of the
3861 * talloc_autofree_ldapmsg() usage
3863 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3864 ldap_state->search_cache.result = result;
3867 return NT_STATUS_OK;
3870 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3871 enum pdb_policy_type type,
3874 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3876 LDAPMod **mods = NULL;
3877 fstring value_string;
3878 const char *policy_attr = NULL;
3880 struct ldapsam_privates *ldap_state =
3881 (struct ldapsam_privates *)methods->private_data;
3883 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3885 if (!ldap_state->domain_dn) {
3886 return NT_STATUS_INVALID_PARAMETER;
3889 policy_attr = get_account_policy_attr(type);
3890 if (policy_attr == NULL) {
3891 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3896 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3898 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3900 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3903 ldap_mods_free(mods, True);
3905 if (rc != LDAP_SUCCESS) {
3909 if (!cache_account_policy_set(type, value)) {
3910 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3911 "update local tdb cache\n"));
3915 return NT_STATUS_OK;
3918 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3919 enum pdb_policy_type type,
3922 return ldapsam_set_account_policy_in_ldap(methods, type,
3926 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3927 enum pdb_policy_type type,
3930 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3931 LDAPMessage *result = NULL;
3932 LDAPMessage *entry = NULL;
3937 const char *policy_attr = NULL;
3939 struct ldapsam_privates *ldap_state =
3940 (struct ldapsam_privates *)methods->private_data;
3942 const char *attrs[2];
3944 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3946 if (!ldap_state->domain_dn) {
3947 return NT_STATUS_INVALID_PARAMETER;
3950 policy_attr = get_account_policy_attr(type);
3952 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3953 "policy index: %d\n", type));
3957 attrs[0] = policy_attr;
3960 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3961 if (filter == NULL) {
3962 return NT_STATUS_NO_MEMORY;
3964 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3965 LDAP_SCOPE_BASE, filter, attrs, 0,
3967 TALLOC_FREE(filter);
3968 if (rc != LDAP_SUCCESS) {
3972 count = ldap_count_entries(priv2ld(ldap_state), result);
3977 entry = ldap_first_entry(priv2ld(ldap_state), result);
3978 if (entry == NULL) {
3982 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3987 *value = (uint32_t)atol(vals[0]);
3989 ntstatus = NT_STATUS_OK;
3993 ldap_value_free(vals);
3994 ldap_msgfree(result);
3999 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4001 - if user hasn't decided to use account policies inside LDAP just reuse the
4004 - if there is a valid cache entry, return that
4005 - if there is an LDAP entry, update cache and return
4006 - otherwise set to default, update cache and return
4010 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4011 enum pdb_policy_type type,
4014 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4016 if (cache_account_policy_get(type, value)) {
4017 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4019 return NT_STATUS_OK;
4022 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4024 if (NT_STATUS_IS_OK(ntstatus)) {
4028 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4032 /* should we automagically migrate old tdb value here ? */
4033 if (account_policy_get(type, value))
4036 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4037 "default\n", type));
4040 if (!account_policy_get_default(type, value)) {
4046 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4047 if (!NT_STATUS_IS_OK(ntstatus)) {
4053 if (!cache_account_policy_set(type, *value)) {
4054 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4055 "tdb as a cache\n"));
4056 return NT_STATUS_UNSUCCESSFUL;
4059 return NT_STATUS_OK;
4062 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4063 const struct dom_sid *domain_sid,
4067 enum lsa_SidType *attrs)
4069 struct ldapsam_privates *ldap_state =
4070 (struct ldapsam_privates *)methods->private_data;
4071 LDAPMessage *msg = NULL;
4073 char *allsids = NULL;
4074 int i, rc, num_mapped;
4075 NTSTATUS result = NT_STATUS_NO_MEMORY;
4076 TALLOC_CTX *mem_ctx;
4080 mem_ctx = talloc_new(NULL);
4081 if (mem_ctx == NULL) {
4082 DEBUG(0, ("talloc_new failed\n"));
4086 if (!sid_check_is_builtin(domain_sid) &&
4087 !sid_check_is_domain(domain_sid)) {
4088 result = NT_STATUS_INVALID_PARAMETER;
4092 if (num_rids == 0) {
4093 result = NT_STATUS_NONE_MAPPED;
4097 for (i=0; i<num_rids; i++)
4098 attrs[i] = SID_NAME_UNKNOWN;
4100 allsids = talloc_strdup(mem_ctx, "");
4101 if (allsids == NULL) {
4105 for (i=0; i<num_rids; i++) {
4107 sid_compose(&sid, domain_sid, rids[i]);
4108 allsids = talloc_asprintf_append_buffer(
4109 allsids, "(sambaSid=%s)",
4110 sid_string_talloc(mem_ctx, &sid));
4111 if (allsids == NULL) {
4116 /* First look for users */
4120 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4122 filter = talloc_asprintf(
4123 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4124 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4126 if (filter == NULL) {
4130 rc = smbldap_search(ldap_state->smbldap_state,
4131 lp_ldap_user_suffix(),
4132 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4134 talloc_autofree_ldapmsg(mem_ctx, msg);
4137 if (rc != LDAP_SUCCESS)
4140 ld = ldap_state->smbldap_state->ldap_struct;
4143 for (entry = ldap_first_entry(ld, msg);
4145 entry = ldap_next_entry(ld, entry)) {
4150 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4152 DEBUG(2, ("Could not find sid from ldap entry\n"));
4156 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4159 DEBUG(2, ("Could not retrieve uid attribute\n"));
4163 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4164 if (rid == rids[rid_index])
4168 if (rid_index == num_rids) {
4169 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4173 attrs[rid_index] = SID_NAME_USER;
4174 names[rid_index] = name;
4178 if (num_mapped == num_rids) {
4179 /* No need to look for groups anymore -- we're done */
4180 result = NT_STATUS_OK;
4184 /* Same game for groups */
4188 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4189 "sambaGroupType", NULL };
4191 filter = talloc_asprintf(
4192 mem_ctx, "(&(objectClass=%s)(|%s))",
4193 LDAP_OBJ_GROUPMAP, allsids);
4194 if (filter == NULL) {
4198 rc = smbldap_search(ldap_state->smbldap_state,
4200 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4202 talloc_autofree_ldapmsg(mem_ctx, msg);
4205 if (rc != LDAP_SUCCESS)
4208 /* ldap_struct might have changed due to a reconnect */
4210 ld = ldap_state->smbldap_state->ldap_struct;
4212 /* For consistency checks, we already checked we're only domain or builtin */
4214 is_builtin = sid_check_is_builtin(domain_sid);
4216 for (entry = ldap_first_entry(ld, msg);
4218 entry = ldap_next_entry(ld, entry))
4223 enum lsa_SidType type;
4224 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4226 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4229 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4234 type = (enum lsa_SidType)atol(attr);
4236 /* Consistency checks */
4237 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4238 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4239 (type != SID_NAME_DOM_GRP)))) {
4240 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4243 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4245 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4249 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4252 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4254 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4258 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4263 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4264 if (rid == rids[rid_index])
4268 if (rid_index == num_rids) {
4269 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4273 attrs[rid_index] = type;
4274 names[rid_index] = attr;
4278 result = NT_STATUS_NONE_MAPPED;
4281 result = (num_mapped == num_rids) ?
4282 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4284 TALLOC_FREE(mem_ctx);
4288 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4290 char *filter = NULL;
4291 char *escaped = NULL;
4292 char *result = NULL;
4294 if (asprintf(&filter, "(&%s(objectclass=%s))",
4295 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4299 escaped = escape_ldap_string(talloc_tos(), username);
4300 if (escaped == NULL) goto done;
4302 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4306 TALLOC_FREE(escaped);
4311 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4315 const char **result;
4317 va_start(ap, mem_ctx);
4318 while (va_arg(ap, const char *) != NULL)
4322 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4326 va_start(ap, mem_ctx);
4327 for (i=0; i<num; i++) {
4328 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4329 if (result[i] == NULL) {
4330 talloc_free(result);
4341 struct ldap_search_state {
4342 struct smbldap_state *connection;
4344 uint32_t acct_flags;
4345 uint16_t group_type;
4352 void *pagedresults_cookie;
4354 LDAPMessage *entries, *current_entry;
4355 bool (*ldap2displayentry)(struct ldap_search_state *state,
4356 TALLOC_CTX *mem_ctx,
4357 LDAP *ld, LDAPMessage *entry,
4358 struct samr_displayentry *result);
4361 static bool ldapsam_search_firstpage(struct pdb_search *search)
4363 struct ldap_search_state *state =
4364 (struct ldap_search_state *)search->private_data;
4366 int rc = LDAP_OPERATIONS_ERROR;
4368 state->entries = NULL;
4370 if (state->connection->paged_results) {
4371 rc = smbldap_search_paged(state->connection, state->base,
4372 state->scope, state->filter,
4373 state->attrs, state->attrsonly,
4374 lp_ldap_page_size(), &state->entries,
4375 &state->pagedresults_cookie);
4378 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4380 if (state->entries != NULL) {
4381 /* Left over from unsuccessful paged attempt */
4382 ldap_msgfree(state->entries);
4383 state->entries = NULL;
4386 rc = smbldap_search(state->connection, state->base,
4387 state->scope, state->filter, state->attrs,
4388 state->attrsonly, &state->entries);
4390 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4393 /* Ok, the server was lying. It told us it could do paged
4394 * searches when it could not. */
4395 state->connection->paged_results = False;
4398 ld = state->connection->ldap_struct;
4400 DEBUG(5, ("Don't have an LDAP connection right after a "
4404 state->current_entry = ldap_first_entry(ld, state->entries);
4409 static bool ldapsam_search_nextpage(struct pdb_search *search)
4411 struct ldap_search_state *state =
4412 (struct ldap_search_state *)search->private_data;
4415 if (!state->connection->paged_results) {
4416 /* There is no next page when there are no paged results */
4420 rc = smbldap_search_paged(state->connection, state->base,
4421 state->scope, state->filter, state->attrs,
4422 state->attrsonly, lp_ldap_page_size(),
4424 &state->pagedresults_cookie);
4426 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4429 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4431 if (state->current_entry == NULL) {
4432 ldap_msgfree(state->entries);
4433 state->entries = NULL;
4440 static bool ldapsam_search_next_entry(struct pdb_search *search,
4441 struct samr_displayentry *entry)
4443 struct ldap_search_state *state =
4444 (struct ldap_search_state *)search->private_data;
4448 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4451 if ((state->entries == NULL) &&
4452 !ldapsam_search_nextpage(search))
4455 if (state->current_entry == NULL) {
4459 result = state->ldap2displayentry(state, search,
4460 state->connection->ldap_struct,
4461 state->current_entry, entry);
4465 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4466 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4467 if (dn != NULL) ldap_memfree(dn);
4470 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4472 if (state->current_entry == NULL) {
4473 ldap_msgfree(state->entries);
4474 state->entries = NULL;
4477 if (!result) goto retry;
4482 static void ldapsam_search_end(struct pdb_search *search)
4484 struct ldap_search_state *state =
4485 (struct ldap_search_state *)search->private_data;
4488 if (state->pagedresults_cookie == NULL)
4491 if (state->entries != NULL)
4492 ldap_msgfree(state->entries);
4494 state->entries = NULL;
4495 state->current_entry = NULL;
4497 if (!state->connection->paged_results)
4500 /* Tell the LDAP server we're not interested in the rest anymore. */
4502 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4503 state->filter, state->attrs,
4504 state->attrsonly, 0, &state->entries,
4505 &state->pagedresults_cookie);
4507 if (rc != LDAP_SUCCESS)
4508 DEBUG(5, ("Could not end search properly\n"));
4513 static bool ldapuser2displayentry(struct ldap_search_state *state,
4514 TALLOC_CTX *mem_ctx,
4515 LDAP *ld, LDAPMessage *entry,
4516 struct samr_displayentry *result)
4519 size_t converted_size;
4521 uint32_t acct_flags;
4523 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4524 if ((vals == NULL) || (vals[0] == NULL)) {
4525 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4528 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4529 ldap_value_free(vals);
4531 if ((state->acct_flags != 0) &&
4532 ((state->acct_flags & acct_flags) == 0))
4535 result->acct_flags = acct_flags;
4536 result->account_name = "";
4537 result->fullname = "";
4538 result->description = "";
4540 vals = ldap_get_values(ld, entry, "uid");
4541 if ((vals == NULL) || (vals[0] == NULL)) {
4542 DEBUG(5, ("\"uid\" not found\n"));
4545 if (!pull_utf8_talloc(mem_ctx,
4546 discard_const_p(char *, &result->account_name),
4547 vals[0], &converted_size))
4549 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4553 ldap_value_free(vals);
4555 vals = ldap_get_values(ld, entry, "displayName");
4556 if ((vals == NULL) || (vals[0] == NULL))
4557 DEBUG(8, ("\"displayName\" not found\n"));
4558 else if (!pull_utf8_talloc(mem_ctx,
4559 discard_const_p(char *, &result->fullname),
4560 vals[0], &converted_size))
4562 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4566 ldap_value_free(vals);
4568 vals = ldap_get_values(ld, entry, "description");
4569 if ((vals == NULL) || (vals[0] == NULL))
4570 DEBUG(8, ("\"description\" not found\n"));
4571 else if (!pull_utf8_talloc(mem_ctx,
4572 discard_const_p(char *, &result->description),
4573 vals[0], &converted_size))
4575 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4579 ldap_value_free(vals);
4581 if ((result->account_name == NULL) ||
4582 (result->fullname == NULL) ||
4583 (result->description == NULL)) {
4584 DEBUG(0, ("talloc failed\n"));
4588 vals = ldap_get_values(ld, entry, "sambaSid");
4589 if ((vals == NULL) || (vals[0] == NULL)) {
4590 DEBUG(0, ("\"objectSid\" not found\n"));
4594 if (!string_to_sid(&sid, vals[0])) {
4595 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4596 ldap_value_free(vals);
4599 ldap_value_free(vals);
4601 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4602 DEBUG(0, ("sid %s does not belong to our domain\n",
4603 sid_string_dbg(&sid)));
4611 static bool ldapsam_search_users(struct pdb_methods *methods,
4612 struct pdb_search *search,
4613 uint32_t acct_flags)
4615 struct ldapsam_privates *ldap_state =
4616 (struct ldapsam_privates *)methods->private_data;
4617 struct ldap_search_state *state;
4619 state = talloc(search, struct ldap_search_state);
4620 if (state == NULL) {
4621 DEBUG(0, ("talloc failed\n"));
4625 state->connection = ldap_state->smbldap_state;
4627 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4628 state->base = lp_ldap_user_suffix();
4629 else if ((acct_flags != 0) &&
4630 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4631 state->base = lp_ldap_machine_suffix();
4633 state->base = lp_ldap_suffix();
4635 state->acct_flags = acct_flags;
4636 state->base = talloc_strdup(search, state->base);
4637 state->scope = LDAP_SCOPE_SUBTREE;
4638 state->filter = get_ldap_filter(search, "*");
4639 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4640 "displayName", "description",
4641 "sambaAcctFlags", NULL);
4642 state->attrsonly = 0;
4643 state->pagedresults_cookie = NULL;
4644 state->entries = NULL;
4645 state->ldap2displayentry = ldapuser2displayentry;
4647 if ((state->filter == NULL) || (state->attrs == NULL)) {
4648 DEBUG(0, ("talloc failed\n"));
4652 search->private_data = state;
4653 search->next_entry = ldapsam_search_next_entry;
4654 search->search_end = ldapsam_search_end;
4656 return ldapsam_search_firstpage(search);
4659 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4660 TALLOC_CTX *mem_ctx,
4661 LDAP *ld, LDAPMessage *entry,
4662 struct samr_displayentry *result)
4665 size_t converted_size;
4667 uint16_t group_type;
4669 result->account_name = "";
4670 result->fullname = "";
4671 result->description = "";
4674 vals = ldap_get_values(ld, entry, "sambaGroupType");
4675 if ((vals == NULL) || (vals[0] == NULL)) {
4676 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4678 ldap_value_free(vals);
4683 group_type = atoi(vals[0]);
4685 if ((state->group_type != 0) &&
4686 ((state->group_type != group_type))) {
4687 ldap_value_free(vals);
4691 ldap_value_free(vals);
4693 /* display name is the NT group name */
4695 vals = ldap_get_values(ld, entry, "displayName");
4696 if ((vals == NULL) || (vals[0] == NULL)) {
4697 DEBUG(8, ("\"displayName\" not found\n"));
4699 /* fallback to the 'cn' attribute */
4700 vals = ldap_get_values(ld, entry, "cn");
4701 if ((vals == NULL) || (vals[0] == NULL)) {
4702 DEBUG(5, ("\"cn\" not found\n"));
4705 if (!pull_utf8_talloc(mem_ctx,
4706 discard_const_p(char *,
4707 &result->account_name),
4708 vals[0], &converted_size))
4710 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4711 "failed: %s", strerror(errno)));
4714 else if (!pull_utf8_talloc(mem_ctx,
4715 discard_const_p(char *,
4716 &result->account_name),
4717 vals[0], &converted_size))
4719 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4723 ldap_value_free(vals);
4725 vals = ldap_get_values(ld, entry, "description");
4726 if ((vals == NULL) || (vals[0] == NULL))
4727 DEBUG(8, ("\"description\" not found\n"));
4728 else if (!pull_utf8_talloc(mem_ctx,
4729 discard_const_p(char *, &result->description),
4730 vals[0], &converted_size))
4732 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4735 ldap_value_free(vals);
4737 if ((result->account_name == NULL) ||
4738 (result->fullname == NULL) ||
4739 (result->description == NULL)) {
4740 DEBUG(0, ("talloc failed\n"));
4744 vals = ldap_get_values(ld, entry, "sambaSid");
4745 if ((vals == NULL) || (vals[0] == NULL)) {
4746 DEBUG(0, ("\"objectSid\" not found\n"));
4748 ldap_value_free(vals);
4753 if (!string_to_sid(&sid, vals[0])) {
4754 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4758 ldap_value_free(vals);
4760 switch (group_type) {
4761 case SID_NAME_DOM_GRP:
4762 case SID_NAME_ALIAS:
4764 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4765 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4767 DEBUG(0, ("%s is not in our domain\n",
4768 sid_string_dbg(&sid)));
4774 DEBUG(0,("unknown group type: %d\n", group_type));
4778 result->acct_flags = 0;
4783 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4784 struct pdb_search *search,
4785 const struct dom_sid *sid,
4786 enum lsa_SidType type)
4788 struct ldapsam_privates *ldap_state =
4789 (struct ldapsam_privates *)methods->private_data;
4790 struct ldap_search_state *state;
4793 state = talloc(search, struct ldap_search_state);
4794 if (state == NULL) {
4795 DEBUG(0, ("talloc failed\n"));
4799 state->connection = ldap_state->smbldap_state;
4801 state->base = talloc_strdup(search, lp_ldap_suffix());
4802 state->connection = ldap_state->smbldap_state;
4803 state->scope = LDAP_SCOPE_SUBTREE;
4804 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4805 "(sambaGroupType=%d)(sambaSID=%s*))",
4807 type, sid_to_fstring(tmp, sid));
4808 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4809 "displayName", "description",
4810 "sambaGroupType", NULL);
4811 state->attrsonly = 0;
4812 state->pagedresults_cookie = NULL;
4813 state->entries = NULL;
4814 state->group_type = type;
4815 state->ldap2displayentry = ldapgroup2displayentry;
4817 if ((state->filter == NULL) || (state->attrs == NULL)) {
4818 DEBUG(0, ("talloc failed\n"));
4822 search->private_data = state;
4823 search->next_entry = ldapsam_search_next_entry;
4824 search->search_end = ldapsam_search_end;
4826 return ldapsam_search_firstpage(search);
4829 static bool ldapsam_search_groups(struct pdb_methods *methods,
4830 struct pdb_search *search)
4832 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4835 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4836 struct pdb_search *search,
4837 const struct dom_sid *sid)
4839 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4842 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4844 return PDB_CAP_STORE_RIDS;
4847 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4850 struct smbldap_state *smbldap_state = priv->smbldap_state;
4852 LDAPMessage *result = NULL;
4853 LDAPMessage *entry = NULL;
4854 LDAPMod **mods = NULL;
4858 uint32_t nextRid = 0;
4861 TALLOC_CTX *mem_ctx;
4863 mem_ctx = talloc_new(NULL);
4864 if (mem_ctx == NULL) {
4865 DEBUG(0, ("talloc_new failed\n"));
4866 return NT_STATUS_NO_MEMORY;
4869 status = smbldap_search_domain_info(smbldap_state, &result,
4870 get_global_sam_name(), False);
4871 if (!NT_STATUS_IS_OK(status)) {
4872 DEBUG(3, ("Could not get domain info: %s\n",
4873 nt_errstr(status)));
4877 talloc_autofree_ldapmsg(mem_ctx, result);
4879 entry = ldap_first_entry(priv2ld(priv), result);
4880 if (entry == NULL) {
4881 DEBUG(0, ("Could not get domain info entry\n"));
4882 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4886 /* Find the largest of the three attributes "sambaNextRid",
4887 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4888 concept of differentiating between user and group rids, and will
4889 use only "sambaNextRid" in the future. But for compatibility
4890 reasons I look if others have chosen different strategies -- VL */
4892 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4893 "sambaNextRid", mem_ctx);
4894 if (value != NULL) {
4895 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4896 nextRid = MAX(nextRid, tmp);
4899 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4900 "sambaNextUserRid", mem_ctx);
4901 if (value != NULL) {
4902 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4903 nextRid = MAX(nextRid, tmp);
4906 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4907 "sambaNextGroupRid", mem_ctx);
4908 if (value != NULL) {
4909 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4910 nextRid = MAX(nextRid, tmp);
4914 nextRid = BASE_RID-1;
4919 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4920 talloc_asprintf(mem_ctx, "%d", nextRid));
4921 talloc_autofree_ldapmod(mem_ctx, mods);
4923 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4924 status = NT_STATUS_NO_MEMORY;
4928 rc = smbldap_modify(smbldap_state, dn, mods);
4930 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4933 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4936 if (NT_STATUS_IS_OK(status)) {
4940 TALLOC_FREE(mem_ctx);
4944 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4948 for (i=0; i<10; i++) {
4949 NTSTATUS result = ldapsam_get_new_rid(
4950 (struct ldapsam_privates *)methods->private_data, rid);
4951 if (NT_STATUS_IS_OK(result)) {
4955 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4959 /* The ldap update failed (maybe a race condition), retry */
4962 /* Tried 10 times, fail. */
4963 return NT_STATUS_ACCESS_DENIED;
4966 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4968 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4969 return NT_STATUS_IS_OK(result) ? True : False;
4972 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4973 const struct dom_sid *sid,
4976 struct ldapsam_privates *priv =
4977 (struct ldapsam_privates *)methods->private_data;
4979 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4981 LDAPMessage *result = NULL;
4982 LDAPMessage *entry = NULL;
4987 TALLOC_CTX *mem_ctx;
4989 mem_ctx = talloc_new(NULL);
4990 if (mem_ctx == NULL) {
4991 DEBUG(0, ("talloc_new failed\n"));
4995 filter = talloc_asprintf(mem_ctx,
4997 "(|(objectClass=%s)(objectClass=%s)))",
4998 sid_string_talloc(mem_ctx, sid),
4999 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5000 if (filter == NULL) {
5001 DEBUG(5, ("talloc_asprintf failed\n"));
5005 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5007 if (rc != LDAP_SUCCESS) {
5010 talloc_autofree_ldapmsg(mem_ctx, result);
5012 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5013 DEBUG(10, ("Got %d entries, expected one\n",
5014 ldap_count_entries(priv2ld(priv), result)));
5018 entry = ldap_first_entry(priv2ld(priv), result);
5020 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5021 "sambaGroupType", mem_ctx);
5023 if (value != NULL) {
5024 const char *gid_str;
5027 gid_str = smbldap_talloc_single_attribute(
5028 priv2ld(priv), entry, "gidNumber", mem_ctx);
5029 if (gid_str == NULL) {
5030 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5031 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5036 id->id = strtoul(gid_str, NULL, 10);
5037 id->type = ID_TYPE_GID;
5038 idmap_cache_set_sid2gid(sid, id->id);
5043 /* It must be a user */
5045 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5046 "uidNumber", mem_ctx);
5047 if (value == NULL) {
5048 DEBUG(1, ("Could not find uidNumber in %s\n",
5049 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5053 id->id = strtoul(value, NULL, 10);
5054 id->type = ID_TYPE_UID;
5055 idmap_cache_set_sid2uid(sid, id->id);
5059 TALLOC_FREE(mem_ctx);
5064 * Find the SID for a uid.
5065 * This is shortcut is only used if ldapsam:trusted is set to true.
5067 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5068 struct dom_sid *sid)
5070 struct ldapsam_privates *priv =
5071 (struct ldapsam_privates *)methods->private_data;
5073 const char *attrs[] = { "sambaSID", NULL };
5074 LDAPMessage *result = NULL;
5075 LDAPMessage *entry = NULL;
5077 char *user_sid_string;
5078 struct dom_sid user_sid;
5080 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5082 filter = talloc_asprintf(tmp_ctx,
5085 "(objectClass=%s))",
5087 LDAP_OBJ_POSIXACCOUNT,
5088 LDAP_OBJ_SAMBASAMACCOUNT);
5089 if (filter == NULL) {
5090 DEBUG(3, ("talloc_asprintf failed\n"));
5094 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5095 if (rc != LDAP_SUCCESS) {
5098 talloc_autofree_ldapmsg(tmp_ctx, result);
5100 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5101 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5102 ldap_count_entries(priv2ld(priv), result),
5103 (unsigned int)uid));
5107 entry = ldap_first_entry(priv2ld(priv), result);
5109 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5110 "sambaSID", tmp_ctx);
5111 if (user_sid_string == NULL) {
5112 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5113 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5117 if (!string_to_sid(&user_sid, user_sid_string)) {
5118 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5123 sid_copy(sid, &user_sid);
5125 idmap_cache_set_sid2uid(sid, uid);
5130 TALLOC_FREE(tmp_ctx);
5135 * Find the SID for a gid.
5136 * This is shortcut is only used if ldapsam:trusted is set to true.
5138 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5139 struct dom_sid *sid)
5141 struct ldapsam_privates *priv =
5142 (struct ldapsam_privates *)methods->private_data;
5144 const char *attrs[] = { "sambaSID", NULL };
5145 LDAPMessage *result = NULL;
5146 LDAPMessage *entry = NULL;
5148 char *group_sid_string;
5149 struct dom_sid group_sid;
5151 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5153 filter = talloc_asprintf(tmp_ctx,
5155 "(objectClass=%s))",
5158 if (filter == NULL) {
5159 DEBUG(3, ("talloc_asprintf failed\n"));
5163 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5164 if (rc != LDAP_SUCCESS) {
5167 talloc_autofree_ldapmsg(tmp_ctx, result);
5169 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5170 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5171 ldap_count_entries(priv2ld(priv), result),
5172 (unsigned int)gid));
5176 entry = ldap_first_entry(priv2ld(priv), result);
5178 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5179 "sambaSID", tmp_ctx);
5180 if (group_sid_string == NULL) {
5181 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5182 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5186 if (!string_to_sid(&group_sid, group_sid_string)) {
5187 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5192 sid_copy(sid, &group_sid);
5194 idmap_cache_set_sid2gid(sid, gid);
5199 TALLOC_FREE(tmp_ctx);
5205 * The following functions are called only if
5206 * ldapsam:trusted and ldapsam:editposix are
5211 * ldapsam_create_user creates a new
5212 * posixAccount and sambaSamAccount object
5213 * in the ldap users subtree
5215 * The uid is allocated by winbindd.
5218 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5219 TALLOC_CTX *tmp_ctx, const char *name,
5220 uint32_t acb_info, uint32_t *rid)
5222 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5223 LDAPMessage *entry = NULL;
5224 LDAPMessage *result = NULL;
5225 uint32_t num_result;
5226 bool is_machine = False;
5227 bool add_posix = False;
5228 LDAPMod **mods = NULL;
5236 const char *dn = NULL;
5237 struct dom_sid group_sid;
5238 struct dom_sid user_sid;
5244 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5245 acb_info & ACB_WSTRUST ||
5246 acb_info & ACB_SVRTRUST ||
5247 acb_info & ACB_DOMTRUST) {
5251 username = escape_ldap_string(talloc_tos(), name);
5252 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5253 username, LDAP_OBJ_POSIXACCOUNT);
5254 TALLOC_FREE(username);
5256 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5257 if (rc != LDAP_SUCCESS) {
5258 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5259 return NT_STATUS_ACCESS_DENIED;
5261 talloc_autofree_ldapmsg(tmp_ctx, result);
5263 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5265 if (num_result > 1) {
5266 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5267 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5270 if (num_result == 1) {
5272 /* check if it is just a posix account.
5273 * or if there is a sid attached to this entry
5276 entry = ldap_first_entry(priv2ld(ldap_state), result);
5278 return NT_STATUS_UNSUCCESSFUL;
5281 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5283 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5284 return NT_STATUS_USER_EXISTS;
5287 /* it is just a posix account, retrieve the dn for later use */
5288 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5290 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5291 return NT_STATUS_NO_MEMORY;
5295 if (num_result == 0) {
5299 /* Create the basic samu structure and generate the mods for the ldap commit */
5300 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5301 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5305 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5307 user = samu_new(tmp_ctx);
5309 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5310 return NT_STATUS_NO_MEMORY;
5313 if (!pdb_set_username(user, name, PDB_SET)) {
5314 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5315 return NT_STATUS_UNSUCCESSFUL;
5317 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5318 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5319 return NT_STATUS_UNSUCCESSFUL;
5322 if (acb_info & ACB_NORMAL) {
5323 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5324 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5325 return NT_STATUS_UNSUCCESSFUL;
5328 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5329 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5330 return NT_STATUS_UNSUCCESSFUL;
5334 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5335 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5336 return NT_STATUS_UNSUCCESSFUL;
5340 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5341 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5342 return NT_STATUS_UNSUCCESSFUL;
5345 if (!init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed)) {
5346 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5347 return NT_STATUS_UNSUCCESSFUL;
5350 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5351 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5353 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5358 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5360 /* retrieve the Domain Users group gid */
5361 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5362 !sid_to_gid(&group_sid, &gid)) {
5363 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5364 return NT_STATUS_INVALID_PRIMARY_GROUP;
5367 /* lets allocate a new userid for this user */
5368 if (!winbind_allocate_uid(&uid)) {
5369 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5370 return NT_STATUS_UNSUCCESSFUL;
5375 /* TODO: choose a more appropriate default for machines */
5376 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5377 shell = talloc_strdup(tmp_ctx, "/bin/false");
5379 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5380 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5382 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5383 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5385 escape_name = escape_rdn_val_string_alloc(name);
5387 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5388 return NT_STATUS_NO_MEMORY;
5392 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5394 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5397 SAFE_FREE(escape_name);
5399 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5400 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5401 return NT_STATUS_NO_MEMORY;
5404 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5405 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5406 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5407 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5408 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5409 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5410 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5413 talloc_autofree_ldapmod(tmp_ctx, mods);
5416 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5418 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5421 if (rc != LDAP_SUCCESS) {
5422 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5423 return NT_STATUS_UNSUCCESSFUL;
5426 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5428 flush_pwnam_cache();
5430 return NT_STATUS_OK;
5433 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5435 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5436 LDAPMessage *result = NULL;
5437 LDAPMessage *entry = NULL;
5443 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5445 filter = talloc_asprintf(tmp_ctx,
5448 "(objectClass=%s))",
5449 pdb_get_username(sam_acct),
5450 LDAP_OBJ_POSIXACCOUNT,
5451 LDAP_OBJ_SAMBASAMACCOUNT);
5452 if (filter == NULL) {
5453 return NT_STATUS_NO_MEMORY;
5456 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5457 if (rc != LDAP_SUCCESS) {
5458 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5459 return NT_STATUS_UNSUCCESSFUL;
5461 talloc_autofree_ldapmsg(tmp_ctx, result);
5463 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5465 if (num_result == 0) {
5466 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5467 return NT_STATUS_NO_SUCH_USER;
5470 if (num_result > 1) {
5471 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5472 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5475 entry = ldap_first_entry(priv2ld(ldap_state), result);
5477 return NT_STATUS_UNSUCCESSFUL;
5480 /* it is just a posix account, retrieve the dn for later use */
5481 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5483 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5484 return NT_STATUS_NO_MEMORY;
5487 /* try to remove memberships first */
5490 struct dom_sid *sids = NULL;
5492 uint32_t num_groups = 0;
5494 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5496 status = ldapsam_enum_group_memberships(my_methods,
5502 if (!NT_STATUS_IS_OK(status)) {
5506 for (i=0; i < num_groups; i++) {
5510 sid_peek_rid(&sids[i], &group_rid);
5512 ldapsam_del_groupmem(my_methods,
5521 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5522 if (rc != LDAP_SUCCESS) {
5523 return NT_STATUS_UNSUCCESSFUL;
5526 flush_pwnam_cache();
5528 return NT_STATUS_OK;
5532 * ldapsam_create_group creates a new
5533 * posixGroup and sambaGroupMapping object
5534 * in the ldap groups subtree
5536 * The gid is allocated by winbindd.
5539 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5540 TALLOC_CTX *tmp_ctx,
5544 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5546 LDAPMessage *entry = NULL;
5547 LDAPMessage *result = NULL;
5548 uint32_t num_result;
5549 bool is_new_entry = False;
5550 LDAPMod **mods = NULL;
5556 const char *dn = NULL;
5557 struct dom_sid group_sid;
5561 groupname = escape_ldap_string(talloc_tos(), name);
5562 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5563 groupname, LDAP_OBJ_POSIXGROUP);
5564 TALLOC_FREE(groupname);
5566 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5567 if (rc != LDAP_SUCCESS) {
5568 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5569 return NT_STATUS_UNSUCCESSFUL;
5571 talloc_autofree_ldapmsg(tmp_ctx, result);
5573 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5575 if (num_result > 1) {
5576 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5577 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5580 if (num_result == 1) {
5582 /* check if it is just a posix group.
5583 * or if there is a sid attached to this entry
5586 entry = ldap_first_entry(priv2ld(ldap_state), result);
5588 return NT_STATUS_UNSUCCESSFUL;
5591 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5593 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5594 return NT_STATUS_GROUP_EXISTS;
5597 /* it is just a posix group, retrieve the gid and the dn for later use */
5598 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5600 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5601 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5604 gid = strtoul(tmp, NULL, 10);
5606 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5608 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5609 return NT_STATUS_NO_MEMORY;
5613 if (num_result == 0) {
5614 is_new_entry = true;
5617 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5618 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5622 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5624 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5626 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5628 if (!groupsidstr || !grouptype) {
5629 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5630 return NT_STATUS_NO_MEMORY;
5633 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5634 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5635 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5636 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5641 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5643 /* lets allocate a new groupid for this group */
5644 if (!winbind_allocate_gid(&gid)) {
5645 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5646 return NT_STATUS_UNSUCCESSFUL;
5649 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5651 escape_name = escape_rdn_val_string_alloc(name);
5653 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5654 return NT_STATUS_NO_MEMORY;
5657 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5659 SAFE_FREE(escape_name);
5661 if (!gidstr || !dn) {
5662 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5663 return NT_STATUS_NO_MEMORY;
5666 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5667 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5668 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5671 talloc_autofree_ldapmod(tmp_ctx, mods);
5674 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5676 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5677 /* This call may fail with rfc2307bis schema */
5678 /* Retry adding a structural class */
5679 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5680 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5684 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5687 if (rc != LDAP_SUCCESS) {
5688 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5689 return NT_STATUS_UNSUCCESSFUL;
5692 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5694 return NT_STATUS_OK;
5697 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5699 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5700 LDAPMessage *result = NULL;
5701 LDAPMessage *entry = NULL;
5706 struct dom_sid group_sid;
5709 /* get the group sid */
5710 sid_compose(&group_sid, get_global_sam_sid(), rid);
5712 filter = talloc_asprintf(tmp_ctx,
5715 "(objectClass=%s))",
5716 sid_string_talloc(tmp_ctx, &group_sid),
5717 LDAP_OBJ_POSIXGROUP,
5719 if (filter == NULL) {
5720 return NT_STATUS_NO_MEMORY;
5723 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5724 if (rc != LDAP_SUCCESS) {
5725 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5726 return NT_STATUS_UNSUCCESSFUL;
5728 talloc_autofree_ldapmsg(tmp_ctx, result);
5730 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5732 if (num_result == 0) {
5733 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5734 return NT_STATUS_NO_SUCH_GROUP;
5737 if (num_result > 1) {
5738 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5739 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5742 entry = ldap_first_entry(priv2ld(ldap_state), result);
5744 return NT_STATUS_UNSUCCESSFUL;
5747 /* here it is, retrieve the dn for later use */
5748 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5750 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5751 return NT_STATUS_NO_MEMORY;
5754 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5756 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5757 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5760 /* check no user have this group marked as primary group */
5761 filter = talloc_asprintf(tmp_ctx,
5764 "(objectClass=%s))",
5766 LDAP_OBJ_POSIXACCOUNT,
5767 LDAP_OBJ_SAMBASAMACCOUNT);
5769 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5770 if (rc != LDAP_SUCCESS) {
5771 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5772 return NT_STATUS_UNSUCCESSFUL;
5774 talloc_autofree_ldapmsg(tmp_ctx, result);
5776 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5778 if (num_result != 0) {
5779 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5780 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5783 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5784 if (rc != LDAP_SUCCESS) {
5785 return NT_STATUS_UNSUCCESSFUL;
5788 return NT_STATUS_OK;
5791 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5792 TALLOC_CTX *tmp_ctx,
5794 uint32_t member_rid,
5797 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5798 LDAPMessage *entry = NULL;
5799 LDAPMessage *result = NULL;
5800 uint32_t num_result;
5801 LDAPMod **mods = NULL;
5804 const char *dn = NULL;
5805 struct dom_sid group_sid;
5806 struct dom_sid member_sid;
5811 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5813 case LDAP_MOD_DELETE:
5814 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5817 return NT_STATUS_UNSUCCESSFUL;
5820 /* get member sid */
5821 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5823 /* get the group sid */
5824 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5826 filter = talloc_asprintf(tmp_ctx,
5829 "(objectClass=%s))",
5830 sid_string_talloc(tmp_ctx, &member_sid),
5831 LDAP_OBJ_POSIXACCOUNT,
5832 LDAP_OBJ_SAMBASAMACCOUNT);
5833 if (filter == NULL) {
5834 return NT_STATUS_NO_MEMORY;
5837 /* get the member uid */
5838 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5839 if (rc != LDAP_SUCCESS) {
5840 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5841 return NT_STATUS_UNSUCCESSFUL;
5843 talloc_autofree_ldapmsg(tmp_ctx, result);
5845 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5847 if (num_result == 0) {
5848 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5849 return NT_STATUS_NO_SUCH_MEMBER;
5852 if (num_result > 1) {
5853 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5854 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5857 entry = ldap_first_entry(priv2ld(ldap_state), result);
5859 return NT_STATUS_UNSUCCESSFUL;
5862 if (modop == LDAP_MOD_DELETE) {
5863 /* check if we are trying to remove the member from his primary group */
5865 gid_t user_gid, group_gid;
5867 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5869 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5870 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5873 user_gid = strtoul(gidstr, NULL, 10);
5875 if (!sid_to_gid(&group_sid, &group_gid)) {
5876 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5877 return NT_STATUS_UNSUCCESSFUL;
5880 if (user_gid == group_gid) {
5881 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5882 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5886 /* here it is, retrieve the uid for later use */
5887 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5889 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5890 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5893 filter = talloc_asprintf(tmp_ctx,
5896 "(objectClass=%s))",
5897 sid_string_talloc(tmp_ctx, &group_sid),
5898 LDAP_OBJ_POSIXGROUP,
5902 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5903 if (rc != LDAP_SUCCESS) {
5904 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5905 return NT_STATUS_UNSUCCESSFUL;
5907 talloc_autofree_ldapmsg(tmp_ctx, result);
5909 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5911 if (num_result == 0) {
5912 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5913 return NT_STATUS_NO_SUCH_GROUP;
5916 if (num_result > 1) {
5917 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5918 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5921 entry = ldap_first_entry(priv2ld(ldap_state), result);
5923 return NT_STATUS_UNSUCCESSFUL;
5926 /* here it is, retrieve the dn for later use */
5927 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5929 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5930 return NT_STATUS_NO_MEMORY;
5933 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5935 talloc_autofree_ldapmod(tmp_ctx, mods);
5937 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5938 if (rc != LDAP_SUCCESS) {
5939 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5940 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5941 return NT_STATUS_MEMBER_IN_GROUP;
5943 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5944 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5945 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5947 return NT_STATUS_UNSUCCESSFUL;
5950 return NT_STATUS_OK;
5953 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5954 TALLOC_CTX *tmp_ctx,
5956 uint32_t member_rid)
5958 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5960 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5961 TALLOC_CTX *tmp_ctx,
5963 uint32_t member_rid)
5965 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5968 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5969 TALLOC_CTX *mem_ctx,
5970 struct samu *sampass)
5972 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5973 LDAPMessage *entry = NULL;
5974 LDAPMessage *result = NULL;
5975 uint32_t num_result;
5976 LDAPMod **mods = NULL;
5978 char *escape_username;
5980 const char *dn = NULL;
5984 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5986 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5987 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5988 return NT_STATUS_UNSUCCESSFUL;
5990 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
5992 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5993 return NT_STATUS_NO_MEMORY;
5996 escape_username = escape_ldap_string(talloc_tos(),
5997 pdb_get_username(sampass));
5998 if (escape_username== NULL) {
5999 return NT_STATUS_NO_MEMORY;
6002 filter = talloc_asprintf(mem_ctx,
6005 "(objectClass=%s))",
6007 LDAP_OBJ_POSIXACCOUNT,
6008 LDAP_OBJ_SAMBASAMACCOUNT);
6010 TALLOC_FREE(escape_username);
6012 if (filter == NULL) {
6013 return NT_STATUS_NO_MEMORY;
6016 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6017 if (rc != LDAP_SUCCESS) {
6018 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6019 return NT_STATUS_UNSUCCESSFUL;
6021 talloc_autofree_ldapmsg(mem_ctx, result);
6023 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6025 if (num_result == 0) {
6026 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6027 return NT_STATUS_NO_SUCH_USER;
6030 if (num_result > 1) {
6031 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6032 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6035 entry = ldap_first_entry(priv2ld(ldap_state), result);
6037 return NT_STATUS_UNSUCCESSFUL;
6040 /* retrieve the dn for later use */
6041 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6043 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6044 return NT_STATUS_NO_MEMORY;
6047 /* remove the old one, and add the new one, this way we do not risk races */
6048 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6051 return NT_STATUS_OK;
6054 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6056 if (rc != LDAP_SUCCESS) {
6057 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6058 pdb_get_username(sampass), gidstr));
6059 return NT_STATUS_UNSUCCESSFUL;
6062 flush_pwnam_cache();
6064 return NT_STATUS_OK;
6068 /**********************************************************************
6069 trusted domains functions
6070 *********************************************************************/
6072 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6075 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6076 ldap_state->domain_dn);
6079 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6080 TALLOC_CTX *mem_ctx,
6081 const char *domain, LDAPMessage **entry)
6085 int scope = LDAP_SCOPE_SUBTREE;
6086 const char **attrs = NULL; /* NULL: get all attrs */
6087 int attrsonly = 0; /* 0: return values too */
6088 LDAPMessage *result = NULL;
6090 uint32_t num_result;
6092 filter = talloc_asprintf(talloc_tos(),
6093 "(&(objectClass=%s)(sambaDomainName=%s))",
6094 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6096 trusted_dn = trusteddom_dn(ldap_state, domain);
6097 if (trusted_dn == NULL) {
6100 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6101 filter, attrs, attrsonly, &result);
6103 if (result != NULL) {
6104 talloc_autofree_ldapmsg(mem_ctx, result);
6107 if (rc == LDAP_NO_SUCH_OBJECT) {
6112 if (rc != LDAP_SUCCESS) {
6116 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6118 if (num_result > 1) {
6119 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6120 "%s object for domain '%s'?!\n",
6121 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6125 if (num_result == 0) {
6126 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6127 "%s object for domain %s.\n",
6128 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6131 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6137 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6140 struct dom_sid *sid,
6141 time_t *pass_last_set_time)
6143 struct ldapsam_privates *ldap_state =
6144 (struct ldapsam_privates *)methods->private_data;
6145 LDAPMessage *entry = NULL;
6147 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6149 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6158 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6159 entry, "sambaClearTextPassword", talloc_tos());
6160 if (pwd_str == NULL) {
6163 /* trusteddom_pw routines do not use talloc yet... */
6164 *pwd = SMB_STRDUP(pwd_str);
6170 /* last change time */
6171 if (pass_last_set_time != NULL) {
6173 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6174 entry, "sambaPwdLastSet", talloc_tos());
6175 if (time_str == NULL) {
6178 *pass_last_set_time = (time_t)atol(time_str);
6184 struct dom_sid dom_sid;
6185 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6188 if (sid_str == NULL) {
6191 if (!string_to_sid(&dom_sid, sid_str)) {
6194 sid_copy(sid, &dom_sid);
6200 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6203 const struct dom_sid *sid)
6205 struct ldapsam_privates *ldap_state =
6206 (struct ldapsam_privates *)methods->private_data;
6207 LDAPMessage *entry = NULL;
6208 LDAPMod **mods = NULL;
6209 char *prev_pwd = NULL;
6210 char *trusted_dn = NULL;
6213 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6216 * get the current entry (if there is one) in order to put the
6217 * current password into the previous password attribute
6219 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6224 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6225 LDAP_OBJ_TRUSTDOM_PASSWORD);
6226 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6228 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6229 sid_string_tos(sid));
6230 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6231 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6232 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6233 "sambaClearTextPassword", pwd);
6235 if (entry != NULL) {
6236 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6237 entry, "sambaClearTextPassword", talloc_tos());
6238 if (prev_pwd != NULL) {
6239 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6240 "sambaPreviousClearTextPassword",
6245 talloc_autofree_ldapmod(talloc_tos(), mods);
6247 trusted_dn = trusteddom_dn(ldap_state, domain);
6248 if (trusted_dn == NULL) {
6251 if (entry == NULL) {
6252 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6254 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6257 if (rc != LDAP_SUCCESS) {
6258 DEBUG(1, ("error writing trusted domain password!\n"));
6265 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6269 struct ldapsam_privates *ldap_state =
6270 (struct ldapsam_privates *)methods->private_data;
6271 LDAPMessage *entry = NULL;
6272 const char *trusted_dn;
6274 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6278 if (entry == NULL) {
6279 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6284 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6286 if (trusted_dn == NULL) {
6287 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6291 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6292 if (rc != LDAP_SUCCESS) {
6299 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6300 TALLOC_CTX *mem_ctx,
6301 uint32_t *num_domains,
6302 struct trustdom_info ***domains)
6305 struct ldapsam_privates *ldap_state =
6306 (struct ldapsam_privates *)methods->private_data;
6308 int scope = LDAP_SCOPE_SUBTREE;
6309 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6310 int attrsonly = 0; /* 0: return values too */
6311 LDAPMessage *result = NULL;
6312 LDAPMessage *entry = NULL;
6314 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6315 LDAP_OBJ_TRUSTDOM_PASSWORD);
6317 rc = smbldap_search(ldap_state->smbldap_state,
6318 ldap_state->domain_dn,
6325 if (result != NULL) {
6326 talloc_autofree_ldapmsg(mem_ctx, result);
6329 if (rc != LDAP_SUCCESS) {
6330 return NT_STATUS_UNSUCCESSFUL;
6334 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6335 DEBUG(1, ("talloc failed\n"));
6336 return NT_STATUS_NO_MEMORY;
6339 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6341 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6343 char *dom_name, *dom_sid_str;
6344 struct trustdom_info *dom_info;
6346 dom_info = talloc(*domains, struct trustdom_info);
6347 if (dom_info == NULL) {
6348 DEBUG(1, ("talloc failed\n"));
6349 return NT_STATUS_NO_MEMORY;
6352 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6356 if (dom_name == NULL) {
6357 DEBUG(1, ("talloc failed\n"));
6358 return NT_STATUS_NO_MEMORY;
6360 dom_info->name = dom_name;
6362 dom_sid_str = smbldap_talloc_single_attribute(
6363 priv2ld(ldap_state), entry, "sambaSID",
6365 if (dom_sid_str == NULL) {
6366 DEBUG(1, ("talloc failed\n"));
6367 return NT_STATUS_NO_MEMORY;
6369 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6370 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6372 return NT_STATUS_UNSUCCESSFUL;
6375 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6376 domains, num_domains);
6378 if (*domains == NULL) {
6379 DEBUG(1, ("talloc failed\n"));
6380 return NT_STATUS_NO_MEMORY;
6384 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6385 return NT_STATUS_OK;
6389 /**********************************************************************
6391 *********************************************************************/
6393 static void free_private_data(void **vp)
6395 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6397 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6399 if ((*ldap_state)->result != NULL) {
6400 ldap_msgfree((*ldap_state)->result);
6401 (*ldap_state)->result = NULL;
6403 if ((*ldap_state)->domain_dn != NULL) {
6404 SAFE_FREE((*ldap_state)->domain_dn);
6409 /* No need to free any further, as it is talloc()ed */
6412 /*********************************************************************
6413 Intitalise the parts of the pdb_methods structure that are common to
6415 *********************************************************************/
6417 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6420 struct ldapsam_privates *ldap_state;
6421 char *bind_dn = NULL;
6422 char *bind_secret = NULL;
6424 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6428 (*pdb_method)->name = "ldapsam";
6430 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6431 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6432 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6433 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6434 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6435 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6437 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6438 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6439 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6440 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6441 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6442 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6443 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6445 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6446 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6448 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6450 (*pdb_method)->capabilities = ldapsam_capabilities;
6451 (*pdb_method)->new_rid = ldapsam_new_rid;
6453 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6454 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6455 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6456 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6458 /* TODO: Setup private data and free */
6460 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6461 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6462 return NT_STATUS_NO_MEMORY;
6465 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6466 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6467 return NT_STATUS_NO_MEMORY;
6470 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6471 location, false, bind_dn, bind_secret,
6472 &ldap_state->smbldap_state);
6473 memset(bind_secret, '\0', strlen(bind_secret));
6474 SAFE_FREE(bind_secret);
6476 if ( !NT_STATUS_IS_OK(nt_status) ) {
6480 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6481 return NT_STATUS_NO_MEMORY;
6484 (*pdb_method)->private_data = ldap_state;
6486 (*pdb_method)->free_private_data = free_private_data;
6488 return NT_STATUS_OK;
6491 /**********************************************************************
6492 Initialise the 'compat' mode for pdb_ldap
6493 *********************************************************************/
6495 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6498 struct ldapsam_privates *ldap_state;
6499 char *uri = talloc_strdup( NULL, location );
6501 trim_char( uri, '\"', '\"' );
6502 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6506 if ( !NT_STATUS_IS_OK(nt_status) ) {
6510 (*pdb_method)->name = "ldapsam_compat";
6512 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6513 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6515 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6517 return NT_STATUS_OK;
6520 /**********************************************************************
6521 Initialise the normal mode for pdb_ldap
6522 *********************************************************************/
6524 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6527 struct ldapsam_privates *ldap_state = NULL;
6528 uint32_t alg_rid_base;
6529 char *alg_rid_base_string = NULL;
6530 LDAPMessage *result = NULL;
6531 LDAPMessage *entry = NULL;
6532 struct dom_sid ldap_domain_sid;
6533 struct dom_sid secrets_domain_sid;
6534 char *domain_sid_string = NULL;
6536 char *uri = talloc_strdup( NULL, location );
6538 trim_char( uri, '\"', '\"' );
6539 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6543 if (!NT_STATUS_IS_OK(nt_status)) {
6547 (*pdb_method)->name = "ldapsam";
6549 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6550 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6551 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6552 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6553 (*pdb_method)->search_users = ldapsam_search_users;
6554 (*pdb_method)->search_groups = ldapsam_search_groups;
6555 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6557 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6558 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6559 (*pdb_method)->enum_group_memberships =
6560 ldapsam_enum_group_memberships;
6561 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6562 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6563 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6564 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6566 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6567 (*pdb_method)->create_user = ldapsam_create_user;
6568 (*pdb_method)->delete_user = ldapsam_delete_user;
6569 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6570 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6571 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6572 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6573 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6577 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6578 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6580 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6582 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6584 ldap_state->domain_name, True);
6586 if ( !NT_STATUS_IS_OK(nt_status) ) {
6587 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6588 "info, nor add one to the domain. "
6589 "We cannot work reliably without it.\n"));
6590 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6593 /* Given that the above might fail, everything below this must be
6596 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6599 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6601 ldap_msgfree(result);
6602 return NT_STATUS_UNSUCCESSFUL;
6605 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6607 ldap_msgfree(result);
6608 return NT_STATUS_UNSUCCESSFUL;
6611 ldap_state->domain_dn = smb_xstrdup(dn);
6614 domain_sid_string = smbldap_talloc_single_attribute(
6615 ldap_state->smbldap_state->ldap_struct,
6617 get_userattr_key2string(ldap_state->schema_ver,
6618 LDAP_ATTR_USER_SID),
6621 if (domain_sid_string) {
6623 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6624 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6625 "read as a valid SID\n", domain_sid_string));
6626 ldap_msgfree(result);
6627 TALLOC_FREE(domain_sid_string);
6628 return NT_STATUS_INVALID_PARAMETER;
6630 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6631 &secrets_domain_sid);
6632 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6633 &ldap_domain_sid)) {
6634 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6635 "%s based on pdb_ldap results %s -> %s\n",
6636 ldap_state->domain_name,
6637 sid_string_dbg(&secrets_domain_sid),
6638 sid_string_dbg(&ldap_domain_sid)));
6640 /* reset secrets.tdb sid */
6641 secrets_store_domain_sid(ldap_state->domain_name,
6643 DEBUG(1, ("New global sam SID: %s\n",
6644 sid_string_dbg(get_global_sam_sid())));
6646 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6647 TALLOC_FREE(domain_sid_string);
6650 alg_rid_base_string = smbldap_talloc_single_attribute(
6651 ldap_state->smbldap_state->ldap_struct,
6653 get_attr_key2string( dominfo_attr_list,
6654 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6656 if (alg_rid_base_string) {
6657 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6658 if (alg_rid_base != algorithmic_rid_base()) {
6659 DEBUG(0, ("The value of 'algorithmic RID base' has "
6660 "changed since the LDAP\n"
6661 "database was initialised. Aborting. \n"));
6662 ldap_msgfree(result);
6663 TALLOC_FREE(alg_rid_base_string);
6664 return NT_STATUS_UNSUCCESSFUL;
6666 TALLOC_FREE(alg_rid_base_string);
6668 ldap_msgfree(result);
6670 return NT_STATUS_OK;
6673 NTSTATUS pdb_ldap_init(void)
6676 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6679 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6682 /* Let pdb_nds register backends */
6687 return NT_STATUS_OK;