talloc: Fix copy&paste errors
[Samba/gebeck_regimport.git] / source3 / passdb / pdb_ldap.c
blobd9e272ed6df1038d29604261ea8b879047178023
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"
55 #undef DBGC_CLASS
56 #define DBGC_CLASS DBGC_PASSDB
58 #include <lber.h>
59 #include <ldap.h>
62 #include "smbldap.h"
63 #include "passdb/pdb_ldap.h"
64 #include "passdb/pdb_nds.h"
65 #include "passdb/pdb_ipa.h"
66 #include "passdb/pdb_ldap_util.h"
67 #include "passdb/pdb_ldap_schema.h"
69 /**********************************************************************
70 Simple helper function to make stuff better readable
71 **********************************************************************/
73 LDAP *priv2ld(struct ldapsam_privates *priv)
75 return priv->smbldap_state->ldap_struct;
78 /**********************************************************************
79 Get the attribute name given a user schame version.
80 **********************************************************************/
82 static const char* get_userattr_key2string( int schema_ver, int key )
84 switch ( schema_ver ) {
85 case SCHEMAVER_SAMBAACCOUNT:
86 return get_attr_key2string( attrib_map_v22, key );
88 case SCHEMAVER_SAMBASAMACCOUNT:
89 return get_attr_key2string( attrib_map_v30, key );
91 default:
92 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
93 break;
95 return NULL;
98 /**********************************************************************
99 Return the list of attribute names given a user schema version.
100 **********************************************************************/
102 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
104 switch ( schema_ver ) {
105 case SCHEMAVER_SAMBAACCOUNT:
106 return get_attr_list( mem_ctx, attrib_map_v22 );
108 case SCHEMAVER_SAMBASAMACCOUNT:
109 return get_attr_list( mem_ctx, attrib_map_v30 );
110 default:
111 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
112 break;
115 return NULL;
118 /**************************************************************************
119 Return the list of attribute names to delete given a user schema version.
120 **************************************************************************/
122 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
123 int schema_ver )
125 switch ( schema_ver ) {
126 case SCHEMAVER_SAMBAACCOUNT:
127 return get_attr_list( mem_ctx,
128 attrib_map_to_delete_v22 );
130 case SCHEMAVER_SAMBASAMACCOUNT:
131 return get_attr_list( mem_ctx,
132 attrib_map_to_delete_v30 );
133 default:
134 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
135 break;
138 return NULL;
142 /*******************************************************************
143 Generate the LDAP search filter for the objectclass based on the
144 version of the schema we are using.
145 ******************************************************************/
147 static const char* get_objclass_filter( int schema_ver )
149 fstring objclass_filter;
150 char *result;
152 switch( schema_ver ) {
153 case SCHEMAVER_SAMBAACCOUNT:
154 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
155 break;
156 case SCHEMAVER_SAMBASAMACCOUNT:
157 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
158 break;
159 default:
160 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
161 objclass_filter[0] = '\0';
162 break;
165 result = talloc_strdup(talloc_tos(), objclass_filter);
166 SMB_ASSERT(result != NULL);
167 return result;
170 /*****************************************************************
171 Scan a sequence number off OpenLDAP's syncrepl contextCSN
172 ******************************************************************/
174 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
176 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
177 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
178 LDAPMessage *msg = NULL;
179 LDAPMessage *entry = NULL;
180 TALLOC_CTX *mem_ctx;
181 char **values = NULL;
182 int rc, num_result, num_values, rid;
183 char *suffix = NULL;
184 char *tok;
185 const char *p;
186 const char **attrs;
188 /* Unfortunatly there is no proper way to detect syncrepl-support in
189 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
190 * but do not show up in the root-DSE yet. Neither we can query the
191 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
192 * objectclass. Currently we require lp_ldap_suffix() to show up as
193 * namingContext. - Guenther
196 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
197 return ntstatus;
200 if (!seq_num) {
201 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
202 return ntstatus;
205 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
206 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
207 "as top-level namingContext\n", lp_ldap_suffix()));
208 return ntstatus;
211 mem_ctx = talloc_init("ldapsam_get_seq_num");
213 if (mem_ctx == NULL)
214 return NT_STATUS_NO_MEMORY;
216 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
217 ntstatus = NT_STATUS_NO_MEMORY;
218 goto done;
221 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
222 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
223 if (rid > 0) {
225 /* consumer syncreplCookie: */
226 /* csn=20050126161620Z#0000001#00#00000 */
227 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
228 attrs[1] = NULL;
229 suffix = talloc_asprintf(mem_ctx,
230 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
231 if (!suffix) {
232 ntstatus = NT_STATUS_NO_MEMORY;
233 goto done;
235 } else {
237 /* provider contextCSN */
238 /* 20050126161620Z#000009#00#000000 */
239 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
240 attrs[1] = NULL;
241 suffix = talloc_asprintf(mem_ctx,
242 "cn=ldapsync,%s", lp_ldap_suffix());
244 if (!suffix) {
245 ntstatus = NT_STATUS_NO_MEMORY;
246 goto done;
250 rc = smbldap_search(ldap_state->smbldap_state, suffix,
251 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
253 if (rc != LDAP_SUCCESS) {
254 goto done;
257 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
258 if (num_result != 1) {
259 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
260 goto done;
263 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
264 if (entry == NULL) {
265 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
266 goto done;
269 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
270 if (values == NULL) {
271 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
272 goto done;
275 num_values = ldap_count_values(values);
276 if (num_values == 0) {
277 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
278 goto done;
281 p = values[0];
282 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
283 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
284 goto done;
287 p = tok;
288 if (!strncmp(p, "csn=", strlen("csn=")))
289 p += strlen("csn=");
291 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
293 *seq_num = generalized_to_unix_time(p);
295 /* very basic sanity check */
296 if (*seq_num <= 0) {
297 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
298 (int)*seq_num));
299 goto done;
302 ntstatus = NT_STATUS_OK;
304 done:
305 if (values != NULL)
306 ldap_value_free(values);
307 if (msg != NULL)
308 ldap_msgfree(msg);
309 if (mem_ctx)
310 talloc_destroy(mem_ctx);
312 return ntstatus;
315 /*******************************************************************
316 Run the search by name.
317 ******************************************************************/
319 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
320 const char *user,
321 LDAPMessage ** result,
322 const char **attr)
324 char *filter = NULL;
325 char *escape_user = escape_ldap_string(talloc_tos(), user);
326 int ret = -1;
328 if (!escape_user) {
329 return LDAP_NO_MEMORY;
333 * in the filter expression, replace %u with the real name
334 * so in ldap filter, %u MUST exist :-)
336 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
337 get_objclass_filter(ldap_state->schema_ver));
338 if (!filter) {
339 TALLOC_FREE(escape_user);
340 return LDAP_NO_MEMORY;
343 * have to use this here because $ is filtered out
344 * in string_sub
347 filter = talloc_all_string_sub(talloc_tos(),
348 filter, "%u", escape_user);
349 TALLOC_FREE(escape_user);
350 if (!filter) {
351 return LDAP_NO_MEMORY;
354 ret = smbldap_search_suffix(ldap_state->smbldap_state,
355 filter, attr, result);
356 TALLOC_FREE(filter);
357 return ret;
360 /*******************************************************************
361 Run the search by rid.
362 ******************************************************************/
364 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
365 uint32_t rid, LDAPMessage ** result,
366 const char **attr)
368 char *filter = NULL;
369 int rc;
371 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
372 get_objclass_filter(ldap_state->schema_ver));
373 if (!filter) {
374 return LDAP_NO_MEMORY;
377 rc = smbldap_search_suffix(ldap_state->smbldap_state,
378 filter, attr, result);
379 TALLOC_FREE(filter);
380 return rc;
383 /*******************************************************************
384 Run the search by SID.
385 ******************************************************************/
387 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
388 const struct dom_sid *sid, LDAPMessage ** result,
389 const char **attr)
391 char *filter = NULL;
392 int rc;
393 fstring sid_string;
395 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
396 get_userattr_key2string(ldap_state->schema_ver,
397 LDAP_ATTR_USER_SID),
398 sid_to_fstring(sid_string, sid),
399 get_objclass_filter(ldap_state->schema_ver));
400 if (!filter) {
401 return LDAP_NO_MEMORY;
404 rc = smbldap_search_suffix(ldap_state->smbldap_state,
405 filter, attr, result);
407 TALLOC_FREE(filter);
408 return rc;
411 /*******************************************************************
412 Delete complete object or objectclass and attrs from
413 object found in search_result depending on lp_ldap_delete_dn
414 ******************************************************************/
416 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
417 TALLOC_CTX *mem_ctx,
418 LDAPMessage *entry,
419 const char *objectclass,
420 const char **attrs)
422 LDAPMod **mods = NULL;
423 char *name;
424 const char *dn;
425 BerElement *ptr = NULL;
427 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
428 if (dn == NULL) {
429 return LDAP_NO_MEMORY;
432 if (lp_ldap_delete_dn()) {
433 return smbldap_delete(priv->smbldap_state, dn);
436 /* Ok, delete only the SAM attributes */
438 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
439 name != NULL;
440 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
441 const char **attrib;
443 /* We are only allowed to delete the attributes that
444 really exist. */
446 for (attrib = attrs; *attrib != NULL; attrib++) {
447 if (strequal(*attrib, name)) {
448 DEBUG(10, ("ldapsam_delete_entry: deleting "
449 "attribute %s\n", name));
450 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
451 NULL);
454 ldap_memfree(name);
457 if (ptr != NULL) {
458 ber_free(ptr, 0);
461 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
462 talloc_autofree_ldapmod(mem_ctx, mods);
464 return smbldap_modify(priv->smbldap_state, dn, mods);
467 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
469 char *temp;
470 struct tm tm;
472 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
473 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
474 talloc_tos());
475 if (!temp) {
476 return (time_t) 0;
479 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
480 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
481 (char*)temp));
482 TALLOC_FREE(temp);
483 return (time_t) 0;
485 TALLOC_FREE(temp);
486 tzset();
487 return timegm(&tm);
490 /**********************************************************************
491 Initialize struct samu from an LDAP query.
492 (Based on init_sam_from_buffer in pdb_tdb.c)
493 *********************************************************************/
495 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
496 struct samu * sampass,
497 LDAPMessage * entry)
499 time_t logon_time,
500 logoff_time,
501 kickoff_time,
502 pass_last_set_time,
503 pass_can_change_time,
504 ldap_entry_time,
505 bad_password_time;
506 char *username = NULL,
507 *domain = NULL,
508 *nt_username = NULL,
509 *fullname = NULL,
510 *homedir = NULL,
511 *dir_drive = NULL,
512 *logon_script = NULL,
513 *profile_path = NULL,
514 *acct_desc = NULL,
515 *workstations = NULL,
516 *munged_dial = NULL;
517 uint32_t user_rid;
518 uint8 smblmpwd[LM_HASH_LEN],
519 smbntpwd[NT_HASH_LEN];
520 bool use_samba_attrs = True;
521 uint32_t acct_ctrl = 0;
522 uint16_t logon_divs;
523 uint16_t bad_password_count = 0,
524 logon_count = 0;
525 uint32_t hours_len;
526 uint8 hours[MAX_HOURS_LEN];
527 char *temp = NULL;
528 struct login_cache cache_entry;
529 uint32_t pwHistLen;
530 bool expand_explicit = lp_passdb_expand_explicit();
531 bool ret = false;
532 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
534 if (!ctx) {
535 return false;
537 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
538 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
539 goto fn_exit;
542 if (priv2ld(ldap_state) == NULL) {
543 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
544 "ldap_struct is NULL!\n"));
545 goto fn_exit;
548 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
549 entry,
550 "uid",
551 ctx))) {
552 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
553 "this user!\n"));
554 goto fn_exit;
557 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
559 nt_username = talloc_strdup(ctx, username);
560 if (!nt_username) {
561 goto fn_exit;
564 domain = talloc_strdup(ctx, ldap_state->domain_name);
565 if (!domain) {
566 goto fn_exit;
569 pdb_set_username(sampass, username, PDB_SET);
571 pdb_set_domain(sampass, domain, PDB_DEFAULT);
572 pdb_set_nt_username(sampass, nt_username, PDB_SET);
574 /* deal with different attributes between the schema first */
576 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
577 if ((temp = smbldap_talloc_single_attribute(
578 ldap_state->smbldap_state->ldap_struct,
579 entry,
580 get_userattr_key2string(ldap_state->schema_ver,
581 LDAP_ATTR_USER_SID),
582 ctx))!=NULL) {
583 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
585 } else {
586 if ((temp = smbldap_talloc_single_attribute(
587 ldap_state->smbldap_state->ldap_struct,
588 entry,
589 get_userattr_key2string(ldap_state->schema_ver,
590 LDAP_ATTR_USER_RID),
591 ctx))!=NULL) {
592 user_rid = (uint32_t)atol(temp);
593 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
597 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
598 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
599 get_userattr_key2string(ldap_state->schema_ver,
600 LDAP_ATTR_USER_SID),
601 get_userattr_key2string(ldap_state->schema_ver,
602 LDAP_ATTR_USER_RID),
603 username));
604 return False;
607 temp = smbldap_talloc_single_attribute(
608 ldap_state->smbldap_state->ldap_struct,
609 entry,
610 get_userattr_key2string(ldap_state->schema_ver,
611 LDAP_ATTR_PWD_LAST_SET),
612 ctx);
613 if (temp) {
614 pass_last_set_time = (time_t) atol(temp);
615 pdb_set_pass_last_set_time(sampass,
616 pass_last_set_time, PDB_SET);
619 temp = smbldap_talloc_single_attribute(
620 ldap_state->smbldap_state->ldap_struct,
621 entry,
622 get_userattr_key2string(ldap_state->schema_ver,
623 LDAP_ATTR_LOGON_TIME),
624 ctx);
625 if (temp) {
626 logon_time = (time_t) atol(temp);
627 pdb_set_logon_time(sampass, logon_time, PDB_SET);
630 temp = smbldap_talloc_single_attribute(
631 ldap_state->smbldap_state->ldap_struct,
632 entry,
633 get_userattr_key2string(ldap_state->schema_ver,
634 LDAP_ATTR_LOGOFF_TIME),
635 ctx);
636 if (temp) {
637 logoff_time = (time_t) atol(temp);
638 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
641 temp = smbldap_talloc_single_attribute(
642 ldap_state->smbldap_state->ldap_struct,
643 entry,
644 get_userattr_key2string(ldap_state->schema_ver,
645 LDAP_ATTR_KICKOFF_TIME),
646 ctx);
647 if (temp) {
648 kickoff_time = (time_t) atol(temp);
649 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
652 temp = smbldap_talloc_single_attribute(
653 ldap_state->smbldap_state->ldap_struct,
654 entry,
655 get_userattr_key2string(ldap_state->schema_ver,
656 LDAP_ATTR_PWD_CAN_CHANGE),
657 ctx);
658 if (temp) {
659 pass_can_change_time = (time_t) atol(temp);
660 pdb_set_pass_can_change_time(sampass,
661 pass_can_change_time, PDB_SET);
664 /* recommend that 'gecos' and 'displayName' should refer to the same
665 * attribute OID. userFullName depreciated, only used by Samba
666 * primary rules of LDAP: don't make a new attribute when one is already defined
667 * that fits your needs; using cn then displayName rather than 'userFullName'
670 fullname = smbldap_talloc_single_attribute(
671 ldap_state->smbldap_state->ldap_struct,
672 entry,
673 get_userattr_key2string(ldap_state->schema_ver,
674 LDAP_ATTR_DISPLAY_NAME),
675 ctx);
676 if (fullname) {
677 pdb_set_fullname(sampass, fullname, PDB_SET);
678 } else {
679 fullname = smbldap_talloc_single_attribute(
680 ldap_state->smbldap_state->ldap_struct,
681 entry,
682 get_userattr_key2string(ldap_state->schema_ver,
683 LDAP_ATTR_CN),
684 ctx);
685 if (fullname) {
686 pdb_set_fullname(sampass, fullname, PDB_SET);
690 dir_drive = smbldap_talloc_single_attribute(
691 ldap_state->smbldap_state->ldap_struct,
692 entry,
693 get_userattr_key2string(ldap_state->schema_ver,
694 LDAP_ATTR_HOME_DRIVE),
695 ctx);
696 if (dir_drive) {
697 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
698 } else {
699 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
702 homedir = smbldap_talloc_single_attribute(
703 ldap_state->smbldap_state->ldap_struct,
704 entry,
705 get_userattr_key2string(ldap_state->schema_ver,
706 LDAP_ATTR_HOME_PATH),
707 ctx);
708 if (homedir) {
709 if (expand_explicit) {
710 homedir = talloc_sub_basic(ctx,
711 username,
712 domain,
713 homedir);
714 if (!homedir) {
715 goto fn_exit;
718 pdb_set_homedir(sampass, homedir, PDB_SET);
719 } else {
720 pdb_set_homedir(sampass,
721 talloc_sub_basic(ctx, username, domain,
722 lp_logon_home()),
723 PDB_DEFAULT);
726 logon_script = smbldap_talloc_single_attribute(
727 ldap_state->smbldap_state->ldap_struct,
728 entry,
729 get_userattr_key2string(ldap_state->schema_ver,
730 LDAP_ATTR_LOGON_SCRIPT),
731 ctx);
732 if (logon_script) {
733 if (expand_explicit) {
734 logon_script = talloc_sub_basic(ctx,
735 username,
736 domain,
737 logon_script);
738 if (!logon_script) {
739 goto fn_exit;
742 pdb_set_logon_script(sampass, logon_script, PDB_SET);
743 } else {
744 pdb_set_logon_script(sampass,
745 talloc_sub_basic(ctx, username, domain,
746 lp_logon_script()),
747 PDB_DEFAULT );
750 profile_path = smbldap_talloc_single_attribute(
751 ldap_state->smbldap_state->ldap_struct,
752 entry,
753 get_userattr_key2string(ldap_state->schema_ver,
754 LDAP_ATTR_PROFILE_PATH),
755 ctx);
756 if (profile_path) {
757 if (expand_explicit) {
758 profile_path = talloc_sub_basic(ctx,
759 username,
760 domain,
761 profile_path);
762 if (!profile_path) {
763 goto fn_exit;
766 pdb_set_profile_path(sampass, profile_path, PDB_SET);
767 } else {
768 pdb_set_profile_path(sampass,
769 talloc_sub_basic(ctx, username, domain,
770 lp_logon_path()),
771 PDB_DEFAULT );
774 acct_desc = smbldap_talloc_single_attribute(
775 ldap_state->smbldap_state->ldap_struct,
776 entry,
777 get_userattr_key2string(ldap_state->schema_ver,
778 LDAP_ATTR_DESC),
779 ctx);
780 if (acct_desc) {
781 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
784 workstations = smbldap_talloc_single_attribute(
785 ldap_state->smbldap_state->ldap_struct,
786 entry,
787 get_userattr_key2string(ldap_state->schema_ver,
788 LDAP_ATTR_USER_WKS),
789 ctx);
790 if (workstations) {
791 pdb_set_workstations(sampass, workstations, PDB_SET);
794 munged_dial = smbldap_talloc_single_attribute(
795 ldap_state->smbldap_state->ldap_struct,
796 entry,
797 get_userattr_key2string(ldap_state->schema_ver,
798 LDAP_ATTR_MUNGED_DIAL),
799 ctx);
800 if (munged_dial) {
801 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
804 /* FIXME: hours stuff should be cleaner */
806 logon_divs = 168;
807 hours_len = 21;
808 memset(hours, 0xff, hours_len);
810 if (ldap_state->is_nds_ldap) {
811 char *user_dn;
812 size_t pwd_len;
813 char clear_text_pw[512];
815 /* Make call to Novell eDirectory ldap extension to get clear text password.
816 NOTE: This will only work if we have an SSL connection to eDirectory. */
817 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
818 if (user_dn != NULL) {
819 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
821 pwd_len = sizeof(clear_text_pw);
822 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
823 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
824 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
825 TALLOC_FREE(user_dn);
826 return False;
828 ZERO_STRUCT(smblmpwd);
829 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
830 TALLOC_FREE(user_dn);
831 return False;
833 ZERO_STRUCT(smbntpwd);
834 use_samba_attrs = False;
837 TALLOC_FREE(user_dn);
839 } else {
840 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
844 if (use_samba_attrs) {
845 temp = smbldap_talloc_single_attribute(
846 ldap_state->smbldap_state->ldap_struct,
847 entry,
848 get_userattr_key2string(ldap_state->schema_ver,
849 LDAP_ATTR_LMPW),
850 ctx);
851 if (temp) {
852 pdb_gethexpwd(temp, smblmpwd);
853 memset((char *)temp, '\0', strlen(temp)+1);
854 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
855 goto fn_exit;
857 ZERO_STRUCT(smblmpwd);
860 temp = smbldap_talloc_single_attribute(
861 ldap_state->smbldap_state->ldap_struct,
862 entry,
863 get_userattr_key2string(ldap_state->schema_ver,
864 LDAP_ATTR_NTPW),
865 ctx);
866 if (temp) {
867 pdb_gethexpwd(temp, smbntpwd);
868 memset((char *)temp, '\0', strlen(temp)+1);
869 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
870 goto fn_exit;
872 ZERO_STRUCT(smbntpwd);
876 pwHistLen = 0;
878 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
879 if (pwHistLen > 0){
880 uint8 *pwhist = NULL;
881 int i;
882 char *history_string = talloc_array(ctx, char,
883 MAX_PW_HISTORY_LEN*64);
885 if (!history_string) {
886 goto fn_exit;
889 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
891 pwhist = talloc_array(ctx, uint8,
892 pwHistLen * PW_HISTORY_ENTRY_LEN);
893 if (pwhist == NULL) {
894 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
895 goto fn_exit;
897 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
899 if (smbldap_get_single_attribute(
900 ldap_state->smbldap_state->ldap_struct,
901 entry,
902 get_userattr_key2string(ldap_state->schema_ver,
903 LDAP_ATTR_PWD_HISTORY),
904 history_string,
905 MAX_PW_HISTORY_LEN*64)) {
906 bool hex_failed = false;
907 for (i = 0; i < pwHistLen; i++){
908 /* Get the 16 byte salt. */
909 if (!pdb_gethexpwd(&history_string[i*64],
910 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
911 hex_failed = true;
912 break;
914 /* Get the 16 byte MD5 hash of salt+passwd. */
915 if (!pdb_gethexpwd(&history_string[(i*64)+32],
916 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
917 PW_HISTORY_SALT_LEN])) {
918 hex_failed = True;
919 break;
922 if (hex_failed) {
923 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
924 username));
925 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
928 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
929 goto fn_exit;
933 temp = smbldap_talloc_single_attribute(
934 ldap_state->smbldap_state->ldap_struct,
935 entry,
936 get_userattr_key2string(ldap_state->schema_ver,
937 LDAP_ATTR_ACB_INFO),
938 ctx);
939 if (temp) {
940 acct_ctrl = pdb_decode_acct_ctrl(temp);
942 if (acct_ctrl == 0) {
943 acct_ctrl |= ACB_NORMAL;
946 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
947 } else {
948 acct_ctrl |= ACB_NORMAL;
951 pdb_set_hours_len(sampass, hours_len, PDB_SET);
952 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
954 temp = smbldap_talloc_single_attribute(
955 ldap_state->smbldap_state->ldap_struct,
956 entry,
957 get_userattr_key2string(ldap_state->schema_ver,
958 LDAP_ATTR_BAD_PASSWORD_COUNT),
959 ctx);
960 if (temp) {
961 bad_password_count = (uint32_t) atol(temp);
962 pdb_set_bad_password_count(sampass,
963 bad_password_count, PDB_SET);
966 temp = smbldap_talloc_single_attribute(
967 ldap_state->smbldap_state->ldap_struct,
968 entry,
969 get_userattr_key2string(ldap_state->schema_ver,
970 LDAP_ATTR_BAD_PASSWORD_TIME),
971 ctx);
972 if (temp) {
973 bad_password_time = (time_t) atol(temp);
974 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
978 temp = smbldap_talloc_single_attribute(
979 ldap_state->smbldap_state->ldap_struct,
980 entry,
981 get_userattr_key2string(ldap_state->schema_ver,
982 LDAP_ATTR_LOGON_COUNT),
983 ctx);
984 if (temp) {
985 logon_count = (uint32_t) atol(temp);
986 pdb_set_logon_count(sampass, logon_count, PDB_SET);
989 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
991 temp = smbldap_talloc_single_attribute(
992 ldap_state->smbldap_state->ldap_struct,
993 entry,
994 get_userattr_key2string(ldap_state->schema_ver,
995 LDAP_ATTR_LOGON_HOURS),
996 ctx);
997 if (temp) {
998 pdb_gethexhours(temp, hours);
999 memset((char *)temp, '\0', strlen(temp) +1);
1000 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
1001 ZERO_STRUCT(hours);
1004 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1005 struct passwd unix_pw;
1006 bool have_uid = false;
1007 bool have_gid = false;
1008 struct dom_sid mapped_gsid;
1009 const struct dom_sid *primary_gsid;
1011 ZERO_STRUCT(unix_pw);
1013 unix_pw.pw_name = username;
1014 unix_pw.pw_passwd = discard_const_p(char, "x");
1016 temp = smbldap_talloc_single_attribute(
1017 priv2ld(ldap_state),
1018 entry,
1019 "uidNumber",
1020 ctx);
1021 if (temp) {
1022 /* We've got a uid, feed the cache */
1023 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1024 have_uid = true;
1026 temp = smbldap_talloc_single_attribute(
1027 priv2ld(ldap_state),
1028 entry,
1029 "gidNumber",
1030 ctx);
1031 if (temp) {
1032 /* We've got a uid, feed the cache */
1033 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1034 have_gid = true;
1036 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state),
1038 entry,
1039 "gecos",
1040 ctx);
1041 if (unix_pw.pw_gecos) {
1042 unix_pw.pw_gecos = fullname;
1044 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1045 priv2ld(ldap_state),
1046 entry,
1047 "homeDirectory",
1048 ctx);
1049 if (unix_pw.pw_dir) {
1050 unix_pw.pw_dir = discard_const_p(char, "");
1052 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1053 priv2ld(ldap_state),
1054 entry,
1055 "loginShell",
1056 ctx);
1057 if (unix_pw.pw_shell) {
1058 unix_pw.pw_shell = discard_const_p(char, "");
1061 if (have_uid && have_gid) {
1062 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1063 } else {
1064 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1067 if (sampass->unix_pw == NULL) {
1068 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1069 pdb_get_username(sampass)));
1070 goto fn_exit;
1073 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
1074 sampass->unix_pw->pw_uid);
1076 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1077 primary_gsid = pdb_get_group_sid(sampass);
1078 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1079 idmap_cache_set_sid2gid(primary_gsid,
1080 sampass->unix_pw->pw_gid);
1084 /* check the timestamp of the cache vs ldap entry */
1085 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1086 entry))) {
1087 ret = true;
1088 goto fn_exit;
1091 /* see if we have newer updates */
1092 if (!login_cache_read(sampass, &cache_entry)) {
1093 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1094 (unsigned int)pdb_get_bad_password_count(sampass),
1095 (unsigned int)pdb_get_bad_password_time(sampass)));
1096 ret = true;
1097 goto fn_exit;
1100 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1101 (unsigned int)ldap_entry_time,
1102 (unsigned int)cache_entry.entry_timestamp,
1103 (unsigned int)cache_entry.bad_password_time));
1105 if (ldap_entry_time > cache_entry.entry_timestamp) {
1106 /* cache is older than directory , so
1107 we need to delete the entry but allow the
1108 fields to be written out */
1109 login_cache_delentry(sampass);
1110 } else {
1111 /* read cache in */
1112 pdb_set_acct_ctrl(sampass,
1113 pdb_get_acct_ctrl(sampass) |
1114 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1115 PDB_SET);
1116 pdb_set_bad_password_count(sampass,
1117 cache_entry.bad_password_count,
1118 PDB_SET);
1119 pdb_set_bad_password_time(sampass,
1120 cache_entry.bad_password_time,
1121 PDB_SET);
1124 ret = true;
1126 fn_exit:
1128 TALLOC_FREE(ctx);
1129 return ret;
1132 /**********************************************************************
1133 Initialize the ldap db from a struct samu. Called on update.
1134 (Based on init_buffer_from_sam in pdb_tdb.c)
1135 *********************************************************************/
1137 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1138 LDAPMessage *existing,
1139 LDAPMod *** mods, struct samu * sampass,
1140 bool (*need_update)(const struct samu *,
1141 enum pdb_elements))
1143 char *temp = NULL;
1144 uint32_t rid;
1146 if (mods == NULL || sampass == NULL) {
1147 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1148 return False;
1151 *mods = NULL;
1154 * took out adding "objectclass: sambaAccount"
1155 * do this on a per-mod basis
1157 if (need_update(sampass, PDB_USERNAME)) {
1158 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1159 "uid", pdb_get_username(sampass));
1160 if (ldap_state->is_nds_ldap) {
1161 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1162 "cn", pdb_get_username(sampass));
1163 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1164 "sn", pdb_get_username(sampass));
1168 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1170 /* only update the RID if we actually need to */
1171 if (need_update(sampass, PDB_USERSID)) {
1172 fstring sid_string;
1173 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1175 switch ( ldap_state->schema_ver ) {
1176 case SCHEMAVER_SAMBAACCOUNT:
1177 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1178 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1179 sid_string_dbg(user_sid),
1180 sid_string_dbg(
1181 &ldap_state->domain_sid)));
1182 return False;
1184 if (asprintf(&temp, "%i", rid) < 0) {
1185 return false;
1187 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1188 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1189 temp);
1190 SAFE_FREE(temp);
1191 break;
1193 case SCHEMAVER_SAMBASAMACCOUNT:
1194 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1195 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1196 sid_to_fstring(sid_string, user_sid));
1197 break;
1199 default:
1200 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1201 break;
1205 /* we don't need to store the primary group RID - so leaving it
1206 'free' to hang off the unix primary group makes life easier */
1208 if (need_update(sampass, PDB_GROUPSID)) {
1209 fstring sid_string;
1210 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1212 switch ( ldap_state->schema_ver ) {
1213 case SCHEMAVER_SAMBAACCOUNT:
1214 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1215 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1216 sid_string_dbg(group_sid),
1217 sid_string_dbg(
1218 &ldap_state->domain_sid)));
1219 return False;
1222 if (asprintf(&temp, "%i", rid) < 0) {
1223 return false;
1225 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1226 get_userattr_key2string(ldap_state->schema_ver,
1227 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1228 SAFE_FREE(temp);
1229 break;
1231 case SCHEMAVER_SAMBASAMACCOUNT:
1232 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1233 get_userattr_key2string(ldap_state->schema_ver,
1234 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1235 break;
1237 default:
1238 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1239 break;
1244 /* displayName, cn, and gecos should all be the same
1245 * most easily accomplished by giving them the same OID
1246 * gecos isn't set here b/c it should be handled by the
1247 * add-user script
1248 * We change displayName only and fall back to cn if
1249 * it does not exist.
1252 if (need_update(sampass, PDB_FULLNAME))
1253 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1254 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1255 pdb_get_fullname(sampass));
1257 if (need_update(sampass, PDB_ACCTDESC))
1258 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1259 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1260 pdb_get_acct_desc(sampass));
1262 if (need_update(sampass, PDB_WORKSTATIONS))
1263 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1264 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1265 pdb_get_workstations(sampass));
1267 if (need_update(sampass, PDB_MUNGEDDIAL))
1268 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1269 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1270 pdb_get_munged_dial(sampass));
1272 if (need_update(sampass, PDB_SMBHOME))
1273 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1274 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1275 pdb_get_homedir(sampass));
1277 if (need_update(sampass, PDB_DRIVE))
1278 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1279 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1280 pdb_get_dir_drive(sampass));
1282 if (need_update(sampass, PDB_LOGONSCRIPT))
1283 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1284 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1285 pdb_get_logon_script(sampass));
1287 if (need_update(sampass, PDB_PROFILE))
1288 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1289 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1290 pdb_get_profile_path(sampass));
1292 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1293 return false;
1295 if (need_update(sampass, PDB_LOGONTIME))
1296 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1297 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1298 SAFE_FREE(temp);
1300 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1301 return false;
1303 if (need_update(sampass, PDB_LOGOFFTIME))
1304 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1305 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1306 SAFE_FREE(temp);
1308 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1309 return false;
1311 if (need_update(sampass, PDB_KICKOFFTIME))
1312 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1313 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1314 SAFE_FREE(temp);
1316 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1317 return false;
1319 if (need_update(sampass, PDB_CANCHANGETIME))
1320 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1321 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1322 SAFE_FREE(temp);
1324 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1325 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1327 if (need_update(sampass, PDB_LMPASSWD)) {
1328 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1329 if (lm_pw) {
1330 char pwstr[34];
1331 pdb_sethexpwd(pwstr, lm_pw,
1332 pdb_get_acct_ctrl(sampass));
1333 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1334 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1335 pwstr);
1336 } else {
1337 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1338 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1339 NULL);
1342 if (need_update(sampass, PDB_NTPASSWD)) {
1343 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1344 if (nt_pw) {
1345 char pwstr[34];
1346 pdb_sethexpwd(pwstr, nt_pw,
1347 pdb_get_acct_ctrl(sampass));
1348 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1349 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1350 pwstr);
1351 } else {
1352 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1353 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1354 NULL);
1358 if (need_update(sampass, PDB_PWHISTORY)) {
1359 char *pwstr = NULL;
1360 uint32_t pwHistLen = 0;
1361 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1363 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1364 if (!pwstr) {
1365 return false;
1367 if (pwHistLen == 0) {
1368 /* Remove any password history from the LDAP store. */
1369 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1370 pwstr[64] = '\0';
1371 } else {
1372 int i;
1373 uint32_t currHistLen = 0;
1374 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1375 if (pwhist != NULL) {
1376 /* We can only store (1024-1/64 password history entries. */
1377 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1378 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1379 /* Store the salt. */
1380 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1381 /* Followed by the md5 hash of salt + md4 hash */
1382 pdb_sethexpwd(&pwstr[(i*64)+32],
1383 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1384 DEBUG(100, ("pwstr=%s\n", pwstr));
1388 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1389 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1390 pwstr);
1391 SAFE_FREE(pwstr);
1394 if (need_update(sampass, PDB_PASSLASTSET)) {
1395 if (asprintf(&temp, "%li",
1396 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1397 return false;
1399 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1400 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1401 temp);
1402 SAFE_FREE(temp);
1406 if (need_update(sampass, PDB_HOURS)) {
1407 const uint8 *hours = pdb_get_hours(sampass);
1408 if (hours) {
1409 char hourstr[44];
1410 pdb_sethexhours(hourstr, hours);
1411 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1412 existing,
1413 mods,
1414 get_userattr_key2string(ldap_state->schema_ver,
1415 LDAP_ATTR_LOGON_HOURS),
1416 hourstr);
1420 if (need_update(sampass, PDB_ACCTCTRL))
1421 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1422 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1423 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1425 /* password lockout cache:
1426 - If we are now autolocking or clearing, we write to ldap
1427 - If we are clearing, we delete the cache entry
1428 - If the count is > 0, we update the cache
1430 This even means when autolocking, we cache, just in case the
1431 update doesn't work, and we have to cache the autolock flag */
1433 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1434 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1435 uint16_t badcount = pdb_get_bad_password_count(sampass);
1436 time_t badtime = pdb_get_bad_password_time(sampass);
1437 uint32_t pol;
1438 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1440 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1441 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1443 if ((badcount >= pol) || (badcount == 0)) {
1444 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1445 (unsigned int)badcount, (unsigned int)badtime));
1446 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1447 return false;
1449 smbldap_make_mod(
1450 ldap_state->smbldap_state->ldap_struct,
1451 existing, mods,
1452 get_userattr_key2string(
1453 ldap_state->schema_ver,
1454 LDAP_ATTR_BAD_PASSWORD_COUNT),
1455 temp);
1456 SAFE_FREE(temp);
1458 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1459 return false;
1461 smbldap_make_mod(
1462 ldap_state->smbldap_state->ldap_struct,
1463 existing, mods,
1464 get_userattr_key2string(
1465 ldap_state->schema_ver,
1466 LDAP_ATTR_BAD_PASSWORD_TIME),
1467 temp);
1468 SAFE_FREE(temp);
1470 if (badcount == 0) {
1471 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1472 login_cache_delentry(sampass);
1473 } else {
1474 struct login_cache cache_entry;
1476 cache_entry.entry_timestamp = time(NULL);
1477 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1478 cache_entry.bad_password_count = badcount;
1479 cache_entry.bad_password_time = badtime;
1481 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1482 login_cache_write(sampass, &cache_entry);
1486 return True;
1489 /**********************************************************************
1490 End enumeration of the LDAP password list.
1491 *********************************************************************/
1493 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1495 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1496 if (ldap_state->result) {
1497 ldap_msgfree(ldap_state->result);
1498 ldap_state->result = NULL;
1502 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1503 const char *new_attr)
1505 int i;
1507 if (new_attr == NULL) {
1508 return;
1511 for (i=0; (*attr_list)[i] != NULL; i++) {
1515 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1516 const char *, i+2);
1517 SMB_ASSERT((*attr_list) != NULL);
1518 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1519 (*attr_list)[i+1] = NULL;
1522 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1523 const char ***attr_list)
1525 append_attr(mem_ctx, attr_list, "uidNumber");
1526 append_attr(mem_ctx, attr_list, "gidNumber");
1527 append_attr(mem_ctx, attr_list, "homeDirectory");
1528 append_attr(mem_ctx, attr_list, "loginShell");
1529 append_attr(mem_ctx, attr_list, "gecos");
1532 /**********************************************************************
1533 Get struct samu entry from LDAP by username.
1534 *********************************************************************/
1536 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1538 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1539 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1540 LDAPMessage *result = NULL;
1541 LDAPMessage *entry = NULL;
1542 int count;
1543 const char ** attr_list;
1544 int rc;
1546 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1547 append_attr(user, &attr_list,
1548 get_userattr_key2string(ldap_state->schema_ver,
1549 LDAP_ATTR_MOD_TIMESTAMP));
1550 ldapsam_add_unix_attributes(user, &attr_list);
1551 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1552 attr_list);
1553 TALLOC_FREE( attr_list );
1555 if ( rc != LDAP_SUCCESS )
1556 return NT_STATUS_NO_SUCH_USER;
1558 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1560 if (count < 1) {
1561 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1562 ldap_msgfree(result);
1563 return NT_STATUS_NO_SUCH_USER;
1564 } else if (count > 1) {
1565 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1566 ldap_msgfree(result);
1567 return NT_STATUS_NO_SUCH_USER;
1570 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1571 if (entry) {
1572 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1573 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1574 ldap_msgfree(result);
1575 return NT_STATUS_NO_SUCH_USER;
1577 pdb_set_backend_private_data(user, result, NULL,
1578 my_methods, PDB_CHANGED);
1579 talloc_autofree_ldapmsg(user, result);
1580 ret = NT_STATUS_OK;
1581 } else {
1582 ldap_msgfree(result);
1584 return ret;
1587 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1588 const struct dom_sid *sid, LDAPMessage **result)
1590 int rc = -1;
1591 const char ** attr_list;
1592 uint32_t rid;
1594 switch ( ldap_state->schema_ver ) {
1595 case SCHEMAVER_SAMBASAMACCOUNT: {
1596 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1597 if (tmp_ctx == NULL) {
1598 return LDAP_NO_MEMORY;
1601 attr_list = get_userattr_list(tmp_ctx,
1602 ldap_state->schema_ver);
1603 append_attr(tmp_ctx, &attr_list,
1604 get_userattr_key2string(
1605 ldap_state->schema_ver,
1606 LDAP_ATTR_MOD_TIMESTAMP));
1607 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1608 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1609 result, attr_list);
1610 TALLOC_FREE(tmp_ctx);
1612 if ( rc != LDAP_SUCCESS )
1613 return rc;
1614 break;
1617 case SCHEMAVER_SAMBAACCOUNT:
1618 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1619 return rc;
1622 attr_list = get_userattr_list(NULL,
1623 ldap_state->schema_ver);
1624 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1625 TALLOC_FREE( attr_list );
1627 if ( rc != LDAP_SUCCESS )
1628 return rc;
1629 break;
1631 return rc;
1634 /**********************************************************************
1635 Get struct samu entry from LDAP by SID.
1636 *********************************************************************/
1638 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1640 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1641 LDAPMessage *result = NULL;
1642 LDAPMessage *entry = NULL;
1643 int count;
1644 int rc;
1646 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1647 sid, &result);
1648 if (rc != LDAP_SUCCESS)
1649 return NT_STATUS_NO_SUCH_USER;
1651 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1653 if (count < 1) {
1654 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1655 "count=%d\n", sid_string_dbg(sid), count));
1656 ldap_msgfree(result);
1657 return NT_STATUS_NO_SUCH_USER;
1658 } else if (count > 1) {
1659 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1660 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1661 count));
1662 ldap_msgfree(result);
1663 return NT_STATUS_NO_SUCH_USER;
1666 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1667 if (!entry) {
1668 ldap_msgfree(result);
1669 return NT_STATUS_NO_SUCH_USER;
1672 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1673 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1674 ldap_msgfree(result);
1675 return NT_STATUS_NO_SUCH_USER;
1678 pdb_set_backend_private_data(user, result, NULL,
1679 my_methods, PDB_CHANGED);
1680 talloc_autofree_ldapmsg(user, result);
1681 return NT_STATUS_OK;
1684 /********************************************************************
1685 Do the actual modification - also change a plaintext passord if
1686 it it set.
1687 **********************************************************************/
1689 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1690 struct samu *newpwd, char *dn,
1691 LDAPMod **mods, int ldap_op,
1692 bool (*need_update)(const struct samu *, enum pdb_elements))
1694 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1695 int rc;
1697 if (!newpwd || !dn) {
1698 return NT_STATUS_INVALID_PARAMETER;
1701 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1702 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1703 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1704 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1705 BerElement *ber;
1706 struct berval *bv;
1707 char *retoid = NULL;
1708 struct berval *retdata = NULL;
1709 char *utf8_password;
1710 char *utf8_dn;
1711 size_t converted_size;
1712 int ret;
1714 if (!ldap_state->is_nds_ldap) {
1716 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1717 LDAP_EXOP_MODIFY_PASSWD)) {
1718 DEBUG(2, ("ldap password change requested, but LDAP "
1719 "server does not support it -- ignoring\n"));
1720 return NT_STATUS_OK;
1724 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1725 pdb_get_plaintext_passwd(newpwd),
1726 &converted_size))
1728 return NT_STATUS_NO_MEMORY;
1731 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1732 TALLOC_FREE(utf8_password);
1733 return NT_STATUS_NO_MEMORY;
1736 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1737 DEBUG(0,("ber_alloc_t returns NULL\n"));
1738 TALLOC_FREE(utf8_password);
1739 TALLOC_FREE(utf8_dn);
1740 return NT_STATUS_UNSUCCESSFUL;
1743 if ((ber_printf (ber, "{") < 0) ||
1744 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1745 utf8_dn) < 0)) {
1746 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1747 "value <0\n"));
1748 ber_free(ber,1);
1749 TALLOC_FREE(utf8_dn);
1750 TALLOC_FREE(utf8_password);
1751 return NT_STATUS_UNSUCCESSFUL;
1754 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1755 ret = ber_printf(ber, "ts}",
1756 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1757 utf8_password);
1758 } else {
1759 ret = ber_printf(ber, "}");
1762 if (ret < 0) {
1763 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1764 "value <0\n"));
1765 ber_free(ber,1);
1766 TALLOC_FREE(utf8_dn);
1767 TALLOC_FREE(utf8_password);
1768 return NT_STATUS_UNSUCCESSFUL;
1771 if ((rc = ber_flatten (ber, &bv))<0) {
1772 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1773 ber_free(ber,1);
1774 TALLOC_FREE(utf8_dn);
1775 TALLOC_FREE(utf8_password);
1776 return NT_STATUS_UNSUCCESSFUL;
1779 TALLOC_FREE(utf8_dn);
1780 TALLOC_FREE(utf8_password);
1781 ber_free(ber, 1);
1783 if (!ldap_state->is_nds_ldap) {
1784 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1785 LDAP_EXOP_MODIFY_PASSWD,
1786 bv, NULL, NULL, &retoid,
1787 &retdata);
1788 } else {
1789 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1790 pdb_get_plaintext_passwd(newpwd));
1792 if (rc != LDAP_SUCCESS) {
1793 char *ld_error = NULL;
1795 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1796 DEBUG(3, ("Could not set userPassword "
1797 "attribute due to an objectClass "
1798 "violation -- ignoring\n"));
1799 ber_bvfree(bv);
1800 return NT_STATUS_OK;
1803 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1804 &ld_error);
1805 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1806 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1807 SAFE_FREE(ld_error);
1808 ber_bvfree(bv);
1809 #if defined(LDAP_CONSTRAINT_VIOLATION)
1810 if (rc == LDAP_CONSTRAINT_VIOLATION)
1811 return NT_STATUS_PASSWORD_RESTRICTION;
1812 #endif
1813 return NT_STATUS_UNSUCCESSFUL;
1814 } else {
1815 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1816 #ifdef DEBUG_PASSWORD
1817 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1818 #endif
1819 if (retdata)
1820 ber_bvfree(retdata);
1821 if (retoid)
1822 ldap_memfree(retoid);
1824 ber_bvfree(bv);
1827 if (!mods) {
1828 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1829 /* may be password change below however */
1830 } else {
1831 switch(ldap_op) {
1832 case LDAP_MOD_ADD:
1833 if (ldap_state->is_nds_ldap) {
1834 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1835 "objectclass",
1836 "inetOrgPerson");
1837 } else {
1838 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1839 "objectclass",
1840 LDAP_OBJ_ACCOUNT);
1842 rc = smbldap_add(ldap_state->smbldap_state,
1843 dn, mods);
1844 break;
1845 case LDAP_MOD_REPLACE:
1846 rc = smbldap_modify(ldap_state->smbldap_state,
1847 dn ,mods);
1848 break;
1849 default:
1850 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1851 ldap_op));
1852 return NT_STATUS_INVALID_PARAMETER;
1855 if (rc!=LDAP_SUCCESS) {
1856 return NT_STATUS_UNSUCCESSFUL;
1860 return NT_STATUS_OK;
1863 /**********************************************************************
1864 Delete entry from LDAP for username.
1865 *********************************************************************/
1867 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1868 struct samu * sam_acct)
1870 struct ldapsam_privates *priv =
1871 (struct ldapsam_privates *)my_methods->private_data;
1872 const char *sname;
1873 int rc;
1874 LDAPMessage *msg, *entry;
1875 NTSTATUS result = NT_STATUS_NO_MEMORY;
1876 const char **attr_list;
1877 TALLOC_CTX *mem_ctx;
1879 if (!sam_acct) {
1880 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1881 return NT_STATUS_INVALID_PARAMETER;
1884 sname = pdb_get_username(sam_acct);
1886 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1887 "LDAP.\n", sname));
1889 mem_ctx = talloc_new(NULL);
1890 if (mem_ctx == NULL) {
1891 DEBUG(0, ("talloc_new failed\n"));
1892 goto done;
1895 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1896 if (attr_list == NULL) {
1897 goto done;
1900 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1902 if ((rc != LDAP_SUCCESS) ||
1903 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1904 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1905 DEBUG(5, ("Could not find user %s\n", sname));
1906 result = NT_STATUS_NO_SUCH_USER;
1907 goto done;
1910 rc = ldapsam_delete_entry(
1911 priv, mem_ctx, entry,
1912 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1913 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1914 attr_list);
1916 result = (rc == LDAP_SUCCESS) ?
1917 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1919 done:
1920 TALLOC_FREE(mem_ctx);
1921 return result;
1924 /**********************************************************************
1925 Update struct samu.
1926 *********************************************************************/
1928 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1930 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1931 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1932 int rc = 0;
1933 char *dn;
1934 LDAPMessage *result = NULL;
1935 LDAPMessage *entry = NULL;
1936 LDAPMod **mods = NULL;
1937 const char **attr_list;
1939 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1940 if (!result) {
1941 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1942 if (pdb_get_username(newpwd) == NULL) {
1943 return NT_STATUS_INVALID_PARAMETER;
1945 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1946 TALLOC_FREE( attr_list );
1947 if (rc != LDAP_SUCCESS) {
1948 return NT_STATUS_UNSUCCESSFUL;
1950 pdb_set_backend_private_data(newpwd, result, NULL,
1951 my_methods, PDB_CHANGED);
1952 talloc_autofree_ldapmsg(newpwd, result);
1955 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1956 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1957 return NT_STATUS_UNSUCCESSFUL;
1960 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1961 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1962 if (!dn) {
1963 return NT_STATUS_UNSUCCESSFUL;
1966 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1968 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1969 pdb_element_is_changed)) {
1970 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1971 TALLOC_FREE(dn);
1972 if (mods != NULL)
1973 ldap_mods_free(mods,True);
1974 return NT_STATUS_UNSUCCESSFUL;
1977 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1978 && (mods == NULL)) {
1979 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1980 pdb_get_username(newpwd)));
1981 TALLOC_FREE(dn);
1982 return NT_STATUS_OK;
1985 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1987 if (mods != NULL) {
1988 ldap_mods_free(mods,True);
1991 TALLOC_FREE(dn);
1994 * We need to set the backend private data to NULL here. For example
1995 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1996 * same one, and with the explicit delete / add logic for attribute
1997 * values the second time we would use the wrong "old" value which
1998 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1999 * the update.
2000 * The existing LDAPMessage is still being auto-freed by the
2001 * destructor.
2003 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2004 PDB_CHANGED);
2006 if (!NT_STATUS_IS_OK(ret)) {
2007 return ret;
2010 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2011 pdb_get_username(newpwd)));
2012 return NT_STATUS_OK;
2015 /***************************************************************************
2016 Renames a struct samu
2017 - The "rename user script" has full responsibility for changing everything
2018 ***************************************************************************/
2020 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2021 TALLOC_CTX *tmp_ctx,
2022 uint32_t group_rid,
2023 uint32_t member_rid);
2025 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2026 TALLOC_CTX *mem_ctx,
2027 struct samu *user,
2028 struct dom_sid **pp_sids,
2029 gid_t **pp_gids,
2030 uint32_t *p_num_groups);
2032 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2033 struct samu *old_acct,
2034 const char *newname)
2036 const char *oldname;
2037 int rc;
2038 char *rename_script = NULL;
2039 fstring oldname_lower, newname_lower;
2041 if (!old_acct) {
2042 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2043 return NT_STATUS_INVALID_PARAMETER;
2045 if (!newname) {
2046 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2047 return NT_STATUS_INVALID_PARAMETER;
2050 oldname = pdb_get_username(old_acct);
2052 /* rename the posix user */
2053 rename_script = talloc_strdup(talloc_tos(), lp_renameuser_script());
2054 if (rename_script == NULL) {
2055 return NT_STATUS_NO_MEMORY;
2058 if (!(*rename_script)) {
2059 TALLOC_FREE(rename_script);
2060 return NT_STATUS_ACCESS_DENIED;
2063 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2064 oldname, newname));
2066 /* We have to allow the account name to end with a '$'.
2067 Also, follow the semantics in _samr_create_user() and lower case the
2068 posix name but preserve the case in passdb */
2070 fstrcpy( oldname_lower, oldname );
2071 strlower_m( oldname_lower );
2072 fstrcpy( newname_lower, newname );
2073 strlower_m( newname_lower );
2074 rename_script = realloc_string_sub2(rename_script,
2075 "%unew",
2076 newname_lower,
2077 true,
2078 true);
2079 if (!rename_script) {
2080 return NT_STATUS_NO_MEMORY;
2082 rename_script = realloc_string_sub2(rename_script,
2083 "%uold",
2084 oldname_lower,
2085 true,
2086 true);
2087 rc = smbrun(rename_script, NULL);
2089 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2090 rename_script, rc));
2092 TALLOC_FREE(rename_script);
2094 if (rc == 0) {
2095 smb_nscd_flush_user_cache();
2098 if (rc)
2099 return NT_STATUS_UNSUCCESSFUL;
2101 return NT_STATUS_OK;
2104 /**********************************************************************
2105 Add struct samu to LDAP.
2106 *********************************************************************/
2108 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2110 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2111 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2112 int rc;
2113 LDAPMessage *result = NULL;
2114 LDAPMessage *entry = NULL;
2115 LDAPMod **mods = NULL;
2116 int ldap_op = LDAP_MOD_REPLACE;
2117 uint32_t num_result;
2118 const char **attr_list;
2119 char *escape_user = NULL;
2120 const char *username = pdb_get_username(newpwd);
2121 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2122 char *filter = NULL;
2123 char *dn = NULL;
2124 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2125 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2127 if (!ctx) {
2128 return NT_STATUS_NO_MEMORY;
2131 if (!username || !*username) {
2132 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2133 status = NT_STATUS_INVALID_PARAMETER;
2134 goto fn_exit;
2137 /* free this list after the second search or in case we exit on failure */
2138 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2140 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2142 if (rc != LDAP_SUCCESS) {
2143 goto fn_exit;
2146 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2147 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2148 username));
2149 goto fn_exit;
2151 ldap_msgfree(result);
2152 result = NULL;
2154 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2155 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2156 sid, &result);
2157 if (rc == LDAP_SUCCESS) {
2158 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2159 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2160 "already in the base, with samba "
2161 "attributes\n", sid_string_dbg(sid)));
2162 goto fn_exit;
2164 ldap_msgfree(result);
2165 result = NULL;
2169 /* does the entry already exist but without a samba attributes?
2170 we need to return the samba attributes here */
2172 escape_user = escape_ldap_string(talloc_tos(), username);
2173 filter = talloc_strdup(attr_list, "(uid=%u)");
2174 if (!filter) {
2175 status = NT_STATUS_NO_MEMORY;
2176 goto fn_exit;
2178 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2179 TALLOC_FREE(escape_user);
2180 if (!filter) {
2181 status = NT_STATUS_NO_MEMORY;
2182 goto fn_exit;
2185 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2186 filter, attr_list, &result);
2187 if ( rc != LDAP_SUCCESS ) {
2188 goto fn_exit;
2191 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2193 if (num_result > 1) {
2194 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2195 goto fn_exit;
2198 /* Check if we need to update an existing entry */
2199 if (num_result == 1) {
2200 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2201 ldap_op = LDAP_MOD_REPLACE;
2202 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2203 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2204 if (!dn) {
2205 status = NT_STATUS_NO_MEMORY;
2206 goto fn_exit;
2209 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2211 /* There might be a SID for this account already - say an idmap entry */
2213 filter = talloc_asprintf(ctx,
2214 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2215 get_userattr_key2string(ldap_state->schema_ver,
2216 LDAP_ATTR_USER_SID),
2217 sid_string_talloc(ctx, sid),
2218 LDAP_OBJ_IDMAP_ENTRY,
2219 LDAP_OBJ_SID_ENTRY);
2220 if (!filter) {
2221 status = NT_STATUS_NO_MEMORY;
2222 goto fn_exit;
2225 /* free old result before doing a new search */
2226 if (result != NULL) {
2227 ldap_msgfree(result);
2228 result = NULL;
2230 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2231 filter, attr_list, &result);
2233 if ( rc != LDAP_SUCCESS ) {
2234 goto fn_exit;
2237 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2239 if (num_result > 1) {
2240 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2241 goto fn_exit;
2244 /* Check if we need to update an existing entry */
2245 if (num_result == 1) {
2247 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2248 ldap_op = LDAP_MOD_REPLACE;
2249 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2250 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2251 if (!dn) {
2252 status = NT_STATUS_NO_MEMORY;
2253 goto fn_exit;
2258 if (num_result == 0) {
2259 char *escape_username;
2260 /* Check if we need to add an entry */
2261 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2262 ldap_op = LDAP_MOD_ADD;
2264 escape_username = escape_rdn_val_string_alloc(username);
2265 if (!escape_username) {
2266 status = NT_STATUS_NO_MEMORY;
2267 goto fn_exit;
2270 if (username[strlen(username)-1] == '$') {
2271 dn = talloc_asprintf(ctx,
2272 "uid=%s,%s",
2273 escape_username,
2274 lp_ldap_machine_suffix());
2275 } else {
2276 dn = talloc_asprintf(ctx,
2277 "uid=%s,%s",
2278 escape_username,
2279 lp_ldap_user_suffix());
2282 SAFE_FREE(escape_username);
2283 if (!dn) {
2284 status = NT_STATUS_NO_MEMORY;
2285 goto fn_exit;
2289 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2290 pdb_element_is_set_or_changed)) {
2291 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2292 if (mods != NULL) {
2293 ldap_mods_free(mods, true);
2295 goto fn_exit;
2298 if (mods == NULL) {
2299 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2300 goto fn_exit;
2302 switch ( ldap_state->schema_ver ) {
2303 case SCHEMAVER_SAMBAACCOUNT:
2304 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2305 break;
2306 case SCHEMAVER_SAMBASAMACCOUNT:
2307 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2308 break;
2309 default:
2310 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2311 break;
2314 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2315 if (!NT_STATUS_IS_OK(ret)) {
2316 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2317 pdb_get_username(newpwd),dn));
2318 ldap_mods_free(mods, true);
2319 goto fn_exit;
2322 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2323 ldap_mods_free(mods, true);
2325 status = NT_STATUS_OK;
2327 fn_exit:
2329 TALLOC_FREE(ctx);
2330 if (result) {
2331 ldap_msgfree(result);
2333 return status;
2336 /**********************************************************************
2337 *********************************************************************/
2339 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2340 const char *filter,
2341 LDAPMessage ** result)
2343 int scope = LDAP_SCOPE_SUBTREE;
2344 int rc;
2345 const char **attr_list;
2347 attr_list = get_attr_list(NULL, groupmap_attr_list);
2348 rc = smbldap_search(ldap_state->smbldap_state,
2349 lp_ldap_suffix (), scope,
2350 filter, attr_list, 0, result);
2351 TALLOC_FREE(attr_list);
2353 return rc;
2356 /**********************************************************************
2357 *********************************************************************/
2359 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2360 GROUP_MAP *map, LDAPMessage *entry)
2362 char *temp = NULL;
2363 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2365 if (ldap_state == NULL || map == NULL || entry == NULL ||
2366 ldap_state->smbldap_state->ldap_struct == NULL) {
2367 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2368 TALLOC_FREE(ctx);
2369 return false;
2372 temp = smbldap_talloc_single_attribute(
2373 ldap_state->smbldap_state->ldap_struct,
2374 entry,
2375 get_attr_key2string(groupmap_attr_list,
2376 LDAP_ATTR_GIDNUMBER),
2377 ctx);
2378 if (!temp) {
2379 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2380 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2381 TALLOC_FREE(ctx);
2382 return false;
2384 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2386 map->gid = (gid_t)atol(temp);
2388 TALLOC_FREE(temp);
2389 temp = smbldap_talloc_single_attribute(
2390 ldap_state->smbldap_state->ldap_struct,
2391 entry,
2392 get_attr_key2string(groupmap_attr_list,
2393 LDAP_ATTR_GROUP_SID),
2394 ctx);
2395 if (!temp) {
2396 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2397 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2398 TALLOC_FREE(ctx);
2399 return false;
2402 if (!string_to_sid(&map->sid, temp)) {
2403 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2404 TALLOC_FREE(ctx);
2405 return false;
2408 TALLOC_FREE(temp);
2409 temp = smbldap_talloc_single_attribute(
2410 ldap_state->smbldap_state->ldap_struct,
2411 entry,
2412 get_attr_key2string(groupmap_attr_list,
2413 LDAP_ATTR_GROUP_TYPE),
2414 ctx);
2415 if (!temp) {
2416 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2417 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2418 TALLOC_FREE(ctx);
2419 return false;
2421 map->sid_name_use = (enum lsa_SidType)atol(temp);
2423 if ((map->sid_name_use < SID_NAME_USER) ||
2424 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2425 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2426 TALLOC_FREE(ctx);
2427 return false;
2430 TALLOC_FREE(temp);
2431 temp = smbldap_talloc_single_attribute(
2432 ldap_state->smbldap_state->ldap_struct,
2433 entry,
2434 get_attr_key2string(groupmap_attr_list,
2435 LDAP_ATTR_DISPLAY_NAME),
2436 ctx);
2437 if (!temp) {
2438 temp = smbldap_talloc_single_attribute(
2439 ldap_state->smbldap_state->ldap_struct,
2440 entry,
2441 get_attr_key2string(groupmap_attr_list,
2442 LDAP_ATTR_CN),
2443 ctx);
2444 if (!temp) {
2445 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2446 for gidNumber(%lu)\n",(unsigned long)map->gid));
2447 TALLOC_FREE(ctx);
2448 return false;
2451 map->nt_name = talloc_strdup(map, temp);
2452 if (!map->nt_name) {
2453 TALLOC_FREE(ctx);
2454 return false;
2457 TALLOC_FREE(temp);
2458 temp = smbldap_talloc_single_attribute(
2459 ldap_state->smbldap_state->ldap_struct,
2460 entry,
2461 get_attr_key2string(groupmap_attr_list,
2462 LDAP_ATTR_DESC),
2463 ctx);
2464 if (!temp) {
2465 temp = talloc_strdup(ctx, "");
2466 if (!temp) {
2467 TALLOC_FREE(ctx);
2468 return false;
2471 map->comment = talloc_strdup(map, temp);
2472 if (!map->comment) {
2473 TALLOC_FREE(ctx);
2474 return false;
2477 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2478 idmap_cache_set_sid2gid(&map->sid, map->gid);
2481 TALLOC_FREE(ctx);
2482 return true;
2485 /**********************************************************************
2486 *********************************************************************/
2488 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2489 const char *filter,
2490 GROUP_MAP *map)
2492 struct ldapsam_privates *ldap_state =
2493 (struct ldapsam_privates *)methods->private_data;
2494 LDAPMessage *result = NULL;
2495 LDAPMessage *entry = NULL;
2496 int count;
2498 if (ldapsam_search_one_group(ldap_state, filter, &result)
2499 != LDAP_SUCCESS) {
2500 return NT_STATUS_NO_SUCH_GROUP;
2503 count = ldap_count_entries(priv2ld(ldap_state), result);
2505 if (count < 1) {
2506 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2507 "%s\n", filter));
2508 ldap_msgfree(result);
2509 return NT_STATUS_NO_SUCH_GROUP;
2512 if (count > 1) {
2513 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2514 "count=%d\n", filter, count));
2515 ldap_msgfree(result);
2516 return NT_STATUS_NO_SUCH_GROUP;
2519 entry = ldap_first_entry(priv2ld(ldap_state), result);
2521 if (!entry) {
2522 ldap_msgfree(result);
2523 return NT_STATUS_UNSUCCESSFUL;
2526 if (!init_group_from_ldap(ldap_state, map, entry)) {
2527 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2528 "group filter %s\n", filter));
2529 ldap_msgfree(result);
2530 return NT_STATUS_NO_SUCH_GROUP;
2533 ldap_msgfree(result);
2534 return NT_STATUS_OK;
2537 /**********************************************************************
2538 *********************************************************************/
2540 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2541 struct dom_sid sid)
2543 char *filter = NULL;
2544 NTSTATUS status;
2545 fstring tmp;
2547 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2548 LDAP_OBJ_GROUPMAP,
2549 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2550 sid_to_fstring(tmp, &sid)) < 0) {
2551 return NT_STATUS_NO_MEMORY;
2554 status = ldapsam_getgroup(methods, filter, map);
2555 SAFE_FREE(filter);
2556 return status;
2559 /**********************************************************************
2560 *********************************************************************/
2562 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2563 gid_t gid)
2565 char *filter = NULL;
2566 NTSTATUS status;
2568 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2569 LDAP_OBJ_GROUPMAP,
2570 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2571 (unsigned long)gid) < 0) {
2572 return NT_STATUS_NO_MEMORY;
2575 status = ldapsam_getgroup(methods, filter, map);
2576 SAFE_FREE(filter);
2577 return status;
2580 /**********************************************************************
2581 *********************************************************************/
2583 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2584 const char *name)
2586 char *filter = NULL;
2587 char *escape_name = escape_ldap_string(talloc_tos(), name);
2588 NTSTATUS status;
2590 if (!escape_name) {
2591 return NT_STATUS_NO_MEMORY;
2594 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2595 LDAP_OBJ_GROUPMAP,
2596 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2597 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2598 escape_name) < 0) {
2599 TALLOC_FREE(escape_name);
2600 return NT_STATUS_NO_MEMORY;
2603 TALLOC_FREE(escape_name);
2604 status = ldapsam_getgroup(methods, filter, map);
2605 SAFE_FREE(filter);
2606 return status;
2609 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2610 LDAPMessage *entry,
2611 const struct dom_sid *domain_sid,
2612 uint32_t *rid)
2614 fstring str;
2615 struct dom_sid sid;
2617 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2618 str, sizeof(str)-1)) {
2619 DEBUG(10, ("Could not find sambaSID attribute\n"));
2620 return False;
2623 if (!string_to_sid(&sid, str)) {
2624 DEBUG(10, ("Could not convert string %s to sid\n", str));
2625 return False;
2628 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2629 DEBUG(10, ("SID %s is not in expected domain %s\n",
2630 str, sid_string_dbg(domain_sid)));
2631 return False;
2634 if (!sid_peek_rid(&sid, rid)) {
2635 DEBUG(10, ("Could not peek into RID\n"));
2636 return False;
2639 return True;
2642 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2643 TALLOC_CTX *mem_ctx,
2644 const struct dom_sid *group,
2645 uint32_t **pp_member_rids,
2646 size_t *p_num_members)
2648 struct ldapsam_privates *ldap_state =
2649 (struct ldapsam_privates *)methods->private_data;
2650 struct smbldap_state *conn = ldap_state->smbldap_state;
2651 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2652 const char *sid_attrs[] = { "sambaSID", NULL };
2653 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2654 LDAPMessage *result = NULL;
2655 LDAPMessage *entry;
2656 char *filter;
2657 char **values = NULL;
2658 char **memberuid;
2659 char *gidstr;
2660 int rc, count;
2662 *pp_member_rids = NULL;
2663 *p_num_members = 0;
2665 filter = talloc_asprintf(mem_ctx,
2666 "(&(objectClass=%s)"
2667 "(objectClass=%s)"
2668 "(sambaSID=%s))",
2669 LDAP_OBJ_POSIXGROUP,
2670 LDAP_OBJ_GROUPMAP,
2671 sid_string_talloc(mem_ctx, group));
2672 if (filter == NULL) {
2673 ret = NT_STATUS_NO_MEMORY;
2674 goto done;
2677 rc = smbldap_search(conn, lp_ldap_suffix(),
2678 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2679 &result);
2681 if (rc != LDAP_SUCCESS)
2682 goto done;
2684 talloc_autofree_ldapmsg(mem_ctx, result);
2686 count = ldap_count_entries(conn->ldap_struct, result);
2688 if (count > 1) {
2689 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2690 sid_string_dbg(group)));
2691 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2692 goto done;
2695 if (count == 0) {
2696 ret = NT_STATUS_NO_SUCH_GROUP;
2697 goto done;
2700 entry = ldap_first_entry(conn->ldap_struct, result);
2701 if (entry == NULL)
2702 goto done;
2704 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2705 if (!gidstr) {
2706 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2707 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2708 goto done;
2711 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2713 if ((values != NULL) && (values[0] != NULL)) {
2715 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2716 if (filter == NULL) {
2717 ret = NT_STATUS_NO_MEMORY;
2718 goto done;
2721 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2722 char *escape_memberuid;
2724 escape_memberuid = escape_ldap_string(talloc_tos(),
2725 *memberuid);
2726 if (escape_memberuid == NULL) {
2727 ret = NT_STATUS_NO_MEMORY;
2728 goto done;
2731 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2732 TALLOC_FREE(escape_memberuid);
2733 if (filter == NULL) {
2734 ret = NT_STATUS_NO_MEMORY;
2735 goto done;
2739 filter = talloc_asprintf_append_buffer(filter, "))");
2740 if (filter == NULL) {
2741 ret = NT_STATUS_NO_MEMORY;
2742 goto done;
2745 rc = smbldap_search(conn, lp_ldap_suffix(),
2746 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2747 &result);
2749 if (rc != LDAP_SUCCESS)
2750 goto done;
2752 count = ldap_count_entries(conn->ldap_struct, result);
2753 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2755 talloc_autofree_ldapmsg(mem_ctx, result);
2757 for (entry = ldap_first_entry(conn->ldap_struct, result);
2758 entry != NULL;
2759 entry = ldap_next_entry(conn->ldap_struct, entry))
2761 char *sidstr;
2762 struct dom_sid sid;
2763 uint32_t rid;
2765 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2766 entry, "sambaSID",
2767 mem_ctx);
2768 if (!sidstr) {
2769 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2770 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2771 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2772 goto done;
2775 if (!string_to_sid(&sid, sidstr))
2776 goto done;
2778 if (!sid_check_is_in_our_domain(&sid)) {
2779 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2780 "in our domain\n"));
2781 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2782 goto done;
2785 sid_peek_rid(&sid, &rid);
2787 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2788 p_num_members)) {
2789 ret = NT_STATUS_NO_MEMORY;
2790 goto done;
2795 filter = talloc_asprintf(mem_ctx,
2796 "(&(objectClass=%s)"
2797 "(gidNumber=%s))",
2798 LDAP_OBJ_SAMBASAMACCOUNT,
2799 gidstr);
2801 rc = smbldap_search(conn, lp_ldap_suffix(),
2802 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2803 &result);
2805 if (rc != LDAP_SUCCESS)
2806 goto done;
2808 talloc_autofree_ldapmsg(mem_ctx, result);
2810 for (entry = ldap_first_entry(conn->ldap_struct, result);
2811 entry != NULL;
2812 entry = ldap_next_entry(conn->ldap_struct, entry))
2814 uint32_t rid;
2816 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2817 entry,
2818 get_global_sam_sid(),
2819 &rid)) {
2820 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2821 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2822 goto done;
2825 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2826 p_num_members)) {
2827 ret = NT_STATUS_NO_MEMORY;
2828 goto done;
2832 ret = NT_STATUS_OK;
2834 done:
2836 if (values)
2837 ldap_value_free(values);
2839 return ret;
2842 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2843 TALLOC_CTX *mem_ctx,
2844 struct samu *user,
2845 struct dom_sid **pp_sids,
2846 gid_t **pp_gids,
2847 uint32_t *p_num_groups)
2849 struct ldapsam_privates *ldap_state =
2850 (struct ldapsam_privates *)methods->private_data;
2851 struct smbldap_state *conn = ldap_state->smbldap_state;
2852 char *filter;
2853 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2854 char *escape_name;
2855 int rc, count;
2856 LDAPMessage *result = NULL;
2857 LDAPMessage *entry;
2858 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2859 uint32_t num_sids;
2860 uint32_t num_gids;
2861 char *gidstr;
2862 gid_t primary_gid = -1;
2864 *pp_sids = NULL;
2865 num_sids = 0;
2867 if (pdb_get_username(user) == NULL) {
2868 return NT_STATUS_INVALID_PARAMETER;
2871 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2872 if (escape_name == NULL)
2873 return NT_STATUS_NO_MEMORY;
2875 if (user->unix_pw) {
2876 primary_gid = user->unix_pw->pw_gid;
2877 } else {
2878 /* retrieve the users primary gid */
2879 filter = talloc_asprintf(mem_ctx,
2880 "(&(objectClass=%s)(uid=%s))",
2881 LDAP_OBJ_SAMBASAMACCOUNT,
2882 escape_name);
2883 if (filter == NULL) {
2884 ret = NT_STATUS_NO_MEMORY;
2885 goto done;
2888 rc = smbldap_search(conn, lp_ldap_suffix(),
2889 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2891 if (rc != LDAP_SUCCESS)
2892 goto done;
2894 talloc_autofree_ldapmsg(mem_ctx, result);
2896 count = ldap_count_entries(priv2ld(ldap_state), result);
2898 switch (count) {
2899 case 0:
2900 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2901 ret = NT_STATUS_NO_SUCH_USER;
2902 goto done;
2903 case 1:
2904 entry = ldap_first_entry(priv2ld(ldap_state), result);
2906 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2907 if (!gidstr) {
2908 DEBUG (1, ("Unable to find the member's gid!\n"));
2909 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2910 goto done;
2912 primary_gid = strtoul(gidstr, NULL, 10);
2913 break;
2914 default:
2915 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2916 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2917 goto done;
2921 filter = talloc_asprintf(mem_ctx,
2922 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2923 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2924 if (filter == NULL) {
2925 ret = NT_STATUS_NO_MEMORY;
2926 goto done;
2929 rc = smbldap_search(conn, lp_ldap_suffix(),
2930 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2932 if (rc != LDAP_SUCCESS)
2933 goto done;
2935 talloc_autofree_ldapmsg(mem_ctx, result);
2937 num_gids = 0;
2938 *pp_gids = NULL;
2940 num_sids = 0;
2941 *pp_sids = NULL;
2943 /* We need to add the primary group as the first gid/sid */
2945 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2946 ret = NT_STATUS_NO_MEMORY;
2947 goto done;
2950 /* This sid will be replaced later */
2952 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2953 &num_sids);
2954 if (!NT_STATUS_IS_OK(ret)) {
2955 goto done;
2958 for (entry = ldap_first_entry(conn->ldap_struct, result);
2959 entry != NULL;
2960 entry = ldap_next_entry(conn->ldap_struct, entry))
2962 fstring str;
2963 struct dom_sid sid;
2964 gid_t gid;
2965 char *end;
2967 if (!smbldap_get_single_attribute(conn->ldap_struct,
2968 entry, "sambaSID",
2969 str, sizeof(str)-1))
2970 continue;
2972 if (!string_to_sid(&sid, str))
2973 goto done;
2975 if (!smbldap_get_single_attribute(conn->ldap_struct,
2976 entry, "gidNumber",
2977 str, sizeof(str)-1))
2978 continue;
2980 gid = strtoul(str, &end, 10);
2982 if (PTR_DIFF(end, str) != strlen(str))
2983 goto done;
2985 if (gid == primary_gid) {
2986 sid_copy(&(*pp_sids)[0], &sid);
2987 } else {
2988 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2989 &num_gids)) {
2990 ret = NT_STATUS_NO_MEMORY;
2991 goto done;
2993 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2994 &num_sids);
2995 if (!NT_STATUS_IS_OK(ret)) {
2996 goto done;
3001 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3002 DEBUG(3, ("primary group of [%s] not found\n",
3003 pdb_get_username(user)));
3004 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3005 goto done;
3008 *p_num_groups = num_sids;
3010 ret = NT_STATUS_OK;
3012 done:
3014 TALLOC_FREE(escape_name);
3015 return ret;
3018 /**********************************************************************
3019 * Augment a posixGroup object with a sambaGroupMapping domgroup
3020 *********************************************************************/
3022 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3023 struct ldapsam_privates *ldap_state,
3024 GROUP_MAP *map)
3026 const char *filter, *dn;
3027 LDAPMessage *msg, *entry;
3028 LDAPMod **mods;
3029 int rc;
3031 filter = talloc_asprintf(mem_ctx,
3032 "(&(objectClass=%s)(gidNumber=%u))",
3033 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3034 if (filter == NULL) {
3035 return NT_STATUS_NO_MEMORY;
3038 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3039 get_attr_list(mem_ctx, groupmap_attr_list),
3040 &msg);
3041 talloc_autofree_ldapmsg(mem_ctx, msg);
3043 if ((rc != LDAP_SUCCESS) ||
3044 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3045 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3046 return NT_STATUS_NO_SUCH_GROUP;
3049 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3050 if (dn == NULL) {
3051 return NT_STATUS_NO_MEMORY;
3054 mods = NULL;
3055 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3056 LDAP_OBJ_GROUPMAP);
3057 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3058 sid_string_talloc(mem_ctx, &map->sid));
3059 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3060 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3061 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3062 map->nt_name);
3063 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3064 map->comment);
3065 talloc_autofree_ldapmod(mem_ctx, mods);
3067 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3068 if (rc != LDAP_SUCCESS) {
3069 return NT_STATUS_ACCESS_DENIED;
3072 return NT_STATUS_OK;
3075 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3076 GROUP_MAP *map)
3078 struct ldapsam_privates *ldap_state =
3079 (struct ldapsam_privates *)methods->private_data;
3080 LDAPMessage *msg = NULL;
3081 LDAPMod **mods = NULL;
3082 const char *attrs[] = { NULL };
3083 char *filter;
3085 char *dn;
3086 TALLOC_CTX *mem_ctx;
3087 NTSTATUS result;
3089 struct dom_sid sid;
3091 int rc;
3093 mem_ctx = talloc_new(NULL);
3094 if (mem_ctx == NULL) {
3095 DEBUG(0, ("talloc_new failed\n"));
3096 return NT_STATUS_NO_MEMORY;
3099 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3100 sid_string_talloc(mem_ctx, &map->sid));
3101 if (filter == NULL) {
3102 result = NT_STATUS_NO_MEMORY;
3103 goto done;
3106 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3107 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3108 talloc_autofree_ldapmsg(mem_ctx, msg);
3110 if ((rc == LDAP_SUCCESS) &&
3111 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3113 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3114 "group mapping entry\n", sid_string_dbg(&map->sid)));
3115 result = NT_STATUS_GROUP_EXISTS;
3116 goto done;
3119 switch (map->sid_name_use) {
3121 case SID_NAME_DOM_GRP:
3122 /* To map a domain group we need to have a posix group
3123 to attach to. */
3124 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3125 goto done;
3126 break;
3128 case SID_NAME_ALIAS:
3129 if (!sid_check_is_in_our_domain(&map->sid)
3130 && !sid_check_is_in_builtin(&map->sid) )
3132 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3133 sid_string_dbg(&map->sid)));
3134 result = NT_STATUS_INVALID_PARAMETER;
3135 goto done;
3137 break;
3139 default:
3140 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3141 sid_type_lookup(map->sid_name_use)));
3142 result = NT_STATUS_INVALID_PARAMETER;
3143 goto done;
3146 /* Domain groups have been mapped in a separate routine, we have to
3147 * create an alias now */
3149 if (map->gid == -1) {
3150 DEBUG(10, ("Refusing to map gid==-1\n"));
3151 result = NT_STATUS_INVALID_PARAMETER;
3152 goto done;
3155 if (pdb_gid_to_sid(map->gid, &sid)) {
3156 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3157 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3158 result = NT_STATUS_GROUP_EXISTS;
3159 goto done;
3162 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3163 * the best we can get out of LDAP. */
3165 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3166 sid_string_talloc(mem_ctx, &map->sid),
3167 lp_ldap_group_suffix());
3168 if (dn == NULL) {
3169 result = NT_STATUS_NO_MEMORY;
3170 goto done;
3173 mods = NULL;
3175 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3176 LDAP_OBJ_SID_ENTRY);
3177 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3178 LDAP_OBJ_GROUPMAP);
3179 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3180 sid_string_talloc(mem_ctx, &map->sid));
3181 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3182 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3183 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3184 map->nt_name);
3185 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3186 map->comment);
3187 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3188 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3189 talloc_autofree_ldapmod(mem_ctx, mods);
3191 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3193 result = (rc == LDAP_SUCCESS) ?
3194 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3196 done:
3197 TALLOC_FREE(mem_ctx);
3198 return result;
3201 /**********************************************************************
3202 * Update a group mapping entry. We're quite strict about what can be changed:
3203 * Only the description and displayname may be changed. It simply does not
3204 * make any sense to change the SID, gid or the type in a mapping.
3205 *********************************************************************/
3207 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3208 GROUP_MAP *map)
3210 struct ldapsam_privates *ldap_state =
3211 (struct ldapsam_privates *)methods->private_data;
3212 int rc;
3213 const char *filter, *dn;
3214 LDAPMessage *msg = NULL;
3215 LDAPMessage *entry = NULL;
3216 LDAPMod **mods = NULL;
3217 TALLOC_CTX *mem_ctx;
3218 NTSTATUS result;
3220 mem_ctx = talloc_new(NULL);
3221 if (mem_ctx == NULL) {
3222 DEBUG(0, ("talloc_new failed\n"));
3223 return NT_STATUS_NO_MEMORY;
3226 /* Make 100% sure that sid, gid and type are not changed by looking up
3227 * exactly the values we're given in LDAP. */
3229 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3230 "(sambaSid=%s)(gidNumber=%u)"
3231 "(sambaGroupType=%d))",
3232 LDAP_OBJ_GROUPMAP,
3233 sid_string_talloc(mem_ctx, &map->sid),
3234 (unsigned int)map->gid, map->sid_name_use);
3235 if (filter == NULL) {
3236 result = NT_STATUS_NO_MEMORY;
3237 goto done;
3240 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3241 get_attr_list(mem_ctx, groupmap_attr_list),
3242 &msg);
3243 talloc_autofree_ldapmsg(mem_ctx, msg);
3245 if ((rc != LDAP_SUCCESS) ||
3246 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3247 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3248 result = NT_STATUS_NO_SUCH_GROUP;
3249 goto done;
3252 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3254 if (dn == NULL) {
3255 result = NT_STATUS_NO_MEMORY;
3256 goto done;
3259 mods = NULL;
3260 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3261 map->nt_name);
3262 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3263 map->comment);
3264 talloc_autofree_ldapmod(mem_ctx, mods);
3266 if (mods == NULL) {
3267 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3268 "nothing to do\n"));
3269 result = NT_STATUS_OK;
3270 goto done;
3273 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3275 if (rc != LDAP_SUCCESS) {
3276 result = NT_STATUS_ACCESS_DENIED;
3277 goto done;
3280 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3281 "group %lu in LDAP\n", (unsigned long)map->gid));
3283 result = NT_STATUS_OK;
3285 done:
3286 TALLOC_FREE(mem_ctx);
3287 return result;
3290 /**********************************************************************
3291 *********************************************************************/
3293 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3294 struct dom_sid sid)
3296 struct ldapsam_privates *priv =
3297 (struct ldapsam_privates *)methods->private_data;
3298 LDAPMessage *msg, *entry;
3299 int rc;
3300 NTSTATUS result;
3301 TALLOC_CTX *mem_ctx;
3302 char *filter;
3304 mem_ctx = talloc_new(NULL);
3305 if (mem_ctx == NULL) {
3306 DEBUG(0, ("talloc_new failed\n"));
3307 return NT_STATUS_NO_MEMORY;
3310 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3311 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3312 sid_string_talloc(mem_ctx, &sid));
3313 if (filter == NULL) {
3314 result = NT_STATUS_NO_MEMORY;
3315 goto done;
3317 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3318 get_attr_list(mem_ctx, groupmap_attr_list),
3319 &msg);
3320 talloc_autofree_ldapmsg(mem_ctx, msg);
3322 if ((rc != LDAP_SUCCESS) ||
3323 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3324 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3325 result = NT_STATUS_NO_SUCH_GROUP;
3326 goto done;
3329 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3330 get_attr_list(mem_ctx,
3331 groupmap_attr_list_to_delete));
3333 if ((rc == LDAP_NAMING_VIOLATION) ||
3334 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3335 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3336 const char *attrs[] = { "sambaGroupType", "description",
3337 "displayName", "sambaSIDList",
3338 NULL };
3340 /* Second try. Don't delete the sambaSID attribute, this is
3341 for "old" entries that are tacked on a winbind
3342 sambaIdmapEntry. */
3344 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3345 LDAP_OBJ_GROUPMAP, attrs);
3348 if ((rc == LDAP_NAMING_VIOLATION) ||
3349 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3350 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3351 const char *attrs[] = { "sambaGroupType", "description",
3352 "displayName", "sambaSIDList",
3353 "gidNumber", NULL };
3355 /* Third try. This is a post-3.0.21 alias (containing only
3356 * sambaSidEntry and sambaGroupMapping classes), we also have
3357 * to delete the gidNumber attribute, only the sambaSidEntry
3358 * remains */
3360 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3361 LDAP_OBJ_GROUPMAP, attrs);
3364 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3366 done:
3367 TALLOC_FREE(mem_ctx);
3368 return result;
3371 /**********************************************************************
3372 *********************************************************************/
3374 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3375 bool update)
3377 struct ldapsam_privates *ldap_state =
3378 (struct ldapsam_privates *)my_methods->private_data;
3379 char *filter = NULL;
3380 int rc;
3381 const char **attr_list;
3383 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3384 if (!filter) {
3385 return NT_STATUS_NO_MEMORY;
3387 attr_list = get_attr_list( NULL, groupmap_attr_list );
3388 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3389 LDAP_SCOPE_SUBTREE, filter,
3390 attr_list, 0, &ldap_state->result);
3391 TALLOC_FREE(attr_list);
3393 if (rc != LDAP_SUCCESS) {
3394 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3395 ldap_err2string(rc)));
3396 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3397 lp_ldap_suffix(), filter));
3398 ldap_msgfree(ldap_state->result);
3399 ldap_state->result = NULL;
3400 TALLOC_FREE(filter);
3401 return NT_STATUS_UNSUCCESSFUL;
3404 TALLOC_FREE(filter);
3406 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3407 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3408 ldap_state->result)));
3410 ldap_state->entry =
3411 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3412 ldap_state->result);
3413 ldap_state->index = 0;
3415 return NT_STATUS_OK;
3418 /**********************************************************************
3419 *********************************************************************/
3421 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3423 ldapsam_endsampwent(my_methods);
3426 /**********************************************************************
3427 *********************************************************************/
3429 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3430 GROUP_MAP *map)
3432 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3433 struct ldapsam_privates *ldap_state =
3434 (struct ldapsam_privates *)my_methods->private_data;
3435 bool bret = False;
3437 while (!bret) {
3438 if (!ldap_state->entry)
3439 return ret;
3441 ldap_state->index++;
3442 bret = init_group_from_ldap(ldap_state, map,
3443 ldap_state->entry);
3445 ldap_state->entry =
3446 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3447 ldap_state->entry);
3450 return NT_STATUS_OK;
3453 /**********************************************************************
3454 *********************************************************************/
3456 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3457 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3458 GROUP_MAP ***pp_rmap,
3459 size_t *p_num_entries,
3460 bool unix_only)
3462 GROUP_MAP *map = NULL;
3463 size_t entries = 0;
3465 *p_num_entries = 0;
3466 *pp_rmap = NULL;
3468 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3469 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3470 "passdb\n"));
3471 return NT_STATUS_ACCESS_DENIED;
3474 while (true) {
3476 map = talloc_zero(NULL, GROUP_MAP);
3477 if (!map) {
3478 return NT_STATUS_NO_MEMORY;
3481 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3482 TALLOC_FREE(map);
3483 break;
3486 if (sid_name_use != SID_NAME_UNKNOWN &&
3487 sid_name_use != map->sid_name_use) {
3488 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3489 "not of the requested type\n",
3490 map->nt_name));
3491 continue;
3493 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3494 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3495 "non mapped\n", map->nt_name));
3496 continue;
3499 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3500 GROUP_MAP *, entries + 1);
3501 if (!(*pp_rmap)) {
3502 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3503 "enlarge group map!\n"));
3504 return NT_STATUS_UNSUCCESSFUL;
3507 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3509 entries += 1;
3512 ldapsam_endsamgrent(methods);
3514 *p_num_entries = entries;
3516 return NT_STATUS_OK;
3519 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3520 const struct dom_sid *alias,
3521 const struct dom_sid *member,
3522 int modop)
3524 struct ldapsam_privates *ldap_state =
3525 (struct ldapsam_privates *)methods->private_data;
3526 char *dn = NULL;
3527 LDAPMessage *result = NULL;
3528 LDAPMessage *entry = NULL;
3529 int count;
3530 LDAPMod **mods = NULL;
3531 int rc;
3532 enum lsa_SidType type = SID_NAME_USE_NONE;
3533 fstring tmp;
3535 char *filter = NULL;
3537 if (sid_check_is_in_builtin(alias)) {
3538 type = SID_NAME_ALIAS;
3541 if (sid_check_is_in_our_domain(alias)) {
3542 type = SID_NAME_ALIAS;
3545 if (type == SID_NAME_USE_NONE) {
3546 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3547 sid_string_dbg(alias)));
3548 return NT_STATUS_NO_SUCH_ALIAS;
3551 if (asprintf(&filter,
3552 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3553 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3554 type) < 0) {
3555 return NT_STATUS_NO_MEMORY;
3558 if (ldapsam_search_one_group(ldap_state, filter,
3559 &result) != LDAP_SUCCESS) {
3560 SAFE_FREE(filter);
3561 return NT_STATUS_NO_SUCH_ALIAS;
3564 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3565 result);
3567 if (count < 1) {
3568 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3569 ldap_msgfree(result);
3570 SAFE_FREE(filter);
3571 return NT_STATUS_NO_SUCH_ALIAS;
3574 if (count > 1) {
3575 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3576 "filter %s: count=%d\n", filter, count));
3577 ldap_msgfree(result);
3578 SAFE_FREE(filter);
3579 return NT_STATUS_NO_SUCH_ALIAS;
3582 SAFE_FREE(filter);
3584 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3585 result);
3587 if (!entry) {
3588 ldap_msgfree(result);
3589 return NT_STATUS_UNSUCCESSFUL;
3592 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3593 if (!dn) {
3594 ldap_msgfree(result);
3595 return NT_STATUS_UNSUCCESSFUL;
3598 smbldap_set_mod(&mods, modop,
3599 get_attr_key2string(groupmap_attr_list,
3600 LDAP_ATTR_SID_LIST),
3601 sid_to_fstring(tmp, member));
3603 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3605 ldap_mods_free(mods, True);
3606 ldap_msgfree(result);
3607 TALLOC_FREE(dn);
3609 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3610 return NT_STATUS_MEMBER_IN_ALIAS;
3613 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3614 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3617 if (rc != LDAP_SUCCESS) {
3618 return NT_STATUS_UNSUCCESSFUL;
3621 return NT_STATUS_OK;
3624 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3625 const struct dom_sid *alias,
3626 const struct dom_sid *member)
3628 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3631 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3632 const struct dom_sid *alias,
3633 const struct dom_sid *member)
3635 return ldapsam_modify_aliasmem(methods, alias, member,
3636 LDAP_MOD_DELETE);
3639 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3640 const struct dom_sid *alias,
3641 TALLOC_CTX *mem_ctx,
3642 struct dom_sid **pp_members,
3643 size_t *p_num_members)
3645 struct ldapsam_privates *ldap_state =
3646 (struct ldapsam_privates *)methods->private_data;
3647 LDAPMessage *result = NULL;
3648 LDAPMessage *entry = NULL;
3649 int count;
3650 char **values = NULL;
3651 int i;
3652 char *filter = NULL;
3653 uint32_t num_members = 0;
3654 enum lsa_SidType type = SID_NAME_USE_NONE;
3655 fstring tmp;
3657 *pp_members = NULL;
3658 *p_num_members = 0;
3660 if (sid_check_is_in_builtin(alias)) {
3661 type = SID_NAME_ALIAS;
3664 if (sid_check_is_in_our_domain(alias)) {
3665 type = SID_NAME_ALIAS;
3668 if (type == SID_NAME_USE_NONE) {
3669 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3670 sid_string_dbg(alias)));
3671 return NT_STATUS_NO_SUCH_ALIAS;
3674 if (asprintf(&filter,
3675 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3676 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3677 type) < 0) {
3678 return NT_STATUS_NO_MEMORY;
3681 if (ldapsam_search_one_group(ldap_state, filter,
3682 &result) != LDAP_SUCCESS) {
3683 SAFE_FREE(filter);
3684 return NT_STATUS_NO_SUCH_ALIAS;
3687 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3688 result);
3690 if (count < 1) {
3691 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3692 ldap_msgfree(result);
3693 SAFE_FREE(filter);
3694 return NT_STATUS_NO_SUCH_ALIAS;
3697 if (count > 1) {
3698 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3699 "filter %s: count=%d\n", filter, count));
3700 ldap_msgfree(result);
3701 SAFE_FREE(filter);
3702 return NT_STATUS_NO_SUCH_ALIAS;
3705 SAFE_FREE(filter);
3707 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3708 result);
3710 if (!entry) {
3711 ldap_msgfree(result);
3712 return NT_STATUS_UNSUCCESSFUL;
3715 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3716 entry,
3717 get_attr_key2string(groupmap_attr_list,
3718 LDAP_ATTR_SID_LIST));
3720 if (values == NULL) {
3721 ldap_msgfree(result);
3722 return NT_STATUS_OK;
3725 count = ldap_count_values(values);
3727 for (i=0; i<count; i++) {
3728 struct dom_sid member;
3729 NTSTATUS status;
3731 if (!string_to_sid(&member, values[i]))
3732 continue;
3734 status = add_sid_to_array(mem_ctx, &member, pp_members,
3735 &num_members);
3736 if (!NT_STATUS_IS_OK(status)) {
3737 ldap_value_free(values);
3738 ldap_msgfree(result);
3739 return status;
3743 *p_num_members = num_members;
3744 ldap_value_free(values);
3745 ldap_msgfree(result);
3747 return NT_STATUS_OK;
3750 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3751 TALLOC_CTX *mem_ctx,
3752 const struct dom_sid *domain_sid,
3753 const struct dom_sid *members,
3754 size_t num_members,
3755 uint32_t **pp_alias_rids,
3756 size_t *p_num_alias_rids)
3758 struct ldapsam_privates *ldap_state =
3759 (struct ldapsam_privates *)methods->private_data;
3760 LDAP *ldap_struct;
3762 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3764 LDAPMessage *result = NULL;
3765 LDAPMessage *entry = NULL;
3766 int i;
3767 int rc;
3768 char *filter;
3769 enum lsa_SidType type = SID_NAME_USE_NONE;
3770 bool is_builtin = false;
3771 bool sid_added = false;
3773 *pp_alias_rids = NULL;
3774 *p_num_alias_rids = 0;
3776 if (sid_check_is_builtin(domain_sid)) {
3777 is_builtin = true;
3778 type = SID_NAME_ALIAS;
3781 if (sid_check_is_domain(domain_sid)) {
3782 type = SID_NAME_ALIAS;
3785 if (type == SID_NAME_USE_NONE) {
3786 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3787 sid_string_dbg(domain_sid)));
3788 return NT_STATUS_UNSUCCESSFUL;
3791 if (num_members == 0) {
3792 return NT_STATUS_OK;
3795 filter = talloc_asprintf(mem_ctx,
3796 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3797 LDAP_OBJ_GROUPMAP, type);
3799 for (i=0; i<num_members; i++)
3800 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3801 filter,
3802 sid_string_talloc(mem_ctx,
3803 &members[i]));
3805 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3807 if (filter == NULL) {
3808 return NT_STATUS_NO_MEMORY;
3811 if (is_builtin &&
3812 ldap_state->search_cache.filter &&
3813 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3814 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3815 result = ldap_state->search_cache.result;
3816 ldap_state->search_cache.result = NULL;
3817 } else {
3818 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3819 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3820 if (rc != LDAP_SUCCESS) {
3821 return NT_STATUS_UNSUCCESSFUL;
3823 talloc_autofree_ldapmsg(filter, result);
3826 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3828 for (entry = ldap_first_entry(ldap_struct, result);
3829 entry != NULL;
3830 entry = ldap_next_entry(ldap_struct, entry))
3832 fstring sid_str;
3833 struct dom_sid sid;
3834 uint32_t rid;
3836 if (!smbldap_get_single_attribute(ldap_struct, entry,
3837 LDAP_ATTRIBUTE_SID,
3838 sid_str,
3839 sizeof(sid_str)-1))
3840 continue;
3842 if (!string_to_sid(&sid, sid_str))
3843 continue;
3845 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3846 continue;
3848 sid_added = true;
3850 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3851 p_num_alias_rids)) {
3852 return NT_STATUS_NO_MEMORY;
3856 if (!is_builtin && !sid_added) {
3857 TALLOC_FREE(ldap_state->search_cache.filter);
3859 * Note: result is a talloc child of filter because of the
3860 * talloc_autofree_ldapmsg() usage
3862 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3863 ldap_state->search_cache.result = result;
3866 return NT_STATUS_OK;
3869 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3870 enum pdb_policy_type type,
3871 uint32_t value)
3873 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3874 int rc;
3875 LDAPMod **mods = NULL;
3876 fstring value_string;
3877 const char *policy_attr = NULL;
3879 struct ldapsam_privates *ldap_state =
3880 (struct ldapsam_privates *)methods->private_data;
3882 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3884 if (!ldap_state->domain_dn) {
3885 return NT_STATUS_INVALID_PARAMETER;
3888 policy_attr = get_account_policy_attr(type);
3889 if (policy_attr == NULL) {
3890 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3891 "policy\n"));
3892 return ntstatus;
3895 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3897 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3899 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3900 mods);
3902 ldap_mods_free(mods, True);
3904 if (rc != LDAP_SUCCESS) {
3905 return ntstatus;
3908 if (!cache_account_policy_set(type, value)) {
3909 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3910 "update local tdb cache\n"));
3911 return ntstatus;
3914 return NT_STATUS_OK;
3917 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3918 enum pdb_policy_type type,
3919 uint32_t value)
3921 return ldapsam_set_account_policy_in_ldap(methods, type,
3922 value);
3925 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3926 enum pdb_policy_type type,
3927 uint32_t *value)
3929 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3930 LDAPMessage *result = NULL;
3931 LDAPMessage *entry = NULL;
3932 int count;
3933 int rc;
3934 char **vals = NULL;
3935 char *filter;
3936 const char *policy_attr = NULL;
3938 struct ldapsam_privates *ldap_state =
3939 (struct ldapsam_privates *)methods->private_data;
3941 const char *attrs[2];
3943 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3945 if (!ldap_state->domain_dn) {
3946 return NT_STATUS_INVALID_PARAMETER;
3949 policy_attr = get_account_policy_attr(type);
3950 if (!policy_attr) {
3951 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3952 "policy index: %d\n", type));
3953 return ntstatus;
3956 attrs[0] = policy_attr;
3957 attrs[1] = NULL;
3959 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3960 if (filter == NULL) {
3961 return NT_STATUS_NO_MEMORY;
3963 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3964 LDAP_SCOPE_BASE, filter, attrs, 0,
3965 &result);
3966 TALLOC_FREE(filter);
3967 if (rc != LDAP_SUCCESS) {
3968 return ntstatus;
3971 count = ldap_count_entries(priv2ld(ldap_state), result);
3972 if (count < 1) {
3973 goto out;
3976 entry = ldap_first_entry(priv2ld(ldap_state), result);
3977 if (entry == NULL) {
3978 goto out;
3981 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3982 if (vals == NULL) {
3983 goto out;
3986 *value = (uint32_t)atol(vals[0]);
3988 ntstatus = NT_STATUS_OK;
3990 out:
3991 if (vals)
3992 ldap_value_free(vals);
3993 ldap_msgfree(result);
3995 return ntstatus;
3998 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4000 - if user hasn't decided to use account policies inside LDAP just reuse the
4001 old tdb values
4003 - if there is a valid cache entry, return that
4004 - if there is an LDAP entry, update cache and return
4005 - otherwise set to default, update cache and return
4007 Guenther
4009 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4010 enum pdb_policy_type type,
4011 uint32_t *value)
4013 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4015 if (cache_account_policy_get(type, value)) {
4016 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4017 "cache\n"));
4018 return NT_STATUS_OK;
4021 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4022 value);
4023 if (NT_STATUS_IS_OK(ntstatus)) {
4024 goto update_cache;
4027 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4028 "ldap\n"));
4030 #if 0
4031 /* should we automagically migrate old tdb value here ? */
4032 if (account_policy_get(type, value))
4033 goto update_ldap;
4035 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4036 "default\n", type));
4037 #endif
4039 if (!account_policy_get_default(type, value)) {
4040 return ntstatus;
4043 /* update_ldap: */
4045 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4046 if (!NT_STATUS_IS_OK(ntstatus)) {
4047 return ntstatus;
4050 update_cache:
4052 if (!cache_account_policy_set(type, *value)) {
4053 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4054 "tdb as a cache\n"));
4055 return NT_STATUS_UNSUCCESSFUL;
4058 return NT_STATUS_OK;
4061 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4062 const struct dom_sid *domain_sid,
4063 int num_rids,
4064 uint32_t *rids,
4065 const char **names,
4066 enum lsa_SidType *attrs)
4068 struct ldapsam_privates *ldap_state =
4069 (struct ldapsam_privates *)methods->private_data;
4070 LDAPMessage *msg = NULL;
4071 LDAPMessage *entry;
4072 char *allsids = NULL;
4073 int i, rc, num_mapped;
4074 NTSTATUS result = NT_STATUS_NO_MEMORY;
4075 TALLOC_CTX *mem_ctx;
4076 LDAP *ld;
4077 bool is_builtin;
4079 mem_ctx = talloc_new(NULL);
4080 if (mem_ctx == NULL) {
4081 DEBUG(0, ("talloc_new failed\n"));
4082 goto done;
4085 if (!sid_check_is_builtin(domain_sid) &&
4086 !sid_check_is_domain(domain_sid)) {
4087 result = NT_STATUS_INVALID_PARAMETER;
4088 goto done;
4091 if (num_rids == 0) {
4092 result = NT_STATUS_NONE_MAPPED;
4093 goto done;
4096 for (i=0; i<num_rids; i++)
4097 attrs[i] = SID_NAME_UNKNOWN;
4099 allsids = talloc_strdup(mem_ctx, "");
4100 if (allsids == NULL) {
4101 goto done;
4104 for (i=0; i<num_rids; i++) {
4105 struct dom_sid sid;
4106 sid_compose(&sid, domain_sid, rids[i]);
4107 allsids = talloc_asprintf_append_buffer(
4108 allsids, "(sambaSid=%s)",
4109 sid_string_talloc(mem_ctx, &sid));
4110 if (allsids == NULL) {
4111 goto done;
4115 /* First look for users */
4118 char *filter;
4119 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4121 filter = talloc_asprintf(
4122 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4123 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4125 if (filter == NULL) {
4126 goto done;
4129 rc = smbldap_search(ldap_state->smbldap_state,
4130 lp_ldap_user_suffix(),
4131 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4132 &msg);
4133 talloc_autofree_ldapmsg(mem_ctx, msg);
4136 if (rc != LDAP_SUCCESS)
4137 goto done;
4139 ld = ldap_state->smbldap_state->ldap_struct;
4140 num_mapped = 0;
4142 for (entry = ldap_first_entry(ld, msg);
4143 entry != NULL;
4144 entry = ldap_next_entry(ld, entry)) {
4145 uint32_t rid;
4146 int rid_index;
4147 const char *name;
4149 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4150 &rid)) {
4151 DEBUG(2, ("Could not find sid from ldap entry\n"));
4152 continue;
4155 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4156 names);
4157 if (name == NULL) {
4158 DEBUG(2, ("Could not retrieve uid attribute\n"));
4159 continue;
4162 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4163 if (rid == rids[rid_index])
4164 break;
4167 if (rid_index == num_rids) {
4168 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4169 continue;
4172 attrs[rid_index] = SID_NAME_USER;
4173 names[rid_index] = name;
4174 num_mapped += 1;
4177 if (num_mapped == num_rids) {
4178 /* No need to look for groups anymore -- we're done */
4179 result = NT_STATUS_OK;
4180 goto done;
4183 /* Same game for groups */
4186 char *filter;
4187 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4188 "sambaGroupType", NULL };
4190 filter = talloc_asprintf(
4191 mem_ctx, "(&(objectClass=%s)(|%s))",
4192 LDAP_OBJ_GROUPMAP, allsids);
4193 if (filter == NULL) {
4194 goto done;
4197 rc = smbldap_search(ldap_state->smbldap_state,
4198 lp_ldap_suffix(),
4199 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4200 &msg);
4201 talloc_autofree_ldapmsg(mem_ctx, msg);
4204 if (rc != LDAP_SUCCESS)
4205 goto done;
4207 /* ldap_struct might have changed due to a reconnect */
4209 ld = ldap_state->smbldap_state->ldap_struct;
4211 /* For consistency checks, we already checked we're only domain or builtin */
4213 is_builtin = sid_check_is_builtin(domain_sid);
4215 for (entry = ldap_first_entry(ld, msg);
4216 entry != NULL;
4217 entry = ldap_next_entry(ld, entry))
4219 uint32_t rid;
4220 int rid_index;
4221 const char *attr;
4222 enum lsa_SidType type;
4223 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4225 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4226 mem_ctx);
4227 if (attr == NULL) {
4228 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4229 dn));
4230 continue;
4233 type = (enum lsa_SidType)atol(attr);
4235 /* Consistency checks */
4236 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4237 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4238 (type != SID_NAME_DOM_GRP)))) {
4239 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4242 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4243 &rid)) {
4244 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4245 continue;
4248 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4250 if (attr == NULL) {
4251 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4252 dn));
4253 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4256 if (attr == NULL) {
4257 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4258 dn));
4259 continue;
4262 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4263 if (rid == rids[rid_index])
4264 break;
4267 if (rid_index == num_rids) {
4268 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4269 continue;
4272 attrs[rid_index] = type;
4273 names[rid_index] = attr;
4274 num_mapped += 1;
4277 result = NT_STATUS_NONE_MAPPED;
4279 if (num_mapped > 0)
4280 result = (num_mapped == num_rids) ?
4281 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4282 done:
4283 TALLOC_FREE(mem_ctx);
4284 return result;
4287 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4289 char *filter = NULL;
4290 char *escaped = NULL;
4291 char *result = NULL;
4293 if (asprintf(&filter, "(&%s(objectclass=%s))",
4294 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4295 goto done;
4298 escaped = escape_ldap_string(talloc_tos(), username);
4299 if (escaped == NULL) goto done;
4301 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4303 done:
4304 SAFE_FREE(filter);
4305 TALLOC_FREE(escaped);
4307 return result;
4310 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4312 int i, num = 0;
4313 va_list ap;
4314 const char **result;
4316 va_start(ap, mem_ctx);
4317 while (va_arg(ap, const char *) != NULL)
4318 num += 1;
4319 va_end(ap);
4321 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4322 return NULL;
4325 va_start(ap, mem_ctx);
4326 for (i=0; i<num; i++) {
4327 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4328 if (result[i] == NULL) {
4329 talloc_free(result);
4330 va_end(ap);
4331 return NULL;
4334 va_end(ap);
4336 result[num] = NULL;
4337 return result;
4340 struct ldap_search_state {
4341 struct smbldap_state *connection;
4343 uint32_t acct_flags;
4344 uint16_t group_type;
4346 const char *base;
4347 int scope;
4348 const char *filter;
4349 const char **attrs;
4350 int attrsonly;
4351 void *pagedresults_cookie;
4353 LDAPMessage *entries, *current_entry;
4354 bool (*ldap2displayentry)(struct ldap_search_state *state,
4355 TALLOC_CTX *mem_ctx,
4356 LDAP *ld, LDAPMessage *entry,
4357 struct samr_displayentry *result);
4360 static bool ldapsam_search_firstpage(struct pdb_search *search)
4362 struct ldap_search_state *state =
4363 (struct ldap_search_state *)search->private_data;
4364 LDAP *ld;
4365 int rc = LDAP_OPERATIONS_ERROR;
4367 state->entries = NULL;
4369 if (state->connection->paged_results) {
4370 rc = smbldap_search_paged(state->connection, state->base,
4371 state->scope, state->filter,
4372 state->attrs, state->attrsonly,
4373 lp_ldap_page_size(), &state->entries,
4374 &state->pagedresults_cookie);
4377 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4379 if (state->entries != NULL) {
4380 /* Left over from unsuccessful paged attempt */
4381 ldap_msgfree(state->entries);
4382 state->entries = NULL;
4385 rc = smbldap_search(state->connection, state->base,
4386 state->scope, state->filter, state->attrs,
4387 state->attrsonly, &state->entries);
4389 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4390 return False;
4392 /* Ok, the server was lying. It told us it could do paged
4393 * searches when it could not. */
4394 state->connection->paged_results = False;
4397 ld = state->connection->ldap_struct;
4398 if ( ld == NULL) {
4399 DEBUG(5, ("Don't have an LDAP connection right after a "
4400 "search\n"));
4401 return False;
4403 state->current_entry = ldap_first_entry(ld, state->entries);
4405 return True;
4408 static bool ldapsam_search_nextpage(struct pdb_search *search)
4410 struct ldap_search_state *state =
4411 (struct ldap_search_state *)search->private_data;
4412 int rc;
4414 if (!state->connection->paged_results) {
4415 /* There is no next page when there are no paged results */
4416 return False;
4419 rc = smbldap_search_paged(state->connection, state->base,
4420 state->scope, state->filter, state->attrs,
4421 state->attrsonly, lp_ldap_page_size(),
4422 &state->entries,
4423 &state->pagedresults_cookie);
4425 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4426 return False;
4428 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4430 if (state->current_entry == NULL) {
4431 ldap_msgfree(state->entries);
4432 state->entries = NULL;
4433 return false;
4436 return True;
4439 static bool ldapsam_search_next_entry(struct pdb_search *search,
4440 struct samr_displayentry *entry)
4442 struct ldap_search_state *state =
4443 (struct ldap_search_state *)search->private_data;
4444 bool result;
4446 retry:
4447 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4448 return False;
4450 if ((state->entries == NULL) &&
4451 !ldapsam_search_nextpage(search))
4452 return False;
4454 if (state->current_entry == NULL) {
4455 return false;
4458 result = state->ldap2displayentry(state, search,
4459 state->connection->ldap_struct,
4460 state->current_entry, entry);
4462 if (!result) {
4463 char *dn;
4464 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4465 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4466 if (dn != NULL) ldap_memfree(dn);
4469 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4471 if (state->current_entry == NULL) {
4472 ldap_msgfree(state->entries);
4473 state->entries = NULL;
4476 if (!result) goto retry;
4478 return True;
4481 static void ldapsam_search_end(struct pdb_search *search)
4483 struct ldap_search_state *state =
4484 (struct ldap_search_state *)search->private_data;
4485 int rc;
4487 if (state->pagedresults_cookie == NULL)
4488 return;
4490 if (state->entries != NULL)
4491 ldap_msgfree(state->entries);
4493 state->entries = NULL;
4494 state->current_entry = NULL;
4496 if (!state->connection->paged_results)
4497 return;
4499 /* Tell the LDAP server we're not interested in the rest anymore. */
4501 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4502 state->filter, state->attrs,
4503 state->attrsonly, 0, &state->entries,
4504 &state->pagedresults_cookie);
4506 if (rc != LDAP_SUCCESS)
4507 DEBUG(5, ("Could not end search properly\n"));
4509 return;
4512 static bool ldapuser2displayentry(struct ldap_search_state *state,
4513 TALLOC_CTX *mem_ctx,
4514 LDAP *ld, LDAPMessage *entry,
4515 struct samr_displayentry *result)
4517 char **vals;
4518 size_t converted_size;
4519 struct dom_sid sid;
4520 uint32_t acct_flags;
4522 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4523 if ((vals == NULL) || (vals[0] == NULL)) {
4524 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4525 return False;
4527 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4528 ldap_value_free(vals);
4530 if ((state->acct_flags != 0) &&
4531 ((state->acct_flags & acct_flags) == 0))
4532 return False;
4534 result->acct_flags = acct_flags;
4535 result->account_name = "";
4536 result->fullname = "";
4537 result->description = "";
4539 vals = ldap_get_values(ld, entry, "uid");
4540 if ((vals == NULL) || (vals[0] == NULL)) {
4541 DEBUG(5, ("\"uid\" not found\n"));
4542 return False;
4544 if (!pull_utf8_talloc(mem_ctx,
4545 discard_const_p(char *, &result->account_name),
4546 vals[0], &converted_size))
4548 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4549 strerror(errno)));
4552 ldap_value_free(vals);
4554 vals = ldap_get_values(ld, entry, "displayName");
4555 if ((vals == NULL) || (vals[0] == NULL))
4556 DEBUG(8, ("\"displayName\" not found\n"));
4557 else if (!pull_utf8_talloc(mem_ctx,
4558 discard_const_p(char *, &result->fullname),
4559 vals[0], &converted_size))
4561 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4562 strerror(errno)));
4565 ldap_value_free(vals);
4567 vals = ldap_get_values(ld, entry, "description");
4568 if ((vals == NULL) || (vals[0] == NULL))
4569 DEBUG(8, ("\"description\" not found\n"));
4570 else if (!pull_utf8_talloc(mem_ctx,
4571 discard_const_p(char *, &result->description),
4572 vals[0], &converted_size))
4574 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4575 strerror(errno)));
4578 ldap_value_free(vals);
4580 if ((result->account_name == NULL) ||
4581 (result->fullname == NULL) ||
4582 (result->description == NULL)) {
4583 DEBUG(0, ("talloc failed\n"));
4584 return False;
4587 vals = ldap_get_values(ld, entry, "sambaSid");
4588 if ((vals == NULL) || (vals[0] == NULL)) {
4589 DEBUG(0, ("\"objectSid\" not found\n"));
4590 return False;
4593 if (!string_to_sid(&sid, vals[0])) {
4594 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4595 ldap_value_free(vals);
4596 return False;
4598 ldap_value_free(vals);
4600 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4601 DEBUG(0, ("sid %s does not belong to our domain\n",
4602 sid_string_dbg(&sid)));
4603 return False;
4606 return True;
4610 static bool ldapsam_search_users(struct pdb_methods *methods,
4611 struct pdb_search *search,
4612 uint32_t acct_flags)
4614 struct ldapsam_privates *ldap_state =
4615 (struct ldapsam_privates *)methods->private_data;
4616 struct ldap_search_state *state;
4618 state = talloc(search, struct ldap_search_state);
4619 if (state == NULL) {
4620 DEBUG(0, ("talloc failed\n"));
4621 return False;
4624 state->connection = ldap_state->smbldap_state;
4626 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4627 state->base = lp_ldap_user_suffix();
4628 else if ((acct_flags != 0) &&
4629 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4630 state->base = lp_ldap_machine_suffix();
4631 else
4632 state->base = lp_ldap_suffix();
4634 state->acct_flags = acct_flags;
4635 state->base = talloc_strdup(search, state->base);
4636 state->scope = LDAP_SCOPE_SUBTREE;
4637 state->filter = get_ldap_filter(search, "*");
4638 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4639 "displayName", "description",
4640 "sambaAcctFlags", NULL);
4641 state->attrsonly = 0;
4642 state->pagedresults_cookie = NULL;
4643 state->entries = NULL;
4644 state->ldap2displayentry = ldapuser2displayentry;
4646 if ((state->filter == NULL) || (state->attrs == NULL)) {
4647 DEBUG(0, ("talloc failed\n"));
4648 return False;
4651 search->private_data = state;
4652 search->next_entry = ldapsam_search_next_entry;
4653 search->search_end = ldapsam_search_end;
4655 return ldapsam_search_firstpage(search);
4658 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4659 TALLOC_CTX *mem_ctx,
4660 LDAP *ld, LDAPMessage *entry,
4661 struct samr_displayentry *result)
4663 char **vals;
4664 size_t converted_size;
4665 struct dom_sid sid;
4666 uint16_t group_type;
4668 result->account_name = "";
4669 result->fullname = "";
4670 result->description = "";
4673 vals = ldap_get_values(ld, entry, "sambaGroupType");
4674 if ((vals == NULL) || (vals[0] == NULL)) {
4675 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4676 if (vals != NULL) {
4677 ldap_value_free(vals);
4679 return False;
4682 group_type = atoi(vals[0]);
4684 if ((state->group_type != 0) &&
4685 ((state->group_type != group_type))) {
4686 ldap_value_free(vals);
4687 return False;
4690 ldap_value_free(vals);
4692 /* display name is the NT group name */
4694 vals = ldap_get_values(ld, entry, "displayName");
4695 if ((vals == NULL) || (vals[0] == NULL)) {
4696 DEBUG(8, ("\"displayName\" not found\n"));
4698 /* fallback to the 'cn' attribute */
4699 vals = ldap_get_values(ld, entry, "cn");
4700 if ((vals == NULL) || (vals[0] == NULL)) {
4701 DEBUG(5, ("\"cn\" not found\n"));
4702 return False;
4704 if (!pull_utf8_talloc(mem_ctx,
4705 discard_const_p(char *,
4706 &result->account_name),
4707 vals[0], &converted_size))
4709 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4710 "failed: %s", strerror(errno)));
4713 else if (!pull_utf8_talloc(mem_ctx,
4714 discard_const_p(char *,
4715 &result->account_name),
4716 vals[0], &converted_size))
4718 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4719 strerror(errno)));
4722 ldap_value_free(vals);
4724 vals = ldap_get_values(ld, entry, "description");
4725 if ((vals == NULL) || (vals[0] == NULL))
4726 DEBUG(8, ("\"description\" not found\n"));
4727 else if (!pull_utf8_talloc(mem_ctx,
4728 discard_const_p(char *, &result->description),
4729 vals[0], &converted_size))
4731 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4732 strerror(errno)));
4734 ldap_value_free(vals);
4736 if ((result->account_name == NULL) ||
4737 (result->fullname == NULL) ||
4738 (result->description == NULL)) {
4739 DEBUG(0, ("talloc failed\n"));
4740 return False;
4743 vals = ldap_get_values(ld, entry, "sambaSid");
4744 if ((vals == NULL) || (vals[0] == NULL)) {
4745 DEBUG(0, ("\"objectSid\" not found\n"));
4746 if (vals != NULL) {
4747 ldap_value_free(vals);
4749 return False;
4752 if (!string_to_sid(&sid, vals[0])) {
4753 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4754 return False;
4757 ldap_value_free(vals);
4759 switch (group_type) {
4760 case SID_NAME_DOM_GRP:
4761 case SID_NAME_ALIAS:
4763 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4764 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4766 DEBUG(0, ("%s is not in our domain\n",
4767 sid_string_dbg(&sid)));
4768 return False;
4770 break;
4772 default:
4773 DEBUG(0,("unknown group type: %d\n", group_type));
4774 return False;
4777 result->acct_flags = 0;
4779 return True;
4782 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4783 struct pdb_search *search,
4784 const struct dom_sid *sid,
4785 enum lsa_SidType type)
4787 struct ldapsam_privates *ldap_state =
4788 (struct ldapsam_privates *)methods->private_data;
4789 struct ldap_search_state *state;
4790 fstring tmp;
4792 state = talloc(search, struct ldap_search_state);
4793 if (state == NULL) {
4794 DEBUG(0, ("talloc failed\n"));
4795 return False;
4798 state->connection = ldap_state->smbldap_state;
4800 state->base = talloc_strdup(search, lp_ldap_suffix());
4801 state->connection = ldap_state->smbldap_state;
4802 state->scope = LDAP_SCOPE_SUBTREE;
4803 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4804 "(sambaGroupType=%d)(sambaSID=%s*))",
4805 LDAP_OBJ_GROUPMAP,
4806 type, sid_to_fstring(tmp, sid));
4807 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4808 "displayName", "description",
4809 "sambaGroupType", NULL);
4810 state->attrsonly = 0;
4811 state->pagedresults_cookie = NULL;
4812 state->entries = NULL;
4813 state->group_type = type;
4814 state->ldap2displayentry = ldapgroup2displayentry;
4816 if ((state->filter == NULL) || (state->attrs == NULL)) {
4817 DEBUG(0, ("talloc failed\n"));
4818 return False;
4821 search->private_data = state;
4822 search->next_entry = ldapsam_search_next_entry;
4823 search->search_end = ldapsam_search_end;
4825 return ldapsam_search_firstpage(search);
4828 static bool ldapsam_search_groups(struct pdb_methods *methods,
4829 struct pdb_search *search)
4831 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4834 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4835 struct pdb_search *search,
4836 const struct dom_sid *sid)
4838 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4841 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4843 return PDB_CAP_STORE_RIDS;
4846 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4847 uint32_t *rid)
4849 struct smbldap_state *smbldap_state = priv->smbldap_state;
4851 LDAPMessage *result = NULL;
4852 LDAPMessage *entry = NULL;
4853 LDAPMod **mods = NULL;
4854 NTSTATUS status;
4855 char *value;
4856 int rc;
4857 uint32_t nextRid = 0;
4858 const char *dn;
4860 TALLOC_CTX *mem_ctx;
4862 mem_ctx = talloc_new(NULL);
4863 if (mem_ctx == NULL) {
4864 DEBUG(0, ("talloc_new failed\n"));
4865 return NT_STATUS_NO_MEMORY;
4868 status = smbldap_search_domain_info(smbldap_state, &result,
4869 get_global_sam_name(), False);
4870 if (!NT_STATUS_IS_OK(status)) {
4871 DEBUG(3, ("Could not get domain info: %s\n",
4872 nt_errstr(status)));
4873 goto done;
4876 talloc_autofree_ldapmsg(mem_ctx, result);
4878 entry = ldap_first_entry(priv2ld(priv), result);
4879 if (entry == NULL) {
4880 DEBUG(0, ("Could not get domain info entry\n"));
4881 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4882 goto done;
4885 /* Find the largest of the three attributes "sambaNextRid",
4886 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4887 concept of differentiating between user and group rids, and will
4888 use only "sambaNextRid" in the future. But for compatibility
4889 reasons I look if others have chosen different strategies -- VL */
4891 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4892 "sambaNextRid", mem_ctx);
4893 if (value != NULL) {
4894 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4895 nextRid = MAX(nextRid, tmp);
4898 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4899 "sambaNextUserRid", mem_ctx);
4900 if (value != NULL) {
4901 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4902 nextRid = MAX(nextRid, tmp);
4905 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4906 "sambaNextGroupRid", mem_ctx);
4907 if (value != NULL) {
4908 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4909 nextRid = MAX(nextRid, tmp);
4912 if (nextRid == 0) {
4913 nextRid = BASE_RID-1;
4916 nextRid += 1;
4918 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4919 talloc_asprintf(mem_ctx, "%d", nextRid));
4920 talloc_autofree_ldapmod(mem_ctx, mods);
4922 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4923 status = NT_STATUS_NO_MEMORY;
4924 goto done;
4927 rc = smbldap_modify(smbldap_state, dn, mods);
4929 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4930 * please retry" */
4932 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4934 done:
4935 if (NT_STATUS_IS_OK(status)) {
4936 *rid = nextRid;
4939 TALLOC_FREE(mem_ctx);
4940 return status;
4943 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4945 int i;
4947 for (i=0; i<10; i++) {
4948 NTSTATUS result = ldapsam_get_new_rid(
4949 (struct ldapsam_privates *)methods->private_data, rid);
4950 if (NT_STATUS_IS_OK(result)) {
4951 return result;
4954 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4955 return result;
4958 /* The ldap update failed (maybe a race condition), retry */
4961 /* Tried 10 times, fail. */
4962 return NT_STATUS_ACCESS_DENIED;
4965 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4967 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4968 return NT_STATUS_IS_OK(result) ? True : False;
4971 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4972 const struct dom_sid *sid,
4973 uid_t *uid, gid_t *gid,
4974 enum lsa_SidType *type)
4976 struct ldapsam_privates *priv =
4977 (struct ldapsam_privates *)methods->private_data;
4978 char *filter;
4979 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4980 NULL };
4981 LDAPMessage *result = NULL;
4982 LDAPMessage *entry = NULL;
4983 bool ret = False;
4984 char *value;
4985 int rc;
4987 TALLOC_CTX *mem_ctx;
4989 mem_ctx = talloc_new(NULL);
4990 if (mem_ctx == NULL) {
4991 DEBUG(0, ("talloc_new failed\n"));
4992 return False;
4995 filter = talloc_asprintf(mem_ctx,
4996 "(&(sambaSid=%s)"
4997 "(|(objectClass=%s)(objectClass=%s)))",
4998 sid_string_talloc(mem_ctx, sid),
4999 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5000 if (filter == NULL) {
5001 DEBUG(5, ("talloc_asprintf failed\n"));
5002 goto done;
5005 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5006 attrs, &result);
5007 if (rc != LDAP_SUCCESS) {
5008 goto done;
5010 talloc_autofree_ldapmsg(mem_ctx, result);
5012 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5013 DEBUG(10, ("Got %d entries, expected one\n",
5014 ldap_count_entries(priv2ld(priv), result)));
5015 goto done;
5018 entry = ldap_first_entry(priv2ld(priv), result);
5020 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5021 "sambaGroupType", mem_ctx);
5023 if (value != NULL) {
5024 const char *gid_str;
5025 /* It's a group */
5027 gid_str = smbldap_talloc_single_attribute(
5028 priv2ld(priv), entry, "gidNumber", mem_ctx);
5029 if (gid_str == NULL) {
5030 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5031 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5032 entry)));
5033 goto done;
5036 *gid = strtoul(gid_str, NULL, 10);
5037 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
5038 idmap_cache_set_sid2gid(sid, *gid);
5039 ret = True;
5040 goto done;
5043 /* It must be a user */
5045 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5046 "uidNumber", mem_ctx);
5047 if (value == NULL) {
5048 DEBUG(1, ("Could not find uidNumber in %s\n",
5049 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5050 goto done;
5053 *uid = strtoul(value, NULL, 10);
5054 *type = SID_NAME_USER;
5055 idmap_cache_set_sid2uid(sid, *uid);
5057 ret = True;
5058 done:
5059 TALLOC_FREE(mem_ctx);
5060 return ret;
5064 * Find the SID for a uid.
5065 * This is shortcut is only used if ldapsam:trusted is set to true.
5067 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5068 struct dom_sid *sid)
5070 struct ldapsam_privates *priv =
5071 (struct ldapsam_privates *)methods->private_data;
5072 char *filter;
5073 const char *attrs[] = { "sambaSID", NULL };
5074 LDAPMessage *result = NULL;
5075 LDAPMessage *entry = NULL;
5076 bool ret = false;
5077 char *user_sid_string;
5078 struct dom_sid user_sid;
5079 int rc;
5080 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5082 filter = talloc_asprintf(tmp_ctx,
5083 "(&(uidNumber=%u)"
5084 "(objectClass=%s)"
5085 "(objectClass=%s))",
5086 (unsigned int)uid,
5087 LDAP_OBJ_POSIXACCOUNT,
5088 LDAP_OBJ_SAMBASAMACCOUNT);
5089 if (filter == NULL) {
5090 DEBUG(3, ("talloc_asprintf failed\n"));
5091 goto done;
5094 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5095 if (rc != LDAP_SUCCESS) {
5096 goto done;
5098 talloc_autofree_ldapmsg(tmp_ctx, result);
5100 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5101 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5102 ldap_count_entries(priv2ld(priv), result),
5103 (unsigned int)uid));
5104 goto done;
5107 entry = ldap_first_entry(priv2ld(priv), result);
5109 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5110 "sambaSID", tmp_ctx);
5111 if (user_sid_string == NULL) {
5112 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5113 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5114 goto done;
5117 if (!string_to_sid(&user_sid, user_sid_string)) {
5118 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5119 user_sid_string));
5120 goto done;
5123 sid_copy(sid, &user_sid);
5125 idmap_cache_set_sid2uid(sid, uid);
5127 ret = true;
5129 done:
5130 TALLOC_FREE(tmp_ctx);
5131 return ret;
5135 * Find the SID for a gid.
5136 * This is shortcut is only used if ldapsam:trusted is set to true.
5138 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5139 struct dom_sid *sid)
5141 struct ldapsam_privates *priv =
5142 (struct ldapsam_privates *)methods->private_data;
5143 char *filter;
5144 const char *attrs[] = { "sambaSID", NULL };
5145 LDAPMessage *result = NULL;
5146 LDAPMessage *entry = NULL;
5147 bool ret = false;
5148 char *group_sid_string;
5149 struct dom_sid group_sid;
5150 int rc;
5151 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5153 filter = talloc_asprintf(tmp_ctx,
5154 "(&(gidNumber=%u)"
5155 "(objectClass=%s))",
5156 (unsigned int)gid,
5157 LDAP_OBJ_GROUPMAP);
5158 if (filter == NULL) {
5159 DEBUG(3, ("talloc_asprintf failed\n"));
5160 goto done;
5163 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5164 if (rc != LDAP_SUCCESS) {
5165 goto done;
5167 talloc_autofree_ldapmsg(tmp_ctx, result);
5169 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5170 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5171 ldap_count_entries(priv2ld(priv), result),
5172 (unsigned int)gid));
5173 goto done;
5176 entry = ldap_first_entry(priv2ld(priv), result);
5178 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5179 "sambaSID", tmp_ctx);
5180 if (group_sid_string == NULL) {
5181 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5182 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5183 goto done;
5186 if (!string_to_sid(&group_sid, group_sid_string)) {
5187 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5188 group_sid_string));
5189 goto done;
5192 sid_copy(sid, &group_sid);
5194 idmap_cache_set_sid2gid(sid, gid);
5196 ret = true;
5198 done:
5199 TALLOC_FREE(tmp_ctx);
5200 return ret;
5205 * The following functions are called only if
5206 * ldapsam:trusted and ldapsam:editposix are
5207 * set to true
5211 * ldapsam_create_user creates a new
5212 * posixAccount and sambaSamAccount object
5213 * in the ldap users subtree
5215 * The uid is allocated by winbindd.
5218 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5219 TALLOC_CTX *tmp_ctx, const char *name,
5220 uint32_t acb_info, uint32_t *rid)
5222 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5223 LDAPMessage *entry = NULL;
5224 LDAPMessage *result = NULL;
5225 uint32_t num_result;
5226 bool is_machine = False;
5227 bool add_posix = False;
5228 LDAPMod **mods = NULL;
5229 struct samu *user;
5230 char *filter;
5231 char *username;
5232 char *homedir;
5233 char *gidstr;
5234 char *uidstr;
5235 char *shell;
5236 const char *dn = NULL;
5237 struct dom_sid group_sid;
5238 struct dom_sid user_sid;
5239 gid_t gid = -1;
5240 uid_t uid = -1;
5241 NTSTATUS ret;
5242 int rc;
5244 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5245 acb_info & ACB_WSTRUST ||
5246 acb_info & ACB_SVRTRUST ||
5247 acb_info & ACB_DOMTRUST) {
5248 is_machine = True;
5251 username = escape_ldap_string(talloc_tos(), name);
5252 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5253 username, LDAP_OBJ_POSIXACCOUNT);
5254 TALLOC_FREE(username);
5256 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5257 if (rc != LDAP_SUCCESS) {
5258 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5259 return NT_STATUS_ACCESS_DENIED;
5261 talloc_autofree_ldapmsg(tmp_ctx, result);
5263 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5265 if (num_result > 1) {
5266 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5267 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5270 if (num_result == 1) {
5271 char *tmp;
5272 /* check if it is just a posix account.
5273 * or if there is a sid attached to this entry
5276 entry = ldap_first_entry(priv2ld(ldap_state), result);
5277 if (!entry) {
5278 return NT_STATUS_UNSUCCESSFUL;
5281 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5282 if (tmp) {
5283 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5284 return NT_STATUS_USER_EXISTS;
5287 /* it is just a posix account, retrieve the dn for later use */
5288 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5289 if (!dn) {
5290 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5291 return NT_STATUS_NO_MEMORY;
5295 if (num_result == 0) {
5296 add_posix = True;
5299 /* Create the basic samu structure and generate the mods for the ldap commit */
5300 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5301 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5302 return ret;
5305 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5307 user = samu_new(tmp_ctx);
5308 if (!user) {
5309 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5310 return NT_STATUS_NO_MEMORY;
5313 if (!pdb_set_username(user, name, PDB_SET)) {
5314 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5315 return NT_STATUS_UNSUCCESSFUL;
5317 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5318 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5319 return NT_STATUS_UNSUCCESSFUL;
5321 if (is_machine) {
5322 if (acb_info & ACB_NORMAL) {
5323 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5324 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5325 return NT_STATUS_UNSUCCESSFUL;
5327 } else {
5328 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5329 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5330 return NT_STATUS_UNSUCCESSFUL;
5333 } else {
5334 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5335 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5336 return NT_STATUS_UNSUCCESSFUL;
5340 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5341 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5342 return NT_STATUS_UNSUCCESSFUL;
5345 if (!init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed)) {
5346 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5347 return NT_STATUS_UNSUCCESSFUL;
5350 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5351 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5353 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5355 if (add_posix) {
5356 char *escape_name;
5358 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5360 /* retrieve the Domain Users group gid */
5361 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5362 !sid_to_gid(&group_sid, &gid)) {
5363 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5364 return NT_STATUS_INVALID_PRIMARY_GROUP;
5367 /* lets allocate a new userid for this user */
5368 if (!winbind_allocate_uid(&uid)) {
5369 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5370 return NT_STATUS_UNSUCCESSFUL;
5374 if (is_machine) {
5375 /* TODO: choose a more appropriate default for machines */
5376 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5377 shell = talloc_strdup(tmp_ctx, "/bin/false");
5378 } else {
5379 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5380 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5382 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5383 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5385 escape_name = escape_rdn_val_string_alloc(name);
5386 if (!escape_name) {
5387 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5388 return NT_STATUS_NO_MEMORY;
5391 if (is_machine) {
5392 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5393 } else {
5394 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5397 SAFE_FREE(escape_name);
5399 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5400 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5401 return NT_STATUS_NO_MEMORY;
5404 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5405 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5406 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5407 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5408 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5409 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5410 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5413 talloc_autofree_ldapmod(tmp_ctx, mods);
5415 if (add_posix) {
5416 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5417 } else {
5418 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5421 if (rc != LDAP_SUCCESS) {
5422 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5423 return NT_STATUS_UNSUCCESSFUL;
5426 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5428 flush_pwnam_cache();
5430 return NT_STATUS_OK;
5433 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5435 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5436 LDAPMessage *result = NULL;
5437 LDAPMessage *entry = NULL;
5438 int num_result;
5439 const char *dn;
5440 char *filter;
5441 int rc;
5443 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5445 filter = talloc_asprintf(tmp_ctx,
5446 "(&(uid=%s)"
5447 "(objectClass=%s)"
5448 "(objectClass=%s))",
5449 pdb_get_username(sam_acct),
5450 LDAP_OBJ_POSIXACCOUNT,
5451 LDAP_OBJ_SAMBASAMACCOUNT);
5452 if (filter == NULL) {
5453 return NT_STATUS_NO_MEMORY;
5456 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5457 if (rc != LDAP_SUCCESS) {
5458 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5459 return NT_STATUS_UNSUCCESSFUL;
5461 talloc_autofree_ldapmsg(tmp_ctx, result);
5463 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5465 if (num_result == 0) {
5466 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5467 return NT_STATUS_NO_SUCH_USER;
5470 if (num_result > 1) {
5471 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5472 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5475 entry = ldap_first_entry(priv2ld(ldap_state), result);
5476 if (!entry) {
5477 return NT_STATUS_UNSUCCESSFUL;
5480 /* it is just a posix account, retrieve the dn for later use */
5481 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5482 if (!dn) {
5483 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5484 return NT_STATUS_NO_MEMORY;
5487 /* try to remove memberships first */
5489 NTSTATUS status;
5490 struct dom_sid *sids = NULL;
5491 gid_t *gids = NULL;
5492 uint32_t num_groups = 0;
5493 int i;
5494 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5496 status = ldapsam_enum_group_memberships(my_methods,
5497 tmp_ctx,
5498 sam_acct,
5499 &sids,
5500 &gids,
5501 &num_groups);
5502 if (!NT_STATUS_IS_OK(status)) {
5503 goto delete_dn;
5506 for (i=0; i < num_groups; i++) {
5508 uint32_t group_rid;
5510 sid_peek_rid(&sids[i], &group_rid);
5512 ldapsam_del_groupmem(my_methods,
5513 tmp_ctx,
5514 group_rid,
5515 user_rid);
5519 delete_dn:
5521 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5522 if (rc != LDAP_SUCCESS) {
5523 return NT_STATUS_UNSUCCESSFUL;
5526 flush_pwnam_cache();
5528 return NT_STATUS_OK;
5532 * ldapsam_create_group creates a new
5533 * posixGroup and sambaGroupMapping object
5534 * in the ldap groups subtree
5536 * The gid is allocated by winbindd.
5539 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5540 TALLOC_CTX *tmp_ctx,
5541 const char *name,
5542 uint32_t *rid)
5544 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5545 NTSTATUS ret;
5546 LDAPMessage *entry = NULL;
5547 LDAPMessage *result = NULL;
5548 uint32_t num_result;
5549 bool is_new_entry = False;
5550 LDAPMod **mods = NULL;
5551 char *filter;
5552 char *groupsidstr;
5553 char *groupname;
5554 char *grouptype;
5555 char *gidstr;
5556 const char *dn = NULL;
5557 struct dom_sid group_sid;
5558 gid_t gid = -1;
5559 int rc;
5561 groupname = escape_ldap_string(talloc_tos(), name);
5562 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5563 groupname, LDAP_OBJ_POSIXGROUP);
5564 TALLOC_FREE(groupname);
5566 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5567 if (rc != LDAP_SUCCESS) {
5568 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5569 return NT_STATUS_UNSUCCESSFUL;
5571 talloc_autofree_ldapmsg(tmp_ctx, result);
5573 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5575 if (num_result > 1) {
5576 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5577 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5580 if (num_result == 1) {
5581 char *tmp;
5582 /* check if it is just a posix group.
5583 * or if there is a sid attached to this entry
5586 entry = ldap_first_entry(priv2ld(ldap_state), result);
5587 if (!entry) {
5588 return NT_STATUS_UNSUCCESSFUL;
5591 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5592 if (tmp) {
5593 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5594 return NT_STATUS_GROUP_EXISTS;
5597 /* it is just a posix group, retrieve the gid and the dn for later use */
5598 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5599 if (!tmp) {
5600 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5601 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5604 gid = strtoul(tmp, NULL, 10);
5606 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5607 if (!dn) {
5608 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5609 return NT_STATUS_NO_MEMORY;
5613 if (num_result == 0) {
5614 is_new_entry = true;
5617 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5618 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5619 return ret;
5622 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5624 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5625 &group_sid));
5626 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5628 if (!groupsidstr || !grouptype) {
5629 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5630 return NT_STATUS_NO_MEMORY;
5633 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5634 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5635 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5636 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5638 if (is_new_entry) {
5639 char *escape_name;
5641 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5643 /* lets allocate a new groupid for this group */
5644 if (!winbind_allocate_gid(&gid)) {
5645 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5646 return NT_STATUS_UNSUCCESSFUL;
5649 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5651 escape_name = escape_rdn_val_string_alloc(name);
5652 if (!escape_name) {
5653 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5654 return NT_STATUS_NO_MEMORY;
5657 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5659 SAFE_FREE(escape_name);
5661 if (!gidstr || !dn) {
5662 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5663 return NT_STATUS_NO_MEMORY;
5666 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5667 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5668 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5671 talloc_autofree_ldapmod(tmp_ctx, mods);
5673 if (is_new_entry) {
5674 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5675 #if 0
5676 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5677 /* This call may fail with rfc2307bis schema */
5678 /* Retry adding a structural class */
5679 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5680 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5682 #endif
5683 } else {
5684 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5687 if (rc != LDAP_SUCCESS) {
5688 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5689 return NT_STATUS_UNSUCCESSFUL;
5692 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5694 return NT_STATUS_OK;
5697 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5699 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5700 LDAPMessage *result = NULL;
5701 LDAPMessage *entry = NULL;
5702 int num_result;
5703 const char *dn;
5704 char *gidstr;
5705 char *filter;
5706 struct dom_sid group_sid;
5707 int rc;
5709 /* get the group sid */
5710 sid_compose(&group_sid, get_global_sam_sid(), rid);
5712 filter = talloc_asprintf(tmp_ctx,
5713 "(&(sambaSID=%s)"
5714 "(objectClass=%s)"
5715 "(objectClass=%s))",
5716 sid_string_talloc(tmp_ctx, &group_sid),
5717 LDAP_OBJ_POSIXGROUP,
5718 LDAP_OBJ_GROUPMAP);
5719 if (filter == NULL) {
5720 return NT_STATUS_NO_MEMORY;
5723 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5724 if (rc != LDAP_SUCCESS) {
5725 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5726 return NT_STATUS_UNSUCCESSFUL;
5728 talloc_autofree_ldapmsg(tmp_ctx, result);
5730 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5732 if (num_result == 0) {
5733 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5734 return NT_STATUS_NO_SUCH_GROUP;
5737 if (num_result > 1) {
5738 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5739 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5742 entry = ldap_first_entry(priv2ld(ldap_state), result);
5743 if (!entry) {
5744 return NT_STATUS_UNSUCCESSFUL;
5747 /* here it is, retrieve the dn for later use */
5748 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5749 if (!dn) {
5750 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5751 return NT_STATUS_NO_MEMORY;
5754 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5755 if (!gidstr) {
5756 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5757 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5760 /* check no user have this group marked as primary group */
5761 filter = talloc_asprintf(tmp_ctx,
5762 "(&(gidNumber=%s)"
5763 "(objectClass=%s)"
5764 "(objectClass=%s))",
5765 gidstr,
5766 LDAP_OBJ_POSIXACCOUNT,
5767 LDAP_OBJ_SAMBASAMACCOUNT);
5769 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5770 if (rc != LDAP_SUCCESS) {
5771 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5772 return NT_STATUS_UNSUCCESSFUL;
5774 talloc_autofree_ldapmsg(tmp_ctx, result);
5776 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5778 if (num_result != 0) {
5779 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5780 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5783 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5784 if (rc != LDAP_SUCCESS) {
5785 return NT_STATUS_UNSUCCESSFUL;
5788 return NT_STATUS_OK;
5791 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5792 TALLOC_CTX *tmp_ctx,
5793 uint32_t group_rid,
5794 uint32_t member_rid,
5795 int modop)
5797 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5798 LDAPMessage *entry = NULL;
5799 LDAPMessage *result = NULL;
5800 uint32_t num_result;
5801 LDAPMod **mods = NULL;
5802 char *filter;
5803 char *uidstr;
5804 const char *dn = NULL;
5805 struct dom_sid group_sid;
5806 struct dom_sid member_sid;
5807 int rc;
5809 switch (modop) {
5810 case LDAP_MOD_ADD:
5811 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5812 break;
5813 case LDAP_MOD_DELETE:
5814 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5815 break;
5816 default:
5817 return NT_STATUS_UNSUCCESSFUL;
5820 /* get member sid */
5821 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5823 /* get the group sid */
5824 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5826 filter = talloc_asprintf(tmp_ctx,
5827 "(&(sambaSID=%s)"
5828 "(objectClass=%s)"
5829 "(objectClass=%s))",
5830 sid_string_talloc(tmp_ctx, &member_sid),
5831 LDAP_OBJ_POSIXACCOUNT,
5832 LDAP_OBJ_SAMBASAMACCOUNT);
5833 if (filter == NULL) {
5834 return NT_STATUS_NO_MEMORY;
5837 /* get the member uid */
5838 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5839 if (rc != LDAP_SUCCESS) {
5840 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5841 return NT_STATUS_UNSUCCESSFUL;
5843 talloc_autofree_ldapmsg(tmp_ctx, result);
5845 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5847 if (num_result == 0) {
5848 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5849 return NT_STATUS_NO_SUCH_MEMBER;
5852 if (num_result > 1) {
5853 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5854 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5857 entry = ldap_first_entry(priv2ld(ldap_state), result);
5858 if (!entry) {
5859 return NT_STATUS_UNSUCCESSFUL;
5862 if (modop == LDAP_MOD_DELETE) {
5863 /* check if we are trying to remove the member from his primary group */
5864 char *gidstr;
5865 gid_t user_gid, group_gid;
5867 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5868 if (!gidstr) {
5869 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5870 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5873 user_gid = strtoul(gidstr, NULL, 10);
5875 if (!sid_to_gid(&group_sid, &group_gid)) {
5876 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5877 return NT_STATUS_UNSUCCESSFUL;
5880 if (user_gid == group_gid) {
5881 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5882 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5886 /* here it is, retrieve the uid for later use */
5887 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5888 if (!uidstr) {
5889 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5890 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5893 filter = talloc_asprintf(tmp_ctx,
5894 "(&(sambaSID=%s)"
5895 "(objectClass=%s)"
5896 "(objectClass=%s))",
5897 sid_string_talloc(tmp_ctx, &group_sid),
5898 LDAP_OBJ_POSIXGROUP,
5899 LDAP_OBJ_GROUPMAP);
5901 /* get the group */
5902 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5903 if (rc != LDAP_SUCCESS) {
5904 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5905 return NT_STATUS_UNSUCCESSFUL;
5907 talloc_autofree_ldapmsg(tmp_ctx, result);
5909 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5911 if (num_result == 0) {
5912 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5913 return NT_STATUS_NO_SUCH_GROUP;
5916 if (num_result > 1) {
5917 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5918 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5921 entry = ldap_first_entry(priv2ld(ldap_state), result);
5922 if (!entry) {
5923 return NT_STATUS_UNSUCCESSFUL;
5926 /* here it is, retrieve the dn for later use */
5927 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5928 if (!dn) {
5929 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5930 return NT_STATUS_NO_MEMORY;
5933 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5935 talloc_autofree_ldapmod(tmp_ctx, mods);
5937 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5938 if (rc != LDAP_SUCCESS) {
5939 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5940 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5941 return NT_STATUS_MEMBER_IN_GROUP;
5943 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5944 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5945 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5947 return NT_STATUS_UNSUCCESSFUL;
5950 return NT_STATUS_OK;
5953 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5954 TALLOC_CTX *tmp_ctx,
5955 uint32_t group_rid,
5956 uint32_t member_rid)
5958 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5960 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5961 TALLOC_CTX *tmp_ctx,
5962 uint32_t group_rid,
5963 uint32_t member_rid)
5965 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5968 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5969 TALLOC_CTX *mem_ctx,
5970 struct samu *sampass)
5972 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5973 LDAPMessage *entry = NULL;
5974 LDAPMessage *result = NULL;
5975 uint32_t num_result;
5976 LDAPMod **mods = NULL;
5977 char *filter;
5978 char *escape_username;
5979 char *gidstr;
5980 const char *dn = NULL;
5981 gid_t gid;
5982 int rc;
5984 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5986 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5987 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5988 return NT_STATUS_UNSUCCESSFUL;
5990 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
5991 if (!gidstr) {
5992 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5993 return NT_STATUS_NO_MEMORY;
5996 escape_username = escape_ldap_string(talloc_tos(),
5997 pdb_get_username(sampass));
5998 if (escape_username== NULL) {
5999 return NT_STATUS_NO_MEMORY;
6002 filter = talloc_asprintf(mem_ctx,
6003 "(&(uid=%s)"
6004 "(objectClass=%s)"
6005 "(objectClass=%s))",
6006 escape_username,
6007 LDAP_OBJ_POSIXACCOUNT,
6008 LDAP_OBJ_SAMBASAMACCOUNT);
6010 TALLOC_FREE(escape_username);
6012 if (filter == NULL) {
6013 return NT_STATUS_NO_MEMORY;
6016 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6017 if (rc != LDAP_SUCCESS) {
6018 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6019 return NT_STATUS_UNSUCCESSFUL;
6021 talloc_autofree_ldapmsg(mem_ctx, result);
6023 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6025 if (num_result == 0) {
6026 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6027 return NT_STATUS_NO_SUCH_USER;
6030 if (num_result > 1) {
6031 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6032 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6035 entry = ldap_first_entry(priv2ld(ldap_state), result);
6036 if (!entry) {
6037 return NT_STATUS_UNSUCCESSFUL;
6040 /* retrieve the dn for later use */
6041 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6042 if (!dn) {
6043 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6044 return NT_STATUS_NO_MEMORY;
6047 /* remove the old one, and add the new one, this way we do not risk races */
6048 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6050 if (mods == NULL) {
6051 return NT_STATUS_OK;
6054 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6056 if (rc != LDAP_SUCCESS) {
6057 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6058 pdb_get_username(sampass), gidstr));
6059 return NT_STATUS_UNSUCCESSFUL;
6062 flush_pwnam_cache();
6064 return NT_STATUS_OK;
6068 /**********************************************************************
6069 trusted domains functions
6070 *********************************************************************/
6072 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6073 const char *domain)
6075 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6076 ldap_state->domain_dn);
6079 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6080 TALLOC_CTX *mem_ctx,
6081 const char *domain, LDAPMessage **entry)
6083 int rc;
6084 char *filter;
6085 int scope = LDAP_SCOPE_SUBTREE;
6086 const char **attrs = NULL; /* NULL: get all attrs */
6087 int attrsonly = 0; /* 0: return values too */
6088 LDAPMessage *result = NULL;
6089 char *trusted_dn;
6090 uint32_t num_result;
6092 filter = talloc_asprintf(talloc_tos(),
6093 "(&(objectClass=%s)(sambaDomainName=%s))",
6094 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6096 trusted_dn = trusteddom_dn(ldap_state, domain);
6097 if (trusted_dn == NULL) {
6098 return False;
6100 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6101 filter, attrs, attrsonly, &result);
6103 if (result != NULL) {
6104 talloc_autofree_ldapmsg(mem_ctx, result);
6107 if (rc == LDAP_NO_SUCH_OBJECT) {
6108 *entry = NULL;
6109 return True;
6112 if (rc != LDAP_SUCCESS) {
6113 return False;
6116 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6118 if (num_result > 1) {
6119 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6120 "%s object for domain '%s'?!\n",
6121 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6122 return False;
6125 if (num_result == 0) {
6126 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6127 "%s object for domain %s.\n",
6128 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6129 *entry = NULL;
6130 } else {
6131 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6134 return True;
6137 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6138 const char *domain,
6139 char** pwd,
6140 struct dom_sid *sid,
6141 time_t *pass_last_set_time)
6143 struct ldapsam_privates *ldap_state =
6144 (struct ldapsam_privates *)methods->private_data;
6145 LDAPMessage *entry = NULL;
6147 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6149 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6150 (entry == NULL))
6152 return False;
6155 /* password */
6156 if (pwd != NULL) {
6157 char *pwd_str;
6158 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6159 entry, "sambaClearTextPassword", talloc_tos());
6160 if (pwd_str == NULL) {
6161 return False;
6163 /* trusteddom_pw routines do not use talloc yet... */
6164 *pwd = SMB_STRDUP(pwd_str);
6165 if (*pwd == NULL) {
6166 return False;
6170 /* last change time */
6171 if (pass_last_set_time != NULL) {
6172 char *time_str;
6173 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6174 entry, "sambaPwdLastSet", talloc_tos());
6175 if (time_str == NULL) {
6176 return False;
6178 *pass_last_set_time = (time_t)atol(time_str);
6181 /* domain sid */
6182 if (sid != NULL) {
6183 char *sid_str;
6184 struct dom_sid dom_sid;
6185 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6186 entry, "sambaSID",
6187 talloc_tos());
6188 if (sid_str == NULL) {
6189 return False;
6191 if (!string_to_sid(&dom_sid, sid_str)) {
6192 return False;
6194 sid_copy(sid, &dom_sid);
6197 return True;
6200 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6201 const char* domain,
6202 const char* pwd,
6203 const struct dom_sid *sid)
6205 struct ldapsam_privates *ldap_state =
6206 (struct ldapsam_privates *)methods->private_data;
6207 LDAPMessage *entry = NULL;
6208 LDAPMod **mods = NULL;
6209 char *prev_pwd = NULL;
6210 char *trusted_dn = NULL;
6211 int rc;
6213 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6216 * get the current entry (if there is one) in order to put the
6217 * current password into the previous password attribute
6219 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6220 return False;
6223 mods = NULL;
6224 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6225 LDAP_OBJ_TRUSTDOM_PASSWORD);
6226 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6227 domain);
6228 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6229 sid_string_tos(sid));
6230 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6231 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6232 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6233 "sambaClearTextPassword", pwd);
6235 if (entry != NULL) {
6236 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6237 entry, "sambaClearTextPassword", talloc_tos());
6238 if (prev_pwd != NULL) {
6239 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6240 "sambaPreviousClearTextPassword",
6241 prev_pwd);
6245 talloc_autofree_ldapmod(talloc_tos(), mods);
6247 trusted_dn = trusteddom_dn(ldap_state, domain);
6248 if (trusted_dn == NULL) {
6249 return False;
6251 if (entry == NULL) {
6252 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6253 } else {
6254 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6257 if (rc != LDAP_SUCCESS) {
6258 DEBUG(1, ("error writing trusted domain password!\n"));
6259 return False;
6262 return True;
6265 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6266 const char *domain)
6268 int rc;
6269 struct ldapsam_privates *ldap_state =
6270 (struct ldapsam_privates *)methods->private_data;
6271 LDAPMessage *entry = NULL;
6272 const char *trusted_dn;
6274 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6275 return False;
6278 if (entry == NULL) {
6279 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6280 "%s\n", domain));
6281 return True;
6284 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6285 entry);
6286 if (trusted_dn == NULL) {
6287 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6288 return False;
6291 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6292 if (rc != LDAP_SUCCESS) {
6293 return False;
6296 return True;
6299 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6300 TALLOC_CTX *mem_ctx,
6301 uint32_t *num_domains,
6302 struct trustdom_info ***domains)
6304 int rc;
6305 struct ldapsam_privates *ldap_state =
6306 (struct ldapsam_privates *)methods->private_data;
6307 char *filter;
6308 int scope = LDAP_SCOPE_SUBTREE;
6309 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6310 int attrsonly = 0; /* 0: return values too */
6311 LDAPMessage *result = NULL;
6312 LDAPMessage *entry = NULL;
6314 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6315 LDAP_OBJ_TRUSTDOM_PASSWORD);
6317 rc = smbldap_search(ldap_state->smbldap_state,
6318 ldap_state->domain_dn,
6319 scope,
6320 filter,
6321 attrs,
6322 attrsonly,
6323 &result);
6325 if (result != NULL) {
6326 talloc_autofree_ldapmsg(mem_ctx, result);
6329 if (rc != LDAP_SUCCESS) {
6330 return NT_STATUS_UNSUCCESSFUL;
6333 *num_domains = 0;
6334 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6335 DEBUG(1, ("talloc failed\n"));
6336 return NT_STATUS_NO_MEMORY;
6339 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6340 entry != NULL;
6341 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6343 char *dom_name, *dom_sid_str;
6344 struct trustdom_info *dom_info;
6346 dom_info = talloc(*domains, struct trustdom_info);
6347 if (dom_info == NULL) {
6348 DEBUG(1, ("talloc failed\n"));
6349 return NT_STATUS_NO_MEMORY;
6352 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6353 entry,
6354 "sambaDomainName",
6355 talloc_tos());
6356 if (dom_name == NULL) {
6357 DEBUG(1, ("talloc failed\n"));
6358 return NT_STATUS_NO_MEMORY;
6360 dom_info->name = dom_name;
6362 dom_sid_str = smbldap_talloc_single_attribute(
6363 priv2ld(ldap_state), entry, "sambaSID",
6364 talloc_tos());
6365 if (dom_sid_str == NULL) {
6366 DEBUG(1, ("talloc failed\n"));
6367 return NT_STATUS_NO_MEMORY;
6369 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6370 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6371 dom_sid_str));
6372 return NT_STATUS_UNSUCCESSFUL;
6375 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6376 domains, num_domains);
6378 if (*domains == NULL) {
6379 DEBUG(1, ("talloc failed\n"));
6380 return NT_STATUS_NO_MEMORY;
6384 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6385 return NT_STATUS_OK;
6389 /**********************************************************************
6390 Housekeeping
6391 *********************************************************************/
6393 static void free_private_data(void **vp)
6395 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6397 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6399 if ((*ldap_state)->result != NULL) {
6400 ldap_msgfree((*ldap_state)->result);
6401 (*ldap_state)->result = NULL;
6403 if ((*ldap_state)->domain_dn != NULL) {
6404 SAFE_FREE((*ldap_state)->domain_dn);
6407 *ldap_state = NULL;
6409 /* No need to free any further, as it is talloc()ed */
6412 /*********************************************************************
6413 Intitalise the parts of the pdb_methods structure that are common to
6414 all pdb_ldap modes
6415 *********************************************************************/
6417 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6419 NTSTATUS nt_status;
6420 struct ldapsam_privates *ldap_state;
6421 char *bind_dn = NULL;
6422 char *bind_secret = NULL;
6424 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6425 return nt_status;
6428 (*pdb_method)->name = "ldapsam";
6430 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6431 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6432 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6433 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6434 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6435 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6437 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6438 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6439 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6440 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6441 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6442 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6443 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6445 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6446 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6448 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6450 (*pdb_method)->capabilities = ldapsam_capabilities;
6451 (*pdb_method)->new_rid = ldapsam_new_rid;
6453 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6454 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6455 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6456 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6458 /* TODO: Setup private data and free */
6460 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6461 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6462 return NT_STATUS_NO_MEMORY;
6465 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6466 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6467 return NT_STATUS_NO_MEMORY;
6470 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6471 location, false, bind_dn, bind_secret,
6472 &ldap_state->smbldap_state);
6473 memset(bind_secret, '\0', strlen(bind_secret));
6474 SAFE_FREE(bind_secret);
6475 SAFE_FREE(bind_dn);
6476 if ( !NT_STATUS_IS_OK(nt_status) ) {
6477 return nt_status;
6480 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6481 return NT_STATUS_NO_MEMORY;
6484 (*pdb_method)->private_data = ldap_state;
6486 (*pdb_method)->free_private_data = free_private_data;
6488 return NT_STATUS_OK;
6491 /**********************************************************************
6492 Initialise the 'compat' mode for pdb_ldap
6493 *********************************************************************/
6495 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6497 NTSTATUS nt_status;
6498 struct ldapsam_privates *ldap_state;
6499 char *uri = talloc_strdup( NULL, location );
6501 trim_char( uri, '\"', '\"' );
6502 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6503 if ( uri )
6504 TALLOC_FREE( uri );
6506 if ( !NT_STATUS_IS_OK(nt_status) ) {
6507 return nt_status;
6510 (*pdb_method)->name = "ldapsam_compat";
6512 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6513 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6515 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6517 return NT_STATUS_OK;
6520 /**********************************************************************
6521 Initialise the normal mode for pdb_ldap
6522 *********************************************************************/
6524 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6526 NTSTATUS nt_status;
6527 struct ldapsam_privates *ldap_state = NULL;
6528 uint32_t alg_rid_base;
6529 char *alg_rid_base_string = NULL;
6530 LDAPMessage *result = NULL;
6531 LDAPMessage *entry = NULL;
6532 struct dom_sid ldap_domain_sid;
6533 struct dom_sid secrets_domain_sid;
6534 char *domain_sid_string = NULL;
6535 char *dn = NULL;
6536 char *uri = talloc_strdup( NULL, location );
6538 trim_char( uri, '\"', '\"' );
6539 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6541 TALLOC_FREE(uri);
6543 if (!NT_STATUS_IS_OK(nt_status)) {
6544 return nt_status;
6547 (*pdb_method)->name = "ldapsam";
6549 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6550 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6551 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6552 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6553 (*pdb_method)->search_users = ldapsam_search_users;
6554 (*pdb_method)->search_groups = ldapsam_search_groups;
6555 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6557 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6558 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6559 (*pdb_method)->enum_group_memberships =
6560 ldapsam_enum_group_memberships;
6561 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6562 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6563 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6564 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6566 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6567 (*pdb_method)->create_user = ldapsam_create_user;
6568 (*pdb_method)->delete_user = ldapsam_delete_user;
6569 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6570 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6571 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6572 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6573 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6577 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6578 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6580 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6582 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6583 &result,
6584 ldap_state->domain_name, True);
6586 if ( !NT_STATUS_IS_OK(nt_status) ) {
6587 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6588 "info, nor add one to the domain. "
6589 "We cannot work reliably without it.\n"));
6590 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6593 /* Given that the above might fail, everything below this must be
6594 * optional */
6596 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6597 result);
6598 if (!entry) {
6599 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6600 "entry\n"));
6601 ldap_msgfree(result);
6602 return NT_STATUS_UNSUCCESSFUL;
6605 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6606 if (!dn) {
6607 ldap_msgfree(result);
6608 return NT_STATUS_UNSUCCESSFUL;
6611 ldap_state->domain_dn = smb_xstrdup(dn);
6612 TALLOC_FREE(dn);
6614 domain_sid_string = smbldap_talloc_single_attribute(
6615 ldap_state->smbldap_state->ldap_struct,
6616 entry,
6617 get_userattr_key2string(ldap_state->schema_ver,
6618 LDAP_ATTR_USER_SID),
6619 talloc_tos());
6621 if (domain_sid_string) {
6622 bool found_sid;
6623 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6624 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6625 "read as a valid SID\n", domain_sid_string));
6626 ldap_msgfree(result);
6627 TALLOC_FREE(domain_sid_string);
6628 return NT_STATUS_INVALID_PARAMETER;
6630 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6631 &secrets_domain_sid);
6632 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6633 &ldap_domain_sid)) {
6634 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6635 "%s based on pdb_ldap results %s -> %s\n",
6636 ldap_state->domain_name,
6637 sid_string_dbg(&secrets_domain_sid),
6638 sid_string_dbg(&ldap_domain_sid)));
6640 /* reset secrets.tdb sid */
6641 secrets_store_domain_sid(ldap_state->domain_name,
6642 &ldap_domain_sid);
6643 DEBUG(1, ("New global sam SID: %s\n",
6644 sid_string_dbg(get_global_sam_sid())));
6646 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6647 TALLOC_FREE(domain_sid_string);
6650 alg_rid_base_string = smbldap_talloc_single_attribute(
6651 ldap_state->smbldap_state->ldap_struct,
6652 entry,
6653 get_attr_key2string( dominfo_attr_list,
6654 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6655 talloc_tos());
6656 if (alg_rid_base_string) {
6657 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6658 if (alg_rid_base != algorithmic_rid_base()) {
6659 DEBUG(0, ("The value of 'algorithmic RID base' has "
6660 "changed since the LDAP\n"
6661 "database was initialised. Aborting. \n"));
6662 ldap_msgfree(result);
6663 TALLOC_FREE(alg_rid_base_string);
6664 return NT_STATUS_UNSUCCESSFUL;
6666 TALLOC_FREE(alg_rid_base_string);
6668 ldap_msgfree(result);
6670 return NT_STATUS_OK;
6673 NTSTATUS pdb_ldap_init(void)
6675 NTSTATUS nt_status;
6676 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6677 return nt_status;
6679 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6680 return nt_status;
6682 /* Let pdb_nds register backends */
6683 pdb_nds_init();
6685 pdb_ipa_init();
6687 return NT_STATUS_OK;