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"
56 #define DBGC_CLASS DBGC_PASSDB
64 /**********************************************************************
65 Simple helper function to make stuff better readable
66 **********************************************************************/
68 LDAP *priv2ld(struct ldapsam_privates *priv)
70 return priv->smbldap_state->ldap_struct;
73 /**********************************************************************
74 Get the attribute name given a user schame version.
75 **********************************************************************/
77 static const char* get_userattr_key2string( int schema_ver, int key )
79 switch ( schema_ver ) {
80 case SCHEMAVER_SAMBAACCOUNT:
81 return get_attr_key2string( attrib_map_v22, key );
83 case SCHEMAVER_SAMBASAMACCOUNT:
84 return get_attr_key2string( attrib_map_v30, key );
87 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
93 /**********************************************************************
94 Return the list of attribute names given a user schema version.
95 **********************************************************************/
97 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
99 switch ( schema_ver ) {
100 case SCHEMAVER_SAMBAACCOUNT:
101 return get_attr_list( mem_ctx, attrib_map_v22 );
103 case SCHEMAVER_SAMBASAMACCOUNT:
104 return get_attr_list( mem_ctx, attrib_map_v30 );
106 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
113 /**************************************************************************
114 Return the list of attribute names to delete given a user schema version.
115 **************************************************************************/
117 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
120 switch ( schema_ver ) {
121 case SCHEMAVER_SAMBAACCOUNT:
122 return get_attr_list( mem_ctx,
123 attrib_map_to_delete_v22 );
125 case SCHEMAVER_SAMBASAMACCOUNT:
126 return get_attr_list( mem_ctx,
127 attrib_map_to_delete_v30 );
129 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
137 /*******************************************************************
138 Generate the LDAP search filter for the objectclass based on the
139 version of the schema we are using.
140 ******************************************************************/
142 static const char* get_objclass_filter( int schema_ver )
144 fstring objclass_filter;
147 switch( schema_ver ) {
148 case SCHEMAVER_SAMBAACCOUNT:
149 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
151 case SCHEMAVER_SAMBASAMACCOUNT:
152 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
155 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
156 objclass_filter[0] = '\0';
160 result = talloc_strdup(talloc_tos(), objclass_filter);
161 SMB_ASSERT(result != NULL);
165 /*****************************************************************
166 Scan a sequence number off OpenLDAP's syncrepl contextCSN
167 ******************************************************************/
169 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
171 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
172 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
173 LDAPMessage *msg = NULL;
174 LDAPMessage *entry = NULL;
176 char **values = NULL;
177 int rc, num_result, num_values, rid;
183 /* Unfortunatly there is no proper way to detect syncrepl-support in
184 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
185 * but do not show up in the root-DSE yet. Neither we can query the
186 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
187 * objectclass. Currently we require lp_ldap_suffix() to show up as
188 * namingContext. - Guenther
191 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
196 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
200 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
201 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
202 "as top-level namingContext\n", lp_ldap_suffix()));
206 mem_ctx = talloc_init("ldapsam_get_seq_num");
209 return NT_STATUS_NO_MEMORY;
211 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
212 ntstatus = NT_STATUS_NO_MEMORY;
216 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
217 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
220 /* consumer syncreplCookie: */
221 /* csn=20050126161620Z#0000001#00#00000 */
222 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
224 suffix = talloc_asprintf(mem_ctx,
225 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
227 ntstatus = NT_STATUS_NO_MEMORY;
232 /* provider contextCSN */
233 /* 20050126161620Z#000009#00#000000 */
234 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
236 suffix = talloc_asprintf(mem_ctx,
237 "cn=ldapsync,%s", lp_ldap_suffix());
240 ntstatus = NT_STATUS_NO_MEMORY;
245 rc = smbldap_search(ldap_state->smbldap_state, suffix,
246 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
248 if (rc != LDAP_SUCCESS) {
252 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
253 if (num_result != 1) {
254 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
258 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
260 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
264 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
265 if (values == NULL) {
266 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
270 num_values = ldap_count_values(values);
271 if (num_values == 0) {
272 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
277 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
278 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
283 if (!strncmp(p, "csn=", strlen("csn=")))
286 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
288 *seq_num = generalized_to_unix_time(p);
290 /* very basic sanity check */
292 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
297 ntstatus = NT_STATUS_OK;
301 ldap_value_free(values);
305 talloc_destroy(mem_ctx);
310 /*******************************************************************
311 Run the search by name.
312 ******************************************************************/
314 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
316 LDAPMessage ** result,
320 char *escape_user = escape_ldap_string(talloc_tos(), user);
324 return LDAP_NO_MEMORY;
328 * in the filter expression, replace %u with the real name
329 * so in ldap filter, %u MUST exist :-)
331 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
332 get_objclass_filter(ldap_state->schema_ver));
334 TALLOC_FREE(escape_user);
335 return LDAP_NO_MEMORY;
338 * have to use this here because $ is filtered out
342 filter = talloc_all_string_sub(talloc_tos(),
343 filter, "%u", escape_user);
344 TALLOC_FREE(escape_user);
346 return LDAP_NO_MEMORY;
349 ret = smbldap_search_suffix(ldap_state->smbldap_state,
350 filter, attr, result);
355 /*******************************************************************
356 Run the search by rid.
357 ******************************************************************/
359 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
360 uint32_t rid, LDAPMessage ** result,
366 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
367 get_objclass_filter(ldap_state->schema_ver));
369 return LDAP_NO_MEMORY;
372 rc = smbldap_search_suffix(ldap_state->smbldap_state,
373 filter, attr, result);
378 /*******************************************************************
379 Run the search by SID.
380 ******************************************************************/
382 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
383 const struct dom_sid *sid, LDAPMessage ** result,
390 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
391 get_userattr_key2string(ldap_state->schema_ver,
393 sid_to_fstring(sid_string, sid),
394 get_objclass_filter(ldap_state->schema_ver));
396 return LDAP_NO_MEMORY;
399 rc = smbldap_search_suffix(ldap_state->smbldap_state,
400 filter, attr, result);
406 /*******************************************************************
407 Delete complete object or objectclass and attrs from
408 object found in search_result depending on lp_ldap_delete_dn
409 ******************************************************************/
411 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
414 const char *objectclass,
417 LDAPMod **mods = NULL;
420 BerElement *ptr = NULL;
422 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
424 return LDAP_NO_MEMORY;
427 if (lp_ldap_delete_dn()) {
428 return smbldap_delete(priv->smbldap_state, dn);
431 /* Ok, delete only the SAM attributes */
433 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
435 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
438 /* We are only allowed to delete the attributes that
441 for (attrib = attrs; *attrib != NULL; attrib++) {
442 if (strequal(*attrib, name)) {
443 DEBUG(10, ("ldapsam_delete_entry: deleting "
444 "attribute %s\n", name));
445 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
456 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
457 talloc_autofree_ldapmod(mem_ctx, mods);
459 return smbldap_modify(priv->smbldap_state, dn, mods);
462 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
467 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
468 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
474 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
475 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
485 /**********************************************************************
486 Initialize struct samu from an LDAP query.
487 (Based on init_sam_from_buffer in pdb_tdb.c)
488 *********************************************************************/
490 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
491 struct samu * sampass,
498 pass_can_change_time,
499 pass_must_change_time,
502 char *username = NULL,
508 *logon_script = NULL,
509 *profile_path = NULL,
511 *workstations = NULL,
514 uint8 smblmpwd[LM_HASH_LEN],
515 smbntpwd[NT_HASH_LEN];
516 bool use_samba_attrs = True;
517 uint32_t acct_ctrl = 0;
519 uint16_t bad_password_count = 0,
522 uint8 hours[MAX_HOURS_LEN];
524 struct login_cache cache_entry;
526 bool expand_explicit = lp_passdb_expand_explicit();
528 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
533 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
534 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
538 if (priv2ld(ldap_state) == NULL) {
539 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
540 "ldap_struct is NULL!\n"));
544 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
548 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
553 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
555 nt_username = talloc_strdup(ctx, username);
560 domain = talloc_strdup(ctx, ldap_state->domain_name);
565 pdb_set_username(sampass, username, PDB_SET);
567 pdb_set_domain(sampass, domain, PDB_DEFAULT);
568 pdb_set_nt_username(sampass, nt_username, PDB_SET);
570 /* deal with different attributes between the schema first */
572 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
573 if ((temp = smbldap_talloc_single_attribute(
574 ldap_state->smbldap_state->ldap_struct,
576 get_userattr_key2string(ldap_state->schema_ver,
579 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
582 if ((temp = smbldap_talloc_single_attribute(
583 ldap_state->smbldap_state->ldap_struct,
585 get_userattr_key2string(ldap_state->schema_ver,
588 user_rid = (uint32_t)atol(temp);
589 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
593 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
594 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
595 get_userattr_key2string(ldap_state->schema_ver,
597 get_userattr_key2string(ldap_state->schema_ver,
603 temp = smbldap_talloc_single_attribute(
604 ldap_state->smbldap_state->ldap_struct,
606 get_userattr_key2string(ldap_state->schema_ver,
607 LDAP_ATTR_PWD_LAST_SET),
610 pass_last_set_time = (time_t) atol(temp);
611 pdb_set_pass_last_set_time(sampass,
612 pass_last_set_time, PDB_SET);
615 temp = smbldap_talloc_single_attribute(
616 ldap_state->smbldap_state->ldap_struct,
618 get_userattr_key2string(ldap_state->schema_ver,
619 LDAP_ATTR_LOGON_TIME),
622 logon_time = (time_t) atol(temp);
623 pdb_set_logon_time(sampass, logon_time, PDB_SET);
626 temp = smbldap_talloc_single_attribute(
627 ldap_state->smbldap_state->ldap_struct,
629 get_userattr_key2string(ldap_state->schema_ver,
630 LDAP_ATTR_LOGOFF_TIME),
633 logoff_time = (time_t) atol(temp);
634 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
637 temp = smbldap_talloc_single_attribute(
638 ldap_state->smbldap_state->ldap_struct,
640 get_userattr_key2string(ldap_state->schema_ver,
641 LDAP_ATTR_KICKOFF_TIME),
644 kickoff_time = (time_t) atol(temp);
645 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
648 temp = smbldap_talloc_single_attribute(
649 ldap_state->smbldap_state->ldap_struct,
651 get_userattr_key2string(ldap_state->schema_ver,
652 LDAP_ATTR_PWD_CAN_CHANGE),
655 pass_can_change_time = (time_t) atol(temp);
656 pdb_set_pass_can_change_time(sampass,
657 pass_can_change_time, PDB_SET);
660 temp = smbldap_talloc_single_attribute(
661 ldap_state->smbldap_state->ldap_struct,
663 get_userattr_key2string(ldap_state->schema_ver,
664 LDAP_ATTR_PWD_MUST_CHANGE),
667 pass_must_change_time = (time_t) atol(temp);
668 pdb_set_pass_must_change_time(sampass,
669 pass_must_change_time, PDB_SET);
672 /* recommend that 'gecos' and 'displayName' should refer to the same
673 * attribute OID. userFullName depreciated, only used by Samba
674 * primary rules of LDAP: don't make a new attribute when one is already defined
675 * that fits your needs; using cn then displayName rather than 'userFullName'
678 fullname = smbldap_talloc_single_attribute(
679 ldap_state->smbldap_state->ldap_struct,
681 get_userattr_key2string(ldap_state->schema_ver,
682 LDAP_ATTR_DISPLAY_NAME),
685 pdb_set_fullname(sampass, fullname, PDB_SET);
687 fullname = smbldap_talloc_single_attribute(
688 ldap_state->smbldap_state->ldap_struct,
690 get_userattr_key2string(ldap_state->schema_ver,
694 pdb_set_fullname(sampass, fullname, PDB_SET);
698 dir_drive = smbldap_talloc_single_attribute(
699 ldap_state->smbldap_state->ldap_struct,
701 get_userattr_key2string(ldap_state->schema_ver,
702 LDAP_ATTR_HOME_DRIVE),
705 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
707 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
710 homedir = smbldap_talloc_single_attribute(
711 ldap_state->smbldap_state->ldap_struct,
713 get_userattr_key2string(ldap_state->schema_ver,
714 LDAP_ATTR_HOME_PATH),
717 if (expand_explicit) {
718 homedir = talloc_sub_basic(ctx,
726 pdb_set_homedir(sampass, homedir, PDB_SET);
728 pdb_set_homedir(sampass,
729 talloc_sub_basic(ctx, username, domain,
734 logon_script = smbldap_talloc_single_attribute(
735 ldap_state->smbldap_state->ldap_struct,
737 get_userattr_key2string(ldap_state->schema_ver,
738 LDAP_ATTR_LOGON_SCRIPT),
741 if (expand_explicit) {
742 logon_script = talloc_sub_basic(ctx,
750 pdb_set_logon_script(sampass, logon_script, PDB_SET);
752 pdb_set_logon_script(sampass,
753 talloc_sub_basic(ctx, username, domain,
758 profile_path = smbldap_talloc_single_attribute(
759 ldap_state->smbldap_state->ldap_struct,
761 get_userattr_key2string(ldap_state->schema_ver,
762 LDAP_ATTR_PROFILE_PATH),
765 if (expand_explicit) {
766 profile_path = talloc_sub_basic(ctx,
774 pdb_set_profile_path(sampass, profile_path, PDB_SET);
776 pdb_set_profile_path(sampass,
777 talloc_sub_basic(ctx, username, domain,
782 acct_desc = smbldap_talloc_single_attribute(
783 ldap_state->smbldap_state->ldap_struct,
785 get_userattr_key2string(ldap_state->schema_ver,
789 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
792 workstations = smbldap_talloc_single_attribute(
793 ldap_state->smbldap_state->ldap_struct,
795 get_userattr_key2string(ldap_state->schema_ver,
799 pdb_set_workstations(sampass, workstations, PDB_SET);
802 munged_dial = smbldap_talloc_single_attribute(
803 ldap_state->smbldap_state->ldap_struct,
805 get_userattr_key2string(ldap_state->schema_ver,
806 LDAP_ATTR_MUNGED_DIAL),
809 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
812 /* FIXME: hours stuff should be cleaner */
816 memset(hours, 0xff, hours_len);
818 if (ldap_state->is_nds_ldap) {
821 char clear_text_pw[512];
823 /* Make call to Novell eDirectory ldap extension to get clear text password.
824 NOTE: This will only work if we have an SSL connection to eDirectory. */
825 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
826 if (user_dn != NULL) {
827 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
829 pwd_len = sizeof(clear_text_pw);
830 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
831 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
832 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
833 TALLOC_FREE(user_dn);
836 ZERO_STRUCT(smblmpwd);
837 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
838 TALLOC_FREE(user_dn);
841 ZERO_STRUCT(smbntpwd);
842 use_samba_attrs = False;
845 TALLOC_FREE(user_dn);
848 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
852 if (use_samba_attrs) {
853 temp = smbldap_talloc_single_attribute(
854 ldap_state->smbldap_state->ldap_struct,
856 get_userattr_key2string(ldap_state->schema_ver,
860 pdb_gethexpwd(temp, smblmpwd);
861 memset((char *)temp, '\0', strlen(temp)+1);
862 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
865 ZERO_STRUCT(smblmpwd);
868 temp = smbldap_talloc_single_attribute(
869 ldap_state->smbldap_state->ldap_struct,
871 get_userattr_key2string(ldap_state->schema_ver,
875 pdb_gethexpwd(temp, smbntpwd);
876 memset((char *)temp, '\0', strlen(temp)+1);
877 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
880 ZERO_STRUCT(smbntpwd);
886 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
888 uint8 *pwhist = NULL;
890 char *history_string = talloc_array(ctx, char,
891 MAX_PW_HISTORY_LEN*64);
893 if (!history_string) {
897 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
899 pwhist = talloc_array(ctx, uint8,
900 pwHistLen * PW_HISTORY_ENTRY_LEN);
901 if (pwhist == NULL) {
902 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
905 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
907 if (smbldap_get_single_attribute(
908 ldap_state->smbldap_state->ldap_struct,
910 get_userattr_key2string(ldap_state->schema_ver,
911 LDAP_ATTR_PWD_HISTORY),
913 MAX_PW_HISTORY_LEN*64)) {
914 bool hex_failed = false;
915 for (i = 0; i < pwHistLen; i++){
916 /* Get the 16 byte salt. */
917 if (!pdb_gethexpwd(&history_string[i*64],
918 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
922 /* Get the 16 byte MD5 hash of salt+passwd. */
923 if (!pdb_gethexpwd(&history_string[(i*64)+32],
924 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
925 PW_HISTORY_SALT_LEN])) {
931 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
933 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
936 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
941 temp = smbldap_talloc_single_attribute(
942 ldap_state->smbldap_state->ldap_struct,
944 get_userattr_key2string(ldap_state->schema_ver,
948 acct_ctrl = pdb_decode_acct_ctrl(temp);
950 if (acct_ctrl == 0) {
951 acct_ctrl |= ACB_NORMAL;
954 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
956 acct_ctrl |= ACB_NORMAL;
959 pdb_set_hours_len(sampass, hours_len, PDB_SET);
960 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
962 temp = smbldap_talloc_single_attribute(
963 ldap_state->smbldap_state->ldap_struct,
965 get_userattr_key2string(ldap_state->schema_ver,
966 LDAP_ATTR_BAD_PASSWORD_COUNT),
969 bad_password_count = (uint32_t) atol(temp);
970 pdb_set_bad_password_count(sampass,
971 bad_password_count, PDB_SET);
974 temp = smbldap_talloc_single_attribute(
975 ldap_state->smbldap_state->ldap_struct,
977 get_userattr_key2string(ldap_state->schema_ver,
978 LDAP_ATTR_BAD_PASSWORD_TIME),
981 bad_password_time = (time_t) atol(temp);
982 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
986 temp = smbldap_talloc_single_attribute(
987 ldap_state->smbldap_state->ldap_struct,
989 get_userattr_key2string(ldap_state->schema_ver,
990 LDAP_ATTR_LOGON_COUNT),
993 logon_count = (uint32_t) atol(temp);
994 pdb_set_logon_count(sampass, logon_count, PDB_SET);
997 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
999 temp = smbldap_talloc_single_attribute(
1000 ldap_state->smbldap_state->ldap_struct,
1002 get_userattr_key2string(ldap_state->schema_ver,
1003 LDAP_ATTR_LOGON_HOURS),
1006 pdb_gethexhours(temp, hours);
1007 memset((char *)temp, '\0', strlen(temp) +1);
1008 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
1012 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1013 struct passwd unix_pw;
1014 bool have_uid = false;
1015 bool have_gid = false;
1016 struct dom_sid mapped_gsid;
1017 const struct dom_sid *primary_gsid;
1019 ZERO_STRUCT(unix_pw);
1021 unix_pw.pw_name = username;
1022 unix_pw.pw_passwd = discard_const_p(char, "x");
1024 temp = smbldap_talloc_single_attribute(
1025 priv2ld(ldap_state),
1030 /* We've got a uid, feed the cache */
1031 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1034 temp = smbldap_talloc_single_attribute(
1035 priv2ld(ldap_state),
1040 /* We've got a uid, feed the cache */
1041 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1044 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1045 priv2ld(ldap_state),
1049 if (unix_pw.pw_gecos) {
1050 unix_pw.pw_gecos = fullname;
1052 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1053 priv2ld(ldap_state),
1057 if (unix_pw.pw_dir) {
1058 unix_pw.pw_dir = discard_const_p(char, "");
1060 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1061 priv2ld(ldap_state),
1065 if (unix_pw.pw_shell) {
1066 unix_pw.pw_shell = discard_const_p(char, "");
1069 if (have_uid && have_gid) {
1070 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1072 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1075 if (sampass->unix_pw == NULL) {
1076 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1077 pdb_get_username(sampass)));
1081 store_uid_sid_cache(pdb_get_user_sid(sampass),
1082 sampass->unix_pw->pw_uid);
1083 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
1084 sampass->unix_pw->pw_uid);
1086 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1087 primary_gsid = pdb_get_group_sid(sampass);
1088 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1089 store_gid_sid_cache(primary_gsid,
1090 sampass->unix_pw->pw_gid);
1091 idmap_cache_set_sid2gid(primary_gsid,
1092 sampass->unix_pw->pw_gid);
1096 /* check the timestamp of the cache vs ldap entry */
1097 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1103 /* see if we have newer updates */
1104 if (!login_cache_read(sampass, &cache_entry)) {
1105 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1106 (unsigned int)pdb_get_bad_password_count(sampass),
1107 (unsigned int)pdb_get_bad_password_time(sampass)));
1112 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1113 (unsigned int)ldap_entry_time,
1114 (unsigned int)cache_entry.entry_timestamp,
1115 (unsigned int)cache_entry.bad_password_time));
1117 if (ldap_entry_time > cache_entry.entry_timestamp) {
1118 /* cache is older than directory , so
1119 we need to delete the entry but allow the
1120 fields to be written out */
1121 login_cache_delentry(sampass);
1124 pdb_set_acct_ctrl(sampass,
1125 pdb_get_acct_ctrl(sampass) |
1126 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1128 pdb_set_bad_password_count(sampass,
1129 cache_entry.bad_password_count,
1131 pdb_set_bad_password_time(sampass,
1132 cache_entry.bad_password_time,
1144 /**********************************************************************
1145 Initialize the ldap db from a struct samu. Called on update.
1146 (Based on init_buffer_from_sam in pdb_tdb.c)
1147 *********************************************************************/
1149 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1150 LDAPMessage *existing,
1151 LDAPMod *** mods, struct samu * sampass,
1152 bool (*need_update)(const struct samu *,
1158 if (mods == NULL || sampass == NULL) {
1159 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1166 * took out adding "objectclass: sambaAccount"
1167 * do this on a per-mod basis
1169 if (need_update(sampass, PDB_USERNAME)) {
1170 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1171 "uid", pdb_get_username(sampass));
1172 if (ldap_state->is_nds_ldap) {
1173 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1174 "cn", pdb_get_username(sampass));
1175 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1176 "sn", pdb_get_username(sampass));
1180 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1182 /* only update the RID if we actually need to */
1183 if (need_update(sampass, PDB_USERSID)) {
1185 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1187 switch ( ldap_state->schema_ver ) {
1188 case SCHEMAVER_SAMBAACCOUNT:
1189 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1190 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1191 sid_string_dbg(user_sid),
1193 &ldap_state->domain_sid)));
1196 if (asprintf(&temp, "%i", rid) < 0) {
1199 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1200 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1205 case SCHEMAVER_SAMBASAMACCOUNT:
1206 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1207 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1208 sid_to_fstring(sid_string, user_sid));
1212 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1217 /* we don't need to store the primary group RID - so leaving it
1218 'free' to hang off the unix primary group makes life easier */
1220 if (need_update(sampass, PDB_GROUPSID)) {
1222 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1224 switch ( ldap_state->schema_ver ) {
1225 case SCHEMAVER_SAMBAACCOUNT:
1226 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1227 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1228 sid_string_dbg(group_sid),
1230 &ldap_state->domain_sid)));
1234 if (asprintf(&temp, "%i", rid) < 0) {
1237 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1238 get_userattr_key2string(ldap_state->schema_ver,
1239 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1243 case SCHEMAVER_SAMBASAMACCOUNT:
1244 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1245 get_userattr_key2string(ldap_state->schema_ver,
1246 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1250 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1256 /* displayName, cn, and gecos should all be the same
1257 * most easily accomplished by giving them the same OID
1258 * gecos isn't set here b/c it should be handled by the
1260 * We change displayName only and fall back to cn if
1261 * it does not exist.
1264 if (need_update(sampass, PDB_FULLNAME))
1265 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1266 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1267 pdb_get_fullname(sampass));
1269 if (need_update(sampass, PDB_ACCTDESC))
1270 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1271 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1272 pdb_get_acct_desc(sampass));
1274 if (need_update(sampass, PDB_WORKSTATIONS))
1275 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1276 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1277 pdb_get_workstations(sampass));
1279 if (need_update(sampass, PDB_MUNGEDDIAL))
1280 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1281 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1282 pdb_get_munged_dial(sampass));
1284 if (need_update(sampass, PDB_SMBHOME))
1285 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1286 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1287 pdb_get_homedir(sampass));
1289 if (need_update(sampass, PDB_DRIVE))
1290 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1291 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1292 pdb_get_dir_drive(sampass));
1294 if (need_update(sampass, PDB_LOGONSCRIPT))
1295 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1296 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1297 pdb_get_logon_script(sampass));
1299 if (need_update(sampass, PDB_PROFILE))
1300 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1301 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1302 pdb_get_profile_path(sampass));
1304 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1307 if (need_update(sampass, PDB_LOGONTIME))
1308 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1309 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1312 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1315 if (need_update(sampass, PDB_LOGOFFTIME))
1316 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1317 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1320 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1323 if (need_update(sampass, PDB_KICKOFFTIME))
1324 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1325 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1328 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1331 if (need_update(sampass, PDB_CANCHANGETIME))
1332 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1333 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1336 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1339 if (need_update(sampass, PDB_MUSTCHANGETIME))
1340 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1341 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1344 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1345 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1347 if (need_update(sampass, PDB_LMPASSWD)) {
1348 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1351 pdb_sethexpwd(pwstr, lm_pw,
1352 pdb_get_acct_ctrl(sampass));
1353 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1354 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1357 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1358 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1362 if (need_update(sampass, PDB_NTPASSWD)) {
1363 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1366 pdb_sethexpwd(pwstr, nt_pw,
1367 pdb_get_acct_ctrl(sampass));
1368 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1369 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1372 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1373 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1378 if (need_update(sampass, PDB_PWHISTORY)) {
1380 uint32_t pwHistLen = 0;
1381 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1383 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1387 if (pwHistLen == 0) {
1388 /* Remove any password history from the LDAP store. */
1389 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1393 uint32_t currHistLen = 0;
1394 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1395 if (pwhist != NULL) {
1396 /* We can only store (1024-1/64 password history entries. */
1397 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1398 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1399 /* Store the salt. */
1400 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1401 /* Followed by the md5 hash of salt + md4 hash */
1402 pdb_sethexpwd(&pwstr[(i*64)+32],
1403 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1404 DEBUG(100, ("pwstr=%s\n", pwstr));
1408 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1409 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1414 if (need_update(sampass, PDB_PASSLASTSET)) {
1415 if (asprintf(&temp, "%li",
1416 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1419 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1420 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1426 if (need_update(sampass, PDB_HOURS)) {
1427 const uint8 *hours = pdb_get_hours(sampass);
1430 pdb_sethexhours(hourstr, hours);
1431 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1434 get_userattr_key2string(ldap_state->schema_ver,
1435 LDAP_ATTR_LOGON_HOURS),
1440 if (need_update(sampass, PDB_ACCTCTRL))
1441 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1442 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1443 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1445 /* password lockout cache:
1446 - If we are now autolocking or clearing, we write to ldap
1447 - If we are clearing, we delete the cache entry
1448 - If the count is > 0, we update the cache
1450 This even means when autolocking, we cache, just in case the
1451 update doesn't work, and we have to cache the autolock flag */
1453 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1454 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1455 uint16_t badcount = pdb_get_bad_password_count(sampass);
1456 time_t badtime = pdb_get_bad_password_time(sampass);
1458 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1460 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1461 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1463 if ((badcount >= pol) || (badcount == 0)) {
1464 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1465 (unsigned int)badcount, (unsigned int)badtime));
1466 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1470 ldap_state->smbldap_state->ldap_struct,
1472 get_userattr_key2string(
1473 ldap_state->schema_ver,
1474 LDAP_ATTR_BAD_PASSWORD_COUNT),
1478 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1482 ldap_state->smbldap_state->ldap_struct,
1484 get_userattr_key2string(
1485 ldap_state->schema_ver,
1486 LDAP_ATTR_BAD_PASSWORD_TIME),
1490 if (badcount == 0) {
1491 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1492 login_cache_delentry(sampass);
1494 struct login_cache cache_entry;
1496 cache_entry.entry_timestamp = time(NULL);
1497 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1498 cache_entry.bad_password_count = badcount;
1499 cache_entry.bad_password_time = badtime;
1501 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1502 login_cache_write(sampass, &cache_entry);
1509 /**********************************************************************
1510 End enumeration of the LDAP password list.
1511 *********************************************************************/
1513 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1515 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1516 if (ldap_state->result) {
1517 ldap_msgfree(ldap_state->result);
1518 ldap_state->result = NULL;
1522 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1523 const char *new_attr)
1527 if (new_attr == NULL) {
1531 for (i=0; (*attr_list)[i] != NULL; i++) {
1535 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1537 SMB_ASSERT((*attr_list) != NULL);
1538 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1539 (*attr_list)[i+1] = NULL;
1542 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1543 const char ***attr_list)
1545 append_attr(mem_ctx, attr_list, "uidNumber");
1546 append_attr(mem_ctx, attr_list, "gidNumber");
1547 append_attr(mem_ctx, attr_list, "homeDirectory");
1548 append_attr(mem_ctx, attr_list, "loginShell");
1549 append_attr(mem_ctx, attr_list, "gecos");
1552 /**********************************************************************
1553 Get struct samu entry from LDAP by username.
1554 *********************************************************************/
1556 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1558 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1559 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1560 LDAPMessage *result = NULL;
1561 LDAPMessage *entry = NULL;
1563 const char ** attr_list;
1566 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1567 append_attr(user, &attr_list,
1568 get_userattr_key2string(ldap_state->schema_ver,
1569 LDAP_ATTR_MOD_TIMESTAMP));
1570 ldapsam_add_unix_attributes(user, &attr_list);
1571 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1573 TALLOC_FREE( attr_list );
1575 if ( rc != LDAP_SUCCESS )
1576 return NT_STATUS_NO_SUCH_USER;
1578 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1581 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1582 ldap_msgfree(result);
1583 return NT_STATUS_NO_SUCH_USER;
1584 } else if (count > 1) {
1585 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1586 ldap_msgfree(result);
1587 return NT_STATUS_NO_SUCH_USER;
1590 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1592 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1593 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1594 ldap_msgfree(result);
1595 return NT_STATUS_NO_SUCH_USER;
1597 pdb_set_backend_private_data(user, result, NULL,
1598 my_methods, PDB_CHANGED);
1599 talloc_autofree_ldapmsg(user, result);
1602 ldap_msgfree(result);
1607 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1608 const struct dom_sid *sid, LDAPMessage **result)
1611 const char ** attr_list;
1614 switch ( ldap_state->schema_ver ) {
1615 case SCHEMAVER_SAMBASAMACCOUNT: {
1616 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1617 if (tmp_ctx == NULL) {
1618 return LDAP_NO_MEMORY;
1621 attr_list = get_userattr_list(tmp_ctx,
1622 ldap_state->schema_ver);
1623 append_attr(tmp_ctx, &attr_list,
1624 get_userattr_key2string(
1625 ldap_state->schema_ver,
1626 LDAP_ATTR_MOD_TIMESTAMP));
1627 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1628 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1630 TALLOC_FREE(tmp_ctx);
1632 if ( rc != LDAP_SUCCESS )
1637 case SCHEMAVER_SAMBAACCOUNT:
1638 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1642 attr_list = get_userattr_list(NULL,
1643 ldap_state->schema_ver);
1644 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1645 TALLOC_FREE( attr_list );
1647 if ( rc != LDAP_SUCCESS )
1654 /**********************************************************************
1655 Get struct samu entry from LDAP by SID.
1656 *********************************************************************/
1658 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1660 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1661 LDAPMessage *result = NULL;
1662 LDAPMessage *entry = NULL;
1666 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1668 if (rc != LDAP_SUCCESS)
1669 return NT_STATUS_NO_SUCH_USER;
1671 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1674 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1675 "count=%d\n", sid_string_dbg(sid), count));
1676 ldap_msgfree(result);
1677 return NT_STATUS_NO_SUCH_USER;
1678 } else if (count > 1) {
1679 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1680 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1682 ldap_msgfree(result);
1683 return NT_STATUS_NO_SUCH_USER;
1686 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1688 ldap_msgfree(result);
1689 return NT_STATUS_NO_SUCH_USER;
1692 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1693 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1694 ldap_msgfree(result);
1695 return NT_STATUS_NO_SUCH_USER;
1698 pdb_set_backend_private_data(user, result, NULL,
1699 my_methods, PDB_CHANGED);
1700 talloc_autofree_ldapmsg(user, result);
1701 return NT_STATUS_OK;
1704 /********************************************************************
1705 Do the actual modification - also change a plaintext passord if
1707 **********************************************************************/
1709 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1710 struct samu *newpwd, char *dn,
1711 LDAPMod **mods, int ldap_op,
1712 bool (*need_update)(const struct samu *, enum pdb_elements))
1714 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1717 if (!newpwd || !dn) {
1718 return NT_STATUS_INVALID_PARAMETER;
1721 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1722 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1723 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1724 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1727 char *retoid = NULL;
1728 struct berval *retdata = NULL;
1729 char *utf8_password;
1731 size_t converted_size;
1734 if (!ldap_state->is_nds_ldap) {
1736 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1737 LDAP_EXOP_MODIFY_PASSWD)) {
1738 DEBUG(2, ("ldap password change requested, but LDAP "
1739 "server does not support it -- ignoring\n"));
1740 return NT_STATUS_OK;
1744 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1745 pdb_get_plaintext_passwd(newpwd),
1748 return NT_STATUS_NO_MEMORY;
1751 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1752 TALLOC_FREE(utf8_password);
1753 return NT_STATUS_NO_MEMORY;
1756 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1757 DEBUG(0,("ber_alloc_t returns NULL\n"));
1758 TALLOC_FREE(utf8_password);
1759 TALLOC_FREE(utf8_dn);
1760 return NT_STATUS_UNSUCCESSFUL;
1763 if ((ber_printf (ber, "{") < 0) ||
1764 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1766 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1769 TALLOC_FREE(utf8_dn);
1770 TALLOC_FREE(utf8_password);
1771 return NT_STATUS_UNSUCCESSFUL;
1774 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1775 ret = ber_printf(ber, "ts}",
1776 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1779 ret = ber_printf(ber, "}");
1783 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1786 TALLOC_FREE(utf8_dn);
1787 TALLOC_FREE(utf8_password);
1788 return NT_STATUS_UNSUCCESSFUL;
1791 if ((rc = ber_flatten (ber, &bv))<0) {
1792 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1794 TALLOC_FREE(utf8_dn);
1795 TALLOC_FREE(utf8_password);
1796 return NT_STATUS_UNSUCCESSFUL;
1799 TALLOC_FREE(utf8_dn);
1800 TALLOC_FREE(utf8_password);
1803 if (!ldap_state->is_nds_ldap) {
1804 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1805 LDAP_EXOP_MODIFY_PASSWD,
1806 bv, NULL, NULL, &retoid,
1809 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1810 pdb_get_plaintext_passwd(newpwd));
1812 if (rc != LDAP_SUCCESS) {
1813 char *ld_error = NULL;
1815 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1816 DEBUG(3, ("Could not set userPassword "
1817 "attribute due to an objectClass "
1818 "violation -- ignoring\n"));
1820 return NT_STATUS_OK;
1823 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1825 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1826 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1827 SAFE_FREE(ld_error);
1829 #if defined(LDAP_CONSTRAINT_VIOLATION)
1830 if (rc == LDAP_CONSTRAINT_VIOLATION)
1831 return NT_STATUS_PASSWORD_RESTRICTION;
1833 return NT_STATUS_UNSUCCESSFUL;
1835 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1836 #ifdef DEBUG_PASSWORD
1837 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1840 ber_bvfree(retdata);
1842 ldap_memfree(retoid);
1848 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1849 /* may be password change below however */
1853 if (ldap_state->is_nds_ldap) {
1854 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1858 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1862 rc = smbldap_add(ldap_state->smbldap_state,
1865 case LDAP_MOD_REPLACE:
1866 rc = smbldap_modify(ldap_state->smbldap_state,
1870 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1872 return NT_STATUS_INVALID_PARAMETER;
1875 if (rc!=LDAP_SUCCESS) {
1876 return NT_STATUS_UNSUCCESSFUL;
1880 return NT_STATUS_OK;
1883 /**********************************************************************
1884 Delete entry from LDAP for username.
1885 *********************************************************************/
1887 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1888 struct samu * sam_acct)
1890 struct ldapsam_privates *priv =
1891 (struct ldapsam_privates *)my_methods->private_data;
1894 LDAPMessage *msg, *entry;
1895 NTSTATUS result = NT_STATUS_NO_MEMORY;
1896 const char **attr_list;
1897 TALLOC_CTX *mem_ctx;
1900 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1901 return NT_STATUS_INVALID_PARAMETER;
1904 sname = pdb_get_username(sam_acct);
1906 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1909 mem_ctx = talloc_new(NULL);
1910 if (mem_ctx == NULL) {
1911 DEBUG(0, ("talloc_new failed\n"));
1915 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1916 if (attr_list == NULL) {
1920 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1922 if ((rc != LDAP_SUCCESS) ||
1923 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1924 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1925 DEBUG(5, ("Could not find user %s\n", sname));
1926 result = NT_STATUS_NO_SUCH_USER;
1930 rc = ldapsam_delete_entry(
1931 priv, mem_ctx, entry,
1932 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1933 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1936 result = (rc == LDAP_SUCCESS) ?
1937 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1940 TALLOC_FREE(mem_ctx);
1944 /**********************************************************************
1945 Helper function to determine for update_sam_account whether
1946 we need LDAP modification.
1947 *********************************************************************/
1949 static bool element_is_changed(const struct samu *sampass,
1950 enum pdb_elements element)
1952 return IS_SAM_CHANGED(sampass, element);
1955 /**********************************************************************
1957 *********************************************************************/
1959 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1961 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1962 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1965 LDAPMessage *result = NULL;
1966 LDAPMessage *entry = NULL;
1967 LDAPMod **mods = NULL;
1968 const char **attr_list;
1970 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1972 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1973 if (pdb_get_username(newpwd) == NULL) {
1974 return NT_STATUS_INVALID_PARAMETER;
1976 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1977 TALLOC_FREE( attr_list );
1978 if (rc != LDAP_SUCCESS) {
1979 return NT_STATUS_UNSUCCESSFUL;
1981 pdb_set_backend_private_data(newpwd, result, NULL,
1982 my_methods, PDB_CHANGED);
1983 talloc_autofree_ldapmsg(newpwd, result);
1986 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1987 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1988 return NT_STATUS_UNSUCCESSFUL;
1991 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1992 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1994 return NT_STATUS_UNSUCCESSFUL;
1997 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1999 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2000 element_is_changed)) {
2001 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2004 ldap_mods_free(mods,True);
2005 return NT_STATUS_UNSUCCESSFUL;
2008 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
2009 && (mods == NULL)) {
2010 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2011 pdb_get_username(newpwd)));
2013 return NT_STATUS_OK;
2016 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
2019 ldap_mods_free(mods,True);
2025 * We need to set the backend private data to NULL here. For example
2026 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2027 * same one, and with the explicit delete / add logic for attribute
2028 * values the second time we would use the wrong "old" value which
2029 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2031 * The existing LDAPMessage is still being auto-freed by the
2034 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2037 if (!NT_STATUS_IS_OK(ret)) {
2041 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2042 pdb_get_username(newpwd)));
2043 return NT_STATUS_OK;
2046 /***************************************************************************
2047 Renames a struct samu
2048 - The "rename user script" has full responsibility for changing everything
2049 ***************************************************************************/
2051 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2052 TALLOC_CTX *tmp_ctx,
2054 uint32_t member_rid);
2056 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2057 TALLOC_CTX *mem_ctx,
2059 struct dom_sid **pp_sids,
2061 uint32_t *p_num_groups);
2063 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2064 struct samu *old_acct,
2065 const char *newname)
2067 const char *oldname;
2069 char *rename_script = NULL;
2070 fstring oldname_lower, newname_lower;
2073 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2074 return NT_STATUS_INVALID_PARAMETER;
2077 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2078 return NT_STATUS_INVALID_PARAMETER;
2081 oldname = pdb_get_username(old_acct);
2083 /* rename the posix user */
2084 rename_script = SMB_STRDUP(lp_renameuser_script());
2085 if (rename_script == NULL) {
2086 return NT_STATUS_NO_MEMORY;
2089 if (!(*rename_script)) {
2090 SAFE_FREE(rename_script);
2091 return NT_STATUS_ACCESS_DENIED;
2094 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2097 /* We have to allow the account name to end with a '$'.
2098 Also, follow the semantics in _samr_create_user() and lower case the
2099 posix name but preserve the case in passdb */
2101 fstrcpy( oldname_lower, oldname );
2102 strlower_m( oldname_lower );
2103 fstrcpy( newname_lower, newname );
2104 strlower_m( newname_lower );
2105 rename_script = realloc_string_sub2(rename_script,
2110 if (!rename_script) {
2111 return NT_STATUS_NO_MEMORY;
2113 rename_script = realloc_string_sub2(rename_script,
2118 rc = smbrun(rename_script, NULL);
2120 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2121 rename_script, rc));
2123 SAFE_FREE(rename_script);
2126 smb_nscd_flush_user_cache();
2130 return NT_STATUS_UNSUCCESSFUL;
2132 return NT_STATUS_OK;
2135 /**********************************************************************
2136 Helper function to determine for update_sam_account whether
2137 we need LDAP modification.
2138 *********************************************************************/
2140 static bool element_is_set_or_changed(const struct samu *sampass,
2141 enum pdb_elements element)
2143 return (IS_SAM_SET(sampass, element) ||
2144 IS_SAM_CHANGED(sampass, element));
2147 /**********************************************************************
2148 Add struct samu to LDAP.
2149 *********************************************************************/
2151 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2153 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2154 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2156 LDAPMessage *result = NULL;
2157 LDAPMessage *entry = NULL;
2158 LDAPMod **mods = NULL;
2159 int ldap_op = LDAP_MOD_REPLACE;
2160 uint32_t num_result;
2161 const char **attr_list;
2162 char *escape_user = NULL;
2163 const char *username = pdb_get_username(newpwd);
2164 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2165 char *filter = NULL;
2167 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2168 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2171 return NT_STATUS_NO_MEMORY;
2174 if (!username || !*username) {
2175 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2176 status = NT_STATUS_INVALID_PARAMETER;
2180 /* free this list after the second search or in case we exit on failure */
2181 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2183 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2185 if (rc != LDAP_SUCCESS) {
2189 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2190 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2194 ldap_msgfree(result);
2197 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2198 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2200 if (rc == LDAP_SUCCESS) {
2201 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2202 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2203 "already in the base, with samba "
2204 "attributes\n", sid_string_dbg(sid)));
2207 ldap_msgfree(result);
2212 /* does the entry already exist but without a samba attributes?
2213 we need to return the samba attributes here */
2215 escape_user = escape_ldap_string(talloc_tos(), username);
2216 filter = talloc_strdup(attr_list, "(uid=%u)");
2218 status = NT_STATUS_NO_MEMORY;
2221 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2222 TALLOC_FREE(escape_user);
2224 status = NT_STATUS_NO_MEMORY;
2228 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2229 filter, attr_list, &result);
2230 if ( rc != LDAP_SUCCESS ) {
2234 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2236 if (num_result > 1) {
2237 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2241 /* Check if we need to update an existing entry */
2242 if (num_result == 1) {
2243 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2244 ldap_op = LDAP_MOD_REPLACE;
2245 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2246 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2248 status = NT_STATUS_NO_MEMORY;
2252 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2254 /* There might be a SID for this account already - say an idmap entry */
2256 filter = talloc_asprintf(ctx,
2257 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2258 get_userattr_key2string(ldap_state->schema_ver,
2259 LDAP_ATTR_USER_SID),
2260 sid_string_talloc(ctx, sid),
2261 LDAP_OBJ_IDMAP_ENTRY,
2262 LDAP_OBJ_SID_ENTRY);
2264 status = NT_STATUS_NO_MEMORY;
2268 /* free old result before doing a new search */
2269 if (result != NULL) {
2270 ldap_msgfree(result);
2273 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2274 filter, attr_list, &result);
2276 if ( rc != LDAP_SUCCESS ) {
2280 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2282 if (num_result > 1) {
2283 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2287 /* Check if we need to update an existing entry */
2288 if (num_result == 1) {
2290 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2291 ldap_op = LDAP_MOD_REPLACE;
2292 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2293 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2295 status = NT_STATUS_NO_MEMORY;
2301 if (num_result == 0) {
2302 char *escape_username;
2303 /* Check if we need to add an entry */
2304 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2305 ldap_op = LDAP_MOD_ADD;
2307 escape_username = escape_rdn_val_string_alloc(username);
2308 if (!escape_username) {
2309 status = NT_STATUS_NO_MEMORY;
2313 if (username[strlen(username)-1] == '$') {
2314 dn = talloc_asprintf(ctx,
2317 lp_ldap_machine_suffix());
2319 dn = talloc_asprintf(ctx,
2322 lp_ldap_user_suffix());
2325 SAFE_FREE(escape_username);
2327 status = NT_STATUS_NO_MEMORY;
2332 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2333 element_is_set_or_changed)) {
2334 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2336 ldap_mods_free(mods, true);
2342 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2345 switch ( ldap_state->schema_ver ) {
2346 case SCHEMAVER_SAMBAACCOUNT:
2347 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2349 case SCHEMAVER_SAMBASAMACCOUNT:
2350 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2353 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2357 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2358 if (!NT_STATUS_IS_OK(ret)) {
2359 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2360 pdb_get_username(newpwd),dn));
2361 ldap_mods_free(mods, true);
2365 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2366 ldap_mods_free(mods, true);
2368 status = NT_STATUS_OK;
2374 ldap_msgfree(result);
2379 /**********************************************************************
2380 *********************************************************************/
2382 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2384 LDAPMessage ** result)
2386 int scope = LDAP_SCOPE_SUBTREE;
2388 const char **attr_list;
2390 attr_list = get_attr_list(NULL, groupmap_attr_list);
2391 rc = smbldap_search(ldap_state->smbldap_state,
2392 lp_ldap_suffix (), scope,
2393 filter, attr_list, 0, result);
2394 TALLOC_FREE(attr_list);
2399 /**********************************************************************
2400 *********************************************************************/
2402 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2403 GROUP_MAP *map, LDAPMessage *entry)
2406 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2408 if (ldap_state == NULL || map == NULL || entry == NULL ||
2409 ldap_state->smbldap_state->ldap_struct == NULL) {
2410 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2415 temp = smbldap_talloc_single_attribute(
2416 ldap_state->smbldap_state->ldap_struct,
2418 get_attr_key2string(groupmap_attr_list,
2419 LDAP_ATTR_GIDNUMBER),
2422 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2423 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2427 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2429 map->gid = (gid_t)atol(temp);
2432 temp = smbldap_talloc_single_attribute(
2433 ldap_state->smbldap_state->ldap_struct,
2435 get_attr_key2string(groupmap_attr_list,
2436 LDAP_ATTR_GROUP_SID),
2439 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2440 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2445 if (!string_to_sid(&map->sid, temp)) {
2446 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2452 temp = smbldap_talloc_single_attribute(
2453 ldap_state->smbldap_state->ldap_struct,
2455 get_attr_key2string(groupmap_attr_list,
2456 LDAP_ATTR_GROUP_TYPE),
2459 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2460 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2464 map->sid_name_use = (enum lsa_SidType)atol(temp);
2466 if ((map->sid_name_use < SID_NAME_USER) ||
2467 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2468 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2474 temp = smbldap_talloc_single_attribute(
2475 ldap_state->smbldap_state->ldap_struct,
2477 get_attr_key2string(groupmap_attr_list,
2478 LDAP_ATTR_DISPLAY_NAME),
2481 temp = smbldap_talloc_single_attribute(
2482 ldap_state->smbldap_state->ldap_struct,
2484 get_attr_key2string(groupmap_attr_list,
2488 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2489 for gidNumber(%lu)\n",(unsigned long)map->gid));
2494 fstrcpy(map->nt_name, temp);
2497 temp = smbldap_talloc_single_attribute(
2498 ldap_state->smbldap_state->ldap_struct,
2500 get_attr_key2string(groupmap_attr_list,
2504 temp = talloc_strdup(ctx, "");
2510 fstrcpy(map->comment, temp);
2512 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2513 store_gid_sid_cache(&map->sid, map->gid);
2514 idmap_cache_set_sid2gid(&map->sid, map->gid);
2521 /**********************************************************************
2522 *********************************************************************/
2524 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2528 struct ldapsam_privates *ldap_state =
2529 (struct ldapsam_privates *)methods->private_data;
2530 LDAPMessage *result = NULL;
2531 LDAPMessage *entry = NULL;
2534 if (ldapsam_search_one_group(ldap_state, filter, &result)
2536 return NT_STATUS_NO_SUCH_GROUP;
2539 count = ldap_count_entries(priv2ld(ldap_state), result);
2542 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2544 ldap_msgfree(result);
2545 return NT_STATUS_NO_SUCH_GROUP;
2549 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2550 "count=%d\n", filter, count));
2551 ldap_msgfree(result);
2552 return NT_STATUS_NO_SUCH_GROUP;
2555 entry = ldap_first_entry(priv2ld(ldap_state), result);
2558 ldap_msgfree(result);
2559 return NT_STATUS_UNSUCCESSFUL;
2562 if (!init_group_from_ldap(ldap_state, map, entry)) {
2563 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2564 "group filter %s\n", filter));
2565 ldap_msgfree(result);
2566 return NT_STATUS_NO_SUCH_GROUP;
2569 ldap_msgfree(result);
2570 return NT_STATUS_OK;
2573 /**********************************************************************
2574 *********************************************************************/
2576 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2579 char *filter = NULL;
2583 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2585 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2586 sid_to_fstring(tmp, &sid)) < 0) {
2587 return NT_STATUS_NO_MEMORY;
2590 status = ldapsam_getgroup(methods, filter, map);
2595 /**********************************************************************
2596 *********************************************************************/
2598 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2601 char *filter = NULL;
2604 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2606 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2607 (unsigned long)gid) < 0) {
2608 return NT_STATUS_NO_MEMORY;
2611 status = ldapsam_getgroup(methods, filter, map);
2616 /**********************************************************************
2617 *********************************************************************/
2619 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2622 char *filter = NULL;
2623 char *escape_name = escape_ldap_string(talloc_tos(), name);
2627 return NT_STATUS_NO_MEMORY;
2630 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2632 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2633 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2635 TALLOC_FREE(escape_name);
2636 return NT_STATUS_NO_MEMORY;
2639 TALLOC_FREE(escape_name);
2640 status = ldapsam_getgroup(methods, filter, map);
2645 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2647 const struct dom_sid *domain_sid,
2653 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2654 str, sizeof(str)-1)) {
2655 DEBUG(10, ("Could not find sambaSID attribute\n"));
2659 if (!string_to_sid(&sid, str)) {
2660 DEBUG(10, ("Could not convert string %s to sid\n", str));
2664 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2665 DEBUG(10, ("SID %s is not in expected domain %s\n",
2666 str, sid_string_dbg(domain_sid)));
2670 if (!sid_peek_rid(&sid, rid)) {
2671 DEBUG(10, ("Could not peek into RID\n"));
2678 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2679 TALLOC_CTX *mem_ctx,
2680 const struct dom_sid *group,
2681 uint32_t **pp_member_rids,
2682 size_t *p_num_members)
2684 struct ldapsam_privates *ldap_state =
2685 (struct ldapsam_privates *)methods->private_data;
2686 struct smbldap_state *conn = ldap_state->smbldap_state;
2687 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2688 const char *sid_attrs[] = { "sambaSID", NULL };
2689 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2690 LDAPMessage *result = NULL;
2693 char **values = NULL;
2698 *pp_member_rids = NULL;
2701 filter = talloc_asprintf(mem_ctx,
2702 "(&(objectClass=%s)"
2705 LDAP_OBJ_POSIXGROUP,
2707 sid_string_talloc(mem_ctx, group));
2708 if (filter == NULL) {
2709 ret = NT_STATUS_NO_MEMORY;
2713 rc = smbldap_search(conn, lp_ldap_suffix(),
2714 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2717 if (rc != LDAP_SUCCESS)
2720 talloc_autofree_ldapmsg(mem_ctx, result);
2722 count = ldap_count_entries(conn->ldap_struct, result);
2725 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2726 sid_string_dbg(group)));
2727 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2732 ret = NT_STATUS_NO_SUCH_GROUP;
2736 entry = ldap_first_entry(conn->ldap_struct, result);
2740 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2742 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2743 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2747 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2749 if ((values != NULL) && (values[0] != NULL)) {
2751 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2752 if (filter == NULL) {
2753 ret = NT_STATUS_NO_MEMORY;
2757 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2758 char *escape_memberuid;
2760 escape_memberuid = escape_ldap_string(talloc_tos(),
2762 if (escape_memberuid == NULL) {
2763 ret = NT_STATUS_NO_MEMORY;
2767 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2768 TALLOC_FREE(escape_memberuid);
2769 if (filter == NULL) {
2770 ret = NT_STATUS_NO_MEMORY;
2775 filter = talloc_asprintf_append_buffer(filter, "))");
2776 if (filter == NULL) {
2777 ret = NT_STATUS_NO_MEMORY;
2781 rc = smbldap_search(conn, lp_ldap_suffix(),
2782 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2785 if (rc != LDAP_SUCCESS)
2788 count = ldap_count_entries(conn->ldap_struct, result);
2789 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2791 talloc_autofree_ldapmsg(mem_ctx, result);
2793 for (entry = ldap_first_entry(conn->ldap_struct, result);
2795 entry = ldap_next_entry(conn->ldap_struct, entry))
2801 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2805 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2806 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2807 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2811 if (!string_to_sid(&sid, sidstr))
2814 if (!sid_check_is_in_our_domain(&sid)) {
2815 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2816 "in our domain\n"));
2817 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2821 sid_peek_rid(&sid, &rid);
2823 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2825 ret = NT_STATUS_NO_MEMORY;
2831 filter = talloc_asprintf(mem_ctx,
2832 "(&(objectClass=%s)"
2834 LDAP_OBJ_SAMBASAMACCOUNT,
2837 rc = smbldap_search(conn, lp_ldap_suffix(),
2838 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2841 if (rc != LDAP_SUCCESS)
2844 talloc_autofree_ldapmsg(mem_ctx, result);
2846 for (entry = ldap_first_entry(conn->ldap_struct, result);
2848 entry = ldap_next_entry(conn->ldap_struct, entry))
2852 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2854 get_global_sam_sid(),
2856 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2857 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2861 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2863 ret = NT_STATUS_NO_MEMORY;
2873 ldap_value_free(values);
2878 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2879 TALLOC_CTX *mem_ctx,
2881 struct dom_sid **pp_sids,
2883 uint32_t *p_num_groups)
2885 struct ldapsam_privates *ldap_state =
2886 (struct ldapsam_privates *)methods->private_data;
2887 struct smbldap_state *conn = ldap_state->smbldap_state;
2889 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2892 LDAPMessage *result = NULL;
2894 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2898 gid_t primary_gid = -1;
2903 if (pdb_get_username(user) == NULL) {
2904 return NT_STATUS_INVALID_PARAMETER;
2907 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2908 if (escape_name == NULL)
2909 return NT_STATUS_NO_MEMORY;
2911 if (user->unix_pw) {
2912 primary_gid = user->unix_pw->pw_gid;
2914 /* retrieve the users primary gid */
2915 filter = talloc_asprintf(mem_ctx,
2916 "(&(objectClass=%s)(uid=%s))",
2917 LDAP_OBJ_SAMBASAMACCOUNT,
2919 if (filter == NULL) {
2920 ret = NT_STATUS_NO_MEMORY;
2924 rc = smbldap_search(conn, lp_ldap_suffix(),
2925 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2927 if (rc != LDAP_SUCCESS)
2930 talloc_autofree_ldapmsg(mem_ctx, result);
2932 count = ldap_count_entries(priv2ld(ldap_state), result);
2936 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2937 ret = NT_STATUS_NO_SUCH_USER;
2940 entry = ldap_first_entry(priv2ld(ldap_state), result);
2942 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2944 DEBUG (1, ("Unable to find the member's gid!\n"));
2945 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2948 primary_gid = strtoul(gidstr, NULL, 10);
2951 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2952 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2957 filter = talloc_asprintf(mem_ctx,
2958 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2959 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2960 if (filter == NULL) {
2961 ret = NT_STATUS_NO_MEMORY;
2965 rc = smbldap_search(conn, lp_ldap_suffix(),
2966 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2968 if (rc != LDAP_SUCCESS)
2971 talloc_autofree_ldapmsg(mem_ctx, result);
2979 /* We need to add the primary group as the first gid/sid */
2981 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2982 ret = NT_STATUS_NO_MEMORY;
2986 /* This sid will be replaced later */
2988 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2990 if (!NT_STATUS_IS_OK(ret)) {
2994 for (entry = ldap_first_entry(conn->ldap_struct, result);
2996 entry = ldap_next_entry(conn->ldap_struct, entry))
3003 if (!smbldap_get_single_attribute(conn->ldap_struct,
3005 str, sizeof(str)-1))
3008 if (!string_to_sid(&sid, str))
3011 if (!smbldap_get_single_attribute(conn->ldap_struct,
3013 str, sizeof(str)-1))
3016 gid = strtoul(str, &end, 10);
3018 if (PTR_DIFF(end, str) != strlen(str))
3021 if (gid == primary_gid) {
3022 sid_copy(&(*pp_sids)[0], &sid);
3024 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3026 ret = NT_STATUS_NO_MEMORY;
3029 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3031 if (!NT_STATUS_IS_OK(ret)) {
3037 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3038 DEBUG(3, ("primary group of [%s] not found\n",
3039 pdb_get_username(user)));
3043 *p_num_groups = num_sids;
3049 TALLOC_FREE(escape_name);
3053 /**********************************************************************
3054 * Augment a posixGroup object with a sambaGroupMapping domgroup
3055 *********************************************************************/
3057 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3058 struct ldapsam_privates *ldap_state,
3061 const char *filter, *dn;
3062 LDAPMessage *msg, *entry;
3066 filter = talloc_asprintf(mem_ctx,
3067 "(&(objectClass=%s)(gidNumber=%u))",
3068 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3069 if (filter == NULL) {
3070 return NT_STATUS_NO_MEMORY;
3073 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3074 get_attr_list(mem_ctx, groupmap_attr_list),
3076 talloc_autofree_ldapmsg(mem_ctx, msg);
3078 if ((rc != LDAP_SUCCESS) ||
3079 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3080 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3081 return NT_STATUS_NO_SUCH_GROUP;
3084 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3086 return NT_STATUS_NO_MEMORY;
3090 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3092 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3093 sid_string_talloc(mem_ctx, &map->sid));
3094 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3095 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3096 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3098 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3100 talloc_autofree_ldapmod(mem_ctx, mods);
3102 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3103 if (rc != LDAP_SUCCESS) {
3104 return NT_STATUS_ACCESS_DENIED;
3107 return NT_STATUS_OK;
3110 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3113 struct ldapsam_privates *ldap_state =
3114 (struct ldapsam_privates *)methods->private_data;
3115 LDAPMessage *msg = NULL;
3116 LDAPMod **mods = NULL;
3117 const char *attrs[] = { NULL };
3121 TALLOC_CTX *mem_ctx;
3128 mem_ctx = talloc_new(NULL);
3129 if (mem_ctx == NULL) {
3130 DEBUG(0, ("talloc_new failed\n"));
3131 return NT_STATUS_NO_MEMORY;
3134 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3135 sid_string_talloc(mem_ctx, &map->sid));
3136 if (filter == NULL) {
3137 result = NT_STATUS_NO_MEMORY;
3141 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3142 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3143 talloc_autofree_ldapmsg(mem_ctx, msg);
3145 if ((rc == LDAP_SUCCESS) &&
3146 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3148 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3149 "group mapping entry\n", sid_string_dbg(&map->sid)));
3150 result = NT_STATUS_GROUP_EXISTS;
3154 switch (map->sid_name_use) {
3156 case SID_NAME_DOM_GRP:
3157 /* To map a domain group we need to have a posix group
3159 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3163 case SID_NAME_ALIAS:
3164 if (!sid_check_is_in_our_domain(&map->sid)
3165 && !sid_check_is_in_builtin(&map->sid) )
3167 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3168 sid_string_dbg(&map->sid)));
3169 result = NT_STATUS_INVALID_PARAMETER;
3175 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3176 sid_type_lookup(map->sid_name_use)));
3177 result = NT_STATUS_INVALID_PARAMETER;
3181 /* Domain groups have been mapped in a separate routine, we have to
3182 * create an alias now */
3184 if (map->gid == -1) {
3185 DEBUG(10, ("Refusing to map gid==-1\n"));
3186 result = NT_STATUS_INVALID_PARAMETER;
3190 if (pdb_gid_to_sid(map->gid, &sid)) {
3191 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3192 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3193 result = NT_STATUS_GROUP_EXISTS;
3197 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3198 * the best we can get out of LDAP. */
3200 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3201 sid_string_talloc(mem_ctx, &map->sid),
3202 lp_ldap_group_suffix());
3204 result = NT_STATUS_NO_MEMORY;
3210 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3211 LDAP_OBJ_SID_ENTRY);
3212 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3214 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3215 sid_string_talloc(mem_ctx, &map->sid));
3216 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3217 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3218 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3220 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3222 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3223 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3224 talloc_autofree_ldapmod(mem_ctx, mods);
3226 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3228 result = (rc == LDAP_SUCCESS) ?
3229 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3232 TALLOC_FREE(mem_ctx);
3236 /**********************************************************************
3237 * Update a group mapping entry. We're quite strict about what can be changed:
3238 * Only the description and displayname may be changed. It simply does not
3239 * make any sense to change the SID, gid or the type in a mapping.
3240 *********************************************************************/
3242 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3245 struct ldapsam_privates *ldap_state =
3246 (struct ldapsam_privates *)methods->private_data;
3248 const char *filter, *dn;
3249 LDAPMessage *msg = NULL;
3250 LDAPMessage *entry = NULL;
3251 LDAPMod **mods = NULL;
3252 TALLOC_CTX *mem_ctx;
3255 mem_ctx = talloc_new(NULL);
3256 if (mem_ctx == NULL) {
3257 DEBUG(0, ("talloc_new failed\n"));
3258 return NT_STATUS_NO_MEMORY;
3261 /* Make 100% sure that sid, gid and type are not changed by looking up
3262 * exactly the values we're given in LDAP. */
3264 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3265 "(sambaSid=%s)(gidNumber=%u)"
3266 "(sambaGroupType=%d))",
3268 sid_string_talloc(mem_ctx, &map->sid),
3269 (unsigned int)map->gid, map->sid_name_use);
3270 if (filter == NULL) {
3271 result = NT_STATUS_NO_MEMORY;
3275 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3276 get_attr_list(mem_ctx, groupmap_attr_list),
3278 talloc_autofree_ldapmsg(mem_ctx, msg);
3280 if ((rc != LDAP_SUCCESS) ||
3281 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3282 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3283 result = NT_STATUS_NO_SUCH_GROUP;
3287 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3290 result = NT_STATUS_NO_MEMORY;
3295 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3297 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3299 talloc_autofree_ldapmod(mem_ctx, mods);
3302 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3303 "nothing to do\n"));
3304 result = NT_STATUS_OK;
3308 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3310 if (rc != LDAP_SUCCESS) {
3311 result = NT_STATUS_ACCESS_DENIED;
3315 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3316 "group %lu in LDAP\n", (unsigned long)map->gid));
3318 result = NT_STATUS_OK;
3321 TALLOC_FREE(mem_ctx);
3325 /**********************************************************************
3326 *********************************************************************/
3328 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3331 struct ldapsam_privates *priv =
3332 (struct ldapsam_privates *)methods->private_data;
3333 LDAPMessage *msg, *entry;
3336 TALLOC_CTX *mem_ctx;
3339 mem_ctx = talloc_new(NULL);
3340 if (mem_ctx == NULL) {
3341 DEBUG(0, ("talloc_new failed\n"));
3342 return NT_STATUS_NO_MEMORY;
3345 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3346 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3347 sid_string_talloc(mem_ctx, &sid));
3348 if (filter == NULL) {
3349 result = NT_STATUS_NO_MEMORY;
3352 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3353 get_attr_list(mem_ctx, groupmap_attr_list),
3355 talloc_autofree_ldapmsg(mem_ctx, msg);
3357 if ((rc != LDAP_SUCCESS) ||
3358 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3359 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3360 result = NT_STATUS_NO_SUCH_GROUP;
3364 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3365 get_attr_list(mem_ctx,
3366 groupmap_attr_list_to_delete));
3368 if ((rc == LDAP_NAMING_VIOLATION) ||
3369 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3370 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3371 const char *attrs[] = { "sambaGroupType", "description",
3372 "displayName", "sambaSIDList",
3375 /* Second try. Don't delete the sambaSID attribute, this is
3376 for "old" entries that are tacked on a winbind
3379 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3380 LDAP_OBJ_GROUPMAP, attrs);
3383 if ((rc == LDAP_NAMING_VIOLATION) ||
3384 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3385 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3386 const char *attrs[] = { "sambaGroupType", "description",
3387 "displayName", "sambaSIDList",
3388 "gidNumber", NULL };
3390 /* Third try. This is a post-3.0.21 alias (containing only
3391 * sambaSidEntry and sambaGroupMapping classes), we also have
3392 * to delete the gidNumber attribute, only the sambaSidEntry
3395 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3396 LDAP_OBJ_GROUPMAP, attrs);
3399 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3402 TALLOC_FREE(mem_ctx);
3406 /**********************************************************************
3407 *********************************************************************/
3409 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3412 struct ldapsam_privates *ldap_state =
3413 (struct ldapsam_privates *)my_methods->private_data;
3414 char *filter = NULL;
3416 const char **attr_list;
3418 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3420 return NT_STATUS_NO_MEMORY;
3422 attr_list = get_attr_list( NULL, groupmap_attr_list );
3423 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3424 LDAP_SCOPE_SUBTREE, filter,
3425 attr_list, 0, &ldap_state->result);
3426 TALLOC_FREE(attr_list);
3428 if (rc != LDAP_SUCCESS) {
3429 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3430 ldap_err2string(rc)));
3431 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3432 lp_ldap_suffix(), filter));
3433 ldap_msgfree(ldap_state->result);
3434 ldap_state->result = NULL;
3435 TALLOC_FREE(filter);
3436 return NT_STATUS_UNSUCCESSFUL;
3439 TALLOC_FREE(filter);
3441 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3442 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3443 ldap_state->result)));
3446 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3447 ldap_state->result);
3448 ldap_state->index = 0;
3450 return NT_STATUS_OK;
3453 /**********************************************************************
3454 *********************************************************************/
3456 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3458 ldapsam_endsampwent(my_methods);
3461 /**********************************************************************
3462 *********************************************************************/
3464 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3467 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3468 struct ldapsam_privates *ldap_state =
3469 (struct ldapsam_privates *)my_methods->private_data;
3473 if (!ldap_state->entry)
3476 ldap_state->index++;
3477 bret = init_group_from_ldap(ldap_state, map,
3481 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3485 return NT_STATUS_OK;
3488 /**********************************************************************
3489 *********************************************************************/
3491 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3492 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3493 GROUP_MAP **pp_rmap,
3494 size_t *p_num_entries,
3497 GROUP_MAP map = { 0, };
3503 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3504 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3506 return NT_STATUS_ACCESS_DENIED;
3509 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3510 if (sid_name_use != SID_NAME_UNKNOWN &&
3511 sid_name_use != map.sid_name_use) {
3512 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3513 "not of the requested type\n", map.nt_name));
3516 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3517 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3518 "non mapped\n", map.nt_name));
3522 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3524 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3525 "enlarge group map!\n"));
3526 return NT_STATUS_UNSUCCESSFUL;
3529 (*pp_rmap)[entries] = map;
3534 ldapsam_endsamgrent(methods);
3536 *p_num_entries = entries;
3538 return NT_STATUS_OK;
3541 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3542 const struct dom_sid *alias,
3543 const struct dom_sid *member,
3546 struct ldapsam_privates *ldap_state =
3547 (struct ldapsam_privates *)methods->private_data;
3549 LDAPMessage *result = NULL;
3550 LDAPMessage *entry = NULL;
3552 LDAPMod **mods = NULL;
3554 enum lsa_SidType type = SID_NAME_USE_NONE;
3557 char *filter = NULL;
3559 if (sid_check_is_in_builtin(alias)) {
3560 type = SID_NAME_ALIAS;
3563 if (sid_check_is_in_our_domain(alias)) {
3564 type = SID_NAME_ALIAS;
3567 if (type == SID_NAME_USE_NONE) {
3568 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3569 sid_string_dbg(alias)));
3570 return NT_STATUS_NO_SUCH_ALIAS;
3573 if (asprintf(&filter,
3574 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3575 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3577 return NT_STATUS_NO_MEMORY;
3580 if (ldapsam_search_one_group(ldap_state, filter,
3581 &result) != LDAP_SUCCESS) {
3583 return NT_STATUS_NO_SUCH_ALIAS;
3586 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3590 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3591 ldap_msgfree(result);
3593 return NT_STATUS_NO_SUCH_ALIAS;
3597 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3598 "filter %s: count=%d\n", filter, count));
3599 ldap_msgfree(result);
3601 return NT_STATUS_NO_SUCH_ALIAS;
3606 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3610 ldap_msgfree(result);
3611 return NT_STATUS_UNSUCCESSFUL;
3614 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3616 ldap_msgfree(result);
3617 return NT_STATUS_UNSUCCESSFUL;
3620 smbldap_set_mod(&mods, modop,
3621 get_attr_key2string(groupmap_attr_list,
3622 LDAP_ATTR_SID_LIST),
3623 sid_to_fstring(tmp, member));
3625 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3627 ldap_mods_free(mods, True);
3628 ldap_msgfree(result);
3631 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3632 return NT_STATUS_MEMBER_IN_ALIAS;
3635 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3636 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3639 if (rc != LDAP_SUCCESS) {
3640 return NT_STATUS_UNSUCCESSFUL;
3643 return NT_STATUS_OK;
3646 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3647 const struct dom_sid *alias,
3648 const struct dom_sid *member)
3650 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3653 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3654 const struct dom_sid *alias,
3655 const struct dom_sid *member)
3657 return ldapsam_modify_aliasmem(methods, alias, member,
3661 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3662 const struct dom_sid *alias,
3663 TALLOC_CTX *mem_ctx,
3664 struct dom_sid **pp_members,
3665 size_t *p_num_members)
3667 struct ldapsam_privates *ldap_state =
3668 (struct ldapsam_privates *)methods->private_data;
3669 LDAPMessage *result = NULL;
3670 LDAPMessage *entry = NULL;
3672 char **values = NULL;
3674 char *filter = NULL;
3675 uint32_t num_members = 0;
3676 enum lsa_SidType type = SID_NAME_USE_NONE;
3682 if (sid_check_is_in_builtin(alias)) {
3683 type = SID_NAME_ALIAS;
3686 if (sid_check_is_in_our_domain(alias)) {
3687 type = SID_NAME_ALIAS;
3690 if (type == SID_NAME_USE_NONE) {
3691 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3692 sid_string_dbg(alias)));
3693 return NT_STATUS_NO_SUCH_ALIAS;
3696 if (asprintf(&filter,
3697 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3698 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3700 return NT_STATUS_NO_MEMORY;
3703 if (ldapsam_search_one_group(ldap_state, filter,
3704 &result) != LDAP_SUCCESS) {
3706 return NT_STATUS_NO_SUCH_ALIAS;
3709 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3713 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3714 ldap_msgfree(result);
3716 return NT_STATUS_NO_SUCH_ALIAS;
3720 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3721 "filter %s: count=%d\n", filter, count));
3722 ldap_msgfree(result);
3724 return NT_STATUS_NO_SUCH_ALIAS;
3729 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3733 ldap_msgfree(result);
3734 return NT_STATUS_UNSUCCESSFUL;
3737 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3739 get_attr_key2string(groupmap_attr_list,
3740 LDAP_ATTR_SID_LIST));
3742 if (values == NULL) {
3743 ldap_msgfree(result);
3744 return NT_STATUS_OK;
3747 count = ldap_count_values(values);
3749 for (i=0; i<count; i++) {
3750 struct dom_sid member;
3753 if (!string_to_sid(&member, values[i]))
3756 status = add_sid_to_array(mem_ctx, &member, pp_members,
3758 if (!NT_STATUS_IS_OK(status)) {
3759 ldap_value_free(values);
3760 ldap_msgfree(result);
3765 *p_num_members = num_members;
3766 ldap_value_free(values);
3767 ldap_msgfree(result);
3769 return NT_STATUS_OK;
3772 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3773 TALLOC_CTX *mem_ctx,
3774 const struct dom_sid *domain_sid,
3775 const struct dom_sid *members,
3777 uint32_t **pp_alias_rids,
3778 size_t *p_num_alias_rids)
3780 struct ldapsam_privates *ldap_state =
3781 (struct ldapsam_privates *)methods->private_data;
3784 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3786 LDAPMessage *result = NULL;
3787 LDAPMessage *entry = NULL;
3791 enum lsa_SidType type = SID_NAME_USE_NONE;
3792 bool is_builtin = false;
3793 bool sid_added = false;
3795 *pp_alias_rids = NULL;
3796 *p_num_alias_rids = 0;
3798 if (sid_check_is_builtin(domain_sid)) {
3800 type = SID_NAME_ALIAS;
3803 if (sid_check_is_domain(domain_sid)) {
3804 type = SID_NAME_ALIAS;
3807 if (type == SID_NAME_USE_NONE) {
3808 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3809 sid_string_dbg(domain_sid)));
3810 return NT_STATUS_UNSUCCESSFUL;
3813 if (num_members == 0) {
3814 return NT_STATUS_OK;
3817 filter = talloc_asprintf(mem_ctx,
3818 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3819 LDAP_OBJ_GROUPMAP, type);
3821 for (i=0; i<num_members; i++)
3822 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3824 sid_string_talloc(mem_ctx,
3827 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3829 if (filter == NULL) {
3830 return NT_STATUS_NO_MEMORY;
3834 ldap_state->search_cache.filter &&
3835 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3836 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3837 result = ldap_state->search_cache.result;
3838 ldap_state->search_cache.result = NULL;
3840 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3841 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3842 if (rc != LDAP_SUCCESS) {
3843 return NT_STATUS_UNSUCCESSFUL;
3845 talloc_autofree_ldapmsg(filter, result);
3848 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3850 for (entry = ldap_first_entry(ldap_struct, result);
3852 entry = ldap_next_entry(ldap_struct, entry))
3858 if (!smbldap_get_single_attribute(ldap_struct, entry,
3864 if (!string_to_sid(&sid, sid_str))
3867 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3872 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3873 p_num_alias_rids)) {
3874 return NT_STATUS_NO_MEMORY;
3878 if (!is_builtin && !sid_added) {
3879 TALLOC_FREE(ldap_state->search_cache.filter);
3881 * Note: result is a talloc child of filter because of the
3882 * talloc_autofree_ldapmsg() usage
3884 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3885 ldap_state->search_cache.result = result;
3888 return NT_STATUS_OK;
3891 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3892 enum pdb_policy_type type,
3895 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3897 LDAPMod **mods = NULL;
3898 fstring value_string;
3899 const char *policy_attr = NULL;
3901 struct ldapsam_privates *ldap_state =
3902 (struct ldapsam_privates *)methods->private_data;
3904 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3906 if (!ldap_state->domain_dn) {
3907 return NT_STATUS_INVALID_PARAMETER;
3910 policy_attr = get_account_policy_attr(type);
3911 if (policy_attr == NULL) {
3912 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3917 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3919 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3921 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3924 ldap_mods_free(mods, True);
3926 if (rc != LDAP_SUCCESS) {
3930 if (!cache_account_policy_set(type, value)) {
3931 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3932 "update local tdb cache\n"));
3936 return NT_STATUS_OK;
3939 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3940 enum pdb_policy_type type,
3943 return ldapsam_set_account_policy_in_ldap(methods, type,
3947 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3948 enum pdb_policy_type type,
3951 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3952 LDAPMessage *result = NULL;
3953 LDAPMessage *entry = NULL;
3958 const char *policy_attr = NULL;
3960 struct ldapsam_privates *ldap_state =
3961 (struct ldapsam_privates *)methods->private_data;
3963 const char *attrs[2];
3965 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3967 if (!ldap_state->domain_dn) {
3968 return NT_STATUS_INVALID_PARAMETER;
3971 policy_attr = get_account_policy_attr(type);
3973 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3974 "policy index: %d\n", type));
3978 attrs[0] = policy_attr;
3981 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3982 if (filter == NULL) {
3983 return NT_STATUS_NO_MEMORY;
3985 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3986 LDAP_SCOPE_BASE, filter, attrs, 0,
3988 TALLOC_FREE(filter);
3989 if (rc != LDAP_SUCCESS) {
3993 count = ldap_count_entries(priv2ld(ldap_state), result);
3998 entry = ldap_first_entry(priv2ld(ldap_state), result);
3999 if (entry == NULL) {
4003 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4008 *value = (uint32_t)atol(vals[0]);
4010 ntstatus = NT_STATUS_OK;
4014 ldap_value_free(vals);
4015 ldap_msgfree(result);
4020 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4022 - if user hasn't decided to use account policies inside LDAP just reuse the
4025 - if there is a valid cache entry, return that
4026 - if there is an LDAP entry, update cache and return
4027 - otherwise set to default, update cache and return
4031 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4032 enum pdb_policy_type type,
4035 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4037 if (cache_account_policy_get(type, value)) {
4038 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4040 return NT_STATUS_OK;
4043 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4045 if (NT_STATUS_IS_OK(ntstatus)) {
4049 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4053 /* should we automagically migrate old tdb value here ? */
4054 if (account_policy_get(type, value))
4057 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4058 "default\n", type));
4061 if (!account_policy_get_default(type, value)) {
4067 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4068 if (!NT_STATUS_IS_OK(ntstatus)) {
4074 if (!cache_account_policy_set(type, *value)) {
4075 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4076 "tdb as a cache\n"));
4077 return NT_STATUS_UNSUCCESSFUL;
4080 return NT_STATUS_OK;
4083 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4084 const struct dom_sid *domain_sid,
4088 enum lsa_SidType *attrs)
4090 struct ldapsam_privates *ldap_state =
4091 (struct ldapsam_privates *)methods->private_data;
4092 LDAPMessage *msg = NULL;
4094 char *allsids = NULL;
4095 int i, rc, num_mapped;
4096 NTSTATUS result = NT_STATUS_NO_MEMORY;
4097 TALLOC_CTX *mem_ctx;
4101 mem_ctx = talloc_new(NULL);
4102 if (mem_ctx == NULL) {
4103 DEBUG(0, ("talloc_new failed\n"));
4107 if (!sid_check_is_builtin(domain_sid) &&
4108 !sid_check_is_domain(domain_sid)) {
4109 result = NT_STATUS_INVALID_PARAMETER;
4113 if (num_rids == 0) {
4114 result = NT_STATUS_NONE_MAPPED;
4118 for (i=0; i<num_rids; i++)
4119 attrs[i] = SID_NAME_UNKNOWN;
4121 allsids = talloc_strdup(mem_ctx, "");
4122 if (allsids == NULL) {
4126 for (i=0; i<num_rids; i++) {
4128 sid_compose(&sid, domain_sid, rids[i]);
4129 allsids = talloc_asprintf_append_buffer(
4130 allsids, "(sambaSid=%s)",
4131 sid_string_talloc(mem_ctx, &sid));
4132 if (allsids == NULL) {
4137 /* First look for users */
4141 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4143 filter = talloc_asprintf(
4144 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4145 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4147 if (filter == NULL) {
4151 rc = smbldap_search(ldap_state->smbldap_state,
4152 lp_ldap_user_suffix(),
4153 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4155 talloc_autofree_ldapmsg(mem_ctx, msg);
4158 if (rc != LDAP_SUCCESS)
4161 ld = ldap_state->smbldap_state->ldap_struct;
4164 for (entry = ldap_first_entry(ld, msg);
4166 entry = ldap_next_entry(ld, entry)) {
4171 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4173 DEBUG(2, ("Could not find sid from ldap entry\n"));
4177 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4180 DEBUG(2, ("Could not retrieve uid attribute\n"));
4184 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4185 if (rid == rids[rid_index])
4189 if (rid_index == num_rids) {
4190 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4194 attrs[rid_index] = SID_NAME_USER;
4195 names[rid_index] = name;
4199 if (num_mapped == num_rids) {
4200 /* No need to look for groups anymore -- we're done */
4201 result = NT_STATUS_OK;
4205 /* Same game for groups */
4209 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4210 "sambaGroupType", NULL };
4212 filter = talloc_asprintf(
4213 mem_ctx, "(&(objectClass=%s)(|%s))",
4214 LDAP_OBJ_GROUPMAP, allsids);
4215 if (filter == NULL) {
4219 rc = smbldap_search(ldap_state->smbldap_state,
4221 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4223 talloc_autofree_ldapmsg(mem_ctx, msg);
4226 if (rc != LDAP_SUCCESS)
4229 /* ldap_struct might have changed due to a reconnect */
4231 ld = ldap_state->smbldap_state->ldap_struct;
4233 /* For consistency checks, we already checked we're only domain or builtin */
4235 is_builtin = sid_check_is_builtin(domain_sid);
4237 for (entry = ldap_first_entry(ld, msg);
4239 entry = ldap_next_entry(ld, entry))
4244 enum lsa_SidType type;
4245 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4247 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4250 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4255 type = (enum lsa_SidType)atol(attr);
4257 /* Consistency checks */
4258 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4259 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4260 (type != SID_NAME_DOM_GRP)))) {
4261 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4264 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4266 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4270 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4273 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4275 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4279 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4284 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4285 if (rid == rids[rid_index])
4289 if (rid_index == num_rids) {
4290 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4294 attrs[rid_index] = type;
4295 names[rid_index] = attr;
4299 result = NT_STATUS_NONE_MAPPED;
4302 result = (num_mapped == num_rids) ?
4303 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4305 TALLOC_FREE(mem_ctx);
4309 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4311 char *filter = NULL;
4312 char *escaped = NULL;
4313 char *result = NULL;
4315 if (asprintf(&filter, "(&%s(objectclass=%s))",
4316 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4320 escaped = escape_ldap_string(talloc_tos(), username);
4321 if (escaped == NULL) goto done;
4323 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4327 TALLOC_FREE(escaped);
4332 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4336 const char **result;
4338 va_start(ap, mem_ctx);
4339 while (va_arg(ap, const char *) != NULL)
4343 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4347 va_start(ap, mem_ctx);
4348 for (i=0; i<num; i++) {
4349 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4350 if (result[i] == NULL) {
4351 talloc_free(result);
4362 struct ldap_search_state {
4363 struct smbldap_state *connection;
4365 uint32_t acct_flags;
4366 uint16_t group_type;
4373 void *pagedresults_cookie;
4375 LDAPMessage *entries, *current_entry;
4376 bool (*ldap2displayentry)(struct ldap_search_state *state,
4377 TALLOC_CTX *mem_ctx,
4378 LDAP *ld, LDAPMessage *entry,
4379 struct samr_displayentry *result);
4382 static bool ldapsam_search_firstpage(struct pdb_search *search)
4384 struct ldap_search_state *state =
4385 (struct ldap_search_state *)search->private_data;
4387 int rc = LDAP_OPERATIONS_ERROR;
4389 state->entries = NULL;
4391 if (state->connection->paged_results) {
4392 rc = smbldap_search_paged(state->connection, state->base,
4393 state->scope, state->filter,
4394 state->attrs, state->attrsonly,
4395 lp_ldap_page_size(), &state->entries,
4396 &state->pagedresults_cookie);
4399 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4401 if (state->entries != NULL) {
4402 /* Left over from unsuccessful paged attempt */
4403 ldap_msgfree(state->entries);
4404 state->entries = NULL;
4407 rc = smbldap_search(state->connection, state->base,
4408 state->scope, state->filter, state->attrs,
4409 state->attrsonly, &state->entries);
4411 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4414 /* Ok, the server was lying. It told us it could do paged
4415 * searches when it could not. */
4416 state->connection->paged_results = False;
4419 ld = state->connection->ldap_struct;
4421 DEBUG(5, ("Don't have an LDAP connection right after a "
4425 state->current_entry = ldap_first_entry(ld, state->entries);
4430 static bool ldapsam_search_nextpage(struct pdb_search *search)
4432 struct ldap_search_state *state =
4433 (struct ldap_search_state *)search->private_data;
4436 if (!state->connection->paged_results) {
4437 /* There is no next page when there are no paged results */
4441 rc = smbldap_search_paged(state->connection, state->base,
4442 state->scope, state->filter, state->attrs,
4443 state->attrsonly, lp_ldap_page_size(),
4445 &state->pagedresults_cookie);
4447 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4450 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4452 if (state->current_entry == NULL) {
4453 ldap_msgfree(state->entries);
4454 state->entries = NULL;
4461 static bool ldapsam_search_next_entry(struct pdb_search *search,
4462 struct samr_displayentry *entry)
4464 struct ldap_search_state *state =
4465 (struct ldap_search_state *)search->private_data;
4469 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4472 if ((state->entries == NULL) &&
4473 !ldapsam_search_nextpage(search))
4476 if (state->current_entry == NULL) {
4480 result = state->ldap2displayentry(state, search,
4481 state->connection->ldap_struct,
4482 state->current_entry, entry);
4486 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4487 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4488 if (dn != NULL) ldap_memfree(dn);
4491 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4493 if (state->current_entry == NULL) {
4494 ldap_msgfree(state->entries);
4495 state->entries = NULL;
4498 if (!result) goto retry;
4503 static void ldapsam_search_end(struct pdb_search *search)
4505 struct ldap_search_state *state =
4506 (struct ldap_search_state *)search->private_data;
4509 if (state->pagedresults_cookie == NULL)
4512 if (state->entries != NULL)
4513 ldap_msgfree(state->entries);
4515 state->entries = NULL;
4516 state->current_entry = NULL;
4518 if (!state->connection->paged_results)
4521 /* Tell the LDAP server we're not interested in the rest anymore. */
4523 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4524 state->filter, state->attrs,
4525 state->attrsonly, 0, &state->entries,
4526 &state->pagedresults_cookie);
4528 if (rc != LDAP_SUCCESS)
4529 DEBUG(5, ("Could not end search properly\n"));
4534 static bool ldapuser2displayentry(struct ldap_search_state *state,
4535 TALLOC_CTX *mem_ctx,
4536 LDAP *ld, LDAPMessage *entry,
4537 struct samr_displayentry *result)
4540 size_t converted_size;
4542 uint32_t acct_flags;
4544 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4545 if ((vals == NULL) || (vals[0] == NULL)) {
4546 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4549 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4550 ldap_value_free(vals);
4552 if ((state->acct_flags != 0) &&
4553 ((state->acct_flags & acct_flags) == 0))
4556 result->acct_flags = acct_flags;
4557 result->account_name = "";
4558 result->fullname = "";
4559 result->description = "";
4561 vals = ldap_get_values(ld, entry, "uid");
4562 if ((vals == NULL) || (vals[0] == NULL)) {
4563 DEBUG(5, ("\"uid\" not found\n"));
4566 if (!pull_utf8_talloc(mem_ctx,
4567 discard_const_p(char *, &result->account_name),
4568 vals[0], &converted_size))
4570 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4574 ldap_value_free(vals);
4576 vals = ldap_get_values(ld, entry, "displayName");
4577 if ((vals == NULL) || (vals[0] == NULL))
4578 DEBUG(8, ("\"displayName\" not found\n"));
4579 else if (!pull_utf8_talloc(mem_ctx,
4580 discard_const_p(char *, &result->fullname),
4581 vals[0], &converted_size))
4583 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4587 ldap_value_free(vals);
4589 vals = ldap_get_values(ld, entry, "description");
4590 if ((vals == NULL) || (vals[0] == NULL))
4591 DEBUG(8, ("\"description\" not found\n"));
4592 else if (!pull_utf8_talloc(mem_ctx,
4593 discard_const_p(char *, &result->description),
4594 vals[0], &converted_size))
4596 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4600 ldap_value_free(vals);
4602 if ((result->account_name == NULL) ||
4603 (result->fullname == NULL) ||
4604 (result->description == NULL)) {
4605 DEBUG(0, ("talloc failed\n"));
4609 vals = ldap_get_values(ld, entry, "sambaSid");
4610 if ((vals == NULL) || (vals[0] == NULL)) {
4611 DEBUG(0, ("\"objectSid\" not found\n"));
4615 if (!string_to_sid(&sid, vals[0])) {
4616 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4617 ldap_value_free(vals);
4620 ldap_value_free(vals);
4622 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4623 DEBUG(0, ("sid %s does not belong to our domain\n",
4624 sid_string_dbg(&sid)));
4632 static bool ldapsam_search_users(struct pdb_methods *methods,
4633 struct pdb_search *search,
4634 uint32_t acct_flags)
4636 struct ldapsam_privates *ldap_state =
4637 (struct ldapsam_privates *)methods->private_data;
4638 struct ldap_search_state *state;
4640 state = talloc(search, struct ldap_search_state);
4641 if (state == NULL) {
4642 DEBUG(0, ("talloc failed\n"));
4646 state->connection = ldap_state->smbldap_state;
4648 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4649 state->base = lp_ldap_user_suffix();
4650 else if ((acct_flags != 0) &&
4651 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4652 state->base = lp_ldap_machine_suffix();
4654 state->base = lp_ldap_suffix();
4656 state->acct_flags = acct_flags;
4657 state->base = talloc_strdup(search, state->base);
4658 state->scope = LDAP_SCOPE_SUBTREE;
4659 state->filter = get_ldap_filter(search, "*");
4660 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4661 "displayName", "description",
4662 "sambaAcctFlags", NULL);
4663 state->attrsonly = 0;
4664 state->pagedresults_cookie = NULL;
4665 state->entries = NULL;
4666 state->ldap2displayentry = ldapuser2displayentry;
4668 if ((state->filter == NULL) || (state->attrs == NULL)) {
4669 DEBUG(0, ("talloc failed\n"));
4673 search->private_data = state;
4674 search->next_entry = ldapsam_search_next_entry;
4675 search->search_end = ldapsam_search_end;
4677 return ldapsam_search_firstpage(search);
4680 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4681 TALLOC_CTX *mem_ctx,
4682 LDAP *ld, LDAPMessage *entry,
4683 struct samr_displayentry *result)
4686 size_t converted_size;
4688 uint16_t group_type;
4690 result->account_name = "";
4691 result->fullname = "";
4692 result->description = "";
4695 vals = ldap_get_values(ld, entry, "sambaGroupType");
4696 if ((vals == NULL) || (vals[0] == NULL)) {
4697 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4699 ldap_value_free(vals);
4704 group_type = atoi(vals[0]);
4706 if ((state->group_type != 0) &&
4707 ((state->group_type != group_type))) {
4708 ldap_value_free(vals);
4712 ldap_value_free(vals);
4714 /* display name is the NT group name */
4716 vals = ldap_get_values(ld, entry, "displayName");
4717 if ((vals == NULL) || (vals[0] == NULL)) {
4718 DEBUG(8, ("\"displayName\" not found\n"));
4720 /* fallback to the 'cn' attribute */
4721 vals = ldap_get_values(ld, entry, "cn");
4722 if ((vals == NULL) || (vals[0] == NULL)) {
4723 DEBUG(5, ("\"cn\" not found\n"));
4726 if (!pull_utf8_talloc(mem_ctx,
4727 discard_const_p(char *,
4728 &result->account_name),
4729 vals[0], &converted_size))
4731 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4732 "failed: %s", strerror(errno)));
4735 else if (!pull_utf8_talloc(mem_ctx,
4736 discard_const_p(char *,
4737 &result->account_name),
4738 vals[0], &converted_size))
4740 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4744 ldap_value_free(vals);
4746 vals = ldap_get_values(ld, entry, "description");
4747 if ((vals == NULL) || (vals[0] == NULL))
4748 DEBUG(8, ("\"description\" not found\n"));
4749 else if (!pull_utf8_talloc(mem_ctx,
4750 discard_const_p(char *, &result->description),
4751 vals[0], &converted_size))
4753 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4756 ldap_value_free(vals);
4758 if ((result->account_name == NULL) ||
4759 (result->fullname == NULL) ||
4760 (result->description == NULL)) {
4761 DEBUG(0, ("talloc failed\n"));
4765 vals = ldap_get_values(ld, entry, "sambaSid");
4766 if ((vals == NULL) || (vals[0] == NULL)) {
4767 DEBUG(0, ("\"objectSid\" not found\n"));
4769 ldap_value_free(vals);
4774 if (!string_to_sid(&sid, vals[0])) {
4775 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4779 ldap_value_free(vals);
4781 switch (group_type) {
4782 case SID_NAME_DOM_GRP:
4783 case SID_NAME_ALIAS:
4785 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4786 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4788 DEBUG(0, ("%s is not in our domain\n",
4789 sid_string_dbg(&sid)));
4795 DEBUG(0,("unknown group type: %d\n", group_type));
4799 result->acct_flags = 0;
4804 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4805 struct pdb_search *search,
4806 const struct dom_sid *sid,
4807 enum lsa_SidType type)
4809 struct ldapsam_privates *ldap_state =
4810 (struct ldapsam_privates *)methods->private_data;
4811 struct ldap_search_state *state;
4814 state = talloc(search, struct ldap_search_state);
4815 if (state == NULL) {
4816 DEBUG(0, ("talloc failed\n"));
4820 state->connection = ldap_state->smbldap_state;
4822 state->base = talloc_strdup(search, lp_ldap_suffix());
4823 state->connection = ldap_state->smbldap_state;
4824 state->scope = LDAP_SCOPE_SUBTREE;
4825 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4826 "(sambaGroupType=%d)(sambaSID=%s*))",
4828 type, sid_to_fstring(tmp, sid));
4829 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4830 "displayName", "description",
4831 "sambaGroupType", NULL);
4832 state->attrsonly = 0;
4833 state->pagedresults_cookie = NULL;
4834 state->entries = NULL;
4835 state->group_type = type;
4836 state->ldap2displayentry = ldapgroup2displayentry;
4838 if ((state->filter == NULL) || (state->attrs == NULL)) {
4839 DEBUG(0, ("talloc failed\n"));
4843 search->private_data = state;
4844 search->next_entry = ldapsam_search_next_entry;
4845 search->search_end = ldapsam_search_end;
4847 return ldapsam_search_firstpage(search);
4850 static bool ldapsam_search_groups(struct pdb_methods *methods,
4851 struct pdb_search *search)
4853 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4856 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4857 struct pdb_search *search,
4858 const struct dom_sid *sid)
4860 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4863 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4865 return PDB_CAP_STORE_RIDS;
4868 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4871 struct smbldap_state *smbldap_state = priv->smbldap_state;
4873 LDAPMessage *result = NULL;
4874 LDAPMessage *entry = NULL;
4875 LDAPMod **mods = NULL;
4879 uint32_t nextRid = 0;
4882 TALLOC_CTX *mem_ctx;
4884 mem_ctx = talloc_new(NULL);
4885 if (mem_ctx == NULL) {
4886 DEBUG(0, ("talloc_new failed\n"));
4887 return NT_STATUS_NO_MEMORY;
4890 status = smbldap_search_domain_info(smbldap_state, &result,
4891 get_global_sam_name(), False);
4892 if (!NT_STATUS_IS_OK(status)) {
4893 DEBUG(3, ("Could not get domain info: %s\n",
4894 nt_errstr(status)));
4898 talloc_autofree_ldapmsg(mem_ctx, result);
4900 entry = ldap_first_entry(priv2ld(priv), result);
4901 if (entry == NULL) {
4902 DEBUG(0, ("Could not get domain info entry\n"));
4903 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4907 /* Find the largest of the three attributes "sambaNextRid",
4908 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4909 concept of differentiating between user and group rids, and will
4910 use only "sambaNextRid" in the future. But for compatibility
4911 reasons I look if others have chosen different strategies -- VL */
4913 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4914 "sambaNextRid", mem_ctx);
4915 if (value != NULL) {
4916 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4917 nextRid = MAX(nextRid, tmp);
4920 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4921 "sambaNextUserRid", mem_ctx);
4922 if (value != NULL) {
4923 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4924 nextRid = MAX(nextRid, tmp);
4927 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4928 "sambaNextGroupRid", mem_ctx);
4929 if (value != NULL) {
4930 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4931 nextRid = MAX(nextRid, tmp);
4935 nextRid = BASE_RID-1;
4940 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4941 talloc_asprintf(mem_ctx, "%d", nextRid));
4942 talloc_autofree_ldapmod(mem_ctx, mods);
4944 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4945 status = NT_STATUS_NO_MEMORY;
4949 rc = smbldap_modify(smbldap_state, dn, mods);
4951 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4954 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4957 if (NT_STATUS_IS_OK(status)) {
4961 TALLOC_FREE(mem_ctx);
4965 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4969 for (i=0; i<10; i++) {
4970 NTSTATUS result = ldapsam_get_new_rid(
4971 (struct ldapsam_privates *)methods->private_data, rid);
4972 if (NT_STATUS_IS_OK(result)) {
4976 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4980 /* The ldap update failed (maybe a race condition), retry */
4983 /* Tried 10 times, fail. */
4984 return NT_STATUS_ACCESS_DENIED;
4987 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4989 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4990 return NT_STATUS_IS_OK(result) ? True : False;
4993 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4994 const struct dom_sid *sid,
4995 union unid_t *id, enum lsa_SidType *type)
4997 struct ldapsam_privates *priv =
4998 (struct ldapsam_privates *)methods->private_data;
5000 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5002 LDAPMessage *result = NULL;
5003 LDAPMessage *entry = NULL;
5008 TALLOC_CTX *mem_ctx;
5010 mem_ctx = talloc_new(NULL);
5011 if (mem_ctx == NULL) {
5012 DEBUG(0, ("talloc_new failed\n"));
5016 filter = talloc_asprintf(mem_ctx,
5018 "(|(objectClass=%s)(objectClass=%s)))",
5019 sid_string_talloc(mem_ctx, sid),
5020 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5021 if (filter == NULL) {
5022 DEBUG(5, ("talloc_asprintf failed\n"));
5026 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5028 if (rc != LDAP_SUCCESS) {
5031 talloc_autofree_ldapmsg(mem_ctx, result);
5033 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5034 DEBUG(10, ("Got %d entries, expected one\n",
5035 ldap_count_entries(priv2ld(priv), result)));
5039 entry = ldap_first_entry(priv2ld(priv), result);
5041 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5042 "sambaGroupType", mem_ctx);
5044 if (value != NULL) {
5045 const char *gid_str;
5048 gid_str = smbldap_talloc_single_attribute(
5049 priv2ld(priv), entry, "gidNumber", mem_ctx);
5050 if (gid_str == NULL) {
5051 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5052 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5057 id->gid = strtoul(gid_str, NULL, 10);
5058 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
5059 store_gid_sid_cache(sid, id->gid);
5060 idmap_cache_set_sid2gid(sid, id->gid);
5065 /* It must be a user */
5067 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5068 "uidNumber", mem_ctx);
5069 if (value == NULL) {
5070 DEBUG(1, ("Could not find uidNumber in %s\n",
5071 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5075 id->uid = strtoul(value, NULL, 10);
5076 *type = SID_NAME_USER;
5077 store_uid_sid_cache(sid, id->uid);
5078 idmap_cache_set_sid2uid(sid, id->uid);
5082 TALLOC_FREE(mem_ctx);
5087 * Find the SID for a uid.
5088 * This is shortcut is only used if ldapsam:trusted is set to true.
5090 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5091 struct dom_sid *sid)
5093 struct ldapsam_privates *priv =
5094 (struct ldapsam_privates *)methods->private_data;
5096 const char *attrs[] = { "sambaSID", NULL };
5097 LDAPMessage *result = NULL;
5098 LDAPMessage *entry = NULL;
5100 char *user_sid_string;
5101 struct dom_sid user_sid;
5103 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5105 filter = talloc_asprintf(tmp_ctx,
5108 "(objectClass=%s))",
5110 LDAP_OBJ_POSIXACCOUNT,
5111 LDAP_OBJ_SAMBASAMACCOUNT);
5112 if (filter == NULL) {
5113 DEBUG(3, ("talloc_asprintf failed\n"));
5117 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5118 if (rc != LDAP_SUCCESS) {
5121 talloc_autofree_ldapmsg(tmp_ctx, result);
5123 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5124 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5125 ldap_count_entries(priv2ld(priv), result),
5126 (unsigned int)uid));
5130 entry = ldap_first_entry(priv2ld(priv), result);
5132 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5133 "sambaSID", tmp_ctx);
5134 if (user_sid_string == NULL) {
5135 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5136 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5140 if (!string_to_sid(&user_sid, user_sid_string)) {
5141 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5146 sid_copy(sid, &user_sid);
5148 store_uid_sid_cache(sid, uid);
5149 idmap_cache_set_sid2uid(sid, uid);
5154 TALLOC_FREE(tmp_ctx);
5159 * Find the SID for a gid.
5160 * This is shortcut is only used if ldapsam:trusted is set to true.
5162 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5163 struct dom_sid *sid)
5165 struct ldapsam_privates *priv =
5166 (struct ldapsam_privates *)methods->private_data;
5168 const char *attrs[] = { "sambaSID", NULL };
5169 LDAPMessage *result = NULL;
5170 LDAPMessage *entry = NULL;
5172 char *group_sid_string;
5173 struct dom_sid group_sid;
5175 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5177 filter = talloc_asprintf(tmp_ctx,
5179 "(objectClass=%s))",
5182 if (filter == NULL) {
5183 DEBUG(3, ("talloc_asprintf failed\n"));
5187 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5188 if (rc != LDAP_SUCCESS) {
5191 talloc_autofree_ldapmsg(tmp_ctx, result);
5193 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5194 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5195 ldap_count_entries(priv2ld(priv), result),
5196 (unsigned int)gid));
5200 entry = ldap_first_entry(priv2ld(priv), result);
5202 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5203 "sambaSID", tmp_ctx);
5204 if (group_sid_string == NULL) {
5205 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5206 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5210 if (!string_to_sid(&group_sid, group_sid_string)) {
5211 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5216 sid_copy(sid, &group_sid);
5218 store_gid_sid_cache(sid, gid);
5219 idmap_cache_set_sid2gid(sid, gid);
5224 TALLOC_FREE(tmp_ctx);
5230 * The following functions are called only if
5231 * ldapsam:trusted and ldapsam:editposix are
5236 * ldapsam_create_user creates a new
5237 * posixAccount and sambaSamAccount object
5238 * in the ldap users subtree
5240 * The uid is allocated by winbindd.
5243 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5244 TALLOC_CTX *tmp_ctx, const char *name,
5245 uint32_t acb_info, uint32_t *rid)
5247 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5248 LDAPMessage *entry = NULL;
5249 LDAPMessage *result = NULL;
5250 uint32_t num_result;
5251 bool is_machine = False;
5252 bool add_posix = False;
5253 LDAPMod **mods = NULL;
5261 const char *dn = NULL;
5262 struct dom_sid group_sid;
5263 struct dom_sid user_sid;
5269 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5270 acb_info & ACB_WSTRUST ||
5271 acb_info & ACB_SVRTRUST ||
5272 acb_info & ACB_DOMTRUST) {
5276 username = escape_ldap_string(talloc_tos(), name);
5277 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5278 username, LDAP_OBJ_POSIXACCOUNT);
5279 TALLOC_FREE(username);
5281 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5282 if (rc != LDAP_SUCCESS) {
5283 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5284 return NT_STATUS_ACCESS_DENIED;
5286 talloc_autofree_ldapmsg(tmp_ctx, result);
5288 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5290 if (num_result > 1) {
5291 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5292 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5295 if (num_result == 1) {
5297 /* check if it is just a posix account.
5298 * or if there is a sid attached to this entry
5301 entry = ldap_first_entry(priv2ld(ldap_state), result);
5303 return NT_STATUS_UNSUCCESSFUL;
5306 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5308 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5309 return NT_STATUS_USER_EXISTS;
5312 /* it is just a posix account, retrieve the dn for later use */
5313 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5315 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5316 return NT_STATUS_NO_MEMORY;
5320 if (num_result == 0) {
5324 /* Create the basic samu structure and generate the mods for the ldap commit */
5325 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5326 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5330 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5332 user = samu_new(tmp_ctx);
5334 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5335 return NT_STATUS_NO_MEMORY;
5338 if (!pdb_set_username(user, name, PDB_SET)) {
5339 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5340 return NT_STATUS_UNSUCCESSFUL;
5342 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5343 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5344 return NT_STATUS_UNSUCCESSFUL;
5347 if (acb_info & ACB_NORMAL) {
5348 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5349 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5350 return NT_STATUS_UNSUCCESSFUL;
5353 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5354 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5355 return NT_STATUS_UNSUCCESSFUL;
5359 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5360 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5361 return NT_STATUS_UNSUCCESSFUL;
5365 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5366 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5367 return NT_STATUS_UNSUCCESSFUL;
5370 if (!init_ldap_from_sam(ldap_state, entry, &mods, user, element_is_set_or_changed)) {
5371 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5372 return NT_STATUS_UNSUCCESSFUL;
5375 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5376 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5378 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5383 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5385 /* retrieve the Domain Users group gid */
5386 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5387 !sid_to_gid(&group_sid, &gid)) {
5388 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5389 return NT_STATUS_INVALID_PRIMARY_GROUP;
5392 /* lets allocate a new userid for this user */
5393 if (!winbind_allocate_uid(&uid)) {
5394 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5395 return NT_STATUS_UNSUCCESSFUL;
5400 /* TODO: choose a more appropriate default for machines */
5401 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5402 shell = talloc_strdup(tmp_ctx, "/bin/false");
5404 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5405 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5407 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5408 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5410 escape_name = escape_rdn_val_string_alloc(name);
5412 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5413 return NT_STATUS_NO_MEMORY;
5417 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5419 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5422 SAFE_FREE(escape_name);
5424 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5425 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5426 return NT_STATUS_NO_MEMORY;
5429 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5430 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5431 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5432 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5433 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5434 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5435 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5438 talloc_autofree_ldapmod(tmp_ctx, mods);
5441 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5443 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5446 if (rc != LDAP_SUCCESS) {
5447 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5448 return NT_STATUS_UNSUCCESSFUL;
5451 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5453 flush_pwnam_cache();
5455 return NT_STATUS_OK;
5458 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5460 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5461 LDAPMessage *result = NULL;
5462 LDAPMessage *entry = NULL;
5468 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5470 filter = talloc_asprintf(tmp_ctx,
5473 "(objectClass=%s))",
5474 pdb_get_username(sam_acct),
5475 LDAP_OBJ_POSIXACCOUNT,
5476 LDAP_OBJ_SAMBASAMACCOUNT);
5477 if (filter == NULL) {
5478 return NT_STATUS_NO_MEMORY;
5481 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5482 if (rc != LDAP_SUCCESS) {
5483 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5484 return NT_STATUS_UNSUCCESSFUL;
5486 talloc_autofree_ldapmsg(tmp_ctx, result);
5488 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5490 if (num_result == 0) {
5491 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5492 return NT_STATUS_NO_SUCH_USER;
5495 if (num_result > 1) {
5496 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5497 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5500 entry = ldap_first_entry(priv2ld(ldap_state), result);
5502 return NT_STATUS_UNSUCCESSFUL;
5505 /* it is just a posix account, retrieve the dn for later use */
5506 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5508 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5509 return NT_STATUS_NO_MEMORY;
5512 /* try to remove memberships first */
5515 struct dom_sid *sids = NULL;
5517 uint32_t num_groups = 0;
5519 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5521 status = ldapsam_enum_group_memberships(my_methods,
5527 if (!NT_STATUS_IS_OK(status)) {
5531 for (i=0; i < num_groups; i++) {
5535 sid_peek_rid(&sids[i], &group_rid);
5537 ldapsam_del_groupmem(my_methods,
5546 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5547 if (rc != LDAP_SUCCESS) {
5548 return NT_STATUS_UNSUCCESSFUL;
5551 flush_pwnam_cache();
5553 return NT_STATUS_OK;
5557 * ldapsam_create_group creates a new
5558 * posixGroup and sambaGroupMapping object
5559 * in the ldap groups subtree
5561 * The gid is allocated by winbindd.
5564 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5565 TALLOC_CTX *tmp_ctx,
5569 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5571 LDAPMessage *entry = NULL;
5572 LDAPMessage *result = NULL;
5573 uint32_t num_result;
5574 bool is_new_entry = False;
5575 LDAPMod **mods = NULL;
5581 const char *dn = NULL;
5582 struct dom_sid group_sid;
5586 groupname = escape_ldap_string(talloc_tos(), name);
5587 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5588 groupname, LDAP_OBJ_POSIXGROUP);
5589 TALLOC_FREE(groupname);
5591 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5592 if (rc != LDAP_SUCCESS) {
5593 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5594 return NT_STATUS_UNSUCCESSFUL;
5596 talloc_autofree_ldapmsg(tmp_ctx, result);
5598 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5600 if (num_result > 1) {
5601 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5602 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5605 if (num_result == 1) {
5607 /* check if it is just a posix group.
5608 * or if there is a sid attached to this entry
5611 entry = ldap_first_entry(priv2ld(ldap_state), result);
5613 return NT_STATUS_UNSUCCESSFUL;
5616 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5618 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5619 return NT_STATUS_GROUP_EXISTS;
5622 /* it is just a posix group, retrieve the gid and the dn for later use */
5623 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5625 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5626 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5629 gid = strtoul(tmp, NULL, 10);
5631 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5633 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5634 return NT_STATUS_NO_MEMORY;
5638 if (num_result == 0) {
5639 is_new_entry = true;
5642 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5643 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5647 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5649 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5651 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5653 if (!groupsidstr || !grouptype) {
5654 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5655 return NT_STATUS_NO_MEMORY;
5658 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5659 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5660 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5661 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5666 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5668 /* lets allocate a new groupid for this group */
5669 if (!winbind_allocate_gid(&gid)) {
5670 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5671 return NT_STATUS_UNSUCCESSFUL;
5674 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5676 escape_name = escape_rdn_val_string_alloc(name);
5678 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5679 return NT_STATUS_NO_MEMORY;
5682 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5684 SAFE_FREE(escape_name);
5686 if (!gidstr || !dn) {
5687 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5688 return NT_STATUS_NO_MEMORY;
5691 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5692 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5693 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5696 talloc_autofree_ldapmod(tmp_ctx, mods);
5699 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5701 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5702 /* This call may fail with rfc2307bis schema */
5703 /* Retry adding a structural class */
5704 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5705 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5709 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5712 if (rc != LDAP_SUCCESS) {
5713 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5714 return NT_STATUS_UNSUCCESSFUL;
5717 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5719 return NT_STATUS_OK;
5722 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5724 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5725 LDAPMessage *result = NULL;
5726 LDAPMessage *entry = NULL;
5731 struct dom_sid group_sid;
5734 /* get the group sid */
5735 sid_compose(&group_sid, get_global_sam_sid(), rid);
5737 filter = talloc_asprintf(tmp_ctx,
5740 "(objectClass=%s))",
5741 sid_string_talloc(tmp_ctx, &group_sid),
5742 LDAP_OBJ_POSIXGROUP,
5744 if (filter == NULL) {
5745 return NT_STATUS_NO_MEMORY;
5748 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5749 if (rc != LDAP_SUCCESS) {
5750 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5751 return NT_STATUS_UNSUCCESSFUL;
5753 talloc_autofree_ldapmsg(tmp_ctx, result);
5755 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5757 if (num_result == 0) {
5758 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5759 return NT_STATUS_NO_SUCH_GROUP;
5762 if (num_result > 1) {
5763 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5764 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5767 entry = ldap_first_entry(priv2ld(ldap_state), result);
5769 return NT_STATUS_UNSUCCESSFUL;
5772 /* here it is, retrieve the dn for later use */
5773 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5775 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5776 return NT_STATUS_NO_MEMORY;
5779 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5781 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5782 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5785 /* check no user have this group marked as primary group */
5786 filter = talloc_asprintf(tmp_ctx,
5789 "(objectClass=%s))",
5791 LDAP_OBJ_POSIXACCOUNT,
5792 LDAP_OBJ_SAMBASAMACCOUNT);
5794 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5795 if (rc != LDAP_SUCCESS) {
5796 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5797 return NT_STATUS_UNSUCCESSFUL;
5799 talloc_autofree_ldapmsg(tmp_ctx, result);
5801 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5803 if (num_result != 0) {
5804 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5805 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5808 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5809 if (rc != LDAP_SUCCESS) {
5810 return NT_STATUS_UNSUCCESSFUL;
5813 return NT_STATUS_OK;
5816 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5817 TALLOC_CTX *tmp_ctx,
5819 uint32_t member_rid,
5822 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5823 LDAPMessage *entry = NULL;
5824 LDAPMessage *result = NULL;
5825 uint32_t num_result;
5826 LDAPMod **mods = NULL;
5829 const char *dn = NULL;
5830 struct dom_sid group_sid;
5831 struct dom_sid member_sid;
5836 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5838 case LDAP_MOD_DELETE:
5839 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5842 return NT_STATUS_UNSUCCESSFUL;
5845 /* get member sid */
5846 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5848 /* get the group sid */
5849 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5851 filter = talloc_asprintf(tmp_ctx,
5854 "(objectClass=%s))",
5855 sid_string_talloc(tmp_ctx, &member_sid),
5856 LDAP_OBJ_POSIXACCOUNT,
5857 LDAP_OBJ_SAMBASAMACCOUNT);
5858 if (filter == NULL) {
5859 return NT_STATUS_NO_MEMORY;
5862 /* get the member uid */
5863 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5864 if (rc != LDAP_SUCCESS) {
5865 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5866 return NT_STATUS_UNSUCCESSFUL;
5868 talloc_autofree_ldapmsg(tmp_ctx, result);
5870 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5872 if (num_result == 0) {
5873 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5874 return NT_STATUS_NO_SUCH_MEMBER;
5877 if (num_result > 1) {
5878 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5879 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5882 entry = ldap_first_entry(priv2ld(ldap_state), result);
5884 return NT_STATUS_UNSUCCESSFUL;
5887 if (modop == LDAP_MOD_DELETE) {
5888 /* check if we are trying to remove the member from his primary group */
5890 gid_t user_gid, group_gid;
5892 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5894 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5895 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5898 user_gid = strtoul(gidstr, NULL, 10);
5900 if (!sid_to_gid(&group_sid, &group_gid)) {
5901 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5902 return NT_STATUS_UNSUCCESSFUL;
5905 if (user_gid == group_gid) {
5906 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5907 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5911 /* here it is, retrieve the uid for later use */
5912 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5914 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5915 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5918 filter = talloc_asprintf(tmp_ctx,
5921 "(objectClass=%s))",
5922 sid_string_talloc(tmp_ctx, &group_sid),
5923 LDAP_OBJ_POSIXGROUP,
5927 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5928 if (rc != LDAP_SUCCESS) {
5929 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5930 return NT_STATUS_UNSUCCESSFUL;
5932 talloc_autofree_ldapmsg(tmp_ctx, result);
5934 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5936 if (num_result == 0) {
5937 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5938 return NT_STATUS_NO_SUCH_GROUP;
5941 if (num_result > 1) {
5942 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5943 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5946 entry = ldap_first_entry(priv2ld(ldap_state), result);
5948 return NT_STATUS_UNSUCCESSFUL;
5951 /* here it is, retrieve the dn for later use */
5952 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5954 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5955 return NT_STATUS_NO_MEMORY;
5958 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5960 talloc_autofree_ldapmod(tmp_ctx, mods);
5962 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5963 if (rc != LDAP_SUCCESS) {
5964 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5965 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5966 return NT_STATUS_MEMBER_IN_GROUP;
5968 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5969 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5970 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5972 return NT_STATUS_UNSUCCESSFUL;
5975 return NT_STATUS_OK;
5978 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5979 TALLOC_CTX *tmp_ctx,
5981 uint32_t member_rid)
5983 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5985 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5986 TALLOC_CTX *tmp_ctx,
5988 uint32_t member_rid)
5990 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5993 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5994 TALLOC_CTX *mem_ctx,
5995 struct samu *sampass)
5997 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5998 LDAPMessage *entry = NULL;
5999 LDAPMessage *result = NULL;
6000 uint32_t num_result;
6001 LDAPMod **mods = NULL;
6003 char *escape_username;
6005 const char *dn = NULL;
6009 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6011 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6012 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6013 return NT_STATUS_UNSUCCESSFUL;
6015 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6017 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6018 return NT_STATUS_NO_MEMORY;
6021 escape_username = escape_ldap_string(talloc_tos(),
6022 pdb_get_username(sampass));
6023 if (escape_username== NULL) {
6024 return NT_STATUS_NO_MEMORY;
6027 filter = talloc_asprintf(mem_ctx,
6030 "(objectClass=%s))",
6032 LDAP_OBJ_POSIXACCOUNT,
6033 LDAP_OBJ_SAMBASAMACCOUNT);
6035 TALLOC_FREE(escape_username);
6037 if (filter == NULL) {
6038 return NT_STATUS_NO_MEMORY;
6041 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6042 if (rc != LDAP_SUCCESS) {
6043 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6044 return NT_STATUS_UNSUCCESSFUL;
6046 talloc_autofree_ldapmsg(mem_ctx, result);
6048 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6050 if (num_result == 0) {
6051 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6052 return NT_STATUS_NO_SUCH_USER;
6055 if (num_result > 1) {
6056 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6057 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6060 entry = ldap_first_entry(priv2ld(ldap_state), result);
6062 return NT_STATUS_UNSUCCESSFUL;
6065 /* retrieve the dn for later use */
6066 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6068 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6069 return NT_STATUS_NO_MEMORY;
6072 /* remove the old one, and add the new one, this way we do not risk races */
6073 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6076 return NT_STATUS_OK;
6079 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6081 if (rc != LDAP_SUCCESS) {
6082 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6083 pdb_get_username(sampass), gidstr));
6084 return NT_STATUS_UNSUCCESSFUL;
6087 flush_pwnam_cache();
6089 return NT_STATUS_OK;
6093 /**********************************************************************
6094 trusted domains functions
6095 *********************************************************************/
6097 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6100 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6101 ldap_state->domain_dn);
6104 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6105 TALLOC_CTX *mem_ctx,
6106 const char *domain, LDAPMessage **entry)
6110 int scope = LDAP_SCOPE_SUBTREE;
6111 const char **attrs = NULL; /* NULL: get all attrs */
6112 int attrsonly = 0; /* 0: return values too */
6113 LDAPMessage *result = NULL;
6115 uint32_t num_result;
6117 filter = talloc_asprintf(talloc_tos(),
6118 "(&(objectClass=%s)(sambaDomainName=%s))",
6119 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6121 trusted_dn = trusteddom_dn(ldap_state, domain);
6122 if (trusted_dn == NULL) {
6125 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6126 filter, attrs, attrsonly, &result);
6128 if (result != NULL) {
6129 talloc_autofree_ldapmsg(mem_ctx, result);
6132 if (rc == LDAP_NO_SUCH_OBJECT) {
6137 if (rc != LDAP_SUCCESS) {
6141 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6143 if (num_result > 1) {
6144 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6145 "%s object for domain '%s'?!\n",
6146 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6150 if (num_result == 0) {
6151 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6152 "%s object for domain %s.\n",
6153 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6156 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6162 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6165 struct dom_sid *sid,
6166 time_t *pass_last_set_time)
6168 struct ldapsam_privates *ldap_state =
6169 (struct ldapsam_privates *)methods->private_data;
6170 LDAPMessage *entry = NULL;
6172 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6174 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6183 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6184 entry, "sambaClearTextPassword", talloc_tos());
6185 if (pwd_str == NULL) {
6188 /* trusteddom_pw routines do not use talloc yet... */
6189 *pwd = SMB_STRDUP(pwd_str);
6195 /* last change time */
6196 if (pass_last_set_time != NULL) {
6198 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6199 entry, "sambaPwdLastSet", talloc_tos());
6200 if (time_str == NULL) {
6203 *pass_last_set_time = (time_t)atol(time_str);
6209 struct dom_sid dom_sid;
6210 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6213 if (sid_str == NULL) {
6216 if (!string_to_sid(&dom_sid, sid_str)) {
6219 sid_copy(sid, &dom_sid);
6225 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6228 const struct dom_sid *sid)
6230 struct ldapsam_privates *ldap_state =
6231 (struct ldapsam_privates *)methods->private_data;
6232 LDAPMessage *entry = NULL;
6233 LDAPMod **mods = NULL;
6234 char *prev_pwd = NULL;
6235 char *trusted_dn = NULL;
6238 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6241 * get the current entry (if there is one) in order to put the
6242 * current password into the previous password attribute
6244 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6249 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6250 LDAP_OBJ_TRUSTDOM_PASSWORD);
6251 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6253 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6254 sid_string_tos(sid));
6255 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6256 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6257 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6258 "sambaClearTextPassword", pwd);
6260 if (entry != NULL) {
6261 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6262 entry, "sambaClearTextPassword", talloc_tos());
6263 if (prev_pwd != NULL) {
6264 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6265 "sambaPreviousClearTextPassword",
6270 talloc_autofree_ldapmod(talloc_tos(), mods);
6272 trusted_dn = trusteddom_dn(ldap_state, domain);
6273 if (trusted_dn == NULL) {
6276 if (entry == NULL) {
6277 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6279 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6282 if (rc != LDAP_SUCCESS) {
6283 DEBUG(1, ("error writing trusted domain password!\n"));
6290 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6294 struct ldapsam_privates *ldap_state =
6295 (struct ldapsam_privates *)methods->private_data;
6296 LDAPMessage *entry = NULL;
6297 const char *trusted_dn;
6299 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6303 if (entry == NULL) {
6304 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6309 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6311 if (trusted_dn == NULL) {
6312 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6316 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6317 if (rc != LDAP_SUCCESS) {
6324 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6325 TALLOC_CTX *mem_ctx,
6326 uint32_t *num_domains,
6327 struct trustdom_info ***domains)
6330 struct ldapsam_privates *ldap_state =
6331 (struct ldapsam_privates *)methods->private_data;
6333 int scope = LDAP_SCOPE_SUBTREE;
6334 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6335 int attrsonly = 0; /* 0: return values too */
6336 LDAPMessage *result = NULL;
6337 LDAPMessage *entry = NULL;
6339 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6340 LDAP_OBJ_TRUSTDOM_PASSWORD);
6342 rc = smbldap_search(ldap_state->smbldap_state,
6343 ldap_state->domain_dn,
6350 if (result != NULL) {
6351 talloc_autofree_ldapmsg(mem_ctx, result);
6354 if (rc != LDAP_SUCCESS) {
6355 return NT_STATUS_UNSUCCESSFUL;
6359 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6360 DEBUG(1, ("talloc failed\n"));
6361 return NT_STATUS_NO_MEMORY;
6364 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6366 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6368 char *dom_name, *dom_sid_str;
6369 struct trustdom_info *dom_info;
6371 dom_info = TALLOC_P(*domains, struct trustdom_info);
6372 if (dom_info == NULL) {
6373 DEBUG(1, ("talloc failed\n"));
6374 return NT_STATUS_NO_MEMORY;
6377 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6381 if (dom_name == NULL) {
6382 DEBUG(1, ("talloc failed\n"));
6383 return NT_STATUS_NO_MEMORY;
6385 dom_info->name = dom_name;
6387 dom_sid_str = smbldap_talloc_single_attribute(
6388 priv2ld(ldap_state), entry, "sambaSID",
6390 if (dom_sid_str == NULL) {
6391 DEBUG(1, ("talloc failed\n"));
6392 return NT_STATUS_NO_MEMORY;
6394 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6395 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6397 return NT_STATUS_UNSUCCESSFUL;
6400 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6401 domains, num_domains);
6403 if (*domains == NULL) {
6404 DEBUG(1, ("talloc failed\n"));
6405 return NT_STATUS_NO_MEMORY;
6409 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6410 return NT_STATUS_OK;
6414 /**********************************************************************
6416 *********************************************************************/
6418 static void free_private_data(void **vp)
6420 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6422 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6424 if ((*ldap_state)->result != NULL) {
6425 ldap_msgfree((*ldap_state)->result);
6426 (*ldap_state)->result = NULL;
6428 if ((*ldap_state)->domain_dn != NULL) {
6429 SAFE_FREE((*ldap_state)->domain_dn);
6434 /* No need to free any further, as it is talloc()ed */
6437 /*********************************************************************
6438 Intitalise the parts of the pdb_methods structure that are common to
6440 *********************************************************************/
6442 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6445 struct ldapsam_privates *ldap_state;
6447 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6451 (*pdb_method)->name = "ldapsam";
6453 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6454 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6455 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6456 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6457 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6458 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6460 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6461 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6462 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6463 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6464 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6465 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6466 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6468 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6469 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6471 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6473 (*pdb_method)->capabilities = ldapsam_capabilities;
6474 (*pdb_method)->new_rid = ldapsam_new_rid;
6476 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6477 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6478 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6479 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6481 /* TODO: Setup private data and free */
6483 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
6484 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6485 return NT_STATUS_NO_MEMORY;
6488 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6489 location, &ldap_state->smbldap_state);
6491 if ( !NT_STATUS_IS_OK(nt_status) ) {
6495 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6496 return NT_STATUS_NO_MEMORY;
6499 (*pdb_method)->private_data = ldap_state;
6501 (*pdb_method)->free_private_data = free_private_data;
6503 return NT_STATUS_OK;
6506 /**********************************************************************
6507 Initialise the 'compat' mode for pdb_ldap
6508 *********************************************************************/
6510 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6513 struct ldapsam_privates *ldap_state;
6514 char *uri = talloc_strdup( NULL, location );
6516 trim_char( uri, '\"', '\"' );
6517 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6521 if ( !NT_STATUS_IS_OK(nt_status) ) {
6525 (*pdb_method)->name = "ldapsam_compat";
6527 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6528 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6530 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6532 return NT_STATUS_OK;
6535 /**********************************************************************
6536 Initialise the normal mode for pdb_ldap
6537 *********************************************************************/
6539 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6542 struct ldapsam_privates *ldap_state = NULL;
6543 uint32_t alg_rid_base;
6544 char *alg_rid_base_string = NULL;
6545 LDAPMessage *result = NULL;
6546 LDAPMessage *entry = NULL;
6547 struct dom_sid ldap_domain_sid;
6548 struct dom_sid secrets_domain_sid;
6549 char *domain_sid_string = NULL;
6551 char *uri = talloc_strdup( NULL, location );
6553 trim_char( uri, '\"', '\"' );
6554 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6558 if (!NT_STATUS_IS_OK(nt_status)) {
6562 (*pdb_method)->name = "ldapsam";
6564 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6565 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6566 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6567 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6568 (*pdb_method)->search_users = ldapsam_search_users;
6569 (*pdb_method)->search_groups = ldapsam_search_groups;
6570 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6572 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6573 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6574 (*pdb_method)->enum_group_memberships =
6575 ldapsam_enum_group_memberships;
6576 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6577 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6578 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6579 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6581 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6582 (*pdb_method)->create_user = ldapsam_create_user;
6583 (*pdb_method)->delete_user = ldapsam_delete_user;
6584 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6585 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6586 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6587 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6588 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6592 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6593 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6595 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6597 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6599 ldap_state->domain_name, True);
6601 if ( !NT_STATUS_IS_OK(nt_status) ) {
6602 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6603 "info, nor add one to the domain\n"));
6604 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6605 "will be unable to allocate new users/groups, "
6606 "and will risk BDCs having inconsistent SIDs\n"));
6607 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6608 return NT_STATUS_OK;
6611 /* Given that the above might fail, everything below this must be
6614 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6617 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6619 ldap_msgfree(result);
6620 return NT_STATUS_UNSUCCESSFUL;
6623 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6625 ldap_msgfree(result);
6626 return NT_STATUS_UNSUCCESSFUL;
6629 ldap_state->domain_dn = smb_xstrdup(dn);
6632 domain_sid_string = smbldap_talloc_single_attribute(
6633 ldap_state->smbldap_state->ldap_struct,
6635 get_userattr_key2string(ldap_state->schema_ver,
6636 LDAP_ATTR_USER_SID),
6639 if (domain_sid_string) {
6641 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6642 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6643 "read as a valid SID\n", domain_sid_string));
6644 ldap_msgfree(result);
6645 TALLOC_FREE(domain_sid_string);
6646 return NT_STATUS_INVALID_PARAMETER;
6648 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6649 &secrets_domain_sid);
6650 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6651 &ldap_domain_sid)) {
6652 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6653 "%s based on pdb_ldap results %s -> %s\n",
6654 ldap_state->domain_name,
6655 sid_string_dbg(&secrets_domain_sid),
6656 sid_string_dbg(&ldap_domain_sid)));
6658 /* reset secrets.tdb sid */
6659 secrets_store_domain_sid(ldap_state->domain_name,
6661 DEBUG(1, ("New global sam SID: %s\n",
6662 sid_string_dbg(get_global_sam_sid())));
6664 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6665 TALLOC_FREE(domain_sid_string);
6668 alg_rid_base_string = smbldap_talloc_single_attribute(
6669 ldap_state->smbldap_state->ldap_struct,
6671 get_attr_key2string( dominfo_attr_list,
6672 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6674 if (alg_rid_base_string) {
6675 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6676 if (alg_rid_base != algorithmic_rid_base()) {
6677 DEBUG(0, ("The value of 'algorithmic RID base' has "
6678 "changed since the LDAP\n"
6679 "database was initialised. Aborting. \n"));
6680 ldap_msgfree(result);
6681 TALLOC_FREE(alg_rid_base_string);
6682 return NT_STATUS_UNSUCCESSFUL;
6684 TALLOC_FREE(alg_rid_base_string);
6686 ldap_msgfree(result);
6688 return NT_STATUS_OK;
6691 NTSTATUS pdb_ldap_init(void)
6694 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6697 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6700 /* Let pdb_nds register backends */
6705 return NT_STATUS_OK;