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 smbldap_get_ldap(priv->smbldap_state);
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(
195 smbldap_get_ldap(ldap_state->smbldap_state),
196 lp_ldap_suffix(talloc_tos()))) {
197 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
198 "as top-level namingContext\n", lp_ldap_suffix(talloc_tos())));
202 mem_ctx = talloc_init("ldapsam_get_seq_num");
205 return NT_STATUS_NO_MEMORY;
207 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
208 ntstatus = NT_STATUS_NO_MEMORY;
212 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
213 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
216 /* consumer syncreplCookie: */
217 /* csn=20050126161620Z#0000001#00#00000 */
218 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
220 suffix = talloc_asprintf(mem_ctx,
221 "cn=syncrepl%d,%s", rid, lp_ldap_suffix(talloc_tos()));
223 ntstatus = NT_STATUS_NO_MEMORY;
228 /* provider contextCSN */
229 /* 20050126161620Z#000009#00#000000 */
230 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
232 suffix = talloc_asprintf(mem_ctx,
233 "cn=ldapsync,%s", lp_ldap_suffix(talloc_tos()));
236 ntstatus = NT_STATUS_NO_MEMORY;
241 rc = smbldap_search(ldap_state->smbldap_state, suffix,
242 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
244 if (rc != LDAP_SUCCESS) {
248 num_result = ldap_count_entries(
249 smbldap_get_ldap(ldap_state->smbldap_state), msg);
250 if (num_result != 1) {
251 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
255 entry = ldap_first_entry(
256 smbldap_get_ldap(ldap_state->smbldap_state), msg);
258 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
262 values = ldap_get_values(
263 smbldap_get_ldap(ldap_state->smbldap_state), entry, attrs[0]);
264 if (values == NULL) {
265 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
269 num_values = ldap_count_values(values);
270 if (num_values == 0) {
271 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
276 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
277 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
282 if (!strncmp(p, "csn=", strlen("csn=")))
285 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
287 *seq_num = generalized_to_unix_time(p);
289 /* very basic sanity check */
291 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
296 ntstatus = NT_STATUS_OK;
300 ldap_value_free(values);
304 talloc_destroy(mem_ctx);
309 /*******************************************************************
310 Run the search by name.
311 ******************************************************************/
313 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
315 LDAPMessage ** result,
319 char *escape_user = escape_ldap_string(talloc_tos(), user);
323 return LDAP_NO_MEMORY;
327 * in the filter expression, replace %u with the real name
328 * so in ldap filter, %u MUST exist :-)
330 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
331 get_objclass_filter(ldap_state->schema_ver));
333 TALLOC_FREE(escape_user);
334 return LDAP_NO_MEMORY;
337 * have to use this here because $ is filtered out
341 filter = talloc_all_string_sub(talloc_tos(),
342 filter, "%u", escape_user);
343 TALLOC_FREE(escape_user);
345 return LDAP_NO_MEMORY;
348 ret = smbldap_search_suffix(ldap_state->smbldap_state,
349 filter, attr, result);
354 /*******************************************************************
355 Run the search by SID.
356 ******************************************************************/
358 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
359 const struct dom_sid *sid, LDAPMessage ** result,
366 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
367 get_userattr_key2string(ldap_state->schema_ver,
369 sid_to_fstring(sid_string, sid),
370 get_objclass_filter(ldap_state->schema_ver));
372 return LDAP_NO_MEMORY;
375 rc = smbldap_search_suffix(ldap_state->smbldap_state,
376 filter, attr, result);
382 /*******************************************************************
383 Delete complete object or objectclass and attrs from
384 object found in search_result depending on lp_ldap_delete_dn
385 ******************************************************************/
387 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
390 const char *objectclass,
393 LDAPMod **mods = NULL;
396 BerElement *ptr = NULL;
398 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
400 return LDAP_NO_MEMORY;
403 if (lp_ldap_delete_dn()) {
404 return smbldap_delete(priv->smbldap_state, dn);
407 /* Ok, delete only the SAM attributes */
409 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
411 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
414 /* We are only allowed to delete the attributes that
417 for (attrib = attrs; *attrib != NULL; attrib++) {
418 if (strequal(*attrib, name)) {
419 DEBUG(10, ("ldapsam_delete_entry: deleting "
420 "attribute %s\n", name));
421 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
432 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
433 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
435 return smbldap_modify(priv->smbldap_state, dn, mods);
438 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
443 temp = smbldap_talloc_single_attribute(
444 smbldap_get_ldap(ldap_state->smbldap_state), entry,
445 get_userattr_key2string(ldap_state->schema_ver,
446 LDAP_ATTR_MOD_TIMESTAMP),
452 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
453 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
463 /**********************************************************************
464 Initialize struct samu from an LDAP query.
465 (Based on init_sam_from_buffer in pdb_tdb.c)
466 *********************************************************************/
468 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
469 struct samu * sampass,
476 pass_can_change_time,
479 char *username = NULL,
485 *logon_script = NULL,
486 *profile_path = NULL,
488 *workstations = NULL,
491 uint8_t smblmpwd[LM_HASH_LEN],
492 smbntpwd[NT_HASH_LEN];
493 bool use_samba_attrs = True;
494 uint32_t acct_ctrl = 0;
496 uint16_t bad_password_count = 0,
499 uint8_t hours[MAX_HOURS_LEN];
501 struct login_cache cache_entry;
503 bool expand_explicit = lp_passdb_expand_explicit();
505 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
510 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
511 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
515 if (priv2ld(ldap_state) == NULL) {
516 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
517 "ldap_struct is NULL!\n"));
521 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
525 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
530 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
532 nt_username = talloc_strdup(ctx, username);
537 domain = talloc_strdup(ctx, ldap_state->domain_name);
542 pdb_set_username(sampass, username, PDB_SET);
544 pdb_set_domain(sampass, domain, PDB_DEFAULT);
545 pdb_set_nt_username(sampass, nt_username, PDB_SET);
547 /* deal with different attributes between the schema first */
549 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
550 if ((temp = smbldap_talloc_single_attribute(
551 smbldap_get_ldap(ldap_state->smbldap_state),
553 get_userattr_key2string(ldap_state->schema_ver,
556 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
559 if ((temp = smbldap_talloc_single_attribute(
560 smbldap_get_ldap(ldap_state->smbldap_state),
562 get_userattr_key2string(ldap_state->schema_ver,
565 user_rid = (uint32_t)atol(temp);
566 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
570 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
571 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
572 get_userattr_key2string(ldap_state->schema_ver,
574 get_userattr_key2string(ldap_state->schema_ver,
580 temp = smbldap_talloc_single_attribute(
581 smbldap_get_ldap(ldap_state->smbldap_state),
583 get_userattr_key2string(ldap_state->schema_ver,
584 LDAP_ATTR_PWD_LAST_SET),
587 pass_last_set_time = (time_t) atol(temp);
588 pdb_set_pass_last_set_time(sampass,
589 pass_last_set_time, PDB_SET);
592 temp = smbldap_talloc_single_attribute(
593 smbldap_get_ldap(ldap_state->smbldap_state),
595 get_userattr_key2string(ldap_state->schema_ver,
596 LDAP_ATTR_LOGON_TIME),
599 logon_time = (time_t) atol(temp);
600 pdb_set_logon_time(sampass, logon_time, PDB_SET);
603 temp = smbldap_talloc_single_attribute(
604 smbldap_get_ldap(ldap_state->smbldap_state),
606 get_userattr_key2string(ldap_state->schema_ver,
607 LDAP_ATTR_LOGOFF_TIME),
610 logoff_time = (time_t) atol(temp);
611 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
614 temp = smbldap_talloc_single_attribute(
615 smbldap_get_ldap(ldap_state->smbldap_state),
617 get_userattr_key2string(ldap_state->schema_ver,
618 LDAP_ATTR_KICKOFF_TIME),
621 kickoff_time = (time_t) atol(temp);
622 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
625 temp = smbldap_talloc_single_attribute(
626 smbldap_get_ldap(ldap_state->smbldap_state),
628 get_userattr_key2string(ldap_state->schema_ver,
629 LDAP_ATTR_PWD_CAN_CHANGE),
632 pass_can_change_time = (time_t) atol(temp);
633 pdb_set_pass_can_change_time(sampass,
634 pass_can_change_time, PDB_SET);
637 /* recommend that 'gecos' and 'displayName' should refer to the same
638 * attribute OID. userFullName depreciated, only used by Samba
639 * primary rules of LDAP: don't make a new attribute when one is already defined
640 * that fits your needs; using cn then displayName rather than 'userFullName'
643 fullname = smbldap_talloc_single_attribute(
644 smbldap_get_ldap(ldap_state->smbldap_state),
646 get_userattr_key2string(ldap_state->schema_ver,
647 LDAP_ATTR_DISPLAY_NAME),
650 pdb_set_fullname(sampass, fullname, PDB_SET);
652 fullname = smbldap_talloc_single_attribute(
653 smbldap_get_ldap(ldap_state->smbldap_state),
655 get_userattr_key2string(ldap_state->schema_ver,
659 pdb_set_fullname(sampass, fullname, PDB_SET);
663 dir_drive = smbldap_talloc_single_attribute(
664 smbldap_get_ldap(ldap_state->smbldap_state),
666 get_userattr_key2string(ldap_state->schema_ver,
667 LDAP_ATTR_HOME_DRIVE),
670 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
672 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
675 homedir = smbldap_talloc_single_attribute(
676 smbldap_get_ldap(ldap_state->smbldap_state),
678 get_userattr_key2string(ldap_state->schema_ver,
679 LDAP_ATTR_HOME_PATH),
682 if (expand_explicit) {
683 homedir = talloc_sub_basic(ctx,
691 pdb_set_homedir(sampass, homedir, PDB_SET);
693 pdb_set_homedir(sampass,
694 talloc_sub_basic(ctx, username, domain,
699 logon_script = smbldap_talloc_single_attribute(
700 smbldap_get_ldap(ldap_state->smbldap_state),
702 get_userattr_key2string(ldap_state->schema_ver,
703 LDAP_ATTR_LOGON_SCRIPT),
706 if (expand_explicit) {
707 logon_script = talloc_sub_basic(ctx,
715 pdb_set_logon_script(sampass, logon_script, PDB_SET);
717 pdb_set_logon_script(sampass,
718 talloc_sub_basic(ctx, username, domain,
723 profile_path = smbldap_talloc_single_attribute(
724 smbldap_get_ldap(ldap_state->smbldap_state),
726 get_userattr_key2string(ldap_state->schema_ver,
727 LDAP_ATTR_PROFILE_PATH),
730 if (expand_explicit) {
731 profile_path = talloc_sub_basic(ctx,
739 pdb_set_profile_path(sampass, profile_path, PDB_SET);
741 pdb_set_profile_path(sampass,
742 talloc_sub_basic(ctx, username, domain,
747 acct_desc = smbldap_talloc_single_attribute(
748 smbldap_get_ldap(ldap_state->smbldap_state),
750 get_userattr_key2string(ldap_state->schema_ver,
754 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
757 workstations = smbldap_talloc_single_attribute(
758 smbldap_get_ldap(ldap_state->smbldap_state),
760 get_userattr_key2string(ldap_state->schema_ver,
764 pdb_set_workstations(sampass, workstations, PDB_SET);
767 munged_dial = smbldap_talloc_single_attribute(
768 smbldap_get_ldap(ldap_state->smbldap_state),
770 get_userattr_key2string(ldap_state->schema_ver,
771 LDAP_ATTR_MUNGED_DIAL),
774 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
777 /* FIXME: hours stuff should be cleaner */
781 memset(hours, 0xff, hours_len);
783 if (ldap_state->is_nds_ldap) {
786 char clear_text_pw[512];
788 /* Make call to Novell eDirectory ldap extension to get clear text password.
789 NOTE: This will only work if we have an SSL connection to eDirectory. */
790 user_dn = smbldap_talloc_dn(
791 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
793 if (user_dn != NULL) {
794 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
796 pwd_len = sizeof(clear_text_pw);
797 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
798 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
799 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
800 TALLOC_FREE(user_dn);
803 ZERO_STRUCT(smblmpwd);
804 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
805 TALLOC_FREE(user_dn);
808 ZERO_STRUCT(smbntpwd);
809 use_samba_attrs = False;
812 TALLOC_FREE(user_dn);
815 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
819 if (use_samba_attrs) {
820 temp = smbldap_talloc_single_attribute(
821 smbldap_get_ldap(ldap_state->smbldap_state),
823 get_userattr_key2string(ldap_state->schema_ver,
827 pdb_gethexpwd(temp, smblmpwd);
828 memset((char *)temp, '\0', strlen(temp)+1);
829 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
832 ZERO_STRUCT(smblmpwd);
835 temp = smbldap_talloc_single_attribute(
836 smbldap_get_ldap(ldap_state->smbldap_state),
838 get_userattr_key2string(ldap_state->schema_ver,
842 pdb_gethexpwd(temp, smbntpwd);
843 memset((char *)temp, '\0', strlen(temp)+1);
844 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
847 ZERO_STRUCT(smbntpwd);
853 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
855 uint8_t *pwhist = NULL;
857 char *history_string = talloc_array(ctx, char,
858 MAX_PW_HISTORY_LEN*64);
860 if (!history_string) {
864 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
866 pwhist = talloc_zero_array(ctx, uint8_t,
867 pwHistLen * PW_HISTORY_ENTRY_LEN);
868 if (pwhist == NULL) {
869 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
873 if (smbldap_get_single_attribute(
874 smbldap_get_ldap(ldap_state->smbldap_state),
876 get_userattr_key2string(ldap_state->schema_ver,
877 LDAP_ATTR_PWD_HISTORY),
879 MAX_PW_HISTORY_LEN*64)) {
880 bool hex_failed = false;
881 for (i = 0; i < pwHistLen; i++){
882 /* Get the 16 byte salt. */
883 if (!pdb_gethexpwd(&history_string[i*64],
884 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
888 /* Get the 16 byte MD5 hash of salt+passwd. */
889 if (!pdb_gethexpwd(&history_string[(i*64)+32],
890 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
891 PW_HISTORY_SALT_LEN])) {
897 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
899 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
902 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
907 temp = smbldap_talloc_single_attribute(
908 smbldap_get_ldap(ldap_state->smbldap_state),
910 get_userattr_key2string(ldap_state->schema_ver,
914 acct_ctrl = pdb_decode_acct_ctrl(temp);
916 if (acct_ctrl == 0) {
917 acct_ctrl |= ACB_NORMAL;
920 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
922 acct_ctrl |= ACB_NORMAL;
925 pdb_set_hours_len(sampass, hours_len, PDB_SET);
926 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
928 temp = smbldap_talloc_single_attribute(
929 smbldap_get_ldap(ldap_state->smbldap_state),
931 get_userattr_key2string(ldap_state->schema_ver,
932 LDAP_ATTR_BAD_PASSWORD_COUNT),
935 bad_password_count = (uint32_t) atol(temp);
936 pdb_set_bad_password_count(sampass,
937 bad_password_count, PDB_SET);
940 temp = smbldap_talloc_single_attribute(
941 smbldap_get_ldap(ldap_state->smbldap_state),
943 get_userattr_key2string(ldap_state->schema_ver,
944 LDAP_ATTR_BAD_PASSWORD_TIME),
947 bad_password_time = (time_t) atol(temp);
948 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
952 temp = smbldap_talloc_single_attribute(
953 smbldap_get_ldap(ldap_state->smbldap_state),
955 get_userattr_key2string(ldap_state->schema_ver,
956 LDAP_ATTR_LOGON_COUNT),
959 logon_count = (uint32_t) atol(temp);
960 pdb_set_logon_count(sampass, logon_count, PDB_SET);
963 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
965 temp = smbldap_talloc_single_attribute(
966 smbldap_get_ldap(ldap_state->smbldap_state),
968 get_userattr_key2string(ldap_state->schema_ver,
969 LDAP_ATTR_LOGON_HOURS),
972 pdb_gethexhours(temp, hours);
973 memset((char *)temp, '\0', strlen(temp) +1);
974 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
978 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
979 struct passwd unix_pw;
980 bool have_uid = false;
981 bool have_gid = false;
982 struct dom_sid mapped_gsid;
983 const struct dom_sid *primary_gsid;
986 ZERO_STRUCT(unix_pw);
988 unix_pw.pw_name = username;
989 unix_pw.pw_passwd = discard_const_p(char, "x");
991 temp = smbldap_talloc_single_attribute(
997 /* We've got a uid, feed the cache */
998 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1001 temp = smbldap_talloc_single_attribute(
1002 priv2ld(ldap_state),
1007 /* We've got a uid, feed the cache */
1008 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1011 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1012 priv2ld(ldap_state),
1016 if (unix_pw.pw_gecos == NULL) {
1017 unix_pw.pw_gecos = fullname;
1019 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1020 priv2ld(ldap_state),
1024 if (unix_pw.pw_dir == NULL) {
1025 unix_pw.pw_dir = discard_const_p(char, "");
1027 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1028 priv2ld(ldap_state),
1032 if (unix_pw.pw_shell == NULL) {
1033 unix_pw.pw_shell = discard_const_p(char, "");
1036 if (have_uid && have_gid) {
1037 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1039 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1042 if (sampass->unix_pw == NULL) {
1043 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1044 pdb_get_username(sampass)));
1048 id.id = sampass->unix_pw->pw_uid;
1049 id.type = ID_TYPE_UID;
1051 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass), &id);
1053 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1054 primary_gsid = pdb_get_group_sid(sampass);
1055 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1056 id.id = sampass->unix_pw->pw_gid;
1057 id.type = ID_TYPE_GID;
1059 idmap_cache_set_sid2unixid(primary_gsid, &id);
1063 /* check the timestamp of the cache vs ldap entry */
1064 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1070 /* see if we have newer updates */
1071 if (!login_cache_read(sampass, &cache_entry)) {
1072 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1073 (unsigned int)pdb_get_bad_password_count(sampass),
1074 (unsigned int)pdb_get_bad_password_time(sampass)));
1079 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1080 (unsigned int)ldap_entry_time,
1081 (unsigned int)cache_entry.entry_timestamp,
1082 (unsigned int)cache_entry.bad_password_time));
1084 if (ldap_entry_time > cache_entry.entry_timestamp) {
1085 /* cache is older than directory , so
1086 we need to delete the entry but allow the
1087 fields to be written out */
1088 login_cache_delentry(sampass);
1091 pdb_set_acct_ctrl(sampass,
1092 pdb_get_acct_ctrl(sampass) |
1093 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1095 pdb_set_bad_password_count(sampass,
1096 cache_entry.bad_password_count,
1098 pdb_set_bad_password_time(sampass,
1099 cache_entry.bad_password_time,
1111 /**********************************************************************
1112 Initialize the ldap db from a struct samu. Called on update.
1113 (Based on init_buffer_from_sam in pdb_tdb.c)
1114 *********************************************************************/
1116 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1117 LDAPMessage *existing,
1118 LDAPMod *** mods, struct samu * sampass,
1119 bool (*need_update)(const struct samu *,
1124 if (mods == NULL || sampass == NULL) {
1125 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1132 * took out adding "objectclass: sambaAccount"
1133 * do this on a per-mod basis
1135 if (need_update(sampass, PDB_USERNAME)) {
1136 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1138 "uid", pdb_get_username(sampass));
1139 if (ldap_state->is_nds_ldap) {
1141 smbldap_get_ldap(ldap_state->smbldap_state),
1143 "cn", pdb_get_username(sampass));
1145 smbldap_get_ldap(ldap_state->smbldap_state),
1147 "sn", pdb_get_username(sampass));
1151 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1153 /* only update the RID if we actually need to */
1154 if (need_update(sampass, PDB_USERSID)) {
1156 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1158 switch ( ldap_state->schema_ver ) {
1159 case SCHEMAVER_SAMBASAMACCOUNT:
1162 ldap_state->smbldap_state),
1164 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1165 sid_to_fstring(sid_string, user_sid));
1169 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1174 /* we don't need to store the primary group RID - so leaving it
1175 'free' to hang off the unix primary group makes life easier */
1177 if (need_update(sampass, PDB_GROUPSID)) {
1179 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1181 switch ( ldap_state->schema_ver ) {
1182 case SCHEMAVER_SAMBASAMACCOUNT:
1185 ldap_state->smbldap_state),
1187 get_userattr_key2string(ldap_state->schema_ver,
1188 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1192 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1198 /* displayName, cn, and gecos should all be the same
1199 * most easily accomplished by giving them the same OID
1200 * gecos isn't set here b/c it should be handled by the
1202 * We change displayName only and fall back to cn if
1203 * it does not exist.
1206 if (need_update(sampass, PDB_FULLNAME))
1207 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1209 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1210 pdb_get_fullname(sampass));
1212 if (need_update(sampass, PDB_ACCTDESC))
1213 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1215 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1216 pdb_get_acct_desc(sampass));
1218 if (need_update(sampass, PDB_WORKSTATIONS))
1219 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1221 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1222 pdb_get_workstations(sampass));
1224 if (need_update(sampass, PDB_MUNGEDDIAL))
1225 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1227 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1228 pdb_get_munged_dial(sampass));
1230 if (need_update(sampass, PDB_SMBHOME))
1231 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1233 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1234 pdb_get_homedir(sampass));
1236 if (need_update(sampass, PDB_DRIVE))
1237 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1239 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1240 pdb_get_dir_drive(sampass));
1242 if (need_update(sampass, PDB_LOGONSCRIPT))
1243 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1245 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1246 pdb_get_logon_script(sampass));
1248 if (need_update(sampass, PDB_PROFILE))
1249 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1251 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1252 pdb_get_profile_path(sampass));
1254 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1257 if (need_update(sampass, PDB_LOGONTIME))
1258 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1260 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1263 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1266 if (need_update(sampass, PDB_LOGOFFTIME))
1267 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1269 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1272 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1275 if (need_update(sampass, PDB_KICKOFFTIME))
1276 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1278 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1281 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1284 if (need_update(sampass, PDB_CANCHANGETIME))
1285 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1287 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1290 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1291 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1293 if (need_update(sampass, PDB_LMPASSWD)) {
1294 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1297 pdb_sethexpwd(pwstr, lm_pw,
1298 pdb_get_acct_ctrl(sampass));
1301 ldap_state->smbldap_state),
1303 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1308 ldap_state->smbldap_state),
1310 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1314 if (need_update(sampass, PDB_NTPASSWD)) {
1315 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1318 pdb_sethexpwd(pwstr, nt_pw,
1319 pdb_get_acct_ctrl(sampass));
1322 ldap_state->smbldap_state),
1324 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1329 ldap_state->smbldap_state),
1331 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1336 if (need_update(sampass, PDB_PWHISTORY)) {
1338 uint32_t pwHistLen = 0;
1339 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1341 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1345 if (pwHistLen == 0) {
1346 /* Remove any password history from the LDAP store. */
1347 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1351 uint32_t currHistLen = 0;
1352 const uint8_t *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1353 if (pwhist != NULL) {
1354 /* We can only store (1024-1/64 password history entries. */
1355 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1356 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1357 /* Store the salt. */
1358 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1359 /* Followed by the md5 hash of salt + md4 hash */
1360 pdb_sethexpwd(&pwstr[(i*64)+32],
1361 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1362 DEBUG(100, ("pwstr=%s\n", pwstr));
1367 smbldap_get_ldap(ldap_state->smbldap_state),
1369 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1374 if (need_update(sampass, PDB_PASSLASTSET)) {
1375 if (asprintf(&temp, "%li",
1376 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1380 smbldap_get_ldap(ldap_state->smbldap_state),
1382 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1388 if (need_update(sampass, PDB_HOURS)) {
1389 const uint8_t *hours = pdb_get_hours(sampass);
1392 pdb_sethexhours(hourstr, hours);
1394 smbldap_get_ldap(ldap_state->smbldap_state),
1397 get_userattr_key2string(ldap_state->schema_ver,
1398 LDAP_ATTR_LOGON_HOURS),
1403 if (need_update(sampass, PDB_ACCTCTRL))
1405 smbldap_get_ldap(ldap_state->smbldap_state),
1407 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1408 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1410 /* password lockout cache:
1411 - If we are now autolocking or clearing, we write to ldap
1412 - If we are clearing, we delete the cache entry
1413 - If the count is > 0, we update the cache
1415 This even means when autolocking, we cache, just in case the
1416 update doesn't work, and we have to cache the autolock flag */
1418 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1419 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1420 uint16_t badcount = pdb_get_bad_password_count(sampass);
1421 time_t badtime = pdb_get_bad_password_time(sampass);
1423 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1425 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1426 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1428 if ((badcount >= pol) || (badcount == 0)) {
1429 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1430 (unsigned int)badcount, (unsigned int)badtime));
1431 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1435 smbldap_get_ldap(ldap_state->smbldap_state),
1437 get_userattr_key2string(
1438 ldap_state->schema_ver,
1439 LDAP_ATTR_BAD_PASSWORD_COUNT),
1443 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1447 smbldap_get_ldap(ldap_state->smbldap_state),
1449 get_userattr_key2string(
1450 ldap_state->schema_ver,
1451 LDAP_ATTR_BAD_PASSWORD_TIME),
1455 if (badcount == 0) {
1456 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1457 login_cache_delentry(sampass);
1459 struct login_cache cache_entry;
1461 cache_entry.entry_timestamp = time(NULL);
1462 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1463 cache_entry.bad_password_count = badcount;
1464 cache_entry.bad_password_time = badtime;
1466 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1467 login_cache_write(sampass, &cache_entry);
1474 /**********************************************************************
1475 End enumeration of the LDAP password list.
1476 *********************************************************************/
1478 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1480 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1481 if (ldap_state->result) {
1482 ldap_msgfree(ldap_state->result);
1483 ldap_state->result = NULL;
1487 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1488 const char *new_attr)
1492 if (new_attr == NULL) {
1496 for (i=0; (*attr_list)[i] != NULL; i++) {
1500 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1502 SMB_ASSERT((*attr_list) != NULL);
1503 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1504 (*attr_list)[i+1] = NULL;
1507 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1508 const char ***attr_list)
1510 append_attr(mem_ctx, attr_list, "uidNumber");
1511 append_attr(mem_ctx, attr_list, "gidNumber");
1512 append_attr(mem_ctx, attr_list, "homeDirectory");
1513 append_attr(mem_ctx, attr_list, "loginShell");
1514 append_attr(mem_ctx, attr_list, "gecos");
1517 /**********************************************************************
1518 Get struct samu entry from LDAP by username.
1519 *********************************************************************/
1521 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1523 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1524 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1525 LDAPMessage *result = NULL;
1526 LDAPMessage *entry = NULL;
1528 const char ** attr_list;
1531 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1532 append_attr(user, &attr_list,
1533 get_userattr_key2string(ldap_state->schema_ver,
1534 LDAP_ATTR_MOD_TIMESTAMP));
1535 ldapsam_add_unix_attributes(user, &attr_list);
1536 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1538 TALLOC_FREE( attr_list );
1540 if ( rc != LDAP_SUCCESS )
1541 return NT_STATUS_NO_SUCH_USER;
1543 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1547 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1548 ldap_msgfree(result);
1549 return NT_STATUS_NO_SUCH_USER;
1550 } else if (count > 1) {
1551 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1552 ldap_msgfree(result);
1553 return NT_STATUS_NO_SUCH_USER;
1556 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1559 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1560 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1561 ldap_msgfree(result);
1562 return NT_STATUS_NO_SUCH_USER;
1564 pdb_set_backend_private_data(user, result, NULL,
1565 my_methods, PDB_CHANGED);
1566 smbldap_talloc_autofree_ldapmsg(user, result);
1569 ldap_msgfree(result);
1574 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1575 const struct dom_sid *sid, LDAPMessage **result)
1578 const char ** attr_list;
1580 switch ( ldap_state->schema_ver ) {
1581 case SCHEMAVER_SAMBASAMACCOUNT: {
1582 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1583 if (tmp_ctx == NULL) {
1584 return LDAP_NO_MEMORY;
1587 attr_list = get_userattr_list(tmp_ctx,
1588 ldap_state->schema_ver);
1589 append_attr(tmp_ctx, &attr_list,
1590 get_userattr_key2string(
1591 ldap_state->schema_ver,
1592 LDAP_ATTR_MOD_TIMESTAMP));
1593 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1594 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1596 TALLOC_FREE(tmp_ctx);
1598 if ( rc != LDAP_SUCCESS )
1604 DEBUG(0,("Invalid schema version specified\n"));
1610 /**********************************************************************
1611 Get struct samu entry from LDAP by SID.
1612 *********************************************************************/
1614 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1616 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1617 LDAPMessage *result = NULL;
1618 LDAPMessage *entry = NULL;
1622 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1624 if (rc != LDAP_SUCCESS)
1625 return NT_STATUS_NO_SUCH_USER;
1627 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1631 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1632 "count=%d\n", sid_string_dbg(sid), count));
1633 ldap_msgfree(result);
1634 return NT_STATUS_NO_SUCH_USER;
1635 } else if (count > 1) {
1636 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1637 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1639 ldap_msgfree(result);
1640 return NT_STATUS_NO_SUCH_USER;
1643 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1646 ldap_msgfree(result);
1647 return NT_STATUS_NO_SUCH_USER;
1650 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1651 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1652 ldap_msgfree(result);
1653 return NT_STATUS_NO_SUCH_USER;
1656 pdb_set_backend_private_data(user, result, NULL,
1657 my_methods, PDB_CHANGED);
1658 smbldap_talloc_autofree_ldapmsg(user, result);
1659 return NT_STATUS_OK;
1662 /********************************************************************
1663 Do the actual modification - also change a plaintext passord if
1665 **********************************************************************/
1667 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1668 struct samu *newpwd, char *dn,
1669 LDAPMod **mods, int ldap_op,
1670 bool (*need_update)(const struct samu *, enum pdb_elements))
1672 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1675 if (!newpwd || !dn) {
1676 return NT_STATUS_INVALID_PARAMETER;
1679 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1680 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1681 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1682 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1685 char *retoid = NULL;
1686 struct berval *retdata = NULL;
1687 char *utf8_password;
1689 size_t converted_size;
1692 if (!ldap_state->is_nds_ldap) {
1694 if (!smbldap_has_extension(
1696 ldap_state->smbldap_state),
1697 LDAP_EXOP_MODIFY_PASSWD)) {
1698 DEBUG(2, ("ldap password change requested, but LDAP "
1699 "server does not support it -- ignoring\n"));
1700 return NT_STATUS_OK;
1704 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1705 pdb_get_plaintext_passwd(newpwd),
1708 return NT_STATUS_NO_MEMORY;
1711 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1712 TALLOC_FREE(utf8_password);
1713 return NT_STATUS_NO_MEMORY;
1716 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1717 DEBUG(0,("ber_alloc_t returns NULL\n"));
1718 TALLOC_FREE(utf8_password);
1719 TALLOC_FREE(utf8_dn);
1720 return NT_STATUS_UNSUCCESSFUL;
1723 if ((ber_printf (ber, "{") < 0) ||
1724 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1726 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1729 TALLOC_FREE(utf8_dn);
1730 TALLOC_FREE(utf8_password);
1731 return NT_STATUS_UNSUCCESSFUL;
1734 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1735 ret = ber_printf(ber, "ts}",
1736 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1739 ret = ber_printf(ber, "}");
1743 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1746 TALLOC_FREE(utf8_dn);
1747 TALLOC_FREE(utf8_password);
1748 return NT_STATUS_UNSUCCESSFUL;
1751 if ((rc = ber_flatten (ber, &bv))<0) {
1752 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1754 TALLOC_FREE(utf8_dn);
1755 TALLOC_FREE(utf8_password);
1756 return NT_STATUS_UNSUCCESSFUL;
1759 TALLOC_FREE(utf8_dn);
1760 TALLOC_FREE(utf8_password);
1763 if (!ldap_state->is_nds_ldap) {
1764 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1765 LDAP_EXOP_MODIFY_PASSWD,
1766 bv, NULL, NULL, &retoid,
1769 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1770 pdb_get_plaintext_passwd(newpwd));
1772 if (rc != LDAP_SUCCESS) {
1773 char *ld_error = NULL;
1775 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1776 DEBUG(3, ("Could not set userPassword "
1777 "attribute due to an objectClass "
1778 "violation -- ignoring\n"));
1780 return NT_STATUS_OK;
1784 smbldap_get_ldap(ldap_state->smbldap_state),
1785 LDAP_OPT_ERROR_STRING,
1787 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1788 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1789 SAFE_FREE(ld_error);
1791 #if defined(LDAP_CONSTRAINT_VIOLATION)
1792 if (rc == LDAP_CONSTRAINT_VIOLATION)
1793 return NT_STATUS_PASSWORD_RESTRICTION;
1795 return NT_STATUS_UNSUCCESSFUL;
1797 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1798 #ifdef DEBUG_PASSWORD
1799 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1802 ber_bvfree(retdata);
1804 ldap_memfree(retoid);
1810 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1811 /* may be password change below however */
1815 if (ldap_state->is_nds_ldap) {
1816 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1820 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1824 rc = smbldap_add(ldap_state->smbldap_state,
1827 case LDAP_MOD_REPLACE:
1828 rc = smbldap_modify(ldap_state->smbldap_state,
1832 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1834 return NT_STATUS_INVALID_PARAMETER;
1837 if (rc!=LDAP_SUCCESS) {
1838 return NT_STATUS_UNSUCCESSFUL;
1842 return NT_STATUS_OK;
1845 /**********************************************************************
1846 Delete entry from LDAP for username.
1847 *********************************************************************/
1849 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1850 struct samu * sam_acct)
1852 struct ldapsam_privates *priv =
1853 (struct ldapsam_privates *)my_methods->private_data;
1856 LDAPMessage *msg, *entry;
1857 NTSTATUS result = NT_STATUS_NO_MEMORY;
1858 const char **attr_list;
1859 TALLOC_CTX *mem_ctx;
1862 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1863 return NT_STATUS_INVALID_PARAMETER;
1866 sname = pdb_get_username(sam_acct);
1868 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1871 mem_ctx = talloc_new(NULL);
1872 if (mem_ctx == NULL) {
1873 DEBUG(0, ("talloc_new failed\n"));
1877 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1878 if (attr_list == NULL) {
1882 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1884 if ((rc != LDAP_SUCCESS) ||
1885 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1886 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1887 DEBUG(5, ("Could not find user %s\n", sname));
1888 result = NT_STATUS_NO_SUCH_USER;
1892 rc = ldapsam_delete_entry(
1893 priv, mem_ctx, entry,
1894 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1895 LDAP_OBJ_SAMBASAMACCOUNT : 0,
1898 result = (rc == LDAP_SUCCESS) ?
1899 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1902 TALLOC_FREE(mem_ctx);
1906 /**********************************************************************
1908 *********************************************************************/
1910 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1912 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1913 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1916 LDAPMessage *result = NULL;
1917 LDAPMessage *entry = NULL;
1918 LDAPMod **mods = NULL;
1919 const char **attr_list;
1921 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1923 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1924 if (pdb_get_username(newpwd) == NULL) {
1925 return NT_STATUS_INVALID_PARAMETER;
1927 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1928 TALLOC_FREE( attr_list );
1929 if (rc != LDAP_SUCCESS) {
1930 return NT_STATUS_UNSUCCESSFUL;
1932 pdb_set_backend_private_data(newpwd, result, NULL,
1933 my_methods, PDB_CHANGED);
1934 smbldap_talloc_autofree_ldapmsg(newpwd, result);
1937 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1939 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1940 return NT_STATUS_UNSUCCESSFUL;
1943 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1945 dn = smbldap_talloc_dn(talloc_tos(),
1946 smbldap_get_ldap(ldap_state->smbldap_state),
1949 return NT_STATUS_UNSUCCESSFUL;
1952 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1954 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1955 pdb_element_is_changed)) {
1956 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1959 ldap_mods_free(mods,True);
1960 return NT_STATUS_UNSUCCESSFUL;
1963 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1964 && (mods == NULL)) {
1965 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1966 pdb_get_username(newpwd)));
1968 return NT_STATUS_OK;
1971 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1974 ldap_mods_free(mods,True);
1980 * We need to set the backend private data to NULL here. For example
1981 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1982 * same one, and with the explicit delete / add logic for attribute
1983 * values the second time we would use the wrong "old" value which
1984 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1986 * The existing LDAPMessage is still being auto-freed by the
1989 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
1992 if (!NT_STATUS_IS_OK(ret)) {
1996 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1997 pdb_get_username(newpwd)));
1998 return NT_STATUS_OK;
2001 /***************************************************************************
2002 Renames a struct samu
2003 - The "rename user script" has full responsibility for changing everything
2004 ***************************************************************************/
2006 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2007 TALLOC_CTX *tmp_ctx,
2009 uint32_t member_rid);
2011 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2012 TALLOC_CTX *mem_ctx,
2014 struct dom_sid **pp_sids,
2016 uint32_t *p_num_groups);
2018 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2019 struct samu *old_acct,
2020 const char *newname)
2022 const char *oldname;
2024 char *rename_script = NULL;
2025 fstring oldname_lower, newname_lower;
2028 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2029 return NT_STATUS_INVALID_PARAMETER;
2032 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2033 return NT_STATUS_INVALID_PARAMETER;
2036 oldname = pdb_get_username(old_acct);
2038 /* rename the posix user */
2039 rename_script = lp_rename_user_script(talloc_tos());
2040 if (rename_script == NULL) {
2041 return NT_STATUS_NO_MEMORY;
2044 if (!(*rename_script)) {
2045 TALLOC_FREE(rename_script);
2046 return NT_STATUS_ACCESS_DENIED;
2049 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2052 /* We have to allow the account name to end with a '$'.
2053 Also, follow the semantics in _samr_create_user() and lower case the
2054 posix name but preserve the case in passdb */
2056 fstrcpy( oldname_lower, oldname );
2057 if (!strlower_m( oldname_lower )) {
2058 return NT_STATUS_INVALID_PARAMETER;
2060 fstrcpy( newname_lower, newname );
2061 if (!strlower_m( newname_lower )) {
2062 return NT_STATUS_INVALID_PARAMETER;
2065 rename_script = realloc_string_sub2(rename_script,
2070 if (!rename_script) {
2071 return NT_STATUS_NO_MEMORY;
2073 rename_script = realloc_string_sub2(rename_script,
2078 rc = smbrun(rename_script, NULL, NULL);
2080 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2081 rename_script, rc));
2083 TALLOC_FREE(rename_script);
2086 smb_nscd_flush_user_cache();
2090 return NT_STATUS_UNSUCCESSFUL;
2092 return NT_STATUS_OK;
2095 /**********************************************************************
2096 Add struct samu to LDAP.
2097 *********************************************************************/
2099 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2101 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2102 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2104 LDAPMessage *result = NULL;
2105 LDAPMessage *entry = NULL;
2106 LDAPMod **mods = NULL;
2107 int ldap_op = LDAP_MOD_REPLACE;
2108 uint32_t num_result;
2109 const char **attr_list;
2110 char *escape_user = NULL;
2111 const char *username = pdb_get_username(newpwd);
2112 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2113 char *filter = NULL;
2115 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2116 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2119 return NT_STATUS_NO_MEMORY;
2122 if (!username || !*username) {
2123 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2124 status = NT_STATUS_INVALID_PARAMETER;
2128 /* free this list after the second search or in case we exit on failure */
2129 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2131 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2133 if (rc != LDAP_SUCCESS) {
2137 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
2139 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2143 ldap_msgfree(result);
2146 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2147 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2149 if (rc == LDAP_SUCCESS) {
2150 if (ldap_count_entries(
2152 ldap_state->smbldap_state),
2154 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2155 "already in the base, with samba "
2156 "attributes\n", sid_string_dbg(sid)));
2159 ldap_msgfree(result);
2164 /* does the entry already exist but without a samba attributes?
2165 we need to return the samba attributes here */
2167 escape_user = escape_ldap_string(talloc_tos(), username);
2168 filter = talloc_strdup(attr_list, "(uid=%u)");
2170 status = NT_STATUS_NO_MEMORY;
2173 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2174 TALLOC_FREE(escape_user);
2176 status = NT_STATUS_NO_MEMORY;
2180 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2181 filter, attr_list, &result);
2182 if ( rc != LDAP_SUCCESS ) {
2186 num_result = ldap_count_entries(
2187 smbldap_get_ldap(ldap_state->smbldap_state), result);
2189 if (num_result > 1) {
2190 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2194 /* Check if we need to update an existing entry */
2195 if (num_result == 1) {
2196 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2197 ldap_op = LDAP_MOD_REPLACE;
2198 entry = ldap_first_entry(
2199 smbldap_get_ldap(ldap_state->smbldap_state), result);
2200 dn = smbldap_talloc_dn(
2201 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
2204 status = NT_STATUS_NO_MEMORY;
2208 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2210 /* There might be a SID for this account already - say an idmap entry */
2212 filter = talloc_asprintf(ctx,
2213 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2214 get_userattr_key2string(ldap_state->schema_ver,
2215 LDAP_ATTR_USER_SID),
2216 sid_string_talloc(ctx, sid),
2217 LDAP_OBJ_IDMAP_ENTRY,
2218 LDAP_OBJ_SID_ENTRY);
2220 status = NT_STATUS_NO_MEMORY;
2224 /* free old result before doing a new search */
2225 if (result != NULL) {
2226 ldap_msgfree(result);
2229 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2230 filter, attr_list, &result);
2232 if ( rc != LDAP_SUCCESS ) {
2236 num_result = ldap_count_entries(
2237 smbldap_get_ldap(ldap_state->smbldap_state), result);
2239 if (num_result > 1) {
2240 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2244 /* Check if we need to update an existing entry */
2245 if (num_result == 1) {
2247 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2248 ldap_op = LDAP_MOD_REPLACE;
2249 entry = ldap_first_entry (
2250 smbldap_get_ldap(ldap_state->smbldap_state),
2252 dn = smbldap_talloc_dn (
2254 smbldap_get_ldap(ldap_state->smbldap_state),
2257 status = NT_STATUS_NO_MEMORY;
2263 if (num_result == 0) {
2264 char *escape_username;
2265 /* Check if we need to add an entry */
2266 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2267 ldap_op = LDAP_MOD_ADD;
2269 escape_username = escape_rdn_val_string_alloc(username);
2270 if (!escape_username) {
2271 status = NT_STATUS_NO_MEMORY;
2275 if (username[strlen(username)-1] == '$') {
2276 dn = talloc_asprintf(ctx,
2279 lp_ldap_machine_suffix(talloc_tos()));
2281 dn = talloc_asprintf(ctx,
2284 lp_ldap_user_suffix(talloc_tos()));
2287 SAFE_FREE(escape_username);
2289 status = NT_STATUS_NO_MEMORY;
2294 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2295 pdb_element_is_set_or_changed)) {
2296 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2298 ldap_mods_free(mods, true);
2304 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2307 switch ( ldap_state->schema_ver ) {
2308 case SCHEMAVER_SAMBASAMACCOUNT:
2309 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2312 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2316 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2317 if (!NT_STATUS_IS_OK(ret)) {
2318 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2319 pdb_get_username(newpwd),dn));
2320 ldap_mods_free(mods, true);
2324 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2325 ldap_mods_free(mods, true);
2327 status = NT_STATUS_OK;
2333 ldap_msgfree(result);
2338 /**********************************************************************
2339 *********************************************************************/
2341 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2343 LDAPMessage ** result)
2345 int scope = LDAP_SCOPE_SUBTREE;
2347 const char **attr_list;
2349 attr_list = get_attr_list(NULL, groupmap_attr_list);
2350 rc = smbldap_search(ldap_state->smbldap_state,
2351 lp_ldap_suffix (talloc_tos()), scope,
2352 filter, attr_list, 0, result);
2353 TALLOC_FREE(attr_list);
2358 /**********************************************************************
2359 *********************************************************************/
2361 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2362 GROUP_MAP *map, LDAPMessage *entry)
2365 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2367 if (ldap_state == NULL || map == NULL || entry == NULL ||
2368 smbldap_get_ldap(ldap_state->smbldap_state) == NULL) {
2369 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2374 temp = smbldap_talloc_single_attribute(
2375 smbldap_get_ldap(ldap_state->smbldap_state),
2377 get_attr_key2string(groupmap_attr_list,
2378 LDAP_ATTR_GIDNUMBER),
2381 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2382 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2386 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2388 map->gid = (gid_t)atol(temp);
2391 temp = smbldap_talloc_single_attribute(
2392 smbldap_get_ldap(ldap_state->smbldap_state),
2394 get_attr_key2string(groupmap_attr_list,
2395 LDAP_ATTR_GROUP_SID),
2398 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2399 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2404 if (!string_to_sid(&map->sid, temp)) {
2405 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2411 temp = smbldap_talloc_single_attribute(
2412 smbldap_get_ldap(ldap_state->smbldap_state),
2414 get_attr_key2string(groupmap_attr_list,
2415 LDAP_ATTR_GROUP_TYPE),
2418 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2419 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2423 map->sid_name_use = (enum lsa_SidType)atol(temp);
2425 if ((map->sid_name_use < SID_NAME_USER) ||
2426 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2427 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2433 temp = smbldap_talloc_single_attribute(
2434 smbldap_get_ldap(ldap_state->smbldap_state),
2436 get_attr_key2string(groupmap_attr_list,
2437 LDAP_ATTR_DISPLAY_NAME),
2440 temp = smbldap_talloc_single_attribute(
2441 smbldap_get_ldap(ldap_state->smbldap_state),
2443 get_attr_key2string(groupmap_attr_list,
2447 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2448 for gidNumber(%lu)\n",(unsigned long)map->gid));
2453 map->nt_name = talloc_strdup(map, temp);
2454 if (!map->nt_name) {
2460 temp = smbldap_talloc_single_attribute(
2461 smbldap_get_ldap(ldap_state->smbldap_state),
2463 get_attr_key2string(groupmap_attr_list,
2467 temp = talloc_strdup(ctx, "");
2473 map->comment = talloc_strdup(map, temp);
2474 if (!map->comment) {
2479 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2482 id.type = ID_TYPE_GID;
2484 idmap_cache_set_sid2unixid(&map->sid, &id);
2491 /**********************************************************************
2492 *********************************************************************/
2494 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2498 struct ldapsam_privates *ldap_state =
2499 (struct ldapsam_privates *)methods->private_data;
2500 LDAPMessage *result = NULL;
2501 LDAPMessage *entry = NULL;
2504 if (ldapsam_search_one_group(ldap_state, filter, &result)
2506 return NT_STATUS_NO_SUCH_GROUP;
2509 count = ldap_count_entries(priv2ld(ldap_state), result);
2512 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2514 ldap_msgfree(result);
2515 return NT_STATUS_NO_SUCH_GROUP;
2519 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2520 "count=%d\n", filter, count));
2521 ldap_msgfree(result);
2522 return NT_STATUS_NO_SUCH_GROUP;
2525 entry = ldap_first_entry(priv2ld(ldap_state), result);
2528 ldap_msgfree(result);
2529 return NT_STATUS_UNSUCCESSFUL;
2532 if (!init_group_from_ldap(ldap_state, map, entry)) {
2533 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2534 "group filter %s\n", filter));
2535 ldap_msgfree(result);
2536 return NT_STATUS_NO_SUCH_GROUP;
2539 ldap_msgfree(result);
2540 return NT_STATUS_OK;
2543 /**********************************************************************
2544 *********************************************************************/
2546 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2549 char *filter = NULL;
2553 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2555 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2556 sid_to_fstring(tmp, &sid)) < 0) {
2557 return NT_STATUS_NO_MEMORY;
2560 status = ldapsam_getgroup(methods, filter, map);
2565 /**********************************************************************
2566 *********************************************************************/
2568 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2571 char *filter = NULL;
2574 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2576 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2577 (unsigned long)gid) < 0) {
2578 return NT_STATUS_NO_MEMORY;
2581 status = ldapsam_getgroup(methods, filter, map);
2586 /**********************************************************************
2587 *********************************************************************/
2589 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2592 char *filter = NULL;
2593 char *escape_name = escape_ldap_string(talloc_tos(), name);
2597 return NT_STATUS_NO_MEMORY;
2600 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2602 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2603 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2605 TALLOC_FREE(escape_name);
2606 return NT_STATUS_NO_MEMORY;
2609 TALLOC_FREE(escape_name);
2610 status = ldapsam_getgroup(methods, filter, map);
2615 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2617 const struct dom_sid *domain_sid,
2623 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2624 str, sizeof(str)-1)) {
2625 DEBUG(10, ("Could not find sambaSID attribute\n"));
2629 if (!string_to_sid(&sid, str)) {
2630 DEBUG(10, ("Could not convert string %s to sid\n", str));
2634 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2635 DEBUG(10, ("SID %s is not in expected domain %s\n",
2636 str, sid_string_dbg(domain_sid)));
2640 if (!sid_peek_rid(&sid, rid)) {
2641 DEBUG(10, ("Could not peek into RID\n"));
2648 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2649 TALLOC_CTX *mem_ctx,
2650 const struct dom_sid *group,
2651 uint32_t **pp_member_rids,
2652 size_t *p_num_members)
2654 struct ldapsam_privates *ldap_state =
2655 (struct ldapsam_privates *)methods->private_data;
2656 struct smbldap_state *conn = ldap_state->smbldap_state;
2657 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2658 const char *sid_attrs[] = { "sambaSID", NULL };
2659 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2660 LDAPMessage *result = NULL;
2663 char **values = NULL;
2668 *pp_member_rids = NULL;
2671 filter = talloc_asprintf(mem_ctx,
2672 "(&(objectClass=%s)"
2675 LDAP_OBJ_POSIXGROUP,
2677 sid_string_talloc(mem_ctx, group));
2678 if (filter == NULL) {
2679 ret = NT_STATUS_NO_MEMORY;
2683 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2684 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2687 if (rc != LDAP_SUCCESS)
2690 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2692 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2695 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2696 sid_string_dbg(group)));
2697 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2702 ret = NT_STATUS_NO_SUCH_GROUP;
2706 entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2710 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2712 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2713 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2717 values = ldap_get_values(smbldap_get_ldap(conn), entry, "memberUid");
2719 if ((values != NULL) && (values[0] != NULL)) {
2721 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2722 if (filter == NULL) {
2723 ret = NT_STATUS_NO_MEMORY;
2727 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2728 char *escape_memberuid;
2730 escape_memberuid = escape_ldap_string(talloc_tos(),
2732 if (escape_memberuid == NULL) {
2733 ret = NT_STATUS_NO_MEMORY;
2737 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2738 TALLOC_FREE(escape_memberuid);
2739 if (filter == NULL) {
2740 ret = NT_STATUS_NO_MEMORY;
2745 filter = talloc_asprintf_append_buffer(filter, "))");
2746 if (filter == NULL) {
2747 ret = NT_STATUS_NO_MEMORY;
2751 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2752 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2755 if (rc != LDAP_SUCCESS)
2758 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2759 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2761 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2763 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2765 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2771 sidstr = smbldap_talloc_single_attribute(
2772 smbldap_get_ldap(conn), entry, "sambaSID",
2775 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2776 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2777 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2781 if (!string_to_sid(&sid, sidstr))
2784 if (!sid_check_is_in_our_sam(&sid)) {
2785 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2786 "in our domain\n"));
2787 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2791 sid_peek_rid(&sid, &rid);
2793 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2795 ret = NT_STATUS_NO_MEMORY;
2801 filter = talloc_asprintf(mem_ctx,
2802 "(&(objectClass=%s)"
2804 LDAP_OBJ_SAMBASAMACCOUNT,
2807 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2808 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2811 if (rc != LDAP_SUCCESS)
2814 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2816 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2818 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2822 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn),
2824 get_global_sam_sid(),
2826 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2827 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2831 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2833 ret = NT_STATUS_NO_MEMORY;
2843 ldap_value_free(values);
2848 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2849 TALLOC_CTX *mem_ctx,
2851 struct dom_sid **pp_sids,
2853 uint32_t *p_num_groups)
2855 struct ldapsam_privates *ldap_state =
2856 (struct ldapsam_privates *)methods->private_data;
2857 struct smbldap_state *conn = ldap_state->smbldap_state;
2859 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2862 LDAPMessage *result = NULL;
2864 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2868 gid_t primary_gid = -1;
2873 if (pdb_get_username(user) == NULL) {
2874 return NT_STATUS_INVALID_PARAMETER;
2877 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2878 if (escape_name == NULL)
2879 return NT_STATUS_NO_MEMORY;
2881 if (user->unix_pw) {
2882 primary_gid = user->unix_pw->pw_gid;
2884 /* retrieve the users primary gid */
2885 filter = talloc_asprintf(mem_ctx,
2886 "(&(objectClass=%s)(uid=%s))",
2887 LDAP_OBJ_SAMBASAMACCOUNT,
2889 if (filter == NULL) {
2890 ret = NT_STATUS_NO_MEMORY;
2894 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2895 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2897 if (rc != LDAP_SUCCESS)
2900 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2902 count = ldap_count_entries(priv2ld(ldap_state), result);
2906 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2907 ret = NT_STATUS_NO_SUCH_USER;
2910 entry = ldap_first_entry(priv2ld(ldap_state), result);
2912 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2914 DEBUG (1, ("Unable to find the member's gid!\n"));
2915 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2918 primary_gid = strtoul(gidstr, NULL, 10);
2921 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2922 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2927 filter = talloc_asprintf(mem_ctx,
2928 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2929 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2930 if (filter == NULL) {
2931 ret = NT_STATUS_NO_MEMORY;
2935 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2936 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2938 if (rc != LDAP_SUCCESS)
2941 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2949 /* We need to add the primary group as the first gid/sid */
2951 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2952 ret = NT_STATUS_NO_MEMORY;
2956 /* This sid will be replaced later */
2958 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2960 if (!NT_STATUS_IS_OK(ret)) {
2964 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2966 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2973 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
2975 str, sizeof(str)-1))
2978 if (!string_to_sid(&sid, str))
2981 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
2983 str, sizeof(str)-1))
2986 gid = strtoul(str, &end, 10);
2988 if (PTR_DIFF(end, str) != strlen(str))
2991 if (gid == primary_gid) {
2992 sid_copy(&(*pp_sids)[0], &sid);
2994 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2996 ret = NT_STATUS_NO_MEMORY;
2999 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3001 if (!NT_STATUS_IS_OK(ret)) {
3007 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3008 DEBUG(3, ("primary group of [%s] not found\n",
3009 pdb_get_username(user)));
3010 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3014 *p_num_groups = num_sids;
3020 TALLOC_FREE(escape_name);
3024 /**********************************************************************
3025 * Augment a posixGroup object with a sambaGroupMapping domgroup
3026 *********************************************************************/
3028 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3029 struct ldapsam_privates *ldap_state,
3032 const char *filter, *dn;
3033 LDAPMessage *msg, *entry;
3037 filter = talloc_asprintf(mem_ctx,
3038 "(&(objectClass=%s)(gidNumber=%u))",
3039 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3040 if (filter == NULL) {
3041 return NT_STATUS_NO_MEMORY;
3044 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3045 get_attr_list(mem_ctx, groupmap_attr_list),
3047 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3049 if ((rc != LDAP_SUCCESS) ||
3050 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3052 ((entry = ldap_first_entry(
3053 smbldap_get_ldap(ldap_state->smbldap_state),
3055 return NT_STATUS_NO_SUCH_GROUP;
3058 dn = smbldap_talloc_dn(mem_ctx,
3059 smbldap_get_ldap(ldap_state->smbldap_state),
3062 return NT_STATUS_NO_MEMORY;
3066 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3068 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3070 sid_string_talloc(mem_ctx, &map->sid));
3071 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3072 &mods, "sambaGroupType",
3073 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3074 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3075 &mods, "displayName",
3077 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3078 &mods, "description",
3080 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3082 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3083 if (rc != LDAP_SUCCESS) {
3084 return NT_STATUS_ACCESS_DENIED;
3087 return NT_STATUS_OK;
3090 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3093 struct ldapsam_privates *ldap_state =
3094 (struct ldapsam_privates *)methods->private_data;
3095 LDAPMessage *msg = NULL;
3096 LDAPMod **mods = NULL;
3097 const char *attrs[] = { NULL };
3101 TALLOC_CTX *mem_ctx;
3109 mem_ctx = talloc_new(NULL);
3110 if (mem_ctx == NULL) {
3111 DEBUG(0, ("talloc_new failed\n"));
3112 return NT_STATUS_NO_MEMORY;
3115 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3116 sid_string_talloc(mem_ctx, &map->sid));
3117 if (filter == NULL) {
3118 result = NT_STATUS_NO_MEMORY;
3122 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3123 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3124 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3126 if ((rc == LDAP_SUCCESS) &&
3127 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3130 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3131 "group mapping entry\n", sid_string_dbg(&map->sid)));
3132 result = NT_STATUS_GROUP_EXISTS;
3136 switch (map->sid_name_use) {
3138 case SID_NAME_DOM_GRP:
3139 /* To map a domain group we need to have a posix group
3141 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3145 case SID_NAME_ALIAS:
3146 if (!sid_check_is_in_our_sam(&map->sid)
3147 && !sid_check_is_in_builtin(&map->sid) )
3149 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3150 sid_string_dbg(&map->sid)));
3151 result = NT_STATUS_INVALID_PARAMETER;
3157 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3158 sid_type_lookup(map->sid_name_use)));
3159 result = NT_STATUS_INVALID_PARAMETER;
3163 /* Domain groups have been mapped in a separate routine, we have to
3164 * create an alias now */
3166 if (map->gid == -1) {
3167 DEBUG(10, ("Refusing to map gid==-1\n"));
3168 result = NT_STATUS_INVALID_PARAMETER;
3173 id.type = ID_TYPE_GID;
3175 if (pdb_id_to_sid(&id, &sid)) {
3176 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3177 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3178 result = NT_STATUS_GROUP_EXISTS;
3182 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3183 * the best we can get out of LDAP. */
3185 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3186 sid_string_talloc(mem_ctx, &map->sid),
3187 lp_ldap_group_suffix(talloc_tos()));
3189 result = NT_STATUS_NO_MEMORY;
3195 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3196 &mods, "objectClass", LDAP_OBJ_SID_ENTRY);
3197 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3198 &mods, "objectClass", LDAP_OBJ_GROUPMAP);
3199 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3201 sid_string_talloc(mem_ctx, &map->sid));
3202 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3203 &mods, "sambaGroupType",
3204 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3205 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3206 &mods, "displayName",
3208 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3209 &mods, "description",
3211 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3213 talloc_asprintf(mem_ctx, "%u",
3214 (unsigned int)map->gid));
3215 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3217 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3219 result = (rc == LDAP_SUCCESS) ?
3220 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3223 TALLOC_FREE(mem_ctx);
3227 /**********************************************************************
3228 * Update a group mapping entry. We're quite strict about what can be changed:
3229 * Only the description and displayname may be changed. It simply does not
3230 * make any sense to change the SID, gid or the type in a mapping.
3231 *********************************************************************/
3233 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3236 struct ldapsam_privates *ldap_state =
3237 (struct ldapsam_privates *)methods->private_data;
3239 const char *filter, *dn;
3240 LDAPMessage *msg = NULL;
3241 LDAPMessage *entry = NULL;
3242 LDAPMod **mods = NULL;
3243 TALLOC_CTX *mem_ctx;
3246 mem_ctx = talloc_new(NULL);
3247 if (mem_ctx == NULL) {
3248 DEBUG(0, ("talloc_new failed\n"));
3249 return NT_STATUS_NO_MEMORY;
3252 /* Make 100% sure that sid, gid and type are not changed by looking up
3253 * exactly the values we're given in LDAP. */
3255 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3256 "(sambaSid=%s)(gidNumber=%u)"
3257 "(sambaGroupType=%d))",
3259 sid_string_talloc(mem_ctx, &map->sid),
3260 (unsigned int)map->gid, map->sid_name_use);
3261 if (filter == NULL) {
3262 result = NT_STATUS_NO_MEMORY;
3266 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3267 get_attr_list(mem_ctx, groupmap_attr_list),
3269 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3271 if ((rc != LDAP_SUCCESS) ||
3272 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3274 ((entry = ldap_first_entry(
3275 smbldap_get_ldap(ldap_state->smbldap_state),
3277 result = NT_STATUS_NO_SUCH_GROUP;
3281 dn = smbldap_talloc_dn(
3282 mem_ctx, smbldap_get_ldap(ldap_state->smbldap_state), entry);
3285 result = NT_STATUS_NO_MEMORY;
3290 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3291 &mods, "displayName", map->nt_name);
3292 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3293 &mods, "description", map->comment);
3294 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3297 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3298 "nothing to do\n"));
3299 result = NT_STATUS_OK;
3303 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3305 if (rc != LDAP_SUCCESS) {
3306 result = NT_STATUS_ACCESS_DENIED;
3310 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3311 "group %lu in LDAP\n", (unsigned long)map->gid));
3313 result = NT_STATUS_OK;
3316 TALLOC_FREE(mem_ctx);
3320 /**********************************************************************
3321 *********************************************************************/
3323 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3326 struct ldapsam_privates *priv =
3327 (struct ldapsam_privates *)methods->private_data;
3328 LDAPMessage *msg, *entry;
3331 TALLOC_CTX *mem_ctx;
3334 mem_ctx = talloc_new(NULL);
3335 if (mem_ctx == NULL) {
3336 DEBUG(0, ("talloc_new failed\n"));
3337 return NT_STATUS_NO_MEMORY;
3340 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3341 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3342 sid_string_talloc(mem_ctx, &sid));
3343 if (filter == NULL) {
3344 result = NT_STATUS_NO_MEMORY;
3347 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3348 get_attr_list(mem_ctx, groupmap_attr_list),
3350 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3352 if ((rc != LDAP_SUCCESS) ||
3353 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3354 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3355 result = NT_STATUS_NO_SUCH_GROUP;
3359 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3360 get_attr_list(mem_ctx,
3361 groupmap_attr_list_to_delete));
3363 if ((rc == LDAP_NAMING_VIOLATION) ||
3364 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3365 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3366 const char *attrs[] = { "sambaGroupType", "description",
3367 "displayName", "sambaSIDList",
3370 /* Second try. Don't delete the sambaSID attribute, this is
3371 for "old" entries that are tacked on a winbind
3374 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3375 LDAP_OBJ_GROUPMAP, attrs);
3378 if ((rc == LDAP_NAMING_VIOLATION) ||
3379 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3380 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3381 const char *attrs[] = { "sambaGroupType", "description",
3382 "displayName", "sambaSIDList",
3383 "gidNumber", NULL };
3385 /* Third try. This is a post-3.0.21 alias (containing only
3386 * sambaSidEntry and sambaGroupMapping classes), we also have
3387 * to delete the gidNumber attribute, only the sambaSidEntry
3390 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3391 LDAP_OBJ_GROUPMAP, attrs);
3394 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3397 TALLOC_FREE(mem_ctx);
3401 /**********************************************************************
3402 *********************************************************************/
3404 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3407 struct ldapsam_privates *ldap_state =
3408 (struct ldapsam_privates *)my_methods->private_data;
3409 char *filter = NULL;
3411 const char **attr_list;
3413 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3415 return NT_STATUS_NO_MEMORY;
3417 attr_list = get_attr_list( NULL, groupmap_attr_list );
3418 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3419 LDAP_SCOPE_SUBTREE, filter,
3420 attr_list, 0, &ldap_state->result);
3421 TALLOC_FREE(attr_list);
3423 if (rc != LDAP_SUCCESS) {
3424 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3425 ldap_err2string(rc)));
3426 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3427 lp_ldap_suffix(talloc_tos()), filter));
3428 ldap_msgfree(ldap_state->result);
3429 ldap_state->result = NULL;
3430 TALLOC_FREE(filter);
3431 return NT_STATUS_UNSUCCESSFUL;
3434 TALLOC_FREE(filter);
3436 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3438 smbldap_get_ldap(ldap_state->smbldap_state),
3439 ldap_state->result)));
3442 ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3443 ldap_state->result);
3444 ldap_state->index = 0;
3446 return NT_STATUS_OK;
3449 /**********************************************************************
3450 *********************************************************************/
3452 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3454 ldapsam_endsampwent(my_methods);
3457 /**********************************************************************
3458 *********************************************************************/
3460 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3463 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3464 struct ldapsam_privates *ldap_state =
3465 (struct ldapsam_privates *)my_methods->private_data;
3469 if (!ldap_state->entry)
3472 ldap_state->index++;
3473 bret = init_group_from_ldap(ldap_state, map,
3476 ldap_state->entry = ldap_next_entry(
3477 smbldap_get_ldap(ldap_state->smbldap_state),
3481 return NT_STATUS_OK;
3484 /**********************************************************************
3485 *********************************************************************/
3487 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3488 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3489 GROUP_MAP ***pp_rmap,
3490 size_t *p_num_entries,
3493 GROUP_MAP *map = NULL;
3499 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3500 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3502 return NT_STATUS_ACCESS_DENIED;
3507 map = talloc_zero(NULL, GROUP_MAP);
3509 return NT_STATUS_NO_MEMORY;
3512 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3517 if (sid_name_use != SID_NAME_UNKNOWN &&
3518 sid_name_use != map->sid_name_use) {
3519 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3520 "not of the requested type\n",
3524 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3525 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3526 "non mapped\n", map->nt_name));
3530 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3531 GROUP_MAP *, entries + 1);
3533 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3534 "enlarge group map!\n"));
3535 return NT_STATUS_UNSUCCESSFUL;
3538 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3543 ldapsam_endsamgrent(methods);
3545 *p_num_entries = entries;
3547 return NT_STATUS_OK;
3550 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3551 const struct dom_sid *alias,
3552 const struct dom_sid *member,
3555 struct ldapsam_privates *ldap_state =
3556 (struct ldapsam_privates *)methods->private_data;
3558 LDAPMessage *result = NULL;
3559 LDAPMessage *entry = NULL;
3561 LDAPMod **mods = NULL;
3563 enum lsa_SidType type = SID_NAME_USE_NONE;
3566 char *filter = NULL;
3568 if (sid_check_is_in_builtin(alias)) {
3569 type = SID_NAME_ALIAS;
3572 if (sid_check_is_in_our_sam(alias)) {
3573 type = SID_NAME_ALIAS;
3576 if (type == SID_NAME_USE_NONE) {
3577 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3578 sid_string_dbg(alias)));
3579 return NT_STATUS_NO_SUCH_ALIAS;
3582 if (asprintf(&filter,
3583 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3584 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3586 return NT_STATUS_NO_MEMORY;
3589 if (ldapsam_search_one_group(ldap_state, filter,
3590 &result) != LDAP_SUCCESS) {
3592 return NT_STATUS_NO_SUCH_ALIAS;
3595 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3599 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3600 ldap_msgfree(result);
3602 return NT_STATUS_NO_SUCH_ALIAS;
3606 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3607 "filter %s: count=%d\n", filter, count));
3608 ldap_msgfree(result);
3610 return NT_STATUS_NO_SUCH_ALIAS;
3615 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3619 ldap_msgfree(result);
3620 return NT_STATUS_UNSUCCESSFUL;
3623 dn = smbldap_talloc_dn(talloc_tos(),
3624 smbldap_get_ldap(ldap_state->smbldap_state),
3627 ldap_msgfree(result);
3628 return NT_STATUS_UNSUCCESSFUL;
3631 smbldap_set_mod(&mods, modop,
3632 get_attr_key2string(groupmap_attr_list,
3633 LDAP_ATTR_SID_LIST),
3634 sid_to_fstring(tmp, member));
3636 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3638 ldap_mods_free(mods, True);
3639 ldap_msgfree(result);
3642 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3643 return NT_STATUS_MEMBER_IN_ALIAS;
3646 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3647 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3650 if (rc != LDAP_SUCCESS) {
3651 return NT_STATUS_UNSUCCESSFUL;
3654 return NT_STATUS_OK;
3657 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3658 const struct dom_sid *alias,
3659 const struct dom_sid *member)
3661 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3664 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3665 const struct dom_sid *alias,
3666 const struct dom_sid *member)
3668 return ldapsam_modify_aliasmem(methods, alias, member,
3672 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3673 const struct dom_sid *alias,
3674 TALLOC_CTX *mem_ctx,
3675 struct dom_sid **pp_members,
3676 size_t *p_num_members)
3678 struct ldapsam_privates *ldap_state =
3679 (struct ldapsam_privates *)methods->private_data;
3680 LDAPMessage *result = NULL;
3681 LDAPMessage *entry = NULL;
3683 char **values = NULL;
3685 char *filter = NULL;
3686 uint32_t num_members = 0;
3687 enum lsa_SidType type = SID_NAME_USE_NONE;
3693 if (sid_check_is_in_builtin(alias)) {
3694 type = SID_NAME_ALIAS;
3697 if (sid_check_is_in_our_sam(alias)) {
3698 type = SID_NAME_ALIAS;
3701 if (type == SID_NAME_USE_NONE) {
3702 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3703 sid_string_dbg(alias)));
3704 return NT_STATUS_NO_SUCH_ALIAS;
3707 if (asprintf(&filter,
3708 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3709 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3711 return NT_STATUS_NO_MEMORY;
3714 if (ldapsam_search_one_group(ldap_state, filter,
3715 &result) != LDAP_SUCCESS) {
3717 return NT_STATUS_NO_SUCH_ALIAS;
3720 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3724 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3725 ldap_msgfree(result);
3727 return NT_STATUS_NO_SUCH_ALIAS;
3731 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3732 "filter %s: count=%d\n", filter, count));
3733 ldap_msgfree(result);
3735 return NT_STATUS_NO_SUCH_ALIAS;
3740 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3744 ldap_msgfree(result);
3745 return NT_STATUS_UNSUCCESSFUL;
3748 values = ldap_get_values(smbldap_get_ldap(ldap_state->smbldap_state),
3750 get_attr_key2string(groupmap_attr_list,
3751 LDAP_ATTR_SID_LIST));
3753 if (values == NULL) {
3754 ldap_msgfree(result);
3755 return NT_STATUS_OK;
3758 count = ldap_count_values(values);
3760 for (i=0; i<count; i++) {
3761 struct dom_sid member;
3764 if (!string_to_sid(&member, values[i]))
3767 status = add_sid_to_array(mem_ctx, &member, pp_members,
3769 if (!NT_STATUS_IS_OK(status)) {
3770 ldap_value_free(values);
3771 ldap_msgfree(result);
3776 *p_num_members = num_members;
3777 ldap_value_free(values);
3778 ldap_msgfree(result);
3780 return NT_STATUS_OK;
3783 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3784 TALLOC_CTX *mem_ctx,
3785 const struct dom_sid *domain_sid,
3786 const struct dom_sid *members,
3788 uint32_t **pp_alias_rids,
3789 size_t *p_num_alias_rids)
3791 struct ldapsam_privates *ldap_state =
3792 (struct ldapsam_privates *)methods->private_data;
3795 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3797 LDAPMessage *result = NULL;
3798 LDAPMessage *entry = NULL;
3802 enum lsa_SidType type = SID_NAME_USE_NONE;
3803 bool is_builtin = false;
3804 bool sid_added = false;
3806 *pp_alias_rids = NULL;
3807 *p_num_alias_rids = 0;
3809 if (sid_check_is_builtin(domain_sid)) {
3811 type = SID_NAME_ALIAS;
3814 if (sid_check_is_our_sam(domain_sid)) {
3815 type = SID_NAME_ALIAS;
3818 if (type == SID_NAME_USE_NONE) {
3819 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3820 sid_string_dbg(domain_sid)));
3821 return NT_STATUS_UNSUCCESSFUL;
3824 if (num_members == 0) {
3825 return NT_STATUS_OK;
3828 filter = talloc_asprintf(mem_ctx,
3829 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3830 LDAP_OBJ_GROUPMAP, type);
3832 for (i=0; i<num_members; i++)
3833 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3835 sid_string_talloc(mem_ctx,
3838 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3840 if (filter == NULL) {
3841 return NT_STATUS_NO_MEMORY;
3845 ldap_state->search_cache.filter &&
3846 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3847 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3848 result = ldap_state->search_cache.result;
3849 ldap_state->search_cache.result = NULL;
3851 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3852 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3853 if (rc != LDAP_SUCCESS) {
3854 return NT_STATUS_UNSUCCESSFUL;
3856 smbldap_talloc_autofree_ldapmsg(filter, result);
3859 ldap_struct = smbldap_get_ldap(ldap_state->smbldap_state);
3861 for (entry = ldap_first_entry(ldap_struct, result);
3863 entry = ldap_next_entry(ldap_struct, entry))
3869 if (!smbldap_get_single_attribute(ldap_struct, entry,
3875 if (!string_to_sid(&sid, sid_str))
3878 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3883 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3884 p_num_alias_rids)) {
3885 return NT_STATUS_NO_MEMORY;
3889 if (!is_builtin && !sid_added) {
3890 TALLOC_FREE(ldap_state->search_cache.filter);
3892 * Note: result is a talloc child of filter because of the
3893 * smbldap_talloc_autofree_ldapmsg() usage
3895 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3896 ldap_state->search_cache.result = result;
3899 return NT_STATUS_OK;
3902 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3903 enum pdb_policy_type type,
3906 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3908 LDAPMod **mods = NULL;
3909 fstring value_string;
3910 const char *policy_attr = NULL;
3912 struct ldapsam_privates *ldap_state =
3913 (struct ldapsam_privates *)methods->private_data;
3915 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3917 if (!ldap_state->domain_dn) {
3918 return NT_STATUS_INVALID_PARAMETER;
3921 policy_attr = get_account_policy_attr(type);
3922 if (policy_attr == NULL) {
3923 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3928 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3930 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3932 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3935 ldap_mods_free(mods, True);
3937 if (rc != LDAP_SUCCESS) {
3941 if (!cache_account_policy_set(type, value)) {
3942 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3943 "update local tdb cache\n"));
3947 return NT_STATUS_OK;
3950 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3951 enum pdb_policy_type type,
3954 return ldapsam_set_account_policy_in_ldap(methods, type,
3958 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3959 enum pdb_policy_type type,
3962 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3963 LDAPMessage *result = NULL;
3964 LDAPMessage *entry = NULL;
3969 const char *policy_attr = NULL;
3971 struct ldapsam_privates *ldap_state =
3972 (struct ldapsam_privates *)methods->private_data;
3974 const char *attrs[2];
3976 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3978 if (!ldap_state->domain_dn) {
3979 return NT_STATUS_INVALID_PARAMETER;
3982 policy_attr = get_account_policy_attr(type);
3984 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3985 "policy index: %d\n", type));
3989 attrs[0] = policy_attr;
3992 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3993 if (filter == NULL) {
3994 return NT_STATUS_NO_MEMORY;
3996 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3997 LDAP_SCOPE_BASE, filter, attrs, 0,
3999 TALLOC_FREE(filter);
4000 if (rc != LDAP_SUCCESS) {
4004 count = ldap_count_entries(priv2ld(ldap_state), result);
4009 entry = ldap_first_entry(priv2ld(ldap_state), result);
4010 if (entry == NULL) {
4014 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4019 *value = (uint32_t)atol(vals[0]);
4021 ntstatus = NT_STATUS_OK;
4025 ldap_value_free(vals);
4026 ldap_msgfree(result);
4031 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4033 - if user hasn't decided to use account policies inside LDAP just reuse the
4036 - if there is a valid cache entry, return that
4037 - if there is an LDAP entry, update cache and return
4038 - otherwise set to default, update cache and return
4042 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4043 enum pdb_policy_type type,
4046 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4048 if (cache_account_policy_get(type, value)) {
4049 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4051 return NT_STATUS_OK;
4054 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4056 if (NT_STATUS_IS_OK(ntstatus)) {
4060 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4064 /* should we automagically migrate old tdb value here ? */
4065 if (account_policy_get(type, value))
4068 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4069 "default\n", type));
4072 if (!account_policy_get_default(type, value)) {
4078 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4079 if (!NT_STATUS_IS_OK(ntstatus)) {
4085 if (!cache_account_policy_set(type, *value)) {
4086 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4087 "tdb as a cache\n"));
4088 return NT_STATUS_UNSUCCESSFUL;
4091 return NT_STATUS_OK;
4094 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4095 const struct dom_sid *domain_sid,
4099 enum lsa_SidType *attrs)
4101 struct ldapsam_privates *ldap_state =
4102 (struct ldapsam_privates *)methods->private_data;
4103 LDAPMessage *msg = NULL;
4105 char *allsids = NULL;
4106 int i, rc, num_mapped;
4107 NTSTATUS result = NT_STATUS_NO_MEMORY;
4108 TALLOC_CTX *mem_ctx;
4112 mem_ctx = talloc_new(NULL);
4113 if (mem_ctx == NULL) {
4114 DEBUG(0, ("talloc_new failed\n"));
4118 if (!sid_check_is_builtin(domain_sid) &&
4119 !sid_check_is_our_sam(domain_sid)) {
4120 result = NT_STATUS_INVALID_PARAMETER;
4124 if (num_rids == 0) {
4125 result = NT_STATUS_NONE_MAPPED;
4129 for (i=0; i<num_rids; i++)
4130 attrs[i] = SID_NAME_UNKNOWN;
4132 allsids = talloc_strdup(mem_ctx, "");
4133 if (allsids == NULL) {
4137 for (i=0; i<num_rids; i++) {
4139 sid_compose(&sid, domain_sid, rids[i]);
4140 allsids = talloc_asprintf_append_buffer(
4141 allsids, "(sambaSid=%s)",
4142 sid_string_talloc(mem_ctx, &sid));
4143 if (allsids == NULL) {
4148 /* First look for users */
4152 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4154 filter = talloc_asprintf(
4155 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4156 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4158 if (filter == NULL) {
4162 rc = smbldap_search(ldap_state->smbldap_state,
4163 lp_ldap_user_suffix(talloc_tos()),
4164 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4166 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4169 if (rc != LDAP_SUCCESS)
4172 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4175 for (entry = ldap_first_entry(ld, msg);
4177 entry = ldap_next_entry(ld, entry)) {
4182 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4184 DEBUG(2, ("Could not find sid from ldap entry\n"));
4188 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4191 DEBUG(2, ("Could not retrieve uid attribute\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] = SID_NAME_USER;
4206 names[rid_index] = name;
4210 if (num_mapped == num_rids) {
4211 /* No need to look for groups anymore -- we're done */
4212 result = NT_STATUS_OK;
4216 /* Same game for groups */
4220 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4221 "sambaGroupType", NULL };
4223 filter = talloc_asprintf(
4224 mem_ctx, "(&(objectClass=%s)(|%s))",
4225 LDAP_OBJ_GROUPMAP, allsids);
4226 if (filter == NULL) {
4230 rc = smbldap_search(ldap_state->smbldap_state,
4231 lp_ldap_suffix(talloc_tos()),
4232 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4234 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4237 if (rc != LDAP_SUCCESS)
4240 /* ldap_struct might have changed due to a reconnect */
4242 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4244 /* For consistency checks, we already checked we're only domain or builtin */
4246 is_builtin = sid_check_is_builtin(domain_sid);
4248 for (entry = ldap_first_entry(ld, msg);
4250 entry = ldap_next_entry(ld, entry))
4255 enum lsa_SidType type;
4256 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4258 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4261 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4266 type = (enum lsa_SidType)atol(attr);
4268 /* Consistency checks */
4269 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4270 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4271 (type != SID_NAME_DOM_GRP)))) {
4272 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4275 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4277 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4281 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4284 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4286 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4290 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4295 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4296 if (rid == rids[rid_index])
4300 if (rid_index == num_rids) {
4301 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4305 attrs[rid_index] = type;
4306 names[rid_index] = attr;
4310 result = NT_STATUS_NONE_MAPPED;
4313 result = (num_mapped == num_rids) ?
4314 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4316 TALLOC_FREE(mem_ctx);
4320 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4322 char *filter = NULL;
4323 char *escaped = NULL;
4324 char *result = NULL;
4326 if (asprintf(&filter, "(&%s(objectclass=%s))",
4327 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4331 escaped = escape_ldap_string(talloc_tos(), username);
4332 if (escaped == NULL) goto done;
4334 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4338 TALLOC_FREE(escaped);
4343 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4347 const char **result;
4349 va_start(ap, mem_ctx);
4350 while (va_arg(ap, const char *) != NULL)
4354 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4358 va_start(ap, mem_ctx);
4359 for (i=0; i<num; i++) {
4360 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4361 if (result[i] == NULL) {
4362 talloc_free(result);
4373 struct ldap_search_state {
4374 struct smbldap_state *connection;
4376 uint32_t acct_flags;
4377 uint16_t group_type;
4384 void *pagedresults_cookie;
4386 LDAPMessage *entries, *current_entry;
4387 bool (*ldap2displayentry)(struct ldap_search_state *state,
4388 TALLOC_CTX *mem_ctx,
4389 LDAP *ld, LDAPMessage *entry,
4390 struct samr_displayentry *result);
4393 static bool ldapsam_search_firstpage(struct pdb_search *search)
4395 struct ldap_search_state *state =
4396 (struct ldap_search_state *)search->private_data;
4398 int rc = LDAP_OPERATIONS_ERROR;
4400 state->entries = NULL;
4402 if (smbldap_get_paged_results(state->connection)) {
4403 rc = smbldap_search_paged(state->connection, state->base,
4404 state->scope, state->filter,
4405 state->attrs, state->attrsonly,
4406 lp_ldap_page_size(), &state->entries,
4407 &state->pagedresults_cookie);
4410 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4412 if (state->entries != NULL) {
4413 /* Left over from unsuccessful paged attempt */
4414 ldap_msgfree(state->entries);
4415 state->entries = NULL;
4418 rc = smbldap_search(state->connection, state->base,
4419 state->scope, state->filter, state->attrs,
4420 state->attrsonly, &state->entries);
4422 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4425 /* Ok, the server was lying. It told us it could do paged
4426 * searches when it could not. */
4427 smbldap_set_paged_results(state->connection, false);
4430 ld = smbldap_get_ldap(state->connection);
4432 DEBUG(5, ("Don't have an LDAP connection right after a "
4436 state->current_entry = ldap_first_entry(ld, state->entries);
4441 static bool ldapsam_search_nextpage(struct pdb_search *search)
4443 struct ldap_search_state *state =
4444 (struct ldap_search_state *)search->private_data;
4447 if (!smbldap_get_paged_results(state->connection)) {
4448 /* There is no next page when there are no paged results */
4452 rc = smbldap_search_paged(state->connection, state->base,
4453 state->scope, state->filter, state->attrs,
4454 state->attrsonly, lp_ldap_page_size(),
4456 &state->pagedresults_cookie);
4458 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4461 state->current_entry = ldap_first_entry(
4462 smbldap_get_ldap(state->connection), state->entries);
4464 if (state->current_entry == NULL) {
4465 ldap_msgfree(state->entries);
4466 state->entries = NULL;
4473 static bool ldapsam_search_next_entry(struct pdb_search *search,
4474 struct samr_displayentry *entry)
4476 struct ldap_search_state *state =
4477 (struct ldap_search_state *)search->private_data;
4481 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4484 if ((state->entries == NULL) &&
4485 !ldapsam_search_nextpage(search))
4488 if (state->current_entry == NULL) {
4492 result = state->ldap2displayentry(state, search,
4493 smbldap_get_ldap(state->connection),
4494 state->current_entry, entry);
4498 dn = ldap_get_dn(smbldap_get_ldap(state->connection),
4499 state->current_entry);
4500 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4501 if (dn != NULL) ldap_memfree(dn);
4504 state->current_entry = ldap_next_entry(
4505 smbldap_get_ldap(state->connection), state->current_entry);
4507 if (state->current_entry == NULL) {
4508 ldap_msgfree(state->entries);
4509 state->entries = NULL;
4512 if (!result) goto retry;
4517 static void ldapsam_search_end(struct pdb_search *search)
4519 struct ldap_search_state *state =
4520 (struct ldap_search_state *)search->private_data;
4523 if (state->pagedresults_cookie == NULL)
4526 if (state->entries != NULL)
4527 ldap_msgfree(state->entries);
4529 state->entries = NULL;
4530 state->current_entry = NULL;
4532 if (!smbldap_get_paged_results(state->connection)) {
4536 /* Tell the LDAP server we're not interested in the rest anymore. */
4538 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4539 state->filter, state->attrs,
4540 state->attrsonly, 0, &state->entries,
4541 &state->pagedresults_cookie);
4543 if (rc != LDAP_SUCCESS)
4544 DEBUG(5, ("Could not end search properly\n"));
4549 static bool ldapuser2displayentry(struct ldap_search_state *state,
4550 TALLOC_CTX *mem_ctx,
4551 LDAP *ld, LDAPMessage *entry,
4552 struct samr_displayentry *result)
4555 size_t converted_size;
4557 uint32_t acct_flags;
4559 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4560 if ((vals == NULL) || (vals[0] == NULL)) {
4561 acct_flags = ACB_NORMAL;
4563 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4564 ldap_value_free(vals);
4567 if ((state->acct_flags != 0) &&
4568 ((state->acct_flags & acct_flags) == 0))
4571 result->acct_flags = acct_flags;
4572 result->account_name = "";
4573 result->fullname = "";
4574 result->description = "";
4576 vals = ldap_get_values(ld, entry, "uid");
4577 if ((vals == NULL) || (vals[0] == NULL)) {
4578 DEBUG(5, ("\"uid\" not found\n"));
4581 if (!pull_utf8_talloc(mem_ctx,
4582 discard_const_p(char *, &result->account_name),
4583 vals[0], &converted_size))
4585 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4589 ldap_value_free(vals);
4591 vals = ldap_get_values(ld, entry, "displayName");
4592 if ((vals == NULL) || (vals[0] == NULL))
4593 DEBUG(8, ("\"displayName\" not found\n"));
4594 else if (!pull_utf8_talloc(mem_ctx,
4595 discard_const_p(char *, &result->fullname),
4596 vals[0], &converted_size))
4598 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4602 ldap_value_free(vals);
4604 vals = ldap_get_values(ld, entry, "description");
4605 if ((vals == NULL) || (vals[0] == NULL))
4606 DEBUG(8, ("\"description\" not found\n"));
4607 else if (!pull_utf8_talloc(mem_ctx,
4608 discard_const_p(char *, &result->description),
4609 vals[0], &converted_size))
4611 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4615 ldap_value_free(vals);
4617 if ((result->account_name == NULL) ||
4618 (result->fullname == NULL) ||
4619 (result->description == NULL)) {
4620 DEBUG(0, ("talloc failed\n"));
4624 vals = ldap_get_values(ld, entry, "sambaSid");
4625 if ((vals == NULL) || (vals[0] == NULL)) {
4626 DEBUG(0, ("\"objectSid\" not found\n"));
4630 if (!string_to_sid(&sid, vals[0])) {
4631 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4632 ldap_value_free(vals);
4635 ldap_value_free(vals);
4637 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4638 DEBUG(0, ("sid %s does not belong to our domain\n",
4639 sid_string_dbg(&sid)));
4647 static bool ldapsam_search_users(struct pdb_methods *methods,
4648 struct pdb_search *search,
4649 uint32_t acct_flags)
4651 struct ldapsam_privates *ldap_state =
4652 (struct ldapsam_privates *)methods->private_data;
4653 struct ldap_search_state *state;
4655 state = talloc(search, struct ldap_search_state);
4656 if (state == NULL) {
4657 DEBUG(0, ("talloc failed\n"));
4661 state->connection = ldap_state->smbldap_state;
4663 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4664 state->base = lp_ldap_user_suffix(talloc_tos());
4665 else if ((acct_flags != 0) &&
4666 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4667 state->base = lp_ldap_machine_suffix(talloc_tos());
4669 state->base = lp_ldap_suffix(talloc_tos());
4671 state->acct_flags = acct_flags;
4672 state->base = talloc_strdup(search, state->base);
4673 state->scope = LDAP_SCOPE_SUBTREE;
4674 state->filter = get_ldap_filter(search, "*");
4675 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4676 "displayName", "description",
4677 "sambaAcctFlags", NULL);
4678 state->attrsonly = 0;
4679 state->pagedresults_cookie = NULL;
4680 state->entries = NULL;
4681 state->ldap2displayentry = ldapuser2displayentry;
4683 if ((state->filter == NULL) || (state->attrs == NULL)) {
4684 DEBUG(0, ("talloc failed\n"));
4688 search->private_data = state;
4689 search->next_entry = ldapsam_search_next_entry;
4690 search->search_end = ldapsam_search_end;
4692 return ldapsam_search_firstpage(search);
4695 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4696 TALLOC_CTX *mem_ctx,
4697 LDAP *ld, LDAPMessage *entry,
4698 struct samr_displayentry *result)
4701 size_t converted_size;
4703 uint16_t group_type;
4705 result->account_name = "";
4706 result->fullname = "";
4707 result->description = "";
4710 vals = ldap_get_values(ld, entry, "sambaGroupType");
4711 if ((vals == NULL) || (vals[0] == NULL)) {
4712 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4714 ldap_value_free(vals);
4719 group_type = atoi(vals[0]);
4721 if ((state->group_type != 0) &&
4722 ((state->group_type != group_type))) {
4723 ldap_value_free(vals);
4727 ldap_value_free(vals);
4729 /* display name is the NT group name */
4731 vals = ldap_get_values(ld, entry, "displayName");
4732 if ((vals == NULL) || (vals[0] == NULL)) {
4733 DEBUG(8, ("\"displayName\" not found\n"));
4735 /* fallback to the 'cn' attribute */
4736 vals = ldap_get_values(ld, entry, "cn");
4737 if ((vals == NULL) || (vals[0] == NULL)) {
4738 DEBUG(5, ("\"cn\" not found\n"));
4741 if (!pull_utf8_talloc(mem_ctx,
4742 discard_const_p(char *,
4743 &result->account_name),
4744 vals[0], &converted_size))
4746 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4747 "failed: %s", strerror(errno)));
4750 else if (!pull_utf8_talloc(mem_ctx,
4751 discard_const_p(char *,
4752 &result->account_name),
4753 vals[0], &converted_size))
4755 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4759 ldap_value_free(vals);
4761 vals = ldap_get_values(ld, entry, "description");
4762 if ((vals == NULL) || (vals[0] == NULL))
4763 DEBUG(8, ("\"description\" not found\n"));
4764 else if (!pull_utf8_talloc(mem_ctx,
4765 discard_const_p(char *, &result->description),
4766 vals[0], &converted_size))
4768 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4771 ldap_value_free(vals);
4773 if ((result->account_name == NULL) ||
4774 (result->fullname == NULL) ||
4775 (result->description == NULL)) {
4776 DEBUG(0, ("talloc failed\n"));
4780 vals = ldap_get_values(ld, entry, "sambaSid");
4781 if ((vals == NULL) || (vals[0] == NULL)) {
4782 DEBUG(0, ("\"objectSid\" not found\n"));
4784 ldap_value_free(vals);
4789 if (!string_to_sid(&sid, vals[0])) {
4790 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4794 ldap_value_free(vals);
4796 switch (group_type) {
4797 case SID_NAME_DOM_GRP:
4798 case SID_NAME_ALIAS:
4800 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4801 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4803 DEBUG(0, ("%s is not in our domain\n",
4804 sid_string_dbg(&sid)));
4810 DEBUG(0,("unknown group type: %d\n", group_type));
4814 result->acct_flags = 0;
4819 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4820 struct pdb_search *search,
4821 const struct dom_sid *sid,
4822 enum lsa_SidType type)
4824 struct ldapsam_privates *ldap_state =
4825 (struct ldapsam_privates *)methods->private_data;
4826 struct ldap_search_state *state;
4829 state = talloc(search, struct ldap_search_state);
4830 if (state == NULL) {
4831 DEBUG(0, ("talloc failed\n"));
4835 state->connection = ldap_state->smbldap_state;
4837 state->base = lp_ldap_suffix(search);
4838 state->connection = ldap_state->smbldap_state;
4839 state->scope = LDAP_SCOPE_SUBTREE;
4840 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4841 "(sambaGroupType=%d)(sambaSID=%s*))",
4843 type, sid_to_fstring(tmp, sid));
4844 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4845 "displayName", "description",
4846 "sambaGroupType", NULL);
4847 state->attrsonly = 0;
4848 state->pagedresults_cookie = NULL;
4849 state->entries = NULL;
4850 state->group_type = type;
4851 state->ldap2displayentry = ldapgroup2displayentry;
4853 if ((state->filter == NULL) || (state->attrs == NULL)) {
4854 DEBUG(0, ("talloc failed\n"));
4858 search->private_data = state;
4859 search->next_entry = ldapsam_search_next_entry;
4860 search->search_end = ldapsam_search_end;
4862 return ldapsam_search_firstpage(search);
4865 static bool ldapsam_search_groups(struct pdb_methods *methods,
4866 struct pdb_search *search)
4868 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4871 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4872 struct pdb_search *search,
4873 const struct dom_sid *sid)
4875 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4878 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4880 return PDB_CAP_STORE_RIDS;
4883 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4886 struct smbldap_state *smbldap_state = priv->smbldap_state;
4888 LDAPMessage *result = NULL;
4889 LDAPMessage *entry = NULL;
4890 LDAPMod **mods = NULL;
4894 uint32_t nextRid = 0;
4897 TALLOC_CTX *mem_ctx;
4899 mem_ctx = talloc_new(NULL);
4900 if (mem_ctx == NULL) {
4901 DEBUG(0, ("talloc_new failed\n"));
4902 return NT_STATUS_NO_MEMORY;
4905 status = smbldap_search_domain_info(smbldap_state, &result,
4906 get_global_sam_name(), False);
4907 if (!NT_STATUS_IS_OK(status)) {
4908 DEBUG(3, ("Could not get domain info: %s\n",
4909 nt_errstr(status)));
4913 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
4915 entry = ldap_first_entry(priv2ld(priv), result);
4916 if (entry == NULL) {
4917 DEBUG(0, ("Could not get domain info entry\n"));
4918 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4922 /* Find the largest of the three attributes "sambaNextRid",
4923 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4924 concept of differentiating between user and group rids, and will
4925 use only "sambaNextRid" in the future. But for compatibility
4926 reasons I look if others have chosen different strategies -- VL */
4928 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4929 "sambaNextRid", mem_ctx);
4930 if (value != NULL) {
4931 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4932 nextRid = MAX(nextRid, tmp);
4935 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4936 "sambaNextUserRid", mem_ctx);
4937 if (value != NULL) {
4938 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4939 nextRid = MAX(nextRid, tmp);
4942 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4943 "sambaNextGroupRid", mem_ctx);
4944 if (value != NULL) {
4945 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4946 nextRid = MAX(nextRid, tmp);
4950 nextRid = BASE_RID-1;
4955 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4956 talloc_asprintf(mem_ctx, "%d", nextRid));
4957 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
4959 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4960 status = NT_STATUS_NO_MEMORY;
4964 rc = smbldap_modify(smbldap_state, dn, mods);
4966 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4969 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4972 if (NT_STATUS_IS_OK(status)) {
4976 TALLOC_FREE(mem_ctx);
4980 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4984 for (i=0; i<10; i++) {
4985 NTSTATUS result = ldapsam_get_new_rid(
4986 (struct ldapsam_privates *)methods->private_data, rid);
4987 if (NT_STATUS_IS_OK(result)) {
4991 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4995 /* The ldap update failed (maybe a race condition), retry */
4998 /* Tried 10 times, fail. */
4999 return NT_STATUS_ACCESS_DENIED;
5002 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
5004 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
5005 return NT_STATUS_IS_OK(result) ? True : False;
5008 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
5009 const struct dom_sid *sid,
5012 struct ldapsam_privates *priv =
5013 (struct ldapsam_privates *)methods->private_data;
5015 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5017 LDAPMessage *result = NULL;
5018 LDAPMessage *entry = NULL;
5023 TALLOC_CTX *mem_ctx;
5025 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
5030 mem_ctx = talloc_new(NULL);
5031 if (mem_ctx == NULL) {
5032 DEBUG(0, ("talloc_new failed\n"));
5036 filter = talloc_asprintf(mem_ctx,
5038 "(|(objectClass=%s)(objectClass=%s)))",
5039 sid_string_talloc(mem_ctx, sid),
5040 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5041 if (filter == NULL) {
5042 DEBUG(5, ("talloc_asprintf failed\n"));
5046 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5048 if (rc != LDAP_SUCCESS) {
5051 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
5053 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5054 DEBUG(10, ("Got %d entries, expected one\n",
5055 ldap_count_entries(priv2ld(priv), result)));
5059 entry = ldap_first_entry(priv2ld(priv), result);
5061 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5062 "sambaGroupType", mem_ctx);
5064 if (value != NULL) {
5065 const char *gid_str;
5068 gid_str = smbldap_talloc_single_attribute(
5069 priv2ld(priv), entry, "gidNumber", mem_ctx);
5070 if (gid_str == NULL) {
5071 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5072 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5077 id->id = strtoul(gid_str, NULL, 10);
5078 id->type = ID_TYPE_GID;
5083 /* It must be a user */
5085 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5086 "uidNumber", mem_ctx);
5087 if (value == NULL) {
5088 DEBUG(1, ("Could not find uidNumber in %s\n",
5089 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5093 id->id = strtoul(value, NULL, 10);
5094 id->type = ID_TYPE_UID;
5098 TALLOC_FREE(mem_ctx);
5103 * Find the SID for a uid.
5104 * This is shortcut is only used if ldapsam:trusted is set to true.
5106 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5107 struct dom_sid *sid)
5109 struct ldapsam_privates *priv =
5110 (struct ldapsam_privates *)methods->private_data;
5112 const char *attrs[] = { "sambaSID", NULL };
5113 LDAPMessage *result = NULL;
5114 LDAPMessage *entry = NULL;
5116 char *user_sid_string;
5117 struct dom_sid user_sid;
5119 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5121 filter = talloc_asprintf(tmp_ctx,
5124 "(objectClass=%s))",
5126 LDAP_OBJ_POSIXACCOUNT,
5127 LDAP_OBJ_SAMBASAMACCOUNT);
5128 if (filter == NULL) {
5129 DEBUG(3, ("talloc_asprintf failed\n"));
5133 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5134 if (rc != LDAP_SUCCESS) {
5137 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5139 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5140 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5141 ldap_count_entries(priv2ld(priv), result),
5142 (unsigned int)uid));
5146 entry = ldap_first_entry(priv2ld(priv), result);
5148 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5149 "sambaSID", tmp_ctx);
5150 if (user_sid_string == NULL) {
5151 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5152 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5156 if (!string_to_sid(&user_sid, user_sid_string)) {
5157 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5162 sid_copy(sid, &user_sid);
5167 TALLOC_FREE(tmp_ctx);
5172 * Find the SID for a gid.
5173 * This is shortcut is only used if ldapsam:trusted is set to true.
5175 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5176 struct dom_sid *sid)
5178 struct ldapsam_privates *priv =
5179 (struct ldapsam_privates *)methods->private_data;
5181 const char *attrs[] = { "sambaSID", NULL };
5182 LDAPMessage *result = NULL;
5183 LDAPMessage *entry = NULL;
5185 char *group_sid_string;
5186 struct dom_sid group_sid;
5188 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5190 filter = talloc_asprintf(tmp_ctx,
5192 "(objectClass=%s))",
5195 if (filter == NULL) {
5196 DEBUG(3, ("talloc_asprintf failed\n"));
5200 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5201 if (rc != LDAP_SUCCESS) {
5204 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5206 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5207 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5208 ldap_count_entries(priv2ld(priv), result),
5209 (unsigned int)gid));
5213 entry = ldap_first_entry(priv2ld(priv), result);
5215 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5216 "sambaSID", tmp_ctx);
5217 if (group_sid_string == NULL) {
5218 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5219 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5223 if (!string_to_sid(&group_sid, group_sid_string)) {
5224 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5229 sid_copy(sid, &group_sid);
5234 TALLOC_FREE(tmp_ctx);
5238 static bool ldapsam_id_to_sid(struct pdb_methods *methods, struct unixid *id,
5239 struct dom_sid *sid)
5243 return ldapsam_uid_to_sid(methods, id->id, sid);
5246 return ldapsam_gid_to_sid(methods, id->id, sid);
5255 * The following functions are called only if
5256 * ldapsam:trusted and ldapsam:editposix are
5261 * ldapsam_create_user creates a new
5262 * posixAccount and sambaSamAccount object
5263 * in the ldap users subtree
5265 * The uid is allocated by winbindd.
5268 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5269 TALLOC_CTX *tmp_ctx, const char *name,
5270 uint32_t acb_info, uint32_t *rid)
5272 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5273 LDAPMessage *entry = NULL;
5274 LDAPMessage *result = NULL;
5275 uint32_t num_result;
5276 bool is_machine = False;
5277 bool add_posix = False;
5278 bool init_okay = False;
5279 LDAPMod **mods = NULL;
5287 const char *dn = NULL;
5288 struct dom_sid group_sid;
5289 struct dom_sid user_sid;
5295 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5296 acb_info & ACB_WSTRUST ||
5297 acb_info & ACB_SVRTRUST ||
5298 acb_info & ACB_DOMTRUST) {
5302 username = escape_ldap_string(talloc_tos(), name);
5303 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5304 username, LDAP_OBJ_POSIXACCOUNT);
5305 TALLOC_FREE(username);
5307 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5308 if (rc != LDAP_SUCCESS) {
5309 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5310 return NT_STATUS_ACCESS_DENIED;
5312 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5314 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5316 if (num_result > 1) {
5317 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5318 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5321 if (num_result == 1) {
5323 /* check if it is just a posix account.
5324 * or if there is a sid attached to this entry
5327 entry = ldap_first_entry(priv2ld(ldap_state), result);
5329 return NT_STATUS_UNSUCCESSFUL;
5332 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5334 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5335 return NT_STATUS_USER_EXISTS;
5338 /* it is just a posix account, retrieve the dn for later use */
5339 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5341 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5342 return NT_STATUS_NO_MEMORY;
5346 if (num_result == 0) {
5350 /* Create the basic samu structure and generate the mods for the ldap commit */
5351 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5352 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5356 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5358 user = samu_new(tmp_ctx);
5360 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5361 return NT_STATUS_NO_MEMORY;
5364 if (!pdb_set_username(user, name, PDB_SET)) {
5365 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5366 return NT_STATUS_UNSUCCESSFUL;
5368 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5369 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5370 return NT_STATUS_UNSUCCESSFUL;
5373 if (acb_info & ACB_NORMAL) {
5374 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5375 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5376 return NT_STATUS_UNSUCCESSFUL;
5379 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5380 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5381 return NT_STATUS_UNSUCCESSFUL;
5385 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5386 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5387 return NT_STATUS_UNSUCCESSFUL;
5391 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5392 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5393 return NT_STATUS_UNSUCCESSFUL;
5396 init_okay = init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed);
5399 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5400 ldap_mods_free(mods, true);
5401 return NT_STATUS_UNSUCCESSFUL;
5404 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5405 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5407 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5412 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5414 /* retrieve the Domain Users group gid */
5415 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5416 !sid_to_gid(&group_sid, &gid)) {
5417 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5418 ldap_mods_free(mods, true);
5419 return NT_STATUS_INVALID_PRIMARY_GROUP;
5422 /* lets allocate a new userid for this user */
5423 if (!winbind_allocate_uid(&uid)) {
5424 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5425 ldap_mods_free(mods, true);
5426 return NT_STATUS_UNSUCCESSFUL;
5431 /* TODO: choose a more appropriate default for machines */
5432 homedir = talloc_sub_specified(tmp_ctx,
5433 lp_template_homedir(),
5434 "SMB_workstations_home",
5436 ldap_state->domain_name,
5439 shell = talloc_strdup(tmp_ctx, "/bin/false");
5441 homedir = talloc_sub_specified(tmp_ctx,
5442 lp_template_homedir(),
5445 ldap_state->domain_name,
5448 shell = talloc_sub_specified(tmp_ctx,
5449 lp_template_shell(),
5452 ldap_state->domain_name,
5456 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5457 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5459 escape_name = escape_rdn_val_string_alloc(name);
5461 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5462 ldap_mods_free(mods, true);
5463 return NT_STATUS_NO_MEMORY;
5467 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix (talloc_tos()));
5469 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix (talloc_tos()));
5472 SAFE_FREE(escape_name);
5474 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5475 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5476 ldap_mods_free(mods, true);
5477 return NT_STATUS_NO_MEMORY;
5480 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5481 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5482 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5483 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5484 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5485 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5486 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5490 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5492 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5495 ldap_mods_free(mods, true);
5497 if (rc != LDAP_SUCCESS) {
5498 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5499 return NT_STATUS_UNSUCCESSFUL;
5502 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5504 flush_pwnam_cache();
5506 return NT_STATUS_OK;
5509 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5511 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5512 LDAPMessage *result = NULL;
5513 LDAPMessage *entry = NULL;
5519 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5521 filter = talloc_asprintf(tmp_ctx,
5524 "(objectClass=%s))",
5525 pdb_get_username(sam_acct),
5526 LDAP_OBJ_POSIXACCOUNT,
5527 LDAP_OBJ_SAMBASAMACCOUNT);
5528 if (filter == NULL) {
5529 return NT_STATUS_NO_MEMORY;
5532 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5533 if (rc != LDAP_SUCCESS) {
5534 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5535 return NT_STATUS_UNSUCCESSFUL;
5537 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5539 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5541 if (num_result == 0) {
5542 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5543 return NT_STATUS_NO_SUCH_USER;
5546 if (num_result > 1) {
5547 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5548 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5551 entry = ldap_first_entry(priv2ld(ldap_state), result);
5553 return NT_STATUS_UNSUCCESSFUL;
5556 /* it is just a posix account, retrieve the dn for later use */
5557 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5559 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5560 return NT_STATUS_NO_MEMORY;
5563 /* try to remove memberships first */
5566 struct dom_sid *sids = NULL;
5568 uint32_t num_groups = 0;
5570 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5572 status = ldapsam_enum_group_memberships(my_methods,
5578 if (!NT_STATUS_IS_OK(status)) {
5582 for (i=0; i < num_groups; i++) {
5586 sid_peek_rid(&sids[i], &group_rid);
5588 ldapsam_del_groupmem(my_methods,
5597 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5598 if (rc != LDAP_SUCCESS) {
5599 return NT_STATUS_UNSUCCESSFUL;
5602 flush_pwnam_cache();
5604 return NT_STATUS_OK;
5608 * ldapsam_create_group creates a new
5609 * posixGroup and sambaGroupMapping object
5610 * in the ldap groups subtree
5612 * The gid is allocated by winbindd.
5615 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5616 TALLOC_CTX *tmp_ctx,
5620 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5622 LDAPMessage *entry = NULL;
5623 LDAPMessage *result = NULL;
5624 uint32_t num_result;
5625 bool is_new_entry = False;
5626 LDAPMod **mods = NULL;
5632 const char *dn = NULL;
5633 struct dom_sid group_sid;
5637 groupname = escape_ldap_string(talloc_tos(), name);
5638 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5639 groupname, LDAP_OBJ_POSIXGROUP);
5640 TALLOC_FREE(groupname);
5642 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5643 if (rc != LDAP_SUCCESS) {
5644 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5645 return NT_STATUS_UNSUCCESSFUL;
5647 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5649 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5651 if (num_result > 1) {
5652 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5653 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5656 if (num_result == 1) {
5658 /* check if it is just a posix group.
5659 * or if there is a sid attached to this entry
5662 entry = ldap_first_entry(priv2ld(ldap_state), result);
5664 return NT_STATUS_UNSUCCESSFUL;
5667 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5669 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5670 return NT_STATUS_GROUP_EXISTS;
5673 /* it is just a posix group, retrieve the gid and the dn for later use */
5674 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5676 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5677 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5680 gid = strtoul(tmp, NULL, 10);
5682 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5684 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5685 return NT_STATUS_NO_MEMORY;
5689 if (num_result == 0) {
5690 is_new_entry = true;
5693 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5694 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5698 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5700 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5702 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5704 if (!groupsidstr || !grouptype) {
5705 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5706 return NT_STATUS_NO_MEMORY;
5709 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5710 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5711 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5712 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5717 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5719 /* lets allocate a new groupid for this group */
5720 if (!winbind_allocate_gid(&gid)) {
5721 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5722 return NT_STATUS_UNSUCCESSFUL;
5725 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5727 escape_name = escape_rdn_val_string_alloc(name);
5729 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5730 return NT_STATUS_NO_MEMORY;
5733 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix(talloc_tos()));
5735 SAFE_FREE(escape_name);
5737 if (!gidstr || !dn) {
5738 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5739 return NT_STATUS_NO_MEMORY;
5742 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5743 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5744 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5747 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
5750 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5752 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5753 /* This call may fail with rfc2307bis schema */
5754 /* Retry adding a structural class */
5755 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5756 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5760 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5763 if (rc != LDAP_SUCCESS) {
5764 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5765 return NT_STATUS_UNSUCCESSFUL;
5768 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5770 return NT_STATUS_OK;
5773 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5775 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5776 LDAPMessage *result = NULL;
5777 LDAPMessage *entry = NULL;
5782 struct dom_sid group_sid;
5785 /* get the group sid */
5786 sid_compose(&group_sid, get_global_sam_sid(), rid);
5788 filter = talloc_asprintf(tmp_ctx,
5791 "(objectClass=%s))",
5792 sid_string_talloc(tmp_ctx, &group_sid),
5793 LDAP_OBJ_POSIXGROUP,
5795 if (filter == NULL) {
5796 return NT_STATUS_NO_MEMORY;
5799 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5800 if (rc != LDAP_SUCCESS) {
5801 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5802 return NT_STATUS_UNSUCCESSFUL;
5804 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5806 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5808 if (num_result == 0) {
5809 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5810 return NT_STATUS_NO_SUCH_GROUP;
5813 if (num_result > 1) {
5814 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5815 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5818 entry = ldap_first_entry(priv2ld(ldap_state), result);
5820 return NT_STATUS_UNSUCCESSFUL;
5823 /* here it is, retrieve the dn for later use */
5824 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5826 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5827 return NT_STATUS_NO_MEMORY;
5830 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5832 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5833 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5836 /* check no user have this group marked as primary group */
5837 filter = talloc_asprintf(tmp_ctx,
5840 "(objectClass=%s))",
5842 LDAP_OBJ_POSIXACCOUNT,
5843 LDAP_OBJ_SAMBASAMACCOUNT);
5845 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5846 if (rc != LDAP_SUCCESS) {
5847 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5848 return NT_STATUS_UNSUCCESSFUL;
5850 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5852 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5854 if (num_result != 0) {
5855 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5856 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5859 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5860 if (rc != LDAP_SUCCESS) {
5861 return NT_STATUS_UNSUCCESSFUL;
5864 return NT_STATUS_OK;
5867 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5868 TALLOC_CTX *tmp_ctx,
5870 uint32_t member_rid,
5873 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5874 LDAPMessage *entry = NULL;
5875 LDAPMessage *result = NULL;
5876 uint32_t num_result;
5877 LDAPMod **mods = NULL;
5880 const char *dn = NULL;
5881 struct dom_sid group_sid;
5882 struct dom_sid member_sid;
5887 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5889 case LDAP_MOD_DELETE:
5890 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5893 return NT_STATUS_UNSUCCESSFUL;
5896 /* get member sid */
5897 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5899 /* get the group sid */
5900 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5902 filter = talloc_asprintf(tmp_ctx,
5905 "(objectClass=%s))",
5906 sid_string_talloc(tmp_ctx, &member_sid),
5907 LDAP_OBJ_POSIXACCOUNT,
5908 LDAP_OBJ_SAMBASAMACCOUNT);
5909 if (filter == NULL) {
5910 return NT_STATUS_NO_MEMORY;
5913 /* get the member uid */
5914 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5915 if (rc != LDAP_SUCCESS) {
5916 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5917 return NT_STATUS_UNSUCCESSFUL;
5919 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5921 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5923 if (num_result == 0) {
5924 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5925 return NT_STATUS_NO_SUCH_MEMBER;
5928 if (num_result > 1) {
5929 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5930 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5933 entry = ldap_first_entry(priv2ld(ldap_state), result);
5935 return NT_STATUS_UNSUCCESSFUL;
5938 if (modop == LDAP_MOD_DELETE) {
5939 /* check if we are trying to remove the member from his primary group */
5941 gid_t user_gid, group_gid;
5943 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5945 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5946 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5949 user_gid = strtoul(gidstr, NULL, 10);
5951 if (!sid_to_gid(&group_sid, &group_gid)) {
5952 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5953 return NT_STATUS_UNSUCCESSFUL;
5956 if (user_gid == group_gid) {
5957 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5958 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5962 /* here it is, retrieve the uid for later use */
5963 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5965 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5966 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5969 filter = talloc_asprintf(tmp_ctx,
5972 "(objectClass=%s))",
5973 sid_string_talloc(tmp_ctx, &group_sid),
5974 LDAP_OBJ_POSIXGROUP,
5978 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5979 if (rc != LDAP_SUCCESS) {
5980 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5981 return NT_STATUS_UNSUCCESSFUL;
5983 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5985 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5987 if (num_result == 0) {
5988 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5989 return NT_STATUS_NO_SUCH_GROUP;
5992 if (num_result > 1) {
5993 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5994 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5997 entry = ldap_first_entry(priv2ld(ldap_state), result);
5999 return NT_STATUS_UNSUCCESSFUL;
6002 /* here it is, retrieve the dn for later use */
6003 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
6005 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6006 return NT_STATUS_NO_MEMORY;
6009 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
6011 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
6013 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6014 if (rc != LDAP_SUCCESS) {
6015 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
6016 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6017 return NT_STATUS_MEMBER_IN_GROUP;
6019 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
6020 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6021 return NT_STATUS_MEMBER_NOT_IN_GROUP;
6023 return NT_STATUS_UNSUCCESSFUL;
6026 return NT_STATUS_OK;
6029 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
6030 TALLOC_CTX *tmp_ctx,
6032 uint32_t member_rid)
6034 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
6036 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
6037 TALLOC_CTX *tmp_ctx,
6039 uint32_t member_rid)
6041 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
6044 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
6045 TALLOC_CTX *mem_ctx,
6046 struct samu *sampass)
6048 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6049 LDAPMessage *entry = NULL;
6050 LDAPMessage *result = NULL;
6051 uint32_t num_result;
6052 LDAPMod **mods = NULL;
6054 char *escape_username;
6056 const char *dn = NULL;
6060 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6062 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6063 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6064 return NT_STATUS_UNSUCCESSFUL;
6066 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6068 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6069 return NT_STATUS_NO_MEMORY;
6072 escape_username = escape_ldap_string(talloc_tos(),
6073 pdb_get_username(sampass));
6074 if (escape_username== NULL) {
6075 return NT_STATUS_NO_MEMORY;
6078 filter = talloc_asprintf(mem_ctx,
6081 "(objectClass=%s))",
6083 LDAP_OBJ_POSIXACCOUNT,
6084 LDAP_OBJ_SAMBASAMACCOUNT);
6086 TALLOC_FREE(escape_username);
6088 if (filter == NULL) {
6089 return NT_STATUS_NO_MEMORY;
6092 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6093 if (rc != LDAP_SUCCESS) {
6094 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6095 return NT_STATUS_UNSUCCESSFUL;
6097 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6099 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6101 if (num_result == 0) {
6102 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6103 return NT_STATUS_NO_SUCH_USER;
6106 if (num_result > 1) {
6107 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6108 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6111 entry = ldap_first_entry(priv2ld(ldap_state), result);
6113 return NT_STATUS_UNSUCCESSFUL;
6116 /* retrieve the dn for later use */
6117 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6119 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6120 return NT_STATUS_NO_MEMORY;
6123 /* remove the old one, and add the new one, this way we do not risk races */
6124 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6127 return NT_STATUS_OK;
6130 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6132 if (rc != LDAP_SUCCESS) {
6133 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6134 pdb_get_username(sampass), gidstr));
6135 return NT_STATUS_UNSUCCESSFUL;
6138 flush_pwnam_cache();
6140 return NT_STATUS_OK;
6144 /**********************************************************************
6145 trusted domains functions
6146 *********************************************************************/
6148 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6151 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6152 ldap_state->domain_dn);
6155 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6156 TALLOC_CTX *mem_ctx,
6157 const char *domain, LDAPMessage **entry)
6161 int scope = LDAP_SCOPE_SUBTREE;
6162 const char **attrs = NULL; /* NULL: get all attrs */
6163 int attrsonly = 0; /* 0: return values too */
6164 LDAPMessage *result = NULL;
6166 uint32_t num_result;
6168 filter = talloc_asprintf(talloc_tos(),
6169 "(&(objectClass=%s)(sambaDomainName=%s))",
6170 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6172 trusted_dn = trusteddom_dn(ldap_state, domain);
6173 if (trusted_dn == NULL) {
6176 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6177 filter, attrs, attrsonly, &result);
6179 if (result != NULL) {
6180 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6183 if (rc == LDAP_NO_SUCH_OBJECT) {
6188 if (rc != LDAP_SUCCESS) {
6192 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6194 if (num_result > 1) {
6195 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6196 "%s object for domain '%s'?!\n",
6197 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6201 if (num_result == 0) {
6202 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6203 "%s object for domain %s.\n",
6204 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6207 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6213 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6216 struct dom_sid *sid,
6217 time_t *pass_last_set_time)
6219 struct ldapsam_privates *ldap_state =
6220 (struct ldapsam_privates *)methods->private_data;
6221 LDAPMessage *entry = NULL;
6223 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6225 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6234 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6235 entry, "sambaClearTextPassword", talloc_tos());
6236 if (pwd_str == NULL) {
6239 /* trusteddom_pw routines do not use talloc yet... */
6240 *pwd = SMB_STRDUP(pwd_str);
6246 /* last change time */
6247 if (pass_last_set_time != NULL) {
6249 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6250 entry, "sambaPwdLastSet", talloc_tos());
6251 if (time_str == NULL) {
6254 *pass_last_set_time = (time_t)atol(time_str);
6260 struct dom_sid dom_sid;
6261 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6264 if (sid_str == NULL) {
6267 if (!string_to_sid(&dom_sid, sid_str)) {
6270 sid_copy(sid, &dom_sid);
6276 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6279 const struct dom_sid *sid)
6281 struct ldapsam_privates *ldap_state =
6282 (struct ldapsam_privates *)methods->private_data;
6283 LDAPMessage *entry = NULL;
6284 LDAPMod **mods = NULL;
6285 char *prev_pwd = NULL;
6286 char *trusted_dn = NULL;
6289 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6292 * get the current entry (if there is one) in order to put the
6293 * current password into the previous password attribute
6295 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6300 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6301 LDAP_OBJ_TRUSTDOM_PASSWORD);
6302 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6304 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6305 sid_string_tos(sid));
6306 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6307 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6308 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6309 "sambaClearTextPassword", pwd);
6311 if (entry != NULL) {
6312 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6313 entry, "sambaClearTextPassword", talloc_tos());
6314 if (prev_pwd != NULL) {
6315 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6316 "sambaPreviousClearTextPassword",
6321 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods);
6323 trusted_dn = trusteddom_dn(ldap_state, domain);
6324 if (trusted_dn == NULL) {
6327 if (entry == NULL) {
6328 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6330 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6333 if (rc != LDAP_SUCCESS) {
6334 DEBUG(1, ("error writing trusted domain password!\n"));
6341 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6345 struct ldapsam_privates *ldap_state =
6346 (struct ldapsam_privates *)methods->private_data;
6347 LDAPMessage *entry = NULL;
6348 const char *trusted_dn;
6350 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6354 if (entry == NULL) {
6355 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6360 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6362 if (trusted_dn == NULL) {
6363 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6367 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6368 if (rc != LDAP_SUCCESS) {
6375 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6376 TALLOC_CTX *mem_ctx,
6377 uint32_t *num_domains,
6378 struct trustdom_info ***domains)
6381 struct ldapsam_privates *ldap_state =
6382 (struct ldapsam_privates *)methods->private_data;
6384 int scope = LDAP_SCOPE_SUBTREE;
6385 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6386 int attrsonly = 0; /* 0: return values too */
6387 LDAPMessage *result = NULL;
6388 LDAPMessage *entry = NULL;
6390 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6391 LDAP_OBJ_TRUSTDOM_PASSWORD);
6393 rc = smbldap_search(ldap_state->smbldap_state,
6394 ldap_state->domain_dn,
6401 if (result != NULL) {
6402 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6405 if (rc != LDAP_SUCCESS) {
6406 return NT_STATUS_UNSUCCESSFUL;
6410 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6411 DEBUG(1, ("talloc failed\n"));
6412 return NT_STATUS_NO_MEMORY;
6415 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6417 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6419 char *dom_name, *dom_sid_str;
6420 struct trustdom_info *dom_info;
6422 dom_info = talloc(*domains, struct trustdom_info);
6423 if (dom_info == NULL) {
6424 DEBUG(1, ("talloc failed\n"));
6425 return NT_STATUS_NO_MEMORY;
6428 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6432 if (dom_name == NULL) {
6433 DEBUG(1, ("talloc failed\n"));
6434 return NT_STATUS_NO_MEMORY;
6436 dom_info->name = dom_name;
6438 dom_sid_str = smbldap_talloc_single_attribute(
6439 priv2ld(ldap_state), entry, "sambaSID",
6441 if (dom_sid_str == NULL) {
6442 DEBUG(1, ("talloc failed\n"));
6443 return NT_STATUS_NO_MEMORY;
6445 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6446 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6448 return NT_STATUS_UNSUCCESSFUL;
6451 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6452 domains, num_domains);
6454 if (*domains == NULL) {
6455 DEBUG(1, ("talloc failed\n"));
6456 return NT_STATUS_NO_MEMORY;
6460 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6461 return NT_STATUS_OK;
6465 /**********************************************************************
6467 *********************************************************************/
6469 static void free_private_data(void **vp)
6471 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6473 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6475 if ((*ldap_state)->result != NULL) {
6476 ldap_msgfree((*ldap_state)->result);
6477 (*ldap_state)->result = NULL;
6479 if ((*ldap_state)->domain_dn != NULL) {
6480 SAFE_FREE((*ldap_state)->domain_dn);
6485 /* No need to free any further, as it is talloc()ed */
6488 /*********************************************************************
6489 Intitalise the parts of the pdb_methods structure that are common to
6491 *********************************************************************/
6493 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6496 struct ldapsam_privates *ldap_state;
6497 char *bind_dn = NULL;
6498 char *bind_secret = NULL;
6500 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6504 (*pdb_method)->name = "ldapsam";
6506 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6507 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6508 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6509 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6510 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6511 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6513 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6514 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6515 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6516 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6517 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6518 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6519 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6521 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6522 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6524 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6526 (*pdb_method)->capabilities = ldapsam_capabilities;
6527 (*pdb_method)->new_rid = ldapsam_new_rid;
6529 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6530 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6531 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6532 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6534 /* TODO: Setup private data and free */
6536 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6537 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6538 return NT_STATUS_NO_MEMORY;
6541 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6542 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6543 return NT_STATUS_NO_MEMORY;
6546 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6547 location, false, bind_dn, bind_secret,
6548 &ldap_state->smbldap_state);
6549 memset(bind_secret, '\0', strlen(bind_secret));
6550 SAFE_FREE(bind_secret);
6552 if ( !NT_STATUS_IS_OK(nt_status) ) {
6556 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6557 return NT_STATUS_NO_MEMORY;
6560 (*pdb_method)->private_data = ldap_state;
6562 (*pdb_method)->free_private_data = free_private_data;
6564 return NT_STATUS_OK;
6567 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods *m)
6572 /**********************************************************************
6573 Initialise the normal mode for pdb_ldap
6574 *********************************************************************/
6576 NTSTATUS pdb_ldapsam_init_common(struct pdb_methods **pdb_method,
6577 const char *location)
6580 struct ldapsam_privates *ldap_state = NULL;
6581 uint32_t alg_rid_base;
6582 char *alg_rid_base_string = NULL;
6583 LDAPMessage *result = NULL;
6584 LDAPMessage *entry = NULL;
6585 struct dom_sid ldap_domain_sid;
6586 struct dom_sid secrets_domain_sid;
6587 char *domain_sid_string = NULL;
6589 char *uri = talloc_strdup( NULL, location );
6591 trim_char( uri, '\"', '\"' );
6592 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6596 if (!NT_STATUS_IS_OK(nt_status)) {
6600 (*pdb_method)->name = "ldapsam";
6602 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6603 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6604 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6605 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6606 (*pdb_method)->search_users = ldapsam_search_users;
6607 (*pdb_method)->search_groups = ldapsam_search_groups;
6608 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6609 (*pdb_method)->is_responsible_for_wellknown =
6610 ldapsam_is_responsible_for_wellknown;
6612 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6613 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6614 (*pdb_method)->enum_group_memberships =
6615 ldapsam_enum_group_memberships;
6616 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6617 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6618 (*pdb_method)->id_to_sid = ldapsam_id_to_sid;
6620 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6621 (*pdb_method)->create_user = ldapsam_create_user;
6622 (*pdb_method)->delete_user = ldapsam_delete_user;
6623 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6624 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6625 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6626 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6627 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6631 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6632 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6634 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6636 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6638 ldap_state->domain_name, True);
6640 if ( !NT_STATUS_IS_OK(nt_status) ) {
6641 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6642 "info, nor add one to the domain. "
6643 "We cannot work reliably without it.\n"));
6644 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6647 /* Given that the above might fail, everything below this must be
6650 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
6653 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6655 ldap_msgfree(result);
6656 return NT_STATUS_UNSUCCESSFUL;
6659 dn = smbldap_talloc_dn(talloc_tos(),
6660 smbldap_get_ldap(ldap_state->smbldap_state),
6663 ldap_msgfree(result);
6664 return NT_STATUS_UNSUCCESSFUL;
6667 ldap_state->domain_dn = smb_xstrdup(dn);
6670 domain_sid_string = smbldap_talloc_single_attribute(
6671 smbldap_get_ldap(ldap_state->smbldap_state),
6673 get_userattr_key2string(ldap_state->schema_ver,
6674 LDAP_ATTR_USER_SID),
6677 if (domain_sid_string) {
6679 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6680 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6681 "read as a valid SID\n", domain_sid_string));
6682 ldap_msgfree(result);
6683 TALLOC_FREE(domain_sid_string);
6684 return NT_STATUS_INVALID_PARAMETER;
6686 found_sid = PDB_secrets_fetch_domain_sid(ldap_state->domain_name,
6687 &secrets_domain_sid);
6688 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6689 &ldap_domain_sid)) {
6690 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6691 "%s based on pdb_ldap results %s -> %s\n",
6692 ldap_state->domain_name,
6693 sid_string_dbg(&secrets_domain_sid),
6694 sid_string_dbg(&ldap_domain_sid)));
6696 /* reset secrets.tdb sid */
6697 PDB_secrets_store_domain_sid(ldap_state->domain_name,
6699 DEBUG(1, ("New global sam SID: %s\n",
6700 sid_string_dbg(get_global_sam_sid())));
6702 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6703 TALLOC_FREE(domain_sid_string);
6706 alg_rid_base_string = smbldap_talloc_single_attribute(
6707 smbldap_get_ldap(ldap_state->smbldap_state),
6709 get_attr_key2string( dominfo_attr_list,
6710 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6712 if (alg_rid_base_string) {
6713 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6714 if (alg_rid_base != algorithmic_rid_base()) {
6715 DEBUG(0, ("The value of 'algorithmic RID base' has "
6716 "changed since the LDAP\n"
6717 "database was initialised. Aborting. \n"));
6718 ldap_msgfree(result);
6719 TALLOC_FREE(alg_rid_base_string);
6720 return NT_STATUS_UNSUCCESSFUL;
6722 TALLOC_FREE(alg_rid_base_string);
6724 ldap_msgfree(result);
6726 return NT_STATUS_OK;
6729 NTSTATUS pdb_ldapsam_init(void)
6733 nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION,
6735 pdb_ldapsam_init_common);
6736 if (!NT_STATUS_IS_OK(nt_status)) {
6740 /* Let pdb_nds register backends */
6743 return NT_STATUS_OK;