vfs: Make function pointer names consistent. They all end in _fn
[Samba/gebeck_regimport.git] / source3 / passdb / pdb_ldap.c
blob49eb3e12fa3b544dc6e6e0617d8fe7f6175f328b
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 = SMB_STRDUP(lp_renameuser_script());
2079 if (rename_script == NULL) {
2080 return NT_STATUS_NO_MEMORY;
2083 if (!(*rename_script)) {
2084 SAFE_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 SAFE_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 goto done;
3033 *p_num_groups = num_sids;
3035 ret = NT_STATUS_OK;
3037 done:
3039 TALLOC_FREE(escape_name);
3040 return ret;
3043 /**********************************************************************
3044 * Augment a posixGroup object with a sambaGroupMapping domgroup
3045 *********************************************************************/
3047 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3048 struct ldapsam_privates *ldap_state,
3049 GROUP_MAP *map)
3051 const char *filter, *dn;
3052 LDAPMessage *msg, *entry;
3053 LDAPMod **mods;
3054 int rc;
3056 filter = talloc_asprintf(mem_ctx,
3057 "(&(objectClass=%s)(gidNumber=%u))",
3058 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3059 if (filter == NULL) {
3060 return NT_STATUS_NO_MEMORY;
3063 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3064 get_attr_list(mem_ctx, groupmap_attr_list),
3065 &msg);
3066 talloc_autofree_ldapmsg(mem_ctx, msg);
3068 if ((rc != LDAP_SUCCESS) ||
3069 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3070 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3071 return NT_STATUS_NO_SUCH_GROUP;
3074 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3075 if (dn == NULL) {
3076 return NT_STATUS_NO_MEMORY;
3079 mods = NULL;
3080 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3081 LDAP_OBJ_GROUPMAP);
3082 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3083 sid_string_talloc(mem_ctx, &map->sid));
3084 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3085 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3086 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3087 map->nt_name);
3088 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3089 map->comment);
3090 talloc_autofree_ldapmod(mem_ctx, mods);
3092 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3093 if (rc != LDAP_SUCCESS) {
3094 return NT_STATUS_ACCESS_DENIED;
3097 return NT_STATUS_OK;
3100 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3101 GROUP_MAP *map)
3103 struct ldapsam_privates *ldap_state =
3104 (struct ldapsam_privates *)methods->private_data;
3105 LDAPMessage *msg = NULL;
3106 LDAPMod **mods = NULL;
3107 const char *attrs[] = { NULL };
3108 char *filter;
3110 char *dn;
3111 TALLOC_CTX *mem_ctx;
3112 NTSTATUS result;
3114 struct dom_sid sid;
3116 int rc;
3118 mem_ctx = talloc_new(NULL);
3119 if (mem_ctx == NULL) {
3120 DEBUG(0, ("talloc_new failed\n"));
3121 return NT_STATUS_NO_MEMORY;
3124 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3125 sid_string_talloc(mem_ctx, &map->sid));
3126 if (filter == NULL) {
3127 result = NT_STATUS_NO_MEMORY;
3128 goto done;
3131 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3132 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3133 talloc_autofree_ldapmsg(mem_ctx, msg);
3135 if ((rc == LDAP_SUCCESS) &&
3136 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3138 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3139 "group mapping entry\n", sid_string_dbg(&map->sid)));
3140 result = NT_STATUS_GROUP_EXISTS;
3141 goto done;
3144 switch (map->sid_name_use) {
3146 case SID_NAME_DOM_GRP:
3147 /* To map a domain group we need to have a posix group
3148 to attach to. */
3149 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3150 goto done;
3151 break;
3153 case SID_NAME_ALIAS:
3154 if (!sid_check_is_in_our_domain(&map->sid)
3155 && !sid_check_is_in_builtin(&map->sid) )
3157 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3158 sid_string_dbg(&map->sid)));
3159 result = NT_STATUS_INVALID_PARAMETER;
3160 goto done;
3162 break;
3164 default:
3165 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3166 sid_type_lookup(map->sid_name_use)));
3167 result = NT_STATUS_INVALID_PARAMETER;
3168 goto done;
3171 /* Domain groups have been mapped in a separate routine, we have to
3172 * create an alias now */
3174 if (map->gid == -1) {
3175 DEBUG(10, ("Refusing to map gid==-1\n"));
3176 result = NT_STATUS_INVALID_PARAMETER;
3177 goto done;
3180 if (pdb_gid_to_sid(map->gid, &sid)) {
3181 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3182 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3183 result = NT_STATUS_GROUP_EXISTS;
3184 goto done;
3187 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3188 * the best we can get out of LDAP. */
3190 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3191 sid_string_talloc(mem_ctx, &map->sid),
3192 lp_ldap_group_suffix());
3193 if (dn == NULL) {
3194 result = NT_STATUS_NO_MEMORY;
3195 goto done;
3198 mods = NULL;
3200 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3201 LDAP_OBJ_SID_ENTRY);
3202 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3203 LDAP_OBJ_GROUPMAP);
3204 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3205 sid_string_talloc(mem_ctx, &map->sid));
3206 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3207 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3208 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3209 map->nt_name);
3210 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3211 map->comment);
3212 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3213 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3214 talloc_autofree_ldapmod(mem_ctx, mods);
3216 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3218 result = (rc == LDAP_SUCCESS) ?
3219 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3221 done:
3222 TALLOC_FREE(mem_ctx);
3223 return result;
3226 /**********************************************************************
3227 * Update a group mapping entry. We're quite strict about what can be changed:
3228 * Only the description and displayname may be changed. It simply does not
3229 * make any sense to change the SID, gid or the type in a mapping.
3230 *********************************************************************/
3232 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3233 GROUP_MAP *map)
3235 struct ldapsam_privates *ldap_state =
3236 (struct ldapsam_privates *)methods->private_data;
3237 int rc;
3238 const char *filter, *dn;
3239 LDAPMessage *msg = NULL;
3240 LDAPMessage *entry = NULL;
3241 LDAPMod **mods = NULL;
3242 TALLOC_CTX *mem_ctx;
3243 NTSTATUS result;
3245 mem_ctx = talloc_new(NULL);
3246 if (mem_ctx == NULL) {
3247 DEBUG(0, ("talloc_new failed\n"));
3248 return NT_STATUS_NO_MEMORY;
3251 /* Make 100% sure that sid, gid and type are not changed by looking up
3252 * exactly the values we're given in LDAP. */
3254 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3255 "(sambaSid=%s)(gidNumber=%u)"
3256 "(sambaGroupType=%d))",
3257 LDAP_OBJ_GROUPMAP,
3258 sid_string_talloc(mem_ctx, &map->sid),
3259 (unsigned int)map->gid, map->sid_name_use);
3260 if (filter == NULL) {
3261 result = NT_STATUS_NO_MEMORY;
3262 goto done;
3265 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3266 get_attr_list(mem_ctx, groupmap_attr_list),
3267 &msg);
3268 talloc_autofree_ldapmsg(mem_ctx, msg);
3270 if ((rc != LDAP_SUCCESS) ||
3271 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3272 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3273 result = NT_STATUS_NO_SUCH_GROUP;
3274 goto done;
3277 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3279 if (dn == NULL) {
3280 result = NT_STATUS_NO_MEMORY;
3281 goto done;
3284 mods = NULL;
3285 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3286 map->nt_name);
3287 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3288 map->comment);
3289 talloc_autofree_ldapmod(mem_ctx, mods);
3291 if (mods == NULL) {
3292 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3293 "nothing to do\n"));
3294 result = NT_STATUS_OK;
3295 goto done;
3298 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3300 if (rc != LDAP_SUCCESS) {
3301 result = NT_STATUS_ACCESS_DENIED;
3302 goto done;
3305 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3306 "group %lu in LDAP\n", (unsigned long)map->gid));
3308 result = NT_STATUS_OK;
3310 done:
3311 TALLOC_FREE(mem_ctx);
3312 return result;
3315 /**********************************************************************
3316 *********************************************************************/
3318 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3319 struct dom_sid sid)
3321 struct ldapsam_privates *priv =
3322 (struct ldapsam_privates *)methods->private_data;
3323 LDAPMessage *msg, *entry;
3324 int rc;
3325 NTSTATUS result;
3326 TALLOC_CTX *mem_ctx;
3327 char *filter;
3329 mem_ctx = talloc_new(NULL);
3330 if (mem_ctx == NULL) {
3331 DEBUG(0, ("talloc_new failed\n"));
3332 return NT_STATUS_NO_MEMORY;
3335 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3336 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3337 sid_string_talloc(mem_ctx, &sid));
3338 if (filter == NULL) {
3339 result = NT_STATUS_NO_MEMORY;
3340 goto done;
3342 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3343 get_attr_list(mem_ctx, groupmap_attr_list),
3344 &msg);
3345 talloc_autofree_ldapmsg(mem_ctx, msg);
3347 if ((rc != LDAP_SUCCESS) ||
3348 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3349 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3350 result = NT_STATUS_NO_SUCH_GROUP;
3351 goto done;
3354 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3355 get_attr_list(mem_ctx,
3356 groupmap_attr_list_to_delete));
3358 if ((rc == LDAP_NAMING_VIOLATION) ||
3359 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3360 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3361 const char *attrs[] = { "sambaGroupType", "description",
3362 "displayName", "sambaSIDList",
3363 NULL };
3365 /* Second try. Don't delete the sambaSID attribute, this is
3366 for "old" entries that are tacked on a winbind
3367 sambaIdmapEntry. */
3369 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3370 LDAP_OBJ_GROUPMAP, attrs);
3373 if ((rc == LDAP_NAMING_VIOLATION) ||
3374 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3375 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3376 const char *attrs[] = { "sambaGroupType", "description",
3377 "displayName", "sambaSIDList",
3378 "gidNumber", NULL };
3380 /* Third try. This is a post-3.0.21 alias (containing only
3381 * sambaSidEntry and sambaGroupMapping classes), we also have
3382 * to delete the gidNumber attribute, only the sambaSidEntry
3383 * remains */
3385 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3386 LDAP_OBJ_GROUPMAP, attrs);
3389 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3391 done:
3392 TALLOC_FREE(mem_ctx);
3393 return result;
3396 /**********************************************************************
3397 *********************************************************************/
3399 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3400 bool update)
3402 struct ldapsam_privates *ldap_state =
3403 (struct ldapsam_privates *)my_methods->private_data;
3404 char *filter = NULL;
3405 int rc;
3406 const char **attr_list;
3408 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3409 if (!filter) {
3410 return NT_STATUS_NO_MEMORY;
3412 attr_list = get_attr_list( NULL, groupmap_attr_list );
3413 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3414 LDAP_SCOPE_SUBTREE, filter,
3415 attr_list, 0, &ldap_state->result);
3416 TALLOC_FREE(attr_list);
3418 if (rc != LDAP_SUCCESS) {
3419 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3420 ldap_err2string(rc)));
3421 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3422 lp_ldap_suffix(), filter));
3423 ldap_msgfree(ldap_state->result);
3424 ldap_state->result = NULL;
3425 TALLOC_FREE(filter);
3426 return NT_STATUS_UNSUCCESSFUL;
3429 TALLOC_FREE(filter);
3431 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3432 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3433 ldap_state->result)));
3435 ldap_state->entry =
3436 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3437 ldap_state->result);
3438 ldap_state->index = 0;
3440 return NT_STATUS_OK;
3443 /**********************************************************************
3444 *********************************************************************/
3446 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3448 ldapsam_endsampwent(my_methods);
3451 /**********************************************************************
3452 *********************************************************************/
3454 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3455 GROUP_MAP *map)
3457 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3458 struct ldapsam_privates *ldap_state =
3459 (struct ldapsam_privates *)my_methods->private_data;
3460 bool bret = False;
3462 while (!bret) {
3463 if (!ldap_state->entry)
3464 return ret;
3466 ldap_state->index++;
3467 bret = init_group_from_ldap(ldap_state, map,
3468 ldap_state->entry);
3470 ldap_state->entry =
3471 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3472 ldap_state->entry);
3475 return NT_STATUS_OK;
3478 /**********************************************************************
3479 *********************************************************************/
3481 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3482 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3483 GROUP_MAP ***pp_rmap,
3484 size_t *p_num_entries,
3485 bool unix_only)
3487 GROUP_MAP *map = NULL;
3488 size_t entries = 0;
3490 *p_num_entries = 0;
3491 *pp_rmap = NULL;
3493 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3494 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3495 "passdb\n"));
3496 return NT_STATUS_ACCESS_DENIED;
3499 while (true) {
3501 map = talloc_zero(NULL, GROUP_MAP);
3502 if (!map) {
3503 return NT_STATUS_NO_MEMORY;
3506 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3507 TALLOC_FREE(map);
3508 break;
3511 if (sid_name_use != SID_NAME_UNKNOWN &&
3512 sid_name_use != map->sid_name_use) {
3513 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3514 "not of the requested type\n",
3515 map->nt_name));
3516 continue;
3518 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3519 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3520 "non mapped\n", map->nt_name));
3521 continue;
3524 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3525 GROUP_MAP *, entries + 1);
3526 if (!(*pp_rmap)) {
3527 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3528 "enlarge group map!\n"));
3529 return NT_STATUS_UNSUCCESSFUL;
3532 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3534 entries += 1;
3537 ldapsam_endsamgrent(methods);
3539 *p_num_entries = entries;
3541 return NT_STATUS_OK;
3544 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3545 const struct dom_sid *alias,
3546 const struct dom_sid *member,
3547 int modop)
3549 struct ldapsam_privates *ldap_state =
3550 (struct ldapsam_privates *)methods->private_data;
3551 char *dn = NULL;
3552 LDAPMessage *result = NULL;
3553 LDAPMessage *entry = NULL;
3554 int count;
3555 LDAPMod **mods = NULL;
3556 int rc;
3557 enum lsa_SidType type = SID_NAME_USE_NONE;
3558 fstring tmp;
3560 char *filter = NULL;
3562 if (sid_check_is_in_builtin(alias)) {
3563 type = SID_NAME_ALIAS;
3566 if (sid_check_is_in_our_domain(alias)) {
3567 type = SID_NAME_ALIAS;
3570 if (type == SID_NAME_USE_NONE) {
3571 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3572 sid_string_dbg(alias)));
3573 return NT_STATUS_NO_SUCH_ALIAS;
3576 if (asprintf(&filter,
3577 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3578 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3579 type) < 0) {
3580 return NT_STATUS_NO_MEMORY;
3583 if (ldapsam_search_one_group(ldap_state, filter,
3584 &result) != LDAP_SUCCESS) {
3585 SAFE_FREE(filter);
3586 return NT_STATUS_NO_SUCH_ALIAS;
3589 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3590 result);
3592 if (count < 1) {
3593 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3594 ldap_msgfree(result);
3595 SAFE_FREE(filter);
3596 return NT_STATUS_NO_SUCH_ALIAS;
3599 if (count > 1) {
3600 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3601 "filter %s: count=%d\n", filter, count));
3602 ldap_msgfree(result);
3603 SAFE_FREE(filter);
3604 return NT_STATUS_NO_SUCH_ALIAS;
3607 SAFE_FREE(filter);
3609 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3610 result);
3612 if (!entry) {
3613 ldap_msgfree(result);
3614 return NT_STATUS_UNSUCCESSFUL;
3617 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3618 if (!dn) {
3619 ldap_msgfree(result);
3620 return NT_STATUS_UNSUCCESSFUL;
3623 smbldap_set_mod(&mods, modop,
3624 get_attr_key2string(groupmap_attr_list,
3625 LDAP_ATTR_SID_LIST),
3626 sid_to_fstring(tmp, member));
3628 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3630 ldap_mods_free(mods, True);
3631 ldap_msgfree(result);
3632 TALLOC_FREE(dn);
3634 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3635 return NT_STATUS_MEMBER_IN_ALIAS;
3638 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3639 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3642 if (rc != LDAP_SUCCESS) {
3643 return NT_STATUS_UNSUCCESSFUL;
3646 return NT_STATUS_OK;
3649 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3650 const struct dom_sid *alias,
3651 const struct dom_sid *member)
3653 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3656 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3657 const struct dom_sid *alias,
3658 const struct dom_sid *member)
3660 return ldapsam_modify_aliasmem(methods, alias, member,
3661 LDAP_MOD_DELETE);
3664 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3665 const struct dom_sid *alias,
3666 TALLOC_CTX *mem_ctx,
3667 struct dom_sid **pp_members,
3668 size_t *p_num_members)
3670 struct ldapsam_privates *ldap_state =
3671 (struct ldapsam_privates *)methods->private_data;
3672 LDAPMessage *result = NULL;
3673 LDAPMessage *entry = NULL;
3674 int count;
3675 char **values = NULL;
3676 int i;
3677 char *filter = NULL;
3678 uint32_t num_members = 0;
3679 enum lsa_SidType type = SID_NAME_USE_NONE;
3680 fstring tmp;
3682 *pp_members = NULL;
3683 *p_num_members = 0;
3685 if (sid_check_is_in_builtin(alias)) {
3686 type = SID_NAME_ALIAS;
3689 if (sid_check_is_in_our_domain(alias)) {
3690 type = SID_NAME_ALIAS;
3693 if (type == SID_NAME_USE_NONE) {
3694 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3695 sid_string_dbg(alias)));
3696 return NT_STATUS_NO_SUCH_ALIAS;
3699 if (asprintf(&filter,
3700 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3701 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3702 type) < 0) {
3703 return NT_STATUS_NO_MEMORY;
3706 if (ldapsam_search_one_group(ldap_state, filter,
3707 &result) != LDAP_SUCCESS) {
3708 SAFE_FREE(filter);
3709 return NT_STATUS_NO_SUCH_ALIAS;
3712 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3713 result);
3715 if (count < 1) {
3716 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3717 ldap_msgfree(result);
3718 SAFE_FREE(filter);
3719 return NT_STATUS_NO_SUCH_ALIAS;
3722 if (count > 1) {
3723 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3724 "filter %s: count=%d\n", filter, count));
3725 ldap_msgfree(result);
3726 SAFE_FREE(filter);
3727 return NT_STATUS_NO_SUCH_ALIAS;
3730 SAFE_FREE(filter);
3732 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3733 result);
3735 if (!entry) {
3736 ldap_msgfree(result);
3737 return NT_STATUS_UNSUCCESSFUL;
3740 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3741 entry,
3742 get_attr_key2string(groupmap_attr_list,
3743 LDAP_ATTR_SID_LIST));
3745 if (values == NULL) {
3746 ldap_msgfree(result);
3747 return NT_STATUS_OK;
3750 count = ldap_count_values(values);
3752 for (i=0; i<count; i++) {
3753 struct dom_sid member;
3754 NTSTATUS status;
3756 if (!string_to_sid(&member, values[i]))
3757 continue;
3759 status = add_sid_to_array(mem_ctx, &member, pp_members,
3760 &num_members);
3761 if (!NT_STATUS_IS_OK(status)) {
3762 ldap_value_free(values);
3763 ldap_msgfree(result);
3764 return status;
3768 *p_num_members = num_members;
3769 ldap_value_free(values);
3770 ldap_msgfree(result);
3772 return NT_STATUS_OK;
3775 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3776 TALLOC_CTX *mem_ctx,
3777 const struct dom_sid *domain_sid,
3778 const struct dom_sid *members,
3779 size_t num_members,
3780 uint32_t **pp_alias_rids,
3781 size_t *p_num_alias_rids)
3783 struct ldapsam_privates *ldap_state =
3784 (struct ldapsam_privates *)methods->private_data;
3785 LDAP *ldap_struct;
3787 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3789 LDAPMessage *result = NULL;
3790 LDAPMessage *entry = NULL;
3791 int i;
3792 int rc;
3793 char *filter;
3794 enum lsa_SidType type = SID_NAME_USE_NONE;
3795 bool is_builtin = false;
3796 bool sid_added = false;
3798 *pp_alias_rids = NULL;
3799 *p_num_alias_rids = 0;
3801 if (sid_check_is_builtin(domain_sid)) {
3802 is_builtin = true;
3803 type = SID_NAME_ALIAS;
3806 if (sid_check_is_domain(domain_sid)) {
3807 type = SID_NAME_ALIAS;
3810 if (type == SID_NAME_USE_NONE) {
3811 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3812 sid_string_dbg(domain_sid)));
3813 return NT_STATUS_UNSUCCESSFUL;
3816 if (num_members == 0) {
3817 return NT_STATUS_OK;
3820 filter = talloc_asprintf(mem_ctx,
3821 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3822 LDAP_OBJ_GROUPMAP, type);
3824 for (i=0; i<num_members; i++)
3825 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3826 filter,
3827 sid_string_talloc(mem_ctx,
3828 &members[i]));
3830 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3832 if (filter == NULL) {
3833 return NT_STATUS_NO_MEMORY;
3836 if (is_builtin &&
3837 ldap_state->search_cache.filter &&
3838 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3839 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3840 result = ldap_state->search_cache.result;
3841 ldap_state->search_cache.result = NULL;
3842 } else {
3843 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3844 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3845 if (rc != LDAP_SUCCESS) {
3846 return NT_STATUS_UNSUCCESSFUL;
3848 talloc_autofree_ldapmsg(filter, result);
3851 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3853 for (entry = ldap_first_entry(ldap_struct, result);
3854 entry != NULL;
3855 entry = ldap_next_entry(ldap_struct, entry))
3857 fstring sid_str;
3858 struct dom_sid sid;
3859 uint32_t rid;
3861 if (!smbldap_get_single_attribute(ldap_struct, entry,
3862 LDAP_ATTRIBUTE_SID,
3863 sid_str,
3864 sizeof(sid_str)-1))
3865 continue;
3867 if (!string_to_sid(&sid, sid_str))
3868 continue;
3870 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3871 continue;
3873 sid_added = true;
3875 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3876 p_num_alias_rids)) {
3877 return NT_STATUS_NO_MEMORY;
3881 if (!is_builtin && !sid_added) {
3882 TALLOC_FREE(ldap_state->search_cache.filter);
3884 * Note: result is a talloc child of filter because of the
3885 * talloc_autofree_ldapmsg() usage
3887 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3888 ldap_state->search_cache.result = result;
3891 return NT_STATUS_OK;
3894 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3895 enum pdb_policy_type type,
3896 uint32_t value)
3898 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3899 int rc;
3900 LDAPMod **mods = NULL;
3901 fstring value_string;
3902 const char *policy_attr = NULL;
3904 struct ldapsam_privates *ldap_state =
3905 (struct ldapsam_privates *)methods->private_data;
3907 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3909 if (!ldap_state->domain_dn) {
3910 return NT_STATUS_INVALID_PARAMETER;
3913 policy_attr = get_account_policy_attr(type);
3914 if (policy_attr == NULL) {
3915 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3916 "policy\n"));
3917 return ntstatus;
3920 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3922 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3924 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3925 mods);
3927 ldap_mods_free(mods, True);
3929 if (rc != LDAP_SUCCESS) {
3930 return ntstatus;
3933 if (!cache_account_policy_set(type, value)) {
3934 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3935 "update local tdb cache\n"));
3936 return ntstatus;
3939 return NT_STATUS_OK;
3942 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3943 enum pdb_policy_type type,
3944 uint32_t value)
3946 return ldapsam_set_account_policy_in_ldap(methods, type,
3947 value);
3950 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3951 enum pdb_policy_type type,
3952 uint32_t *value)
3954 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3955 LDAPMessage *result = NULL;
3956 LDAPMessage *entry = NULL;
3957 int count;
3958 int rc;
3959 char **vals = NULL;
3960 char *filter;
3961 const char *policy_attr = NULL;
3963 struct ldapsam_privates *ldap_state =
3964 (struct ldapsam_privates *)methods->private_data;
3966 const char *attrs[2];
3968 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3970 if (!ldap_state->domain_dn) {
3971 return NT_STATUS_INVALID_PARAMETER;
3974 policy_attr = get_account_policy_attr(type);
3975 if (!policy_attr) {
3976 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3977 "policy index: %d\n", type));
3978 return ntstatus;
3981 attrs[0] = policy_attr;
3982 attrs[1] = NULL;
3984 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3985 if (filter == NULL) {
3986 return NT_STATUS_NO_MEMORY;
3988 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3989 LDAP_SCOPE_BASE, filter, attrs, 0,
3990 &result);
3991 TALLOC_FREE(filter);
3992 if (rc != LDAP_SUCCESS) {
3993 return ntstatus;
3996 count = ldap_count_entries(priv2ld(ldap_state), result);
3997 if (count < 1) {
3998 goto out;
4001 entry = ldap_first_entry(priv2ld(ldap_state), result);
4002 if (entry == NULL) {
4003 goto out;
4006 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4007 if (vals == NULL) {
4008 goto out;
4011 *value = (uint32_t)atol(vals[0]);
4013 ntstatus = NT_STATUS_OK;
4015 out:
4016 if (vals)
4017 ldap_value_free(vals);
4018 ldap_msgfree(result);
4020 return ntstatus;
4023 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4025 - if user hasn't decided to use account policies inside LDAP just reuse the
4026 old tdb values
4028 - if there is a valid cache entry, return that
4029 - if there is an LDAP entry, update cache and return
4030 - otherwise set to default, update cache and return
4032 Guenther
4034 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4035 enum pdb_policy_type type,
4036 uint32_t *value)
4038 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4040 if (cache_account_policy_get(type, value)) {
4041 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4042 "cache\n"));
4043 return NT_STATUS_OK;
4046 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4047 value);
4048 if (NT_STATUS_IS_OK(ntstatus)) {
4049 goto update_cache;
4052 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4053 "ldap\n"));
4055 #if 0
4056 /* should we automagically migrate old tdb value here ? */
4057 if (account_policy_get(type, value))
4058 goto update_ldap;
4060 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4061 "default\n", type));
4062 #endif
4064 if (!account_policy_get_default(type, value)) {
4065 return ntstatus;
4068 /* update_ldap: */
4070 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4071 if (!NT_STATUS_IS_OK(ntstatus)) {
4072 return ntstatus;
4075 update_cache:
4077 if (!cache_account_policy_set(type, *value)) {
4078 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4079 "tdb as a cache\n"));
4080 return NT_STATUS_UNSUCCESSFUL;
4083 return NT_STATUS_OK;
4086 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4087 const struct dom_sid *domain_sid,
4088 int num_rids,
4089 uint32_t *rids,
4090 const char **names,
4091 enum lsa_SidType *attrs)
4093 struct ldapsam_privates *ldap_state =
4094 (struct ldapsam_privates *)methods->private_data;
4095 LDAPMessage *msg = NULL;
4096 LDAPMessage *entry;
4097 char *allsids = NULL;
4098 int i, rc, num_mapped;
4099 NTSTATUS result = NT_STATUS_NO_MEMORY;
4100 TALLOC_CTX *mem_ctx;
4101 LDAP *ld;
4102 bool is_builtin;
4104 mem_ctx = talloc_new(NULL);
4105 if (mem_ctx == NULL) {
4106 DEBUG(0, ("talloc_new failed\n"));
4107 goto done;
4110 if (!sid_check_is_builtin(domain_sid) &&
4111 !sid_check_is_domain(domain_sid)) {
4112 result = NT_STATUS_INVALID_PARAMETER;
4113 goto done;
4116 if (num_rids == 0) {
4117 result = NT_STATUS_NONE_MAPPED;
4118 goto done;
4121 for (i=0; i<num_rids; i++)
4122 attrs[i] = SID_NAME_UNKNOWN;
4124 allsids = talloc_strdup(mem_ctx, "");
4125 if (allsids == NULL) {
4126 goto done;
4129 for (i=0; i<num_rids; i++) {
4130 struct dom_sid sid;
4131 sid_compose(&sid, domain_sid, rids[i]);
4132 allsids = talloc_asprintf_append_buffer(
4133 allsids, "(sambaSid=%s)",
4134 sid_string_talloc(mem_ctx, &sid));
4135 if (allsids == NULL) {
4136 goto done;
4140 /* First look for users */
4143 char *filter;
4144 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4146 filter = talloc_asprintf(
4147 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4148 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4150 if (filter == NULL) {
4151 goto done;
4154 rc = smbldap_search(ldap_state->smbldap_state,
4155 lp_ldap_user_suffix(),
4156 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4157 &msg);
4158 talloc_autofree_ldapmsg(mem_ctx, msg);
4161 if (rc != LDAP_SUCCESS)
4162 goto done;
4164 ld = ldap_state->smbldap_state->ldap_struct;
4165 num_mapped = 0;
4167 for (entry = ldap_first_entry(ld, msg);
4168 entry != NULL;
4169 entry = ldap_next_entry(ld, entry)) {
4170 uint32_t rid;
4171 int rid_index;
4172 const char *name;
4174 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4175 &rid)) {
4176 DEBUG(2, ("Could not find sid from ldap entry\n"));
4177 continue;
4180 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4181 names);
4182 if (name == NULL) {
4183 DEBUG(2, ("Could not retrieve uid attribute\n"));
4184 continue;
4187 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4188 if (rid == rids[rid_index])
4189 break;
4192 if (rid_index == num_rids) {
4193 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4194 continue;
4197 attrs[rid_index] = SID_NAME_USER;
4198 names[rid_index] = name;
4199 num_mapped += 1;
4202 if (num_mapped == num_rids) {
4203 /* No need to look for groups anymore -- we're done */
4204 result = NT_STATUS_OK;
4205 goto done;
4208 /* Same game for groups */
4211 char *filter;
4212 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4213 "sambaGroupType", NULL };
4215 filter = talloc_asprintf(
4216 mem_ctx, "(&(objectClass=%s)(|%s))",
4217 LDAP_OBJ_GROUPMAP, allsids);
4218 if (filter == NULL) {
4219 goto done;
4222 rc = smbldap_search(ldap_state->smbldap_state,
4223 lp_ldap_suffix(),
4224 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4225 &msg);
4226 talloc_autofree_ldapmsg(mem_ctx, msg);
4229 if (rc != LDAP_SUCCESS)
4230 goto done;
4232 /* ldap_struct might have changed due to a reconnect */
4234 ld = ldap_state->smbldap_state->ldap_struct;
4236 /* For consistency checks, we already checked we're only domain or builtin */
4238 is_builtin = sid_check_is_builtin(domain_sid);
4240 for (entry = ldap_first_entry(ld, msg);
4241 entry != NULL;
4242 entry = ldap_next_entry(ld, entry))
4244 uint32_t rid;
4245 int rid_index;
4246 const char *attr;
4247 enum lsa_SidType type;
4248 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4250 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4251 mem_ctx);
4252 if (attr == NULL) {
4253 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4254 dn));
4255 continue;
4258 type = (enum lsa_SidType)atol(attr);
4260 /* Consistency checks */
4261 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4262 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4263 (type != SID_NAME_DOM_GRP)))) {
4264 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4267 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4268 &rid)) {
4269 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4270 continue;
4273 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4275 if (attr == NULL) {
4276 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4277 dn));
4278 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4281 if (attr == NULL) {
4282 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4283 dn));
4284 continue;
4287 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4288 if (rid == rids[rid_index])
4289 break;
4292 if (rid_index == num_rids) {
4293 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4294 continue;
4297 attrs[rid_index] = type;
4298 names[rid_index] = attr;
4299 num_mapped += 1;
4302 result = NT_STATUS_NONE_MAPPED;
4304 if (num_mapped > 0)
4305 result = (num_mapped == num_rids) ?
4306 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4307 done:
4308 TALLOC_FREE(mem_ctx);
4309 return result;
4312 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4314 char *filter = NULL;
4315 char *escaped = NULL;
4316 char *result = NULL;
4318 if (asprintf(&filter, "(&%s(objectclass=%s))",
4319 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4320 goto done;
4323 escaped = escape_ldap_string(talloc_tos(), username);
4324 if (escaped == NULL) goto done;
4326 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4328 done:
4329 SAFE_FREE(filter);
4330 TALLOC_FREE(escaped);
4332 return result;
4335 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4337 int i, num = 0;
4338 va_list ap;
4339 const char **result;
4341 va_start(ap, mem_ctx);
4342 while (va_arg(ap, const char *) != NULL)
4343 num += 1;
4344 va_end(ap);
4346 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4347 return NULL;
4350 va_start(ap, mem_ctx);
4351 for (i=0; i<num; i++) {
4352 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4353 if (result[i] == NULL) {
4354 talloc_free(result);
4355 va_end(ap);
4356 return NULL;
4359 va_end(ap);
4361 result[num] = NULL;
4362 return result;
4365 struct ldap_search_state {
4366 struct smbldap_state *connection;
4368 uint32_t acct_flags;
4369 uint16_t group_type;
4371 const char *base;
4372 int scope;
4373 const char *filter;
4374 const char **attrs;
4375 int attrsonly;
4376 void *pagedresults_cookie;
4378 LDAPMessage *entries, *current_entry;
4379 bool (*ldap2displayentry)(struct ldap_search_state *state,
4380 TALLOC_CTX *mem_ctx,
4381 LDAP *ld, LDAPMessage *entry,
4382 struct samr_displayentry *result);
4385 static bool ldapsam_search_firstpage(struct pdb_search *search)
4387 struct ldap_search_state *state =
4388 (struct ldap_search_state *)search->private_data;
4389 LDAP *ld;
4390 int rc = LDAP_OPERATIONS_ERROR;
4392 state->entries = NULL;
4394 if (state->connection->paged_results) {
4395 rc = smbldap_search_paged(state->connection, state->base,
4396 state->scope, state->filter,
4397 state->attrs, state->attrsonly,
4398 lp_ldap_page_size(), &state->entries,
4399 &state->pagedresults_cookie);
4402 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4404 if (state->entries != NULL) {
4405 /* Left over from unsuccessful paged attempt */
4406 ldap_msgfree(state->entries);
4407 state->entries = NULL;
4410 rc = smbldap_search(state->connection, state->base,
4411 state->scope, state->filter, state->attrs,
4412 state->attrsonly, &state->entries);
4414 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4415 return False;
4417 /* Ok, the server was lying. It told us it could do paged
4418 * searches when it could not. */
4419 state->connection->paged_results = False;
4422 ld = state->connection->ldap_struct;
4423 if ( ld == NULL) {
4424 DEBUG(5, ("Don't have an LDAP connection right after a "
4425 "search\n"));
4426 return False;
4428 state->current_entry = ldap_first_entry(ld, state->entries);
4430 return True;
4433 static bool ldapsam_search_nextpage(struct pdb_search *search)
4435 struct ldap_search_state *state =
4436 (struct ldap_search_state *)search->private_data;
4437 int rc;
4439 if (!state->connection->paged_results) {
4440 /* There is no next page when there are no paged results */
4441 return False;
4444 rc = smbldap_search_paged(state->connection, state->base,
4445 state->scope, state->filter, state->attrs,
4446 state->attrsonly, lp_ldap_page_size(),
4447 &state->entries,
4448 &state->pagedresults_cookie);
4450 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4451 return False;
4453 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4455 if (state->current_entry == NULL) {
4456 ldap_msgfree(state->entries);
4457 state->entries = NULL;
4458 return false;
4461 return True;
4464 static bool ldapsam_search_next_entry(struct pdb_search *search,
4465 struct samr_displayentry *entry)
4467 struct ldap_search_state *state =
4468 (struct ldap_search_state *)search->private_data;
4469 bool result;
4471 retry:
4472 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4473 return False;
4475 if ((state->entries == NULL) &&
4476 !ldapsam_search_nextpage(search))
4477 return False;
4479 if (state->current_entry == NULL) {
4480 return false;
4483 result = state->ldap2displayentry(state, search,
4484 state->connection->ldap_struct,
4485 state->current_entry, entry);
4487 if (!result) {
4488 char *dn;
4489 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4490 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4491 if (dn != NULL) ldap_memfree(dn);
4494 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4496 if (state->current_entry == NULL) {
4497 ldap_msgfree(state->entries);
4498 state->entries = NULL;
4501 if (!result) goto retry;
4503 return True;
4506 static void ldapsam_search_end(struct pdb_search *search)
4508 struct ldap_search_state *state =
4509 (struct ldap_search_state *)search->private_data;
4510 int rc;
4512 if (state->pagedresults_cookie == NULL)
4513 return;
4515 if (state->entries != NULL)
4516 ldap_msgfree(state->entries);
4518 state->entries = NULL;
4519 state->current_entry = NULL;
4521 if (!state->connection->paged_results)
4522 return;
4524 /* Tell the LDAP server we're not interested in the rest anymore. */
4526 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4527 state->filter, state->attrs,
4528 state->attrsonly, 0, &state->entries,
4529 &state->pagedresults_cookie);
4531 if (rc != LDAP_SUCCESS)
4532 DEBUG(5, ("Could not end search properly\n"));
4534 return;
4537 static bool ldapuser2displayentry(struct ldap_search_state *state,
4538 TALLOC_CTX *mem_ctx,
4539 LDAP *ld, LDAPMessage *entry,
4540 struct samr_displayentry *result)
4542 char **vals;
4543 size_t converted_size;
4544 struct dom_sid sid;
4545 uint32_t acct_flags;
4547 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4548 if ((vals == NULL) || (vals[0] == NULL)) {
4549 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4550 return False;
4552 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4553 ldap_value_free(vals);
4555 if ((state->acct_flags != 0) &&
4556 ((state->acct_flags & acct_flags) == 0))
4557 return False;
4559 result->acct_flags = acct_flags;
4560 result->account_name = "";
4561 result->fullname = "";
4562 result->description = "";
4564 vals = ldap_get_values(ld, entry, "uid");
4565 if ((vals == NULL) || (vals[0] == NULL)) {
4566 DEBUG(5, ("\"uid\" not found\n"));
4567 return False;
4569 if (!pull_utf8_talloc(mem_ctx,
4570 discard_const_p(char *, &result->account_name),
4571 vals[0], &converted_size))
4573 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4574 strerror(errno)));
4577 ldap_value_free(vals);
4579 vals = ldap_get_values(ld, entry, "displayName");
4580 if ((vals == NULL) || (vals[0] == NULL))
4581 DEBUG(8, ("\"displayName\" not found\n"));
4582 else if (!pull_utf8_talloc(mem_ctx,
4583 discard_const_p(char *, &result->fullname),
4584 vals[0], &converted_size))
4586 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4587 strerror(errno)));
4590 ldap_value_free(vals);
4592 vals = ldap_get_values(ld, entry, "description");
4593 if ((vals == NULL) || (vals[0] == NULL))
4594 DEBUG(8, ("\"description\" not found\n"));
4595 else if (!pull_utf8_talloc(mem_ctx,
4596 discard_const_p(char *, &result->description),
4597 vals[0], &converted_size))
4599 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4600 strerror(errno)));
4603 ldap_value_free(vals);
4605 if ((result->account_name == NULL) ||
4606 (result->fullname == NULL) ||
4607 (result->description == NULL)) {
4608 DEBUG(0, ("talloc failed\n"));
4609 return False;
4612 vals = ldap_get_values(ld, entry, "sambaSid");
4613 if ((vals == NULL) || (vals[0] == NULL)) {
4614 DEBUG(0, ("\"objectSid\" not found\n"));
4615 return False;
4618 if (!string_to_sid(&sid, vals[0])) {
4619 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4620 ldap_value_free(vals);
4621 return False;
4623 ldap_value_free(vals);
4625 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4626 DEBUG(0, ("sid %s does not belong to our domain\n",
4627 sid_string_dbg(&sid)));
4628 return False;
4631 return True;
4635 static bool ldapsam_search_users(struct pdb_methods *methods,
4636 struct pdb_search *search,
4637 uint32_t acct_flags)
4639 struct ldapsam_privates *ldap_state =
4640 (struct ldapsam_privates *)methods->private_data;
4641 struct ldap_search_state *state;
4643 state = talloc(search, struct ldap_search_state);
4644 if (state == NULL) {
4645 DEBUG(0, ("talloc failed\n"));
4646 return False;
4649 state->connection = ldap_state->smbldap_state;
4651 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4652 state->base = lp_ldap_user_suffix();
4653 else if ((acct_flags != 0) &&
4654 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4655 state->base = lp_ldap_machine_suffix();
4656 else
4657 state->base = lp_ldap_suffix();
4659 state->acct_flags = acct_flags;
4660 state->base = talloc_strdup(search, state->base);
4661 state->scope = LDAP_SCOPE_SUBTREE;
4662 state->filter = get_ldap_filter(search, "*");
4663 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4664 "displayName", "description",
4665 "sambaAcctFlags", NULL);
4666 state->attrsonly = 0;
4667 state->pagedresults_cookie = NULL;
4668 state->entries = NULL;
4669 state->ldap2displayentry = ldapuser2displayentry;
4671 if ((state->filter == NULL) || (state->attrs == NULL)) {
4672 DEBUG(0, ("talloc failed\n"));
4673 return False;
4676 search->private_data = state;
4677 search->next_entry = ldapsam_search_next_entry;
4678 search->search_end = ldapsam_search_end;
4680 return ldapsam_search_firstpage(search);
4683 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4684 TALLOC_CTX *mem_ctx,
4685 LDAP *ld, LDAPMessage *entry,
4686 struct samr_displayentry *result)
4688 char **vals;
4689 size_t converted_size;
4690 struct dom_sid sid;
4691 uint16_t group_type;
4693 result->account_name = "";
4694 result->fullname = "";
4695 result->description = "";
4698 vals = ldap_get_values(ld, entry, "sambaGroupType");
4699 if ((vals == NULL) || (vals[0] == NULL)) {
4700 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4701 if (vals != NULL) {
4702 ldap_value_free(vals);
4704 return False;
4707 group_type = atoi(vals[0]);
4709 if ((state->group_type != 0) &&
4710 ((state->group_type != group_type))) {
4711 ldap_value_free(vals);
4712 return False;
4715 ldap_value_free(vals);
4717 /* display name is the NT group name */
4719 vals = ldap_get_values(ld, entry, "displayName");
4720 if ((vals == NULL) || (vals[0] == NULL)) {
4721 DEBUG(8, ("\"displayName\" not found\n"));
4723 /* fallback to the 'cn' attribute */
4724 vals = ldap_get_values(ld, entry, "cn");
4725 if ((vals == NULL) || (vals[0] == NULL)) {
4726 DEBUG(5, ("\"cn\" not found\n"));
4727 return False;
4729 if (!pull_utf8_talloc(mem_ctx,
4730 discard_const_p(char *,
4731 &result->account_name),
4732 vals[0], &converted_size))
4734 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4735 "failed: %s", strerror(errno)));
4738 else if (!pull_utf8_talloc(mem_ctx,
4739 discard_const_p(char *,
4740 &result->account_name),
4741 vals[0], &converted_size))
4743 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4744 strerror(errno)));
4747 ldap_value_free(vals);
4749 vals = ldap_get_values(ld, entry, "description");
4750 if ((vals == NULL) || (vals[0] == NULL))
4751 DEBUG(8, ("\"description\" not found\n"));
4752 else if (!pull_utf8_talloc(mem_ctx,
4753 discard_const_p(char *, &result->description),
4754 vals[0], &converted_size))
4756 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4757 strerror(errno)));
4759 ldap_value_free(vals);
4761 if ((result->account_name == NULL) ||
4762 (result->fullname == NULL) ||
4763 (result->description == NULL)) {
4764 DEBUG(0, ("talloc failed\n"));
4765 return False;
4768 vals = ldap_get_values(ld, entry, "sambaSid");
4769 if ((vals == NULL) || (vals[0] == NULL)) {
4770 DEBUG(0, ("\"objectSid\" not found\n"));
4771 if (vals != NULL) {
4772 ldap_value_free(vals);
4774 return False;
4777 if (!string_to_sid(&sid, vals[0])) {
4778 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4779 return False;
4782 ldap_value_free(vals);
4784 switch (group_type) {
4785 case SID_NAME_DOM_GRP:
4786 case SID_NAME_ALIAS:
4788 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4789 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4791 DEBUG(0, ("%s is not in our domain\n",
4792 sid_string_dbg(&sid)));
4793 return False;
4795 break;
4797 default:
4798 DEBUG(0,("unknown group type: %d\n", group_type));
4799 return False;
4802 result->acct_flags = 0;
4804 return True;
4807 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4808 struct pdb_search *search,
4809 const struct dom_sid *sid,
4810 enum lsa_SidType type)
4812 struct ldapsam_privates *ldap_state =
4813 (struct ldapsam_privates *)methods->private_data;
4814 struct ldap_search_state *state;
4815 fstring tmp;
4817 state = talloc(search, struct ldap_search_state);
4818 if (state == NULL) {
4819 DEBUG(0, ("talloc failed\n"));
4820 return False;
4823 state->connection = ldap_state->smbldap_state;
4825 state->base = talloc_strdup(search, lp_ldap_suffix());
4826 state->connection = ldap_state->smbldap_state;
4827 state->scope = LDAP_SCOPE_SUBTREE;
4828 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4829 "(sambaGroupType=%d)(sambaSID=%s*))",
4830 LDAP_OBJ_GROUPMAP,
4831 type, sid_to_fstring(tmp, sid));
4832 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4833 "displayName", "description",
4834 "sambaGroupType", NULL);
4835 state->attrsonly = 0;
4836 state->pagedresults_cookie = NULL;
4837 state->entries = NULL;
4838 state->group_type = type;
4839 state->ldap2displayentry = ldapgroup2displayentry;
4841 if ((state->filter == NULL) || (state->attrs == NULL)) {
4842 DEBUG(0, ("talloc failed\n"));
4843 return False;
4846 search->private_data = state;
4847 search->next_entry = ldapsam_search_next_entry;
4848 search->search_end = ldapsam_search_end;
4850 return ldapsam_search_firstpage(search);
4853 static bool ldapsam_search_groups(struct pdb_methods *methods,
4854 struct pdb_search *search)
4856 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4859 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4860 struct pdb_search *search,
4861 const struct dom_sid *sid)
4863 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4866 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4868 return PDB_CAP_STORE_RIDS;
4871 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4872 uint32_t *rid)
4874 struct smbldap_state *smbldap_state = priv->smbldap_state;
4876 LDAPMessage *result = NULL;
4877 LDAPMessage *entry = NULL;
4878 LDAPMod **mods = NULL;
4879 NTSTATUS status;
4880 char *value;
4881 int rc;
4882 uint32_t nextRid = 0;
4883 const char *dn;
4885 TALLOC_CTX *mem_ctx;
4887 mem_ctx = talloc_new(NULL);
4888 if (mem_ctx == NULL) {
4889 DEBUG(0, ("talloc_new failed\n"));
4890 return NT_STATUS_NO_MEMORY;
4893 status = smbldap_search_domain_info(smbldap_state, &result,
4894 get_global_sam_name(), False);
4895 if (!NT_STATUS_IS_OK(status)) {
4896 DEBUG(3, ("Could not get domain info: %s\n",
4897 nt_errstr(status)));
4898 goto done;
4901 talloc_autofree_ldapmsg(mem_ctx, result);
4903 entry = ldap_first_entry(priv2ld(priv), result);
4904 if (entry == NULL) {
4905 DEBUG(0, ("Could not get domain info entry\n"));
4906 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4907 goto done;
4910 /* Find the largest of the three attributes "sambaNextRid",
4911 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4912 concept of differentiating between user and group rids, and will
4913 use only "sambaNextRid" in the future. But for compatibility
4914 reasons I look if others have chosen different strategies -- VL */
4916 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4917 "sambaNextRid", mem_ctx);
4918 if (value != NULL) {
4919 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4920 nextRid = MAX(nextRid, tmp);
4923 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4924 "sambaNextUserRid", mem_ctx);
4925 if (value != NULL) {
4926 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4927 nextRid = MAX(nextRid, tmp);
4930 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4931 "sambaNextGroupRid", mem_ctx);
4932 if (value != NULL) {
4933 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4934 nextRid = MAX(nextRid, tmp);
4937 if (nextRid == 0) {
4938 nextRid = BASE_RID-1;
4941 nextRid += 1;
4943 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4944 talloc_asprintf(mem_ctx, "%d", nextRid));
4945 talloc_autofree_ldapmod(mem_ctx, mods);
4947 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4948 status = NT_STATUS_NO_MEMORY;
4949 goto done;
4952 rc = smbldap_modify(smbldap_state, dn, mods);
4954 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4955 * please retry" */
4957 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4959 done:
4960 if (NT_STATUS_IS_OK(status)) {
4961 *rid = nextRid;
4964 TALLOC_FREE(mem_ctx);
4965 return status;
4968 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4970 int i;
4972 for (i=0; i<10; i++) {
4973 NTSTATUS result = ldapsam_get_new_rid(
4974 (struct ldapsam_privates *)methods->private_data, rid);
4975 if (NT_STATUS_IS_OK(result)) {
4976 return result;
4979 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4980 return result;
4983 /* The ldap update failed (maybe a race condition), retry */
4986 /* Tried 10 times, fail. */
4987 return NT_STATUS_ACCESS_DENIED;
4990 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4992 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4993 return NT_STATUS_IS_OK(result) ? True : False;
4996 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4997 const struct dom_sid *sid,
4998 uid_t *uid, gid_t *gid,
4999 enum lsa_SidType *type)
5001 struct ldapsam_privates *priv =
5002 (struct ldapsam_privates *)methods->private_data;
5003 char *filter;
5004 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5005 NULL };
5006 LDAPMessage *result = NULL;
5007 LDAPMessage *entry = NULL;
5008 bool ret = False;
5009 char *value;
5010 int rc;
5012 TALLOC_CTX *mem_ctx;
5014 mem_ctx = talloc_new(NULL);
5015 if (mem_ctx == NULL) {
5016 DEBUG(0, ("talloc_new failed\n"));
5017 return False;
5020 filter = talloc_asprintf(mem_ctx,
5021 "(&(sambaSid=%s)"
5022 "(|(objectClass=%s)(objectClass=%s)))",
5023 sid_string_talloc(mem_ctx, sid),
5024 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5025 if (filter == NULL) {
5026 DEBUG(5, ("talloc_asprintf failed\n"));
5027 goto done;
5030 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5031 attrs, &result);
5032 if (rc != LDAP_SUCCESS) {
5033 goto done;
5035 talloc_autofree_ldapmsg(mem_ctx, result);
5037 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5038 DEBUG(10, ("Got %d entries, expected one\n",
5039 ldap_count_entries(priv2ld(priv), result)));
5040 goto done;
5043 entry = ldap_first_entry(priv2ld(priv), result);
5045 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5046 "sambaGroupType", mem_ctx);
5048 if (value != NULL) {
5049 const char *gid_str;
5050 /* It's a group */
5052 gid_str = smbldap_talloc_single_attribute(
5053 priv2ld(priv), entry, "gidNumber", mem_ctx);
5054 if (gid_str == NULL) {
5055 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5056 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5057 entry)));
5058 goto done;
5061 *gid = strtoul(gid_str, NULL, 10);
5062 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
5063 store_gid_sid_cache(sid, *gid);
5064 idmap_cache_set_sid2gid(sid, *gid);
5065 ret = True;
5066 goto done;
5069 /* It must be a user */
5071 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5072 "uidNumber", mem_ctx);
5073 if (value == NULL) {
5074 DEBUG(1, ("Could not find uidNumber in %s\n",
5075 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5076 goto done;
5079 *uid = strtoul(value, NULL, 10);
5080 *type = SID_NAME_USER;
5081 store_uid_sid_cache(sid, *uid);
5082 idmap_cache_set_sid2uid(sid, *uid);
5084 ret = True;
5085 done:
5086 TALLOC_FREE(mem_ctx);
5087 return ret;
5091 * Find the SID for a uid.
5092 * This is shortcut is only used if ldapsam:trusted is set to true.
5094 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5095 struct dom_sid *sid)
5097 struct ldapsam_privates *priv =
5098 (struct ldapsam_privates *)methods->private_data;
5099 char *filter;
5100 const char *attrs[] = { "sambaSID", NULL };
5101 LDAPMessage *result = NULL;
5102 LDAPMessage *entry = NULL;
5103 bool ret = false;
5104 char *user_sid_string;
5105 struct dom_sid user_sid;
5106 int rc;
5107 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5109 filter = talloc_asprintf(tmp_ctx,
5110 "(&(uidNumber=%u)"
5111 "(objectClass=%s)"
5112 "(objectClass=%s))",
5113 (unsigned int)uid,
5114 LDAP_OBJ_POSIXACCOUNT,
5115 LDAP_OBJ_SAMBASAMACCOUNT);
5116 if (filter == NULL) {
5117 DEBUG(3, ("talloc_asprintf failed\n"));
5118 goto done;
5121 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5122 if (rc != LDAP_SUCCESS) {
5123 goto done;
5125 talloc_autofree_ldapmsg(tmp_ctx, result);
5127 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5128 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5129 ldap_count_entries(priv2ld(priv), result),
5130 (unsigned int)uid));
5131 goto done;
5134 entry = ldap_first_entry(priv2ld(priv), result);
5136 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5137 "sambaSID", tmp_ctx);
5138 if (user_sid_string == NULL) {
5139 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5140 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5141 goto done;
5144 if (!string_to_sid(&user_sid, user_sid_string)) {
5145 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5146 user_sid_string));
5147 goto done;
5150 sid_copy(sid, &user_sid);
5152 store_uid_sid_cache(sid, uid);
5153 idmap_cache_set_sid2uid(sid, uid);
5155 ret = true;
5157 done:
5158 TALLOC_FREE(tmp_ctx);
5159 return ret;
5163 * Find the SID for a gid.
5164 * This is shortcut is only used if ldapsam:trusted is set to true.
5166 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5167 struct dom_sid *sid)
5169 struct ldapsam_privates *priv =
5170 (struct ldapsam_privates *)methods->private_data;
5171 char *filter;
5172 const char *attrs[] = { "sambaSID", NULL };
5173 LDAPMessage *result = NULL;
5174 LDAPMessage *entry = NULL;
5175 bool ret = false;
5176 char *group_sid_string;
5177 struct dom_sid group_sid;
5178 int rc;
5179 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5181 filter = talloc_asprintf(tmp_ctx,
5182 "(&(gidNumber=%u)"
5183 "(objectClass=%s))",
5184 (unsigned int)gid,
5185 LDAP_OBJ_GROUPMAP);
5186 if (filter == NULL) {
5187 DEBUG(3, ("talloc_asprintf failed\n"));
5188 goto done;
5191 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5192 if (rc != LDAP_SUCCESS) {
5193 goto done;
5195 talloc_autofree_ldapmsg(tmp_ctx, result);
5197 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5198 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5199 ldap_count_entries(priv2ld(priv), result),
5200 (unsigned int)gid));
5201 goto done;
5204 entry = ldap_first_entry(priv2ld(priv), result);
5206 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5207 "sambaSID", tmp_ctx);
5208 if (group_sid_string == NULL) {
5209 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5210 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5211 goto done;
5214 if (!string_to_sid(&group_sid, group_sid_string)) {
5215 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5216 group_sid_string));
5217 goto done;
5220 sid_copy(sid, &group_sid);
5222 store_gid_sid_cache(sid, gid);
5223 idmap_cache_set_sid2gid(sid, gid);
5225 ret = true;
5227 done:
5228 TALLOC_FREE(tmp_ctx);
5229 return ret;
5234 * The following functions are called only if
5235 * ldapsam:trusted and ldapsam:editposix are
5236 * set to true
5240 * ldapsam_create_user creates a new
5241 * posixAccount and sambaSamAccount object
5242 * in the ldap users subtree
5244 * The uid is allocated by winbindd.
5247 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5248 TALLOC_CTX *tmp_ctx, const char *name,
5249 uint32_t acb_info, uint32_t *rid)
5251 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5252 LDAPMessage *entry = NULL;
5253 LDAPMessage *result = NULL;
5254 uint32_t num_result;
5255 bool is_machine = False;
5256 bool add_posix = False;
5257 LDAPMod **mods = NULL;
5258 struct samu *user;
5259 char *filter;
5260 char *username;
5261 char *homedir;
5262 char *gidstr;
5263 char *uidstr;
5264 char *shell;
5265 const char *dn = NULL;
5266 struct dom_sid group_sid;
5267 struct dom_sid user_sid;
5268 gid_t gid = -1;
5269 uid_t uid = -1;
5270 NTSTATUS ret;
5271 int rc;
5273 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5274 acb_info & ACB_WSTRUST ||
5275 acb_info & ACB_SVRTRUST ||
5276 acb_info & ACB_DOMTRUST) {
5277 is_machine = True;
5280 username = escape_ldap_string(talloc_tos(), name);
5281 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5282 username, LDAP_OBJ_POSIXACCOUNT);
5283 TALLOC_FREE(username);
5285 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5286 if (rc != LDAP_SUCCESS) {
5287 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5288 return NT_STATUS_ACCESS_DENIED;
5290 talloc_autofree_ldapmsg(tmp_ctx, result);
5292 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5294 if (num_result > 1) {
5295 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5296 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5299 if (num_result == 1) {
5300 char *tmp;
5301 /* check if it is just a posix account.
5302 * or if there is a sid attached to this entry
5305 entry = ldap_first_entry(priv2ld(ldap_state), result);
5306 if (!entry) {
5307 return NT_STATUS_UNSUCCESSFUL;
5310 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5311 if (tmp) {
5312 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5313 return NT_STATUS_USER_EXISTS;
5316 /* it is just a posix account, retrieve the dn for later use */
5317 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5318 if (!dn) {
5319 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5320 return NT_STATUS_NO_MEMORY;
5324 if (num_result == 0) {
5325 add_posix = True;
5328 /* Create the basic samu structure and generate the mods for the ldap commit */
5329 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5330 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5331 return ret;
5334 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5336 user = samu_new(tmp_ctx);
5337 if (!user) {
5338 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5339 return NT_STATUS_NO_MEMORY;
5342 if (!pdb_set_username(user, name, PDB_SET)) {
5343 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5344 return NT_STATUS_UNSUCCESSFUL;
5346 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5347 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5348 return NT_STATUS_UNSUCCESSFUL;
5350 if (is_machine) {
5351 if (acb_info & ACB_NORMAL) {
5352 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5353 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5354 return NT_STATUS_UNSUCCESSFUL;
5356 } else {
5357 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5358 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5359 return NT_STATUS_UNSUCCESSFUL;
5362 } else {
5363 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5364 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5365 return NT_STATUS_UNSUCCESSFUL;
5369 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5370 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5371 return NT_STATUS_UNSUCCESSFUL;
5374 if (!init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed)) {
5375 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5376 return NT_STATUS_UNSUCCESSFUL;
5379 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5380 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5382 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5384 if (add_posix) {
5385 char *escape_name;
5387 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5389 /* retrieve the Domain Users group gid */
5390 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5391 !sid_to_gid(&group_sid, &gid)) {
5392 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5393 return NT_STATUS_INVALID_PRIMARY_GROUP;
5396 /* lets allocate a new userid for this user */
5397 if (!winbind_allocate_uid(&uid)) {
5398 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5399 return NT_STATUS_UNSUCCESSFUL;
5403 if (is_machine) {
5404 /* TODO: choose a more appropriate default for machines */
5405 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5406 shell = talloc_strdup(tmp_ctx, "/bin/false");
5407 } else {
5408 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5409 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5411 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5412 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5414 escape_name = escape_rdn_val_string_alloc(name);
5415 if (!escape_name) {
5416 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5417 return NT_STATUS_NO_MEMORY;
5420 if (is_machine) {
5421 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5422 } else {
5423 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5426 SAFE_FREE(escape_name);
5428 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5429 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5430 return NT_STATUS_NO_MEMORY;
5433 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5434 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5435 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5436 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5437 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5438 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5439 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5442 talloc_autofree_ldapmod(tmp_ctx, mods);
5444 if (add_posix) {
5445 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5446 } else {
5447 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5450 if (rc != LDAP_SUCCESS) {
5451 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5452 return NT_STATUS_UNSUCCESSFUL;
5455 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5457 flush_pwnam_cache();
5459 return NT_STATUS_OK;
5462 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5464 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5465 LDAPMessage *result = NULL;
5466 LDAPMessage *entry = NULL;
5467 int num_result;
5468 const char *dn;
5469 char *filter;
5470 int rc;
5472 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5474 filter = talloc_asprintf(tmp_ctx,
5475 "(&(uid=%s)"
5476 "(objectClass=%s)"
5477 "(objectClass=%s))",
5478 pdb_get_username(sam_acct),
5479 LDAP_OBJ_POSIXACCOUNT,
5480 LDAP_OBJ_SAMBASAMACCOUNT);
5481 if (filter == NULL) {
5482 return NT_STATUS_NO_MEMORY;
5485 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5486 if (rc != LDAP_SUCCESS) {
5487 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5488 return NT_STATUS_UNSUCCESSFUL;
5490 talloc_autofree_ldapmsg(tmp_ctx, result);
5492 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5494 if (num_result == 0) {
5495 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5496 return NT_STATUS_NO_SUCH_USER;
5499 if (num_result > 1) {
5500 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5501 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5504 entry = ldap_first_entry(priv2ld(ldap_state), result);
5505 if (!entry) {
5506 return NT_STATUS_UNSUCCESSFUL;
5509 /* it is just a posix account, retrieve the dn for later use */
5510 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5511 if (!dn) {
5512 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5513 return NT_STATUS_NO_MEMORY;
5516 /* try to remove memberships first */
5518 NTSTATUS status;
5519 struct dom_sid *sids = NULL;
5520 gid_t *gids = NULL;
5521 uint32_t num_groups = 0;
5522 int i;
5523 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5525 status = ldapsam_enum_group_memberships(my_methods,
5526 tmp_ctx,
5527 sam_acct,
5528 &sids,
5529 &gids,
5530 &num_groups);
5531 if (!NT_STATUS_IS_OK(status)) {
5532 goto delete_dn;
5535 for (i=0; i < num_groups; i++) {
5537 uint32_t group_rid;
5539 sid_peek_rid(&sids[i], &group_rid);
5541 ldapsam_del_groupmem(my_methods,
5542 tmp_ctx,
5543 group_rid,
5544 user_rid);
5548 delete_dn:
5550 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5551 if (rc != LDAP_SUCCESS) {
5552 return NT_STATUS_UNSUCCESSFUL;
5555 flush_pwnam_cache();
5557 return NT_STATUS_OK;
5561 * ldapsam_create_group creates a new
5562 * posixGroup and sambaGroupMapping object
5563 * in the ldap groups subtree
5565 * The gid is allocated by winbindd.
5568 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5569 TALLOC_CTX *tmp_ctx,
5570 const char *name,
5571 uint32_t *rid)
5573 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5574 NTSTATUS ret;
5575 LDAPMessage *entry = NULL;
5576 LDAPMessage *result = NULL;
5577 uint32_t num_result;
5578 bool is_new_entry = False;
5579 LDAPMod **mods = NULL;
5580 char *filter;
5581 char *groupsidstr;
5582 char *groupname;
5583 char *grouptype;
5584 char *gidstr;
5585 const char *dn = NULL;
5586 struct dom_sid group_sid;
5587 gid_t gid = -1;
5588 int rc;
5590 groupname = escape_ldap_string(talloc_tos(), name);
5591 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5592 groupname, LDAP_OBJ_POSIXGROUP);
5593 TALLOC_FREE(groupname);
5595 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5596 if (rc != LDAP_SUCCESS) {
5597 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5598 return NT_STATUS_UNSUCCESSFUL;
5600 talloc_autofree_ldapmsg(tmp_ctx, result);
5602 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5604 if (num_result > 1) {
5605 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5606 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5609 if (num_result == 1) {
5610 char *tmp;
5611 /* check if it is just a posix group.
5612 * or if there is a sid attached to this entry
5615 entry = ldap_first_entry(priv2ld(ldap_state), result);
5616 if (!entry) {
5617 return NT_STATUS_UNSUCCESSFUL;
5620 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5621 if (tmp) {
5622 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5623 return NT_STATUS_GROUP_EXISTS;
5626 /* it is just a posix group, retrieve the gid and the dn for later use */
5627 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5628 if (!tmp) {
5629 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5630 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5633 gid = strtoul(tmp, NULL, 10);
5635 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5636 if (!dn) {
5637 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5638 return NT_STATUS_NO_MEMORY;
5642 if (num_result == 0) {
5643 is_new_entry = true;
5646 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5647 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5648 return ret;
5651 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5653 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5654 &group_sid));
5655 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5657 if (!groupsidstr || !grouptype) {
5658 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5659 return NT_STATUS_NO_MEMORY;
5662 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5663 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5664 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5665 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5667 if (is_new_entry) {
5668 char *escape_name;
5670 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5672 /* lets allocate a new groupid for this group */
5673 if (!winbind_allocate_gid(&gid)) {
5674 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5675 return NT_STATUS_UNSUCCESSFUL;
5678 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5680 escape_name = escape_rdn_val_string_alloc(name);
5681 if (!escape_name) {
5682 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5683 return NT_STATUS_NO_MEMORY;
5686 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5688 SAFE_FREE(escape_name);
5690 if (!gidstr || !dn) {
5691 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5692 return NT_STATUS_NO_MEMORY;
5695 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5696 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5697 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5700 talloc_autofree_ldapmod(tmp_ctx, mods);
5702 if (is_new_entry) {
5703 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5704 #if 0
5705 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5706 /* This call may fail with rfc2307bis schema */
5707 /* Retry adding a structural class */
5708 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5709 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5711 #endif
5712 } else {
5713 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5716 if (rc != LDAP_SUCCESS) {
5717 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5718 return NT_STATUS_UNSUCCESSFUL;
5721 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5723 return NT_STATUS_OK;
5726 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5728 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5729 LDAPMessage *result = NULL;
5730 LDAPMessage *entry = NULL;
5731 int num_result;
5732 const char *dn;
5733 char *gidstr;
5734 char *filter;
5735 struct dom_sid group_sid;
5736 int rc;
5738 /* get the group sid */
5739 sid_compose(&group_sid, get_global_sam_sid(), rid);
5741 filter = talloc_asprintf(tmp_ctx,
5742 "(&(sambaSID=%s)"
5743 "(objectClass=%s)"
5744 "(objectClass=%s))",
5745 sid_string_talloc(tmp_ctx, &group_sid),
5746 LDAP_OBJ_POSIXGROUP,
5747 LDAP_OBJ_GROUPMAP);
5748 if (filter == NULL) {
5749 return NT_STATUS_NO_MEMORY;
5752 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5753 if (rc != LDAP_SUCCESS) {
5754 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5755 return NT_STATUS_UNSUCCESSFUL;
5757 talloc_autofree_ldapmsg(tmp_ctx, result);
5759 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5761 if (num_result == 0) {
5762 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5763 return NT_STATUS_NO_SUCH_GROUP;
5766 if (num_result > 1) {
5767 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5768 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5771 entry = ldap_first_entry(priv2ld(ldap_state), result);
5772 if (!entry) {
5773 return NT_STATUS_UNSUCCESSFUL;
5776 /* here it is, retrieve the dn for later use */
5777 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5778 if (!dn) {
5779 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5780 return NT_STATUS_NO_MEMORY;
5783 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5784 if (!gidstr) {
5785 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5786 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5789 /* check no user have this group marked as primary group */
5790 filter = talloc_asprintf(tmp_ctx,
5791 "(&(gidNumber=%s)"
5792 "(objectClass=%s)"
5793 "(objectClass=%s))",
5794 gidstr,
5795 LDAP_OBJ_POSIXACCOUNT,
5796 LDAP_OBJ_SAMBASAMACCOUNT);
5798 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5799 if (rc != LDAP_SUCCESS) {
5800 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5801 return NT_STATUS_UNSUCCESSFUL;
5803 talloc_autofree_ldapmsg(tmp_ctx, result);
5805 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5807 if (num_result != 0) {
5808 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5809 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5812 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5813 if (rc != LDAP_SUCCESS) {
5814 return NT_STATUS_UNSUCCESSFUL;
5817 return NT_STATUS_OK;
5820 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5821 TALLOC_CTX *tmp_ctx,
5822 uint32_t group_rid,
5823 uint32_t member_rid,
5824 int modop)
5826 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5827 LDAPMessage *entry = NULL;
5828 LDAPMessage *result = NULL;
5829 uint32_t num_result;
5830 LDAPMod **mods = NULL;
5831 char *filter;
5832 char *uidstr;
5833 const char *dn = NULL;
5834 struct dom_sid group_sid;
5835 struct dom_sid member_sid;
5836 int rc;
5838 switch (modop) {
5839 case LDAP_MOD_ADD:
5840 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5841 break;
5842 case LDAP_MOD_DELETE:
5843 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5844 break;
5845 default:
5846 return NT_STATUS_UNSUCCESSFUL;
5849 /* get member sid */
5850 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5852 /* get the group sid */
5853 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5855 filter = talloc_asprintf(tmp_ctx,
5856 "(&(sambaSID=%s)"
5857 "(objectClass=%s)"
5858 "(objectClass=%s))",
5859 sid_string_talloc(tmp_ctx, &member_sid),
5860 LDAP_OBJ_POSIXACCOUNT,
5861 LDAP_OBJ_SAMBASAMACCOUNT);
5862 if (filter == NULL) {
5863 return NT_STATUS_NO_MEMORY;
5866 /* get the member uid */
5867 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5868 if (rc != LDAP_SUCCESS) {
5869 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5870 return NT_STATUS_UNSUCCESSFUL;
5872 talloc_autofree_ldapmsg(tmp_ctx, result);
5874 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5876 if (num_result == 0) {
5877 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5878 return NT_STATUS_NO_SUCH_MEMBER;
5881 if (num_result > 1) {
5882 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5883 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5886 entry = ldap_first_entry(priv2ld(ldap_state), result);
5887 if (!entry) {
5888 return NT_STATUS_UNSUCCESSFUL;
5891 if (modop == LDAP_MOD_DELETE) {
5892 /* check if we are trying to remove the member from his primary group */
5893 char *gidstr;
5894 gid_t user_gid, group_gid;
5896 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5897 if (!gidstr) {
5898 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5899 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5902 user_gid = strtoul(gidstr, NULL, 10);
5904 if (!sid_to_gid(&group_sid, &group_gid)) {
5905 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5906 return NT_STATUS_UNSUCCESSFUL;
5909 if (user_gid == group_gid) {
5910 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5911 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5915 /* here it is, retrieve the uid for later use */
5916 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5917 if (!uidstr) {
5918 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5919 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5922 filter = talloc_asprintf(tmp_ctx,
5923 "(&(sambaSID=%s)"
5924 "(objectClass=%s)"
5925 "(objectClass=%s))",
5926 sid_string_talloc(tmp_ctx, &group_sid),
5927 LDAP_OBJ_POSIXGROUP,
5928 LDAP_OBJ_GROUPMAP);
5930 /* get the group */
5931 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5932 if (rc != LDAP_SUCCESS) {
5933 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5934 return NT_STATUS_UNSUCCESSFUL;
5936 talloc_autofree_ldapmsg(tmp_ctx, result);
5938 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5940 if (num_result == 0) {
5941 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5942 return NT_STATUS_NO_SUCH_GROUP;
5945 if (num_result > 1) {
5946 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5947 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5950 entry = ldap_first_entry(priv2ld(ldap_state), result);
5951 if (!entry) {
5952 return NT_STATUS_UNSUCCESSFUL;
5955 /* here it is, retrieve the dn for later use */
5956 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5957 if (!dn) {
5958 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5959 return NT_STATUS_NO_MEMORY;
5962 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5964 talloc_autofree_ldapmod(tmp_ctx, mods);
5966 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5967 if (rc != LDAP_SUCCESS) {
5968 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5969 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5970 return NT_STATUS_MEMBER_IN_GROUP;
5972 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5973 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5974 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5976 return NT_STATUS_UNSUCCESSFUL;
5979 return NT_STATUS_OK;
5982 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5983 TALLOC_CTX *tmp_ctx,
5984 uint32_t group_rid,
5985 uint32_t member_rid)
5987 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5989 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5990 TALLOC_CTX *tmp_ctx,
5991 uint32_t group_rid,
5992 uint32_t member_rid)
5994 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5997 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5998 TALLOC_CTX *mem_ctx,
5999 struct samu *sampass)
6001 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6002 LDAPMessage *entry = NULL;
6003 LDAPMessage *result = NULL;
6004 uint32_t num_result;
6005 LDAPMod **mods = NULL;
6006 char *filter;
6007 char *escape_username;
6008 char *gidstr;
6009 const char *dn = NULL;
6010 gid_t gid;
6011 int rc;
6013 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6015 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6016 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6017 return NT_STATUS_UNSUCCESSFUL;
6019 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6020 if (!gidstr) {
6021 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6022 return NT_STATUS_NO_MEMORY;
6025 escape_username = escape_ldap_string(talloc_tos(),
6026 pdb_get_username(sampass));
6027 if (escape_username== NULL) {
6028 return NT_STATUS_NO_MEMORY;
6031 filter = talloc_asprintf(mem_ctx,
6032 "(&(uid=%s)"
6033 "(objectClass=%s)"
6034 "(objectClass=%s))",
6035 escape_username,
6036 LDAP_OBJ_POSIXACCOUNT,
6037 LDAP_OBJ_SAMBASAMACCOUNT);
6039 TALLOC_FREE(escape_username);
6041 if (filter == NULL) {
6042 return NT_STATUS_NO_MEMORY;
6045 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6046 if (rc != LDAP_SUCCESS) {
6047 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6048 return NT_STATUS_UNSUCCESSFUL;
6050 talloc_autofree_ldapmsg(mem_ctx, result);
6052 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6054 if (num_result == 0) {
6055 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6056 return NT_STATUS_NO_SUCH_USER;
6059 if (num_result > 1) {
6060 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6061 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6064 entry = ldap_first_entry(priv2ld(ldap_state), result);
6065 if (!entry) {
6066 return NT_STATUS_UNSUCCESSFUL;
6069 /* retrieve the dn for later use */
6070 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6071 if (!dn) {
6072 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6073 return NT_STATUS_NO_MEMORY;
6076 /* remove the old one, and add the new one, this way we do not risk races */
6077 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6079 if (mods == NULL) {
6080 return NT_STATUS_OK;
6083 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6085 if (rc != LDAP_SUCCESS) {
6086 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6087 pdb_get_username(sampass), gidstr));
6088 return NT_STATUS_UNSUCCESSFUL;
6091 flush_pwnam_cache();
6093 return NT_STATUS_OK;
6097 /**********************************************************************
6098 trusted domains functions
6099 *********************************************************************/
6101 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6102 const char *domain)
6104 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6105 ldap_state->domain_dn);
6108 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6109 TALLOC_CTX *mem_ctx,
6110 const char *domain, LDAPMessage **entry)
6112 int rc;
6113 char *filter;
6114 int scope = LDAP_SCOPE_SUBTREE;
6115 const char **attrs = NULL; /* NULL: get all attrs */
6116 int attrsonly = 0; /* 0: return values too */
6117 LDAPMessage *result = NULL;
6118 char *trusted_dn;
6119 uint32_t num_result;
6121 filter = talloc_asprintf(talloc_tos(),
6122 "(&(objectClass=%s)(sambaDomainName=%s))",
6123 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6125 trusted_dn = trusteddom_dn(ldap_state, domain);
6126 if (trusted_dn == NULL) {
6127 return False;
6129 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6130 filter, attrs, attrsonly, &result);
6132 if (result != NULL) {
6133 talloc_autofree_ldapmsg(mem_ctx, result);
6136 if (rc == LDAP_NO_SUCH_OBJECT) {
6137 *entry = NULL;
6138 return True;
6141 if (rc != LDAP_SUCCESS) {
6142 return False;
6145 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6147 if (num_result > 1) {
6148 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6149 "%s object for domain '%s'?!\n",
6150 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6151 return False;
6154 if (num_result == 0) {
6155 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6156 "%s object for domain %s.\n",
6157 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6158 *entry = NULL;
6159 } else {
6160 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6163 return True;
6166 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6167 const char *domain,
6168 char** pwd,
6169 struct dom_sid *sid,
6170 time_t *pass_last_set_time)
6172 struct ldapsam_privates *ldap_state =
6173 (struct ldapsam_privates *)methods->private_data;
6174 LDAPMessage *entry = NULL;
6176 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6178 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6179 (entry == NULL))
6181 return False;
6184 /* password */
6185 if (pwd != NULL) {
6186 char *pwd_str;
6187 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6188 entry, "sambaClearTextPassword", talloc_tos());
6189 if (pwd_str == NULL) {
6190 return False;
6192 /* trusteddom_pw routines do not use talloc yet... */
6193 *pwd = SMB_STRDUP(pwd_str);
6194 if (*pwd == NULL) {
6195 return False;
6199 /* last change time */
6200 if (pass_last_set_time != NULL) {
6201 char *time_str;
6202 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6203 entry, "sambaPwdLastSet", talloc_tos());
6204 if (time_str == NULL) {
6205 return False;
6207 *pass_last_set_time = (time_t)atol(time_str);
6210 /* domain sid */
6211 if (sid != NULL) {
6212 char *sid_str;
6213 struct dom_sid dom_sid;
6214 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6215 entry, "sambaSID",
6216 talloc_tos());
6217 if (sid_str == NULL) {
6218 return False;
6220 if (!string_to_sid(&dom_sid, sid_str)) {
6221 return False;
6223 sid_copy(sid, &dom_sid);
6226 return True;
6229 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6230 const char* domain,
6231 const char* pwd,
6232 const struct dom_sid *sid)
6234 struct ldapsam_privates *ldap_state =
6235 (struct ldapsam_privates *)methods->private_data;
6236 LDAPMessage *entry = NULL;
6237 LDAPMod **mods = NULL;
6238 char *prev_pwd = NULL;
6239 char *trusted_dn = NULL;
6240 int rc;
6242 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6245 * get the current entry (if there is one) in order to put the
6246 * current password into the previous password attribute
6248 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6249 return False;
6252 mods = NULL;
6253 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6254 LDAP_OBJ_TRUSTDOM_PASSWORD);
6255 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6256 domain);
6257 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6258 sid_string_tos(sid));
6259 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6260 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6261 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6262 "sambaClearTextPassword", pwd);
6264 if (entry != NULL) {
6265 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6266 entry, "sambaClearTextPassword", talloc_tos());
6267 if (prev_pwd != NULL) {
6268 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6269 "sambaPreviousClearTextPassword",
6270 prev_pwd);
6274 talloc_autofree_ldapmod(talloc_tos(), mods);
6276 trusted_dn = trusteddom_dn(ldap_state, domain);
6277 if (trusted_dn == NULL) {
6278 return False;
6280 if (entry == NULL) {
6281 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6282 } else {
6283 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6286 if (rc != LDAP_SUCCESS) {
6287 DEBUG(1, ("error writing trusted domain password!\n"));
6288 return False;
6291 return True;
6294 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6295 const char *domain)
6297 int rc;
6298 struct ldapsam_privates *ldap_state =
6299 (struct ldapsam_privates *)methods->private_data;
6300 LDAPMessage *entry = NULL;
6301 const char *trusted_dn;
6303 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6304 return False;
6307 if (entry == NULL) {
6308 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6309 "%s\n", domain));
6310 return True;
6313 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6314 entry);
6315 if (trusted_dn == NULL) {
6316 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6317 return False;
6320 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6321 if (rc != LDAP_SUCCESS) {
6322 return False;
6325 return True;
6328 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6329 TALLOC_CTX *mem_ctx,
6330 uint32_t *num_domains,
6331 struct trustdom_info ***domains)
6333 int rc;
6334 struct ldapsam_privates *ldap_state =
6335 (struct ldapsam_privates *)methods->private_data;
6336 char *filter;
6337 int scope = LDAP_SCOPE_SUBTREE;
6338 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6339 int attrsonly = 0; /* 0: return values too */
6340 LDAPMessage *result = NULL;
6341 LDAPMessage *entry = NULL;
6343 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6344 LDAP_OBJ_TRUSTDOM_PASSWORD);
6346 rc = smbldap_search(ldap_state->smbldap_state,
6347 ldap_state->domain_dn,
6348 scope,
6349 filter,
6350 attrs,
6351 attrsonly,
6352 &result);
6354 if (result != NULL) {
6355 talloc_autofree_ldapmsg(mem_ctx, result);
6358 if (rc != LDAP_SUCCESS) {
6359 return NT_STATUS_UNSUCCESSFUL;
6362 *num_domains = 0;
6363 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6364 DEBUG(1, ("talloc failed\n"));
6365 return NT_STATUS_NO_MEMORY;
6368 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6369 entry != NULL;
6370 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6372 char *dom_name, *dom_sid_str;
6373 struct trustdom_info *dom_info;
6375 dom_info = talloc(*domains, struct trustdom_info);
6376 if (dom_info == NULL) {
6377 DEBUG(1, ("talloc failed\n"));
6378 return NT_STATUS_NO_MEMORY;
6381 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6382 entry,
6383 "sambaDomainName",
6384 talloc_tos());
6385 if (dom_name == NULL) {
6386 DEBUG(1, ("talloc failed\n"));
6387 return NT_STATUS_NO_MEMORY;
6389 dom_info->name = dom_name;
6391 dom_sid_str = smbldap_talloc_single_attribute(
6392 priv2ld(ldap_state), entry, "sambaSID",
6393 talloc_tos());
6394 if (dom_sid_str == NULL) {
6395 DEBUG(1, ("talloc failed\n"));
6396 return NT_STATUS_NO_MEMORY;
6398 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6399 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6400 dom_sid_str));
6401 return NT_STATUS_UNSUCCESSFUL;
6404 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6405 domains, num_domains);
6407 if (*domains == NULL) {
6408 DEBUG(1, ("talloc failed\n"));
6409 return NT_STATUS_NO_MEMORY;
6413 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6414 return NT_STATUS_OK;
6418 /**********************************************************************
6419 Housekeeping
6420 *********************************************************************/
6422 static void free_private_data(void **vp)
6424 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6426 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6428 if ((*ldap_state)->result != NULL) {
6429 ldap_msgfree((*ldap_state)->result);
6430 (*ldap_state)->result = NULL;
6432 if ((*ldap_state)->domain_dn != NULL) {
6433 SAFE_FREE((*ldap_state)->domain_dn);
6436 *ldap_state = NULL;
6438 /* No need to free any further, as it is talloc()ed */
6441 /*********************************************************************
6442 Intitalise the parts of the pdb_methods structure that are common to
6443 all pdb_ldap modes
6444 *********************************************************************/
6446 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6448 NTSTATUS nt_status;
6449 struct ldapsam_privates *ldap_state;
6450 char *bind_dn = NULL;
6451 char *bind_secret = NULL;
6453 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6454 return nt_status;
6457 (*pdb_method)->name = "ldapsam";
6459 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6460 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6461 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6462 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6463 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6464 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6466 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6467 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6468 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6469 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6470 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6471 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6472 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6474 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6475 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6477 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6479 (*pdb_method)->capabilities = ldapsam_capabilities;
6480 (*pdb_method)->new_rid = ldapsam_new_rid;
6482 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6483 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6484 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6485 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6487 /* TODO: Setup private data and free */
6489 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6490 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6491 return NT_STATUS_NO_MEMORY;
6494 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6495 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6496 return NT_STATUS_NO_MEMORY;
6499 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6500 location, false, bind_dn, bind_secret,
6501 &ldap_state->smbldap_state);
6502 memset(bind_secret, '\0', strlen(bind_secret));
6503 SAFE_FREE(bind_secret);
6504 SAFE_FREE(bind_dn);
6505 if ( !NT_STATUS_IS_OK(nt_status) ) {
6506 return nt_status;
6509 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6510 return NT_STATUS_NO_MEMORY;
6513 (*pdb_method)->private_data = ldap_state;
6515 (*pdb_method)->free_private_data = free_private_data;
6517 return NT_STATUS_OK;
6520 /**********************************************************************
6521 Initialise the 'compat' mode for pdb_ldap
6522 *********************************************************************/
6524 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6526 NTSTATUS nt_status;
6527 struct ldapsam_privates *ldap_state;
6528 char *uri = talloc_strdup( NULL, location );
6530 trim_char( uri, '\"', '\"' );
6531 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6532 if ( uri )
6533 TALLOC_FREE( uri );
6535 if ( !NT_STATUS_IS_OK(nt_status) ) {
6536 return nt_status;
6539 (*pdb_method)->name = "ldapsam_compat";
6541 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6542 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6544 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6546 return NT_STATUS_OK;
6549 /**********************************************************************
6550 Initialise the normal mode for pdb_ldap
6551 *********************************************************************/
6553 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6555 NTSTATUS nt_status;
6556 struct ldapsam_privates *ldap_state = NULL;
6557 uint32_t alg_rid_base;
6558 char *alg_rid_base_string = NULL;
6559 LDAPMessage *result = NULL;
6560 LDAPMessage *entry = NULL;
6561 struct dom_sid ldap_domain_sid;
6562 struct dom_sid secrets_domain_sid;
6563 char *domain_sid_string = NULL;
6564 char *dn = NULL;
6565 char *uri = talloc_strdup( NULL, location );
6567 trim_char( uri, '\"', '\"' );
6568 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6570 TALLOC_FREE(uri);
6572 if (!NT_STATUS_IS_OK(nt_status)) {
6573 return nt_status;
6576 (*pdb_method)->name = "ldapsam";
6578 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6579 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6580 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6581 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6582 (*pdb_method)->search_users = ldapsam_search_users;
6583 (*pdb_method)->search_groups = ldapsam_search_groups;
6584 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6586 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6587 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6588 (*pdb_method)->enum_group_memberships =
6589 ldapsam_enum_group_memberships;
6590 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6591 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6592 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6593 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6595 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6596 (*pdb_method)->create_user = ldapsam_create_user;
6597 (*pdb_method)->delete_user = ldapsam_delete_user;
6598 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6599 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6600 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6601 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6602 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6606 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6607 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6609 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6611 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6612 &result,
6613 ldap_state->domain_name, True);
6615 if ( !NT_STATUS_IS_OK(nt_status) ) {
6616 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6617 "info, nor add one to the domain. "
6618 "We cannot work reliably without it.\n"));
6619 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6622 /* Given that the above might fail, everything below this must be
6623 * optional */
6625 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6626 result);
6627 if (!entry) {
6628 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6629 "entry\n"));
6630 ldap_msgfree(result);
6631 return NT_STATUS_UNSUCCESSFUL;
6634 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6635 if (!dn) {
6636 ldap_msgfree(result);
6637 return NT_STATUS_UNSUCCESSFUL;
6640 ldap_state->domain_dn = smb_xstrdup(dn);
6641 TALLOC_FREE(dn);
6643 domain_sid_string = smbldap_talloc_single_attribute(
6644 ldap_state->smbldap_state->ldap_struct,
6645 entry,
6646 get_userattr_key2string(ldap_state->schema_ver,
6647 LDAP_ATTR_USER_SID),
6648 talloc_tos());
6650 if (domain_sid_string) {
6651 bool found_sid;
6652 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6653 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6654 "read as a valid SID\n", domain_sid_string));
6655 ldap_msgfree(result);
6656 TALLOC_FREE(domain_sid_string);
6657 return NT_STATUS_INVALID_PARAMETER;
6659 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6660 &secrets_domain_sid);
6661 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6662 &ldap_domain_sid)) {
6663 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6664 "%s based on pdb_ldap results %s -> %s\n",
6665 ldap_state->domain_name,
6666 sid_string_dbg(&secrets_domain_sid),
6667 sid_string_dbg(&ldap_domain_sid)));
6669 /* reset secrets.tdb sid */
6670 secrets_store_domain_sid(ldap_state->domain_name,
6671 &ldap_domain_sid);
6672 DEBUG(1, ("New global sam SID: %s\n",
6673 sid_string_dbg(get_global_sam_sid())));
6675 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6676 TALLOC_FREE(domain_sid_string);
6679 alg_rid_base_string = smbldap_talloc_single_attribute(
6680 ldap_state->smbldap_state->ldap_struct,
6681 entry,
6682 get_attr_key2string( dominfo_attr_list,
6683 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6684 talloc_tos());
6685 if (alg_rid_base_string) {
6686 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6687 if (alg_rid_base != algorithmic_rid_base()) {
6688 DEBUG(0, ("The value of 'algorithmic RID base' has "
6689 "changed since the LDAP\n"
6690 "database was initialised. Aborting. \n"));
6691 ldap_msgfree(result);
6692 TALLOC_FREE(alg_rid_base_string);
6693 return NT_STATUS_UNSUCCESSFUL;
6695 TALLOC_FREE(alg_rid_base_string);
6697 ldap_msgfree(result);
6699 return NT_STATUS_OK;
6702 NTSTATUS pdb_ldap_init(void)
6704 NTSTATUS nt_status;
6705 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6706 return nt_status;
6708 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6709 return nt_status;
6711 /* Let pdb_nds register backends */
6712 pdb_nds_init();
6714 pdb_ipa_init();
6716 return NT_STATUS_OK;