s3:passdb: Fix code spelling
[Samba.git] / source3 / passdb / pdb_ldap.c
blob83c61e206b5860bc42e8d967c2a1e03eaf276d99
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"
59 #include "source3/lib/substitute.h"
61 #undef DBGC_CLASS
62 #define DBGC_CLASS DBGC_PASSDB
64 #include <lber.h>
65 #include <ldap.h>
68 #include "smbldap.h"
69 #include "passdb/pdb_ldap.h"
70 #include "passdb/pdb_nds.h"
71 #include "passdb/pdb_ldap_util.h"
72 #include "passdb/pdb_ldap_schema.h"
74 /**********************************************************************
75 Simple helper function to make stuff better readable
76 **********************************************************************/
78 LDAP *priv2ld(struct ldapsam_privates *priv)
80 return smbldap_get_ldap(priv->smbldap_state);
83 /**********************************************************************
84 Get the attribute name given a user schame version.
85 **********************************************************************/
87 static const char* get_userattr_key2string( int schema_ver, int key )
89 switch ( schema_ver ) {
90 case SCHEMAVER_SAMBASAMACCOUNT:
91 return get_attr_key2string( attrib_map_v30, key );
93 default:
94 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
95 break;
97 return NULL;
100 /**********************************************************************
101 Return the list of attribute names given a user schema version.
102 **********************************************************************/
104 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
106 switch ( schema_ver ) {
107 case SCHEMAVER_SAMBASAMACCOUNT:
108 return get_attr_list( mem_ctx, attrib_map_v30 );
109 default:
110 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
111 break;
114 return NULL;
117 /**************************************************************************
118 Return the list of attribute names to delete given a user schema version.
119 **************************************************************************/
121 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
122 int schema_ver )
124 switch ( schema_ver ) {
125 case SCHEMAVER_SAMBASAMACCOUNT:
126 return get_attr_list( mem_ctx,
127 attrib_map_to_delete_v30 );
128 default:
129 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
130 break;
133 return NULL;
137 /*******************************************************************
138 Generate the LDAP search filter for the objectclass based on the
139 version of the schema we are using.
140 ******************************************************************/
142 static const char* get_objclass_filter( int schema_ver )
144 fstring objclass_filter;
145 char *result;
147 switch( schema_ver ) {
148 case SCHEMAVER_SAMBASAMACCOUNT:
149 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
150 break;
151 default:
152 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
153 objclass_filter[0] = '\0';
154 break;
157 result = talloc_strdup(talloc_tos(), objclass_filter);
158 SMB_ASSERT(result != NULL);
159 return result;
162 /*****************************************************************
163 Scan a sequence number off OpenLDAP's syncrepl contextCSN
164 ******************************************************************/
166 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
168 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
169 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
170 LDAPMessage *msg = NULL;
171 LDAPMessage *entry = NULL;
172 TALLOC_CTX *mem_ctx;
173 char **values = NULL;
174 int rc, num_result, num_values, rid;
175 char *suffix = NULL;
176 char *tok;
177 const char *p;
178 const char **attrs;
180 /* Unfortunately there is no proper way to detect syncrepl-support in
181 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
182 * but do not show up in the root-DSE yet. Neither we can query the
183 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
184 * objectclass. Currently we require lp_ldap_suffix() to show up as
185 * namingContext. - Guenther
188 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
189 return ntstatus;
192 if (!seq_num) {
193 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
194 return ntstatus;
197 if (!smbldap_has_naming_context(
198 smbldap_get_ldap(ldap_state->smbldap_state),
199 lp_ldap_suffix())) {
200 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
201 "as top-level namingContext\n", lp_ldap_suffix()));
202 return ntstatus;
205 mem_ctx = talloc_init("ldapsam_get_seq_num");
207 if (mem_ctx == NULL)
208 return NT_STATUS_NO_MEMORY;
210 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
211 ntstatus = NT_STATUS_NO_MEMORY;
212 goto done;
215 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
216 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
217 if (rid > 0) {
219 /* consumer syncreplCookie: */
220 /* csn=20050126161620Z#0000001#00#00000 */
221 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
222 attrs[1] = NULL;
223 suffix = talloc_asprintf(mem_ctx,
224 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
225 if (!suffix) {
226 ntstatus = NT_STATUS_NO_MEMORY;
227 goto done;
229 } else {
231 /* provider contextCSN */
232 /* 20050126161620Z#000009#00#000000 */
233 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
234 attrs[1] = NULL;
235 suffix = talloc_asprintf(mem_ctx,
236 "cn=ldapsync,%s", lp_ldap_suffix());
238 if (!suffix) {
239 ntstatus = NT_STATUS_NO_MEMORY;
240 goto done;
244 rc = smbldap_search(ldap_state->smbldap_state, suffix,
245 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
247 if (rc != LDAP_SUCCESS) {
248 goto done;
251 num_result = ldap_count_entries(
252 smbldap_get_ldap(ldap_state->smbldap_state), msg);
253 if (num_result != 1) {
254 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
255 goto done;
258 entry = ldap_first_entry(
259 smbldap_get_ldap(ldap_state->smbldap_state), msg);
260 if (entry == NULL) {
261 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
262 goto done;
265 values = ldap_get_values(
266 smbldap_get_ldap(ldap_state->smbldap_state), entry, attrs[0]);
267 if (values == NULL) {
268 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
269 goto done;
272 num_values = ldap_count_values(values);
273 if (num_values == 0) {
274 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
275 goto done;
278 p = values[0];
279 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
280 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
281 goto done;
284 p = tok;
285 if (!strncmp(p, "csn=", strlen("csn=")))
286 p += strlen("csn=");
288 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
290 *seq_num = generalized_to_unix_time(p);
292 /* very basic sanity check */
293 if (*seq_num <= 0) {
294 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
295 (int)*seq_num));
296 goto done;
299 ntstatus = NT_STATUS_OK;
301 done:
302 if (values != NULL)
303 ldap_value_free(values);
304 if (msg != NULL)
305 ldap_msgfree(msg);
306 if (mem_ctx)
307 talloc_destroy(mem_ctx);
309 return ntstatus;
312 /*******************************************************************
313 Run the search by name.
314 ******************************************************************/
316 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
317 const char *user,
318 LDAPMessage ** result,
319 const char **attr)
321 char *filter = NULL;
322 char *escape_user = escape_ldap_string(talloc_tos(), user);
323 int ret = -1;
325 if (!escape_user) {
326 return LDAP_NO_MEMORY;
330 * in the filter expression, replace %u with the real name
331 * so in ldap filter, %u MUST exist :-)
333 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
334 get_objclass_filter(ldap_state->schema_ver));
335 if (!filter) {
336 TALLOC_FREE(escape_user);
337 return LDAP_NO_MEMORY;
340 * have to use this here because $ is filtered out
341 * in string_sub
344 filter = talloc_all_string_sub(talloc_tos(),
345 filter, "%u", escape_user);
346 TALLOC_FREE(escape_user);
347 if (!filter) {
348 return LDAP_NO_MEMORY;
351 ret = smbldap_search_suffix(ldap_state->smbldap_state,
352 filter, attr, result);
353 TALLOC_FREE(filter);
354 return ret;
357 /*******************************************************************
358 Run the search by SID.
359 ******************************************************************/
361 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
362 const struct dom_sid *sid, LDAPMessage ** result,
363 const char **attr)
365 char *filter = NULL;
366 int rc;
367 struct dom_sid_buf sid_string;
369 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
370 get_userattr_key2string(ldap_state->schema_ver,
371 LDAP_ATTR_USER_SID),
372 dom_sid_str_buf(sid, &sid_string),
373 get_objclass_filter(ldap_state->schema_ver));
374 if (!filter) {
375 return LDAP_NO_MEMORY;
378 rc = smbldap_search_suffix(ldap_state->smbldap_state,
379 filter, attr, result);
381 TALLOC_FREE(filter);
382 return rc;
385 /*******************************************************************
386 Delete complete object or objectclass and attrs from
387 object found in search_result depending on lp_ldap_delete_dn
388 ******************************************************************/
390 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
391 TALLOC_CTX *mem_ctx,
392 LDAPMessage *entry,
393 const char *objectclass,
394 const char **attrs)
396 LDAPMod **mods = NULL;
397 char *name;
398 const char *dn;
399 BerElement *ptr = NULL;
401 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
402 if (dn == NULL) {
403 return LDAP_NO_MEMORY;
406 if (lp_ldap_delete_dn()) {
407 return smbldap_delete(priv->smbldap_state, dn);
410 /* Ok, delete only the SAM attributes */
412 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
413 name != NULL;
414 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
415 const char **attrib;
417 /* We are only allowed to delete the attributes that
418 really exist. */
420 for (attrib = attrs; *attrib != NULL; attrib++) {
421 if (strequal(*attrib, name)) {
422 DEBUG(10, ("ldapsam_delete_entry: deleting "
423 "attribute %s\n", name));
424 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
425 NULL);
428 ldap_memfree(name);
431 if (ptr != NULL) {
432 ber_free(ptr, 0);
435 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
436 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
438 return smbldap_modify(priv->smbldap_state, dn, mods);
441 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
443 char *temp;
444 struct tm tm;
446 temp = smbldap_talloc_single_attribute(
447 smbldap_get_ldap(ldap_state->smbldap_state), entry,
448 get_userattr_key2string(ldap_state->schema_ver,
449 LDAP_ATTR_MOD_TIMESTAMP),
450 talloc_tos());
451 if (!temp) {
452 return (time_t) 0;
455 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
456 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
457 (char*)temp));
458 TALLOC_FREE(temp);
459 return (time_t) 0;
461 TALLOC_FREE(temp);
462 tzset();
463 return timegm(&tm);
466 /**********************************************************************
467 Initialize struct samu from an LDAP query.
468 (Based on init_sam_from_buffer in pdb_tdb.c)
469 *********************************************************************/
471 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
472 struct samu * sampass,
473 LDAPMessage * entry)
475 time_t logon_time,
476 logoff_time,
477 kickoff_time,
478 pass_last_set_time,
479 pass_can_change_time,
480 ldap_entry_time,
481 bad_password_time;
482 char *username = NULL,
483 *domain = NULL,
484 *nt_username = NULL,
485 *fullname = NULL,
486 *homedir = NULL,
487 *dir_drive = NULL,
488 *logon_script = NULL,
489 *profile_path = NULL,
490 *acct_desc = NULL,
491 *workstations = NULL,
492 *munged_dial = NULL;
493 uint32_t user_rid;
494 uint8_t smblmpwd[LM_HASH_LEN],
495 smbntpwd[NT_HASH_LEN];
496 bool use_samba_attrs = True;
497 uint16_t logon_divs;
498 uint16_t bad_password_count = 0,
499 logon_count = 0;
500 uint32_t hours_len;
501 uint8_t hours[MAX_HOURS_LEN];
502 char *temp = NULL;
503 struct login_cache cache_entry;
504 uint32_t pwHistLen;
505 bool expand_explicit = lp_passdb_expand_explicit();
506 bool ret = false;
507 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
509 if (!ctx) {
510 return false;
512 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
513 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
514 goto fn_exit;
517 if (priv2ld(ldap_state) == NULL) {
518 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
519 "ldap_struct is NULL!\n"));
520 goto fn_exit;
523 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
524 entry,
525 "uid",
526 ctx))) {
527 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
528 "this user!\n"));
529 goto fn_exit;
532 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
534 nt_username = talloc_strdup(ctx, username);
535 if (!nt_username) {
536 goto fn_exit;
539 domain = talloc_strdup(ctx, ldap_state->domain_name);
540 if (!domain) {
541 goto fn_exit;
544 pdb_set_username(sampass, username, PDB_SET);
546 pdb_set_domain(sampass, domain, PDB_DEFAULT);
547 pdb_set_nt_username(sampass, nt_username, PDB_SET);
549 /* deal with different attributes between the schema first */
551 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
552 if ((temp = smbldap_talloc_single_attribute(
553 smbldap_get_ldap(ldap_state->smbldap_state),
554 entry,
555 get_userattr_key2string(ldap_state->schema_ver,
556 LDAP_ATTR_USER_SID),
557 ctx))!=NULL) {
558 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
560 } else {
561 if ((temp = smbldap_talloc_single_attribute(
562 smbldap_get_ldap(ldap_state->smbldap_state),
563 entry,
564 get_userattr_key2string(ldap_state->schema_ver,
565 LDAP_ATTR_USER_RID),
566 ctx))!=NULL) {
567 user_rid = (uint32_t)atol(temp);
568 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
572 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
573 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
574 get_userattr_key2string(ldap_state->schema_ver,
575 LDAP_ATTR_USER_SID),
576 get_userattr_key2string(ldap_state->schema_ver,
577 LDAP_ATTR_USER_RID),
578 username));
579 return False;
582 temp = smbldap_talloc_single_attribute(
583 smbldap_get_ldap(ldap_state->smbldap_state),
584 entry,
585 get_userattr_key2string(ldap_state->schema_ver,
586 LDAP_ATTR_PWD_LAST_SET),
587 ctx);
588 if (temp) {
589 pass_last_set_time = (time_t) atol(temp);
590 pdb_set_pass_last_set_time(sampass,
591 pass_last_set_time, PDB_SET);
594 temp = smbldap_talloc_single_attribute(
595 smbldap_get_ldap(ldap_state->smbldap_state),
596 entry,
597 get_userattr_key2string(ldap_state->schema_ver,
598 LDAP_ATTR_LOGON_TIME),
599 ctx);
600 if (temp) {
601 logon_time = (time_t) atol(temp);
602 pdb_set_logon_time(sampass, logon_time, PDB_SET);
605 temp = smbldap_talloc_single_attribute(
606 smbldap_get_ldap(ldap_state->smbldap_state),
607 entry,
608 get_userattr_key2string(ldap_state->schema_ver,
609 LDAP_ATTR_LOGOFF_TIME),
610 ctx);
611 if (temp) {
612 logoff_time = (time_t) atol(temp);
613 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
616 temp = smbldap_talloc_single_attribute(
617 smbldap_get_ldap(ldap_state->smbldap_state),
618 entry,
619 get_userattr_key2string(ldap_state->schema_ver,
620 LDAP_ATTR_KICKOFF_TIME),
621 ctx);
622 if (temp) {
623 kickoff_time = (time_t) atol(temp);
624 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
627 temp = smbldap_talloc_single_attribute(
628 smbldap_get_ldap(ldap_state->smbldap_state),
629 entry,
630 get_userattr_key2string(ldap_state->schema_ver,
631 LDAP_ATTR_PWD_CAN_CHANGE),
632 ctx);
633 if (temp) {
634 pass_can_change_time = (time_t) atol(temp);
635 pdb_set_pass_can_change_time(sampass,
636 pass_can_change_time, PDB_SET);
639 /* recommend that 'gecos' and 'displayName' should refer to the same
640 * attribute OID. userFullName depreciated, only used by Samba
641 * primary rules of LDAP: don't make a new attribute when one is already defined
642 * that fits your needs; using cn then displayName rather than 'userFullName'
645 fullname = smbldap_talloc_single_attribute(
646 smbldap_get_ldap(ldap_state->smbldap_state),
647 entry,
648 get_userattr_key2string(ldap_state->schema_ver,
649 LDAP_ATTR_DISPLAY_NAME),
650 ctx);
651 if (fullname) {
652 pdb_set_fullname(sampass, fullname, PDB_SET);
653 } else {
654 fullname = smbldap_talloc_single_attribute(
655 smbldap_get_ldap(ldap_state->smbldap_state),
656 entry,
657 get_userattr_key2string(ldap_state->schema_ver,
658 LDAP_ATTR_CN),
659 ctx);
660 if (fullname) {
661 pdb_set_fullname(sampass, fullname, PDB_SET);
665 dir_drive = smbldap_talloc_single_attribute(
666 smbldap_get_ldap(ldap_state->smbldap_state),
667 entry,
668 get_userattr_key2string(ldap_state->schema_ver,
669 LDAP_ATTR_HOME_DRIVE),
670 ctx);
671 if (dir_drive) {
672 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
673 } else {
674 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
677 homedir = smbldap_talloc_single_attribute(
678 smbldap_get_ldap(ldap_state->smbldap_state),
679 entry,
680 get_userattr_key2string(ldap_state->schema_ver,
681 LDAP_ATTR_HOME_PATH),
682 ctx);
683 if (homedir) {
684 if (expand_explicit) {
685 homedir = talloc_sub_basic(ctx,
686 username,
687 domain,
688 homedir);
689 if (!homedir) {
690 goto fn_exit;
693 pdb_set_homedir(sampass, homedir, PDB_SET);
694 } else {
695 pdb_set_homedir(sampass,
696 talloc_sub_basic(ctx, username, domain,
697 lp_logon_home()),
698 PDB_DEFAULT);
701 logon_script = smbldap_talloc_single_attribute(
702 smbldap_get_ldap(ldap_state->smbldap_state),
703 entry,
704 get_userattr_key2string(ldap_state->schema_ver,
705 LDAP_ATTR_LOGON_SCRIPT),
706 ctx);
707 if (logon_script) {
708 if (expand_explicit) {
709 logon_script = talloc_sub_basic(ctx,
710 username,
711 domain,
712 logon_script);
713 if (!logon_script) {
714 goto fn_exit;
717 pdb_set_logon_script(sampass, logon_script, PDB_SET);
718 } else {
719 pdb_set_logon_script(sampass,
720 talloc_sub_basic(ctx, username, domain,
721 lp_logon_script()),
722 PDB_DEFAULT );
725 profile_path = smbldap_talloc_single_attribute(
726 smbldap_get_ldap(ldap_state->smbldap_state),
727 entry,
728 get_userattr_key2string(ldap_state->schema_ver,
729 LDAP_ATTR_PROFILE_PATH),
730 ctx);
731 if (profile_path) {
732 if (expand_explicit) {
733 profile_path = talloc_sub_basic(ctx,
734 username,
735 domain,
736 profile_path);
737 if (!profile_path) {
738 goto fn_exit;
741 pdb_set_profile_path(sampass, profile_path, PDB_SET);
742 } else {
743 pdb_set_profile_path(sampass,
744 talloc_sub_basic(ctx, username, domain,
745 lp_logon_path()),
746 PDB_DEFAULT );
749 acct_desc = smbldap_talloc_single_attribute(
750 smbldap_get_ldap(ldap_state->smbldap_state),
751 entry,
752 get_userattr_key2string(ldap_state->schema_ver,
753 LDAP_ATTR_DESC),
754 ctx);
755 if (acct_desc) {
756 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
759 workstations = smbldap_talloc_single_attribute(
760 smbldap_get_ldap(ldap_state->smbldap_state),
761 entry,
762 get_userattr_key2string(ldap_state->schema_ver,
763 LDAP_ATTR_USER_WKS),
764 ctx);
765 if (workstations) {
766 pdb_set_workstations(sampass, workstations, PDB_SET);
769 munged_dial = smbldap_talloc_single_attribute(
770 smbldap_get_ldap(ldap_state->smbldap_state),
771 entry,
772 get_userattr_key2string(ldap_state->schema_ver,
773 LDAP_ATTR_MUNGED_DIAL),
774 ctx);
775 if (munged_dial) {
776 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
779 /* FIXME: hours stuff should be cleaner */
781 logon_divs = 168;
782 hours_len = 21;
783 memset(hours, 0xff, hours_len);
785 if (ldap_state->is_nds_ldap) {
786 char *user_dn;
787 size_t pwd_len;
788 char clear_text_pw[512];
790 /* Make call to Novell eDirectory ldap extension to get clear text password.
791 NOTE: This will only work if we have an SSL connection to eDirectory. */
792 user_dn = smbldap_talloc_dn(
793 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
794 entry);
795 if (user_dn != NULL) {
796 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
798 pwd_len = sizeof(clear_text_pw);
799 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
800 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
801 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
802 TALLOC_FREE(user_dn);
803 return False;
805 ZERO_STRUCT(smblmpwd);
806 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
807 TALLOC_FREE(user_dn);
808 return False;
810 ZERO_STRUCT(smbntpwd);
811 use_samba_attrs = False;
814 TALLOC_FREE(user_dn);
816 } else {
817 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
821 if (use_samba_attrs) {
822 temp = smbldap_talloc_single_attribute(
823 smbldap_get_ldap(ldap_state->smbldap_state),
824 entry,
825 get_userattr_key2string(ldap_state->schema_ver,
826 LDAP_ATTR_LMPW),
827 ctx);
828 if (temp) {
829 pdb_gethexpwd(temp, smblmpwd);
830 memset((char *)temp, '\0', strlen(temp)+1);
831 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
832 goto fn_exit;
834 ZERO_STRUCT(smblmpwd);
837 temp = smbldap_talloc_single_attribute(
838 smbldap_get_ldap(ldap_state->smbldap_state),
839 entry,
840 get_userattr_key2string(ldap_state->schema_ver,
841 LDAP_ATTR_NTPW),
842 ctx);
843 if (temp) {
844 pdb_gethexpwd(temp, smbntpwd);
845 memset((char *)temp, '\0', strlen(temp)+1);
846 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
847 goto fn_exit;
849 ZERO_STRUCT(smbntpwd);
853 pwHistLen = 0;
855 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
856 if (pwHistLen > 0){
857 uint8_t *pwhist = NULL;
858 int i;
859 char *history_string = talloc_array(ctx, char,
860 MAX_PW_HISTORY_LEN*64);
862 if (!history_string) {
863 goto fn_exit;
866 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
868 pwhist = talloc_zero_array(ctx, uint8_t,
869 pwHistLen * PW_HISTORY_ENTRY_LEN);
870 if (pwhist == NULL) {
871 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
872 goto fn_exit;
875 if (smbldap_get_single_attribute(
876 smbldap_get_ldap(ldap_state->smbldap_state),
877 entry,
878 get_userattr_key2string(ldap_state->schema_ver,
879 LDAP_ATTR_PWD_HISTORY),
880 history_string,
881 MAX_PW_HISTORY_LEN*64)) {
882 bool hex_failed = false;
883 for (i = 0; i < pwHistLen; i++){
884 /* Get the 16 byte salt. */
885 if (!pdb_gethexpwd(&history_string[i*64],
886 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
887 hex_failed = true;
888 break;
890 /* Get the 16 byte MD5 hash of salt+passwd. */
891 if (!pdb_gethexpwd(&history_string[(i*64)+32],
892 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
893 PW_HISTORY_SALT_LEN])) {
894 hex_failed = True;
895 break;
898 if (hex_failed) {
899 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
900 username));
901 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
904 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
905 goto fn_exit;
909 temp = smbldap_talloc_single_attribute(
910 smbldap_get_ldap(ldap_state->smbldap_state),
911 entry,
912 get_userattr_key2string(ldap_state->schema_ver,
913 LDAP_ATTR_ACB_INFO),
914 ctx);
915 if (temp) {
916 uint32_t acct_ctrl = 0;
917 acct_ctrl = pdb_decode_acct_ctrl(temp);
919 if (acct_ctrl == 0) {
920 acct_ctrl |= ACB_NORMAL;
923 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
926 pdb_set_hours_len(sampass, hours_len, PDB_SET);
927 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
929 temp = smbldap_talloc_single_attribute(
930 smbldap_get_ldap(ldap_state->smbldap_state),
931 entry,
932 get_userattr_key2string(ldap_state->schema_ver,
933 LDAP_ATTR_BAD_PASSWORD_COUNT),
934 ctx);
935 if (temp) {
936 bad_password_count = (uint32_t) atol(temp);
937 pdb_set_bad_password_count(sampass,
938 bad_password_count, PDB_SET);
941 temp = smbldap_talloc_single_attribute(
942 smbldap_get_ldap(ldap_state->smbldap_state),
943 entry,
944 get_userattr_key2string(ldap_state->schema_ver,
945 LDAP_ATTR_BAD_PASSWORD_TIME),
946 ctx);
947 if (temp) {
948 bad_password_time = (time_t) atol(temp);
949 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
953 temp = smbldap_talloc_single_attribute(
954 smbldap_get_ldap(ldap_state->smbldap_state),
955 entry,
956 get_userattr_key2string(ldap_state->schema_ver,
957 LDAP_ATTR_LOGON_COUNT),
958 ctx);
959 if (temp) {
960 logon_count = (uint32_t) atol(temp);
961 pdb_set_logon_count(sampass, logon_count, PDB_SET);
964 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
966 temp = smbldap_talloc_single_attribute(
967 smbldap_get_ldap(ldap_state->smbldap_state),
968 entry,
969 get_userattr_key2string(ldap_state->schema_ver,
970 LDAP_ATTR_LOGON_HOURS),
971 ctx);
972 if (temp) {
973 pdb_gethexhours(temp, hours);
974 memset((char *)temp, '\0', strlen(temp) +1);
975 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
976 ZERO_STRUCT(hours);
979 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
980 struct passwd unix_pw;
981 bool have_uid = false;
982 bool have_gid = false;
983 struct dom_sid mapped_gsid;
984 const struct dom_sid *primary_gsid;
985 struct unixid id;
986 int error = 0;
988 ZERO_STRUCT(unix_pw);
990 unix_pw.pw_name = username;
991 unix_pw.pw_passwd = discard_const_p(char, "x");
993 temp = smbldap_talloc_single_attribute(
994 priv2ld(ldap_state),
995 entry,
996 "uidNumber",
997 ctx);
998 if (temp) {
999 /* We've got a uid, feed the cache */
1000 unix_pw.pw_uid = smb_strtoul(temp,
1001 NULL,
1003 &error,
1004 SMB_STR_STANDARD);
1005 if (error != 0) {
1006 DBG_ERR("Failed to convert UID\n");
1007 goto fn_exit;
1009 have_uid = true;
1011 temp = smbldap_talloc_single_attribute(
1012 priv2ld(ldap_state),
1013 entry,
1014 "gidNumber",
1015 ctx);
1016 if (temp) {
1017 /* We've got a uid, feed the cache */
1018 unix_pw.pw_gid = smb_strtoul(temp,
1019 NULL,
1021 &error,
1022 SMB_STR_STANDARD);
1023 if (error != 0) {
1024 DBG_ERR("Failed to convert GID\n");
1025 goto fn_exit;
1027 have_gid = true;
1029 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1030 priv2ld(ldap_state),
1031 entry,
1032 "gecos",
1033 ctx);
1034 if (unix_pw.pw_gecos == NULL) {
1035 unix_pw.pw_gecos = fullname;
1037 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1038 priv2ld(ldap_state),
1039 entry,
1040 "homeDirectory",
1041 ctx);
1042 if (unix_pw.pw_dir == NULL) {
1043 unix_pw.pw_dir = discard_const_p(char, "");
1045 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1046 priv2ld(ldap_state),
1047 entry,
1048 "loginShell",
1049 ctx);
1050 if (unix_pw.pw_shell == NULL) {
1051 unix_pw.pw_shell = discard_const_p(char, "");
1054 if (have_uid && have_gid) {
1055 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1056 } else {
1057 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1060 if (sampass->unix_pw == NULL) {
1061 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1062 pdb_get_username(sampass)));
1063 goto fn_exit;
1066 id.id = sampass->unix_pw->pw_uid;
1067 id.type = ID_TYPE_UID;
1069 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass), &id);
1071 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1072 primary_gsid = pdb_get_group_sid(sampass);
1073 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1074 id.id = sampass->unix_pw->pw_gid;
1075 id.type = ID_TYPE_GID;
1077 idmap_cache_set_sid2unixid(primary_gsid, &id);
1081 /* check the timestamp of the cache vs ldap entry */
1082 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1083 entry))) {
1084 ret = true;
1085 goto fn_exit;
1088 /* see if we have newer updates */
1089 if (!login_cache_read(sampass, &cache_entry)) {
1090 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1091 (unsigned int)pdb_get_bad_password_count(sampass),
1092 (unsigned int)pdb_get_bad_password_time(sampass)));
1093 ret = true;
1094 goto fn_exit;
1097 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1098 (unsigned int)ldap_entry_time,
1099 (unsigned int)cache_entry.entry_timestamp,
1100 (unsigned int)cache_entry.bad_password_time));
1102 if (ldap_entry_time > cache_entry.entry_timestamp) {
1103 /* cache is older than directory , so
1104 we need to delete the entry but allow the
1105 fields to be written out */
1106 login_cache_delentry(sampass);
1107 } else {
1108 /* read cache in */
1109 pdb_set_acct_ctrl(sampass,
1110 pdb_get_acct_ctrl(sampass) |
1111 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1112 PDB_SET);
1113 pdb_set_bad_password_count(sampass,
1114 cache_entry.bad_password_count,
1115 PDB_SET);
1116 pdb_set_bad_password_time(sampass,
1117 cache_entry.bad_password_time,
1118 PDB_SET);
1121 ret = true;
1123 fn_exit:
1125 TALLOC_FREE(ctx);
1126 return ret;
1129 /**********************************************************************
1130 Initialize the ldap db from a struct samu. Called on update.
1131 (Based on init_buffer_from_sam in pdb_tdb.c)
1132 *********************************************************************/
1134 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1135 LDAPMessage *existing,
1136 LDAPMod *** mods, struct samu * sampass,
1137 bool (*need_update)(const struct samu *,
1138 enum pdb_elements))
1140 char *temp = NULL;
1142 if (mods == NULL || sampass == NULL) {
1143 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1144 return False;
1147 *mods = NULL;
1150 * took out adding "objectclass: sambaAccount"
1151 * do this on a per-mod basis
1153 if (need_update(sampass, PDB_USERNAME)) {
1154 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1155 existing, mods,
1156 "uid", pdb_get_username(sampass));
1157 if (ldap_state->is_nds_ldap) {
1158 smbldap_make_mod(
1159 smbldap_get_ldap(ldap_state->smbldap_state),
1160 existing, mods,
1161 "cn", pdb_get_username(sampass));
1162 smbldap_make_mod(
1163 smbldap_get_ldap(ldap_state->smbldap_state),
1164 existing, mods,
1165 "sn", pdb_get_username(sampass));
1169 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1171 /* only update the RID if we actually need to */
1172 if (need_update(sampass, PDB_USERSID)) {
1173 struct dom_sid_buf sid_str;
1174 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1176 switch ( ldap_state->schema_ver ) {
1177 case SCHEMAVER_SAMBASAMACCOUNT:
1178 smbldap_make_mod(
1179 smbldap_get_ldap(
1180 ldap_state->smbldap_state),
1181 existing, mods,
1182 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1183 dom_sid_str_buf(user_sid, &sid_str));
1184 break;
1186 default:
1187 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1188 break;
1192 /* we don't need to store the primary group RID - so leaving it
1193 'free' to hang off the unix primary group makes life easier */
1195 if (need_update(sampass, PDB_GROUPSID)) {
1196 struct dom_sid_buf sid_str;
1197 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1199 switch ( ldap_state->schema_ver ) {
1200 case SCHEMAVER_SAMBASAMACCOUNT:
1201 smbldap_make_mod(
1202 smbldap_get_ldap(
1203 ldap_state->smbldap_state),
1204 existing, mods,
1205 get_userattr_key2string(ldap_state->schema_ver,
1206 LDAP_ATTR_PRIMARY_GROUP_SID),
1207 dom_sid_str_buf(group_sid, &sid_str));
1208 break;
1210 default:
1211 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1212 break;
1217 /* displayName, cn, and gecos should all be the same
1218 * most easily accomplished by giving them the same OID
1219 * gecos isn't set here b/c it should be handled by the
1220 * add-user script
1221 * We change displayName only and fall back to cn if
1222 * it does not exist.
1225 if (need_update(sampass, PDB_FULLNAME))
1226 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1227 existing, mods,
1228 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1229 pdb_get_fullname(sampass));
1231 if (need_update(sampass, PDB_ACCTDESC))
1232 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1233 existing, mods,
1234 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1235 pdb_get_acct_desc(sampass));
1237 if (need_update(sampass, PDB_WORKSTATIONS))
1238 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1239 existing, mods,
1240 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1241 pdb_get_workstations(sampass));
1243 if (need_update(sampass, PDB_MUNGEDDIAL))
1244 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1245 existing, mods,
1246 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1247 pdb_get_munged_dial(sampass));
1249 if (need_update(sampass, PDB_SMBHOME))
1250 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1251 existing, mods,
1252 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1253 pdb_get_homedir(sampass));
1255 if (need_update(sampass, PDB_DRIVE))
1256 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1257 existing, mods,
1258 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1259 pdb_get_dir_drive(sampass));
1261 if (need_update(sampass, PDB_LOGONSCRIPT))
1262 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1263 existing, mods,
1264 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1265 pdb_get_logon_script(sampass));
1267 if (need_update(sampass, PDB_PROFILE))
1268 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1269 existing, mods,
1270 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1271 pdb_get_profile_path(sampass));
1273 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1274 return false;
1276 if (need_update(sampass, PDB_LOGONTIME))
1277 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1278 existing, mods,
1279 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1280 SAFE_FREE(temp);
1282 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1283 return false;
1285 if (need_update(sampass, PDB_LOGOFFTIME))
1286 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1287 existing, mods,
1288 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1289 SAFE_FREE(temp);
1291 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1292 return false;
1294 if (need_update(sampass, PDB_KICKOFFTIME))
1295 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1296 existing, mods,
1297 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1298 SAFE_FREE(temp);
1300 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1301 return false;
1303 if (need_update(sampass, PDB_CANCHANGETIME))
1304 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1305 existing, mods,
1306 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1307 SAFE_FREE(temp);
1309 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1310 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1312 if (need_update(sampass, PDB_LMPASSWD)) {
1313 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1314 if (lm_pw) {
1315 char pwstr[34];
1316 pdb_sethexpwd(pwstr, lm_pw,
1317 pdb_get_acct_ctrl(sampass));
1318 smbldap_make_mod(
1319 smbldap_get_ldap(
1320 ldap_state->smbldap_state),
1321 existing, mods,
1322 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1323 pwstr);
1324 } else {
1325 smbldap_make_mod(
1326 smbldap_get_ldap(
1327 ldap_state->smbldap_state),
1328 existing, mods,
1329 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1330 NULL);
1333 if (need_update(sampass, PDB_NTPASSWD)) {
1334 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1335 if (nt_pw) {
1336 char pwstr[34];
1337 pdb_sethexpwd(pwstr, nt_pw,
1338 pdb_get_acct_ctrl(sampass));
1339 smbldap_make_mod(
1340 smbldap_get_ldap(
1341 ldap_state->smbldap_state),
1342 existing, mods,
1343 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1344 pwstr);
1345 } else {
1346 smbldap_make_mod(
1347 smbldap_get_ldap(
1348 ldap_state->smbldap_state),
1349 existing, mods,
1350 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1351 NULL);
1355 if (need_update(sampass, PDB_PWHISTORY)) {
1356 char *pwstr = NULL;
1357 uint32_t pwHistLen = 0;
1358 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1360 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1361 if (!pwstr) {
1362 return false;
1364 if (pwHistLen == 0) {
1365 /* Remove any password history from the LDAP store. */
1366 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1367 pwstr[64] = '\0';
1368 } else {
1369 int i;
1370 uint32_t currHistLen = 0;
1371 const uint8_t *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1372 if (pwhist != NULL) {
1373 /* We can only store (1024-1/64 password history entries. */
1374 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1375 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1376 /* Store the salt. */
1377 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1378 /* Followed by the md5 hash of salt + md4 hash */
1379 pdb_sethexpwd(&pwstr[(i*64)+32],
1380 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1381 DEBUG(100, ("pwstr=%s\n", pwstr));
1385 smbldap_make_mod(
1386 smbldap_get_ldap(ldap_state->smbldap_state),
1387 existing, mods,
1388 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1389 pwstr);
1390 SAFE_FREE(pwstr);
1393 if (need_update(sampass, PDB_PASSLASTSET)) {
1394 if (asprintf(&temp, "%li",
1395 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1396 return false;
1398 smbldap_make_mod(
1399 smbldap_get_ldap(ldap_state->smbldap_state),
1400 existing, mods,
1401 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1402 temp);
1403 SAFE_FREE(temp);
1407 if (need_update(sampass, PDB_HOURS)) {
1408 const uint8_t *hours = pdb_get_hours(sampass);
1409 if (hours) {
1410 char hourstr[44];
1411 pdb_sethexhours(hourstr, hours);
1412 smbldap_make_mod(
1413 smbldap_get_ldap(ldap_state->smbldap_state),
1414 existing,
1415 mods,
1416 get_userattr_key2string(ldap_state->schema_ver,
1417 LDAP_ATTR_LOGON_HOURS),
1418 hourstr);
1422 if (need_update(sampass, PDB_ACCTCTRL))
1423 smbldap_make_mod(
1424 smbldap_get_ldap(ldap_state->smbldap_state),
1425 existing, mods,
1426 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1427 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1429 /* password lockout cache:
1430 - If we are now autolocking or clearing, we write to ldap
1431 - If we are clearing, we delete the cache entry
1432 - If the count is > 0, we update the cache
1434 This even means when autolocking, we cache, just in case the
1435 update doesn't work, and we have to cache the autolock flag */
1437 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1438 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1439 uint16_t badcount = pdb_get_bad_password_count(sampass);
1440 time_t badtime = pdb_get_bad_password_time(sampass);
1441 uint32_t pol;
1442 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1444 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1445 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1447 if ((badcount >= pol) || (badcount == 0)) {
1448 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1449 (unsigned int)badcount, (unsigned int)badtime));
1450 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1451 return false;
1453 smbldap_make_mod(
1454 smbldap_get_ldap(ldap_state->smbldap_state),
1455 existing, mods,
1456 get_userattr_key2string(
1457 ldap_state->schema_ver,
1458 LDAP_ATTR_BAD_PASSWORD_COUNT),
1459 temp);
1460 SAFE_FREE(temp);
1462 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1463 return false;
1465 smbldap_make_mod(
1466 smbldap_get_ldap(ldap_state->smbldap_state),
1467 existing, mods,
1468 get_userattr_key2string(
1469 ldap_state->schema_ver,
1470 LDAP_ATTR_BAD_PASSWORD_TIME),
1471 temp);
1472 SAFE_FREE(temp);
1474 if (badcount == 0) {
1475 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1476 login_cache_delentry(sampass);
1477 } else {
1478 struct login_cache cache_entry;
1480 cache_entry.entry_timestamp = time(NULL);
1481 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1482 cache_entry.bad_password_count = badcount;
1483 cache_entry.bad_password_time = badtime;
1485 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1486 login_cache_write(sampass, &cache_entry);
1490 return True;
1493 /**********************************************************************
1494 End enumeration of the LDAP password list.
1495 *********************************************************************/
1497 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1499 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1500 if (ldap_state->result) {
1501 ldap_msgfree(ldap_state->result);
1502 ldap_state->result = NULL;
1506 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1507 const char *new_attr)
1509 int i;
1511 if (new_attr == NULL) {
1512 return;
1515 for (i=0; (*attr_list)[i] != NULL; i++) {
1519 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1520 const char *, i+2);
1521 SMB_ASSERT((*attr_list) != NULL);
1522 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1523 (*attr_list)[i+1] = NULL;
1526 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1527 const char ***attr_list)
1529 append_attr(mem_ctx, attr_list, "uidNumber");
1530 append_attr(mem_ctx, attr_list, "gidNumber");
1531 append_attr(mem_ctx, attr_list, "homeDirectory");
1532 append_attr(mem_ctx, attr_list, "loginShell");
1533 append_attr(mem_ctx, attr_list, "gecos");
1536 /**********************************************************************
1537 Get struct samu entry from LDAP by username.
1538 *********************************************************************/
1540 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1542 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1543 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1544 LDAPMessage *result = NULL;
1545 LDAPMessage *entry = NULL;
1546 int count;
1547 const char ** attr_list;
1548 int rc;
1550 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1551 append_attr(user, &attr_list,
1552 get_userattr_key2string(ldap_state->schema_ver,
1553 LDAP_ATTR_MOD_TIMESTAMP));
1554 ldapsam_add_unix_attributes(user, &attr_list);
1555 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1556 attr_list);
1557 TALLOC_FREE( attr_list );
1559 if ( rc != LDAP_SUCCESS )
1560 return NT_STATUS_NO_SUCH_USER;
1562 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1563 result);
1565 if (count < 1) {
1566 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1567 ldap_msgfree(result);
1568 return NT_STATUS_NO_SUCH_USER;
1569 } else if (count > 1) {
1570 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1571 ldap_msgfree(result);
1572 return NT_STATUS_NO_SUCH_USER;
1575 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1576 result);
1577 if (entry) {
1578 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1579 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1580 ldap_msgfree(result);
1581 return NT_STATUS_NO_SUCH_USER;
1583 pdb_set_backend_private_data(user, result, NULL,
1584 my_methods, PDB_CHANGED);
1585 smbldap_talloc_autofree_ldapmsg(user, result);
1586 ret = NT_STATUS_OK;
1587 } else {
1588 ldap_msgfree(result);
1590 return ret;
1593 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1594 const struct dom_sid *sid, LDAPMessage **result)
1596 int rc = -1;
1597 const char ** attr_list;
1599 switch ( ldap_state->schema_ver ) {
1600 case SCHEMAVER_SAMBASAMACCOUNT: {
1601 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1602 if (tmp_ctx == NULL) {
1603 return LDAP_NO_MEMORY;
1606 attr_list = get_userattr_list(tmp_ctx,
1607 ldap_state->schema_ver);
1608 append_attr(tmp_ctx, &attr_list,
1609 get_userattr_key2string(
1610 ldap_state->schema_ver,
1611 LDAP_ATTR_MOD_TIMESTAMP));
1612 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1613 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1614 result, attr_list);
1615 TALLOC_FREE(tmp_ctx);
1617 if ( rc != LDAP_SUCCESS )
1618 return rc;
1619 break;
1622 default:
1623 DEBUG(0,("Invalid schema version specified\n"));
1624 break;
1626 return rc;
1629 /**********************************************************************
1630 Get struct samu entry from LDAP by SID.
1631 *********************************************************************/
1633 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1635 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1636 LDAPMessage *result = NULL;
1637 LDAPMessage *entry = NULL;
1638 int count;
1639 int rc;
1641 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1642 sid, &result);
1643 if (rc != LDAP_SUCCESS)
1644 return NT_STATUS_NO_SUCH_USER;
1646 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1647 result);
1649 if (count < 1) {
1650 struct dom_sid_buf buf;
1651 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1652 "count=%d\n",
1653 dom_sid_str_buf(sid, &buf),
1654 count));
1655 ldap_msgfree(result);
1656 return NT_STATUS_NO_SUCH_USER;
1657 } else if (count > 1) {
1658 struct dom_sid_buf buf;
1659 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1660 "[%s]. Failing. count=%d\n",
1661 dom_sid_str_buf(sid, &buf),
1662 count));
1663 ldap_msgfree(result);
1664 return NT_STATUS_NO_SUCH_USER;
1667 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1668 result);
1669 if (!entry) {
1670 ldap_msgfree(result);
1671 return NT_STATUS_NO_SUCH_USER;
1674 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1675 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1676 ldap_msgfree(result);
1677 return NT_STATUS_NO_SUCH_USER;
1680 pdb_set_backend_private_data(user, result, NULL,
1681 my_methods, PDB_CHANGED);
1682 smbldap_talloc_autofree_ldapmsg(user, result);
1683 return NT_STATUS_OK;
1686 /********************************************************************
1687 Do the actual modification - also change a plaintext password if
1688 it it set.
1689 **********************************************************************/
1691 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1692 struct samu *newpwd, char *dn,
1693 LDAPMod **mods, int ldap_op,
1694 bool (*need_update)(const struct samu *, enum pdb_elements))
1696 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1697 int rc;
1699 if (!newpwd || !dn) {
1700 return NT_STATUS_INVALID_PARAMETER;
1703 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1704 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1705 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1706 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1707 BerElement *ber;
1708 struct berval *bv;
1709 char *retoid = NULL;
1710 struct berval *retdata = NULL;
1711 char *utf8_password;
1712 char *utf8_dn;
1713 size_t converted_size;
1714 int ret;
1716 if (!ldap_state->is_nds_ldap) {
1718 if (!smbldap_has_extension(
1719 smbldap_get_ldap(
1720 ldap_state->smbldap_state),
1721 LDAP_EXOP_MODIFY_PASSWD)) {
1722 DEBUG(2, ("ldap password change requested, but LDAP "
1723 "server does not support it -- ignoring\n"));
1724 return NT_STATUS_OK;
1728 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1729 pdb_get_plaintext_passwd(newpwd),
1730 &converted_size))
1732 return NT_STATUS_NO_MEMORY;
1735 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1736 TALLOC_FREE(utf8_password);
1737 return NT_STATUS_NO_MEMORY;
1740 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1741 DEBUG(0,("ber_alloc_t returns NULL\n"));
1742 TALLOC_FREE(utf8_password);
1743 TALLOC_FREE(utf8_dn);
1744 return NT_STATUS_UNSUCCESSFUL;
1747 if ((ber_printf (ber, "{") < 0) ||
1748 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1749 utf8_dn) < 0)) {
1750 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1751 "value <0\n"));
1752 ber_free(ber,1);
1753 TALLOC_FREE(utf8_dn);
1754 TALLOC_FREE(utf8_password);
1755 return NT_STATUS_UNSUCCESSFUL;
1758 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1759 ret = ber_printf(ber, "ts}",
1760 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1761 utf8_password);
1762 } else {
1763 ret = ber_printf(ber, "}");
1766 if (ret < 0) {
1767 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1768 "value <0\n"));
1769 ber_free(ber,1);
1770 TALLOC_FREE(utf8_dn);
1771 TALLOC_FREE(utf8_password);
1772 return NT_STATUS_UNSUCCESSFUL;
1775 if ((rc = ber_flatten (ber, &bv))<0) {
1776 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1777 ber_free(ber,1);
1778 TALLOC_FREE(utf8_dn);
1779 TALLOC_FREE(utf8_password);
1780 return NT_STATUS_UNSUCCESSFUL;
1783 TALLOC_FREE(utf8_dn);
1784 TALLOC_FREE(utf8_password);
1785 ber_free(ber, 1);
1787 if (!ldap_state->is_nds_ldap) {
1788 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1789 LDAP_EXOP_MODIFY_PASSWD,
1790 bv, NULL, NULL, &retoid,
1791 &retdata);
1792 } else {
1793 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1794 pdb_get_plaintext_passwd(newpwd));
1796 if (rc != LDAP_SUCCESS) {
1797 char *ld_error = NULL;
1799 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1800 DEBUG(3, ("Could not set userPassword "
1801 "attribute due to an objectClass "
1802 "violation -- ignoring\n"));
1803 ber_bvfree(bv);
1804 return NT_STATUS_OK;
1807 ldap_get_option(
1808 smbldap_get_ldap(ldap_state->smbldap_state),
1809 LDAP_OPT_ERROR_STRING,
1810 &ld_error);
1811 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1812 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1813 SAFE_FREE(ld_error);
1814 ber_bvfree(bv);
1815 #if defined(LDAP_CONSTRAINT_VIOLATION)
1816 if (rc == LDAP_CONSTRAINT_VIOLATION)
1817 return NT_STATUS_PASSWORD_RESTRICTION;
1818 #endif
1819 return NT_STATUS_UNSUCCESSFUL;
1820 } else {
1821 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1822 #ifdef DEBUG_PASSWORD
1823 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1824 #endif
1825 if (retdata)
1826 ber_bvfree(retdata);
1827 if (retoid)
1828 ldap_memfree(retoid);
1830 ber_bvfree(bv);
1833 if (!mods) {
1834 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1835 /* may be password change below however */
1836 } else {
1837 switch(ldap_op) {
1838 case LDAP_MOD_ADD:
1839 if (ldap_state->is_nds_ldap) {
1840 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1841 "objectclass",
1842 "inetOrgPerson");
1843 } else {
1844 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1845 "objectclass",
1846 LDAP_OBJ_ACCOUNT);
1848 rc = smbldap_add(ldap_state->smbldap_state,
1849 dn, mods);
1850 break;
1851 case LDAP_MOD_REPLACE:
1852 rc = smbldap_modify(ldap_state->smbldap_state,
1853 dn ,mods);
1854 break;
1855 default:
1856 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1857 ldap_op));
1858 return NT_STATUS_INVALID_PARAMETER;
1861 if (rc!=LDAP_SUCCESS) {
1862 return NT_STATUS_UNSUCCESSFUL;
1866 return NT_STATUS_OK;
1869 /**********************************************************************
1870 Delete entry from LDAP for username.
1871 *********************************************************************/
1873 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1874 struct samu * sam_acct)
1876 struct ldapsam_privates *priv =
1877 (struct ldapsam_privates *)my_methods->private_data;
1878 const char *sname;
1879 int rc;
1880 LDAPMessage *msg, *entry;
1881 NTSTATUS result = NT_STATUS_NO_MEMORY;
1882 const char **attr_list;
1883 TALLOC_CTX *mem_ctx;
1885 if (!sam_acct) {
1886 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1887 return NT_STATUS_INVALID_PARAMETER;
1890 sname = pdb_get_username(sam_acct);
1892 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1893 "LDAP.\n", sname));
1895 mem_ctx = talloc_new(NULL);
1896 if (mem_ctx == NULL) {
1897 DEBUG(0, ("talloc_new failed\n"));
1898 goto done;
1901 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1902 if (attr_list == NULL) {
1903 goto done;
1906 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1908 if ((rc != LDAP_SUCCESS) ||
1909 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1910 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1911 DEBUG(5, ("Could not find user %s\n", sname));
1912 result = NT_STATUS_NO_SUCH_USER;
1913 goto done;
1916 rc = ldapsam_delete_entry(
1917 priv, mem_ctx, entry,
1918 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1919 LDAP_OBJ_SAMBASAMACCOUNT : 0,
1920 attr_list);
1922 result = (rc == LDAP_SUCCESS) ?
1923 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1925 done:
1926 TALLOC_FREE(mem_ctx);
1927 return result;
1930 /**********************************************************************
1931 Update struct samu.
1932 *********************************************************************/
1934 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1936 NTSTATUS ret;
1937 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1938 int rc = 0;
1939 char *dn;
1940 LDAPMessage *result = NULL;
1941 LDAPMessage *entry = NULL;
1942 LDAPMod **mods = NULL;
1943 const char **attr_list;
1945 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1946 if (!result) {
1947 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1948 if (pdb_get_username(newpwd) == NULL) {
1949 return NT_STATUS_INVALID_PARAMETER;
1951 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1952 TALLOC_FREE( attr_list );
1953 if (rc != LDAP_SUCCESS) {
1954 return NT_STATUS_UNSUCCESSFUL;
1956 pdb_set_backend_private_data(newpwd, result, NULL,
1957 my_methods, PDB_CHANGED);
1958 smbldap_talloc_autofree_ldapmsg(newpwd, result);
1961 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1962 result) == 0) {
1963 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1964 return NT_STATUS_UNSUCCESSFUL;
1967 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1968 result);
1969 dn = smbldap_talloc_dn(talloc_tos(),
1970 smbldap_get_ldap(ldap_state->smbldap_state),
1971 entry);
1972 if (!dn) {
1973 return NT_STATUS_UNSUCCESSFUL;
1976 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1978 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1979 pdb_element_is_changed)) {
1980 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1981 TALLOC_FREE(dn);
1982 if (mods != NULL)
1983 ldap_mods_free(mods,True);
1984 return NT_STATUS_UNSUCCESSFUL;
1987 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1988 && (mods == NULL)) {
1989 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1990 pdb_get_username(newpwd)));
1991 TALLOC_FREE(dn);
1992 return NT_STATUS_OK;
1995 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1997 if (mods != NULL) {
1998 ldap_mods_free(mods,True);
2001 TALLOC_FREE(dn);
2004 * We need to set the backend private data to NULL here. For example
2005 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2006 * same one, and with the explicit delete / add logic for attribute
2007 * values the second time we would use the wrong "old" value which
2008 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2009 * the update.
2010 * The existing LDAPMessage is still being auto-freed by the
2011 * destructor.
2013 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2014 PDB_CHANGED);
2016 if (!NT_STATUS_IS_OK(ret)) {
2017 return ret;
2020 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2021 pdb_get_username(newpwd)));
2022 return NT_STATUS_OK;
2025 /***************************************************************************
2026 Renames a struct samu
2027 - The "rename user script" has full responsibility for changing everything
2028 ***************************************************************************/
2030 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2031 TALLOC_CTX *tmp_ctx,
2032 uint32_t group_rid,
2033 uint32_t member_rid);
2035 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2036 TALLOC_CTX *mem_ctx,
2037 struct samu *user,
2038 struct dom_sid **pp_sids,
2039 gid_t **pp_gids,
2040 uint32_t *p_num_groups);
2042 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2043 struct samu *old_acct,
2044 const char *newname)
2046 const struct loadparm_substitution *lp_sub =
2047 loadparm_s3_global_substitution();
2048 const char *oldname;
2049 int rc;
2050 char *rename_script = NULL;
2051 fstring oldname_lower, newname_lower;
2053 if (!old_acct) {
2054 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2055 return NT_STATUS_INVALID_PARAMETER;
2057 if (!newname) {
2058 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2059 return NT_STATUS_INVALID_PARAMETER;
2062 oldname = pdb_get_username(old_acct);
2064 /* rename the posix user */
2065 rename_script = lp_rename_user_script(talloc_tos(), lp_sub);
2066 if (rename_script == NULL) {
2067 return NT_STATUS_NO_MEMORY;
2070 if (!(*rename_script)) {
2071 TALLOC_FREE(rename_script);
2072 return NT_STATUS_ACCESS_DENIED;
2075 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2076 oldname, newname));
2078 /* We have to allow the account name to end with a '$'.
2079 Also, follow the semantics in _samr_create_user() and lower case the
2080 posix name but preserve the case in passdb */
2082 fstrcpy( oldname_lower, oldname );
2083 if (!strlower_m( oldname_lower )) {
2084 return NT_STATUS_INVALID_PARAMETER;
2086 fstrcpy( newname_lower, newname );
2087 if (!strlower_m( newname_lower )) {
2088 return NT_STATUS_INVALID_PARAMETER;
2091 rename_script = realloc_string_sub2(rename_script,
2092 "%unew",
2093 newname_lower,
2094 true,
2095 true);
2096 if (!rename_script) {
2097 return NT_STATUS_NO_MEMORY;
2099 rename_script = realloc_string_sub2(rename_script,
2100 "%uold",
2101 oldname_lower,
2102 true,
2103 true);
2104 rc = smbrun(rename_script, NULL, NULL);
2106 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2107 rename_script, rc));
2109 TALLOC_FREE(rename_script);
2111 if (rc == 0) {
2112 smb_nscd_flush_user_cache();
2115 if (rc)
2116 return NT_STATUS_UNSUCCESSFUL;
2118 return NT_STATUS_OK;
2121 /**********************************************************************
2122 Add struct samu to LDAP.
2123 *********************************************************************/
2125 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2127 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2128 int rc;
2129 LDAPMessage *result = NULL;
2130 LDAPMessage *entry = NULL;
2131 LDAPMod **mods = NULL;
2132 int ldap_op = LDAP_MOD_REPLACE;
2133 uint32_t num_result;
2134 const char **attr_list;
2135 char *escape_user = NULL;
2136 const char *username = pdb_get_username(newpwd);
2137 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2138 char *filter = NULL;
2139 char *dn = NULL;
2140 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2141 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2143 if (!ctx) {
2144 return NT_STATUS_NO_MEMORY;
2147 if (!username || !*username) {
2148 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2149 status = NT_STATUS_INVALID_PARAMETER;
2150 goto fn_exit;
2153 /* free this list after the second search or in case we exit on failure */
2154 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2156 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2158 if (rc != LDAP_SUCCESS) {
2159 goto fn_exit;
2162 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
2163 result) != 0) {
2164 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2165 username));
2166 goto fn_exit;
2168 ldap_msgfree(result);
2169 result = NULL;
2171 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2172 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2173 sid, &result);
2174 if (rc == LDAP_SUCCESS) {
2175 if (ldap_count_entries(
2176 smbldap_get_ldap(
2177 ldap_state->smbldap_state),
2178 result) != 0) {
2179 struct dom_sid_buf buf;
2180 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2181 "already in the base, with samba "
2182 "attributes\n",
2183 dom_sid_str_buf(sid, &buf)));
2184 goto fn_exit;
2186 ldap_msgfree(result);
2187 result = NULL;
2191 /* does the entry already exist but without a samba attributes?
2192 we need to return the samba attributes here */
2194 escape_user = escape_ldap_string(talloc_tos(), username);
2195 filter = talloc_strdup(attr_list, "(uid=%u)");
2196 if (!filter) {
2197 status = NT_STATUS_NO_MEMORY;
2198 goto fn_exit;
2200 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2201 TALLOC_FREE(escape_user);
2202 if (!filter) {
2203 status = NT_STATUS_NO_MEMORY;
2204 goto fn_exit;
2207 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2208 filter, attr_list, &result);
2209 if ( rc != LDAP_SUCCESS ) {
2210 goto fn_exit;
2213 num_result = ldap_count_entries(
2214 smbldap_get_ldap(ldap_state->smbldap_state), result);
2216 if (num_result > 1) {
2217 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2218 goto fn_exit;
2221 /* Check if we need to update an existing entry */
2222 if (num_result == 1) {
2223 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2224 ldap_op = LDAP_MOD_REPLACE;
2225 entry = ldap_first_entry(
2226 smbldap_get_ldap(ldap_state->smbldap_state), result);
2227 dn = smbldap_talloc_dn(
2228 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
2229 entry);
2230 if (!dn) {
2231 status = NT_STATUS_NO_MEMORY;
2232 goto fn_exit;
2235 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2237 struct dom_sid_buf buf;
2239 /* There might be a SID for this account already - say an idmap entry */
2241 filter = talloc_asprintf(ctx,
2242 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2243 get_userattr_key2string(ldap_state->schema_ver,
2244 LDAP_ATTR_USER_SID),
2245 dom_sid_str_buf(sid, &buf),
2246 LDAP_OBJ_IDMAP_ENTRY,
2247 LDAP_OBJ_SID_ENTRY);
2248 if (!filter) {
2249 status = NT_STATUS_NO_MEMORY;
2250 goto fn_exit;
2253 /* free old result before doing a new search */
2254 if (result != NULL) {
2255 ldap_msgfree(result);
2256 result = NULL;
2258 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2259 filter, attr_list, &result);
2261 if ( rc != LDAP_SUCCESS ) {
2262 goto fn_exit;
2265 num_result = ldap_count_entries(
2266 smbldap_get_ldap(ldap_state->smbldap_state), result);
2268 if (num_result > 1) {
2269 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2270 goto fn_exit;
2273 /* Check if we need to update an existing entry */
2274 if (num_result == 1) {
2276 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2277 ldap_op = LDAP_MOD_REPLACE;
2278 entry = ldap_first_entry (
2279 smbldap_get_ldap(ldap_state->smbldap_state),
2280 result);
2281 dn = smbldap_talloc_dn (
2282 ctx,
2283 smbldap_get_ldap(ldap_state->smbldap_state),
2284 entry);
2285 if (!dn) {
2286 status = NT_STATUS_NO_MEMORY;
2287 goto fn_exit;
2292 if (num_result == 0) {
2293 char *escape_username;
2294 /* Check if we need to add an entry */
2295 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2296 ldap_op = LDAP_MOD_ADD;
2298 escape_username = escape_rdn_val_string_alloc(username);
2299 if (!escape_username) {
2300 status = NT_STATUS_NO_MEMORY;
2301 goto fn_exit;
2304 if (username[strlen(username)-1] == '$') {
2305 dn = talloc_asprintf(ctx,
2306 "uid=%s,%s",
2307 escape_username,
2308 lp_ldap_machine_suffix(talloc_tos()));
2309 } else {
2310 dn = talloc_asprintf(ctx,
2311 "uid=%s,%s",
2312 escape_username,
2313 lp_ldap_user_suffix(talloc_tos()));
2316 SAFE_FREE(escape_username);
2317 if (!dn) {
2318 status = NT_STATUS_NO_MEMORY;
2319 goto fn_exit;
2323 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2324 pdb_element_is_set_or_changed)) {
2325 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2326 if (mods != NULL) {
2327 ldap_mods_free(mods, true);
2329 goto fn_exit;
2332 if (mods == NULL) {
2333 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2334 goto fn_exit;
2336 switch ( ldap_state->schema_ver ) {
2337 case SCHEMAVER_SAMBASAMACCOUNT:
2338 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2339 break;
2340 default:
2341 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2342 break;
2345 status = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2346 if (!NT_STATUS_IS_OK(status)) {
2347 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2348 pdb_get_username(newpwd),dn));
2349 ldap_mods_free(mods, true);
2350 goto fn_exit;
2353 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2354 ldap_mods_free(mods, true);
2356 status = NT_STATUS_OK;
2358 fn_exit:
2360 TALLOC_FREE(ctx);
2361 if (result) {
2362 ldap_msgfree(result);
2364 return status;
2367 /**********************************************************************
2368 *********************************************************************/
2370 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2371 const char *filter,
2372 LDAPMessage ** result)
2374 int scope = LDAP_SCOPE_SUBTREE;
2375 int rc;
2376 const char **attr_list;
2378 attr_list = get_attr_list(NULL, groupmap_attr_list);
2379 rc = smbldap_search(ldap_state->smbldap_state,
2380 lp_ldap_suffix(), scope,
2381 filter, attr_list, 0, result);
2382 TALLOC_FREE(attr_list);
2384 return rc;
2387 /**********************************************************************
2388 *********************************************************************/
2390 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2391 GROUP_MAP *map, LDAPMessage *entry)
2393 char *temp = NULL;
2394 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2396 if (ldap_state == NULL || map == NULL || entry == NULL ||
2397 smbldap_get_ldap(ldap_state->smbldap_state) == NULL) {
2398 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2399 TALLOC_FREE(ctx);
2400 return false;
2403 temp = smbldap_talloc_single_attribute(
2404 smbldap_get_ldap(ldap_state->smbldap_state),
2405 entry,
2406 get_attr_key2string(groupmap_attr_list,
2407 LDAP_ATTR_GIDNUMBER),
2408 ctx);
2409 if (!temp) {
2410 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2411 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2412 TALLOC_FREE(ctx);
2413 return false;
2415 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2417 map->gid = (gid_t)atol(temp);
2419 TALLOC_FREE(temp);
2420 temp = smbldap_talloc_single_attribute(
2421 smbldap_get_ldap(ldap_state->smbldap_state),
2422 entry,
2423 get_attr_key2string(groupmap_attr_list,
2424 LDAP_ATTR_GROUP_SID),
2425 ctx);
2426 if (!temp) {
2427 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2428 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2429 TALLOC_FREE(ctx);
2430 return false;
2433 if (!string_to_sid(&map->sid, temp)) {
2434 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2435 TALLOC_FREE(ctx);
2436 return false;
2439 TALLOC_FREE(temp);
2440 temp = smbldap_talloc_single_attribute(
2441 smbldap_get_ldap(ldap_state->smbldap_state),
2442 entry,
2443 get_attr_key2string(groupmap_attr_list,
2444 LDAP_ATTR_GROUP_TYPE),
2445 ctx);
2446 if (!temp) {
2447 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2448 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2449 TALLOC_FREE(ctx);
2450 return false;
2452 map->sid_name_use = (enum lsa_SidType)atol(temp);
2454 if ((map->sid_name_use < SID_NAME_USER) ||
2455 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2456 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2457 TALLOC_FREE(ctx);
2458 return false;
2461 TALLOC_FREE(temp);
2462 temp = smbldap_talloc_single_attribute(
2463 smbldap_get_ldap(ldap_state->smbldap_state),
2464 entry,
2465 get_attr_key2string(groupmap_attr_list,
2466 LDAP_ATTR_DISPLAY_NAME),
2467 ctx);
2468 if (!temp) {
2469 temp = smbldap_talloc_single_attribute(
2470 smbldap_get_ldap(ldap_state->smbldap_state),
2471 entry,
2472 get_attr_key2string(groupmap_attr_list,
2473 LDAP_ATTR_CN),
2474 ctx);
2475 if (!temp) {
2476 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2477 for gidNumber(%lu)\n",(unsigned long)map->gid));
2478 TALLOC_FREE(ctx);
2479 return false;
2482 map->nt_name = talloc_strdup(map, temp);
2483 if (!map->nt_name) {
2484 TALLOC_FREE(ctx);
2485 return false;
2488 TALLOC_FREE(temp);
2489 temp = smbldap_talloc_single_attribute(
2490 smbldap_get_ldap(ldap_state->smbldap_state),
2491 entry,
2492 get_attr_key2string(groupmap_attr_list,
2493 LDAP_ATTR_DESC),
2494 ctx);
2495 if (!temp) {
2496 temp = talloc_strdup(ctx, "");
2497 if (!temp) {
2498 TALLOC_FREE(ctx);
2499 return false;
2502 map->comment = talloc_strdup(map, temp);
2503 if (!map->comment) {
2504 TALLOC_FREE(ctx);
2505 return false;
2508 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2509 struct unixid id;
2510 id.id = map->gid;
2511 id.type = ID_TYPE_GID;
2513 idmap_cache_set_sid2unixid(&map->sid, &id);
2516 TALLOC_FREE(ctx);
2517 return true;
2520 /**********************************************************************
2521 *********************************************************************/
2523 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2524 const char *filter,
2525 GROUP_MAP *map)
2527 struct ldapsam_privates *ldap_state =
2528 (struct ldapsam_privates *)methods->private_data;
2529 LDAPMessage *result = NULL;
2530 LDAPMessage *entry = NULL;
2531 int count;
2533 if (ldapsam_search_one_group(ldap_state, filter, &result)
2534 != LDAP_SUCCESS) {
2535 return NT_STATUS_NO_SUCH_GROUP;
2538 count = ldap_count_entries(priv2ld(ldap_state), result);
2540 if (count < 1) {
2541 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2542 "%s\n", filter));
2543 ldap_msgfree(result);
2544 return NT_STATUS_NO_SUCH_GROUP;
2547 if (count > 1) {
2548 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2549 "count=%d\n", filter, count));
2550 ldap_msgfree(result);
2551 return NT_STATUS_NO_SUCH_GROUP;
2554 entry = ldap_first_entry(priv2ld(ldap_state), result);
2556 if (!entry) {
2557 ldap_msgfree(result);
2558 return NT_STATUS_UNSUCCESSFUL;
2561 if (!init_group_from_ldap(ldap_state, map, entry)) {
2562 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2563 "group filter %s\n", filter));
2564 ldap_msgfree(result);
2565 return NT_STATUS_NO_SUCH_GROUP;
2568 ldap_msgfree(result);
2569 return NT_STATUS_OK;
2572 /**********************************************************************
2573 *********************************************************************/
2575 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2576 struct dom_sid sid)
2578 char *filter = NULL;
2579 NTSTATUS status;
2580 struct dom_sid_buf tmp;
2582 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2583 LDAP_OBJ_GROUPMAP,
2584 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2585 dom_sid_str_buf(&sid, &tmp)) < 0) {
2586 return NT_STATUS_NO_MEMORY;
2589 status = ldapsam_getgroup(methods, filter, map);
2590 SAFE_FREE(filter);
2591 return status;
2594 /**********************************************************************
2595 *********************************************************************/
2597 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2598 gid_t gid)
2600 char *filter = NULL;
2601 NTSTATUS status;
2603 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2604 LDAP_OBJ_GROUPMAP,
2605 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2606 (unsigned long)gid) < 0) {
2607 return NT_STATUS_NO_MEMORY;
2610 status = ldapsam_getgroup(methods, filter, map);
2611 SAFE_FREE(filter);
2612 return status;
2615 /**********************************************************************
2616 *********************************************************************/
2618 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2619 const char *name)
2621 char *filter = NULL;
2622 char *escape_name = escape_ldap_string(talloc_tos(), name);
2623 NTSTATUS status;
2625 if (!escape_name) {
2626 return NT_STATUS_NO_MEMORY;
2629 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2630 LDAP_OBJ_GROUPMAP,
2631 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2632 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2633 escape_name) < 0) {
2634 TALLOC_FREE(escape_name);
2635 return NT_STATUS_NO_MEMORY;
2638 TALLOC_FREE(escape_name);
2639 status = ldapsam_getgroup(methods, filter, map);
2640 SAFE_FREE(filter);
2641 return status;
2644 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2645 LDAPMessage *entry,
2646 const struct dom_sid *domain_sid,
2647 uint32_t *rid)
2649 fstring str;
2650 struct dom_sid sid;
2652 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2653 str, sizeof(str)-1)) {
2654 DEBUG(10, ("Could not find sambaSID attribute\n"));
2655 return False;
2658 if (!string_to_sid(&sid, str)) {
2659 DEBUG(10, ("Could not convert string %s to sid\n", str));
2660 return False;
2663 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2664 struct dom_sid_buf buf;
2665 DEBUG(10, ("SID %s is not in expected domain %s\n",
2666 str,
2667 dom_sid_str_buf(domain_sid, &buf)));
2668 return False;
2671 if (!sid_peek_rid(&sid, rid)) {
2672 DEBUG(10, ("Could not peek into RID\n"));
2673 return False;
2676 return True;
2679 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2680 TALLOC_CTX *mem_ctx,
2681 const struct dom_sid *group,
2682 uint32_t **pp_member_rids,
2683 size_t *p_num_members)
2685 struct ldapsam_privates *ldap_state =
2686 (struct ldapsam_privates *)methods->private_data;
2687 struct smbldap_state *conn = ldap_state->smbldap_state;
2688 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2689 const char *sid_attrs[] = { "sambaSID", NULL };
2690 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2691 LDAPMessage *result = NULL;
2692 LDAPMessage *entry;
2693 char *filter;
2694 char **values = NULL;
2695 char **memberuid;
2696 char *gidstr;
2697 int rc, count;
2698 struct dom_sid_buf buf;
2700 *pp_member_rids = NULL;
2701 *p_num_members = 0;
2703 filter = talloc_asprintf(mem_ctx,
2704 "(&(objectClass="LDAP_OBJ_POSIXGROUP")"
2705 "(objectClass="LDAP_OBJ_GROUPMAP")"
2706 "(sambaSID=%s))",
2707 dom_sid_str_buf(group, &buf));
2708 if (filter == NULL) {
2709 ret = NT_STATUS_NO_MEMORY;
2710 goto done;
2713 rc = smbldap_search(conn, lp_ldap_suffix(),
2714 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2715 &result);
2717 if (rc != LDAP_SUCCESS)
2718 goto done;
2720 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2722 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2724 if (count > 1) {
2725 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2726 dom_sid_str_buf(group, &buf)));
2727 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2728 goto done;
2731 if (count == 0) {
2732 ret = NT_STATUS_NO_SUCH_GROUP;
2733 goto done;
2736 entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2737 if (entry == NULL)
2738 goto done;
2740 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2741 if (!gidstr) {
2742 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2743 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2744 goto done;
2747 values = ldap_get_values(smbldap_get_ldap(conn), entry, "memberUid");
2749 if ((values != NULL) && (values[0] != NULL)) {
2751 filter = talloc_strdup(mem_ctx, "(&(objectClass="LDAP_OBJ_SAMBASAMACCOUNT")(|");
2753 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2754 char *escape_memberuid;
2756 escape_memberuid = escape_ldap_string(talloc_tos(),
2757 *memberuid);
2758 if (escape_memberuid == NULL) {
2759 ret = NT_STATUS_NO_MEMORY;
2760 goto done;
2763 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2764 TALLOC_FREE(escape_memberuid);
2765 if (filter == NULL) {
2766 ret = NT_STATUS_NO_MEMORY;
2767 goto done;
2771 filter = talloc_asprintf_append_buffer(filter, "))");
2772 if (filter == NULL) {
2773 ret = NT_STATUS_NO_MEMORY;
2774 goto done;
2777 rc = smbldap_search(conn, lp_ldap_suffix(),
2778 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2779 &result);
2781 if (rc != LDAP_SUCCESS)
2782 goto done;
2784 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2785 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2787 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2789 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2790 entry != NULL;
2791 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2793 char *sidstr;
2794 struct dom_sid sid;
2795 uint32_t rid;
2797 sidstr = smbldap_talloc_single_attribute(
2798 smbldap_get_ldap(conn), entry, "sambaSID",
2799 mem_ctx);
2800 if (!sidstr) {
2801 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2802 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2803 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2804 goto done;
2807 if (!string_to_sid(&sid, sidstr))
2808 goto done;
2810 if (!sid_check_is_in_our_sam(&sid)) {
2811 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2812 "in our domain\n"));
2813 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2814 goto done;
2817 sid_peek_rid(&sid, &rid);
2819 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2820 p_num_members)) {
2821 ret = NT_STATUS_NO_MEMORY;
2822 goto done;
2827 filter = talloc_asprintf(mem_ctx,
2828 "(&(objectClass=%s)"
2829 "(gidNumber=%s))",
2830 LDAP_OBJ_SAMBASAMACCOUNT,
2831 gidstr);
2833 rc = smbldap_search(conn, lp_ldap_suffix(),
2834 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2835 &result);
2837 if (rc != LDAP_SUCCESS)
2838 goto done;
2840 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2842 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2843 entry != NULL;
2844 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2846 uint32_t rid;
2848 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn),
2849 entry,
2850 get_global_sam_sid(),
2851 &rid)) {
2852 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2853 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2854 goto done;
2857 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2858 p_num_members)) {
2859 ret = NT_STATUS_NO_MEMORY;
2860 goto done;
2864 ret = NT_STATUS_OK;
2866 done:
2868 if (values)
2869 ldap_value_free(values);
2871 return ret;
2874 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2875 TALLOC_CTX *mem_ctx,
2876 struct samu *user,
2877 struct dom_sid **pp_sids,
2878 gid_t **pp_gids,
2879 uint32_t *p_num_groups)
2881 struct ldapsam_privates *ldap_state =
2882 (struct ldapsam_privates *)methods->private_data;
2883 struct smbldap_state *conn = ldap_state->smbldap_state;
2884 char *filter;
2885 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2886 char *escape_name;
2887 int rc, count;
2888 LDAPMessage *result = NULL;
2889 LDAPMessage *entry;
2890 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2891 uint32_t num_sids;
2892 uint32_t num_gids;
2893 char *gidstr;
2894 gid_t primary_gid = -1;
2895 int error = 0;
2897 *pp_sids = NULL;
2898 num_sids = 0;
2900 if (pdb_get_username(user) == NULL) {
2901 return NT_STATUS_INVALID_PARAMETER;
2904 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2905 if (escape_name == NULL)
2906 return NT_STATUS_NO_MEMORY;
2908 if (user->unix_pw) {
2909 primary_gid = user->unix_pw->pw_gid;
2910 } else {
2911 /* retrieve the users primary gid */
2912 filter = talloc_asprintf(mem_ctx,
2913 "(&(objectClass="LDAP_OBJ_SAMBASAMACCOUNT")(uid=%s))",
2914 escape_name);
2915 if (filter == NULL) {
2916 ret = NT_STATUS_NO_MEMORY;
2917 goto done;
2920 rc = smbldap_search(conn, lp_ldap_suffix(),
2921 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2923 if (rc != LDAP_SUCCESS)
2924 goto done;
2926 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2928 count = ldap_count_entries(priv2ld(ldap_state), result);
2930 switch (count) {
2931 case 0:
2932 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2933 ret = NT_STATUS_NO_SUCH_USER;
2934 goto done;
2935 case 1:
2936 entry = ldap_first_entry(priv2ld(ldap_state), result);
2938 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2939 if (!gidstr) {
2940 DEBUG (1, ("Unable to find the member's gid!\n"));
2941 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2942 goto done;
2944 primary_gid = smb_strtoul(gidstr,
2945 NULL,
2947 &error,
2948 SMB_STR_STANDARD);
2949 if (error != 0) {
2950 DBG_ERR("Failed to convert GID\n");
2951 goto done;
2953 break;
2954 default:
2955 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2956 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2957 goto done;
2961 filter = talloc_asprintf(mem_ctx,
2962 "(&(objectClass="LDAP_OBJ_POSIXGROUP")(|(memberUid=%s)(gidNumber=%u)))",
2963 escape_name, (unsigned int)primary_gid);
2964 if (filter == NULL) {
2965 ret = NT_STATUS_NO_MEMORY;
2966 goto done;
2969 rc = smbldap_search(conn, lp_ldap_suffix(),
2970 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2972 if (rc != LDAP_SUCCESS)
2973 goto done;
2975 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2977 num_gids = 0;
2978 *pp_gids = NULL;
2980 num_sids = 0;
2981 *pp_sids = NULL;
2983 /* We need to add the primary group as the first gid/sid */
2985 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2986 ret = NT_STATUS_NO_MEMORY;
2987 goto done;
2990 /* This sid will be replaced later */
2992 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2993 &num_sids);
2994 if (!NT_STATUS_IS_OK(ret)) {
2995 goto done;
2998 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2999 entry != NULL;
3000 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
3002 fstring str;
3003 struct dom_sid sid;
3004 gid_t gid;
3006 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3007 entry, "sambaSID",
3008 str, sizeof(str)-1))
3009 continue;
3011 if (!string_to_sid(&sid, str))
3012 goto done;
3014 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3015 entry, "gidNumber",
3016 str, sizeof(str)-1))
3017 continue;
3019 gid = smb_strtoul(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
3021 if (error != 0) {
3022 goto done;
3025 if (gid == primary_gid) {
3026 sid_copy(&(*pp_sids)[0], &sid);
3027 } else {
3028 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3029 &num_gids)) {
3030 ret = NT_STATUS_NO_MEMORY;
3031 goto done;
3033 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3034 &num_sids);
3035 if (!NT_STATUS_IS_OK(ret)) {
3036 goto done;
3041 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3042 DEBUG(3, ("primary group of [%s] not found\n",
3043 pdb_get_username(user)));
3044 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3045 goto done;
3048 *p_num_groups = num_sids;
3050 ret = NT_STATUS_OK;
3052 done:
3054 TALLOC_FREE(escape_name);
3055 return ret;
3058 /**********************************************************************
3059 * Augment a posixGroup object with a sambaGroupMapping domgroup
3060 *********************************************************************/
3062 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3063 struct ldapsam_privates *ldap_state,
3064 GROUP_MAP *map)
3066 const char *filter, *dn;
3067 LDAPMessage *msg, *entry;
3068 LDAPMod **mods;
3069 struct dom_sid_buf buf;
3070 int rc;
3072 filter = talloc_asprintf(mem_ctx,
3073 "(&(objectClass="LDAP_OBJ_POSIXGROUP")(gidNumber=%u))",
3074 (unsigned int)map->gid);
3075 if (filter == NULL) {
3076 return NT_STATUS_NO_MEMORY;
3079 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3080 get_attr_list(mem_ctx, groupmap_attr_list),
3081 &msg);
3082 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3084 if ((rc != LDAP_SUCCESS) ||
3085 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3086 msg) != 1) ||
3087 ((entry = ldap_first_entry(
3088 smbldap_get_ldap(ldap_state->smbldap_state),
3089 msg)) == NULL)) {
3090 return NT_STATUS_NO_SUCH_GROUP;
3093 dn = smbldap_talloc_dn(mem_ctx,
3094 smbldap_get_ldap(ldap_state->smbldap_state),
3095 entry);
3096 if (dn == NULL) {
3097 return NT_STATUS_NO_MEMORY;
3100 mods = NULL;
3101 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3102 LDAP_OBJ_GROUPMAP);
3103 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3104 &mods, "sambaSid",
3105 dom_sid_str_buf(&map->sid, &buf));
3106 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3107 &mods, "sambaGroupType",
3108 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3109 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3110 &mods, "displayName",
3111 map->nt_name);
3112 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3113 &mods, "description",
3114 map->comment);
3115 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3117 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3118 if (rc != LDAP_SUCCESS) {
3119 return NT_STATUS_ACCESS_DENIED;
3122 return NT_STATUS_OK;
3125 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3126 GROUP_MAP *map)
3128 struct ldapsam_privates *ldap_state =
3129 (struct ldapsam_privates *)methods->private_data;
3130 LDAPMessage *msg = NULL;
3131 LDAPMod **mods = NULL;
3132 const char *attrs[] = { NULL };
3133 char *filter;
3135 char *dn;
3136 TALLOC_CTX *mem_ctx;
3137 NTSTATUS result;
3139 struct dom_sid sid;
3140 struct dom_sid_buf buf;
3141 struct unixid id;
3143 int rc;
3145 mem_ctx = talloc_new(NULL);
3146 if (mem_ctx == NULL) {
3147 DEBUG(0, ("talloc_new failed\n"));
3148 return NT_STATUS_NO_MEMORY;
3151 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3152 dom_sid_str_buf(&map->sid, &buf));
3153 if (filter == NULL) {
3154 result = NT_STATUS_NO_MEMORY;
3155 goto done;
3158 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3159 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3160 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3162 if ((rc == LDAP_SUCCESS) &&
3163 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3164 msg) > 0)) {
3166 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3167 "group mapping entry\n",
3168 dom_sid_str_buf(&map->sid, &buf)));
3169 result = NT_STATUS_GROUP_EXISTS;
3170 goto done;
3173 switch (map->sid_name_use) {
3175 case SID_NAME_DOM_GRP:
3176 /* To map a domain group we need to have a posix group
3177 to attach to. */
3178 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3179 goto done;
3180 break;
3182 case SID_NAME_ALIAS:
3183 if (!sid_check_is_in_our_sam(&map->sid)
3184 && !sid_check_is_in_builtin(&map->sid) )
3186 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3187 dom_sid_str_buf(&map->sid, &buf)));
3188 result = NT_STATUS_INVALID_PARAMETER;
3189 goto done;
3191 break;
3193 default:
3194 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3195 sid_type_lookup(map->sid_name_use)));
3196 result = NT_STATUS_INVALID_PARAMETER;
3197 goto done;
3200 /* Domain groups have been mapped in a separate routine, we have to
3201 * create an alias now */
3203 if (map->gid == -1) {
3204 DEBUG(10, ("Refusing to map gid==-1\n"));
3205 result = NT_STATUS_INVALID_PARAMETER;
3206 goto done;
3209 id.id = map->gid;
3210 id.type = ID_TYPE_GID;
3212 if (pdb_id_to_sid(&id, &sid)) {
3213 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3214 "add\n",
3215 (unsigned int)map->gid,
3216 dom_sid_str_buf(&sid, &buf)));
3217 result = NT_STATUS_GROUP_EXISTS;
3218 goto done;
3221 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3222 * the best we can get out of LDAP. */
3224 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3225 dom_sid_str_buf(&map->sid, &buf),
3226 lp_ldap_group_suffix(talloc_tos()));
3227 if (dn == NULL) {
3228 result = NT_STATUS_NO_MEMORY;
3229 goto done;
3232 mods = NULL;
3234 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3235 &mods, "objectClass", LDAP_OBJ_SID_ENTRY);
3236 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3237 &mods, "objectClass", LDAP_OBJ_GROUPMAP);
3238 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3239 &mods, "sambaSid",
3240 dom_sid_str_buf(&map->sid, &buf));
3241 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3242 &mods, "sambaGroupType",
3243 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3244 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3245 &mods, "displayName",
3246 map->nt_name);
3247 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3248 &mods, "description",
3249 map->comment);
3250 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3251 &mods, "gidNumber",
3252 talloc_asprintf(mem_ctx, "%u",
3253 (unsigned int)map->gid));
3254 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3256 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3258 result = (rc == LDAP_SUCCESS) ?
3259 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3261 done:
3262 TALLOC_FREE(mem_ctx);
3263 return result;
3266 /**********************************************************************
3267 * Update a group mapping entry. We're quite strict about what can be changed:
3268 * Only the description and displayname may be changed. It simply does not
3269 * make any sense to change the SID, gid or the type in a mapping.
3270 *********************************************************************/
3272 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3273 GROUP_MAP *map)
3275 struct ldapsam_privates *ldap_state =
3276 (struct ldapsam_privates *)methods->private_data;
3277 int rc;
3278 const char *filter, *dn;
3279 LDAPMessage *msg = NULL;
3280 LDAPMessage *entry = NULL;
3281 LDAPMod **mods = NULL;
3282 TALLOC_CTX *mem_ctx;
3283 NTSTATUS result;
3284 struct dom_sid_buf buf;
3286 mem_ctx = talloc_new(NULL);
3287 if (mem_ctx == NULL) {
3288 DEBUG(0, ("talloc_new failed\n"));
3289 return NT_STATUS_NO_MEMORY;
3292 /* Make 100% sure that sid, gid and type are not changed by looking up
3293 * exactly the values we're given in LDAP. */
3295 filter = talloc_asprintf(mem_ctx, "(&(objectClass="LDAP_OBJ_GROUPMAP")"
3296 "(sambaSid=%s)(gidNumber=%u)"
3297 "(sambaGroupType=%d))",
3298 dom_sid_str_buf(&map->sid, &buf),
3299 (unsigned int)map->gid, map->sid_name_use);
3300 if (filter == NULL) {
3301 result = NT_STATUS_NO_MEMORY;
3302 goto done;
3305 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3306 get_attr_list(mem_ctx, groupmap_attr_list),
3307 &msg);
3308 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3310 if ((rc != LDAP_SUCCESS) ||
3311 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3312 msg) != 1) ||
3313 ((entry = ldap_first_entry(
3314 smbldap_get_ldap(ldap_state->smbldap_state),
3315 msg)) == NULL)) {
3316 result = NT_STATUS_NO_SUCH_GROUP;
3317 goto done;
3320 dn = smbldap_talloc_dn(
3321 mem_ctx, smbldap_get_ldap(ldap_state->smbldap_state), entry);
3323 if (dn == NULL) {
3324 result = NT_STATUS_NO_MEMORY;
3325 goto done;
3328 mods = NULL;
3329 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3330 &mods, "displayName", map->nt_name);
3331 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3332 &mods, "description", map->comment);
3333 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3335 if (mods == NULL) {
3336 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3337 "nothing to do\n"));
3338 result = NT_STATUS_OK;
3339 goto done;
3342 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3344 if (rc != LDAP_SUCCESS) {
3345 result = NT_STATUS_ACCESS_DENIED;
3346 goto done;
3349 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3350 "group %lu in LDAP\n", (unsigned long)map->gid));
3352 result = NT_STATUS_OK;
3354 done:
3355 TALLOC_FREE(mem_ctx);
3356 return result;
3359 /**********************************************************************
3360 *********************************************************************/
3362 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3363 struct dom_sid sid)
3365 struct ldapsam_privates *priv =
3366 (struct ldapsam_privates *)methods->private_data;
3367 LDAPMessage *msg, *entry;
3368 int rc;
3369 NTSTATUS result;
3370 TALLOC_CTX *mem_ctx;
3371 char *filter;
3372 struct dom_sid_buf buf;
3374 mem_ctx = talloc_new(NULL);
3375 if (mem_ctx == NULL) {
3376 DEBUG(0, ("talloc_new failed\n"));
3377 return NT_STATUS_NO_MEMORY;
3380 filter = talloc_asprintf(mem_ctx, "(&(objectClass="LDAP_OBJ_GROUPMAP")("LDAP_ATTRIBUTE_SID"=%s))",
3381 dom_sid_str_buf(&sid, &buf));
3382 if (filter == NULL) {
3383 result = NT_STATUS_NO_MEMORY;
3384 goto done;
3386 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3387 get_attr_list(mem_ctx, groupmap_attr_list),
3388 &msg);
3389 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3391 if ((rc != LDAP_SUCCESS) ||
3392 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3393 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3394 result = NT_STATUS_NO_SUCH_GROUP;
3395 goto done;
3398 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3399 get_attr_list(mem_ctx,
3400 groupmap_attr_list_to_delete));
3402 if ((rc == LDAP_NAMING_VIOLATION) ||
3403 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3404 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3405 const char *attrs[] = { "sambaGroupType", "description",
3406 "displayName", "sambaSIDList",
3407 NULL };
3409 /* Second try. Don't delete the sambaSID attribute, this is
3410 for "old" entries that are tacked on a winbind
3411 sambaIdmapEntry. */
3413 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3414 LDAP_OBJ_GROUPMAP, attrs);
3417 if ((rc == LDAP_NAMING_VIOLATION) ||
3418 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3419 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3420 const char *attrs[] = { "sambaGroupType", "description",
3421 "displayName", "sambaSIDList",
3422 "gidNumber", NULL };
3424 /* Third try. This is a post-3.0.21 alias (containing only
3425 * sambaSidEntry and sambaGroupMapping classes), we also have
3426 * to delete the gidNumber attribute, only the sambaSidEntry
3427 * remains */
3429 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3430 LDAP_OBJ_GROUPMAP, attrs);
3433 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3435 done:
3436 TALLOC_FREE(mem_ctx);
3437 return result;
3440 /**********************************************************************
3441 *********************************************************************/
3443 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3444 bool update)
3446 struct ldapsam_privates *ldap_state =
3447 (struct ldapsam_privates *)my_methods->private_data;
3448 const char *filter = NULL;
3449 int rc;
3450 const char **attr_list;
3452 filter = "(objectclass="LDAP_OBJ_GROUPMAP")";
3453 attr_list = get_attr_list( NULL, groupmap_attr_list );
3454 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3455 LDAP_SCOPE_SUBTREE, filter,
3456 attr_list, 0, &ldap_state->result);
3457 TALLOC_FREE(attr_list);
3459 if (rc != LDAP_SUCCESS) {
3460 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3461 ldap_err2string(rc)));
3462 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3463 lp_ldap_suffix(), filter));
3464 ldap_msgfree(ldap_state->result);
3465 ldap_state->result = NULL;
3466 return NT_STATUS_UNSUCCESSFUL;
3469 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3470 ldap_count_entries(
3471 smbldap_get_ldap(ldap_state->smbldap_state),
3472 ldap_state->result)));
3474 ldap_state->entry =
3475 ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3476 ldap_state->result);
3477 ldap_state->index = 0;
3479 return NT_STATUS_OK;
3482 /**********************************************************************
3483 *********************************************************************/
3485 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3487 ldapsam_endsampwent(my_methods);
3490 /**********************************************************************
3491 *********************************************************************/
3493 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3494 GROUP_MAP *map)
3496 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3497 struct ldapsam_privates *ldap_state =
3498 (struct ldapsam_privates *)my_methods->private_data;
3499 bool bret = False;
3501 while (!bret) {
3502 if (!ldap_state->entry)
3503 return ret;
3505 ldap_state->index++;
3506 bret = init_group_from_ldap(ldap_state, map,
3507 ldap_state->entry);
3509 ldap_state->entry = ldap_next_entry(
3510 smbldap_get_ldap(ldap_state->smbldap_state),
3511 ldap_state->entry);
3514 return NT_STATUS_OK;
3517 /**********************************************************************
3518 *********************************************************************/
3520 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3521 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3522 GROUP_MAP ***pp_rmap,
3523 size_t *p_num_entries,
3524 bool unix_only)
3526 GROUP_MAP *map = NULL;
3527 size_t entries = 0;
3529 *p_num_entries = 0;
3530 *pp_rmap = NULL;
3532 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3533 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3534 "passdb\n"));
3535 return NT_STATUS_ACCESS_DENIED;
3538 while (true) {
3540 map = talloc_zero(NULL, GROUP_MAP);
3541 if (!map) {
3542 return NT_STATUS_NO_MEMORY;
3545 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3546 TALLOC_FREE(map);
3547 break;
3550 if (sid_name_use != SID_NAME_UNKNOWN &&
3551 sid_name_use != map->sid_name_use) {
3552 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3553 "not of the requested type\n",
3554 map->nt_name));
3555 continue;
3557 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3558 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3559 "non mapped\n", map->nt_name));
3560 continue;
3563 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3564 GROUP_MAP *, entries + 1);
3565 if (!(*pp_rmap)) {
3566 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3567 "enlarge group map!\n"));
3568 return NT_STATUS_UNSUCCESSFUL;
3571 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3573 entries += 1;
3576 ldapsam_endsamgrent(methods);
3578 *p_num_entries = entries;
3580 return NT_STATUS_OK;
3583 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3584 const struct dom_sid *alias,
3585 const struct dom_sid *member,
3586 int modop)
3588 struct ldapsam_privates *ldap_state =
3589 (struct ldapsam_privates *)methods->private_data;
3590 char *dn = NULL;
3591 LDAPMessage *result = NULL;
3592 LDAPMessage *entry = NULL;
3593 int count;
3594 LDAPMod **mods = NULL;
3595 int rc;
3596 enum lsa_SidType type = SID_NAME_USE_NONE;
3597 struct dom_sid_buf tmp;
3599 char *filter = NULL;
3601 if (sid_check_is_in_builtin(alias)) {
3602 type = SID_NAME_ALIAS;
3605 if (sid_check_is_in_our_sam(alias)) {
3606 type = SID_NAME_ALIAS;
3609 if (type == SID_NAME_USE_NONE) {
3610 struct dom_sid_buf buf;
3611 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3612 dom_sid_str_buf(alias, &buf)));
3613 return NT_STATUS_NO_SUCH_ALIAS;
3616 if (asprintf(&filter,
3617 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3618 LDAP_OBJ_GROUPMAP,
3619 dom_sid_str_buf(alias, &tmp),
3620 type) < 0) {
3621 return NT_STATUS_NO_MEMORY;
3624 if (ldapsam_search_one_group(ldap_state, filter,
3625 &result) != LDAP_SUCCESS) {
3626 SAFE_FREE(filter);
3627 return NT_STATUS_NO_SUCH_ALIAS;
3630 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3631 result);
3633 if (count < 1) {
3634 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3635 ldap_msgfree(result);
3636 SAFE_FREE(filter);
3637 return NT_STATUS_NO_SUCH_ALIAS;
3640 if (count > 1) {
3641 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3642 "filter %s: count=%d\n", filter, count));
3643 ldap_msgfree(result);
3644 SAFE_FREE(filter);
3645 return NT_STATUS_NO_SUCH_ALIAS;
3648 SAFE_FREE(filter);
3650 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3651 result);
3653 if (!entry) {
3654 ldap_msgfree(result);
3655 return NT_STATUS_UNSUCCESSFUL;
3658 dn = smbldap_talloc_dn(talloc_tos(),
3659 smbldap_get_ldap(ldap_state->smbldap_state),
3660 entry);
3661 if (!dn) {
3662 ldap_msgfree(result);
3663 return NT_STATUS_UNSUCCESSFUL;
3666 smbldap_set_mod(&mods, modop,
3667 get_attr_key2string(groupmap_attr_list,
3668 LDAP_ATTR_SID_LIST),
3669 dom_sid_str_buf(member, &tmp));
3671 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3673 ldap_mods_free(mods, True);
3674 ldap_msgfree(result);
3675 TALLOC_FREE(dn);
3677 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3678 return NT_STATUS_MEMBER_IN_ALIAS;
3681 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3682 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3685 if (rc != LDAP_SUCCESS) {
3686 return NT_STATUS_UNSUCCESSFUL;
3689 return NT_STATUS_OK;
3692 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3693 const struct dom_sid *alias,
3694 const struct dom_sid *member)
3696 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3699 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3700 const struct dom_sid *alias,
3701 const struct dom_sid *member)
3703 return ldapsam_modify_aliasmem(methods, alias, member,
3704 LDAP_MOD_DELETE);
3707 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3708 const struct dom_sid *alias,
3709 TALLOC_CTX *mem_ctx,
3710 struct dom_sid **pp_members,
3711 size_t *p_num_members)
3713 struct ldapsam_privates *ldap_state =
3714 (struct ldapsam_privates *)methods->private_data;
3715 LDAPMessage *result = NULL;
3716 LDAPMessage *entry = NULL;
3717 int count;
3718 char **values = NULL;
3719 int i;
3720 char *filter = NULL;
3721 uint32_t num_members = 0;
3722 enum lsa_SidType type = SID_NAME_USE_NONE;
3723 struct dom_sid_buf tmp;
3725 *pp_members = NULL;
3726 *p_num_members = 0;
3728 if (sid_check_is_in_builtin(alias)) {
3729 type = SID_NAME_ALIAS;
3732 if (sid_check_is_in_our_sam(alias)) {
3733 type = SID_NAME_ALIAS;
3736 if (type == SID_NAME_USE_NONE) {
3737 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3738 dom_sid_str_buf(alias, &tmp)));
3739 return NT_STATUS_NO_SUCH_ALIAS;
3742 if (asprintf(&filter,
3743 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3744 LDAP_OBJ_GROUPMAP,
3745 dom_sid_str_buf(alias, &tmp),
3746 type) < 0) {
3747 return NT_STATUS_NO_MEMORY;
3750 if (ldapsam_search_one_group(ldap_state, filter,
3751 &result) != LDAP_SUCCESS) {
3752 SAFE_FREE(filter);
3753 return NT_STATUS_NO_SUCH_ALIAS;
3756 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3757 result);
3759 if (count < 1) {
3760 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3761 ldap_msgfree(result);
3762 SAFE_FREE(filter);
3763 return NT_STATUS_NO_SUCH_ALIAS;
3766 if (count > 1) {
3767 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3768 "filter %s: count=%d\n", filter, count));
3769 ldap_msgfree(result);
3770 SAFE_FREE(filter);
3771 return NT_STATUS_NO_SUCH_ALIAS;
3774 SAFE_FREE(filter);
3776 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3777 result);
3779 if (!entry) {
3780 ldap_msgfree(result);
3781 return NT_STATUS_UNSUCCESSFUL;
3784 values = ldap_get_values(smbldap_get_ldap(ldap_state->smbldap_state),
3785 entry,
3786 get_attr_key2string(groupmap_attr_list,
3787 LDAP_ATTR_SID_LIST));
3789 if (values == NULL) {
3790 ldap_msgfree(result);
3791 return NT_STATUS_OK;
3794 count = ldap_count_values(values);
3796 for (i=0; i<count; i++) {
3797 struct dom_sid member;
3798 NTSTATUS status;
3800 if (!string_to_sid(&member, values[i]))
3801 continue;
3803 status = add_sid_to_array(mem_ctx, &member, pp_members,
3804 &num_members);
3805 if (!NT_STATUS_IS_OK(status)) {
3806 ldap_value_free(values);
3807 ldap_msgfree(result);
3808 return status;
3812 *p_num_members = num_members;
3813 ldap_value_free(values);
3814 ldap_msgfree(result);
3816 return NT_STATUS_OK;
3819 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3820 TALLOC_CTX *mem_ctx,
3821 const struct dom_sid *domain_sid,
3822 const struct dom_sid *members,
3823 size_t num_members,
3824 uint32_t **pp_alias_rids,
3825 size_t *p_num_alias_rids)
3827 struct ldapsam_privates *ldap_state =
3828 (struct ldapsam_privates *)methods->private_data;
3829 LDAP *ldap_struct;
3831 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3833 LDAPMessage *result = NULL;
3834 LDAPMessage *entry = NULL;
3835 int i;
3836 int rc;
3837 char *filter;
3838 enum lsa_SidType type = SID_NAME_USE_NONE;
3839 bool is_builtin = false;
3840 bool sid_added = false;
3842 *pp_alias_rids = NULL;
3843 *p_num_alias_rids = 0;
3845 if (sid_check_is_builtin(domain_sid)) {
3846 is_builtin = true;
3847 type = SID_NAME_ALIAS;
3850 if (sid_check_is_our_sam(domain_sid)) {
3851 type = SID_NAME_ALIAS;
3854 if (type == SID_NAME_USE_NONE) {
3855 struct dom_sid_buf buf;
3856 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3857 dom_sid_str_buf(domain_sid, &buf)));
3858 return NT_STATUS_UNSUCCESSFUL;
3861 if (num_members == 0) {
3862 return NT_STATUS_OK;
3865 filter = talloc_asprintf(mem_ctx,
3866 "(&(objectclass="LDAP_OBJ_GROUPMAP")(sambaGroupType=%d)(|",
3867 type);
3869 for (i=0; i<num_members; i++) {
3870 struct dom_sid_buf buf;
3871 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3872 filter,
3873 dom_sid_str_buf(&members[i], &buf));
3876 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3878 if (filter == NULL) {
3879 return NT_STATUS_NO_MEMORY;
3882 if (is_builtin &&
3883 ldap_state->search_cache.filter &&
3884 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3885 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3886 result = ldap_state->search_cache.result;
3887 ldap_state->search_cache.result = NULL;
3888 } else {
3889 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3890 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3891 if (rc != LDAP_SUCCESS) {
3892 return NT_STATUS_UNSUCCESSFUL;
3894 smbldap_talloc_autofree_ldapmsg(filter, result);
3897 ldap_struct = smbldap_get_ldap(ldap_state->smbldap_state);
3899 for (entry = ldap_first_entry(ldap_struct, result);
3900 entry != NULL;
3901 entry = ldap_next_entry(ldap_struct, entry))
3903 fstring sid_str;
3904 struct dom_sid sid;
3905 uint32_t rid;
3907 if (!smbldap_get_single_attribute(ldap_struct, entry,
3908 LDAP_ATTRIBUTE_SID,
3909 sid_str,
3910 sizeof(sid_str)-1))
3911 continue;
3913 if (!string_to_sid(&sid, sid_str))
3914 continue;
3916 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3917 continue;
3919 sid_added = true;
3921 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3922 p_num_alias_rids)) {
3923 return NT_STATUS_NO_MEMORY;
3927 if (!is_builtin && !sid_added) {
3928 TALLOC_FREE(ldap_state->search_cache.filter);
3930 * Note: result is a talloc child of filter because of the
3931 * smbldap_talloc_autofree_ldapmsg() usage
3933 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3934 ldap_state->search_cache.result = result;
3937 return NT_STATUS_OK;
3940 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3941 enum pdb_policy_type type,
3942 uint32_t value)
3944 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3945 int rc;
3946 LDAPMod **mods = NULL;
3947 fstring value_string;
3948 const char *policy_attr = NULL;
3950 struct ldapsam_privates *ldap_state =
3951 (struct ldapsam_privates *)methods->private_data;
3953 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3955 if (!ldap_state->domain_dn) {
3956 return NT_STATUS_INVALID_PARAMETER;
3959 policy_attr = get_account_policy_attr(type);
3960 if (policy_attr == NULL) {
3961 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3962 "policy\n"));
3963 return ntstatus;
3966 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3968 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3970 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3971 mods);
3973 ldap_mods_free(mods, True);
3975 if (rc != LDAP_SUCCESS) {
3976 return ntstatus;
3979 if (!cache_account_policy_set(type, value)) {
3980 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3981 "update local tdb cache\n"));
3982 return ntstatus;
3985 return NT_STATUS_OK;
3988 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3989 enum pdb_policy_type type,
3990 uint32_t value)
3992 return ldapsam_set_account_policy_in_ldap(methods, type,
3993 value);
3996 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3997 enum pdb_policy_type type,
3998 uint32_t *value)
4000 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4001 LDAPMessage *result = NULL;
4002 LDAPMessage *entry = NULL;
4003 int count;
4004 int rc;
4005 char **vals = NULL;
4006 const char *filter;
4007 const char *policy_attr = NULL;
4009 struct ldapsam_privates *ldap_state =
4010 (struct ldapsam_privates *)methods->private_data;
4012 const char *attrs[2];
4014 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4016 if (!ldap_state->domain_dn) {
4017 return NT_STATUS_INVALID_PARAMETER;
4020 policy_attr = get_account_policy_attr(type);
4021 if (!policy_attr) {
4022 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4023 "policy index: %d\n", type));
4024 return ntstatus;
4027 attrs[0] = policy_attr;
4028 attrs[1] = NULL;
4030 filter = "(objectClass="LDAP_OBJ_DOMINFO")";
4031 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
4032 LDAP_SCOPE_BASE, filter, attrs, 0,
4033 &result);
4034 if (rc != LDAP_SUCCESS) {
4035 return ntstatus;
4038 count = ldap_count_entries(priv2ld(ldap_state), result);
4039 if (count < 1) {
4040 goto out;
4043 entry = ldap_first_entry(priv2ld(ldap_state), result);
4044 if (entry == NULL) {
4045 goto out;
4048 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4049 if (vals == NULL) {
4050 goto out;
4053 *value = (uint32_t)atol(vals[0]);
4055 ntstatus = NT_STATUS_OK;
4057 out:
4058 if (vals)
4059 ldap_value_free(vals);
4060 ldap_msgfree(result);
4062 return ntstatus;
4065 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4067 - if user hasn't decided to use account policies inside LDAP just reuse the
4068 old tdb values
4070 - if there is a valid cache entry, return that
4071 - if there is an LDAP entry, update cache and return
4072 - otherwise set to default, update cache and return
4074 Guenther
4076 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4077 enum pdb_policy_type type,
4078 uint32_t *value)
4080 NTSTATUS ntstatus;
4082 if (cache_account_policy_get(type, value)) {
4083 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4084 "cache\n"));
4085 return NT_STATUS_OK;
4088 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4089 value);
4090 if (NT_STATUS_IS_OK(ntstatus)) {
4091 goto update_cache;
4094 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4095 "ldap\n"));
4097 #if 0
4098 /* should we automagically migrate old tdb value here ? */
4099 if (account_policy_get(type, value))
4100 goto update_ldap;
4102 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4103 "default\n", type));
4104 #endif
4106 if (!account_policy_get_default(type, value)) {
4107 return ntstatus;
4110 /* update_ldap: */
4112 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4113 if (!NT_STATUS_IS_OK(ntstatus)) {
4114 return ntstatus;
4117 update_cache:
4119 if (!cache_account_policy_set(type, *value)) {
4120 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4121 "tdb as a cache\n"));
4122 return NT_STATUS_UNSUCCESSFUL;
4125 return NT_STATUS_OK;
4128 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4129 const struct dom_sid *domain_sid,
4130 int num_rids,
4131 uint32_t *rids,
4132 const char **names,
4133 enum lsa_SidType *attrs)
4135 struct ldapsam_privates *ldap_state =
4136 (struct ldapsam_privates *)methods->private_data;
4137 LDAPMessage *msg = NULL;
4138 LDAPMessage *entry;
4139 char *allsids = NULL;
4140 size_t i, num_mapped;
4141 int rc;
4142 NTSTATUS result = NT_STATUS_NO_MEMORY;
4143 TALLOC_CTX *mem_ctx;
4144 LDAP *ld;
4145 bool is_builtin;
4147 mem_ctx = talloc_new(NULL);
4148 if (mem_ctx == NULL) {
4149 DEBUG(0, ("talloc_new failed\n"));
4150 goto done;
4153 if (!sid_check_is_builtin(domain_sid) &&
4154 !sid_check_is_our_sam(domain_sid)) {
4155 result = NT_STATUS_INVALID_PARAMETER;
4156 goto done;
4159 if (num_rids == 0) {
4160 result = NT_STATUS_NONE_MAPPED;
4161 goto done;
4164 for (i=0; i<num_rids; i++)
4165 attrs[i] = SID_NAME_UNKNOWN;
4167 allsids = talloc_strdup(mem_ctx, "");
4168 if (allsids == NULL) {
4169 goto done;
4172 for (i=0; i<num_rids; i++) {
4173 struct dom_sid sid;
4174 struct dom_sid_buf buf;
4175 sid_compose(&sid, domain_sid, rids[i]);
4176 allsids = talloc_asprintf_append_buffer(
4177 allsids,
4178 "(sambaSid=%s)",
4179 dom_sid_str_buf(&sid, &buf));
4180 if (allsids == NULL) {
4181 goto done;
4185 /* First look for users */
4188 char *filter;
4189 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4191 filter = talloc_asprintf(
4192 mem_ctx, ("(&(objectClass="LDAP_OBJ_SAMBASAMACCOUNT")(|%s))"),
4193 allsids);
4195 if (filter == NULL) {
4196 goto done;
4199 rc = smbldap_search(ldap_state->smbldap_state,
4200 lp_ldap_user_suffix(talloc_tos()),
4201 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4202 &msg);
4203 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4206 if (rc != LDAP_SUCCESS)
4207 goto done;
4209 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4210 num_mapped = 0;
4212 for (entry = ldap_first_entry(ld, msg);
4213 entry != NULL;
4214 entry = ldap_next_entry(ld, entry)) {
4215 uint32_t rid;
4216 int rid_index;
4217 const char *name;
4219 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4220 &rid)) {
4221 DEBUG(2, ("Could not find sid from ldap entry\n"));
4222 continue;
4225 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4226 names);
4227 if (name == NULL) {
4228 DEBUG(2, ("Could not retrieve uid attribute\n"));
4229 continue;
4232 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4233 if (rid == rids[rid_index])
4234 break;
4237 if (rid_index == num_rids) {
4238 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4239 continue;
4242 attrs[rid_index] = SID_NAME_USER;
4243 names[rid_index] = name;
4244 num_mapped += 1;
4247 if (num_mapped == num_rids) {
4248 /* No need to look for groups anymore -- we're done */
4249 result = NT_STATUS_OK;
4250 goto done;
4253 /* Same game for groups */
4256 char *filter;
4257 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4258 "sambaGroupType", NULL };
4260 filter = talloc_asprintf(
4261 mem_ctx, "(&(objectClass="LDAP_OBJ_GROUPMAP")(|%s))",
4262 allsids);
4263 if (filter == NULL) {
4264 goto done;
4267 rc = smbldap_search(ldap_state->smbldap_state,
4268 lp_ldap_suffix(),
4269 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4270 &msg);
4271 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4274 if (rc != LDAP_SUCCESS)
4275 goto done;
4277 /* ldap_struct might have changed due to a reconnect */
4279 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4281 /* For consistency checks, we already checked we're only domain or builtin */
4283 is_builtin = sid_check_is_builtin(domain_sid);
4285 for (entry = ldap_first_entry(ld, msg);
4286 entry != NULL;
4287 entry = ldap_next_entry(ld, entry))
4289 uint32_t rid;
4290 int rid_index;
4291 const char *attr;
4292 enum lsa_SidType type;
4293 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4295 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4296 mem_ctx);
4297 if (attr == NULL) {
4298 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4299 dn));
4300 continue;
4303 type = (enum lsa_SidType)atol(attr);
4305 /* Consistency checks */
4306 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4307 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4308 (type != SID_NAME_DOM_GRP)))) {
4309 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4312 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4313 &rid)) {
4314 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4315 continue;
4318 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4320 if (attr == NULL) {
4321 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4322 dn));
4323 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4326 if (attr == NULL) {
4327 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4328 dn));
4329 continue;
4332 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4333 if (rid == rids[rid_index])
4334 break;
4337 if (rid_index == num_rids) {
4338 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4339 continue;
4342 attrs[rid_index] = type;
4343 names[rid_index] = attr;
4344 num_mapped += 1;
4347 result = NT_STATUS_NONE_MAPPED;
4349 if (num_mapped > 0)
4350 result = (num_mapped == num_rids) ?
4351 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4352 done:
4353 TALLOC_FREE(mem_ctx);
4354 return result;
4357 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4359 char *filter = NULL;
4360 char *escaped = NULL;
4361 char *result = NULL;
4363 if (asprintf(&filter, "(&%s(objectclass=%s))",
4364 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4365 goto done;
4368 escaped = escape_ldap_string(talloc_tos(), username);
4369 if (escaped == NULL) goto done;
4371 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4373 done:
4374 SAFE_FREE(filter);
4375 TALLOC_FREE(escaped);
4377 return result;
4380 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4382 int i, num = 0;
4383 va_list ap;
4384 const char **result;
4386 va_start(ap, mem_ctx);
4387 while (va_arg(ap, const char *) != NULL)
4388 num += 1;
4389 va_end(ap);
4391 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4392 return NULL;
4395 va_start(ap, mem_ctx);
4396 for (i=0; i<num; i++) {
4397 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4398 if (result[i] == NULL) {
4399 talloc_free(result);
4400 va_end(ap);
4401 return NULL;
4404 va_end(ap);
4406 result[num] = NULL;
4407 return result;
4410 struct ldap_search_state {
4411 struct smbldap_state *connection;
4413 uint32_t acct_flags;
4414 uint16_t group_type;
4416 const char *base;
4417 int scope;
4418 const char *filter;
4419 const char **attrs;
4420 int attrsonly;
4421 void *pagedresults_cookie;
4423 LDAPMessage *entries, *current_entry;
4424 bool (*ldap2displayentry)(struct ldap_search_state *state,
4425 TALLOC_CTX *mem_ctx,
4426 LDAP *ld, LDAPMessage *entry,
4427 struct samr_displayentry *result);
4430 static bool ldapsam_search_firstpage(struct pdb_search *search)
4432 struct ldap_search_state *state =
4433 (struct ldap_search_state *)search->private_data;
4434 LDAP *ld;
4435 int rc = LDAP_OPERATIONS_ERROR;
4437 state->entries = NULL;
4439 if (smbldap_get_paged_results(state->connection)) {
4440 rc = smbldap_search_paged(state->connection, state->base,
4441 state->scope, state->filter,
4442 state->attrs, state->attrsonly,
4443 lp_ldap_page_size(), &state->entries,
4444 &state->pagedresults_cookie);
4447 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4449 if (state->entries != NULL) {
4450 /* Left over from unsuccessful paged attempt */
4451 ldap_msgfree(state->entries);
4452 state->entries = NULL;
4455 rc = smbldap_search(state->connection, state->base,
4456 state->scope, state->filter, state->attrs,
4457 state->attrsonly, &state->entries);
4459 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4460 return False;
4462 /* Ok, the server was lying. It told us it could do paged
4463 * searches when it could not. */
4464 smbldap_set_paged_results(state->connection, false);
4467 ld = smbldap_get_ldap(state->connection);
4468 if ( ld == NULL) {
4469 DEBUG(5, ("Don't have an LDAP connection right after a "
4470 "search\n"));
4471 return False;
4473 state->current_entry = ldap_first_entry(ld, state->entries);
4475 return True;
4478 static bool ldapsam_search_nextpage(struct pdb_search *search)
4480 struct ldap_search_state *state =
4481 (struct ldap_search_state *)search->private_data;
4482 int rc;
4484 if (!smbldap_get_paged_results(state->connection)) {
4485 /* There is no next page when there are no paged results */
4486 return False;
4489 rc = smbldap_search_paged(state->connection, state->base,
4490 state->scope, state->filter, state->attrs,
4491 state->attrsonly, lp_ldap_page_size(),
4492 &state->entries,
4493 &state->pagedresults_cookie);
4495 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4496 return False;
4498 state->current_entry = ldap_first_entry(
4499 smbldap_get_ldap(state->connection), state->entries);
4501 if (state->current_entry == NULL) {
4502 ldap_msgfree(state->entries);
4503 state->entries = NULL;
4504 return false;
4507 return True;
4510 static bool ldapsam_search_next_entry(struct pdb_search *search,
4511 struct samr_displayentry *entry)
4513 struct ldap_search_state *state =
4514 (struct ldap_search_state *)search->private_data;
4515 bool result;
4517 retry:
4518 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4519 return False;
4521 if ((state->entries == NULL) &&
4522 !ldapsam_search_nextpage(search))
4523 return False;
4525 if (state->current_entry == NULL) {
4526 return false;
4529 result = state->ldap2displayentry(state, search,
4530 smbldap_get_ldap(state->connection),
4531 state->current_entry, entry);
4533 if (!result) {
4534 char *dn;
4535 dn = ldap_get_dn(smbldap_get_ldap(state->connection),
4536 state->current_entry);
4537 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4538 if (dn != NULL) ldap_memfree(dn);
4541 state->current_entry = ldap_next_entry(
4542 smbldap_get_ldap(state->connection), state->current_entry);
4544 if (state->current_entry == NULL) {
4545 ldap_msgfree(state->entries);
4546 state->entries = NULL;
4549 if (!result) goto retry;
4551 return True;
4554 static void ldapsam_search_end(struct pdb_search *search)
4556 struct ldap_search_state *state =
4557 (struct ldap_search_state *)search->private_data;
4558 int rc;
4560 if (state->pagedresults_cookie == NULL)
4561 return;
4563 if (state->entries != NULL)
4564 ldap_msgfree(state->entries);
4566 state->entries = NULL;
4567 state->current_entry = NULL;
4569 if (!smbldap_get_paged_results(state->connection)) {
4570 return;
4573 /* Tell the LDAP server we're not interested in the rest anymore. */
4575 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4576 state->filter, state->attrs,
4577 state->attrsonly, 0, &state->entries,
4578 &state->pagedresults_cookie);
4580 if (rc != LDAP_SUCCESS)
4581 DEBUG(5, ("Could not end search properly\n"));
4583 return;
4586 static bool ldapuser2displayentry(struct ldap_search_state *state,
4587 TALLOC_CTX *mem_ctx,
4588 LDAP *ld, LDAPMessage *entry,
4589 struct samr_displayentry *result)
4591 char **vals;
4592 size_t converted_size;
4593 struct dom_sid sid;
4594 uint32_t acct_flags;
4596 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4597 if ((vals == NULL) || (vals[0] == NULL)) {
4598 acct_flags = ACB_NORMAL;
4599 } else {
4600 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4601 ldap_value_free(vals);
4604 if ((state->acct_flags != 0) &&
4605 ((state->acct_flags & acct_flags) == 0))
4606 return False;
4608 result->acct_flags = acct_flags;
4609 result->account_name = "";
4610 result->fullname = "";
4611 result->description = "";
4613 vals = ldap_get_values(ld, entry, "uid");
4614 if ((vals == NULL) || (vals[0] == NULL)) {
4615 DEBUG(5, ("\"uid\" not found\n"));
4616 return False;
4618 if (!pull_utf8_talloc(mem_ctx,
4619 discard_const_p(char *, &result->account_name),
4620 vals[0], &converted_size))
4622 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4623 strerror(errno)));
4626 ldap_value_free(vals);
4628 vals = ldap_get_values(ld, entry, "displayName");
4629 if ((vals == NULL) || (vals[0] == NULL))
4630 DEBUG(8, ("\"displayName\" not found\n"));
4631 else if (!pull_utf8_talloc(mem_ctx,
4632 discard_const_p(char *, &result->fullname),
4633 vals[0], &converted_size))
4635 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4636 strerror(errno)));
4639 ldap_value_free(vals);
4641 vals = ldap_get_values(ld, entry, "description");
4642 if ((vals == NULL) || (vals[0] == NULL))
4643 DEBUG(8, ("\"description\" not found\n"));
4644 else if (!pull_utf8_talloc(mem_ctx,
4645 discard_const_p(char *, &result->description),
4646 vals[0], &converted_size))
4648 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4649 strerror(errno)));
4652 ldap_value_free(vals);
4654 if ((result->account_name == NULL) ||
4655 (result->fullname == NULL) ||
4656 (result->description == NULL)) {
4657 DEBUG(0, ("talloc failed\n"));
4658 return False;
4661 vals = ldap_get_values(ld, entry, "sambaSid");
4662 if ((vals == NULL) || (vals[0] == NULL)) {
4663 DEBUG(0, ("\"objectSid\" not found\n"));
4664 return False;
4667 if (!string_to_sid(&sid, vals[0])) {
4668 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4669 ldap_value_free(vals);
4670 return False;
4672 ldap_value_free(vals);
4674 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4675 struct dom_sid_buf buf;
4676 DEBUG(0, ("sid %s does not belong to our domain\n",
4677 dom_sid_str_buf(&sid, &buf)));
4678 return False;
4681 return True;
4685 static bool ldapsam_search_users(struct pdb_methods *methods,
4686 struct pdb_search *search,
4687 uint32_t acct_flags)
4689 struct ldapsam_privates *ldap_state =
4690 (struct ldapsam_privates *)methods->private_data;
4691 struct ldap_search_state *state;
4693 state = talloc(search, struct ldap_search_state);
4694 if (state == NULL) {
4695 DEBUG(0, ("talloc failed\n"));
4696 return False;
4699 state->connection = ldap_state->smbldap_state;
4701 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4702 state->base = lp_ldap_user_suffix(talloc_tos());
4703 else if ((acct_flags != 0) &&
4704 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4705 state->base = lp_ldap_machine_suffix(talloc_tos());
4706 else
4707 state->base = lp_ldap_suffix();
4709 state->acct_flags = acct_flags;
4710 state->base = talloc_strdup(search, state->base);
4711 state->scope = LDAP_SCOPE_SUBTREE;
4712 state->filter = get_ldap_filter(search, "*");
4713 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4714 "displayName", "description",
4715 "sambaAcctFlags", NULL);
4716 state->attrsonly = 0;
4717 state->pagedresults_cookie = NULL;
4718 state->entries = NULL;
4719 state->ldap2displayentry = ldapuser2displayentry;
4721 if ((state->filter == NULL) || (state->attrs == NULL)) {
4722 DEBUG(0, ("talloc failed\n"));
4723 return False;
4726 search->private_data = state;
4727 search->next_entry = ldapsam_search_next_entry;
4728 search->search_end = ldapsam_search_end;
4730 return ldapsam_search_firstpage(search);
4733 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4734 TALLOC_CTX *mem_ctx,
4735 LDAP *ld, LDAPMessage *entry,
4736 struct samr_displayentry *result)
4738 char **vals;
4739 size_t converted_size;
4740 struct dom_sid sid;
4741 uint16_t group_type;
4743 result->account_name = "";
4744 result->fullname = "";
4745 result->description = "";
4748 vals = ldap_get_values(ld, entry, "sambaGroupType");
4749 if ((vals == NULL) || (vals[0] == NULL)) {
4750 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4751 if (vals != NULL) {
4752 ldap_value_free(vals);
4754 return False;
4757 group_type = atoi(vals[0]);
4759 if ((state->group_type != 0) &&
4760 ((state->group_type != group_type))) {
4761 ldap_value_free(vals);
4762 return False;
4765 ldap_value_free(vals);
4767 /* display name is the NT group name */
4769 vals = ldap_get_values(ld, entry, "displayName");
4770 if ((vals == NULL) || (vals[0] == NULL)) {
4771 DEBUG(8, ("\"displayName\" not found\n"));
4773 /* fallback to the 'cn' attribute */
4774 vals = ldap_get_values(ld, entry, "cn");
4775 if ((vals == NULL) || (vals[0] == NULL)) {
4776 DEBUG(5, ("\"cn\" not found\n"));
4777 return False;
4779 if (!pull_utf8_talloc(mem_ctx,
4780 discard_const_p(char *,
4781 &result->account_name),
4782 vals[0], &converted_size))
4784 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4785 "failed: %s", strerror(errno)));
4788 else if (!pull_utf8_talloc(mem_ctx,
4789 discard_const_p(char *,
4790 &result->account_name),
4791 vals[0], &converted_size))
4793 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4794 strerror(errno)));
4797 ldap_value_free(vals);
4799 vals = ldap_get_values(ld, entry, "description");
4800 if ((vals == NULL) || (vals[0] == NULL))
4801 DEBUG(8, ("\"description\" not found\n"));
4802 else if (!pull_utf8_talloc(mem_ctx,
4803 discard_const_p(char *, &result->description),
4804 vals[0], &converted_size))
4806 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4807 strerror(errno)));
4809 ldap_value_free(vals);
4811 if ((result->account_name == NULL) ||
4812 (result->fullname == NULL) ||
4813 (result->description == NULL)) {
4814 DEBUG(0, ("talloc failed\n"));
4815 return False;
4818 vals = ldap_get_values(ld, entry, "sambaSid");
4819 if ((vals == NULL) || (vals[0] == NULL)) {
4820 DEBUG(0, ("\"objectSid\" not found\n"));
4821 if (vals != NULL) {
4822 ldap_value_free(vals);
4824 return False;
4827 if (!string_to_sid(&sid, vals[0])) {
4828 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4829 return False;
4832 ldap_value_free(vals);
4834 switch (group_type) {
4835 case SID_NAME_DOM_GRP:
4836 case SID_NAME_ALIAS:
4838 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4839 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4841 struct dom_sid_buf buf;
4842 DEBUG(0, ("%s is not in our domain\n",
4843 dom_sid_str_buf(&sid, &buf)));
4844 return False;
4846 break;
4848 default:
4849 DEBUG(0,("unknown group type: %d\n", group_type));
4850 return False;
4853 result->acct_flags = 0;
4855 return True;
4858 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4859 struct pdb_search *search,
4860 const struct dom_sid *sid,
4861 enum lsa_SidType type)
4863 struct ldapsam_privates *ldap_state =
4864 (struct ldapsam_privates *)methods->private_data;
4865 struct ldap_search_state *state;
4866 struct dom_sid_buf tmp;
4868 state = talloc(search, struct ldap_search_state);
4869 if (state == NULL) {
4870 DEBUG(0, ("talloc failed\n"));
4871 return False;
4874 state->connection = ldap_state->smbldap_state;
4876 state->base = lp_ldap_suffix();
4877 state->connection = ldap_state->smbldap_state;
4878 state->scope = LDAP_SCOPE_SUBTREE;
4879 state->filter = talloc_asprintf(search, "(&(objectclass="LDAP_OBJ_GROUPMAP")"
4880 "(sambaGroupType=%d)(sambaSID=%s*))",
4881 type,
4882 dom_sid_str_buf(sid, &tmp));
4883 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4884 "displayName", "description",
4885 "sambaGroupType", NULL);
4886 state->attrsonly = 0;
4887 state->pagedresults_cookie = NULL;
4888 state->entries = NULL;
4889 state->group_type = type;
4890 state->ldap2displayentry = ldapgroup2displayentry;
4892 if ((state->filter == NULL) || (state->attrs == NULL)) {
4893 DEBUG(0, ("talloc failed\n"));
4894 return False;
4897 search->private_data = state;
4898 search->next_entry = ldapsam_search_next_entry;
4899 search->search_end = ldapsam_search_end;
4901 return ldapsam_search_firstpage(search);
4904 static bool ldapsam_search_groups(struct pdb_methods *methods,
4905 struct pdb_search *search)
4907 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4910 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4911 struct pdb_search *search,
4912 const struct dom_sid *sid)
4914 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4917 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4919 return PDB_CAP_STORE_RIDS;
4922 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4923 uint32_t *rid)
4925 struct smbldap_state *smbldap_state = priv->smbldap_state;
4927 LDAPMessage *result = NULL;
4928 LDAPMessage *entry = NULL;
4929 LDAPMod **mods = NULL;
4930 NTSTATUS status;
4931 char *value;
4932 int rc;
4933 uint32_t nextRid = 0;
4934 const char *dn;
4935 uint32_t tmp;
4936 int error = 0;
4938 TALLOC_CTX *mem_ctx;
4940 mem_ctx = talloc_new(NULL);
4941 if (mem_ctx == NULL) {
4942 DEBUG(0, ("talloc_new failed\n"));
4943 return NT_STATUS_NO_MEMORY;
4946 status = smbldap_search_domain_info(smbldap_state, &result,
4947 get_global_sam_name(), False);
4948 if (!NT_STATUS_IS_OK(status)) {
4949 DEBUG(3, ("Could not get domain info: %s\n",
4950 nt_errstr(status)));
4951 goto done;
4954 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
4956 entry = ldap_first_entry(priv2ld(priv), result);
4957 if (entry == NULL) {
4958 DEBUG(0, ("Could not get domain info entry\n"));
4959 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4960 goto done;
4963 /* Find the largest of the three attributes "sambaNextRid",
4964 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4965 concept of differentiating between user and group rids, and will
4966 use only "sambaNextRid" in the future. But for compatibility
4967 reasons I look if others have chosen different strategies -- VL */
4969 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4970 "sambaNextRid", mem_ctx);
4971 if (value != NULL) {
4972 tmp = (uint32_t)smb_strtoul(value,
4973 NULL,
4975 &error,
4976 SMB_STR_STANDARD);
4977 if (error != 0) {
4978 goto done;
4981 nextRid = MAX(nextRid, tmp);
4984 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4985 "sambaNextUserRid", mem_ctx);
4986 if (value != NULL) {
4987 tmp = (uint32_t)smb_strtoul(value,
4988 NULL,
4990 &error,
4991 SMB_STR_STANDARD);
4992 if (error != 0) {
4993 goto done;
4996 nextRid = MAX(nextRid, tmp);
4999 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5000 "sambaNextGroupRid", mem_ctx);
5001 if (value != NULL) {
5002 tmp = (uint32_t)smb_strtoul(value,
5003 NULL,
5005 &error,
5006 SMB_STR_STANDARD);
5007 if (error != 0) {
5008 goto done;
5011 nextRid = MAX(nextRid, tmp);
5014 if (nextRid == 0) {
5015 nextRid = BASE_RID-1;
5018 nextRid += 1;
5020 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
5021 talloc_asprintf(mem_ctx, "%d", nextRid));
5022 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
5024 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
5025 status = NT_STATUS_NO_MEMORY;
5026 goto done;
5029 rc = smbldap_modify(smbldap_state, dn, mods);
5031 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5032 * please retry" */
5034 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
5036 done:
5037 if (NT_STATUS_IS_OK(status)) {
5038 *rid = nextRid;
5041 TALLOC_FREE(mem_ctx);
5042 return status;
5045 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
5047 int i;
5049 for (i=0; i<10; i++) {
5050 NTSTATUS result = ldapsam_get_new_rid(
5051 (struct ldapsam_privates *)methods->private_data, rid);
5052 if (NT_STATUS_IS_OK(result)) {
5053 return result;
5056 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
5057 return result;
5060 /* The ldap update failed (maybe a race condition), retry */
5063 /* Tried 10 times, fail. */
5064 return NT_STATUS_ACCESS_DENIED;
5067 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
5069 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
5070 return NT_STATUS_IS_OK(result) ? True : False;
5073 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
5074 const struct dom_sid *sid,
5075 struct unixid *id)
5077 struct ldapsam_privates *priv =
5078 (struct ldapsam_privates *)methods->private_data;
5079 char *filter;
5080 int error = 0;
5081 struct dom_sid_buf buf;
5082 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5083 NULL };
5084 LDAPMessage *result = NULL;
5085 LDAPMessage *entry = NULL;
5086 bool ret = False;
5087 char *value;
5088 int rc;
5090 TALLOC_CTX *mem_ctx;
5092 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
5093 if (ret == true) {
5094 return true;
5097 mem_ctx = talloc_new(NULL);
5098 if (mem_ctx == NULL) {
5099 DEBUG(0, ("talloc_new failed\n"));
5100 return False;
5103 filter = talloc_asprintf(mem_ctx,
5104 "(&(sambaSid=%s)"
5105 "(|(objectClass="LDAP_OBJ_GROUPMAP")(objectClass="LDAP_OBJ_SAMBASAMACCOUNT")))",
5106 dom_sid_str_buf(sid, &buf));
5107 if (filter == NULL) {
5108 DEBUG(5, ("talloc_asprintf failed\n"));
5109 goto done;
5112 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5113 attrs, &result);
5114 if (rc != LDAP_SUCCESS) {
5115 goto done;
5117 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
5119 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5120 DEBUG(10, ("Got %d entries, expected one\n",
5121 ldap_count_entries(priv2ld(priv), result)));
5122 goto done;
5125 entry = ldap_first_entry(priv2ld(priv), result);
5127 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5128 "sambaGroupType", mem_ctx);
5130 if (value != NULL) {
5131 const char *gid_str;
5132 /* It's a group */
5134 gid_str = smbldap_talloc_single_attribute(
5135 priv2ld(priv), entry, "gidNumber", mem_ctx);
5136 if (gid_str == NULL) {
5137 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5138 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5139 entry)));
5140 goto done;
5143 id->id = smb_strtoul(gid_str,
5144 NULL,
5146 &error,
5147 SMB_STR_STANDARD);
5148 if (error != 0) {
5149 goto done;
5152 id->type = ID_TYPE_GID;
5153 ret = True;
5154 goto done;
5157 /* It must be a user */
5159 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5160 "uidNumber", mem_ctx);
5161 if (value == NULL) {
5162 DEBUG(1, ("Could not find uidNumber in %s\n",
5163 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5164 goto done;
5167 id->id = smb_strtoul(value, NULL, 10, &error, SMB_STR_STANDARD);
5168 if (error != 0) {
5169 goto done;
5172 id->type = ID_TYPE_UID;
5173 ret = True;
5174 done:
5175 TALLOC_FREE(mem_ctx);
5176 return ret;
5180 * Find the SID for a uid.
5181 * This is shortcut is only used if ldapsam:trusted is set to true.
5183 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5184 struct dom_sid *sid)
5186 struct ldapsam_privates *priv =
5187 (struct ldapsam_privates *)methods->private_data;
5188 char *filter;
5189 const char *attrs[] = { "sambaSID", NULL };
5190 LDAPMessage *result = NULL;
5191 LDAPMessage *entry = NULL;
5192 bool ret = false;
5193 char *user_sid_string;
5194 struct dom_sid user_sid;
5195 int rc;
5196 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5198 filter = talloc_asprintf(tmp_ctx,
5199 "(&(uidNumber=%u)"
5200 "(objectClass="LDAP_OBJ_POSIXACCOUNT")"
5201 "(objectClass="LDAP_OBJ_SAMBASAMACCOUNT"))",
5202 (unsigned int)uid);
5203 if (filter == NULL) {
5204 DEBUG(3, ("talloc_asprintf failed\n"));
5205 goto done;
5208 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5209 if (rc != LDAP_SUCCESS) {
5210 goto done;
5212 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5214 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5215 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5216 ldap_count_entries(priv2ld(priv), result),
5217 (unsigned int)uid));
5218 goto done;
5221 entry = ldap_first_entry(priv2ld(priv), result);
5223 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5224 "sambaSID", tmp_ctx);
5225 if (user_sid_string == NULL) {
5226 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5227 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5228 goto done;
5231 if (!string_to_sid(&user_sid, user_sid_string)) {
5232 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5233 user_sid_string));
5234 goto done;
5237 sid_copy(sid, &user_sid);
5239 ret = true;
5241 done:
5242 TALLOC_FREE(tmp_ctx);
5243 return ret;
5247 * Find the SID for a gid.
5248 * This is shortcut is only used if ldapsam:trusted is set to true.
5250 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5251 struct dom_sid *sid)
5253 struct ldapsam_privates *priv =
5254 (struct ldapsam_privates *)methods->private_data;
5255 char *filter;
5256 const char *attrs[] = { "sambaSID", NULL };
5257 LDAPMessage *result = NULL;
5258 LDAPMessage *entry = NULL;
5259 bool ret = false;
5260 char *group_sid_string;
5261 struct dom_sid group_sid;
5262 int rc;
5263 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5265 filter = talloc_asprintf(tmp_ctx,
5266 "(&(gidNumber=%u)"
5267 "(objectClass="LDAP_OBJ_GROUPMAP"))",
5268 (unsigned int)gid);
5269 if (filter == NULL) {
5270 DEBUG(3, ("talloc_asprintf failed\n"));
5271 goto done;
5274 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5275 if (rc != LDAP_SUCCESS) {
5276 goto done;
5278 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5280 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5281 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5282 ldap_count_entries(priv2ld(priv), result),
5283 (unsigned int)gid));
5284 goto done;
5287 entry = ldap_first_entry(priv2ld(priv), result);
5289 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5290 "sambaSID", tmp_ctx);
5291 if (group_sid_string == NULL) {
5292 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5293 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5294 goto done;
5297 if (!string_to_sid(&group_sid, group_sid_string)) {
5298 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5299 group_sid_string));
5300 goto done;
5303 sid_copy(sid, &group_sid);
5305 ret = true;
5307 done:
5308 TALLOC_FREE(tmp_ctx);
5309 return ret;
5312 static bool ldapsam_id_to_sid(struct pdb_methods *methods, struct unixid *id,
5313 struct dom_sid *sid)
5315 switch (id->type) {
5316 case ID_TYPE_UID:
5317 return ldapsam_uid_to_sid(methods, id->id, sid);
5319 case ID_TYPE_GID:
5320 return ldapsam_gid_to_sid(methods, id->id, sid);
5322 default:
5323 return false;
5329 * The following functions are called only if
5330 * ldapsam:trusted and ldapsam:editposix are
5331 * set to true
5335 * ldapsam_create_user creates a new
5336 * posixAccount and sambaSamAccount object
5337 * in the ldap users subtree
5339 * The uid is allocated by winbindd.
5342 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5343 TALLOC_CTX *tmp_ctx, const char *name,
5344 uint32_t acb_info, uint32_t *rid)
5346 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5347 LDAPMessage *entry = NULL;
5348 LDAPMessage *result = NULL;
5349 uint32_t num_result;
5350 bool is_machine = False;
5351 bool add_posix = False;
5352 bool init_okay = False;
5353 LDAPMod **mods = NULL;
5354 struct samu *user;
5355 char *filter;
5356 char *username;
5357 char *homedir;
5358 char *gidstr;
5359 char *uidstr;
5360 char *shell;
5361 const char *dn = NULL;
5362 struct dom_sid group_sid;
5363 struct dom_sid user_sid;
5364 gid_t gid = -1;
5365 uid_t uid = -1;
5366 NTSTATUS ret;
5367 int rc;
5369 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5370 acb_info & ACB_WSTRUST ||
5371 acb_info & ACB_SVRTRUST ||
5372 acb_info & ACB_DOMTRUST) {
5373 is_machine = True;
5376 username = escape_ldap_string(talloc_tos(), name);
5377 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass="LDAP_OBJ_POSIXACCOUNT"))",
5378 username);
5379 TALLOC_FREE(username);
5381 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5382 if (rc != LDAP_SUCCESS) {
5383 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5384 return NT_STATUS_ACCESS_DENIED;
5386 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5388 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5390 if (num_result > 1) {
5391 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5392 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5395 if (num_result == 1) {
5396 char *tmp;
5397 /* check if it is just a posix account.
5398 * or if there is a sid attached to this entry
5401 entry = ldap_first_entry(priv2ld(ldap_state), result);
5402 if (!entry) {
5403 return NT_STATUS_UNSUCCESSFUL;
5406 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5407 if (tmp) {
5408 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5409 return NT_STATUS_USER_EXISTS;
5412 /* it is just a posix account, retrieve the dn for later use */
5413 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5414 if (!dn) {
5415 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5416 return NT_STATUS_NO_MEMORY;
5420 if (num_result == 0) {
5421 add_posix = True;
5424 /* Create the basic samu structure and generate the mods for the ldap commit */
5425 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5426 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5427 return ret;
5430 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5432 user = samu_new(tmp_ctx);
5433 if (!user) {
5434 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5435 return NT_STATUS_NO_MEMORY;
5438 if (!pdb_set_username(user, name, PDB_SET)) {
5439 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5440 return NT_STATUS_UNSUCCESSFUL;
5442 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5443 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5444 return NT_STATUS_UNSUCCESSFUL;
5446 if (is_machine) {
5447 if (acb_info & ACB_NORMAL) {
5448 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5449 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5450 return NT_STATUS_UNSUCCESSFUL;
5452 } else {
5453 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5454 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5455 return NT_STATUS_UNSUCCESSFUL;
5458 } else {
5459 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5460 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5461 return NT_STATUS_UNSUCCESSFUL;
5465 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5466 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5467 return NT_STATUS_UNSUCCESSFUL;
5470 init_okay = init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed);
5472 if (!init_okay) {
5473 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5474 ldap_mods_free(mods, true);
5475 return NT_STATUS_UNSUCCESSFUL;
5478 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5479 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5481 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5483 if (add_posix) {
5484 char *escape_name;
5486 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5488 /* retrieve the Domain Users group gid */
5489 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5490 !sid_to_gid(&group_sid, &gid)) {
5491 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5492 ldap_mods_free(mods, true);
5493 return NT_STATUS_INVALID_PRIMARY_GROUP;
5496 /* lets allocate a new userid for this user */
5497 if (!winbind_allocate_uid(&uid)) {
5498 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5499 ldap_mods_free(mods, true);
5500 return NT_STATUS_UNSUCCESSFUL;
5504 if (is_machine) {
5505 /* TODO: choose a more appropriate default for machines */
5506 homedir = talloc_sub_specified(tmp_ctx,
5507 lp_template_homedir(),
5508 "SMB_workstations_home",
5509 NULL,
5510 ldap_state->domain_name,
5511 uid,
5512 gid);
5513 shell = talloc_strdup(tmp_ctx, "/bin/false");
5514 } else {
5515 homedir = talloc_sub_specified(tmp_ctx,
5516 lp_template_homedir(),
5517 name,
5518 NULL,
5519 ldap_state->domain_name,
5520 uid,
5521 gid);
5522 shell = talloc_sub_specified(tmp_ctx,
5523 lp_template_shell(),
5524 name,
5525 NULL,
5526 ldap_state->domain_name,
5527 uid,
5528 gid);
5530 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5531 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5533 escape_name = escape_rdn_val_string_alloc(name);
5534 if (!escape_name) {
5535 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5536 ldap_mods_free(mods, true);
5537 return NT_STATUS_NO_MEMORY;
5540 if (is_machine) {
5541 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix (talloc_tos()));
5542 } else {
5543 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix (talloc_tos()));
5546 SAFE_FREE(escape_name);
5548 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5549 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5550 ldap_mods_free(mods, true);
5551 return NT_STATUS_NO_MEMORY;
5554 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5555 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5556 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5557 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5558 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5559 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5560 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5563 if (add_posix) {
5564 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5565 } else {
5566 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5569 ldap_mods_free(mods, true);
5571 if (rc != LDAP_SUCCESS) {
5572 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5573 return NT_STATUS_UNSUCCESSFUL;
5576 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5578 flush_pwnam_cache();
5580 return NT_STATUS_OK;
5583 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5585 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5586 LDAPMessage *result = NULL;
5587 LDAPMessage *entry = NULL;
5588 int num_result;
5589 const char *dn;
5590 char *filter;
5591 int rc;
5593 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5595 filter = talloc_asprintf(tmp_ctx,
5596 "(&(uid=%s)"
5597 "(objectClass="LDAP_OBJ_POSIXACCOUNT")"
5598 "(objectClass="LDAP_OBJ_SAMBASAMACCOUNT"))",
5599 pdb_get_username(sam_acct));
5600 if (filter == NULL) {
5601 return NT_STATUS_NO_MEMORY;
5604 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5605 if (rc != LDAP_SUCCESS) {
5606 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5607 return NT_STATUS_UNSUCCESSFUL;
5609 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5611 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5613 if (num_result == 0) {
5614 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5615 return NT_STATUS_NO_SUCH_USER;
5618 if (num_result > 1) {
5619 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5620 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5623 entry = ldap_first_entry(priv2ld(ldap_state), result);
5624 if (!entry) {
5625 return NT_STATUS_UNSUCCESSFUL;
5628 /* it is just a posix account, retrieve the dn for later use */
5629 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5630 if (!dn) {
5631 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5632 return NT_STATUS_NO_MEMORY;
5635 /* try to remove memberships first */
5637 NTSTATUS status;
5638 struct dom_sid *sids = NULL;
5639 gid_t *gids = NULL;
5640 uint32_t num_groups = 0;
5641 int i;
5642 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5644 status = ldapsam_enum_group_memberships(my_methods,
5645 tmp_ctx,
5646 sam_acct,
5647 &sids,
5648 &gids,
5649 &num_groups);
5650 if (!NT_STATUS_IS_OK(status)) {
5651 goto delete_dn;
5654 for (i=0; i < num_groups; i++) {
5656 uint32_t group_rid;
5658 sid_peek_rid(&sids[i], &group_rid);
5660 ldapsam_del_groupmem(my_methods,
5661 tmp_ctx,
5662 group_rid,
5663 user_rid);
5667 delete_dn:
5669 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5670 if (rc != LDAP_SUCCESS) {
5671 return NT_STATUS_UNSUCCESSFUL;
5674 flush_pwnam_cache();
5676 return NT_STATUS_OK;
5680 * ldapsam_create_group creates a new
5681 * posixGroup and sambaGroupMapping object
5682 * in the ldap groups subtree
5684 * The gid is allocated by winbindd.
5687 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5688 TALLOC_CTX *tmp_ctx,
5689 const char *name,
5690 uint32_t *rid)
5692 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5693 NTSTATUS ret;
5694 LDAPMessage *entry = NULL;
5695 LDAPMessage *result = NULL;
5696 uint32_t num_result;
5697 bool is_new_entry = False;
5698 LDAPMod **mods = NULL;
5699 char *filter;
5700 char *groupname;
5701 const char *grouptype;
5702 char *gidstr;
5703 const char *dn = NULL;
5704 struct dom_sid group_sid;
5705 struct dom_sid_buf buf;
5706 gid_t gid = -1;
5707 int rc;
5708 int error = 0;
5710 groupname = escape_ldap_string(talloc_tos(), name);
5711 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass="LDAP_OBJ_POSIXGROUP"))",
5712 groupname);
5713 TALLOC_FREE(groupname);
5715 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5716 if (rc != LDAP_SUCCESS) {
5717 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5718 return NT_STATUS_UNSUCCESSFUL;
5720 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5722 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5724 if (num_result > 1) {
5725 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5726 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5729 if (num_result == 1) {
5730 char *tmp;
5731 /* check if it is just a posix group.
5732 * or if there is a sid attached to this entry
5735 entry = ldap_first_entry(priv2ld(ldap_state), result);
5736 if (!entry) {
5737 return NT_STATUS_UNSUCCESSFUL;
5740 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5741 if (tmp) {
5742 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5743 return NT_STATUS_GROUP_EXISTS;
5746 /* it is just a posix group, retrieve the gid and the dn for later use */
5747 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5748 if (!tmp) {
5749 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5750 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5753 gid = smb_strtoul(tmp, NULL, 10, &error, SMB_STR_STANDARD);
5754 if (error != 0) {
5755 DBG_ERR("Failed to convert gidNumber\n");
5756 return NT_STATUS_UNSUCCESSFUL;
5759 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5760 if (!dn) {
5761 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5762 return NT_STATUS_NO_MEMORY;
5766 if (num_result == 0) {
5767 is_new_entry = true;
5770 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5771 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5772 return ret;
5775 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5777 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5779 if (!grouptype) {
5780 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5781 return NT_STATUS_NO_MEMORY;
5784 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5785 smbldap_set_mod(&mods,
5786 LDAP_MOD_ADD,
5787 "sambaSid",
5788 dom_sid_str_buf(&group_sid, &buf));
5789 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5790 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5792 if (is_new_entry) {
5793 char *escape_name;
5795 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5797 /* lets allocate a new groupid for this group */
5798 if (!winbind_allocate_gid(&gid)) {
5799 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5800 return NT_STATUS_UNSUCCESSFUL;
5803 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5805 escape_name = escape_rdn_val_string_alloc(name);
5806 if (!escape_name) {
5807 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5808 return NT_STATUS_NO_MEMORY;
5811 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix(talloc_tos()));
5813 SAFE_FREE(escape_name);
5815 if (!gidstr || !dn) {
5816 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5817 return NT_STATUS_NO_MEMORY;
5820 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5821 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5822 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5825 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
5827 if (is_new_entry) {
5828 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5829 #if 0
5830 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5831 /* This call may fail with rfc2307bis schema */
5832 /* Retry adding a structural class */
5833 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5834 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5836 #endif
5837 } else {
5838 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5841 if (rc != LDAP_SUCCESS) {
5842 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5843 return NT_STATUS_UNSUCCESSFUL;
5846 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5848 return NT_STATUS_OK;
5851 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5853 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5854 LDAPMessage *result = NULL;
5855 LDAPMessage *entry = NULL;
5856 int num_result;
5857 const char *dn;
5858 char *gidstr;
5859 char *filter;
5860 struct dom_sid group_sid;
5861 struct dom_sid_buf buf;
5862 int rc;
5864 /* get the group sid */
5865 sid_compose(&group_sid, get_global_sam_sid(), rid);
5867 filter = talloc_asprintf(tmp_ctx,
5868 "(&(sambaSID=%s)"
5869 "(objectClass="LDAP_OBJ_POSIXGROUP")"
5870 "(objectClass="LDAP_OBJ_GROUPMAP"))",
5871 dom_sid_str_buf(&group_sid, &buf));
5872 if (filter == NULL) {
5873 return NT_STATUS_NO_MEMORY;
5876 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5877 if (rc != LDAP_SUCCESS) {
5878 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5879 return NT_STATUS_UNSUCCESSFUL;
5881 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5883 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5885 if (num_result == 0) {
5886 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5887 return NT_STATUS_NO_SUCH_GROUP;
5890 if (num_result > 1) {
5891 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5892 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5895 entry = ldap_first_entry(priv2ld(ldap_state), result);
5896 if (!entry) {
5897 return NT_STATUS_UNSUCCESSFUL;
5900 /* here it is, retrieve the dn for later use */
5901 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5902 if (!dn) {
5903 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5904 return NT_STATUS_NO_MEMORY;
5907 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5908 if (!gidstr) {
5909 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5910 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5913 /* check no user have this group marked as primary group */
5914 filter = talloc_asprintf(tmp_ctx,
5915 "(&(gidNumber=%s)"
5916 "(objectClass="LDAP_OBJ_POSIXACCOUNT")"
5917 "(objectClass="LDAP_OBJ_SAMBASAMACCOUNT"))",
5918 gidstr);
5920 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5921 if (rc != LDAP_SUCCESS) {
5922 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5923 return NT_STATUS_UNSUCCESSFUL;
5925 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5927 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5929 if (num_result != 0) {
5930 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5931 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5934 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5935 if (rc != LDAP_SUCCESS) {
5936 return NT_STATUS_UNSUCCESSFUL;
5939 return NT_STATUS_OK;
5942 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5943 TALLOC_CTX *tmp_ctx,
5944 uint32_t group_rid,
5945 uint32_t member_rid,
5946 int modop)
5948 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5949 LDAPMessage *entry = NULL;
5950 LDAPMessage *result = NULL;
5951 uint32_t num_result;
5952 LDAPMod **mods = NULL;
5953 char *filter;
5954 char *uidstr;
5955 const char *dn = NULL;
5956 struct dom_sid group_sid;
5957 struct dom_sid member_sid;
5958 struct dom_sid_buf buf;
5959 int rc;
5960 int error = 0;
5962 switch (modop) {
5963 case LDAP_MOD_ADD:
5964 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5965 break;
5966 case LDAP_MOD_DELETE:
5967 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5968 break;
5969 default:
5970 return NT_STATUS_UNSUCCESSFUL;
5973 /* get member sid */
5974 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5976 /* get the group sid */
5977 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5979 filter = talloc_asprintf(tmp_ctx,
5980 "(&(sambaSID=%s)"
5981 "(objectClass="LDAP_OBJ_POSIXACCOUNT")"
5982 "(objectClass="LDAP_OBJ_SAMBASAMACCOUNT"))",
5983 dom_sid_str_buf(&member_sid, &buf));
5984 if (filter == NULL) {
5985 return NT_STATUS_NO_MEMORY;
5988 /* get the member uid */
5989 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5990 if (rc != LDAP_SUCCESS) {
5991 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5992 return NT_STATUS_UNSUCCESSFUL;
5994 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5996 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5998 if (num_result == 0) {
5999 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6000 return NT_STATUS_NO_SUCH_MEMBER;
6003 if (num_result > 1) {
6004 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6005 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6008 entry = ldap_first_entry(priv2ld(ldap_state), result);
6009 if (!entry) {
6010 return NT_STATUS_UNSUCCESSFUL;
6013 if (modop == LDAP_MOD_DELETE) {
6014 /* check if we are trying to remove the member from his primary group */
6015 char *gidstr;
6016 gid_t user_gid, group_gid;
6018 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
6019 if (!gidstr) {
6020 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6021 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6024 user_gid = smb_strtoul(gidstr, NULL, 10, &error, SMB_STR_STANDARD);
6025 if (error != 0) {
6026 DBG_ERR("Failed to convert user gid\n");
6027 return NT_STATUS_UNSUCCESSFUL;
6030 if (!sid_to_gid(&group_sid, &group_gid)) {
6031 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6032 return NT_STATUS_UNSUCCESSFUL;
6035 if (user_gid == group_gid) {
6036 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6037 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
6041 /* here it is, retrieve the uid for later use */
6042 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
6043 if (!uidstr) {
6044 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6045 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6048 filter = talloc_asprintf(tmp_ctx,
6049 "(&(sambaSID=%s)"
6050 "(objectClass="LDAP_OBJ_POSIXGROUP")"
6051 "(objectClass="LDAP_OBJ_GROUPMAP"))",
6052 dom_sid_str_buf(&group_sid, &buf));
6054 /* get the group */
6055 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6056 if (rc != LDAP_SUCCESS) {
6057 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6058 return NT_STATUS_UNSUCCESSFUL;
6060 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
6062 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6064 if (num_result == 0) {
6065 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6066 return NT_STATUS_NO_SUCH_GROUP;
6069 if (num_result > 1) {
6070 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6071 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6074 entry = ldap_first_entry(priv2ld(ldap_state), result);
6075 if (!entry) {
6076 return NT_STATUS_UNSUCCESSFUL;
6079 /* here it is, retrieve the dn for later use */
6080 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
6081 if (!dn) {
6082 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6083 return NT_STATUS_NO_MEMORY;
6086 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
6088 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
6090 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6091 if (rc != LDAP_SUCCESS) {
6092 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
6093 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6094 return NT_STATUS_MEMBER_IN_GROUP;
6096 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
6097 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6098 return NT_STATUS_MEMBER_NOT_IN_GROUP;
6100 return NT_STATUS_UNSUCCESSFUL;
6103 return NT_STATUS_OK;
6106 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
6107 TALLOC_CTX *tmp_ctx,
6108 uint32_t group_rid,
6109 uint32_t member_rid)
6111 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
6113 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
6114 TALLOC_CTX *tmp_ctx,
6115 uint32_t group_rid,
6116 uint32_t member_rid)
6118 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
6121 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
6122 TALLOC_CTX *mem_ctx,
6123 struct samu *sampass)
6125 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6126 LDAPMessage *entry = NULL;
6127 LDAPMessage *result = NULL;
6128 uint32_t num_result;
6129 LDAPMod **mods = NULL;
6130 char *filter;
6131 char *escape_username;
6132 char *gidstr;
6133 char *dn = NULL;
6134 gid_t gid;
6135 int rc;
6137 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6139 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6140 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6141 return NT_STATUS_UNSUCCESSFUL;
6143 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6144 if (!gidstr) {
6145 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6146 return NT_STATUS_NO_MEMORY;
6149 escape_username = escape_ldap_string(talloc_tos(),
6150 pdb_get_username(sampass));
6151 if (escape_username== NULL) {
6152 return NT_STATUS_NO_MEMORY;
6155 filter = talloc_asprintf(mem_ctx,
6156 "(&(uid=%s)"
6157 "(objectClass="LDAP_OBJ_POSIXACCOUNT")"
6158 "(objectClass="LDAP_OBJ_SAMBASAMACCOUNT"))",
6159 escape_username);
6161 TALLOC_FREE(escape_username);
6163 if (filter == NULL) {
6164 return NT_STATUS_NO_MEMORY;
6167 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6168 if (rc != LDAP_SUCCESS) {
6169 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6170 return NT_STATUS_UNSUCCESSFUL;
6172 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6174 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6176 if (num_result == 0) {
6177 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6178 return NT_STATUS_NO_SUCH_USER;
6181 if (num_result > 1) {
6182 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6183 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6186 entry = ldap_first_entry(priv2ld(ldap_state), result);
6187 if (!entry) {
6188 return NT_STATUS_UNSUCCESSFUL;
6191 /* retrieve the dn for later use */
6192 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6193 if (!dn) {
6194 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6195 return NT_STATUS_NO_MEMORY;
6198 /* remove the old one, and add the new one, this way we do not risk races */
6199 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6201 if (mods == NULL) {
6202 TALLOC_FREE(dn);
6203 return NT_STATUS_OK;
6206 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6207 TALLOC_FREE(dn);
6208 if (rc != LDAP_SUCCESS) {
6209 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6210 pdb_get_username(sampass), gidstr));
6211 return NT_STATUS_UNSUCCESSFUL;
6214 flush_pwnam_cache();
6216 return NT_STATUS_OK;
6220 /**********************************************************************
6221 trusted domains functions
6222 *********************************************************************/
6224 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6225 const char *domain)
6227 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6228 ldap_state->domain_dn);
6231 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6232 TALLOC_CTX *mem_ctx,
6233 const char *domain, LDAPMessage **entry)
6235 int rc;
6236 char *filter;
6237 int scope = LDAP_SCOPE_SUBTREE;
6238 const char **attrs = NULL; /* NULL: get all attrs */
6239 int attrsonly = 0; /* 0: return values too */
6240 LDAPMessage *result = NULL;
6241 char *trusted_dn;
6242 uint32_t num_result;
6244 filter = talloc_asprintf(talloc_tos(),
6245 "(&(objectClass="LDAP_OBJ_TRUSTDOM_PASSWORD")(sambaDomainName=%s))",
6246 domain);
6248 trusted_dn = trusteddom_dn(ldap_state, domain);
6249 if (trusted_dn == NULL) {
6250 return False;
6252 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6253 filter, attrs, attrsonly, &result);
6255 if (result != NULL) {
6256 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6259 if (rc == LDAP_NO_SUCH_OBJECT) {
6260 *entry = NULL;
6261 return True;
6264 if (rc != LDAP_SUCCESS) {
6265 return False;
6268 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6270 if (num_result > 1) {
6271 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6272 "%s object for domain '%s'?!\n",
6273 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6274 return False;
6277 if (num_result == 0) {
6278 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6279 "%s object for domain %s.\n",
6280 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6281 *entry = NULL;
6282 } else {
6283 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6286 return True;
6289 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6290 const char *domain,
6291 char** pwd,
6292 struct dom_sid *sid,
6293 time_t *pass_last_set_time)
6295 struct ldapsam_privates *ldap_state =
6296 (struct ldapsam_privates *)methods->private_data;
6297 LDAPMessage *entry = NULL;
6299 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6301 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6302 (entry == NULL))
6304 return False;
6307 /* password */
6308 if (pwd != NULL) {
6309 char *pwd_str;
6310 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6311 entry, "sambaClearTextPassword", talloc_tos());
6312 if (pwd_str == NULL) {
6313 return False;
6315 /* trusteddom_pw routines do not use talloc yet... */
6316 *pwd = SMB_STRDUP(pwd_str);
6317 if (*pwd == NULL) {
6318 return False;
6322 /* last change time */
6323 if (pass_last_set_time != NULL) {
6324 char *time_str;
6325 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6326 entry, "sambaPwdLastSet", talloc_tos());
6327 if (time_str == NULL) {
6328 return False;
6330 *pass_last_set_time = (time_t)atol(time_str);
6333 /* domain sid */
6334 if (sid != NULL) {
6335 char *sid_str;
6336 struct dom_sid dom_sid;
6337 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6338 entry, "sambaSID",
6339 talloc_tos());
6340 if (sid_str == NULL) {
6341 return False;
6343 if (!string_to_sid(&dom_sid, sid_str)) {
6344 return False;
6346 sid_copy(sid, &dom_sid);
6349 return True;
6352 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6353 const char* domain,
6354 const char* pwd,
6355 const struct dom_sid *sid)
6357 struct ldapsam_privates *ldap_state =
6358 (struct ldapsam_privates *)methods->private_data;
6359 LDAPMessage *entry = NULL;
6360 LDAPMod **mods = NULL;
6361 char *prev_pwd = NULL;
6362 char *trusted_dn = NULL;
6363 struct dom_sid_buf buf;
6364 int rc;
6366 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6369 * get the current entry (if there is one) in order to put the
6370 * current password into the previous password attribute
6372 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6373 return False;
6376 mods = NULL;
6377 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6378 LDAP_OBJ_TRUSTDOM_PASSWORD);
6379 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6380 domain);
6381 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6382 dom_sid_str_buf(sid, &buf));
6383 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6384 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6385 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6386 "sambaClearTextPassword", pwd);
6388 if (entry != NULL) {
6389 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6390 entry, "sambaClearTextPassword", talloc_tos());
6391 if (prev_pwd != NULL) {
6392 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6393 "sambaPreviousClearTextPassword",
6394 prev_pwd);
6398 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods);
6400 trusted_dn = trusteddom_dn(ldap_state, domain);
6401 if (trusted_dn == NULL) {
6402 return False;
6404 if (entry == NULL) {
6405 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6406 } else {
6407 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6410 if (rc != LDAP_SUCCESS) {
6411 DEBUG(1, ("error writing trusted domain password!\n"));
6412 return False;
6415 return True;
6418 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6419 const char *domain)
6421 int rc;
6422 struct ldapsam_privates *ldap_state =
6423 (struct ldapsam_privates *)methods->private_data;
6424 LDAPMessage *entry = NULL;
6425 const char *trusted_dn;
6427 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6428 return False;
6431 if (entry == NULL) {
6432 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6433 "%s\n", domain));
6434 return True;
6437 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6438 entry);
6439 if (trusted_dn == NULL) {
6440 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6441 return False;
6444 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6445 if (rc != LDAP_SUCCESS) {
6446 return False;
6449 return True;
6452 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6453 TALLOC_CTX *mem_ctx,
6454 uint32_t *num_domains,
6455 struct trustdom_info ***domains)
6457 int rc;
6458 struct ldapsam_privates *ldap_state =
6459 (struct ldapsam_privates *)methods->private_data;
6460 const char *filter;
6461 int scope = LDAP_SCOPE_SUBTREE;
6462 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6463 int attrsonly = 0; /* 0: return values too */
6464 LDAPMessage *result = NULL;
6465 LDAPMessage *entry = NULL;
6467 filter = "(objectClass="LDAP_OBJ_TRUSTDOM_PASSWORD")";
6469 rc = smbldap_search(ldap_state->smbldap_state,
6470 ldap_state->domain_dn,
6471 scope,
6472 filter,
6473 attrs,
6474 attrsonly,
6475 &result);
6477 if (result != NULL) {
6478 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6481 if (rc != LDAP_SUCCESS) {
6482 return NT_STATUS_UNSUCCESSFUL;
6485 *num_domains = 0;
6486 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6487 DEBUG(1, ("talloc failed\n"));
6488 return NT_STATUS_NO_MEMORY;
6491 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6492 entry != NULL;
6493 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6495 char *dom_name, *dom_sid_str;
6496 struct trustdom_info *dom_info;
6498 dom_info = talloc(*domains, struct trustdom_info);
6499 if (dom_info == NULL) {
6500 DEBUG(1, ("talloc failed\n"));
6501 return NT_STATUS_NO_MEMORY;
6504 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6505 entry,
6506 "sambaDomainName",
6507 talloc_tos());
6508 if (dom_name == NULL) {
6509 DEBUG(1, ("talloc failed\n"));
6510 return NT_STATUS_NO_MEMORY;
6512 dom_info->name = dom_name;
6514 dom_sid_str = smbldap_talloc_single_attribute(
6515 priv2ld(ldap_state), entry, "sambaSID",
6516 talloc_tos());
6517 if (dom_sid_str == NULL) {
6518 DEBUG(1, ("talloc failed\n"));
6519 return NT_STATUS_NO_MEMORY;
6521 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6522 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6523 dom_sid_str));
6524 return NT_STATUS_UNSUCCESSFUL;
6527 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6528 domains, num_domains);
6530 if (*domains == NULL) {
6531 DEBUG(1, ("talloc failed\n"));
6532 return NT_STATUS_NO_MEMORY;
6536 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6537 return NT_STATUS_OK;
6541 /**********************************************************************
6542 Housekeeping
6543 *********************************************************************/
6545 static void free_private_data(void **vp)
6547 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6549 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6551 if ((*ldap_state)->result != NULL) {
6552 ldap_msgfree((*ldap_state)->result);
6553 (*ldap_state)->result = NULL;
6555 if ((*ldap_state)->domain_dn != NULL) {
6556 SAFE_FREE((*ldap_state)->domain_dn);
6559 *ldap_state = NULL;
6561 /* No need to free any further, as it is talloc()ed */
6564 /*********************************************************************
6565 Intitalise the parts of the pdb_methods structure that are common to
6566 all pdb_ldap modes
6567 *********************************************************************/
6569 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6571 NTSTATUS nt_status;
6572 struct ldapsam_privates *ldap_state;
6573 char *bind_dn = NULL;
6574 char *bind_secret = NULL;
6576 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6577 return nt_status;
6580 (*pdb_method)->name = "ldapsam";
6582 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6583 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6584 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6585 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6586 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6587 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6589 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6590 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6591 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6592 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6593 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6594 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6595 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6597 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6598 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6600 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6602 (*pdb_method)->capabilities = ldapsam_capabilities;
6603 (*pdb_method)->new_rid = ldapsam_new_rid;
6605 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6606 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6607 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6608 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6610 /* TODO: Setup private data and free */
6612 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6613 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6614 return NT_STATUS_NO_MEMORY;
6617 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6618 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6619 return NT_STATUS_NO_MEMORY;
6622 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6623 location, false, bind_dn, bind_secret,
6624 &ldap_state->smbldap_state);
6625 BURN_FREE_STR(bind_secret);
6626 SAFE_FREE(bind_dn);
6627 if ( !NT_STATUS_IS_OK(nt_status) ) {
6628 return nt_status;
6631 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6632 return NT_STATUS_NO_MEMORY;
6635 (*pdb_method)->private_data = ldap_state;
6637 (*pdb_method)->free_private_data = free_private_data;
6639 return NT_STATUS_OK;
6642 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods *m)
6644 return true;
6647 /**********************************************************************
6648 Initialise the normal mode for pdb_ldap
6649 *********************************************************************/
6651 NTSTATUS pdb_ldapsam_init_common(struct pdb_methods **pdb_method,
6652 const char *location)
6654 NTSTATUS nt_status;
6655 struct ldapsam_privates *ldap_state = NULL;
6656 uint32_t alg_rid_base;
6657 char *alg_rid_base_string = NULL;
6658 LDAPMessage *result = NULL;
6659 LDAPMessage *entry = NULL;
6660 struct dom_sid ldap_domain_sid;
6661 struct dom_sid secrets_domain_sid;
6662 char *domain_sid_string = NULL;
6663 char *dn = NULL;
6664 char *uri = talloc_strdup( NULL, location );
6666 trim_char( uri, '\"', '\"' );
6667 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6669 TALLOC_FREE(uri);
6671 if (!NT_STATUS_IS_OK(nt_status)) {
6672 return nt_status;
6675 (*pdb_method)->name = "ldapsam";
6677 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6678 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6679 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6680 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6681 (*pdb_method)->search_users = ldapsam_search_users;
6682 (*pdb_method)->search_groups = ldapsam_search_groups;
6683 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6684 (*pdb_method)->is_responsible_for_wellknown =
6685 ldapsam_is_responsible_for_wellknown;
6687 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6688 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6689 (*pdb_method)->enum_group_memberships =
6690 ldapsam_enum_group_memberships;
6691 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6692 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6693 (*pdb_method)->id_to_sid = ldapsam_id_to_sid;
6695 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6696 (*pdb_method)->create_user = ldapsam_create_user;
6697 (*pdb_method)->delete_user = ldapsam_delete_user;
6698 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6699 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6700 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6701 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6702 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6706 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6707 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6709 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6711 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6712 &result,
6713 ldap_state->domain_name, True);
6715 if ( !NT_STATUS_IS_OK(nt_status) ) {
6716 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6717 "info, nor add one to the domain. "
6718 "We cannot work reliably without it.\n"));
6719 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6722 /* Given that the above might fail, everything below this must be
6723 * optional */
6725 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
6726 result);
6727 if (!entry) {
6728 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6729 "entry\n"));
6730 ldap_msgfree(result);
6731 return NT_STATUS_UNSUCCESSFUL;
6734 dn = smbldap_talloc_dn(talloc_tos(),
6735 smbldap_get_ldap(ldap_state->smbldap_state),
6736 entry);
6737 if (!dn) {
6738 ldap_msgfree(result);
6739 return NT_STATUS_UNSUCCESSFUL;
6742 ldap_state->domain_dn = smb_xstrdup(dn);
6743 TALLOC_FREE(dn);
6745 domain_sid_string = smbldap_talloc_single_attribute(
6746 smbldap_get_ldap(ldap_state->smbldap_state),
6747 entry,
6748 get_userattr_key2string(ldap_state->schema_ver,
6749 LDAP_ATTR_USER_SID),
6750 talloc_tos());
6752 if (domain_sid_string) {
6753 bool found_sid;
6754 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6755 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6756 "read as a valid SID\n", domain_sid_string));
6757 ldap_msgfree(result);
6758 TALLOC_FREE(domain_sid_string);
6759 return NT_STATUS_INVALID_PARAMETER;
6761 found_sid = PDB_secrets_fetch_domain_sid(ldap_state->domain_name,
6762 &secrets_domain_sid);
6763 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6764 &ldap_domain_sid)) {
6765 struct dom_sid_buf buf1, buf2;
6766 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6767 "%s based on pdb_ldap results %s -> %s\n",
6768 ldap_state->domain_name,
6769 dom_sid_str_buf(&secrets_domain_sid, &buf1),
6770 dom_sid_str_buf(&ldap_domain_sid, &buf2)));
6772 /* reset secrets.tdb sid */
6773 PDB_secrets_store_domain_sid(ldap_state->domain_name,
6774 &ldap_domain_sid);
6775 DEBUG(1, ("New global sam SID: %s\n",
6776 dom_sid_str_buf(get_global_sam_sid(),
6777 &buf1)));
6779 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6780 TALLOC_FREE(domain_sid_string);
6783 alg_rid_base_string = smbldap_talloc_single_attribute(
6784 smbldap_get_ldap(ldap_state->smbldap_state),
6785 entry,
6786 get_attr_key2string( dominfo_attr_list,
6787 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6788 talloc_tos());
6789 if (alg_rid_base_string) {
6790 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6791 if (alg_rid_base != algorithmic_rid_base()) {
6792 DEBUG(0, ("The value of 'algorithmic RID base' has "
6793 "changed since the LDAP\n"
6794 "database was initialised. Aborting. \n"));
6795 ldap_msgfree(result);
6796 TALLOC_FREE(alg_rid_base_string);
6797 return NT_STATUS_UNSUCCESSFUL;
6799 TALLOC_FREE(alg_rid_base_string);
6801 ldap_msgfree(result);
6803 return NT_STATUS_OK;
6806 NTSTATUS pdb_ldapsam_init(TALLOC_CTX *ctx)
6808 NTSTATUS nt_status;
6810 nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION,
6811 "ldapsam",
6812 pdb_ldapsam_init_common);
6813 if (!NT_STATUS_IS_OK(nt_status)) {
6814 return nt_status;
6817 /* Let pdb_nds register backends */
6818 pdb_nds_init(ctx);
6820 return NT_STATUS_OK;