2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
48 #include "../libcli/auth/libcli_auth.h"
50 #include "idmap_cache.h"
51 #include "../libcli/security/security.h"
52 #include "../lib/util/util_pw.h"
53 #include "lib/winbind_util.h"
54 #include "librpc/gen_ndr/idmap.h"
55 #include "lib/param/loadparm.h"
56 #include "lib/util_sid_passdb.h"
59 #define DBGC_CLASS DBGC_PASSDB
66 #include "passdb/pdb_ldap.h"
67 #include "passdb/pdb_nds.h"
68 #include "passdb/pdb_ldap_util.h"
69 #include "passdb/pdb_ldap_schema.h"
71 /**********************************************************************
72 Simple helper function to make stuff better readable
73 **********************************************************************/
75 LDAP *priv2ld(struct ldapsam_privates *priv)
77 return priv->smbldap_state->ldap_struct;
80 /**********************************************************************
81 Get the attribute name given a user schame version.
82 **********************************************************************/
84 static const char* get_userattr_key2string( int schema_ver, int key )
86 switch ( schema_ver ) {
87 case SCHEMAVER_SAMBASAMACCOUNT:
88 return get_attr_key2string( attrib_map_v30, key );
91 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
97 /**********************************************************************
98 Return the list of attribute names given a user schema version.
99 **********************************************************************/
101 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
103 switch ( schema_ver ) {
104 case SCHEMAVER_SAMBASAMACCOUNT:
105 return get_attr_list( mem_ctx, attrib_map_v30 );
107 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
114 /**************************************************************************
115 Return the list of attribute names to delete given a user schema version.
116 **************************************************************************/
118 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
121 switch ( schema_ver ) {
122 case SCHEMAVER_SAMBASAMACCOUNT:
123 return get_attr_list( mem_ctx,
124 attrib_map_to_delete_v30 );
126 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
134 /*******************************************************************
135 Generate the LDAP search filter for the objectclass based on the
136 version of the schema we are using.
137 ******************************************************************/
139 static const char* get_objclass_filter( int schema_ver )
141 fstring objclass_filter;
144 switch( schema_ver ) {
145 case SCHEMAVER_SAMBASAMACCOUNT:
146 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
149 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
150 objclass_filter[0] = '\0';
154 result = talloc_strdup(talloc_tos(), objclass_filter);
155 SMB_ASSERT(result != NULL);
159 /*****************************************************************
160 Scan a sequence number off OpenLDAP's syncrepl contextCSN
161 ******************************************************************/
163 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
165 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
166 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
167 LDAPMessage *msg = NULL;
168 LDAPMessage *entry = NULL;
170 char **values = NULL;
171 int rc, num_result, num_values, rid;
177 /* Unfortunatly there is no proper way to detect syncrepl-support in
178 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
179 * but do not show up in the root-DSE yet. Neither we can query the
180 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
181 * objectclass. Currently we require lp_ldap_suffix() to show up as
182 * namingContext. - Guenther
185 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
190 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
194 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix(talloc_tos()))) {
195 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
196 "as top-level namingContext\n", lp_ldap_suffix(talloc_tos())));
200 mem_ctx = talloc_init("ldapsam_get_seq_num");
203 return NT_STATUS_NO_MEMORY;
205 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
206 ntstatus = NT_STATUS_NO_MEMORY;
210 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
211 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
214 /* consumer syncreplCookie: */
215 /* csn=20050126161620Z#0000001#00#00000 */
216 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
218 suffix = talloc_asprintf(mem_ctx,
219 "cn=syncrepl%d,%s", rid, lp_ldap_suffix(talloc_tos()));
221 ntstatus = NT_STATUS_NO_MEMORY;
226 /* provider contextCSN */
227 /* 20050126161620Z#000009#00#000000 */
228 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
230 suffix = talloc_asprintf(mem_ctx,
231 "cn=ldapsync,%s", lp_ldap_suffix(talloc_tos()));
234 ntstatus = NT_STATUS_NO_MEMORY;
239 rc = smbldap_search(ldap_state->smbldap_state, suffix,
240 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
242 if (rc != LDAP_SUCCESS) {
246 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
247 if (num_result != 1) {
248 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
252 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
254 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
258 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
259 if (values == NULL) {
260 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
264 num_values = ldap_count_values(values);
265 if (num_values == 0) {
266 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
271 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
272 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
277 if (!strncmp(p, "csn=", strlen("csn=")))
280 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
282 *seq_num = generalized_to_unix_time(p);
284 /* very basic sanity check */
286 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
291 ntstatus = NT_STATUS_OK;
295 ldap_value_free(values);
299 talloc_destroy(mem_ctx);
304 /*******************************************************************
305 Run the search by name.
306 ******************************************************************/
308 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
310 LDAPMessage ** result,
314 char *escape_user = escape_ldap_string(talloc_tos(), user);
318 return LDAP_NO_MEMORY;
322 * in the filter expression, replace %u with the real name
323 * so in ldap filter, %u MUST exist :-)
325 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
326 get_objclass_filter(ldap_state->schema_ver));
328 TALLOC_FREE(escape_user);
329 return LDAP_NO_MEMORY;
332 * have to use this here because $ is filtered out
336 filter = talloc_all_string_sub(talloc_tos(),
337 filter, "%u", escape_user);
338 TALLOC_FREE(escape_user);
340 return LDAP_NO_MEMORY;
343 ret = smbldap_search_suffix(ldap_state->smbldap_state,
344 filter, attr, result);
349 /*******************************************************************
350 Run the search by SID.
351 ******************************************************************/
353 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
354 const struct dom_sid *sid, LDAPMessage ** result,
361 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
362 get_userattr_key2string(ldap_state->schema_ver,
364 sid_to_fstring(sid_string, sid),
365 get_objclass_filter(ldap_state->schema_ver));
367 return LDAP_NO_MEMORY;
370 rc = smbldap_search_suffix(ldap_state->smbldap_state,
371 filter, attr, result);
377 /*******************************************************************
378 Delete complete object or objectclass and attrs from
379 object found in search_result depending on lp_ldap_delete_dn
380 ******************************************************************/
382 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
385 const char *objectclass,
388 LDAPMod **mods = NULL;
391 BerElement *ptr = NULL;
393 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
395 return LDAP_NO_MEMORY;
398 if (lp_ldap_delete_dn()) {
399 return smbldap_delete(priv->smbldap_state, dn);
402 /* Ok, delete only the SAM attributes */
404 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
406 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
409 /* We are only allowed to delete the attributes that
412 for (attrib = attrs; *attrib != NULL; attrib++) {
413 if (strequal(*attrib, name)) {
414 DEBUG(10, ("ldapsam_delete_entry: deleting "
415 "attribute %s\n", name));
416 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
427 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
428 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
430 return smbldap_modify(priv->smbldap_state, dn, mods);
433 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
438 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
439 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
445 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
446 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
456 /**********************************************************************
457 Initialize struct samu from an LDAP query.
458 (Based on init_sam_from_buffer in pdb_tdb.c)
459 *********************************************************************/
461 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
462 struct samu * sampass,
469 pass_can_change_time,
472 char *username = NULL,
478 *logon_script = NULL,
479 *profile_path = NULL,
481 *workstations = NULL,
484 uint8_t smblmpwd[LM_HASH_LEN],
485 smbntpwd[NT_HASH_LEN];
486 bool use_samba_attrs = True;
487 uint32_t acct_ctrl = 0;
489 uint16_t bad_password_count = 0,
492 uint8_t hours[MAX_HOURS_LEN];
494 struct login_cache cache_entry;
496 bool expand_explicit = lp_passdb_expand_explicit();
498 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
503 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
504 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
508 if (priv2ld(ldap_state) == NULL) {
509 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
510 "ldap_struct is NULL!\n"));
514 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
518 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
523 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
525 nt_username = talloc_strdup(ctx, username);
530 domain = talloc_strdup(ctx, ldap_state->domain_name);
535 pdb_set_username(sampass, username, PDB_SET);
537 pdb_set_domain(sampass, domain, PDB_DEFAULT);
538 pdb_set_nt_username(sampass, nt_username, PDB_SET);
540 /* deal with different attributes between the schema first */
542 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
543 if ((temp = smbldap_talloc_single_attribute(
544 ldap_state->smbldap_state->ldap_struct,
546 get_userattr_key2string(ldap_state->schema_ver,
549 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
552 if ((temp = smbldap_talloc_single_attribute(
553 ldap_state->smbldap_state->ldap_struct,
555 get_userattr_key2string(ldap_state->schema_ver,
558 user_rid = (uint32_t)atol(temp);
559 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
563 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
564 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
565 get_userattr_key2string(ldap_state->schema_ver,
567 get_userattr_key2string(ldap_state->schema_ver,
573 temp = smbldap_talloc_single_attribute(
574 ldap_state->smbldap_state->ldap_struct,
576 get_userattr_key2string(ldap_state->schema_ver,
577 LDAP_ATTR_PWD_LAST_SET),
580 pass_last_set_time = (time_t) atol(temp);
581 pdb_set_pass_last_set_time(sampass,
582 pass_last_set_time, PDB_SET);
585 temp = smbldap_talloc_single_attribute(
586 ldap_state->smbldap_state->ldap_struct,
588 get_userattr_key2string(ldap_state->schema_ver,
589 LDAP_ATTR_LOGON_TIME),
592 logon_time = (time_t) atol(temp);
593 pdb_set_logon_time(sampass, logon_time, PDB_SET);
596 temp = smbldap_talloc_single_attribute(
597 ldap_state->smbldap_state->ldap_struct,
599 get_userattr_key2string(ldap_state->schema_ver,
600 LDAP_ATTR_LOGOFF_TIME),
603 logoff_time = (time_t) atol(temp);
604 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
607 temp = smbldap_talloc_single_attribute(
608 ldap_state->smbldap_state->ldap_struct,
610 get_userattr_key2string(ldap_state->schema_ver,
611 LDAP_ATTR_KICKOFF_TIME),
614 kickoff_time = (time_t) atol(temp);
615 pdb_set_kickoff_time(sampass, kickoff_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_PWD_CAN_CHANGE),
625 pass_can_change_time = (time_t) atol(temp);
626 pdb_set_pass_can_change_time(sampass,
627 pass_can_change_time, PDB_SET);
630 /* recommend that 'gecos' and 'displayName' should refer to the same
631 * attribute OID. userFullName depreciated, only used by Samba
632 * primary rules of LDAP: don't make a new attribute when one is already defined
633 * that fits your needs; using cn then displayName rather than 'userFullName'
636 fullname = smbldap_talloc_single_attribute(
637 ldap_state->smbldap_state->ldap_struct,
639 get_userattr_key2string(ldap_state->schema_ver,
640 LDAP_ATTR_DISPLAY_NAME),
643 pdb_set_fullname(sampass, fullname, PDB_SET);
645 fullname = smbldap_talloc_single_attribute(
646 ldap_state->smbldap_state->ldap_struct,
648 get_userattr_key2string(ldap_state->schema_ver,
652 pdb_set_fullname(sampass, fullname, PDB_SET);
656 dir_drive = smbldap_talloc_single_attribute(
657 ldap_state->smbldap_state->ldap_struct,
659 get_userattr_key2string(ldap_state->schema_ver,
660 LDAP_ATTR_HOME_DRIVE),
663 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
665 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
668 homedir = smbldap_talloc_single_attribute(
669 ldap_state->smbldap_state->ldap_struct,
671 get_userattr_key2string(ldap_state->schema_ver,
672 LDAP_ATTR_HOME_PATH),
675 if (expand_explicit) {
676 homedir = talloc_sub_basic(ctx,
684 pdb_set_homedir(sampass, homedir, PDB_SET);
686 pdb_set_homedir(sampass,
687 talloc_sub_basic(ctx, username, domain,
692 logon_script = smbldap_talloc_single_attribute(
693 ldap_state->smbldap_state->ldap_struct,
695 get_userattr_key2string(ldap_state->schema_ver,
696 LDAP_ATTR_LOGON_SCRIPT),
699 if (expand_explicit) {
700 logon_script = talloc_sub_basic(ctx,
708 pdb_set_logon_script(sampass, logon_script, PDB_SET);
710 pdb_set_logon_script(sampass,
711 talloc_sub_basic(ctx, username, domain,
716 profile_path = smbldap_talloc_single_attribute(
717 ldap_state->smbldap_state->ldap_struct,
719 get_userattr_key2string(ldap_state->schema_ver,
720 LDAP_ATTR_PROFILE_PATH),
723 if (expand_explicit) {
724 profile_path = talloc_sub_basic(ctx,
732 pdb_set_profile_path(sampass, profile_path, PDB_SET);
734 pdb_set_profile_path(sampass,
735 talloc_sub_basic(ctx, username, domain,
740 acct_desc = smbldap_talloc_single_attribute(
741 ldap_state->smbldap_state->ldap_struct,
743 get_userattr_key2string(ldap_state->schema_ver,
747 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
750 workstations = smbldap_talloc_single_attribute(
751 ldap_state->smbldap_state->ldap_struct,
753 get_userattr_key2string(ldap_state->schema_ver,
757 pdb_set_workstations(sampass, workstations, PDB_SET);
760 munged_dial = smbldap_talloc_single_attribute(
761 ldap_state->smbldap_state->ldap_struct,
763 get_userattr_key2string(ldap_state->schema_ver,
764 LDAP_ATTR_MUNGED_DIAL),
767 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
770 /* FIXME: hours stuff should be cleaner */
774 memset(hours, 0xff, hours_len);
776 if (ldap_state->is_nds_ldap) {
779 char clear_text_pw[512];
781 /* Make call to Novell eDirectory ldap extension to get clear text password.
782 NOTE: This will only work if we have an SSL connection to eDirectory. */
783 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
784 if (user_dn != NULL) {
785 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
787 pwd_len = sizeof(clear_text_pw);
788 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
789 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
790 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
791 TALLOC_FREE(user_dn);
794 ZERO_STRUCT(smblmpwd);
795 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
796 TALLOC_FREE(user_dn);
799 ZERO_STRUCT(smbntpwd);
800 use_samba_attrs = False;
803 TALLOC_FREE(user_dn);
806 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
810 if (use_samba_attrs) {
811 temp = smbldap_talloc_single_attribute(
812 ldap_state->smbldap_state->ldap_struct,
814 get_userattr_key2string(ldap_state->schema_ver,
818 pdb_gethexpwd(temp, smblmpwd);
819 memset((char *)temp, '\0', strlen(temp)+1);
820 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
823 ZERO_STRUCT(smblmpwd);
826 temp = smbldap_talloc_single_attribute(
827 ldap_state->smbldap_state->ldap_struct,
829 get_userattr_key2string(ldap_state->schema_ver,
833 pdb_gethexpwd(temp, smbntpwd);
834 memset((char *)temp, '\0', strlen(temp)+1);
835 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
838 ZERO_STRUCT(smbntpwd);
844 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
846 uint8_t *pwhist = NULL;
848 char *history_string = talloc_array(ctx, char,
849 MAX_PW_HISTORY_LEN*64);
851 if (!history_string) {
855 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
857 pwhist = talloc_zero_array(ctx, uint8_t,
858 pwHistLen * PW_HISTORY_ENTRY_LEN);
859 if (pwhist == NULL) {
860 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
864 if (smbldap_get_single_attribute(
865 ldap_state->smbldap_state->ldap_struct,
867 get_userattr_key2string(ldap_state->schema_ver,
868 LDAP_ATTR_PWD_HISTORY),
870 MAX_PW_HISTORY_LEN*64)) {
871 bool hex_failed = false;
872 for (i = 0; i < pwHistLen; i++){
873 /* Get the 16 byte salt. */
874 if (!pdb_gethexpwd(&history_string[i*64],
875 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
879 /* Get the 16 byte MD5 hash of salt+passwd. */
880 if (!pdb_gethexpwd(&history_string[(i*64)+32],
881 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
882 PW_HISTORY_SALT_LEN])) {
888 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
890 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
893 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
898 temp = smbldap_talloc_single_attribute(
899 ldap_state->smbldap_state->ldap_struct,
901 get_userattr_key2string(ldap_state->schema_ver,
905 acct_ctrl = pdb_decode_acct_ctrl(temp);
907 if (acct_ctrl == 0) {
908 acct_ctrl |= ACB_NORMAL;
911 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
913 acct_ctrl |= ACB_NORMAL;
916 pdb_set_hours_len(sampass, hours_len, PDB_SET);
917 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
919 temp = smbldap_talloc_single_attribute(
920 ldap_state->smbldap_state->ldap_struct,
922 get_userattr_key2string(ldap_state->schema_ver,
923 LDAP_ATTR_BAD_PASSWORD_COUNT),
926 bad_password_count = (uint32_t) atol(temp);
927 pdb_set_bad_password_count(sampass,
928 bad_password_count, PDB_SET);
931 temp = smbldap_talloc_single_attribute(
932 ldap_state->smbldap_state->ldap_struct,
934 get_userattr_key2string(ldap_state->schema_ver,
935 LDAP_ATTR_BAD_PASSWORD_TIME),
938 bad_password_time = (time_t) atol(temp);
939 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
943 temp = smbldap_talloc_single_attribute(
944 ldap_state->smbldap_state->ldap_struct,
946 get_userattr_key2string(ldap_state->schema_ver,
947 LDAP_ATTR_LOGON_COUNT),
950 logon_count = (uint32_t) atol(temp);
951 pdb_set_logon_count(sampass, logon_count, PDB_SET);
954 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
956 temp = smbldap_talloc_single_attribute(
957 ldap_state->smbldap_state->ldap_struct,
959 get_userattr_key2string(ldap_state->schema_ver,
960 LDAP_ATTR_LOGON_HOURS),
963 pdb_gethexhours(temp, hours);
964 memset((char *)temp, '\0', strlen(temp) +1);
965 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
969 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
970 struct passwd unix_pw;
971 bool have_uid = false;
972 bool have_gid = false;
973 struct dom_sid mapped_gsid;
974 const struct dom_sid *primary_gsid;
977 ZERO_STRUCT(unix_pw);
979 unix_pw.pw_name = username;
980 unix_pw.pw_passwd = discard_const_p(char, "x");
982 temp = smbldap_talloc_single_attribute(
988 /* We've got a uid, feed the cache */
989 unix_pw.pw_uid = strtoul(temp, NULL, 10);
992 temp = smbldap_talloc_single_attribute(
998 /* We've got a uid, feed the cache */
999 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1002 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1003 priv2ld(ldap_state),
1007 if (unix_pw.pw_gecos == NULL) {
1008 unix_pw.pw_gecos = fullname;
1010 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1011 priv2ld(ldap_state),
1015 if (unix_pw.pw_dir == NULL) {
1016 unix_pw.pw_dir = discard_const_p(char, "");
1018 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1019 priv2ld(ldap_state),
1023 if (unix_pw.pw_shell == NULL) {
1024 unix_pw.pw_shell = discard_const_p(char, "");
1027 if (have_uid && have_gid) {
1028 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1030 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1033 if (sampass->unix_pw == NULL) {
1034 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1035 pdb_get_username(sampass)));
1039 id.id = sampass->unix_pw->pw_uid;
1040 id.type = ID_TYPE_UID;
1042 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass), &id);
1044 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1045 primary_gsid = pdb_get_group_sid(sampass);
1046 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1047 id.id = sampass->unix_pw->pw_gid;
1048 id.type = ID_TYPE_GID;
1050 idmap_cache_set_sid2unixid(primary_gsid, &id);
1054 /* check the timestamp of the cache vs ldap entry */
1055 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1061 /* see if we have newer updates */
1062 if (!login_cache_read(sampass, &cache_entry)) {
1063 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1064 (unsigned int)pdb_get_bad_password_count(sampass),
1065 (unsigned int)pdb_get_bad_password_time(sampass)));
1070 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1071 (unsigned int)ldap_entry_time,
1072 (unsigned int)cache_entry.entry_timestamp,
1073 (unsigned int)cache_entry.bad_password_time));
1075 if (ldap_entry_time > cache_entry.entry_timestamp) {
1076 /* cache is older than directory , so
1077 we need to delete the entry but allow the
1078 fields to be written out */
1079 login_cache_delentry(sampass);
1082 pdb_set_acct_ctrl(sampass,
1083 pdb_get_acct_ctrl(sampass) |
1084 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1086 pdb_set_bad_password_count(sampass,
1087 cache_entry.bad_password_count,
1089 pdb_set_bad_password_time(sampass,
1090 cache_entry.bad_password_time,
1102 /**********************************************************************
1103 Initialize the ldap db from a struct samu. Called on update.
1104 (Based on init_buffer_from_sam in pdb_tdb.c)
1105 *********************************************************************/
1107 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1108 LDAPMessage *existing,
1109 LDAPMod *** mods, struct samu * sampass,
1110 bool (*need_update)(const struct samu *,
1115 if (mods == NULL || sampass == NULL) {
1116 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1123 * took out adding "objectclass: sambaAccount"
1124 * do this on a per-mod basis
1126 if (need_update(sampass, PDB_USERNAME)) {
1127 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1128 "uid", pdb_get_username(sampass));
1129 if (ldap_state->is_nds_ldap) {
1130 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1131 "cn", pdb_get_username(sampass));
1132 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1133 "sn", pdb_get_username(sampass));
1137 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1139 /* only update the RID if we actually need to */
1140 if (need_update(sampass, PDB_USERSID)) {
1142 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1144 switch ( ldap_state->schema_ver ) {
1145 case SCHEMAVER_SAMBASAMACCOUNT:
1146 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1147 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1148 sid_to_fstring(sid_string, user_sid));
1152 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1157 /* we don't need to store the primary group RID - so leaving it
1158 'free' to hang off the unix primary group makes life easier */
1160 if (need_update(sampass, PDB_GROUPSID)) {
1162 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1164 switch ( ldap_state->schema_ver ) {
1165 case SCHEMAVER_SAMBASAMACCOUNT:
1166 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1167 get_userattr_key2string(ldap_state->schema_ver,
1168 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1172 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1178 /* displayName, cn, and gecos should all be the same
1179 * most easily accomplished by giving them the same OID
1180 * gecos isn't set here b/c it should be handled by the
1182 * We change displayName only and fall back to cn if
1183 * it does not exist.
1186 if (need_update(sampass, PDB_FULLNAME))
1187 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1188 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1189 pdb_get_fullname(sampass));
1191 if (need_update(sampass, PDB_ACCTDESC))
1192 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1193 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1194 pdb_get_acct_desc(sampass));
1196 if (need_update(sampass, PDB_WORKSTATIONS))
1197 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1198 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1199 pdb_get_workstations(sampass));
1201 if (need_update(sampass, PDB_MUNGEDDIAL))
1202 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1203 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1204 pdb_get_munged_dial(sampass));
1206 if (need_update(sampass, PDB_SMBHOME))
1207 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1208 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1209 pdb_get_homedir(sampass));
1211 if (need_update(sampass, PDB_DRIVE))
1212 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1213 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1214 pdb_get_dir_drive(sampass));
1216 if (need_update(sampass, PDB_LOGONSCRIPT))
1217 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1218 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1219 pdb_get_logon_script(sampass));
1221 if (need_update(sampass, PDB_PROFILE))
1222 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1223 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1224 pdb_get_profile_path(sampass));
1226 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1229 if (need_update(sampass, PDB_LOGONTIME))
1230 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1231 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1234 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1237 if (need_update(sampass, PDB_LOGOFFTIME))
1238 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1239 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1242 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1245 if (need_update(sampass, PDB_KICKOFFTIME))
1246 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1247 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1250 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1253 if (need_update(sampass, PDB_CANCHANGETIME))
1254 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1255 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1258 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1259 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1261 if (need_update(sampass, PDB_LMPASSWD)) {
1262 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1265 pdb_sethexpwd(pwstr, lm_pw,
1266 pdb_get_acct_ctrl(sampass));
1267 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1268 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1271 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1272 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1276 if (need_update(sampass, PDB_NTPASSWD)) {
1277 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1280 pdb_sethexpwd(pwstr, nt_pw,
1281 pdb_get_acct_ctrl(sampass));
1282 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1283 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1286 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1287 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1292 if (need_update(sampass, PDB_PWHISTORY)) {
1294 uint32_t pwHistLen = 0;
1295 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1297 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1301 if (pwHistLen == 0) {
1302 /* Remove any password history from the LDAP store. */
1303 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1307 uint32_t currHistLen = 0;
1308 const uint8_t *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1309 if (pwhist != NULL) {
1310 /* We can only store (1024-1/64 password history entries. */
1311 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1312 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1313 /* Store the salt. */
1314 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1315 /* Followed by the md5 hash of salt + md4 hash */
1316 pdb_sethexpwd(&pwstr[(i*64)+32],
1317 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1318 DEBUG(100, ("pwstr=%s\n", pwstr));
1322 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1323 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1328 if (need_update(sampass, PDB_PASSLASTSET)) {
1329 if (asprintf(&temp, "%li",
1330 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1333 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1334 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1340 if (need_update(sampass, PDB_HOURS)) {
1341 const uint8_t *hours = pdb_get_hours(sampass);
1344 pdb_sethexhours(hourstr, hours);
1345 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1348 get_userattr_key2string(ldap_state->schema_ver,
1349 LDAP_ATTR_LOGON_HOURS),
1354 if (need_update(sampass, PDB_ACCTCTRL))
1355 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1356 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1357 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1359 /* password lockout cache:
1360 - If we are now autolocking or clearing, we write to ldap
1361 - If we are clearing, we delete the cache entry
1362 - If the count is > 0, we update the cache
1364 This even means when autolocking, we cache, just in case the
1365 update doesn't work, and we have to cache the autolock flag */
1367 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1368 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1369 uint16_t badcount = pdb_get_bad_password_count(sampass);
1370 time_t badtime = pdb_get_bad_password_time(sampass);
1372 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1374 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1375 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1377 if ((badcount >= pol) || (badcount == 0)) {
1378 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1379 (unsigned int)badcount, (unsigned int)badtime));
1380 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1384 ldap_state->smbldap_state->ldap_struct,
1386 get_userattr_key2string(
1387 ldap_state->schema_ver,
1388 LDAP_ATTR_BAD_PASSWORD_COUNT),
1392 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1396 ldap_state->smbldap_state->ldap_struct,
1398 get_userattr_key2string(
1399 ldap_state->schema_ver,
1400 LDAP_ATTR_BAD_PASSWORD_TIME),
1404 if (badcount == 0) {
1405 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1406 login_cache_delentry(sampass);
1408 struct login_cache cache_entry;
1410 cache_entry.entry_timestamp = time(NULL);
1411 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1412 cache_entry.bad_password_count = badcount;
1413 cache_entry.bad_password_time = badtime;
1415 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1416 login_cache_write(sampass, &cache_entry);
1423 /**********************************************************************
1424 End enumeration of the LDAP password list.
1425 *********************************************************************/
1427 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1429 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1430 if (ldap_state->result) {
1431 ldap_msgfree(ldap_state->result);
1432 ldap_state->result = NULL;
1436 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1437 const char *new_attr)
1441 if (new_attr == NULL) {
1445 for (i=0; (*attr_list)[i] != NULL; i++) {
1449 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1451 SMB_ASSERT((*attr_list) != NULL);
1452 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1453 (*attr_list)[i+1] = NULL;
1456 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1457 const char ***attr_list)
1459 append_attr(mem_ctx, attr_list, "uidNumber");
1460 append_attr(mem_ctx, attr_list, "gidNumber");
1461 append_attr(mem_ctx, attr_list, "homeDirectory");
1462 append_attr(mem_ctx, attr_list, "loginShell");
1463 append_attr(mem_ctx, attr_list, "gecos");
1466 /**********************************************************************
1467 Get struct samu entry from LDAP by username.
1468 *********************************************************************/
1470 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1472 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1473 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1474 LDAPMessage *result = NULL;
1475 LDAPMessage *entry = NULL;
1477 const char ** attr_list;
1480 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1481 append_attr(user, &attr_list,
1482 get_userattr_key2string(ldap_state->schema_ver,
1483 LDAP_ATTR_MOD_TIMESTAMP));
1484 ldapsam_add_unix_attributes(user, &attr_list);
1485 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1487 TALLOC_FREE( attr_list );
1489 if ( rc != LDAP_SUCCESS )
1490 return NT_STATUS_NO_SUCH_USER;
1492 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1495 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1496 ldap_msgfree(result);
1497 return NT_STATUS_NO_SUCH_USER;
1498 } else if (count > 1) {
1499 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1500 ldap_msgfree(result);
1501 return NT_STATUS_NO_SUCH_USER;
1504 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1506 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1507 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1508 ldap_msgfree(result);
1509 return NT_STATUS_NO_SUCH_USER;
1511 pdb_set_backend_private_data(user, result, NULL,
1512 my_methods, PDB_CHANGED);
1513 smbldap_talloc_autofree_ldapmsg(user, result);
1516 ldap_msgfree(result);
1521 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1522 const struct dom_sid *sid, LDAPMessage **result)
1525 const char ** attr_list;
1527 switch ( ldap_state->schema_ver ) {
1528 case SCHEMAVER_SAMBASAMACCOUNT: {
1529 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1530 if (tmp_ctx == NULL) {
1531 return LDAP_NO_MEMORY;
1534 attr_list = get_userattr_list(tmp_ctx,
1535 ldap_state->schema_ver);
1536 append_attr(tmp_ctx, &attr_list,
1537 get_userattr_key2string(
1538 ldap_state->schema_ver,
1539 LDAP_ATTR_MOD_TIMESTAMP));
1540 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1541 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1543 TALLOC_FREE(tmp_ctx);
1545 if ( rc != LDAP_SUCCESS )
1551 DEBUG(0,("Invalid schema version specified\n"));
1557 /**********************************************************************
1558 Get struct samu entry from LDAP by SID.
1559 *********************************************************************/
1561 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1563 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1564 LDAPMessage *result = NULL;
1565 LDAPMessage *entry = NULL;
1569 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1571 if (rc != LDAP_SUCCESS)
1572 return NT_STATUS_NO_SUCH_USER;
1574 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1577 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1578 "count=%d\n", sid_string_dbg(sid), count));
1579 ldap_msgfree(result);
1580 return NT_STATUS_NO_SUCH_USER;
1581 } else if (count > 1) {
1582 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1583 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1585 ldap_msgfree(result);
1586 return NT_STATUS_NO_SUCH_USER;
1589 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1591 ldap_msgfree(result);
1592 return NT_STATUS_NO_SUCH_USER;
1595 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1596 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1597 ldap_msgfree(result);
1598 return NT_STATUS_NO_SUCH_USER;
1601 pdb_set_backend_private_data(user, result, NULL,
1602 my_methods, PDB_CHANGED);
1603 smbldap_talloc_autofree_ldapmsg(user, result);
1604 return NT_STATUS_OK;
1607 /********************************************************************
1608 Do the actual modification - also change a plaintext passord if
1610 **********************************************************************/
1612 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1613 struct samu *newpwd, char *dn,
1614 LDAPMod **mods, int ldap_op,
1615 bool (*need_update)(const struct samu *, enum pdb_elements))
1617 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1620 if (!newpwd || !dn) {
1621 return NT_STATUS_INVALID_PARAMETER;
1624 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1625 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1626 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1627 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1630 char *retoid = NULL;
1631 struct berval *retdata = NULL;
1632 char *utf8_password;
1634 size_t converted_size;
1637 if (!ldap_state->is_nds_ldap) {
1639 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1640 LDAP_EXOP_MODIFY_PASSWD)) {
1641 DEBUG(2, ("ldap password change requested, but LDAP "
1642 "server does not support it -- ignoring\n"));
1643 return NT_STATUS_OK;
1647 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1648 pdb_get_plaintext_passwd(newpwd),
1651 return NT_STATUS_NO_MEMORY;
1654 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1655 TALLOC_FREE(utf8_password);
1656 return NT_STATUS_NO_MEMORY;
1659 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1660 DEBUG(0,("ber_alloc_t returns NULL\n"));
1661 TALLOC_FREE(utf8_password);
1662 TALLOC_FREE(utf8_dn);
1663 return NT_STATUS_UNSUCCESSFUL;
1666 if ((ber_printf (ber, "{") < 0) ||
1667 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1669 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1672 TALLOC_FREE(utf8_dn);
1673 TALLOC_FREE(utf8_password);
1674 return NT_STATUS_UNSUCCESSFUL;
1677 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1678 ret = ber_printf(ber, "ts}",
1679 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1682 ret = ber_printf(ber, "}");
1686 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1689 TALLOC_FREE(utf8_dn);
1690 TALLOC_FREE(utf8_password);
1691 return NT_STATUS_UNSUCCESSFUL;
1694 if ((rc = ber_flatten (ber, &bv))<0) {
1695 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1697 TALLOC_FREE(utf8_dn);
1698 TALLOC_FREE(utf8_password);
1699 return NT_STATUS_UNSUCCESSFUL;
1702 TALLOC_FREE(utf8_dn);
1703 TALLOC_FREE(utf8_password);
1706 if (!ldap_state->is_nds_ldap) {
1707 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1708 LDAP_EXOP_MODIFY_PASSWD,
1709 bv, NULL, NULL, &retoid,
1712 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1713 pdb_get_plaintext_passwd(newpwd));
1715 if (rc != LDAP_SUCCESS) {
1716 char *ld_error = NULL;
1718 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1719 DEBUG(3, ("Could not set userPassword "
1720 "attribute due to an objectClass "
1721 "violation -- ignoring\n"));
1723 return NT_STATUS_OK;
1726 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1728 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1729 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1730 SAFE_FREE(ld_error);
1732 #if defined(LDAP_CONSTRAINT_VIOLATION)
1733 if (rc == LDAP_CONSTRAINT_VIOLATION)
1734 return NT_STATUS_PASSWORD_RESTRICTION;
1736 return NT_STATUS_UNSUCCESSFUL;
1738 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1739 #ifdef DEBUG_PASSWORD
1740 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1743 ber_bvfree(retdata);
1745 ldap_memfree(retoid);
1751 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1752 /* may be password change below however */
1756 if (ldap_state->is_nds_ldap) {
1757 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1761 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1765 rc = smbldap_add(ldap_state->smbldap_state,
1768 case LDAP_MOD_REPLACE:
1769 rc = smbldap_modify(ldap_state->smbldap_state,
1773 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1775 return NT_STATUS_INVALID_PARAMETER;
1778 if (rc!=LDAP_SUCCESS) {
1779 return NT_STATUS_UNSUCCESSFUL;
1783 return NT_STATUS_OK;
1786 /**********************************************************************
1787 Delete entry from LDAP for username.
1788 *********************************************************************/
1790 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1791 struct samu * sam_acct)
1793 struct ldapsam_privates *priv =
1794 (struct ldapsam_privates *)my_methods->private_data;
1797 LDAPMessage *msg, *entry;
1798 NTSTATUS result = NT_STATUS_NO_MEMORY;
1799 const char **attr_list;
1800 TALLOC_CTX *mem_ctx;
1803 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1804 return NT_STATUS_INVALID_PARAMETER;
1807 sname = pdb_get_username(sam_acct);
1809 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1812 mem_ctx = talloc_new(NULL);
1813 if (mem_ctx == NULL) {
1814 DEBUG(0, ("talloc_new failed\n"));
1818 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1819 if (attr_list == NULL) {
1823 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1825 if ((rc != LDAP_SUCCESS) ||
1826 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1827 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1828 DEBUG(5, ("Could not find user %s\n", sname));
1829 result = NT_STATUS_NO_SUCH_USER;
1833 rc = ldapsam_delete_entry(
1834 priv, mem_ctx, entry,
1835 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1836 LDAP_OBJ_SAMBASAMACCOUNT : 0,
1839 result = (rc == LDAP_SUCCESS) ?
1840 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1843 TALLOC_FREE(mem_ctx);
1847 /**********************************************************************
1849 *********************************************************************/
1851 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1853 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1854 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1857 LDAPMessage *result = NULL;
1858 LDAPMessage *entry = NULL;
1859 LDAPMod **mods = NULL;
1860 const char **attr_list;
1862 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1864 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1865 if (pdb_get_username(newpwd) == NULL) {
1866 return NT_STATUS_INVALID_PARAMETER;
1868 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1869 TALLOC_FREE( attr_list );
1870 if (rc != LDAP_SUCCESS) {
1871 return NT_STATUS_UNSUCCESSFUL;
1873 pdb_set_backend_private_data(newpwd, result, NULL,
1874 my_methods, PDB_CHANGED);
1875 smbldap_talloc_autofree_ldapmsg(newpwd, result);
1878 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1879 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1880 return NT_STATUS_UNSUCCESSFUL;
1883 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1884 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1886 return NT_STATUS_UNSUCCESSFUL;
1889 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1891 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1892 pdb_element_is_changed)) {
1893 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1896 ldap_mods_free(mods,True);
1897 return NT_STATUS_UNSUCCESSFUL;
1900 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1901 && (mods == NULL)) {
1902 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1903 pdb_get_username(newpwd)));
1905 return NT_STATUS_OK;
1908 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1911 ldap_mods_free(mods,True);
1917 * We need to set the backend private data to NULL here. For example
1918 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1919 * same one, and with the explicit delete / add logic for attribute
1920 * values the second time we would use the wrong "old" value which
1921 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1923 * The existing LDAPMessage is still being auto-freed by the
1926 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
1929 if (!NT_STATUS_IS_OK(ret)) {
1933 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1934 pdb_get_username(newpwd)));
1935 return NT_STATUS_OK;
1938 /***************************************************************************
1939 Renames a struct samu
1940 - The "rename user script" has full responsibility for changing everything
1941 ***************************************************************************/
1943 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
1944 TALLOC_CTX *tmp_ctx,
1946 uint32_t member_rid);
1948 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
1949 TALLOC_CTX *mem_ctx,
1951 struct dom_sid **pp_sids,
1953 uint32_t *p_num_groups);
1955 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
1956 struct samu *old_acct,
1957 const char *newname)
1959 const char *oldname;
1961 char *rename_script = NULL;
1962 fstring oldname_lower, newname_lower;
1965 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1966 return NT_STATUS_INVALID_PARAMETER;
1969 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1970 return NT_STATUS_INVALID_PARAMETER;
1973 oldname = pdb_get_username(old_acct);
1975 /* rename the posix user */
1976 rename_script = lp_rename_user_script(talloc_tos());
1977 if (rename_script == NULL) {
1978 return NT_STATUS_NO_MEMORY;
1981 if (!(*rename_script)) {
1982 TALLOC_FREE(rename_script);
1983 return NT_STATUS_ACCESS_DENIED;
1986 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1989 /* We have to allow the account name to end with a '$'.
1990 Also, follow the semantics in _samr_create_user() and lower case the
1991 posix name but preserve the case in passdb */
1993 fstrcpy( oldname_lower, oldname );
1994 if (!strlower_m( oldname_lower )) {
1995 return NT_STATUS_INVALID_PARAMETER;
1997 fstrcpy( newname_lower, newname );
1998 if (!strlower_m( newname_lower )) {
1999 return NT_STATUS_INVALID_PARAMETER;
2002 rename_script = realloc_string_sub2(rename_script,
2007 if (!rename_script) {
2008 return NT_STATUS_NO_MEMORY;
2010 rename_script = realloc_string_sub2(rename_script,
2015 rc = smbrun(rename_script, NULL, NULL);
2017 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2018 rename_script, rc));
2020 TALLOC_FREE(rename_script);
2023 smb_nscd_flush_user_cache();
2027 return NT_STATUS_UNSUCCESSFUL;
2029 return NT_STATUS_OK;
2032 /**********************************************************************
2033 Add struct samu to LDAP.
2034 *********************************************************************/
2036 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2038 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2039 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2041 LDAPMessage *result = NULL;
2042 LDAPMessage *entry = NULL;
2043 LDAPMod **mods = NULL;
2044 int ldap_op = LDAP_MOD_REPLACE;
2045 uint32_t num_result;
2046 const char **attr_list;
2047 char *escape_user = NULL;
2048 const char *username = pdb_get_username(newpwd);
2049 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2050 char *filter = NULL;
2052 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2053 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2056 return NT_STATUS_NO_MEMORY;
2059 if (!username || !*username) {
2060 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2061 status = NT_STATUS_INVALID_PARAMETER;
2065 /* free this list after the second search or in case we exit on failure */
2066 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2068 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2070 if (rc != LDAP_SUCCESS) {
2074 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2075 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2079 ldap_msgfree(result);
2082 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2083 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2085 if (rc == LDAP_SUCCESS) {
2086 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2087 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2088 "already in the base, with samba "
2089 "attributes\n", sid_string_dbg(sid)));
2092 ldap_msgfree(result);
2097 /* does the entry already exist but without a samba attributes?
2098 we need to return the samba attributes here */
2100 escape_user = escape_ldap_string(talloc_tos(), username);
2101 filter = talloc_strdup(attr_list, "(uid=%u)");
2103 status = NT_STATUS_NO_MEMORY;
2106 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2107 TALLOC_FREE(escape_user);
2109 status = NT_STATUS_NO_MEMORY;
2113 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2114 filter, attr_list, &result);
2115 if ( rc != LDAP_SUCCESS ) {
2119 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2121 if (num_result > 1) {
2122 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2126 /* Check if we need to update an existing entry */
2127 if (num_result == 1) {
2128 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2129 ldap_op = LDAP_MOD_REPLACE;
2130 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2131 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2133 status = NT_STATUS_NO_MEMORY;
2137 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2139 /* There might be a SID for this account already - say an idmap entry */
2141 filter = talloc_asprintf(ctx,
2142 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2143 get_userattr_key2string(ldap_state->schema_ver,
2144 LDAP_ATTR_USER_SID),
2145 sid_string_talloc(ctx, sid),
2146 LDAP_OBJ_IDMAP_ENTRY,
2147 LDAP_OBJ_SID_ENTRY);
2149 status = NT_STATUS_NO_MEMORY;
2153 /* free old result before doing a new search */
2154 if (result != NULL) {
2155 ldap_msgfree(result);
2158 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2159 filter, attr_list, &result);
2161 if ( rc != LDAP_SUCCESS ) {
2165 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2167 if (num_result > 1) {
2168 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2172 /* Check if we need to update an existing entry */
2173 if (num_result == 1) {
2175 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2176 ldap_op = LDAP_MOD_REPLACE;
2177 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2178 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2180 status = NT_STATUS_NO_MEMORY;
2186 if (num_result == 0) {
2187 char *escape_username;
2188 /* Check if we need to add an entry */
2189 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2190 ldap_op = LDAP_MOD_ADD;
2192 escape_username = escape_rdn_val_string_alloc(username);
2193 if (!escape_username) {
2194 status = NT_STATUS_NO_MEMORY;
2198 if (username[strlen(username)-1] == '$') {
2199 dn = talloc_asprintf(ctx,
2202 lp_ldap_machine_suffix(talloc_tos()));
2204 dn = talloc_asprintf(ctx,
2207 lp_ldap_user_suffix(talloc_tos()));
2210 SAFE_FREE(escape_username);
2212 status = NT_STATUS_NO_MEMORY;
2217 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2218 pdb_element_is_set_or_changed)) {
2219 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2221 ldap_mods_free(mods, true);
2227 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2230 switch ( ldap_state->schema_ver ) {
2231 case SCHEMAVER_SAMBASAMACCOUNT:
2232 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2235 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2239 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2240 if (!NT_STATUS_IS_OK(ret)) {
2241 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2242 pdb_get_username(newpwd),dn));
2243 ldap_mods_free(mods, true);
2247 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2248 ldap_mods_free(mods, true);
2250 status = NT_STATUS_OK;
2256 ldap_msgfree(result);
2261 /**********************************************************************
2262 *********************************************************************/
2264 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2266 LDAPMessage ** result)
2268 int scope = LDAP_SCOPE_SUBTREE;
2270 const char **attr_list;
2272 attr_list = get_attr_list(NULL, groupmap_attr_list);
2273 rc = smbldap_search(ldap_state->smbldap_state,
2274 lp_ldap_suffix (talloc_tos()), scope,
2275 filter, attr_list, 0, result);
2276 TALLOC_FREE(attr_list);
2281 /**********************************************************************
2282 *********************************************************************/
2284 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2285 GROUP_MAP *map, LDAPMessage *entry)
2288 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2290 if (ldap_state == NULL || map == NULL || entry == NULL ||
2291 ldap_state->smbldap_state->ldap_struct == NULL) {
2292 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2297 temp = smbldap_talloc_single_attribute(
2298 ldap_state->smbldap_state->ldap_struct,
2300 get_attr_key2string(groupmap_attr_list,
2301 LDAP_ATTR_GIDNUMBER),
2304 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2305 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2309 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2311 map->gid = (gid_t)atol(temp);
2314 temp = smbldap_talloc_single_attribute(
2315 ldap_state->smbldap_state->ldap_struct,
2317 get_attr_key2string(groupmap_attr_list,
2318 LDAP_ATTR_GROUP_SID),
2321 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2322 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2327 if (!string_to_sid(&map->sid, temp)) {
2328 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2334 temp = smbldap_talloc_single_attribute(
2335 ldap_state->smbldap_state->ldap_struct,
2337 get_attr_key2string(groupmap_attr_list,
2338 LDAP_ATTR_GROUP_TYPE),
2341 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2342 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2346 map->sid_name_use = (enum lsa_SidType)atol(temp);
2348 if ((map->sid_name_use < SID_NAME_USER) ||
2349 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2350 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2356 temp = smbldap_talloc_single_attribute(
2357 ldap_state->smbldap_state->ldap_struct,
2359 get_attr_key2string(groupmap_attr_list,
2360 LDAP_ATTR_DISPLAY_NAME),
2363 temp = smbldap_talloc_single_attribute(
2364 ldap_state->smbldap_state->ldap_struct,
2366 get_attr_key2string(groupmap_attr_list,
2370 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2371 for gidNumber(%lu)\n",(unsigned long)map->gid));
2376 map->nt_name = talloc_strdup(map, temp);
2377 if (!map->nt_name) {
2383 temp = smbldap_talloc_single_attribute(
2384 ldap_state->smbldap_state->ldap_struct,
2386 get_attr_key2string(groupmap_attr_list,
2390 temp = talloc_strdup(ctx, "");
2396 map->comment = talloc_strdup(map, temp);
2397 if (!map->comment) {
2402 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2405 id.type = ID_TYPE_GID;
2407 idmap_cache_set_sid2unixid(&map->sid, &id);
2414 /**********************************************************************
2415 *********************************************************************/
2417 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2421 struct ldapsam_privates *ldap_state =
2422 (struct ldapsam_privates *)methods->private_data;
2423 LDAPMessage *result = NULL;
2424 LDAPMessage *entry = NULL;
2427 if (ldapsam_search_one_group(ldap_state, filter, &result)
2429 return NT_STATUS_NO_SUCH_GROUP;
2432 count = ldap_count_entries(priv2ld(ldap_state), result);
2435 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2437 ldap_msgfree(result);
2438 return NT_STATUS_NO_SUCH_GROUP;
2442 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2443 "count=%d\n", filter, count));
2444 ldap_msgfree(result);
2445 return NT_STATUS_NO_SUCH_GROUP;
2448 entry = ldap_first_entry(priv2ld(ldap_state), result);
2451 ldap_msgfree(result);
2452 return NT_STATUS_UNSUCCESSFUL;
2455 if (!init_group_from_ldap(ldap_state, map, entry)) {
2456 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2457 "group filter %s\n", filter));
2458 ldap_msgfree(result);
2459 return NT_STATUS_NO_SUCH_GROUP;
2462 ldap_msgfree(result);
2463 return NT_STATUS_OK;
2466 /**********************************************************************
2467 *********************************************************************/
2469 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2472 char *filter = NULL;
2476 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2478 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2479 sid_to_fstring(tmp, &sid)) < 0) {
2480 return NT_STATUS_NO_MEMORY;
2483 status = ldapsam_getgroup(methods, filter, map);
2488 /**********************************************************************
2489 *********************************************************************/
2491 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2494 char *filter = NULL;
2497 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2499 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2500 (unsigned long)gid) < 0) {
2501 return NT_STATUS_NO_MEMORY;
2504 status = ldapsam_getgroup(methods, filter, map);
2509 /**********************************************************************
2510 *********************************************************************/
2512 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2515 char *filter = NULL;
2516 char *escape_name = escape_ldap_string(talloc_tos(), name);
2520 return NT_STATUS_NO_MEMORY;
2523 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2525 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2526 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2528 TALLOC_FREE(escape_name);
2529 return NT_STATUS_NO_MEMORY;
2532 TALLOC_FREE(escape_name);
2533 status = ldapsam_getgroup(methods, filter, map);
2538 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2540 const struct dom_sid *domain_sid,
2546 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2547 str, sizeof(str)-1)) {
2548 DEBUG(10, ("Could not find sambaSID attribute\n"));
2552 if (!string_to_sid(&sid, str)) {
2553 DEBUG(10, ("Could not convert string %s to sid\n", str));
2557 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2558 DEBUG(10, ("SID %s is not in expected domain %s\n",
2559 str, sid_string_dbg(domain_sid)));
2563 if (!sid_peek_rid(&sid, rid)) {
2564 DEBUG(10, ("Could not peek into RID\n"));
2571 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2572 TALLOC_CTX *mem_ctx,
2573 const struct dom_sid *group,
2574 uint32_t **pp_member_rids,
2575 size_t *p_num_members)
2577 struct ldapsam_privates *ldap_state =
2578 (struct ldapsam_privates *)methods->private_data;
2579 struct smbldap_state *conn = ldap_state->smbldap_state;
2580 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2581 const char *sid_attrs[] = { "sambaSID", NULL };
2582 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2583 LDAPMessage *result = NULL;
2586 char **values = NULL;
2591 *pp_member_rids = NULL;
2594 filter = talloc_asprintf(mem_ctx,
2595 "(&(objectClass=%s)"
2598 LDAP_OBJ_POSIXGROUP,
2600 sid_string_talloc(mem_ctx, group));
2601 if (filter == NULL) {
2602 ret = NT_STATUS_NO_MEMORY;
2606 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2607 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2610 if (rc != LDAP_SUCCESS)
2613 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2615 count = ldap_count_entries(conn->ldap_struct, result);
2618 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2619 sid_string_dbg(group)));
2620 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2625 ret = NT_STATUS_NO_SUCH_GROUP;
2629 entry = ldap_first_entry(conn->ldap_struct, result);
2633 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2635 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2636 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2640 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2642 if ((values != NULL) && (values[0] != NULL)) {
2644 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2645 if (filter == NULL) {
2646 ret = NT_STATUS_NO_MEMORY;
2650 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2651 char *escape_memberuid;
2653 escape_memberuid = escape_ldap_string(talloc_tos(),
2655 if (escape_memberuid == NULL) {
2656 ret = NT_STATUS_NO_MEMORY;
2660 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2661 TALLOC_FREE(escape_memberuid);
2662 if (filter == NULL) {
2663 ret = NT_STATUS_NO_MEMORY;
2668 filter = talloc_asprintf_append_buffer(filter, "))");
2669 if (filter == NULL) {
2670 ret = NT_STATUS_NO_MEMORY;
2674 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2675 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2678 if (rc != LDAP_SUCCESS)
2681 count = ldap_count_entries(conn->ldap_struct, result);
2682 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2684 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2686 for (entry = ldap_first_entry(conn->ldap_struct, result);
2688 entry = ldap_next_entry(conn->ldap_struct, entry))
2694 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2698 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2699 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2700 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2704 if (!string_to_sid(&sid, sidstr))
2707 if (!sid_check_is_in_our_sam(&sid)) {
2708 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2709 "in our domain\n"));
2710 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2714 sid_peek_rid(&sid, &rid);
2716 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2718 ret = NT_STATUS_NO_MEMORY;
2724 filter = talloc_asprintf(mem_ctx,
2725 "(&(objectClass=%s)"
2727 LDAP_OBJ_SAMBASAMACCOUNT,
2730 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2731 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2734 if (rc != LDAP_SUCCESS)
2737 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2739 for (entry = ldap_first_entry(conn->ldap_struct, result);
2741 entry = ldap_next_entry(conn->ldap_struct, entry))
2745 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2747 get_global_sam_sid(),
2749 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2750 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2754 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2756 ret = NT_STATUS_NO_MEMORY;
2766 ldap_value_free(values);
2771 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2772 TALLOC_CTX *mem_ctx,
2774 struct dom_sid **pp_sids,
2776 uint32_t *p_num_groups)
2778 struct ldapsam_privates *ldap_state =
2779 (struct ldapsam_privates *)methods->private_data;
2780 struct smbldap_state *conn = ldap_state->smbldap_state;
2782 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2785 LDAPMessage *result = NULL;
2787 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2791 gid_t primary_gid = -1;
2796 if (pdb_get_username(user) == NULL) {
2797 return NT_STATUS_INVALID_PARAMETER;
2800 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2801 if (escape_name == NULL)
2802 return NT_STATUS_NO_MEMORY;
2804 if (user->unix_pw) {
2805 primary_gid = user->unix_pw->pw_gid;
2807 /* retrieve the users primary gid */
2808 filter = talloc_asprintf(mem_ctx,
2809 "(&(objectClass=%s)(uid=%s))",
2810 LDAP_OBJ_SAMBASAMACCOUNT,
2812 if (filter == NULL) {
2813 ret = NT_STATUS_NO_MEMORY;
2817 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2818 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2820 if (rc != LDAP_SUCCESS)
2823 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2825 count = ldap_count_entries(priv2ld(ldap_state), result);
2829 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2830 ret = NT_STATUS_NO_SUCH_USER;
2833 entry = ldap_first_entry(priv2ld(ldap_state), result);
2835 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2837 DEBUG (1, ("Unable to find the member's gid!\n"));
2838 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2841 primary_gid = strtoul(gidstr, NULL, 10);
2844 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2845 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2850 filter = talloc_asprintf(mem_ctx,
2851 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2852 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2853 if (filter == NULL) {
2854 ret = NT_STATUS_NO_MEMORY;
2858 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2859 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2861 if (rc != LDAP_SUCCESS)
2864 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2872 /* We need to add the primary group as the first gid/sid */
2874 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2875 ret = NT_STATUS_NO_MEMORY;
2879 /* This sid will be replaced later */
2881 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2883 if (!NT_STATUS_IS_OK(ret)) {
2887 for (entry = ldap_first_entry(conn->ldap_struct, result);
2889 entry = ldap_next_entry(conn->ldap_struct, entry))
2896 if (!smbldap_get_single_attribute(conn->ldap_struct,
2898 str, sizeof(str)-1))
2901 if (!string_to_sid(&sid, str))
2904 if (!smbldap_get_single_attribute(conn->ldap_struct,
2906 str, sizeof(str)-1))
2909 gid = strtoul(str, &end, 10);
2911 if (PTR_DIFF(end, str) != strlen(str))
2914 if (gid == primary_gid) {
2915 sid_copy(&(*pp_sids)[0], &sid);
2917 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2919 ret = NT_STATUS_NO_MEMORY;
2922 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2924 if (!NT_STATUS_IS_OK(ret)) {
2930 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
2931 DEBUG(3, ("primary group of [%s] not found\n",
2932 pdb_get_username(user)));
2933 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2937 *p_num_groups = num_sids;
2943 TALLOC_FREE(escape_name);
2947 /**********************************************************************
2948 * Augment a posixGroup object with a sambaGroupMapping domgroup
2949 *********************************************************************/
2951 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
2952 struct ldapsam_privates *ldap_state,
2955 const char *filter, *dn;
2956 LDAPMessage *msg, *entry;
2960 filter = talloc_asprintf(mem_ctx,
2961 "(&(objectClass=%s)(gidNumber=%u))",
2962 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
2963 if (filter == NULL) {
2964 return NT_STATUS_NO_MEMORY;
2967 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2968 get_attr_list(mem_ctx, groupmap_attr_list),
2970 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
2972 if ((rc != LDAP_SUCCESS) ||
2973 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2974 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2975 return NT_STATUS_NO_SUCH_GROUP;
2978 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2980 return NT_STATUS_NO_MEMORY;
2984 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
2986 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
2987 sid_string_talloc(mem_ctx, &map->sid));
2988 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
2989 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
2990 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
2992 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
2994 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
2996 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2997 if (rc != LDAP_SUCCESS) {
2998 return NT_STATUS_ACCESS_DENIED;
3001 return NT_STATUS_OK;
3004 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3007 struct ldapsam_privates *ldap_state =
3008 (struct ldapsam_privates *)methods->private_data;
3009 LDAPMessage *msg = NULL;
3010 LDAPMod **mods = NULL;
3011 const char *attrs[] = { NULL };
3015 TALLOC_CTX *mem_ctx;
3023 mem_ctx = talloc_new(NULL);
3024 if (mem_ctx == NULL) {
3025 DEBUG(0, ("talloc_new failed\n"));
3026 return NT_STATUS_NO_MEMORY;
3029 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3030 sid_string_talloc(mem_ctx, &map->sid));
3031 if (filter == NULL) {
3032 result = NT_STATUS_NO_MEMORY;
3036 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3037 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3038 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3040 if ((rc == LDAP_SUCCESS) &&
3041 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3043 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3044 "group mapping entry\n", sid_string_dbg(&map->sid)));
3045 result = NT_STATUS_GROUP_EXISTS;
3049 switch (map->sid_name_use) {
3051 case SID_NAME_DOM_GRP:
3052 /* To map a domain group we need to have a posix group
3054 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3058 case SID_NAME_ALIAS:
3059 if (!sid_check_is_in_our_sam(&map->sid)
3060 && !sid_check_is_in_builtin(&map->sid) )
3062 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3063 sid_string_dbg(&map->sid)));
3064 result = NT_STATUS_INVALID_PARAMETER;
3070 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3071 sid_type_lookup(map->sid_name_use)));
3072 result = NT_STATUS_INVALID_PARAMETER;
3076 /* Domain groups have been mapped in a separate routine, we have to
3077 * create an alias now */
3079 if (map->gid == -1) {
3080 DEBUG(10, ("Refusing to map gid==-1\n"));
3081 result = NT_STATUS_INVALID_PARAMETER;
3086 id.type = ID_TYPE_GID;
3088 if (pdb_id_to_sid(&id, &sid)) {
3089 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3090 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3091 result = NT_STATUS_GROUP_EXISTS;
3095 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3096 * the best we can get out of LDAP. */
3098 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3099 sid_string_talloc(mem_ctx, &map->sid),
3100 lp_ldap_group_suffix(talloc_tos()));
3102 result = NT_STATUS_NO_MEMORY;
3108 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3109 LDAP_OBJ_SID_ENTRY);
3110 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3112 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3113 sid_string_talloc(mem_ctx, &map->sid));
3114 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3115 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3116 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3118 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3120 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3121 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3122 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3124 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3126 result = (rc == LDAP_SUCCESS) ?
3127 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3130 TALLOC_FREE(mem_ctx);
3134 /**********************************************************************
3135 * Update a group mapping entry. We're quite strict about what can be changed:
3136 * Only the description and displayname may be changed. It simply does not
3137 * make any sense to change the SID, gid or the type in a mapping.
3138 *********************************************************************/
3140 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3143 struct ldapsam_privates *ldap_state =
3144 (struct ldapsam_privates *)methods->private_data;
3146 const char *filter, *dn;
3147 LDAPMessage *msg = NULL;
3148 LDAPMessage *entry = NULL;
3149 LDAPMod **mods = NULL;
3150 TALLOC_CTX *mem_ctx;
3153 mem_ctx = talloc_new(NULL);
3154 if (mem_ctx == NULL) {
3155 DEBUG(0, ("talloc_new failed\n"));
3156 return NT_STATUS_NO_MEMORY;
3159 /* Make 100% sure that sid, gid and type are not changed by looking up
3160 * exactly the values we're given in LDAP. */
3162 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3163 "(sambaSid=%s)(gidNumber=%u)"
3164 "(sambaGroupType=%d))",
3166 sid_string_talloc(mem_ctx, &map->sid),
3167 (unsigned int)map->gid, map->sid_name_use);
3168 if (filter == NULL) {
3169 result = NT_STATUS_NO_MEMORY;
3173 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3174 get_attr_list(mem_ctx, groupmap_attr_list),
3176 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3178 if ((rc != LDAP_SUCCESS) ||
3179 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3180 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3181 result = NT_STATUS_NO_SUCH_GROUP;
3185 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3188 result = NT_STATUS_NO_MEMORY;
3193 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3195 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3197 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3200 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3201 "nothing to do\n"));
3202 result = NT_STATUS_OK;
3206 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3208 if (rc != LDAP_SUCCESS) {
3209 result = NT_STATUS_ACCESS_DENIED;
3213 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3214 "group %lu in LDAP\n", (unsigned long)map->gid));
3216 result = NT_STATUS_OK;
3219 TALLOC_FREE(mem_ctx);
3223 /**********************************************************************
3224 *********************************************************************/
3226 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3229 struct ldapsam_privates *priv =
3230 (struct ldapsam_privates *)methods->private_data;
3231 LDAPMessage *msg, *entry;
3234 TALLOC_CTX *mem_ctx;
3237 mem_ctx = talloc_new(NULL);
3238 if (mem_ctx == NULL) {
3239 DEBUG(0, ("talloc_new failed\n"));
3240 return NT_STATUS_NO_MEMORY;
3243 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3244 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3245 sid_string_talloc(mem_ctx, &sid));
3246 if (filter == NULL) {
3247 result = NT_STATUS_NO_MEMORY;
3250 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3251 get_attr_list(mem_ctx, groupmap_attr_list),
3253 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3255 if ((rc != LDAP_SUCCESS) ||
3256 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3257 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3258 result = NT_STATUS_NO_SUCH_GROUP;
3262 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3263 get_attr_list(mem_ctx,
3264 groupmap_attr_list_to_delete));
3266 if ((rc == LDAP_NAMING_VIOLATION) ||
3267 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3268 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3269 const char *attrs[] = { "sambaGroupType", "description",
3270 "displayName", "sambaSIDList",
3273 /* Second try. Don't delete the sambaSID attribute, this is
3274 for "old" entries that are tacked on a winbind
3277 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3278 LDAP_OBJ_GROUPMAP, attrs);
3281 if ((rc == LDAP_NAMING_VIOLATION) ||
3282 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3283 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3284 const char *attrs[] = { "sambaGroupType", "description",
3285 "displayName", "sambaSIDList",
3286 "gidNumber", NULL };
3288 /* Third try. This is a post-3.0.21 alias (containing only
3289 * sambaSidEntry and sambaGroupMapping classes), we also have
3290 * to delete the gidNumber attribute, only the sambaSidEntry
3293 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3294 LDAP_OBJ_GROUPMAP, attrs);
3297 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3300 TALLOC_FREE(mem_ctx);
3304 /**********************************************************************
3305 *********************************************************************/
3307 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3310 struct ldapsam_privates *ldap_state =
3311 (struct ldapsam_privates *)my_methods->private_data;
3312 char *filter = NULL;
3314 const char **attr_list;
3316 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3318 return NT_STATUS_NO_MEMORY;
3320 attr_list = get_attr_list( NULL, groupmap_attr_list );
3321 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3322 LDAP_SCOPE_SUBTREE, filter,
3323 attr_list, 0, &ldap_state->result);
3324 TALLOC_FREE(attr_list);
3326 if (rc != LDAP_SUCCESS) {
3327 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3328 ldap_err2string(rc)));
3329 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3330 lp_ldap_suffix(talloc_tos()), filter));
3331 ldap_msgfree(ldap_state->result);
3332 ldap_state->result = NULL;
3333 TALLOC_FREE(filter);
3334 return NT_STATUS_UNSUCCESSFUL;
3337 TALLOC_FREE(filter);
3339 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3340 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3341 ldap_state->result)));
3344 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3345 ldap_state->result);
3346 ldap_state->index = 0;
3348 return NT_STATUS_OK;
3351 /**********************************************************************
3352 *********************************************************************/
3354 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3356 ldapsam_endsampwent(my_methods);
3359 /**********************************************************************
3360 *********************************************************************/
3362 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3365 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3366 struct ldapsam_privates *ldap_state =
3367 (struct ldapsam_privates *)my_methods->private_data;
3371 if (!ldap_state->entry)
3374 ldap_state->index++;
3375 bret = init_group_from_ldap(ldap_state, map,
3379 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3383 return NT_STATUS_OK;
3386 /**********************************************************************
3387 *********************************************************************/
3389 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3390 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3391 GROUP_MAP ***pp_rmap,
3392 size_t *p_num_entries,
3395 GROUP_MAP *map = NULL;
3401 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3402 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3404 return NT_STATUS_ACCESS_DENIED;
3409 map = talloc_zero(NULL, GROUP_MAP);
3411 return NT_STATUS_NO_MEMORY;
3414 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3419 if (sid_name_use != SID_NAME_UNKNOWN &&
3420 sid_name_use != map->sid_name_use) {
3421 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3422 "not of the requested type\n",
3426 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3427 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3428 "non mapped\n", map->nt_name));
3432 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3433 GROUP_MAP *, entries + 1);
3435 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3436 "enlarge group map!\n"));
3437 return NT_STATUS_UNSUCCESSFUL;
3440 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3445 ldapsam_endsamgrent(methods);
3447 *p_num_entries = entries;
3449 return NT_STATUS_OK;
3452 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3453 const struct dom_sid *alias,
3454 const struct dom_sid *member,
3457 struct ldapsam_privates *ldap_state =
3458 (struct ldapsam_privates *)methods->private_data;
3460 LDAPMessage *result = NULL;
3461 LDAPMessage *entry = NULL;
3463 LDAPMod **mods = NULL;
3465 enum lsa_SidType type = SID_NAME_USE_NONE;
3468 char *filter = NULL;
3470 if (sid_check_is_in_builtin(alias)) {
3471 type = SID_NAME_ALIAS;
3474 if (sid_check_is_in_our_sam(alias)) {
3475 type = SID_NAME_ALIAS;
3478 if (type == SID_NAME_USE_NONE) {
3479 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3480 sid_string_dbg(alias)));
3481 return NT_STATUS_NO_SUCH_ALIAS;
3484 if (asprintf(&filter,
3485 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3486 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3488 return NT_STATUS_NO_MEMORY;
3491 if (ldapsam_search_one_group(ldap_state, filter,
3492 &result) != LDAP_SUCCESS) {
3494 return NT_STATUS_NO_SUCH_ALIAS;
3497 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3501 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3502 ldap_msgfree(result);
3504 return NT_STATUS_NO_SUCH_ALIAS;
3508 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3509 "filter %s: count=%d\n", filter, count));
3510 ldap_msgfree(result);
3512 return NT_STATUS_NO_SUCH_ALIAS;
3517 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3521 ldap_msgfree(result);
3522 return NT_STATUS_UNSUCCESSFUL;
3525 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3527 ldap_msgfree(result);
3528 return NT_STATUS_UNSUCCESSFUL;
3531 smbldap_set_mod(&mods, modop,
3532 get_attr_key2string(groupmap_attr_list,
3533 LDAP_ATTR_SID_LIST),
3534 sid_to_fstring(tmp, member));
3536 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3538 ldap_mods_free(mods, True);
3539 ldap_msgfree(result);
3542 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3543 return NT_STATUS_MEMBER_IN_ALIAS;
3546 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3547 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3550 if (rc != LDAP_SUCCESS) {
3551 return NT_STATUS_UNSUCCESSFUL;
3554 return NT_STATUS_OK;
3557 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3558 const struct dom_sid *alias,
3559 const struct dom_sid *member)
3561 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3564 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3565 const struct dom_sid *alias,
3566 const struct dom_sid *member)
3568 return ldapsam_modify_aliasmem(methods, alias, member,
3572 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3573 const struct dom_sid *alias,
3574 TALLOC_CTX *mem_ctx,
3575 struct dom_sid **pp_members,
3576 size_t *p_num_members)
3578 struct ldapsam_privates *ldap_state =
3579 (struct ldapsam_privates *)methods->private_data;
3580 LDAPMessage *result = NULL;
3581 LDAPMessage *entry = NULL;
3583 char **values = NULL;
3585 char *filter = NULL;
3586 uint32_t num_members = 0;
3587 enum lsa_SidType type = SID_NAME_USE_NONE;
3593 if (sid_check_is_in_builtin(alias)) {
3594 type = SID_NAME_ALIAS;
3597 if (sid_check_is_in_our_sam(alias)) {
3598 type = SID_NAME_ALIAS;
3601 if (type == SID_NAME_USE_NONE) {
3602 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3603 sid_string_dbg(alias)));
3604 return NT_STATUS_NO_SUCH_ALIAS;
3607 if (asprintf(&filter,
3608 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3609 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3611 return NT_STATUS_NO_MEMORY;
3614 if (ldapsam_search_one_group(ldap_state, filter,
3615 &result) != LDAP_SUCCESS) {
3617 return NT_STATUS_NO_SUCH_ALIAS;
3620 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3624 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3625 ldap_msgfree(result);
3627 return NT_STATUS_NO_SUCH_ALIAS;
3631 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3632 "filter %s: count=%d\n", filter, count));
3633 ldap_msgfree(result);
3635 return NT_STATUS_NO_SUCH_ALIAS;
3640 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3644 ldap_msgfree(result);
3645 return NT_STATUS_UNSUCCESSFUL;
3648 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3650 get_attr_key2string(groupmap_attr_list,
3651 LDAP_ATTR_SID_LIST));
3653 if (values == NULL) {
3654 ldap_msgfree(result);
3655 return NT_STATUS_OK;
3658 count = ldap_count_values(values);
3660 for (i=0; i<count; i++) {
3661 struct dom_sid member;
3664 if (!string_to_sid(&member, values[i]))
3667 status = add_sid_to_array(mem_ctx, &member, pp_members,
3669 if (!NT_STATUS_IS_OK(status)) {
3670 ldap_value_free(values);
3671 ldap_msgfree(result);
3676 *p_num_members = num_members;
3677 ldap_value_free(values);
3678 ldap_msgfree(result);
3680 return NT_STATUS_OK;
3683 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3684 TALLOC_CTX *mem_ctx,
3685 const struct dom_sid *domain_sid,
3686 const struct dom_sid *members,
3688 uint32_t **pp_alias_rids,
3689 size_t *p_num_alias_rids)
3691 struct ldapsam_privates *ldap_state =
3692 (struct ldapsam_privates *)methods->private_data;
3695 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3697 LDAPMessage *result = NULL;
3698 LDAPMessage *entry = NULL;
3702 enum lsa_SidType type = SID_NAME_USE_NONE;
3703 bool is_builtin = false;
3704 bool sid_added = false;
3706 *pp_alias_rids = NULL;
3707 *p_num_alias_rids = 0;
3709 if (sid_check_is_builtin(domain_sid)) {
3711 type = SID_NAME_ALIAS;
3714 if (sid_check_is_our_sam(domain_sid)) {
3715 type = SID_NAME_ALIAS;
3718 if (type == SID_NAME_USE_NONE) {
3719 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3720 sid_string_dbg(domain_sid)));
3721 return NT_STATUS_UNSUCCESSFUL;
3724 if (num_members == 0) {
3725 return NT_STATUS_OK;
3728 filter = talloc_asprintf(mem_ctx,
3729 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3730 LDAP_OBJ_GROUPMAP, type);
3732 for (i=0; i<num_members; i++)
3733 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3735 sid_string_talloc(mem_ctx,
3738 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3740 if (filter == NULL) {
3741 return NT_STATUS_NO_MEMORY;
3745 ldap_state->search_cache.filter &&
3746 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3747 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3748 result = ldap_state->search_cache.result;
3749 ldap_state->search_cache.result = NULL;
3751 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3752 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3753 if (rc != LDAP_SUCCESS) {
3754 return NT_STATUS_UNSUCCESSFUL;
3756 smbldap_talloc_autofree_ldapmsg(filter, result);
3759 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3761 for (entry = ldap_first_entry(ldap_struct, result);
3763 entry = ldap_next_entry(ldap_struct, entry))
3769 if (!smbldap_get_single_attribute(ldap_struct, entry,
3775 if (!string_to_sid(&sid, sid_str))
3778 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3783 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3784 p_num_alias_rids)) {
3785 return NT_STATUS_NO_MEMORY;
3789 if (!is_builtin && !sid_added) {
3790 TALLOC_FREE(ldap_state->search_cache.filter);
3792 * Note: result is a talloc child of filter because of the
3793 * smbldap_talloc_autofree_ldapmsg() usage
3795 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3796 ldap_state->search_cache.result = result;
3799 return NT_STATUS_OK;
3802 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3803 enum pdb_policy_type type,
3806 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3808 LDAPMod **mods = NULL;
3809 fstring value_string;
3810 const char *policy_attr = NULL;
3812 struct ldapsam_privates *ldap_state =
3813 (struct ldapsam_privates *)methods->private_data;
3815 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3817 if (!ldap_state->domain_dn) {
3818 return NT_STATUS_INVALID_PARAMETER;
3821 policy_attr = get_account_policy_attr(type);
3822 if (policy_attr == NULL) {
3823 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3828 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3830 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3832 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3835 ldap_mods_free(mods, True);
3837 if (rc != LDAP_SUCCESS) {
3841 if (!cache_account_policy_set(type, value)) {
3842 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3843 "update local tdb cache\n"));
3847 return NT_STATUS_OK;
3850 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3851 enum pdb_policy_type type,
3854 return ldapsam_set_account_policy_in_ldap(methods, type,
3858 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3859 enum pdb_policy_type type,
3862 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3863 LDAPMessage *result = NULL;
3864 LDAPMessage *entry = NULL;
3869 const char *policy_attr = NULL;
3871 struct ldapsam_privates *ldap_state =
3872 (struct ldapsam_privates *)methods->private_data;
3874 const char *attrs[2];
3876 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3878 if (!ldap_state->domain_dn) {
3879 return NT_STATUS_INVALID_PARAMETER;
3882 policy_attr = get_account_policy_attr(type);
3884 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3885 "policy index: %d\n", type));
3889 attrs[0] = policy_attr;
3892 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3893 if (filter == NULL) {
3894 return NT_STATUS_NO_MEMORY;
3896 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3897 LDAP_SCOPE_BASE, filter, attrs, 0,
3899 TALLOC_FREE(filter);
3900 if (rc != LDAP_SUCCESS) {
3904 count = ldap_count_entries(priv2ld(ldap_state), result);
3909 entry = ldap_first_entry(priv2ld(ldap_state), result);
3910 if (entry == NULL) {
3914 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3919 *value = (uint32_t)atol(vals[0]);
3921 ntstatus = NT_STATUS_OK;
3925 ldap_value_free(vals);
3926 ldap_msgfree(result);
3931 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3933 - if user hasn't decided to use account policies inside LDAP just reuse the
3936 - if there is a valid cache entry, return that
3937 - if there is an LDAP entry, update cache and return
3938 - otherwise set to default, update cache and return
3942 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
3943 enum pdb_policy_type type,
3946 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3948 if (cache_account_policy_get(type, value)) {
3949 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3951 return NT_STATUS_OK;
3954 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
3956 if (NT_STATUS_IS_OK(ntstatus)) {
3960 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3964 /* should we automagically migrate old tdb value here ? */
3965 if (account_policy_get(type, value))
3968 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3969 "default\n", type));
3972 if (!account_policy_get_default(type, value)) {
3978 ntstatus = ldapsam_set_account_policy(methods, type, *value);
3979 if (!NT_STATUS_IS_OK(ntstatus)) {
3985 if (!cache_account_policy_set(type, *value)) {
3986 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3987 "tdb as a cache\n"));
3988 return NT_STATUS_UNSUCCESSFUL;
3991 return NT_STATUS_OK;
3994 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
3995 const struct dom_sid *domain_sid,
3999 enum lsa_SidType *attrs)
4001 struct ldapsam_privates *ldap_state =
4002 (struct ldapsam_privates *)methods->private_data;
4003 LDAPMessage *msg = NULL;
4005 char *allsids = NULL;
4006 int i, rc, num_mapped;
4007 NTSTATUS result = NT_STATUS_NO_MEMORY;
4008 TALLOC_CTX *mem_ctx;
4012 mem_ctx = talloc_new(NULL);
4013 if (mem_ctx == NULL) {
4014 DEBUG(0, ("talloc_new failed\n"));
4018 if (!sid_check_is_builtin(domain_sid) &&
4019 !sid_check_is_our_sam(domain_sid)) {
4020 result = NT_STATUS_INVALID_PARAMETER;
4024 if (num_rids == 0) {
4025 result = NT_STATUS_NONE_MAPPED;
4029 for (i=0; i<num_rids; i++)
4030 attrs[i] = SID_NAME_UNKNOWN;
4032 allsids = talloc_strdup(mem_ctx, "");
4033 if (allsids == NULL) {
4037 for (i=0; i<num_rids; i++) {
4039 sid_compose(&sid, domain_sid, rids[i]);
4040 allsids = talloc_asprintf_append_buffer(
4041 allsids, "(sambaSid=%s)",
4042 sid_string_talloc(mem_ctx, &sid));
4043 if (allsids == NULL) {
4048 /* First look for users */
4052 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4054 filter = talloc_asprintf(
4055 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4056 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4058 if (filter == NULL) {
4062 rc = smbldap_search(ldap_state->smbldap_state,
4063 lp_ldap_user_suffix(talloc_tos()),
4064 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4066 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4069 if (rc != LDAP_SUCCESS)
4072 ld = ldap_state->smbldap_state->ldap_struct;
4075 for (entry = ldap_first_entry(ld, msg);
4077 entry = ldap_next_entry(ld, entry)) {
4082 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4084 DEBUG(2, ("Could not find sid from ldap entry\n"));
4088 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4091 DEBUG(2, ("Could not retrieve uid attribute\n"));
4095 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4096 if (rid == rids[rid_index])
4100 if (rid_index == num_rids) {
4101 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4105 attrs[rid_index] = SID_NAME_USER;
4106 names[rid_index] = name;
4110 if (num_mapped == num_rids) {
4111 /* No need to look for groups anymore -- we're done */
4112 result = NT_STATUS_OK;
4116 /* Same game for groups */
4120 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4121 "sambaGroupType", NULL };
4123 filter = talloc_asprintf(
4124 mem_ctx, "(&(objectClass=%s)(|%s))",
4125 LDAP_OBJ_GROUPMAP, allsids);
4126 if (filter == NULL) {
4130 rc = smbldap_search(ldap_state->smbldap_state,
4131 lp_ldap_suffix(talloc_tos()),
4132 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4134 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4137 if (rc != LDAP_SUCCESS)
4140 /* ldap_struct might have changed due to a reconnect */
4142 ld = ldap_state->smbldap_state->ldap_struct;
4144 /* For consistency checks, we already checked we're only domain or builtin */
4146 is_builtin = sid_check_is_builtin(domain_sid);
4148 for (entry = ldap_first_entry(ld, msg);
4150 entry = ldap_next_entry(ld, entry))
4155 enum lsa_SidType type;
4156 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4158 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4161 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4166 type = (enum lsa_SidType)atol(attr);
4168 /* Consistency checks */
4169 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4170 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4171 (type != SID_NAME_DOM_GRP)))) {
4172 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4175 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4177 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4181 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4184 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4186 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4190 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4195 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4196 if (rid == rids[rid_index])
4200 if (rid_index == num_rids) {
4201 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4205 attrs[rid_index] = type;
4206 names[rid_index] = attr;
4210 result = NT_STATUS_NONE_MAPPED;
4213 result = (num_mapped == num_rids) ?
4214 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4216 TALLOC_FREE(mem_ctx);
4220 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4222 char *filter = NULL;
4223 char *escaped = NULL;
4224 char *result = NULL;
4226 if (asprintf(&filter, "(&%s(objectclass=%s))",
4227 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4231 escaped = escape_ldap_string(talloc_tos(), username);
4232 if (escaped == NULL) goto done;
4234 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4238 TALLOC_FREE(escaped);
4243 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4247 const char **result;
4249 va_start(ap, mem_ctx);
4250 while (va_arg(ap, const char *) != NULL)
4254 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4258 va_start(ap, mem_ctx);
4259 for (i=0; i<num; i++) {
4260 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4261 if (result[i] == NULL) {
4262 talloc_free(result);
4273 struct ldap_search_state {
4274 struct smbldap_state *connection;
4276 uint32_t acct_flags;
4277 uint16_t group_type;
4284 void *pagedresults_cookie;
4286 LDAPMessage *entries, *current_entry;
4287 bool (*ldap2displayentry)(struct ldap_search_state *state,
4288 TALLOC_CTX *mem_ctx,
4289 LDAP *ld, LDAPMessage *entry,
4290 struct samr_displayentry *result);
4293 static bool ldapsam_search_firstpage(struct pdb_search *search)
4295 struct ldap_search_state *state =
4296 (struct ldap_search_state *)search->private_data;
4298 int rc = LDAP_OPERATIONS_ERROR;
4300 state->entries = NULL;
4302 if (state->connection->paged_results) {
4303 rc = smbldap_search_paged(state->connection, state->base,
4304 state->scope, state->filter,
4305 state->attrs, state->attrsonly,
4306 lp_ldap_page_size(), &state->entries,
4307 &state->pagedresults_cookie);
4310 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4312 if (state->entries != NULL) {
4313 /* Left over from unsuccessful paged attempt */
4314 ldap_msgfree(state->entries);
4315 state->entries = NULL;
4318 rc = smbldap_search(state->connection, state->base,
4319 state->scope, state->filter, state->attrs,
4320 state->attrsonly, &state->entries);
4322 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4325 /* Ok, the server was lying. It told us it could do paged
4326 * searches when it could not. */
4327 state->connection->paged_results = False;
4330 ld = state->connection->ldap_struct;
4332 DEBUG(5, ("Don't have an LDAP connection right after a "
4336 state->current_entry = ldap_first_entry(ld, state->entries);
4341 static bool ldapsam_search_nextpage(struct pdb_search *search)
4343 struct ldap_search_state *state =
4344 (struct ldap_search_state *)search->private_data;
4347 if (!state->connection->paged_results) {
4348 /* There is no next page when there are no paged results */
4352 rc = smbldap_search_paged(state->connection, state->base,
4353 state->scope, state->filter, state->attrs,
4354 state->attrsonly, lp_ldap_page_size(),
4356 &state->pagedresults_cookie);
4358 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4361 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4363 if (state->current_entry == NULL) {
4364 ldap_msgfree(state->entries);
4365 state->entries = NULL;
4372 static bool ldapsam_search_next_entry(struct pdb_search *search,
4373 struct samr_displayentry *entry)
4375 struct ldap_search_state *state =
4376 (struct ldap_search_state *)search->private_data;
4380 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4383 if ((state->entries == NULL) &&
4384 !ldapsam_search_nextpage(search))
4387 if (state->current_entry == NULL) {
4391 result = state->ldap2displayentry(state, search,
4392 state->connection->ldap_struct,
4393 state->current_entry, entry);
4397 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4398 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4399 if (dn != NULL) ldap_memfree(dn);
4402 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4404 if (state->current_entry == NULL) {
4405 ldap_msgfree(state->entries);
4406 state->entries = NULL;
4409 if (!result) goto retry;
4414 static void ldapsam_search_end(struct pdb_search *search)
4416 struct ldap_search_state *state =
4417 (struct ldap_search_state *)search->private_data;
4420 if (state->pagedresults_cookie == NULL)
4423 if (state->entries != NULL)
4424 ldap_msgfree(state->entries);
4426 state->entries = NULL;
4427 state->current_entry = NULL;
4429 if (!state->connection->paged_results)
4432 /* Tell the LDAP server we're not interested in the rest anymore. */
4434 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4435 state->filter, state->attrs,
4436 state->attrsonly, 0, &state->entries,
4437 &state->pagedresults_cookie);
4439 if (rc != LDAP_SUCCESS)
4440 DEBUG(5, ("Could not end search properly\n"));
4445 static bool ldapuser2displayentry(struct ldap_search_state *state,
4446 TALLOC_CTX *mem_ctx,
4447 LDAP *ld, LDAPMessage *entry,
4448 struct samr_displayentry *result)
4451 size_t converted_size;
4453 uint32_t acct_flags;
4455 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4456 if ((vals == NULL) || (vals[0] == NULL)) {
4457 acct_flags = ACB_NORMAL;
4459 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4460 ldap_value_free(vals);
4463 if ((state->acct_flags != 0) &&
4464 ((state->acct_flags & acct_flags) == 0))
4467 result->acct_flags = acct_flags;
4468 result->account_name = "";
4469 result->fullname = "";
4470 result->description = "";
4472 vals = ldap_get_values(ld, entry, "uid");
4473 if ((vals == NULL) || (vals[0] == NULL)) {
4474 DEBUG(5, ("\"uid\" not found\n"));
4477 if (!pull_utf8_talloc(mem_ctx,
4478 discard_const_p(char *, &result->account_name),
4479 vals[0], &converted_size))
4481 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4485 ldap_value_free(vals);
4487 vals = ldap_get_values(ld, entry, "displayName");
4488 if ((vals == NULL) || (vals[0] == NULL))
4489 DEBUG(8, ("\"displayName\" not found\n"));
4490 else if (!pull_utf8_talloc(mem_ctx,
4491 discard_const_p(char *, &result->fullname),
4492 vals[0], &converted_size))
4494 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4498 ldap_value_free(vals);
4500 vals = ldap_get_values(ld, entry, "description");
4501 if ((vals == NULL) || (vals[0] == NULL))
4502 DEBUG(8, ("\"description\" not found\n"));
4503 else if (!pull_utf8_talloc(mem_ctx,
4504 discard_const_p(char *, &result->description),
4505 vals[0], &converted_size))
4507 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4511 ldap_value_free(vals);
4513 if ((result->account_name == NULL) ||
4514 (result->fullname == NULL) ||
4515 (result->description == NULL)) {
4516 DEBUG(0, ("talloc failed\n"));
4520 vals = ldap_get_values(ld, entry, "sambaSid");
4521 if ((vals == NULL) || (vals[0] == NULL)) {
4522 DEBUG(0, ("\"objectSid\" not found\n"));
4526 if (!string_to_sid(&sid, vals[0])) {
4527 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4528 ldap_value_free(vals);
4531 ldap_value_free(vals);
4533 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4534 DEBUG(0, ("sid %s does not belong to our domain\n",
4535 sid_string_dbg(&sid)));
4543 static bool ldapsam_search_users(struct pdb_methods *methods,
4544 struct pdb_search *search,
4545 uint32_t acct_flags)
4547 struct ldapsam_privates *ldap_state =
4548 (struct ldapsam_privates *)methods->private_data;
4549 struct ldap_search_state *state;
4551 state = talloc(search, struct ldap_search_state);
4552 if (state == NULL) {
4553 DEBUG(0, ("talloc failed\n"));
4557 state->connection = ldap_state->smbldap_state;
4559 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4560 state->base = lp_ldap_user_suffix(talloc_tos());
4561 else if ((acct_flags != 0) &&
4562 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4563 state->base = lp_ldap_machine_suffix(talloc_tos());
4565 state->base = lp_ldap_suffix(talloc_tos());
4567 state->acct_flags = acct_flags;
4568 state->base = talloc_strdup(search, state->base);
4569 state->scope = LDAP_SCOPE_SUBTREE;
4570 state->filter = get_ldap_filter(search, "*");
4571 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4572 "displayName", "description",
4573 "sambaAcctFlags", NULL);
4574 state->attrsonly = 0;
4575 state->pagedresults_cookie = NULL;
4576 state->entries = NULL;
4577 state->ldap2displayentry = ldapuser2displayentry;
4579 if ((state->filter == NULL) || (state->attrs == NULL)) {
4580 DEBUG(0, ("talloc failed\n"));
4584 search->private_data = state;
4585 search->next_entry = ldapsam_search_next_entry;
4586 search->search_end = ldapsam_search_end;
4588 return ldapsam_search_firstpage(search);
4591 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4592 TALLOC_CTX *mem_ctx,
4593 LDAP *ld, LDAPMessage *entry,
4594 struct samr_displayentry *result)
4597 size_t converted_size;
4599 uint16_t group_type;
4601 result->account_name = "";
4602 result->fullname = "";
4603 result->description = "";
4606 vals = ldap_get_values(ld, entry, "sambaGroupType");
4607 if ((vals == NULL) || (vals[0] == NULL)) {
4608 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4610 ldap_value_free(vals);
4615 group_type = atoi(vals[0]);
4617 if ((state->group_type != 0) &&
4618 ((state->group_type != group_type))) {
4619 ldap_value_free(vals);
4623 ldap_value_free(vals);
4625 /* display name is the NT group name */
4627 vals = ldap_get_values(ld, entry, "displayName");
4628 if ((vals == NULL) || (vals[0] == NULL)) {
4629 DEBUG(8, ("\"displayName\" not found\n"));
4631 /* fallback to the 'cn' attribute */
4632 vals = ldap_get_values(ld, entry, "cn");
4633 if ((vals == NULL) || (vals[0] == NULL)) {
4634 DEBUG(5, ("\"cn\" not found\n"));
4637 if (!pull_utf8_talloc(mem_ctx,
4638 discard_const_p(char *,
4639 &result->account_name),
4640 vals[0], &converted_size))
4642 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4643 "failed: %s", strerror(errno)));
4646 else if (!pull_utf8_talloc(mem_ctx,
4647 discard_const_p(char *,
4648 &result->account_name),
4649 vals[0], &converted_size))
4651 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4655 ldap_value_free(vals);
4657 vals = ldap_get_values(ld, entry, "description");
4658 if ((vals == NULL) || (vals[0] == NULL))
4659 DEBUG(8, ("\"description\" not found\n"));
4660 else if (!pull_utf8_talloc(mem_ctx,
4661 discard_const_p(char *, &result->description),
4662 vals[0], &converted_size))
4664 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4667 ldap_value_free(vals);
4669 if ((result->account_name == NULL) ||
4670 (result->fullname == NULL) ||
4671 (result->description == NULL)) {
4672 DEBUG(0, ("talloc failed\n"));
4676 vals = ldap_get_values(ld, entry, "sambaSid");
4677 if ((vals == NULL) || (vals[0] == NULL)) {
4678 DEBUG(0, ("\"objectSid\" not found\n"));
4680 ldap_value_free(vals);
4685 if (!string_to_sid(&sid, vals[0])) {
4686 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4690 ldap_value_free(vals);
4692 switch (group_type) {
4693 case SID_NAME_DOM_GRP:
4694 case SID_NAME_ALIAS:
4696 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4697 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4699 DEBUG(0, ("%s is not in our domain\n",
4700 sid_string_dbg(&sid)));
4706 DEBUG(0,("unknown group type: %d\n", group_type));
4710 result->acct_flags = 0;
4715 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4716 struct pdb_search *search,
4717 const struct dom_sid *sid,
4718 enum lsa_SidType type)
4720 struct ldapsam_privates *ldap_state =
4721 (struct ldapsam_privates *)methods->private_data;
4722 struct ldap_search_state *state;
4725 state = talloc(search, struct ldap_search_state);
4726 if (state == NULL) {
4727 DEBUG(0, ("talloc failed\n"));
4731 state->connection = ldap_state->smbldap_state;
4733 state->base = lp_ldap_suffix(search);
4734 state->connection = ldap_state->smbldap_state;
4735 state->scope = LDAP_SCOPE_SUBTREE;
4736 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4737 "(sambaGroupType=%d)(sambaSID=%s*))",
4739 type, sid_to_fstring(tmp, sid));
4740 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4741 "displayName", "description",
4742 "sambaGroupType", NULL);
4743 state->attrsonly = 0;
4744 state->pagedresults_cookie = NULL;
4745 state->entries = NULL;
4746 state->group_type = type;
4747 state->ldap2displayentry = ldapgroup2displayentry;
4749 if ((state->filter == NULL) || (state->attrs == NULL)) {
4750 DEBUG(0, ("talloc failed\n"));
4754 search->private_data = state;
4755 search->next_entry = ldapsam_search_next_entry;
4756 search->search_end = ldapsam_search_end;
4758 return ldapsam_search_firstpage(search);
4761 static bool ldapsam_search_groups(struct pdb_methods *methods,
4762 struct pdb_search *search)
4764 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4767 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4768 struct pdb_search *search,
4769 const struct dom_sid *sid)
4771 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4774 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4776 return PDB_CAP_STORE_RIDS;
4779 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4782 struct smbldap_state *smbldap_state = priv->smbldap_state;
4784 LDAPMessage *result = NULL;
4785 LDAPMessage *entry = NULL;
4786 LDAPMod **mods = NULL;
4790 uint32_t nextRid = 0;
4793 TALLOC_CTX *mem_ctx;
4795 mem_ctx = talloc_new(NULL);
4796 if (mem_ctx == NULL) {
4797 DEBUG(0, ("talloc_new failed\n"));
4798 return NT_STATUS_NO_MEMORY;
4801 status = smbldap_search_domain_info(smbldap_state, &result,
4802 get_global_sam_name(), False);
4803 if (!NT_STATUS_IS_OK(status)) {
4804 DEBUG(3, ("Could not get domain info: %s\n",
4805 nt_errstr(status)));
4809 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
4811 entry = ldap_first_entry(priv2ld(priv), result);
4812 if (entry == NULL) {
4813 DEBUG(0, ("Could not get domain info entry\n"));
4814 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4818 /* Find the largest of the three attributes "sambaNextRid",
4819 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4820 concept of differentiating between user and group rids, and will
4821 use only "sambaNextRid" in the future. But for compatibility
4822 reasons I look if others have chosen different strategies -- VL */
4824 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4825 "sambaNextRid", mem_ctx);
4826 if (value != NULL) {
4827 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4828 nextRid = MAX(nextRid, tmp);
4831 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4832 "sambaNextUserRid", mem_ctx);
4833 if (value != NULL) {
4834 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4835 nextRid = MAX(nextRid, tmp);
4838 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4839 "sambaNextGroupRid", mem_ctx);
4840 if (value != NULL) {
4841 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4842 nextRid = MAX(nextRid, tmp);
4846 nextRid = BASE_RID-1;
4851 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4852 talloc_asprintf(mem_ctx, "%d", nextRid));
4853 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
4855 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4856 status = NT_STATUS_NO_MEMORY;
4860 rc = smbldap_modify(smbldap_state, dn, mods);
4862 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4865 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4868 if (NT_STATUS_IS_OK(status)) {
4872 TALLOC_FREE(mem_ctx);
4876 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4880 for (i=0; i<10; i++) {
4881 NTSTATUS result = ldapsam_get_new_rid(
4882 (struct ldapsam_privates *)methods->private_data, rid);
4883 if (NT_STATUS_IS_OK(result)) {
4887 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4891 /* The ldap update failed (maybe a race condition), retry */
4894 /* Tried 10 times, fail. */
4895 return NT_STATUS_ACCESS_DENIED;
4898 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4900 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4901 return NT_STATUS_IS_OK(result) ? True : False;
4904 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4905 const struct dom_sid *sid,
4908 struct ldapsam_privates *priv =
4909 (struct ldapsam_privates *)methods->private_data;
4911 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4913 LDAPMessage *result = NULL;
4914 LDAPMessage *entry = NULL;
4919 TALLOC_CTX *mem_ctx;
4921 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
4926 mem_ctx = talloc_new(NULL);
4927 if (mem_ctx == NULL) {
4928 DEBUG(0, ("talloc_new failed\n"));
4932 filter = talloc_asprintf(mem_ctx,
4934 "(|(objectClass=%s)(objectClass=%s)))",
4935 sid_string_talloc(mem_ctx, sid),
4936 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
4937 if (filter == NULL) {
4938 DEBUG(5, ("talloc_asprintf failed\n"));
4942 rc = smbldap_search_suffix(priv->smbldap_state, filter,
4944 if (rc != LDAP_SUCCESS) {
4947 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
4949 if (ldap_count_entries(priv2ld(priv), result) != 1) {
4950 DEBUG(10, ("Got %d entries, expected one\n",
4951 ldap_count_entries(priv2ld(priv), result)));
4955 entry = ldap_first_entry(priv2ld(priv), result);
4957 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4958 "sambaGroupType", mem_ctx);
4960 if (value != NULL) {
4961 const char *gid_str;
4964 gid_str = smbldap_talloc_single_attribute(
4965 priv2ld(priv), entry, "gidNumber", mem_ctx);
4966 if (gid_str == NULL) {
4967 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4968 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
4973 id->id = strtoul(gid_str, NULL, 10);
4974 id->type = ID_TYPE_GID;
4979 /* It must be a user */
4981 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4982 "uidNumber", mem_ctx);
4983 if (value == NULL) {
4984 DEBUG(1, ("Could not find uidNumber in %s\n",
4985 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
4989 id->id = strtoul(value, NULL, 10);
4990 id->type = ID_TYPE_UID;
4994 TALLOC_FREE(mem_ctx);
4999 * Find the SID for a uid.
5000 * This is shortcut is only used if ldapsam:trusted is set to true.
5002 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5003 struct dom_sid *sid)
5005 struct ldapsam_privates *priv =
5006 (struct ldapsam_privates *)methods->private_data;
5008 const char *attrs[] = { "sambaSID", NULL };
5009 LDAPMessage *result = NULL;
5010 LDAPMessage *entry = NULL;
5012 char *user_sid_string;
5013 struct dom_sid user_sid;
5015 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5017 filter = talloc_asprintf(tmp_ctx,
5020 "(objectClass=%s))",
5022 LDAP_OBJ_POSIXACCOUNT,
5023 LDAP_OBJ_SAMBASAMACCOUNT);
5024 if (filter == NULL) {
5025 DEBUG(3, ("talloc_asprintf failed\n"));
5029 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5030 if (rc != LDAP_SUCCESS) {
5033 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5035 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5036 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5037 ldap_count_entries(priv2ld(priv), result),
5038 (unsigned int)uid));
5042 entry = ldap_first_entry(priv2ld(priv), result);
5044 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5045 "sambaSID", tmp_ctx);
5046 if (user_sid_string == NULL) {
5047 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5048 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5052 if (!string_to_sid(&user_sid, user_sid_string)) {
5053 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5058 sid_copy(sid, &user_sid);
5063 TALLOC_FREE(tmp_ctx);
5068 * Find the SID for a gid.
5069 * This is shortcut is only used if ldapsam:trusted is set to true.
5071 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5072 struct dom_sid *sid)
5074 struct ldapsam_privates *priv =
5075 (struct ldapsam_privates *)methods->private_data;
5077 const char *attrs[] = { "sambaSID", NULL };
5078 LDAPMessage *result = NULL;
5079 LDAPMessage *entry = NULL;
5081 char *group_sid_string;
5082 struct dom_sid group_sid;
5084 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5086 filter = talloc_asprintf(tmp_ctx,
5088 "(objectClass=%s))",
5091 if (filter == NULL) {
5092 DEBUG(3, ("talloc_asprintf failed\n"));
5096 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5097 if (rc != LDAP_SUCCESS) {
5100 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5102 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5103 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5104 ldap_count_entries(priv2ld(priv), result),
5105 (unsigned int)gid));
5109 entry = ldap_first_entry(priv2ld(priv), result);
5111 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5112 "sambaSID", tmp_ctx);
5113 if (group_sid_string == NULL) {
5114 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5115 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5119 if (!string_to_sid(&group_sid, group_sid_string)) {
5120 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5125 sid_copy(sid, &group_sid);
5130 TALLOC_FREE(tmp_ctx);
5134 static bool ldapsam_id_to_sid(struct pdb_methods *methods, struct unixid *id,
5135 struct dom_sid *sid)
5139 return ldapsam_uid_to_sid(methods, id->id, sid);
5142 return ldapsam_gid_to_sid(methods, id->id, sid);
5151 * The following functions are called only if
5152 * ldapsam:trusted and ldapsam:editposix are
5157 * ldapsam_create_user creates a new
5158 * posixAccount and sambaSamAccount object
5159 * in the ldap users subtree
5161 * The uid is allocated by winbindd.
5164 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5165 TALLOC_CTX *tmp_ctx, const char *name,
5166 uint32_t acb_info, uint32_t *rid)
5168 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5169 LDAPMessage *entry = NULL;
5170 LDAPMessage *result = NULL;
5171 uint32_t num_result;
5172 bool is_machine = False;
5173 bool add_posix = False;
5174 bool init_okay = False;
5175 LDAPMod **mods = NULL;
5183 const char *dn = NULL;
5184 struct dom_sid group_sid;
5185 struct dom_sid user_sid;
5191 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5192 acb_info & ACB_WSTRUST ||
5193 acb_info & ACB_SVRTRUST ||
5194 acb_info & ACB_DOMTRUST) {
5198 username = escape_ldap_string(talloc_tos(), name);
5199 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5200 username, LDAP_OBJ_POSIXACCOUNT);
5201 TALLOC_FREE(username);
5203 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5204 if (rc != LDAP_SUCCESS) {
5205 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5206 return NT_STATUS_ACCESS_DENIED;
5208 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5210 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5212 if (num_result > 1) {
5213 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5214 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5217 if (num_result == 1) {
5219 /* check if it is just a posix account.
5220 * or if there is a sid attached to this entry
5223 entry = ldap_first_entry(priv2ld(ldap_state), result);
5225 return NT_STATUS_UNSUCCESSFUL;
5228 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5230 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5231 return NT_STATUS_USER_EXISTS;
5234 /* it is just a posix account, retrieve the dn for later use */
5235 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5237 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5238 return NT_STATUS_NO_MEMORY;
5242 if (num_result == 0) {
5246 /* Create the basic samu structure and generate the mods for the ldap commit */
5247 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5248 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5252 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5254 user = samu_new(tmp_ctx);
5256 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5257 return NT_STATUS_NO_MEMORY;
5260 if (!pdb_set_username(user, name, PDB_SET)) {
5261 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5262 return NT_STATUS_UNSUCCESSFUL;
5264 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5265 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5266 return NT_STATUS_UNSUCCESSFUL;
5269 if (acb_info & ACB_NORMAL) {
5270 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5271 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5272 return NT_STATUS_UNSUCCESSFUL;
5275 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5276 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5277 return NT_STATUS_UNSUCCESSFUL;
5281 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5282 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5283 return NT_STATUS_UNSUCCESSFUL;
5287 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5288 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5289 return NT_STATUS_UNSUCCESSFUL;
5292 init_okay = init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed);
5295 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5296 ldap_mods_free(mods, true);
5297 return NT_STATUS_UNSUCCESSFUL;
5300 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5301 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5303 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5308 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5310 /* retrieve the Domain Users group gid */
5311 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5312 !sid_to_gid(&group_sid, &gid)) {
5313 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5314 ldap_mods_free(mods, true);
5315 return NT_STATUS_INVALID_PRIMARY_GROUP;
5318 /* lets allocate a new userid for this user */
5319 if (!winbind_allocate_uid(&uid)) {
5320 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5321 ldap_mods_free(mods, true);
5322 return NT_STATUS_UNSUCCESSFUL;
5327 /* TODO: choose a more appropriate default for machines */
5328 homedir = talloc_sub_specified(tmp_ctx,
5329 lp_template_homedir(),
5330 "SMB_workstations_home",
5332 ldap_state->domain_name,
5335 shell = talloc_strdup(tmp_ctx, "/bin/false");
5337 homedir = talloc_sub_specified(tmp_ctx,
5338 lp_template_homedir(),
5341 ldap_state->domain_name,
5344 shell = talloc_sub_specified(tmp_ctx,
5345 lp_template_shell(),
5348 ldap_state->domain_name,
5352 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5353 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5355 escape_name = escape_rdn_val_string_alloc(name);
5357 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5358 ldap_mods_free(mods, true);
5359 return NT_STATUS_NO_MEMORY;
5363 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix (talloc_tos()));
5365 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix (talloc_tos()));
5368 SAFE_FREE(escape_name);
5370 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5371 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5372 ldap_mods_free(mods, true);
5373 return NT_STATUS_NO_MEMORY;
5376 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5377 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5378 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5379 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5380 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5381 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5382 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5386 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5388 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5391 ldap_mods_free(mods, true);
5393 if (rc != LDAP_SUCCESS) {
5394 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5395 return NT_STATUS_UNSUCCESSFUL;
5398 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5400 flush_pwnam_cache();
5402 return NT_STATUS_OK;
5405 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5407 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5408 LDAPMessage *result = NULL;
5409 LDAPMessage *entry = NULL;
5415 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5417 filter = talloc_asprintf(tmp_ctx,
5420 "(objectClass=%s))",
5421 pdb_get_username(sam_acct),
5422 LDAP_OBJ_POSIXACCOUNT,
5423 LDAP_OBJ_SAMBASAMACCOUNT);
5424 if (filter == NULL) {
5425 return NT_STATUS_NO_MEMORY;
5428 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5429 if (rc != LDAP_SUCCESS) {
5430 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5431 return NT_STATUS_UNSUCCESSFUL;
5433 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5435 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5437 if (num_result == 0) {
5438 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5439 return NT_STATUS_NO_SUCH_USER;
5442 if (num_result > 1) {
5443 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5444 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5447 entry = ldap_first_entry(priv2ld(ldap_state), result);
5449 return NT_STATUS_UNSUCCESSFUL;
5452 /* it is just a posix account, retrieve the dn for later use */
5453 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5455 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5456 return NT_STATUS_NO_MEMORY;
5459 /* try to remove memberships first */
5462 struct dom_sid *sids = NULL;
5464 uint32_t num_groups = 0;
5466 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5468 status = ldapsam_enum_group_memberships(my_methods,
5474 if (!NT_STATUS_IS_OK(status)) {
5478 for (i=0; i < num_groups; i++) {
5482 sid_peek_rid(&sids[i], &group_rid);
5484 ldapsam_del_groupmem(my_methods,
5493 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5494 if (rc != LDAP_SUCCESS) {
5495 return NT_STATUS_UNSUCCESSFUL;
5498 flush_pwnam_cache();
5500 return NT_STATUS_OK;
5504 * ldapsam_create_group creates a new
5505 * posixGroup and sambaGroupMapping object
5506 * in the ldap groups subtree
5508 * The gid is allocated by winbindd.
5511 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5512 TALLOC_CTX *tmp_ctx,
5516 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5518 LDAPMessage *entry = NULL;
5519 LDAPMessage *result = NULL;
5520 uint32_t num_result;
5521 bool is_new_entry = False;
5522 LDAPMod **mods = NULL;
5528 const char *dn = NULL;
5529 struct dom_sid group_sid;
5533 groupname = escape_ldap_string(talloc_tos(), name);
5534 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5535 groupname, LDAP_OBJ_POSIXGROUP);
5536 TALLOC_FREE(groupname);
5538 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5539 if (rc != LDAP_SUCCESS) {
5540 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5541 return NT_STATUS_UNSUCCESSFUL;
5543 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5545 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5547 if (num_result > 1) {
5548 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5549 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5552 if (num_result == 1) {
5554 /* check if it is just a posix group.
5555 * or if there is a sid attached to this entry
5558 entry = ldap_first_entry(priv2ld(ldap_state), result);
5560 return NT_STATUS_UNSUCCESSFUL;
5563 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5565 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5566 return NT_STATUS_GROUP_EXISTS;
5569 /* it is just a posix group, retrieve the gid and the dn for later use */
5570 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5572 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5573 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5576 gid = strtoul(tmp, NULL, 10);
5578 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5580 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5581 return NT_STATUS_NO_MEMORY;
5585 if (num_result == 0) {
5586 is_new_entry = true;
5589 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5590 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5594 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5596 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5598 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5600 if (!groupsidstr || !grouptype) {
5601 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5602 return NT_STATUS_NO_MEMORY;
5605 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5606 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5607 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5608 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5613 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5615 /* lets allocate a new groupid for this group */
5616 if (!winbind_allocate_gid(&gid)) {
5617 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5618 return NT_STATUS_UNSUCCESSFUL;
5621 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5623 escape_name = escape_rdn_val_string_alloc(name);
5625 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5626 return NT_STATUS_NO_MEMORY;
5629 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix(talloc_tos()));
5631 SAFE_FREE(escape_name);
5633 if (!gidstr || !dn) {
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_POSIXGROUP);
5639 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5640 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5643 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
5646 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5648 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5649 /* This call may fail with rfc2307bis schema */
5650 /* Retry adding a structural class */
5651 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5652 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5656 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5659 if (rc != LDAP_SUCCESS) {
5660 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5661 return NT_STATUS_UNSUCCESSFUL;
5664 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5666 return NT_STATUS_OK;
5669 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5671 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5672 LDAPMessage *result = NULL;
5673 LDAPMessage *entry = NULL;
5678 struct dom_sid group_sid;
5681 /* get the group sid */
5682 sid_compose(&group_sid, get_global_sam_sid(), rid);
5684 filter = talloc_asprintf(tmp_ctx,
5687 "(objectClass=%s))",
5688 sid_string_talloc(tmp_ctx, &group_sid),
5689 LDAP_OBJ_POSIXGROUP,
5691 if (filter == NULL) {
5692 return NT_STATUS_NO_MEMORY;
5695 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5696 if (rc != LDAP_SUCCESS) {
5697 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5698 return NT_STATUS_UNSUCCESSFUL;
5700 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5702 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5704 if (num_result == 0) {
5705 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5706 return NT_STATUS_NO_SUCH_GROUP;
5709 if (num_result > 1) {
5710 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5711 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5714 entry = ldap_first_entry(priv2ld(ldap_state), result);
5716 return NT_STATUS_UNSUCCESSFUL;
5719 /* here it is, retrieve the dn for later use */
5720 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5722 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5723 return NT_STATUS_NO_MEMORY;
5726 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5728 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5729 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5732 /* check no user have this group marked as primary group */
5733 filter = talloc_asprintf(tmp_ctx,
5736 "(objectClass=%s))",
5738 LDAP_OBJ_POSIXACCOUNT,
5739 LDAP_OBJ_SAMBASAMACCOUNT);
5741 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5742 if (rc != LDAP_SUCCESS) {
5743 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5744 return NT_STATUS_UNSUCCESSFUL;
5746 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5748 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5750 if (num_result != 0) {
5751 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5752 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5755 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5756 if (rc != LDAP_SUCCESS) {
5757 return NT_STATUS_UNSUCCESSFUL;
5760 return NT_STATUS_OK;
5763 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5764 TALLOC_CTX *tmp_ctx,
5766 uint32_t member_rid,
5769 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5770 LDAPMessage *entry = NULL;
5771 LDAPMessage *result = NULL;
5772 uint32_t num_result;
5773 LDAPMod **mods = NULL;
5776 const char *dn = NULL;
5777 struct dom_sid group_sid;
5778 struct dom_sid member_sid;
5783 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5785 case LDAP_MOD_DELETE:
5786 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5789 return NT_STATUS_UNSUCCESSFUL;
5792 /* get member sid */
5793 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5795 /* get the group sid */
5796 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5798 filter = talloc_asprintf(tmp_ctx,
5801 "(objectClass=%s))",
5802 sid_string_talloc(tmp_ctx, &member_sid),
5803 LDAP_OBJ_POSIXACCOUNT,
5804 LDAP_OBJ_SAMBASAMACCOUNT);
5805 if (filter == NULL) {
5806 return NT_STATUS_NO_MEMORY;
5809 /* get the member uid */
5810 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5811 if (rc != LDAP_SUCCESS) {
5812 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5813 return NT_STATUS_UNSUCCESSFUL;
5815 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5817 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5819 if (num_result == 0) {
5820 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5821 return NT_STATUS_NO_SUCH_MEMBER;
5824 if (num_result > 1) {
5825 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5826 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5829 entry = ldap_first_entry(priv2ld(ldap_state), result);
5831 return NT_STATUS_UNSUCCESSFUL;
5834 if (modop == LDAP_MOD_DELETE) {
5835 /* check if we are trying to remove the member from his primary group */
5837 gid_t user_gid, group_gid;
5839 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5841 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5842 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5845 user_gid = strtoul(gidstr, NULL, 10);
5847 if (!sid_to_gid(&group_sid, &group_gid)) {
5848 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5849 return NT_STATUS_UNSUCCESSFUL;
5852 if (user_gid == group_gid) {
5853 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5854 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5858 /* here it is, retrieve the uid for later use */
5859 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5861 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5862 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5865 filter = talloc_asprintf(tmp_ctx,
5868 "(objectClass=%s))",
5869 sid_string_talloc(tmp_ctx, &group_sid),
5870 LDAP_OBJ_POSIXGROUP,
5874 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5875 if (rc != LDAP_SUCCESS) {
5876 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5877 return NT_STATUS_UNSUCCESSFUL;
5879 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5881 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5883 if (num_result == 0) {
5884 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5885 return NT_STATUS_NO_SUCH_GROUP;
5888 if (num_result > 1) {
5889 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5890 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5893 entry = ldap_first_entry(priv2ld(ldap_state), result);
5895 return NT_STATUS_UNSUCCESSFUL;
5898 /* here it is, retrieve the dn for later use */
5899 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5901 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5902 return NT_STATUS_NO_MEMORY;
5905 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5907 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
5909 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5910 if (rc != LDAP_SUCCESS) {
5911 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5912 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5913 return NT_STATUS_MEMBER_IN_GROUP;
5915 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5916 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5917 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5919 return NT_STATUS_UNSUCCESSFUL;
5922 return NT_STATUS_OK;
5925 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5926 TALLOC_CTX *tmp_ctx,
5928 uint32_t member_rid)
5930 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5932 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5933 TALLOC_CTX *tmp_ctx,
5935 uint32_t member_rid)
5937 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5940 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5941 TALLOC_CTX *mem_ctx,
5942 struct samu *sampass)
5944 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5945 LDAPMessage *entry = NULL;
5946 LDAPMessage *result = NULL;
5947 uint32_t num_result;
5948 LDAPMod **mods = NULL;
5950 char *escape_username;
5952 const char *dn = NULL;
5956 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5958 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5959 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5960 return NT_STATUS_UNSUCCESSFUL;
5962 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
5964 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5965 return NT_STATUS_NO_MEMORY;
5968 escape_username = escape_ldap_string(talloc_tos(),
5969 pdb_get_username(sampass));
5970 if (escape_username== NULL) {
5971 return NT_STATUS_NO_MEMORY;
5974 filter = talloc_asprintf(mem_ctx,
5977 "(objectClass=%s))",
5979 LDAP_OBJ_POSIXACCOUNT,
5980 LDAP_OBJ_SAMBASAMACCOUNT);
5982 TALLOC_FREE(escape_username);
5984 if (filter == NULL) {
5985 return NT_STATUS_NO_MEMORY;
5988 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5989 if (rc != LDAP_SUCCESS) {
5990 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5991 return NT_STATUS_UNSUCCESSFUL;
5993 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
5995 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5997 if (num_result == 0) {
5998 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5999 return NT_STATUS_NO_SUCH_USER;
6002 if (num_result > 1) {
6003 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6004 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6007 entry = ldap_first_entry(priv2ld(ldap_state), result);
6009 return NT_STATUS_UNSUCCESSFUL;
6012 /* retrieve the dn for later use */
6013 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6015 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6016 return NT_STATUS_NO_MEMORY;
6019 /* remove the old one, and add the new one, this way we do not risk races */
6020 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6023 return NT_STATUS_OK;
6026 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6028 if (rc != LDAP_SUCCESS) {
6029 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6030 pdb_get_username(sampass), gidstr));
6031 return NT_STATUS_UNSUCCESSFUL;
6034 flush_pwnam_cache();
6036 return NT_STATUS_OK;
6040 /**********************************************************************
6041 trusted domains functions
6042 *********************************************************************/
6044 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6047 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6048 ldap_state->domain_dn);
6051 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6052 TALLOC_CTX *mem_ctx,
6053 const char *domain, LDAPMessage **entry)
6057 int scope = LDAP_SCOPE_SUBTREE;
6058 const char **attrs = NULL; /* NULL: get all attrs */
6059 int attrsonly = 0; /* 0: return values too */
6060 LDAPMessage *result = NULL;
6062 uint32_t num_result;
6064 filter = talloc_asprintf(talloc_tos(),
6065 "(&(objectClass=%s)(sambaDomainName=%s))",
6066 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6068 trusted_dn = trusteddom_dn(ldap_state, domain);
6069 if (trusted_dn == NULL) {
6072 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6073 filter, attrs, attrsonly, &result);
6075 if (result != NULL) {
6076 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6079 if (rc == LDAP_NO_SUCH_OBJECT) {
6084 if (rc != LDAP_SUCCESS) {
6088 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6090 if (num_result > 1) {
6091 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6092 "%s object for domain '%s'?!\n",
6093 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6097 if (num_result == 0) {
6098 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6099 "%s object for domain %s.\n",
6100 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6103 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6109 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6112 struct dom_sid *sid,
6113 time_t *pass_last_set_time)
6115 struct ldapsam_privates *ldap_state =
6116 (struct ldapsam_privates *)methods->private_data;
6117 LDAPMessage *entry = NULL;
6119 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6121 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6130 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6131 entry, "sambaClearTextPassword", talloc_tos());
6132 if (pwd_str == NULL) {
6135 /* trusteddom_pw routines do not use talloc yet... */
6136 *pwd = SMB_STRDUP(pwd_str);
6142 /* last change time */
6143 if (pass_last_set_time != NULL) {
6145 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6146 entry, "sambaPwdLastSet", talloc_tos());
6147 if (time_str == NULL) {
6150 *pass_last_set_time = (time_t)atol(time_str);
6156 struct dom_sid dom_sid;
6157 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6160 if (sid_str == NULL) {
6163 if (!string_to_sid(&dom_sid, sid_str)) {
6166 sid_copy(sid, &dom_sid);
6172 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6175 const struct dom_sid *sid)
6177 struct ldapsam_privates *ldap_state =
6178 (struct ldapsam_privates *)methods->private_data;
6179 LDAPMessage *entry = NULL;
6180 LDAPMod **mods = NULL;
6181 char *prev_pwd = NULL;
6182 char *trusted_dn = NULL;
6185 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6188 * get the current entry (if there is one) in order to put the
6189 * current password into the previous password attribute
6191 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6196 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6197 LDAP_OBJ_TRUSTDOM_PASSWORD);
6198 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6200 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6201 sid_string_tos(sid));
6202 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6203 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6204 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6205 "sambaClearTextPassword", pwd);
6207 if (entry != NULL) {
6208 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6209 entry, "sambaClearTextPassword", talloc_tos());
6210 if (prev_pwd != NULL) {
6211 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6212 "sambaPreviousClearTextPassword",
6217 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods);
6219 trusted_dn = trusteddom_dn(ldap_state, domain);
6220 if (trusted_dn == NULL) {
6223 if (entry == NULL) {
6224 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6226 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6229 if (rc != LDAP_SUCCESS) {
6230 DEBUG(1, ("error writing trusted domain password!\n"));
6237 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6241 struct ldapsam_privates *ldap_state =
6242 (struct ldapsam_privates *)methods->private_data;
6243 LDAPMessage *entry = NULL;
6244 const char *trusted_dn;
6246 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6250 if (entry == NULL) {
6251 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6256 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6258 if (trusted_dn == NULL) {
6259 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6263 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6264 if (rc != LDAP_SUCCESS) {
6271 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6272 TALLOC_CTX *mem_ctx,
6273 uint32_t *num_domains,
6274 struct trustdom_info ***domains)
6277 struct ldapsam_privates *ldap_state =
6278 (struct ldapsam_privates *)methods->private_data;
6280 int scope = LDAP_SCOPE_SUBTREE;
6281 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6282 int attrsonly = 0; /* 0: return values too */
6283 LDAPMessage *result = NULL;
6284 LDAPMessage *entry = NULL;
6286 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6287 LDAP_OBJ_TRUSTDOM_PASSWORD);
6289 rc = smbldap_search(ldap_state->smbldap_state,
6290 ldap_state->domain_dn,
6297 if (result != NULL) {
6298 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6301 if (rc != LDAP_SUCCESS) {
6302 return NT_STATUS_UNSUCCESSFUL;
6306 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6307 DEBUG(1, ("talloc failed\n"));
6308 return NT_STATUS_NO_MEMORY;
6311 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6313 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6315 char *dom_name, *dom_sid_str;
6316 struct trustdom_info *dom_info;
6318 dom_info = talloc(*domains, struct trustdom_info);
6319 if (dom_info == NULL) {
6320 DEBUG(1, ("talloc failed\n"));
6321 return NT_STATUS_NO_MEMORY;
6324 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6328 if (dom_name == NULL) {
6329 DEBUG(1, ("talloc failed\n"));
6330 return NT_STATUS_NO_MEMORY;
6332 dom_info->name = dom_name;
6334 dom_sid_str = smbldap_talloc_single_attribute(
6335 priv2ld(ldap_state), entry, "sambaSID",
6337 if (dom_sid_str == NULL) {
6338 DEBUG(1, ("talloc failed\n"));
6339 return NT_STATUS_NO_MEMORY;
6341 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6342 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6344 return NT_STATUS_UNSUCCESSFUL;
6347 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6348 domains, num_domains);
6350 if (*domains == NULL) {
6351 DEBUG(1, ("talloc failed\n"));
6352 return NT_STATUS_NO_MEMORY;
6356 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6357 return NT_STATUS_OK;
6361 /**********************************************************************
6363 *********************************************************************/
6365 static void free_private_data(void **vp)
6367 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6369 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6371 if ((*ldap_state)->result != NULL) {
6372 ldap_msgfree((*ldap_state)->result);
6373 (*ldap_state)->result = NULL;
6375 if ((*ldap_state)->domain_dn != NULL) {
6376 SAFE_FREE((*ldap_state)->domain_dn);
6381 /* No need to free any further, as it is talloc()ed */
6384 /*********************************************************************
6385 Intitalise the parts of the pdb_methods structure that are common to
6387 *********************************************************************/
6389 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6392 struct ldapsam_privates *ldap_state;
6393 char *bind_dn = NULL;
6394 char *bind_secret = NULL;
6396 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6400 (*pdb_method)->name = "ldapsam";
6402 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6403 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6404 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6405 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6406 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6407 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6409 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6410 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6411 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6412 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6413 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6414 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6415 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6417 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6418 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6420 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6422 (*pdb_method)->capabilities = ldapsam_capabilities;
6423 (*pdb_method)->new_rid = ldapsam_new_rid;
6425 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6426 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6427 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6428 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6430 /* TODO: Setup private data and free */
6432 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6433 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6434 return NT_STATUS_NO_MEMORY;
6437 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6438 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6439 return NT_STATUS_NO_MEMORY;
6442 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6443 location, false, bind_dn, bind_secret,
6444 &ldap_state->smbldap_state);
6445 memset(bind_secret, '\0', strlen(bind_secret));
6446 SAFE_FREE(bind_secret);
6448 if ( !NT_STATUS_IS_OK(nt_status) ) {
6452 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6453 return NT_STATUS_NO_MEMORY;
6456 (*pdb_method)->private_data = ldap_state;
6458 (*pdb_method)->free_private_data = free_private_data;
6460 return NT_STATUS_OK;
6463 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods *m)
6468 /**********************************************************************
6469 Initialise the normal mode for pdb_ldap
6470 *********************************************************************/
6472 NTSTATUS pdb_ldapsam_init_common(struct pdb_methods **pdb_method,
6473 const char *location)
6476 struct ldapsam_privates *ldap_state = NULL;
6477 uint32_t alg_rid_base;
6478 char *alg_rid_base_string = NULL;
6479 LDAPMessage *result = NULL;
6480 LDAPMessage *entry = NULL;
6481 struct dom_sid ldap_domain_sid;
6482 struct dom_sid secrets_domain_sid;
6483 char *domain_sid_string = NULL;
6485 char *uri = talloc_strdup( NULL, location );
6487 trim_char( uri, '\"', '\"' );
6488 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6492 if (!NT_STATUS_IS_OK(nt_status)) {
6496 (*pdb_method)->name = "ldapsam";
6498 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6499 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6500 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6501 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6502 (*pdb_method)->search_users = ldapsam_search_users;
6503 (*pdb_method)->search_groups = ldapsam_search_groups;
6504 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6505 (*pdb_method)->is_responsible_for_wellknown =
6506 ldapsam_is_responsible_for_wellknown;
6508 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6509 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6510 (*pdb_method)->enum_group_memberships =
6511 ldapsam_enum_group_memberships;
6512 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6513 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6514 (*pdb_method)->id_to_sid = ldapsam_id_to_sid;
6516 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6517 (*pdb_method)->create_user = ldapsam_create_user;
6518 (*pdb_method)->delete_user = ldapsam_delete_user;
6519 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6520 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6521 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6522 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6523 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6527 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6528 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6530 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6532 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6534 ldap_state->domain_name, True);
6536 if ( !NT_STATUS_IS_OK(nt_status) ) {
6537 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6538 "info, nor add one to the domain. "
6539 "We cannot work reliably without it.\n"));
6540 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6543 /* Given that the above might fail, everything below this must be
6546 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6549 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6551 ldap_msgfree(result);
6552 return NT_STATUS_UNSUCCESSFUL;
6555 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6557 ldap_msgfree(result);
6558 return NT_STATUS_UNSUCCESSFUL;
6561 ldap_state->domain_dn = smb_xstrdup(dn);
6564 domain_sid_string = smbldap_talloc_single_attribute(
6565 ldap_state->smbldap_state->ldap_struct,
6567 get_userattr_key2string(ldap_state->schema_ver,
6568 LDAP_ATTR_USER_SID),
6571 if (domain_sid_string) {
6573 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6574 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6575 "read as a valid SID\n", domain_sid_string));
6576 ldap_msgfree(result);
6577 TALLOC_FREE(domain_sid_string);
6578 return NT_STATUS_INVALID_PARAMETER;
6580 found_sid = PDB_secrets_fetch_domain_sid(ldap_state->domain_name,
6581 &secrets_domain_sid);
6582 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6583 &ldap_domain_sid)) {
6584 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6585 "%s based on pdb_ldap results %s -> %s\n",
6586 ldap_state->domain_name,
6587 sid_string_dbg(&secrets_domain_sid),
6588 sid_string_dbg(&ldap_domain_sid)));
6590 /* reset secrets.tdb sid */
6591 PDB_secrets_store_domain_sid(ldap_state->domain_name,
6593 DEBUG(1, ("New global sam SID: %s\n",
6594 sid_string_dbg(get_global_sam_sid())));
6596 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6597 TALLOC_FREE(domain_sid_string);
6600 alg_rid_base_string = smbldap_talloc_single_attribute(
6601 ldap_state->smbldap_state->ldap_struct,
6603 get_attr_key2string( dominfo_attr_list,
6604 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6606 if (alg_rid_base_string) {
6607 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6608 if (alg_rid_base != algorithmic_rid_base()) {
6609 DEBUG(0, ("The value of 'algorithmic RID base' has "
6610 "changed since the LDAP\n"
6611 "database was initialised. Aborting. \n"));
6612 ldap_msgfree(result);
6613 TALLOC_FREE(alg_rid_base_string);
6614 return NT_STATUS_UNSUCCESSFUL;
6616 TALLOC_FREE(alg_rid_base_string);
6618 ldap_msgfree(result);
6620 return NT_STATUS_OK;
6623 NTSTATUS pdb_ldapsam_init(void)
6627 nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION,
6629 pdb_ldapsam_init_common);
6630 if (!NT_STATUS_IS_OK(nt_status)) {
6634 /* Let pdb_nds register backends */
6637 return NT_STATUS_OK;