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
63 #include "passdb/pdb_ldap.h"
64 #include "passdb/pdb_nds.h"
65 #include "passdb/pdb_ipa.h"
67 /**********************************************************************
68 Simple helper function to make stuff better readable
69 **********************************************************************/
71 LDAP *priv2ld(struct ldapsam_privates *priv)
73 return priv->smbldap_state->ldap_struct;
76 /**********************************************************************
77 Get the attribute name given a user schame version.
78 **********************************************************************/
80 static const char* get_userattr_key2string( int schema_ver, int key )
82 switch ( schema_ver ) {
83 case SCHEMAVER_SAMBAACCOUNT:
84 return get_attr_key2string( attrib_map_v22, key );
86 case SCHEMAVER_SAMBASAMACCOUNT:
87 return get_attr_key2string( attrib_map_v30, key );
90 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
96 /**********************************************************************
97 Return the list of attribute names given a user schema version.
98 **********************************************************************/
100 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
102 switch ( schema_ver ) {
103 case SCHEMAVER_SAMBAACCOUNT:
104 return get_attr_list( mem_ctx, attrib_map_v22 );
106 case SCHEMAVER_SAMBASAMACCOUNT:
107 return get_attr_list( mem_ctx, attrib_map_v30 );
109 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
116 /**************************************************************************
117 Return the list of attribute names to delete given a user schema version.
118 **************************************************************************/
120 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
123 switch ( schema_ver ) {
124 case SCHEMAVER_SAMBAACCOUNT:
125 return get_attr_list( mem_ctx,
126 attrib_map_to_delete_v22 );
128 case SCHEMAVER_SAMBASAMACCOUNT:
129 return get_attr_list( mem_ctx,
130 attrib_map_to_delete_v30 );
132 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
140 /*******************************************************************
141 Generate the LDAP search filter for the objectclass based on the
142 version of the schema we are using.
143 ******************************************************************/
145 static const char* get_objclass_filter( int schema_ver )
147 fstring objclass_filter;
150 switch( schema_ver ) {
151 case SCHEMAVER_SAMBAACCOUNT:
152 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
154 case SCHEMAVER_SAMBASAMACCOUNT:
155 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
158 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
159 objclass_filter[0] = '\0';
163 result = talloc_strdup(talloc_tos(), objclass_filter);
164 SMB_ASSERT(result != NULL);
168 /*****************************************************************
169 Scan a sequence number off OpenLDAP's syncrepl contextCSN
170 ******************************************************************/
172 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
174 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
175 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
176 LDAPMessage *msg = NULL;
177 LDAPMessage *entry = NULL;
179 char **values = NULL;
180 int rc, num_result, num_values, rid;
186 /* Unfortunatly there is no proper way to detect syncrepl-support in
187 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
188 * but do not show up in the root-DSE yet. Neither we can query the
189 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
190 * objectclass. Currently we require lp_ldap_suffix() to show up as
191 * namingContext. - Guenther
194 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
199 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
203 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
204 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
205 "as top-level namingContext\n", lp_ldap_suffix()));
209 mem_ctx = talloc_init("ldapsam_get_seq_num");
212 return NT_STATUS_NO_MEMORY;
214 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
215 ntstatus = NT_STATUS_NO_MEMORY;
219 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
220 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
223 /* consumer syncreplCookie: */
224 /* csn=20050126161620Z#0000001#00#00000 */
225 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
227 suffix = talloc_asprintf(mem_ctx,
228 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
230 ntstatus = NT_STATUS_NO_MEMORY;
235 /* provider contextCSN */
236 /* 20050126161620Z#000009#00#000000 */
237 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
239 suffix = talloc_asprintf(mem_ctx,
240 "cn=ldapsync,%s", lp_ldap_suffix());
243 ntstatus = NT_STATUS_NO_MEMORY;
248 rc = smbldap_search(ldap_state->smbldap_state, suffix,
249 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
251 if (rc != LDAP_SUCCESS) {
255 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
256 if (num_result != 1) {
257 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
261 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
263 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
267 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
268 if (values == NULL) {
269 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
273 num_values = ldap_count_values(values);
274 if (num_values == 0) {
275 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
280 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
281 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
286 if (!strncmp(p, "csn=", strlen("csn=")))
289 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
291 *seq_num = generalized_to_unix_time(p);
293 /* very basic sanity check */
295 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
300 ntstatus = NT_STATUS_OK;
304 ldap_value_free(values);
308 talloc_destroy(mem_ctx);
313 /*******************************************************************
314 Run the search by name.
315 ******************************************************************/
317 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
319 LDAPMessage ** result,
323 char *escape_user = escape_ldap_string(talloc_tos(), user);
327 return LDAP_NO_MEMORY;
331 * in the filter expression, replace %u with the real name
332 * so in ldap filter, %u MUST exist :-)
334 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
335 get_objclass_filter(ldap_state->schema_ver));
337 TALLOC_FREE(escape_user);
338 return LDAP_NO_MEMORY;
341 * have to use this here because $ is filtered out
345 filter = talloc_all_string_sub(talloc_tos(),
346 filter, "%u", escape_user);
347 TALLOC_FREE(escape_user);
349 return LDAP_NO_MEMORY;
352 ret = smbldap_search_suffix(ldap_state->smbldap_state,
353 filter, attr, result);
358 /*******************************************************************
359 Run the search by rid.
360 ******************************************************************/
362 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
363 uint32_t rid, LDAPMessage ** result,
369 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
370 get_objclass_filter(ldap_state->schema_ver));
372 return LDAP_NO_MEMORY;
375 rc = smbldap_search_suffix(ldap_state->smbldap_state,
376 filter, attr, result);
381 /*******************************************************************
382 Run the search by SID.
383 ******************************************************************/
385 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
386 const struct dom_sid *sid, LDAPMessage ** result,
393 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
394 get_userattr_key2string(ldap_state->schema_ver,
396 sid_to_fstring(sid_string, sid),
397 get_objclass_filter(ldap_state->schema_ver));
399 return LDAP_NO_MEMORY;
402 rc = smbldap_search_suffix(ldap_state->smbldap_state,
403 filter, attr, result);
409 /*******************************************************************
410 Delete complete object or objectclass and attrs from
411 object found in search_result depending on lp_ldap_delete_dn
412 ******************************************************************/
414 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
417 const char *objectclass,
420 LDAPMod **mods = NULL;
423 BerElement *ptr = NULL;
425 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
427 return LDAP_NO_MEMORY;
430 if (lp_ldap_delete_dn()) {
431 return smbldap_delete(priv->smbldap_state, dn);
434 /* Ok, delete only the SAM attributes */
436 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
438 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
441 /* We are only allowed to delete the attributes that
444 for (attrib = attrs; *attrib != NULL; attrib++) {
445 if (strequal(*attrib, name)) {
446 DEBUG(10, ("ldapsam_delete_entry: deleting "
447 "attribute %s\n", name));
448 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
459 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
460 talloc_autofree_ldapmod(mem_ctx, mods);
462 return smbldap_modify(priv->smbldap_state, dn, mods);
465 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
470 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
471 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
477 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
478 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
488 /**********************************************************************
489 Initialize struct samu from an LDAP query.
490 (Based on init_sam_from_buffer in pdb_tdb.c)
491 *********************************************************************/
493 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
494 struct samu * sampass,
501 pass_can_change_time,
502 pass_must_change_time,
505 char *username = NULL,
511 *logon_script = NULL,
512 *profile_path = NULL,
514 *workstations = NULL,
517 uint8 smblmpwd[LM_HASH_LEN],
518 smbntpwd[NT_HASH_LEN];
519 bool use_samba_attrs = True;
520 uint32_t acct_ctrl = 0;
522 uint16_t bad_password_count = 0,
525 uint8 hours[MAX_HOURS_LEN];
527 struct login_cache cache_entry;
529 bool expand_explicit = lp_passdb_expand_explicit();
531 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
536 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
537 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
541 if (priv2ld(ldap_state) == NULL) {
542 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
543 "ldap_struct is NULL!\n"));
547 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
551 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
556 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
558 nt_username = talloc_strdup(ctx, username);
563 domain = talloc_strdup(ctx, ldap_state->domain_name);
568 pdb_set_username(sampass, username, PDB_SET);
570 pdb_set_domain(sampass, domain, PDB_DEFAULT);
571 pdb_set_nt_username(sampass, nt_username, PDB_SET);
573 /* deal with different attributes between the schema first */
575 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
576 if ((temp = smbldap_talloc_single_attribute(
577 ldap_state->smbldap_state->ldap_struct,
579 get_userattr_key2string(ldap_state->schema_ver,
582 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
585 if ((temp = smbldap_talloc_single_attribute(
586 ldap_state->smbldap_state->ldap_struct,
588 get_userattr_key2string(ldap_state->schema_ver,
591 user_rid = (uint32_t)atol(temp);
592 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
596 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
597 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
598 get_userattr_key2string(ldap_state->schema_ver,
600 get_userattr_key2string(ldap_state->schema_ver,
606 temp = smbldap_talloc_single_attribute(
607 ldap_state->smbldap_state->ldap_struct,
609 get_userattr_key2string(ldap_state->schema_ver,
610 LDAP_ATTR_PWD_LAST_SET),
613 pass_last_set_time = (time_t) atol(temp);
614 pdb_set_pass_last_set_time(sampass,
615 pass_last_set_time, PDB_SET);
618 temp = smbldap_talloc_single_attribute(
619 ldap_state->smbldap_state->ldap_struct,
621 get_userattr_key2string(ldap_state->schema_ver,
622 LDAP_ATTR_LOGON_TIME),
625 logon_time = (time_t) atol(temp);
626 pdb_set_logon_time(sampass, logon_time, PDB_SET);
629 temp = smbldap_talloc_single_attribute(
630 ldap_state->smbldap_state->ldap_struct,
632 get_userattr_key2string(ldap_state->schema_ver,
633 LDAP_ATTR_LOGOFF_TIME),
636 logoff_time = (time_t) atol(temp);
637 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
640 temp = smbldap_talloc_single_attribute(
641 ldap_state->smbldap_state->ldap_struct,
643 get_userattr_key2string(ldap_state->schema_ver,
644 LDAP_ATTR_KICKOFF_TIME),
647 kickoff_time = (time_t) atol(temp);
648 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
651 temp = smbldap_talloc_single_attribute(
652 ldap_state->smbldap_state->ldap_struct,
654 get_userattr_key2string(ldap_state->schema_ver,
655 LDAP_ATTR_PWD_CAN_CHANGE),
658 pass_can_change_time = (time_t) atol(temp);
659 pdb_set_pass_can_change_time(sampass,
660 pass_can_change_time, PDB_SET);
663 temp = smbldap_talloc_single_attribute(
664 ldap_state->smbldap_state->ldap_struct,
666 get_userattr_key2string(ldap_state->schema_ver,
667 LDAP_ATTR_PWD_MUST_CHANGE),
670 pass_must_change_time = (time_t) atol(temp);
671 pdb_set_pass_must_change_time(sampass,
672 pass_must_change_time, PDB_SET);
675 /* recommend that 'gecos' and 'displayName' should refer to the same
676 * attribute OID. userFullName depreciated, only used by Samba
677 * primary rules of LDAP: don't make a new attribute when one is already defined
678 * that fits your needs; using cn then displayName rather than 'userFullName'
681 fullname = smbldap_talloc_single_attribute(
682 ldap_state->smbldap_state->ldap_struct,
684 get_userattr_key2string(ldap_state->schema_ver,
685 LDAP_ATTR_DISPLAY_NAME),
688 pdb_set_fullname(sampass, fullname, PDB_SET);
690 fullname = smbldap_talloc_single_attribute(
691 ldap_state->smbldap_state->ldap_struct,
693 get_userattr_key2string(ldap_state->schema_ver,
697 pdb_set_fullname(sampass, fullname, PDB_SET);
701 dir_drive = smbldap_talloc_single_attribute(
702 ldap_state->smbldap_state->ldap_struct,
704 get_userattr_key2string(ldap_state->schema_ver,
705 LDAP_ATTR_HOME_DRIVE),
708 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
710 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
713 homedir = smbldap_talloc_single_attribute(
714 ldap_state->smbldap_state->ldap_struct,
716 get_userattr_key2string(ldap_state->schema_ver,
717 LDAP_ATTR_HOME_PATH),
720 if (expand_explicit) {
721 homedir = talloc_sub_basic(ctx,
729 pdb_set_homedir(sampass, homedir, PDB_SET);
731 pdb_set_homedir(sampass,
732 talloc_sub_basic(ctx, username, domain,
737 logon_script = smbldap_talloc_single_attribute(
738 ldap_state->smbldap_state->ldap_struct,
740 get_userattr_key2string(ldap_state->schema_ver,
741 LDAP_ATTR_LOGON_SCRIPT),
744 if (expand_explicit) {
745 logon_script = talloc_sub_basic(ctx,
753 pdb_set_logon_script(sampass, logon_script, PDB_SET);
755 pdb_set_logon_script(sampass,
756 talloc_sub_basic(ctx, username, domain,
761 profile_path = smbldap_talloc_single_attribute(
762 ldap_state->smbldap_state->ldap_struct,
764 get_userattr_key2string(ldap_state->schema_ver,
765 LDAP_ATTR_PROFILE_PATH),
768 if (expand_explicit) {
769 profile_path = talloc_sub_basic(ctx,
777 pdb_set_profile_path(sampass, profile_path, PDB_SET);
779 pdb_set_profile_path(sampass,
780 talloc_sub_basic(ctx, username, domain,
785 acct_desc = smbldap_talloc_single_attribute(
786 ldap_state->smbldap_state->ldap_struct,
788 get_userattr_key2string(ldap_state->schema_ver,
792 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
795 workstations = smbldap_talloc_single_attribute(
796 ldap_state->smbldap_state->ldap_struct,
798 get_userattr_key2string(ldap_state->schema_ver,
802 pdb_set_workstations(sampass, workstations, PDB_SET);
805 munged_dial = smbldap_talloc_single_attribute(
806 ldap_state->smbldap_state->ldap_struct,
808 get_userattr_key2string(ldap_state->schema_ver,
809 LDAP_ATTR_MUNGED_DIAL),
812 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
815 /* FIXME: hours stuff should be cleaner */
819 memset(hours, 0xff, hours_len);
821 if (ldap_state->is_nds_ldap) {
824 char clear_text_pw[512];
826 /* Make call to Novell eDirectory ldap extension to get clear text password.
827 NOTE: This will only work if we have an SSL connection to eDirectory. */
828 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
829 if (user_dn != NULL) {
830 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
832 pwd_len = sizeof(clear_text_pw);
833 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
834 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
835 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
836 TALLOC_FREE(user_dn);
839 ZERO_STRUCT(smblmpwd);
840 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
841 TALLOC_FREE(user_dn);
844 ZERO_STRUCT(smbntpwd);
845 use_samba_attrs = False;
848 TALLOC_FREE(user_dn);
851 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
855 if (use_samba_attrs) {
856 temp = smbldap_talloc_single_attribute(
857 ldap_state->smbldap_state->ldap_struct,
859 get_userattr_key2string(ldap_state->schema_ver,
863 pdb_gethexpwd(temp, smblmpwd);
864 memset((char *)temp, '\0', strlen(temp)+1);
865 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
868 ZERO_STRUCT(smblmpwd);
871 temp = smbldap_talloc_single_attribute(
872 ldap_state->smbldap_state->ldap_struct,
874 get_userattr_key2string(ldap_state->schema_ver,
878 pdb_gethexpwd(temp, smbntpwd);
879 memset((char *)temp, '\0', strlen(temp)+1);
880 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
883 ZERO_STRUCT(smbntpwd);
889 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
891 uint8 *pwhist = NULL;
893 char *history_string = talloc_array(ctx, char,
894 MAX_PW_HISTORY_LEN*64);
896 if (!history_string) {
900 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
902 pwhist = talloc_array(ctx, uint8,
903 pwHistLen * PW_HISTORY_ENTRY_LEN);
904 if (pwhist == NULL) {
905 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
908 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
910 if (smbldap_get_single_attribute(
911 ldap_state->smbldap_state->ldap_struct,
913 get_userattr_key2string(ldap_state->schema_ver,
914 LDAP_ATTR_PWD_HISTORY),
916 MAX_PW_HISTORY_LEN*64)) {
917 bool hex_failed = false;
918 for (i = 0; i < pwHistLen; i++){
919 /* Get the 16 byte salt. */
920 if (!pdb_gethexpwd(&history_string[i*64],
921 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
925 /* Get the 16 byte MD5 hash of salt+passwd. */
926 if (!pdb_gethexpwd(&history_string[(i*64)+32],
927 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
928 PW_HISTORY_SALT_LEN])) {
934 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
936 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
939 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
944 temp = smbldap_talloc_single_attribute(
945 ldap_state->smbldap_state->ldap_struct,
947 get_userattr_key2string(ldap_state->schema_ver,
951 acct_ctrl = pdb_decode_acct_ctrl(temp);
953 if (acct_ctrl == 0) {
954 acct_ctrl |= ACB_NORMAL;
957 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
959 acct_ctrl |= ACB_NORMAL;
962 pdb_set_hours_len(sampass, hours_len, PDB_SET);
963 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
965 temp = smbldap_talloc_single_attribute(
966 ldap_state->smbldap_state->ldap_struct,
968 get_userattr_key2string(ldap_state->schema_ver,
969 LDAP_ATTR_BAD_PASSWORD_COUNT),
972 bad_password_count = (uint32_t) atol(temp);
973 pdb_set_bad_password_count(sampass,
974 bad_password_count, PDB_SET);
977 temp = smbldap_talloc_single_attribute(
978 ldap_state->smbldap_state->ldap_struct,
980 get_userattr_key2string(ldap_state->schema_ver,
981 LDAP_ATTR_BAD_PASSWORD_TIME),
984 bad_password_time = (time_t) atol(temp);
985 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
989 temp = smbldap_talloc_single_attribute(
990 ldap_state->smbldap_state->ldap_struct,
992 get_userattr_key2string(ldap_state->schema_ver,
993 LDAP_ATTR_LOGON_COUNT),
996 logon_count = (uint32_t) atol(temp);
997 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1000 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1002 temp = smbldap_talloc_single_attribute(
1003 ldap_state->smbldap_state->ldap_struct,
1005 get_userattr_key2string(ldap_state->schema_ver,
1006 LDAP_ATTR_LOGON_HOURS),
1009 pdb_gethexhours(temp, hours);
1010 memset((char *)temp, '\0', strlen(temp) +1);
1011 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
1015 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1016 struct passwd unix_pw;
1017 bool have_uid = false;
1018 bool have_gid = false;
1019 struct dom_sid mapped_gsid;
1020 const struct dom_sid *primary_gsid;
1022 ZERO_STRUCT(unix_pw);
1024 unix_pw.pw_name = username;
1025 unix_pw.pw_passwd = discard_const_p(char, "x");
1027 temp = smbldap_talloc_single_attribute(
1028 priv2ld(ldap_state),
1033 /* We've got a uid, feed the cache */
1034 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1037 temp = smbldap_talloc_single_attribute(
1038 priv2ld(ldap_state),
1043 /* We've got a uid, feed the cache */
1044 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1047 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1048 priv2ld(ldap_state),
1052 if (unix_pw.pw_gecos) {
1053 unix_pw.pw_gecos = fullname;
1055 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1056 priv2ld(ldap_state),
1060 if (unix_pw.pw_dir) {
1061 unix_pw.pw_dir = discard_const_p(char, "");
1063 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1064 priv2ld(ldap_state),
1068 if (unix_pw.pw_shell) {
1069 unix_pw.pw_shell = discard_const_p(char, "");
1072 if (have_uid && have_gid) {
1073 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1075 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1078 if (sampass->unix_pw == NULL) {
1079 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1080 pdb_get_username(sampass)));
1084 store_uid_sid_cache(pdb_get_user_sid(sampass),
1085 sampass->unix_pw->pw_uid);
1086 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
1087 sampass->unix_pw->pw_uid);
1089 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1090 primary_gsid = pdb_get_group_sid(sampass);
1091 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1092 store_gid_sid_cache(primary_gsid,
1093 sampass->unix_pw->pw_gid);
1094 idmap_cache_set_sid2gid(primary_gsid,
1095 sampass->unix_pw->pw_gid);
1099 /* check the timestamp of the cache vs ldap entry */
1100 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1106 /* see if we have newer updates */
1107 if (!login_cache_read(sampass, &cache_entry)) {
1108 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1109 (unsigned int)pdb_get_bad_password_count(sampass),
1110 (unsigned int)pdb_get_bad_password_time(sampass)));
1115 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1116 (unsigned int)ldap_entry_time,
1117 (unsigned int)cache_entry.entry_timestamp,
1118 (unsigned int)cache_entry.bad_password_time));
1120 if (ldap_entry_time > cache_entry.entry_timestamp) {
1121 /* cache is older than directory , so
1122 we need to delete the entry but allow the
1123 fields to be written out */
1124 login_cache_delentry(sampass);
1127 pdb_set_acct_ctrl(sampass,
1128 pdb_get_acct_ctrl(sampass) |
1129 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1131 pdb_set_bad_password_count(sampass,
1132 cache_entry.bad_password_count,
1134 pdb_set_bad_password_time(sampass,
1135 cache_entry.bad_password_time,
1147 /**********************************************************************
1148 Initialize the ldap db from a struct samu. Called on update.
1149 (Based on init_buffer_from_sam in pdb_tdb.c)
1150 *********************************************************************/
1152 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1153 LDAPMessage *existing,
1154 LDAPMod *** mods, struct samu * sampass,
1155 bool (*need_update)(const struct samu *,
1161 if (mods == NULL || sampass == NULL) {
1162 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1169 * took out adding "objectclass: sambaAccount"
1170 * do this on a per-mod basis
1172 if (need_update(sampass, PDB_USERNAME)) {
1173 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1174 "uid", pdb_get_username(sampass));
1175 if (ldap_state->is_nds_ldap) {
1176 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1177 "cn", pdb_get_username(sampass));
1178 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1179 "sn", pdb_get_username(sampass));
1183 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1185 /* only update the RID if we actually need to */
1186 if (need_update(sampass, PDB_USERSID)) {
1188 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1190 switch ( ldap_state->schema_ver ) {
1191 case SCHEMAVER_SAMBAACCOUNT:
1192 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1193 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1194 sid_string_dbg(user_sid),
1196 &ldap_state->domain_sid)));
1199 if (asprintf(&temp, "%i", rid) < 0) {
1202 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1203 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1208 case SCHEMAVER_SAMBASAMACCOUNT:
1209 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1210 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1211 sid_to_fstring(sid_string, user_sid));
1215 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1220 /* we don't need to store the primary group RID - so leaving it
1221 'free' to hang off the unix primary group makes life easier */
1223 if (need_update(sampass, PDB_GROUPSID)) {
1225 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1227 switch ( ldap_state->schema_ver ) {
1228 case SCHEMAVER_SAMBAACCOUNT:
1229 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1230 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1231 sid_string_dbg(group_sid),
1233 &ldap_state->domain_sid)));
1237 if (asprintf(&temp, "%i", rid) < 0) {
1240 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1241 get_userattr_key2string(ldap_state->schema_ver,
1242 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1246 case SCHEMAVER_SAMBASAMACCOUNT:
1247 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1248 get_userattr_key2string(ldap_state->schema_ver,
1249 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1253 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1259 /* displayName, cn, and gecos should all be the same
1260 * most easily accomplished by giving them the same OID
1261 * gecos isn't set here b/c it should be handled by the
1263 * We change displayName only and fall back to cn if
1264 * it does not exist.
1267 if (need_update(sampass, PDB_FULLNAME))
1268 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1269 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1270 pdb_get_fullname(sampass));
1272 if (need_update(sampass, PDB_ACCTDESC))
1273 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1274 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1275 pdb_get_acct_desc(sampass));
1277 if (need_update(sampass, PDB_WORKSTATIONS))
1278 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1279 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1280 pdb_get_workstations(sampass));
1282 if (need_update(sampass, PDB_MUNGEDDIAL))
1283 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1284 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1285 pdb_get_munged_dial(sampass));
1287 if (need_update(sampass, PDB_SMBHOME))
1288 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1289 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1290 pdb_get_homedir(sampass));
1292 if (need_update(sampass, PDB_DRIVE))
1293 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1294 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1295 pdb_get_dir_drive(sampass));
1297 if (need_update(sampass, PDB_LOGONSCRIPT))
1298 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1299 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1300 pdb_get_logon_script(sampass));
1302 if (need_update(sampass, PDB_PROFILE))
1303 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1304 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1305 pdb_get_profile_path(sampass));
1307 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1310 if (need_update(sampass, PDB_LOGONTIME))
1311 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1312 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1315 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1318 if (need_update(sampass, PDB_LOGOFFTIME))
1319 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1320 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1323 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1326 if (need_update(sampass, PDB_KICKOFFTIME))
1327 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1328 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1331 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1334 if (need_update(sampass, PDB_CANCHANGETIME))
1335 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1336 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1339 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1342 if (need_update(sampass, PDB_MUSTCHANGETIME))
1343 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1344 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1347 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1348 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1350 if (need_update(sampass, PDB_LMPASSWD)) {
1351 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1354 pdb_sethexpwd(pwstr, lm_pw,
1355 pdb_get_acct_ctrl(sampass));
1356 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1357 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1360 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1361 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1365 if (need_update(sampass, PDB_NTPASSWD)) {
1366 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1369 pdb_sethexpwd(pwstr, nt_pw,
1370 pdb_get_acct_ctrl(sampass));
1371 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1372 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1375 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1376 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1381 if (need_update(sampass, PDB_PWHISTORY)) {
1383 uint32_t pwHistLen = 0;
1384 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1386 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1390 if (pwHistLen == 0) {
1391 /* Remove any password history from the LDAP store. */
1392 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1396 uint32_t currHistLen = 0;
1397 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1398 if (pwhist != NULL) {
1399 /* We can only store (1024-1/64 password history entries. */
1400 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1401 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1402 /* Store the salt. */
1403 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1404 /* Followed by the md5 hash of salt + md4 hash */
1405 pdb_sethexpwd(&pwstr[(i*64)+32],
1406 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1407 DEBUG(100, ("pwstr=%s\n", pwstr));
1411 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1412 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1417 if (need_update(sampass, PDB_PASSLASTSET)) {
1418 if (asprintf(&temp, "%li",
1419 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1422 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1423 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1429 if (need_update(sampass, PDB_HOURS)) {
1430 const uint8 *hours = pdb_get_hours(sampass);
1433 pdb_sethexhours(hourstr, hours);
1434 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1437 get_userattr_key2string(ldap_state->schema_ver,
1438 LDAP_ATTR_LOGON_HOURS),
1443 if (need_update(sampass, PDB_ACCTCTRL))
1444 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1445 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1446 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1448 /* password lockout cache:
1449 - If we are now autolocking or clearing, we write to ldap
1450 - If we are clearing, we delete the cache entry
1451 - If the count is > 0, we update the cache
1453 This even means when autolocking, we cache, just in case the
1454 update doesn't work, and we have to cache the autolock flag */
1456 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1457 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1458 uint16_t badcount = pdb_get_bad_password_count(sampass);
1459 time_t badtime = pdb_get_bad_password_time(sampass);
1461 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1463 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1464 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1466 if ((badcount >= pol) || (badcount == 0)) {
1467 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1468 (unsigned int)badcount, (unsigned int)badtime));
1469 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1473 ldap_state->smbldap_state->ldap_struct,
1475 get_userattr_key2string(
1476 ldap_state->schema_ver,
1477 LDAP_ATTR_BAD_PASSWORD_COUNT),
1481 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1485 ldap_state->smbldap_state->ldap_struct,
1487 get_userattr_key2string(
1488 ldap_state->schema_ver,
1489 LDAP_ATTR_BAD_PASSWORD_TIME),
1493 if (badcount == 0) {
1494 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1495 login_cache_delentry(sampass);
1497 struct login_cache cache_entry;
1499 cache_entry.entry_timestamp = time(NULL);
1500 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1501 cache_entry.bad_password_count = badcount;
1502 cache_entry.bad_password_time = badtime;
1504 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1505 login_cache_write(sampass, &cache_entry);
1512 /**********************************************************************
1513 End enumeration of the LDAP password list.
1514 *********************************************************************/
1516 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1518 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1519 if (ldap_state->result) {
1520 ldap_msgfree(ldap_state->result);
1521 ldap_state->result = NULL;
1525 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1526 const char *new_attr)
1530 if (new_attr == NULL) {
1534 for (i=0; (*attr_list)[i] != NULL; i++) {
1538 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1540 SMB_ASSERT((*attr_list) != NULL);
1541 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1542 (*attr_list)[i+1] = NULL;
1545 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1546 const char ***attr_list)
1548 append_attr(mem_ctx, attr_list, "uidNumber");
1549 append_attr(mem_ctx, attr_list, "gidNumber");
1550 append_attr(mem_ctx, attr_list, "homeDirectory");
1551 append_attr(mem_ctx, attr_list, "loginShell");
1552 append_attr(mem_ctx, attr_list, "gecos");
1555 /**********************************************************************
1556 Get struct samu entry from LDAP by username.
1557 *********************************************************************/
1559 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1561 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1562 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1563 LDAPMessage *result = NULL;
1564 LDAPMessage *entry = NULL;
1566 const char ** attr_list;
1569 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1570 append_attr(user, &attr_list,
1571 get_userattr_key2string(ldap_state->schema_ver,
1572 LDAP_ATTR_MOD_TIMESTAMP));
1573 ldapsam_add_unix_attributes(user, &attr_list);
1574 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1576 TALLOC_FREE( attr_list );
1578 if ( rc != LDAP_SUCCESS )
1579 return NT_STATUS_NO_SUCH_USER;
1581 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1584 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1585 ldap_msgfree(result);
1586 return NT_STATUS_NO_SUCH_USER;
1587 } else if (count > 1) {
1588 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1589 ldap_msgfree(result);
1590 return NT_STATUS_NO_SUCH_USER;
1593 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1595 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1596 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1597 ldap_msgfree(result);
1598 return NT_STATUS_NO_SUCH_USER;
1600 pdb_set_backend_private_data(user, result, NULL,
1601 my_methods, PDB_CHANGED);
1602 talloc_autofree_ldapmsg(user, result);
1605 ldap_msgfree(result);
1610 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1611 const struct dom_sid *sid, LDAPMessage **result)
1614 const char ** attr_list;
1617 switch ( ldap_state->schema_ver ) {
1618 case SCHEMAVER_SAMBASAMACCOUNT: {
1619 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1620 if (tmp_ctx == NULL) {
1621 return LDAP_NO_MEMORY;
1624 attr_list = get_userattr_list(tmp_ctx,
1625 ldap_state->schema_ver);
1626 append_attr(tmp_ctx, &attr_list,
1627 get_userattr_key2string(
1628 ldap_state->schema_ver,
1629 LDAP_ATTR_MOD_TIMESTAMP));
1630 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1631 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1633 TALLOC_FREE(tmp_ctx);
1635 if ( rc != LDAP_SUCCESS )
1640 case SCHEMAVER_SAMBAACCOUNT:
1641 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1645 attr_list = get_userattr_list(NULL,
1646 ldap_state->schema_ver);
1647 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1648 TALLOC_FREE( attr_list );
1650 if ( rc != LDAP_SUCCESS )
1657 /**********************************************************************
1658 Get struct samu entry from LDAP by SID.
1659 *********************************************************************/
1661 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1663 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1664 LDAPMessage *result = NULL;
1665 LDAPMessage *entry = NULL;
1669 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1671 if (rc != LDAP_SUCCESS)
1672 return NT_STATUS_NO_SUCH_USER;
1674 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1677 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1678 "count=%d\n", sid_string_dbg(sid), count));
1679 ldap_msgfree(result);
1680 return NT_STATUS_NO_SUCH_USER;
1681 } else if (count > 1) {
1682 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1683 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1685 ldap_msgfree(result);
1686 return NT_STATUS_NO_SUCH_USER;
1689 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1691 ldap_msgfree(result);
1692 return NT_STATUS_NO_SUCH_USER;
1695 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1696 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1697 ldap_msgfree(result);
1698 return NT_STATUS_NO_SUCH_USER;
1701 pdb_set_backend_private_data(user, result, NULL,
1702 my_methods, PDB_CHANGED);
1703 talloc_autofree_ldapmsg(user, result);
1704 return NT_STATUS_OK;
1707 /********************************************************************
1708 Do the actual modification - also change a plaintext passord if
1710 **********************************************************************/
1712 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1713 struct samu *newpwd, char *dn,
1714 LDAPMod **mods, int ldap_op,
1715 bool (*need_update)(const struct samu *, enum pdb_elements))
1717 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1720 if (!newpwd || !dn) {
1721 return NT_STATUS_INVALID_PARAMETER;
1724 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1725 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1726 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1727 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1730 char *retoid = NULL;
1731 struct berval *retdata = NULL;
1732 char *utf8_password;
1734 size_t converted_size;
1737 if (!ldap_state->is_nds_ldap) {
1739 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1740 LDAP_EXOP_MODIFY_PASSWD)) {
1741 DEBUG(2, ("ldap password change requested, but LDAP "
1742 "server does not support it -- ignoring\n"));
1743 return NT_STATUS_OK;
1747 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1748 pdb_get_plaintext_passwd(newpwd),
1751 return NT_STATUS_NO_MEMORY;
1754 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1755 TALLOC_FREE(utf8_password);
1756 return NT_STATUS_NO_MEMORY;
1759 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1760 DEBUG(0,("ber_alloc_t returns NULL\n"));
1761 TALLOC_FREE(utf8_password);
1762 TALLOC_FREE(utf8_dn);
1763 return NT_STATUS_UNSUCCESSFUL;
1766 if ((ber_printf (ber, "{") < 0) ||
1767 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1769 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1772 TALLOC_FREE(utf8_dn);
1773 TALLOC_FREE(utf8_password);
1774 return NT_STATUS_UNSUCCESSFUL;
1777 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1778 ret = ber_printf(ber, "ts}",
1779 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1782 ret = ber_printf(ber, "}");
1786 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1789 TALLOC_FREE(utf8_dn);
1790 TALLOC_FREE(utf8_password);
1791 return NT_STATUS_UNSUCCESSFUL;
1794 if ((rc = ber_flatten (ber, &bv))<0) {
1795 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1797 TALLOC_FREE(utf8_dn);
1798 TALLOC_FREE(utf8_password);
1799 return NT_STATUS_UNSUCCESSFUL;
1802 TALLOC_FREE(utf8_dn);
1803 TALLOC_FREE(utf8_password);
1806 if (!ldap_state->is_nds_ldap) {
1807 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1808 LDAP_EXOP_MODIFY_PASSWD,
1809 bv, NULL, NULL, &retoid,
1812 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1813 pdb_get_plaintext_passwd(newpwd));
1815 if (rc != LDAP_SUCCESS) {
1816 char *ld_error = NULL;
1818 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1819 DEBUG(3, ("Could not set userPassword "
1820 "attribute due to an objectClass "
1821 "violation -- ignoring\n"));
1823 return NT_STATUS_OK;
1826 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1828 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1829 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1830 SAFE_FREE(ld_error);
1832 #if defined(LDAP_CONSTRAINT_VIOLATION)
1833 if (rc == LDAP_CONSTRAINT_VIOLATION)
1834 return NT_STATUS_PASSWORD_RESTRICTION;
1836 return NT_STATUS_UNSUCCESSFUL;
1838 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1839 #ifdef DEBUG_PASSWORD
1840 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1843 ber_bvfree(retdata);
1845 ldap_memfree(retoid);
1851 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1852 /* may be password change below however */
1856 if (ldap_state->is_nds_ldap) {
1857 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1861 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1865 rc = smbldap_add(ldap_state->smbldap_state,
1868 case LDAP_MOD_REPLACE:
1869 rc = smbldap_modify(ldap_state->smbldap_state,
1873 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1875 return NT_STATUS_INVALID_PARAMETER;
1878 if (rc!=LDAP_SUCCESS) {
1879 return NT_STATUS_UNSUCCESSFUL;
1883 return NT_STATUS_OK;
1886 /**********************************************************************
1887 Delete entry from LDAP for username.
1888 *********************************************************************/
1890 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1891 struct samu * sam_acct)
1893 struct ldapsam_privates *priv =
1894 (struct ldapsam_privates *)my_methods->private_data;
1897 LDAPMessage *msg, *entry;
1898 NTSTATUS result = NT_STATUS_NO_MEMORY;
1899 const char **attr_list;
1900 TALLOC_CTX *mem_ctx;
1903 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1904 return NT_STATUS_INVALID_PARAMETER;
1907 sname = pdb_get_username(sam_acct);
1909 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1912 mem_ctx = talloc_new(NULL);
1913 if (mem_ctx == NULL) {
1914 DEBUG(0, ("talloc_new failed\n"));
1918 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1919 if (attr_list == NULL) {
1923 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1925 if ((rc != LDAP_SUCCESS) ||
1926 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1927 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1928 DEBUG(5, ("Could not find user %s\n", sname));
1929 result = NT_STATUS_NO_SUCH_USER;
1933 rc = ldapsam_delete_entry(
1934 priv, mem_ctx, entry,
1935 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1936 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1939 result = (rc == LDAP_SUCCESS) ?
1940 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1943 TALLOC_FREE(mem_ctx);
1947 /**********************************************************************
1949 *********************************************************************/
1951 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1953 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1954 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1957 LDAPMessage *result = NULL;
1958 LDAPMessage *entry = NULL;
1959 LDAPMod **mods = NULL;
1960 const char **attr_list;
1962 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1964 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1965 if (pdb_get_username(newpwd) == NULL) {
1966 return NT_STATUS_INVALID_PARAMETER;
1968 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1969 TALLOC_FREE( attr_list );
1970 if (rc != LDAP_SUCCESS) {
1971 return NT_STATUS_UNSUCCESSFUL;
1973 pdb_set_backend_private_data(newpwd, result, NULL,
1974 my_methods, PDB_CHANGED);
1975 talloc_autofree_ldapmsg(newpwd, result);
1978 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1979 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1980 return NT_STATUS_UNSUCCESSFUL;
1983 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1984 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1986 return NT_STATUS_UNSUCCESSFUL;
1989 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1991 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1992 pdb_element_is_changed)) {
1993 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1996 ldap_mods_free(mods,True);
1997 return NT_STATUS_UNSUCCESSFUL;
2000 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
2001 && (mods == NULL)) {
2002 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2003 pdb_get_username(newpwd)));
2005 return NT_STATUS_OK;
2008 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
2011 ldap_mods_free(mods,True);
2017 * We need to set the backend private data to NULL here. For example
2018 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2019 * same one, and with the explicit delete / add logic for attribute
2020 * values the second time we would use the wrong "old" value which
2021 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2023 * The existing LDAPMessage is still being auto-freed by the
2026 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2029 if (!NT_STATUS_IS_OK(ret)) {
2033 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2034 pdb_get_username(newpwd)));
2035 return NT_STATUS_OK;
2038 /***************************************************************************
2039 Renames a struct samu
2040 - The "rename user script" has full responsibility for changing everything
2041 ***************************************************************************/
2043 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2044 TALLOC_CTX *tmp_ctx,
2046 uint32_t member_rid);
2048 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2049 TALLOC_CTX *mem_ctx,
2051 struct dom_sid **pp_sids,
2053 uint32_t *p_num_groups);
2055 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2056 struct samu *old_acct,
2057 const char *newname)
2059 const char *oldname;
2061 char *rename_script = NULL;
2062 fstring oldname_lower, newname_lower;
2065 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2066 return NT_STATUS_INVALID_PARAMETER;
2069 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2070 return NT_STATUS_INVALID_PARAMETER;
2073 oldname = pdb_get_username(old_acct);
2075 /* rename the posix user */
2076 rename_script = SMB_STRDUP(lp_renameuser_script());
2077 if (rename_script == NULL) {
2078 return NT_STATUS_NO_MEMORY;
2081 if (!(*rename_script)) {
2082 SAFE_FREE(rename_script);
2083 return NT_STATUS_ACCESS_DENIED;
2086 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2089 /* We have to allow the account name to end with a '$'.
2090 Also, follow the semantics in _samr_create_user() and lower case the
2091 posix name but preserve the case in passdb */
2093 fstrcpy( oldname_lower, oldname );
2094 strlower_m( oldname_lower );
2095 fstrcpy( newname_lower, newname );
2096 strlower_m( newname_lower );
2097 rename_script = realloc_string_sub2(rename_script,
2102 if (!rename_script) {
2103 return NT_STATUS_NO_MEMORY;
2105 rename_script = realloc_string_sub2(rename_script,
2110 rc = smbrun(rename_script, NULL);
2112 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2113 rename_script, rc));
2115 SAFE_FREE(rename_script);
2118 smb_nscd_flush_user_cache();
2122 return NT_STATUS_UNSUCCESSFUL;
2124 return NT_STATUS_OK;
2127 /**********************************************************************
2128 Add struct samu to LDAP.
2129 *********************************************************************/
2131 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2133 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2134 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2136 LDAPMessage *result = NULL;
2137 LDAPMessage *entry = NULL;
2138 LDAPMod **mods = NULL;
2139 int ldap_op = LDAP_MOD_REPLACE;
2140 uint32_t num_result;
2141 const char **attr_list;
2142 char *escape_user = NULL;
2143 const char *username = pdb_get_username(newpwd);
2144 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2145 char *filter = NULL;
2147 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2148 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2151 return NT_STATUS_NO_MEMORY;
2154 if (!username || !*username) {
2155 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2156 status = NT_STATUS_INVALID_PARAMETER;
2160 /* free this list after the second search or in case we exit on failure */
2161 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2163 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2165 if (rc != LDAP_SUCCESS) {
2169 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2170 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2174 ldap_msgfree(result);
2177 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2178 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2180 if (rc == LDAP_SUCCESS) {
2181 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2182 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2183 "already in the base, with samba "
2184 "attributes\n", sid_string_dbg(sid)));
2187 ldap_msgfree(result);
2192 /* does the entry already exist but without a samba attributes?
2193 we need to return the samba attributes here */
2195 escape_user = escape_ldap_string(talloc_tos(), username);
2196 filter = talloc_strdup(attr_list, "(uid=%u)");
2198 status = NT_STATUS_NO_MEMORY;
2201 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2202 TALLOC_FREE(escape_user);
2204 status = NT_STATUS_NO_MEMORY;
2208 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2209 filter, attr_list, &result);
2210 if ( rc != LDAP_SUCCESS ) {
2214 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2216 if (num_result > 1) {
2217 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2221 /* Check if we need to update an existing entry */
2222 if (num_result == 1) {
2223 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2224 ldap_op = LDAP_MOD_REPLACE;
2225 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2226 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2228 status = NT_STATUS_NO_MEMORY;
2232 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2234 /* There might be a SID for this account already - say an idmap entry */
2236 filter = talloc_asprintf(ctx,
2237 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2238 get_userattr_key2string(ldap_state->schema_ver,
2239 LDAP_ATTR_USER_SID),
2240 sid_string_talloc(ctx, sid),
2241 LDAP_OBJ_IDMAP_ENTRY,
2242 LDAP_OBJ_SID_ENTRY);
2244 status = NT_STATUS_NO_MEMORY;
2248 /* free old result before doing a new search */
2249 if (result != NULL) {
2250 ldap_msgfree(result);
2253 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2254 filter, attr_list, &result);
2256 if ( rc != LDAP_SUCCESS ) {
2260 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2262 if (num_result > 1) {
2263 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2267 /* Check if we need to update an existing entry */
2268 if (num_result == 1) {
2270 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2271 ldap_op = LDAP_MOD_REPLACE;
2272 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2273 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2275 status = NT_STATUS_NO_MEMORY;
2281 if (num_result == 0) {
2282 char *escape_username;
2283 /* Check if we need to add an entry */
2284 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2285 ldap_op = LDAP_MOD_ADD;
2287 escape_username = escape_rdn_val_string_alloc(username);
2288 if (!escape_username) {
2289 status = NT_STATUS_NO_MEMORY;
2293 if (username[strlen(username)-1] == '$') {
2294 dn = talloc_asprintf(ctx,
2297 lp_ldap_machine_suffix());
2299 dn = talloc_asprintf(ctx,
2302 lp_ldap_user_suffix());
2305 SAFE_FREE(escape_username);
2307 status = NT_STATUS_NO_MEMORY;
2312 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2313 pdb_element_is_set_or_changed)) {
2314 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2316 ldap_mods_free(mods, true);
2322 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2325 switch ( ldap_state->schema_ver ) {
2326 case SCHEMAVER_SAMBAACCOUNT:
2327 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2329 case SCHEMAVER_SAMBASAMACCOUNT:
2330 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2333 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2337 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2338 if (!NT_STATUS_IS_OK(ret)) {
2339 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2340 pdb_get_username(newpwd),dn));
2341 ldap_mods_free(mods, true);
2345 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2346 ldap_mods_free(mods, true);
2348 status = NT_STATUS_OK;
2354 ldap_msgfree(result);
2359 /**********************************************************************
2360 *********************************************************************/
2362 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2364 LDAPMessage ** result)
2366 int scope = LDAP_SCOPE_SUBTREE;
2368 const char **attr_list;
2370 attr_list = get_attr_list(NULL, groupmap_attr_list);
2371 rc = smbldap_search(ldap_state->smbldap_state,
2372 lp_ldap_suffix (), scope,
2373 filter, attr_list, 0, result);
2374 TALLOC_FREE(attr_list);
2379 /**********************************************************************
2380 *********************************************************************/
2382 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2383 GROUP_MAP *map, LDAPMessage *entry)
2386 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2388 if (ldap_state == NULL || map == NULL || entry == NULL ||
2389 ldap_state->smbldap_state->ldap_struct == NULL) {
2390 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2395 temp = smbldap_talloc_single_attribute(
2396 ldap_state->smbldap_state->ldap_struct,
2398 get_attr_key2string(groupmap_attr_list,
2399 LDAP_ATTR_GIDNUMBER),
2402 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2403 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2407 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2409 map->gid = (gid_t)atol(temp);
2412 temp = smbldap_talloc_single_attribute(
2413 ldap_state->smbldap_state->ldap_struct,
2415 get_attr_key2string(groupmap_attr_list,
2416 LDAP_ATTR_GROUP_SID),
2419 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2420 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2425 if (!string_to_sid(&map->sid, temp)) {
2426 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", 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_TYPE),
2439 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2440 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2444 map->sid_name_use = (enum lsa_SidType)atol(temp);
2446 if ((map->sid_name_use < SID_NAME_USER) ||
2447 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2448 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2454 temp = smbldap_talloc_single_attribute(
2455 ldap_state->smbldap_state->ldap_struct,
2457 get_attr_key2string(groupmap_attr_list,
2458 LDAP_ATTR_DISPLAY_NAME),
2461 temp = smbldap_talloc_single_attribute(
2462 ldap_state->smbldap_state->ldap_struct,
2464 get_attr_key2string(groupmap_attr_list,
2468 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2469 for gidNumber(%lu)\n",(unsigned long)map->gid));
2474 fstrcpy(map->nt_name, temp);
2477 temp = smbldap_talloc_single_attribute(
2478 ldap_state->smbldap_state->ldap_struct,
2480 get_attr_key2string(groupmap_attr_list,
2484 temp = talloc_strdup(ctx, "");
2490 fstrcpy(map->comment, temp);
2492 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2493 store_gid_sid_cache(&map->sid, map->gid);
2494 idmap_cache_set_sid2gid(&map->sid, map->gid);
2501 /**********************************************************************
2502 *********************************************************************/
2504 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2508 struct ldapsam_privates *ldap_state =
2509 (struct ldapsam_privates *)methods->private_data;
2510 LDAPMessage *result = NULL;
2511 LDAPMessage *entry = NULL;
2514 if (ldapsam_search_one_group(ldap_state, filter, &result)
2516 return NT_STATUS_NO_SUCH_GROUP;
2519 count = ldap_count_entries(priv2ld(ldap_state), result);
2522 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2524 ldap_msgfree(result);
2525 return NT_STATUS_NO_SUCH_GROUP;
2529 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2530 "count=%d\n", filter, count));
2531 ldap_msgfree(result);
2532 return NT_STATUS_NO_SUCH_GROUP;
2535 entry = ldap_first_entry(priv2ld(ldap_state), result);
2538 ldap_msgfree(result);
2539 return NT_STATUS_UNSUCCESSFUL;
2542 if (!init_group_from_ldap(ldap_state, map, entry)) {
2543 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2544 "group filter %s\n", filter));
2545 ldap_msgfree(result);
2546 return NT_STATUS_NO_SUCH_GROUP;
2549 ldap_msgfree(result);
2550 return NT_STATUS_OK;
2553 /**********************************************************************
2554 *********************************************************************/
2556 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2559 char *filter = NULL;
2563 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2565 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2566 sid_to_fstring(tmp, &sid)) < 0) {
2567 return NT_STATUS_NO_MEMORY;
2570 status = ldapsam_getgroup(methods, filter, map);
2575 /**********************************************************************
2576 *********************************************************************/
2578 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2581 char *filter = NULL;
2584 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2586 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2587 (unsigned long)gid) < 0) {
2588 return NT_STATUS_NO_MEMORY;
2591 status = ldapsam_getgroup(methods, filter, map);
2596 /**********************************************************************
2597 *********************************************************************/
2599 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2602 char *filter = NULL;
2603 char *escape_name = escape_ldap_string(talloc_tos(), name);
2607 return NT_STATUS_NO_MEMORY;
2610 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2612 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2613 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2615 TALLOC_FREE(escape_name);
2616 return NT_STATUS_NO_MEMORY;
2619 TALLOC_FREE(escape_name);
2620 status = ldapsam_getgroup(methods, filter, map);
2625 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2627 const struct dom_sid *domain_sid,
2633 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2634 str, sizeof(str)-1)) {
2635 DEBUG(10, ("Could not find sambaSID attribute\n"));
2639 if (!string_to_sid(&sid, str)) {
2640 DEBUG(10, ("Could not convert string %s to sid\n", str));
2644 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2645 DEBUG(10, ("SID %s is not in expected domain %s\n",
2646 str, sid_string_dbg(domain_sid)));
2650 if (!sid_peek_rid(&sid, rid)) {
2651 DEBUG(10, ("Could not peek into RID\n"));
2658 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2659 TALLOC_CTX *mem_ctx,
2660 const struct dom_sid *group,
2661 uint32_t **pp_member_rids,
2662 size_t *p_num_members)
2664 struct ldapsam_privates *ldap_state =
2665 (struct ldapsam_privates *)methods->private_data;
2666 struct smbldap_state *conn = ldap_state->smbldap_state;
2667 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2668 const char *sid_attrs[] = { "sambaSID", NULL };
2669 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2670 LDAPMessage *result = NULL;
2673 char **values = NULL;
2678 *pp_member_rids = NULL;
2681 filter = talloc_asprintf(mem_ctx,
2682 "(&(objectClass=%s)"
2685 LDAP_OBJ_POSIXGROUP,
2687 sid_string_talloc(mem_ctx, group));
2688 if (filter == NULL) {
2689 ret = NT_STATUS_NO_MEMORY;
2693 rc = smbldap_search(conn, lp_ldap_suffix(),
2694 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2697 if (rc != LDAP_SUCCESS)
2700 talloc_autofree_ldapmsg(mem_ctx, result);
2702 count = ldap_count_entries(conn->ldap_struct, result);
2705 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2706 sid_string_dbg(group)));
2707 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2712 ret = NT_STATUS_NO_SUCH_GROUP;
2716 entry = ldap_first_entry(conn->ldap_struct, result);
2720 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2722 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2723 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2727 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2729 if ((values != NULL) && (values[0] != NULL)) {
2731 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2732 if (filter == NULL) {
2733 ret = NT_STATUS_NO_MEMORY;
2737 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2738 char *escape_memberuid;
2740 escape_memberuid = escape_ldap_string(talloc_tos(),
2742 if (escape_memberuid == NULL) {
2743 ret = NT_STATUS_NO_MEMORY;
2747 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2748 TALLOC_FREE(escape_memberuid);
2749 if (filter == NULL) {
2750 ret = NT_STATUS_NO_MEMORY;
2755 filter = talloc_asprintf_append_buffer(filter, "))");
2756 if (filter == NULL) {
2757 ret = NT_STATUS_NO_MEMORY;
2761 rc = smbldap_search(conn, lp_ldap_suffix(),
2762 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2765 if (rc != LDAP_SUCCESS)
2768 count = ldap_count_entries(conn->ldap_struct, result);
2769 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2771 talloc_autofree_ldapmsg(mem_ctx, result);
2773 for (entry = ldap_first_entry(conn->ldap_struct, result);
2775 entry = ldap_next_entry(conn->ldap_struct, entry))
2781 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2785 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2786 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2787 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2791 if (!string_to_sid(&sid, sidstr))
2794 if (!sid_check_is_in_our_domain(&sid)) {
2795 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2796 "in our domain\n"));
2797 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2801 sid_peek_rid(&sid, &rid);
2803 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2805 ret = NT_STATUS_NO_MEMORY;
2811 filter = talloc_asprintf(mem_ctx,
2812 "(&(objectClass=%s)"
2814 LDAP_OBJ_SAMBASAMACCOUNT,
2817 rc = smbldap_search(conn, lp_ldap_suffix(),
2818 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2821 if (rc != LDAP_SUCCESS)
2824 talloc_autofree_ldapmsg(mem_ctx, result);
2826 for (entry = ldap_first_entry(conn->ldap_struct, result);
2828 entry = ldap_next_entry(conn->ldap_struct, entry))
2832 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2834 get_global_sam_sid(),
2836 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2837 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2841 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2843 ret = NT_STATUS_NO_MEMORY;
2853 ldap_value_free(values);
2858 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2859 TALLOC_CTX *mem_ctx,
2861 struct dom_sid **pp_sids,
2863 uint32_t *p_num_groups)
2865 struct ldapsam_privates *ldap_state =
2866 (struct ldapsam_privates *)methods->private_data;
2867 struct smbldap_state *conn = ldap_state->smbldap_state;
2869 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2872 LDAPMessage *result = NULL;
2874 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2878 gid_t primary_gid = -1;
2883 if (pdb_get_username(user) == NULL) {
2884 return NT_STATUS_INVALID_PARAMETER;
2887 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2888 if (escape_name == NULL)
2889 return NT_STATUS_NO_MEMORY;
2891 if (user->unix_pw) {
2892 primary_gid = user->unix_pw->pw_gid;
2894 /* retrieve the users primary gid */
2895 filter = talloc_asprintf(mem_ctx,
2896 "(&(objectClass=%s)(uid=%s))",
2897 LDAP_OBJ_SAMBASAMACCOUNT,
2899 if (filter == NULL) {
2900 ret = NT_STATUS_NO_MEMORY;
2904 rc = smbldap_search(conn, lp_ldap_suffix(),
2905 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2907 if (rc != LDAP_SUCCESS)
2910 talloc_autofree_ldapmsg(mem_ctx, result);
2912 count = ldap_count_entries(priv2ld(ldap_state), result);
2916 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2917 ret = NT_STATUS_NO_SUCH_USER;
2920 entry = ldap_first_entry(priv2ld(ldap_state), result);
2922 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2924 DEBUG (1, ("Unable to find the member's gid!\n"));
2925 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2928 primary_gid = strtoul(gidstr, NULL, 10);
2931 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2932 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2937 filter = talloc_asprintf(mem_ctx,
2938 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2939 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2940 if (filter == NULL) {
2941 ret = NT_STATUS_NO_MEMORY;
2945 rc = smbldap_search(conn, lp_ldap_suffix(),
2946 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2948 if (rc != LDAP_SUCCESS)
2951 talloc_autofree_ldapmsg(mem_ctx, result);
2959 /* We need to add the primary group as the first gid/sid */
2961 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2962 ret = NT_STATUS_NO_MEMORY;
2966 /* This sid will be replaced later */
2968 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2970 if (!NT_STATUS_IS_OK(ret)) {
2974 for (entry = ldap_first_entry(conn->ldap_struct, result);
2976 entry = ldap_next_entry(conn->ldap_struct, entry))
2983 if (!smbldap_get_single_attribute(conn->ldap_struct,
2985 str, sizeof(str)-1))
2988 if (!string_to_sid(&sid, str))
2991 if (!smbldap_get_single_attribute(conn->ldap_struct,
2993 str, sizeof(str)-1))
2996 gid = strtoul(str, &end, 10);
2998 if (PTR_DIFF(end, str) != strlen(str))
3001 if (gid == primary_gid) {
3002 sid_copy(&(*pp_sids)[0], &sid);
3004 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3006 ret = NT_STATUS_NO_MEMORY;
3009 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3011 if (!NT_STATUS_IS_OK(ret)) {
3017 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3018 DEBUG(3, ("primary group of [%s] not found\n",
3019 pdb_get_username(user)));
3023 *p_num_groups = num_sids;
3029 TALLOC_FREE(escape_name);
3033 /**********************************************************************
3034 * Augment a posixGroup object with a sambaGroupMapping domgroup
3035 *********************************************************************/
3037 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3038 struct ldapsam_privates *ldap_state,
3041 const char *filter, *dn;
3042 LDAPMessage *msg, *entry;
3046 filter = talloc_asprintf(mem_ctx,
3047 "(&(objectClass=%s)(gidNumber=%u))",
3048 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3049 if (filter == NULL) {
3050 return NT_STATUS_NO_MEMORY;
3053 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3054 get_attr_list(mem_ctx, groupmap_attr_list),
3056 talloc_autofree_ldapmsg(mem_ctx, msg);
3058 if ((rc != LDAP_SUCCESS) ||
3059 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3060 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3061 return NT_STATUS_NO_SUCH_GROUP;
3064 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3066 return NT_STATUS_NO_MEMORY;
3070 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3072 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3073 sid_string_talloc(mem_ctx, &map->sid));
3074 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3075 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3076 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3078 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3080 talloc_autofree_ldapmod(mem_ctx, mods);
3082 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3083 if (rc != LDAP_SUCCESS) {
3084 return NT_STATUS_ACCESS_DENIED;
3087 return NT_STATUS_OK;
3090 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3093 struct ldapsam_privates *ldap_state =
3094 (struct ldapsam_privates *)methods->private_data;
3095 LDAPMessage *msg = NULL;
3096 LDAPMod **mods = NULL;
3097 const char *attrs[] = { NULL };
3101 TALLOC_CTX *mem_ctx;
3108 mem_ctx = talloc_new(NULL);
3109 if (mem_ctx == NULL) {
3110 DEBUG(0, ("talloc_new failed\n"));
3111 return NT_STATUS_NO_MEMORY;
3114 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3115 sid_string_talloc(mem_ctx, &map->sid));
3116 if (filter == NULL) {
3117 result = NT_STATUS_NO_MEMORY;
3121 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3122 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3123 talloc_autofree_ldapmsg(mem_ctx, msg);
3125 if ((rc == LDAP_SUCCESS) &&
3126 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3128 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3129 "group mapping entry\n", sid_string_dbg(&map->sid)));
3130 result = NT_STATUS_GROUP_EXISTS;
3134 switch (map->sid_name_use) {
3136 case SID_NAME_DOM_GRP:
3137 /* To map a domain group we need to have a posix group
3139 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3143 case SID_NAME_ALIAS:
3144 if (!sid_check_is_in_our_domain(&map->sid)
3145 && !sid_check_is_in_builtin(&map->sid) )
3147 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3148 sid_string_dbg(&map->sid)));
3149 result = NT_STATUS_INVALID_PARAMETER;
3155 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3156 sid_type_lookup(map->sid_name_use)));
3157 result = NT_STATUS_INVALID_PARAMETER;
3161 /* Domain groups have been mapped in a separate routine, we have to
3162 * create an alias now */
3164 if (map->gid == -1) {
3165 DEBUG(10, ("Refusing to map gid==-1\n"));
3166 result = NT_STATUS_INVALID_PARAMETER;
3170 if (pdb_gid_to_sid(map->gid, &sid)) {
3171 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3172 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3173 result = NT_STATUS_GROUP_EXISTS;
3177 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3178 * the best we can get out of LDAP. */
3180 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3181 sid_string_talloc(mem_ctx, &map->sid),
3182 lp_ldap_group_suffix());
3184 result = NT_STATUS_NO_MEMORY;
3190 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3191 LDAP_OBJ_SID_ENTRY);
3192 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3194 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3195 sid_string_talloc(mem_ctx, &map->sid));
3196 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3197 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3198 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3200 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3202 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3203 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3204 talloc_autofree_ldapmod(mem_ctx, mods);
3206 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3208 result = (rc == LDAP_SUCCESS) ?
3209 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3212 TALLOC_FREE(mem_ctx);
3216 /**********************************************************************
3217 * Update a group mapping entry. We're quite strict about what can be changed:
3218 * Only the description and displayname may be changed. It simply does not
3219 * make any sense to change the SID, gid or the type in a mapping.
3220 *********************************************************************/
3222 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3225 struct ldapsam_privates *ldap_state =
3226 (struct ldapsam_privates *)methods->private_data;
3228 const char *filter, *dn;
3229 LDAPMessage *msg = NULL;
3230 LDAPMessage *entry = NULL;
3231 LDAPMod **mods = NULL;
3232 TALLOC_CTX *mem_ctx;
3235 mem_ctx = talloc_new(NULL);
3236 if (mem_ctx == NULL) {
3237 DEBUG(0, ("talloc_new failed\n"));
3238 return NT_STATUS_NO_MEMORY;
3241 /* Make 100% sure that sid, gid and type are not changed by looking up
3242 * exactly the values we're given in LDAP. */
3244 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3245 "(sambaSid=%s)(gidNumber=%u)"
3246 "(sambaGroupType=%d))",
3248 sid_string_talloc(mem_ctx, &map->sid),
3249 (unsigned int)map->gid, map->sid_name_use);
3250 if (filter == NULL) {
3251 result = NT_STATUS_NO_MEMORY;
3255 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3256 get_attr_list(mem_ctx, groupmap_attr_list),
3258 talloc_autofree_ldapmsg(mem_ctx, msg);
3260 if ((rc != LDAP_SUCCESS) ||
3261 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3262 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3263 result = NT_STATUS_NO_SUCH_GROUP;
3267 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3270 result = NT_STATUS_NO_MEMORY;
3275 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3277 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3279 talloc_autofree_ldapmod(mem_ctx, mods);
3282 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3283 "nothing to do\n"));
3284 result = NT_STATUS_OK;
3288 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3290 if (rc != LDAP_SUCCESS) {
3291 result = NT_STATUS_ACCESS_DENIED;
3295 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3296 "group %lu in LDAP\n", (unsigned long)map->gid));
3298 result = NT_STATUS_OK;
3301 TALLOC_FREE(mem_ctx);
3305 /**********************************************************************
3306 *********************************************************************/
3308 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3311 struct ldapsam_privates *priv =
3312 (struct ldapsam_privates *)methods->private_data;
3313 LDAPMessage *msg, *entry;
3316 TALLOC_CTX *mem_ctx;
3319 mem_ctx = talloc_new(NULL);
3320 if (mem_ctx == NULL) {
3321 DEBUG(0, ("talloc_new failed\n"));
3322 return NT_STATUS_NO_MEMORY;
3325 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3326 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3327 sid_string_talloc(mem_ctx, &sid));
3328 if (filter == NULL) {
3329 result = NT_STATUS_NO_MEMORY;
3332 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3333 get_attr_list(mem_ctx, groupmap_attr_list),
3335 talloc_autofree_ldapmsg(mem_ctx, msg);
3337 if ((rc != LDAP_SUCCESS) ||
3338 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3339 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3340 result = NT_STATUS_NO_SUCH_GROUP;
3344 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3345 get_attr_list(mem_ctx,
3346 groupmap_attr_list_to_delete));
3348 if ((rc == LDAP_NAMING_VIOLATION) ||
3349 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3350 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3351 const char *attrs[] = { "sambaGroupType", "description",
3352 "displayName", "sambaSIDList",
3355 /* Second try. Don't delete the sambaSID attribute, this is
3356 for "old" entries that are tacked on a winbind
3359 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3360 LDAP_OBJ_GROUPMAP, attrs);
3363 if ((rc == LDAP_NAMING_VIOLATION) ||
3364 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3365 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3366 const char *attrs[] = { "sambaGroupType", "description",
3367 "displayName", "sambaSIDList",
3368 "gidNumber", NULL };
3370 /* Third try. This is a post-3.0.21 alias (containing only
3371 * sambaSidEntry and sambaGroupMapping classes), we also have
3372 * to delete the gidNumber attribute, only the sambaSidEntry
3375 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3376 LDAP_OBJ_GROUPMAP, attrs);
3379 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3382 TALLOC_FREE(mem_ctx);
3386 /**********************************************************************
3387 *********************************************************************/
3389 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3392 struct ldapsam_privates *ldap_state =
3393 (struct ldapsam_privates *)my_methods->private_data;
3394 char *filter = NULL;
3396 const char **attr_list;
3398 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3400 return NT_STATUS_NO_MEMORY;
3402 attr_list = get_attr_list( NULL, groupmap_attr_list );
3403 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3404 LDAP_SCOPE_SUBTREE, filter,
3405 attr_list, 0, &ldap_state->result);
3406 TALLOC_FREE(attr_list);
3408 if (rc != LDAP_SUCCESS) {
3409 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3410 ldap_err2string(rc)));
3411 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3412 lp_ldap_suffix(), filter));
3413 ldap_msgfree(ldap_state->result);
3414 ldap_state->result = NULL;
3415 TALLOC_FREE(filter);
3416 return NT_STATUS_UNSUCCESSFUL;
3419 TALLOC_FREE(filter);
3421 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3422 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3423 ldap_state->result)));
3426 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3427 ldap_state->result);
3428 ldap_state->index = 0;
3430 return NT_STATUS_OK;
3433 /**********************************************************************
3434 *********************************************************************/
3436 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3438 ldapsam_endsampwent(my_methods);
3441 /**********************************************************************
3442 *********************************************************************/
3444 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3447 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3448 struct ldapsam_privates *ldap_state =
3449 (struct ldapsam_privates *)my_methods->private_data;
3453 if (!ldap_state->entry)
3456 ldap_state->index++;
3457 bret = init_group_from_ldap(ldap_state, map,
3461 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3465 return NT_STATUS_OK;
3468 /**********************************************************************
3469 *********************************************************************/
3471 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3472 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3473 GROUP_MAP **pp_rmap,
3474 size_t *p_num_entries,
3477 GROUP_MAP map = { 0, };
3483 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3484 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3486 return NT_STATUS_ACCESS_DENIED;
3489 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3490 if (sid_name_use != SID_NAME_UNKNOWN &&
3491 sid_name_use != map.sid_name_use) {
3492 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3493 "not of the requested type\n", map.nt_name));
3496 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3497 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3498 "non mapped\n", map.nt_name));
3502 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3504 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3505 "enlarge group map!\n"));
3506 return NT_STATUS_UNSUCCESSFUL;
3509 (*pp_rmap)[entries] = map;
3514 ldapsam_endsamgrent(methods);
3516 *p_num_entries = entries;
3518 return NT_STATUS_OK;
3521 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3522 const struct dom_sid *alias,
3523 const struct dom_sid *member,
3526 struct ldapsam_privates *ldap_state =
3527 (struct ldapsam_privates *)methods->private_data;
3529 LDAPMessage *result = NULL;
3530 LDAPMessage *entry = NULL;
3532 LDAPMod **mods = NULL;
3534 enum lsa_SidType type = SID_NAME_USE_NONE;
3537 char *filter = NULL;
3539 if (sid_check_is_in_builtin(alias)) {
3540 type = SID_NAME_ALIAS;
3543 if (sid_check_is_in_our_domain(alias)) {
3544 type = SID_NAME_ALIAS;
3547 if (type == SID_NAME_USE_NONE) {
3548 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3549 sid_string_dbg(alias)));
3550 return NT_STATUS_NO_SUCH_ALIAS;
3553 if (asprintf(&filter,
3554 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3555 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3557 return NT_STATUS_NO_MEMORY;
3560 if (ldapsam_search_one_group(ldap_state, filter,
3561 &result) != LDAP_SUCCESS) {
3563 return NT_STATUS_NO_SUCH_ALIAS;
3566 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3570 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3571 ldap_msgfree(result);
3573 return NT_STATUS_NO_SUCH_ALIAS;
3577 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3578 "filter %s: count=%d\n", filter, count));
3579 ldap_msgfree(result);
3581 return NT_STATUS_NO_SUCH_ALIAS;
3586 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3590 ldap_msgfree(result);
3591 return NT_STATUS_UNSUCCESSFUL;
3594 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3596 ldap_msgfree(result);
3597 return NT_STATUS_UNSUCCESSFUL;
3600 smbldap_set_mod(&mods, modop,
3601 get_attr_key2string(groupmap_attr_list,
3602 LDAP_ATTR_SID_LIST),
3603 sid_to_fstring(tmp, member));
3605 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3607 ldap_mods_free(mods, True);
3608 ldap_msgfree(result);
3611 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3612 return NT_STATUS_MEMBER_IN_ALIAS;
3615 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3616 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3619 if (rc != LDAP_SUCCESS) {
3620 return NT_STATUS_UNSUCCESSFUL;
3623 return NT_STATUS_OK;
3626 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3627 const struct dom_sid *alias,
3628 const struct dom_sid *member)
3630 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3633 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3634 const struct dom_sid *alias,
3635 const struct dom_sid *member)
3637 return ldapsam_modify_aliasmem(methods, alias, member,
3641 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3642 const struct dom_sid *alias,
3643 TALLOC_CTX *mem_ctx,
3644 struct dom_sid **pp_members,
3645 size_t *p_num_members)
3647 struct ldapsam_privates *ldap_state =
3648 (struct ldapsam_privates *)methods->private_data;
3649 LDAPMessage *result = NULL;
3650 LDAPMessage *entry = NULL;
3652 char **values = NULL;
3654 char *filter = NULL;
3655 uint32_t num_members = 0;
3656 enum lsa_SidType type = SID_NAME_USE_NONE;
3662 if (sid_check_is_in_builtin(alias)) {
3663 type = SID_NAME_ALIAS;
3666 if (sid_check_is_in_our_domain(alias)) {
3667 type = SID_NAME_ALIAS;
3670 if (type == SID_NAME_USE_NONE) {
3671 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3672 sid_string_dbg(alias)));
3673 return NT_STATUS_NO_SUCH_ALIAS;
3676 if (asprintf(&filter,
3677 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3678 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3680 return NT_STATUS_NO_MEMORY;
3683 if (ldapsam_search_one_group(ldap_state, filter,
3684 &result) != LDAP_SUCCESS) {
3686 return NT_STATUS_NO_SUCH_ALIAS;
3689 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3693 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3694 ldap_msgfree(result);
3696 return NT_STATUS_NO_SUCH_ALIAS;
3700 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3701 "filter %s: count=%d\n", filter, count));
3702 ldap_msgfree(result);
3704 return NT_STATUS_NO_SUCH_ALIAS;
3709 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3713 ldap_msgfree(result);
3714 return NT_STATUS_UNSUCCESSFUL;
3717 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3719 get_attr_key2string(groupmap_attr_list,
3720 LDAP_ATTR_SID_LIST));
3722 if (values == NULL) {
3723 ldap_msgfree(result);
3724 return NT_STATUS_OK;
3727 count = ldap_count_values(values);
3729 for (i=0; i<count; i++) {
3730 struct dom_sid member;
3733 if (!string_to_sid(&member, values[i]))
3736 status = add_sid_to_array(mem_ctx, &member, pp_members,
3738 if (!NT_STATUS_IS_OK(status)) {
3739 ldap_value_free(values);
3740 ldap_msgfree(result);
3745 *p_num_members = num_members;
3746 ldap_value_free(values);
3747 ldap_msgfree(result);
3749 return NT_STATUS_OK;
3752 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3753 TALLOC_CTX *mem_ctx,
3754 const struct dom_sid *domain_sid,
3755 const struct dom_sid *members,
3757 uint32_t **pp_alias_rids,
3758 size_t *p_num_alias_rids)
3760 struct ldapsam_privates *ldap_state =
3761 (struct ldapsam_privates *)methods->private_data;
3764 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3766 LDAPMessage *result = NULL;
3767 LDAPMessage *entry = NULL;
3771 enum lsa_SidType type = SID_NAME_USE_NONE;
3772 bool is_builtin = false;
3773 bool sid_added = false;
3775 *pp_alias_rids = NULL;
3776 *p_num_alias_rids = 0;
3778 if (sid_check_is_builtin(domain_sid)) {
3780 type = SID_NAME_ALIAS;
3783 if (sid_check_is_domain(domain_sid)) {
3784 type = SID_NAME_ALIAS;
3787 if (type == SID_NAME_USE_NONE) {
3788 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3789 sid_string_dbg(domain_sid)));
3790 return NT_STATUS_UNSUCCESSFUL;
3793 if (num_members == 0) {
3794 return NT_STATUS_OK;
3797 filter = talloc_asprintf(mem_ctx,
3798 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3799 LDAP_OBJ_GROUPMAP, type);
3801 for (i=0; i<num_members; i++)
3802 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3804 sid_string_talloc(mem_ctx,
3807 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3809 if (filter == NULL) {
3810 return NT_STATUS_NO_MEMORY;
3814 ldap_state->search_cache.filter &&
3815 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3816 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3817 result = ldap_state->search_cache.result;
3818 ldap_state->search_cache.result = NULL;
3820 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3821 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3822 if (rc != LDAP_SUCCESS) {
3823 return NT_STATUS_UNSUCCESSFUL;
3825 talloc_autofree_ldapmsg(filter, result);
3828 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3830 for (entry = ldap_first_entry(ldap_struct, result);
3832 entry = ldap_next_entry(ldap_struct, entry))
3838 if (!smbldap_get_single_attribute(ldap_struct, entry,
3844 if (!string_to_sid(&sid, sid_str))
3847 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3852 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3853 p_num_alias_rids)) {
3854 return NT_STATUS_NO_MEMORY;
3858 if (!is_builtin && !sid_added) {
3859 TALLOC_FREE(ldap_state->search_cache.filter);
3861 * Note: result is a talloc child of filter because of the
3862 * talloc_autofree_ldapmsg() usage
3864 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3865 ldap_state->search_cache.result = result;
3868 return NT_STATUS_OK;
3871 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3872 enum pdb_policy_type type,
3875 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3877 LDAPMod **mods = NULL;
3878 fstring value_string;
3879 const char *policy_attr = NULL;
3881 struct ldapsam_privates *ldap_state =
3882 (struct ldapsam_privates *)methods->private_data;
3884 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3886 if (!ldap_state->domain_dn) {
3887 return NT_STATUS_INVALID_PARAMETER;
3890 policy_attr = get_account_policy_attr(type);
3891 if (policy_attr == NULL) {
3892 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3897 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3899 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3901 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3904 ldap_mods_free(mods, True);
3906 if (rc != LDAP_SUCCESS) {
3910 if (!cache_account_policy_set(type, value)) {
3911 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3912 "update local tdb cache\n"));
3916 return NT_STATUS_OK;
3919 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3920 enum pdb_policy_type type,
3923 return ldapsam_set_account_policy_in_ldap(methods, type,
3927 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3928 enum pdb_policy_type type,
3931 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3932 LDAPMessage *result = NULL;
3933 LDAPMessage *entry = NULL;
3938 const char *policy_attr = NULL;
3940 struct ldapsam_privates *ldap_state =
3941 (struct ldapsam_privates *)methods->private_data;
3943 const char *attrs[2];
3945 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3947 if (!ldap_state->domain_dn) {
3948 return NT_STATUS_INVALID_PARAMETER;
3951 policy_attr = get_account_policy_attr(type);
3953 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3954 "policy index: %d\n", type));
3958 attrs[0] = policy_attr;
3961 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3962 if (filter == NULL) {
3963 return NT_STATUS_NO_MEMORY;
3965 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3966 LDAP_SCOPE_BASE, filter, attrs, 0,
3968 TALLOC_FREE(filter);
3969 if (rc != LDAP_SUCCESS) {
3973 count = ldap_count_entries(priv2ld(ldap_state), result);
3978 entry = ldap_first_entry(priv2ld(ldap_state), result);
3979 if (entry == NULL) {
3983 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3988 *value = (uint32_t)atol(vals[0]);
3990 ntstatus = NT_STATUS_OK;
3994 ldap_value_free(vals);
3995 ldap_msgfree(result);
4000 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4002 - if user hasn't decided to use account policies inside LDAP just reuse the
4005 - if there is a valid cache entry, return that
4006 - if there is an LDAP entry, update cache and return
4007 - otherwise set to default, update cache and return
4011 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4012 enum pdb_policy_type type,
4015 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4017 if (cache_account_policy_get(type, value)) {
4018 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4020 return NT_STATUS_OK;
4023 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4025 if (NT_STATUS_IS_OK(ntstatus)) {
4029 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4033 /* should we automagically migrate old tdb value here ? */
4034 if (account_policy_get(type, value))
4037 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4038 "default\n", type));
4041 if (!account_policy_get_default(type, value)) {
4047 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4048 if (!NT_STATUS_IS_OK(ntstatus)) {
4054 if (!cache_account_policy_set(type, *value)) {
4055 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4056 "tdb as a cache\n"));
4057 return NT_STATUS_UNSUCCESSFUL;
4060 return NT_STATUS_OK;
4063 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4064 const struct dom_sid *domain_sid,
4068 enum lsa_SidType *attrs)
4070 struct ldapsam_privates *ldap_state =
4071 (struct ldapsam_privates *)methods->private_data;
4072 LDAPMessage *msg = NULL;
4074 char *allsids = NULL;
4075 int i, rc, num_mapped;
4076 NTSTATUS result = NT_STATUS_NO_MEMORY;
4077 TALLOC_CTX *mem_ctx;
4081 mem_ctx = talloc_new(NULL);
4082 if (mem_ctx == NULL) {
4083 DEBUG(0, ("talloc_new failed\n"));
4087 if (!sid_check_is_builtin(domain_sid) &&
4088 !sid_check_is_domain(domain_sid)) {
4089 result = NT_STATUS_INVALID_PARAMETER;
4093 if (num_rids == 0) {
4094 result = NT_STATUS_NONE_MAPPED;
4098 for (i=0; i<num_rids; i++)
4099 attrs[i] = SID_NAME_UNKNOWN;
4101 allsids = talloc_strdup(mem_ctx, "");
4102 if (allsids == NULL) {
4106 for (i=0; i<num_rids; i++) {
4108 sid_compose(&sid, domain_sid, rids[i]);
4109 allsids = talloc_asprintf_append_buffer(
4110 allsids, "(sambaSid=%s)",
4111 sid_string_talloc(mem_ctx, &sid));
4112 if (allsids == NULL) {
4117 /* First look for users */
4121 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4123 filter = talloc_asprintf(
4124 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4125 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4127 if (filter == NULL) {
4131 rc = smbldap_search(ldap_state->smbldap_state,
4132 lp_ldap_user_suffix(),
4133 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4135 talloc_autofree_ldapmsg(mem_ctx, msg);
4138 if (rc != LDAP_SUCCESS)
4141 ld = ldap_state->smbldap_state->ldap_struct;
4144 for (entry = ldap_first_entry(ld, msg);
4146 entry = ldap_next_entry(ld, entry)) {
4151 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4153 DEBUG(2, ("Could not find sid from ldap entry\n"));
4157 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4160 DEBUG(2, ("Could not retrieve uid attribute\n"));
4164 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4165 if (rid == rids[rid_index])
4169 if (rid_index == num_rids) {
4170 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4174 attrs[rid_index] = SID_NAME_USER;
4175 names[rid_index] = name;
4179 if (num_mapped == num_rids) {
4180 /* No need to look for groups anymore -- we're done */
4181 result = NT_STATUS_OK;
4185 /* Same game for groups */
4189 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4190 "sambaGroupType", NULL };
4192 filter = talloc_asprintf(
4193 mem_ctx, "(&(objectClass=%s)(|%s))",
4194 LDAP_OBJ_GROUPMAP, allsids);
4195 if (filter == NULL) {
4199 rc = smbldap_search(ldap_state->smbldap_state,
4201 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4203 talloc_autofree_ldapmsg(mem_ctx, msg);
4206 if (rc != LDAP_SUCCESS)
4209 /* ldap_struct might have changed due to a reconnect */
4211 ld = ldap_state->smbldap_state->ldap_struct;
4213 /* For consistency checks, we already checked we're only domain or builtin */
4215 is_builtin = sid_check_is_builtin(domain_sid);
4217 for (entry = ldap_first_entry(ld, msg);
4219 entry = ldap_next_entry(ld, entry))
4224 enum lsa_SidType type;
4225 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4227 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4230 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4235 type = (enum lsa_SidType)atol(attr);
4237 /* Consistency checks */
4238 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4239 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4240 (type != SID_NAME_DOM_GRP)))) {
4241 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4244 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4246 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4250 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4253 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4255 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4259 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4264 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4265 if (rid == rids[rid_index])
4269 if (rid_index == num_rids) {
4270 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4274 attrs[rid_index] = type;
4275 names[rid_index] = attr;
4279 result = NT_STATUS_NONE_MAPPED;
4282 result = (num_mapped == num_rids) ?
4283 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4285 TALLOC_FREE(mem_ctx);
4289 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4291 char *filter = NULL;
4292 char *escaped = NULL;
4293 char *result = NULL;
4295 if (asprintf(&filter, "(&%s(objectclass=%s))",
4296 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4300 escaped = escape_ldap_string(talloc_tos(), username);
4301 if (escaped == NULL) goto done;
4303 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4307 TALLOC_FREE(escaped);
4312 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4316 const char **result;
4318 va_start(ap, mem_ctx);
4319 while (va_arg(ap, const char *) != NULL)
4323 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4327 va_start(ap, mem_ctx);
4328 for (i=0; i<num; i++) {
4329 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4330 if (result[i] == NULL) {
4331 talloc_free(result);
4342 struct ldap_search_state {
4343 struct smbldap_state *connection;
4345 uint32_t acct_flags;
4346 uint16_t group_type;
4353 void *pagedresults_cookie;
4355 LDAPMessage *entries, *current_entry;
4356 bool (*ldap2displayentry)(struct ldap_search_state *state,
4357 TALLOC_CTX *mem_ctx,
4358 LDAP *ld, LDAPMessage *entry,
4359 struct samr_displayentry *result);
4362 static bool ldapsam_search_firstpage(struct pdb_search *search)
4364 struct ldap_search_state *state =
4365 (struct ldap_search_state *)search->private_data;
4367 int rc = LDAP_OPERATIONS_ERROR;
4369 state->entries = NULL;
4371 if (state->connection->paged_results) {
4372 rc = smbldap_search_paged(state->connection, state->base,
4373 state->scope, state->filter,
4374 state->attrs, state->attrsonly,
4375 lp_ldap_page_size(), &state->entries,
4376 &state->pagedresults_cookie);
4379 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4381 if (state->entries != NULL) {
4382 /* Left over from unsuccessful paged attempt */
4383 ldap_msgfree(state->entries);
4384 state->entries = NULL;
4387 rc = smbldap_search(state->connection, state->base,
4388 state->scope, state->filter, state->attrs,
4389 state->attrsonly, &state->entries);
4391 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4394 /* Ok, the server was lying. It told us it could do paged
4395 * searches when it could not. */
4396 state->connection->paged_results = False;
4399 ld = state->connection->ldap_struct;
4401 DEBUG(5, ("Don't have an LDAP connection right after a "
4405 state->current_entry = ldap_first_entry(ld, state->entries);
4410 static bool ldapsam_search_nextpage(struct pdb_search *search)
4412 struct ldap_search_state *state =
4413 (struct ldap_search_state *)search->private_data;
4416 if (!state->connection->paged_results) {
4417 /* There is no next page when there are no paged results */
4421 rc = smbldap_search_paged(state->connection, state->base,
4422 state->scope, state->filter, state->attrs,
4423 state->attrsonly, lp_ldap_page_size(),
4425 &state->pagedresults_cookie);
4427 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4430 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4432 if (state->current_entry == NULL) {
4433 ldap_msgfree(state->entries);
4434 state->entries = NULL;
4441 static bool ldapsam_search_next_entry(struct pdb_search *search,
4442 struct samr_displayentry *entry)
4444 struct ldap_search_state *state =
4445 (struct ldap_search_state *)search->private_data;
4449 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4452 if ((state->entries == NULL) &&
4453 !ldapsam_search_nextpage(search))
4456 if (state->current_entry == NULL) {
4460 result = state->ldap2displayentry(state, search,
4461 state->connection->ldap_struct,
4462 state->current_entry, entry);
4466 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4467 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4468 if (dn != NULL) ldap_memfree(dn);
4471 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4473 if (state->current_entry == NULL) {
4474 ldap_msgfree(state->entries);
4475 state->entries = NULL;
4478 if (!result) goto retry;
4483 static void ldapsam_search_end(struct pdb_search *search)
4485 struct ldap_search_state *state =
4486 (struct ldap_search_state *)search->private_data;
4489 if (state->pagedresults_cookie == NULL)
4492 if (state->entries != NULL)
4493 ldap_msgfree(state->entries);
4495 state->entries = NULL;
4496 state->current_entry = NULL;
4498 if (!state->connection->paged_results)
4501 /* Tell the LDAP server we're not interested in the rest anymore. */
4503 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4504 state->filter, state->attrs,
4505 state->attrsonly, 0, &state->entries,
4506 &state->pagedresults_cookie);
4508 if (rc != LDAP_SUCCESS)
4509 DEBUG(5, ("Could not end search properly\n"));
4514 static bool ldapuser2displayentry(struct ldap_search_state *state,
4515 TALLOC_CTX *mem_ctx,
4516 LDAP *ld, LDAPMessage *entry,
4517 struct samr_displayentry *result)
4520 size_t converted_size;
4522 uint32_t acct_flags;
4524 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4525 if ((vals == NULL) || (vals[0] == NULL)) {
4526 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4529 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4530 ldap_value_free(vals);
4532 if ((state->acct_flags != 0) &&
4533 ((state->acct_flags & acct_flags) == 0))
4536 result->acct_flags = acct_flags;
4537 result->account_name = "";
4538 result->fullname = "";
4539 result->description = "";
4541 vals = ldap_get_values(ld, entry, "uid");
4542 if ((vals == NULL) || (vals[0] == NULL)) {
4543 DEBUG(5, ("\"uid\" not found\n"));
4546 if (!pull_utf8_talloc(mem_ctx,
4547 discard_const_p(char *, &result->account_name),
4548 vals[0], &converted_size))
4550 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4554 ldap_value_free(vals);
4556 vals = ldap_get_values(ld, entry, "displayName");
4557 if ((vals == NULL) || (vals[0] == NULL))
4558 DEBUG(8, ("\"displayName\" not found\n"));
4559 else if (!pull_utf8_talloc(mem_ctx,
4560 discard_const_p(char *, &result->fullname),
4561 vals[0], &converted_size))
4563 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4567 ldap_value_free(vals);
4569 vals = ldap_get_values(ld, entry, "description");
4570 if ((vals == NULL) || (vals[0] == NULL))
4571 DEBUG(8, ("\"description\" not found\n"));
4572 else if (!pull_utf8_talloc(mem_ctx,
4573 discard_const_p(char *, &result->description),
4574 vals[0], &converted_size))
4576 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4580 ldap_value_free(vals);
4582 if ((result->account_name == NULL) ||
4583 (result->fullname == NULL) ||
4584 (result->description == NULL)) {
4585 DEBUG(0, ("talloc failed\n"));
4589 vals = ldap_get_values(ld, entry, "sambaSid");
4590 if ((vals == NULL) || (vals[0] == NULL)) {
4591 DEBUG(0, ("\"objectSid\" not found\n"));
4595 if (!string_to_sid(&sid, vals[0])) {
4596 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4597 ldap_value_free(vals);
4600 ldap_value_free(vals);
4602 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4603 DEBUG(0, ("sid %s does not belong to our domain\n",
4604 sid_string_dbg(&sid)));
4612 static bool ldapsam_search_users(struct pdb_methods *methods,
4613 struct pdb_search *search,
4614 uint32_t acct_flags)
4616 struct ldapsam_privates *ldap_state =
4617 (struct ldapsam_privates *)methods->private_data;
4618 struct ldap_search_state *state;
4620 state = talloc(search, struct ldap_search_state);
4621 if (state == NULL) {
4622 DEBUG(0, ("talloc failed\n"));
4626 state->connection = ldap_state->smbldap_state;
4628 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4629 state->base = lp_ldap_user_suffix();
4630 else if ((acct_flags != 0) &&
4631 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4632 state->base = lp_ldap_machine_suffix();
4634 state->base = lp_ldap_suffix();
4636 state->acct_flags = acct_flags;
4637 state->base = talloc_strdup(search, state->base);
4638 state->scope = LDAP_SCOPE_SUBTREE;
4639 state->filter = get_ldap_filter(search, "*");
4640 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4641 "displayName", "description",
4642 "sambaAcctFlags", NULL);
4643 state->attrsonly = 0;
4644 state->pagedresults_cookie = NULL;
4645 state->entries = NULL;
4646 state->ldap2displayentry = ldapuser2displayentry;
4648 if ((state->filter == NULL) || (state->attrs == NULL)) {
4649 DEBUG(0, ("talloc failed\n"));
4653 search->private_data = state;
4654 search->next_entry = ldapsam_search_next_entry;
4655 search->search_end = ldapsam_search_end;
4657 return ldapsam_search_firstpage(search);
4660 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4661 TALLOC_CTX *mem_ctx,
4662 LDAP *ld, LDAPMessage *entry,
4663 struct samr_displayentry *result)
4666 size_t converted_size;
4668 uint16_t group_type;
4670 result->account_name = "";
4671 result->fullname = "";
4672 result->description = "";
4675 vals = ldap_get_values(ld, entry, "sambaGroupType");
4676 if ((vals == NULL) || (vals[0] == NULL)) {
4677 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4679 ldap_value_free(vals);
4684 group_type = atoi(vals[0]);
4686 if ((state->group_type != 0) &&
4687 ((state->group_type != group_type))) {
4688 ldap_value_free(vals);
4692 ldap_value_free(vals);
4694 /* display name is the NT group name */
4696 vals = ldap_get_values(ld, entry, "displayName");
4697 if ((vals == NULL) || (vals[0] == NULL)) {
4698 DEBUG(8, ("\"displayName\" not found\n"));
4700 /* fallback to the 'cn' attribute */
4701 vals = ldap_get_values(ld, entry, "cn");
4702 if ((vals == NULL) || (vals[0] == NULL)) {
4703 DEBUG(5, ("\"cn\" not found\n"));
4706 if (!pull_utf8_talloc(mem_ctx,
4707 discard_const_p(char *,
4708 &result->account_name),
4709 vals[0], &converted_size))
4711 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4712 "failed: %s", strerror(errno)));
4715 else if (!pull_utf8_talloc(mem_ctx,
4716 discard_const_p(char *,
4717 &result->account_name),
4718 vals[0], &converted_size))
4720 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4724 ldap_value_free(vals);
4726 vals = ldap_get_values(ld, entry, "description");
4727 if ((vals == NULL) || (vals[0] == NULL))
4728 DEBUG(8, ("\"description\" not found\n"));
4729 else if (!pull_utf8_talloc(mem_ctx,
4730 discard_const_p(char *, &result->description),
4731 vals[0], &converted_size))
4733 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4736 ldap_value_free(vals);
4738 if ((result->account_name == NULL) ||
4739 (result->fullname == NULL) ||
4740 (result->description == NULL)) {
4741 DEBUG(0, ("talloc failed\n"));
4745 vals = ldap_get_values(ld, entry, "sambaSid");
4746 if ((vals == NULL) || (vals[0] == NULL)) {
4747 DEBUG(0, ("\"objectSid\" not found\n"));
4749 ldap_value_free(vals);
4754 if (!string_to_sid(&sid, vals[0])) {
4755 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4759 ldap_value_free(vals);
4761 switch (group_type) {
4762 case SID_NAME_DOM_GRP:
4763 case SID_NAME_ALIAS:
4765 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4766 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4768 DEBUG(0, ("%s is not in our domain\n",
4769 sid_string_dbg(&sid)));
4775 DEBUG(0,("unknown group type: %d\n", group_type));
4779 result->acct_flags = 0;
4784 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4785 struct pdb_search *search,
4786 const struct dom_sid *sid,
4787 enum lsa_SidType type)
4789 struct ldapsam_privates *ldap_state =
4790 (struct ldapsam_privates *)methods->private_data;
4791 struct ldap_search_state *state;
4794 state = talloc(search, struct ldap_search_state);
4795 if (state == NULL) {
4796 DEBUG(0, ("talloc failed\n"));
4800 state->connection = ldap_state->smbldap_state;
4802 state->base = talloc_strdup(search, lp_ldap_suffix());
4803 state->connection = ldap_state->smbldap_state;
4804 state->scope = LDAP_SCOPE_SUBTREE;
4805 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4806 "(sambaGroupType=%d)(sambaSID=%s*))",
4808 type, sid_to_fstring(tmp, sid));
4809 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4810 "displayName", "description",
4811 "sambaGroupType", NULL);
4812 state->attrsonly = 0;
4813 state->pagedresults_cookie = NULL;
4814 state->entries = NULL;
4815 state->group_type = type;
4816 state->ldap2displayentry = ldapgroup2displayentry;
4818 if ((state->filter == NULL) || (state->attrs == NULL)) {
4819 DEBUG(0, ("talloc failed\n"));
4823 search->private_data = state;
4824 search->next_entry = ldapsam_search_next_entry;
4825 search->search_end = ldapsam_search_end;
4827 return ldapsam_search_firstpage(search);
4830 static bool ldapsam_search_groups(struct pdb_methods *methods,
4831 struct pdb_search *search)
4833 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4836 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4837 struct pdb_search *search,
4838 const struct dom_sid *sid)
4840 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4843 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4845 return PDB_CAP_STORE_RIDS;
4848 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4851 struct smbldap_state *smbldap_state = priv->smbldap_state;
4853 LDAPMessage *result = NULL;
4854 LDAPMessage *entry = NULL;
4855 LDAPMod **mods = NULL;
4859 uint32_t nextRid = 0;
4862 TALLOC_CTX *mem_ctx;
4864 mem_ctx = talloc_new(NULL);
4865 if (mem_ctx == NULL) {
4866 DEBUG(0, ("talloc_new failed\n"));
4867 return NT_STATUS_NO_MEMORY;
4870 status = smbldap_search_domain_info(smbldap_state, &result,
4871 get_global_sam_name(), False);
4872 if (!NT_STATUS_IS_OK(status)) {
4873 DEBUG(3, ("Could not get domain info: %s\n",
4874 nt_errstr(status)));
4878 talloc_autofree_ldapmsg(mem_ctx, result);
4880 entry = ldap_first_entry(priv2ld(priv), result);
4881 if (entry == NULL) {
4882 DEBUG(0, ("Could not get domain info entry\n"));
4883 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4887 /* Find the largest of the three attributes "sambaNextRid",
4888 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4889 concept of differentiating between user and group rids, and will
4890 use only "sambaNextRid" in the future. But for compatibility
4891 reasons I look if others have chosen different strategies -- VL */
4893 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4894 "sambaNextRid", mem_ctx);
4895 if (value != NULL) {
4896 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4897 nextRid = MAX(nextRid, tmp);
4900 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4901 "sambaNextUserRid", mem_ctx);
4902 if (value != NULL) {
4903 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4904 nextRid = MAX(nextRid, tmp);
4907 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4908 "sambaNextGroupRid", mem_ctx);
4909 if (value != NULL) {
4910 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4911 nextRid = MAX(nextRid, tmp);
4915 nextRid = BASE_RID-1;
4920 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4921 talloc_asprintf(mem_ctx, "%d", nextRid));
4922 talloc_autofree_ldapmod(mem_ctx, mods);
4924 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4925 status = NT_STATUS_NO_MEMORY;
4929 rc = smbldap_modify(smbldap_state, dn, mods);
4931 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4934 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4937 if (NT_STATUS_IS_OK(status)) {
4941 TALLOC_FREE(mem_ctx);
4945 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4949 for (i=0; i<10; i++) {
4950 NTSTATUS result = ldapsam_get_new_rid(
4951 (struct ldapsam_privates *)methods->private_data, rid);
4952 if (NT_STATUS_IS_OK(result)) {
4956 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4960 /* The ldap update failed (maybe a race condition), retry */
4963 /* Tried 10 times, fail. */
4964 return NT_STATUS_ACCESS_DENIED;
4967 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4969 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4970 return NT_STATUS_IS_OK(result) ? True : False;
4973 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4974 const struct dom_sid *sid,
4975 union unid_t *id, enum lsa_SidType *type)
4977 struct ldapsam_privates *priv =
4978 (struct ldapsam_privates *)methods->private_data;
4980 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4982 LDAPMessage *result = NULL;
4983 LDAPMessage *entry = NULL;
4988 TALLOC_CTX *mem_ctx;
4990 mem_ctx = talloc_new(NULL);
4991 if (mem_ctx == NULL) {
4992 DEBUG(0, ("talloc_new failed\n"));
4996 filter = talloc_asprintf(mem_ctx,
4998 "(|(objectClass=%s)(objectClass=%s)))",
4999 sid_string_talloc(mem_ctx, sid),
5000 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5001 if (filter == NULL) {
5002 DEBUG(5, ("talloc_asprintf failed\n"));
5006 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5008 if (rc != LDAP_SUCCESS) {
5011 talloc_autofree_ldapmsg(mem_ctx, result);
5013 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5014 DEBUG(10, ("Got %d entries, expected one\n",
5015 ldap_count_entries(priv2ld(priv), result)));
5019 entry = ldap_first_entry(priv2ld(priv), result);
5021 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5022 "sambaGroupType", mem_ctx);
5024 if (value != NULL) {
5025 const char *gid_str;
5028 gid_str = smbldap_talloc_single_attribute(
5029 priv2ld(priv), entry, "gidNumber", mem_ctx);
5030 if (gid_str == NULL) {
5031 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5032 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5037 id->gid = strtoul(gid_str, NULL, 10);
5038 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
5039 store_gid_sid_cache(sid, id->gid);
5040 idmap_cache_set_sid2gid(sid, id->gid);
5045 /* It must be a user */
5047 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5048 "uidNumber", mem_ctx);
5049 if (value == NULL) {
5050 DEBUG(1, ("Could not find uidNumber in %s\n",
5051 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5055 id->uid = strtoul(value, NULL, 10);
5056 *type = SID_NAME_USER;
5057 store_uid_sid_cache(sid, id->uid);
5058 idmap_cache_set_sid2uid(sid, id->uid);
5062 TALLOC_FREE(mem_ctx);
5067 * Find the SID for a uid.
5068 * This is shortcut is only used if ldapsam:trusted is set to true.
5070 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5071 struct dom_sid *sid)
5073 struct ldapsam_privates *priv =
5074 (struct ldapsam_privates *)methods->private_data;
5076 const char *attrs[] = { "sambaSID", NULL };
5077 LDAPMessage *result = NULL;
5078 LDAPMessage *entry = NULL;
5080 char *user_sid_string;
5081 struct dom_sid user_sid;
5083 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5085 filter = talloc_asprintf(tmp_ctx,
5088 "(objectClass=%s))",
5090 LDAP_OBJ_POSIXACCOUNT,
5091 LDAP_OBJ_SAMBASAMACCOUNT);
5092 if (filter == NULL) {
5093 DEBUG(3, ("talloc_asprintf failed\n"));
5097 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5098 if (rc != LDAP_SUCCESS) {
5101 talloc_autofree_ldapmsg(tmp_ctx, result);
5103 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5104 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5105 ldap_count_entries(priv2ld(priv), result),
5106 (unsigned int)uid));
5110 entry = ldap_first_entry(priv2ld(priv), result);
5112 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5113 "sambaSID", tmp_ctx);
5114 if (user_sid_string == NULL) {
5115 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5116 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5120 if (!string_to_sid(&user_sid, user_sid_string)) {
5121 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5126 sid_copy(sid, &user_sid);
5128 store_uid_sid_cache(sid, uid);
5129 idmap_cache_set_sid2uid(sid, uid);
5134 TALLOC_FREE(tmp_ctx);
5139 * Find the SID for a gid.
5140 * This is shortcut is only used if ldapsam:trusted is set to true.
5142 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5143 struct dom_sid *sid)
5145 struct ldapsam_privates *priv =
5146 (struct ldapsam_privates *)methods->private_data;
5148 const char *attrs[] = { "sambaSID", NULL };
5149 LDAPMessage *result = NULL;
5150 LDAPMessage *entry = NULL;
5152 char *group_sid_string;
5153 struct dom_sid group_sid;
5155 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5157 filter = talloc_asprintf(tmp_ctx,
5159 "(objectClass=%s))",
5162 if (filter == NULL) {
5163 DEBUG(3, ("talloc_asprintf failed\n"));
5167 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5168 if (rc != LDAP_SUCCESS) {
5171 talloc_autofree_ldapmsg(tmp_ctx, result);
5173 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5174 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5175 ldap_count_entries(priv2ld(priv), result),
5176 (unsigned int)gid));
5180 entry = ldap_first_entry(priv2ld(priv), result);
5182 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5183 "sambaSID", tmp_ctx);
5184 if (group_sid_string == NULL) {
5185 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5186 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5190 if (!string_to_sid(&group_sid, group_sid_string)) {
5191 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5196 sid_copy(sid, &group_sid);
5198 store_gid_sid_cache(sid, gid);
5199 idmap_cache_set_sid2gid(sid, gid);
5204 TALLOC_FREE(tmp_ctx);
5210 * The following functions are called only if
5211 * ldapsam:trusted and ldapsam:editposix are
5216 * ldapsam_create_user creates a new
5217 * posixAccount and sambaSamAccount object
5218 * in the ldap users subtree
5220 * The uid is allocated by winbindd.
5223 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5224 TALLOC_CTX *tmp_ctx, const char *name,
5225 uint32_t acb_info, uint32_t *rid)
5227 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5228 LDAPMessage *entry = NULL;
5229 LDAPMessage *result = NULL;
5230 uint32_t num_result;
5231 bool is_machine = False;
5232 bool add_posix = False;
5233 LDAPMod **mods = NULL;
5241 const char *dn = NULL;
5242 struct dom_sid group_sid;
5243 struct dom_sid user_sid;
5249 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5250 acb_info & ACB_WSTRUST ||
5251 acb_info & ACB_SVRTRUST ||
5252 acb_info & ACB_DOMTRUST) {
5256 username = escape_ldap_string(talloc_tos(), name);
5257 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5258 username, LDAP_OBJ_POSIXACCOUNT);
5259 TALLOC_FREE(username);
5261 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5262 if (rc != LDAP_SUCCESS) {
5263 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5264 return NT_STATUS_ACCESS_DENIED;
5266 talloc_autofree_ldapmsg(tmp_ctx, result);
5268 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5270 if (num_result > 1) {
5271 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5272 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5275 if (num_result == 1) {
5277 /* check if it is just a posix account.
5278 * or if there is a sid attached to this entry
5281 entry = ldap_first_entry(priv2ld(ldap_state), result);
5283 return NT_STATUS_UNSUCCESSFUL;
5286 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5288 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5289 return NT_STATUS_USER_EXISTS;
5292 /* it is just a posix account, retrieve the dn for later use */
5293 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5295 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5296 return NT_STATUS_NO_MEMORY;
5300 if (num_result == 0) {
5304 /* Create the basic samu structure and generate the mods for the ldap commit */
5305 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5306 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5310 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5312 user = samu_new(tmp_ctx);
5314 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5315 return NT_STATUS_NO_MEMORY;
5318 if (!pdb_set_username(user, name, PDB_SET)) {
5319 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5320 return NT_STATUS_UNSUCCESSFUL;
5322 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5323 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5324 return NT_STATUS_UNSUCCESSFUL;
5327 if (acb_info & ACB_NORMAL) {
5328 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5329 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5330 return NT_STATUS_UNSUCCESSFUL;
5333 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5334 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5335 return NT_STATUS_UNSUCCESSFUL;
5339 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5340 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5341 return NT_STATUS_UNSUCCESSFUL;
5345 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5346 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5347 return NT_STATUS_UNSUCCESSFUL;
5350 if (!init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed)) {
5351 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5352 return NT_STATUS_UNSUCCESSFUL;
5355 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5356 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5358 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5363 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5365 /* retrieve the Domain Users group gid */
5366 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5367 !sid_to_gid(&group_sid, &gid)) {
5368 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5369 return NT_STATUS_INVALID_PRIMARY_GROUP;
5372 /* lets allocate a new userid for this user */
5373 if (!winbind_allocate_uid(&uid)) {
5374 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5375 return NT_STATUS_UNSUCCESSFUL;
5380 /* TODO: choose a more appropriate default for machines */
5381 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5382 shell = talloc_strdup(tmp_ctx, "/bin/false");
5384 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5385 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5387 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5388 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5390 escape_name = escape_rdn_val_string_alloc(name);
5392 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5393 return NT_STATUS_NO_MEMORY;
5397 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5399 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5402 SAFE_FREE(escape_name);
5404 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5405 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5406 return NT_STATUS_NO_MEMORY;
5409 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5410 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5411 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5412 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5413 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5414 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5415 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5418 talloc_autofree_ldapmod(tmp_ctx, mods);
5421 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5423 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5426 if (rc != LDAP_SUCCESS) {
5427 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5428 return NT_STATUS_UNSUCCESSFUL;
5431 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5433 flush_pwnam_cache();
5435 return NT_STATUS_OK;
5438 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5440 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5441 LDAPMessage *result = NULL;
5442 LDAPMessage *entry = NULL;
5448 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5450 filter = talloc_asprintf(tmp_ctx,
5453 "(objectClass=%s))",
5454 pdb_get_username(sam_acct),
5455 LDAP_OBJ_POSIXACCOUNT,
5456 LDAP_OBJ_SAMBASAMACCOUNT);
5457 if (filter == NULL) {
5458 return NT_STATUS_NO_MEMORY;
5461 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5462 if (rc != LDAP_SUCCESS) {
5463 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5464 return NT_STATUS_UNSUCCESSFUL;
5466 talloc_autofree_ldapmsg(tmp_ctx, result);
5468 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5470 if (num_result == 0) {
5471 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5472 return NT_STATUS_NO_SUCH_USER;
5475 if (num_result > 1) {
5476 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5477 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5480 entry = ldap_first_entry(priv2ld(ldap_state), result);
5482 return NT_STATUS_UNSUCCESSFUL;
5485 /* it is just a posix account, retrieve the dn for later use */
5486 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5488 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5489 return NT_STATUS_NO_MEMORY;
5492 /* try to remove memberships first */
5495 struct dom_sid *sids = NULL;
5497 uint32_t num_groups = 0;
5499 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5501 status = ldapsam_enum_group_memberships(my_methods,
5507 if (!NT_STATUS_IS_OK(status)) {
5511 for (i=0; i < num_groups; i++) {
5515 sid_peek_rid(&sids[i], &group_rid);
5517 ldapsam_del_groupmem(my_methods,
5526 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5527 if (rc != LDAP_SUCCESS) {
5528 return NT_STATUS_UNSUCCESSFUL;
5531 flush_pwnam_cache();
5533 return NT_STATUS_OK;
5537 * ldapsam_create_group creates a new
5538 * posixGroup and sambaGroupMapping object
5539 * in the ldap groups subtree
5541 * The gid is allocated by winbindd.
5544 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5545 TALLOC_CTX *tmp_ctx,
5549 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5551 LDAPMessage *entry = NULL;
5552 LDAPMessage *result = NULL;
5553 uint32_t num_result;
5554 bool is_new_entry = False;
5555 LDAPMod **mods = NULL;
5561 const char *dn = NULL;
5562 struct dom_sid group_sid;
5566 groupname = escape_ldap_string(talloc_tos(), name);
5567 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5568 groupname, LDAP_OBJ_POSIXGROUP);
5569 TALLOC_FREE(groupname);
5571 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5572 if (rc != LDAP_SUCCESS) {
5573 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5574 return NT_STATUS_UNSUCCESSFUL;
5576 talloc_autofree_ldapmsg(tmp_ctx, result);
5578 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5580 if (num_result > 1) {
5581 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5582 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5585 if (num_result == 1) {
5587 /* check if it is just a posix group.
5588 * or if there is a sid attached to this entry
5591 entry = ldap_first_entry(priv2ld(ldap_state), result);
5593 return NT_STATUS_UNSUCCESSFUL;
5596 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5598 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5599 return NT_STATUS_GROUP_EXISTS;
5602 /* it is just a posix group, retrieve the gid and the dn for later use */
5603 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5605 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5606 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5609 gid = strtoul(tmp, NULL, 10);
5611 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5613 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5614 return NT_STATUS_NO_MEMORY;
5618 if (num_result == 0) {
5619 is_new_entry = true;
5622 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5623 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5627 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5629 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5631 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5633 if (!groupsidstr || !grouptype) {
5634 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5635 return NT_STATUS_NO_MEMORY;
5638 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5639 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5640 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5641 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5646 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5648 /* lets allocate a new groupid for this group */
5649 if (!winbind_allocate_gid(&gid)) {
5650 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5651 return NT_STATUS_UNSUCCESSFUL;
5654 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5656 escape_name = escape_rdn_val_string_alloc(name);
5658 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5659 return NT_STATUS_NO_MEMORY;
5662 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5664 SAFE_FREE(escape_name);
5666 if (!gidstr || !dn) {
5667 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5668 return NT_STATUS_NO_MEMORY;
5671 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5672 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5673 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5676 talloc_autofree_ldapmod(tmp_ctx, mods);
5679 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5681 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5682 /* This call may fail with rfc2307bis schema */
5683 /* Retry adding a structural class */
5684 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5685 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5689 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5692 if (rc != LDAP_SUCCESS) {
5693 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5694 return NT_STATUS_UNSUCCESSFUL;
5697 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5699 return NT_STATUS_OK;
5702 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5704 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5705 LDAPMessage *result = NULL;
5706 LDAPMessage *entry = NULL;
5711 struct dom_sid group_sid;
5714 /* get the group sid */
5715 sid_compose(&group_sid, get_global_sam_sid(), rid);
5717 filter = talloc_asprintf(tmp_ctx,
5720 "(objectClass=%s))",
5721 sid_string_talloc(tmp_ctx, &group_sid),
5722 LDAP_OBJ_POSIXGROUP,
5724 if (filter == NULL) {
5725 return NT_STATUS_NO_MEMORY;
5728 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5729 if (rc != LDAP_SUCCESS) {
5730 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5731 return NT_STATUS_UNSUCCESSFUL;
5733 talloc_autofree_ldapmsg(tmp_ctx, result);
5735 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5737 if (num_result == 0) {
5738 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5739 return NT_STATUS_NO_SUCH_GROUP;
5742 if (num_result > 1) {
5743 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5744 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5747 entry = ldap_first_entry(priv2ld(ldap_state), result);
5749 return NT_STATUS_UNSUCCESSFUL;
5752 /* here it is, retrieve the dn for later use */
5753 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5755 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5756 return NT_STATUS_NO_MEMORY;
5759 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5761 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5762 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5765 /* check no user have this group marked as primary group */
5766 filter = talloc_asprintf(tmp_ctx,
5769 "(objectClass=%s))",
5771 LDAP_OBJ_POSIXACCOUNT,
5772 LDAP_OBJ_SAMBASAMACCOUNT);
5774 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5775 if (rc != LDAP_SUCCESS) {
5776 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5777 return NT_STATUS_UNSUCCESSFUL;
5779 talloc_autofree_ldapmsg(tmp_ctx, result);
5781 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5783 if (num_result != 0) {
5784 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5785 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5788 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5789 if (rc != LDAP_SUCCESS) {
5790 return NT_STATUS_UNSUCCESSFUL;
5793 return NT_STATUS_OK;
5796 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5797 TALLOC_CTX *tmp_ctx,
5799 uint32_t member_rid,
5802 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5803 LDAPMessage *entry = NULL;
5804 LDAPMessage *result = NULL;
5805 uint32_t num_result;
5806 LDAPMod **mods = NULL;
5809 const char *dn = NULL;
5810 struct dom_sid group_sid;
5811 struct dom_sid member_sid;
5816 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5818 case LDAP_MOD_DELETE:
5819 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5822 return NT_STATUS_UNSUCCESSFUL;
5825 /* get member sid */
5826 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5828 /* get the group sid */
5829 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5831 filter = talloc_asprintf(tmp_ctx,
5834 "(objectClass=%s))",
5835 sid_string_talloc(tmp_ctx, &member_sid),
5836 LDAP_OBJ_POSIXACCOUNT,
5837 LDAP_OBJ_SAMBASAMACCOUNT);
5838 if (filter == NULL) {
5839 return NT_STATUS_NO_MEMORY;
5842 /* get the member uid */
5843 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5844 if (rc != LDAP_SUCCESS) {
5845 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5846 return NT_STATUS_UNSUCCESSFUL;
5848 talloc_autofree_ldapmsg(tmp_ctx, result);
5850 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5852 if (num_result == 0) {
5853 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5854 return NT_STATUS_NO_SUCH_MEMBER;
5857 if (num_result > 1) {
5858 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5859 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5862 entry = ldap_first_entry(priv2ld(ldap_state), result);
5864 return NT_STATUS_UNSUCCESSFUL;
5867 if (modop == LDAP_MOD_DELETE) {
5868 /* check if we are trying to remove the member from his primary group */
5870 gid_t user_gid, group_gid;
5872 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5874 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5875 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5878 user_gid = strtoul(gidstr, NULL, 10);
5880 if (!sid_to_gid(&group_sid, &group_gid)) {
5881 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5882 return NT_STATUS_UNSUCCESSFUL;
5885 if (user_gid == group_gid) {
5886 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5887 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5891 /* here it is, retrieve the uid for later use */
5892 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5894 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5895 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5898 filter = talloc_asprintf(tmp_ctx,
5901 "(objectClass=%s))",
5902 sid_string_talloc(tmp_ctx, &group_sid),
5903 LDAP_OBJ_POSIXGROUP,
5907 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5908 if (rc != LDAP_SUCCESS) {
5909 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5910 return NT_STATUS_UNSUCCESSFUL;
5912 talloc_autofree_ldapmsg(tmp_ctx, result);
5914 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5916 if (num_result == 0) {
5917 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5918 return NT_STATUS_NO_SUCH_GROUP;
5921 if (num_result > 1) {
5922 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5923 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5926 entry = ldap_first_entry(priv2ld(ldap_state), result);
5928 return NT_STATUS_UNSUCCESSFUL;
5931 /* here it is, retrieve the dn for later use */
5932 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5934 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5935 return NT_STATUS_NO_MEMORY;
5938 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5940 talloc_autofree_ldapmod(tmp_ctx, mods);
5942 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5943 if (rc != LDAP_SUCCESS) {
5944 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5945 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5946 return NT_STATUS_MEMBER_IN_GROUP;
5948 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5949 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5950 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5952 return NT_STATUS_UNSUCCESSFUL;
5955 return NT_STATUS_OK;
5958 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5959 TALLOC_CTX *tmp_ctx,
5961 uint32_t member_rid)
5963 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5965 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5966 TALLOC_CTX *tmp_ctx,
5968 uint32_t member_rid)
5970 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5973 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5974 TALLOC_CTX *mem_ctx,
5975 struct samu *sampass)
5977 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5978 LDAPMessage *entry = NULL;
5979 LDAPMessage *result = NULL;
5980 uint32_t num_result;
5981 LDAPMod **mods = NULL;
5983 char *escape_username;
5985 const char *dn = NULL;
5989 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5991 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5992 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5993 return NT_STATUS_UNSUCCESSFUL;
5995 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
5997 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5998 return NT_STATUS_NO_MEMORY;
6001 escape_username = escape_ldap_string(talloc_tos(),
6002 pdb_get_username(sampass));
6003 if (escape_username== NULL) {
6004 return NT_STATUS_NO_MEMORY;
6007 filter = talloc_asprintf(mem_ctx,
6010 "(objectClass=%s))",
6012 LDAP_OBJ_POSIXACCOUNT,
6013 LDAP_OBJ_SAMBASAMACCOUNT);
6015 TALLOC_FREE(escape_username);
6017 if (filter == NULL) {
6018 return NT_STATUS_NO_MEMORY;
6021 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6022 if (rc != LDAP_SUCCESS) {
6023 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6024 return NT_STATUS_UNSUCCESSFUL;
6026 talloc_autofree_ldapmsg(mem_ctx, result);
6028 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6030 if (num_result == 0) {
6031 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6032 return NT_STATUS_NO_SUCH_USER;
6035 if (num_result > 1) {
6036 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6037 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6040 entry = ldap_first_entry(priv2ld(ldap_state), result);
6042 return NT_STATUS_UNSUCCESSFUL;
6045 /* retrieve the dn for later use */
6046 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6048 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6049 return NT_STATUS_NO_MEMORY;
6052 /* remove the old one, and add the new one, this way we do not risk races */
6053 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6056 return NT_STATUS_OK;
6059 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6061 if (rc != LDAP_SUCCESS) {
6062 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6063 pdb_get_username(sampass), gidstr));
6064 return NT_STATUS_UNSUCCESSFUL;
6067 flush_pwnam_cache();
6069 return NT_STATUS_OK;
6073 /**********************************************************************
6074 trusted domains functions
6075 *********************************************************************/
6077 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6080 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6081 ldap_state->domain_dn);
6084 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6085 TALLOC_CTX *mem_ctx,
6086 const char *domain, LDAPMessage **entry)
6090 int scope = LDAP_SCOPE_SUBTREE;
6091 const char **attrs = NULL; /* NULL: get all attrs */
6092 int attrsonly = 0; /* 0: return values too */
6093 LDAPMessage *result = NULL;
6095 uint32_t num_result;
6097 filter = talloc_asprintf(talloc_tos(),
6098 "(&(objectClass=%s)(sambaDomainName=%s))",
6099 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6101 trusted_dn = trusteddom_dn(ldap_state, domain);
6102 if (trusted_dn == NULL) {
6105 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6106 filter, attrs, attrsonly, &result);
6108 if (result != NULL) {
6109 talloc_autofree_ldapmsg(mem_ctx, result);
6112 if (rc == LDAP_NO_SUCH_OBJECT) {
6117 if (rc != LDAP_SUCCESS) {
6121 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6123 if (num_result > 1) {
6124 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6125 "%s object for domain '%s'?!\n",
6126 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6130 if (num_result == 0) {
6131 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6132 "%s object for domain %s.\n",
6133 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6136 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6142 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6145 struct dom_sid *sid,
6146 time_t *pass_last_set_time)
6148 struct ldapsam_privates *ldap_state =
6149 (struct ldapsam_privates *)methods->private_data;
6150 LDAPMessage *entry = NULL;
6152 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6154 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6163 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6164 entry, "sambaClearTextPassword", talloc_tos());
6165 if (pwd_str == NULL) {
6168 /* trusteddom_pw routines do not use talloc yet... */
6169 *pwd = SMB_STRDUP(pwd_str);
6175 /* last change time */
6176 if (pass_last_set_time != NULL) {
6178 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6179 entry, "sambaPwdLastSet", talloc_tos());
6180 if (time_str == NULL) {
6183 *pass_last_set_time = (time_t)atol(time_str);
6189 struct dom_sid dom_sid;
6190 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6193 if (sid_str == NULL) {
6196 if (!string_to_sid(&dom_sid, sid_str)) {
6199 sid_copy(sid, &dom_sid);
6205 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6208 const struct dom_sid *sid)
6210 struct ldapsam_privates *ldap_state =
6211 (struct ldapsam_privates *)methods->private_data;
6212 LDAPMessage *entry = NULL;
6213 LDAPMod **mods = NULL;
6214 char *prev_pwd = NULL;
6215 char *trusted_dn = NULL;
6218 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6221 * get the current entry (if there is one) in order to put the
6222 * current password into the previous password attribute
6224 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6229 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6230 LDAP_OBJ_TRUSTDOM_PASSWORD);
6231 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6233 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6234 sid_string_tos(sid));
6235 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6236 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6237 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6238 "sambaClearTextPassword", pwd);
6240 if (entry != NULL) {
6241 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6242 entry, "sambaClearTextPassword", talloc_tos());
6243 if (prev_pwd != NULL) {
6244 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6245 "sambaPreviousClearTextPassword",
6250 talloc_autofree_ldapmod(talloc_tos(), mods);
6252 trusted_dn = trusteddom_dn(ldap_state, domain);
6253 if (trusted_dn == NULL) {
6256 if (entry == NULL) {
6257 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6259 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6262 if (rc != LDAP_SUCCESS) {
6263 DEBUG(1, ("error writing trusted domain password!\n"));
6270 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6274 struct ldapsam_privates *ldap_state =
6275 (struct ldapsam_privates *)methods->private_data;
6276 LDAPMessage *entry = NULL;
6277 const char *trusted_dn;
6279 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6283 if (entry == NULL) {
6284 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6289 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6291 if (trusted_dn == NULL) {
6292 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6296 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6297 if (rc != LDAP_SUCCESS) {
6304 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6305 TALLOC_CTX *mem_ctx,
6306 uint32_t *num_domains,
6307 struct trustdom_info ***domains)
6310 struct ldapsam_privates *ldap_state =
6311 (struct ldapsam_privates *)methods->private_data;
6313 int scope = LDAP_SCOPE_SUBTREE;
6314 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6315 int attrsonly = 0; /* 0: return values too */
6316 LDAPMessage *result = NULL;
6317 LDAPMessage *entry = NULL;
6319 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6320 LDAP_OBJ_TRUSTDOM_PASSWORD);
6322 rc = smbldap_search(ldap_state->smbldap_state,
6323 ldap_state->domain_dn,
6330 if (result != NULL) {
6331 talloc_autofree_ldapmsg(mem_ctx, result);
6334 if (rc != LDAP_SUCCESS) {
6335 return NT_STATUS_UNSUCCESSFUL;
6339 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6340 DEBUG(1, ("talloc failed\n"));
6341 return NT_STATUS_NO_MEMORY;
6344 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6346 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6348 char *dom_name, *dom_sid_str;
6349 struct trustdom_info *dom_info;
6351 dom_info = talloc(*domains, struct trustdom_info);
6352 if (dom_info == NULL) {
6353 DEBUG(1, ("talloc failed\n"));
6354 return NT_STATUS_NO_MEMORY;
6357 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6361 if (dom_name == NULL) {
6362 DEBUG(1, ("talloc failed\n"));
6363 return NT_STATUS_NO_MEMORY;
6365 dom_info->name = dom_name;
6367 dom_sid_str = smbldap_talloc_single_attribute(
6368 priv2ld(ldap_state), entry, "sambaSID",
6370 if (dom_sid_str == NULL) {
6371 DEBUG(1, ("talloc failed\n"));
6372 return NT_STATUS_NO_MEMORY;
6374 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6375 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6377 return NT_STATUS_UNSUCCESSFUL;
6380 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6381 domains, num_domains);
6383 if (*domains == NULL) {
6384 DEBUG(1, ("talloc failed\n"));
6385 return NT_STATUS_NO_MEMORY;
6389 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6390 return NT_STATUS_OK;
6394 /**********************************************************************
6396 *********************************************************************/
6398 static void free_private_data(void **vp)
6400 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6402 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6404 if ((*ldap_state)->result != NULL) {
6405 ldap_msgfree((*ldap_state)->result);
6406 (*ldap_state)->result = NULL;
6408 if ((*ldap_state)->domain_dn != NULL) {
6409 SAFE_FREE((*ldap_state)->domain_dn);
6414 /* No need to free any further, as it is talloc()ed */
6417 /*********************************************************************
6418 Intitalise the parts of the pdb_methods structure that are common to
6420 *********************************************************************/
6422 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6425 struct ldapsam_privates *ldap_state;
6427 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6431 (*pdb_method)->name = "ldapsam";
6433 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6434 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6435 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6436 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6437 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6438 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6440 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6441 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6442 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6443 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6444 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6445 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6446 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6448 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6449 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6451 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6453 (*pdb_method)->capabilities = ldapsam_capabilities;
6454 (*pdb_method)->new_rid = ldapsam_new_rid;
6456 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6457 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6458 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6459 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6461 /* TODO: Setup private data and free */
6463 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6464 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6465 return NT_STATUS_NO_MEMORY;
6468 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6469 location, &ldap_state->smbldap_state);
6471 if ( !NT_STATUS_IS_OK(nt_status) ) {
6475 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6476 return NT_STATUS_NO_MEMORY;
6479 (*pdb_method)->private_data = ldap_state;
6481 (*pdb_method)->free_private_data = free_private_data;
6483 return NT_STATUS_OK;
6486 /**********************************************************************
6487 Initialise the 'compat' mode for pdb_ldap
6488 *********************************************************************/
6490 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6493 struct ldapsam_privates *ldap_state;
6494 char *uri = talloc_strdup( NULL, location );
6496 trim_char( uri, '\"', '\"' );
6497 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6501 if ( !NT_STATUS_IS_OK(nt_status) ) {
6505 (*pdb_method)->name = "ldapsam_compat";
6507 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6508 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6510 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6512 return NT_STATUS_OK;
6515 /**********************************************************************
6516 Initialise the normal mode for pdb_ldap
6517 *********************************************************************/
6519 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6522 struct ldapsam_privates *ldap_state = NULL;
6523 uint32_t alg_rid_base;
6524 char *alg_rid_base_string = NULL;
6525 LDAPMessage *result = NULL;
6526 LDAPMessage *entry = NULL;
6527 struct dom_sid ldap_domain_sid;
6528 struct dom_sid secrets_domain_sid;
6529 char *domain_sid_string = NULL;
6531 char *uri = talloc_strdup( NULL, location );
6533 trim_char( uri, '\"', '\"' );
6534 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6538 if (!NT_STATUS_IS_OK(nt_status)) {
6542 (*pdb_method)->name = "ldapsam";
6544 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6545 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6546 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6547 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6548 (*pdb_method)->search_users = ldapsam_search_users;
6549 (*pdb_method)->search_groups = ldapsam_search_groups;
6550 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6552 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6553 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6554 (*pdb_method)->enum_group_memberships =
6555 ldapsam_enum_group_memberships;
6556 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6557 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6558 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6559 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6561 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6562 (*pdb_method)->create_user = ldapsam_create_user;
6563 (*pdb_method)->delete_user = ldapsam_delete_user;
6564 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6565 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6566 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6567 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6568 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6572 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6573 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6575 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6577 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6579 ldap_state->domain_name, True);
6581 if ( !NT_STATUS_IS_OK(nt_status) ) {
6582 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6583 "info, nor add one to the domain. "
6584 "We cannot work reliably without it.\n"));
6585 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6588 /* Given that the above might fail, everything below this must be
6591 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6594 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6596 ldap_msgfree(result);
6597 return NT_STATUS_UNSUCCESSFUL;
6600 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6602 ldap_msgfree(result);
6603 return NT_STATUS_UNSUCCESSFUL;
6606 ldap_state->domain_dn = smb_xstrdup(dn);
6609 domain_sid_string = smbldap_talloc_single_attribute(
6610 ldap_state->smbldap_state->ldap_struct,
6612 get_userattr_key2string(ldap_state->schema_ver,
6613 LDAP_ATTR_USER_SID),
6616 if (domain_sid_string) {
6618 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6619 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6620 "read as a valid SID\n", domain_sid_string));
6621 ldap_msgfree(result);
6622 TALLOC_FREE(domain_sid_string);
6623 return NT_STATUS_INVALID_PARAMETER;
6625 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6626 &secrets_domain_sid);
6627 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6628 &ldap_domain_sid)) {
6629 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6630 "%s based on pdb_ldap results %s -> %s\n",
6631 ldap_state->domain_name,
6632 sid_string_dbg(&secrets_domain_sid),
6633 sid_string_dbg(&ldap_domain_sid)));
6635 /* reset secrets.tdb sid */
6636 secrets_store_domain_sid(ldap_state->domain_name,
6638 DEBUG(1, ("New global sam SID: %s\n",
6639 sid_string_dbg(get_global_sam_sid())));
6641 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6642 TALLOC_FREE(domain_sid_string);
6645 alg_rid_base_string = smbldap_talloc_single_attribute(
6646 ldap_state->smbldap_state->ldap_struct,
6648 get_attr_key2string( dominfo_attr_list,
6649 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6651 if (alg_rid_base_string) {
6652 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6653 if (alg_rid_base != algorithmic_rid_base()) {
6654 DEBUG(0, ("The value of 'algorithmic RID base' has "
6655 "changed since the LDAP\n"
6656 "database was initialised. Aborting. \n"));
6657 ldap_msgfree(result);
6658 TALLOC_FREE(alg_rid_base_string);
6659 return NT_STATUS_UNSUCCESSFUL;
6661 TALLOC_FREE(alg_rid_base_string);
6663 ldap_msgfree(result);
6665 return NT_STATUS_OK;
6668 NTSTATUS pdb_ldap_init(void)
6671 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6674 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6677 /* Let pdb_nds register backends */
6682 return NT_STATUS_OK;