CVE-2020-25719 kdc: Avoid races and multiple DB lookups in s4u2self check
[Samba.git] / source3 / passdb / pdb_ldap.c
blobe6d8a84c60fa22cf52c92ac06cff839ce6590c62
1 /*
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/>.
26 /* TODO:
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
43 * and/or winbind
46 #include "includes.h"
47 #include "passdb.h"
48 #include "../libcli/auth/libcli_auth.h"
49 #include "secrets.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"
57 #include "lib/util/smb_strtox.h"
58 #include "lib/util/string_wrappers.h"
60 #undef DBGC_CLASS
61 #define DBGC_CLASS DBGC_PASSDB
63 #include <lber.h>
64 #include <ldap.h>
67 #include "smbldap.h"
68 #include "passdb/pdb_ldap.h"
69 #include "passdb/pdb_nds.h"
70 #include "passdb/pdb_ldap_util.h"
71 #include "passdb/pdb_ldap_schema.h"
73 /**********************************************************************
74 Simple helper function to make stuff better readable
75 **********************************************************************/
77 LDAP *priv2ld(struct ldapsam_privates *priv)
79 return smbldap_get_ldap(priv->smbldap_state);
82 /**********************************************************************
83 Get the attribute name given a user schame version.
84 **********************************************************************/
86 static const char* get_userattr_key2string( int schema_ver, int key )
88 switch ( schema_ver ) {
89 case SCHEMAVER_SAMBASAMACCOUNT:
90 return get_attr_key2string( attrib_map_v30, key );
92 default:
93 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
94 break;
96 return NULL;
99 /**********************************************************************
100 Return the list of attribute names given a user schema version.
101 **********************************************************************/
103 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
105 switch ( schema_ver ) {
106 case SCHEMAVER_SAMBASAMACCOUNT:
107 return get_attr_list( mem_ctx, attrib_map_v30 );
108 default:
109 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
110 break;
113 return NULL;
116 /**************************************************************************
117 Return the list of attribute names to delete given a user schema version.
118 **************************************************************************/
120 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
121 int schema_ver )
123 switch ( schema_ver ) {
124 case SCHEMAVER_SAMBASAMACCOUNT:
125 return get_attr_list( mem_ctx,
126 attrib_map_to_delete_v30 );
127 default:
128 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
129 break;
132 return NULL;
136 /*******************************************************************
137 Generate the LDAP search filter for the objectclass based on the
138 version of the schema we are using.
139 ******************************************************************/
141 static const char* get_objclass_filter( int schema_ver )
143 fstring objclass_filter;
144 char *result;
146 switch( schema_ver ) {
147 case SCHEMAVER_SAMBASAMACCOUNT:
148 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
149 break;
150 default:
151 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
152 objclass_filter[0] = '\0';
153 break;
156 result = talloc_strdup(talloc_tos(), objclass_filter);
157 SMB_ASSERT(result != NULL);
158 return result;
161 /*****************************************************************
162 Scan a sequence number off OpenLDAP's syncrepl contextCSN
163 ******************************************************************/
165 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
167 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
168 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
169 LDAPMessage *msg = NULL;
170 LDAPMessage *entry = NULL;
171 TALLOC_CTX *mem_ctx;
172 char **values = NULL;
173 int rc, num_result, num_values, rid;
174 char *suffix = NULL;
175 char *tok;
176 const char *p;
177 const char **attrs;
179 /* Unfortunatly there is no proper way to detect syncrepl-support in
180 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
181 * but do not show up in the root-DSE yet. Neither we can query the
182 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
183 * objectclass. Currently we require lp_ldap_suffix() to show up as
184 * namingContext. - Guenther
187 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
188 return ntstatus;
191 if (!seq_num) {
192 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
193 return ntstatus;
196 if (!smbldap_has_naming_context(
197 smbldap_get_ldap(ldap_state->smbldap_state),
198 lp_ldap_suffix())) {
199 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
200 "as top-level namingContext\n", lp_ldap_suffix()));
201 return ntstatus;
204 mem_ctx = talloc_init("ldapsam_get_seq_num");
206 if (mem_ctx == NULL)
207 return NT_STATUS_NO_MEMORY;
209 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
210 ntstatus = NT_STATUS_NO_MEMORY;
211 goto done;
214 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
215 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
216 if (rid > 0) {
218 /* consumer syncreplCookie: */
219 /* csn=20050126161620Z#0000001#00#00000 */
220 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
221 attrs[1] = NULL;
222 suffix = talloc_asprintf(mem_ctx,
223 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
224 if (!suffix) {
225 ntstatus = NT_STATUS_NO_MEMORY;
226 goto done;
228 } else {
230 /* provider contextCSN */
231 /* 20050126161620Z#000009#00#000000 */
232 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
233 attrs[1] = NULL;
234 suffix = talloc_asprintf(mem_ctx,
235 "cn=ldapsync,%s", lp_ldap_suffix());
237 if (!suffix) {
238 ntstatus = NT_STATUS_NO_MEMORY;
239 goto done;
243 rc = smbldap_search(ldap_state->smbldap_state, suffix,
244 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
246 if (rc != LDAP_SUCCESS) {
247 goto done;
250 num_result = ldap_count_entries(
251 smbldap_get_ldap(ldap_state->smbldap_state), msg);
252 if (num_result != 1) {
253 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
254 goto done;
257 entry = ldap_first_entry(
258 smbldap_get_ldap(ldap_state->smbldap_state), msg);
259 if (entry == NULL) {
260 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
261 goto done;
264 values = ldap_get_values(
265 smbldap_get_ldap(ldap_state->smbldap_state), entry, attrs[0]);
266 if (values == NULL) {
267 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
268 goto done;
271 num_values = ldap_count_values(values);
272 if (num_values == 0) {
273 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
274 goto done;
277 p = values[0];
278 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
279 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
280 goto done;
283 p = tok;
284 if (!strncmp(p, "csn=", strlen("csn=")))
285 p += strlen("csn=");
287 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
289 *seq_num = generalized_to_unix_time(p);
291 /* very basic sanity check */
292 if (*seq_num <= 0) {
293 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
294 (int)*seq_num));
295 goto done;
298 ntstatus = NT_STATUS_OK;
300 done:
301 if (values != NULL)
302 ldap_value_free(values);
303 if (msg != NULL)
304 ldap_msgfree(msg);
305 if (mem_ctx)
306 talloc_destroy(mem_ctx);
308 return ntstatus;
311 /*******************************************************************
312 Run the search by name.
313 ******************************************************************/
315 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
316 const char *user,
317 LDAPMessage ** result,
318 const char **attr)
320 char *filter = NULL;
321 char *escape_user = escape_ldap_string(talloc_tos(), user);
322 int ret = -1;
324 if (!escape_user) {
325 return LDAP_NO_MEMORY;
329 * in the filter expression, replace %u with the real name
330 * so in ldap filter, %u MUST exist :-)
332 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
333 get_objclass_filter(ldap_state->schema_ver));
334 if (!filter) {
335 TALLOC_FREE(escape_user);
336 return LDAP_NO_MEMORY;
339 * have to use this here because $ is filtered out
340 * in string_sub
343 filter = talloc_all_string_sub(talloc_tos(),
344 filter, "%u", escape_user);
345 TALLOC_FREE(escape_user);
346 if (!filter) {
347 return LDAP_NO_MEMORY;
350 ret = smbldap_search_suffix(ldap_state->smbldap_state,
351 filter, attr, result);
352 TALLOC_FREE(filter);
353 return ret;
356 /*******************************************************************
357 Run the search by SID.
358 ******************************************************************/
360 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
361 const struct dom_sid *sid, LDAPMessage ** result,
362 const char **attr)
364 char *filter = NULL;
365 int rc;
366 struct dom_sid_buf sid_string;
368 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
369 get_userattr_key2string(ldap_state->schema_ver,
370 LDAP_ATTR_USER_SID),
371 dom_sid_str_buf(sid, &sid_string),
372 get_objclass_filter(ldap_state->schema_ver));
373 if (!filter) {
374 return LDAP_NO_MEMORY;
377 rc = smbldap_search_suffix(ldap_state->smbldap_state,
378 filter, attr, result);
380 TALLOC_FREE(filter);
381 return rc;
384 /*******************************************************************
385 Delete complete object or objectclass and attrs from
386 object found in search_result depending on lp_ldap_delete_dn
387 ******************************************************************/
389 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
390 TALLOC_CTX *mem_ctx,
391 LDAPMessage *entry,
392 const char *objectclass,
393 const char **attrs)
395 LDAPMod **mods = NULL;
396 char *name;
397 const char *dn;
398 BerElement *ptr = NULL;
400 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
401 if (dn == NULL) {
402 return LDAP_NO_MEMORY;
405 if (lp_ldap_delete_dn()) {
406 return smbldap_delete(priv->smbldap_state, dn);
409 /* Ok, delete only the SAM attributes */
411 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
412 name != NULL;
413 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
414 const char **attrib;
416 /* We are only allowed to delete the attributes that
417 really exist. */
419 for (attrib = attrs; *attrib != NULL; attrib++) {
420 if (strequal(*attrib, name)) {
421 DEBUG(10, ("ldapsam_delete_entry: deleting "
422 "attribute %s\n", name));
423 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
424 NULL);
427 ldap_memfree(name);
430 if (ptr != NULL) {
431 ber_free(ptr, 0);
434 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
435 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
437 return smbldap_modify(priv->smbldap_state, dn, mods);
440 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
442 char *temp;
443 struct tm tm;
445 temp = smbldap_talloc_single_attribute(
446 smbldap_get_ldap(ldap_state->smbldap_state), entry,
447 get_userattr_key2string(ldap_state->schema_ver,
448 LDAP_ATTR_MOD_TIMESTAMP),
449 talloc_tos());
450 if (!temp) {
451 return (time_t) 0;
454 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
455 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
456 (char*)temp));
457 TALLOC_FREE(temp);
458 return (time_t) 0;
460 TALLOC_FREE(temp);
461 tzset();
462 return timegm(&tm);
465 /**********************************************************************
466 Initialize struct samu from an LDAP query.
467 (Based on init_sam_from_buffer in pdb_tdb.c)
468 *********************************************************************/
470 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
471 struct samu * sampass,
472 LDAPMessage * entry)
474 time_t logon_time,
475 logoff_time,
476 kickoff_time,
477 pass_last_set_time,
478 pass_can_change_time,
479 ldap_entry_time,
480 bad_password_time;
481 char *username = NULL,
482 *domain = NULL,
483 *nt_username = NULL,
484 *fullname = NULL,
485 *homedir = NULL,
486 *dir_drive = NULL,
487 *logon_script = NULL,
488 *profile_path = NULL,
489 *acct_desc = NULL,
490 *workstations = NULL,
491 *munged_dial = NULL;
492 uint32_t user_rid;
493 uint8_t smblmpwd[LM_HASH_LEN],
494 smbntpwd[NT_HASH_LEN];
495 bool use_samba_attrs = True;
496 uint16_t logon_divs;
497 uint16_t bad_password_count = 0,
498 logon_count = 0;
499 uint32_t hours_len;
500 uint8_t hours[MAX_HOURS_LEN];
501 char *temp = NULL;
502 struct login_cache cache_entry;
503 uint32_t pwHistLen;
504 bool expand_explicit = lp_passdb_expand_explicit();
505 bool ret = false;
506 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
508 if (!ctx) {
509 return false;
511 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
512 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
513 goto fn_exit;
516 if (priv2ld(ldap_state) == NULL) {
517 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
518 "ldap_struct is NULL!\n"));
519 goto fn_exit;
522 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
523 entry,
524 "uid",
525 ctx))) {
526 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
527 "this user!\n"));
528 goto fn_exit;
531 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
533 nt_username = talloc_strdup(ctx, username);
534 if (!nt_username) {
535 goto fn_exit;
538 domain = talloc_strdup(ctx, ldap_state->domain_name);
539 if (!domain) {
540 goto fn_exit;
543 pdb_set_username(sampass, username, PDB_SET);
545 pdb_set_domain(sampass, domain, PDB_DEFAULT);
546 pdb_set_nt_username(sampass, nt_username, PDB_SET);
548 /* deal with different attributes between the schema first */
550 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
551 if ((temp = smbldap_talloc_single_attribute(
552 smbldap_get_ldap(ldap_state->smbldap_state),
553 entry,
554 get_userattr_key2string(ldap_state->schema_ver,
555 LDAP_ATTR_USER_SID),
556 ctx))!=NULL) {
557 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
559 } else {
560 if ((temp = smbldap_talloc_single_attribute(
561 smbldap_get_ldap(ldap_state->smbldap_state),
562 entry,
563 get_userattr_key2string(ldap_state->schema_ver,
564 LDAP_ATTR_USER_RID),
565 ctx))!=NULL) {
566 user_rid = (uint32_t)atol(temp);
567 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
571 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
572 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
573 get_userattr_key2string(ldap_state->schema_ver,
574 LDAP_ATTR_USER_SID),
575 get_userattr_key2string(ldap_state->schema_ver,
576 LDAP_ATTR_USER_RID),
577 username));
578 return False;
581 temp = smbldap_talloc_single_attribute(
582 smbldap_get_ldap(ldap_state->smbldap_state),
583 entry,
584 get_userattr_key2string(ldap_state->schema_ver,
585 LDAP_ATTR_PWD_LAST_SET),
586 ctx);
587 if (temp) {
588 pass_last_set_time = (time_t) atol(temp);
589 pdb_set_pass_last_set_time(sampass,
590 pass_last_set_time, PDB_SET);
593 temp = smbldap_talloc_single_attribute(
594 smbldap_get_ldap(ldap_state->smbldap_state),
595 entry,
596 get_userattr_key2string(ldap_state->schema_ver,
597 LDAP_ATTR_LOGON_TIME),
598 ctx);
599 if (temp) {
600 logon_time = (time_t) atol(temp);
601 pdb_set_logon_time(sampass, logon_time, PDB_SET);
604 temp = smbldap_talloc_single_attribute(
605 smbldap_get_ldap(ldap_state->smbldap_state),
606 entry,
607 get_userattr_key2string(ldap_state->schema_ver,
608 LDAP_ATTR_LOGOFF_TIME),
609 ctx);
610 if (temp) {
611 logoff_time = (time_t) atol(temp);
612 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
615 temp = smbldap_talloc_single_attribute(
616 smbldap_get_ldap(ldap_state->smbldap_state),
617 entry,
618 get_userattr_key2string(ldap_state->schema_ver,
619 LDAP_ATTR_KICKOFF_TIME),
620 ctx);
621 if (temp) {
622 kickoff_time = (time_t) atol(temp);
623 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
626 temp = smbldap_talloc_single_attribute(
627 smbldap_get_ldap(ldap_state->smbldap_state),
628 entry,
629 get_userattr_key2string(ldap_state->schema_ver,
630 LDAP_ATTR_PWD_CAN_CHANGE),
631 ctx);
632 if (temp) {
633 pass_can_change_time = (time_t) atol(temp);
634 pdb_set_pass_can_change_time(sampass,
635 pass_can_change_time, PDB_SET);
638 /* recommend that 'gecos' and 'displayName' should refer to the same
639 * attribute OID. userFullName depreciated, only used by Samba
640 * primary rules of LDAP: don't make a new attribute when one is already defined
641 * that fits your needs; using cn then displayName rather than 'userFullName'
644 fullname = smbldap_talloc_single_attribute(
645 smbldap_get_ldap(ldap_state->smbldap_state),
646 entry,
647 get_userattr_key2string(ldap_state->schema_ver,
648 LDAP_ATTR_DISPLAY_NAME),
649 ctx);
650 if (fullname) {
651 pdb_set_fullname(sampass, fullname, PDB_SET);
652 } else {
653 fullname = smbldap_talloc_single_attribute(
654 smbldap_get_ldap(ldap_state->smbldap_state),
655 entry,
656 get_userattr_key2string(ldap_state->schema_ver,
657 LDAP_ATTR_CN),
658 ctx);
659 if (fullname) {
660 pdb_set_fullname(sampass, fullname, PDB_SET);
664 dir_drive = smbldap_talloc_single_attribute(
665 smbldap_get_ldap(ldap_state->smbldap_state),
666 entry,
667 get_userattr_key2string(ldap_state->schema_ver,
668 LDAP_ATTR_HOME_DRIVE),
669 ctx);
670 if (dir_drive) {
671 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
672 } else {
673 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
676 homedir = smbldap_talloc_single_attribute(
677 smbldap_get_ldap(ldap_state->smbldap_state),
678 entry,
679 get_userattr_key2string(ldap_state->schema_ver,
680 LDAP_ATTR_HOME_PATH),
681 ctx);
682 if (homedir) {
683 if (expand_explicit) {
684 homedir = talloc_sub_basic(ctx,
685 username,
686 domain,
687 homedir);
688 if (!homedir) {
689 goto fn_exit;
692 pdb_set_homedir(sampass, homedir, PDB_SET);
693 } else {
694 pdb_set_homedir(sampass,
695 talloc_sub_basic(ctx, username, domain,
696 lp_logon_home()),
697 PDB_DEFAULT);
700 logon_script = smbldap_talloc_single_attribute(
701 smbldap_get_ldap(ldap_state->smbldap_state),
702 entry,
703 get_userattr_key2string(ldap_state->schema_ver,
704 LDAP_ATTR_LOGON_SCRIPT),
705 ctx);
706 if (logon_script) {
707 if (expand_explicit) {
708 logon_script = talloc_sub_basic(ctx,
709 username,
710 domain,
711 logon_script);
712 if (!logon_script) {
713 goto fn_exit;
716 pdb_set_logon_script(sampass, logon_script, PDB_SET);
717 } else {
718 pdb_set_logon_script(sampass,
719 talloc_sub_basic(ctx, username, domain,
720 lp_logon_script()),
721 PDB_DEFAULT );
724 profile_path = smbldap_talloc_single_attribute(
725 smbldap_get_ldap(ldap_state->smbldap_state),
726 entry,
727 get_userattr_key2string(ldap_state->schema_ver,
728 LDAP_ATTR_PROFILE_PATH),
729 ctx);
730 if (profile_path) {
731 if (expand_explicit) {
732 profile_path = talloc_sub_basic(ctx,
733 username,
734 domain,
735 profile_path);
736 if (!profile_path) {
737 goto fn_exit;
740 pdb_set_profile_path(sampass, profile_path, PDB_SET);
741 } else {
742 pdb_set_profile_path(sampass,
743 talloc_sub_basic(ctx, username, domain,
744 lp_logon_path()),
745 PDB_DEFAULT );
748 acct_desc = smbldap_talloc_single_attribute(
749 smbldap_get_ldap(ldap_state->smbldap_state),
750 entry,
751 get_userattr_key2string(ldap_state->schema_ver,
752 LDAP_ATTR_DESC),
753 ctx);
754 if (acct_desc) {
755 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
758 workstations = smbldap_talloc_single_attribute(
759 smbldap_get_ldap(ldap_state->smbldap_state),
760 entry,
761 get_userattr_key2string(ldap_state->schema_ver,
762 LDAP_ATTR_USER_WKS),
763 ctx);
764 if (workstations) {
765 pdb_set_workstations(sampass, workstations, PDB_SET);
768 munged_dial = smbldap_talloc_single_attribute(
769 smbldap_get_ldap(ldap_state->smbldap_state),
770 entry,
771 get_userattr_key2string(ldap_state->schema_ver,
772 LDAP_ATTR_MUNGED_DIAL),
773 ctx);
774 if (munged_dial) {
775 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
778 /* FIXME: hours stuff should be cleaner */
780 logon_divs = 168;
781 hours_len = 21;
782 memset(hours, 0xff, hours_len);
784 if (ldap_state->is_nds_ldap) {
785 char *user_dn;
786 size_t pwd_len;
787 char clear_text_pw[512];
789 /* Make call to Novell eDirectory ldap extension to get clear text password.
790 NOTE: This will only work if we have an SSL connection to eDirectory. */
791 user_dn = smbldap_talloc_dn(
792 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
793 entry);
794 if (user_dn != NULL) {
795 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
797 pwd_len = sizeof(clear_text_pw);
798 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
799 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
800 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
801 TALLOC_FREE(user_dn);
802 return False;
804 ZERO_STRUCT(smblmpwd);
805 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
806 TALLOC_FREE(user_dn);
807 return False;
809 ZERO_STRUCT(smbntpwd);
810 use_samba_attrs = False;
813 TALLOC_FREE(user_dn);
815 } else {
816 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
820 if (use_samba_attrs) {
821 temp = smbldap_talloc_single_attribute(
822 smbldap_get_ldap(ldap_state->smbldap_state),
823 entry,
824 get_userattr_key2string(ldap_state->schema_ver,
825 LDAP_ATTR_LMPW),
826 ctx);
827 if (temp) {
828 pdb_gethexpwd(temp, smblmpwd);
829 memset((char *)temp, '\0', strlen(temp)+1);
830 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
831 goto fn_exit;
833 ZERO_STRUCT(smblmpwd);
836 temp = smbldap_talloc_single_attribute(
837 smbldap_get_ldap(ldap_state->smbldap_state),
838 entry,
839 get_userattr_key2string(ldap_state->schema_ver,
840 LDAP_ATTR_NTPW),
841 ctx);
842 if (temp) {
843 pdb_gethexpwd(temp, smbntpwd);
844 memset((char *)temp, '\0', strlen(temp)+1);
845 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
846 goto fn_exit;
848 ZERO_STRUCT(smbntpwd);
852 pwHistLen = 0;
854 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
855 if (pwHistLen > 0){
856 uint8_t *pwhist = NULL;
857 int i;
858 char *history_string = talloc_array(ctx, char,
859 MAX_PW_HISTORY_LEN*64);
861 if (!history_string) {
862 goto fn_exit;
865 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
867 pwhist = talloc_zero_array(ctx, uint8_t,
868 pwHistLen * PW_HISTORY_ENTRY_LEN);
869 if (pwhist == NULL) {
870 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
871 goto fn_exit;
874 if (smbldap_get_single_attribute(
875 smbldap_get_ldap(ldap_state->smbldap_state),
876 entry,
877 get_userattr_key2string(ldap_state->schema_ver,
878 LDAP_ATTR_PWD_HISTORY),
879 history_string,
880 MAX_PW_HISTORY_LEN*64)) {
881 bool hex_failed = false;
882 for (i = 0; i < pwHistLen; i++){
883 /* Get the 16 byte salt. */
884 if (!pdb_gethexpwd(&history_string[i*64],
885 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
886 hex_failed = true;
887 break;
889 /* Get the 16 byte MD5 hash of salt+passwd. */
890 if (!pdb_gethexpwd(&history_string[(i*64)+32],
891 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
892 PW_HISTORY_SALT_LEN])) {
893 hex_failed = True;
894 break;
897 if (hex_failed) {
898 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
899 username));
900 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
903 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
904 goto fn_exit;
908 temp = smbldap_talloc_single_attribute(
909 smbldap_get_ldap(ldap_state->smbldap_state),
910 entry,
911 get_userattr_key2string(ldap_state->schema_ver,
912 LDAP_ATTR_ACB_INFO),
913 ctx);
914 if (temp) {
915 uint32_t acct_ctrl = 0;
916 acct_ctrl = pdb_decode_acct_ctrl(temp);
918 if (acct_ctrl == 0) {
919 acct_ctrl |= ACB_NORMAL;
922 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
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),
930 entry,
931 get_userattr_key2string(ldap_state->schema_ver,
932 LDAP_ATTR_BAD_PASSWORD_COUNT),
933 ctx);
934 if (temp) {
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),
942 entry,
943 get_userattr_key2string(ldap_state->schema_ver,
944 LDAP_ATTR_BAD_PASSWORD_TIME),
945 ctx);
946 if (temp) {
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),
954 entry,
955 get_userattr_key2string(ldap_state->schema_ver,
956 LDAP_ATTR_LOGON_COUNT),
957 ctx);
958 if (temp) {
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),
967 entry,
968 get_userattr_key2string(ldap_state->schema_ver,
969 LDAP_ATTR_LOGON_HOURS),
970 ctx);
971 if (temp) {
972 pdb_gethexhours(temp, hours);
973 memset((char *)temp, '\0', strlen(temp) +1);
974 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
975 ZERO_STRUCT(hours);
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;
984 struct unixid id;
985 int error = 0;
987 ZERO_STRUCT(unix_pw);
989 unix_pw.pw_name = username;
990 unix_pw.pw_passwd = discard_const_p(char, "x");
992 temp = smbldap_talloc_single_attribute(
993 priv2ld(ldap_state),
994 entry,
995 "uidNumber",
996 ctx);
997 if (temp) {
998 /* We've got a uid, feed the cache */
999 unix_pw.pw_uid = smb_strtoul(temp,
1000 NULL,
1002 &error,
1003 SMB_STR_STANDARD);
1004 if (error != 0) {
1005 DBG_ERR("Failed to convert UID\n");
1006 goto fn_exit;
1008 have_uid = true;
1010 temp = smbldap_talloc_single_attribute(
1011 priv2ld(ldap_state),
1012 entry,
1013 "gidNumber",
1014 ctx);
1015 if (temp) {
1016 /* We've got a uid, feed the cache */
1017 unix_pw.pw_gid = smb_strtoul(temp,
1018 NULL,
1020 &error,
1021 SMB_STR_STANDARD);
1022 if (error != 0) {
1023 DBG_ERR("Failed to convert GID\n");
1024 goto fn_exit;
1026 have_gid = true;
1028 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1029 priv2ld(ldap_state),
1030 entry,
1031 "gecos",
1032 ctx);
1033 if (unix_pw.pw_gecos == NULL) {
1034 unix_pw.pw_gecos = fullname;
1036 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state),
1038 entry,
1039 "homeDirectory",
1040 ctx);
1041 if (unix_pw.pw_dir == NULL) {
1042 unix_pw.pw_dir = discard_const_p(char, "");
1044 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1045 priv2ld(ldap_state),
1046 entry,
1047 "loginShell",
1048 ctx);
1049 if (unix_pw.pw_shell == NULL) {
1050 unix_pw.pw_shell = discard_const_p(char, "");
1053 if (have_uid && have_gid) {
1054 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1055 } else {
1056 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1059 if (sampass->unix_pw == NULL) {
1060 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1061 pdb_get_username(sampass)));
1062 goto fn_exit;
1065 id.id = sampass->unix_pw->pw_uid;
1066 id.type = ID_TYPE_UID;
1068 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass), &id);
1070 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1071 primary_gsid = pdb_get_group_sid(sampass);
1072 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1073 id.id = sampass->unix_pw->pw_gid;
1074 id.type = ID_TYPE_GID;
1076 idmap_cache_set_sid2unixid(primary_gsid, &id);
1080 /* check the timestamp of the cache vs ldap entry */
1081 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1082 entry))) {
1083 ret = true;
1084 goto fn_exit;
1087 /* see if we have newer updates */
1088 if (!login_cache_read(sampass, &cache_entry)) {
1089 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1090 (unsigned int)pdb_get_bad_password_count(sampass),
1091 (unsigned int)pdb_get_bad_password_time(sampass)));
1092 ret = true;
1093 goto fn_exit;
1096 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1097 (unsigned int)ldap_entry_time,
1098 (unsigned int)cache_entry.entry_timestamp,
1099 (unsigned int)cache_entry.bad_password_time));
1101 if (ldap_entry_time > cache_entry.entry_timestamp) {
1102 /* cache is older than directory , so
1103 we need to delete the entry but allow the
1104 fields to be written out */
1105 login_cache_delentry(sampass);
1106 } else {
1107 /* read cache in */
1108 pdb_set_acct_ctrl(sampass,
1109 pdb_get_acct_ctrl(sampass) |
1110 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1111 PDB_SET);
1112 pdb_set_bad_password_count(sampass,
1113 cache_entry.bad_password_count,
1114 PDB_SET);
1115 pdb_set_bad_password_time(sampass,
1116 cache_entry.bad_password_time,
1117 PDB_SET);
1120 ret = true;
1122 fn_exit:
1124 TALLOC_FREE(ctx);
1125 return ret;
1128 /**********************************************************************
1129 Initialize the ldap db from a struct samu. Called on update.
1130 (Based on init_buffer_from_sam in pdb_tdb.c)
1131 *********************************************************************/
1133 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1134 LDAPMessage *existing,
1135 LDAPMod *** mods, struct samu * sampass,
1136 bool (*need_update)(const struct samu *,
1137 enum pdb_elements))
1139 char *temp = NULL;
1141 if (mods == NULL || sampass == NULL) {
1142 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1143 return False;
1146 *mods = NULL;
1149 * took out adding "objectclass: sambaAccount"
1150 * do this on a per-mod basis
1152 if (need_update(sampass, PDB_USERNAME)) {
1153 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1154 existing, mods,
1155 "uid", pdb_get_username(sampass));
1156 if (ldap_state->is_nds_ldap) {
1157 smbldap_make_mod(
1158 smbldap_get_ldap(ldap_state->smbldap_state),
1159 existing, mods,
1160 "cn", pdb_get_username(sampass));
1161 smbldap_make_mod(
1162 smbldap_get_ldap(ldap_state->smbldap_state),
1163 existing, mods,
1164 "sn", pdb_get_username(sampass));
1168 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1170 /* only update the RID if we actually need to */
1171 if (need_update(sampass, PDB_USERSID)) {
1172 struct dom_sid_buf sid_str;
1173 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1175 switch ( ldap_state->schema_ver ) {
1176 case SCHEMAVER_SAMBASAMACCOUNT:
1177 smbldap_make_mod(
1178 smbldap_get_ldap(
1179 ldap_state->smbldap_state),
1180 existing, mods,
1181 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1182 dom_sid_str_buf(user_sid, &sid_str));
1183 break;
1185 default:
1186 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1187 break;
1191 /* we don't need to store the primary group RID - so leaving it
1192 'free' to hang off the unix primary group makes life easier */
1194 if (need_update(sampass, PDB_GROUPSID)) {
1195 struct dom_sid_buf sid_str;
1196 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1198 switch ( ldap_state->schema_ver ) {
1199 case SCHEMAVER_SAMBASAMACCOUNT:
1200 smbldap_make_mod(
1201 smbldap_get_ldap(
1202 ldap_state->smbldap_state),
1203 existing, mods,
1204 get_userattr_key2string(ldap_state->schema_ver,
1205 LDAP_ATTR_PRIMARY_GROUP_SID),
1206 dom_sid_str_buf(group_sid, &sid_str));
1207 break;
1209 default:
1210 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1211 break;
1216 /* displayName, cn, and gecos should all be the same
1217 * most easily accomplished by giving them the same OID
1218 * gecos isn't set here b/c it should be handled by the
1219 * add-user script
1220 * We change displayName only and fall back to cn if
1221 * it does not exist.
1224 if (need_update(sampass, PDB_FULLNAME))
1225 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1226 existing, mods,
1227 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1228 pdb_get_fullname(sampass));
1230 if (need_update(sampass, PDB_ACCTDESC))
1231 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1232 existing, mods,
1233 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1234 pdb_get_acct_desc(sampass));
1236 if (need_update(sampass, PDB_WORKSTATIONS))
1237 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1238 existing, mods,
1239 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1240 pdb_get_workstations(sampass));
1242 if (need_update(sampass, PDB_MUNGEDDIAL))
1243 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1244 existing, mods,
1245 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1246 pdb_get_munged_dial(sampass));
1248 if (need_update(sampass, PDB_SMBHOME))
1249 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1250 existing, mods,
1251 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1252 pdb_get_homedir(sampass));
1254 if (need_update(sampass, PDB_DRIVE))
1255 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1256 existing, mods,
1257 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1258 pdb_get_dir_drive(sampass));
1260 if (need_update(sampass, PDB_LOGONSCRIPT))
1261 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1262 existing, mods,
1263 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1264 pdb_get_logon_script(sampass));
1266 if (need_update(sampass, PDB_PROFILE))
1267 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1268 existing, mods,
1269 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1270 pdb_get_profile_path(sampass));
1272 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1273 return false;
1275 if (need_update(sampass, PDB_LOGONTIME))
1276 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1277 existing, mods,
1278 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1279 SAFE_FREE(temp);
1281 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1282 return false;
1284 if (need_update(sampass, PDB_LOGOFFTIME))
1285 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1286 existing, mods,
1287 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1288 SAFE_FREE(temp);
1290 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1291 return false;
1293 if (need_update(sampass, PDB_KICKOFFTIME))
1294 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1295 existing, mods,
1296 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1297 SAFE_FREE(temp);
1299 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1300 return false;
1302 if (need_update(sampass, PDB_CANCHANGETIME))
1303 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1304 existing, mods,
1305 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1306 SAFE_FREE(temp);
1308 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1309 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1311 if (need_update(sampass, PDB_LMPASSWD)) {
1312 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1313 if (lm_pw) {
1314 char pwstr[34];
1315 pdb_sethexpwd(pwstr, lm_pw,
1316 pdb_get_acct_ctrl(sampass));
1317 smbldap_make_mod(
1318 smbldap_get_ldap(
1319 ldap_state->smbldap_state),
1320 existing, mods,
1321 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1322 pwstr);
1323 } else {
1324 smbldap_make_mod(
1325 smbldap_get_ldap(
1326 ldap_state->smbldap_state),
1327 existing, mods,
1328 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1329 NULL);
1332 if (need_update(sampass, PDB_NTPASSWD)) {
1333 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1334 if (nt_pw) {
1335 char pwstr[34];
1336 pdb_sethexpwd(pwstr, nt_pw,
1337 pdb_get_acct_ctrl(sampass));
1338 smbldap_make_mod(
1339 smbldap_get_ldap(
1340 ldap_state->smbldap_state),
1341 existing, mods,
1342 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1343 pwstr);
1344 } else {
1345 smbldap_make_mod(
1346 smbldap_get_ldap(
1347 ldap_state->smbldap_state),
1348 existing, mods,
1349 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1350 NULL);
1354 if (need_update(sampass, PDB_PWHISTORY)) {
1355 char *pwstr = NULL;
1356 uint32_t pwHistLen = 0;
1357 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1359 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1360 if (!pwstr) {
1361 return false;
1363 if (pwHistLen == 0) {
1364 /* Remove any password history from the LDAP store. */
1365 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1366 pwstr[64] = '\0';
1367 } else {
1368 int i;
1369 uint32_t currHistLen = 0;
1370 const uint8_t *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1371 if (pwhist != NULL) {
1372 /* We can only store (1024-1/64 password history entries. */
1373 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1374 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1375 /* Store the salt. */
1376 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1377 /* Followed by the md5 hash of salt + md4 hash */
1378 pdb_sethexpwd(&pwstr[(i*64)+32],
1379 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1380 DEBUG(100, ("pwstr=%s\n", pwstr));
1384 smbldap_make_mod(
1385 smbldap_get_ldap(ldap_state->smbldap_state),
1386 existing, mods,
1387 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1388 pwstr);
1389 SAFE_FREE(pwstr);
1392 if (need_update(sampass, PDB_PASSLASTSET)) {
1393 if (asprintf(&temp, "%li",
1394 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1395 return false;
1397 smbldap_make_mod(
1398 smbldap_get_ldap(ldap_state->smbldap_state),
1399 existing, mods,
1400 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1401 temp);
1402 SAFE_FREE(temp);
1406 if (need_update(sampass, PDB_HOURS)) {
1407 const uint8_t *hours = pdb_get_hours(sampass);
1408 if (hours) {
1409 char hourstr[44];
1410 pdb_sethexhours(hourstr, hours);
1411 smbldap_make_mod(
1412 smbldap_get_ldap(ldap_state->smbldap_state),
1413 existing,
1414 mods,
1415 get_userattr_key2string(ldap_state->schema_ver,
1416 LDAP_ATTR_LOGON_HOURS),
1417 hourstr);
1421 if (need_update(sampass, PDB_ACCTCTRL))
1422 smbldap_make_mod(
1423 smbldap_get_ldap(ldap_state->smbldap_state),
1424 existing, mods,
1425 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1426 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1428 /* password lockout cache:
1429 - If we are now autolocking or clearing, we write to ldap
1430 - If we are clearing, we delete the cache entry
1431 - If the count is > 0, we update the cache
1433 This even means when autolocking, we cache, just in case the
1434 update doesn't work, and we have to cache the autolock flag */
1436 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1437 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1438 uint16_t badcount = pdb_get_bad_password_count(sampass);
1439 time_t badtime = pdb_get_bad_password_time(sampass);
1440 uint32_t pol;
1441 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1443 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1444 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1446 if ((badcount >= pol) || (badcount == 0)) {
1447 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1448 (unsigned int)badcount, (unsigned int)badtime));
1449 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1450 return false;
1452 smbldap_make_mod(
1453 smbldap_get_ldap(ldap_state->smbldap_state),
1454 existing, mods,
1455 get_userattr_key2string(
1456 ldap_state->schema_ver,
1457 LDAP_ATTR_BAD_PASSWORD_COUNT),
1458 temp);
1459 SAFE_FREE(temp);
1461 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1462 return false;
1464 smbldap_make_mod(
1465 smbldap_get_ldap(ldap_state->smbldap_state),
1466 existing, mods,
1467 get_userattr_key2string(
1468 ldap_state->schema_ver,
1469 LDAP_ATTR_BAD_PASSWORD_TIME),
1470 temp);
1471 SAFE_FREE(temp);
1473 if (badcount == 0) {
1474 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1475 login_cache_delentry(sampass);
1476 } else {
1477 struct login_cache cache_entry;
1479 cache_entry.entry_timestamp = time(NULL);
1480 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1481 cache_entry.bad_password_count = badcount;
1482 cache_entry.bad_password_time = badtime;
1484 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1485 login_cache_write(sampass, &cache_entry);
1489 return True;
1492 /**********************************************************************
1493 End enumeration of the LDAP password list.
1494 *********************************************************************/
1496 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1498 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1499 if (ldap_state->result) {
1500 ldap_msgfree(ldap_state->result);
1501 ldap_state->result = NULL;
1505 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1506 const char *new_attr)
1508 int i;
1510 if (new_attr == NULL) {
1511 return;
1514 for (i=0; (*attr_list)[i] != NULL; i++) {
1518 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1519 const char *, i+2);
1520 SMB_ASSERT((*attr_list) != NULL);
1521 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1522 (*attr_list)[i+1] = NULL;
1525 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1526 const char ***attr_list)
1528 append_attr(mem_ctx, attr_list, "uidNumber");
1529 append_attr(mem_ctx, attr_list, "gidNumber");
1530 append_attr(mem_ctx, attr_list, "homeDirectory");
1531 append_attr(mem_ctx, attr_list, "loginShell");
1532 append_attr(mem_ctx, attr_list, "gecos");
1535 /**********************************************************************
1536 Get struct samu entry from LDAP by username.
1537 *********************************************************************/
1539 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1541 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1542 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1543 LDAPMessage *result = NULL;
1544 LDAPMessage *entry = NULL;
1545 int count;
1546 const char ** attr_list;
1547 int rc;
1549 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1550 append_attr(user, &attr_list,
1551 get_userattr_key2string(ldap_state->schema_ver,
1552 LDAP_ATTR_MOD_TIMESTAMP));
1553 ldapsam_add_unix_attributes(user, &attr_list);
1554 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1555 attr_list);
1556 TALLOC_FREE( attr_list );
1558 if ( rc != LDAP_SUCCESS )
1559 return NT_STATUS_NO_SUCH_USER;
1561 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1562 result);
1564 if (count < 1) {
1565 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1566 ldap_msgfree(result);
1567 return NT_STATUS_NO_SUCH_USER;
1568 } else if (count > 1) {
1569 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1570 ldap_msgfree(result);
1571 return NT_STATUS_NO_SUCH_USER;
1574 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1575 result);
1576 if (entry) {
1577 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1578 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1579 ldap_msgfree(result);
1580 return NT_STATUS_NO_SUCH_USER;
1582 pdb_set_backend_private_data(user, result, NULL,
1583 my_methods, PDB_CHANGED);
1584 smbldap_talloc_autofree_ldapmsg(user, result);
1585 ret = NT_STATUS_OK;
1586 } else {
1587 ldap_msgfree(result);
1589 return ret;
1592 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1593 const struct dom_sid *sid, LDAPMessage **result)
1595 int rc = -1;
1596 const char ** attr_list;
1598 switch ( ldap_state->schema_ver ) {
1599 case SCHEMAVER_SAMBASAMACCOUNT: {
1600 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1601 if (tmp_ctx == NULL) {
1602 return LDAP_NO_MEMORY;
1605 attr_list = get_userattr_list(tmp_ctx,
1606 ldap_state->schema_ver);
1607 append_attr(tmp_ctx, &attr_list,
1608 get_userattr_key2string(
1609 ldap_state->schema_ver,
1610 LDAP_ATTR_MOD_TIMESTAMP));
1611 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1612 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1613 result, attr_list);
1614 TALLOC_FREE(tmp_ctx);
1616 if ( rc != LDAP_SUCCESS )
1617 return rc;
1618 break;
1621 default:
1622 DEBUG(0,("Invalid schema version specified\n"));
1623 break;
1625 return rc;
1628 /**********************************************************************
1629 Get struct samu entry from LDAP by SID.
1630 *********************************************************************/
1632 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1634 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1635 LDAPMessage *result = NULL;
1636 LDAPMessage *entry = NULL;
1637 int count;
1638 int rc;
1640 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1641 sid, &result);
1642 if (rc != LDAP_SUCCESS)
1643 return NT_STATUS_NO_SUCH_USER;
1645 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1646 result);
1648 if (count < 1) {
1649 struct dom_sid_buf buf;
1650 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1651 "count=%d\n",
1652 dom_sid_str_buf(sid, &buf),
1653 count));
1654 ldap_msgfree(result);
1655 return NT_STATUS_NO_SUCH_USER;
1656 } else if (count > 1) {
1657 struct dom_sid_buf buf;
1658 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1659 "[%s]. Failing. count=%d\n",
1660 dom_sid_str_buf(sid, &buf),
1661 count));
1662 ldap_msgfree(result);
1663 return NT_STATUS_NO_SUCH_USER;
1666 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1667 result);
1668 if (!entry) {
1669 ldap_msgfree(result);
1670 return NT_STATUS_NO_SUCH_USER;
1673 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1674 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1675 ldap_msgfree(result);
1676 return NT_STATUS_NO_SUCH_USER;
1679 pdb_set_backend_private_data(user, result, NULL,
1680 my_methods, PDB_CHANGED);
1681 smbldap_talloc_autofree_ldapmsg(user, result);
1682 return NT_STATUS_OK;
1685 /********************************************************************
1686 Do the actual modification - also change a plaintext passord if
1687 it it set.
1688 **********************************************************************/
1690 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1691 struct samu *newpwd, char *dn,
1692 LDAPMod **mods, int ldap_op,
1693 bool (*need_update)(const struct samu *, enum pdb_elements))
1695 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1696 int rc;
1698 if (!newpwd || !dn) {
1699 return NT_STATUS_INVALID_PARAMETER;
1702 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1703 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1704 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1705 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1706 BerElement *ber;
1707 struct berval *bv;
1708 char *retoid = NULL;
1709 struct berval *retdata = NULL;
1710 char *utf8_password;
1711 char *utf8_dn;
1712 size_t converted_size;
1713 int ret;
1715 if (!ldap_state->is_nds_ldap) {
1717 if (!smbldap_has_extension(
1718 smbldap_get_ldap(
1719 ldap_state->smbldap_state),
1720 LDAP_EXOP_MODIFY_PASSWD)) {
1721 DEBUG(2, ("ldap password change requested, but LDAP "
1722 "server does not support it -- ignoring\n"));
1723 return NT_STATUS_OK;
1727 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1728 pdb_get_plaintext_passwd(newpwd),
1729 &converted_size))
1731 return NT_STATUS_NO_MEMORY;
1734 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1735 TALLOC_FREE(utf8_password);
1736 return NT_STATUS_NO_MEMORY;
1739 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1740 DEBUG(0,("ber_alloc_t returns NULL\n"));
1741 TALLOC_FREE(utf8_password);
1742 TALLOC_FREE(utf8_dn);
1743 return NT_STATUS_UNSUCCESSFUL;
1746 if ((ber_printf (ber, "{") < 0) ||
1747 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1748 utf8_dn) < 0)) {
1749 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1750 "value <0\n"));
1751 ber_free(ber,1);
1752 TALLOC_FREE(utf8_dn);
1753 TALLOC_FREE(utf8_password);
1754 return NT_STATUS_UNSUCCESSFUL;
1757 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1758 ret = ber_printf(ber, "ts}",
1759 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1760 utf8_password);
1761 } else {
1762 ret = ber_printf(ber, "}");
1765 if (ret < 0) {
1766 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1767 "value <0\n"));
1768 ber_free(ber,1);
1769 TALLOC_FREE(utf8_dn);
1770 TALLOC_FREE(utf8_password);
1771 return NT_STATUS_UNSUCCESSFUL;
1774 if ((rc = ber_flatten (ber, &bv))<0) {
1775 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1776 ber_free(ber,1);
1777 TALLOC_FREE(utf8_dn);
1778 TALLOC_FREE(utf8_password);
1779 return NT_STATUS_UNSUCCESSFUL;
1782 TALLOC_FREE(utf8_dn);
1783 TALLOC_FREE(utf8_password);
1784 ber_free(ber, 1);
1786 if (!ldap_state->is_nds_ldap) {
1787 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1788 LDAP_EXOP_MODIFY_PASSWD,
1789 bv, NULL, NULL, &retoid,
1790 &retdata);
1791 } else {
1792 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1793 pdb_get_plaintext_passwd(newpwd));
1795 if (rc != LDAP_SUCCESS) {
1796 char *ld_error = NULL;
1798 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1799 DEBUG(3, ("Could not set userPassword "
1800 "attribute due to an objectClass "
1801 "violation -- ignoring\n"));
1802 ber_bvfree(bv);
1803 return NT_STATUS_OK;
1806 ldap_get_option(
1807 smbldap_get_ldap(ldap_state->smbldap_state),
1808 LDAP_OPT_ERROR_STRING,
1809 &ld_error);
1810 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1811 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1812 SAFE_FREE(ld_error);
1813 ber_bvfree(bv);
1814 #if defined(LDAP_CONSTRAINT_VIOLATION)
1815 if (rc == LDAP_CONSTRAINT_VIOLATION)
1816 return NT_STATUS_PASSWORD_RESTRICTION;
1817 #endif
1818 return NT_STATUS_UNSUCCESSFUL;
1819 } else {
1820 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1821 #ifdef DEBUG_PASSWORD
1822 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1823 #endif
1824 if (retdata)
1825 ber_bvfree(retdata);
1826 if (retoid)
1827 ldap_memfree(retoid);
1829 ber_bvfree(bv);
1832 if (!mods) {
1833 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1834 /* may be password change below however */
1835 } else {
1836 switch(ldap_op) {
1837 case LDAP_MOD_ADD:
1838 if (ldap_state->is_nds_ldap) {
1839 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1840 "objectclass",
1841 "inetOrgPerson");
1842 } else {
1843 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1844 "objectclass",
1845 LDAP_OBJ_ACCOUNT);
1847 rc = smbldap_add(ldap_state->smbldap_state,
1848 dn, mods);
1849 break;
1850 case LDAP_MOD_REPLACE:
1851 rc = smbldap_modify(ldap_state->smbldap_state,
1852 dn ,mods);
1853 break;
1854 default:
1855 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1856 ldap_op));
1857 return NT_STATUS_INVALID_PARAMETER;
1860 if (rc!=LDAP_SUCCESS) {
1861 return NT_STATUS_UNSUCCESSFUL;
1865 return NT_STATUS_OK;
1868 /**********************************************************************
1869 Delete entry from LDAP for username.
1870 *********************************************************************/
1872 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1873 struct samu * sam_acct)
1875 struct ldapsam_privates *priv =
1876 (struct ldapsam_privates *)my_methods->private_data;
1877 const char *sname;
1878 int rc;
1879 LDAPMessage *msg, *entry;
1880 NTSTATUS result = NT_STATUS_NO_MEMORY;
1881 const char **attr_list;
1882 TALLOC_CTX *mem_ctx;
1884 if (!sam_acct) {
1885 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1886 return NT_STATUS_INVALID_PARAMETER;
1889 sname = pdb_get_username(sam_acct);
1891 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1892 "LDAP.\n", sname));
1894 mem_ctx = talloc_new(NULL);
1895 if (mem_ctx == NULL) {
1896 DEBUG(0, ("talloc_new failed\n"));
1897 goto done;
1900 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1901 if (attr_list == NULL) {
1902 goto done;
1905 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1907 if ((rc != LDAP_SUCCESS) ||
1908 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1909 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1910 DEBUG(5, ("Could not find user %s\n", sname));
1911 result = NT_STATUS_NO_SUCH_USER;
1912 goto done;
1915 rc = ldapsam_delete_entry(
1916 priv, mem_ctx, entry,
1917 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1918 LDAP_OBJ_SAMBASAMACCOUNT : 0,
1919 attr_list);
1921 result = (rc == LDAP_SUCCESS) ?
1922 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1924 done:
1925 TALLOC_FREE(mem_ctx);
1926 return result;
1929 /**********************************************************************
1930 Update struct samu.
1931 *********************************************************************/
1933 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1935 NTSTATUS ret;
1936 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1937 int rc = 0;
1938 char *dn;
1939 LDAPMessage *result = NULL;
1940 LDAPMessage *entry = NULL;
1941 LDAPMod **mods = NULL;
1942 const char **attr_list;
1944 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1945 if (!result) {
1946 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1947 if (pdb_get_username(newpwd) == NULL) {
1948 return NT_STATUS_INVALID_PARAMETER;
1950 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1951 TALLOC_FREE( attr_list );
1952 if (rc != LDAP_SUCCESS) {
1953 return NT_STATUS_UNSUCCESSFUL;
1955 pdb_set_backend_private_data(newpwd, result, NULL,
1956 my_methods, PDB_CHANGED);
1957 smbldap_talloc_autofree_ldapmsg(newpwd, result);
1960 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1961 result) == 0) {
1962 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1963 return NT_STATUS_UNSUCCESSFUL;
1966 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1967 result);
1968 dn = smbldap_talloc_dn(talloc_tos(),
1969 smbldap_get_ldap(ldap_state->smbldap_state),
1970 entry);
1971 if (!dn) {
1972 return NT_STATUS_UNSUCCESSFUL;
1975 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1977 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1978 pdb_element_is_changed)) {
1979 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1980 TALLOC_FREE(dn);
1981 if (mods != NULL)
1982 ldap_mods_free(mods,True);
1983 return NT_STATUS_UNSUCCESSFUL;
1986 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1987 && (mods == NULL)) {
1988 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1989 pdb_get_username(newpwd)));
1990 TALLOC_FREE(dn);
1991 return NT_STATUS_OK;
1994 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1996 if (mods != NULL) {
1997 ldap_mods_free(mods,True);
2000 TALLOC_FREE(dn);
2003 * We need to set the backend private data to NULL here. For example
2004 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2005 * same one, and with the explicit delete / add logic for attribute
2006 * values the second time we would use the wrong "old" value which
2007 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2008 * the update.
2009 * The existing LDAPMessage is still being auto-freed by the
2010 * destructor.
2012 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2013 PDB_CHANGED);
2015 if (!NT_STATUS_IS_OK(ret)) {
2016 return ret;
2019 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2020 pdb_get_username(newpwd)));
2021 return NT_STATUS_OK;
2024 /***************************************************************************
2025 Renames a struct samu
2026 - The "rename user script" has full responsibility for changing everything
2027 ***************************************************************************/
2029 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2030 TALLOC_CTX *tmp_ctx,
2031 uint32_t group_rid,
2032 uint32_t member_rid);
2034 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2035 TALLOC_CTX *mem_ctx,
2036 struct samu *user,
2037 struct dom_sid **pp_sids,
2038 gid_t **pp_gids,
2039 uint32_t *p_num_groups);
2041 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2042 struct samu *old_acct,
2043 const char *newname)
2045 const struct loadparm_substitution *lp_sub =
2046 loadparm_s3_global_substitution();
2047 const char *oldname;
2048 int rc;
2049 char *rename_script = NULL;
2050 fstring oldname_lower, newname_lower;
2052 if (!old_acct) {
2053 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2054 return NT_STATUS_INVALID_PARAMETER;
2056 if (!newname) {
2057 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2058 return NT_STATUS_INVALID_PARAMETER;
2061 oldname = pdb_get_username(old_acct);
2063 /* rename the posix user */
2064 rename_script = lp_rename_user_script(talloc_tos(), lp_sub);
2065 if (rename_script == NULL) {
2066 return NT_STATUS_NO_MEMORY;
2069 if (!(*rename_script)) {
2070 TALLOC_FREE(rename_script);
2071 return NT_STATUS_ACCESS_DENIED;
2074 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2075 oldname, newname));
2077 /* We have to allow the account name to end with a '$'.
2078 Also, follow the semantics in _samr_create_user() and lower case the
2079 posix name but preserve the case in passdb */
2081 fstrcpy( oldname_lower, oldname );
2082 if (!strlower_m( oldname_lower )) {
2083 return NT_STATUS_INVALID_PARAMETER;
2085 fstrcpy( newname_lower, newname );
2086 if (!strlower_m( newname_lower )) {
2087 return NT_STATUS_INVALID_PARAMETER;
2090 rename_script = realloc_string_sub2(rename_script,
2091 "%unew",
2092 newname_lower,
2093 true,
2094 true);
2095 if (!rename_script) {
2096 return NT_STATUS_NO_MEMORY;
2098 rename_script = realloc_string_sub2(rename_script,
2099 "%uold",
2100 oldname_lower,
2101 true,
2102 true);
2103 rc = smbrun(rename_script, NULL, NULL);
2105 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2106 rename_script, rc));
2108 TALLOC_FREE(rename_script);
2110 if (rc == 0) {
2111 smb_nscd_flush_user_cache();
2114 if (rc)
2115 return NT_STATUS_UNSUCCESSFUL;
2117 return NT_STATUS_OK;
2120 /**********************************************************************
2121 Add struct samu to LDAP.
2122 *********************************************************************/
2124 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2126 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2127 int rc;
2128 LDAPMessage *result = NULL;
2129 LDAPMessage *entry = NULL;
2130 LDAPMod **mods = NULL;
2131 int ldap_op = LDAP_MOD_REPLACE;
2132 uint32_t num_result;
2133 const char **attr_list;
2134 char *escape_user = NULL;
2135 const char *username = pdb_get_username(newpwd);
2136 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2137 char *filter = NULL;
2138 char *dn = NULL;
2139 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2140 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2142 if (!ctx) {
2143 return NT_STATUS_NO_MEMORY;
2146 if (!username || !*username) {
2147 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2148 status = NT_STATUS_INVALID_PARAMETER;
2149 goto fn_exit;
2152 /* free this list after the second search or in case we exit on failure */
2153 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2155 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2157 if (rc != LDAP_SUCCESS) {
2158 goto fn_exit;
2161 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
2162 result) != 0) {
2163 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2164 username));
2165 goto fn_exit;
2167 ldap_msgfree(result);
2168 result = NULL;
2170 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2171 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2172 sid, &result);
2173 if (rc == LDAP_SUCCESS) {
2174 if (ldap_count_entries(
2175 smbldap_get_ldap(
2176 ldap_state->smbldap_state),
2177 result) != 0) {
2178 struct dom_sid_buf buf;
2179 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2180 "already in the base, with samba "
2181 "attributes\n",
2182 dom_sid_str_buf(sid, &buf)));
2183 goto fn_exit;
2185 ldap_msgfree(result);
2186 result = NULL;
2190 /* does the entry already exist but without a samba attributes?
2191 we need to return the samba attributes here */
2193 escape_user = escape_ldap_string(talloc_tos(), username);
2194 filter = talloc_strdup(attr_list, "(uid=%u)");
2195 if (!filter) {
2196 status = NT_STATUS_NO_MEMORY;
2197 goto fn_exit;
2199 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2200 TALLOC_FREE(escape_user);
2201 if (!filter) {
2202 status = NT_STATUS_NO_MEMORY;
2203 goto fn_exit;
2206 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2207 filter, attr_list, &result);
2208 if ( rc != LDAP_SUCCESS ) {
2209 goto fn_exit;
2212 num_result = ldap_count_entries(
2213 smbldap_get_ldap(ldap_state->smbldap_state), result);
2215 if (num_result > 1) {
2216 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2217 goto fn_exit;
2220 /* Check if we need to update an existing entry */
2221 if (num_result == 1) {
2222 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2223 ldap_op = LDAP_MOD_REPLACE;
2224 entry = ldap_first_entry(
2225 smbldap_get_ldap(ldap_state->smbldap_state), result);
2226 dn = smbldap_talloc_dn(
2227 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
2228 entry);
2229 if (!dn) {
2230 status = NT_STATUS_NO_MEMORY;
2231 goto fn_exit;
2234 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2236 struct dom_sid_buf buf;
2238 /* There might be a SID for this account already - say an idmap entry */
2240 filter = talloc_asprintf(ctx,
2241 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2242 get_userattr_key2string(ldap_state->schema_ver,
2243 LDAP_ATTR_USER_SID),
2244 dom_sid_str_buf(sid, &buf),
2245 LDAP_OBJ_IDMAP_ENTRY,
2246 LDAP_OBJ_SID_ENTRY);
2247 if (!filter) {
2248 status = NT_STATUS_NO_MEMORY;
2249 goto fn_exit;
2252 /* free old result before doing a new search */
2253 if (result != NULL) {
2254 ldap_msgfree(result);
2255 result = NULL;
2257 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2258 filter, attr_list, &result);
2260 if ( rc != LDAP_SUCCESS ) {
2261 goto fn_exit;
2264 num_result = ldap_count_entries(
2265 smbldap_get_ldap(ldap_state->smbldap_state), result);
2267 if (num_result > 1) {
2268 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2269 goto fn_exit;
2272 /* Check if we need to update an existing entry */
2273 if (num_result == 1) {
2275 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2276 ldap_op = LDAP_MOD_REPLACE;
2277 entry = ldap_first_entry (
2278 smbldap_get_ldap(ldap_state->smbldap_state),
2279 result);
2280 dn = smbldap_talloc_dn (
2281 ctx,
2282 smbldap_get_ldap(ldap_state->smbldap_state),
2283 entry);
2284 if (!dn) {
2285 status = NT_STATUS_NO_MEMORY;
2286 goto fn_exit;
2291 if (num_result == 0) {
2292 char *escape_username;
2293 /* Check if we need to add an entry */
2294 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2295 ldap_op = LDAP_MOD_ADD;
2297 escape_username = escape_rdn_val_string_alloc(username);
2298 if (!escape_username) {
2299 status = NT_STATUS_NO_MEMORY;
2300 goto fn_exit;
2303 if (username[strlen(username)-1] == '$') {
2304 dn = talloc_asprintf(ctx,
2305 "uid=%s,%s",
2306 escape_username,
2307 lp_ldap_machine_suffix(talloc_tos()));
2308 } else {
2309 dn = talloc_asprintf(ctx,
2310 "uid=%s,%s",
2311 escape_username,
2312 lp_ldap_user_suffix(talloc_tos()));
2315 SAFE_FREE(escape_username);
2316 if (!dn) {
2317 status = NT_STATUS_NO_MEMORY;
2318 goto fn_exit;
2322 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2323 pdb_element_is_set_or_changed)) {
2324 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2325 if (mods != NULL) {
2326 ldap_mods_free(mods, true);
2328 goto fn_exit;
2331 if (mods == NULL) {
2332 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2333 goto fn_exit;
2335 switch ( ldap_state->schema_ver ) {
2336 case SCHEMAVER_SAMBASAMACCOUNT:
2337 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2338 break;
2339 default:
2340 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2341 break;
2344 status = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2345 if (!NT_STATUS_IS_OK(status)) {
2346 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2347 pdb_get_username(newpwd),dn));
2348 ldap_mods_free(mods, true);
2349 goto fn_exit;
2352 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2353 ldap_mods_free(mods, true);
2355 status = NT_STATUS_OK;
2357 fn_exit:
2359 TALLOC_FREE(ctx);
2360 if (result) {
2361 ldap_msgfree(result);
2363 return status;
2366 /**********************************************************************
2367 *********************************************************************/
2369 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2370 const char *filter,
2371 LDAPMessage ** result)
2373 int scope = LDAP_SCOPE_SUBTREE;
2374 int rc;
2375 const char **attr_list;
2377 attr_list = get_attr_list(NULL, groupmap_attr_list);
2378 rc = smbldap_search(ldap_state->smbldap_state,
2379 lp_ldap_suffix(), scope,
2380 filter, attr_list, 0, result);
2381 TALLOC_FREE(attr_list);
2383 return rc;
2386 /**********************************************************************
2387 *********************************************************************/
2389 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2390 GROUP_MAP *map, LDAPMessage *entry)
2392 char *temp = NULL;
2393 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2395 if (ldap_state == NULL || map == NULL || entry == NULL ||
2396 smbldap_get_ldap(ldap_state->smbldap_state) == NULL) {
2397 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2398 TALLOC_FREE(ctx);
2399 return false;
2402 temp = smbldap_talloc_single_attribute(
2403 smbldap_get_ldap(ldap_state->smbldap_state),
2404 entry,
2405 get_attr_key2string(groupmap_attr_list,
2406 LDAP_ATTR_GIDNUMBER),
2407 ctx);
2408 if (!temp) {
2409 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2410 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2411 TALLOC_FREE(ctx);
2412 return false;
2414 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2416 map->gid = (gid_t)atol(temp);
2418 TALLOC_FREE(temp);
2419 temp = smbldap_talloc_single_attribute(
2420 smbldap_get_ldap(ldap_state->smbldap_state),
2421 entry,
2422 get_attr_key2string(groupmap_attr_list,
2423 LDAP_ATTR_GROUP_SID),
2424 ctx);
2425 if (!temp) {
2426 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2427 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2428 TALLOC_FREE(ctx);
2429 return false;
2432 if (!string_to_sid(&map->sid, temp)) {
2433 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2434 TALLOC_FREE(ctx);
2435 return false;
2438 TALLOC_FREE(temp);
2439 temp = smbldap_talloc_single_attribute(
2440 smbldap_get_ldap(ldap_state->smbldap_state),
2441 entry,
2442 get_attr_key2string(groupmap_attr_list,
2443 LDAP_ATTR_GROUP_TYPE),
2444 ctx);
2445 if (!temp) {
2446 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2447 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2448 TALLOC_FREE(ctx);
2449 return false;
2451 map->sid_name_use = (enum lsa_SidType)atol(temp);
2453 if ((map->sid_name_use < SID_NAME_USER) ||
2454 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2455 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2456 TALLOC_FREE(ctx);
2457 return false;
2460 TALLOC_FREE(temp);
2461 temp = smbldap_talloc_single_attribute(
2462 smbldap_get_ldap(ldap_state->smbldap_state),
2463 entry,
2464 get_attr_key2string(groupmap_attr_list,
2465 LDAP_ATTR_DISPLAY_NAME),
2466 ctx);
2467 if (!temp) {
2468 temp = smbldap_talloc_single_attribute(
2469 smbldap_get_ldap(ldap_state->smbldap_state),
2470 entry,
2471 get_attr_key2string(groupmap_attr_list,
2472 LDAP_ATTR_CN),
2473 ctx);
2474 if (!temp) {
2475 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2476 for gidNumber(%lu)\n",(unsigned long)map->gid));
2477 TALLOC_FREE(ctx);
2478 return false;
2481 map->nt_name = talloc_strdup(map, temp);
2482 if (!map->nt_name) {
2483 TALLOC_FREE(ctx);
2484 return false;
2487 TALLOC_FREE(temp);
2488 temp = smbldap_talloc_single_attribute(
2489 smbldap_get_ldap(ldap_state->smbldap_state),
2490 entry,
2491 get_attr_key2string(groupmap_attr_list,
2492 LDAP_ATTR_DESC),
2493 ctx);
2494 if (!temp) {
2495 temp = talloc_strdup(ctx, "");
2496 if (!temp) {
2497 TALLOC_FREE(ctx);
2498 return false;
2501 map->comment = talloc_strdup(map, temp);
2502 if (!map->comment) {
2503 TALLOC_FREE(ctx);
2504 return false;
2507 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2508 struct unixid id;
2509 id.id = map->gid;
2510 id.type = ID_TYPE_GID;
2512 idmap_cache_set_sid2unixid(&map->sid, &id);
2515 TALLOC_FREE(ctx);
2516 return true;
2519 /**********************************************************************
2520 *********************************************************************/
2522 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2523 const char *filter,
2524 GROUP_MAP *map)
2526 struct ldapsam_privates *ldap_state =
2527 (struct ldapsam_privates *)methods->private_data;
2528 LDAPMessage *result = NULL;
2529 LDAPMessage *entry = NULL;
2530 int count;
2532 if (ldapsam_search_one_group(ldap_state, filter, &result)
2533 != LDAP_SUCCESS) {
2534 return NT_STATUS_NO_SUCH_GROUP;
2537 count = ldap_count_entries(priv2ld(ldap_state), result);
2539 if (count < 1) {
2540 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2541 "%s\n", filter));
2542 ldap_msgfree(result);
2543 return NT_STATUS_NO_SUCH_GROUP;
2546 if (count > 1) {
2547 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2548 "count=%d\n", filter, count));
2549 ldap_msgfree(result);
2550 return NT_STATUS_NO_SUCH_GROUP;
2553 entry = ldap_first_entry(priv2ld(ldap_state), result);
2555 if (!entry) {
2556 ldap_msgfree(result);
2557 return NT_STATUS_UNSUCCESSFUL;
2560 if (!init_group_from_ldap(ldap_state, map, entry)) {
2561 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2562 "group filter %s\n", filter));
2563 ldap_msgfree(result);
2564 return NT_STATUS_NO_SUCH_GROUP;
2567 ldap_msgfree(result);
2568 return NT_STATUS_OK;
2571 /**********************************************************************
2572 *********************************************************************/
2574 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2575 struct dom_sid sid)
2577 char *filter = NULL;
2578 NTSTATUS status;
2579 struct dom_sid_buf tmp;
2581 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2582 LDAP_OBJ_GROUPMAP,
2583 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2584 dom_sid_str_buf(&sid, &tmp)) < 0) {
2585 return NT_STATUS_NO_MEMORY;
2588 status = ldapsam_getgroup(methods, filter, map);
2589 SAFE_FREE(filter);
2590 return status;
2593 /**********************************************************************
2594 *********************************************************************/
2596 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2597 gid_t gid)
2599 char *filter = NULL;
2600 NTSTATUS status;
2602 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2603 LDAP_OBJ_GROUPMAP,
2604 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2605 (unsigned long)gid) < 0) {
2606 return NT_STATUS_NO_MEMORY;
2609 status = ldapsam_getgroup(methods, filter, map);
2610 SAFE_FREE(filter);
2611 return status;
2614 /**********************************************************************
2615 *********************************************************************/
2617 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2618 const char *name)
2620 char *filter = NULL;
2621 char *escape_name = escape_ldap_string(talloc_tos(), name);
2622 NTSTATUS status;
2624 if (!escape_name) {
2625 return NT_STATUS_NO_MEMORY;
2628 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2629 LDAP_OBJ_GROUPMAP,
2630 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2631 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2632 escape_name) < 0) {
2633 TALLOC_FREE(escape_name);
2634 return NT_STATUS_NO_MEMORY;
2637 TALLOC_FREE(escape_name);
2638 status = ldapsam_getgroup(methods, filter, map);
2639 SAFE_FREE(filter);
2640 return status;
2643 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2644 LDAPMessage *entry,
2645 const struct dom_sid *domain_sid,
2646 uint32_t *rid)
2648 fstring str;
2649 struct dom_sid sid;
2651 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2652 str, sizeof(str)-1)) {
2653 DEBUG(10, ("Could not find sambaSID attribute\n"));
2654 return False;
2657 if (!string_to_sid(&sid, str)) {
2658 DEBUG(10, ("Could not convert string %s to sid\n", str));
2659 return False;
2662 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2663 struct dom_sid_buf buf;
2664 DEBUG(10, ("SID %s is not in expected domain %s\n",
2665 str,
2666 dom_sid_str_buf(domain_sid, &buf)));
2667 return False;
2670 if (!sid_peek_rid(&sid, rid)) {
2671 DEBUG(10, ("Could not peek into RID\n"));
2672 return False;
2675 return True;
2678 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2679 TALLOC_CTX *mem_ctx,
2680 const struct dom_sid *group,
2681 uint32_t **pp_member_rids,
2682 size_t *p_num_members)
2684 struct ldapsam_privates *ldap_state =
2685 (struct ldapsam_privates *)methods->private_data;
2686 struct smbldap_state *conn = ldap_state->smbldap_state;
2687 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2688 const char *sid_attrs[] = { "sambaSID", NULL };
2689 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2690 LDAPMessage *result = NULL;
2691 LDAPMessage *entry;
2692 char *filter;
2693 char **values = NULL;
2694 char **memberuid;
2695 char *gidstr;
2696 int rc, count;
2697 struct dom_sid_buf buf;
2699 *pp_member_rids = NULL;
2700 *p_num_members = 0;
2702 filter = talloc_asprintf(mem_ctx,
2703 "(&(objectClass=%s)"
2704 "(objectClass=%s)"
2705 "(sambaSID=%s))",
2706 LDAP_OBJ_POSIXGROUP,
2707 LDAP_OBJ_GROUPMAP,
2708 dom_sid_str_buf(group, &buf));
2709 if (filter == NULL) {
2710 ret = NT_STATUS_NO_MEMORY;
2711 goto done;
2714 rc = smbldap_search(conn, lp_ldap_suffix(),
2715 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2716 &result);
2718 if (rc != LDAP_SUCCESS)
2719 goto done;
2721 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2723 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2725 if (count > 1) {
2726 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2727 dom_sid_str_buf(group, &buf)));
2728 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2729 goto done;
2732 if (count == 0) {
2733 ret = NT_STATUS_NO_SUCH_GROUP;
2734 goto done;
2737 entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2738 if (entry == NULL)
2739 goto done;
2741 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2742 if (!gidstr) {
2743 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2744 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2745 goto done;
2748 values = ldap_get_values(smbldap_get_ldap(conn), entry, "memberUid");
2750 if ((values != NULL) && (values[0] != NULL)) {
2752 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2753 if (filter == NULL) {
2754 ret = NT_STATUS_NO_MEMORY;
2755 goto done;
2758 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2759 char *escape_memberuid;
2761 escape_memberuid = escape_ldap_string(talloc_tos(),
2762 *memberuid);
2763 if (escape_memberuid == NULL) {
2764 ret = NT_STATUS_NO_MEMORY;
2765 goto done;
2768 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2769 TALLOC_FREE(escape_memberuid);
2770 if (filter == NULL) {
2771 ret = NT_STATUS_NO_MEMORY;
2772 goto done;
2776 filter = talloc_asprintf_append_buffer(filter, "))");
2777 if (filter == NULL) {
2778 ret = NT_STATUS_NO_MEMORY;
2779 goto done;
2782 rc = smbldap_search(conn, lp_ldap_suffix(),
2783 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2784 &result);
2786 if (rc != LDAP_SUCCESS)
2787 goto done;
2789 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2790 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2792 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2794 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2795 entry != NULL;
2796 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2798 char *sidstr;
2799 struct dom_sid sid;
2800 uint32_t rid;
2802 sidstr = smbldap_talloc_single_attribute(
2803 smbldap_get_ldap(conn), entry, "sambaSID",
2804 mem_ctx);
2805 if (!sidstr) {
2806 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2807 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2808 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2809 goto done;
2812 if (!string_to_sid(&sid, sidstr))
2813 goto done;
2815 if (!sid_check_is_in_our_sam(&sid)) {
2816 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2817 "in our domain\n"));
2818 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2819 goto done;
2822 sid_peek_rid(&sid, &rid);
2824 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2825 p_num_members)) {
2826 ret = NT_STATUS_NO_MEMORY;
2827 goto done;
2832 filter = talloc_asprintf(mem_ctx,
2833 "(&(objectClass=%s)"
2834 "(gidNumber=%s))",
2835 LDAP_OBJ_SAMBASAMACCOUNT,
2836 gidstr);
2838 rc = smbldap_search(conn, lp_ldap_suffix(),
2839 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2840 &result);
2842 if (rc != LDAP_SUCCESS)
2843 goto done;
2845 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2847 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2848 entry != NULL;
2849 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2851 uint32_t rid;
2853 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn),
2854 entry,
2855 get_global_sam_sid(),
2856 &rid)) {
2857 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2858 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2859 goto done;
2862 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2863 p_num_members)) {
2864 ret = NT_STATUS_NO_MEMORY;
2865 goto done;
2869 ret = NT_STATUS_OK;
2871 done:
2873 if (values)
2874 ldap_value_free(values);
2876 return ret;
2879 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2880 TALLOC_CTX *mem_ctx,
2881 struct samu *user,
2882 struct dom_sid **pp_sids,
2883 gid_t **pp_gids,
2884 uint32_t *p_num_groups)
2886 struct ldapsam_privates *ldap_state =
2887 (struct ldapsam_privates *)methods->private_data;
2888 struct smbldap_state *conn = ldap_state->smbldap_state;
2889 char *filter;
2890 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2891 char *escape_name;
2892 int rc, count;
2893 LDAPMessage *result = NULL;
2894 LDAPMessage *entry;
2895 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2896 uint32_t num_sids;
2897 uint32_t num_gids;
2898 char *gidstr;
2899 gid_t primary_gid = -1;
2900 int error = 0;
2902 *pp_sids = NULL;
2903 num_sids = 0;
2905 if (pdb_get_username(user) == NULL) {
2906 return NT_STATUS_INVALID_PARAMETER;
2909 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2910 if (escape_name == NULL)
2911 return NT_STATUS_NO_MEMORY;
2913 if (user->unix_pw) {
2914 primary_gid = user->unix_pw->pw_gid;
2915 } else {
2916 /* retrieve the users primary gid */
2917 filter = talloc_asprintf(mem_ctx,
2918 "(&(objectClass=%s)(uid=%s))",
2919 LDAP_OBJ_SAMBASAMACCOUNT,
2920 escape_name);
2921 if (filter == NULL) {
2922 ret = NT_STATUS_NO_MEMORY;
2923 goto done;
2926 rc = smbldap_search(conn, lp_ldap_suffix(),
2927 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2929 if (rc != LDAP_SUCCESS)
2930 goto done;
2932 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2934 count = ldap_count_entries(priv2ld(ldap_state), result);
2936 switch (count) {
2937 case 0:
2938 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2939 ret = NT_STATUS_NO_SUCH_USER;
2940 goto done;
2941 case 1:
2942 entry = ldap_first_entry(priv2ld(ldap_state), result);
2944 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2945 if (!gidstr) {
2946 DEBUG (1, ("Unable to find the member's gid!\n"));
2947 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2948 goto done;
2950 primary_gid = smb_strtoul(gidstr,
2951 NULL,
2953 &error,
2954 SMB_STR_STANDARD);
2955 if (error != 0) {
2956 DBG_ERR("Failed to convert GID\n");
2957 goto done;
2959 break;
2960 default:
2961 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2962 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2963 goto done;
2967 filter = talloc_asprintf(mem_ctx,
2968 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2969 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2970 if (filter == NULL) {
2971 ret = NT_STATUS_NO_MEMORY;
2972 goto done;
2975 rc = smbldap_search(conn, lp_ldap_suffix(),
2976 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2978 if (rc != LDAP_SUCCESS)
2979 goto done;
2981 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2983 num_gids = 0;
2984 *pp_gids = NULL;
2986 num_sids = 0;
2987 *pp_sids = NULL;
2989 /* We need to add the primary group as the first gid/sid */
2991 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2992 ret = NT_STATUS_NO_MEMORY;
2993 goto done;
2996 /* This sid will be replaced later */
2998 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2999 &num_sids);
3000 if (!NT_STATUS_IS_OK(ret)) {
3001 goto done;
3004 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
3005 entry != NULL;
3006 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
3008 fstring str;
3009 struct dom_sid sid;
3010 gid_t gid;
3012 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3013 entry, "sambaSID",
3014 str, sizeof(str)-1))
3015 continue;
3017 if (!string_to_sid(&sid, str))
3018 goto done;
3020 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3021 entry, "gidNumber",
3022 str, sizeof(str)-1))
3023 continue;
3025 gid = smb_strtoul(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
3027 if (error != 0) {
3028 goto done;
3031 if (gid == primary_gid) {
3032 sid_copy(&(*pp_sids)[0], &sid);
3033 } else {
3034 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3035 &num_gids)) {
3036 ret = NT_STATUS_NO_MEMORY;
3037 goto done;
3039 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3040 &num_sids);
3041 if (!NT_STATUS_IS_OK(ret)) {
3042 goto done;
3047 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3048 DEBUG(3, ("primary group of [%s] not found\n",
3049 pdb_get_username(user)));
3050 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3051 goto done;
3054 *p_num_groups = num_sids;
3056 ret = NT_STATUS_OK;
3058 done:
3060 TALLOC_FREE(escape_name);
3061 return ret;
3064 /**********************************************************************
3065 * Augment a posixGroup object with a sambaGroupMapping domgroup
3066 *********************************************************************/
3068 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3069 struct ldapsam_privates *ldap_state,
3070 GROUP_MAP *map)
3072 const char *filter, *dn;
3073 LDAPMessage *msg, *entry;
3074 LDAPMod **mods;
3075 struct dom_sid_buf buf;
3076 int rc;
3078 filter = talloc_asprintf(mem_ctx,
3079 "(&(objectClass=%s)(gidNumber=%u))",
3080 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3081 if (filter == NULL) {
3082 return NT_STATUS_NO_MEMORY;
3085 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3086 get_attr_list(mem_ctx, groupmap_attr_list),
3087 &msg);
3088 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3090 if ((rc != LDAP_SUCCESS) ||
3091 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3092 msg) != 1) ||
3093 ((entry = ldap_first_entry(
3094 smbldap_get_ldap(ldap_state->smbldap_state),
3095 msg)) == NULL)) {
3096 return NT_STATUS_NO_SUCH_GROUP;
3099 dn = smbldap_talloc_dn(mem_ctx,
3100 smbldap_get_ldap(ldap_state->smbldap_state),
3101 entry);
3102 if (dn == NULL) {
3103 return NT_STATUS_NO_MEMORY;
3106 mods = NULL;
3107 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3108 LDAP_OBJ_GROUPMAP);
3109 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3110 &mods, "sambaSid",
3111 dom_sid_str_buf(&map->sid, &buf));
3112 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3113 &mods, "sambaGroupType",
3114 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3115 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3116 &mods, "displayName",
3117 map->nt_name);
3118 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3119 &mods, "description",
3120 map->comment);
3121 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3123 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3124 if (rc != LDAP_SUCCESS) {
3125 return NT_STATUS_ACCESS_DENIED;
3128 return NT_STATUS_OK;
3131 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3132 GROUP_MAP *map)
3134 struct ldapsam_privates *ldap_state =
3135 (struct ldapsam_privates *)methods->private_data;
3136 LDAPMessage *msg = NULL;
3137 LDAPMod **mods = NULL;
3138 const char *attrs[] = { NULL };
3139 char *filter;
3141 char *dn;
3142 TALLOC_CTX *mem_ctx;
3143 NTSTATUS result;
3145 struct dom_sid sid;
3146 struct dom_sid_buf buf;
3147 struct unixid id;
3149 int rc;
3151 mem_ctx = talloc_new(NULL);
3152 if (mem_ctx == NULL) {
3153 DEBUG(0, ("talloc_new failed\n"));
3154 return NT_STATUS_NO_MEMORY;
3157 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3158 dom_sid_str_buf(&map->sid, &buf));
3159 if (filter == NULL) {
3160 result = NT_STATUS_NO_MEMORY;
3161 goto done;
3164 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3165 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3166 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3168 if ((rc == LDAP_SUCCESS) &&
3169 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3170 msg) > 0)) {
3172 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3173 "group mapping entry\n",
3174 dom_sid_str_buf(&map->sid, &buf)));
3175 result = NT_STATUS_GROUP_EXISTS;
3176 goto done;
3179 switch (map->sid_name_use) {
3181 case SID_NAME_DOM_GRP:
3182 /* To map a domain group we need to have a posix group
3183 to attach to. */
3184 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3185 goto done;
3186 break;
3188 case SID_NAME_ALIAS:
3189 if (!sid_check_is_in_our_sam(&map->sid)
3190 && !sid_check_is_in_builtin(&map->sid) )
3192 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3193 dom_sid_str_buf(&map->sid, &buf)));
3194 result = NT_STATUS_INVALID_PARAMETER;
3195 goto done;
3197 break;
3199 default:
3200 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3201 sid_type_lookup(map->sid_name_use)));
3202 result = NT_STATUS_INVALID_PARAMETER;
3203 goto done;
3206 /* Domain groups have been mapped in a separate routine, we have to
3207 * create an alias now */
3209 if (map->gid == -1) {
3210 DEBUG(10, ("Refusing to map gid==-1\n"));
3211 result = NT_STATUS_INVALID_PARAMETER;
3212 goto done;
3215 id.id = map->gid;
3216 id.type = ID_TYPE_GID;
3218 if (pdb_id_to_sid(&id, &sid)) {
3219 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3220 "add\n",
3221 (unsigned int)map->gid,
3222 dom_sid_str_buf(&sid, &buf)));
3223 result = NT_STATUS_GROUP_EXISTS;
3224 goto done;
3227 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3228 * the best we can get out of LDAP. */
3230 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3231 dom_sid_str_buf(&map->sid, &buf),
3232 lp_ldap_group_suffix(talloc_tos()));
3233 if (dn == NULL) {
3234 result = NT_STATUS_NO_MEMORY;
3235 goto done;
3238 mods = NULL;
3240 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3241 &mods, "objectClass", LDAP_OBJ_SID_ENTRY);
3242 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3243 &mods, "objectClass", LDAP_OBJ_GROUPMAP);
3244 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3245 &mods, "sambaSid",
3246 dom_sid_str_buf(&map->sid, &buf));
3247 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3248 &mods, "sambaGroupType",
3249 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3250 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3251 &mods, "displayName",
3252 map->nt_name);
3253 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3254 &mods, "description",
3255 map->comment);
3256 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3257 &mods, "gidNumber",
3258 talloc_asprintf(mem_ctx, "%u",
3259 (unsigned int)map->gid));
3260 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3262 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3264 result = (rc == LDAP_SUCCESS) ?
3265 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3267 done:
3268 TALLOC_FREE(mem_ctx);
3269 return result;
3272 /**********************************************************************
3273 * Update a group mapping entry. We're quite strict about what can be changed:
3274 * Only the description and displayname may be changed. It simply does not
3275 * make any sense to change the SID, gid or the type in a mapping.
3276 *********************************************************************/
3278 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3279 GROUP_MAP *map)
3281 struct ldapsam_privates *ldap_state =
3282 (struct ldapsam_privates *)methods->private_data;
3283 int rc;
3284 const char *filter, *dn;
3285 LDAPMessage *msg = NULL;
3286 LDAPMessage *entry = NULL;
3287 LDAPMod **mods = NULL;
3288 TALLOC_CTX *mem_ctx;
3289 NTSTATUS result;
3290 struct dom_sid_buf buf;
3292 mem_ctx = talloc_new(NULL);
3293 if (mem_ctx == NULL) {
3294 DEBUG(0, ("talloc_new failed\n"));
3295 return NT_STATUS_NO_MEMORY;
3298 /* Make 100% sure that sid, gid and type are not changed by looking up
3299 * exactly the values we're given in LDAP. */
3301 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3302 "(sambaSid=%s)(gidNumber=%u)"
3303 "(sambaGroupType=%d))",
3304 LDAP_OBJ_GROUPMAP,
3305 dom_sid_str_buf(&map->sid, &buf),
3306 (unsigned int)map->gid, map->sid_name_use);
3307 if (filter == NULL) {
3308 result = NT_STATUS_NO_MEMORY;
3309 goto done;
3312 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3313 get_attr_list(mem_ctx, groupmap_attr_list),
3314 &msg);
3315 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3317 if ((rc != LDAP_SUCCESS) ||
3318 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3319 msg) != 1) ||
3320 ((entry = ldap_first_entry(
3321 smbldap_get_ldap(ldap_state->smbldap_state),
3322 msg)) == NULL)) {
3323 result = NT_STATUS_NO_SUCH_GROUP;
3324 goto done;
3327 dn = smbldap_talloc_dn(
3328 mem_ctx, smbldap_get_ldap(ldap_state->smbldap_state), entry);
3330 if (dn == NULL) {
3331 result = NT_STATUS_NO_MEMORY;
3332 goto done;
3335 mods = NULL;
3336 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3337 &mods, "displayName", map->nt_name);
3338 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3339 &mods, "description", map->comment);
3340 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3342 if (mods == NULL) {
3343 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3344 "nothing to do\n"));
3345 result = NT_STATUS_OK;
3346 goto done;
3349 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3351 if (rc != LDAP_SUCCESS) {
3352 result = NT_STATUS_ACCESS_DENIED;
3353 goto done;
3356 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3357 "group %lu in LDAP\n", (unsigned long)map->gid));
3359 result = NT_STATUS_OK;
3361 done:
3362 TALLOC_FREE(mem_ctx);
3363 return result;
3366 /**********************************************************************
3367 *********************************************************************/
3369 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3370 struct dom_sid sid)
3372 struct ldapsam_privates *priv =
3373 (struct ldapsam_privates *)methods->private_data;
3374 LDAPMessage *msg, *entry;
3375 int rc;
3376 NTSTATUS result;
3377 TALLOC_CTX *mem_ctx;
3378 char *filter;
3379 struct dom_sid_buf buf;
3381 mem_ctx = talloc_new(NULL);
3382 if (mem_ctx == NULL) {
3383 DEBUG(0, ("talloc_new failed\n"));
3384 return NT_STATUS_NO_MEMORY;
3387 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3388 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3389 dom_sid_str_buf(&sid, &buf));
3390 if (filter == NULL) {
3391 result = NT_STATUS_NO_MEMORY;
3392 goto done;
3394 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3395 get_attr_list(mem_ctx, groupmap_attr_list),
3396 &msg);
3397 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3399 if ((rc != LDAP_SUCCESS) ||
3400 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3401 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3402 result = NT_STATUS_NO_SUCH_GROUP;
3403 goto done;
3406 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3407 get_attr_list(mem_ctx,
3408 groupmap_attr_list_to_delete));
3410 if ((rc == LDAP_NAMING_VIOLATION) ||
3411 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3412 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3413 const char *attrs[] = { "sambaGroupType", "description",
3414 "displayName", "sambaSIDList",
3415 NULL };
3417 /* Second try. Don't delete the sambaSID attribute, this is
3418 for "old" entries that are tacked on a winbind
3419 sambaIdmapEntry. */
3421 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3422 LDAP_OBJ_GROUPMAP, attrs);
3425 if ((rc == LDAP_NAMING_VIOLATION) ||
3426 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3427 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3428 const char *attrs[] = { "sambaGroupType", "description",
3429 "displayName", "sambaSIDList",
3430 "gidNumber", NULL };
3432 /* Third try. This is a post-3.0.21 alias (containing only
3433 * sambaSidEntry and sambaGroupMapping classes), we also have
3434 * to delete the gidNumber attribute, only the sambaSidEntry
3435 * remains */
3437 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3438 LDAP_OBJ_GROUPMAP, attrs);
3441 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3443 done:
3444 TALLOC_FREE(mem_ctx);
3445 return result;
3448 /**********************************************************************
3449 *********************************************************************/
3451 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3452 bool update)
3454 struct ldapsam_privates *ldap_state =
3455 (struct ldapsam_privates *)my_methods->private_data;
3456 char *filter = NULL;
3457 int rc;
3458 const char **attr_list;
3460 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3461 if (!filter) {
3462 return NT_STATUS_NO_MEMORY;
3464 attr_list = get_attr_list( NULL, groupmap_attr_list );
3465 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3466 LDAP_SCOPE_SUBTREE, filter,
3467 attr_list, 0, &ldap_state->result);
3468 TALLOC_FREE(attr_list);
3470 if (rc != LDAP_SUCCESS) {
3471 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3472 ldap_err2string(rc)));
3473 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3474 lp_ldap_suffix(), filter));
3475 ldap_msgfree(ldap_state->result);
3476 ldap_state->result = NULL;
3477 TALLOC_FREE(filter);
3478 return NT_STATUS_UNSUCCESSFUL;
3481 TALLOC_FREE(filter);
3483 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3484 ldap_count_entries(
3485 smbldap_get_ldap(ldap_state->smbldap_state),
3486 ldap_state->result)));
3488 ldap_state->entry =
3489 ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3490 ldap_state->result);
3491 ldap_state->index = 0;
3493 return NT_STATUS_OK;
3496 /**********************************************************************
3497 *********************************************************************/
3499 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3501 ldapsam_endsampwent(my_methods);
3504 /**********************************************************************
3505 *********************************************************************/
3507 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3508 GROUP_MAP *map)
3510 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3511 struct ldapsam_privates *ldap_state =
3512 (struct ldapsam_privates *)my_methods->private_data;
3513 bool bret = False;
3515 while (!bret) {
3516 if (!ldap_state->entry)
3517 return ret;
3519 ldap_state->index++;
3520 bret = init_group_from_ldap(ldap_state, map,
3521 ldap_state->entry);
3523 ldap_state->entry = ldap_next_entry(
3524 smbldap_get_ldap(ldap_state->smbldap_state),
3525 ldap_state->entry);
3528 return NT_STATUS_OK;
3531 /**********************************************************************
3532 *********************************************************************/
3534 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3535 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3536 GROUP_MAP ***pp_rmap,
3537 size_t *p_num_entries,
3538 bool unix_only)
3540 GROUP_MAP *map = NULL;
3541 size_t entries = 0;
3543 *p_num_entries = 0;
3544 *pp_rmap = NULL;
3546 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3547 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3548 "passdb\n"));
3549 return NT_STATUS_ACCESS_DENIED;
3552 while (true) {
3554 map = talloc_zero(NULL, GROUP_MAP);
3555 if (!map) {
3556 return NT_STATUS_NO_MEMORY;
3559 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3560 TALLOC_FREE(map);
3561 break;
3564 if (sid_name_use != SID_NAME_UNKNOWN &&
3565 sid_name_use != map->sid_name_use) {
3566 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3567 "not of the requested type\n",
3568 map->nt_name));
3569 continue;
3571 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3572 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3573 "non mapped\n", map->nt_name));
3574 continue;
3577 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3578 GROUP_MAP *, entries + 1);
3579 if (!(*pp_rmap)) {
3580 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3581 "enlarge group map!\n"));
3582 return NT_STATUS_UNSUCCESSFUL;
3585 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3587 entries += 1;
3590 ldapsam_endsamgrent(methods);
3592 *p_num_entries = entries;
3594 return NT_STATUS_OK;
3597 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3598 const struct dom_sid *alias,
3599 const struct dom_sid *member,
3600 int modop)
3602 struct ldapsam_privates *ldap_state =
3603 (struct ldapsam_privates *)methods->private_data;
3604 char *dn = NULL;
3605 LDAPMessage *result = NULL;
3606 LDAPMessage *entry = NULL;
3607 int count;
3608 LDAPMod **mods = NULL;
3609 int rc;
3610 enum lsa_SidType type = SID_NAME_USE_NONE;
3611 struct dom_sid_buf tmp;
3613 char *filter = NULL;
3615 if (sid_check_is_in_builtin(alias)) {
3616 type = SID_NAME_ALIAS;
3619 if (sid_check_is_in_our_sam(alias)) {
3620 type = SID_NAME_ALIAS;
3623 if (type == SID_NAME_USE_NONE) {
3624 struct dom_sid_buf buf;
3625 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3626 dom_sid_str_buf(alias, &buf)));
3627 return NT_STATUS_NO_SUCH_ALIAS;
3630 if (asprintf(&filter,
3631 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3632 LDAP_OBJ_GROUPMAP,
3633 dom_sid_str_buf(alias, &tmp),
3634 type) < 0) {
3635 return NT_STATUS_NO_MEMORY;
3638 if (ldapsam_search_one_group(ldap_state, filter,
3639 &result) != LDAP_SUCCESS) {
3640 SAFE_FREE(filter);
3641 return NT_STATUS_NO_SUCH_ALIAS;
3644 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3645 result);
3647 if (count < 1) {
3648 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3649 ldap_msgfree(result);
3650 SAFE_FREE(filter);
3651 return NT_STATUS_NO_SUCH_ALIAS;
3654 if (count > 1) {
3655 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3656 "filter %s: count=%d\n", filter, count));
3657 ldap_msgfree(result);
3658 SAFE_FREE(filter);
3659 return NT_STATUS_NO_SUCH_ALIAS;
3662 SAFE_FREE(filter);
3664 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3665 result);
3667 if (!entry) {
3668 ldap_msgfree(result);
3669 return NT_STATUS_UNSUCCESSFUL;
3672 dn = smbldap_talloc_dn(talloc_tos(),
3673 smbldap_get_ldap(ldap_state->smbldap_state),
3674 entry);
3675 if (!dn) {
3676 ldap_msgfree(result);
3677 return NT_STATUS_UNSUCCESSFUL;
3680 smbldap_set_mod(&mods, modop,
3681 get_attr_key2string(groupmap_attr_list,
3682 LDAP_ATTR_SID_LIST),
3683 dom_sid_str_buf(member, &tmp));
3685 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3687 ldap_mods_free(mods, True);
3688 ldap_msgfree(result);
3689 TALLOC_FREE(dn);
3691 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3692 return NT_STATUS_MEMBER_IN_ALIAS;
3695 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3696 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3699 if (rc != LDAP_SUCCESS) {
3700 return NT_STATUS_UNSUCCESSFUL;
3703 return NT_STATUS_OK;
3706 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3707 const struct dom_sid *alias,
3708 const struct dom_sid *member)
3710 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3713 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3714 const struct dom_sid *alias,
3715 const struct dom_sid *member)
3717 return ldapsam_modify_aliasmem(methods, alias, member,
3718 LDAP_MOD_DELETE);
3721 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3722 const struct dom_sid *alias,
3723 TALLOC_CTX *mem_ctx,
3724 struct dom_sid **pp_members,
3725 size_t *p_num_members)
3727 struct ldapsam_privates *ldap_state =
3728 (struct ldapsam_privates *)methods->private_data;
3729 LDAPMessage *result = NULL;
3730 LDAPMessage *entry = NULL;
3731 int count;
3732 char **values = NULL;
3733 int i;
3734 char *filter = NULL;
3735 uint32_t num_members = 0;
3736 enum lsa_SidType type = SID_NAME_USE_NONE;
3737 struct dom_sid_buf tmp;
3739 *pp_members = NULL;
3740 *p_num_members = 0;
3742 if (sid_check_is_in_builtin(alias)) {
3743 type = SID_NAME_ALIAS;
3746 if (sid_check_is_in_our_sam(alias)) {
3747 type = SID_NAME_ALIAS;
3750 if (type == SID_NAME_USE_NONE) {
3751 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3752 dom_sid_str_buf(alias, &tmp)));
3753 return NT_STATUS_NO_SUCH_ALIAS;
3756 if (asprintf(&filter,
3757 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3758 LDAP_OBJ_GROUPMAP,
3759 dom_sid_str_buf(alias, &tmp),
3760 type) < 0) {
3761 return NT_STATUS_NO_MEMORY;
3764 if (ldapsam_search_one_group(ldap_state, filter,
3765 &result) != LDAP_SUCCESS) {
3766 SAFE_FREE(filter);
3767 return NT_STATUS_NO_SUCH_ALIAS;
3770 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3771 result);
3773 if (count < 1) {
3774 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3775 ldap_msgfree(result);
3776 SAFE_FREE(filter);
3777 return NT_STATUS_NO_SUCH_ALIAS;
3780 if (count > 1) {
3781 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3782 "filter %s: count=%d\n", filter, count));
3783 ldap_msgfree(result);
3784 SAFE_FREE(filter);
3785 return NT_STATUS_NO_SUCH_ALIAS;
3788 SAFE_FREE(filter);
3790 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3791 result);
3793 if (!entry) {
3794 ldap_msgfree(result);
3795 return NT_STATUS_UNSUCCESSFUL;
3798 values = ldap_get_values(smbldap_get_ldap(ldap_state->smbldap_state),
3799 entry,
3800 get_attr_key2string(groupmap_attr_list,
3801 LDAP_ATTR_SID_LIST));
3803 if (values == NULL) {
3804 ldap_msgfree(result);
3805 return NT_STATUS_OK;
3808 count = ldap_count_values(values);
3810 for (i=0; i<count; i++) {
3811 struct dom_sid member;
3812 NTSTATUS status;
3814 if (!string_to_sid(&member, values[i]))
3815 continue;
3817 status = add_sid_to_array(mem_ctx, &member, pp_members,
3818 &num_members);
3819 if (!NT_STATUS_IS_OK(status)) {
3820 ldap_value_free(values);
3821 ldap_msgfree(result);
3822 return status;
3826 *p_num_members = num_members;
3827 ldap_value_free(values);
3828 ldap_msgfree(result);
3830 return NT_STATUS_OK;
3833 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3834 TALLOC_CTX *mem_ctx,
3835 const struct dom_sid *domain_sid,
3836 const struct dom_sid *members,
3837 size_t num_members,
3838 uint32_t **pp_alias_rids,
3839 size_t *p_num_alias_rids)
3841 struct ldapsam_privates *ldap_state =
3842 (struct ldapsam_privates *)methods->private_data;
3843 LDAP *ldap_struct;
3845 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3847 LDAPMessage *result = NULL;
3848 LDAPMessage *entry = NULL;
3849 int i;
3850 int rc;
3851 char *filter;
3852 enum lsa_SidType type = SID_NAME_USE_NONE;
3853 bool is_builtin = false;
3854 bool sid_added = false;
3856 *pp_alias_rids = NULL;
3857 *p_num_alias_rids = 0;
3859 if (sid_check_is_builtin(domain_sid)) {
3860 is_builtin = true;
3861 type = SID_NAME_ALIAS;
3864 if (sid_check_is_our_sam(domain_sid)) {
3865 type = SID_NAME_ALIAS;
3868 if (type == SID_NAME_USE_NONE) {
3869 struct dom_sid_buf buf;
3870 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3871 dom_sid_str_buf(domain_sid, &buf)));
3872 return NT_STATUS_UNSUCCESSFUL;
3875 if (num_members == 0) {
3876 return NT_STATUS_OK;
3879 filter = talloc_asprintf(mem_ctx,
3880 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3881 LDAP_OBJ_GROUPMAP, type);
3883 for (i=0; i<num_members; i++) {
3884 struct dom_sid_buf buf;
3885 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3886 filter,
3887 dom_sid_str_buf(&members[i], &buf));
3890 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3892 if (filter == NULL) {
3893 return NT_STATUS_NO_MEMORY;
3896 if (is_builtin &&
3897 ldap_state->search_cache.filter &&
3898 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3899 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3900 result = ldap_state->search_cache.result;
3901 ldap_state->search_cache.result = NULL;
3902 } else {
3903 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3904 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3905 if (rc != LDAP_SUCCESS) {
3906 return NT_STATUS_UNSUCCESSFUL;
3908 smbldap_talloc_autofree_ldapmsg(filter, result);
3911 ldap_struct = smbldap_get_ldap(ldap_state->smbldap_state);
3913 for (entry = ldap_first_entry(ldap_struct, result);
3914 entry != NULL;
3915 entry = ldap_next_entry(ldap_struct, entry))
3917 fstring sid_str;
3918 struct dom_sid sid;
3919 uint32_t rid;
3921 if (!smbldap_get_single_attribute(ldap_struct, entry,
3922 LDAP_ATTRIBUTE_SID,
3923 sid_str,
3924 sizeof(sid_str)-1))
3925 continue;
3927 if (!string_to_sid(&sid, sid_str))
3928 continue;
3930 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3931 continue;
3933 sid_added = true;
3935 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3936 p_num_alias_rids)) {
3937 return NT_STATUS_NO_MEMORY;
3941 if (!is_builtin && !sid_added) {
3942 TALLOC_FREE(ldap_state->search_cache.filter);
3944 * Note: result is a talloc child of filter because of the
3945 * smbldap_talloc_autofree_ldapmsg() usage
3947 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3948 ldap_state->search_cache.result = result;
3951 return NT_STATUS_OK;
3954 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3955 enum pdb_policy_type type,
3956 uint32_t value)
3958 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3959 int rc;
3960 LDAPMod **mods = NULL;
3961 fstring value_string;
3962 const char *policy_attr = NULL;
3964 struct ldapsam_privates *ldap_state =
3965 (struct ldapsam_privates *)methods->private_data;
3967 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3969 if (!ldap_state->domain_dn) {
3970 return NT_STATUS_INVALID_PARAMETER;
3973 policy_attr = get_account_policy_attr(type);
3974 if (policy_attr == NULL) {
3975 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3976 "policy\n"));
3977 return ntstatus;
3980 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3982 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3984 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3985 mods);
3987 ldap_mods_free(mods, True);
3989 if (rc != LDAP_SUCCESS) {
3990 return ntstatus;
3993 if (!cache_account_policy_set(type, value)) {
3994 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3995 "update local tdb cache\n"));
3996 return ntstatus;
3999 return NT_STATUS_OK;
4002 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
4003 enum pdb_policy_type type,
4004 uint32_t value)
4006 return ldapsam_set_account_policy_in_ldap(methods, type,
4007 value);
4010 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
4011 enum pdb_policy_type type,
4012 uint32_t *value)
4014 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4015 LDAPMessage *result = NULL;
4016 LDAPMessage *entry = NULL;
4017 int count;
4018 int rc;
4019 char **vals = NULL;
4020 char *filter;
4021 const char *policy_attr = NULL;
4023 struct ldapsam_privates *ldap_state =
4024 (struct ldapsam_privates *)methods->private_data;
4026 const char *attrs[2];
4028 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4030 if (!ldap_state->domain_dn) {
4031 return NT_STATUS_INVALID_PARAMETER;
4034 policy_attr = get_account_policy_attr(type);
4035 if (!policy_attr) {
4036 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4037 "policy index: %d\n", type));
4038 return ntstatus;
4041 attrs[0] = policy_attr;
4042 attrs[1] = NULL;
4044 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
4045 if (filter == NULL) {
4046 return NT_STATUS_NO_MEMORY;
4048 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
4049 LDAP_SCOPE_BASE, filter, attrs, 0,
4050 &result);
4051 TALLOC_FREE(filter);
4052 if (rc != LDAP_SUCCESS) {
4053 return ntstatus;
4056 count = ldap_count_entries(priv2ld(ldap_state), result);
4057 if (count < 1) {
4058 goto out;
4061 entry = ldap_first_entry(priv2ld(ldap_state), result);
4062 if (entry == NULL) {
4063 goto out;
4066 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4067 if (vals == NULL) {
4068 goto out;
4071 *value = (uint32_t)atol(vals[0]);
4073 ntstatus = NT_STATUS_OK;
4075 out:
4076 if (vals)
4077 ldap_value_free(vals);
4078 ldap_msgfree(result);
4080 return ntstatus;
4083 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4085 - if user hasn't decided to use account policies inside LDAP just reuse the
4086 old tdb values
4088 - if there is a valid cache entry, return that
4089 - if there is an LDAP entry, update cache and return
4090 - otherwise set to default, update cache and return
4092 Guenther
4094 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4095 enum pdb_policy_type type,
4096 uint32_t *value)
4098 NTSTATUS ntstatus;
4100 if (cache_account_policy_get(type, value)) {
4101 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4102 "cache\n"));
4103 return NT_STATUS_OK;
4106 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4107 value);
4108 if (NT_STATUS_IS_OK(ntstatus)) {
4109 goto update_cache;
4112 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4113 "ldap\n"));
4115 #if 0
4116 /* should we automagically migrate old tdb value here ? */
4117 if (account_policy_get(type, value))
4118 goto update_ldap;
4120 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4121 "default\n", type));
4122 #endif
4124 if (!account_policy_get_default(type, value)) {
4125 return ntstatus;
4128 /* update_ldap: */
4130 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4131 if (!NT_STATUS_IS_OK(ntstatus)) {
4132 return ntstatus;
4135 update_cache:
4137 if (!cache_account_policy_set(type, *value)) {
4138 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4139 "tdb as a cache\n"));
4140 return NT_STATUS_UNSUCCESSFUL;
4143 return NT_STATUS_OK;
4146 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4147 const struct dom_sid *domain_sid,
4148 int num_rids,
4149 uint32_t *rids,
4150 const char **names,
4151 enum lsa_SidType *attrs)
4153 struct ldapsam_privates *ldap_state =
4154 (struct ldapsam_privates *)methods->private_data;
4155 LDAPMessage *msg = NULL;
4156 LDAPMessage *entry;
4157 char *allsids = NULL;
4158 size_t i, num_mapped;
4159 int rc;
4160 NTSTATUS result = NT_STATUS_NO_MEMORY;
4161 TALLOC_CTX *mem_ctx;
4162 LDAP *ld;
4163 bool is_builtin;
4165 mem_ctx = talloc_new(NULL);
4166 if (mem_ctx == NULL) {
4167 DEBUG(0, ("talloc_new failed\n"));
4168 goto done;
4171 if (!sid_check_is_builtin(domain_sid) &&
4172 !sid_check_is_our_sam(domain_sid)) {
4173 result = NT_STATUS_INVALID_PARAMETER;
4174 goto done;
4177 if (num_rids == 0) {
4178 result = NT_STATUS_NONE_MAPPED;
4179 goto done;
4182 for (i=0; i<num_rids; i++)
4183 attrs[i] = SID_NAME_UNKNOWN;
4185 allsids = talloc_strdup(mem_ctx, "");
4186 if (allsids == NULL) {
4187 goto done;
4190 for (i=0; i<num_rids; i++) {
4191 struct dom_sid sid;
4192 struct dom_sid_buf buf;
4193 sid_compose(&sid, domain_sid, rids[i]);
4194 allsids = talloc_asprintf_append_buffer(
4195 allsids,
4196 "(sambaSid=%s)",
4197 dom_sid_str_buf(&sid, &buf));
4198 if (allsids == NULL) {
4199 goto done;
4203 /* First look for users */
4206 char *filter;
4207 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4209 filter = talloc_asprintf(
4210 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4211 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4213 if (filter == NULL) {
4214 goto done;
4217 rc = smbldap_search(ldap_state->smbldap_state,
4218 lp_ldap_user_suffix(talloc_tos()),
4219 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4220 &msg);
4221 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4224 if (rc != LDAP_SUCCESS)
4225 goto done;
4227 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4228 num_mapped = 0;
4230 for (entry = ldap_first_entry(ld, msg);
4231 entry != NULL;
4232 entry = ldap_next_entry(ld, entry)) {
4233 uint32_t rid;
4234 int rid_index;
4235 const char *name;
4237 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4238 &rid)) {
4239 DEBUG(2, ("Could not find sid from ldap entry\n"));
4240 continue;
4243 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4244 names);
4245 if (name == NULL) {
4246 DEBUG(2, ("Could not retrieve uid attribute\n"));
4247 continue;
4250 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4251 if (rid == rids[rid_index])
4252 break;
4255 if (rid_index == num_rids) {
4256 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4257 continue;
4260 attrs[rid_index] = SID_NAME_USER;
4261 names[rid_index] = name;
4262 num_mapped += 1;
4265 if (num_mapped == num_rids) {
4266 /* No need to look for groups anymore -- we're done */
4267 result = NT_STATUS_OK;
4268 goto done;
4271 /* Same game for groups */
4274 char *filter;
4275 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4276 "sambaGroupType", NULL };
4278 filter = talloc_asprintf(
4279 mem_ctx, "(&(objectClass=%s)(|%s))",
4280 LDAP_OBJ_GROUPMAP, allsids);
4281 if (filter == NULL) {
4282 goto done;
4285 rc = smbldap_search(ldap_state->smbldap_state,
4286 lp_ldap_suffix(),
4287 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4288 &msg);
4289 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4292 if (rc != LDAP_SUCCESS)
4293 goto done;
4295 /* ldap_struct might have changed due to a reconnect */
4297 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4299 /* For consistency checks, we already checked we're only domain or builtin */
4301 is_builtin = sid_check_is_builtin(domain_sid);
4303 for (entry = ldap_first_entry(ld, msg);
4304 entry != NULL;
4305 entry = ldap_next_entry(ld, entry))
4307 uint32_t rid;
4308 int rid_index;
4309 const char *attr;
4310 enum lsa_SidType type;
4311 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4313 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4314 mem_ctx);
4315 if (attr == NULL) {
4316 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4317 dn));
4318 continue;
4321 type = (enum lsa_SidType)atol(attr);
4323 /* Consistency checks */
4324 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4325 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4326 (type != SID_NAME_DOM_GRP)))) {
4327 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4330 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4331 &rid)) {
4332 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4333 continue;
4336 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4338 if (attr == NULL) {
4339 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4340 dn));
4341 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4344 if (attr == NULL) {
4345 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4346 dn));
4347 continue;
4350 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4351 if (rid == rids[rid_index])
4352 break;
4355 if (rid_index == num_rids) {
4356 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4357 continue;
4360 attrs[rid_index] = type;
4361 names[rid_index] = attr;
4362 num_mapped += 1;
4365 result = NT_STATUS_NONE_MAPPED;
4367 if (num_mapped > 0)
4368 result = (num_mapped == num_rids) ?
4369 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4370 done:
4371 TALLOC_FREE(mem_ctx);
4372 return result;
4375 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4377 char *filter = NULL;
4378 char *escaped = NULL;
4379 char *result = NULL;
4381 if (asprintf(&filter, "(&%s(objectclass=%s))",
4382 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4383 goto done;
4386 escaped = escape_ldap_string(talloc_tos(), username);
4387 if (escaped == NULL) goto done;
4389 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4391 done:
4392 SAFE_FREE(filter);
4393 TALLOC_FREE(escaped);
4395 return result;
4398 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4400 int i, num = 0;
4401 va_list ap;
4402 const char **result;
4404 va_start(ap, mem_ctx);
4405 while (va_arg(ap, const char *) != NULL)
4406 num += 1;
4407 va_end(ap);
4409 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4410 return NULL;
4413 va_start(ap, mem_ctx);
4414 for (i=0; i<num; i++) {
4415 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4416 if (result[i] == NULL) {
4417 talloc_free(result);
4418 va_end(ap);
4419 return NULL;
4422 va_end(ap);
4424 result[num] = NULL;
4425 return result;
4428 struct ldap_search_state {
4429 struct smbldap_state *connection;
4431 uint32_t acct_flags;
4432 uint16_t group_type;
4434 const char *base;
4435 int scope;
4436 const char *filter;
4437 const char **attrs;
4438 int attrsonly;
4439 void *pagedresults_cookie;
4441 LDAPMessage *entries, *current_entry;
4442 bool (*ldap2displayentry)(struct ldap_search_state *state,
4443 TALLOC_CTX *mem_ctx,
4444 LDAP *ld, LDAPMessage *entry,
4445 struct samr_displayentry *result);
4448 static bool ldapsam_search_firstpage(struct pdb_search *search)
4450 struct ldap_search_state *state =
4451 (struct ldap_search_state *)search->private_data;
4452 LDAP *ld;
4453 int rc = LDAP_OPERATIONS_ERROR;
4455 state->entries = NULL;
4457 if (smbldap_get_paged_results(state->connection)) {
4458 rc = smbldap_search_paged(state->connection, state->base,
4459 state->scope, state->filter,
4460 state->attrs, state->attrsonly,
4461 lp_ldap_page_size(), &state->entries,
4462 &state->pagedresults_cookie);
4465 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4467 if (state->entries != NULL) {
4468 /* Left over from unsuccessful paged attempt */
4469 ldap_msgfree(state->entries);
4470 state->entries = NULL;
4473 rc = smbldap_search(state->connection, state->base,
4474 state->scope, state->filter, state->attrs,
4475 state->attrsonly, &state->entries);
4477 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4478 return False;
4480 /* Ok, the server was lying. It told us it could do paged
4481 * searches when it could not. */
4482 smbldap_set_paged_results(state->connection, false);
4485 ld = smbldap_get_ldap(state->connection);
4486 if ( ld == NULL) {
4487 DEBUG(5, ("Don't have an LDAP connection right after a "
4488 "search\n"));
4489 return False;
4491 state->current_entry = ldap_first_entry(ld, state->entries);
4493 return True;
4496 static bool ldapsam_search_nextpage(struct pdb_search *search)
4498 struct ldap_search_state *state =
4499 (struct ldap_search_state *)search->private_data;
4500 int rc;
4502 if (!smbldap_get_paged_results(state->connection)) {
4503 /* There is no next page when there are no paged results */
4504 return False;
4507 rc = smbldap_search_paged(state->connection, state->base,
4508 state->scope, state->filter, state->attrs,
4509 state->attrsonly, lp_ldap_page_size(),
4510 &state->entries,
4511 &state->pagedresults_cookie);
4513 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4514 return False;
4516 state->current_entry = ldap_first_entry(
4517 smbldap_get_ldap(state->connection), state->entries);
4519 if (state->current_entry == NULL) {
4520 ldap_msgfree(state->entries);
4521 state->entries = NULL;
4522 return false;
4525 return True;
4528 static bool ldapsam_search_next_entry(struct pdb_search *search,
4529 struct samr_displayentry *entry)
4531 struct ldap_search_state *state =
4532 (struct ldap_search_state *)search->private_data;
4533 bool result;
4535 retry:
4536 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4537 return False;
4539 if ((state->entries == NULL) &&
4540 !ldapsam_search_nextpage(search))
4541 return False;
4543 if (state->current_entry == NULL) {
4544 return false;
4547 result = state->ldap2displayentry(state, search,
4548 smbldap_get_ldap(state->connection),
4549 state->current_entry, entry);
4551 if (!result) {
4552 char *dn;
4553 dn = ldap_get_dn(smbldap_get_ldap(state->connection),
4554 state->current_entry);
4555 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4556 if (dn != NULL) ldap_memfree(dn);
4559 state->current_entry = ldap_next_entry(
4560 smbldap_get_ldap(state->connection), state->current_entry);
4562 if (state->current_entry == NULL) {
4563 ldap_msgfree(state->entries);
4564 state->entries = NULL;
4567 if (!result) goto retry;
4569 return True;
4572 static void ldapsam_search_end(struct pdb_search *search)
4574 struct ldap_search_state *state =
4575 (struct ldap_search_state *)search->private_data;
4576 int rc;
4578 if (state->pagedresults_cookie == NULL)
4579 return;
4581 if (state->entries != NULL)
4582 ldap_msgfree(state->entries);
4584 state->entries = NULL;
4585 state->current_entry = NULL;
4587 if (!smbldap_get_paged_results(state->connection)) {
4588 return;
4591 /* Tell the LDAP server we're not interested in the rest anymore. */
4593 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4594 state->filter, state->attrs,
4595 state->attrsonly, 0, &state->entries,
4596 &state->pagedresults_cookie);
4598 if (rc != LDAP_SUCCESS)
4599 DEBUG(5, ("Could not end search properly\n"));
4601 return;
4604 static bool ldapuser2displayentry(struct ldap_search_state *state,
4605 TALLOC_CTX *mem_ctx,
4606 LDAP *ld, LDAPMessage *entry,
4607 struct samr_displayentry *result)
4609 char **vals;
4610 size_t converted_size;
4611 struct dom_sid sid;
4612 uint32_t acct_flags;
4614 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4615 if ((vals == NULL) || (vals[0] == NULL)) {
4616 acct_flags = ACB_NORMAL;
4617 } else {
4618 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4619 ldap_value_free(vals);
4622 if ((state->acct_flags != 0) &&
4623 ((state->acct_flags & acct_flags) == 0))
4624 return False;
4626 result->acct_flags = acct_flags;
4627 result->account_name = "";
4628 result->fullname = "";
4629 result->description = "";
4631 vals = ldap_get_values(ld, entry, "uid");
4632 if ((vals == NULL) || (vals[0] == NULL)) {
4633 DEBUG(5, ("\"uid\" not found\n"));
4634 return False;
4636 if (!pull_utf8_talloc(mem_ctx,
4637 discard_const_p(char *, &result->account_name),
4638 vals[0], &converted_size))
4640 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4641 strerror(errno)));
4644 ldap_value_free(vals);
4646 vals = ldap_get_values(ld, entry, "displayName");
4647 if ((vals == NULL) || (vals[0] == NULL))
4648 DEBUG(8, ("\"displayName\" not found\n"));
4649 else if (!pull_utf8_talloc(mem_ctx,
4650 discard_const_p(char *, &result->fullname),
4651 vals[0], &converted_size))
4653 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4654 strerror(errno)));
4657 ldap_value_free(vals);
4659 vals = ldap_get_values(ld, entry, "description");
4660 if ((vals == NULL) || (vals[0] == NULL))
4661 DEBUG(8, ("\"description\" not found\n"));
4662 else if (!pull_utf8_talloc(mem_ctx,
4663 discard_const_p(char *, &result->description),
4664 vals[0], &converted_size))
4666 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4667 strerror(errno)));
4670 ldap_value_free(vals);
4672 if ((result->account_name == NULL) ||
4673 (result->fullname == NULL) ||
4674 (result->description == NULL)) {
4675 DEBUG(0, ("talloc failed\n"));
4676 return False;
4679 vals = ldap_get_values(ld, entry, "sambaSid");
4680 if ((vals == NULL) || (vals[0] == NULL)) {
4681 DEBUG(0, ("\"objectSid\" not found\n"));
4682 return False;
4685 if (!string_to_sid(&sid, vals[0])) {
4686 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4687 ldap_value_free(vals);
4688 return False;
4690 ldap_value_free(vals);
4692 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4693 struct dom_sid_buf buf;
4694 DEBUG(0, ("sid %s does not belong to our domain\n",
4695 dom_sid_str_buf(&sid, &buf)));
4696 return False;
4699 return True;
4703 static bool ldapsam_search_users(struct pdb_methods *methods,
4704 struct pdb_search *search,
4705 uint32_t acct_flags)
4707 struct ldapsam_privates *ldap_state =
4708 (struct ldapsam_privates *)methods->private_data;
4709 struct ldap_search_state *state;
4711 state = talloc(search, struct ldap_search_state);
4712 if (state == NULL) {
4713 DEBUG(0, ("talloc failed\n"));
4714 return False;
4717 state->connection = ldap_state->smbldap_state;
4719 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4720 state->base = lp_ldap_user_suffix(talloc_tos());
4721 else if ((acct_flags != 0) &&
4722 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4723 state->base = lp_ldap_machine_suffix(talloc_tos());
4724 else
4725 state->base = lp_ldap_suffix();
4727 state->acct_flags = acct_flags;
4728 state->base = talloc_strdup(search, state->base);
4729 state->scope = LDAP_SCOPE_SUBTREE;
4730 state->filter = get_ldap_filter(search, "*");
4731 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4732 "displayName", "description",
4733 "sambaAcctFlags", NULL);
4734 state->attrsonly = 0;
4735 state->pagedresults_cookie = NULL;
4736 state->entries = NULL;
4737 state->ldap2displayentry = ldapuser2displayentry;
4739 if ((state->filter == NULL) || (state->attrs == NULL)) {
4740 DEBUG(0, ("talloc failed\n"));
4741 return False;
4744 search->private_data = state;
4745 search->next_entry = ldapsam_search_next_entry;
4746 search->search_end = ldapsam_search_end;
4748 return ldapsam_search_firstpage(search);
4751 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4752 TALLOC_CTX *mem_ctx,
4753 LDAP *ld, LDAPMessage *entry,
4754 struct samr_displayentry *result)
4756 char **vals;
4757 size_t converted_size;
4758 struct dom_sid sid;
4759 uint16_t group_type;
4761 result->account_name = "";
4762 result->fullname = "";
4763 result->description = "";
4766 vals = ldap_get_values(ld, entry, "sambaGroupType");
4767 if ((vals == NULL) || (vals[0] == NULL)) {
4768 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4769 if (vals != NULL) {
4770 ldap_value_free(vals);
4772 return False;
4775 group_type = atoi(vals[0]);
4777 if ((state->group_type != 0) &&
4778 ((state->group_type != group_type))) {
4779 ldap_value_free(vals);
4780 return False;
4783 ldap_value_free(vals);
4785 /* display name is the NT group name */
4787 vals = ldap_get_values(ld, entry, "displayName");
4788 if ((vals == NULL) || (vals[0] == NULL)) {
4789 DEBUG(8, ("\"displayName\" not found\n"));
4791 /* fallback to the 'cn' attribute */
4792 vals = ldap_get_values(ld, entry, "cn");
4793 if ((vals == NULL) || (vals[0] == NULL)) {
4794 DEBUG(5, ("\"cn\" not found\n"));
4795 return False;
4797 if (!pull_utf8_talloc(mem_ctx,
4798 discard_const_p(char *,
4799 &result->account_name),
4800 vals[0], &converted_size))
4802 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4803 "failed: %s", strerror(errno)));
4806 else if (!pull_utf8_talloc(mem_ctx,
4807 discard_const_p(char *,
4808 &result->account_name),
4809 vals[0], &converted_size))
4811 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4812 strerror(errno)));
4815 ldap_value_free(vals);
4817 vals = ldap_get_values(ld, entry, "description");
4818 if ((vals == NULL) || (vals[0] == NULL))
4819 DEBUG(8, ("\"description\" not found\n"));
4820 else if (!pull_utf8_talloc(mem_ctx,
4821 discard_const_p(char *, &result->description),
4822 vals[0], &converted_size))
4824 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4825 strerror(errno)));
4827 ldap_value_free(vals);
4829 if ((result->account_name == NULL) ||
4830 (result->fullname == NULL) ||
4831 (result->description == NULL)) {
4832 DEBUG(0, ("talloc failed\n"));
4833 return False;
4836 vals = ldap_get_values(ld, entry, "sambaSid");
4837 if ((vals == NULL) || (vals[0] == NULL)) {
4838 DEBUG(0, ("\"objectSid\" not found\n"));
4839 if (vals != NULL) {
4840 ldap_value_free(vals);
4842 return False;
4845 if (!string_to_sid(&sid, vals[0])) {
4846 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4847 return False;
4850 ldap_value_free(vals);
4852 switch (group_type) {
4853 case SID_NAME_DOM_GRP:
4854 case SID_NAME_ALIAS:
4856 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4857 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4859 struct dom_sid_buf buf;
4860 DEBUG(0, ("%s is not in our domain\n",
4861 dom_sid_str_buf(&sid, &buf)));
4862 return False;
4864 break;
4866 default:
4867 DEBUG(0,("unknown group type: %d\n", group_type));
4868 return False;
4871 result->acct_flags = 0;
4873 return True;
4876 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4877 struct pdb_search *search,
4878 const struct dom_sid *sid,
4879 enum lsa_SidType type)
4881 struct ldapsam_privates *ldap_state =
4882 (struct ldapsam_privates *)methods->private_data;
4883 struct ldap_search_state *state;
4884 struct dom_sid_buf tmp;
4886 state = talloc(search, struct ldap_search_state);
4887 if (state == NULL) {
4888 DEBUG(0, ("talloc failed\n"));
4889 return False;
4892 state->connection = ldap_state->smbldap_state;
4894 state->base = lp_ldap_suffix();
4895 state->connection = ldap_state->smbldap_state;
4896 state->scope = LDAP_SCOPE_SUBTREE;
4897 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4898 "(sambaGroupType=%d)(sambaSID=%s*))",
4899 LDAP_OBJ_GROUPMAP,
4900 type,
4901 dom_sid_str_buf(sid, &tmp));
4902 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4903 "displayName", "description",
4904 "sambaGroupType", NULL);
4905 state->attrsonly = 0;
4906 state->pagedresults_cookie = NULL;
4907 state->entries = NULL;
4908 state->group_type = type;
4909 state->ldap2displayentry = ldapgroup2displayentry;
4911 if ((state->filter == NULL) || (state->attrs == NULL)) {
4912 DEBUG(0, ("talloc failed\n"));
4913 return False;
4916 search->private_data = state;
4917 search->next_entry = ldapsam_search_next_entry;
4918 search->search_end = ldapsam_search_end;
4920 return ldapsam_search_firstpage(search);
4923 static bool ldapsam_search_groups(struct pdb_methods *methods,
4924 struct pdb_search *search)
4926 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4929 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4930 struct pdb_search *search,
4931 const struct dom_sid *sid)
4933 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4936 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4938 return PDB_CAP_STORE_RIDS;
4941 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4942 uint32_t *rid)
4944 struct smbldap_state *smbldap_state = priv->smbldap_state;
4946 LDAPMessage *result = NULL;
4947 LDAPMessage *entry = NULL;
4948 LDAPMod **mods = NULL;
4949 NTSTATUS status;
4950 char *value;
4951 int rc;
4952 uint32_t nextRid = 0;
4953 const char *dn;
4954 uint32_t tmp;
4955 int error = 0;
4957 TALLOC_CTX *mem_ctx;
4959 mem_ctx = talloc_new(NULL);
4960 if (mem_ctx == NULL) {
4961 DEBUG(0, ("talloc_new failed\n"));
4962 return NT_STATUS_NO_MEMORY;
4965 status = smbldap_search_domain_info(smbldap_state, &result,
4966 get_global_sam_name(), False);
4967 if (!NT_STATUS_IS_OK(status)) {
4968 DEBUG(3, ("Could not get domain info: %s\n",
4969 nt_errstr(status)));
4970 goto done;
4973 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
4975 entry = ldap_first_entry(priv2ld(priv), result);
4976 if (entry == NULL) {
4977 DEBUG(0, ("Could not get domain info entry\n"));
4978 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4979 goto done;
4982 /* Find the largest of the three attributes "sambaNextRid",
4983 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4984 concept of differentiating between user and group rids, and will
4985 use only "sambaNextRid" in the future. But for compatibility
4986 reasons I look if others have chosen different strategies -- VL */
4988 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4989 "sambaNextRid", mem_ctx);
4990 if (value != NULL) {
4991 tmp = (uint32_t)smb_strtoul(value,
4992 NULL,
4994 &error,
4995 SMB_STR_STANDARD);
4996 if (error != 0) {
4997 goto done;
5000 nextRid = MAX(nextRid, tmp);
5003 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5004 "sambaNextUserRid", mem_ctx);
5005 if (value != NULL) {
5006 tmp = (uint32_t)smb_strtoul(value,
5007 NULL,
5009 &error,
5010 SMB_STR_STANDARD);
5011 if (error != 0) {
5012 goto done;
5015 nextRid = MAX(nextRid, tmp);
5018 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5019 "sambaNextGroupRid", mem_ctx);
5020 if (value != NULL) {
5021 tmp = (uint32_t)smb_strtoul(value,
5022 NULL,
5024 &error,
5025 SMB_STR_STANDARD);
5026 if (error != 0) {
5027 goto done;
5030 nextRid = MAX(nextRid, tmp);
5033 if (nextRid == 0) {
5034 nextRid = BASE_RID-1;
5037 nextRid += 1;
5039 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
5040 talloc_asprintf(mem_ctx, "%d", nextRid));
5041 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
5043 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
5044 status = NT_STATUS_NO_MEMORY;
5045 goto done;
5048 rc = smbldap_modify(smbldap_state, dn, mods);
5050 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5051 * please retry" */
5053 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
5055 done:
5056 if (NT_STATUS_IS_OK(status)) {
5057 *rid = nextRid;
5060 TALLOC_FREE(mem_ctx);
5061 return status;
5064 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
5066 int i;
5068 for (i=0; i<10; i++) {
5069 NTSTATUS result = ldapsam_get_new_rid(
5070 (struct ldapsam_privates *)methods->private_data, rid);
5071 if (NT_STATUS_IS_OK(result)) {
5072 return result;
5075 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
5076 return result;
5079 /* The ldap update failed (maybe a race condition), retry */
5082 /* Tried 10 times, fail. */
5083 return NT_STATUS_ACCESS_DENIED;
5086 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
5088 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
5089 return NT_STATUS_IS_OK(result) ? True : False;
5092 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
5093 const struct dom_sid *sid,
5094 struct unixid *id)
5096 struct ldapsam_privates *priv =
5097 (struct ldapsam_privates *)methods->private_data;
5098 char *filter;
5099 int error = 0;
5100 struct dom_sid_buf buf;
5101 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5102 NULL };
5103 LDAPMessage *result = NULL;
5104 LDAPMessage *entry = NULL;
5105 bool ret = False;
5106 char *value;
5107 int rc;
5109 TALLOC_CTX *mem_ctx;
5111 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
5112 if (ret == true) {
5113 return true;
5116 mem_ctx = talloc_new(NULL);
5117 if (mem_ctx == NULL) {
5118 DEBUG(0, ("talloc_new failed\n"));
5119 return False;
5122 filter = talloc_asprintf(mem_ctx,
5123 "(&(sambaSid=%s)"
5124 "(|(objectClass=%s)(objectClass=%s)))",
5125 dom_sid_str_buf(sid, &buf),
5126 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5127 if (filter == NULL) {
5128 DEBUG(5, ("talloc_asprintf failed\n"));
5129 goto done;
5132 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5133 attrs, &result);
5134 if (rc != LDAP_SUCCESS) {
5135 goto done;
5137 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
5139 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5140 DEBUG(10, ("Got %d entries, expected one\n",
5141 ldap_count_entries(priv2ld(priv), result)));
5142 goto done;
5145 entry = ldap_first_entry(priv2ld(priv), result);
5147 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5148 "sambaGroupType", mem_ctx);
5150 if (value != NULL) {
5151 const char *gid_str;
5152 /* It's a group */
5154 gid_str = smbldap_talloc_single_attribute(
5155 priv2ld(priv), entry, "gidNumber", mem_ctx);
5156 if (gid_str == NULL) {
5157 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5158 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5159 entry)));
5160 goto done;
5163 id->id = smb_strtoul(gid_str,
5164 NULL,
5166 &error,
5167 SMB_STR_STANDARD);
5168 if (error != 0) {
5169 goto done;
5172 id->type = ID_TYPE_GID;
5173 ret = True;
5174 goto done;
5177 /* It must be a user */
5179 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5180 "uidNumber", mem_ctx);
5181 if (value == NULL) {
5182 DEBUG(1, ("Could not find uidNumber in %s\n",
5183 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5184 goto done;
5187 id->id = smb_strtoul(value, NULL, 10, &error, SMB_STR_STANDARD);
5188 if (error != 0) {
5189 goto done;
5192 id->type = ID_TYPE_UID;
5193 ret = True;
5194 done:
5195 TALLOC_FREE(mem_ctx);
5196 return ret;
5200 * Find the SID for a uid.
5201 * This is shortcut is only used if ldapsam:trusted is set to true.
5203 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5204 struct dom_sid *sid)
5206 struct ldapsam_privates *priv =
5207 (struct ldapsam_privates *)methods->private_data;
5208 char *filter;
5209 const char *attrs[] = { "sambaSID", NULL };
5210 LDAPMessage *result = NULL;
5211 LDAPMessage *entry = NULL;
5212 bool ret = false;
5213 char *user_sid_string;
5214 struct dom_sid user_sid;
5215 int rc;
5216 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5218 filter = talloc_asprintf(tmp_ctx,
5219 "(&(uidNumber=%u)"
5220 "(objectClass=%s)"
5221 "(objectClass=%s))",
5222 (unsigned int)uid,
5223 LDAP_OBJ_POSIXACCOUNT,
5224 LDAP_OBJ_SAMBASAMACCOUNT);
5225 if (filter == NULL) {
5226 DEBUG(3, ("talloc_asprintf failed\n"));
5227 goto done;
5230 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5231 if (rc != LDAP_SUCCESS) {
5232 goto done;
5234 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5236 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5237 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5238 ldap_count_entries(priv2ld(priv), result),
5239 (unsigned int)uid));
5240 goto done;
5243 entry = ldap_first_entry(priv2ld(priv), result);
5245 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5246 "sambaSID", tmp_ctx);
5247 if (user_sid_string == NULL) {
5248 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5249 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5250 goto done;
5253 if (!string_to_sid(&user_sid, user_sid_string)) {
5254 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5255 user_sid_string));
5256 goto done;
5259 sid_copy(sid, &user_sid);
5261 ret = true;
5263 done:
5264 TALLOC_FREE(tmp_ctx);
5265 return ret;
5269 * Find the SID for a gid.
5270 * This is shortcut is only used if ldapsam:trusted is set to true.
5272 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5273 struct dom_sid *sid)
5275 struct ldapsam_privates *priv =
5276 (struct ldapsam_privates *)methods->private_data;
5277 char *filter;
5278 const char *attrs[] = { "sambaSID", NULL };
5279 LDAPMessage *result = NULL;
5280 LDAPMessage *entry = NULL;
5281 bool ret = false;
5282 char *group_sid_string;
5283 struct dom_sid group_sid;
5284 int rc;
5285 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5287 filter = talloc_asprintf(tmp_ctx,
5288 "(&(gidNumber=%u)"
5289 "(objectClass=%s))",
5290 (unsigned int)gid,
5291 LDAP_OBJ_GROUPMAP);
5292 if (filter == NULL) {
5293 DEBUG(3, ("talloc_asprintf failed\n"));
5294 goto done;
5297 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5298 if (rc != LDAP_SUCCESS) {
5299 goto done;
5301 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5303 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5304 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5305 ldap_count_entries(priv2ld(priv), result),
5306 (unsigned int)gid));
5307 goto done;
5310 entry = ldap_first_entry(priv2ld(priv), result);
5312 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5313 "sambaSID", tmp_ctx);
5314 if (group_sid_string == NULL) {
5315 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5316 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5317 goto done;
5320 if (!string_to_sid(&group_sid, group_sid_string)) {
5321 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5322 group_sid_string));
5323 goto done;
5326 sid_copy(sid, &group_sid);
5328 ret = true;
5330 done:
5331 TALLOC_FREE(tmp_ctx);
5332 return ret;
5335 static bool ldapsam_id_to_sid(struct pdb_methods *methods, struct unixid *id,
5336 struct dom_sid *sid)
5338 switch (id->type) {
5339 case ID_TYPE_UID:
5340 return ldapsam_uid_to_sid(methods, id->id, sid);
5342 case ID_TYPE_GID:
5343 return ldapsam_gid_to_sid(methods, id->id, sid);
5345 default:
5346 return false;
5352 * The following functions are called only if
5353 * ldapsam:trusted and ldapsam:editposix are
5354 * set to true
5358 * ldapsam_create_user creates a new
5359 * posixAccount and sambaSamAccount object
5360 * in the ldap users subtree
5362 * The uid is allocated by winbindd.
5365 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5366 TALLOC_CTX *tmp_ctx, const char *name,
5367 uint32_t acb_info, uint32_t *rid)
5369 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5370 LDAPMessage *entry = NULL;
5371 LDAPMessage *result = NULL;
5372 uint32_t num_result;
5373 bool is_machine = False;
5374 bool add_posix = False;
5375 bool init_okay = False;
5376 LDAPMod **mods = NULL;
5377 struct samu *user;
5378 char *filter;
5379 char *username;
5380 char *homedir;
5381 char *gidstr;
5382 char *uidstr;
5383 char *shell;
5384 const char *dn = NULL;
5385 struct dom_sid group_sid;
5386 struct dom_sid user_sid;
5387 gid_t gid = -1;
5388 uid_t uid = -1;
5389 NTSTATUS ret;
5390 int rc;
5392 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5393 acb_info & ACB_WSTRUST ||
5394 acb_info & ACB_SVRTRUST ||
5395 acb_info & ACB_DOMTRUST) {
5396 is_machine = True;
5399 username = escape_ldap_string(talloc_tos(), name);
5400 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5401 username, LDAP_OBJ_POSIXACCOUNT);
5402 TALLOC_FREE(username);
5404 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5405 if (rc != LDAP_SUCCESS) {
5406 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5407 return NT_STATUS_ACCESS_DENIED;
5409 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5411 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5413 if (num_result > 1) {
5414 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5415 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5418 if (num_result == 1) {
5419 char *tmp;
5420 /* check if it is just a posix account.
5421 * or if there is a sid attached to this entry
5424 entry = ldap_first_entry(priv2ld(ldap_state), result);
5425 if (!entry) {
5426 return NT_STATUS_UNSUCCESSFUL;
5429 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5430 if (tmp) {
5431 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5432 return NT_STATUS_USER_EXISTS;
5435 /* it is just a posix account, retrieve the dn for later use */
5436 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5437 if (!dn) {
5438 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5439 return NT_STATUS_NO_MEMORY;
5443 if (num_result == 0) {
5444 add_posix = True;
5447 /* Create the basic samu structure and generate the mods for the ldap commit */
5448 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5449 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5450 return ret;
5453 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5455 user = samu_new(tmp_ctx);
5456 if (!user) {
5457 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5458 return NT_STATUS_NO_MEMORY;
5461 if (!pdb_set_username(user, name, PDB_SET)) {
5462 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5463 return NT_STATUS_UNSUCCESSFUL;
5465 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5466 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5467 return NT_STATUS_UNSUCCESSFUL;
5469 if (is_machine) {
5470 if (acb_info & ACB_NORMAL) {
5471 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5472 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5473 return NT_STATUS_UNSUCCESSFUL;
5475 } else {
5476 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5477 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5478 return NT_STATUS_UNSUCCESSFUL;
5481 } else {
5482 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5483 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5484 return NT_STATUS_UNSUCCESSFUL;
5488 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5489 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5490 return NT_STATUS_UNSUCCESSFUL;
5493 init_okay = init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed);
5495 if (!init_okay) {
5496 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5497 ldap_mods_free(mods, true);
5498 return NT_STATUS_UNSUCCESSFUL;
5501 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5502 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5504 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5506 if (add_posix) {
5507 char *escape_name;
5509 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5511 /* retrieve the Domain Users group gid */
5512 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5513 !sid_to_gid(&group_sid, &gid)) {
5514 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5515 ldap_mods_free(mods, true);
5516 return NT_STATUS_INVALID_PRIMARY_GROUP;
5519 /* lets allocate a new userid for this user */
5520 if (!winbind_allocate_uid(&uid)) {
5521 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5522 ldap_mods_free(mods, true);
5523 return NT_STATUS_UNSUCCESSFUL;
5527 if (is_machine) {
5528 /* TODO: choose a more appropriate default for machines */
5529 homedir = talloc_sub_specified(tmp_ctx,
5530 lp_template_homedir(),
5531 "SMB_workstations_home",
5532 NULL,
5533 ldap_state->domain_name,
5534 uid,
5535 gid);
5536 shell = talloc_strdup(tmp_ctx, "/bin/false");
5537 } else {
5538 homedir = talloc_sub_specified(tmp_ctx,
5539 lp_template_homedir(),
5540 name,
5541 NULL,
5542 ldap_state->domain_name,
5543 uid,
5544 gid);
5545 shell = talloc_sub_specified(tmp_ctx,
5546 lp_template_shell(),
5547 name,
5548 NULL,
5549 ldap_state->domain_name,
5550 uid,
5551 gid);
5553 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5554 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5556 escape_name = escape_rdn_val_string_alloc(name);
5557 if (!escape_name) {
5558 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5559 ldap_mods_free(mods, true);
5560 return NT_STATUS_NO_MEMORY;
5563 if (is_machine) {
5564 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix (talloc_tos()));
5565 } else {
5566 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix (talloc_tos()));
5569 SAFE_FREE(escape_name);
5571 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5572 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5573 ldap_mods_free(mods, true);
5574 return NT_STATUS_NO_MEMORY;
5577 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5578 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5579 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5580 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5581 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5582 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5583 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5586 if (add_posix) {
5587 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5588 } else {
5589 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5592 ldap_mods_free(mods, true);
5594 if (rc != LDAP_SUCCESS) {
5595 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5596 return NT_STATUS_UNSUCCESSFUL;
5599 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5601 flush_pwnam_cache();
5603 return NT_STATUS_OK;
5606 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5608 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5609 LDAPMessage *result = NULL;
5610 LDAPMessage *entry = NULL;
5611 int num_result;
5612 const char *dn;
5613 char *filter;
5614 int rc;
5616 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5618 filter = talloc_asprintf(tmp_ctx,
5619 "(&(uid=%s)"
5620 "(objectClass=%s)"
5621 "(objectClass=%s))",
5622 pdb_get_username(sam_acct),
5623 LDAP_OBJ_POSIXACCOUNT,
5624 LDAP_OBJ_SAMBASAMACCOUNT);
5625 if (filter == NULL) {
5626 return NT_STATUS_NO_MEMORY;
5629 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5630 if (rc != LDAP_SUCCESS) {
5631 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5632 return NT_STATUS_UNSUCCESSFUL;
5634 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5636 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5638 if (num_result == 0) {
5639 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5640 return NT_STATUS_NO_SUCH_USER;
5643 if (num_result > 1) {
5644 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5645 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5648 entry = ldap_first_entry(priv2ld(ldap_state), result);
5649 if (!entry) {
5650 return NT_STATUS_UNSUCCESSFUL;
5653 /* it is just a posix account, retrieve the dn for later use */
5654 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5655 if (!dn) {
5656 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5657 return NT_STATUS_NO_MEMORY;
5660 /* try to remove memberships first */
5662 NTSTATUS status;
5663 struct dom_sid *sids = NULL;
5664 gid_t *gids = NULL;
5665 uint32_t num_groups = 0;
5666 int i;
5667 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5669 status = ldapsam_enum_group_memberships(my_methods,
5670 tmp_ctx,
5671 sam_acct,
5672 &sids,
5673 &gids,
5674 &num_groups);
5675 if (!NT_STATUS_IS_OK(status)) {
5676 goto delete_dn;
5679 for (i=0; i < num_groups; i++) {
5681 uint32_t group_rid;
5683 sid_peek_rid(&sids[i], &group_rid);
5685 ldapsam_del_groupmem(my_methods,
5686 tmp_ctx,
5687 group_rid,
5688 user_rid);
5692 delete_dn:
5694 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5695 if (rc != LDAP_SUCCESS) {
5696 return NT_STATUS_UNSUCCESSFUL;
5699 flush_pwnam_cache();
5701 return NT_STATUS_OK;
5705 * ldapsam_create_group creates a new
5706 * posixGroup and sambaGroupMapping object
5707 * in the ldap groups subtree
5709 * The gid is allocated by winbindd.
5712 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5713 TALLOC_CTX *tmp_ctx,
5714 const char *name,
5715 uint32_t *rid)
5717 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5718 NTSTATUS ret;
5719 LDAPMessage *entry = NULL;
5720 LDAPMessage *result = NULL;
5721 uint32_t num_result;
5722 bool is_new_entry = False;
5723 LDAPMod **mods = NULL;
5724 char *filter;
5725 char *groupname;
5726 char *grouptype;
5727 char *gidstr;
5728 const char *dn = NULL;
5729 struct dom_sid group_sid;
5730 struct dom_sid_buf buf;
5731 gid_t gid = -1;
5732 int rc;
5733 int error = 0;
5735 groupname = escape_ldap_string(talloc_tos(), name);
5736 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5737 groupname, LDAP_OBJ_POSIXGROUP);
5738 TALLOC_FREE(groupname);
5740 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5741 if (rc != LDAP_SUCCESS) {
5742 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5743 return NT_STATUS_UNSUCCESSFUL;
5745 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5747 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5749 if (num_result > 1) {
5750 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5751 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5754 if (num_result == 1) {
5755 char *tmp;
5756 /* check if it is just a posix group.
5757 * or if there is a sid attached to this entry
5760 entry = ldap_first_entry(priv2ld(ldap_state), result);
5761 if (!entry) {
5762 return NT_STATUS_UNSUCCESSFUL;
5765 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5766 if (tmp) {
5767 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5768 return NT_STATUS_GROUP_EXISTS;
5771 /* it is just a posix group, retrieve the gid and the dn for later use */
5772 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5773 if (!tmp) {
5774 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5775 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5778 gid = smb_strtoul(tmp, NULL, 10, &error, SMB_STR_STANDARD);
5779 if (error != 0) {
5780 DBG_ERR("Failed to convert gidNumber\n");
5781 return NT_STATUS_UNSUCCESSFUL;
5784 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5785 if (!dn) {
5786 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5787 return NT_STATUS_NO_MEMORY;
5791 if (num_result == 0) {
5792 is_new_entry = true;
5795 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5796 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5797 return ret;
5800 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5802 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5804 if (!grouptype) {
5805 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5806 return NT_STATUS_NO_MEMORY;
5809 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5810 smbldap_set_mod(&mods,
5811 LDAP_MOD_ADD,
5812 "sambaSid",
5813 dom_sid_str_buf(&group_sid, &buf));
5814 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5815 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5817 if (is_new_entry) {
5818 char *escape_name;
5820 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5822 /* lets allocate a new groupid for this group */
5823 if (!winbind_allocate_gid(&gid)) {
5824 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5825 return NT_STATUS_UNSUCCESSFUL;
5828 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5830 escape_name = escape_rdn_val_string_alloc(name);
5831 if (!escape_name) {
5832 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5833 return NT_STATUS_NO_MEMORY;
5836 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix(talloc_tos()));
5838 SAFE_FREE(escape_name);
5840 if (!gidstr || !dn) {
5841 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5842 return NT_STATUS_NO_MEMORY;
5845 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5846 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5847 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5850 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
5852 if (is_new_entry) {
5853 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5854 #if 0
5855 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5856 /* This call may fail with rfc2307bis schema */
5857 /* Retry adding a structural class */
5858 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5859 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5861 #endif
5862 } else {
5863 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5866 if (rc != LDAP_SUCCESS) {
5867 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5868 return NT_STATUS_UNSUCCESSFUL;
5871 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5873 return NT_STATUS_OK;
5876 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5878 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5879 LDAPMessage *result = NULL;
5880 LDAPMessage *entry = NULL;
5881 int num_result;
5882 const char *dn;
5883 char *gidstr;
5884 char *filter;
5885 struct dom_sid group_sid;
5886 struct dom_sid_buf buf;
5887 int rc;
5889 /* get the group sid */
5890 sid_compose(&group_sid, get_global_sam_sid(), rid);
5892 filter = talloc_asprintf(tmp_ctx,
5893 "(&(sambaSID=%s)"
5894 "(objectClass=%s)"
5895 "(objectClass=%s))",
5896 dom_sid_str_buf(&group_sid, &buf),
5897 LDAP_OBJ_POSIXGROUP,
5898 LDAP_OBJ_GROUPMAP);
5899 if (filter == NULL) {
5900 return NT_STATUS_NO_MEMORY;
5903 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5904 if (rc != LDAP_SUCCESS) {
5905 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5906 return NT_STATUS_UNSUCCESSFUL;
5908 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5910 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5912 if (num_result == 0) {
5913 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5914 return NT_STATUS_NO_SUCH_GROUP;
5917 if (num_result > 1) {
5918 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5919 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5922 entry = ldap_first_entry(priv2ld(ldap_state), result);
5923 if (!entry) {
5924 return NT_STATUS_UNSUCCESSFUL;
5927 /* here it is, retrieve the dn for later use */
5928 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5929 if (!dn) {
5930 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5931 return NT_STATUS_NO_MEMORY;
5934 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5935 if (!gidstr) {
5936 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5937 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5940 /* check no user have this group marked as primary group */
5941 filter = talloc_asprintf(tmp_ctx,
5942 "(&(gidNumber=%s)"
5943 "(objectClass=%s)"
5944 "(objectClass=%s))",
5945 gidstr,
5946 LDAP_OBJ_POSIXACCOUNT,
5947 LDAP_OBJ_SAMBASAMACCOUNT);
5949 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5950 if (rc != LDAP_SUCCESS) {
5951 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5952 return NT_STATUS_UNSUCCESSFUL;
5954 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5956 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5958 if (num_result != 0) {
5959 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5960 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5963 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5964 if (rc != LDAP_SUCCESS) {
5965 return NT_STATUS_UNSUCCESSFUL;
5968 return NT_STATUS_OK;
5971 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5972 TALLOC_CTX *tmp_ctx,
5973 uint32_t group_rid,
5974 uint32_t member_rid,
5975 int modop)
5977 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5978 LDAPMessage *entry = NULL;
5979 LDAPMessage *result = NULL;
5980 uint32_t num_result;
5981 LDAPMod **mods = NULL;
5982 char *filter;
5983 char *uidstr;
5984 const char *dn = NULL;
5985 struct dom_sid group_sid;
5986 struct dom_sid member_sid;
5987 struct dom_sid_buf buf;
5988 int rc;
5989 int error = 0;
5991 switch (modop) {
5992 case LDAP_MOD_ADD:
5993 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5994 break;
5995 case LDAP_MOD_DELETE:
5996 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5997 break;
5998 default:
5999 return NT_STATUS_UNSUCCESSFUL;
6002 /* get member sid */
6003 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
6005 /* get the group sid */
6006 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
6008 filter = talloc_asprintf(tmp_ctx,
6009 "(&(sambaSID=%s)"
6010 "(objectClass=%s)"
6011 "(objectClass=%s))",
6012 dom_sid_str_buf(&member_sid, &buf),
6013 LDAP_OBJ_POSIXACCOUNT,
6014 LDAP_OBJ_SAMBASAMACCOUNT);
6015 if (filter == NULL) {
6016 return NT_STATUS_NO_MEMORY;
6019 /* get the member uid */
6020 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6021 if (rc != LDAP_SUCCESS) {
6022 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
6023 return NT_STATUS_UNSUCCESSFUL;
6025 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
6027 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6029 if (num_result == 0) {
6030 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6031 return NT_STATUS_NO_SUCH_MEMBER;
6034 if (num_result > 1) {
6035 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6036 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6039 entry = ldap_first_entry(priv2ld(ldap_state), result);
6040 if (!entry) {
6041 return NT_STATUS_UNSUCCESSFUL;
6044 if (modop == LDAP_MOD_DELETE) {
6045 /* check if we are trying to remove the member from his primary group */
6046 char *gidstr;
6047 gid_t user_gid, group_gid;
6049 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
6050 if (!gidstr) {
6051 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6052 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6055 user_gid = smb_strtoul(gidstr, NULL, 10, &error, SMB_STR_STANDARD);
6056 if (error != 0) {
6057 DBG_ERR("Failed to convert user gid\n");
6058 return NT_STATUS_UNSUCCESSFUL;
6061 if (!sid_to_gid(&group_sid, &group_gid)) {
6062 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6063 return NT_STATUS_UNSUCCESSFUL;
6066 if (user_gid == group_gid) {
6067 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6068 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
6072 /* here it is, retrieve the uid for later use */
6073 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
6074 if (!uidstr) {
6075 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6076 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6079 filter = talloc_asprintf(tmp_ctx,
6080 "(&(sambaSID=%s)"
6081 "(objectClass=%s)"
6082 "(objectClass=%s))",
6083 dom_sid_str_buf(&group_sid, &buf),
6084 LDAP_OBJ_POSIXGROUP,
6085 LDAP_OBJ_GROUPMAP);
6087 /* get the group */
6088 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6089 if (rc != LDAP_SUCCESS) {
6090 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6091 return NT_STATUS_UNSUCCESSFUL;
6093 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
6095 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6097 if (num_result == 0) {
6098 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6099 return NT_STATUS_NO_SUCH_GROUP;
6102 if (num_result > 1) {
6103 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6104 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6107 entry = ldap_first_entry(priv2ld(ldap_state), result);
6108 if (!entry) {
6109 return NT_STATUS_UNSUCCESSFUL;
6112 /* here it is, retrieve the dn for later use */
6113 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
6114 if (!dn) {
6115 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6116 return NT_STATUS_NO_MEMORY;
6119 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
6121 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
6123 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6124 if (rc != LDAP_SUCCESS) {
6125 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
6126 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6127 return NT_STATUS_MEMBER_IN_GROUP;
6129 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
6130 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6131 return NT_STATUS_MEMBER_NOT_IN_GROUP;
6133 return NT_STATUS_UNSUCCESSFUL;
6136 return NT_STATUS_OK;
6139 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
6140 TALLOC_CTX *tmp_ctx,
6141 uint32_t group_rid,
6142 uint32_t member_rid)
6144 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
6146 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
6147 TALLOC_CTX *tmp_ctx,
6148 uint32_t group_rid,
6149 uint32_t member_rid)
6151 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
6154 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
6155 TALLOC_CTX *mem_ctx,
6156 struct samu *sampass)
6158 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6159 LDAPMessage *entry = NULL;
6160 LDAPMessage *result = NULL;
6161 uint32_t num_result;
6162 LDAPMod **mods = NULL;
6163 char *filter;
6164 char *escape_username;
6165 char *gidstr;
6166 char *dn = NULL;
6167 gid_t gid;
6168 int rc;
6170 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6172 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6173 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6174 return NT_STATUS_UNSUCCESSFUL;
6176 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6177 if (!gidstr) {
6178 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6179 return NT_STATUS_NO_MEMORY;
6182 escape_username = escape_ldap_string(talloc_tos(),
6183 pdb_get_username(sampass));
6184 if (escape_username== NULL) {
6185 return NT_STATUS_NO_MEMORY;
6188 filter = talloc_asprintf(mem_ctx,
6189 "(&(uid=%s)"
6190 "(objectClass=%s)"
6191 "(objectClass=%s))",
6192 escape_username,
6193 LDAP_OBJ_POSIXACCOUNT,
6194 LDAP_OBJ_SAMBASAMACCOUNT);
6196 TALLOC_FREE(escape_username);
6198 if (filter == NULL) {
6199 return NT_STATUS_NO_MEMORY;
6202 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6203 if (rc != LDAP_SUCCESS) {
6204 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6205 return NT_STATUS_UNSUCCESSFUL;
6207 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6209 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6211 if (num_result == 0) {
6212 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6213 return NT_STATUS_NO_SUCH_USER;
6216 if (num_result > 1) {
6217 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6218 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6221 entry = ldap_first_entry(priv2ld(ldap_state), result);
6222 if (!entry) {
6223 return NT_STATUS_UNSUCCESSFUL;
6226 /* retrieve the dn for later use */
6227 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6228 if (!dn) {
6229 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6230 return NT_STATUS_NO_MEMORY;
6233 /* remove the old one, and add the new one, this way we do not risk races */
6234 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6236 if (mods == NULL) {
6237 TALLOC_FREE(dn);
6238 return NT_STATUS_OK;
6241 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6242 TALLOC_FREE(dn);
6243 if (rc != LDAP_SUCCESS) {
6244 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6245 pdb_get_username(sampass), gidstr));
6246 return NT_STATUS_UNSUCCESSFUL;
6249 flush_pwnam_cache();
6251 return NT_STATUS_OK;
6255 /**********************************************************************
6256 trusted domains functions
6257 *********************************************************************/
6259 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6260 const char *domain)
6262 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6263 ldap_state->domain_dn);
6266 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6267 TALLOC_CTX *mem_ctx,
6268 const char *domain, LDAPMessage **entry)
6270 int rc;
6271 char *filter;
6272 int scope = LDAP_SCOPE_SUBTREE;
6273 const char **attrs = NULL; /* NULL: get all attrs */
6274 int attrsonly = 0; /* 0: return values too */
6275 LDAPMessage *result = NULL;
6276 char *trusted_dn;
6277 uint32_t num_result;
6279 filter = talloc_asprintf(talloc_tos(),
6280 "(&(objectClass=%s)(sambaDomainName=%s))",
6281 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6283 trusted_dn = trusteddom_dn(ldap_state, domain);
6284 if (trusted_dn == NULL) {
6285 return False;
6287 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6288 filter, attrs, attrsonly, &result);
6290 if (result != NULL) {
6291 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6294 if (rc == LDAP_NO_SUCH_OBJECT) {
6295 *entry = NULL;
6296 return True;
6299 if (rc != LDAP_SUCCESS) {
6300 return False;
6303 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6305 if (num_result > 1) {
6306 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6307 "%s object for domain '%s'?!\n",
6308 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6309 return False;
6312 if (num_result == 0) {
6313 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6314 "%s object for domain %s.\n",
6315 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6316 *entry = NULL;
6317 } else {
6318 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6321 return True;
6324 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6325 const char *domain,
6326 char** pwd,
6327 struct dom_sid *sid,
6328 time_t *pass_last_set_time)
6330 struct ldapsam_privates *ldap_state =
6331 (struct ldapsam_privates *)methods->private_data;
6332 LDAPMessage *entry = NULL;
6334 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6336 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6337 (entry == NULL))
6339 return False;
6342 /* password */
6343 if (pwd != NULL) {
6344 char *pwd_str;
6345 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6346 entry, "sambaClearTextPassword", talloc_tos());
6347 if (pwd_str == NULL) {
6348 return False;
6350 /* trusteddom_pw routines do not use talloc yet... */
6351 *pwd = SMB_STRDUP(pwd_str);
6352 if (*pwd == NULL) {
6353 return False;
6357 /* last change time */
6358 if (pass_last_set_time != NULL) {
6359 char *time_str;
6360 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6361 entry, "sambaPwdLastSet", talloc_tos());
6362 if (time_str == NULL) {
6363 return False;
6365 *pass_last_set_time = (time_t)atol(time_str);
6368 /* domain sid */
6369 if (sid != NULL) {
6370 char *sid_str;
6371 struct dom_sid dom_sid;
6372 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6373 entry, "sambaSID",
6374 talloc_tos());
6375 if (sid_str == NULL) {
6376 return False;
6378 if (!string_to_sid(&dom_sid, sid_str)) {
6379 return False;
6381 sid_copy(sid, &dom_sid);
6384 return True;
6387 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6388 const char* domain,
6389 const char* pwd,
6390 const struct dom_sid *sid)
6392 struct ldapsam_privates *ldap_state =
6393 (struct ldapsam_privates *)methods->private_data;
6394 LDAPMessage *entry = NULL;
6395 LDAPMod **mods = NULL;
6396 char *prev_pwd = NULL;
6397 char *trusted_dn = NULL;
6398 struct dom_sid_buf buf;
6399 int rc;
6401 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6404 * get the current entry (if there is one) in order to put the
6405 * current password into the previous password attribute
6407 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6408 return False;
6411 mods = NULL;
6412 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6413 LDAP_OBJ_TRUSTDOM_PASSWORD);
6414 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6415 domain);
6416 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6417 dom_sid_str_buf(sid, &buf));
6418 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6419 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6420 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6421 "sambaClearTextPassword", pwd);
6423 if (entry != NULL) {
6424 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6425 entry, "sambaClearTextPassword", talloc_tos());
6426 if (prev_pwd != NULL) {
6427 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6428 "sambaPreviousClearTextPassword",
6429 prev_pwd);
6433 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods);
6435 trusted_dn = trusteddom_dn(ldap_state, domain);
6436 if (trusted_dn == NULL) {
6437 return False;
6439 if (entry == NULL) {
6440 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6441 } else {
6442 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6445 if (rc != LDAP_SUCCESS) {
6446 DEBUG(1, ("error writing trusted domain password!\n"));
6447 return False;
6450 return True;
6453 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6454 const char *domain)
6456 int rc;
6457 struct ldapsam_privates *ldap_state =
6458 (struct ldapsam_privates *)methods->private_data;
6459 LDAPMessage *entry = NULL;
6460 const char *trusted_dn;
6462 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6463 return False;
6466 if (entry == NULL) {
6467 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6468 "%s\n", domain));
6469 return True;
6472 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6473 entry);
6474 if (trusted_dn == NULL) {
6475 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6476 return False;
6479 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6480 if (rc != LDAP_SUCCESS) {
6481 return False;
6484 return True;
6487 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6488 TALLOC_CTX *mem_ctx,
6489 uint32_t *num_domains,
6490 struct trustdom_info ***domains)
6492 int rc;
6493 struct ldapsam_privates *ldap_state =
6494 (struct ldapsam_privates *)methods->private_data;
6495 char *filter;
6496 int scope = LDAP_SCOPE_SUBTREE;
6497 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6498 int attrsonly = 0; /* 0: return values too */
6499 LDAPMessage *result = NULL;
6500 LDAPMessage *entry = NULL;
6502 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6503 LDAP_OBJ_TRUSTDOM_PASSWORD);
6505 rc = smbldap_search(ldap_state->smbldap_state,
6506 ldap_state->domain_dn,
6507 scope,
6508 filter,
6509 attrs,
6510 attrsonly,
6511 &result);
6513 if (result != NULL) {
6514 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6517 if (rc != LDAP_SUCCESS) {
6518 return NT_STATUS_UNSUCCESSFUL;
6521 *num_domains = 0;
6522 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6523 DEBUG(1, ("talloc failed\n"));
6524 return NT_STATUS_NO_MEMORY;
6527 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6528 entry != NULL;
6529 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6531 char *dom_name, *dom_sid_str;
6532 struct trustdom_info *dom_info;
6534 dom_info = talloc(*domains, struct trustdom_info);
6535 if (dom_info == NULL) {
6536 DEBUG(1, ("talloc failed\n"));
6537 return NT_STATUS_NO_MEMORY;
6540 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6541 entry,
6542 "sambaDomainName",
6543 talloc_tos());
6544 if (dom_name == NULL) {
6545 DEBUG(1, ("talloc failed\n"));
6546 return NT_STATUS_NO_MEMORY;
6548 dom_info->name = dom_name;
6550 dom_sid_str = smbldap_talloc_single_attribute(
6551 priv2ld(ldap_state), entry, "sambaSID",
6552 talloc_tos());
6553 if (dom_sid_str == NULL) {
6554 DEBUG(1, ("talloc failed\n"));
6555 return NT_STATUS_NO_MEMORY;
6557 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6558 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6559 dom_sid_str));
6560 return NT_STATUS_UNSUCCESSFUL;
6563 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6564 domains, num_domains);
6566 if (*domains == NULL) {
6567 DEBUG(1, ("talloc failed\n"));
6568 return NT_STATUS_NO_MEMORY;
6572 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6573 return NT_STATUS_OK;
6577 /**********************************************************************
6578 Housekeeping
6579 *********************************************************************/
6581 static void free_private_data(void **vp)
6583 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6585 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6587 if ((*ldap_state)->result != NULL) {
6588 ldap_msgfree((*ldap_state)->result);
6589 (*ldap_state)->result = NULL;
6591 if ((*ldap_state)->domain_dn != NULL) {
6592 SAFE_FREE((*ldap_state)->domain_dn);
6595 *ldap_state = NULL;
6597 /* No need to free any further, as it is talloc()ed */
6600 /*********************************************************************
6601 Intitalise the parts of the pdb_methods structure that are common to
6602 all pdb_ldap modes
6603 *********************************************************************/
6605 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6607 NTSTATUS nt_status;
6608 struct ldapsam_privates *ldap_state;
6609 char *bind_dn = NULL;
6610 char *bind_secret = NULL;
6612 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6613 return nt_status;
6616 (*pdb_method)->name = "ldapsam";
6618 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6619 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6620 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6621 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6622 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6623 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6625 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6626 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6627 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6628 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6629 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6630 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6631 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6633 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6634 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6636 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6638 (*pdb_method)->capabilities = ldapsam_capabilities;
6639 (*pdb_method)->new_rid = ldapsam_new_rid;
6641 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6642 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6643 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6644 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6646 /* TODO: Setup private data and free */
6648 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6649 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6650 return NT_STATUS_NO_MEMORY;
6653 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6654 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6655 return NT_STATUS_NO_MEMORY;
6658 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6659 location, false, bind_dn, bind_secret,
6660 &ldap_state->smbldap_state);
6661 memset(bind_secret, '\0', strlen(bind_secret));
6662 SAFE_FREE(bind_secret);
6663 SAFE_FREE(bind_dn);
6664 if ( !NT_STATUS_IS_OK(nt_status) ) {
6665 return nt_status;
6668 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6669 return NT_STATUS_NO_MEMORY;
6672 (*pdb_method)->private_data = ldap_state;
6674 (*pdb_method)->free_private_data = free_private_data;
6676 return NT_STATUS_OK;
6679 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods *m)
6681 return true;
6684 /**********************************************************************
6685 Initialise the normal mode for pdb_ldap
6686 *********************************************************************/
6688 NTSTATUS pdb_ldapsam_init_common(struct pdb_methods **pdb_method,
6689 const char *location)
6691 NTSTATUS nt_status;
6692 struct ldapsam_privates *ldap_state = NULL;
6693 uint32_t alg_rid_base;
6694 char *alg_rid_base_string = NULL;
6695 LDAPMessage *result = NULL;
6696 LDAPMessage *entry = NULL;
6697 struct dom_sid ldap_domain_sid;
6698 struct dom_sid secrets_domain_sid;
6699 char *domain_sid_string = NULL;
6700 char *dn = NULL;
6701 char *uri = talloc_strdup( NULL, location );
6703 trim_char( uri, '\"', '\"' );
6704 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6706 TALLOC_FREE(uri);
6708 if (!NT_STATUS_IS_OK(nt_status)) {
6709 return nt_status;
6712 (*pdb_method)->name = "ldapsam";
6714 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6715 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6716 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6717 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6718 (*pdb_method)->search_users = ldapsam_search_users;
6719 (*pdb_method)->search_groups = ldapsam_search_groups;
6720 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6721 (*pdb_method)->is_responsible_for_wellknown =
6722 ldapsam_is_responsible_for_wellknown;
6724 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6725 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6726 (*pdb_method)->enum_group_memberships =
6727 ldapsam_enum_group_memberships;
6728 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6729 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6730 (*pdb_method)->id_to_sid = ldapsam_id_to_sid;
6732 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6733 (*pdb_method)->create_user = ldapsam_create_user;
6734 (*pdb_method)->delete_user = ldapsam_delete_user;
6735 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6736 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6737 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6738 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6739 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6743 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6744 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6746 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6748 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6749 &result,
6750 ldap_state->domain_name, True);
6752 if ( !NT_STATUS_IS_OK(nt_status) ) {
6753 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6754 "info, nor add one to the domain. "
6755 "We cannot work reliably without it.\n"));
6756 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6759 /* Given that the above might fail, everything below this must be
6760 * optional */
6762 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
6763 result);
6764 if (!entry) {
6765 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6766 "entry\n"));
6767 ldap_msgfree(result);
6768 return NT_STATUS_UNSUCCESSFUL;
6771 dn = smbldap_talloc_dn(talloc_tos(),
6772 smbldap_get_ldap(ldap_state->smbldap_state),
6773 entry);
6774 if (!dn) {
6775 ldap_msgfree(result);
6776 return NT_STATUS_UNSUCCESSFUL;
6779 ldap_state->domain_dn = smb_xstrdup(dn);
6780 TALLOC_FREE(dn);
6782 domain_sid_string = smbldap_talloc_single_attribute(
6783 smbldap_get_ldap(ldap_state->smbldap_state),
6784 entry,
6785 get_userattr_key2string(ldap_state->schema_ver,
6786 LDAP_ATTR_USER_SID),
6787 talloc_tos());
6789 if (domain_sid_string) {
6790 bool found_sid;
6791 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6792 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6793 "read as a valid SID\n", domain_sid_string));
6794 ldap_msgfree(result);
6795 TALLOC_FREE(domain_sid_string);
6796 return NT_STATUS_INVALID_PARAMETER;
6798 found_sid = PDB_secrets_fetch_domain_sid(ldap_state->domain_name,
6799 &secrets_domain_sid);
6800 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6801 &ldap_domain_sid)) {
6802 struct dom_sid_buf buf1, buf2;
6803 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6804 "%s based on pdb_ldap results %s -> %s\n",
6805 ldap_state->domain_name,
6806 dom_sid_str_buf(&secrets_domain_sid, &buf1),
6807 dom_sid_str_buf(&ldap_domain_sid, &buf2)));
6809 /* reset secrets.tdb sid */
6810 PDB_secrets_store_domain_sid(ldap_state->domain_name,
6811 &ldap_domain_sid);
6812 DEBUG(1, ("New global sam SID: %s\n",
6813 dom_sid_str_buf(get_global_sam_sid(),
6814 &buf1)));
6816 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6817 TALLOC_FREE(domain_sid_string);
6820 alg_rid_base_string = smbldap_talloc_single_attribute(
6821 smbldap_get_ldap(ldap_state->smbldap_state),
6822 entry,
6823 get_attr_key2string( dominfo_attr_list,
6824 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6825 talloc_tos());
6826 if (alg_rid_base_string) {
6827 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6828 if (alg_rid_base != algorithmic_rid_base()) {
6829 DEBUG(0, ("The value of 'algorithmic RID base' has "
6830 "changed since the LDAP\n"
6831 "database was initialised. Aborting. \n"));
6832 ldap_msgfree(result);
6833 TALLOC_FREE(alg_rid_base_string);
6834 return NT_STATUS_UNSUCCESSFUL;
6836 TALLOC_FREE(alg_rid_base_string);
6838 ldap_msgfree(result);
6840 return NT_STATUS_OK;
6843 NTSTATUS pdb_ldapsam_init(TALLOC_CTX *ctx)
6845 NTSTATUS nt_status;
6847 nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION,
6848 "ldapsam",
6849 pdb_ldapsam_init_common);
6850 if (!NT_STATUS_IS_OK(nt_status)) {
6851 return nt_status;
6854 /* Let pdb_nds register backends */
6855 pdb_nds_init(ctx);
6857 return NT_STATUS_OK;