Talloc doc: Fix a cut&paste error
[Samba/gebeck_regimport.git] / source3 / passdb / pdb_ldap.c
blob04541e881de28787937f70aee0ef2fbdc05c7a5b
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 pass_must_change_time,
505 ldap_entry_time,
506 bad_password_time;
507 char *username = NULL,
508 *domain = NULL,
509 *nt_username = NULL,
510 *fullname = NULL,
511 *homedir = NULL,
512 *dir_drive = NULL,
513 *logon_script = NULL,
514 *profile_path = NULL,
515 *acct_desc = NULL,
516 *workstations = NULL,
517 *munged_dial = NULL;
518 uint32_t user_rid;
519 uint8 smblmpwd[LM_HASH_LEN],
520 smbntpwd[NT_HASH_LEN];
521 bool use_samba_attrs = True;
522 uint32_t acct_ctrl = 0;
523 uint16_t logon_divs;
524 uint16_t bad_password_count = 0,
525 logon_count = 0;
526 uint32_t hours_len;
527 uint8 hours[MAX_HOURS_LEN];
528 char *temp = NULL;
529 struct login_cache cache_entry;
530 uint32_t pwHistLen;
531 bool expand_explicit = lp_passdb_expand_explicit();
532 bool ret = false;
533 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
535 if (!ctx) {
536 return false;
538 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
539 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
540 goto fn_exit;
543 if (priv2ld(ldap_state) == NULL) {
544 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
545 "ldap_struct is NULL!\n"));
546 goto fn_exit;
549 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
550 entry,
551 "uid",
552 ctx))) {
553 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
554 "this user!\n"));
555 goto fn_exit;
558 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
560 nt_username = talloc_strdup(ctx, username);
561 if (!nt_username) {
562 goto fn_exit;
565 domain = talloc_strdup(ctx, ldap_state->domain_name);
566 if (!domain) {
567 goto fn_exit;
570 pdb_set_username(sampass, username, PDB_SET);
572 pdb_set_domain(sampass, domain, PDB_DEFAULT);
573 pdb_set_nt_username(sampass, nt_username, PDB_SET);
575 /* deal with different attributes between the schema first */
577 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
578 if ((temp = smbldap_talloc_single_attribute(
579 ldap_state->smbldap_state->ldap_struct,
580 entry,
581 get_userattr_key2string(ldap_state->schema_ver,
582 LDAP_ATTR_USER_SID),
583 ctx))!=NULL) {
584 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
586 } else {
587 if ((temp = smbldap_talloc_single_attribute(
588 ldap_state->smbldap_state->ldap_struct,
589 entry,
590 get_userattr_key2string(ldap_state->schema_ver,
591 LDAP_ATTR_USER_RID),
592 ctx))!=NULL) {
593 user_rid = (uint32_t)atol(temp);
594 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
598 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
599 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
600 get_userattr_key2string(ldap_state->schema_ver,
601 LDAP_ATTR_USER_SID),
602 get_userattr_key2string(ldap_state->schema_ver,
603 LDAP_ATTR_USER_RID),
604 username));
605 return False;
608 temp = smbldap_talloc_single_attribute(
609 ldap_state->smbldap_state->ldap_struct,
610 entry,
611 get_userattr_key2string(ldap_state->schema_ver,
612 LDAP_ATTR_PWD_LAST_SET),
613 ctx);
614 if (temp) {
615 pass_last_set_time = (time_t) atol(temp);
616 pdb_set_pass_last_set_time(sampass,
617 pass_last_set_time, PDB_SET);
620 temp = smbldap_talloc_single_attribute(
621 ldap_state->smbldap_state->ldap_struct,
622 entry,
623 get_userattr_key2string(ldap_state->schema_ver,
624 LDAP_ATTR_LOGON_TIME),
625 ctx);
626 if (temp) {
627 logon_time = (time_t) atol(temp);
628 pdb_set_logon_time(sampass, logon_time, PDB_SET);
631 temp = smbldap_talloc_single_attribute(
632 ldap_state->smbldap_state->ldap_struct,
633 entry,
634 get_userattr_key2string(ldap_state->schema_ver,
635 LDAP_ATTR_LOGOFF_TIME),
636 ctx);
637 if (temp) {
638 logoff_time = (time_t) atol(temp);
639 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
642 temp = smbldap_talloc_single_attribute(
643 ldap_state->smbldap_state->ldap_struct,
644 entry,
645 get_userattr_key2string(ldap_state->schema_ver,
646 LDAP_ATTR_KICKOFF_TIME),
647 ctx);
648 if (temp) {
649 kickoff_time = (time_t) atol(temp);
650 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
653 temp = smbldap_talloc_single_attribute(
654 ldap_state->smbldap_state->ldap_struct,
655 entry,
656 get_userattr_key2string(ldap_state->schema_ver,
657 LDAP_ATTR_PWD_CAN_CHANGE),
658 ctx);
659 if (temp) {
660 pass_can_change_time = (time_t) atol(temp);
661 pdb_set_pass_can_change_time(sampass,
662 pass_can_change_time, PDB_SET);
665 temp = smbldap_talloc_single_attribute(
666 ldap_state->smbldap_state->ldap_struct,
667 entry,
668 get_userattr_key2string(ldap_state->schema_ver,
669 LDAP_ATTR_PWD_MUST_CHANGE),
670 ctx);
671 if (temp) {
672 pass_must_change_time = (time_t) atol(temp);
673 pdb_set_pass_must_change_time(sampass,
674 pass_must_change_time, PDB_SET);
677 /* recommend that 'gecos' and 'displayName' should refer to the same
678 * attribute OID. userFullName depreciated, only used by Samba
679 * primary rules of LDAP: don't make a new attribute when one is already defined
680 * that fits your needs; using cn then displayName rather than 'userFullName'
683 fullname = smbldap_talloc_single_attribute(
684 ldap_state->smbldap_state->ldap_struct,
685 entry,
686 get_userattr_key2string(ldap_state->schema_ver,
687 LDAP_ATTR_DISPLAY_NAME),
688 ctx);
689 if (fullname) {
690 pdb_set_fullname(sampass, fullname, PDB_SET);
691 } else {
692 fullname = smbldap_talloc_single_attribute(
693 ldap_state->smbldap_state->ldap_struct,
694 entry,
695 get_userattr_key2string(ldap_state->schema_ver,
696 LDAP_ATTR_CN),
697 ctx);
698 if (fullname) {
699 pdb_set_fullname(sampass, fullname, PDB_SET);
703 dir_drive = smbldap_talloc_single_attribute(
704 ldap_state->smbldap_state->ldap_struct,
705 entry,
706 get_userattr_key2string(ldap_state->schema_ver,
707 LDAP_ATTR_HOME_DRIVE),
708 ctx);
709 if (dir_drive) {
710 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
711 } else {
712 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
715 homedir = smbldap_talloc_single_attribute(
716 ldap_state->smbldap_state->ldap_struct,
717 entry,
718 get_userattr_key2string(ldap_state->schema_ver,
719 LDAP_ATTR_HOME_PATH),
720 ctx);
721 if (homedir) {
722 if (expand_explicit) {
723 homedir = talloc_sub_basic(ctx,
724 username,
725 domain,
726 homedir);
727 if (!homedir) {
728 goto fn_exit;
731 pdb_set_homedir(sampass, homedir, PDB_SET);
732 } else {
733 pdb_set_homedir(sampass,
734 talloc_sub_basic(ctx, username, domain,
735 lp_logon_home()),
736 PDB_DEFAULT);
739 logon_script = smbldap_talloc_single_attribute(
740 ldap_state->smbldap_state->ldap_struct,
741 entry,
742 get_userattr_key2string(ldap_state->schema_ver,
743 LDAP_ATTR_LOGON_SCRIPT),
744 ctx);
745 if (logon_script) {
746 if (expand_explicit) {
747 logon_script = talloc_sub_basic(ctx,
748 username,
749 domain,
750 logon_script);
751 if (!logon_script) {
752 goto fn_exit;
755 pdb_set_logon_script(sampass, logon_script, PDB_SET);
756 } else {
757 pdb_set_logon_script(sampass,
758 talloc_sub_basic(ctx, username, domain,
759 lp_logon_script()),
760 PDB_DEFAULT );
763 profile_path = smbldap_talloc_single_attribute(
764 ldap_state->smbldap_state->ldap_struct,
765 entry,
766 get_userattr_key2string(ldap_state->schema_ver,
767 LDAP_ATTR_PROFILE_PATH),
768 ctx);
769 if (profile_path) {
770 if (expand_explicit) {
771 profile_path = talloc_sub_basic(ctx,
772 username,
773 domain,
774 profile_path);
775 if (!profile_path) {
776 goto fn_exit;
779 pdb_set_profile_path(sampass, profile_path, PDB_SET);
780 } else {
781 pdb_set_profile_path(sampass,
782 talloc_sub_basic(ctx, username, domain,
783 lp_logon_path()),
784 PDB_DEFAULT );
787 acct_desc = smbldap_talloc_single_attribute(
788 ldap_state->smbldap_state->ldap_struct,
789 entry,
790 get_userattr_key2string(ldap_state->schema_ver,
791 LDAP_ATTR_DESC),
792 ctx);
793 if (acct_desc) {
794 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
797 workstations = smbldap_talloc_single_attribute(
798 ldap_state->smbldap_state->ldap_struct,
799 entry,
800 get_userattr_key2string(ldap_state->schema_ver,
801 LDAP_ATTR_USER_WKS),
802 ctx);
803 if (workstations) {
804 pdb_set_workstations(sampass, workstations, PDB_SET);
807 munged_dial = smbldap_talloc_single_attribute(
808 ldap_state->smbldap_state->ldap_struct,
809 entry,
810 get_userattr_key2string(ldap_state->schema_ver,
811 LDAP_ATTR_MUNGED_DIAL),
812 ctx);
813 if (munged_dial) {
814 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
817 /* FIXME: hours stuff should be cleaner */
819 logon_divs = 168;
820 hours_len = 21;
821 memset(hours, 0xff, hours_len);
823 if (ldap_state->is_nds_ldap) {
824 char *user_dn;
825 size_t pwd_len;
826 char clear_text_pw[512];
828 /* Make call to Novell eDirectory ldap extension to get clear text password.
829 NOTE: This will only work if we have an SSL connection to eDirectory. */
830 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
831 if (user_dn != NULL) {
832 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
834 pwd_len = sizeof(clear_text_pw);
835 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
836 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
837 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
838 TALLOC_FREE(user_dn);
839 return False;
841 ZERO_STRUCT(smblmpwd);
842 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
843 TALLOC_FREE(user_dn);
844 return False;
846 ZERO_STRUCT(smbntpwd);
847 use_samba_attrs = False;
850 TALLOC_FREE(user_dn);
852 } else {
853 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
857 if (use_samba_attrs) {
858 temp = smbldap_talloc_single_attribute(
859 ldap_state->smbldap_state->ldap_struct,
860 entry,
861 get_userattr_key2string(ldap_state->schema_ver,
862 LDAP_ATTR_LMPW),
863 ctx);
864 if (temp) {
865 pdb_gethexpwd(temp, smblmpwd);
866 memset((char *)temp, '\0', strlen(temp)+1);
867 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
868 goto fn_exit;
870 ZERO_STRUCT(smblmpwd);
873 temp = smbldap_talloc_single_attribute(
874 ldap_state->smbldap_state->ldap_struct,
875 entry,
876 get_userattr_key2string(ldap_state->schema_ver,
877 LDAP_ATTR_NTPW),
878 ctx);
879 if (temp) {
880 pdb_gethexpwd(temp, smbntpwd);
881 memset((char *)temp, '\0', strlen(temp)+1);
882 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
883 goto fn_exit;
885 ZERO_STRUCT(smbntpwd);
889 pwHistLen = 0;
891 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
892 if (pwHistLen > 0){
893 uint8 *pwhist = NULL;
894 int i;
895 char *history_string = talloc_array(ctx, char,
896 MAX_PW_HISTORY_LEN*64);
898 if (!history_string) {
899 goto fn_exit;
902 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
904 pwhist = talloc_array(ctx, uint8,
905 pwHistLen * PW_HISTORY_ENTRY_LEN);
906 if (pwhist == NULL) {
907 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
908 goto fn_exit;
910 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
912 if (smbldap_get_single_attribute(
913 ldap_state->smbldap_state->ldap_struct,
914 entry,
915 get_userattr_key2string(ldap_state->schema_ver,
916 LDAP_ATTR_PWD_HISTORY),
917 history_string,
918 MAX_PW_HISTORY_LEN*64)) {
919 bool hex_failed = false;
920 for (i = 0; i < pwHistLen; i++){
921 /* Get the 16 byte salt. */
922 if (!pdb_gethexpwd(&history_string[i*64],
923 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
924 hex_failed = true;
925 break;
927 /* Get the 16 byte MD5 hash of salt+passwd. */
928 if (!pdb_gethexpwd(&history_string[(i*64)+32],
929 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
930 PW_HISTORY_SALT_LEN])) {
931 hex_failed = True;
932 break;
935 if (hex_failed) {
936 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
937 username));
938 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
941 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
942 goto fn_exit;
946 temp = smbldap_talloc_single_attribute(
947 ldap_state->smbldap_state->ldap_struct,
948 entry,
949 get_userattr_key2string(ldap_state->schema_ver,
950 LDAP_ATTR_ACB_INFO),
951 ctx);
952 if (temp) {
953 acct_ctrl = pdb_decode_acct_ctrl(temp);
955 if (acct_ctrl == 0) {
956 acct_ctrl |= ACB_NORMAL;
959 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
960 } else {
961 acct_ctrl |= ACB_NORMAL;
964 pdb_set_hours_len(sampass, hours_len, PDB_SET);
965 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
967 temp = smbldap_talloc_single_attribute(
968 ldap_state->smbldap_state->ldap_struct,
969 entry,
970 get_userattr_key2string(ldap_state->schema_ver,
971 LDAP_ATTR_BAD_PASSWORD_COUNT),
972 ctx);
973 if (temp) {
974 bad_password_count = (uint32_t) atol(temp);
975 pdb_set_bad_password_count(sampass,
976 bad_password_count, PDB_SET);
979 temp = smbldap_talloc_single_attribute(
980 ldap_state->smbldap_state->ldap_struct,
981 entry,
982 get_userattr_key2string(ldap_state->schema_ver,
983 LDAP_ATTR_BAD_PASSWORD_TIME),
984 ctx);
985 if (temp) {
986 bad_password_time = (time_t) atol(temp);
987 pdb_set_bad_password_time(sampass, bad_password_time, 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_COUNT),
996 ctx);
997 if (temp) {
998 logon_count = (uint32_t) atol(temp);
999 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1002 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1004 temp = smbldap_talloc_single_attribute(
1005 ldap_state->smbldap_state->ldap_struct,
1006 entry,
1007 get_userattr_key2string(ldap_state->schema_ver,
1008 LDAP_ATTR_LOGON_HOURS),
1009 ctx);
1010 if (temp) {
1011 pdb_gethexhours(temp, hours);
1012 memset((char *)temp, '\0', strlen(temp) +1);
1013 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
1014 ZERO_STRUCT(hours);
1017 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1018 struct passwd unix_pw;
1019 bool have_uid = false;
1020 bool have_gid = false;
1021 struct dom_sid mapped_gsid;
1022 const struct dom_sid *primary_gsid;
1024 ZERO_STRUCT(unix_pw);
1026 unix_pw.pw_name = username;
1027 unix_pw.pw_passwd = discard_const_p(char, "x");
1029 temp = smbldap_talloc_single_attribute(
1030 priv2ld(ldap_state),
1031 entry,
1032 "uidNumber",
1033 ctx);
1034 if (temp) {
1035 /* We've got a uid, feed the cache */
1036 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1037 have_uid = true;
1039 temp = smbldap_talloc_single_attribute(
1040 priv2ld(ldap_state),
1041 entry,
1042 "gidNumber",
1043 ctx);
1044 if (temp) {
1045 /* We've got a uid, feed the cache */
1046 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1047 have_gid = true;
1049 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1050 priv2ld(ldap_state),
1051 entry,
1052 "gecos",
1053 ctx);
1054 if (unix_pw.pw_gecos) {
1055 unix_pw.pw_gecos = fullname;
1057 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1058 priv2ld(ldap_state),
1059 entry,
1060 "homeDirectory",
1061 ctx);
1062 if (unix_pw.pw_dir) {
1063 unix_pw.pw_dir = discard_const_p(char, "");
1065 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1066 priv2ld(ldap_state),
1067 entry,
1068 "loginShell",
1069 ctx);
1070 if (unix_pw.pw_shell) {
1071 unix_pw.pw_shell = discard_const_p(char, "");
1074 if (have_uid && have_gid) {
1075 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1076 } else {
1077 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1080 if (sampass->unix_pw == NULL) {
1081 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1082 pdb_get_username(sampass)));
1083 goto fn_exit;
1086 store_uid_sid_cache(pdb_get_user_sid(sampass),
1087 sampass->unix_pw->pw_uid);
1088 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
1089 sampass->unix_pw->pw_uid);
1091 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1092 primary_gsid = pdb_get_group_sid(sampass);
1093 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1094 store_gid_sid_cache(primary_gsid,
1095 sampass->unix_pw->pw_gid);
1096 idmap_cache_set_sid2gid(primary_gsid,
1097 sampass->unix_pw->pw_gid);
1101 /* check the timestamp of the cache vs ldap entry */
1102 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1103 entry))) {
1104 ret = true;
1105 goto fn_exit;
1108 /* see if we have newer updates */
1109 if (!login_cache_read(sampass, &cache_entry)) {
1110 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1111 (unsigned int)pdb_get_bad_password_count(sampass),
1112 (unsigned int)pdb_get_bad_password_time(sampass)));
1113 ret = true;
1114 goto fn_exit;
1117 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1118 (unsigned int)ldap_entry_time,
1119 (unsigned int)cache_entry.entry_timestamp,
1120 (unsigned int)cache_entry.bad_password_time));
1122 if (ldap_entry_time > cache_entry.entry_timestamp) {
1123 /* cache is older than directory , so
1124 we need to delete the entry but allow the
1125 fields to be written out */
1126 login_cache_delentry(sampass);
1127 } else {
1128 /* read cache in */
1129 pdb_set_acct_ctrl(sampass,
1130 pdb_get_acct_ctrl(sampass) |
1131 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1132 PDB_SET);
1133 pdb_set_bad_password_count(sampass,
1134 cache_entry.bad_password_count,
1135 PDB_SET);
1136 pdb_set_bad_password_time(sampass,
1137 cache_entry.bad_password_time,
1138 PDB_SET);
1141 ret = true;
1143 fn_exit:
1145 TALLOC_FREE(ctx);
1146 return ret;
1149 /**********************************************************************
1150 Initialize the ldap db from a struct samu. Called on update.
1151 (Based on init_buffer_from_sam in pdb_tdb.c)
1152 *********************************************************************/
1154 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1155 LDAPMessage *existing,
1156 LDAPMod *** mods, struct samu * sampass,
1157 bool (*need_update)(const struct samu *,
1158 enum pdb_elements))
1160 char *temp = NULL;
1161 uint32_t rid;
1163 if (mods == NULL || sampass == NULL) {
1164 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1165 return False;
1168 *mods = NULL;
1171 * took out adding "objectclass: sambaAccount"
1172 * do this on a per-mod basis
1174 if (need_update(sampass, PDB_USERNAME)) {
1175 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1176 "uid", pdb_get_username(sampass));
1177 if (ldap_state->is_nds_ldap) {
1178 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1179 "cn", pdb_get_username(sampass));
1180 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1181 "sn", pdb_get_username(sampass));
1185 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1187 /* only update the RID if we actually need to */
1188 if (need_update(sampass, PDB_USERSID)) {
1189 fstring sid_string;
1190 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1192 switch ( ldap_state->schema_ver ) {
1193 case SCHEMAVER_SAMBAACCOUNT:
1194 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1195 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1196 sid_string_dbg(user_sid),
1197 sid_string_dbg(
1198 &ldap_state->domain_sid)));
1199 return False;
1201 if (asprintf(&temp, "%i", rid) < 0) {
1202 return false;
1204 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1205 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1206 temp);
1207 SAFE_FREE(temp);
1208 break;
1210 case SCHEMAVER_SAMBASAMACCOUNT:
1211 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1212 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1213 sid_to_fstring(sid_string, user_sid));
1214 break;
1216 default:
1217 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1218 break;
1222 /* we don't need to store the primary group RID - so leaving it
1223 'free' to hang off the unix primary group makes life easier */
1225 if (need_update(sampass, PDB_GROUPSID)) {
1226 fstring sid_string;
1227 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1229 switch ( ldap_state->schema_ver ) {
1230 case SCHEMAVER_SAMBAACCOUNT:
1231 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1232 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1233 sid_string_dbg(group_sid),
1234 sid_string_dbg(
1235 &ldap_state->domain_sid)));
1236 return False;
1239 if (asprintf(&temp, "%i", rid) < 0) {
1240 return false;
1242 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1243 get_userattr_key2string(ldap_state->schema_ver,
1244 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1245 SAFE_FREE(temp);
1246 break;
1248 case SCHEMAVER_SAMBASAMACCOUNT:
1249 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1250 get_userattr_key2string(ldap_state->schema_ver,
1251 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1252 break;
1254 default:
1255 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1256 break;
1261 /* displayName, cn, and gecos should all be the same
1262 * most easily accomplished by giving them the same OID
1263 * gecos isn't set here b/c it should be handled by the
1264 * add-user script
1265 * We change displayName only and fall back to cn if
1266 * it does not exist.
1269 if (need_update(sampass, PDB_FULLNAME))
1270 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1271 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1272 pdb_get_fullname(sampass));
1274 if (need_update(sampass, PDB_ACCTDESC))
1275 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1276 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1277 pdb_get_acct_desc(sampass));
1279 if (need_update(sampass, PDB_WORKSTATIONS))
1280 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1281 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1282 pdb_get_workstations(sampass));
1284 if (need_update(sampass, PDB_MUNGEDDIAL))
1285 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1286 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1287 pdb_get_munged_dial(sampass));
1289 if (need_update(sampass, PDB_SMBHOME))
1290 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1291 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1292 pdb_get_homedir(sampass));
1294 if (need_update(sampass, PDB_DRIVE))
1295 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1296 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1297 pdb_get_dir_drive(sampass));
1299 if (need_update(sampass, PDB_LOGONSCRIPT))
1300 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1301 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1302 pdb_get_logon_script(sampass));
1304 if (need_update(sampass, PDB_PROFILE))
1305 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1306 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1307 pdb_get_profile_path(sampass));
1309 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1310 return false;
1312 if (need_update(sampass, PDB_LOGONTIME))
1313 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1314 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1315 SAFE_FREE(temp);
1317 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1318 return false;
1320 if (need_update(sampass, PDB_LOGOFFTIME))
1321 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1322 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1323 SAFE_FREE(temp);
1325 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1326 return false;
1328 if (need_update(sampass, PDB_KICKOFFTIME))
1329 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1330 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1331 SAFE_FREE(temp);
1333 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1334 return false;
1336 if (need_update(sampass, PDB_CANCHANGETIME))
1337 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1338 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1339 SAFE_FREE(temp);
1341 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1342 return false;
1344 if (need_update(sampass, PDB_MUSTCHANGETIME))
1345 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1346 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1347 SAFE_FREE(temp);
1349 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1350 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1352 if (need_update(sampass, PDB_LMPASSWD)) {
1353 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1354 if (lm_pw) {
1355 char pwstr[34];
1356 pdb_sethexpwd(pwstr, lm_pw,
1357 pdb_get_acct_ctrl(sampass));
1358 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1359 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1360 pwstr);
1361 } else {
1362 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1363 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1364 NULL);
1367 if (need_update(sampass, PDB_NTPASSWD)) {
1368 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1369 if (nt_pw) {
1370 char pwstr[34];
1371 pdb_sethexpwd(pwstr, nt_pw,
1372 pdb_get_acct_ctrl(sampass));
1373 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1374 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1375 pwstr);
1376 } else {
1377 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1378 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1379 NULL);
1383 if (need_update(sampass, PDB_PWHISTORY)) {
1384 char *pwstr = NULL;
1385 uint32_t pwHistLen = 0;
1386 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1388 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1389 if (!pwstr) {
1390 return false;
1392 if (pwHistLen == 0) {
1393 /* Remove any password history from the LDAP store. */
1394 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1395 pwstr[64] = '\0';
1396 } else {
1397 int i;
1398 uint32_t currHistLen = 0;
1399 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1400 if (pwhist != NULL) {
1401 /* We can only store (1024-1/64 password history entries. */
1402 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1403 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1404 /* Store the salt. */
1405 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1406 /* Followed by the md5 hash of salt + md4 hash */
1407 pdb_sethexpwd(&pwstr[(i*64)+32],
1408 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1409 DEBUG(100, ("pwstr=%s\n", pwstr));
1413 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1414 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1415 pwstr);
1416 SAFE_FREE(pwstr);
1419 if (need_update(sampass, PDB_PASSLASTSET)) {
1420 if (asprintf(&temp, "%li",
1421 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1422 return false;
1424 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1425 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1426 temp);
1427 SAFE_FREE(temp);
1431 if (need_update(sampass, PDB_HOURS)) {
1432 const uint8 *hours = pdb_get_hours(sampass);
1433 if (hours) {
1434 char hourstr[44];
1435 pdb_sethexhours(hourstr, hours);
1436 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1437 existing,
1438 mods,
1439 get_userattr_key2string(ldap_state->schema_ver,
1440 LDAP_ATTR_LOGON_HOURS),
1441 hourstr);
1445 if (need_update(sampass, PDB_ACCTCTRL))
1446 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1447 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1448 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1450 /* password lockout cache:
1451 - If we are now autolocking or clearing, we write to ldap
1452 - If we are clearing, we delete the cache entry
1453 - If the count is > 0, we update the cache
1455 This even means when autolocking, we cache, just in case the
1456 update doesn't work, and we have to cache the autolock flag */
1458 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1459 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1460 uint16_t badcount = pdb_get_bad_password_count(sampass);
1461 time_t badtime = pdb_get_bad_password_time(sampass);
1462 uint32_t pol;
1463 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1465 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1466 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1468 if ((badcount >= pol) || (badcount == 0)) {
1469 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1470 (unsigned int)badcount, (unsigned int)badtime));
1471 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1472 return false;
1474 smbldap_make_mod(
1475 ldap_state->smbldap_state->ldap_struct,
1476 existing, mods,
1477 get_userattr_key2string(
1478 ldap_state->schema_ver,
1479 LDAP_ATTR_BAD_PASSWORD_COUNT),
1480 temp);
1481 SAFE_FREE(temp);
1483 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1484 return false;
1486 smbldap_make_mod(
1487 ldap_state->smbldap_state->ldap_struct,
1488 existing, mods,
1489 get_userattr_key2string(
1490 ldap_state->schema_ver,
1491 LDAP_ATTR_BAD_PASSWORD_TIME),
1492 temp);
1493 SAFE_FREE(temp);
1495 if (badcount == 0) {
1496 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1497 login_cache_delentry(sampass);
1498 } else {
1499 struct login_cache cache_entry;
1501 cache_entry.entry_timestamp = time(NULL);
1502 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1503 cache_entry.bad_password_count = badcount;
1504 cache_entry.bad_password_time = badtime;
1506 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1507 login_cache_write(sampass, &cache_entry);
1511 return True;
1514 /**********************************************************************
1515 End enumeration of the LDAP password list.
1516 *********************************************************************/
1518 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1520 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1521 if (ldap_state->result) {
1522 ldap_msgfree(ldap_state->result);
1523 ldap_state->result = NULL;
1527 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1528 const char *new_attr)
1530 int i;
1532 if (new_attr == NULL) {
1533 return;
1536 for (i=0; (*attr_list)[i] != NULL; i++) {
1540 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1541 const char *, i+2);
1542 SMB_ASSERT((*attr_list) != NULL);
1543 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1544 (*attr_list)[i+1] = NULL;
1547 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1548 const char ***attr_list)
1550 append_attr(mem_ctx, attr_list, "uidNumber");
1551 append_attr(mem_ctx, attr_list, "gidNumber");
1552 append_attr(mem_ctx, attr_list, "homeDirectory");
1553 append_attr(mem_ctx, attr_list, "loginShell");
1554 append_attr(mem_ctx, attr_list, "gecos");
1557 /**********************************************************************
1558 Get struct samu entry from LDAP by username.
1559 *********************************************************************/
1561 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1563 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1564 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1565 LDAPMessage *result = NULL;
1566 LDAPMessage *entry = NULL;
1567 int count;
1568 const char ** attr_list;
1569 int rc;
1571 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1572 append_attr(user, &attr_list,
1573 get_userattr_key2string(ldap_state->schema_ver,
1574 LDAP_ATTR_MOD_TIMESTAMP));
1575 ldapsam_add_unix_attributes(user, &attr_list);
1576 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1577 attr_list);
1578 TALLOC_FREE( attr_list );
1580 if ( rc != LDAP_SUCCESS )
1581 return NT_STATUS_NO_SUCH_USER;
1583 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1585 if (count < 1) {
1586 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1587 ldap_msgfree(result);
1588 return NT_STATUS_NO_SUCH_USER;
1589 } else if (count > 1) {
1590 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1591 ldap_msgfree(result);
1592 return NT_STATUS_NO_SUCH_USER;
1595 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1596 if (entry) {
1597 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1598 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1599 ldap_msgfree(result);
1600 return NT_STATUS_NO_SUCH_USER;
1602 pdb_set_backend_private_data(user, result, NULL,
1603 my_methods, PDB_CHANGED);
1604 talloc_autofree_ldapmsg(user, result);
1605 ret = NT_STATUS_OK;
1606 } else {
1607 ldap_msgfree(result);
1609 return ret;
1612 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1613 const struct dom_sid *sid, LDAPMessage **result)
1615 int rc = -1;
1616 const char ** attr_list;
1617 uint32_t rid;
1619 switch ( ldap_state->schema_ver ) {
1620 case SCHEMAVER_SAMBASAMACCOUNT: {
1621 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1622 if (tmp_ctx == NULL) {
1623 return LDAP_NO_MEMORY;
1626 attr_list = get_userattr_list(tmp_ctx,
1627 ldap_state->schema_ver);
1628 append_attr(tmp_ctx, &attr_list,
1629 get_userattr_key2string(
1630 ldap_state->schema_ver,
1631 LDAP_ATTR_MOD_TIMESTAMP));
1632 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1633 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1634 result, attr_list);
1635 TALLOC_FREE(tmp_ctx);
1637 if ( rc != LDAP_SUCCESS )
1638 return rc;
1639 break;
1642 case SCHEMAVER_SAMBAACCOUNT:
1643 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1644 return rc;
1647 attr_list = get_userattr_list(NULL,
1648 ldap_state->schema_ver);
1649 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1650 TALLOC_FREE( attr_list );
1652 if ( rc != LDAP_SUCCESS )
1653 return rc;
1654 break;
1656 return rc;
1659 /**********************************************************************
1660 Get struct samu entry from LDAP by SID.
1661 *********************************************************************/
1663 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1665 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1666 LDAPMessage *result = NULL;
1667 LDAPMessage *entry = NULL;
1668 int count;
1669 int rc;
1671 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1672 sid, &result);
1673 if (rc != LDAP_SUCCESS)
1674 return NT_STATUS_NO_SUCH_USER;
1676 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1678 if (count < 1) {
1679 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1680 "count=%d\n", sid_string_dbg(sid), count));
1681 ldap_msgfree(result);
1682 return NT_STATUS_NO_SUCH_USER;
1683 } else if (count > 1) {
1684 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1685 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1686 count));
1687 ldap_msgfree(result);
1688 return NT_STATUS_NO_SUCH_USER;
1691 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1692 if (!entry) {
1693 ldap_msgfree(result);
1694 return NT_STATUS_NO_SUCH_USER;
1697 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1698 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1699 ldap_msgfree(result);
1700 return NT_STATUS_NO_SUCH_USER;
1703 pdb_set_backend_private_data(user, result, NULL,
1704 my_methods, PDB_CHANGED);
1705 talloc_autofree_ldapmsg(user, result);
1706 return NT_STATUS_OK;
1709 /********************************************************************
1710 Do the actual modification - also change a plaintext passord if
1711 it it set.
1712 **********************************************************************/
1714 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1715 struct samu *newpwd, char *dn,
1716 LDAPMod **mods, int ldap_op,
1717 bool (*need_update)(const struct samu *, enum pdb_elements))
1719 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1720 int rc;
1722 if (!newpwd || !dn) {
1723 return NT_STATUS_INVALID_PARAMETER;
1726 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1727 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1728 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1729 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1730 BerElement *ber;
1731 struct berval *bv;
1732 char *retoid = NULL;
1733 struct berval *retdata = NULL;
1734 char *utf8_password;
1735 char *utf8_dn;
1736 size_t converted_size;
1737 int ret;
1739 if (!ldap_state->is_nds_ldap) {
1741 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1742 LDAP_EXOP_MODIFY_PASSWD)) {
1743 DEBUG(2, ("ldap password change requested, but LDAP "
1744 "server does not support it -- ignoring\n"));
1745 return NT_STATUS_OK;
1749 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1750 pdb_get_plaintext_passwd(newpwd),
1751 &converted_size))
1753 return NT_STATUS_NO_MEMORY;
1756 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1757 TALLOC_FREE(utf8_password);
1758 return NT_STATUS_NO_MEMORY;
1761 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1762 DEBUG(0,("ber_alloc_t returns NULL\n"));
1763 TALLOC_FREE(utf8_password);
1764 TALLOC_FREE(utf8_dn);
1765 return NT_STATUS_UNSUCCESSFUL;
1768 if ((ber_printf (ber, "{") < 0) ||
1769 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1770 utf8_dn) < 0)) {
1771 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1772 "value <0\n"));
1773 ber_free(ber,1);
1774 TALLOC_FREE(utf8_dn);
1775 TALLOC_FREE(utf8_password);
1776 return NT_STATUS_UNSUCCESSFUL;
1779 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1780 ret = ber_printf(ber, "ts}",
1781 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1782 utf8_password);
1783 } else {
1784 ret = ber_printf(ber, "}");
1787 if (ret < 0) {
1788 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1789 "value <0\n"));
1790 ber_free(ber,1);
1791 TALLOC_FREE(utf8_dn);
1792 TALLOC_FREE(utf8_password);
1793 return NT_STATUS_UNSUCCESSFUL;
1796 if ((rc = ber_flatten (ber, &bv))<0) {
1797 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1798 ber_free(ber,1);
1799 TALLOC_FREE(utf8_dn);
1800 TALLOC_FREE(utf8_password);
1801 return NT_STATUS_UNSUCCESSFUL;
1804 TALLOC_FREE(utf8_dn);
1805 TALLOC_FREE(utf8_password);
1806 ber_free(ber, 1);
1808 if (!ldap_state->is_nds_ldap) {
1809 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1810 LDAP_EXOP_MODIFY_PASSWD,
1811 bv, NULL, NULL, &retoid,
1812 &retdata);
1813 } else {
1814 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1815 pdb_get_plaintext_passwd(newpwd));
1817 if (rc != LDAP_SUCCESS) {
1818 char *ld_error = NULL;
1820 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1821 DEBUG(3, ("Could not set userPassword "
1822 "attribute due to an objectClass "
1823 "violation -- ignoring\n"));
1824 ber_bvfree(bv);
1825 return NT_STATUS_OK;
1828 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1829 &ld_error);
1830 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1831 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1832 SAFE_FREE(ld_error);
1833 ber_bvfree(bv);
1834 #if defined(LDAP_CONSTRAINT_VIOLATION)
1835 if (rc == LDAP_CONSTRAINT_VIOLATION)
1836 return NT_STATUS_PASSWORD_RESTRICTION;
1837 #endif
1838 return NT_STATUS_UNSUCCESSFUL;
1839 } else {
1840 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1841 #ifdef DEBUG_PASSWORD
1842 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1843 #endif
1844 if (retdata)
1845 ber_bvfree(retdata);
1846 if (retoid)
1847 ldap_memfree(retoid);
1849 ber_bvfree(bv);
1852 if (!mods) {
1853 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1854 /* may be password change below however */
1855 } else {
1856 switch(ldap_op) {
1857 case LDAP_MOD_ADD:
1858 if (ldap_state->is_nds_ldap) {
1859 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1860 "objectclass",
1861 "inetOrgPerson");
1862 } else {
1863 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1864 "objectclass",
1865 LDAP_OBJ_ACCOUNT);
1867 rc = smbldap_add(ldap_state->smbldap_state,
1868 dn, mods);
1869 break;
1870 case LDAP_MOD_REPLACE:
1871 rc = smbldap_modify(ldap_state->smbldap_state,
1872 dn ,mods);
1873 break;
1874 default:
1875 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1876 ldap_op));
1877 return NT_STATUS_INVALID_PARAMETER;
1880 if (rc!=LDAP_SUCCESS) {
1881 return NT_STATUS_UNSUCCESSFUL;
1885 return NT_STATUS_OK;
1888 /**********************************************************************
1889 Delete entry from LDAP for username.
1890 *********************************************************************/
1892 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1893 struct samu * sam_acct)
1895 struct ldapsam_privates *priv =
1896 (struct ldapsam_privates *)my_methods->private_data;
1897 const char *sname;
1898 int rc;
1899 LDAPMessage *msg, *entry;
1900 NTSTATUS result = NT_STATUS_NO_MEMORY;
1901 const char **attr_list;
1902 TALLOC_CTX *mem_ctx;
1904 if (!sam_acct) {
1905 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1906 return NT_STATUS_INVALID_PARAMETER;
1909 sname = pdb_get_username(sam_acct);
1911 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1912 "LDAP.\n", sname));
1914 mem_ctx = talloc_new(NULL);
1915 if (mem_ctx == NULL) {
1916 DEBUG(0, ("talloc_new failed\n"));
1917 goto done;
1920 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1921 if (attr_list == NULL) {
1922 goto done;
1925 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1927 if ((rc != LDAP_SUCCESS) ||
1928 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1929 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1930 DEBUG(5, ("Could not find user %s\n", sname));
1931 result = NT_STATUS_NO_SUCH_USER;
1932 goto done;
1935 rc = ldapsam_delete_entry(
1936 priv, mem_ctx, entry,
1937 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1938 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1939 attr_list);
1941 result = (rc == LDAP_SUCCESS) ?
1942 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1944 done:
1945 TALLOC_FREE(mem_ctx);
1946 return result;
1949 /**********************************************************************
1950 Update struct samu.
1951 *********************************************************************/
1953 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1955 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1956 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1957 int rc = 0;
1958 char *dn;
1959 LDAPMessage *result = NULL;
1960 LDAPMessage *entry = NULL;
1961 LDAPMod **mods = NULL;
1962 const char **attr_list;
1964 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1965 if (!result) {
1966 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1967 if (pdb_get_username(newpwd) == NULL) {
1968 return NT_STATUS_INVALID_PARAMETER;
1970 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1971 TALLOC_FREE( attr_list );
1972 if (rc != LDAP_SUCCESS) {
1973 return NT_STATUS_UNSUCCESSFUL;
1975 pdb_set_backend_private_data(newpwd, result, NULL,
1976 my_methods, PDB_CHANGED);
1977 talloc_autofree_ldapmsg(newpwd, result);
1980 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1981 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1982 return NT_STATUS_UNSUCCESSFUL;
1985 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1986 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1987 if (!dn) {
1988 return NT_STATUS_UNSUCCESSFUL;
1991 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1993 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1994 pdb_element_is_changed)) {
1995 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1996 TALLOC_FREE(dn);
1997 if (mods != NULL)
1998 ldap_mods_free(mods,True);
1999 return NT_STATUS_UNSUCCESSFUL;
2002 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
2003 && (mods == NULL)) {
2004 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2005 pdb_get_username(newpwd)));
2006 TALLOC_FREE(dn);
2007 return NT_STATUS_OK;
2010 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
2012 if (mods != NULL) {
2013 ldap_mods_free(mods,True);
2016 TALLOC_FREE(dn);
2019 * We need to set the backend private data to NULL here. For example
2020 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2021 * same one, and with the explicit delete / add logic for attribute
2022 * values the second time we would use the wrong "old" value which
2023 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2024 * the update.
2025 * The existing LDAPMessage is still being auto-freed by the
2026 * destructor.
2028 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2029 PDB_CHANGED);
2031 if (!NT_STATUS_IS_OK(ret)) {
2032 return ret;
2035 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2036 pdb_get_username(newpwd)));
2037 return NT_STATUS_OK;
2040 /***************************************************************************
2041 Renames a struct samu
2042 - The "rename user script" has full responsibility for changing everything
2043 ***************************************************************************/
2045 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2046 TALLOC_CTX *tmp_ctx,
2047 uint32_t group_rid,
2048 uint32_t member_rid);
2050 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2051 TALLOC_CTX *mem_ctx,
2052 struct samu *user,
2053 struct dom_sid **pp_sids,
2054 gid_t **pp_gids,
2055 uint32_t *p_num_groups);
2057 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2058 struct samu *old_acct,
2059 const char *newname)
2061 const char *oldname;
2062 int rc;
2063 char *rename_script = NULL;
2064 fstring oldname_lower, newname_lower;
2066 if (!old_acct) {
2067 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2068 return NT_STATUS_INVALID_PARAMETER;
2070 if (!newname) {
2071 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2072 return NT_STATUS_INVALID_PARAMETER;
2075 oldname = pdb_get_username(old_acct);
2077 /* rename the posix user */
2078 rename_script = talloc_strdup(talloc_tos(), lp_renameuser_script());
2079 if (rename_script == NULL) {
2080 return NT_STATUS_NO_MEMORY;
2083 if (!(*rename_script)) {
2084 TALLOC_FREE(rename_script);
2085 return NT_STATUS_ACCESS_DENIED;
2088 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2089 oldname, newname));
2091 /* We have to allow the account name to end with a '$'.
2092 Also, follow the semantics in _samr_create_user() and lower case the
2093 posix name but preserve the case in passdb */
2095 fstrcpy( oldname_lower, oldname );
2096 strlower_m( oldname_lower );
2097 fstrcpy( newname_lower, newname );
2098 strlower_m( newname_lower );
2099 rename_script = realloc_string_sub2(rename_script,
2100 "%unew",
2101 newname_lower,
2102 true,
2103 true);
2104 if (!rename_script) {
2105 return NT_STATUS_NO_MEMORY;
2107 rename_script = realloc_string_sub2(rename_script,
2108 "%uold",
2109 oldname_lower,
2110 true,
2111 true);
2112 rc = smbrun(rename_script, NULL);
2114 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2115 rename_script, rc));
2117 TALLOC_FREE(rename_script);
2119 if (rc == 0) {
2120 smb_nscd_flush_user_cache();
2123 if (rc)
2124 return NT_STATUS_UNSUCCESSFUL;
2126 return NT_STATUS_OK;
2129 /**********************************************************************
2130 Add struct samu to LDAP.
2131 *********************************************************************/
2133 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2135 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2136 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2137 int rc;
2138 LDAPMessage *result = NULL;
2139 LDAPMessage *entry = NULL;
2140 LDAPMod **mods = NULL;
2141 int ldap_op = LDAP_MOD_REPLACE;
2142 uint32_t num_result;
2143 const char **attr_list;
2144 char *escape_user = NULL;
2145 const char *username = pdb_get_username(newpwd);
2146 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2147 char *filter = NULL;
2148 char *dn = NULL;
2149 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2150 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2152 if (!ctx) {
2153 return NT_STATUS_NO_MEMORY;
2156 if (!username || !*username) {
2157 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2158 status = NT_STATUS_INVALID_PARAMETER;
2159 goto fn_exit;
2162 /* free this list after the second search or in case we exit on failure */
2163 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2165 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2167 if (rc != LDAP_SUCCESS) {
2168 goto fn_exit;
2171 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2172 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2173 username));
2174 goto fn_exit;
2176 ldap_msgfree(result);
2177 result = NULL;
2179 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2180 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2181 sid, &result);
2182 if (rc == LDAP_SUCCESS) {
2183 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2184 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2185 "already in the base, with samba "
2186 "attributes\n", sid_string_dbg(sid)));
2187 goto fn_exit;
2189 ldap_msgfree(result);
2190 result = NULL;
2194 /* does the entry already exist but without a samba attributes?
2195 we need to return the samba attributes here */
2197 escape_user = escape_ldap_string(talloc_tos(), username);
2198 filter = talloc_strdup(attr_list, "(uid=%u)");
2199 if (!filter) {
2200 status = NT_STATUS_NO_MEMORY;
2201 goto fn_exit;
2203 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2204 TALLOC_FREE(escape_user);
2205 if (!filter) {
2206 status = NT_STATUS_NO_MEMORY;
2207 goto fn_exit;
2210 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2211 filter, attr_list, &result);
2212 if ( rc != LDAP_SUCCESS ) {
2213 goto fn_exit;
2216 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2218 if (num_result > 1) {
2219 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2220 goto fn_exit;
2223 /* Check if we need to update an existing entry */
2224 if (num_result == 1) {
2225 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2226 ldap_op = LDAP_MOD_REPLACE;
2227 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2228 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2229 if (!dn) {
2230 status = NT_STATUS_NO_MEMORY;
2231 goto fn_exit;
2234 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2236 /* There might be a SID for this account already - say an idmap entry */
2238 filter = talloc_asprintf(ctx,
2239 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2240 get_userattr_key2string(ldap_state->schema_ver,
2241 LDAP_ATTR_USER_SID),
2242 sid_string_talloc(ctx, sid),
2243 LDAP_OBJ_IDMAP_ENTRY,
2244 LDAP_OBJ_SID_ENTRY);
2245 if (!filter) {
2246 status = NT_STATUS_NO_MEMORY;
2247 goto fn_exit;
2250 /* free old result before doing a new search */
2251 if (result != NULL) {
2252 ldap_msgfree(result);
2253 result = NULL;
2255 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2256 filter, attr_list, &result);
2258 if ( rc != LDAP_SUCCESS ) {
2259 goto fn_exit;
2262 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2264 if (num_result > 1) {
2265 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2266 goto fn_exit;
2269 /* Check if we need to update an existing entry */
2270 if (num_result == 1) {
2272 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2273 ldap_op = LDAP_MOD_REPLACE;
2274 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2275 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2276 if (!dn) {
2277 status = NT_STATUS_NO_MEMORY;
2278 goto fn_exit;
2283 if (num_result == 0) {
2284 char *escape_username;
2285 /* Check if we need to add an entry */
2286 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2287 ldap_op = LDAP_MOD_ADD;
2289 escape_username = escape_rdn_val_string_alloc(username);
2290 if (!escape_username) {
2291 status = NT_STATUS_NO_MEMORY;
2292 goto fn_exit;
2295 if (username[strlen(username)-1] == '$') {
2296 dn = talloc_asprintf(ctx,
2297 "uid=%s,%s",
2298 escape_username,
2299 lp_ldap_machine_suffix());
2300 } else {
2301 dn = talloc_asprintf(ctx,
2302 "uid=%s,%s",
2303 escape_username,
2304 lp_ldap_user_suffix());
2307 SAFE_FREE(escape_username);
2308 if (!dn) {
2309 status = NT_STATUS_NO_MEMORY;
2310 goto fn_exit;
2314 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2315 pdb_element_is_set_or_changed)) {
2316 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2317 if (mods != NULL) {
2318 ldap_mods_free(mods, true);
2320 goto fn_exit;
2323 if (mods == NULL) {
2324 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2325 goto fn_exit;
2327 switch ( ldap_state->schema_ver ) {
2328 case SCHEMAVER_SAMBAACCOUNT:
2329 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2330 break;
2331 case SCHEMAVER_SAMBASAMACCOUNT:
2332 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2333 break;
2334 default:
2335 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2336 break;
2339 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2340 if (!NT_STATUS_IS_OK(ret)) {
2341 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2342 pdb_get_username(newpwd),dn));
2343 ldap_mods_free(mods, true);
2344 goto fn_exit;
2347 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2348 ldap_mods_free(mods, true);
2350 status = NT_STATUS_OK;
2352 fn_exit:
2354 TALLOC_FREE(ctx);
2355 if (result) {
2356 ldap_msgfree(result);
2358 return status;
2361 /**********************************************************************
2362 *********************************************************************/
2364 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2365 const char *filter,
2366 LDAPMessage ** result)
2368 int scope = LDAP_SCOPE_SUBTREE;
2369 int rc;
2370 const char **attr_list;
2372 attr_list = get_attr_list(NULL, groupmap_attr_list);
2373 rc = smbldap_search(ldap_state->smbldap_state,
2374 lp_ldap_suffix (), scope,
2375 filter, attr_list, 0, result);
2376 TALLOC_FREE(attr_list);
2378 return rc;
2381 /**********************************************************************
2382 *********************************************************************/
2384 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2385 GROUP_MAP *map, LDAPMessage *entry)
2387 char *temp = NULL;
2388 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2390 if (ldap_state == NULL || map == NULL || entry == NULL ||
2391 ldap_state->smbldap_state->ldap_struct == NULL) {
2392 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2393 TALLOC_FREE(ctx);
2394 return false;
2397 temp = smbldap_talloc_single_attribute(
2398 ldap_state->smbldap_state->ldap_struct,
2399 entry,
2400 get_attr_key2string(groupmap_attr_list,
2401 LDAP_ATTR_GIDNUMBER),
2402 ctx);
2403 if (!temp) {
2404 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2405 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2406 TALLOC_FREE(ctx);
2407 return false;
2409 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2411 map->gid = (gid_t)atol(temp);
2413 TALLOC_FREE(temp);
2414 temp = smbldap_talloc_single_attribute(
2415 ldap_state->smbldap_state->ldap_struct,
2416 entry,
2417 get_attr_key2string(groupmap_attr_list,
2418 LDAP_ATTR_GROUP_SID),
2419 ctx);
2420 if (!temp) {
2421 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2422 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2423 TALLOC_FREE(ctx);
2424 return false;
2427 if (!string_to_sid(&map->sid, temp)) {
2428 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2429 TALLOC_FREE(ctx);
2430 return false;
2433 TALLOC_FREE(temp);
2434 temp = smbldap_talloc_single_attribute(
2435 ldap_state->smbldap_state->ldap_struct,
2436 entry,
2437 get_attr_key2string(groupmap_attr_list,
2438 LDAP_ATTR_GROUP_TYPE),
2439 ctx);
2440 if (!temp) {
2441 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2442 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2443 TALLOC_FREE(ctx);
2444 return false;
2446 map->sid_name_use = (enum lsa_SidType)atol(temp);
2448 if ((map->sid_name_use < SID_NAME_USER) ||
2449 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2450 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2451 TALLOC_FREE(ctx);
2452 return false;
2455 TALLOC_FREE(temp);
2456 temp = smbldap_talloc_single_attribute(
2457 ldap_state->smbldap_state->ldap_struct,
2458 entry,
2459 get_attr_key2string(groupmap_attr_list,
2460 LDAP_ATTR_DISPLAY_NAME),
2461 ctx);
2462 if (!temp) {
2463 temp = smbldap_talloc_single_attribute(
2464 ldap_state->smbldap_state->ldap_struct,
2465 entry,
2466 get_attr_key2string(groupmap_attr_list,
2467 LDAP_ATTR_CN),
2468 ctx);
2469 if (!temp) {
2470 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2471 for gidNumber(%lu)\n",(unsigned long)map->gid));
2472 TALLOC_FREE(ctx);
2473 return false;
2476 map->nt_name = talloc_strdup(map, temp);
2477 if (!map->nt_name) {
2478 TALLOC_FREE(ctx);
2479 return false;
2482 TALLOC_FREE(temp);
2483 temp = smbldap_talloc_single_attribute(
2484 ldap_state->smbldap_state->ldap_struct,
2485 entry,
2486 get_attr_key2string(groupmap_attr_list,
2487 LDAP_ATTR_DESC),
2488 ctx);
2489 if (!temp) {
2490 temp = talloc_strdup(ctx, "");
2491 if (!temp) {
2492 TALLOC_FREE(ctx);
2493 return false;
2496 map->comment = talloc_strdup(map, temp);
2497 if (!map->comment) {
2498 TALLOC_FREE(ctx);
2499 return false;
2502 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2503 store_gid_sid_cache(&map->sid, map->gid);
2504 idmap_cache_set_sid2gid(&map->sid, map->gid);
2507 TALLOC_FREE(ctx);
2508 return true;
2511 /**********************************************************************
2512 *********************************************************************/
2514 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2515 const char *filter,
2516 GROUP_MAP *map)
2518 struct ldapsam_privates *ldap_state =
2519 (struct ldapsam_privates *)methods->private_data;
2520 LDAPMessage *result = NULL;
2521 LDAPMessage *entry = NULL;
2522 int count;
2524 if (ldapsam_search_one_group(ldap_state, filter, &result)
2525 != LDAP_SUCCESS) {
2526 return NT_STATUS_NO_SUCH_GROUP;
2529 count = ldap_count_entries(priv2ld(ldap_state), result);
2531 if (count < 1) {
2532 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2533 "%s\n", filter));
2534 ldap_msgfree(result);
2535 return NT_STATUS_NO_SUCH_GROUP;
2538 if (count > 1) {
2539 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2540 "count=%d\n", filter, count));
2541 ldap_msgfree(result);
2542 return NT_STATUS_NO_SUCH_GROUP;
2545 entry = ldap_first_entry(priv2ld(ldap_state), result);
2547 if (!entry) {
2548 ldap_msgfree(result);
2549 return NT_STATUS_UNSUCCESSFUL;
2552 if (!init_group_from_ldap(ldap_state, map, entry)) {
2553 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2554 "group filter %s\n", filter));
2555 ldap_msgfree(result);
2556 return NT_STATUS_NO_SUCH_GROUP;
2559 ldap_msgfree(result);
2560 return NT_STATUS_OK;
2563 /**********************************************************************
2564 *********************************************************************/
2566 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2567 struct dom_sid sid)
2569 char *filter = NULL;
2570 NTSTATUS status;
2571 fstring tmp;
2573 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2574 LDAP_OBJ_GROUPMAP,
2575 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2576 sid_to_fstring(tmp, &sid)) < 0) {
2577 return NT_STATUS_NO_MEMORY;
2580 status = ldapsam_getgroup(methods, filter, map);
2581 SAFE_FREE(filter);
2582 return status;
2585 /**********************************************************************
2586 *********************************************************************/
2588 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2589 gid_t gid)
2591 char *filter = NULL;
2592 NTSTATUS status;
2594 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2595 LDAP_OBJ_GROUPMAP,
2596 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2597 (unsigned long)gid) < 0) {
2598 return NT_STATUS_NO_MEMORY;
2601 status = ldapsam_getgroup(methods, filter, map);
2602 SAFE_FREE(filter);
2603 return status;
2606 /**********************************************************************
2607 *********************************************************************/
2609 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2610 const char *name)
2612 char *filter = NULL;
2613 char *escape_name = escape_ldap_string(talloc_tos(), name);
2614 NTSTATUS status;
2616 if (!escape_name) {
2617 return NT_STATUS_NO_MEMORY;
2620 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2621 LDAP_OBJ_GROUPMAP,
2622 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2623 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2624 escape_name) < 0) {
2625 TALLOC_FREE(escape_name);
2626 return NT_STATUS_NO_MEMORY;
2629 TALLOC_FREE(escape_name);
2630 status = ldapsam_getgroup(methods, filter, map);
2631 SAFE_FREE(filter);
2632 return status;
2635 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2636 LDAPMessage *entry,
2637 const struct dom_sid *domain_sid,
2638 uint32_t *rid)
2640 fstring str;
2641 struct dom_sid sid;
2643 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2644 str, sizeof(str)-1)) {
2645 DEBUG(10, ("Could not find sambaSID attribute\n"));
2646 return False;
2649 if (!string_to_sid(&sid, str)) {
2650 DEBUG(10, ("Could not convert string %s to sid\n", str));
2651 return False;
2654 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2655 DEBUG(10, ("SID %s is not in expected domain %s\n",
2656 str, sid_string_dbg(domain_sid)));
2657 return False;
2660 if (!sid_peek_rid(&sid, rid)) {
2661 DEBUG(10, ("Could not peek into RID\n"));
2662 return False;
2665 return True;
2668 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2669 TALLOC_CTX *mem_ctx,
2670 const struct dom_sid *group,
2671 uint32_t **pp_member_rids,
2672 size_t *p_num_members)
2674 struct ldapsam_privates *ldap_state =
2675 (struct ldapsam_privates *)methods->private_data;
2676 struct smbldap_state *conn = ldap_state->smbldap_state;
2677 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2678 const char *sid_attrs[] = { "sambaSID", NULL };
2679 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2680 LDAPMessage *result = NULL;
2681 LDAPMessage *entry;
2682 char *filter;
2683 char **values = NULL;
2684 char **memberuid;
2685 char *gidstr;
2686 int rc, count;
2688 *pp_member_rids = NULL;
2689 *p_num_members = 0;
2691 filter = talloc_asprintf(mem_ctx,
2692 "(&(objectClass=%s)"
2693 "(objectClass=%s)"
2694 "(sambaSID=%s))",
2695 LDAP_OBJ_POSIXGROUP,
2696 LDAP_OBJ_GROUPMAP,
2697 sid_string_talloc(mem_ctx, group));
2698 if (filter == NULL) {
2699 ret = NT_STATUS_NO_MEMORY;
2700 goto done;
2703 rc = smbldap_search(conn, lp_ldap_suffix(),
2704 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2705 &result);
2707 if (rc != LDAP_SUCCESS)
2708 goto done;
2710 talloc_autofree_ldapmsg(mem_ctx, result);
2712 count = ldap_count_entries(conn->ldap_struct, result);
2714 if (count > 1) {
2715 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2716 sid_string_dbg(group)));
2717 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2718 goto done;
2721 if (count == 0) {
2722 ret = NT_STATUS_NO_SUCH_GROUP;
2723 goto done;
2726 entry = ldap_first_entry(conn->ldap_struct, result);
2727 if (entry == NULL)
2728 goto done;
2730 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2731 if (!gidstr) {
2732 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2733 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2734 goto done;
2737 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2739 if ((values != NULL) && (values[0] != NULL)) {
2741 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2742 if (filter == NULL) {
2743 ret = NT_STATUS_NO_MEMORY;
2744 goto done;
2747 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2748 char *escape_memberuid;
2750 escape_memberuid = escape_ldap_string(talloc_tos(),
2751 *memberuid);
2752 if (escape_memberuid == NULL) {
2753 ret = NT_STATUS_NO_MEMORY;
2754 goto done;
2757 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2758 TALLOC_FREE(escape_memberuid);
2759 if (filter == NULL) {
2760 ret = NT_STATUS_NO_MEMORY;
2761 goto done;
2765 filter = talloc_asprintf_append_buffer(filter, "))");
2766 if (filter == NULL) {
2767 ret = NT_STATUS_NO_MEMORY;
2768 goto done;
2771 rc = smbldap_search(conn, lp_ldap_suffix(),
2772 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2773 &result);
2775 if (rc != LDAP_SUCCESS)
2776 goto done;
2778 count = ldap_count_entries(conn->ldap_struct, result);
2779 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2781 talloc_autofree_ldapmsg(mem_ctx, result);
2783 for (entry = ldap_first_entry(conn->ldap_struct, result);
2784 entry != NULL;
2785 entry = ldap_next_entry(conn->ldap_struct, entry))
2787 char *sidstr;
2788 struct dom_sid sid;
2789 uint32_t rid;
2791 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2792 entry, "sambaSID",
2793 mem_ctx);
2794 if (!sidstr) {
2795 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2796 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2797 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2798 goto done;
2801 if (!string_to_sid(&sid, sidstr))
2802 goto done;
2804 if (!sid_check_is_in_our_domain(&sid)) {
2805 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2806 "in our domain\n"));
2807 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2808 goto done;
2811 sid_peek_rid(&sid, &rid);
2813 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2814 p_num_members)) {
2815 ret = NT_STATUS_NO_MEMORY;
2816 goto done;
2821 filter = talloc_asprintf(mem_ctx,
2822 "(&(objectClass=%s)"
2823 "(gidNumber=%s))",
2824 LDAP_OBJ_SAMBASAMACCOUNT,
2825 gidstr);
2827 rc = smbldap_search(conn, lp_ldap_suffix(),
2828 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2829 &result);
2831 if (rc != LDAP_SUCCESS)
2832 goto done;
2834 talloc_autofree_ldapmsg(mem_ctx, result);
2836 for (entry = ldap_first_entry(conn->ldap_struct, result);
2837 entry != NULL;
2838 entry = ldap_next_entry(conn->ldap_struct, entry))
2840 uint32_t rid;
2842 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2843 entry,
2844 get_global_sam_sid(),
2845 &rid)) {
2846 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2847 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2848 goto done;
2851 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2852 p_num_members)) {
2853 ret = NT_STATUS_NO_MEMORY;
2854 goto done;
2858 ret = NT_STATUS_OK;
2860 done:
2862 if (values)
2863 ldap_value_free(values);
2865 return ret;
2868 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2869 TALLOC_CTX *mem_ctx,
2870 struct samu *user,
2871 struct dom_sid **pp_sids,
2872 gid_t **pp_gids,
2873 uint32_t *p_num_groups)
2875 struct ldapsam_privates *ldap_state =
2876 (struct ldapsam_privates *)methods->private_data;
2877 struct smbldap_state *conn = ldap_state->smbldap_state;
2878 char *filter;
2879 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2880 char *escape_name;
2881 int rc, count;
2882 LDAPMessage *result = NULL;
2883 LDAPMessage *entry;
2884 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2885 uint32_t num_sids;
2886 uint32_t num_gids;
2887 char *gidstr;
2888 gid_t primary_gid = -1;
2890 *pp_sids = NULL;
2891 num_sids = 0;
2893 if (pdb_get_username(user) == NULL) {
2894 return NT_STATUS_INVALID_PARAMETER;
2897 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2898 if (escape_name == NULL)
2899 return NT_STATUS_NO_MEMORY;
2901 if (user->unix_pw) {
2902 primary_gid = user->unix_pw->pw_gid;
2903 } else {
2904 /* retrieve the users primary gid */
2905 filter = talloc_asprintf(mem_ctx,
2906 "(&(objectClass=%s)(uid=%s))",
2907 LDAP_OBJ_SAMBASAMACCOUNT,
2908 escape_name);
2909 if (filter == NULL) {
2910 ret = NT_STATUS_NO_MEMORY;
2911 goto done;
2914 rc = smbldap_search(conn, lp_ldap_suffix(),
2915 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2917 if (rc != LDAP_SUCCESS)
2918 goto done;
2920 talloc_autofree_ldapmsg(mem_ctx, result);
2922 count = ldap_count_entries(priv2ld(ldap_state), result);
2924 switch (count) {
2925 case 0:
2926 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2927 ret = NT_STATUS_NO_SUCH_USER;
2928 goto done;
2929 case 1:
2930 entry = ldap_first_entry(priv2ld(ldap_state), result);
2932 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2933 if (!gidstr) {
2934 DEBUG (1, ("Unable to find the member's gid!\n"));
2935 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2936 goto done;
2938 primary_gid = strtoul(gidstr, NULL, 10);
2939 break;
2940 default:
2941 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2942 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2943 goto done;
2947 filter = talloc_asprintf(mem_ctx,
2948 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2949 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2950 if (filter == NULL) {
2951 ret = NT_STATUS_NO_MEMORY;
2952 goto done;
2955 rc = smbldap_search(conn, lp_ldap_suffix(),
2956 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2958 if (rc != LDAP_SUCCESS)
2959 goto done;
2961 talloc_autofree_ldapmsg(mem_ctx, result);
2963 num_gids = 0;
2964 *pp_gids = NULL;
2966 num_sids = 0;
2967 *pp_sids = NULL;
2969 /* We need to add the primary group as the first gid/sid */
2971 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2972 ret = NT_STATUS_NO_MEMORY;
2973 goto done;
2976 /* This sid will be replaced later */
2978 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2979 &num_sids);
2980 if (!NT_STATUS_IS_OK(ret)) {
2981 goto done;
2984 for (entry = ldap_first_entry(conn->ldap_struct, result);
2985 entry != NULL;
2986 entry = ldap_next_entry(conn->ldap_struct, entry))
2988 fstring str;
2989 struct dom_sid sid;
2990 gid_t gid;
2991 char *end;
2993 if (!smbldap_get_single_attribute(conn->ldap_struct,
2994 entry, "sambaSID",
2995 str, sizeof(str)-1))
2996 continue;
2998 if (!string_to_sid(&sid, str))
2999 goto done;
3001 if (!smbldap_get_single_attribute(conn->ldap_struct,
3002 entry, "gidNumber",
3003 str, sizeof(str)-1))
3004 continue;
3006 gid = strtoul(str, &end, 10);
3008 if (PTR_DIFF(end, str) != strlen(str))
3009 goto done;
3011 if (gid == primary_gid) {
3012 sid_copy(&(*pp_sids)[0], &sid);
3013 } else {
3014 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3015 &num_gids)) {
3016 ret = NT_STATUS_NO_MEMORY;
3017 goto done;
3019 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3020 &num_sids);
3021 if (!NT_STATUS_IS_OK(ret)) {
3022 goto done;
3027 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3028 DEBUG(3, ("primary group of [%s] not found\n",
3029 pdb_get_username(user)));
3030 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3031 goto done;
3034 *p_num_groups = num_sids;
3036 ret = NT_STATUS_OK;
3038 done:
3040 TALLOC_FREE(escape_name);
3041 return ret;
3044 /**********************************************************************
3045 * Augment a posixGroup object with a sambaGroupMapping domgroup
3046 *********************************************************************/
3048 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3049 struct ldapsam_privates *ldap_state,
3050 GROUP_MAP *map)
3052 const char *filter, *dn;
3053 LDAPMessage *msg, *entry;
3054 LDAPMod **mods;
3055 int rc;
3057 filter = talloc_asprintf(mem_ctx,
3058 "(&(objectClass=%s)(gidNumber=%u))",
3059 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3060 if (filter == NULL) {
3061 return NT_STATUS_NO_MEMORY;
3064 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3065 get_attr_list(mem_ctx, groupmap_attr_list),
3066 &msg);
3067 talloc_autofree_ldapmsg(mem_ctx, msg);
3069 if ((rc != LDAP_SUCCESS) ||
3070 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3071 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3072 return NT_STATUS_NO_SUCH_GROUP;
3075 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3076 if (dn == NULL) {
3077 return NT_STATUS_NO_MEMORY;
3080 mods = NULL;
3081 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3082 LDAP_OBJ_GROUPMAP);
3083 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3084 sid_string_talloc(mem_ctx, &map->sid));
3085 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3086 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3087 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3088 map->nt_name);
3089 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3090 map->comment);
3091 talloc_autofree_ldapmod(mem_ctx, mods);
3093 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3094 if (rc != LDAP_SUCCESS) {
3095 return NT_STATUS_ACCESS_DENIED;
3098 return NT_STATUS_OK;
3101 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3102 GROUP_MAP *map)
3104 struct ldapsam_privates *ldap_state =
3105 (struct ldapsam_privates *)methods->private_data;
3106 LDAPMessage *msg = NULL;
3107 LDAPMod **mods = NULL;
3108 const char *attrs[] = { NULL };
3109 char *filter;
3111 char *dn;
3112 TALLOC_CTX *mem_ctx;
3113 NTSTATUS result;
3115 struct dom_sid sid;
3117 int rc;
3119 mem_ctx = talloc_new(NULL);
3120 if (mem_ctx == NULL) {
3121 DEBUG(0, ("talloc_new failed\n"));
3122 return NT_STATUS_NO_MEMORY;
3125 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3126 sid_string_talloc(mem_ctx, &map->sid));
3127 if (filter == NULL) {
3128 result = NT_STATUS_NO_MEMORY;
3129 goto done;
3132 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3133 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3134 talloc_autofree_ldapmsg(mem_ctx, msg);
3136 if ((rc == LDAP_SUCCESS) &&
3137 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3139 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3140 "group mapping entry\n", sid_string_dbg(&map->sid)));
3141 result = NT_STATUS_GROUP_EXISTS;
3142 goto done;
3145 switch (map->sid_name_use) {
3147 case SID_NAME_DOM_GRP:
3148 /* To map a domain group we need to have a posix group
3149 to attach to. */
3150 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3151 goto done;
3152 break;
3154 case SID_NAME_ALIAS:
3155 if (!sid_check_is_in_our_domain(&map->sid)
3156 && !sid_check_is_in_builtin(&map->sid) )
3158 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3159 sid_string_dbg(&map->sid)));
3160 result = NT_STATUS_INVALID_PARAMETER;
3161 goto done;
3163 break;
3165 default:
3166 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3167 sid_type_lookup(map->sid_name_use)));
3168 result = NT_STATUS_INVALID_PARAMETER;
3169 goto done;
3172 /* Domain groups have been mapped in a separate routine, we have to
3173 * create an alias now */
3175 if (map->gid == -1) {
3176 DEBUG(10, ("Refusing to map gid==-1\n"));
3177 result = NT_STATUS_INVALID_PARAMETER;
3178 goto done;
3181 if (pdb_gid_to_sid(map->gid, &sid)) {
3182 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3183 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3184 result = NT_STATUS_GROUP_EXISTS;
3185 goto done;
3188 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3189 * the best we can get out of LDAP. */
3191 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3192 sid_string_talloc(mem_ctx, &map->sid),
3193 lp_ldap_group_suffix());
3194 if (dn == NULL) {
3195 result = NT_STATUS_NO_MEMORY;
3196 goto done;
3199 mods = NULL;
3201 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3202 LDAP_OBJ_SID_ENTRY);
3203 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3204 LDAP_OBJ_GROUPMAP);
3205 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3206 sid_string_talloc(mem_ctx, &map->sid));
3207 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3208 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3209 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3210 map->nt_name);
3211 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3212 map->comment);
3213 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3214 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3215 talloc_autofree_ldapmod(mem_ctx, mods);
3217 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3219 result = (rc == LDAP_SUCCESS) ?
3220 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3222 done:
3223 TALLOC_FREE(mem_ctx);
3224 return result;
3227 /**********************************************************************
3228 * Update a group mapping entry. We're quite strict about what can be changed:
3229 * Only the description and displayname may be changed. It simply does not
3230 * make any sense to change the SID, gid or the type in a mapping.
3231 *********************************************************************/
3233 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3234 GROUP_MAP *map)
3236 struct ldapsam_privates *ldap_state =
3237 (struct ldapsam_privates *)methods->private_data;
3238 int rc;
3239 const char *filter, *dn;
3240 LDAPMessage *msg = NULL;
3241 LDAPMessage *entry = NULL;
3242 LDAPMod **mods = NULL;
3243 TALLOC_CTX *mem_ctx;
3244 NTSTATUS result;
3246 mem_ctx = talloc_new(NULL);
3247 if (mem_ctx == NULL) {
3248 DEBUG(0, ("talloc_new failed\n"));
3249 return NT_STATUS_NO_MEMORY;
3252 /* Make 100% sure that sid, gid and type are not changed by looking up
3253 * exactly the values we're given in LDAP. */
3255 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3256 "(sambaSid=%s)(gidNumber=%u)"
3257 "(sambaGroupType=%d))",
3258 LDAP_OBJ_GROUPMAP,
3259 sid_string_talloc(mem_ctx, &map->sid),
3260 (unsigned int)map->gid, map->sid_name_use);
3261 if (filter == NULL) {
3262 result = NT_STATUS_NO_MEMORY;
3263 goto done;
3266 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3267 get_attr_list(mem_ctx, groupmap_attr_list),
3268 &msg);
3269 talloc_autofree_ldapmsg(mem_ctx, msg);
3271 if ((rc != LDAP_SUCCESS) ||
3272 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3273 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3274 result = NT_STATUS_NO_SUCH_GROUP;
3275 goto done;
3278 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3280 if (dn == NULL) {
3281 result = NT_STATUS_NO_MEMORY;
3282 goto done;
3285 mods = NULL;
3286 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3287 map->nt_name);
3288 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3289 map->comment);
3290 talloc_autofree_ldapmod(mem_ctx, mods);
3292 if (mods == NULL) {
3293 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3294 "nothing to do\n"));
3295 result = NT_STATUS_OK;
3296 goto done;
3299 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3301 if (rc != LDAP_SUCCESS) {
3302 result = NT_STATUS_ACCESS_DENIED;
3303 goto done;
3306 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3307 "group %lu in LDAP\n", (unsigned long)map->gid));
3309 result = NT_STATUS_OK;
3311 done:
3312 TALLOC_FREE(mem_ctx);
3313 return result;
3316 /**********************************************************************
3317 *********************************************************************/
3319 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3320 struct dom_sid sid)
3322 struct ldapsam_privates *priv =
3323 (struct ldapsam_privates *)methods->private_data;
3324 LDAPMessage *msg, *entry;
3325 int rc;
3326 NTSTATUS result;
3327 TALLOC_CTX *mem_ctx;
3328 char *filter;
3330 mem_ctx = talloc_new(NULL);
3331 if (mem_ctx == NULL) {
3332 DEBUG(0, ("talloc_new failed\n"));
3333 return NT_STATUS_NO_MEMORY;
3336 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3337 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3338 sid_string_talloc(mem_ctx, &sid));
3339 if (filter == NULL) {
3340 result = NT_STATUS_NO_MEMORY;
3341 goto done;
3343 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3344 get_attr_list(mem_ctx, groupmap_attr_list),
3345 &msg);
3346 talloc_autofree_ldapmsg(mem_ctx, msg);
3348 if ((rc != LDAP_SUCCESS) ||
3349 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3350 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3351 result = NT_STATUS_NO_SUCH_GROUP;
3352 goto done;
3355 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3356 get_attr_list(mem_ctx,
3357 groupmap_attr_list_to_delete));
3359 if ((rc == LDAP_NAMING_VIOLATION) ||
3360 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3361 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3362 const char *attrs[] = { "sambaGroupType", "description",
3363 "displayName", "sambaSIDList",
3364 NULL };
3366 /* Second try. Don't delete the sambaSID attribute, this is
3367 for "old" entries that are tacked on a winbind
3368 sambaIdmapEntry. */
3370 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3371 LDAP_OBJ_GROUPMAP, attrs);
3374 if ((rc == LDAP_NAMING_VIOLATION) ||
3375 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3376 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3377 const char *attrs[] = { "sambaGroupType", "description",
3378 "displayName", "sambaSIDList",
3379 "gidNumber", NULL };
3381 /* Third try. This is a post-3.0.21 alias (containing only
3382 * sambaSidEntry and sambaGroupMapping classes), we also have
3383 * to delete the gidNumber attribute, only the sambaSidEntry
3384 * remains */
3386 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3387 LDAP_OBJ_GROUPMAP, attrs);
3390 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3392 done:
3393 TALLOC_FREE(mem_ctx);
3394 return result;
3397 /**********************************************************************
3398 *********************************************************************/
3400 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3401 bool update)
3403 struct ldapsam_privates *ldap_state =
3404 (struct ldapsam_privates *)my_methods->private_data;
3405 char *filter = NULL;
3406 int rc;
3407 const char **attr_list;
3409 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3410 if (!filter) {
3411 return NT_STATUS_NO_MEMORY;
3413 attr_list = get_attr_list( NULL, groupmap_attr_list );
3414 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3415 LDAP_SCOPE_SUBTREE, filter,
3416 attr_list, 0, &ldap_state->result);
3417 TALLOC_FREE(attr_list);
3419 if (rc != LDAP_SUCCESS) {
3420 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3421 ldap_err2string(rc)));
3422 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3423 lp_ldap_suffix(), filter));
3424 ldap_msgfree(ldap_state->result);
3425 ldap_state->result = NULL;
3426 TALLOC_FREE(filter);
3427 return NT_STATUS_UNSUCCESSFUL;
3430 TALLOC_FREE(filter);
3432 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3433 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3434 ldap_state->result)));
3436 ldap_state->entry =
3437 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3438 ldap_state->result);
3439 ldap_state->index = 0;
3441 return NT_STATUS_OK;
3444 /**********************************************************************
3445 *********************************************************************/
3447 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3449 ldapsam_endsampwent(my_methods);
3452 /**********************************************************************
3453 *********************************************************************/
3455 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3456 GROUP_MAP *map)
3458 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3459 struct ldapsam_privates *ldap_state =
3460 (struct ldapsam_privates *)my_methods->private_data;
3461 bool bret = False;
3463 while (!bret) {
3464 if (!ldap_state->entry)
3465 return ret;
3467 ldap_state->index++;
3468 bret = init_group_from_ldap(ldap_state, map,
3469 ldap_state->entry);
3471 ldap_state->entry =
3472 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3473 ldap_state->entry);
3476 return NT_STATUS_OK;
3479 /**********************************************************************
3480 *********************************************************************/
3482 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3483 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3484 GROUP_MAP ***pp_rmap,
3485 size_t *p_num_entries,
3486 bool unix_only)
3488 GROUP_MAP *map = NULL;
3489 size_t entries = 0;
3491 *p_num_entries = 0;
3492 *pp_rmap = NULL;
3494 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3495 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3496 "passdb\n"));
3497 return NT_STATUS_ACCESS_DENIED;
3500 while (true) {
3502 map = talloc_zero(NULL, GROUP_MAP);
3503 if (!map) {
3504 return NT_STATUS_NO_MEMORY;
3507 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3508 TALLOC_FREE(map);
3509 break;
3512 if (sid_name_use != SID_NAME_UNKNOWN &&
3513 sid_name_use != map->sid_name_use) {
3514 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3515 "not of the requested type\n",
3516 map->nt_name));
3517 continue;
3519 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3520 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3521 "non mapped\n", map->nt_name));
3522 continue;
3525 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3526 GROUP_MAP *, entries + 1);
3527 if (!(*pp_rmap)) {
3528 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3529 "enlarge group map!\n"));
3530 return NT_STATUS_UNSUCCESSFUL;
3533 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3535 entries += 1;
3538 ldapsam_endsamgrent(methods);
3540 *p_num_entries = entries;
3542 return NT_STATUS_OK;
3545 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3546 const struct dom_sid *alias,
3547 const struct dom_sid *member,
3548 int modop)
3550 struct ldapsam_privates *ldap_state =
3551 (struct ldapsam_privates *)methods->private_data;
3552 char *dn = NULL;
3553 LDAPMessage *result = NULL;
3554 LDAPMessage *entry = NULL;
3555 int count;
3556 LDAPMod **mods = NULL;
3557 int rc;
3558 enum lsa_SidType type = SID_NAME_USE_NONE;
3559 fstring tmp;
3561 char *filter = NULL;
3563 if (sid_check_is_in_builtin(alias)) {
3564 type = SID_NAME_ALIAS;
3567 if (sid_check_is_in_our_domain(alias)) {
3568 type = SID_NAME_ALIAS;
3571 if (type == SID_NAME_USE_NONE) {
3572 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3573 sid_string_dbg(alias)));
3574 return NT_STATUS_NO_SUCH_ALIAS;
3577 if (asprintf(&filter,
3578 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3579 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3580 type) < 0) {
3581 return NT_STATUS_NO_MEMORY;
3584 if (ldapsam_search_one_group(ldap_state, filter,
3585 &result) != LDAP_SUCCESS) {
3586 SAFE_FREE(filter);
3587 return NT_STATUS_NO_SUCH_ALIAS;
3590 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3591 result);
3593 if (count < 1) {
3594 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3595 ldap_msgfree(result);
3596 SAFE_FREE(filter);
3597 return NT_STATUS_NO_SUCH_ALIAS;
3600 if (count > 1) {
3601 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3602 "filter %s: count=%d\n", filter, count));
3603 ldap_msgfree(result);
3604 SAFE_FREE(filter);
3605 return NT_STATUS_NO_SUCH_ALIAS;
3608 SAFE_FREE(filter);
3610 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3611 result);
3613 if (!entry) {
3614 ldap_msgfree(result);
3615 return NT_STATUS_UNSUCCESSFUL;
3618 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3619 if (!dn) {
3620 ldap_msgfree(result);
3621 return NT_STATUS_UNSUCCESSFUL;
3624 smbldap_set_mod(&mods, modop,
3625 get_attr_key2string(groupmap_attr_list,
3626 LDAP_ATTR_SID_LIST),
3627 sid_to_fstring(tmp, member));
3629 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3631 ldap_mods_free(mods, True);
3632 ldap_msgfree(result);
3633 TALLOC_FREE(dn);
3635 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3636 return NT_STATUS_MEMBER_IN_ALIAS;
3639 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3640 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3643 if (rc != LDAP_SUCCESS) {
3644 return NT_STATUS_UNSUCCESSFUL;
3647 return NT_STATUS_OK;
3650 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3651 const struct dom_sid *alias,
3652 const struct dom_sid *member)
3654 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3657 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3658 const struct dom_sid *alias,
3659 const struct dom_sid *member)
3661 return ldapsam_modify_aliasmem(methods, alias, member,
3662 LDAP_MOD_DELETE);
3665 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3666 const struct dom_sid *alias,
3667 TALLOC_CTX *mem_ctx,
3668 struct dom_sid **pp_members,
3669 size_t *p_num_members)
3671 struct ldapsam_privates *ldap_state =
3672 (struct ldapsam_privates *)methods->private_data;
3673 LDAPMessage *result = NULL;
3674 LDAPMessage *entry = NULL;
3675 int count;
3676 char **values = NULL;
3677 int i;
3678 char *filter = NULL;
3679 uint32_t num_members = 0;
3680 enum lsa_SidType type = SID_NAME_USE_NONE;
3681 fstring tmp;
3683 *pp_members = NULL;
3684 *p_num_members = 0;
3686 if (sid_check_is_in_builtin(alias)) {
3687 type = SID_NAME_ALIAS;
3690 if (sid_check_is_in_our_domain(alias)) {
3691 type = SID_NAME_ALIAS;
3694 if (type == SID_NAME_USE_NONE) {
3695 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3696 sid_string_dbg(alias)));
3697 return NT_STATUS_NO_SUCH_ALIAS;
3700 if (asprintf(&filter,
3701 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3702 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3703 type) < 0) {
3704 return NT_STATUS_NO_MEMORY;
3707 if (ldapsam_search_one_group(ldap_state, filter,
3708 &result) != LDAP_SUCCESS) {
3709 SAFE_FREE(filter);
3710 return NT_STATUS_NO_SUCH_ALIAS;
3713 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3714 result);
3716 if (count < 1) {
3717 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3718 ldap_msgfree(result);
3719 SAFE_FREE(filter);
3720 return NT_STATUS_NO_SUCH_ALIAS;
3723 if (count > 1) {
3724 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3725 "filter %s: count=%d\n", filter, count));
3726 ldap_msgfree(result);
3727 SAFE_FREE(filter);
3728 return NT_STATUS_NO_SUCH_ALIAS;
3731 SAFE_FREE(filter);
3733 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3734 result);
3736 if (!entry) {
3737 ldap_msgfree(result);
3738 return NT_STATUS_UNSUCCESSFUL;
3741 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3742 entry,
3743 get_attr_key2string(groupmap_attr_list,
3744 LDAP_ATTR_SID_LIST));
3746 if (values == NULL) {
3747 ldap_msgfree(result);
3748 return NT_STATUS_OK;
3751 count = ldap_count_values(values);
3753 for (i=0; i<count; i++) {
3754 struct dom_sid member;
3755 NTSTATUS status;
3757 if (!string_to_sid(&member, values[i]))
3758 continue;
3760 status = add_sid_to_array(mem_ctx, &member, pp_members,
3761 &num_members);
3762 if (!NT_STATUS_IS_OK(status)) {
3763 ldap_value_free(values);
3764 ldap_msgfree(result);
3765 return status;
3769 *p_num_members = num_members;
3770 ldap_value_free(values);
3771 ldap_msgfree(result);
3773 return NT_STATUS_OK;
3776 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3777 TALLOC_CTX *mem_ctx,
3778 const struct dom_sid *domain_sid,
3779 const struct dom_sid *members,
3780 size_t num_members,
3781 uint32_t **pp_alias_rids,
3782 size_t *p_num_alias_rids)
3784 struct ldapsam_privates *ldap_state =
3785 (struct ldapsam_privates *)methods->private_data;
3786 LDAP *ldap_struct;
3788 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3790 LDAPMessage *result = NULL;
3791 LDAPMessage *entry = NULL;
3792 int i;
3793 int rc;
3794 char *filter;
3795 enum lsa_SidType type = SID_NAME_USE_NONE;
3796 bool is_builtin = false;
3797 bool sid_added = false;
3799 *pp_alias_rids = NULL;
3800 *p_num_alias_rids = 0;
3802 if (sid_check_is_builtin(domain_sid)) {
3803 is_builtin = true;
3804 type = SID_NAME_ALIAS;
3807 if (sid_check_is_domain(domain_sid)) {
3808 type = SID_NAME_ALIAS;
3811 if (type == SID_NAME_USE_NONE) {
3812 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3813 sid_string_dbg(domain_sid)));
3814 return NT_STATUS_UNSUCCESSFUL;
3817 if (num_members == 0) {
3818 return NT_STATUS_OK;
3821 filter = talloc_asprintf(mem_ctx,
3822 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3823 LDAP_OBJ_GROUPMAP, type);
3825 for (i=0; i<num_members; i++)
3826 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3827 filter,
3828 sid_string_talloc(mem_ctx,
3829 &members[i]));
3831 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3833 if (filter == NULL) {
3834 return NT_STATUS_NO_MEMORY;
3837 if (is_builtin &&
3838 ldap_state->search_cache.filter &&
3839 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3840 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3841 result = ldap_state->search_cache.result;
3842 ldap_state->search_cache.result = NULL;
3843 } else {
3844 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3845 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3846 if (rc != LDAP_SUCCESS) {
3847 return NT_STATUS_UNSUCCESSFUL;
3849 talloc_autofree_ldapmsg(filter, result);
3852 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3854 for (entry = ldap_first_entry(ldap_struct, result);
3855 entry != NULL;
3856 entry = ldap_next_entry(ldap_struct, entry))
3858 fstring sid_str;
3859 struct dom_sid sid;
3860 uint32_t rid;
3862 if (!smbldap_get_single_attribute(ldap_struct, entry,
3863 LDAP_ATTRIBUTE_SID,
3864 sid_str,
3865 sizeof(sid_str)-1))
3866 continue;
3868 if (!string_to_sid(&sid, sid_str))
3869 continue;
3871 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3872 continue;
3874 sid_added = true;
3876 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3877 p_num_alias_rids)) {
3878 return NT_STATUS_NO_MEMORY;
3882 if (!is_builtin && !sid_added) {
3883 TALLOC_FREE(ldap_state->search_cache.filter);
3885 * Note: result is a talloc child of filter because of the
3886 * talloc_autofree_ldapmsg() usage
3888 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3889 ldap_state->search_cache.result = result;
3892 return NT_STATUS_OK;
3895 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3896 enum pdb_policy_type type,
3897 uint32_t value)
3899 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3900 int rc;
3901 LDAPMod **mods = NULL;
3902 fstring value_string;
3903 const char *policy_attr = NULL;
3905 struct ldapsam_privates *ldap_state =
3906 (struct ldapsam_privates *)methods->private_data;
3908 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3910 if (!ldap_state->domain_dn) {
3911 return NT_STATUS_INVALID_PARAMETER;
3914 policy_attr = get_account_policy_attr(type);
3915 if (policy_attr == NULL) {
3916 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3917 "policy\n"));
3918 return ntstatus;
3921 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3923 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3925 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3926 mods);
3928 ldap_mods_free(mods, True);
3930 if (rc != LDAP_SUCCESS) {
3931 return ntstatus;
3934 if (!cache_account_policy_set(type, value)) {
3935 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3936 "update local tdb cache\n"));
3937 return ntstatus;
3940 return NT_STATUS_OK;
3943 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3944 enum pdb_policy_type type,
3945 uint32_t value)
3947 return ldapsam_set_account_policy_in_ldap(methods, type,
3948 value);
3951 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3952 enum pdb_policy_type type,
3953 uint32_t *value)
3955 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3956 LDAPMessage *result = NULL;
3957 LDAPMessage *entry = NULL;
3958 int count;
3959 int rc;
3960 char **vals = NULL;
3961 char *filter;
3962 const char *policy_attr = NULL;
3964 struct ldapsam_privates *ldap_state =
3965 (struct ldapsam_privates *)methods->private_data;
3967 const char *attrs[2];
3969 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3971 if (!ldap_state->domain_dn) {
3972 return NT_STATUS_INVALID_PARAMETER;
3975 policy_attr = get_account_policy_attr(type);
3976 if (!policy_attr) {
3977 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3978 "policy index: %d\n", type));
3979 return ntstatus;
3982 attrs[0] = policy_attr;
3983 attrs[1] = NULL;
3985 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3986 if (filter == NULL) {
3987 return NT_STATUS_NO_MEMORY;
3989 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3990 LDAP_SCOPE_BASE, filter, attrs, 0,
3991 &result);
3992 TALLOC_FREE(filter);
3993 if (rc != LDAP_SUCCESS) {
3994 return ntstatus;
3997 count = ldap_count_entries(priv2ld(ldap_state), result);
3998 if (count < 1) {
3999 goto out;
4002 entry = ldap_first_entry(priv2ld(ldap_state), result);
4003 if (entry == NULL) {
4004 goto out;
4007 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4008 if (vals == NULL) {
4009 goto out;
4012 *value = (uint32_t)atol(vals[0]);
4014 ntstatus = NT_STATUS_OK;
4016 out:
4017 if (vals)
4018 ldap_value_free(vals);
4019 ldap_msgfree(result);
4021 return ntstatus;
4024 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4026 - if user hasn't decided to use account policies inside LDAP just reuse the
4027 old tdb values
4029 - if there is a valid cache entry, return that
4030 - if there is an LDAP entry, update cache and return
4031 - otherwise set to default, update cache and return
4033 Guenther
4035 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4036 enum pdb_policy_type type,
4037 uint32_t *value)
4039 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4041 if (cache_account_policy_get(type, value)) {
4042 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4043 "cache\n"));
4044 return NT_STATUS_OK;
4047 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4048 value);
4049 if (NT_STATUS_IS_OK(ntstatus)) {
4050 goto update_cache;
4053 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4054 "ldap\n"));
4056 #if 0
4057 /* should we automagically migrate old tdb value here ? */
4058 if (account_policy_get(type, value))
4059 goto update_ldap;
4061 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4062 "default\n", type));
4063 #endif
4065 if (!account_policy_get_default(type, value)) {
4066 return ntstatus;
4069 /* update_ldap: */
4071 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4072 if (!NT_STATUS_IS_OK(ntstatus)) {
4073 return ntstatus;
4076 update_cache:
4078 if (!cache_account_policy_set(type, *value)) {
4079 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4080 "tdb as a cache\n"));
4081 return NT_STATUS_UNSUCCESSFUL;
4084 return NT_STATUS_OK;
4087 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4088 const struct dom_sid *domain_sid,
4089 int num_rids,
4090 uint32_t *rids,
4091 const char **names,
4092 enum lsa_SidType *attrs)
4094 struct ldapsam_privates *ldap_state =
4095 (struct ldapsam_privates *)methods->private_data;
4096 LDAPMessage *msg = NULL;
4097 LDAPMessage *entry;
4098 char *allsids = NULL;
4099 int i, rc, num_mapped;
4100 NTSTATUS result = NT_STATUS_NO_MEMORY;
4101 TALLOC_CTX *mem_ctx;
4102 LDAP *ld;
4103 bool is_builtin;
4105 mem_ctx = talloc_new(NULL);
4106 if (mem_ctx == NULL) {
4107 DEBUG(0, ("talloc_new failed\n"));
4108 goto done;
4111 if (!sid_check_is_builtin(domain_sid) &&
4112 !sid_check_is_domain(domain_sid)) {
4113 result = NT_STATUS_INVALID_PARAMETER;
4114 goto done;
4117 if (num_rids == 0) {
4118 result = NT_STATUS_NONE_MAPPED;
4119 goto done;
4122 for (i=0; i<num_rids; i++)
4123 attrs[i] = SID_NAME_UNKNOWN;
4125 allsids = talloc_strdup(mem_ctx, "");
4126 if (allsids == NULL) {
4127 goto done;
4130 for (i=0; i<num_rids; i++) {
4131 struct dom_sid sid;
4132 sid_compose(&sid, domain_sid, rids[i]);
4133 allsids = talloc_asprintf_append_buffer(
4134 allsids, "(sambaSid=%s)",
4135 sid_string_talloc(mem_ctx, &sid));
4136 if (allsids == NULL) {
4137 goto done;
4141 /* First look for users */
4144 char *filter;
4145 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4147 filter = talloc_asprintf(
4148 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4149 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4151 if (filter == NULL) {
4152 goto done;
4155 rc = smbldap_search(ldap_state->smbldap_state,
4156 lp_ldap_user_suffix(),
4157 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4158 &msg);
4159 talloc_autofree_ldapmsg(mem_ctx, msg);
4162 if (rc != LDAP_SUCCESS)
4163 goto done;
4165 ld = ldap_state->smbldap_state->ldap_struct;
4166 num_mapped = 0;
4168 for (entry = ldap_first_entry(ld, msg);
4169 entry != NULL;
4170 entry = ldap_next_entry(ld, entry)) {
4171 uint32_t rid;
4172 int rid_index;
4173 const char *name;
4175 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4176 &rid)) {
4177 DEBUG(2, ("Could not find sid from ldap entry\n"));
4178 continue;
4181 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4182 names);
4183 if (name == NULL) {
4184 DEBUG(2, ("Could not retrieve uid attribute\n"));
4185 continue;
4188 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4189 if (rid == rids[rid_index])
4190 break;
4193 if (rid_index == num_rids) {
4194 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4195 continue;
4198 attrs[rid_index] = SID_NAME_USER;
4199 names[rid_index] = name;
4200 num_mapped += 1;
4203 if (num_mapped == num_rids) {
4204 /* No need to look for groups anymore -- we're done */
4205 result = NT_STATUS_OK;
4206 goto done;
4209 /* Same game for groups */
4212 char *filter;
4213 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4214 "sambaGroupType", NULL };
4216 filter = talloc_asprintf(
4217 mem_ctx, "(&(objectClass=%s)(|%s))",
4218 LDAP_OBJ_GROUPMAP, allsids);
4219 if (filter == NULL) {
4220 goto done;
4223 rc = smbldap_search(ldap_state->smbldap_state,
4224 lp_ldap_suffix(),
4225 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4226 &msg);
4227 talloc_autofree_ldapmsg(mem_ctx, msg);
4230 if (rc != LDAP_SUCCESS)
4231 goto done;
4233 /* ldap_struct might have changed due to a reconnect */
4235 ld = ldap_state->smbldap_state->ldap_struct;
4237 /* For consistency checks, we already checked we're only domain or builtin */
4239 is_builtin = sid_check_is_builtin(domain_sid);
4241 for (entry = ldap_first_entry(ld, msg);
4242 entry != NULL;
4243 entry = ldap_next_entry(ld, entry))
4245 uint32_t rid;
4246 int rid_index;
4247 const char *attr;
4248 enum lsa_SidType type;
4249 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4251 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4252 mem_ctx);
4253 if (attr == NULL) {
4254 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4255 dn));
4256 continue;
4259 type = (enum lsa_SidType)atol(attr);
4261 /* Consistency checks */
4262 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4263 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4264 (type != SID_NAME_DOM_GRP)))) {
4265 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4268 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4269 &rid)) {
4270 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4271 continue;
4274 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4276 if (attr == NULL) {
4277 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4278 dn));
4279 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4282 if (attr == NULL) {
4283 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4284 dn));
4285 continue;
4288 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4289 if (rid == rids[rid_index])
4290 break;
4293 if (rid_index == num_rids) {
4294 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4295 continue;
4298 attrs[rid_index] = type;
4299 names[rid_index] = attr;
4300 num_mapped += 1;
4303 result = NT_STATUS_NONE_MAPPED;
4305 if (num_mapped > 0)
4306 result = (num_mapped == num_rids) ?
4307 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4308 done:
4309 TALLOC_FREE(mem_ctx);
4310 return result;
4313 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4315 char *filter = NULL;
4316 char *escaped = NULL;
4317 char *result = NULL;
4319 if (asprintf(&filter, "(&%s(objectclass=%s))",
4320 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4321 goto done;
4324 escaped = escape_ldap_string(talloc_tos(), username);
4325 if (escaped == NULL) goto done;
4327 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4329 done:
4330 SAFE_FREE(filter);
4331 TALLOC_FREE(escaped);
4333 return result;
4336 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4338 int i, num = 0;
4339 va_list ap;
4340 const char **result;
4342 va_start(ap, mem_ctx);
4343 while (va_arg(ap, const char *) != NULL)
4344 num += 1;
4345 va_end(ap);
4347 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4348 return NULL;
4351 va_start(ap, mem_ctx);
4352 for (i=0; i<num; i++) {
4353 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4354 if (result[i] == NULL) {
4355 talloc_free(result);
4356 va_end(ap);
4357 return NULL;
4360 va_end(ap);
4362 result[num] = NULL;
4363 return result;
4366 struct ldap_search_state {
4367 struct smbldap_state *connection;
4369 uint32_t acct_flags;
4370 uint16_t group_type;
4372 const char *base;
4373 int scope;
4374 const char *filter;
4375 const char **attrs;
4376 int attrsonly;
4377 void *pagedresults_cookie;
4379 LDAPMessage *entries, *current_entry;
4380 bool (*ldap2displayentry)(struct ldap_search_state *state,
4381 TALLOC_CTX *mem_ctx,
4382 LDAP *ld, LDAPMessage *entry,
4383 struct samr_displayentry *result);
4386 static bool ldapsam_search_firstpage(struct pdb_search *search)
4388 struct ldap_search_state *state =
4389 (struct ldap_search_state *)search->private_data;
4390 LDAP *ld;
4391 int rc = LDAP_OPERATIONS_ERROR;
4393 state->entries = NULL;
4395 if (state->connection->paged_results) {
4396 rc = smbldap_search_paged(state->connection, state->base,
4397 state->scope, state->filter,
4398 state->attrs, state->attrsonly,
4399 lp_ldap_page_size(), &state->entries,
4400 &state->pagedresults_cookie);
4403 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4405 if (state->entries != NULL) {
4406 /* Left over from unsuccessful paged attempt */
4407 ldap_msgfree(state->entries);
4408 state->entries = NULL;
4411 rc = smbldap_search(state->connection, state->base,
4412 state->scope, state->filter, state->attrs,
4413 state->attrsonly, &state->entries);
4415 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4416 return False;
4418 /* Ok, the server was lying. It told us it could do paged
4419 * searches when it could not. */
4420 state->connection->paged_results = False;
4423 ld = state->connection->ldap_struct;
4424 if ( ld == NULL) {
4425 DEBUG(5, ("Don't have an LDAP connection right after a "
4426 "search\n"));
4427 return False;
4429 state->current_entry = ldap_first_entry(ld, state->entries);
4431 return True;
4434 static bool ldapsam_search_nextpage(struct pdb_search *search)
4436 struct ldap_search_state *state =
4437 (struct ldap_search_state *)search->private_data;
4438 int rc;
4440 if (!state->connection->paged_results) {
4441 /* There is no next page when there are no paged results */
4442 return False;
4445 rc = smbldap_search_paged(state->connection, state->base,
4446 state->scope, state->filter, state->attrs,
4447 state->attrsonly, lp_ldap_page_size(),
4448 &state->entries,
4449 &state->pagedresults_cookie);
4451 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4452 return False;
4454 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4456 if (state->current_entry == NULL) {
4457 ldap_msgfree(state->entries);
4458 state->entries = NULL;
4459 return false;
4462 return True;
4465 static bool ldapsam_search_next_entry(struct pdb_search *search,
4466 struct samr_displayentry *entry)
4468 struct ldap_search_state *state =
4469 (struct ldap_search_state *)search->private_data;
4470 bool result;
4472 retry:
4473 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4474 return False;
4476 if ((state->entries == NULL) &&
4477 !ldapsam_search_nextpage(search))
4478 return False;
4480 if (state->current_entry == NULL) {
4481 return false;
4484 result = state->ldap2displayentry(state, search,
4485 state->connection->ldap_struct,
4486 state->current_entry, entry);
4488 if (!result) {
4489 char *dn;
4490 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4491 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4492 if (dn != NULL) ldap_memfree(dn);
4495 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4497 if (state->current_entry == NULL) {
4498 ldap_msgfree(state->entries);
4499 state->entries = NULL;
4502 if (!result) goto retry;
4504 return True;
4507 static void ldapsam_search_end(struct pdb_search *search)
4509 struct ldap_search_state *state =
4510 (struct ldap_search_state *)search->private_data;
4511 int rc;
4513 if (state->pagedresults_cookie == NULL)
4514 return;
4516 if (state->entries != NULL)
4517 ldap_msgfree(state->entries);
4519 state->entries = NULL;
4520 state->current_entry = NULL;
4522 if (!state->connection->paged_results)
4523 return;
4525 /* Tell the LDAP server we're not interested in the rest anymore. */
4527 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4528 state->filter, state->attrs,
4529 state->attrsonly, 0, &state->entries,
4530 &state->pagedresults_cookie);
4532 if (rc != LDAP_SUCCESS)
4533 DEBUG(5, ("Could not end search properly\n"));
4535 return;
4538 static bool ldapuser2displayentry(struct ldap_search_state *state,
4539 TALLOC_CTX *mem_ctx,
4540 LDAP *ld, LDAPMessage *entry,
4541 struct samr_displayentry *result)
4543 char **vals;
4544 size_t converted_size;
4545 struct dom_sid sid;
4546 uint32_t acct_flags;
4548 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4549 if ((vals == NULL) || (vals[0] == NULL)) {
4550 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4551 return False;
4553 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4554 ldap_value_free(vals);
4556 if ((state->acct_flags != 0) &&
4557 ((state->acct_flags & acct_flags) == 0))
4558 return False;
4560 result->acct_flags = acct_flags;
4561 result->account_name = "";
4562 result->fullname = "";
4563 result->description = "";
4565 vals = ldap_get_values(ld, entry, "uid");
4566 if ((vals == NULL) || (vals[0] == NULL)) {
4567 DEBUG(5, ("\"uid\" not found\n"));
4568 return False;
4570 if (!pull_utf8_talloc(mem_ctx,
4571 discard_const_p(char *, &result->account_name),
4572 vals[0], &converted_size))
4574 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4575 strerror(errno)));
4578 ldap_value_free(vals);
4580 vals = ldap_get_values(ld, entry, "displayName");
4581 if ((vals == NULL) || (vals[0] == NULL))
4582 DEBUG(8, ("\"displayName\" not found\n"));
4583 else if (!pull_utf8_talloc(mem_ctx,
4584 discard_const_p(char *, &result->fullname),
4585 vals[0], &converted_size))
4587 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4588 strerror(errno)));
4591 ldap_value_free(vals);
4593 vals = ldap_get_values(ld, entry, "description");
4594 if ((vals == NULL) || (vals[0] == NULL))
4595 DEBUG(8, ("\"description\" not found\n"));
4596 else if (!pull_utf8_talloc(mem_ctx,
4597 discard_const_p(char *, &result->description),
4598 vals[0], &converted_size))
4600 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4601 strerror(errno)));
4604 ldap_value_free(vals);
4606 if ((result->account_name == NULL) ||
4607 (result->fullname == NULL) ||
4608 (result->description == NULL)) {
4609 DEBUG(0, ("talloc failed\n"));
4610 return False;
4613 vals = ldap_get_values(ld, entry, "sambaSid");
4614 if ((vals == NULL) || (vals[0] == NULL)) {
4615 DEBUG(0, ("\"objectSid\" not found\n"));
4616 return False;
4619 if (!string_to_sid(&sid, vals[0])) {
4620 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4621 ldap_value_free(vals);
4622 return False;
4624 ldap_value_free(vals);
4626 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4627 DEBUG(0, ("sid %s does not belong to our domain\n",
4628 sid_string_dbg(&sid)));
4629 return False;
4632 return True;
4636 static bool ldapsam_search_users(struct pdb_methods *methods,
4637 struct pdb_search *search,
4638 uint32_t acct_flags)
4640 struct ldapsam_privates *ldap_state =
4641 (struct ldapsam_privates *)methods->private_data;
4642 struct ldap_search_state *state;
4644 state = talloc(search, struct ldap_search_state);
4645 if (state == NULL) {
4646 DEBUG(0, ("talloc failed\n"));
4647 return False;
4650 state->connection = ldap_state->smbldap_state;
4652 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4653 state->base = lp_ldap_user_suffix();
4654 else if ((acct_flags != 0) &&
4655 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4656 state->base = lp_ldap_machine_suffix();
4657 else
4658 state->base = lp_ldap_suffix();
4660 state->acct_flags = acct_flags;
4661 state->base = talloc_strdup(search, state->base);
4662 state->scope = LDAP_SCOPE_SUBTREE;
4663 state->filter = get_ldap_filter(search, "*");
4664 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4665 "displayName", "description",
4666 "sambaAcctFlags", NULL);
4667 state->attrsonly = 0;
4668 state->pagedresults_cookie = NULL;
4669 state->entries = NULL;
4670 state->ldap2displayentry = ldapuser2displayentry;
4672 if ((state->filter == NULL) || (state->attrs == NULL)) {
4673 DEBUG(0, ("talloc failed\n"));
4674 return False;
4677 search->private_data = state;
4678 search->next_entry = ldapsam_search_next_entry;
4679 search->search_end = ldapsam_search_end;
4681 return ldapsam_search_firstpage(search);
4684 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4685 TALLOC_CTX *mem_ctx,
4686 LDAP *ld, LDAPMessage *entry,
4687 struct samr_displayentry *result)
4689 char **vals;
4690 size_t converted_size;
4691 struct dom_sid sid;
4692 uint16_t group_type;
4694 result->account_name = "";
4695 result->fullname = "";
4696 result->description = "";
4699 vals = ldap_get_values(ld, entry, "sambaGroupType");
4700 if ((vals == NULL) || (vals[0] == NULL)) {
4701 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4702 if (vals != NULL) {
4703 ldap_value_free(vals);
4705 return False;
4708 group_type = atoi(vals[0]);
4710 if ((state->group_type != 0) &&
4711 ((state->group_type != group_type))) {
4712 ldap_value_free(vals);
4713 return False;
4716 ldap_value_free(vals);
4718 /* display name is the NT group name */
4720 vals = ldap_get_values(ld, entry, "displayName");
4721 if ((vals == NULL) || (vals[0] == NULL)) {
4722 DEBUG(8, ("\"displayName\" not found\n"));
4724 /* fallback to the 'cn' attribute */
4725 vals = ldap_get_values(ld, entry, "cn");
4726 if ((vals == NULL) || (vals[0] == NULL)) {
4727 DEBUG(5, ("\"cn\" not found\n"));
4728 return False;
4730 if (!pull_utf8_talloc(mem_ctx,
4731 discard_const_p(char *,
4732 &result->account_name),
4733 vals[0], &converted_size))
4735 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4736 "failed: %s", strerror(errno)));
4739 else if (!pull_utf8_talloc(mem_ctx,
4740 discard_const_p(char *,
4741 &result->account_name),
4742 vals[0], &converted_size))
4744 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4745 strerror(errno)));
4748 ldap_value_free(vals);
4750 vals = ldap_get_values(ld, entry, "description");
4751 if ((vals == NULL) || (vals[0] == NULL))
4752 DEBUG(8, ("\"description\" not found\n"));
4753 else if (!pull_utf8_talloc(mem_ctx,
4754 discard_const_p(char *, &result->description),
4755 vals[0], &converted_size))
4757 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4758 strerror(errno)));
4760 ldap_value_free(vals);
4762 if ((result->account_name == NULL) ||
4763 (result->fullname == NULL) ||
4764 (result->description == NULL)) {
4765 DEBUG(0, ("talloc failed\n"));
4766 return False;
4769 vals = ldap_get_values(ld, entry, "sambaSid");
4770 if ((vals == NULL) || (vals[0] == NULL)) {
4771 DEBUG(0, ("\"objectSid\" not found\n"));
4772 if (vals != NULL) {
4773 ldap_value_free(vals);
4775 return False;
4778 if (!string_to_sid(&sid, vals[0])) {
4779 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4780 return False;
4783 ldap_value_free(vals);
4785 switch (group_type) {
4786 case SID_NAME_DOM_GRP:
4787 case SID_NAME_ALIAS:
4789 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4790 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4792 DEBUG(0, ("%s is not in our domain\n",
4793 sid_string_dbg(&sid)));
4794 return False;
4796 break;
4798 default:
4799 DEBUG(0,("unknown group type: %d\n", group_type));
4800 return False;
4803 result->acct_flags = 0;
4805 return True;
4808 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4809 struct pdb_search *search,
4810 const struct dom_sid *sid,
4811 enum lsa_SidType type)
4813 struct ldapsam_privates *ldap_state =
4814 (struct ldapsam_privates *)methods->private_data;
4815 struct ldap_search_state *state;
4816 fstring tmp;
4818 state = talloc(search, struct ldap_search_state);
4819 if (state == NULL) {
4820 DEBUG(0, ("talloc failed\n"));
4821 return False;
4824 state->connection = ldap_state->smbldap_state;
4826 state->base = talloc_strdup(search, lp_ldap_suffix());
4827 state->connection = ldap_state->smbldap_state;
4828 state->scope = LDAP_SCOPE_SUBTREE;
4829 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4830 "(sambaGroupType=%d)(sambaSID=%s*))",
4831 LDAP_OBJ_GROUPMAP,
4832 type, sid_to_fstring(tmp, sid));
4833 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4834 "displayName", "description",
4835 "sambaGroupType", NULL);
4836 state->attrsonly = 0;
4837 state->pagedresults_cookie = NULL;
4838 state->entries = NULL;
4839 state->group_type = type;
4840 state->ldap2displayentry = ldapgroup2displayentry;
4842 if ((state->filter == NULL) || (state->attrs == NULL)) {
4843 DEBUG(0, ("talloc failed\n"));
4844 return False;
4847 search->private_data = state;
4848 search->next_entry = ldapsam_search_next_entry;
4849 search->search_end = ldapsam_search_end;
4851 return ldapsam_search_firstpage(search);
4854 static bool ldapsam_search_groups(struct pdb_methods *methods,
4855 struct pdb_search *search)
4857 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4860 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4861 struct pdb_search *search,
4862 const struct dom_sid *sid)
4864 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4867 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4869 return PDB_CAP_STORE_RIDS;
4872 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4873 uint32_t *rid)
4875 struct smbldap_state *smbldap_state = priv->smbldap_state;
4877 LDAPMessage *result = NULL;
4878 LDAPMessage *entry = NULL;
4879 LDAPMod **mods = NULL;
4880 NTSTATUS status;
4881 char *value;
4882 int rc;
4883 uint32_t nextRid = 0;
4884 const char *dn;
4886 TALLOC_CTX *mem_ctx;
4888 mem_ctx = talloc_new(NULL);
4889 if (mem_ctx == NULL) {
4890 DEBUG(0, ("talloc_new failed\n"));
4891 return NT_STATUS_NO_MEMORY;
4894 status = smbldap_search_domain_info(smbldap_state, &result,
4895 get_global_sam_name(), False);
4896 if (!NT_STATUS_IS_OK(status)) {
4897 DEBUG(3, ("Could not get domain info: %s\n",
4898 nt_errstr(status)));
4899 goto done;
4902 talloc_autofree_ldapmsg(mem_ctx, result);
4904 entry = ldap_first_entry(priv2ld(priv), result);
4905 if (entry == NULL) {
4906 DEBUG(0, ("Could not get domain info entry\n"));
4907 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4908 goto done;
4911 /* Find the largest of the three attributes "sambaNextRid",
4912 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4913 concept of differentiating between user and group rids, and will
4914 use only "sambaNextRid" in the future. But for compatibility
4915 reasons I look if others have chosen different strategies -- VL */
4917 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4918 "sambaNextRid", mem_ctx);
4919 if (value != NULL) {
4920 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4921 nextRid = MAX(nextRid, tmp);
4924 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4925 "sambaNextUserRid", mem_ctx);
4926 if (value != NULL) {
4927 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4928 nextRid = MAX(nextRid, tmp);
4931 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4932 "sambaNextGroupRid", mem_ctx);
4933 if (value != NULL) {
4934 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4935 nextRid = MAX(nextRid, tmp);
4938 if (nextRid == 0) {
4939 nextRid = BASE_RID-1;
4942 nextRid += 1;
4944 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4945 talloc_asprintf(mem_ctx, "%d", nextRid));
4946 talloc_autofree_ldapmod(mem_ctx, mods);
4948 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4949 status = NT_STATUS_NO_MEMORY;
4950 goto done;
4953 rc = smbldap_modify(smbldap_state, dn, mods);
4955 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4956 * please retry" */
4958 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4960 done:
4961 if (NT_STATUS_IS_OK(status)) {
4962 *rid = nextRid;
4965 TALLOC_FREE(mem_ctx);
4966 return status;
4969 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4971 int i;
4973 for (i=0; i<10; i++) {
4974 NTSTATUS result = ldapsam_get_new_rid(
4975 (struct ldapsam_privates *)methods->private_data, rid);
4976 if (NT_STATUS_IS_OK(result)) {
4977 return result;
4980 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4981 return result;
4984 /* The ldap update failed (maybe a race condition), retry */
4987 /* Tried 10 times, fail. */
4988 return NT_STATUS_ACCESS_DENIED;
4991 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4993 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4994 return NT_STATUS_IS_OK(result) ? True : False;
4997 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4998 const struct dom_sid *sid,
4999 uid_t *uid, gid_t *gid,
5000 enum lsa_SidType *type)
5002 struct ldapsam_privates *priv =
5003 (struct ldapsam_privates *)methods->private_data;
5004 char *filter;
5005 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5006 NULL };
5007 LDAPMessage *result = NULL;
5008 LDAPMessage *entry = NULL;
5009 bool ret = False;
5010 char *value;
5011 int rc;
5013 TALLOC_CTX *mem_ctx;
5015 mem_ctx = talloc_new(NULL);
5016 if (mem_ctx == NULL) {
5017 DEBUG(0, ("talloc_new failed\n"));
5018 return False;
5021 filter = talloc_asprintf(mem_ctx,
5022 "(&(sambaSid=%s)"
5023 "(|(objectClass=%s)(objectClass=%s)))",
5024 sid_string_talloc(mem_ctx, sid),
5025 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5026 if (filter == NULL) {
5027 DEBUG(5, ("talloc_asprintf failed\n"));
5028 goto done;
5031 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5032 attrs, &result);
5033 if (rc != LDAP_SUCCESS) {
5034 goto done;
5036 talloc_autofree_ldapmsg(mem_ctx, result);
5038 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5039 DEBUG(10, ("Got %d entries, expected one\n",
5040 ldap_count_entries(priv2ld(priv), result)));
5041 goto done;
5044 entry = ldap_first_entry(priv2ld(priv), result);
5046 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5047 "sambaGroupType", mem_ctx);
5049 if (value != NULL) {
5050 const char *gid_str;
5051 /* It's a group */
5053 gid_str = smbldap_talloc_single_attribute(
5054 priv2ld(priv), entry, "gidNumber", mem_ctx);
5055 if (gid_str == NULL) {
5056 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5057 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5058 entry)));
5059 goto done;
5062 *gid = strtoul(gid_str, NULL, 10);
5063 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
5064 store_gid_sid_cache(sid, *gid);
5065 idmap_cache_set_sid2gid(sid, *gid);
5066 ret = True;
5067 goto done;
5070 /* It must be a user */
5072 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5073 "uidNumber", mem_ctx);
5074 if (value == NULL) {
5075 DEBUG(1, ("Could not find uidNumber in %s\n",
5076 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5077 goto done;
5080 *uid = strtoul(value, NULL, 10);
5081 *type = SID_NAME_USER;
5082 store_uid_sid_cache(sid, *uid);
5083 idmap_cache_set_sid2uid(sid, *uid);
5085 ret = True;
5086 done:
5087 TALLOC_FREE(mem_ctx);
5088 return ret;
5092 * Find the SID for a uid.
5093 * This is shortcut is only used if ldapsam:trusted is set to true.
5095 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5096 struct dom_sid *sid)
5098 struct ldapsam_privates *priv =
5099 (struct ldapsam_privates *)methods->private_data;
5100 char *filter;
5101 const char *attrs[] = { "sambaSID", NULL };
5102 LDAPMessage *result = NULL;
5103 LDAPMessage *entry = NULL;
5104 bool ret = false;
5105 char *user_sid_string;
5106 struct dom_sid user_sid;
5107 int rc;
5108 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5110 filter = talloc_asprintf(tmp_ctx,
5111 "(&(uidNumber=%u)"
5112 "(objectClass=%s)"
5113 "(objectClass=%s))",
5114 (unsigned int)uid,
5115 LDAP_OBJ_POSIXACCOUNT,
5116 LDAP_OBJ_SAMBASAMACCOUNT);
5117 if (filter == NULL) {
5118 DEBUG(3, ("talloc_asprintf failed\n"));
5119 goto done;
5122 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5123 if (rc != LDAP_SUCCESS) {
5124 goto done;
5126 talloc_autofree_ldapmsg(tmp_ctx, result);
5128 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5129 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5130 ldap_count_entries(priv2ld(priv), result),
5131 (unsigned int)uid));
5132 goto done;
5135 entry = ldap_first_entry(priv2ld(priv), result);
5137 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5138 "sambaSID", tmp_ctx);
5139 if (user_sid_string == NULL) {
5140 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5141 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5142 goto done;
5145 if (!string_to_sid(&user_sid, user_sid_string)) {
5146 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5147 user_sid_string));
5148 goto done;
5151 sid_copy(sid, &user_sid);
5153 store_uid_sid_cache(sid, uid);
5154 idmap_cache_set_sid2uid(sid, uid);
5156 ret = true;
5158 done:
5159 TALLOC_FREE(tmp_ctx);
5160 return ret;
5164 * Find the SID for a gid.
5165 * This is shortcut is only used if ldapsam:trusted is set to true.
5167 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5168 struct dom_sid *sid)
5170 struct ldapsam_privates *priv =
5171 (struct ldapsam_privates *)methods->private_data;
5172 char *filter;
5173 const char *attrs[] = { "sambaSID", NULL };
5174 LDAPMessage *result = NULL;
5175 LDAPMessage *entry = NULL;
5176 bool ret = false;
5177 char *group_sid_string;
5178 struct dom_sid group_sid;
5179 int rc;
5180 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5182 filter = talloc_asprintf(tmp_ctx,
5183 "(&(gidNumber=%u)"
5184 "(objectClass=%s))",
5185 (unsigned int)gid,
5186 LDAP_OBJ_GROUPMAP);
5187 if (filter == NULL) {
5188 DEBUG(3, ("talloc_asprintf failed\n"));
5189 goto done;
5192 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5193 if (rc != LDAP_SUCCESS) {
5194 goto done;
5196 talloc_autofree_ldapmsg(tmp_ctx, result);
5198 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5199 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5200 ldap_count_entries(priv2ld(priv), result),
5201 (unsigned int)gid));
5202 goto done;
5205 entry = ldap_first_entry(priv2ld(priv), result);
5207 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5208 "sambaSID", tmp_ctx);
5209 if (group_sid_string == NULL) {
5210 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5211 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5212 goto done;
5215 if (!string_to_sid(&group_sid, group_sid_string)) {
5216 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5217 group_sid_string));
5218 goto done;
5221 sid_copy(sid, &group_sid);
5223 store_gid_sid_cache(sid, gid);
5224 idmap_cache_set_sid2gid(sid, gid);
5226 ret = true;
5228 done:
5229 TALLOC_FREE(tmp_ctx);
5230 return ret;
5235 * The following functions are called only if
5236 * ldapsam:trusted and ldapsam:editposix are
5237 * set to true
5241 * ldapsam_create_user creates a new
5242 * posixAccount and sambaSamAccount object
5243 * in the ldap users subtree
5245 * The uid is allocated by winbindd.
5248 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5249 TALLOC_CTX *tmp_ctx, const char *name,
5250 uint32_t acb_info, uint32_t *rid)
5252 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5253 LDAPMessage *entry = NULL;
5254 LDAPMessage *result = NULL;
5255 uint32_t num_result;
5256 bool is_machine = False;
5257 bool add_posix = False;
5258 LDAPMod **mods = NULL;
5259 struct samu *user;
5260 char *filter;
5261 char *username;
5262 char *homedir;
5263 char *gidstr;
5264 char *uidstr;
5265 char *shell;
5266 const char *dn = NULL;
5267 struct dom_sid group_sid;
5268 struct dom_sid user_sid;
5269 gid_t gid = -1;
5270 uid_t uid = -1;
5271 NTSTATUS ret;
5272 int rc;
5274 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5275 acb_info & ACB_WSTRUST ||
5276 acb_info & ACB_SVRTRUST ||
5277 acb_info & ACB_DOMTRUST) {
5278 is_machine = True;
5281 username = escape_ldap_string(talloc_tos(), name);
5282 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5283 username, LDAP_OBJ_POSIXACCOUNT);
5284 TALLOC_FREE(username);
5286 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5287 if (rc != LDAP_SUCCESS) {
5288 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5289 return NT_STATUS_ACCESS_DENIED;
5291 talloc_autofree_ldapmsg(tmp_ctx, result);
5293 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5295 if (num_result > 1) {
5296 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5297 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5300 if (num_result == 1) {
5301 char *tmp;
5302 /* check if it is just a posix account.
5303 * or if there is a sid attached to this entry
5306 entry = ldap_first_entry(priv2ld(ldap_state), result);
5307 if (!entry) {
5308 return NT_STATUS_UNSUCCESSFUL;
5311 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5312 if (tmp) {
5313 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5314 return NT_STATUS_USER_EXISTS;
5317 /* it is just a posix account, retrieve the dn for later use */
5318 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5319 if (!dn) {
5320 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5321 return NT_STATUS_NO_MEMORY;
5325 if (num_result == 0) {
5326 add_posix = True;
5329 /* Create the basic samu structure and generate the mods for the ldap commit */
5330 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5331 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5332 return ret;
5335 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5337 user = samu_new(tmp_ctx);
5338 if (!user) {
5339 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5340 return NT_STATUS_NO_MEMORY;
5343 if (!pdb_set_username(user, name, PDB_SET)) {
5344 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5345 return NT_STATUS_UNSUCCESSFUL;
5347 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5348 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5349 return NT_STATUS_UNSUCCESSFUL;
5351 if (is_machine) {
5352 if (acb_info & ACB_NORMAL) {
5353 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5354 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5355 return NT_STATUS_UNSUCCESSFUL;
5357 } else {
5358 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5359 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5360 return NT_STATUS_UNSUCCESSFUL;
5363 } else {
5364 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5365 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5366 return NT_STATUS_UNSUCCESSFUL;
5370 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5371 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5372 return NT_STATUS_UNSUCCESSFUL;
5375 if (!init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed)) {
5376 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5377 return NT_STATUS_UNSUCCESSFUL;
5380 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5381 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5383 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5385 if (add_posix) {
5386 char *escape_name;
5388 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5390 /* retrieve the Domain Users group gid */
5391 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5392 !sid_to_gid(&group_sid, &gid)) {
5393 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5394 return NT_STATUS_INVALID_PRIMARY_GROUP;
5397 /* lets allocate a new userid for this user */
5398 if (!winbind_allocate_uid(&uid)) {
5399 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5400 return NT_STATUS_UNSUCCESSFUL;
5404 if (is_machine) {
5405 /* TODO: choose a more appropriate default for machines */
5406 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5407 shell = talloc_strdup(tmp_ctx, "/bin/false");
5408 } else {
5409 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5410 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5412 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5413 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5415 escape_name = escape_rdn_val_string_alloc(name);
5416 if (!escape_name) {
5417 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5418 return NT_STATUS_NO_MEMORY;
5421 if (is_machine) {
5422 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5423 } else {
5424 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5427 SAFE_FREE(escape_name);
5429 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5430 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5431 return NT_STATUS_NO_MEMORY;
5434 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5435 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5436 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5437 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5438 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5439 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5440 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5443 talloc_autofree_ldapmod(tmp_ctx, mods);
5445 if (add_posix) {
5446 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5447 } else {
5448 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5451 if (rc != LDAP_SUCCESS) {
5452 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5453 return NT_STATUS_UNSUCCESSFUL;
5456 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5458 flush_pwnam_cache();
5460 return NT_STATUS_OK;
5463 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5465 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5466 LDAPMessage *result = NULL;
5467 LDAPMessage *entry = NULL;
5468 int num_result;
5469 const char *dn;
5470 char *filter;
5471 int rc;
5473 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5475 filter = talloc_asprintf(tmp_ctx,
5476 "(&(uid=%s)"
5477 "(objectClass=%s)"
5478 "(objectClass=%s))",
5479 pdb_get_username(sam_acct),
5480 LDAP_OBJ_POSIXACCOUNT,
5481 LDAP_OBJ_SAMBASAMACCOUNT);
5482 if (filter == NULL) {
5483 return NT_STATUS_NO_MEMORY;
5486 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5487 if (rc != LDAP_SUCCESS) {
5488 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5489 return NT_STATUS_UNSUCCESSFUL;
5491 talloc_autofree_ldapmsg(tmp_ctx, result);
5493 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5495 if (num_result == 0) {
5496 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5497 return NT_STATUS_NO_SUCH_USER;
5500 if (num_result > 1) {
5501 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5502 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5505 entry = ldap_first_entry(priv2ld(ldap_state), result);
5506 if (!entry) {
5507 return NT_STATUS_UNSUCCESSFUL;
5510 /* it is just a posix account, retrieve the dn for later use */
5511 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5512 if (!dn) {
5513 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5514 return NT_STATUS_NO_MEMORY;
5517 /* try to remove memberships first */
5519 NTSTATUS status;
5520 struct dom_sid *sids = NULL;
5521 gid_t *gids = NULL;
5522 uint32_t num_groups = 0;
5523 int i;
5524 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5526 status = ldapsam_enum_group_memberships(my_methods,
5527 tmp_ctx,
5528 sam_acct,
5529 &sids,
5530 &gids,
5531 &num_groups);
5532 if (!NT_STATUS_IS_OK(status)) {
5533 goto delete_dn;
5536 for (i=0; i < num_groups; i++) {
5538 uint32_t group_rid;
5540 sid_peek_rid(&sids[i], &group_rid);
5542 ldapsam_del_groupmem(my_methods,
5543 tmp_ctx,
5544 group_rid,
5545 user_rid);
5549 delete_dn:
5551 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5552 if (rc != LDAP_SUCCESS) {
5553 return NT_STATUS_UNSUCCESSFUL;
5556 flush_pwnam_cache();
5558 return NT_STATUS_OK;
5562 * ldapsam_create_group creates a new
5563 * posixGroup and sambaGroupMapping object
5564 * in the ldap groups subtree
5566 * The gid is allocated by winbindd.
5569 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5570 TALLOC_CTX *tmp_ctx,
5571 const char *name,
5572 uint32_t *rid)
5574 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5575 NTSTATUS ret;
5576 LDAPMessage *entry = NULL;
5577 LDAPMessage *result = NULL;
5578 uint32_t num_result;
5579 bool is_new_entry = False;
5580 LDAPMod **mods = NULL;
5581 char *filter;
5582 char *groupsidstr;
5583 char *groupname;
5584 char *grouptype;
5585 char *gidstr;
5586 const char *dn = NULL;
5587 struct dom_sid group_sid;
5588 gid_t gid = -1;
5589 int rc;
5591 groupname = escape_ldap_string(talloc_tos(), name);
5592 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5593 groupname, LDAP_OBJ_POSIXGROUP);
5594 TALLOC_FREE(groupname);
5596 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5597 if (rc != LDAP_SUCCESS) {
5598 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5599 return NT_STATUS_UNSUCCESSFUL;
5601 talloc_autofree_ldapmsg(tmp_ctx, result);
5603 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5605 if (num_result > 1) {
5606 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5607 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5610 if (num_result == 1) {
5611 char *tmp;
5612 /* check if it is just a posix group.
5613 * or if there is a sid attached to this entry
5616 entry = ldap_first_entry(priv2ld(ldap_state), result);
5617 if (!entry) {
5618 return NT_STATUS_UNSUCCESSFUL;
5621 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5622 if (tmp) {
5623 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5624 return NT_STATUS_GROUP_EXISTS;
5627 /* it is just a posix group, retrieve the gid and the dn for later use */
5628 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5629 if (!tmp) {
5630 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5631 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5634 gid = strtoul(tmp, NULL, 10);
5636 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5637 if (!dn) {
5638 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5639 return NT_STATUS_NO_MEMORY;
5643 if (num_result == 0) {
5644 is_new_entry = true;
5647 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5648 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5649 return ret;
5652 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5654 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5655 &group_sid));
5656 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5658 if (!groupsidstr || !grouptype) {
5659 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5660 return NT_STATUS_NO_MEMORY;
5663 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5664 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5665 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5666 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5668 if (is_new_entry) {
5669 char *escape_name;
5671 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5673 /* lets allocate a new groupid for this group */
5674 if (!winbind_allocate_gid(&gid)) {
5675 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5676 return NT_STATUS_UNSUCCESSFUL;
5679 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5681 escape_name = escape_rdn_val_string_alloc(name);
5682 if (!escape_name) {
5683 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5684 return NT_STATUS_NO_MEMORY;
5687 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5689 SAFE_FREE(escape_name);
5691 if (!gidstr || !dn) {
5692 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5693 return NT_STATUS_NO_MEMORY;
5696 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5697 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5698 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5701 talloc_autofree_ldapmod(tmp_ctx, mods);
5703 if (is_new_entry) {
5704 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5705 #if 0
5706 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5707 /* This call may fail with rfc2307bis schema */
5708 /* Retry adding a structural class */
5709 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5710 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5712 #endif
5713 } else {
5714 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5717 if (rc != LDAP_SUCCESS) {
5718 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5719 return NT_STATUS_UNSUCCESSFUL;
5722 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5724 return NT_STATUS_OK;
5727 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5729 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5730 LDAPMessage *result = NULL;
5731 LDAPMessage *entry = NULL;
5732 int num_result;
5733 const char *dn;
5734 char *gidstr;
5735 char *filter;
5736 struct dom_sid group_sid;
5737 int rc;
5739 /* get the group sid */
5740 sid_compose(&group_sid, get_global_sam_sid(), rid);
5742 filter = talloc_asprintf(tmp_ctx,
5743 "(&(sambaSID=%s)"
5744 "(objectClass=%s)"
5745 "(objectClass=%s))",
5746 sid_string_talloc(tmp_ctx, &group_sid),
5747 LDAP_OBJ_POSIXGROUP,
5748 LDAP_OBJ_GROUPMAP);
5749 if (filter == NULL) {
5750 return NT_STATUS_NO_MEMORY;
5753 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5754 if (rc != LDAP_SUCCESS) {
5755 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5756 return NT_STATUS_UNSUCCESSFUL;
5758 talloc_autofree_ldapmsg(tmp_ctx, result);
5760 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5762 if (num_result == 0) {
5763 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5764 return NT_STATUS_NO_SUCH_GROUP;
5767 if (num_result > 1) {
5768 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5769 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5772 entry = ldap_first_entry(priv2ld(ldap_state), result);
5773 if (!entry) {
5774 return NT_STATUS_UNSUCCESSFUL;
5777 /* here it is, retrieve the dn for later use */
5778 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5779 if (!dn) {
5780 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5781 return NT_STATUS_NO_MEMORY;
5784 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5785 if (!gidstr) {
5786 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5787 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5790 /* check no user have this group marked as primary group */
5791 filter = talloc_asprintf(tmp_ctx,
5792 "(&(gidNumber=%s)"
5793 "(objectClass=%s)"
5794 "(objectClass=%s))",
5795 gidstr,
5796 LDAP_OBJ_POSIXACCOUNT,
5797 LDAP_OBJ_SAMBASAMACCOUNT);
5799 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5800 if (rc != LDAP_SUCCESS) {
5801 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5802 return NT_STATUS_UNSUCCESSFUL;
5804 talloc_autofree_ldapmsg(tmp_ctx, result);
5806 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5808 if (num_result != 0) {
5809 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5810 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5813 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5814 if (rc != LDAP_SUCCESS) {
5815 return NT_STATUS_UNSUCCESSFUL;
5818 return NT_STATUS_OK;
5821 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5822 TALLOC_CTX *tmp_ctx,
5823 uint32_t group_rid,
5824 uint32_t member_rid,
5825 int modop)
5827 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5828 LDAPMessage *entry = NULL;
5829 LDAPMessage *result = NULL;
5830 uint32_t num_result;
5831 LDAPMod **mods = NULL;
5832 char *filter;
5833 char *uidstr;
5834 const char *dn = NULL;
5835 struct dom_sid group_sid;
5836 struct dom_sid member_sid;
5837 int rc;
5839 switch (modop) {
5840 case LDAP_MOD_ADD:
5841 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5842 break;
5843 case LDAP_MOD_DELETE:
5844 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5845 break;
5846 default:
5847 return NT_STATUS_UNSUCCESSFUL;
5850 /* get member sid */
5851 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5853 /* get the group sid */
5854 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5856 filter = talloc_asprintf(tmp_ctx,
5857 "(&(sambaSID=%s)"
5858 "(objectClass=%s)"
5859 "(objectClass=%s))",
5860 sid_string_talloc(tmp_ctx, &member_sid),
5861 LDAP_OBJ_POSIXACCOUNT,
5862 LDAP_OBJ_SAMBASAMACCOUNT);
5863 if (filter == NULL) {
5864 return NT_STATUS_NO_MEMORY;
5867 /* get the member uid */
5868 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5869 if (rc != LDAP_SUCCESS) {
5870 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5871 return NT_STATUS_UNSUCCESSFUL;
5873 talloc_autofree_ldapmsg(tmp_ctx, result);
5875 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5877 if (num_result == 0) {
5878 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5879 return NT_STATUS_NO_SUCH_MEMBER;
5882 if (num_result > 1) {
5883 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5884 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5887 entry = ldap_first_entry(priv2ld(ldap_state), result);
5888 if (!entry) {
5889 return NT_STATUS_UNSUCCESSFUL;
5892 if (modop == LDAP_MOD_DELETE) {
5893 /* check if we are trying to remove the member from his primary group */
5894 char *gidstr;
5895 gid_t user_gid, group_gid;
5897 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5898 if (!gidstr) {
5899 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5900 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5903 user_gid = strtoul(gidstr, NULL, 10);
5905 if (!sid_to_gid(&group_sid, &group_gid)) {
5906 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5907 return NT_STATUS_UNSUCCESSFUL;
5910 if (user_gid == group_gid) {
5911 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5912 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5916 /* here it is, retrieve the uid for later use */
5917 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5918 if (!uidstr) {
5919 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5920 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5923 filter = talloc_asprintf(tmp_ctx,
5924 "(&(sambaSID=%s)"
5925 "(objectClass=%s)"
5926 "(objectClass=%s))",
5927 sid_string_talloc(tmp_ctx, &group_sid),
5928 LDAP_OBJ_POSIXGROUP,
5929 LDAP_OBJ_GROUPMAP);
5931 /* get the group */
5932 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5933 if (rc != LDAP_SUCCESS) {
5934 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5935 return NT_STATUS_UNSUCCESSFUL;
5937 talloc_autofree_ldapmsg(tmp_ctx, result);
5939 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5941 if (num_result == 0) {
5942 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5943 return NT_STATUS_NO_SUCH_GROUP;
5946 if (num_result > 1) {
5947 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5948 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5951 entry = ldap_first_entry(priv2ld(ldap_state), result);
5952 if (!entry) {
5953 return NT_STATUS_UNSUCCESSFUL;
5956 /* here it is, retrieve the dn for later use */
5957 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5958 if (!dn) {
5959 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5960 return NT_STATUS_NO_MEMORY;
5963 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5965 talloc_autofree_ldapmod(tmp_ctx, mods);
5967 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5968 if (rc != LDAP_SUCCESS) {
5969 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5970 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5971 return NT_STATUS_MEMBER_IN_GROUP;
5973 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5974 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5975 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5977 return NT_STATUS_UNSUCCESSFUL;
5980 return NT_STATUS_OK;
5983 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5984 TALLOC_CTX *tmp_ctx,
5985 uint32_t group_rid,
5986 uint32_t member_rid)
5988 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5990 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5991 TALLOC_CTX *tmp_ctx,
5992 uint32_t group_rid,
5993 uint32_t member_rid)
5995 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5998 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5999 TALLOC_CTX *mem_ctx,
6000 struct samu *sampass)
6002 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6003 LDAPMessage *entry = NULL;
6004 LDAPMessage *result = NULL;
6005 uint32_t num_result;
6006 LDAPMod **mods = NULL;
6007 char *filter;
6008 char *escape_username;
6009 char *gidstr;
6010 const char *dn = NULL;
6011 gid_t gid;
6012 int rc;
6014 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6016 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6017 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6018 return NT_STATUS_UNSUCCESSFUL;
6020 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6021 if (!gidstr) {
6022 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6023 return NT_STATUS_NO_MEMORY;
6026 escape_username = escape_ldap_string(talloc_tos(),
6027 pdb_get_username(sampass));
6028 if (escape_username== NULL) {
6029 return NT_STATUS_NO_MEMORY;
6032 filter = talloc_asprintf(mem_ctx,
6033 "(&(uid=%s)"
6034 "(objectClass=%s)"
6035 "(objectClass=%s))",
6036 escape_username,
6037 LDAP_OBJ_POSIXACCOUNT,
6038 LDAP_OBJ_SAMBASAMACCOUNT);
6040 TALLOC_FREE(escape_username);
6042 if (filter == NULL) {
6043 return NT_STATUS_NO_MEMORY;
6046 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6047 if (rc != LDAP_SUCCESS) {
6048 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6049 return NT_STATUS_UNSUCCESSFUL;
6051 talloc_autofree_ldapmsg(mem_ctx, result);
6053 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6055 if (num_result == 0) {
6056 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6057 return NT_STATUS_NO_SUCH_USER;
6060 if (num_result > 1) {
6061 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6062 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6065 entry = ldap_first_entry(priv2ld(ldap_state), result);
6066 if (!entry) {
6067 return NT_STATUS_UNSUCCESSFUL;
6070 /* retrieve the dn for later use */
6071 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6072 if (!dn) {
6073 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6074 return NT_STATUS_NO_MEMORY;
6077 /* remove the old one, and add the new one, this way we do not risk races */
6078 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6080 if (mods == NULL) {
6081 return NT_STATUS_OK;
6084 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6086 if (rc != LDAP_SUCCESS) {
6087 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6088 pdb_get_username(sampass), gidstr));
6089 return NT_STATUS_UNSUCCESSFUL;
6092 flush_pwnam_cache();
6094 return NT_STATUS_OK;
6098 /**********************************************************************
6099 trusted domains functions
6100 *********************************************************************/
6102 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6103 const char *domain)
6105 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6106 ldap_state->domain_dn);
6109 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6110 TALLOC_CTX *mem_ctx,
6111 const char *domain, LDAPMessage **entry)
6113 int rc;
6114 char *filter;
6115 int scope = LDAP_SCOPE_SUBTREE;
6116 const char **attrs = NULL; /* NULL: get all attrs */
6117 int attrsonly = 0; /* 0: return values too */
6118 LDAPMessage *result = NULL;
6119 char *trusted_dn;
6120 uint32_t num_result;
6122 filter = talloc_asprintf(talloc_tos(),
6123 "(&(objectClass=%s)(sambaDomainName=%s))",
6124 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6126 trusted_dn = trusteddom_dn(ldap_state, domain);
6127 if (trusted_dn == NULL) {
6128 return False;
6130 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6131 filter, attrs, attrsonly, &result);
6133 if (result != NULL) {
6134 talloc_autofree_ldapmsg(mem_ctx, result);
6137 if (rc == LDAP_NO_SUCH_OBJECT) {
6138 *entry = NULL;
6139 return True;
6142 if (rc != LDAP_SUCCESS) {
6143 return False;
6146 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6148 if (num_result > 1) {
6149 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6150 "%s object for domain '%s'?!\n",
6151 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6152 return False;
6155 if (num_result == 0) {
6156 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6157 "%s object for domain %s.\n",
6158 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6159 *entry = NULL;
6160 } else {
6161 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6164 return True;
6167 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6168 const char *domain,
6169 char** pwd,
6170 struct dom_sid *sid,
6171 time_t *pass_last_set_time)
6173 struct ldapsam_privates *ldap_state =
6174 (struct ldapsam_privates *)methods->private_data;
6175 LDAPMessage *entry = NULL;
6177 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6179 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6180 (entry == NULL))
6182 return False;
6185 /* password */
6186 if (pwd != NULL) {
6187 char *pwd_str;
6188 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6189 entry, "sambaClearTextPassword", talloc_tos());
6190 if (pwd_str == NULL) {
6191 return False;
6193 /* trusteddom_pw routines do not use talloc yet... */
6194 *pwd = SMB_STRDUP(pwd_str);
6195 if (*pwd == NULL) {
6196 return False;
6200 /* last change time */
6201 if (pass_last_set_time != NULL) {
6202 char *time_str;
6203 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6204 entry, "sambaPwdLastSet", talloc_tos());
6205 if (time_str == NULL) {
6206 return False;
6208 *pass_last_set_time = (time_t)atol(time_str);
6211 /* domain sid */
6212 if (sid != NULL) {
6213 char *sid_str;
6214 struct dom_sid dom_sid;
6215 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6216 entry, "sambaSID",
6217 talloc_tos());
6218 if (sid_str == NULL) {
6219 return False;
6221 if (!string_to_sid(&dom_sid, sid_str)) {
6222 return False;
6224 sid_copy(sid, &dom_sid);
6227 return True;
6230 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6231 const char* domain,
6232 const char* pwd,
6233 const struct dom_sid *sid)
6235 struct ldapsam_privates *ldap_state =
6236 (struct ldapsam_privates *)methods->private_data;
6237 LDAPMessage *entry = NULL;
6238 LDAPMod **mods = NULL;
6239 char *prev_pwd = NULL;
6240 char *trusted_dn = NULL;
6241 int rc;
6243 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6246 * get the current entry (if there is one) in order to put the
6247 * current password into the previous password attribute
6249 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6250 return False;
6253 mods = NULL;
6254 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6255 LDAP_OBJ_TRUSTDOM_PASSWORD);
6256 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6257 domain);
6258 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6259 sid_string_tos(sid));
6260 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6261 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6262 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6263 "sambaClearTextPassword", pwd);
6265 if (entry != NULL) {
6266 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6267 entry, "sambaClearTextPassword", talloc_tos());
6268 if (prev_pwd != NULL) {
6269 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6270 "sambaPreviousClearTextPassword",
6271 prev_pwd);
6275 talloc_autofree_ldapmod(talloc_tos(), mods);
6277 trusted_dn = trusteddom_dn(ldap_state, domain);
6278 if (trusted_dn == NULL) {
6279 return False;
6281 if (entry == NULL) {
6282 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6283 } else {
6284 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6287 if (rc != LDAP_SUCCESS) {
6288 DEBUG(1, ("error writing trusted domain password!\n"));
6289 return False;
6292 return True;
6295 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6296 const char *domain)
6298 int rc;
6299 struct ldapsam_privates *ldap_state =
6300 (struct ldapsam_privates *)methods->private_data;
6301 LDAPMessage *entry = NULL;
6302 const char *trusted_dn;
6304 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6305 return False;
6308 if (entry == NULL) {
6309 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6310 "%s\n", domain));
6311 return True;
6314 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6315 entry);
6316 if (trusted_dn == NULL) {
6317 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6318 return False;
6321 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6322 if (rc != LDAP_SUCCESS) {
6323 return False;
6326 return True;
6329 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6330 TALLOC_CTX *mem_ctx,
6331 uint32_t *num_domains,
6332 struct trustdom_info ***domains)
6334 int rc;
6335 struct ldapsam_privates *ldap_state =
6336 (struct ldapsam_privates *)methods->private_data;
6337 char *filter;
6338 int scope = LDAP_SCOPE_SUBTREE;
6339 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6340 int attrsonly = 0; /* 0: return values too */
6341 LDAPMessage *result = NULL;
6342 LDAPMessage *entry = NULL;
6344 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6345 LDAP_OBJ_TRUSTDOM_PASSWORD);
6347 rc = smbldap_search(ldap_state->smbldap_state,
6348 ldap_state->domain_dn,
6349 scope,
6350 filter,
6351 attrs,
6352 attrsonly,
6353 &result);
6355 if (result != NULL) {
6356 talloc_autofree_ldapmsg(mem_ctx, result);
6359 if (rc != LDAP_SUCCESS) {
6360 return NT_STATUS_UNSUCCESSFUL;
6363 *num_domains = 0;
6364 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6365 DEBUG(1, ("talloc failed\n"));
6366 return NT_STATUS_NO_MEMORY;
6369 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6370 entry != NULL;
6371 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6373 char *dom_name, *dom_sid_str;
6374 struct trustdom_info *dom_info;
6376 dom_info = talloc(*domains, struct trustdom_info);
6377 if (dom_info == NULL) {
6378 DEBUG(1, ("talloc failed\n"));
6379 return NT_STATUS_NO_MEMORY;
6382 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6383 entry,
6384 "sambaDomainName",
6385 talloc_tos());
6386 if (dom_name == NULL) {
6387 DEBUG(1, ("talloc failed\n"));
6388 return NT_STATUS_NO_MEMORY;
6390 dom_info->name = dom_name;
6392 dom_sid_str = smbldap_talloc_single_attribute(
6393 priv2ld(ldap_state), entry, "sambaSID",
6394 talloc_tos());
6395 if (dom_sid_str == NULL) {
6396 DEBUG(1, ("talloc failed\n"));
6397 return NT_STATUS_NO_MEMORY;
6399 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6400 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6401 dom_sid_str));
6402 return NT_STATUS_UNSUCCESSFUL;
6405 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6406 domains, num_domains);
6408 if (*domains == NULL) {
6409 DEBUG(1, ("talloc failed\n"));
6410 return NT_STATUS_NO_MEMORY;
6414 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6415 return NT_STATUS_OK;
6419 /**********************************************************************
6420 Housekeeping
6421 *********************************************************************/
6423 static void free_private_data(void **vp)
6425 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6427 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6429 if ((*ldap_state)->result != NULL) {
6430 ldap_msgfree((*ldap_state)->result);
6431 (*ldap_state)->result = NULL;
6433 if ((*ldap_state)->domain_dn != NULL) {
6434 SAFE_FREE((*ldap_state)->domain_dn);
6437 *ldap_state = NULL;
6439 /* No need to free any further, as it is talloc()ed */
6442 /*********************************************************************
6443 Intitalise the parts of the pdb_methods structure that are common to
6444 all pdb_ldap modes
6445 *********************************************************************/
6447 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6449 NTSTATUS nt_status;
6450 struct ldapsam_privates *ldap_state;
6451 char *bind_dn = NULL;
6452 char *bind_secret = NULL;
6454 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6455 return nt_status;
6458 (*pdb_method)->name = "ldapsam";
6460 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6461 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6462 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6463 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6464 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6465 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6467 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6468 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6469 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6470 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6471 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6472 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6473 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6475 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6476 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6478 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6480 (*pdb_method)->capabilities = ldapsam_capabilities;
6481 (*pdb_method)->new_rid = ldapsam_new_rid;
6483 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6484 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6485 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6486 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6488 /* TODO: Setup private data and free */
6490 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6491 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6492 return NT_STATUS_NO_MEMORY;
6495 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6496 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6497 return NT_STATUS_NO_MEMORY;
6500 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6501 location, false, bind_dn, bind_secret,
6502 &ldap_state->smbldap_state);
6503 memset(bind_secret, '\0', strlen(bind_secret));
6504 SAFE_FREE(bind_secret);
6505 SAFE_FREE(bind_dn);
6506 if ( !NT_STATUS_IS_OK(nt_status) ) {
6507 return nt_status;
6510 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6511 return NT_STATUS_NO_MEMORY;
6514 (*pdb_method)->private_data = ldap_state;
6516 (*pdb_method)->free_private_data = free_private_data;
6518 return NT_STATUS_OK;
6521 /**********************************************************************
6522 Initialise the 'compat' mode for pdb_ldap
6523 *********************************************************************/
6525 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6527 NTSTATUS nt_status;
6528 struct ldapsam_privates *ldap_state;
6529 char *uri = talloc_strdup( NULL, location );
6531 trim_char( uri, '\"', '\"' );
6532 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6533 if ( uri )
6534 TALLOC_FREE( uri );
6536 if ( !NT_STATUS_IS_OK(nt_status) ) {
6537 return nt_status;
6540 (*pdb_method)->name = "ldapsam_compat";
6542 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6543 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6545 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6547 return NT_STATUS_OK;
6550 /**********************************************************************
6551 Initialise the normal mode for pdb_ldap
6552 *********************************************************************/
6554 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6556 NTSTATUS nt_status;
6557 struct ldapsam_privates *ldap_state = NULL;
6558 uint32_t alg_rid_base;
6559 char *alg_rid_base_string = NULL;
6560 LDAPMessage *result = NULL;
6561 LDAPMessage *entry = NULL;
6562 struct dom_sid ldap_domain_sid;
6563 struct dom_sid secrets_domain_sid;
6564 char *domain_sid_string = NULL;
6565 char *dn = NULL;
6566 char *uri = talloc_strdup( NULL, location );
6568 trim_char( uri, '\"', '\"' );
6569 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6571 TALLOC_FREE(uri);
6573 if (!NT_STATUS_IS_OK(nt_status)) {
6574 return nt_status;
6577 (*pdb_method)->name = "ldapsam";
6579 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6580 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6581 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6582 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6583 (*pdb_method)->search_users = ldapsam_search_users;
6584 (*pdb_method)->search_groups = ldapsam_search_groups;
6585 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6587 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6588 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6589 (*pdb_method)->enum_group_memberships =
6590 ldapsam_enum_group_memberships;
6591 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6592 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6593 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6594 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6596 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6597 (*pdb_method)->create_user = ldapsam_create_user;
6598 (*pdb_method)->delete_user = ldapsam_delete_user;
6599 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6600 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6601 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6602 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6603 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6607 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6608 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6610 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6612 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6613 &result,
6614 ldap_state->domain_name, True);
6616 if ( !NT_STATUS_IS_OK(nt_status) ) {
6617 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6618 "info, nor add one to the domain. "
6619 "We cannot work reliably without it.\n"));
6620 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6623 /* Given that the above might fail, everything below this must be
6624 * optional */
6626 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6627 result);
6628 if (!entry) {
6629 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6630 "entry\n"));
6631 ldap_msgfree(result);
6632 return NT_STATUS_UNSUCCESSFUL;
6635 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6636 if (!dn) {
6637 ldap_msgfree(result);
6638 return NT_STATUS_UNSUCCESSFUL;
6641 ldap_state->domain_dn = smb_xstrdup(dn);
6642 TALLOC_FREE(dn);
6644 domain_sid_string = smbldap_talloc_single_attribute(
6645 ldap_state->smbldap_state->ldap_struct,
6646 entry,
6647 get_userattr_key2string(ldap_state->schema_ver,
6648 LDAP_ATTR_USER_SID),
6649 talloc_tos());
6651 if (domain_sid_string) {
6652 bool found_sid;
6653 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6654 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6655 "read as a valid SID\n", domain_sid_string));
6656 ldap_msgfree(result);
6657 TALLOC_FREE(domain_sid_string);
6658 return NT_STATUS_INVALID_PARAMETER;
6660 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6661 &secrets_domain_sid);
6662 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6663 &ldap_domain_sid)) {
6664 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6665 "%s based on pdb_ldap results %s -> %s\n",
6666 ldap_state->domain_name,
6667 sid_string_dbg(&secrets_domain_sid),
6668 sid_string_dbg(&ldap_domain_sid)));
6670 /* reset secrets.tdb sid */
6671 secrets_store_domain_sid(ldap_state->domain_name,
6672 &ldap_domain_sid);
6673 DEBUG(1, ("New global sam SID: %s\n",
6674 sid_string_dbg(get_global_sam_sid())));
6676 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6677 TALLOC_FREE(domain_sid_string);
6680 alg_rid_base_string = smbldap_talloc_single_attribute(
6681 ldap_state->smbldap_state->ldap_struct,
6682 entry,
6683 get_attr_key2string( dominfo_attr_list,
6684 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6685 talloc_tos());
6686 if (alg_rid_base_string) {
6687 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6688 if (alg_rid_base != algorithmic_rid_base()) {
6689 DEBUG(0, ("The value of 'algorithmic RID base' has "
6690 "changed since the LDAP\n"
6691 "database was initialised. Aborting. \n"));
6692 ldap_msgfree(result);
6693 TALLOC_FREE(alg_rid_base_string);
6694 return NT_STATUS_UNSUCCESSFUL;
6696 TALLOC_FREE(alg_rid_base_string);
6698 ldap_msgfree(result);
6700 return NT_STATUS_OK;
6703 NTSTATUS pdb_ldap_init(void)
6705 NTSTATUS nt_status;
6706 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6707 return nt_status;
6709 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6710 return nt_status;
6712 /* Let pdb_nds register backends */
6713 pdb_nds_init();
6715 pdb_ipa_init();
6717 return NT_STATUS_OK;