lib: Update all consumers of strtoul_err(), strtoull_err() to new API
[samba.git] / source3 / passdb / pdb_ldap.c
blob85e9db8bb1f98ec8c7446953a9d5f5329d3c1527
1 /*
2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /* TODO:
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
43 * and/or winbind
46 #include "includes.h"
47 #include "passdb.h"
48 #include "../libcli/auth/libcli_auth.h"
49 #include "secrets.h"
50 #include "idmap_cache.h"
51 #include "../libcli/security/security.h"
52 #include "../lib/util/util_pw.h"
53 #include "lib/winbind_util.h"
54 #include "librpc/gen_ndr/idmap.h"
55 #include "lib/param/loadparm.h"
56 #include "lib/util_sid_passdb.h"
58 #undef DBGC_CLASS
59 #define DBGC_CLASS DBGC_PASSDB
61 #include <lber.h>
62 #include <ldap.h>
65 #include "smbldap.h"
66 #include "passdb/pdb_ldap.h"
67 #include "passdb/pdb_nds.h"
68 #include "passdb/pdb_ldap_util.h"
69 #include "passdb/pdb_ldap_schema.h"
71 /**********************************************************************
72 Simple helper function to make stuff better readable
73 **********************************************************************/
75 LDAP *priv2ld(struct ldapsam_privates *priv)
77 return smbldap_get_ldap(priv->smbldap_state);
80 /**********************************************************************
81 Get the attribute name given a user schame version.
82 **********************************************************************/
84 static const char* get_userattr_key2string( int schema_ver, int key )
86 switch ( schema_ver ) {
87 case SCHEMAVER_SAMBASAMACCOUNT:
88 return get_attr_key2string( attrib_map_v30, key );
90 default:
91 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
92 break;
94 return NULL;
97 /**********************************************************************
98 Return the list of attribute names given a user schema version.
99 **********************************************************************/
101 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
103 switch ( schema_ver ) {
104 case SCHEMAVER_SAMBASAMACCOUNT:
105 return get_attr_list( mem_ctx, attrib_map_v30 );
106 default:
107 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
108 break;
111 return NULL;
114 /**************************************************************************
115 Return the list of attribute names to delete given a user schema version.
116 **************************************************************************/
118 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
119 int schema_ver )
121 switch ( schema_ver ) {
122 case SCHEMAVER_SAMBASAMACCOUNT:
123 return get_attr_list( mem_ctx,
124 attrib_map_to_delete_v30 );
125 default:
126 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
127 break;
130 return NULL;
134 /*******************************************************************
135 Generate the LDAP search filter for the objectclass based on the
136 version of the schema we are using.
137 ******************************************************************/
139 static const char* get_objclass_filter( int schema_ver )
141 fstring objclass_filter;
142 char *result;
144 switch( schema_ver ) {
145 case SCHEMAVER_SAMBASAMACCOUNT:
146 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
147 break;
148 default:
149 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
150 objclass_filter[0] = '\0';
151 break;
154 result = talloc_strdup(talloc_tos(), objclass_filter);
155 SMB_ASSERT(result != NULL);
156 return result;
159 /*****************************************************************
160 Scan a sequence number off OpenLDAP's syncrepl contextCSN
161 ******************************************************************/
163 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
165 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
166 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
167 LDAPMessage *msg = NULL;
168 LDAPMessage *entry = NULL;
169 TALLOC_CTX *mem_ctx;
170 char **values = NULL;
171 int rc, num_result, num_values, rid;
172 char *suffix = NULL;
173 char *tok;
174 const char *p;
175 const char **attrs;
177 /* Unfortunatly there is no proper way to detect syncrepl-support in
178 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
179 * but do not show up in the root-DSE yet. Neither we can query the
180 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
181 * objectclass. Currently we require lp_ldap_suffix() to show up as
182 * namingContext. - Guenther
185 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
186 return ntstatus;
189 if (!seq_num) {
190 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
191 return ntstatus;
194 if (!smbldap_has_naming_context(
195 smbldap_get_ldap(ldap_state->smbldap_state),
196 lp_ldap_suffix(talloc_tos()))) {
197 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
198 "as top-level namingContext\n", lp_ldap_suffix(talloc_tos())));
199 return ntstatus;
202 mem_ctx = talloc_init("ldapsam_get_seq_num");
204 if (mem_ctx == NULL)
205 return NT_STATUS_NO_MEMORY;
207 if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
208 ntstatus = NT_STATUS_NO_MEMORY;
209 goto done;
212 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
213 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
214 if (rid > 0) {
216 /* consumer syncreplCookie: */
217 /* csn=20050126161620Z#0000001#00#00000 */
218 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
219 attrs[1] = NULL;
220 suffix = talloc_asprintf(mem_ctx,
221 "cn=syncrepl%d,%s", rid, lp_ldap_suffix(talloc_tos()));
222 if (!suffix) {
223 ntstatus = NT_STATUS_NO_MEMORY;
224 goto done;
226 } else {
228 /* provider contextCSN */
229 /* 20050126161620Z#000009#00#000000 */
230 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
231 attrs[1] = NULL;
232 suffix = talloc_asprintf(mem_ctx,
233 "cn=ldapsync,%s", lp_ldap_suffix(talloc_tos()));
235 if (!suffix) {
236 ntstatus = NT_STATUS_NO_MEMORY;
237 goto done;
241 rc = smbldap_search(ldap_state->smbldap_state, suffix,
242 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
244 if (rc != LDAP_SUCCESS) {
245 goto done;
248 num_result = ldap_count_entries(
249 smbldap_get_ldap(ldap_state->smbldap_state), msg);
250 if (num_result != 1) {
251 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
252 goto done;
255 entry = ldap_first_entry(
256 smbldap_get_ldap(ldap_state->smbldap_state), msg);
257 if (entry == NULL) {
258 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
259 goto done;
262 values = ldap_get_values(
263 smbldap_get_ldap(ldap_state->smbldap_state), entry, attrs[0]);
264 if (values == NULL) {
265 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
266 goto done;
269 num_values = ldap_count_values(values);
270 if (num_values == 0) {
271 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
272 goto done;
275 p = values[0];
276 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
277 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
278 goto done;
281 p = tok;
282 if (!strncmp(p, "csn=", strlen("csn=")))
283 p += strlen("csn=");
285 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
287 *seq_num = generalized_to_unix_time(p);
289 /* very basic sanity check */
290 if (*seq_num <= 0) {
291 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
292 (int)*seq_num));
293 goto done;
296 ntstatus = NT_STATUS_OK;
298 done:
299 if (values != NULL)
300 ldap_value_free(values);
301 if (msg != NULL)
302 ldap_msgfree(msg);
303 if (mem_ctx)
304 talloc_destroy(mem_ctx);
306 return ntstatus;
309 /*******************************************************************
310 Run the search by name.
311 ******************************************************************/
313 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
314 const char *user,
315 LDAPMessage ** result,
316 const char **attr)
318 char *filter = NULL;
319 char *escape_user = escape_ldap_string(talloc_tos(), user);
320 int ret = -1;
322 if (!escape_user) {
323 return LDAP_NO_MEMORY;
327 * in the filter expression, replace %u with the real name
328 * so in ldap filter, %u MUST exist :-)
330 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
331 get_objclass_filter(ldap_state->schema_ver));
332 if (!filter) {
333 TALLOC_FREE(escape_user);
334 return LDAP_NO_MEMORY;
337 * have to use this here because $ is filtered out
338 * in string_sub
341 filter = talloc_all_string_sub(talloc_tos(),
342 filter, "%u", escape_user);
343 TALLOC_FREE(escape_user);
344 if (!filter) {
345 return LDAP_NO_MEMORY;
348 ret = smbldap_search_suffix(ldap_state->smbldap_state,
349 filter, attr, result);
350 TALLOC_FREE(filter);
351 return ret;
354 /*******************************************************************
355 Run the search by SID.
356 ******************************************************************/
358 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
359 const struct dom_sid *sid, LDAPMessage ** result,
360 const char **attr)
362 char *filter = NULL;
363 int rc;
364 struct dom_sid_buf sid_string;
366 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
367 get_userattr_key2string(ldap_state->schema_ver,
368 LDAP_ATTR_USER_SID),
369 dom_sid_str_buf(sid, &sid_string),
370 get_objclass_filter(ldap_state->schema_ver));
371 if (!filter) {
372 return LDAP_NO_MEMORY;
375 rc = smbldap_search_suffix(ldap_state->smbldap_state,
376 filter, attr, result);
378 TALLOC_FREE(filter);
379 return rc;
382 /*******************************************************************
383 Delete complete object or objectclass and attrs from
384 object found in search_result depending on lp_ldap_delete_dn
385 ******************************************************************/
387 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
388 TALLOC_CTX *mem_ctx,
389 LDAPMessage *entry,
390 const char *objectclass,
391 const char **attrs)
393 LDAPMod **mods = NULL;
394 char *name;
395 const char *dn;
396 BerElement *ptr = NULL;
398 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
399 if (dn == NULL) {
400 return LDAP_NO_MEMORY;
403 if (lp_ldap_delete_dn()) {
404 return smbldap_delete(priv->smbldap_state, dn);
407 /* Ok, delete only the SAM attributes */
409 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
410 name != NULL;
411 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
412 const char **attrib;
414 /* We are only allowed to delete the attributes that
415 really exist. */
417 for (attrib = attrs; *attrib != NULL; attrib++) {
418 if (strequal(*attrib, name)) {
419 DEBUG(10, ("ldapsam_delete_entry: deleting "
420 "attribute %s\n", name));
421 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
422 NULL);
425 ldap_memfree(name);
428 if (ptr != NULL) {
429 ber_free(ptr, 0);
432 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
433 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
435 return smbldap_modify(priv->smbldap_state, dn, mods);
438 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
440 char *temp;
441 struct tm tm;
443 temp = smbldap_talloc_single_attribute(
444 smbldap_get_ldap(ldap_state->smbldap_state), entry,
445 get_userattr_key2string(ldap_state->schema_ver,
446 LDAP_ATTR_MOD_TIMESTAMP),
447 talloc_tos());
448 if (!temp) {
449 return (time_t) 0;
452 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
453 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
454 (char*)temp));
455 TALLOC_FREE(temp);
456 return (time_t) 0;
458 TALLOC_FREE(temp);
459 tzset();
460 return timegm(&tm);
463 /**********************************************************************
464 Initialize struct samu from an LDAP query.
465 (Based on init_sam_from_buffer in pdb_tdb.c)
466 *********************************************************************/
468 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
469 struct samu * sampass,
470 LDAPMessage * entry)
472 time_t logon_time,
473 logoff_time,
474 kickoff_time,
475 pass_last_set_time,
476 pass_can_change_time,
477 ldap_entry_time,
478 bad_password_time;
479 char *username = NULL,
480 *domain = NULL,
481 *nt_username = NULL,
482 *fullname = NULL,
483 *homedir = NULL,
484 *dir_drive = NULL,
485 *logon_script = NULL,
486 *profile_path = NULL,
487 *acct_desc = NULL,
488 *workstations = NULL,
489 *munged_dial = NULL;
490 uint32_t user_rid;
491 uint8_t smblmpwd[LM_HASH_LEN],
492 smbntpwd[NT_HASH_LEN];
493 bool use_samba_attrs = True;
494 uint32_t acct_ctrl = 0;
495 uint16_t logon_divs;
496 uint16_t bad_password_count = 0,
497 logon_count = 0;
498 uint32_t hours_len;
499 uint8_t hours[MAX_HOURS_LEN];
500 char *temp = NULL;
501 struct login_cache cache_entry;
502 uint32_t pwHistLen;
503 bool expand_explicit = lp_passdb_expand_explicit();
504 bool ret = false;
505 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
507 if (!ctx) {
508 return false;
510 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
511 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
512 goto fn_exit;
515 if (priv2ld(ldap_state) == NULL) {
516 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
517 "ldap_struct is NULL!\n"));
518 goto fn_exit;
521 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
522 entry,
523 "uid",
524 ctx))) {
525 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
526 "this user!\n"));
527 goto fn_exit;
530 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
532 nt_username = talloc_strdup(ctx, username);
533 if (!nt_username) {
534 goto fn_exit;
537 domain = talloc_strdup(ctx, ldap_state->domain_name);
538 if (!domain) {
539 goto fn_exit;
542 pdb_set_username(sampass, username, PDB_SET);
544 pdb_set_domain(sampass, domain, PDB_DEFAULT);
545 pdb_set_nt_username(sampass, nt_username, PDB_SET);
547 /* deal with different attributes between the schema first */
549 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
550 if ((temp = smbldap_talloc_single_attribute(
551 smbldap_get_ldap(ldap_state->smbldap_state),
552 entry,
553 get_userattr_key2string(ldap_state->schema_ver,
554 LDAP_ATTR_USER_SID),
555 ctx))!=NULL) {
556 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
558 } else {
559 if ((temp = smbldap_talloc_single_attribute(
560 smbldap_get_ldap(ldap_state->smbldap_state),
561 entry,
562 get_userattr_key2string(ldap_state->schema_ver,
563 LDAP_ATTR_USER_RID),
564 ctx))!=NULL) {
565 user_rid = (uint32_t)atol(temp);
566 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
570 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
571 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
572 get_userattr_key2string(ldap_state->schema_ver,
573 LDAP_ATTR_USER_SID),
574 get_userattr_key2string(ldap_state->schema_ver,
575 LDAP_ATTR_USER_RID),
576 username));
577 return False;
580 temp = smbldap_talloc_single_attribute(
581 smbldap_get_ldap(ldap_state->smbldap_state),
582 entry,
583 get_userattr_key2string(ldap_state->schema_ver,
584 LDAP_ATTR_PWD_LAST_SET),
585 ctx);
586 if (temp) {
587 pass_last_set_time = (time_t) atol(temp);
588 pdb_set_pass_last_set_time(sampass,
589 pass_last_set_time, PDB_SET);
592 temp = smbldap_talloc_single_attribute(
593 smbldap_get_ldap(ldap_state->smbldap_state),
594 entry,
595 get_userattr_key2string(ldap_state->schema_ver,
596 LDAP_ATTR_LOGON_TIME),
597 ctx);
598 if (temp) {
599 logon_time = (time_t) atol(temp);
600 pdb_set_logon_time(sampass, logon_time, PDB_SET);
603 temp = smbldap_talloc_single_attribute(
604 smbldap_get_ldap(ldap_state->smbldap_state),
605 entry,
606 get_userattr_key2string(ldap_state->schema_ver,
607 LDAP_ATTR_LOGOFF_TIME),
608 ctx);
609 if (temp) {
610 logoff_time = (time_t) atol(temp);
611 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
614 temp = smbldap_talloc_single_attribute(
615 smbldap_get_ldap(ldap_state->smbldap_state),
616 entry,
617 get_userattr_key2string(ldap_state->schema_ver,
618 LDAP_ATTR_KICKOFF_TIME),
619 ctx);
620 if (temp) {
621 kickoff_time = (time_t) atol(temp);
622 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
625 temp = smbldap_talloc_single_attribute(
626 smbldap_get_ldap(ldap_state->smbldap_state),
627 entry,
628 get_userattr_key2string(ldap_state->schema_ver,
629 LDAP_ATTR_PWD_CAN_CHANGE),
630 ctx);
631 if (temp) {
632 pass_can_change_time = (time_t) atol(temp);
633 pdb_set_pass_can_change_time(sampass,
634 pass_can_change_time, PDB_SET);
637 /* recommend that 'gecos' and 'displayName' should refer to the same
638 * attribute OID. userFullName depreciated, only used by Samba
639 * primary rules of LDAP: don't make a new attribute when one is already defined
640 * that fits your needs; using cn then displayName rather than 'userFullName'
643 fullname = smbldap_talloc_single_attribute(
644 smbldap_get_ldap(ldap_state->smbldap_state),
645 entry,
646 get_userattr_key2string(ldap_state->schema_ver,
647 LDAP_ATTR_DISPLAY_NAME),
648 ctx);
649 if (fullname) {
650 pdb_set_fullname(sampass, fullname, PDB_SET);
651 } else {
652 fullname = smbldap_talloc_single_attribute(
653 smbldap_get_ldap(ldap_state->smbldap_state),
654 entry,
655 get_userattr_key2string(ldap_state->schema_ver,
656 LDAP_ATTR_CN),
657 ctx);
658 if (fullname) {
659 pdb_set_fullname(sampass, fullname, PDB_SET);
663 dir_drive = smbldap_talloc_single_attribute(
664 smbldap_get_ldap(ldap_state->smbldap_state),
665 entry,
666 get_userattr_key2string(ldap_state->schema_ver,
667 LDAP_ATTR_HOME_DRIVE),
668 ctx);
669 if (dir_drive) {
670 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
671 } else {
672 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
675 homedir = smbldap_talloc_single_attribute(
676 smbldap_get_ldap(ldap_state->smbldap_state),
677 entry,
678 get_userattr_key2string(ldap_state->schema_ver,
679 LDAP_ATTR_HOME_PATH),
680 ctx);
681 if (homedir) {
682 if (expand_explicit) {
683 homedir = talloc_sub_basic(ctx,
684 username,
685 domain,
686 homedir);
687 if (!homedir) {
688 goto fn_exit;
691 pdb_set_homedir(sampass, homedir, PDB_SET);
692 } else {
693 pdb_set_homedir(sampass,
694 talloc_sub_basic(ctx, username, domain,
695 lp_logon_home()),
696 PDB_DEFAULT);
699 logon_script = smbldap_talloc_single_attribute(
700 smbldap_get_ldap(ldap_state->smbldap_state),
701 entry,
702 get_userattr_key2string(ldap_state->schema_ver,
703 LDAP_ATTR_LOGON_SCRIPT),
704 ctx);
705 if (logon_script) {
706 if (expand_explicit) {
707 logon_script = talloc_sub_basic(ctx,
708 username,
709 domain,
710 logon_script);
711 if (!logon_script) {
712 goto fn_exit;
715 pdb_set_logon_script(sampass, logon_script, PDB_SET);
716 } else {
717 pdb_set_logon_script(sampass,
718 talloc_sub_basic(ctx, username, domain,
719 lp_logon_script()),
720 PDB_DEFAULT );
723 profile_path = smbldap_talloc_single_attribute(
724 smbldap_get_ldap(ldap_state->smbldap_state),
725 entry,
726 get_userattr_key2string(ldap_state->schema_ver,
727 LDAP_ATTR_PROFILE_PATH),
728 ctx);
729 if (profile_path) {
730 if (expand_explicit) {
731 profile_path = talloc_sub_basic(ctx,
732 username,
733 domain,
734 profile_path);
735 if (!profile_path) {
736 goto fn_exit;
739 pdb_set_profile_path(sampass, profile_path, PDB_SET);
740 } else {
741 pdb_set_profile_path(sampass,
742 talloc_sub_basic(ctx, username, domain,
743 lp_logon_path()),
744 PDB_DEFAULT );
747 acct_desc = smbldap_talloc_single_attribute(
748 smbldap_get_ldap(ldap_state->smbldap_state),
749 entry,
750 get_userattr_key2string(ldap_state->schema_ver,
751 LDAP_ATTR_DESC),
752 ctx);
753 if (acct_desc) {
754 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
757 workstations = smbldap_talloc_single_attribute(
758 smbldap_get_ldap(ldap_state->smbldap_state),
759 entry,
760 get_userattr_key2string(ldap_state->schema_ver,
761 LDAP_ATTR_USER_WKS),
762 ctx);
763 if (workstations) {
764 pdb_set_workstations(sampass, workstations, PDB_SET);
767 munged_dial = smbldap_talloc_single_attribute(
768 smbldap_get_ldap(ldap_state->smbldap_state),
769 entry,
770 get_userattr_key2string(ldap_state->schema_ver,
771 LDAP_ATTR_MUNGED_DIAL),
772 ctx);
773 if (munged_dial) {
774 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
777 /* FIXME: hours stuff should be cleaner */
779 logon_divs = 168;
780 hours_len = 21;
781 memset(hours, 0xff, hours_len);
783 if (ldap_state->is_nds_ldap) {
784 char *user_dn;
785 size_t pwd_len;
786 char clear_text_pw[512];
788 /* Make call to Novell eDirectory ldap extension to get clear text password.
789 NOTE: This will only work if we have an SSL connection to eDirectory. */
790 user_dn = smbldap_talloc_dn(
791 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
792 entry);
793 if (user_dn != NULL) {
794 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
796 pwd_len = sizeof(clear_text_pw);
797 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
798 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
799 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
800 TALLOC_FREE(user_dn);
801 return False;
803 ZERO_STRUCT(smblmpwd);
804 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
805 TALLOC_FREE(user_dn);
806 return False;
808 ZERO_STRUCT(smbntpwd);
809 use_samba_attrs = False;
812 TALLOC_FREE(user_dn);
814 } else {
815 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
819 if (use_samba_attrs) {
820 temp = smbldap_talloc_single_attribute(
821 smbldap_get_ldap(ldap_state->smbldap_state),
822 entry,
823 get_userattr_key2string(ldap_state->schema_ver,
824 LDAP_ATTR_LMPW),
825 ctx);
826 if (temp) {
827 pdb_gethexpwd(temp, smblmpwd);
828 memset((char *)temp, '\0', strlen(temp)+1);
829 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
830 goto fn_exit;
832 ZERO_STRUCT(smblmpwd);
835 temp = smbldap_talloc_single_attribute(
836 smbldap_get_ldap(ldap_state->smbldap_state),
837 entry,
838 get_userattr_key2string(ldap_state->schema_ver,
839 LDAP_ATTR_NTPW),
840 ctx);
841 if (temp) {
842 pdb_gethexpwd(temp, smbntpwd);
843 memset((char *)temp, '\0', strlen(temp)+1);
844 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
845 goto fn_exit;
847 ZERO_STRUCT(smbntpwd);
851 pwHistLen = 0;
853 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
854 if (pwHistLen > 0){
855 uint8_t *pwhist = NULL;
856 int i;
857 char *history_string = talloc_array(ctx, char,
858 MAX_PW_HISTORY_LEN*64);
860 if (!history_string) {
861 goto fn_exit;
864 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
866 pwhist = talloc_zero_array(ctx, uint8_t,
867 pwHistLen * PW_HISTORY_ENTRY_LEN);
868 if (pwhist == NULL) {
869 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
870 goto fn_exit;
873 if (smbldap_get_single_attribute(
874 smbldap_get_ldap(ldap_state->smbldap_state),
875 entry,
876 get_userattr_key2string(ldap_state->schema_ver,
877 LDAP_ATTR_PWD_HISTORY),
878 history_string,
879 MAX_PW_HISTORY_LEN*64)) {
880 bool hex_failed = false;
881 for (i = 0; i < pwHistLen; i++){
882 /* Get the 16 byte salt. */
883 if (!pdb_gethexpwd(&history_string[i*64],
884 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
885 hex_failed = true;
886 break;
888 /* Get the 16 byte MD5 hash of salt+passwd. */
889 if (!pdb_gethexpwd(&history_string[(i*64)+32],
890 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
891 PW_HISTORY_SALT_LEN])) {
892 hex_failed = True;
893 break;
896 if (hex_failed) {
897 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
898 username));
899 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
902 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
903 goto fn_exit;
907 temp = smbldap_talloc_single_attribute(
908 smbldap_get_ldap(ldap_state->smbldap_state),
909 entry,
910 get_userattr_key2string(ldap_state->schema_ver,
911 LDAP_ATTR_ACB_INFO),
912 ctx);
913 if (temp) {
914 acct_ctrl = pdb_decode_acct_ctrl(temp);
916 if (acct_ctrl == 0) {
917 acct_ctrl |= ACB_NORMAL;
920 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
921 } else {
922 acct_ctrl |= ACB_NORMAL;
925 pdb_set_hours_len(sampass, hours_len, PDB_SET);
926 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
928 temp = smbldap_talloc_single_attribute(
929 smbldap_get_ldap(ldap_state->smbldap_state),
930 entry,
931 get_userattr_key2string(ldap_state->schema_ver,
932 LDAP_ATTR_BAD_PASSWORD_COUNT),
933 ctx);
934 if (temp) {
935 bad_password_count = (uint32_t) atol(temp);
936 pdb_set_bad_password_count(sampass,
937 bad_password_count, PDB_SET);
940 temp = smbldap_talloc_single_attribute(
941 smbldap_get_ldap(ldap_state->smbldap_state),
942 entry,
943 get_userattr_key2string(ldap_state->schema_ver,
944 LDAP_ATTR_BAD_PASSWORD_TIME),
945 ctx);
946 if (temp) {
947 bad_password_time = (time_t) atol(temp);
948 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
952 temp = smbldap_talloc_single_attribute(
953 smbldap_get_ldap(ldap_state->smbldap_state),
954 entry,
955 get_userattr_key2string(ldap_state->schema_ver,
956 LDAP_ATTR_LOGON_COUNT),
957 ctx);
958 if (temp) {
959 logon_count = (uint32_t) atol(temp);
960 pdb_set_logon_count(sampass, logon_count, PDB_SET);
963 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
965 temp = smbldap_talloc_single_attribute(
966 smbldap_get_ldap(ldap_state->smbldap_state),
967 entry,
968 get_userattr_key2string(ldap_state->schema_ver,
969 LDAP_ATTR_LOGON_HOURS),
970 ctx);
971 if (temp) {
972 pdb_gethexhours(temp, hours);
973 memset((char *)temp, '\0', strlen(temp) +1);
974 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
975 ZERO_STRUCT(hours);
978 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
979 struct passwd unix_pw;
980 bool have_uid = false;
981 bool have_gid = false;
982 struct dom_sid mapped_gsid;
983 const struct dom_sid *primary_gsid;
984 struct unixid id;
985 int error = 0;
987 ZERO_STRUCT(unix_pw);
989 unix_pw.pw_name = username;
990 unix_pw.pw_passwd = discard_const_p(char, "x");
992 temp = smbldap_talloc_single_attribute(
993 priv2ld(ldap_state),
994 entry,
995 "uidNumber",
996 ctx);
997 if (temp) {
998 /* We've got a uid, feed the cache */
999 unix_pw.pw_uid = strtoul_err(temp, NULL, 10, &error);
1000 if (error != 0) {
1001 DBG_ERR("Failed to convert UID\n");
1002 goto fn_exit;
1004 have_uid = true;
1006 temp = smbldap_talloc_single_attribute(
1007 priv2ld(ldap_state),
1008 entry,
1009 "gidNumber",
1010 ctx);
1011 if (temp) {
1012 /* We've got a uid, feed the cache */
1013 unix_pw.pw_gid = strtoul_err(temp, NULL, 10, &error);
1014 if (error != 0) {
1015 DBG_ERR("Failed to convert GID\n");
1016 goto fn_exit;
1018 have_gid = true;
1020 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1021 priv2ld(ldap_state),
1022 entry,
1023 "gecos",
1024 ctx);
1025 if (unix_pw.pw_gecos == NULL) {
1026 unix_pw.pw_gecos = fullname;
1028 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1029 priv2ld(ldap_state),
1030 entry,
1031 "homeDirectory",
1032 ctx);
1033 if (unix_pw.pw_dir == NULL) {
1034 unix_pw.pw_dir = discard_const_p(char, "");
1036 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state),
1038 entry,
1039 "loginShell",
1040 ctx);
1041 if (unix_pw.pw_shell == NULL) {
1042 unix_pw.pw_shell = discard_const_p(char, "");
1045 if (have_uid && have_gid) {
1046 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1047 } else {
1048 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1051 if (sampass->unix_pw == NULL) {
1052 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1053 pdb_get_username(sampass)));
1054 goto fn_exit;
1057 id.id = sampass->unix_pw->pw_uid;
1058 id.type = ID_TYPE_UID;
1060 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass), &id);
1062 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1063 primary_gsid = pdb_get_group_sid(sampass);
1064 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1065 id.id = sampass->unix_pw->pw_gid;
1066 id.type = ID_TYPE_GID;
1068 idmap_cache_set_sid2unixid(primary_gsid, &id);
1072 /* check the timestamp of the cache vs ldap entry */
1073 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1074 entry))) {
1075 ret = true;
1076 goto fn_exit;
1079 /* see if we have newer updates */
1080 if (!login_cache_read(sampass, &cache_entry)) {
1081 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1082 (unsigned int)pdb_get_bad_password_count(sampass),
1083 (unsigned int)pdb_get_bad_password_time(sampass)));
1084 ret = true;
1085 goto fn_exit;
1088 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1089 (unsigned int)ldap_entry_time,
1090 (unsigned int)cache_entry.entry_timestamp,
1091 (unsigned int)cache_entry.bad_password_time));
1093 if (ldap_entry_time > cache_entry.entry_timestamp) {
1094 /* cache is older than directory , so
1095 we need to delete the entry but allow the
1096 fields to be written out */
1097 login_cache_delentry(sampass);
1098 } else {
1099 /* read cache in */
1100 pdb_set_acct_ctrl(sampass,
1101 pdb_get_acct_ctrl(sampass) |
1102 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1103 PDB_SET);
1104 pdb_set_bad_password_count(sampass,
1105 cache_entry.bad_password_count,
1106 PDB_SET);
1107 pdb_set_bad_password_time(sampass,
1108 cache_entry.bad_password_time,
1109 PDB_SET);
1112 ret = true;
1114 fn_exit:
1116 TALLOC_FREE(ctx);
1117 return ret;
1120 /**********************************************************************
1121 Initialize the ldap db from a struct samu. Called on update.
1122 (Based on init_buffer_from_sam in pdb_tdb.c)
1123 *********************************************************************/
1125 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1126 LDAPMessage *existing,
1127 LDAPMod *** mods, struct samu * sampass,
1128 bool (*need_update)(const struct samu *,
1129 enum pdb_elements))
1131 char *temp = NULL;
1133 if (mods == NULL || sampass == NULL) {
1134 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1135 return False;
1138 *mods = NULL;
1141 * took out adding "objectclass: sambaAccount"
1142 * do this on a per-mod basis
1144 if (need_update(sampass, PDB_USERNAME)) {
1145 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1146 existing, mods,
1147 "uid", pdb_get_username(sampass));
1148 if (ldap_state->is_nds_ldap) {
1149 smbldap_make_mod(
1150 smbldap_get_ldap(ldap_state->smbldap_state),
1151 existing, mods,
1152 "cn", pdb_get_username(sampass));
1153 smbldap_make_mod(
1154 smbldap_get_ldap(ldap_state->smbldap_state),
1155 existing, mods,
1156 "sn", pdb_get_username(sampass));
1160 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1162 /* only update the RID if we actually need to */
1163 if (need_update(sampass, PDB_USERSID)) {
1164 struct dom_sid_buf sid_str;
1165 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1167 switch ( ldap_state->schema_ver ) {
1168 case SCHEMAVER_SAMBASAMACCOUNT:
1169 smbldap_make_mod(
1170 smbldap_get_ldap(
1171 ldap_state->smbldap_state),
1172 existing, mods,
1173 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1174 dom_sid_str_buf(user_sid, &sid_str));
1175 break;
1177 default:
1178 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1179 break;
1183 /* we don't need to store the primary group RID - so leaving it
1184 'free' to hang off the unix primary group makes life easier */
1186 if (need_update(sampass, PDB_GROUPSID)) {
1187 struct dom_sid_buf sid_str;
1188 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1190 switch ( ldap_state->schema_ver ) {
1191 case SCHEMAVER_SAMBASAMACCOUNT:
1192 smbldap_make_mod(
1193 smbldap_get_ldap(
1194 ldap_state->smbldap_state),
1195 existing, mods,
1196 get_userattr_key2string(ldap_state->schema_ver,
1197 LDAP_ATTR_PRIMARY_GROUP_SID),
1198 dom_sid_str_buf(group_sid, &sid_str));
1199 break;
1201 default:
1202 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1203 break;
1208 /* displayName, cn, and gecos should all be the same
1209 * most easily accomplished by giving them the same OID
1210 * gecos isn't set here b/c it should be handled by the
1211 * add-user script
1212 * We change displayName only and fall back to cn if
1213 * it does not exist.
1216 if (need_update(sampass, PDB_FULLNAME))
1217 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1218 existing, mods,
1219 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1220 pdb_get_fullname(sampass));
1222 if (need_update(sampass, PDB_ACCTDESC))
1223 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1224 existing, mods,
1225 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1226 pdb_get_acct_desc(sampass));
1228 if (need_update(sampass, PDB_WORKSTATIONS))
1229 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1230 existing, mods,
1231 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1232 pdb_get_workstations(sampass));
1234 if (need_update(sampass, PDB_MUNGEDDIAL))
1235 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1236 existing, mods,
1237 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1238 pdb_get_munged_dial(sampass));
1240 if (need_update(sampass, PDB_SMBHOME))
1241 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1242 existing, mods,
1243 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1244 pdb_get_homedir(sampass));
1246 if (need_update(sampass, PDB_DRIVE))
1247 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1248 existing, mods,
1249 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1250 pdb_get_dir_drive(sampass));
1252 if (need_update(sampass, PDB_LOGONSCRIPT))
1253 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1254 existing, mods,
1255 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1256 pdb_get_logon_script(sampass));
1258 if (need_update(sampass, PDB_PROFILE))
1259 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1260 existing, mods,
1261 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1262 pdb_get_profile_path(sampass));
1264 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1265 return false;
1267 if (need_update(sampass, PDB_LOGONTIME))
1268 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1269 existing, mods,
1270 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1271 SAFE_FREE(temp);
1273 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1274 return false;
1276 if (need_update(sampass, PDB_LOGOFFTIME))
1277 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1278 existing, mods,
1279 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1280 SAFE_FREE(temp);
1282 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1283 return false;
1285 if (need_update(sampass, PDB_KICKOFFTIME))
1286 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1287 existing, mods,
1288 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1289 SAFE_FREE(temp);
1291 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1292 return false;
1294 if (need_update(sampass, PDB_CANCHANGETIME))
1295 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1296 existing, mods,
1297 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1298 SAFE_FREE(temp);
1300 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1301 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1303 if (need_update(sampass, PDB_LMPASSWD)) {
1304 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1305 if (lm_pw) {
1306 char pwstr[34];
1307 pdb_sethexpwd(pwstr, lm_pw,
1308 pdb_get_acct_ctrl(sampass));
1309 smbldap_make_mod(
1310 smbldap_get_ldap(
1311 ldap_state->smbldap_state),
1312 existing, mods,
1313 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1314 pwstr);
1315 } else {
1316 smbldap_make_mod(
1317 smbldap_get_ldap(
1318 ldap_state->smbldap_state),
1319 existing, mods,
1320 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1321 NULL);
1324 if (need_update(sampass, PDB_NTPASSWD)) {
1325 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1326 if (nt_pw) {
1327 char pwstr[34];
1328 pdb_sethexpwd(pwstr, nt_pw,
1329 pdb_get_acct_ctrl(sampass));
1330 smbldap_make_mod(
1331 smbldap_get_ldap(
1332 ldap_state->smbldap_state),
1333 existing, mods,
1334 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1335 pwstr);
1336 } else {
1337 smbldap_make_mod(
1338 smbldap_get_ldap(
1339 ldap_state->smbldap_state),
1340 existing, mods,
1341 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1342 NULL);
1346 if (need_update(sampass, PDB_PWHISTORY)) {
1347 char *pwstr = NULL;
1348 uint32_t pwHistLen = 0;
1349 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1351 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1352 if (!pwstr) {
1353 return false;
1355 if (pwHistLen == 0) {
1356 /* Remove any password history from the LDAP store. */
1357 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1358 pwstr[64] = '\0';
1359 } else {
1360 int i;
1361 uint32_t currHistLen = 0;
1362 const uint8_t *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1363 if (pwhist != NULL) {
1364 /* We can only store (1024-1/64 password history entries. */
1365 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1366 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1367 /* Store the salt. */
1368 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1369 /* Followed by the md5 hash of salt + md4 hash */
1370 pdb_sethexpwd(&pwstr[(i*64)+32],
1371 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1372 DEBUG(100, ("pwstr=%s\n", pwstr));
1376 smbldap_make_mod(
1377 smbldap_get_ldap(ldap_state->smbldap_state),
1378 existing, mods,
1379 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1380 pwstr);
1381 SAFE_FREE(pwstr);
1384 if (need_update(sampass, PDB_PASSLASTSET)) {
1385 if (asprintf(&temp, "%li",
1386 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1387 return false;
1389 smbldap_make_mod(
1390 smbldap_get_ldap(ldap_state->smbldap_state),
1391 existing, mods,
1392 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1393 temp);
1394 SAFE_FREE(temp);
1398 if (need_update(sampass, PDB_HOURS)) {
1399 const uint8_t *hours = pdb_get_hours(sampass);
1400 if (hours) {
1401 char hourstr[44];
1402 pdb_sethexhours(hourstr, hours);
1403 smbldap_make_mod(
1404 smbldap_get_ldap(ldap_state->smbldap_state),
1405 existing,
1406 mods,
1407 get_userattr_key2string(ldap_state->schema_ver,
1408 LDAP_ATTR_LOGON_HOURS),
1409 hourstr);
1413 if (need_update(sampass, PDB_ACCTCTRL))
1414 smbldap_make_mod(
1415 smbldap_get_ldap(ldap_state->smbldap_state),
1416 existing, mods,
1417 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1418 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1420 /* password lockout cache:
1421 - If we are now autolocking or clearing, we write to ldap
1422 - If we are clearing, we delete the cache entry
1423 - If the count is > 0, we update the cache
1425 This even means when autolocking, we cache, just in case the
1426 update doesn't work, and we have to cache the autolock flag */
1428 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1429 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1430 uint16_t badcount = pdb_get_bad_password_count(sampass);
1431 time_t badtime = pdb_get_bad_password_time(sampass);
1432 uint32_t pol;
1433 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1435 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1436 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1438 if ((badcount >= pol) || (badcount == 0)) {
1439 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1440 (unsigned int)badcount, (unsigned int)badtime));
1441 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1442 return false;
1444 smbldap_make_mod(
1445 smbldap_get_ldap(ldap_state->smbldap_state),
1446 existing, mods,
1447 get_userattr_key2string(
1448 ldap_state->schema_ver,
1449 LDAP_ATTR_BAD_PASSWORD_COUNT),
1450 temp);
1451 SAFE_FREE(temp);
1453 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1454 return false;
1456 smbldap_make_mod(
1457 smbldap_get_ldap(ldap_state->smbldap_state),
1458 existing, mods,
1459 get_userattr_key2string(
1460 ldap_state->schema_ver,
1461 LDAP_ATTR_BAD_PASSWORD_TIME),
1462 temp);
1463 SAFE_FREE(temp);
1465 if (badcount == 0) {
1466 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1467 login_cache_delentry(sampass);
1468 } else {
1469 struct login_cache cache_entry;
1471 cache_entry.entry_timestamp = time(NULL);
1472 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1473 cache_entry.bad_password_count = badcount;
1474 cache_entry.bad_password_time = badtime;
1476 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1477 login_cache_write(sampass, &cache_entry);
1481 return True;
1484 /**********************************************************************
1485 End enumeration of the LDAP password list.
1486 *********************************************************************/
1488 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1490 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1491 if (ldap_state->result) {
1492 ldap_msgfree(ldap_state->result);
1493 ldap_state->result = NULL;
1497 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1498 const char *new_attr)
1500 int i;
1502 if (new_attr == NULL) {
1503 return;
1506 for (i=0; (*attr_list)[i] != NULL; i++) {
1510 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1511 const char *, i+2);
1512 SMB_ASSERT((*attr_list) != NULL);
1513 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1514 (*attr_list)[i+1] = NULL;
1517 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1518 const char ***attr_list)
1520 append_attr(mem_ctx, attr_list, "uidNumber");
1521 append_attr(mem_ctx, attr_list, "gidNumber");
1522 append_attr(mem_ctx, attr_list, "homeDirectory");
1523 append_attr(mem_ctx, attr_list, "loginShell");
1524 append_attr(mem_ctx, attr_list, "gecos");
1527 /**********************************************************************
1528 Get struct samu entry from LDAP by username.
1529 *********************************************************************/
1531 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1533 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1534 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1535 LDAPMessage *result = NULL;
1536 LDAPMessage *entry = NULL;
1537 int count;
1538 const char ** attr_list;
1539 int rc;
1541 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1542 append_attr(user, &attr_list,
1543 get_userattr_key2string(ldap_state->schema_ver,
1544 LDAP_ATTR_MOD_TIMESTAMP));
1545 ldapsam_add_unix_attributes(user, &attr_list);
1546 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1547 attr_list);
1548 TALLOC_FREE( attr_list );
1550 if ( rc != LDAP_SUCCESS )
1551 return NT_STATUS_NO_SUCH_USER;
1553 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1554 result);
1556 if (count < 1) {
1557 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1558 ldap_msgfree(result);
1559 return NT_STATUS_NO_SUCH_USER;
1560 } else if (count > 1) {
1561 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1562 ldap_msgfree(result);
1563 return NT_STATUS_NO_SUCH_USER;
1566 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1567 result);
1568 if (entry) {
1569 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1570 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1571 ldap_msgfree(result);
1572 return NT_STATUS_NO_SUCH_USER;
1574 pdb_set_backend_private_data(user, result, NULL,
1575 my_methods, PDB_CHANGED);
1576 smbldap_talloc_autofree_ldapmsg(user, result);
1577 ret = NT_STATUS_OK;
1578 } else {
1579 ldap_msgfree(result);
1581 return ret;
1584 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1585 const struct dom_sid *sid, LDAPMessage **result)
1587 int rc = -1;
1588 const char ** attr_list;
1590 switch ( ldap_state->schema_ver ) {
1591 case SCHEMAVER_SAMBASAMACCOUNT: {
1592 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1593 if (tmp_ctx == NULL) {
1594 return LDAP_NO_MEMORY;
1597 attr_list = get_userattr_list(tmp_ctx,
1598 ldap_state->schema_ver);
1599 append_attr(tmp_ctx, &attr_list,
1600 get_userattr_key2string(
1601 ldap_state->schema_ver,
1602 LDAP_ATTR_MOD_TIMESTAMP));
1603 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1604 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1605 result, attr_list);
1606 TALLOC_FREE(tmp_ctx);
1608 if ( rc != LDAP_SUCCESS )
1609 return rc;
1610 break;
1613 default:
1614 DEBUG(0,("Invalid schema version specified\n"));
1615 break;
1617 return rc;
1620 /**********************************************************************
1621 Get struct samu entry from LDAP by SID.
1622 *********************************************************************/
1624 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1626 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1627 LDAPMessage *result = NULL;
1628 LDAPMessage *entry = NULL;
1629 int count;
1630 int rc;
1632 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1633 sid, &result);
1634 if (rc != LDAP_SUCCESS)
1635 return NT_STATUS_NO_SUCH_USER;
1637 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1638 result);
1640 if (count < 1) {
1641 struct dom_sid_buf buf;
1642 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1643 "count=%d\n",
1644 dom_sid_str_buf(sid, &buf),
1645 count));
1646 ldap_msgfree(result);
1647 return NT_STATUS_NO_SUCH_USER;
1648 } else if (count > 1) {
1649 struct dom_sid_buf buf;
1650 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1651 "[%s]. Failing. count=%d\n",
1652 dom_sid_str_buf(sid, &buf),
1653 count));
1654 ldap_msgfree(result);
1655 return NT_STATUS_NO_SUCH_USER;
1658 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1659 result);
1660 if (!entry) {
1661 ldap_msgfree(result);
1662 return NT_STATUS_NO_SUCH_USER;
1665 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1666 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1667 ldap_msgfree(result);
1668 return NT_STATUS_NO_SUCH_USER;
1671 pdb_set_backend_private_data(user, result, NULL,
1672 my_methods, PDB_CHANGED);
1673 smbldap_talloc_autofree_ldapmsg(user, result);
1674 return NT_STATUS_OK;
1677 /********************************************************************
1678 Do the actual modification - also change a plaintext passord if
1679 it it set.
1680 **********************************************************************/
1682 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1683 struct samu *newpwd, char *dn,
1684 LDAPMod **mods, int ldap_op,
1685 bool (*need_update)(const struct samu *, enum pdb_elements))
1687 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1688 int rc;
1690 if (!newpwd || !dn) {
1691 return NT_STATUS_INVALID_PARAMETER;
1694 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1695 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1696 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1697 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1698 BerElement *ber;
1699 struct berval *bv;
1700 char *retoid = NULL;
1701 struct berval *retdata = NULL;
1702 char *utf8_password;
1703 char *utf8_dn;
1704 size_t converted_size;
1705 int ret;
1707 if (!ldap_state->is_nds_ldap) {
1709 if (!smbldap_has_extension(
1710 smbldap_get_ldap(
1711 ldap_state->smbldap_state),
1712 LDAP_EXOP_MODIFY_PASSWD)) {
1713 DEBUG(2, ("ldap password change requested, but LDAP "
1714 "server does not support it -- ignoring\n"));
1715 return NT_STATUS_OK;
1719 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1720 pdb_get_plaintext_passwd(newpwd),
1721 &converted_size))
1723 return NT_STATUS_NO_MEMORY;
1726 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1727 TALLOC_FREE(utf8_password);
1728 return NT_STATUS_NO_MEMORY;
1731 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1732 DEBUG(0,("ber_alloc_t returns NULL\n"));
1733 TALLOC_FREE(utf8_password);
1734 TALLOC_FREE(utf8_dn);
1735 return NT_STATUS_UNSUCCESSFUL;
1738 if ((ber_printf (ber, "{") < 0) ||
1739 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1740 utf8_dn) < 0)) {
1741 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1742 "value <0\n"));
1743 ber_free(ber,1);
1744 TALLOC_FREE(utf8_dn);
1745 TALLOC_FREE(utf8_password);
1746 return NT_STATUS_UNSUCCESSFUL;
1749 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1750 ret = ber_printf(ber, "ts}",
1751 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1752 utf8_password);
1753 } else {
1754 ret = ber_printf(ber, "}");
1757 if (ret < 0) {
1758 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1759 "value <0\n"));
1760 ber_free(ber,1);
1761 TALLOC_FREE(utf8_dn);
1762 TALLOC_FREE(utf8_password);
1763 return NT_STATUS_UNSUCCESSFUL;
1766 if ((rc = ber_flatten (ber, &bv))<0) {
1767 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1768 ber_free(ber,1);
1769 TALLOC_FREE(utf8_dn);
1770 TALLOC_FREE(utf8_password);
1771 return NT_STATUS_UNSUCCESSFUL;
1774 TALLOC_FREE(utf8_dn);
1775 TALLOC_FREE(utf8_password);
1776 ber_free(ber, 1);
1778 if (!ldap_state->is_nds_ldap) {
1779 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1780 LDAP_EXOP_MODIFY_PASSWD,
1781 bv, NULL, NULL, &retoid,
1782 &retdata);
1783 } else {
1784 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1785 pdb_get_plaintext_passwd(newpwd));
1787 if (rc != LDAP_SUCCESS) {
1788 char *ld_error = NULL;
1790 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1791 DEBUG(3, ("Could not set userPassword "
1792 "attribute due to an objectClass "
1793 "violation -- ignoring\n"));
1794 ber_bvfree(bv);
1795 return NT_STATUS_OK;
1798 ldap_get_option(
1799 smbldap_get_ldap(ldap_state->smbldap_state),
1800 LDAP_OPT_ERROR_STRING,
1801 &ld_error);
1802 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1803 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1804 SAFE_FREE(ld_error);
1805 ber_bvfree(bv);
1806 #if defined(LDAP_CONSTRAINT_VIOLATION)
1807 if (rc == LDAP_CONSTRAINT_VIOLATION)
1808 return NT_STATUS_PASSWORD_RESTRICTION;
1809 #endif
1810 return NT_STATUS_UNSUCCESSFUL;
1811 } else {
1812 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1813 #ifdef DEBUG_PASSWORD
1814 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1815 #endif
1816 if (retdata)
1817 ber_bvfree(retdata);
1818 if (retoid)
1819 ldap_memfree(retoid);
1821 ber_bvfree(bv);
1824 if (!mods) {
1825 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1826 /* may be password change below however */
1827 } else {
1828 switch(ldap_op) {
1829 case LDAP_MOD_ADD:
1830 if (ldap_state->is_nds_ldap) {
1831 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1832 "objectclass",
1833 "inetOrgPerson");
1834 } else {
1835 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1836 "objectclass",
1837 LDAP_OBJ_ACCOUNT);
1839 rc = smbldap_add(ldap_state->smbldap_state,
1840 dn, mods);
1841 break;
1842 case LDAP_MOD_REPLACE:
1843 rc = smbldap_modify(ldap_state->smbldap_state,
1844 dn ,mods);
1845 break;
1846 default:
1847 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1848 ldap_op));
1849 return NT_STATUS_INVALID_PARAMETER;
1852 if (rc!=LDAP_SUCCESS) {
1853 return NT_STATUS_UNSUCCESSFUL;
1857 return NT_STATUS_OK;
1860 /**********************************************************************
1861 Delete entry from LDAP for username.
1862 *********************************************************************/
1864 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1865 struct samu * sam_acct)
1867 struct ldapsam_privates *priv =
1868 (struct ldapsam_privates *)my_methods->private_data;
1869 const char *sname;
1870 int rc;
1871 LDAPMessage *msg, *entry;
1872 NTSTATUS result = NT_STATUS_NO_MEMORY;
1873 const char **attr_list;
1874 TALLOC_CTX *mem_ctx;
1876 if (!sam_acct) {
1877 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1878 return NT_STATUS_INVALID_PARAMETER;
1881 sname = pdb_get_username(sam_acct);
1883 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1884 "LDAP.\n", sname));
1886 mem_ctx = talloc_new(NULL);
1887 if (mem_ctx == NULL) {
1888 DEBUG(0, ("talloc_new failed\n"));
1889 goto done;
1892 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1893 if (attr_list == NULL) {
1894 goto done;
1897 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1899 if ((rc != LDAP_SUCCESS) ||
1900 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1901 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1902 DEBUG(5, ("Could not find user %s\n", sname));
1903 result = NT_STATUS_NO_SUCH_USER;
1904 goto done;
1907 rc = ldapsam_delete_entry(
1908 priv, mem_ctx, entry,
1909 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1910 LDAP_OBJ_SAMBASAMACCOUNT : 0,
1911 attr_list);
1913 result = (rc == LDAP_SUCCESS) ?
1914 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1916 done:
1917 TALLOC_FREE(mem_ctx);
1918 return result;
1921 /**********************************************************************
1922 Update struct samu.
1923 *********************************************************************/
1925 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1927 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1928 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1929 int rc = 0;
1930 char *dn;
1931 LDAPMessage *result = NULL;
1932 LDAPMessage *entry = NULL;
1933 LDAPMod **mods = NULL;
1934 const char **attr_list;
1936 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1937 if (!result) {
1938 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1939 if (pdb_get_username(newpwd) == NULL) {
1940 return NT_STATUS_INVALID_PARAMETER;
1942 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1943 TALLOC_FREE( attr_list );
1944 if (rc != LDAP_SUCCESS) {
1945 return NT_STATUS_UNSUCCESSFUL;
1947 pdb_set_backend_private_data(newpwd, result, NULL,
1948 my_methods, PDB_CHANGED);
1949 smbldap_talloc_autofree_ldapmsg(newpwd, result);
1952 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1953 result) == 0) {
1954 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1955 return NT_STATUS_UNSUCCESSFUL;
1958 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1959 result);
1960 dn = smbldap_talloc_dn(talloc_tos(),
1961 smbldap_get_ldap(ldap_state->smbldap_state),
1962 entry);
1963 if (!dn) {
1964 return NT_STATUS_UNSUCCESSFUL;
1967 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1969 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1970 pdb_element_is_changed)) {
1971 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1972 TALLOC_FREE(dn);
1973 if (mods != NULL)
1974 ldap_mods_free(mods,True);
1975 return NT_STATUS_UNSUCCESSFUL;
1978 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1979 && (mods == NULL)) {
1980 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1981 pdb_get_username(newpwd)));
1982 TALLOC_FREE(dn);
1983 return NT_STATUS_OK;
1986 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1988 if (mods != NULL) {
1989 ldap_mods_free(mods,True);
1992 TALLOC_FREE(dn);
1995 * We need to set the backend private data to NULL here. For example
1996 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1997 * same one, and with the explicit delete / add logic for attribute
1998 * values the second time we would use the wrong "old" value which
1999 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2000 * the update.
2001 * The existing LDAPMessage is still being auto-freed by the
2002 * destructor.
2004 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2005 PDB_CHANGED);
2007 if (!NT_STATUS_IS_OK(ret)) {
2008 return ret;
2011 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2012 pdb_get_username(newpwd)));
2013 return NT_STATUS_OK;
2016 /***************************************************************************
2017 Renames a struct samu
2018 - The "rename user script" has full responsibility for changing everything
2019 ***************************************************************************/
2021 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2022 TALLOC_CTX *tmp_ctx,
2023 uint32_t group_rid,
2024 uint32_t member_rid);
2026 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2027 TALLOC_CTX *mem_ctx,
2028 struct samu *user,
2029 struct dom_sid **pp_sids,
2030 gid_t **pp_gids,
2031 uint32_t *p_num_groups);
2033 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2034 struct samu *old_acct,
2035 const char *newname)
2037 const char *oldname;
2038 int rc;
2039 char *rename_script = NULL;
2040 fstring oldname_lower, newname_lower;
2042 if (!old_acct) {
2043 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2044 return NT_STATUS_INVALID_PARAMETER;
2046 if (!newname) {
2047 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2048 return NT_STATUS_INVALID_PARAMETER;
2051 oldname = pdb_get_username(old_acct);
2053 /* rename the posix user */
2054 rename_script = lp_rename_user_script(talloc_tos());
2055 if (rename_script == NULL) {
2056 return NT_STATUS_NO_MEMORY;
2059 if (!(*rename_script)) {
2060 TALLOC_FREE(rename_script);
2061 return NT_STATUS_ACCESS_DENIED;
2064 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2065 oldname, newname));
2067 /* We have to allow the account name to end with a '$'.
2068 Also, follow the semantics in _samr_create_user() and lower case the
2069 posix name but preserve the case in passdb */
2071 fstrcpy( oldname_lower, oldname );
2072 if (!strlower_m( oldname_lower )) {
2073 return NT_STATUS_INVALID_PARAMETER;
2075 fstrcpy( newname_lower, newname );
2076 if (!strlower_m( newname_lower )) {
2077 return NT_STATUS_INVALID_PARAMETER;
2080 rename_script = realloc_string_sub2(rename_script,
2081 "%unew",
2082 newname_lower,
2083 true,
2084 true);
2085 if (!rename_script) {
2086 return NT_STATUS_NO_MEMORY;
2088 rename_script = realloc_string_sub2(rename_script,
2089 "%uold",
2090 oldname_lower,
2091 true,
2092 true);
2093 rc = smbrun(rename_script, NULL, NULL);
2095 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2096 rename_script, rc));
2098 TALLOC_FREE(rename_script);
2100 if (rc == 0) {
2101 smb_nscd_flush_user_cache();
2104 if (rc)
2105 return NT_STATUS_UNSUCCESSFUL;
2107 return NT_STATUS_OK;
2110 /**********************************************************************
2111 Add struct samu to LDAP.
2112 *********************************************************************/
2114 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2116 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2117 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2118 int rc;
2119 LDAPMessage *result = NULL;
2120 LDAPMessage *entry = NULL;
2121 LDAPMod **mods = NULL;
2122 int ldap_op = LDAP_MOD_REPLACE;
2123 uint32_t num_result;
2124 const char **attr_list;
2125 char *escape_user = NULL;
2126 const char *username = pdb_get_username(newpwd);
2127 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2128 char *filter = NULL;
2129 char *dn = NULL;
2130 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2131 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2133 if (!ctx) {
2134 return NT_STATUS_NO_MEMORY;
2137 if (!username || !*username) {
2138 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2139 status = NT_STATUS_INVALID_PARAMETER;
2140 goto fn_exit;
2143 /* free this list after the second search or in case we exit on failure */
2144 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2146 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2148 if (rc != LDAP_SUCCESS) {
2149 goto fn_exit;
2152 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
2153 result) != 0) {
2154 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2155 username));
2156 goto fn_exit;
2158 ldap_msgfree(result);
2159 result = NULL;
2161 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2162 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2163 sid, &result);
2164 if (rc == LDAP_SUCCESS) {
2165 if (ldap_count_entries(
2166 smbldap_get_ldap(
2167 ldap_state->smbldap_state),
2168 result) != 0) {
2169 struct dom_sid_buf buf;
2170 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2171 "already in the base, with samba "
2172 "attributes\n",
2173 dom_sid_str_buf(sid, &buf)));
2174 goto fn_exit;
2176 ldap_msgfree(result);
2177 result = NULL;
2181 /* does the entry already exist but without a samba attributes?
2182 we need to return the samba attributes here */
2184 escape_user = escape_ldap_string(talloc_tos(), username);
2185 filter = talloc_strdup(attr_list, "(uid=%u)");
2186 if (!filter) {
2187 status = NT_STATUS_NO_MEMORY;
2188 goto fn_exit;
2190 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2191 TALLOC_FREE(escape_user);
2192 if (!filter) {
2193 status = NT_STATUS_NO_MEMORY;
2194 goto fn_exit;
2197 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2198 filter, attr_list, &result);
2199 if ( rc != LDAP_SUCCESS ) {
2200 goto fn_exit;
2203 num_result = ldap_count_entries(
2204 smbldap_get_ldap(ldap_state->smbldap_state), result);
2206 if (num_result > 1) {
2207 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2208 goto fn_exit;
2211 /* Check if we need to update an existing entry */
2212 if (num_result == 1) {
2213 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2214 ldap_op = LDAP_MOD_REPLACE;
2215 entry = ldap_first_entry(
2216 smbldap_get_ldap(ldap_state->smbldap_state), result);
2217 dn = smbldap_talloc_dn(
2218 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
2219 entry);
2220 if (!dn) {
2221 status = NT_STATUS_NO_MEMORY;
2222 goto fn_exit;
2225 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2227 struct dom_sid_buf buf;
2229 /* There might be a SID for this account already - say an idmap entry */
2231 filter = talloc_asprintf(ctx,
2232 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2233 get_userattr_key2string(ldap_state->schema_ver,
2234 LDAP_ATTR_USER_SID),
2235 dom_sid_str_buf(sid, &buf),
2236 LDAP_OBJ_IDMAP_ENTRY,
2237 LDAP_OBJ_SID_ENTRY);
2238 if (!filter) {
2239 status = NT_STATUS_NO_MEMORY;
2240 goto fn_exit;
2243 /* free old result before doing a new search */
2244 if (result != NULL) {
2245 ldap_msgfree(result);
2246 result = NULL;
2248 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2249 filter, attr_list, &result);
2251 if ( rc != LDAP_SUCCESS ) {
2252 goto fn_exit;
2255 num_result = ldap_count_entries(
2256 smbldap_get_ldap(ldap_state->smbldap_state), result);
2258 if (num_result > 1) {
2259 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2260 goto fn_exit;
2263 /* Check if we need to update an existing entry */
2264 if (num_result == 1) {
2266 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2267 ldap_op = LDAP_MOD_REPLACE;
2268 entry = ldap_first_entry (
2269 smbldap_get_ldap(ldap_state->smbldap_state),
2270 result);
2271 dn = smbldap_talloc_dn (
2272 ctx,
2273 smbldap_get_ldap(ldap_state->smbldap_state),
2274 entry);
2275 if (!dn) {
2276 status = NT_STATUS_NO_MEMORY;
2277 goto fn_exit;
2282 if (num_result == 0) {
2283 char *escape_username;
2284 /* Check if we need to add an entry */
2285 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2286 ldap_op = LDAP_MOD_ADD;
2288 escape_username = escape_rdn_val_string_alloc(username);
2289 if (!escape_username) {
2290 status = NT_STATUS_NO_MEMORY;
2291 goto fn_exit;
2294 if (username[strlen(username)-1] == '$') {
2295 dn = talloc_asprintf(ctx,
2296 "uid=%s,%s",
2297 escape_username,
2298 lp_ldap_machine_suffix(talloc_tos()));
2299 } else {
2300 dn = talloc_asprintf(ctx,
2301 "uid=%s,%s",
2302 escape_username,
2303 lp_ldap_user_suffix(talloc_tos()));
2306 SAFE_FREE(escape_username);
2307 if (!dn) {
2308 status = NT_STATUS_NO_MEMORY;
2309 goto fn_exit;
2313 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2314 pdb_element_is_set_or_changed)) {
2315 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2316 if (mods != NULL) {
2317 ldap_mods_free(mods, true);
2319 goto fn_exit;
2322 if (mods == NULL) {
2323 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2324 goto fn_exit;
2326 switch ( ldap_state->schema_ver ) {
2327 case SCHEMAVER_SAMBASAMACCOUNT:
2328 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2329 break;
2330 default:
2331 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2332 break;
2335 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2336 if (!NT_STATUS_IS_OK(ret)) {
2337 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2338 pdb_get_username(newpwd),dn));
2339 ldap_mods_free(mods, true);
2340 goto fn_exit;
2343 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2344 ldap_mods_free(mods, true);
2346 status = NT_STATUS_OK;
2348 fn_exit:
2350 TALLOC_FREE(ctx);
2351 if (result) {
2352 ldap_msgfree(result);
2354 return status;
2357 /**********************************************************************
2358 *********************************************************************/
2360 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2361 const char *filter,
2362 LDAPMessage ** result)
2364 int scope = LDAP_SCOPE_SUBTREE;
2365 int rc;
2366 const char **attr_list;
2368 attr_list = get_attr_list(NULL, groupmap_attr_list);
2369 rc = smbldap_search(ldap_state->smbldap_state,
2370 lp_ldap_suffix (talloc_tos()), scope,
2371 filter, attr_list, 0, result);
2372 TALLOC_FREE(attr_list);
2374 return rc;
2377 /**********************************************************************
2378 *********************************************************************/
2380 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2381 GROUP_MAP *map, LDAPMessage *entry)
2383 char *temp = NULL;
2384 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2386 if (ldap_state == NULL || map == NULL || entry == NULL ||
2387 smbldap_get_ldap(ldap_state->smbldap_state) == NULL) {
2388 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2389 TALLOC_FREE(ctx);
2390 return false;
2393 temp = smbldap_talloc_single_attribute(
2394 smbldap_get_ldap(ldap_state->smbldap_state),
2395 entry,
2396 get_attr_key2string(groupmap_attr_list,
2397 LDAP_ATTR_GIDNUMBER),
2398 ctx);
2399 if (!temp) {
2400 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2401 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2402 TALLOC_FREE(ctx);
2403 return false;
2405 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2407 map->gid = (gid_t)atol(temp);
2409 TALLOC_FREE(temp);
2410 temp = smbldap_talloc_single_attribute(
2411 smbldap_get_ldap(ldap_state->smbldap_state),
2412 entry,
2413 get_attr_key2string(groupmap_attr_list,
2414 LDAP_ATTR_GROUP_SID),
2415 ctx);
2416 if (!temp) {
2417 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2418 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2419 TALLOC_FREE(ctx);
2420 return false;
2423 if (!string_to_sid(&map->sid, temp)) {
2424 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2425 TALLOC_FREE(ctx);
2426 return false;
2429 TALLOC_FREE(temp);
2430 temp = smbldap_talloc_single_attribute(
2431 smbldap_get_ldap(ldap_state->smbldap_state),
2432 entry,
2433 get_attr_key2string(groupmap_attr_list,
2434 LDAP_ATTR_GROUP_TYPE),
2435 ctx);
2436 if (!temp) {
2437 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2438 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2439 TALLOC_FREE(ctx);
2440 return false;
2442 map->sid_name_use = (enum lsa_SidType)atol(temp);
2444 if ((map->sid_name_use < SID_NAME_USER) ||
2445 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2446 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2447 TALLOC_FREE(ctx);
2448 return false;
2451 TALLOC_FREE(temp);
2452 temp = smbldap_talloc_single_attribute(
2453 smbldap_get_ldap(ldap_state->smbldap_state),
2454 entry,
2455 get_attr_key2string(groupmap_attr_list,
2456 LDAP_ATTR_DISPLAY_NAME),
2457 ctx);
2458 if (!temp) {
2459 temp = smbldap_talloc_single_attribute(
2460 smbldap_get_ldap(ldap_state->smbldap_state),
2461 entry,
2462 get_attr_key2string(groupmap_attr_list,
2463 LDAP_ATTR_CN),
2464 ctx);
2465 if (!temp) {
2466 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2467 for gidNumber(%lu)\n",(unsigned long)map->gid));
2468 TALLOC_FREE(ctx);
2469 return false;
2472 map->nt_name = talloc_strdup(map, temp);
2473 if (!map->nt_name) {
2474 TALLOC_FREE(ctx);
2475 return false;
2478 TALLOC_FREE(temp);
2479 temp = smbldap_talloc_single_attribute(
2480 smbldap_get_ldap(ldap_state->smbldap_state),
2481 entry,
2482 get_attr_key2string(groupmap_attr_list,
2483 LDAP_ATTR_DESC),
2484 ctx);
2485 if (!temp) {
2486 temp = talloc_strdup(ctx, "");
2487 if (!temp) {
2488 TALLOC_FREE(ctx);
2489 return false;
2492 map->comment = talloc_strdup(map, temp);
2493 if (!map->comment) {
2494 TALLOC_FREE(ctx);
2495 return false;
2498 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2499 struct unixid id;
2500 id.id = map->gid;
2501 id.type = ID_TYPE_GID;
2503 idmap_cache_set_sid2unixid(&map->sid, &id);
2506 TALLOC_FREE(ctx);
2507 return true;
2510 /**********************************************************************
2511 *********************************************************************/
2513 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2514 const char *filter,
2515 GROUP_MAP *map)
2517 struct ldapsam_privates *ldap_state =
2518 (struct ldapsam_privates *)methods->private_data;
2519 LDAPMessage *result = NULL;
2520 LDAPMessage *entry = NULL;
2521 int count;
2523 if (ldapsam_search_one_group(ldap_state, filter, &result)
2524 != LDAP_SUCCESS) {
2525 return NT_STATUS_NO_SUCH_GROUP;
2528 count = ldap_count_entries(priv2ld(ldap_state), result);
2530 if (count < 1) {
2531 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2532 "%s\n", filter));
2533 ldap_msgfree(result);
2534 return NT_STATUS_NO_SUCH_GROUP;
2537 if (count > 1) {
2538 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2539 "count=%d\n", filter, count));
2540 ldap_msgfree(result);
2541 return NT_STATUS_NO_SUCH_GROUP;
2544 entry = ldap_first_entry(priv2ld(ldap_state), result);
2546 if (!entry) {
2547 ldap_msgfree(result);
2548 return NT_STATUS_UNSUCCESSFUL;
2551 if (!init_group_from_ldap(ldap_state, map, entry)) {
2552 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2553 "group filter %s\n", filter));
2554 ldap_msgfree(result);
2555 return NT_STATUS_NO_SUCH_GROUP;
2558 ldap_msgfree(result);
2559 return NT_STATUS_OK;
2562 /**********************************************************************
2563 *********************************************************************/
2565 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2566 struct dom_sid sid)
2568 char *filter = NULL;
2569 NTSTATUS status;
2570 struct dom_sid_buf tmp;
2572 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2573 LDAP_OBJ_GROUPMAP,
2574 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2575 dom_sid_str_buf(&sid, &tmp)) < 0) {
2576 return NT_STATUS_NO_MEMORY;
2579 status = ldapsam_getgroup(methods, filter, map);
2580 SAFE_FREE(filter);
2581 return status;
2584 /**********************************************************************
2585 *********************************************************************/
2587 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2588 gid_t gid)
2590 char *filter = NULL;
2591 NTSTATUS status;
2593 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2594 LDAP_OBJ_GROUPMAP,
2595 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2596 (unsigned long)gid) < 0) {
2597 return NT_STATUS_NO_MEMORY;
2600 status = ldapsam_getgroup(methods, filter, map);
2601 SAFE_FREE(filter);
2602 return status;
2605 /**********************************************************************
2606 *********************************************************************/
2608 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2609 const char *name)
2611 char *filter = NULL;
2612 char *escape_name = escape_ldap_string(talloc_tos(), name);
2613 NTSTATUS status;
2615 if (!escape_name) {
2616 return NT_STATUS_NO_MEMORY;
2619 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2620 LDAP_OBJ_GROUPMAP,
2621 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2622 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2623 escape_name) < 0) {
2624 TALLOC_FREE(escape_name);
2625 return NT_STATUS_NO_MEMORY;
2628 TALLOC_FREE(escape_name);
2629 status = ldapsam_getgroup(methods, filter, map);
2630 SAFE_FREE(filter);
2631 return status;
2634 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2635 LDAPMessage *entry,
2636 const struct dom_sid *domain_sid,
2637 uint32_t *rid)
2639 fstring str;
2640 struct dom_sid sid;
2642 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2643 str, sizeof(str)-1)) {
2644 DEBUG(10, ("Could not find sambaSID attribute\n"));
2645 return False;
2648 if (!string_to_sid(&sid, str)) {
2649 DEBUG(10, ("Could not convert string %s to sid\n", str));
2650 return False;
2653 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2654 struct dom_sid_buf buf;
2655 DEBUG(10, ("SID %s is not in expected domain %s\n",
2656 str,
2657 dom_sid_str_buf(domain_sid, &buf)));
2658 return False;
2661 if (!sid_peek_rid(&sid, rid)) {
2662 DEBUG(10, ("Could not peek into RID\n"));
2663 return False;
2666 return True;
2669 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2670 TALLOC_CTX *mem_ctx,
2671 const struct dom_sid *group,
2672 uint32_t **pp_member_rids,
2673 size_t *p_num_members)
2675 struct ldapsam_privates *ldap_state =
2676 (struct ldapsam_privates *)methods->private_data;
2677 struct smbldap_state *conn = ldap_state->smbldap_state;
2678 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2679 const char *sid_attrs[] = { "sambaSID", NULL };
2680 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2681 LDAPMessage *result = NULL;
2682 LDAPMessage *entry;
2683 char *filter;
2684 char **values = NULL;
2685 char **memberuid;
2686 char *gidstr;
2687 int rc, count;
2688 struct dom_sid_buf buf;
2690 *pp_member_rids = NULL;
2691 *p_num_members = 0;
2693 filter = talloc_asprintf(mem_ctx,
2694 "(&(objectClass=%s)"
2695 "(objectClass=%s)"
2696 "(sambaSID=%s))",
2697 LDAP_OBJ_POSIXGROUP,
2698 LDAP_OBJ_GROUPMAP,
2699 dom_sid_str_buf(group, &buf));
2700 if (filter == NULL) {
2701 ret = NT_STATUS_NO_MEMORY;
2702 goto done;
2705 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2706 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2707 &result);
2709 if (rc != LDAP_SUCCESS)
2710 goto done;
2712 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2714 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2716 if (count > 1) {
2717 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2718 dom_sid_str_buf(group, &buf)));
2719 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2720 goto done;
2723 if (count == 0) {
2724 ret = NT_STATUS_NO_SUCH_GROUP;
2725 goto done;
2728 entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2729 if (entry == NULL)
2730 goto done;
2732 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2733 if (!gidstr) {
2734 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2735 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2736 goto done;
2739 values = ldap_get_values(smbldap_get_ldap(conn), entry, "memberUid");
2741 if ((values != NULL) && (values[0] != NULL)) {
2743 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2744 if (filter == NULL) {
2745 ret = NT_STATUS_NO_MEMORY;
2746 goto done;
2749 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2750 char *escape_memberuid;
2752 escape_memberuid = escape_ldap_string(talloc_tos(),
2753 *memberuid);
2754 if (escape_memberuid == NULL) {
2755 ret = NT_STATUS_NO_MEMORY;
2756 goto done;
2759 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2760 TALLOC_FREE(escape_memberuid);
2761 if (filter == NULL) {
2762 ret = NT_STATUS_NO_MEMORY;
2763 goto done;
2767 filter = talloc_asprintf_append_buffer(filter, "))");
2768 if (filter == NULL) {
2769 ret = NT_STATUS_NO_MEMORY;
2770 goto done;
2773 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2774 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2775 &result);
2777 if (rc != LDAP_SUCCESS)
2778 goto done;
2780 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2781 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2783 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2785 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2786 entry != NULL;
2787 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2789 char *sidstr;
2790 struct dom_sid sid;
2791 uint32_t rid;
2793 sidstr = smbldap_talloc_single_attribute(
2794 smbldap_get_ldap(conn), entry, "sambaSID",
2795 mem_ctx);
2796 if (!sidstr) {
2797 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2798 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2799 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2800 goto done;
2803 if (!string_to_sid(&sid, sidstr))
2804 goto done;
2806 if (!sid_check_is_in_our_sam(&sid)) {
2807 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2808 "in our domain\n"));
2809 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2810 goto done;
2813 sid_peek_rid(&sid, &rid);
2815 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2816 p_num_members)) {
2817 ret = NT_STATUS_NO_MEMORY;
2818 goto done;
2823 filter = talloc_asprintf(mem_ctx,
2824 "(&(objectClass=%s)"
2825 "(gidNumber=%s))",
2826 LDAP_OBJ_SAMBASAMACCOUNT,
2827 gidstr);
2829 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2830 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2831 &result);
2833 if (rc != LDAP_SUCCESS)
2834 goto done;
2836 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2838 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2839 entry != NULL;
2840 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2842 uint32_t rid;
2844 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn),
2845 entry,
2846 get_global_sam_sid(),
2847 &rid)) {
2848 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2849 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2850 goto done;
2853 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2854 p_num_members)) {
2855 ret = NT_STATUS_NO_MEMORY;
2856 goto done;
2860 ret = NT_STATUS_OK;
2862 done:
2864 if (values)
2865 ldap_value_free(values);
2867 return ret;
2870 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2871 TALLOC_CTX *mem_ctx,
2872 struct samu *user,
2873 struct dom_sid **pp_sids,
2874 gid_t **pp_gids,
2875 uint32_t *p_num_groups)
2877 struct ldapsam_privates *ldap_state =
2878 (struct ldapsam_privates *)methods->private_data;
2879 struct smbldap_state *conn = ldap_state->smbldap_state;
2880 char *filter;
2881 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2882 char *escape_name;
2883 int rc, count;
2884 LDAPMessage *result = NULL;
2885 LDAPMessage *entry;
2886 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2887 uint32_t num_sids;
2888 uint32_t num_gids;
2889 char *gidstr;
2890 gid_t primary_gid = -1;
2891 int error = 0;
2893 *pp_sids = NULL;
2894 num_sids = 0;
2896 if (pdb_get_username(user) == NULL) {
2897 return NT_STATUS_INVALID_PARAMETER;
2900 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2901 if (escape_name == NULL)
2902 return NT_STATUS_NO_MEMORY;
2904 if (user->unix_pw) {
2905 primary_gid = user->unix_pw->pw_gid;
2906 } else {
2907 /* retrieve the users primary gid */
2908 filter = talloc_asprintf(mem_ctx,
2909 "(&(objectClass=%s)(uid=%s))",
2910 LDAP_OBJ_SAMBASAMACCOUNT,
2911 escape_name);
2912 if (filter == NULL) {
2913 ret = NT_STATUS_NO_MEMORY;
2914 goto done;
2917 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2918 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2920 if (rc != LDAP_SUCCESS)
2921 goto done;
2923 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2925 count = ldap_count_entries(priv2ld(ldap_state), result);
2927 switch (count) {
2928 case 0:
2929 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2930 ret = NT_STATUS_NO_SUCH_USER;
2931 goto done;
2932 case 1:
2933 entry = ldap_first_entry(priv2ld(ldap_state), result);
2935 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2936 if (!gidstr) {
2937 DEBUG (1, ("Unable to find the member's gid!\n"));
2938 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2939 goto done;
2941 primary_gid = strtoul_err(gidstr, NULL, 10, &error);
2942 if (error != 0) {
2943 DBG_ERR("Failed to convert GID\n");
2944 goto done;
2946 break;
2947 default:
2948 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2949 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2950 goto done;
2954 filter = talloc_asprintf(mem_ctx,
2955 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2956 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2957 if (filter == NULL) {
2958 ret = NT_STATUS_NO_MEMORY;
2959 goto done;
2962 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2963 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2965 if (rc != LDAP_SUCCESS)
2966 goto done;
2968 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2970 num_gids = 0;
2971 *pp_gids = NULL;
2973 num_sids = 0;
2974 *pp_sids = NULL;
2976 /* We need to add the primary group as the first gid/sid */
2978 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2979 ret = NT_STATUS_NO_MEMORY;
2980 goto done;
2983 /* This sid will be replaced later */
2985 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2986 &num_sids);
2987 if (!NT_STATUS_IS_OK(ret)) {
2988 goto done;
2991 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2992 entry != NULL;
2993 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2995 fstring str;
2996 struct dom_sid sid;
2997 gid_t gid;
2998 char *end;
3000 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3001 entry, "sambaSID",
3002 str, sizeof(str)-1))
3003 continue;
3005 if (!string_to_sid(&sid, str))
3006 goto done;
3008 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3009 entry, "gidNumber",
3010 str, sizeof(str)-1))
3011 continue;
3013 gid = strtoul_err(str, &end, 10, &error);
3015 if ((PTR_DIFF(end, str) != strlen(str)) || (error != 0)) {
3016 goto done;
3019 if (gid == primary_gid) {
3020 sid_copy(&(*pp_sids)[0], &sid);
3021 } else {
3022 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3023 &num_gids)) {
3024 ret = NT_STATUS_NO_MEMORY;
3025 goto done;
3027 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3028 &num_sids);
3029 if (!NT_STATUS_IS_OK(ret)) {
3030 goto done;
3035 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3036 DEBUG(3, ("primary group of [%s] not found\n",
3037 pdb_get_username(user)));
3038 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3039 goto done;
3042 *p_num_groups = num_sids;
3044 ret = NT_STATUS_OK;
3046 done:
3048 TALLOC_FREE(escape_name);
3049 return ret;
3052 /**********************************************************************
3053 * Augment a posixGroup object with a sambaGroupMapping domgroup
3054 *********************************************************************/
3056 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3057 struct ldapsam_privates *ldap_state,
3058 GROUP_MAP *map)
3060 const char *filter, *dn;
3061 LDAPMessage *msg, *entry;
3062 LDAPMod **mods;
3063 struct dom_sid_buf buf;
3064 int rc;
3066 filter = talloc_asprintf(mem_ctx,
3067 "(&(objectClass=%s)(gidNumber=%u))",
3068 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3069 if (filter == NULL) {
3070 return NT_STATUS_NO_MEMORY;
3073 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3074 get_attr_list(mem_ctx, groupmap_attr_list),
3075 &msg);
3076 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3078 if ((rc != LDAP_SUCCESS) ||
3079 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3080 msg) != 1) ||
3081 ((entry = ldap_first_entry(
3082 smbldap_get_ldap(ldap_state->smbldap_state),
3083 msg)) == NULL)) {
3084 return NT_STATUS_NO_SUCH_GROUP;
3087 dn = smbldap_talloc_dn(mem_ctx,
3088 smbldap_get_ldap(ldap_state->smbldap_state),
3089 entry);
3090 if (dn == NULL) {
3091 return NT_STATUS_NO_MEMORY;
3094 mods = NULL;
3095 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3096 LDAP_OBJ_GROUPMAP);
3097 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3098 &mods, "sambaSid",
3099 dom_sid_str_buf(&map->sid, &buf));
3100 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3101 &mods, "sambaGroupType",
3102 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3103 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3104 &mods, "displayName",
3105 map->nt_name);
3106 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3107 &mods, "description",
3108 map->comment);
3109 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3111 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3112 if (rc != LDAP_SUCCESS) {
3113 return NT_STATUS_ACCESS_DENIED;
3116 return NT_STATUS_OK;
3119 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3120 GROUP_MAP *map)
3122 struct ldapsam_privates *ldap_state =
3123 (struct ldapsam_privates *)methods->private_data;
3124 LDAPMessage *msg = NULL;
3125 LDAPMod **mods = NULL;
3126 const char *attrs[] = { NULL };
3127 char *filter;
3129 char *dn;
3130 TALLOC_CTX *mem_ctx;
3131 NTSTATUS result;
3133 struct dom_sid sid;
3134 struct dom_sid_buf buf;
3135 struct unixid id;
3137 int rc;
3139 mem_ctx = talloc_new(NULL);
3140 if (mem_ctx == NULL) {
3141 DEBUG(0, ("talloc_new failed\n"));
3142 return NT_STATUS_NO_MEMORY;
3145 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3146 dom_sid_str_buf(&map->sid, &buf));
3147 if (filter == NULL) {
3148 result = NT_STATUS_NO_MEMORY;
3149 goto done;
3152 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3153 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3154 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3156 if ((rc == LDAP_SUCCESS) &&
3157 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3158 msg) > 0)) {
3160 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3161 "group mapping entry\n",
3162 dom_sid_str_buf(&map->sid, &buf)));
3163 result = NT_STATUS_GROUP_EXISTS;
3164 goto done;
3167 switch (map->sid_name_use) {
3169 case SID_NAME_DOM_GRP:
3170 /* To map a domain group we need to have a posix group
3171 to attach to. */
3172 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3173 goto done;
3174 break;
3176 case SID_NAME_ALIAS:
3177 if (!sid_check_is_in_our_sam(&map->sid)
3178 && !sid_check_is_in_builtin(&map->sid) )
3180 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3181 dom_sid_str_buf(&map->sid, &buf)));
3182 result = NT_STATUS_INVALID_PARAMETER;
3183 goto done;
3185 break;
3187 default:
3188 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3189 sid_type_lookup(map->sid_name_use)));
3190 result = NT_STATUS_INVALID_PARAMETER;
3191 goto done;
3194 /* Domain groups have been mapped in a separate routine, we have to
3195 * create an alias now */
3197 if (map->gid == -1) {
3198 DEBUG(10, ("Refusing to map gid==-1\n"));
3199 result = NT_STATUS_INVALID_PARAMETER;
3200 goto done;
3203 id.id = map->gid;
3204 id.type = ID_TYPE_GID;
3206 if (pdb_id_to_sid(&id, &sid)) {
3207 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3208 "add\n",
3209 (unsigned int)map->gid,
3210 dom_sid_str_buf(&sid, &buf)));
3211 result = NT_STATUS_GROUP_EXISTS;
3212 goto done;
3215 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3216 * the best we can get out of LDAP. */
3218 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3219 dom_sid_str_buf(&map->sid, &buf),
3220 lp_ldap_group_suffix(talloc_tos()));
3221 if (dn == NULL) {
3222 result = NT_STATUS_NO_MEMORY;
3223 goto done;
3226 mods = NULL;
3228 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3229 &mods, "objectClass", LDAP_OBJ_SID_ENTRY);
3230 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3231 &mods, "objectClass", LDAP_OBJ_GROUPMAP);
3232 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3233 &mods, "sambaSid",
3234 dom_sid_str_buf(&map->sid, &buf));
3235 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3236 &mods, "sambaGroupType",
3237 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3238 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3239 &mods, "displayName",
3240 map->nt_name);
3241 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3242 &mods, "description",
3243 map->comment);
3244 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3245 &mods, "gidNumber",
3246 talloc_asprintf(mem_ctx, "%u",
3247 (unsigned int)map->gid));
3248 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3250 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3252 result = (rc == LDAP_SUCCESS) ?
3253 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3255 done:
3256 TALLOC_FREE(mem_ctx);
3257 return result;
3260 /**********************************************************************
3261 * Update a group mapping entry. We're quite strict about what can be changed:
3262 * Only the description and displayname may be changed. It simply does not
3263 * make any sense to change the SID, gid or the type in a mapping.
3264 *********************************************************************/
3266 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3267 GROUP_MAP *map)
3269 struct ldapsam_privates *ldap_state =
3270 (struct ldapsam_privates *)methods->private_data;
3271 int rc;
3272 const char *filter, *dn;
3273 LDAPMessage *msg = NULL;
3274 LDAPMessage *entry = NULL;
3275 LDAPMod **mods = NULL;
3276 TALLOC_CTX *mem_ctx;
3277 NTSTATUS result;
3278 struct dom_sid_buf buf;
3280 mem_ctx = talloc_new(NULL);
3281 if (mem_ctx == NULL) {
3282 DEBUG(0, ("talloc_new failed\n"));
3283 return NT_STATUS_NO_MEMORY;
3286 /* Make 100% sure that sid, gid and type are not changed by looking up
3287 * exactly the values we're given in LDAP. */
3289 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3290 "(sambaSid=%s)(gidNumber=%u)"
3291 "(sambaGroupType=%d))",
3292 LDAP_OBJ_GROUPMAP,
3293 dom_sid_str_buf(&map->sid, &buf),
3294 (unsigned int)map->gid, map->sid_name_use);
3295 if (filter == NULL) {
3296 result = NT_STATUS_NO_MEMORY;
3297 goto done;
3300 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3301 get_attr_list(mem_ctx, groupmap_attr_list),
3302 &msg);
3303 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3305 if ((rc != LDAP_SUCCESS) ||
3306 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3307 msg) != 1) ||
3308 ((entry = ldap_first_entry(
3309 smbldap_get_ldap(ldap_state->smbldap_state),
3310 msg)) == NULL)) {
3311 result = NT_STATUS_NO_SUCH_GROUP;
3312 goto done;
3315 dn = smbldap_talloc_dn(
3316 mem_ctx, smbldap_get_ldap(ldap_state->smbldap_state), entry);
3318 if (dn == NULL) {
3319 result = NT_STATUS_NO_MEMORY;
3320 goto done;
3323 mods = NULL;
3324 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3325 &mods, "displayName", map->nt_name);
3326 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3327 &mods, "description", map->comment);
3328 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3330 if (mods == NULL) {
3331 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3332 "nothing to do\n"));
3333 result = NT_STATUS_OK;
3334 goto done;
3337 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3339 if (rc != LDAP_SUCCESS) {
3340 result = NT_STATUS_ACCESS_DENIED;
3341 goto done;
3344 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3345 "group %lu in LDAP\n", (unsigned long)map->gid));
3347 result = NT_STATUS_OK;
3349 done:
3350 TALLOC_FREE(mem_ctx);
3351 return result;
3354 /**********************************************************************
3355 *********************************************************************/
3357 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3358 struct dom_sid sid)
3360 struct ldapsam_privates *priv =
3361 (struct ldapsam_privates *)methods->private_data;
3362 LDAPMessage *msg, *entry;
3363 int rc;
3364 NTSTATUS result;
3365 TALLOC_CTX *mem_ctx;
3366 char *filter;
3367 struct dom_sid_buf buf;
3369 mem_ctx = talloc_new(NULL);
3370 if (mem_ctx == NULL) {
3371 DEBUG(0, ("talloc_new failed\n"));
3372 return NT_STATUS_NO_MEMORY;
3375 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3376 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3377 dom_sid_str_buf(&sid, &buf));
3378 if (filter == NULL) {
3379 result = NT_STATUS_NO_MEMORY;
3380 goto done;
3382 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3383 get_attr_list(mem_ctx, groupmap_attr_list),
3384 &msg);
3385 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3387 if ((rc != LDAP_SUCCESS) ||
3388 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3389 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3390 result = NT_STATUS_NO_SUCH_GROUP;
3391 goto done;
3394 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3395 get_attr_list(mem_ctx,
3396 groupmap_attr_list_to_delete));
3398 if ((rc == LDAP_NAMING_VIOLATION) ||
3399 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3400 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3401 const char *attrs[] = { "sambaGroupType", "description",
3402 "displayName", "sambaSIDList",
3403 NULL };
3405 /* Second try. Don't delete the sambaSID attribute, this is
3406 for "old" entries that are tacked on a winbind
3407 sambaIdmapEntry. */
3409 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3410 LDAP_OBJ_GROUPMAP, attrs);
3413 if ((rc == LDAP_NAMING_VIOLATION) ||
3414 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3415 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3416 const char *attrs[] = { "sambaGroupType", "description",
3417 "displayName", "sambaSIDList",
3418 "gidNumber", NULL };
3420 /* Third try. This is a post-3.0.21 alias (containing only
3421 * sambaSidEntry and sambaGroupMapping classes), we also have
3422 * to delete the gidNumber attribute, only the sambaSidEntry
3423 * remains */
3425 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3426 LDAP_OBJ_GROUPMAP, attrs);
3429 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3431 done:
3432 TALLOC_FREE(mem_ctx);
3433 return result;
3436 /**********************************************************************
3437 *********************************************************************/
3439 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3440 bool update)
3442 struct ldapsam_privates *ldap_state =
3443 (struct ldapsam_privates *)my_methods->private_data;
3444 char *filter = NULL;
3445 int rc;
3446 const char **attr_list;
3448 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3449 if (!filter) {
3450 return NT_STATUS_NO_MEMORY;
3452 attr_list = get_attr_list( NULL, groupmap_attr_list );
3453 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3454 LDAP_SCOPE_SUBTREE, filter,
3455 attr_list, 0, &ldap_state->result);
3456 TALLOC_FREE(attr_list);
3458 if (rc != LDAP_SUCCESS) {
3459 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3460 ldap_err2string(rc)));
3461 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3462 lp_ldap_suffix(talloc_tos()), filter));
3463 ldap_msgfree(ldap_state->result);
3464 ldap_state->result = NULL;
3465 TALLOC_FREE(filter);
3466 return NT_STATUS_UNSUCCESSFUL;
3469 TALLOC_FREE(filter);
3471 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3472 ldap_count_entries(
3473 smbldap_get_ldap(ldap_state->smbldap_state),
3474 ldap_state->result)));
3476 ldap_state->entry =
3477 ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3478 ldap_state->result);
3479 ldap_state->index = 0;
3481 return NT_STATUS_OK;
3484 /**********************************************************************
3485 *********************************************************************/
3487 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3489 ldapsam_endsampwent(my_methods);
3492 /**********************************************************************
3493 *********************************************************************/
3495 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3496 GROUP_MAP *map)
3498 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3499 struct ldapsam_privates *ldap_state =
3500 (struct ldapsam_privates *)my_methods->private_data;
3501 bool bret = False;
3503 while (!bret) {
3504 if (!ldap_state->entry)
3505 return ret;
3507 ldap_state->index++;
3508 bret = init_group_from_ldap(ldap_state, map,
3509 ldap_state->entry);
3511 ldap_state->entry = ldap_next_entry(
3512 smbldap_get_ldap(ldap_state->smbldap_state),
3513 ldap_state->entry);
3516 return NT_STATUS_OK;
3519 /**********************************************************************
3520 *********************************************************************/
3522 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3523 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3524 GROUP_MAP ***pp_rmap,
3525 size_t *p_num_entries,
3526 bool unix_only)
3528 GROUP_MAP *map = NULL;
3529 size_t entries = 0;
3531 *p_num_entries = 0;
3532 *pp_rmap = NULL;
3534 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3535 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3536 "passdb\n"));
3537 return NT_STATUS_ACCESS_DENIED;
3540 while (true) {
3542 map = talloc_zero(NULL, GROUP_MAP);
3543 if (!map) {
3544 return NT_STATUS_NO_MEMORY;
3547 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3548 TALLOC_FREE(map);
3549 break;
3552 if (sid_name_use != SID_NAME_UNKNOWN &&
3553 sid_name_use != map->sid_name_use) {
3554 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3555 "not of the requested type\n",
3556 map->nt_name));
3557 continue;
3559 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3560 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3561 "non mapped\n", map->nt_name));
3562 continue;
3565 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3566 GROUP_MAP *, entries + 1);
3567 if (!(*pp_rmap)) {
3568 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3569 "enlarge group map!\n"));
3570 return NT_STATUS_UNSUCCESSFUL;
3573 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3575 entries += 1;
3578 ldapsam_endsamgrent(methods);
3580 *p_num_entries = entries;
3582 return NT_STATUS_OK;
3585 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3586 const struct dom_sid *alias,
3587 const struct dom_sid *member,
3588 int modop)
3590 struct ldapsam_privates *ldap_state =
3591 (struct ldapsam_privates *)methods->private_data;
3592 char *dn = NULL;
3593 LDAPMessage *result = NULL;
3594 LDAPMessage *entry = NULL;
3595 int count;
3596 LDAPMod **mods = NULL;
3597 int rc;
3598 enum lsa_SidType type = SID_NAME_USE_NONE;
3599 struct dom_sid_buf tmp;
3601 char *filter = NULL;
3603 if (sid_check_is_in_builtin(alias)) {
3604 type = SID_NAME_ALIAS;
3607 if (sid_check_is_in_our_sam(alias)) {
3608 type = SID_NAME_ALIAS;
3611 if (type == SID_NAME_USE_NONE) {
3612 struct dom_sid_buf buf;
3613 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3614 dom_sid_str_buf(alias, &buf)));
3615 return NT_STATUS_NO_SUCH_ALIAS;
3618 if (asprintf(&filter,
3619 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3620 LDAP_OBJ_GROUPMAP,
3621 dom_sid_str_buf(alias, &tmp),
3622 type) < 0) {
3623 return NT_STATUS_NO_MEMORY;
3626 if (ldapsam_search_one_group(ldap_state, filter,
3627 &result) != LDAP_SUCCESS) {
3628 SAFE_FREE(filter);
3629 return NT_STATUS_NO_SUCH_ALIAS;
3632 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3633 result);
3635 if (count < 1) {
3636 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3637 ldap_msgfree(result);
3638 SAFE_FREE(filter);
3639 return NT_STATUS_NO_SUCH_ALIAS;
3642 if (count > 1) {
3643 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3644 "filter %s: count=%d\n", filter, count));
3645 ldap_msgfree(result);
3646 SAFE_FREE(filter);
3647 return NT_STATUS_NO_SUCH_ALIAS;
3650 SAFE_FREE(filter);
3652 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3653 result);
3655 if (!entry) {
3656 ldap_msgfree(result);
3657 return NT_STATUS_UNSUCCESSFUL;
3660 dn = smbldap_talloc_dn(talloc_tos(),
3661 smbldap_get_ldap(ldap_state->smbldap_state),
3662 entry);
3663 if (!dn) {
3664 ldap_msgfree(result);
3665 return NT_STATUS_UNSUCCESSFUL;
3668 smbldap_set_mod(&mods, modop,
3669 get_attr_key2string(groupmap_attr_list,
3670 LDAP_ATTR_SID_LIST),
3671 dom_sid_str_buf(member, &tmp));
3673 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3675 ldap_mods_free(mods, True);
3676 ldap_msgfree(result);
3677 TALLOC_FREE(dn);
3679 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3680 return NT_STATUS_MEMBER_IN_ALIAS;
3683 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3684 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3687 if (rc != LDAP_SUCCESS) {
3688 return NT_STATUS_UNSUCCESSFUL;
3691 return NT_STATUS_OK;
3694 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3695 const struct dom_sid *alias,
3696 const struct dom_sid *member)
3698 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3701 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3702 const struct dom_sid *alias,
3703 const struct dom_sid *member)
3705 return ldapsam_modify_aliasmem(methods, alias, member,
3706 LDAP_MOD_DELETE);
3709 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3710 const struct dom_sid *alias,
3711 TALLOC_CTX *mem_ctx,
3712 struct dom_sid **pp_members,
3713 size_t *p_num_members)
3715 struct ldapsam_privates *ldap_state =
3716 (struct ldapsam_privates *)methods->private_data;
3717 LDAPMessage *result = NULL;
3718 LDAPMessage *entry = NULL;
3719 int count;
3720 char **values = NULL;
3721 int i;
3722 char *filter = NULL;
3723 uint32_t num_members = 0;
3724 enum lsa_SidType type = SID_NAME_USE_NONE;
3725 struct dom_sid_buf tmp;
3727 *pp_members = NULL;
3728 *p_num_members = 0;
3730 if (sid_check_is_in_builtin(alias)) {
3731 type = SID_NAME_ALIAS;
3734 if (sid_check_is_in_our_sam(alias)) {
3735 type = SID_NAME_ALIAS;
3738 if (type == SID_NAME_USE_NONE) {
3739 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3740 dom_sid_str_buf(alias, &tmp)));
3741 return NT_STATUS_NO_SUCH_ALIAS;
3744 if (asprintf(&filter,
3745 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3746 LDAP_OBJ_GROUPMAP,
3747 dom_sid_str_buf(alias, &tmp),
3748 type) < 0) {
3749 return NT_STATUS_NO_MEMORY;
3752 if (ldapsam_search_one_group(ldap_state, filter,
3753 &result) != LDAP_SUCCESS) {
3754 SAFE_FREE(filter);
3755 return NT_STATUS_NO_SUCH_ALIAS;
3758 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3759 result);
3761 if (count < 1) {
3762 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3763 ldap_msgfree(result);
3764 SAFE_FREE(filter);
3765 return NT_STATUS_NO_SUCH_ALIAS;
3768 if (count > 1) {
3769 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3770 "filter %s: count=%d\n", filter, count));
3771 ldap_msgfree(result);
3772 SAFE_FREE(filter);
3773 return NT_STATUS_NO_SUCH_ALIAS;
3776 SAFE_FREE(filter);
3778 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3779 result);
3781 if (!entry) {
3782 ldap_msgfree(result);
3783 return NT_STATUS_UNSUCCESSFUL;
3786 values = ldap_get_values(smbldap_get_ldap(ldap_state->smbldap_state),
3787 entry,
3788 get_attr_key2string(groupmap_attr_list,
3789 LDAP_ATTR_SID_LIST));
3791 if (values == NULL) {
3792 ldap_msgfree(result);
3793 return NT_STATUS_OK;
3796 count = ldap_count_values(values);
3798 for (i=0; i<count; i++) {
3799 struct dom_sid member;
3800 NTSTATUS status;
3802 if (!string_to_sid(&member, values[i]))
3803 continue;
3805 status = add_sid_to_array(mem_ctx, &member, pp_members,
3806 &num_members);
3807 if (!NT_STATUS_IS_OK(status)) {
3808 ldap_value_free(values);
3809 ldap_msgfree(result);
3810 return status;
3814 *p_num_members = num_members;
3815 ldap_value_free(values);
3816 ldap_msgfree(result);
3818 return NT_STATUS_OK;
3821 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3822 TALLOC_CTX *mem_ctx,
3823 const struct dom_sid *domain_sid,
3824 const struct dom_sid *members,
3825 size_t num_members,
3826 uint32_t **pp_alias_rids,
3827 size_t *p_num_alias_rids)
3829 struct ldapsam_privates *ldap_state =
3830 (struct ldapsam_privates *)methods->private_data;
3831 LDAP *ldap_struct;
3833 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3835 LDAPMessage *result = NULL;
3836 LDAPMessage *entry = NULL;
3837 int i;
3838 int rc;
3839 char *filter;
3840 enum lsa_SidType type = SID_NAME_USE_NONE;
3841 bool is_builtin = false;
3842 bool sid_added = false;
3844 *pp_alias_rids = NULL;
3845 *p_num_alias_rids = 0;
3847 if (sid_check_is_builtin(domain_sid)) {
3848 is_builtin = true;
3849 type = SID_NAME_ALIAS;
3852 if (sid_check_is_our_sam(domain_sid)) {
3853 type = SID_NAME_ALIAS;
3856 if (type == SID_NAME_USE_NONE) {
3857 struct dom_sid_buf buf;
3858 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3859 dom_sid_str_buf(domain_sid, &buf)));
3860 return NT_STATUS_UNSUCCESSFUL;
3863 if (num_members == 0) {
3864 return NT_STATUS_OK;
3867 filter = talloc_asprintf(mem_ctx,
3868 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3869 LDAP_OBJ_GROUPMAP, type);
3871 for (i=0; i<num_members; i++) {
3872 struct dom_sid_buf buf;
3873 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3874 filter,
3875 dom_sid_str_buf(&members[i], &buf));
3878 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3880 if (filter == NULL) {
3881 return NT_STATUS_NO_MEMORY;
3884 if (is_builtin &&
3885 ldap_state->search_cache.filter &&
3886 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3887 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3888 result = ldap_state->search_cache.result;
3889 ldap_state->search_cache.result = NULL;
3890 } else {
3891 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3892 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3893 if (rc != LDAP_SUCCESS) {
3894 return NT_STATUS_UNSUCCESSFUL;
3896 smbldap_talloc_autofree_ldapmsg(filter, result);
3899 ldap_struct = smbldap_get_ldap(ldap_state->smbldap_state);
3901 for (entry = ldap_first_entry(ldap_struct, result);
3902 entry != NULL;
3903 entry = ldap_next_entry(ldap_struct, entry))
3905 fstring sid_str;
3906 struct dom_sid sid;
3907 uint32_t rid;
3909 if (!smbldap_get_single_attribute(ldap_struct, entry,
3910 LDAP_ATTRIBUTE_SID,
3911 sid_str,
3912 sizeof(sid_str)-1))
3913 continue;
3915 if (!string_to_sid(&sid, sid_str))
3916 continue;
3918 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3919 continue;
3921 sid_added = true;
3923 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3924 p_num_alias_rids)) {
3925 return NT_STATUS_NO_MEMORY;
3929 if (!is_builtin && !sid_added) {
3930 TALLOC_FREE(ldap_state->search_cache.filter);
3932 * Note: result is a talloc child of filter because of the
3933 * smbldap_talloc_autofree_ldapmsg() usage
3935 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3936 ldap_state->search_cache.result = result;
3939 return NT_STATUS_OK;
3942 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3943 enum pdb_policy_type type,
3944 uint32_t value)
3946 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3947 int rc;
3948 LDAPMod **mods = NULL;
3949 fstring value_string;
3950 const char *policy_attr = NULL;
3952 struct ldapsam_privates *ldap_state =
3953 (struct ldapsam_privates *)methods->private_data;
3955 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3957 if (!ldap_state->domain_dn) {
3958 return NT_STATUS_INVALID_PARAMETER;
3961 policy_attr = get_account_policy_attr(type);
3962 if (policy_attr == NULL) {
3963 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3964 "policy\n"));
3965 return ntstatus;
3968 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3970 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3972 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3973 mods);
3975 ldap_mods_free(mods, True);
3977 if (rc != LDAP_SUCCESS) {
3978 return ntstatus;
3981 if (!cache_account_policy_set(type, value)) {
3982 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3983 "update local tdb cache\n"));
3984 return ntstatus;
3987 return NT_STATUS_OK;
3990 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3991 enum pdb_policy_type type,
3992 uint32_t value)
3994 return ldapsam_set_account_policy_in_ldap(methods, type,
3995 value);
3998 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3999 enum pdb_policy_type type,
4000 uint32_t *value)
4002 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4003 LDAPMessage *result = NULL;
4004 LDAPMessage *entry = NULL;
4005 int count;
4006 int rc;
4007 char **vals = NULL;
4008 char *filter;
4009 const char *policy_attr = NULL;
4011 struct ldapsam_privates *ldap_state =
4012 (struct ldapsam_privates *)methods->private_data;
4014 const char *attrs[2];
4016 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4018 if (!ldap_state->domain_dn) {
4019 return NT_STATUS_INVALID_PARAMETER;
4022 policy_attr = get_account_policy_attr(type);
4023 if (!policy_attr) {
4024 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4025 "policy index: %d\n", type));
4026 return ntstatus;
4029 attrs[0] = policy_attr;
4030 attrs[1] = NULL;
4032 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
4033 if (filter == NULL) {
4034 return NT_STATUS_NO_MEMORY;
4036 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
4037 LDAP_SCOPE_BASE, filter, attrs, 0,
4038 &result);
4039 TALLOC_FREE(filter);
4040 if (rc != LDAP_SUCCESS) {
4041 return ntstatus;
4044 count = ldap_count_entries(priv2ld(ldap_state), result);
4045 if (count < 1) {
4046 goto out;
4049 entry = ldap_first_entry(priv2ld(ldap_state), result);
4050 if (entry == NULL) {
4051 goto out;
4054 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4055 if (vals == NULL) {
4056 goto out;
4059 *value = (uint32_t)atol(vals[0]);
4061 ntstatus = NT_STATUS_OK;
4063 out:
4064 if (vals)
4065 ldap_value_free(vals);
4066 ldap_msgfree(result);
4068 return ntstatus;
4071 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4073 - if user hasn't decided to use account policies inside LDAP just reuse the
4074 old tdb values
4076 - if there is a valid cache entry, return that
4077 - if there is an LDAP entry, update cache and return
4078 - otherwise set to default, update cache and return
4080 Guenther
4082 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4083 enum pdb_policy_type type,
4084 uint32_t *value)
4086 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4088 if (cache_account_policy_get(type, value)) {
4089 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4090 "cache\n"));
4091 return NT_STATUS_OK;
4094 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4095 value);
4096 if (NT_STATUS_IS_OK(ntstatus)) {
4097 goto update_cache;
4100 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4101 "ldap\n"));
4103 #if 0
4104 /* should we automagically migrate old tdb value here ? */
4105 if (account_policy_get(type, value))
4106 goto update_ldap;
4108 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4109 "default\n", type));
4110 #endif
4112 if (!account_policy_get_default(type, value)) {
4113 return ntstatus;
4116 /* update_ldap: */
4118 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4119 if (!NT_STATUS_IS_OK(ntstatus)) {
4120 return ntstatus;
4123 update_cache:
4125 if (!cache_account_policy_set(type, *value)) {
4126 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4127 "tdb as a cache\n"));
4128 return NT_STATUS_UNSUCCESSFUL;
4131 return NT_STATUS_OK;
4134 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4135 const struct dom_sid *domain_sid,
4136 int num_rids,
4137 uint32_t *rids,
4138 const char **names,
4139 enum lsa_SidType *attrs)
4141 struct ldapsam_privates *ldap_state =
4142 (struct ldapsam_privates *)methods->private_data;
4143 LDAPMessage *msg = NULL;
4144 LDAPMessage *entry;
4145 char *allsids = NULL;
4146 size_t i, num_mapped;
4147 int rc;
4148 NTSTATUS result = NT_STATUS_NO_MEMORY;
4149 TALLOC_CTX *mem_ctx;
4150 LDAP *ld;
4151 bool is_builtin;
4153 mem_ctx = talloc_new(NULL);
4154 if (mem_ctx == NULL) {
4155 DEBUG(0, ("talloc_new failed\n"));
4156 goto done;
4159 if (!sid_check_is_builtin(domain_sid) &&
4160 !sid_check_is_our_sam(domain_sid)) {
4161 result = NT_STATUS_INVALID_PARAMETER;
4162 goto done;
4165 if (num_rids == 0) {
4166 result = NT_STATUS_NONE_MAPPED;
4167 goto done;
4170 for (i=0; i<num_rids; i++)
4171 attrs[i] = SID_NAME_UNKNOWN;
4173 allsids = talloc_strdup(mem_ctx, "");
4174 if (allsids == NULL) {
4175 goto done;
4178 for (i=0; i<num_rids; i++) {
4179 struct dom_sid sid;
4180 struct dom_sid_buf buf;
4181 sid_compose(&sid, domain_sid, rids[i]);
4182 allsids = talloc_asprintf_append_buffer(
4183 allsids,
4184 "(sambaSid=%s)",
4185 dom_sid_str_buf(&sid, &buf));
4186 if (allsids == NULL) {
4187 goto done;
4191 /* First look for users */
4194 char *filter;
4195 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4197 filter = talloc_asprintf(
4198 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4199 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4201 if (filter == NULL) {
4202 goto done;
4205 rc = smbldap_search(ldap_state->smbldap_state,
4206 lp_ldap_user_suffix(talloc_tos()),
4207 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4208 &msg);
4209 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4212 if (rc != LDAP_SUCCESS)
4213 goto done;
4215 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4216 num_mapped = 0;
4218 for (entry = ldap_first_entry(ld, msg);
4219 entry != NULL;
4220 entry = ldap_next_entry(ld, entry)) {
4221 uint32_t rid;
4222 int rid_index;
4223 const char *name;
4225 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4226 &rid)) {
4227 DEBUG(2, ("Could not find sid from ldap entry\n"));
4228 continue;
4231 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4232 names);
4233 if (name == NULL) {
4234 DEBUG(2, ("Could not retrieve uid attribute\n"));
4235 continue;
4238 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4239 if (rid == rids[rid_index])
4240 break;
4243 if (rid_index == num_rids) {
4244 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4245 continue;
4248 attrs[rid_index] = SID_NAME_USER;
4249 names[rid_index] = name;
4250 num_mapped += 1;
4253 if (num_mapped == num_rids) {
4254 /* No need to look for groups anymore -- we're done */
4255 result = NT_STATUS_OK;
4256 goto done;
4259 /* Same game for groups */
4262 char *filter;
4263 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4264 "sambaGroupType", NULL };
4266 filter = talloc_asprintf(
4267 mem_ctx, "(&(objectClass=%s)(|%s))",
4268 LDAP_OBJ_GROUPMAP, allsids);
4269 if (filter == NULL) {
4270 goto done;
4273 rc = smbldap_search(ldap_state->smbldap_state,
4274 lp_ldap_suffix(talloc_tos()),
4275 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4276 &msg);
4277 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4280 if (rc != LDAP_SUCCESS)
4281 goto done;
4283 /* ldap_struct might have changed due to a reconnect */
4285 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4287 /* For consistency checks, we already checked we're only domain or builtin */
4289 is_builtin = sid_check_is_builtin(domain_sid);
4291 for (entry = ldap_first_entry(ld, msg);
4292 entry != NULL;
4293 entry = ldap_next_entry(ld, entry))
4295 uint32_t rid;
4296 int rid_index;
4297 const char *attr;
4298 enum lsa_SidType type;
4299 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4301 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4302 mem_ctx);
4303 if (attr == NULL) {
4304 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4305 dn));
4306 continue;
4309 type = (enum lsa_SidType)atol(attr);
4311 /* Consistency checks */
4312 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4313 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4314 (type != SID_NAME_DOM_GRP)))) {
4315 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4318 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4319 &rid)) {
4320 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4321 continue;
4324 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4326 if (attr == NULL) {
4327 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4328 dn));
4329 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4332 if (attr == NULL) {
4333 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4334 dn));
4335 continue;
4338 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4339 if (rid == rids[rid_index])
4340 break;
4343 if (rid_index == num_rids) {
4344 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4345 continue;
4348 attrs[rid_index] = type;
4349 names[rid_index] = attr;
4350 num_mapped += 1;
4353 result = NT_STATUS_NONE_MAPPED;
4355 if (num_mapped > 0)
4356 result = (num_mapped == num_rids) ?
4357 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4358 done:
4359 TALLOC_FREE(mem_ctx);
4360 return result;
4363 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4365 char *filter = NULL;
4366 char *escaped = NULL;
4367 char *result = NULL;
4369 if (asprintf(&filter, "(&%s(objectclass=%s))",
4370 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4371 goto done;
4374 escaped = escape_ldap_string(talloc_tos(), username);
4375 if (escaped == NULL) goto done;
4377 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4379 done:
4380 SAFE_FREE(filter);
4381 TALLOC_FREE(escaped);
4383 return result;
4386 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4388 int i, num = 0;
4389 va_list ap;
4390 const char **result;
4392 va_start(ap, mem_ctx);
4393 while (va_arg(ap, const char *) != NULL)
4394 num += 1;
4395 va_end(ap);
4397 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4398 return NULL;
4401 va_start(ap, mem_ctx);
4402 for (i=0; i<num; i++) {
4403 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4404 if (result[i] == NULL) {
4405 talloc_free(result);
4406 va_end(ap);
4407 return NULL;
4410 va_end(ap);
4412 result[num] = NULL;
4413 return result;
4416 struct ldap_search_state {
4417 struct smbldap_state *connection;
4419 uint32_t acct_flags;
4420 uint16_t group_type;
4422 const char *base;
4423 int scope;
4424 const char *filter;
4425 const char **attrs;
4426 int attrsonly;
4427 void *pagedresults_cookie;
4429 LDAPMessage *entries, *current_entry;
4430 bool (*ldap2displayentry)(struct ldap_search_state *state,
4431 TALLOC_CTX *mem_ctx,
4432 LDAP *ld, LDAPMessage *entry,
4433 struct samr_displayentry *result);
4436 static bool ldapsam_search_firstpage(struct pdb_search *search)
4438 struct ldap_search_state *state =
4439 (struct ldap_search_state *)search->private_data;
4440 LDAP *ld;
4441 int rc = LDAP_OPERATIONS_ERROR;
4443 state->entries = NULL;
4445 if (smbldap_get_paged_results(state->connection)) {
4446 rc = smbldap_search_paged(state->connection, state->base,
4447 state->scope, state->filter,
4448 state->attrs, state->attrsonly,
4449 lp_ldap_page_size(), &state->entries,
4450 &state->pagedresults_cookie);
4453 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4455 if (state->entries != NULL) {
4456 /* Left over from unsuccessful paged attempt */
4457 ldap_msgfree(state->entries);
4458 state->entries = NULL;
4461 rc = smbldap_search(state->connection, state->base,
4462 state->scope, state->filter, state->attrs,
4463 state->attrsonly, &state->entries);
4465 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4466 return False;
4468 /* Ok, the server was lying. It told us it could do paged
4469 * searches when it could not. */
4470 smbldap_set_paged_results(state->connection, false);
4473 ld = smbldap_get_ldap(state->connection);
4474 if ( ld == NULL) {
4475 DEBUG(5, ("Don't have an LDAP connection right after a "
4476 "search\n"));
4477 return False;
4479 state->current_entry = ldap_first_entry(ld, state->entries);
4481 return True;
4484 static bool ldapsam_search_nextpage(struct pdb_search *search)
4486 struct ldap_search_state *state =
4487 (struct ldap_search_state *)search->private_data;
4488 int rc;
4490 if (!smbldap_get_paged_results(state->connection)) {
4491 /* There is no next page when there are no paged results */
4492 return False;
4495 rc = smbldap_search_paged(state->connection, state->base,
4496 state->scope, state->filter, state->attrs,
4497 state->attrsonly, lp_ldap_page_size(),
4498 &state->entries,
4499 &state->pagedresults_cookie);
4501 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4502 return False;
4504 state->current_entry = ldap_first_entry(
4505 smbldap_get_ldap(state->connection), state->entries);
4507 if (state->current_entry == NULL) {
4508 ldap_msgfree(state->entries);
4509 state->entries = NULL;
4510 return false;
4513 return True;
4516 static bool ldapsam_search_next_entry(struct pdb_search *search,
4517 struct samr_displayentry *entry)
4519 struct ldap_search_state *state =
4520 (struct ldap_search_state *)search->private_data;
4521 bool result;
4523 retry:
4524 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4525 return False;
4527 if ((state->entries == NULL) &&
4528 !ldapsam_search_nextpage(search))
4529 return False;
4531 if (state->current_entry == NULL) {
4532 return false;
4535 result = state->ldap2displayentry(state, search,
4536 smbldap_get_ldap(state->connection),
4537 state->current_entry, entry);
4539 if (!result) {
4540 char *dn;
4541 dn = ldap_get_dn(smbldap_get_ldap(state->connection),
4542 state->current_entry);
4543 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4544 if (dn != NULL) ldap_memfree(dn);
4547 state->current_entry = ldap_next_entry(
4548 smbldap_get_ldap(state->connection), state->current_entry);
4550 if (state->current_entry == NULL) {
4551 ldap_msgfree(state->entries);
4552 state->entries = NULL;
4555 if (!result) goto retry;
4557 return True;
4560 static void ldapsam_search_end(struct pdb_search *search)
4562 struct ldap_search_state *state =
4563 (struct ldap_search_state *)search->private_data;
4564 int rc;
4566 if (state->pagedresults_cookie == NULL)
4567 return;
4569 if (state->entries != NULL)
4570 ldap_msgfree(state->entries);
4572 state->entries = NULL;
4573 state->current_entry = NULL;
4575 if (!smbldap_get_paged_results(state->connection)) {
4576 return;
4579 /* Tell the LDAP server we're not interested in the rest anymore. */
4581 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4582 state->filter, state->attrs,
4583 state->attrsonly, 0, &state->entries,
4584 &state->pagedresults_cookie);
4586 if (rc != LDAP_SUCCESS)
4587 DEBUG(5, ("Could not end search properly\n"));
4589 return;
4592 static bool ldapuser2displayentry(struct ldap_search_state *state,
4593 TALLOC_CTX *mem_ctx,
4594 LDAP *ld, LDAPMessage *entry,
4595 struct samr_displayentry *result)
4597 char **vals;
4598 size_t converted_size;
4599 struct dom_sid sid;
4600 uint32_t acct_flags;
4602 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4603 if ((vals == NULL) || (vals[0] == NULL)) {
4604 acct_flags = ACB_NORMAL;
4605 } else {
4606 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4607 ldap_value_free(vals);
4610 if ((state->acct_flags != 0) &&
4611 ((state->acct_flags & acct_flags) == 0))
4612 return False;
4614 result->acct_flags = acct_flags;
4615 result->account_name = "";
4616 result->fullname = "";
4617 result->description = "";
4619 vals = ldap_get_values(ld, entry, "uid");
4620 if ((vals == NULL) || (vals[0] == NULL)) {
4621 DEBUG(5, ("\"uid\" not found\n"));
4622 return False;
4624 if (!pull_utf8_talloc(mem_ctx,
4625 discard_const_p(char *, &result->account_name),
4626 vals[0], &converted_size))
4628 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4629 strerror(errno)));
4632 ldap_value_free(vals);
4634 vals = ldap_get_values(ld, entry, "displayName");
4635 if ((vals == NULL) || (vals[0] == NULL))
4636 DEBUG(8, ("\"displayName\" not found\n"));
4637 else if (!pull_utf8_talloc(mem_ctx,
4638 discard_const_p(char *, &result->fullname),
4639 vals[0], &converted_size))
4641 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4642 strerror(errno)));
4645 ldap_value_free(vals);
4647 vals = ldap_get_values(ld, entry, "description");
4648 if ((vals == NULL) || (vals[0] == NULL))
4649 DEBUG(8, ("\"description\" not found\n"));
4650 else if (!pull_utf8_talloc(mem_ctx,
4651 discard_const_p(char *, &result->description),
4652 vals[0], &converted_size))
4654 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4655 strerror(errno)));
4658 ldap_value_free(vals);
4660 if ((result->account_name == NULL) ||
4661 (result->fullname == NULL) ||
4662 (result->description == NULL)) {
4663 DEBUG(0, ("talloc failed\n"));
4664 return False;
4667 vals = ldap_get_values(ld, entry, "sambaSid");
4668 if ((vals == NULL) || (vals[0] == NULL)) {
4669 DEBUG(0, ("\"objectSid\" not found\n"));
4670 return False;
4673 if (!string_to_sid(&sid, vals[0])) {
4674 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4675 ldap_value_free(vals);
4676 return False;
4678 ldap_value_free(vals);
4680 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4681 struct dom_sid_buf buf;
4682 DEBUG(0, ("sid %s does not belong to our domain\n",
4683 dom_sid_str_buf(&sid, &buf)));
4684 return False;
4687 return True;
4691 static bool ldapsam_search_users(struct pdb_methods *methods,
4692 struct pdb_search *search,
4693 uint32_t acct_flags)
4695 struct ldapsam_privates *ldap_state =
4696 (struct ldapsam_privates *)methods->private_data;
4697 struct ldap_search_state *state;
4699 state = talloc(search, struct ldap_search_state);
4700 if (state == NULL) {
4701 DEBUG(0, ("talloc failed\n"));
4702 return False;
4705 state->connection = ldap_state->smbldap_state;
4707 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4708 state->base = lp_ldap_user_suffix(talloc_tos());
4709 else if ((acct_flags != 0) &&
4710 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4711 state->base = lp_ldap_machine_suffix(talloc_tos());
4712 else
4713 state->base = lp_ldap_suffix(talloc_tos());
4715 state->acct_flags = acct_flags;
4716 state->base = talloc_strdup(search, state->base);
4717 state->scope = LDAP_SCOPE_SUBTREE;
4718 state->filter = get_ldap_filter(search, "*");
4719 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4720 "displayName", "description",
4721 "sambaAcctFlags", NULL);
4722 state->attrsonly = 0;
4723 state->pagedresults_cookie = NULL;
4724 state->entries = NULL;
4725 state->ldap2displayentry = ldapuser2displayentry;
4727 if ((state->filter == NULL) || (state->attrs == NULL)) {
4728 DEBUG(0, ("talloc failed\n"));
4729 return False;
4732 search->private_data = state;
4733 search->next_entry = ldapsam_search_next_entry;
4734 search->search_end = ldapsam_search_end;
4736 return ldapsam_search_firstpage(search);
4739 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4740 TALLOC_CTX *mem_ctx,
4741 LDAP *ld, LDAPMessage *entry,
4742 struct samr_displayentry *result)
4744 char **vals;
4745 size_t converted_size;
4746 struct dom_sid sid;
4747 uint16_t group_type;
4749 result->account_name = "";
4750 result->fullname = "";
4751 result->description = "";
4754 vals = ldap_get_values(ld, entry, "sambaGroupType");
4755 if ((vals == NULL) || (vals[0] == NULL)) {
4756 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4757 if (vals != NULL) {
4758 ldap_value_free(vals);
4760 return False;
4763 group_type = atoi(vals[0]);
4765 if ((state->group_type != 0) &&
4766 ((state->group_type != group_type))) {
4767 ldap_value_free(vals);
4768 return False;
4771 ldap_value_free(vals);
4773 /* display name is the NT group name */
4775 vals = ldap_get_values(ld, entry, "displayName");
4776 if ((vals == NULL) || (vals[0] == NULL)) {
4777 DEBUG(8, ("\"displayName\" not found\n"));
4779 /* fallback to the 'cn' attribute */
4780 vals = ldap_get_values(ld, entry, "cn");
4781 if ((vals == NULL) || (vals[0] == NULL)) {
4782 DEBUG(5, ("\"cn\" not found\n"));
4783 return False;
4785 if (!pull_utf8_talloc(mem_ctx,
4786 discard_const_p(char *,
4787 &result->account_name),
4788 vals[0], &converted_size))
4790 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4791 "failed: %s", strerror(errno)));
4794 else if (!pull_utf8_talloc(mem_ctx,
4795 discard_const_p(char *,
4796 &result->account_name),
4797 vals[0], &converted_size))
4799 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4800 strerror(errno)));
4803 ldap_value_free(vals);
4805 vals = ldap_get_values(ld, entry, "description");
4806 if ((vals == NULL) || (vals[0] == NULL))
4807 DEBUG(8, ("\"description\" not found\n"));
4808 else if (!pull_utf8_talloc(mem_ctx,
4809 discard_const_p(char *, &result->description),
4810 vals[0], &converted_size))
4812 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4813 strerror(errno)));
4815 ldap_value_free(vals);
4817 if ((result->account_name == NULL) ||
4818 (result->fullname == NULL) ||
4819 (result->description == NULL)) {
4820 DEBUG(0, ("talloc failed\n"));
4821 return False;
4824 vals = ldap_get_values(ld, entry, "sambaSid");
4825 if ((vals == NULL) || (vals[0] == NULL)) {
4826 DEBUG(0, ("\"objectSid\" not found\n"));
4827 if (vals != NULL) {
4828 ldap_value_free(vals);
4830 return False;
4833 if (!string_to_sid(&sid, vals[0])) {
4834 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4835 return False;
4838 ldap_value_free(vals);
4840 switch (group_type) {
4841 case SID_NAME_DOM_GRP:
4842 case SID_NAME_ALIAS:
4844 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4845 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4847 struct dom_sid_buf buf;
4848 DEBUG(0, ("%s is not in our domain\n",
4849 dom_sid_str_buf(&sid, &buf)));
4850 return False;
4852 break;
4854 default:
4855 DEBUG(0,("unknown group type: %d\n", group_type));
4856 return False;
4859 result->acct_flags = 0;
4861 return True;
4864 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4865 struct pdb_search *search,
4866 const struct dom_sid *sid,
4867 enum lsa_SidType type)
4869 struct ldapsam_privates *ldap_state =
4870 (struct ldapsam_privates *)methods->private_data;
4871 struct ldap_search_state *state;
4872 struct dom_sid_buf tmp;
4874 state = talloc(search, struct ldap_search_state);
4875 if (state == NULL) {
4876 DEBUG(0, ("talloc failed\n"));
4877 return False;
4880 state->connection = ldap_state->smbldap_state;
4882 state->base = lp_ldap_suffix(search);
4883 state->connection = ldap_state->smbldap_state;
4884 state->scope = LDAP_SCOPE_SUBTREE;
4885 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4886 "(sambaGroupType=%d)(sambaSID=%s*))",
4887 LDAP_OBJ_GROUPMAP,
4888 type,
4889 dom_sid_str_buf(sid, &tmp));
4890 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4891 "displayName", "description",
4892 "sambaGroupType", NULL);
4893 state->attrsonly = 0;
4894 state->pagedresults_cookie = NULL;
4895 state->entries = NULL;
4896 state->group_type = type;
4897 state->ldap2displayentry = ldapgroup2displayentry;
4899 if ((state->filter == NULL) || (state->attrs == NULL)) {
4900 DEBUG(0, ("talloc failed\n"));
4901 return False;
4904 search->private_data = state;
4905 search->next_entry = ldapsam_search_next_entry;
4906 search->search_end = ldapsam_search_end;
4908 return ldapsam_search_firstpage(search);
4911 static bool ldapsam_search_groups(struct pdb_methods *methods,
4912 struct pdb_search *search)
4914 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4917 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4918 struct pdb_search *search,
4919 const struct dom_sid *sid)
4921 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4924 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4926 return PDB_CAP_STORE_RIDS;
4929 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4930 uint32_t *rid)
4932 struct smbldap_state *smbldap_state = priv->smbldap_state;
4934 LDAPMessage *result = NULL;
4935 LDAPMessage *entry = NULL;
4936 LDAPMod **mods = NULL;
4937 NTSTATUS status;
4938 char *value;
4939 int rc;
4940 uint32_t nextRid = 0;
4941 const char *dn;
4942 uint32_t tmp;
4943 int error = 0;
4945 TALLOC_CTX *mem_ctx;
4947 mem_ctx = talloc_new(NULL);
4948 if (mem_ctx == NULL) {
4949 DEBUG(0, ("talloc_new failed\n"));
4950 return NT_STATUS_NO_MEMORY;
4953 status = smbldap_search_domain_info(smbldap_state, &result,
4954 get_global_sam_name(), False);
4955 if (!NT_STATUS_IS_OK(status)) {
4956 DEBUG(3, ("Could not get domain info: %s\n",
4957 nt_errstr(status)));
4958 goto done;
4961 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
4963 entry = ldap_first_entry(priv2ld(priv), result);
4964 if (entry == NULL) {
4965 DEBUG(0, ("Could not get domain info entry\n"));
4966 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4967 goto done;
4970 /* Find the largest of the three attributes "sambaNextRid",
4971 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4972 concept of differentiating between user and group rids, and will
4973 use only "sambaNextRid" in the future. But for compatibility
4974 reasons I look if others have chosen different strategies -- VL */
4976 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4977 "sambaNextRid", mem_ctx);
4978 if (value != NULL) {
4979 tmp = (uint32_t)strtoul_err(value, NULL, 10, &error);
4980 if (error != 0) {
4981 goto done;
4984 nextRid = MAX(nextRid, tmp);
4987 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4988 "sambaNextUserRid", mem_ctx);
4989 if (value != NULL) {
4990 tmp = (uint32_t)strtoul_err(value, NULL, 10, &error);
4991 if (error != 0) {
4992 goto done;
4995 nextRid = MAX(nextRid, tmp);
4998 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4999 "sambaNextGroupRid", mem_ctx);
5000 if (value != NULL) {
5001 tmp = (uint32_t)strtoul_err(value, NULL, 10, &error);
5002 if (error != 0) {
5003 goto done;
5006 nextRid = MAX(nextRid, tmp);
5009 if (nextRid == 0) {
5010 nextRid = BASE_RID-1;
5013 nextRid += 1;
5015 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
5016 talloc_asprintf(mem_ctx, "%d", nextRid));
5017 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
5019 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
5020 status = NT_STATUS_NO_MEMORY;
5021 goto done;
5024 rc = smbldap_modify(smbldap_state, dn, mods);
5026 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5027 * please retry" */
5029 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
5031 done:
5032 if (NT_STATUS_IS_OK(status)) {
5033 *rid = nextRid;
5036 TALLOC_FREE(mem_ctx);
5037 return status;
5040 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
5042 int i;
5044 for (i=0; i<10; i++) {
5045 NTSTATUS result = ldapsam_get_new_rid(
5046 (struct ldapsam_privates *)methods->private_data, rid);
5047 if (NT_STATUS_IS_OK(result)) {
5048 return result;
5051 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
5052 return result;
5055 /* The ldap update failed (maybe a race condition), retry */
5058 /* Tried 10 times, fail. */
5059 return NT_STATUS_ACCESS_DENIED;
5062 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
5064 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
5065 return NT_STATUS_IS_OK(result) ? True : False;
5068 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
5069 const struct dom_sid *sid,
5070 struct unixid *id)
5072 struct ldapsam_privates *priv =
5073 (struct ldapsam_privates *)methods->private_data;
5074 char *filter;
5075 int error = 0;
5076 struct dom_sid_buf buf;
5077 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5078 NULL };
5079 LDAPMessage *result = NULL;
5080 LDAPMessage *entry = NULL;
5081 bool ret = False;
5082 char *value;
5083 int rc;
5085 TALLOC_CTX *mem_ctx;
5087 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
5088 if (ret == true) {
5089 return true;
5092 mem_ctx = talloc_new(NULL);
5093 if (mem_ctx == NULL) {
5094 DEBUG(0, ("talloc_new failed\n"));
5095 return False;
5098 filter = talloc_asprintf(mem_ctx,
5099 "(&(sambaSid=%s)"
5100 "(|(objectClass=%s)(objectClass=%s)))",
5101 dom_sid_str_buf(sid, &buf),
5102 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5103 if (filter == NULL) {
5104 DEBUG(5, ("talloc_asprintf failed\n"));
5105 goto done;
5108 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5109 attrs, &result);
5110 if (rc != LDAP_SUCCESS) {
5111 goto done;
5113 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
5115 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5116 DEBUG(10, ("Got %d entries, expected one\n",
5117 ldap_count_entries(priv2ld(priv), result)));
5118 goto done;
5121 entry = ldap_first_entry(priv2ld(priv), result);
5123 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5124 "sambaGroupType", mem_ctx);
5126 if (value != NULL) {
5127 const char *gid_str;
5128 /* It's a group */
5130 gid_str = smbldap_talloc_single_attribute(
5131 priv2ld(priv), entry, "gidNumber", mem_ctx);
5132 if (gid_str == NULL) {
5133 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5134 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5135 entry)));
5136 goto done;
5139 id->id = strtoul_err(gid_str, NULL, 10, &error);
5140 if (error != 0) {
5141 goto done;
5144 id->type = ID_TYPE_GID;
5145 ret = True;
5146 goto done;
5149 /* It must be a user */
5151 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5152 "uidNumber", mem_ctx);
5153 if (value == NULL) {
5154 DEBUG(1, ("Could not find uidNumber in %s\n",
5155 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5156 goto done;
5159 id->id = strtoul_err(value, NULL, 10, &error);
5160 if (error != 0) {
5161 goto done;
5164 id->type = ID_TYPE_UID;
5165 ret = True;
5166 done:
5167 TALLOC_FREE(mem_ctx);
5168 return ret;
5172 * Find the SID for a uid.
5173 * This is shortcut is only used if ldapsam:trusted is set to true.
5175 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5176 struct dom_sid *sid)
5178 struct ldapsam_privates *priv =
5179 (struct ldapsam_privates *)methods->private_data;
5180 char *filter;
5181 const char *attrs[] = { "sambaSID", NULL };
5182 LDAPMessage *result = NULL;
5183 LDAPMessage *entry = NULL;
5184 bool ret = false;
5185 char *user_sid_string;
5186 struct dom_sid user_sid;
5187 int rc;
5188 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5190 filter = talloc_asprintf(tmp_ctx,
5191 "(&(uidNumber=%u)"
5192 "(objectClass=%s)"
5193 "(objectClass=%s))",
5194 (unsigned int)uid,
5195 LDAP_OBJ_POSIXACCOUNT,
5196 LDAP_OBJ_SAMBASAMACCOUNT);
5197 if (filter == NULL) {
5198 DEBUG(3, ("talloc_asprintf failed\n"));
5199 goto done;
5202 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5203 if (rc != LDAP_SUCCESS) {
5204 goto done;
5206 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5208 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5209 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5210 ldap_count_entries(priv2ld(priv), result),
5211 (unsigned int)uid));
5212 goto done;
5215 entry = ldap_first_entry(priv2ld(priv), result);
5217 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5218 "sambaSID", tmp_ctx);
5219 if (user_sid_string == NULL) {
5220 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5221 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5222 goto done;
5225 if (!string_to_sid(&user_sid, user_sid_string)) {
5226 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5227 user_sid_string));
5228 goto done;
5231 sid_copy(sid, &user_sid);
5233 ret = true;
5235 done:
5236 TALLOC_FREE(tmp_ctx);
5237 return ret;
5241 * Find the SID for a gid.
5242 * This is shortcut is only used if ldapsam:trusted is set to true.
5244 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5245 struct dom_sid *sid)
5247 struct ldapsam_privates *priv =
5248 (struct ldapsam_privates *)methods->private_data;
5249 char *filter;
5250 const char *attrs[] = { "sambaSID", NULL };
5251 LDAPMessage *result = NULL;
5252 LDAPMessage *entry = NULL;
5253 bool ret = false;
5254 char *group_sid_string;
5255 struct dom_sid group_sid;
5256 int rc;
5257 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5259 filter = talloc_asprintf(tmp_ctx,
5260 "(&(gidNumber=%u)"
5261 "(objectClass=%s))",
5262 (unsigned int)gid,
5263 LDAP_OBJ_GROUPMAP);
5264 if (filter == NULL) {
5265 DEBUG(3, ("talloc_asprintf failed\n"));
5266 goto done;
5269 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5270 if (rc != LDAP_SUCCESS) {
5271 goto done;
5273 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5275 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5276 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5277 ldap_count_entries(priv2ld(priv), result),
5278 (unsigned int)gid));
5279 goto done;
5282 entry = ldap_first_entry(priv2ld(priv), result);
5284 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5285 "sambaSID", tmp_ctx);
5286 if (group_sid_string == NULL) {
5287 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5288 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5289 goto done;
5292 if (!string_to_sid(&group_sid, group_sid_string)) {
5293 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5294 group_sid_string));
5295 goto done;
5298 sid_copy(sid, &group_sid);
5300 ret = true;
5302 done:
5303 TALLOC_FREE(tmp_ctx);
5304 return ret;
5307 static bool ldapsam_id_to_sid(struct pdb_methods *methods, struct unixid *id,
5308 struct dom_sid *sid)
5310 switch (id->type) {
5311 case ID_TYPE_UID:
5312 return ldapsam_uid_to_sid(methods, id->id, sid);
5314 case ID_TYPE_GID:
5315 return ldapsam_gid_to_sid(methods, id->id, sid);
5317 default:
5318 return false;
5324 * The following functions are called only if
5325 * ldapsam:trusted and ldapsam:editposix are
5326 * set to true
5330 * ldapsam_create_user creates a new
5331 * posixAccount and sambaSamAccount object
5332 * in the ldap users subtree
5334 * The uid is allocated by winbindd.
5337 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5338 TALLOC_CTX *tmp_ctx, const char *name,
5339 uint32_t acb_info, uint32_t *rid)
5341 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5342 LDAPMessage *entry = NULL;
5343 LDAPMessage *result = NULL;
5344 uint32_t num_result;
5345 bool is_machine = False;
5346 bool add_posix = False;
5347 bool init_okay = False;
5348 LDAPMod **mods = NULL;
5349 struct samu *user;
5350 char *filter;
5351 char *username;
5352 char *homedir;
5353 char *gidstr;
5354 char *uidstr;
5355 char *shell;
5356 const char *dn = NULL;
5357 struct dom_sid group_sid;
5358 struct dom_sid user_sid;
5359 gid_t gid = -1;
5360 uid_t uid = -1;
5361 NTSTATUS ret;
5362 int rc;
5364 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5365 acb_info & ACB_WSTRUST ||
5366 acb_info & ACB_SVRTRUST ||
5367 acb_info & ACB_DOMTRUST) {
5368 is_machine = True;
5371 username = escape_ldap_string(talloc_tos(), name);
5372 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5373 username, LDAP_OBJ_POSIXACCOUNT);
5374 TALLOC_FREE(username);
5376 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5377 if (rc != LDAP_SUCCESS) {
5378 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5379 return NT_STATUS_ACCESS_DENIED;
5381 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5383 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5385 if (num_result > 1) {
5386 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5387 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5390 if (num_result == 1) {
5391 char *tmp;
5392 /* check if it is just a posix account.
5393 * or if there is a sid attached to this entry
5396 entry = ldap_first_entry(priv2ld(ldap_state), result);
5397 if (!entry) {
5398 return NT_STATUS_UNSUCCESSFUL;
5401 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5402 if (tmp) {
5403 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5404 return NT_STATUS_USER_EXISTS;
5407 /* it is just a posix account, retrieve the dn for later use */
5408 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5409 if (!dn) {
5410 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5411 return NT_STATUS_NO_MEMORY;
5415 if (num_result == 0) {
5416 add_posix = True;
5419 /* Create the basic samu structure and generate the mods for the ldap commit */
5420 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5421 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5422 return ret;
5425 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5427 user = samu_new(tmp_ctx);
5428 if (!user) {
5429 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5430 return NT_STATUS_NO_MEMORY;
5433 if (!pdb_set_username(user, name, PDB_SET)) {
5434 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5435 return NT_STATUS_UNSUCCESSFUL;
5437 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5438 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5439 return NT_STATUS_UNSUCCESSFUL;
5441 if (is_machine) {
5442 if (acb_info & ACB_NORMAL) {
5443 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5444 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5445 return NT_STATUS_UNSUCCESSFUL;
5447 } else {
5448 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5449 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5450 return NT_STATUS_UNSUCCESSFUL;
5453 } else {
5454 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5455 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5456 return NT_STATUS_UNSUCCESSFUL;
5460 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5461 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5462 return NT_STATUS_UNSUCCESSFUL;
5465 init_okay = init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed);
5467 if (!init_okay) {
5468 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5469 ldap_mods_free(mods, true);
5470 return NT_STATUS_UNSUCCESSFUL;
5473 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5474 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5476 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5478 if (add_posix) {
5479 char *escape_name;
5481 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5483 /* retrieve the Domain Users group gid */
5484 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5485 !sid_to_gid(&group_sid, &gid)) {
5486 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5487 ldap_mods_free(mods, true);
5488 return NT_STATUS_INVALID_PRIMARY_GROUP;
5491 /* lets allocate a new userid for this user */
5492 if (!winbind_allocate_uid(&uid)) {
5493 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5494 ldap_mods_free(mods, true);
5495 return NT_STATUS_UNSUCCESSFUL;
5499 if (is_machine) {
5500 /* TODO: choose a more appropriate default for machines */
5501 homedir = talloc_sub_specified(tmp_ctx,
5502 lp_template_homedir(),
5503 "SMB_workstations_home",
5504 NULL,
5505 ldap_state->domain_name,
5506 uid,
5507 gid);
5508 shell = talloc_strdup(tmp_ctx, "/bin/false");
5509 } else {
5510 homedir = talloc_sub_specified(tmp_ctx,
5511 lp_template_homedir(),
5512 name,
5513 NULL,
5514 ldap_state->domain_name,
5515 uid,
5516 gid);
5517 shell = talloc_sub_specified(tmp_ctx,
5518 lp_template_shell(),
5519 name,
5520 NULL,
5521 ldap_state->domain_name,
5522 uid,
5523 gid);
5525 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5526 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5528 escape_name = escape_rdn_val_string_alloc(name);
5529 if (!escape_name) {
5530 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5531 ldap_mods_free(mods, true);
5532 return NT_STATUS_NO_MEMORY;
5535 if (is_machine) {
5536 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix (talloc_tos()));
5537 } else {
5538 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix (talloc_tos()));
5541 SAFE_FREE(escape_name);
5543 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5544 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5545 ldap_mods_free(mods, true);
5546 return NT_STATUS_NO_MEMORY;
5549 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5550 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5551 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5552 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5553 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5554 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5555 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5558 if (add_posix) {
5559 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5560 } else {
5561 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5564 ldap_mods_free(mods, true);
5566 if (rc != LDAP_SUCCESS) {
5567 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5568 return NT_STATUS_UNSUCCESSFUL;
5571 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5573 flush_pwnam_cache();
5575 return NT_STATUS_OK;
5578 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5580 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5581 LDAPMessage *result = NULL;
5582 LDAPMessage *entry = NULL;
5583 int num_result;
5584 const char *dn;
5585 char *filter;
5586 int rc;
5588 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5590 filter = talloc_asprintf(tmp_ctx,
5591 "(&(uid=%s)"
5592 "(objectClass=%s)"
5593 "(objectClass=%s))",
5594 pdb_get_username(sam_acct),
5595 LDAP_OBJ_POSIXACCOUNT,
5596 LDAP_OBJ_SAMBASAMACCOUNT);
5597 if (filter == NULL) {
5598 return NT_STATUS_NO_MEMORY;
5601 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5602 if (rc != LDAP_SUCCESS) {
5603 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5604 return NT_STATUS_UNSUCCESSFUL;
5606 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5608 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5610 if (num_result == 0) {
5611 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5612 return NT_STATUS_NO_SUCH_USER;
5615 if (num_result > 1) {
5616 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5617 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5620 entry = ldap_first_entry(priv2ld(ldap_state), result);
5621 if (!entry) {
5622 return NT_STATUS_UNSUCCESSFUL;
5625 /* it is just a posix account, retrieve the dn for later use */
5626 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5627 if (!dn) {
5628 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5629 return NT_STATUS_NO_MEMORY;
5632 /* try to remove memberships first */
5634 NTSTATUS status;
5635 struct dom_sid *sids = NULL;
5636 gid_t *gids = NULL;
5637 uint32_t num_groups = 0;
5638 int i;
5639 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5641 status = ldapsam_enum_group_memberships(my_methods,
5642 tmp_ctx,
5643 sam_acct,
5644 &sids,
5645 &gids,
5646 &num_groups);
5647 if (!NT_STATUS_IS_OK(status)) {
5648 goto delete_dn;
5651 for (i=0; i < num_groups; i++) {
5653 uint32_t group_rid;
5655 sid_peek_rid(&sids[i], &group_rid);
5657 ldapsam_del_groupmem(my_methods,
5658 tmp_ctx,
5659 group_rid,
5660 user_rid);
5664 delete_dn:
5666 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5667 if (rc != LDAP_SUCCESS) {
5668 return NT_STATUS_UNSUCCESSFUL;
5671 flush_pwnam_cache();
5673 return NT_STATUS_OK;
5677 * ldapsam_create_group creates a new
5678 * posixGroup and sambaGroupMapping object
5679 * in the ldap groups subtree
5681 * The gid is allocated by winbindd.
5684 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5685 TALLOC_CTX *tmp_ctx,
5686 const char *name,
5687 uint32_t *rid)
5689 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5690 NTSTATUS ret;
5691 LDAPMessage *entry = NULL;
5692 LDAPMessage *result = NULL;
5693 uint32_t num_result;
5694 bool is_new_entry = False;
5695 LDAPMod **mods = NULL;
5696 char *filter;
5697 char *groupname;
5698 char *grouptype;
5699 char *gidstr;
5700 const char *dn = NULL;
5701 struct dom_sid group_sid;
5702 struct dom_sid_buf buf;
5703 gid_t gid = -1;
5704 int rc;
5705 int error = 0;
5707 groupname = escape_ldap_string(talloc_tos(), name);
5708 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5709 groupname, LDAP_OBJ_POSIXGROUP);
5710 TALLOC_FREE(groupname);
5712 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5713 if (rc != LDAP_SUCCESS) {
5714 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5715 return NT_STATUS_UNSUCCESSFUL;
5717 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5719 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5721 if (num_result > 1) {
5722 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5723 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5726 if (num_result == 1) {
5727 char *tmp;
5728 /* check if it is just a posix group.
5729 * or if there is a sid attached to this entry
5732 entry = ldap_first_entry(priv2ld(ldap_state), result);
5733 if (!entry) {
5734 return NT_STATUS_UNSUCCESSFUL;
5737 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5738 if (tmp) {
5739 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5740 return NT_STATUS_GROUP_EXISTS;
5743 /* it is just a posix group, retrieve the gid and the dn for later use */
5744 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5745 if (!tmp) {
5746 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5747 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5750 gid = strtoul_err(tmp, NULL, 10, &error);
5751 if (error != 0) {
5752 DBG_ERR("Failed to convert gidNumber\n");
5753 return NT_STATUS_UNSUCCESSFUL;
5756 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5757 if (!dn) {
5758 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5759 return NT_STATUS_NO_MEMORY;
5763 if (num_result == 0) {
5764 is_new_entry = true;
5767 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5768 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5769 return ret;
5772 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5774 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5776 if (!grouptype) {
5777 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5778 return NT_STATUS_NO_MEMORY;
5781 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5782 smbldap_set_mod(&mods,
5783 LDAP_MOD_ADD,
5784 "sambaSid",
5785 dom_sid_str_buf(&group_sid, &buf));
5786 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5787 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5789 if (is_new_entry) {
5790 char *escape_name;
5792 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5794 /* lets allocate a new groupid for this group */
5795 if (!winbind_allocate_gid(&gid)) {
5796 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5797 return NT_STATUS_UNSUCCESSFUL;
5800 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5802 escape_name = escape_rdn_val_string_alloc(name);
5803 if (!escape_name) {
5804 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5805 return NT_STATUS_NO_MEMORY;
5808 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix(talloc_tos()));
5810 SAFE_FREE(escape_name);
5812 if (!gidstr || !dn) {
5813 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5814 return NT_STATUS_NO_MEMORY;
5817 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5818 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5819 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5822 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
5824 if (is_new_entry) {
5825 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5826 #if 0
5827 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5828 /* This call may fail with rfc2307bis schema */
5829 /* Retry adding a structural class */
5830 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5831 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5833 #endif
5834 } else {
5835 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5838 if (rc != LDAP_SUCCESS) {
5839 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5840 return NT_STATUS_UNSUCCESSFUL;
5843 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5845 return NT_STATUS_OK;
5848 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5850 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5851 LDAPMessage *result = NULL;
5852 LDAPMessage *entry = NULL;
5853 int num_result;
5854 const char *dn;
5855 char *gidstr;
5856 char *filter;
5857 struct dom_sid group_sid;
5858 struct dom_sid_buf buf;
5859 int rc;
5861 /* get the group sid */
5862 sid_compose(&group_sid, get_global_sam_sid(), rid);
5864 filter = talloc_asprintf(tmp_ctx,
5865 "(&(sambaSID=%s)"
5866 "(objectClass=%s)"
5867 "(objectClass=%s))",
5868 dom_sid_str_buf(&group_sid, &buf),
5869 LDAP_OBJ_POSIXGROUP,
5870 LDAP_OBJ_GROUPMAP);
5871 if (filter == NULL) {
5872 return NT_STATUS_NO_MEMORY;
5875 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5876 if (rc != LDAP_SUCCESS) {
5877 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5878 return NT_STATUS_UNSUCCESSFUL;
5880 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5882 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5884 if (num_result == 0) {
5885 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5886 return NT_STATUS_NO_SUCH_GROUP;
5889 if (num_result > 1) {
5890 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5891 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5894 entry = ldap_first_entry(priv2ld(ldap_state), result);
5895 if (!entry) {
5896 return NT_STATUS_UNSUCCESSFUL;
5899 /* here it is, retrieve the dn for later use */
5900 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5901 if (!dn) {
5902 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5903 return NT_STATUS_NO_MEMORY;
5906 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5907 if (!gidstr) {
5908 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5909 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5912 /* check no user have this group marked as primary group */
5913 filter = talloc_asprintf(tmp_ctx,
5914 "(&(gidNumber=%s)"
5915 "(objectClass=%s)"
5916 "(objectClass=%s))",
5917 gidstr,
5918 LDAP_OBJ_POSIXACCOUNT,
5919 LDAP_OBJ_SAMBASAMACCOUNT);
5921 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5922 if (rc != LDAP_SUCCESS) {
5923 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5924 return NT_STATUS_UNSUCCESSFUL;
5926 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5928 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5930 if (num_result != 0) {
5931 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5932 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5935 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5936 if (rc != LDAP_SUCCESS) {
5937 return NT_STATUS_UNSUCCESSFUL;
5940 return NT_STATUS_OK;
5943 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5944 TALLOC_CTX *tmp_ctx,
5945 uint32_t group_rid,
5946 uint32_t member_rid,
5947 int modop)
5949 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5950 LDAPMessage *entry = NULL;
5951 LDAPMessage *result = NULL;
5952 uint32_t num_result;
5953 LDAPMod **mods = NULL;
5954 char *filter;
5955 char *uidstr;
5956 const char *dn = NULL;
5957 struct dom_sid group_sid;
5958 struct dom_sid member_sid;
5959 struct dom_sid_buf buf;
5960 int rc;
5961 int error = 0;
5963 switch (modop) {
5964 case LDAP_MOD_ADD:
5965 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5966 break;
5967 case LDAP_MOD_DELETE:
5968 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5969 break;
5970 default:
5971 return NT_STATUS_UNSUCCESSFUL;
5974 /* get member sid */
5975 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5977 /* get the group sid */
5978 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5980 filter = talloc_asprintf(tmp_ctx,
5981 "(&(sambaSID=%s)"
5982 "(objectClass=%s)"
5983 "(objectClass=%s))",
5984 dom_sid_str_buf(&member_sid, &buf),
5985 LDAP_OBJ_POSIXACCOUNT,
5986 LDAP_OBJ_SAMBASAMACCOUNT);
5987 if (filter == NULL) {
5988 return NT_STATUS_NO_MEMORY;
5991 /* get the member uid */
5992 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5993 if (rc != LDAP_SUCCESS) {
5994 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5995 return NT_STATUS_UNSUCCESSFUL;
5997 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5999 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6001 if (num_result == 0) {
6002 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6003 return NT_STATUS_NO_SUCH_MEMBER;
6006 if (num_result > 1) {
6007 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6008 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6011 entry = ldap_first_entry(priv2ld(ldap_state), result);
6012 if (!entry) {
6013 return NT_STATUS_UNSUCCESSFUL;
6016 if (modop == LDAP_MOD_DELETE) {
6017 /* check if we are trying to remove the member from his primary group */
6018 char *gidstr;
6019 gid_t user_gid, group_gid;
6021 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
6022 if (!gidstr) {
6023 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6024 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6027 user_gid = strtoul_err(gidstr, NULL, 10, &error);
6028 if (error != 0) {
6029 DBG_ERR("Failed to convert user gid\n");
6030 return NT_STATUS_UNSUCCESSFUL;
6033 if (!sid_to_gid(&group_sid, &group_gid)) {
6034 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6035 return NT_STATUS_UNSUCCESSFUL;
6038 if (user_gid == group_gid) {
6039 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6040 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
6044 /* here it is, retrieve the uid for later use */
6045 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
6046 if (!uidstr) {
6047 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6048 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6051 filter = talloc_asprintf(tmp_ctx,
6052 "(&(sambaSID=%s)"
6053 "(objectClass=%s)"
6054 "(objectClass=%s))",
6055 dom_sid_str_buf(&group_sid, &buf),
6056 LDAP_OBJ_POSIXGROUP,
6057 LDAP_OBJ_GROUPMAP);
6059 /* get the group */
6060 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6061 if (rc != LDAP_SUCCESS) {
6062 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6063 return NT_STATUS_UNSUCCESSFUL;
6065 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
6067 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6069 if (num_result == 0) {
6070 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6071 return NT_STATUS_NO_SUCH_GROUP;
6074 if (num_result > 1) {
6075 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6076 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6079 entry = ldap_first_entry(priv2ld(ldap_state), result);
6080 if (!entry) {
6081 return NT_STATUS_UNSUCCESSFUL;
6084 /* here it is, retrieve the dn for later use */
6085 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
6086 if (!dn) {
6087 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6088 return NT_STATUS_NO_MEMORY;
6091 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
6093 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
6095 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6096 if (rc != LDAP_SUCCESS) {
6097 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
6098 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6099 return NT_STATUS_MEMBER_IN_GROUP;
6101 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
6102 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6103 return NT_STATUS_MEMBER_NOT_IN_GROUP;
6105 return NT_STATUS_UNSUCCESSFUL;
6108 return NT_STATUS_OK;
6111 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
6112 TALLOC_CTX *tmp_ctx,
6113 uint32_t group_rid,
6114 uint32_t member_rid)
6116 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
6118 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
6119 TALLOC_CTX *tmp_ctx,
6120 uint32_t group_rid,
6121 uint32_t member_rid)
6123 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
6126 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
6127 TALLOC_CTX *mem_ctx,
6128 struct samu *sampass)
6130 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6131 LDAPMessage *entry = NULL;
6132 LDAPMessage *result = NULL;
6133 uint32_t num_result;
6134 LDAPMod **mods = NULL;
6135 char *filter;
6136 char *escape_username;
6137 char *gidstr;
6138 char *dn = NULL;
6139 gid_t gid;
6140 int rc;
6142 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6144 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6145 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6146 return NT_STATUS_UNSUCCESSFUL;
6148 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6149 if (!gidstr) {
6150 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6151 return NT_STATUS_NO_MEMORY;
6154 escape_username = escape_ldap_string(talloc_tos(),
6155 pdb_get_username(sampass));
6156 if (escape_username== NULL) {
6157 return NT_STATUS_NO_MEMORY;
6160 filter = talloc_asprintf(mem_ctx,
6161 "(&(uid=%s)"
6162 "(objectClass=%s)"
6163 "(objectClass=%s))",
6164 escape_username,
6165 LDAP_OBJ_POSIXACCOUNT,
6166 LDAP_OBJ_SAMBASAMACCOUNT);
6168 TALLOC_FREE(escape_username);
6170 if (filter == NULL) {
6171 return NT_STATUS_NO_MEMORY;
6174 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6175 if (rc != LDAP_SUCCESS) {
6176 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6177 return NT_STATUS_UNSUCCESSFUL;
6179 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6181 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6183 if (num_result == 0) {
6184 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6185 return NT_STATUS_NO_SUCH_USER;
6188 if (num_result > 1) {
6189 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6190 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6193 entry = ldap_first_entry(priv2ld(ldap_state), result);
6194 if (!entry) {
6195 return NT_STATUS_UNSUCCESSFUL;
6198 /* retrieve the dn for later use */
6199 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6200 if (!dn) {
6201 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6202 return NT_STATUS_NO_MEMORY;
6205 /* remove the old one, and add the new one, this way we do not risk races */
6206 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6208 if (mods == NULL) {
6209 TALLOC_FREE(dn);
6210 return NT_STATUS_OK;
6213 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6214 TALLOC_FREE(dn);
6215 if (rc != LDAP_SUCCESS) {
6216 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6217 pdb_get_username(sampass), gidstr));
6218 return NT_STATUS_UNSUCCESSFUL;
6221 flush_pwnam_cache();
6223 return NT_STATUS_OK;
6227 /**********************************************************************
6228 trusted domains functions
6229 *********************************************************************/
6231 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6232 const char *domain)
6234 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6235 ldap_state->domain_dn);
6238 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6239 TALLOC_CTX *mem_ctx,
6240 const char *domain, LDAPMessage **entry)
6242 int rc;
6243 char *filter;
6244 int scope = LDAP_SCOPE_SUBTREE;
6245 const char **attrs = NULL; /* NULL: get all attrs */
6246 int attrsonly = 0; /* 0: return values too */
6247 LDAPMessage *result = NULL;
6248 char *trusted_dn;
6249 uint32_t num_result;
6251 filter = talloc_asprintf(talloc_tos(),
6252 "(&(objectClass=%s)(sambaDomainName=%s))",
6253 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6255 trusted_dn = trusteddom_dn(ldap_state, domain);
6256 if (trusted_dn == NULL) {
6257 return False;
6259 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6260 filter, attrs, attrsonly, &result);
6262 if (result != NULL) {
6263 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6266 if (rc == LDAP_NO_SUCH_OBJECT) {
6267 *entry = NULL;
6268 return True;
6271 if (rc != LDAP_SUCCESS) {
6272 return False;
6275 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6277 if (num_result > 1) {
6278 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6279 "%s object for domain '%s'?!\n",
6280 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6281 return False;
6284 if (num_result == 0) {
6285 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6286 "%s object for domain %s.\n",
6287 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6288 *entry = NULL;
6289 } else {
6290 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6293 return True;
6296 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6297 const char *domain,
6298 char** pwd,
6299 struct dom_sid *sid,
6300 time_t *pass_last_set_time)
6302 struct ldapsam_privates *ldap_state =
6303 (struct ldapsam_privates *)methods->private_data;
6304 LDAPMessage *entry = NULL;
6306 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6308 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6309 (entry == NULL))
6311 return False;
6314 /* password */
6315 if (pwd != NULL) {
6316 char *pwd_str;
6317 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6318 entry, "sambaClearTextPassword", talloc_tos());
6319 if (pwd_str == NULL) {
6320 return False;
6322 /* trusteddom_pw routines do not use talloc yet... */
6323 *pwd = SMB_STRDUP(pwd_str);
6324 if (*pwd == NULL) {
6325 return False;
6329 /* last change time */
6330 if (pass_last_set_time != NULL) {
6331 char *time_str;
6332 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6333 entry, "sambaPwdLastSet", talloc_tos());
6334 if (time_str == NULL) {
6335 return False;
6337 *pass_last_set_time = (time_t)atol(time_str);
6340 /* domain sid */
6341 if (sid != NULL) {
6342 char *sid_str;
6343 struct dom_sid dom_sid;
6344 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6345 entry, "sambaSID",
6346 talloc_tos());
6347 if (sid_str == NULL) {
6348 return False;
6350 if (!string_to_sid(&dom_sid, sid_str)) {
6351 return False;
6353 sid_copy(sid, &dom_sid);
6356 return True;
6359 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6360 const char* domain,
6361 const char* pwd,
6362 const struct dom_sid *sid)
6364 struct ldapsam_privates *ldap_state =
6365 (struct ldapsam_privates *)methods->private_data;
6366 LDAPMessage *entry = NULL;
6367 LDAPMod **mods = NULL;
6368 char *prev_pwd = NULL;
6369 char *trusted_dn = NULL;
6370 struct dom_sid_buf buf;
6371 int rc;
6373 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6376 * get the current entry (if there is one) in order to put the
6377 * current password into the previous password attribute
6379 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6380 return False;
6383 mods = NULL;
6384 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6385 LDAP_OBJ_TRUSTDOM_PASSWORD);
6386 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6387 domain);
6388 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6389 dom_sid_str_buf(sid, &buf));
6390 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6391 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6392 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6393 "sambaClearTextPassword", pwd);
6395 if (entry != NULL) {
6396 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6397 entry, "sambaClearTextPassword", talloc_tos());
6398 if (prev_pwd != NULL) {
6399 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6400 "sambaPreviousClearTextPassword",
6401 prev_pwd);
6405 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods);
6407 trusted_dn = trusteddom_dn(ldap_state, domain);
6408 if (trusted_dn == NULL) {
6409 return False;
6411 if (entry == NULL) {
6412 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6413 } else {
6414 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6417 if (rc != LDAP_SUCCESS) {
6418 DEBUG(1, ("error writing trusted domain password!\n"));
6419 return False;
6422 return True;
6425 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6426 const char *domain)
6428 int rc;
6429 struct ldapsam_privates *ldap_state =
6430 (struct ldapsam_privates *)methods->private_data;
6431 LDAPMessage *entry = NULL;
6432 const char *trusted_dn;
6434 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6435 return False;
6438 if (entry == NULL) {
6439 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6440 "%s\n", domain));
6441 return True;
6444 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6445 entry);
6446 if (trusted_dn == NULL) {
6447 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6448 return False;
6451 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6452 if (rc != LDAP_SUCCESS) {
6453 return False;
6456 return True;
6459 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6460 TALLOC_CTX *mem_ctx,
6461 uint32_t *num_domains,
6462 struct trustdom_info ***domains)
6464 int rc;
6465 struct ldapsam_privates *ldap_state =
6466 (struct ldapsam_privates *)methods->private_data;
6467 char *filter;
6468 int scope = LDAP_SCOPE_SUBTREE;
6469 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6470 int attrsonly = 0; /* 0: return values too */
6471 LDAPMessage *result = NULL;
6472 LDAPMessage *entry = NULL;
6474 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6475 LDAP_OBJ_TRUSTDOM_PASSWORD);
6477 rc = smbldap_search(ldap_state->smbldap_state,
6478 ldap_state->domain_dn,
6479 scope,
6480 filter,
6481 attrs,
6482 attrsonly,
6483 &result);
6485 if (result != NULL) {
6486 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6489 if (rc != LDAP_SUCCESS) {
6490 return NT_STATUS_UNSUCCESSFUL;
6493 *num_domains = 0;
6494 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6495 DEBUG(1, ("talloc failed\n"));
6496 return NT_STATUS_NO_MEMORY;
6499 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6500 entry != NULL;
6501 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6503 char *dom_name, *dom_sid_str;
6504 struct trustdom_info *dom_info;
6506 dom_info = talloc(*domains, struct trustdom_info);
6507 if (dom_info == NULL) {
6508 DEBUG(1, ("talloc failed\n"));
6509 return NT_STATUS_NO_MEMORY;
6512 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6513 entry,
6514 "sambaDomainName",
6515 talloc_tos());
6516 if (dom_name == NULL) {
6517 DEBUG(1, ("talloc failed\n"));
6518 return NT_STATUS_NO_MEMORY;
6520 dom_info->name = dom_name;
6522 dom_sid_str = smbldap_talloc_single_attribute(
6523 priv2ld(ldap_state), entry, "sambaSID",
6524 talloc_tos());
6525 if (dom_sid_str == NULL) {
6526 DEBUG(1, ("talloc failed\n"));
6527 return NT_STATUS_NO_MEMORY;
6529 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6530 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6531 dom_sid_str));
6532 return NT_STATUS_UNSUCCESSFUL;
6535 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6536 domains, num_domains);
6538 if (*domains == NULL) {
6539 DEBUG(1, ("talloc failed\n"));
6540 return NT_STATUS_NO_MEMORY;
6544 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6545 return NT_STATUS_OK;
6549 /**********************************************************************
6550 Housekeeping
6551 *********************************************************************/
6553 static void free_private_data(void **vp)
6555 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6557 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6559 if ((*ldap_state)->result != NULL) {
6560 ldap_msgfree((*ldap_state)->result);
6561 (*ldap_state)->result = NULL;
6563 if ((*ldap_state)->domain_dn != NULL) {
6564 SAFE_FREE((*ldap_state)->domain_dn);
6567 *ldap_state = NULL;
6569 /* No need to free any further, as it is talloc()ed */
6572 /*********************************************************************
6573 Intitalise the parts of the pdb_methods structure that are common to
6574 all pdb_ldap modes
6575 *********************************************************************/
6577 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6579 NTSTATUS nt_status;
6580 struct ldapsam_privates *ldap_state;
6581 char *bind_dn = NULL;
6582 char *bind_secret = NULL;
6584 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6585 return nt_status;
6588 (*pdb_method)->name = "ldapsam";
6590 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6591 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6592 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6593 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6594 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6595 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6597 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6598 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6599 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6600 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6601 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6602 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6603 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6605 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6606 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6608 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6610 (*pdb_method)->capabilities = ldapsam_capabilities;
6611 (*pdb_method)->new_rid = ldapsam_new_rid;
6613 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6614 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6615 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6616 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6618 /* TODO: Setup private data and free */
6620 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6621 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6622 return NT_STATUS_NO_MEMORY;
6625 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6626 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6627 return NT_STATUS_NO_MEMORY;
6630 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6631 location, false, bind_dn, bind_secret,
6632 &ldap_state->smbldap_state);
6633 memset(bind_secret, '\0', strlen(bind_secret));
6634 SAFE_FREE(bind_secret);
6635 SAFE_FREE(bind_dn);
6636 if ( !NT_STATUS_IS_OK(nt_status) ) {
6637 return nt_status;
6640 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6641 return NT_STATUS_NO_MEMORY;
6644 (*pdb_method)->private_data = ldap_state;
6646 (*pdb_method)->free_private_data = free_private_data;
6648 return NT_STATUS_OK;
6651 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods *m)
6653 return true;
6656 /**********************************************************************
6657 Initialise the normal mode for pdb_ldap
6658 *********************************************************************/
6660 NTSTATUS pdb_ldapsam_init_common(struct pdb_methods **pdb_method,
6661 const char *location)
6663 NTSTATUS nt_status;
6664 struct ldapsam_privates *ldap_state = NULL;
6665 uint32_t alg_rid_base;
6666 char *alg_rid_base_string = NULL;
6667 LDAPMessage *result = NULL;
6668 LDAPMessage *entry = NULL;
6669 struct dom_sid ldap_domain_sid;
6670 struct dom_sid secrets_domain_sid;
6671 char *domain_sid_string = NULL;
6672 char *dn = NULL;
6673 char *uri = talloc_strdup( NULL, location );
6675 trim_char( uri, '\"', '\"' );
6676 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6678 TALLOC_FREE(uri);
6680 if (!NT_STATUS_IS_OK(nt_status)) {
6681 return nt_status;
6684 (*pdb_method)->name = "ldapsam";
6686 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6687 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6688 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6689 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6690 (*pdb_method)->search_users = ldapsam_search_users;
6691 (*pdb_method)->search_groups = ldapsam_search_groups;
6692 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6693 (*pdb_method)->is_responsible_for_wellknown =
6694 ldapsam_is_responsible_for_wellknown;
6696 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6697 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6698 (*pdb_method)->enum_group_memberships =
6699 ldapsam_enum_group_memberships;
6700 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6701 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6702 (*pdb_method)->id_to_sid = ldapsam_id_to_sid;
6704 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6705 (*pdb_method)->create_user = ldapsam_create_user;
6706 (*pdb_method)->delete_user = ldapsam_delete_user;
6707 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6708 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6709 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6710 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6711 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6715 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6716 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6718 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6720 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6721 &result,
6722 ldap_state->domain_name, True);
6724 if ( !NT_STATUS_IS_OK(nt_status) ) {
6725 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6726 "info, nor add one to the domain. "
6727 "We cannot work reliably without it.\n"));
6728 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6731 /* Given that the above might fail, everything below this must be
6732 * optional */
6734 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
6735 result);
6736 if (!entry) {
6737 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6738 "entry\n"));
6739 ldap_msgfree(result);
6740 return NT_STATUS_UNSUCCESSFUL;
6743 dn = smbldap_talloc_dn(talloc_tos(),
6744 smbldap_get_ldap(ldap_state->smbldap_state),
6745 entry);
6746 if (!dn) {
6747 ldap_msgfree(result);
6748 return NT_STATUS_UNSUCCESSFUL;
6751 ldap_state->domain_dn = smb_xstrdup(dn);
6752 TALLOC_FREE(dn);
6754 domain_sid_string = smbldap_talloc_single_attribute(
6755 smbldap_get_ldap(ldap_state->smbldap_state),
6756 entry,
6757 get_userattr_key2string(ldap_state->schema_ver,
6758 LDAP_ATTR_USER_SID),
6759 talloc_tos());
6761 if (domain_sid_string) {
6762 bool found_sid;
6763 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6764 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6765 "read as a valid SID\n", domain_sid_string));
6766 ldap_msgfree(result);
6767 TALLOC_FREE(domain_sid_string);
6768 return NT_STATUS_INVALID_PARAMETER;
6770 found_sid = PDB_secrets_fetch_domain_sid(ldap_state->domain_name,
6771 &secrets_domain_sid);
6772 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6773 &ldap_domain_sid)) {
6774 struct dom_sid_buf buf1, buf2;
6775 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6776 "%s based on pdb_ldap results %s -> %s\n",
6777 ldap_state->domain_name,
6778 dom_sid_str_buf(&secrets_domain_sid, &buf1),
6779 dom_sid_str_buf(&ldap_domain_sid, &buf2)));
6781 /* reset secrets.tdb sid */
6782 PDB_secrets_store_domain_sid(ldap_state->domain_name,
6783 &ldap_domain_sid);
6784 DEBUG(1, ("New global sam SID: %s\n",
6785 dom_sid_str_buf(get_global_sam_sid(),
6786 &buf1)));
6788 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6789 TALLOC_FREE(domain_sid_string);
6792 alg_rid_base_string = smbldap_talloc_single_attribute(
6793 smbldap_get_ldap(ldap_state->smbldap_state),
6794 entry,
6795 get_attr_key2string( dominfo_attr_list,
6796 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6797 talloc_tos());
6798 if (alg_rid_base_string) {
6799 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6800 if (alg_rid_base != algorithmic_rid_base()) {
6801 DEBUG(0, ("The value of 'algorithmic RID base' has "
6802 "changed since the LDAP\n"
6803 "database was initialised. Aborting. \n"));
6804 ldap_msgfree(result);
6805 TALLOC_FREE(alg_rid_base_string);
6806 return NT_STATUS_UNSUCCESSFUL;
6808 TALLOC_FREE(alg_rid_base_string);
6810 ldap_msgfree(result);
6812 return NT_STATUS_OK;
6815 NTSTATUS pdb_ldapsam_init(TALLOC_CTX *ctx)
6817 NTSTATUS nt_status;
6819 nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION,
6820 "ldapsam",
6821 pdb_ldapsam_init_common);
6822 if (!NT_STATUS_IS_OK(nt_status)) {
6823 return nt_status;
6826 /* Let pdb_nds register backends */
6827 pdb_nds_init(ctx);
6829 return NT_STATUS_OK;