samba-tool tests: add tests for contact management
[Samba.git] / source3 / passdb / pdb_ldap.c
blob3dbc8f9f07ab5f4918517d8357b406cdc90938bf
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 = smb_strtoul(temp,
1000 NULL,
1002 &error,
1003 SMB_STR_STANDARD);
1004 if (error != 0) {
1005 DBG_ERR("Failed to convert UID\n");
1006 goto fn_exit;
1008 have_uid = true;
1010 temp = smbldap_talloc_single_attribute(
1011 priv2ld(ldap_state),
1012 entry,
1013 "gidNumber",
1014 ctx);
1015 if (temp) {
1016 /* We've got a uid, feed the cache */
1017 unix_pw.pw_gid = smb_strtoul(temp,
1018 NULL,
1020 &error,
1021 SMB_STR_STANDARD);
1022 if (error != 0) {
1023 DBG_ERR("Failed to convert GID\n");
1024 goto fn_exit;
1026 have_gid = true;
1028 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1029 priv2ld(ldap_state),
1030 entry,
1031 "gecos",
1032 ctx);
1033 if (unix_pw.pw_gecos == NULL) {
1034 unix_pw.pw_gecos = fullname;
1036 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1037 priv2ld(ldap_state),
1038 entry,
1039 "homeDirectory",
1040 ctx);
1041 if (unix_pw.pw_dir == NULL) {
1042 unix_pw.pw_dir = discard_const_p(char, "");
1044 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1045 priv2ld(ldap_state),
1046 entry,
1047 "loginShell",
1048 ctx);
1049 if (unix_pw.pw_shell == NULL) {
1050 unix_pw.pw_shell = discard_const_p(char, "");
1053 if (have_uid && have_gid) {
1054 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1055 } else {
1056 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1059 if (sampass->unix_pw == NULL) {
1060 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1061 pdb_get_username(sampass)));
1062 goto fn_exit;
1065 id.id = sampass->unix_pw->pw_uid;
1066 id.type = ID_TYPE_UID;
1068 idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass), &id);
1070 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1071 primary_gsid = pdb_get_group_sid(sampass);
1072 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1073 id.id = sampass->unix_pw->pw_gid;
1074 id.type = ID_TYPE_GID;
1076 idmap_cache_set_sid2unixid(primary_gsid, &id);
1080 /* check the timestamp of the cache vs ldap entry */
1081 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1082 entry))) {
1083 ret = true;
1084 goto fn_exit;
1087 /* see if we have newer updates */
1088 if (!login_cache_read(sampass, &cache_entry)) {
1089 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1090 (unsigned int)pdb_get_bad_password_count(sampass),
1091 (unsigned int)pdb_get_bad_password_time(sampass)));
1092 ret = true;
1093 goto fn_exit;
1096 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1097 (unsigned int)ldap_entry_time,
1098 (unsigned int)cache_entry.entry_timestamp,
1099 (unsigned int)cache_entry.bad_password_time));
1101 if (ldap_entry_time > cache_entry.entry_timestamp) {
1102 /* cache is older than directory , so
1103 we need to delete the entry but allow the
1104 fields to be written out */
1105 login_cache_delentry(sampass);
1106 } else {
1107 /* read cache in */
1108 pdb_set_acct_ctrl(sampass,
1109 pdb_get_acct_ctrl(sampass) |
1110 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1111 PDB_SET);
1112 pdb_set_bad_password_count(sampass,
1113 cache_entry.bad_password_count,
1114 PDB_SET);
1115 pdb_set_bad_password_time(sampass,
1116 cache_entry.bad_password_time,
1117 PDB_SET);
1120 ret = true;
1122 fn_exit:
1124 TALLOC_FREE(ctx);
1125 return ret;
1128 /**********************************************************************
1129 Initialize the ldap db from a struct samu. Called on update.
1130 (Based on init_buffer_from_sam in pdb_tdb.c)
1131 *********************************************************************/
1133 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1134 LDAPMessage *existing,
1135 LDAPMod *** mods, struct samu * sampass,
1136 bool (*need_update)(const struct samu *,
1137 enum pdb_elements))
1139 char *temp = NULL;
1141 if (mods == NULL || sampass == NULL) {
1142 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1143 return False;
1146 *mods = NULL;
1149 * took out adding "objectclass: sambaAccount"
1150 * do this on a per-mod basis
1152 if (need_update(sampass, PDB_USERNAME)) {
1153 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1154 existing, mods,
1155 "uid", pdb_get_username(sampass));
1156 if (ldap_state->is_nds_ldap) {
1157 smbldap_make_mod(
1158 smbldap_get_ldap(ldap_state->smbldap_state),
1159 existing, mods,
1160 "cn", pdb_get_username(sampass));
1161 smbldap_make_mod(
1162 smbldap_get_ldap(ldap_state->smbldap_state),
1163 existing, mods,
1164 "sn", pdb_get_username(sampass));
1168 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1170 /* only update the RID if we actually need to */
1171 if (need_update(sampass, PDB_USERSID)) {
1172 struct dom_sid_buf sid_str;
1173 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1175 switch ( ldap_state->schema_ver ) {
1176 case SCHEMAVER_SAMBASAMACCOUNT:
1177 smbldap_make_mod(
1178 smbldap_get_ldap(
1179 ldap_state->smbldap_state),
1180 existing, mods,
1181 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1182 dom_sid_str_buf(user_sid, &sid_str));
1183 break;
1185 default:
1186 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1187 break;
1191 /* we don't need to store the primary group RID - so leaving it
1192 'free' to hang off the unix primary group makes life easier */
1194 if (need_update(sampass, PDB_GROUPSID)) {
1195 struct dom_sid_buf sid_str;
1196 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1198 switch ( ldap_state->schema_ver ) {
1199 case SCHEMAVER_SAMBASAMACCOUNT:
1200 smbldap_make_mod(
1201 smbldap_get_ldap(
1202 ldap_state->smbldap_state),
1203 existing, mods,
1204 get_userattr_key2string(ldap_state->schema_ver,
1205 LDAP_ATTR_PRIMARY_GROUP_SID),
1206 dom_sid_str_buf(group_sid, &sid_str));
1207 break;
1209 default:
1210 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1211 break;
1216 /* displayName, cn, and gecos should all be the same
1217 * most easily accomplished by giving them the same OID
1218 * gecos isn't set here b/c it should be handled by the
1219 * add-user script
1220 * We change displayName only and fall back to cn if
1221 * it does not exist.
1224 if (need_update(sampass, PDB_FULLNAME))
1225 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1226 existing, mods,
1227 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1228 pdb_get_fullname(sampass));
1230 if (need_update(sampass, PDB_ACCTDESC))
1231 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1232 existing, mods,
1233 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1234 pdb_get_acct_desc(sampass));
1236 if (need_update(sampass, PDB_WORKSTATIONS))
1237 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1238 existing, mods,
1239 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1240 pdb_get_workstations(sampass));
1242 if (need_update(sampass, PDB_MUNGEDDIAL))
1243 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1244 existing, mods,
1245 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1246 pdb_get_munged_dial(sampass));
1248 if (need_update(sampass, PDB_SMBHOME))
1249 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1250 existing, mods,
1251 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1252 pdb_get_homedir(sampass));
1254 if (need_update(sampass, PDB_DRIVE))
1255 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1256 existing, mods,
1257 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1258 pdb_get_dir_drive(sampass));
1260 if (need_update(sampass, PDB_LOGONSCRIPT))
1261 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1262 existing, mods,
1263 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1264 pdb_get_logon_script(sampass));
1266 if (need_update(sampass, PDB_PROFILE))
1267 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1268 existing, mods,
1269 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1270 pdb_get_profile_path(sampass));
1272 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1273 return false;
1275 if (need_update(sampass, PDB_LOGONTIME))
1276 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1277 existing, mods,
1278 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1279 SAFE_FREE(temp);
1281 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1282 return false;
1284 if (need_update(sampass, PDB_LOGOFFTIME))
1285 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1286 existing, mods,
1287 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1288 SAFE_FREE(temp);
1290 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1291 return false;
1293 if (need_update(sampass, PDB_KICKOFFTIME))
1294 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1295 existing, mods,
1296 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1297 SAFE_FREE(temp);
1299 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1300 return false;
1302 if (need_update(sampass, PDB_CANCHANGETIME))
1303 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state),
1304 existing, mods,
1305 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1306 SAFE_FREE(temp);
1308 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1309 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1311 if (need_update(sampass, PDB_LMPASSWD)) {
1312 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1313 if (lm_pw) {
1314 char pwstr[34];
1315 pdb_sethexpwd(pwstr, lm_pw,
1316 pdb_get_acct_ctrl(sampass));
1317 smbldap_make_mod(
1318 smbldap_get_ldap(
1319 ldap_state->smbldap_state),
1320 existing, mods,
1321 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1322 pwstr);
1323 } else {
1324 smbldap_make_mod(
1325 smbldap_get_ldap(
1326 ldap_state->smbldap_state),
1327 existing, mods,
1328 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1329 NULL);
1332 if (need_update(sampass, PDB_NTPASSWD)) {
1333 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1334 if (nt_pw) {
1335 char pwstr[34];
1336 pdb_sethexpwd(pwstr, nt_pw,
1337 pdb_get_acct_ctrl(sampass));
1338 smbldap_make_mod(
1339 smbldap_get_ldap(
1340 ldap_state->smbldap_state),
1341 existing, mods,
1342 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1343 pwstr);
1344 } else {
1345 smbldap_make_mod(
1346 smbldap_get_ldap(
1347 ldap_state->smbldap_state),
1348 existing, mods,
1349 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1350 NULL);
1354 if (need_update(sampass, PDB_PWHISTORY)) {
1355 char *pwstr = NULL;
1356 uint32_t pwHistLen = 0;
1357 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1359 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1360 if (!pwstr) {
1361 return false;
1363 if (pwHistLen == 0) {
1364 /* Remove any password history from the LDAP store. */
1365 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1366 pwstr[64] = '\0';
1367 } else {
1368 int i;
1369 uint32_t currHistLen = 0;
1370 const uint8_t *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1371 if (pwhist != NULL) {
1372 /* We can only store (1024-1/64 password history entries. */
1373 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1374 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1375 /* Store the salt. */
1376 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1377 /* Followed by the md5 hash of salt + md4 hash */
1378 pdb_sethexpwd(&pwstr[(i*64)+32],
1379 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1380 DEBUG(100, ("pwstr=%s\n", pwstr));
1384 smbldap_make_mod(
1385 smbldap_get_ldap(ldap_state->smbldap_state),
1386 existing, mods,
1387 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1388 pwstr);
1389 SAFE_FREE(pwstr);
1392 if (need_update(sampass, PDB_PASSLASTSET)) {
1393 if (asprintf(&temp, "%li",
1394 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1395 return false;
1397 smbldap_make_mod(
1398 smbldap_get_ldap(ldap_state->smbldap_state),
1399 existing, mods,
1400 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1401 temp);
1402 SAFE_FREE(temp);
1406 if (need_update(sampass, PDB_HOURS)) {
1407 const uint8_t *hours = pdb_get_hours(sampass);
1408 if (hours) {
1409 char hourstr[44];
1410 pdb_sethexhours(hourstr, hours);
1411 smbldap_make_mod(
1412 smbldap_get_ldap(ldap_state->smbldap_state),
1413 existing,
1414 mods,
1415 get_userattr_key2string(ldap_state->schema_ver,
1416 LDAP_ATTR_LOGON_HOURS),
1417 hourstr);
1421 if (need_update(sampass, PDB_ACCTCTRL))
1422 smbldap_make_mod(
1423 smbldap_get_ldap(ldap_state->smbldap_state),
1424 existing, mods,
1425 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1426 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1428 /* password lockout cache:
1429 - If we are now autolocking or clearing, we write to ldap
1430 - If we are clearing, we delete the cache entry
1431 - If the count is > 0, we update the cache
1433 This even means when autolocking, we cache, just in case the
1434 update doesn't work, and we have to cache the autolock flag */
1436 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1437 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1438 uint16_t badcount = pdb_get_bad_password_count(sampass);
1439 time_t badtime = pdb_get_bad_password_time(sampass);
1440 uint32_t pol;
1441 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1443 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1444 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1446 if ((badcount >= pol) || (badcount == 0)) {
1447 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1448 (unsigned int)badcount, (unsigned int)badtime));
1449 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1450 return false;
1452 smbldap_make_mod(
1453 smbldap_get_ldap(ldap_state->smbldap_state),
1454 existing, mods,
1455 get_userattr_key2string(
1456 ldap_state->schema_ver,
1457 LDAP_ATTR_BAD_PASSWORD_COUNT),
1458 temp);
1459 SAFE_FREE(temp);
1461 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1462 return false;
1464 smbldap_make_mod(
1465 smbldap_get_ldap(ldap_state->smbldap_state),
1466 existing, mods,
1467 get_userattr_key2string(
1468 ldap_state->schema_ver,
1469 LDAP_ATTR_BAD_PASSWORD_TIME),
1470 temp);
1471 SAFE_FREE(temp);
1473 if (badcount == 0) {
1474 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1475 login_cache_delentry(sampass);
1476 } else {
1477 struct login_cache cache_entry;
1479 cache_entry.entry_timestamp = time(NULL);
1480 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1481 cache_entry.bad_password_count = badcount;
1482 cache_entry.bad_password_time = badtime;
1484 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1485 login_cache_write(sampass, &cache_entry);
1489 return True;
1492 /**********************************************************************
1493 End enumeration of the LDAP password list.
1494 *********************************************************************/
1496 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1498 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1499 if (ldap_state->result) {
1500 ldap_msgfree(ldap_state->result);
1501 ldap_state->result = NULL;
1505 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1506 const char *new_attr)
1508 int i;
1510 if (new_attr == NULL) {
1511 return;
1514 for (i=0; (*attr_list)[i] != NULL; i++) {
1518 (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
1519 const char *, i+2);
1520 SMB_ASSERT((*attr_list) != NULL);
1521 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1522 (*attr_list)[i+1] = NULL;
1525 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1526 const char ***attr_list)
1528 append_attr(mem_ctx, attr_list, "uidNumber");
1529 append_attr(mem_ctx, attr_list, "gidNumber");
1530 append_attr(mem_ctx, attr_list, "homeDirectory");
1531 append_attr(mem_ctx, attr_list, "loginShell");
1532 append_attr(mem_ctx, attr_list, "gecos");
1535 /**********************************************************************
1536 Get struct samu entry from LDAP by username.
1537 *********************************************************************/
1539 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1541 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1542 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1543 LDAPMessage *result = NULL;
1544 LDAPMessage *entry = NULL;
1545 int count;
1546 const char ** attr_list;
1547 int rc;
1549 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1550 append_attr(user, &attr_list,
1551 get_userattr_key2string(ldap_state->schema_ver,
1552 LDAP_ATTR_MOD_TIMESTAMP));
1553 ldapsam_add_unix_attributes(user, &attr_list);
1554 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1555 attr_list);
1556 TALLOC_FREE( attr_list );
1558 if ( rc != LDAP_SUCCESS )
1559 return NT_STATUS_NO_SUCH_USER;
1561 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1562 result);
1564 if (count < 1) {
1565 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1566 ldap_msgfree(result);
1567 return NT_STATUS_NO_SUCH_USER;
1568 } else if (count > 1) {
1569 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1570 ldap_msgfree(result);
1571 return NT_STATUS_NO_SUCH_USER;
1574 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1575 result);
1576 if (entry) {
1577 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1578 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1579 ldap_msgfree(result);
1580 return NT_STATUS_NO_SUCH_USER;
1582 pdb_set_backend_private_data(user, result, NULL,
1583 my_methods, PDB_CHANGED);
1584 smbldap_talloc_autofree_ldapmsg(user, result);
1585 ret = NT_STATUS_OK;
1586 } else {
1587 ldap_msgfree(result);
1589 return ret;
1592 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1593 const struct dom_sid *sid, LDAPMessage **result)
1595 int rc = -1;
1596 const char ** attr_list;
1598 switch ( ldap_state->schema_ver ) {
1599 case SCHEMAVER_SAMBASAMACCOUNT: {
1600 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1601 if (tmp_ctx == NULL) {
1602 return LDAP_NO_MEMORY;
1605 attr_list = get_userattr_list(tmp_ctx,
1606 ldap_state->schema_ver);
1607 append_attr(tmp_ctx, &attr_list,
1608 get_userattr_key2string(
1609 ldap_state->schema_ver,
1610 LDAP_ATTR_MOD_TIMESTAMP));
1611 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1612 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1613 result, attr_list);
1614 TALLOC_FREE(tmp_ctx);
1616 if ( rc != LDAP_SUCCESS )
1617 return rc;
1618 break;
1621 default:
1622 DEBUG(0,("Invalid schema version specified\n"));
1623 break;
1625 return rc;
1628 /**********************************************************************
1629 Get struct samu entry from LDAP by SID.
1630 *********************************************************************/
1632 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1634 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1635 LDAPMessage *result = NULL;
1636 LDAPMessage *entry = NULL;
1637 int count;
1638 int rc;
1640 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1641 sid, &result);
1642 if (rc != LDAP_SUCCESS)
1643 return NT_STATUS_NO_SUCH_USER;
1645 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1646 result);
1648 if (count < 1) {
1649 struct dom_sid_buf buf;
1650 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1651 "count=%d\n",
1652 dom_sid_str_buf(sid, &buf),
1653 count));
1654 ldap_msgfree(result);
1655 return NT_STATUS_NO_SUCH_USER;
1656 } else if (count > 1) {
1657 struct dom_sid_buf buf;
1658 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1659 "[%s]. Failing. count=%d\n",
1660 dom_sid_str_buf(sid, &buf),
1661 count));
1662 ldap_msgfree(result);
1663 return NT_STATUS_NO_SUCH_USER;
1666 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1667 result);
1668 if (!entry) {
1669 ldap_msgfree(result);
1670 return NT_STATUS_NO_SUCH_USER;
1673 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1674 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1675 ldap_msgfree(result);
1676 return NT_STATUS_NO_SUCH_USER;
1679 pdb_set_backend_private_data(user, result, NULL,
1680 my_methods, PDB_CHANGED);
1681 smbldap_talloc_autofree_ldapmsg(user, result);
1682 return NT_STATUS_OK;
1685 /********************************************************************
1686 Do the actual modification - also change a plaintext passord if
1687 it it set.
1688 **********************************************************************/
1690 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1691 struct samu *newpwd, char *dn,
1692 LDAPMod **mods, int ldap_op,
1693 bool (*need_update)(const struct samu *, enum pdb_elements))
1695 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1696 int rc;
1698 if (!newpwd || !dn) {
1699 return NT_STATUS_INVALID_PARAMETER;
1702 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1703 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1704 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1705 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1706 BerElement *ber;
1707 struct berval *bv;
1708 char *retoid = NULL;
1709 struct berval *retdata = NULL;
1710 char *utf8_password;
1711 char *utf8_dn;
1712 size_t converted_size;
1713 int ret;
1715 if (!ldap_state->is_nds_ldap) {
1717 if (!smbldap_has_extension(
1718 smbldap_get_ldap(
1719 ldap_state->smbldap_state),
1720 LDAP_EXOP_MODIFY_PASSWD)) {
1721 DEBUG(2, ("ldap password change requested, but LDAP "
1722 "server does not support it -- ignoring\n"));
1723 return NT_STATUS_OK;
1727 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1728 pdb_get_plaintext_passwd(newpwd),
1729 &converted_size))
1731 return NT_STATUS_NO_MEMORY;
1734 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1735 TALLOC_FREE(utf8_password);
1736 return NT_STATUS_NO_MEMORY;
1739 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1740 DEBUG(0,("ber_alloc_t returns NULL\n"));
1741 TALLOC_FREE(utf8_password);
1742 TALLOC_FREE(utf8_dn);
1743 return NT_STATUS_UNSUCCESSFUL;
1746 if ((ber_printf (ber, "{") < 0) ||
1747 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1748 utf8_dn) < 0)) {
1749 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1750 "value <0\n"));
1751 ber_free(ber,1);
1752 TALLOC_FREE(utf8_dn);
1753 TALLOC_FREE(utf8_password);
1754 return NT_STATUS_UNSUCCESSFUL;
1757 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1758 ret = ber_printf(ber, "ts}",
1759 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1760 utf8_password);
1761 } else {
1762 ret = ber_printf(ber, "}");
1765 if (ret < 0) {
1766 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1767 "value <0\n"));
1768 ber_free(ber,1);
1769 TALLOC_FREE(utf8_dn);
1770 TALLOC_FREE(utf8_password);
1771 return NT_STATUS_UNSUCCESSFUL;
1774 if ((rc = ber_flatten (ber, &bv))<0) {
1775 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1776 ber_free(ber,1);
1777 TALLOC_FREE(utf8_dn);
1778 TALLOC_FREE(utf8_password);
1779 return NT_STATUS_UNSUCCESSFUL;
1782 TALLOC_FREE(utf8_dn);
1783 TALLOC_FREE(utf8_password);
1784 ber_free(ber, 1);
1786 if (!ldap_state->is_nds_ldap) {
1787 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1788 LDAP_EXOP_MODIFY_PASSWD,
1789 bv, NULL, NULL, &retoid,
1790 &retdata);
1791 } else {
1792 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1793 pdb_get_plaintext_passwd(newpwd));
1795 if (rc != LDAP_SUCCESS) {
1796 char *ld_error = NULL;
1798 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1799 DEBUG(3, ("Could not set userPassword "
1800 "attribute due to an objectClass "
1801 "violation -- ignoring\n"));
1802 ber_bvfree(bv);
1803 return NT_STATUS_OK;
1806 ldap_get_option(
1807 smbldap_get_ldap(ldap_state->smbldap_state),
1808 LDAP_OPT_ERROR_STRING,
1809 &ld_error);
1810 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1811 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1812 SAFE_FREE(ld_error);
1813 ber_bvfree(bv);
1814 #if defined(LDAP_CONSTRAINT_VIOLATION)
1815 if (rc == LDAP_CONSTRAINT_VIOLATION)
1816 return NT_STATUS_PASSWORD_RESTRICTION;
1817 #endif
1818 return NT_STATUS_UNSUCCESSFUL;
1819 } else {
1820 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1821 #ifdef DEBUG_PASSWORD
1822 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1823 #endif
1824 if (retdata)
1825 ber_bvfree(retdata);
1826 if (retoid)
1827 ldap_memfree(retoid);
1829 ber_bvfree(bv);
1832 if (!mods) {
1833 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1834 /* may be password change below however */
1835 } else {
1836 switch(ldap_op) {
1837 case LDAP_MOD_ADD:
1838 if (ldap_state->is_nds_ldap) {
1839 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1840 "objectclass",
1841 "inetOrgPerson");
1842 } else {
1843 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1844 "objectclass",
1845 LDAP_OBJ_ACCOUNT);
1847 rc = smbldap_add(ldap_state->smbldap_state,
1848 dn, mods);
1849 break;
1850 case LDAP_MOD_REPLACE:
1851 rc = smbldap_modify(ldap_state->smbldap_state,
1852 dn ,mods);
1853 break;
1854 default:
1855 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1856 ldap_op));
1857 return NT_STATUS_INVALID_PARAMETER;
1860 if (rc!=LDAP_SUCCESS) {
1861 return NT_STATUS_UNSUCCESSFUL;
1865 return NT_STATUS_OK;
1868 /**********************************************************************
1869 Delete entry from LDAP for username.
1870 *********************************************************************/
1872 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1873 struct samu * sam_acct)
1875 struct ldapsam_privates *priv =
1876 (struct ldapsam_privates *)my_methods->private_data;
1877 const char *sname;
1878 int rc;
1879 LDAPMessage *msg, *entry;
1880 NTSTATUS result = NT_STATUS_NO_MEMORY;
1881 const char **attr_list;
1882 TALLOC_CTX *mem_ctx;
1884 if (!sam_acct) {
1885 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1886 return NT_STATUS_INVALID_PARAMETER;
1889 sname = pdb_get_username(sam_acct);
1891 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1892 "LDAP.\n", sname));
1894 mem_ctx = talloc_new(NULL);
1895 if (mem_ctx == NULL) {
1896 DEBUG(0, ("talloc_new failed\n"));
1897 goto done;
1900 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1901 if (attr_list == NULL) {
1902 goto done;
1905 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1907 if ((rc != LDAP_SUCCESS) ||
1908 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1909 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1910 DEBUG(5, ("Could not find user %s\n", sname));
1911 result = NT_STATUS_NO_SUCH_USER;
1912 goto done;
1915 rc = ldapsam_delete_entry(
1916 priv, mem_ctx, entry,
1917 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1918 LDAP_OBJ_SAMBASAMACCOUNT : 0,
1919 attr_list);
1921 result = (rc == LDAP_SUCCESS) ?
1922 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1924 done:
1925 TALLOC_FREE(mem_ctx);
1926 return result;
1929 /**********************************************************************
1930 Update struct samu.
1931 *********************************************************************/
1933 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1935 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1936 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1937 int rc = 0;
1938 char *dn;
1939 LDAPMessage *result = NULL;
1940 LDAPMessage *entry = NULL;
1941 LDAPMod **mods = NULL;
1942 const char **attr_list;
1944 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1945 if (!result) {
1946 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1947 if (pdb_get_username(newpwd) == NULL) {
1948 return NT_STATUS_INVALID_PARAMETER;
1950 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1951 TALLOC_FREE( attr_list );
1952 if (rc != LDAP_SUCCESS) {
1953 return NT_STATUS_UNSUCCESSFUL;
1955 pdb_set_backend_private_data(newpwd, result, NULL,
1956 my_methods, PDB_CHANGED);
1957 smbldap_talloc_autofree_ldapmsg(newpwd, result);
1960 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
1961 result) == 0) {
1962 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1963 return NT_STATUS_UNSUCCESSFUL;
1966 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
1967 result);
1968 dn = smbldap_talloc_dn(talloc_tos(),
1969 smbldap_get_ldap(ldap_state->smbldap_state),
1970 entry);
1971 if (!dn) {
1972 return NT_STATUS_UNSUCCESSFUL;
1975 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1977 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1978 pdb_element_is_changed)) {
1979 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1980 TALLOC_FREE(dn);
1981 if (mods != NULL)
1982 ldap_mods_free(mods,True);
1983 return NT_STATUS_UNSUCCESSFUL;
1986 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1987 && (mods == NULL)) {
1988 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1989 pdb_get_username(newpwd)));
1990 TALLOC_FREE(dn);
1991 return NT_STATUS_OK;
1994 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
1996 if (mods != NULL) {
1997 ldap_mods_free(mods,True);
2000 TALLOC_FREE(dn);
2003 * We need to set the backend private data to NULL here. For example
2004 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2005 * same one, and with the explicit delete / add logic for attribute
2006 * values the second time we would use the wrong "old" value which
2007 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2008 * the update.
2009 * The existing LDAPMessage is still being auto-freed by the
2010 * destructor.
2012 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2013 PDB_CHANGED);
2015 if (!NT_STATUS_IS_OK(ret)) {
2016 return ret;
2019 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2020 pdb_get_username(newpwd)));
2021 return NT_STATUS_OK;
2024 /***************************************************************************
2025 Renames a struct samu
2026 - The "rename user script" has full responsibility for changing everything
2027 ***************************************************************************/
2029 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2030 TALLOC_CTX *tmp_ctx,
2031 uint32_t group_rid,
2032 uint32_t member_rid);
2034 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2035 TALLOC_CTX *mem_ctx,
2036 struct samu *user,
2037 struct dom_sid **pp_sids,
2038 gid_t **pp_gids,
2039 uint32_t *p_num_groups);
2041 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2042 struct samu *old_acct,
2043 const char *newname)
2045 const char *oldname;
2046 int rc;
2047 char *rename_script = NULL;
2048 fstring oldname_lower, newname_lower;
2050 if (!old_acct) {
2051 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2052 return NT_STATUS_INVALID_PARAMETER;
2054 if (!newname) {
2055 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2056 return NT_STATUS_INVALID_PARAMETER;
2059 oldname = pdb_get_username(old_acct);
2061 /* rename the posix user */
2062 rename_script = lp_rename_user_script(talloc_tos());
2063 if (rename_script == NULL) {
2064 return NT_STATUS_NO_MEMORY;
2067 if (!(*rename_script)) {
2068 TALLOC_FREE(rename_script);
2069 return NT_STATUS_ACCESS_DENIED;
2072 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2073 oldname, newname));
2075 /* We have to allow the account name to end with a '$'.
2076 Also, follow the semantics in _samr_create_user() and lower case the
2077 posix name but preserve the case in passdb */
2079 fstrcpy( oldname_lower, oldname );
2080 if (!strlower_m( oldname_lower )) {
2081 return NT_STATUS_INVALID_PARAMETER;
2083 fstrcpy( newname_lower, newname );
2084 if (!strlower_m( newname_lower )) {
2085 return NT_STATUS_INVALID_PARAMETER;
2088 rename_script = realloc_string_sub2(rename_script,
2089 "%unew",
2090 newname_lower,
2091 true,
2092 true);
2093 if (!rename_script) {
2094 return NT_STATUS_NO_MEMORY;
2096 rename_script = realloc_string_sub2(rename_script,
2097 "%uold",
2098 oldname_lower,
2099 true,
2100 true);
2101 rc = smbrun(rename_script, NULL, NULL);
2103 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2104 rename_script, rc));
2106 TALLOC_FREE(rename_script);
2108 if (rc == 0) {
2109 smb_nscd_flush_user_cache();
2112 if (rc)
2113 return NT_STATUS_UNSUCCESSFUL;
2115 return NT_STATUS_OK;
2118 /**********************************************************************
2119 Add struct samu to LDAP.
2120 *********************************************************************/
2122 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2124 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2125 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2126 int rc;
2127 LDAPMessage *result = NULL;
2128 LDAPMessage *entry = NULL;
2129 LDAPMod **mods = NULL;
2130 int ldap_op = LDAP_MOD_REPLACE;
2131 uint32_t num_result;
2132 const char **attr_list;
2133 char *escape_user = NULL;
2134 const char *username = pdb_get_username(newpwd);
2135 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2136 char *filter = NULL;
2137 char *dn = NULL;
2138 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2139 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2141 if (!ctx) {
2142 return NT_STATUS_NO_MEMORY;
2145 if (!username || !*username) {
2146 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2147 status = NT_STATUS_INVALID_PARAMETER;
2148 goto fn_exit;
2151 /* free this list after the second search or in case we exit on failure */
2152 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2154 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2156 if (rc != LDAP_SUCCESS) {
2157 goto fn_exit;
2160 if (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
2161 result) != 0) {
2162 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2163 username));
2164 goto fn_exit;
2166 ldap_msgfree(result);
2167 result = NULL;
2169 if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
2170 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2171 sid, &result);
2172 if (rc == LDAP_SUCCESS) {
2173 if (ldap_count_entries(
2174 smbldap_get_ldap(
2175 ldap_state->smbldap_state),
2176 result) != 0) {
2177 struct dom_sid_buf buf;
2178 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2179 "already in the base, with samba "
2180 "attributes\n",
2181 dom_sid_str_buf(sid, &buf)));
2182 goto fn_exit;
2184 ldap_msgfree(result);
2185 result = NULL;
2189 /* does the entry already exist but without a samba attributes?
2190 we need to return the samba attributes here */
2192 escape_user = escape_ldap_string(talloc_tos(), username);
2193 filter = talloc_strdup(attr_list, "(uid=%u)");
2194 if (!filter) {
2195 status = NT_STATUS_NO_MEMORY;
2196 goto fn_exit;
2198 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2199 TALLOC_FREE(escape_user);
2200 if (!filter) {
2201 status = NT_STATUS_NO_MEMORY;
2202 goto fn_exit;
2205 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2206 filter, attr_list, &result);
2207 if ( rc != LDAP_SUCCESS ) {
2208 goto fn_exit;
2211 num_result = ldap_count_entries(
2212 smbldap_get_ldap(ldap_state->smbldap_state), result);
2214 if (num_result > 1) {
2215 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2216 goto fn_exit;
2219 /* Check if we need to update an existing entry */
2220 if (num_result == 1) {
2221 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2222 ldap_op = LDAP_MOD_REPLACE;
2223 entry = ldap_first_entry(
2224 smbldap_get_ldap(ldap_state->smbldap_state), result);
2225 dn = smbldap_talloc_dn(
2226 ctx, smbldap_get_ldap(ldap_state->smbldap_state),
2227 entry);
2228 if (!dn) {
2229 status = NT_STATUS_NO_MEMORY;
2230 goto fn_exit;
2233 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2235 struct dom_sid_buf buf;
2237 /* There might be a SID for this account already - say an idmap entry */
2239 filter = talloc_asprintf(ctx,
2240 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2241 get_userattr_key2string(ldap_state->schema_ver,
2242 LDAP_ATTR_USER_SID),
2243 dom_sid_str_buf(sid, &buf),
2244 LDAP_OBJ_IDMAP_ENTRY,
2245 LDAP_OBJ_SID_ENTRY);
2246 if (!filter) {
2247 status = NT_STATUS_NO_MEMORY;
2248 goto fn_exit;
2251 /* free old result before doing a new search */
2252 if (result != NULL) {
2253 ldap_msgfree(result);
2254 result = NULL;
2256 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2257 filter, attr_list, &result);
2259 if ( rc != LDAP_SUCCESS ) {
2260 goto fn_exit;
2263 num_result = ldap_count_entries(
2264 smbldap_get_ldap(ldap_state->smbldap_state), result);
2266 if (num_result > 1) {
2267 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2268 goto fn_exit;
2271 /* Check if we need to update an existing entry */
2272 if (num_result == 1) {
2274 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2275 ldap_op = LDAP_MOD_REPLACE;
2276 entry = ldap_first_entry (
2277 smbldap_get_ldap(ldap_state->smbldap_state),
2278 result);
2279 dn = smbldap_talloc_dn (
2280 ctx,
2281 smbldap_get_ldap(ldap_state->smbldap_state),
2282 entry);
2283 if (!dn) {
2284 status = NT_STATUS_NO_MEMORY;
2285 goto fn_exit;
2290 if (num_result == 0) {
2291 char *escape_username;
2292 /* Check if we need to add an entry */
2293 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2294 ldap_op = LDAP_MOD_ADD;
2296 escape_username = escape_rdn_val_string_alloc(username);
2297 if (!escape_username) {
2298 status = NT_STATUS_NO_MEMORY;
2299 goto fn_exit;
2302 if (username[strlen(username)-1] == '$') {
2303 dn = talloc_asprintf(ctx,
2304 "uid=%s,%s",
2305 escape_username,
2306 lp_ldap_machine_suffix(talloc_tos()));
2307 } else {
2308 dn = talloc_asprintf(ctx,
2309 "uid=%s,%s",
2310 escape_username,
2311 lp_ldap_user_suffix(talloc_tos()));
2314 SAFE_FREE(escape_username);
2315 if (!dn) {
2316 status = NT_STATUS_NO_MEMORY;
2317 goto fn_exit;
2321 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2322 pdb_element_is_set_or_changed)) {
2323 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2324 if (mods != NULL) {
2325 ldap_mods_free(mods, true);
2327 goto fn_exit;
2330 if (mods == NULL) {
2331 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2332 goto fn_exit;
2334 switch ( ldap_state->schema_ver ) {
2335 case SCHEMAVER_SAMBASAMACCOUNT:
2336 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2337 break;
2338 default:
2339 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2340 break;
2343 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
2344 if (!NT_STATUS_IS_OK(ret)) {
2345 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2346 pdb_get_username(newpwd),dn));
2347 ldap_mods_free(mods, true);
2348 goto fn_exit;
2351 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2352 ldap_mods_free(mods, true);
2354 status = NT_STATUS_OK;
2356 fn_exit:
2358 TALLOC_FREE(ctx);
2359 if (result) {
2360 ldap_msgfree(result);
2362 return status;
2365 /**********************************************************************
2366 *********************************************************************/
2368 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2369 const char *filter,
2370 LDAPMessage ** result)
2372 int scope = LDAP_SCOPE_SUBTREE;
2373 int rc;
2374 const char **attr_list;
2376 attr_list = get_attr_list(NULL, groupmap_attr_list);
2377 rc = smbldap_search(ldap_state->smbldap_state,
2378 lp_ldap_suffix (talloc_tos()), scope,
2379 filter, attr_list, 0, result);
2380 TALLOC_FREE(attr_list);
2382 return rc;
2385 /**********************************************************************
2386 *********************************************************************/
2388 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2389 GROUP_MAP *map, LDAPMessage *entry)
2391 char *temp = NULL;
2392 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2394 if (ldap_state == NULL || map == NULL || entry == NULL ||
2395 smbldap_get_ldap(ldap_state->smbldap_state) == NULL) {
2396 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2397 TALLOC_FREE(ctx);
2398 return false;
2401 temp = smbldap_talloc_single_attribute(
2402 smbldap_get_ldap(ldap_state->smbldap_state),
2403 entry,
2404 get_attr_key2string(groupmap_attr_list,
2405 LDAP_ATTR_GIDNUMBER),
2406 ctx);
2407 if (!temp) {
2408 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2409 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2410 TALLOC_FREE(ctx);
2411 return false;
2413 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2415 map->gid = (gid_t)atol(temp);
2417 TALLOC_FREE(temp);
2418 temp = smbldap_talloc_single_attribute(
2419 smbldap_get_ldap(ldap_state->smbldap_state),
2420 entry,
2421 get_attr_key2string(groupmap_attr_list,
2422 LDAP_ATTR_GROUP_SID),
2423 ctx);
2424 if (!temp) {
2425 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2426 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2427 TALLOC_FREE(ctx);
2428 return false;
2431 if (!string_to_sid(&map->sid, temp)) {
2432 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2433 TALLOC_FREE(ctx);
2434 return false;
2437 TALLOC_FREE(temp);
2438 temp = smbldap_talloc_single_attribute(
2439 smbldap_get_ldap(ldap_state->smbldap_state),
2440 entry,
2441 get_attr_key2string(groupmap_attr_list,
2442 LDAP_ATTR_GROUP_TYPE),
2443 ctx);
2444 if (!temp) {
2445 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2446 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2447 TALLOC_FREE(ctx);
2448 return false;
2450 map->sid_name_use = (enum lsa_SidType)atol(temp);
2452 if ((map->sid_name_use < SID_NAME_USER) ||
2453 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2454 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2455 TALLOC_FREE(ctx);
2456 return false;
2459 TALLOC_FREE(temp);
2460 temp = smbldap_talloc_single_attribute(
2461 smbldap_get_ldap(ldap_state->smbldap_state),
2462 entry,
2463 get_attr_key2string(groupmap_attr_list,
2464 LDAP_ATTR_DISPLAY_NAME),
2465 ctx);
2466 if (!temp) {
2467 temp = smbldap_talloc_single_attribute(
2468 smbldap_get_ldap(ldap_state->smbldap_state),
2469 entry,
2470 get_attr_key2string(groupmap_attr_list,
2471 LDAP_ATTR_CN),
2472 ctx);
2473 if (!temp) {
2474 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2475 for gidNumber(%lu)\n",(unsigned long)map->gid));
2476 TALLOC_FREE(ctx);
2477 return false;
2480 map->nt_name = talloc_strdup(map, temp);
2481 if (!map->nt_name) {
2482 TALLOC_FREE(ctx);
2483 return false;
2486 TALLOC_FREE(temp);
2487 temp = smbldap_talloc_single_attribute(
2488 smbldap_get_ldap(ldap_state->smbldap_state),
2489 entry,
2490 get_attr_key2string(groupmap_attr_list,
2491 LDAP_ATTR_DESC),
2492 ctx);
2493 if (!temp) {
2494 temp = talloc_strdup(ctx, "");
2495 if (!temp) {
2496 TALLOC_FREE(ctx);
2497 return false;
2500 map->comment = talloc_strdup(map, temp);
2501 if (!map->comment) {
2502 TALLOC_FREE(ctx);
2503 return false;
2506 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2507 struct unixid id;
2508 id.id = map->gid;
2509 id.type = ID_TYPE_GID;
2511 idmap_cache_set_sid2unixid(&map->sid, &id);
2514 TALLOC_FREE(ctx);
2515 return true;
2518 /**********************************************************************
2519 *********************************************************************/
2521 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2522 const char *filter,
2523 GROUP_MAP *map)
2525 struct ldapsam_privates *ldap_state =
2526 (struct ldapsam_privates *)methods->private_data;
2527 LDAPMessage *result = NULL;
2528 LDAPMessage *entry = NULL;
2529 int count;
2531 if (ldapsam_search_one_group(ldap_state, filter, &result)
2532 != LDAP_SUCCESS) {
2533 return NT_STATUS_NO_SUCH_GROUP;
2536 count = ldap_count_entries(priv2ld(ldap_state), result);
2538 if (count < 1) {
2539 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2540 "%s\n", filter));
2541 ldap_msgfree(result);
2542 return NT_STATUS_NO_SUCH_GROUP;
2545 if (count > 1) {
2546 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2547 "count=%d\n", filter, count));
2548 ldap_msgfree(result);
2549 return NT_STATUS_NO_SUCH_GROUP;
2552 entry = ldap_first_entry(priv2ld(ldap_state), result);
2554 if (!entry) {
2555 ldap_msgfree(result);
2556 return NT_STATUS_UNSUCCESSFUL;
2559 if (!init_group_from_ldap(ldap_state, map, entry)) {
2560 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2561 "group filter %s\n", filter));
2562 ldap_msgfree(result);
2563 return NT_STATUS_NO_SUCH_GROUP;
2566 ldap_msgfree(result);
2567 return NT_STATUS_OK;
2570 /**********************************************************************
2571 *********************************************************************/
2573 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2574 struct dom_sid sid)
2576 char *filter = NULL;
2577 NTSTATUS status;
2578 struct dom_sid_buf tmp;
2580 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2581 LDAP_OBJ_GROUPMAP,
2582 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2583 dom_sid_str_buf(&sid, &tmp)) < 0) {
2584 return NT_STATUS_NO_MEMORY;
2587 status = ldapsam_getgroup(methods, filter, map);
2588 SAFE_FREE(filter);
2589 return status;
2592 /**********************************************************************
2593 *********************************************************************/
2595 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2596 gid_t gid)
2598 char *filter = NULL;
2599 NTSTATUS status;
2601 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2602 LDAP_OBJ_GROUPMAP,
2603 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2604 (unsigned long)gid) < 0) {
2605 return NT_STATUS_NO_MEMORY;
2608 status = ldapsam_getgroup(methods, filter, map);
2609 SAFE_FREE(filter);
2610 return status;
2613 /**********************************************************************
2614 *********************************************************************/
2616 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2617 const char *name)
2619 char *filter = NULL;
2620 char *escape_name = escape_ldap_string(talloc_tos(), name);
2621 NTSTATUS status;
2623 if (!escape_name) {
2624 return NT_STATUS_NO_MEMORY;
2627 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2628 LDAP_OBJ_GROUPMAP,
2629 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2630 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2631 escape_name) < 0) {
2632 TALLOC_FREE(escape_name);
2633 return NT_STATUS_NO_MEMORY;
2636 TALLOC_FREE(escape_name);
2637 status = ldapsam_getgroup(methods, filter, map);
2638 SAFE_FREE(filter);
2639 return status;
2642 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2643 LDAPMessage *entry,
2644 const struct dom_sid *domain_sid,
2645 uint32_t *rid)
2647 fstring str;
2648 struct dom_sid sid;
2650 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2651 str, sizeof(str)-1)) {
2652 DEBUG(10, ("Could not find sambaSID attribute\n"));
2653 return False;
2656 if (!string_to_sid(&sid, str)) {
2657 DEBUG(10, ("Could not convert string %s to sid\n", str));
2658 return False;
2661 if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
2662 struct dom_sid_buf buf;
2663 DEBUG(10, ("SID %s is not in expected domain %s\n",
2664 str,
2665 dom_sid_str_buf(domain_sid, &buf)));
2666 return False;
2669 if (!sid_peek_rid(&sid, rid)) {
2670 DEBUG(10, ("Could not peek into RID\n"));
2671 return False;
2674 return True;
2677 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2678 TALLOC_CTX *mem_ctx,
2679 const struct dom_sid *group,
2680 uint32_t **pp_member_rids,
2681 size_t *p_num_members)
2683 struct ldapsam_privates *ldap_state =
2684 (struct ldapsam_privates *)methods->private_data;
2685 struct smbldap_state *conn = ldap_state->smbldap_state;
2686 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2687 const char *sid_attrs[] = { "sambaSID", NULL };
2688 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2689 LDAPMessage *result = NULL;
2690 LDAPMessage *entry;
2691 char *filter;
2692 char **values = NULL;
2693 char **memberuid;
2694 char *gidstr;
2695 int rc, count;
2696 struct dom_sid_buf buf;
2698 *pp_member_rids = NULL;
2699 *p_num_members = 0;
2701 filter = talloc_asprintf(mem_ctx,
2702 "(&(objectClass=%s)"
2703 "(objectClass=%s)"
2704 "(sambaSID=%s))",
2705 LDAP_OBJ_POSIXGROUP,
2706 LDAP_OBJ_GROUPMAP,
2707 dom_sid_str_buf(group, &buf));
2708 if (filter == NULL) {
2709 ret = NT_STATUS_NO_MEMORY;
2710 goto done;
2713 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2714 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2715 &result);
2717 if (rc != LDAP_SUCCESS)
2718 goto done;
2720 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2722 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2724 if (count > 1) {
2725 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2726 dom_sid_str_buf(group, &buf)));
2727 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2728 goto done;
2731 if (count == 0) {
2732 ret = NT_STATUS_NO_SUCH_GROUP;
2733 goto done;
2736 entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2737 if (entry == NULL)
2738 goto done;
2740 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2741 if (!gidstr) {
2742 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2743 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2744 goto done;
2747 values = ldap_get_values(smbldap_get_ldap(conn), entry, "memberUid");
2749 if ((values != NULL) && (values[0] != NULL)) {
2751 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2752 if (filter == NULL) {
2753 ret = NT_STATUS_NO_MEMORY;
2754 goto done;
2757 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2758 char *escape_memberuid;
2760 escape_memberuid = escape_ldap_string(talloc_tos(),
2761 *memberuid);
2762 if (escape_memberuid == NULL) {
2763 ret = NT_STATUS_NO_MEMORY;
2764 goto done;
2767 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2768 TALLOC_FREE(escape_memberuid);
2769 if (filter == NULL) {
2770 ret = NT_STATUS_NO_MEMORY;
2771 goto done;
2775 filter = talloc_asprintf_append_buffer(filter, "))");
2776 if (filter == NULL) {
2777 ret = NT_STATUS_NO_MEMORY;
2778 goto done;
2781 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2782 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2783 &result);
2785 if (rc != LDAP_SUCCESS)
2786 goto done;
2788 count = ldap_count_entries(smbldap_get_ldap(conn), result);
2789 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2791 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2793 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2794 entry != NULL;
2795 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2797 char *sidstr;
2798 struct dom_sid sid;
2799 uint32_t rid;
2801 sidstr = smbldap_talloc_single_attribute(
2802 smbldap_get_ldap(conn), entry, "sambaSID",
2803 mem_ctx);
2804 if (!sidstr) {
2805 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2806 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2807 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2808 goto done;
2811 if (!string_to_sid(&sid, sidstr))
2812 goto done;
2814 if (!sid_check_is_in_our_sam(&sid)) {
2815 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2816 "in our domain\n"));
2817 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2818 goto done;
2821 sid_peek_rid(&sid, &rid);
2823 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2824 p_num_members)) {
2825 ret = NT_STATUS_NO_MEMORY;
2826 goto done;
2831 filter = talloc_asprintf(mem_ctx,
2832 "(&(objectClass=%s)"
2833 "(gidNumber=%s))",
2834 LDAP_OBJ_SAMBASAMACCOUNT,
2835 gidstr);
2837 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2838 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2839 &result);
2841 if (rc != LDAP_SUCCESS)
2842 goto done;
2844 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2846 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
2847 entry != NULL;
2848 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
2850 uint32_t rid;
2852 if (!ldapsam_extract_rid_from_entry(smbldap_get_ldap(conn),
2853 entry,
2854 get_global_sam_sid(),
2855 &rid)) {
2856 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2857 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2858 goto done;
2861 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2862 p_num_members)) {
2863 ret = NT_STATUS_NO_MEMORY;
2864 goto done;
2868 ret = NT_STATUS_OK;
2870 done:
2872 if (values)
2873 ldap_value_free(values);
2875 return ret;
2878 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2879 TALLOC_CTX *mem_ctx,
2880 struct samu *user,
2881 struct dom_sid **pp_sids,
2882 gid_t **pp_gids,
2883 uint32_t *p_num_groups)
2885 struct ldapsam_privates *ldap_state =
2886 (struct ldapsam_privates *)methods->private_data;
2887 struct smbldap_state *conn = ldap_state->smbldap_state;
2888 char *filter;
2889 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2890 char *escape_name;
2891 int rc, count;
2892 LDAPMessage *result = NULL;
2893 LDAPMessage *entry;
2894 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2895 uint32_t num_sids;
2896 uint32_t num_gids;
2897 char *gidstr;
2898 gid_t primary_gid = -1;
2899 int error = 0;
2901 *pp_sids = NULL;
2902 num_sids = 0;
2904 if (pdb_get_username(user) == NULL) {
2905 return NT_STATUS_INVALID_PARAMETER;
2908 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2909 if (escape_name == NULL)
2910 return NT_STATUS_NO_MEMORY;
2912 if (user->unix_pw) {
2913 primary_gid = user->unix_pw->pw_gid;
2914 } else {
2915 /* retrieve the users primary gid */
2916 filter = talloc_asprintf(mem_ctx,
2917 "(&(objectClass=%s)(uid=%s))",
2918 LDAP_OBJ_SAMBASAMACCOUNT,
2919 escape_name);
2920 if (filter == NULL) {
2921 ret = NT_STATUS_NO_MEMORY;
2922 goto done;
2925 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2926 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2928 if (rc != LDAP_SUCCESS)
2929 goto done;
2931 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2933 count = ldap_count_entries(priv2ld(ldap_state), result);
2935 switch (count) {
2936 case 0:
2937 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2938 ret = NT_STATUS_NO_SUCH_USER;
2939 goto done;
2940 case 1:
2941 entry = ldap_first_entry(priv2ld(ldap_state), result);
2943 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2944 if (!gidstr) {
2945 DEBUG (1, ("Unable to find the member's gid!\n"));
2946 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2947 goto done;
2949 primary_gid = smb_strtoul(gidstr,
2950 NULL,
2952 &error,
2953 SMB_STR_STANDARD);
2954 if (error != 0) {
2955 DBG_ERR("Failed to convert GID\n");
2956 goto done;
2958 break;
2959 default:
2960 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2961 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2962 goto done;
2966 filter = talloc_asprintf(mem_ctx,
2967 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2968 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2969 if (filter == NULL) {
2970 ret = NT_STATUS_NO_MEMORY;
2971 goto done;
2974 rc = smbldap_search(conn, lp_ldap_suffix(talloc_tos()),
2975 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2977 if (rc != LDAP_SUCCESS)
2978 goto done;
2980 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
2982 num_gids = 0;
2983 *pp_gids = NULL;
2985 num_sids = 0;
2986 *pp_sids = NULL;
2988 /* We need to add the primary group as the first gid/sid */
2990 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2991 ret = NT_STATUS_NO_MEMORY;
2992 goto done;
2995 /* This sid will be replaced later */
2997 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2998 &num_sids);
2999 if (!NT_STATUS_IS_OK(ret)) {
3000 goto done;
3003 for (entry = ldap_first_entry(smbldap_get_ldap(conn), result);
3004 entry != NULL;
3005 entry = ldap_next_entry(smbldap_get_ldap(conn), entry))
3007 fstring str;
3008 struct dom_sid sid;
3009 gid_t gid;
3011 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3012 entry, "sambaSID",
3013 str, sizeof(str)-1))
3014 continue;
3016 if (!string_to_sid(&sid, str))
3017 goto done;
3019 if (!smbldap_get_single_attribute(smbldap_get_ldap(conn),
3020 entry, "gidNumber",
3021 str, sizeof(str)-1))
3022 continue;
3024 gid = smb_strtoul(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
3026 if (error != 0) {
3027 goto done;
3030 if (gid == primary_gid) {
3031 sid_copy(&(*pp_sids)[0], &sid);
3032 } else {
3033 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3034 &num_gids)) {
3035 ret = NT_STATUS_NO_MEMORY;
3036 goto done;
3038 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3039 &num_sids);
3040 if (!NT_STATUS_IS_OK(ret)) {
3041 goto done;
3046 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3047 DEBUG(3, ("primary group of [%s] not found\n",
3048 pdb_get_username(user)));
3049 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
3050 goto done;
3053 *p_num_groups = num_sids;
3055 ret = NT_STATUS_OK;
3057 done:
3059 TALLOC_FREE(escape_name);
3060 return ret;
3063 /**********************************************************************
3064 * Augment a posixGroup object with a sambaGroupMapping domgroup
3065 *********************************************************************/
3067 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3068 struct ldapsam_privates *ldap_state,
3069 GROUP_MAP *map)
3071 const char *filter, *dn;
3072 LDAPMessage *msg, *entry;
3073 LDAPMod **mods;
3074 struct dom_sid_buf buf;
3075 int rc;
3077 filter = talloc_asprintf(mem_ctx,
3078 "(&(objectClass=%s)(gidNumber=%u))",
3079 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3080 if (filter == NULL) {
3081 return NT_STATUS_NO_MEMORY;
3084 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3085 get_attr_list(mem_ctx, groupmap_attr_list),
3086 &msg);
3087 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3089 if ((rc != LDAP_SUCCESS) ||
3090 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3091 msg) != 1) ||
3092 ((entry = ldap_first_entry(
3093 smbldap_get_ldap(ldap_state->smbldap_state),
3094 msg)) == NULL)) {
3095 return NT_STATUS_NO_SUCH_GROUP;
3098 dn = smbldap_talloc_dn(mem_ctx,
3099 smbldap_get_ldap(ldap_state->smbldap_state),
3100 entry);
3101 if (dn == NULL) {
3102 return NT_STATUS_NO_MEMORY;
3105 mods = NULL;
3106 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3107 LDAP_OBJ_GROUPMAP);
3108 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3109 &mods, "sambaSid",
3110 dom_sid_str_buf(&map->sid, &buf));
3111 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3112 &mods, "sambaGroupType",
3113 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3114 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3115 &mods, "displayName",
3116 map->nt_name);
3117 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3118 &mods, "description",
3119 map->comment);
3120 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3122 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3123 if (rc != LDAP_SUCCESS) {
3124 return NT_STATUS_ACCESS_DENIED;
3127 return NT_STATUS_OK;
3130 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3131 GROUP_MAP *map)
3133 struct ldapsam_privates *ldap_state =
3134 (struct ldapsam_privates *)methods->private_data;
3135 LDAPMessage *msg = NULL;
3136 LDAPMod **mods = NULL;
3137 const char *attrs[] = { NULL };
3138 char *filter;
3140 char *dn;
3141 TALLOC_CTX *mem_ctx;
3142 NTSTATUS result;
3144 struct dom_sid sid;
3145 struct dom_sid_buf buf;
3146 struct unixid id;
3148 int rc;
3150 mem_ctx = talloc_new(NULL);
3151 if (mem_ctx == NULL) {
3152 DEBUG(0, ("talloc_new failed\n"));
3153 return NT_STATUS_NO_MEMORY;
3156 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3157 dom_sid_str_buf(&map->sid, &buf));
3158 if (filter == NULL) {
3159 result = NT_STATUS_NO_MEMORY;
3160 goto done;
3163 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3164 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3165 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3167 if ((rc == LDAP_SUCCESS) &&
3168 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3169 msg) > 0)) {
3171 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3172 "group mapping entry\n",
3173 dom_sid_str_buf(&map->sid, &buf)));
3174 result = NT_STATUS_GROUP_EXISTS;
3175 goto done;
3178 switch (map->sid_name_use) {
3180 case SID_NAME_DOM_GRP:
3181 /* To map a domain group we need to have a posix group
3182 to attach to. */
3183 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3184 goto done;
3185 break;
3187 case SID_NAME_ALIAS:
3188 if (!sid_check_is_in_our_sam(&map->sid)
3189 && !sid_check_is_in_builtin(&map->sid) )
3191 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3192 dom_sid_str_buf(&map->sid, &buf)));
3193 result = NT_STATUS_INVALID_PARAMETER;
3194 goto done;
3196 break;
3198 default:
3199 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3200 sid_type_lookup(map->sid_name_use)));
3201 result = NT_STATUS_INVALID_PARAMETER;
3202 goto done;
3205 /* Domain groups have been mapped in a separate routine, we have to
3206 * create an alias now */
3208 if (map->gid == -1) {
3209 DEBUG(10, ("Refusing to map gid==-1\n"));
3210 result = NT_STATUS_INVALID_PARAMETER;
3211 goto done;
3214 id.id = map->gid;
3215 id.type = ID_TYPE_GID;
3217 if (pdb_id_to_sid(&id, &sid)) {
3218 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3219 "add\n",
3220 (unsigned int)map->gid,
3221 dom_sid_str_buf(&sid, &buf)));
3222 result = NT_STATUS_GROUP_EXISTS;
3223 goto done;
3226 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3227 * the best we can get out of LDAP. */
3229 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3230 dom_sid_str_buf(&map->sid, &buf),
3231 lp_ldap_group_suffix(talloc_tos()));
3232 if (dn == NULL) {
3233 result = NT_STATUS_NO_MEMORY;
3234 goto done;
3237 mods = NULL;
3239 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3240 &mods, "objectClass", LDAP_OBJ_SID_ENTRY);
3241 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3242 &mods, "objectClass", LDAP_OBJ_GROUPMAP);
3243 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3244 &mods, "sambaSid",
3245 dom_sid_str_buf(&map->sid, &buf));
3246 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3247 &mods, "sambaGroupType",
3248 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3249 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3250 &mods, "displayName",
3251 map->nt_name);
3252 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3253 &mods, "description",
3254 map->comment);
3255 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), NULL,
3256 &mods, "gidNumber",
3257 talloc_asprintf(mem_ctx, "%u",
3258 (unsigned int)map->gid));
3259 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3261 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3263 result = (rc == LDAP_SUCCESS) ?
3264 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3266 done:
3267 TALLOC_FREE(mem_ctx);
3268 return result;
3271 /**********************************************************************
3272 * Update a group mapping entry. We're quite strict about what can be changed:
3273 * Only the description and displayname may be changed. It simply does not
3274 * make any sense to change the SID, gid or the type in a mapping.
3275 *********************************************************************/
3277 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3278 GROUP_MAP *map)
3280 struct ldapsam_privates *ldap_state =
3281 (struct ldapsam_privates *)methods->private_data;
3282 int rc;
3283 const char *filter, *dn;
3284 LDAPMessage *msg = NULL;
3285 LDAPMessage *entry = NULL;
3286 LDAPMod **mods = NULL;
3287 TALLOC_CTX *mem_ctx;
3288 NTSTATUS result;
3289 struct dom_sid_buf buf;
3291 mem_ctx = talloc_new(NULL);
3292 if (mem_ctx == NULL) {
3293 DEBUG(0, ("talloc_new failed\n"));
3294 return NT_STATUS_NO_MEMORY;
3297 /* Make 100% sure that sid, gid and type are not changed by looking up
3298 * exactly the values we're given in LDAP. */
3300 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3301 "(sambaSid=%s)(gidNumber=%u)"
3302 "(sambaGroupType=%d))",
3303 LDAP_OBJ_GROUPMAP,
3304 dom_sid_str_buf(&map->sid, &buf),
3305 (unsigned int)map->gid, map->sid_name_use);
3306 if (filter == NULL) {
3307 result = NT_STATUS_NO_MEMORY;
3308 goto done;
3311 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3312 get_attr_list(mem_ctx, groupmap_attr_list),
3313 &msg);
3314 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3316 if ((rc != LDAP_SUCCESS) ||
3317 (ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3318 msg) != 1) ||
3319 ((entry = ldap_first_entry(
3320 smbldap_get_ldap(ldap_state->smbldap_state),
3321 msg)) == NULL)) {
3322 result = NT_STATUS_NO_SUCH_GROUP;
3323 goto done;
3326 dn = smbldap_talloc_dn(
3327 mem_ctx, smbldap_get_ldap(ldap_state->smbldap_state), entry);
3329 if (dn == NULL) {
3330 result = NT_STATUS_NO_MEMORY;
3331 goto done;
3334 mods = NULL;
3335 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3336 &mods, "displayName", map->nt_name);
3337 smbldap_make_mod(smbldap_get_ldap(ldap_state->smbldap_state), entry,
3338 &mods, "description", map->comment);
3339 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
3341 if (mods == NULL) {
3342 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3343 "nothing to do\n"));
3344 result = NT_STATUS_OK;
3345 goto done;
3348 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3350 if (rc != LDAP_SUCCESS) {
3351 result = NT_STATUS_ACCESS_DENIED;
3352 goto done;
3355 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3356 "group %lu in LDAP\n", (unsigned long)map->gid));
3358 result = NT_STATUS_OK;
3360 done:
3361 TALLOC_FREE(mem_ctx);
3362 return result;
3365 /**********************************************************************
3366 *********************************************************************/
3368 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3369 struct dom_sid sid)
3371 struct ldapsam_privates *priv =
3372 (struct ldapsam_privates *)methods->private_data;
3373 LDAPMessage *msg, *entry;
3374 int rc;
3375 NTSTATUS result;
3376 TALLOC_CTX *mem_ctx;
3377 char *filter;
3378 struct dom_sid_buf buf;
3380 mem_ctx = talloc_new(NULL);
3381 if (mem_ctx == NULL) {
3382 DEBUG(0, ("talloc_new failed\n"));
3383 return NT_STATUS_NO_MEMORY;
3386 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3387 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3388 dom_sid_str_buf(&sid, &buf));
3389 if (filter == NULL) {
3390 result = NT_STATUS_NO_MEMORY;
3391 goto done;
3393 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3394 get_attr_list(mem_ctx, groupmap_attr_list),
3395 &msg);
3396 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
3398 if ((rc != LDAP_SUCCESS) ||
3399 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3400 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3401 result = NT_STATUS_NO_SUCH_GROUP;
3402 goto done;
3405 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3406 get_attr_list(mem_ctx,
3407 groupmap_attr_list_to_delete));
3409 if ((rc == LDAP_NAMING_VIOLATION) ||
3410 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3411 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3412 const char *attrs[] = { "sambaGroupType", "description",
3413 "displayName", "sambaSIDList",
3414 NULL };
3416 /* Second try. Don't delete the sambaSID attribute, this is
3417 for "old" entries that are tacked on a winbind
3418 sambaIdmapEntry. */
3420 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3421 LDAP_OBJ_GROUPMAP, attrs);
3424 if ((rc == LDAP_NAMING_VIOLATION) ||
3425 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3426 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3427 const char *attrs[] = { "sambaGroupType", "description",
3428 "displayName", "sambaSIDList",
3429 "gidNumber", NULL };
3431 /* Third try. This is a post-3.0.21 alias (containing only
3432 * sambaSidEntry and sambaGroupMapping classes), we also have
3433 * to delete the gidNumber attribute, only the sambaSidEntry
3434 * remains */
3436 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3437 LDAP_OBJ_GROUPMAP, attrs);
3440 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3442 done:
3443 TALLOC_FREE(mem_ctx);
3444 return result;
3447 /**********************************************************************
3448 *********************************************************************/
3450 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3451 bool update)
3453 struct ldapsam_privates *ldap_state =
3454 (struct ldapsam_privates *)my_methods->private_data;
3455 char *filter = NULL;
3456 int rc;
3457 const char **attr_list;
3459 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3460 if (!filter) {
3461 return NT_STATUS_NO_MEMORY;
3463 attr_list = get_attr_list( NULL, groupmap_attr_list );
3464 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3465 LDAP_SCOPE_SUBTREE, filter,
3466 attr_list, 0, &ldap_state->result);
3467 TALLOC_FREE(attr_list);
3469 if (rc != LDAP_SUCCESS) {
3470 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3471 ldap_err2string(rc)));
3472 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3473 lp_ldap_suffix(talloc_tos()), filter));
3474 ldap_msgfree(ldap_state->result);
3475 ldap_state->result = NULL;
3476 TALLOC_FREE(filter);
3477 return NT_STATUS_UNSUCCESSFUL;
3480 TALLOC_FREE(filter);
3482 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3483 ldap_count_entries(
3484 smbldap_get_ldap(ldap_state->smbldap_state),
3485 ldap_state->result)));
3487 ldap_state->entry =
3488 ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3489 ldap_state->result);
3490 ldap_state->index = 0;
3492 return NT_STATUS_OK;
3495 /**********************************************************************
3496 *********************************************************************/
3498 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3500 ldapsam_endsampwent(my_methods);
3503 /**********************************************************************
3504 *********************************************************************/
3506 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3507 GROUP_MAP *map)
3509 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3510 struct ldapsam_privates *ldap_state =
3511 (struct ldapsam_privates *)my_methods->private_data;
3512 bool bret = False;
3514 while (!bret) {
3515 if (!ldap_state->entry)
3516 return ret;
3518 ldap_state->index++;
3519 bret = init_group_from_ldap(ldap_state, map,
3520 ldap_state->entry);
3522 ldap_state->entry = ldap_next_entry(
3523 smbldap_get_ldap(ldap_state->smbldap_state),
3524 ldap_state->entry);
3527 return NT_STATUS_OK;
3530 /**********************************************************************
3531 *********************************************************************/
3533 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3534 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3535 GROUP_MAP ***pp_rmap,
3536 size_t *p_num_entries,
3537 bool unix_only)
3539 GROUP_MAP *map = NULL;
3540 size_t entries = 0;
3542 *p_num_entries = 0;
3543 *pp_rmap = NULL;
3545 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3546 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3547 "passdb\n"));
3548 return NT_STATUS_ACCESS_DENIED;
3551 while (true) {
3553 map = talloc_zero(NULL, GROUP_MAP);
3554 if (!map) {
3555 return NT_STATUS_NO_MEMORY;
3558 if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
3559 TALLOC_FREE(map);
3560 break;
3563 if (sid_name_use != SID_NAME_UNKNOWN &&
3564 sid_name_use != map->sid_name_use) {
3565 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3566 "not of the requested type\n",
3567 map->nt_name));
3568 continue;
3570 if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
3571 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3572 "non mapped\n", map->nt_name));
3573 continue;
3576 *pp_rmap = talloc_realloc(NULL, *pp_rmap,
3577 GROUP_MAP *, entries + 1);
3578 if (!(*pp_rmap)) {
3579 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3580 "enlarge group map!\n"));
3581 return NT_STATUS_UNSUCCESSFUL;
3584 (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
3586 entries += 1;
3589 ldapsam_endsamgrent(methods);
3591 *p_num_entries = entries;
3593 return NT_STATUS_OK;
3596 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3597 const struct dom_sid *alias,
3598 const struct dom_sid *member,
3599 int modop)
3601 struct ldapsam_privates *ldap_state =
3602 (struct ldapsam_privates *)methods->private_data;
3603 char *dn = NULL;
3604 LDAPMessage *result = NULL;
3605 LDAPMessage *entry = NULL;
3606 int count;
3607 LDAPMod **mods = NULL;
3608 int rc;
3609 enum lsa_SidType type = SID_NAME_USE_NONE;
3610 struct dom_sid_buf tmp;
3612 char *filter = NULL;
3614 if (sid_check_is_in_builtin(alias)) {
3615 type = SID_NAME_ALIAS;
3618 if (sid_check_is_in_our_sam(alias)) {
3619 type = SID_NAME_ALIAS;
3622 if (type == SID_NAME_USE_NONE) {
3623 struct dom_sid_buf buf;
3624 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3625 dom_sid_str_buf(alias, &buf)));
3626 return NT_STATUS_NO_SUCH_ALIAS;
3629 if (asprintf(&filter,
3630 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3631 LDAP_OBJ_GROUPMAP,
3632 dom_sid_str_buf(alias, &tmp),
3633 type) < 0) {
3634 return NT_STATUS_NO_MEMORY;
3637 if (ldapsam_search_one_group(ldap_state, filter,
3638 &result) != LDAP_SUCCESS) {
3639 SAFE_FREE(filter);
3640 return NT_STATUS_NO_SUCH_ALIAS;
3643 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3644 result);
3646 if (count < 1) {
3647 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3648 ldap_msgfree(result);
3649 SAFE_FREE(filter);
3650 return NT_STATUS_NO_SUCH_ALIAS;
3653 if (count > 1) {
3654 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3655 "filter %s: count=%d\n", filter, count));
3656 ldap_msgfree(result);
3657 SAFE_FREE(filter);
3658 return NT_STATUS_NO_SUCH_ALIAS;
3661 SAFE_FREE(filter);
3663 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3664 result);
3666 if (!entry) {
3667 ldap_msgfree(result);
3668 return NT_STATUS_UNSUCCESSFUL;
3671 dn = smbldap_talloc_dn(talloc_tos(),
3672 smbldap_get_ldap(ldap_state->smbldap_state),
3673 entry);
3674 if (!dn) {
3675 ldap_msgfree(result);
3676 return NT_STATUS_UNSUCCESSFUL;
3679 smbldap_set_mod(&mods, modop,
3680 get_attr_key2string(groupmap_attr_list,
3681 LDAP_ATTR_SID_LIST),
3682 dom_sid_str_buf(member, &tmp));
3684 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3686 ldap_mods_free(mods, True);
3687 ldap_msgfree(result);
3688 TALLOC_FREE(dn);
3690 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3691 return NT_STATUS_MEMBER_IN_ALIAS;
3694 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3695 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3698 if (rc != LDAP_SUCCESS) {
3699 return NT_STATUS_UNSUCCESSFUL;
3702 return NT_STATUS_OK;
3705 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3706 const struct dom_sid *alias,
3707 const struct dom_sid *member)
3709 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3712 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3713 const struct dom_sid *alias,
3714 const struct dom_sid *member)
3716 return ldapsam_modify_aliasmem(methods, alias, member,
3717 LDAP_MOD_DELETE);
3720 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3721 const struct dom_sid *alias,
3722 TALLOC_CTX *mem_ctx,
3723 struct dom_sid **pp_members,
3724 size_t *p_num_members)
3726 struct ldapsam_privates *ldap_state =
3727 (struct ldapsam_privates *)methods->private_data;
3728 LDAPMessage *result = NULL;
3729 LDAPMessage *entry = NULL;
3730 int count;
3731 char **values = NULL;
3732 int i;
3733 char *filter = NULL;
3734 uint32_t num_members = 0;
3735 enum lsa_SidType type = SID_NAME_USE_NONE;
3736 struct dom_sid_buf tmp;
3738 *pp_members = NULL;
3739 *p_num_members = 0;
3741 if (sid_check_is_in_builtin(alias)) {
3742 type = SID_NAME_ALIAS;
3745 if (sid_check_is_in_our_sam(alias)) {
3746 type = SID_NAME_ALIAS;
3749 if (type == SID_NAME_USE_NONE) {
3750 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3751 dom_sid_str_buf(alias, &tmp)));
3752 return NT_STATUS_NO_SUCH_ALIAS;
3755 if (asprintf(&filter,
3756 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3757 LDAP_OBJ_GROUPMAP,
3758 dom_sid_str_buf(alias, &tmp),
3759 type) < 0) {
3760 return NT_STATUS_NO_MEMORY;
3763 if (ldapsam_search_one_group(ldap_state, filter,
3764 &result) != LDAP_SUCCESS) {
3765 SAFE_FREE(filter);
3766 return NT_STATUS_NO_SUCH_ALIAS;
3769 count = ldap_count_entries(smbldap_get_ldap(ldap_state->smbldap_state),
3770 result);
3772 if (count < 1) {
3773 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3774 ldap_msgfree(result);
3775 SAFE_FREE(filter);
3776 return NT_STATUS_NO_SUCH_ALIAS;
3779 if (count > 1) {
3780 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3781 "filter %s: count=%d\n", filter, count));
3782 ldap_msgfree(result);
3783 SAFE_FREE(filter);
3784 return NT_STATUS_NO_SUCH_ALIAS;
3787 SAFE_FREE(filter);
3789 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
3790 result);
3792 if (!entry) {
3793 ldap_msgfree(result);
3794 return NT_STATUS_UNSUCCESSFUL;
3797 values = ldap_get_values(smbldap_get_ldap(ldap_state->smbldap_state),
3798 entry,
3799 get_attr_key2string(groupmap_attr_list,
3800 LDAP_ATTR_SID_LIST));
3802 if (values == NULL) {
3803 ldap_msgfree(result);
3804 return NT_STATUS_OK;
3807 count = ldap_count_values(values);
3809 for (i=0; i<count; i++) {
3810 struct dom_sid member;
3811 NTSTATUS status;
3813 if (!string_to_sid(&member, values[i]))
3814 continue;
3816 status = add_sid_to_array(mem_ctx, &member, pp_members,
3817 &num_members);
3818 if (!NT_STATUS_IS_OK(status)) {
3819 ldap_value_free(values);
3820 ldap_msgfree(result);
3821 return status;
3825 *p_num_members = num_members;
3826 ldap_value_free(values);
3827 ldap_msgfree(result);
3829 return NT_STATUS_OK;
3832 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3833 TALLOC_CTX *mem_ctx,
3834 const struct dom_sid *domain_sid,
3835 const struct dom_sid *members,
3836 size_t num_members,
3837 uint32_t **pp_alias_rids,
3838 size_t *p_num_alias_rids)
3840 struct ldapsam_privates *ldap_state =
3841 (struct ldapsam_privates *)methods->private_data;
3842 LDAP *ldap_struct;
3844 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3846 LDAPMessage *result = NULL;
3847 LDAPMessage *entry = NULL;
3848 int i;
3849 int rc;
3850 char *filter;
3851 enum lsa_SidType type = SID_NAME_USE_NONE;
3852 bool is_builtin = false;
3853 bool sid_added = false;
3855 *pp_alias_rids = NULL;
3856 *p_num_alias_rids = 0;
3858 if (sid_check_is_builtin(domain_sid)) {
3859 is_builtin = true;
3860 type = SID_NAME_ALIAS;
3863 if (sid_check_is_our_sam(domain_sid)) {
3864 type = SID_NAME_ALIAS;
3867 if (type == SID_NAME_USE_NONE) {
3868 struct dom_sid_buf buf;
3869 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3870 dom_sid_str_buf(domain_sid, &buf)));
3871 return NT_STATUS_UNSUCCESSFUL;
3874 if (num_members == 0) {
3875 return NT_STATUS_OK;
3878 filter = talloc_asprintf(mem_ctx,
3879 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3880 LDAP_OBJ_GROUPMAP, type);
3882 for (i=0; i<num_members; i++) {
3883 struct dom_sid_buf buf;
3884 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3885 filter,
3886 dom_sid_str_buf(&members[i], &buf));
3889 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3891 if (filter == NULL) {
3892 return NT_STATUS_NO_MEMORY;
3895 if (is_builtin &&
3896 ldap_state->search_cache.filter &&
3897 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3898 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3899 result = ldap_state->search_cache.result;
3900 ldap_state->search_cache.result = NULL;
3901 } else {
3902 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(talloc_tos()),
3903 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3904 if (rc != LDAP_SUCCESS) {
3905 return NT_STATUS_UNSUCCESSFUL;
3907 smbldap_talloc_autofree_ldapmsg(filter, result);
3910 ldap_struct = smbldap_get_ldap(ldap_state->smbldap_state);
3912 for (entry = ldap_first_entry(ldap_struct, result);
3913 entry != NULL;
3914 entry = ldap_next_entry(ldap_struct, entry))
3916 fstring sid_str;
3917 struct dom_sid sid;
3918 uint32_t rid;
3920 if (!smbldap_get_single_attribute(ldap_struct, entry,
3921 LDAP_ATTRIBUTE_SID,
3922 sid_str,
3923 sizeof(sid_str)-1))
3924 continue;
3926 if (!string_to_sid(&sid, sid_str))
3927 continue;
3929 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3930 continue;
3932 sid_added = true;
3934 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3935 p_num_alias_rids)) {
3936 return NT_STATUS_NO_MEMORY;
3940 if (!is_builtin && !sid_added) {
3941 TALLOC_FREE(ldap_state->search_cache.filter);
3943 * Note: result is a talloc child of filter because of the
3944 * smbldap_talloc_autofree_ldapmsg() usage
3946 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3947 ldap_state->search_cache.result = result;
3950 return NT_STATUS_OK;
3953 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3954 enum pdb_policy_type type,
3955 uint32_t value)
3957 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3958 int rc;
3959 LDAPMod **mods = NULL;
3960 fstring value_string;
3961 const char *policy_attr = NULL;
3963 struct ldapsam_privates *ldap_state =
3964 (struct ldapsam_privates *)methods->private_data;
3966 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3968 if (!ldap_state->domain_dn) {
3969 return NT_STATUS_INVALID_PARAMETER;
3972 policy_attr = get_account_policy_attr(type);
3973 if (policy_attr == NULL) {
3974 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3975 "policy\n"));
3976 return ntstatus;
3979 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3981 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3983 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3984 mods);
3986 ldap_mods_free(mods, True);
3988 if (rc != LDAP_SUCCESS) {
3989 return ntstatus;
3992 if (!cache_account_policy_set(type, value)) {
3993 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3994 "update local tdb cache\n"));
3995 return ntstatus;
3998 return NT_STATUS_OK;
4001 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
4002 enum pdb_policy_type type,
4003 uint32_t value)
4005 return ldapsam_set_account_policy_in_ldap(methods, type,
4006 value);
4009 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
4010 enum pdb_policy_type type,
4011 uint32_t *value)
4013 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4014 LDAPMessage *result = NULL;
4015 LDAPMessage *entry = NULL;
4016 int count;
4017 int rc;
4018 char **vals = NULL;
4019 char *filter;
4020 const char *policy_attr = NULL;
4022 struct ldapsam_privates *ldap_state =
4023 (struct ldapsam_privates *)methods->private_data;
4025 const char *attrs[2];
4027 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
4029 if (!ldap_state->domain_dn) {
4030 return NT_STATUS_INVALID_PARAMETER;
4033 policy_attr = get_account_policy_attr(type);
4034 if (!policy_attr) {
4035 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
4036 "policy index: %d\n", type));
4037 return ntstatus;
4040 attrs[0] = policy_attr;
4041 attrs[1] = NULL;
4043 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
4044 if (filter == NULL) {
4045 return NT_STATUS_NO_MEMORY;
4047 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
4048 LDAP_SCOPE_BASE, filter, attrs, 0,
4049 &result);
4050 TALLOC_FREE(filter);
4051 if (rc != LDAP_SUCCESS) {
4052 return ntstatus;
4055 count = ldap_count_entries(priv2ld(ldap_state), result);
4056 if (count < 1) {
4057 goto out;
4060 entry = ldap_first_entry(priv2ld(ldap_state), result);
4061 if (entry == NULL) {
4062 goto out;
4065 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4066 if (vals == NULL) {
4067 goto out;
4070 *value = (uint32_t)atol(vals[0]);
4072 ntstatus = NT_STATUS_OK;
4074 out:
4075 if (vals)
4076 ldap_value_free(vals);
4077 ldap_msgfree(result);
4079 return ntstatus;
4082 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4084 - if user hasn't decided to use account policies inside LDAP just reuse the
4085 old tdb values
4087 - if there is a valid cache entry, return that
4088 - if there is an LDAP entry, update cache and return
4089 - otherwise set to default, update cache and return
4091 Guenther
4093 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4094 enum pdb_policy_type type,
4095 uint32_t *value)
4097 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4099 if (cache_account_policy_get(type, value)) {
4100 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4101 "cache\n"));
4102 return NT_STATUS_OK;
4105 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4106 value);
4107 if (NT_STATUS_IS_OK(ntstatus)) {
4108 goto update_cache;
4111 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4112 "ldap\n"));
4114 #if 0
4115 /* should we automagically migrate old tdb value here ? */
4116 if (account_policy_get(type, value))
4117 goto update_ldap;
4119 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4120 "default\n", type));
4121 #endif
4123 if (!account_policy_get_default(type, value)) {
4124 return ntstatus;
4127 /* update_ldap: */
4129 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4130 if (!NT_STATUS_IS_OK(ntstatus)) {
4131 return ntstatus;
4134 update_cache:
4136 if (!cache_account_policy_set(type, *value)) {
4137 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4138 "tdb as a cache\n"));
4139 return NT_STATUS_UNSUCCESSFUL;
4142 return NT_STATUS_OK;
4145 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4146 const struct dom_sid *domain_sid,
4147 int num_rids,
4148 uint32_t *rids,
4149 const char **names,
4150 enum lsa_SidType *attrs)
4152 struct ldapsam_privates *ldap_state =
4153 (struct ldapsam_privates *)methods->private_data;
4154 LDAPMessage *msg = NULL;
4155 LDAPMessage *entry;
4156 char *allsids = NULL;
4157 size_t i, num_mapped;
4158 int rc;
4159 NTSTATUS result = NT_STATUS_NO_MEMORY;
4160 TALLOC_CTX *mem_ctx;
4161 LDAP *ld;
4162 bool is_builtin;
4164 mem_ctx = talloc_new(NULL);
4165 if (mem_ctx == NULL) {
4166 DEBUG(0, ("talloc_new failed\n"));
4167 goto done;
4170 if (!sid_check_is_builtin(domain_sid) &&
4171 !sid_check_is_our_sam(domain_sid)) {
4172 result = NT_STATUS_INVALID_PARAMETER;
4173 goto done;
4176 if (num_rids == 0) {
4177 result = NT_STATUS_NONE_MAPPED;
4178 goto done;
4181 for (i=0; i<num_rids; i++)
4182 attrs[i] = SID_NAME_UNKNOWN;
4184 allsids = talloc_strdup(mem_ctx, "");
4185 if (allsids == NULL) {
4186 goto done;
4189 for (i=0; i<num_rids; i++) {
4190 struct dom_sid sid;
4191 struct dom_sid_buf buf;
4192 sid_compose(&sid, domain_sid, rids[i]);
4193 allsids = talloc_asprintf_append_buffer(
4194 allsids,
4195 "(sambaSid=%s)",
4196 dom_sid_str_buf(&sid, &buf));
4197 if (allsids == NULL) {
4198 goto done;
4202 /* First look for users */
4205 char *filter;
4206 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4208 filter = talloc_asprintf(
4209 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4210 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4212 if (filter == NULL) {
4213 goto done;
4216 rc = smbldap_search(ldap_state->smbldap_state,
4217 lp_ldap_user_suffix(talloc_tos()),
4218 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4219 &msg);
4220 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4223 if (rc != LDAP_SUCCESS)
4224 goto done;
4226 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4227 num_mapped = 0;
4229 for (entry = ldap_first_entry(ld, msg);
4230 entry != NULL;
4231 entry = ldap_next_entry(ld, entry)) {
4232 uint32_t rid;
4233 int rid_index;
4234 const char *name;
4236 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4237 &rid)) {
4238 DEBUG(2, ("Could not find sid from ldap entry\n"));
4239 continue;
4242 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4243 names);
4244 if (name == NULL) {
4245 DEBUG(2, ("Could not retrieve uid attribute\n"));
4246 continue;
4249 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4250 if (rid == rids[rid_index])
4251 break;
4254 if (rid_index == num_rids) {
4255 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4256 continue;
4259 attrs[rid_index] = SID_NAME_USER;
4260 names[rid_index] = name;
4261 num_mapped += 1;
4264 if (num_mapped == num_rids) {
4265 /* No need to look for groups anymore -- we're done */
4266 result = NT_STATUS_OK;
4267 goto done;
4270 /* Same game for groups */
4273 char *filter;
4274 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4275 "sambaGroupType", NULL };
4277 filter = talloc_asprintf(
4278 mem_ctx, "(&(objectClass=%s)(|%s))",
4279 LDAP_OBJ_GROUPMAP, allsids);
4280 if (filter == NULL) {
4281 goto done;
4284 rc = smbldap_search(ldap_state->smbldap_state,
4285 lp_ldap_suffix(talloc_tos()),
4286 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4287 &msg);
4288 smbldap_talloc_autofree_ldapmsg(mem_ctx, msg);
4291 if (rc != LDAP_SUCCESS)
4292 goto done;
4294 /* ldap_struct might have changed due to a reconnect */
4296 ld = smbldap_get_ldap(ldap_state->smbldap_state);
4298 /* For consistency checks, we already checked we're only domain or builtin */
4300 is_builtin = sid_check_is_builtin(domain_sid);
4302 for (entry = ldap_first_entry(ld, msg);
4303 entry != NULL;
4304 entry = ldap_next_entry(ld, entry))
4306 uint32_t rid;
4307 int rid_index;
4308 const char *attr;
4309 enum lsa_SidType type;
4310 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4312 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4313 mem_ctx);
4314 if (attr == NULL) {
4315 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4316 dn));
4317 continue;
4320 type = (enum lsa_SidType)atol(attr);
4322 /* Consistency checks */
4323 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4324 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4325 (type != SID_NAME_DOM_GRP)))) {
4326 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4329 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4330 &rid)) {
4331 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4332 continue;
4335 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4337 if (attr == NULL) {
4338 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4339 dn));
4340 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4343 if (attr == NULL) {
4344 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4345 dn));
4346 continue;
4349 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4350 if (rid == rids[rid_index])
4351 break;
4354 if (rid_index == num_rids) {
4355 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4356 continue;
4359 attrs[rid_index] = type;
4360 names[rid_index] = attr;
4361 num_mapped += 1;
4364 result = NT_STATUS_NONE_MAPPED;
4366 if (num_mapped > 0)
4367 result = (num_mapped == num_rids) ?
4368 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4369 done:
4370 TALLOC_FREE(mem_ctx);
4371 return result;
4374 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4376 char *filter = NULL;
4377 char *escaped = NULL;
4378 char *result = NULL;
4380 if (asprintf(&filter, "(&%s(objectclass=%s))",
4381 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4382 goto done;
4385 escaped = escape_ldap_string(talloc_tos(), username);
4386 if (escaped == NULL) goto done;
4388 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4390 done:
4391 SAFE_FREE(filter);
4392 TALLOC_FREE(escaped);
4394 return result;
4397 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4399 int i, num = 0;
4400 va_list ap;
4401 const char **result;
4403 va_start(ap, mem_ctx);
4404 while (va_arg(ap, const char *) != NULL)
4405 num += 1;
4406 va_end(ap);
4408 if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
4409 return NULL;
4412 va_start(ap, mem_ctx);
4413 for (i=0; i<num; i++) {
4414 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4415 if (result[i] == NULL) {
4416 talloc_free(result);
4417 va_end(ap);
4418 return NULL;
4421 va_end(ap);
4423 result[num] = NULL;
4424 return result;
4427 struct ldap_search_state {
4428 struct smbldap_state *connection;
4430 uint32_t acct_flags;
4431 uint16_t group_type;
4433 const char *base;
4434 int scope;
4435 const char *filter;
4436 const char **attrs;
4437 int attrsonly;
4438 void *pagedresults_cookie;
4440 LDAPMessage *entries, *current_entry;
4441 bool (*ldap2displayentry)(struct ldap_search_state *state,
4442 TALLOC_CTX *mem_ctx,
4443 LDAP *ld, LDAPMessage *entry,
4444 struct samr_displayentry *result);
4447 static bool ldapsam_search_firstpage(struct pdb_search *search)
4449 struct ldap_search_state *state =
4450 (struct ldap_search_state *)search->private_data;
4451 LDAP *ld;
4452 int rc = LDAP_OPERATIONS_ERROR;
4454 state->entries = NULL;
4456 if (smbldap_get_paged_results(state->connection)) {
4457 rc = smbldap_search_paged(state->connection, state->base,
4458 state->scope, state->filter,
4459 state->attrs, state->attrsonly,
4460 lp_ldap_page_size(), &state->entries,
4461 &state->pagedresults_cookie);
4464 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4466 if (state->entries != NULL) {
4467 /* Left over from unsuccessful paged attempt */
4468 ldap_msgfree(state->entries);
4469 state->entries = NULL;
4472 rc = smbldap_search(state->connection, state->base,
4473 state->scope, state->filter, state->attrs,
4474 state->attrsonly, &state->entries);
4476 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4477 return False;
4479 /* Ok, the server was lying. It told us it could do paged
4480 * searches when it could not. */
4481 smbldap_set_paged_results(state->connection, false);
4484 ld = smbldap_get_ldap(state->connection);
4485 if ( ld == NULL) {
4486 DEBUG(5, ("Don't have an LDAP connection right after a "
4487 "search\n"));
4488 return False;
4490 state->current_entry = ldap_first_entry(ld, state->entries);
4492 return True;
4495 static bool ldapsam_search_nextpage(struct pdb_search *search)
4497 struct ldap_search_state *state =
4498 (struct ldap_search_state *)search->private_data;
4499 int rc;
4501 if (!smbldap_get_paged_results(state->connection)) {
4502 /* There is no next page when there are no paged results */
4503 return False;
4506 rc = smbldap_search_paged(state->connection, state->base,
4507 state->scope, state->filter, state->attrs,
4508 state->attrsonly, lp_ldap_page_size(),
4509 &state->entries,
4510 &state->pagedresults_cookie);
4512 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4513 return False;
4515 state->current_entry = ldap_first_entry(
4516 smbldap_get_ldap(state->connection), state->entries);
4518 if (state->current_entry == NULL) {
4519 ldap_msgfree(state->entries);
4520 state->entries = NULL;
4521 return false;
4524 return True;
4527 static bool ldapsam_search_next_entry(struct pdb_search *search,
4528 struct samr_displayentry *entry)
4530 struct ldap_search_state *state =
4531 (struct ldap_search_state *)search->private_data;
4532 bool result;
4534 retry:
4535 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4536 return False;
4538 if ((state->entries == NULL) &&
4539 !ldapsam_search_nextpage(search))
4540 return False;
4542 if (state->current_entry == NULL) {
4543 return false;
4546 result = state->ldap2displayentry(state, search,
4547 smbldap_get_ldap(state->connection),
4548 state->current_entry, entry);
4550 if (!result) {
4551 char *dn;
4552 dn = ldap_get_dn(smbldap_get_ldap(state->connection),
4553 state->current_entry);
4554 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4555 if (dn != NULL) ldap_memfree(dn);
4558 state->current_entry = ldap_next_entry(
4559 smbldap_get_ldap(state->connection), state->current_entry);
4561 if (state->current_entry == NULL) {
4562 ldap_msgfree(state->entries);
4563 state->entries = NULL;
4566 if (!result) goto retry;
4568 return True;
4571 static void ldapsam_search_end(struct pdb_search *search)
4573 struct ldap_search_state *state =
4574 (struct ldap_search_state *)search->private_data;
4575 int rc;
4577 if (state->pagedresults_cookie == NULL)
4578 return;
4580 if (state->entries != NULL)
4581 ldap_msgfree(state->entries);
4583 state->entries = NULL;
4584 state->current_entry = NULL;
4586 if (!smbldap_get_paged_results(state->connection)) {
4587 return;
4590 /* Tell the LDAP server we're not interested in the rest anymore. */
4592 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4593 state->filter, state->attrs,
4594 state->attrsonly, 0, &state->entries,
4595 &state->pagedresults_cookie);
4597 if (rc != LDAP_SUCCESS)
4598 DEBUG(5, ("Could not end search properly\n"));
4600 return;
4603 static bool ldapuser2displayentry(struct ldap_search_state *state,
4604 TALLOC_CTX *mem_ctx,
4605 LDAP *ld, LDAPMessage *entry,
4606 struct samr_displayentry *result)
4608 char **vals;
4609 size_t converted_size;
4610 struct dom_sid sid;
4611 uint32_t acct_flags;
4613 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4614 if ((vals == NULL) || (vals[0] == NULL)) {
4615 acct_flags = ACB_NORMAL;
4616 } else {
4617 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4618 ldap_value_free(vals);
4621 if ((state->acct_flags != 0) &&
4622 ((state->acct_flags & acct_flags) == 0))
4623 return False;
4625 result->acct_flags = acct_flags;
4626 result->account_name = "";
4627 result->fullname = "";
4628 result->description = "";
4630 vals = ldap_get_values(ld, entry, "uid");
4631 if ((vals == NULL) || (vals[0] == NULL)) {
4632 DEBUG(5, ("\"uid\" not found\n"));
4633 return False;
4635 if (!pull_utf8_talloc(mem_ctx,
4636 discard_const_p(char *, &result->account_name),
4637 vals[0], &converted_size))
4639 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4640 strerror(errno)));
4643 ldap_value_free(vals);
4645 vals = ldap_get_values(ld, entry, "displayName");
4646 if ((vals == NULL) || (vals[0] == NULL))
4647 DEBUG(8, ("\"displayName\" not found\n"));
4648 else if (!pull_utf8_talloc(mem_ctx,
4649 discard_const_p(char *, &result->fullname),
4650 vals[0], &converted_size))
4652 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4653 strerror(errno)));
4656 ldap_value_free(vals);
4658 vals = ldap_get_values(ld, entry, "description");
4659 if ((vals == NULL) || (vals[0] == NULL))
4660 DEBUG(8, ("\"description\" not found\n"));
4661 else if (!pull_utf8_talloc(mem_ctx,
4662 discard_const_p(char *, &result->description),
4663 vals[0], &converted_size))
4665 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4666 strerror(errno)));
4669 ldap_value_free(vals);
4671 if ((result->account_name == NULL) ||
4672 (result->fullname == NULL) ||
4673 (result->description == NULL)) {
4674 DEBUG(0, ("talloc failed\n"));
4675 return False;
4678 vals = ldap_get_values(ld, entry, "sambaSid");
4679 if ((vals == NULL) || (vals[0] == NULL)) {
4680 DEBUG(0, ("\"objectSid\" not found\n"));
4681 return False;
4684 if (!string_to_sid(&sid, vals[0])) {
4685 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4686 ldap_value_free(vals);
4687 return False;
4689 ldap_value_free(vals);
4691 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4692 struct dom_sid_buf buf;
4693 DEBUG(0, ("sid %s does not belong to our domain\n",
4694 dom_sid_str_buf(&sid, &buf)));
4695 return False;
4698 return True;
4702 static bool ldapsam_search_users(struct pdb_methods *methods,
4703 struct pdb_search *search,
4704 uint32_t acct_flags)
4706 struct ldapsam_privates *ldap_state =
4707 (struct ldapsam_privates *)methods->private_data;
4708 struct ldap_search_state *state;
4710 state = talloc(search, struct ldap_search_state);
4711 if (state == NULL) {
4712 DEBUG(0, ("talloc failed\n"));
4713 return False;
4716 state->connection = ldap_state->smbldap_state;
4718 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4719 state->base = lp_ldap_user_suffix(talloc_tos());
4720 else if ((acct_flags != 0) &&
4721 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4722 state->base = lp_ldap_machine_suffix(talloc_tos());
4723 else
4724 state->base = lp_ldap_suffix(talloc_tos());
4726 state->acct_flags = acct_flags;
4727 state->base = talloc_strdup(search, state->base);
4728 state->scope = LDAP_SCOPE_SUBTREE;
4729 state->filter = get_ldap_filter(search, "*");
4730 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4731 "displayName", "description",
4732 "sambaAcctFlags", NULL);
4733 state->attrsonly = 0;
4734 state->pagedresults_cookie = NULL;
4735 state->entries = NULL;
4736 state->ldap2displayentry = ldapuser2displayentry;
4738 if ((state->filter == NULL) || (state->attrs == NULL)) {
4739 DEBUG(0, ("talloc failed\n"));
4740 return False;
4743 search->private_data = state;
4744 search->next_entry = ldapsam_search_next_entry;
4745 search->search_end = ldapsam_search_end;
4747 return ldapsam_search_firstpage(search);
4750 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4751 TALLOC_CTX *mem_ctx,
4752 LDAP *ld, LDAPMessage *entry,
4753 struct samr_displayentry *result)
4755 char **vals;
4756 size_t converted_size;
4757 struct dom_sid sid;
4758 uint16_t group_type;
4760 result->account_name = "";
4761 result->fullname = "";
4762 result->description = "";
4765 vals = ldap_get_values(ld, entry, "sambaGroupType");
4766 if ((vals == NULL) || (vals[0] == NULL)) {
4767 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4768 if (vals != NULL) {
4769 ldap_value_free(vals);
4771 return False;
4774 group_type = atoi(vals[0]);
4776 if ((state->group_type != 0) &&
4777 ((state->group_type != group_type))) {
4778 ldap_value_free(vals);
4779 return False;
4782 ldap_value_free(vals);
4784 /* display name is the NT group name */
4786 vals = ldap_get_values(ld, entry, "displayName");
4787 if ((vals == NULL) || (vals[0] == NULL)) {
4788 DEBUG(8, ("\"displayName\" not found\n"));
4790 /* fallback to the 'cn' attribute */
4791 vals = ldap_get_values(ld, entry, "cn");
4792 if ((vals == NULL) || (vals[0] == NULL)) {
4793 DEBUG(5, ("\"cn\" not found\n"));
4794 return False;
4796 if (!pull_utf8_talloc(mem_ctx,
4797 discard_const_p(char *,
4798 &result->account_name),
4799 vals[0], &converted_size))
4801 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4802 "failed: %s", strerror(errno)));
4805 else if (!pull_utf8_talloc(mem_ctx,
4806 discard_const_p(char *,
4807 &result->account_name),
4808 vals[0], &converted_size))
4810 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4811 strerror(errno)));
4814 ldap_value_free(vals);
4816 vals = ldap_get_values(ld, entry, "description");
4817 if ((vals == NULL) || (vals[0] == NULL))
4818 DEBUG(8, ("\"description\" not found\n"));
4819 else if (!pull_utf8_talloc(mem_ctx,
4820 discard_const_p(char *, &result->description),
4821 vals[0], &converted_size))
4823 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4824 strerror(errno)));
4826 ldap_value_free(vals);
4828 if ((result->account_name == NULL) ||
4829 (result->fullname == NULL) ||
4830 (result->description == NULL)) {
4831 DEBUG(0, ("talloc failed\n"));
4832 return False;
4835 vals = ldap_get_values(ld, entry, "sambaSid");
4836 if ((vals == NULL) || (vals[0] == NULL)) {
4837 DEBUG(0, ("\"objectSid\" not found\n"));
4838 if (vals != NULL) {
4839 ldap_value_free(vals);
4841 return False;
4844 if (!string_to_sid(&sid, vals[0])) {
4845 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4846 return False;
4849 ldap_value_free(vals);
4851 switch (group_type) {
4852 case SID_NAME_DOM_GRP:
4853 case SID_NAME_ALIAS:
4855 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4856 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4858 struct dom_sid_buf buf;
4859 DEBUG(0, ("%s is not in our domain\n",
4860 dom_sid_str_buf(&sid, &buf)));
4861 return False;
4863 break;
4865 default:
4866 DEBUG(0,("unknown group type: %d\n", group_type));
4867 return False;
4870 result->acct_flags = 0;
4872 return True;
4875 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4876 struct pdb_search *search,
4877 const struct dom_sid *sid,
4878 enum lsa_SidType type)
4880 struct ldapsam_privates *ldap_state =
4881 (struct ldapsam_privates *)methods->private_data;
4882 struct ldap_search_state *state;
4883 struct dom_sid_buf tmp;
4885 state = talloc(search, struct ldap_search_state);
4886 if (state == NULL) {
4887 DEBUG(0, ("talloc failed\n"));
4888 return False;
4891 state->connection = ldap_state->smbldap_state;
4893 state->base = lp_ldap_suffix(search);
4894 state->connection = ldap_state->smbldap_state;
4895 state->scope = LDAP_SCOPE_SUBTREE;
4896 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4897 "(sambaGroupType=%d)(sambaSID=%s*))",
4898 LDAP_OBJ_GROUPMAP,
4899 type,
4900 dom_sid_str_buf(sid, &tmp));
4901 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4902 "displayName", "description",
4903 "sambaGroupType", NULL);
4904 state->attrsonly = 0;
4905 state->pagedresults_cookie = NULL;
4906 state->entries = NULL;
4907 state->group_type = type;
4908 state->ldap2displayentry = ldapgroup2displayentry;
4910 if ((state->filter == NULL) || (state->attrs == NULL)) {
4911 DEBUG(0, ("talloc failed\n"));
4912 return False;
4915 search->private_data = state;
4916 search->next_entry = ldapsam_search_next_entry;
4917 search->search_end = ldapsam_search_end;
4919 return ldapsam_search_firstpage(search);
4922 static bool ldapsam_search_groups(struct pdb_methods *methods,
4923 struct pdb_search *search)
4925 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4928 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4929 struct pdb_search *search,
4930 const struct dom_sid *sid)
4932 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4935 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4937 return PDB_CAP_STORE_RIDS;
4940 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4941 uint32_t *rid)
4943 struct smbldap_state *smbldap_state = priv->smbldap_state;
4945 LDAPMessage *result = NULL;
4946 LDAPMessage *entry = NULL;
4947 LDAPMod **mods = NULL;
4948 NTSTATUS status;
4949 char *value;
4950 int rc;
4951 uint32_t nextRid = 0;
4952 const char *dn;
4953 uint32_t tmp;
4954 int error = 0;
4956 TALLOC_CTX *mem_ctx;
4958 mem_ctx = talloc_new(NULL);
4959 if (mem_ctx == NULL) {
4960 DEBUG(0, ("talloc_new failed\n"));
4961 return NT_STATUS_NO_MEMORY;
4964 status = smbldap_search_domain_info(smbldap_state, &result,
4965 get_global_sam_name(), False);
4966 if (!NT_STATUS_IS_OK(status)) {
4967 DEBUG(3, ("Could not get domain info: %s\n",
4968 nt_errstr(status)));
4969 goto done;
4972 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
4974 entry = ldap_first_entry(priv2ld(priv), result);
4975 if (entry == NULL) {
4976 DEBUG(0, ("Could not get domain info entry\n"));
4977 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4978 goto done;
4981 /* Find the largest of the three attributes "sambaNextRid",
4982 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4983 concept of differentiating between user and group rids, and will
4984 use only "sambaNextRid" in the future. But for compatibility
4985 reasons I look if others have chosen different strategies -- VL */
4987 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4988 "sambaNextRid", mem_ctx);
4989 if (value != NULL) {
4990 tmp = (uint32_t)smb_strtoul(value,
4991 NULL,
4993 &error,
4994 SMB_STR_STANDARD);
4995 if (error != 0) {
4996 goto done;
4999 nextRid = MAX(nextRid, tmp);
5002 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5003 "sambaNextUserRid", mem_ctx);
5004 if (value != NULL) {
5005 tmp = (uint32_t)smb_strtoul(value,
5006 NULL,
5008 &error,
5009 SMB_STR_STANDARD);
5010 if (error != 0) {
5011 goto done;
5014 nextRid = MAX(nextRid, tmp);
5017 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5018 "sambaNextGroupRid", mem_ctx);
5019 if (value != NULL) {
5020 tmp = (uint32_t)smb_strtoul(value,
5021 NULL,
5023 &error,
5024 SMB_STR_STANDARD);
5025 if (error != 0) {
5026 goto done;
5029 nextRid = MAX(nextRid, tmp);
5032 if (nextRid == 0) {
5033 nextRid = BASE_RID-1;
5036 nextRid += 1;
5038 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
5039 talloc_asprintf(mem_ctx, "%d", nextRid));
5040 smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
5042 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
5043 status = NT_STATUS_NO_MEMORY;
5044 goto done;
5047 rc = smbldap_modify(smbldap_state, dn, mods);
5049 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
5050 * please retry" */
5052 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
5054 done:
5055 if (NT_STATUS_IS_OK(status)) {
5056 *rid = nextRid;
5059 TALLOC_FREE(mem_ctx);
5060 return status;
5063 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
5065 int i;
5067 for (i=0; i<10; i++) {
5068 NTSTATUS result = ldapsam_get_new_rid(
5069 (struct ldapsam_privates *)methods->private_data, rid);
5070 if (NT_STATUS_IS_OK(result)) {
5071 return result;
5074 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
5075 return result;
5078 /* The ldap update failed (maybe a race condition), retry */
5081 /* Tried 10 times, fail. */
5082 return NT_STATUS_ACCESS_DENIED;
5085 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
5087 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
5088 return NT_STATUS_IS_OK(result) ? True : False;
5091 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
5092 const struct dom_sid *sid,
5093 struct unixid *id)
5095 struct ldapsam_privates *priv =
5096 (struct ldapsam_privates *)methods->private_data;
5097 char *filter;
5098 int error = 0;
5099 struct dom_sid_buf buf;
5100 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
5101 NULL };
5102 LDAPMessage *result = NULL;
5103 LDAPMessage *entry = NULL;
5104 bool ret = False;
5105 char *value;
5106 int rc;
5108 TALLOC_CTX *mem_ctx;
5110 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
5111 if (ret == true) {
5112 return true;
5115 mem_ctx = talloc_new(NULL);
5116 if (mem_ctx == NULL) {
5117 DEBUG(0, ("talloc_new failed\n"));
5118 return False;
5121 filter = talloc_asprintf(mem_ctx,
5122 "(&(sambaSid=%s)"
5123 "(|(objectClass=%s)(objectClass=%s)))",
5124 dom_sid_str_buf(sid, &buf),
5125 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5126 if (filter == NULL) {
5127 DEBUG(5, ("talloc_asprintf failed\n"));
5128 goto done;
5131 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5132 attrs, &result);
5133 if (rc != LDAP_SUCCESS) {
5134 goto done;
5136 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
5138 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5139 DEBUG(10, ("Got %d entries, expected one\n",
5140 ldap_count_entries(priv2ld(priv), result)));
5141 goto done;
5144 entry = ldap_first_entry(priv2ld(priv), result);
5146 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5147 "sambaGroupType", mem_ctx);
5149 if (value != NULL) {
5150 const char *gid_str;
5151 /* It's a group */
5153 gid_str = smbldap_talloc_single_attribute(
5154 priv2ld(priv), entry, "gidNumber", mem_ctx);
5155 if (gid_str == NULL) {
5156 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5157 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5158 entry)));
5159 goto done;
5162 id->id = smb_strtoul(gid_str,
5163 NULL,
5165 &error,
5166 SMB_STR_STANDARD);
5167 if (error != 0) {
5168 goto done;
5171 id->type = ID_TYPE_GID;
5172 ret = True;
5173 goto done;
5176 /* It must be a user */
5178 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5179 "uidNumber", mem_ctx);
5180 if (value == NULL) {
5181 DEBUG(1, ("Could not find uidNumber in %s\n",
5182 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5183 goto done;
5186 id->id = smb_strtoul(value, NULL, 10, &error, SMB_STR_STANDARD);
5187 if (error != 0) {
5188 goto done;
5191 id->type = ID_TYPE_UID;
5192 ret = True;
5193 done:
5194 TALLOC_FREE(mem_ctx);
5195 return ret;
5199 * Find the SID for a uid.
5200 * This is shortcut is only used if ldapsam:trusted is set to true.
5202 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5203 struct dom_sid *sid)
5205 struct ldapsam_privates *priv =
5206 (struct ldapsam_privates *)methods->private_data;
5207 char *filter;
5208 const char *attrs[] = { "sambaSID", NULL };
5209 LDAPMessage *result = NULL;
5210 LDAPMessage *entry = NULL;
5211 bool ret = false;
5212 char *user_sid_string;
5213 struct dom_sid user_sid;
5214 int rc;
5215 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5217 filter = talloc_asprintf(tmp_ctx,
5218 "(&(uidNumber=%u)"
5219 "(objectClass=%s)"
5220 "(objectClass=%s))",
5221 (unsigned int)uid,
5222 LDAP_OBJ_POSIXACCOUNT,
5223 LDAP_OBJ_SAMBASAMACCOUNT);
5224 if (filter == NULL) {
5225 DEBUG(3, ("talloc_asprintf failed\n"));
5226 goto done;
5229 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5230 if (rc != LDAP_SUCCESS) {
5231 goto done;
5233 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5235 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5236 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5237 ldap_count_entries(priv2ld(priv), result),
5238 (unsigned int)uid));
5239 goto done;
5242 entry = ldap_first_entry(priv2ld(priv), result);
5244 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5245 "sambaSID", tmp_ctx);
5246 if (user_sid_string == NULL) {
5247 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5248 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5249 goto done;
5252 if (!string_to_sid(&user_sid, user_sid_string)) {
5253 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5254 user_sid_string));
5255 goto done;
5258 sid_copy(sid, &user_sid);
5260 ret = true;
5262 done:
5263 TALLOC_FREE(tmp_ctx);
5264 return ret;
5268 * Find the SID for a gid.
5269 * This is shortcut is only used if ldapsam:trusted is set to true.
5271 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5272 struct dom_sid *sid)
5274 struct ldapsam_privates *priv =
5275 (struct ldapsam_privates *)methods->private_data;
5276 char *filter;
5277 const char *attrs[] = { "sambaSID", NULL };
5278 LDAPMessage *result = NULL;
5279 LDAPMessage *entry = NULL;
5280 bool ret = false;
5281 char *group_sid_string;
5282 struct dom_sid group_sid;
5283 int rc;
5284 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5286 filter = talloc_asprintf(tmp_ctx,
5287 "(&(gidNumber=%u)"
5288 "(objectClass=%s))",
5289 (unsigned int)gid,
5290 LDAP_OBJ_GROUPMAP);
5291 if (filter == NULL) {
5292 DEBUG(3, ("talloc_asprintf failed\n"));
5293 goto done;
5296 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5297 if (rc != LDAP_SUCCESS) {
5298 goto done;
5300 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5302 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5303 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5304 ldap_count_entries(priv2ld(priv), result),
5305 (unsigned int)gid));
5306 goto done;
5309 entry = ldap_first_entry(priv2ld(priv), result);
5311 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5312 "sambaSID", tmp_ctx);
5313 if (group_sid_string == NULL) {
5314 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5315 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5316 goto done;
5319 if (!string_to_sid(&group_sid, group_sid_string)) {
5320 DEBUG(3, ("Error calling string_to_sid for sid '%s'\n",
5321 group_sid_string));
5322 goto done;
5325 sid_copy(sid, &group_sid);
5327 ret = true;
5329 done:
5330 TALLOC_FREE(tmp_ctx);
5331 return ret;
5334 static bool ldapsam_id_to_sid(struct pdb_methods *methods, struct unixid *id,
5335 struct dom_sid *sid)
5337 switch (id->type) {
5338 case ID_TYPE_UID:
5339 return ldapsam_uid_to_sid(methods, id->id, sid);
5341 case ID_TYPE_GID:
5342 return ldapsam_gid_to_sid(methods, id->id, sid);
5344 default:
5345 return false;
5351 * The following functions are called only if
5352 * ldapsam:trusted and ldapsam:editposix are
5353 * set to true
5357 * ldapsam_create_user creates a new
5358 * posixAccount and sambaSamAccount object
5359 * in the ldap users subtree
5361 * The uid is allocated by winbindd.
5364 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5365 TALLOC_CTX *tmp_ctx, const char *name,
5366 uint32_t acb_info, uint32_t *rid)
5368 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5369 LDAPMessage *entry = NULL;
5370 LDAPMessage *result = NULL;
5371 uint32_t num_result;
5372 bool is_machine = False;
5373 bool add_posix = False;
5374 bool init_okay = False;
5375 LDAPMod **mods = NULL;
5376 struct samu *user;
5377 char *filter;
5378 char *username;
5379 char *homedir;
5380 char *gidstr;
5381 char *uidstr;
5382 char *shell;
5383 const char *dn = NULL;
5384 struct dom_sid group_sid;
5385 struct dom_sid user_sid;
5386 gid_t gid = -1;
5387 uid_t uid = -1;
5388 NTSTATUS ret;
5389 int rc;
5391 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5392 acb_info & ACB_WSTRUST ||
5393 acb_info & ACB_SVRTRUST ||
5394 acb_info & ACB_DOMTRUST) {
5395 is_machine = True;
5398 username = escape_ldap_string(talloc_tos(), name);
5399 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5400 username, LDAP_OBJ_POSIXACCOUNT);
5401 TALLOC_FREE(username);
5403 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5404 if (rc != LDAP_SUCCESS) {
5405 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5406 return NT_STATUS_ACCESS_DENIED;
5408 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5410 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5412 if (num_result > 1) {
5413 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5414 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5417 if (num_result == 1) {
5418 char *tmp;
5419 /* check if it is just a posix account.
5420 * or if there is a sid attached to this entry
5423 entry = ldap_first_entry(priv2ld(ldap_state), result);
5424 if (!entry) {
5425 return NT_STATUS_UNSUCCESSFUL;
5428 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5429 if (tmp) {
5430 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5431 return NT_STATUS_USER_EXISTS;
5434 /* it is just a posix account, retrieve the dn for later use */
5435 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5436 if (!dn) {
5437 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5438 return NT_STATUS_NO_MEMORY;
5442 if (num_result == 0) {
5443 add_posix = True;
5446 /* Create the basic samu structure and generate the mods for the ldap commit */
5447 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5448 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5449 return ret;
5452 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5454 user = samu_new(tmp_ctx);
5455 if (!user) {
5456 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5457 return NT_STATUS_NO_MEMORY;
5460 if (!pdb_set_username(user, name, PDB_SET)) {
5461 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5462 return NT_STATUS_UNSUCCESSFUL;
5464 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5465 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5466 return NT_STATUS_UNSUCCESSFUL;
5468 if (is_machine) {
5469 if (acb_info & ACB_NORMAL) {
5470 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5471 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5472 return NT_STATUS_UNSUCCESSFUL;
5474 } else {
5475 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5476 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5477 return NT_STATUS_UNSUCCESSFUL;
5480 } else {
5481 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5482 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5483 return NT_STATUS_UNSUCCESSFUL;
5487 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5488 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5489 return NT_STATUS_UNSUCCESSFUL;
5492 init_okay = init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed);
5494 if (!init_okay) {
5495 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5496 ldap_mods_free(mods, true);
5497 return NT_STATUS_UNSUCCESSFUL;
5500 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5501 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5503 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5505 if (add_posix) {
5506 char *escape_name;
5508 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5510 /* retrieve the Domain Users group gid */
5511 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5512 !sid_to_gid(&group_sid, &gid)) {
5513 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5514 ldap_mods_free(mods, true);
5515 return NT_STATUS_INVALID_PRIMARY_GROUP;
5518 /* lets allocate a new userid for this user */
5519 if (!winbind_allocate_uid(&uid)) {
5520 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5521 ldap_mods_free(mods, true);
5522 return NT_STATUS_UNSUCCESSFUL;
5526 if (is_machine) {
5527 /* TODO: choose a more appropriate default for machines */
5528 homedir = talloc_sub_specified(tmp_ctx,
5529 lp_template_homedir(),
5530 "SMB_workstations_home",
5531 NULL,
5532 ldap_state->domain_name,
5533 uid,
5534 gid);
5535 shell = talloc_strdup(tmp_ctx, "/bin/false");
5536 } else {
5537 homedir = talloc_sub_specified(tmp_ctx,
5538 lp_template_homedir(),
5539 name,
5540 NULL,
5541 ldap_state->domain_name,
5542 uid,
5543 gid);
5544 shell = talloc_sub_specified(tmp_ctx,
5545 lp_template_shell(),
5546 name,
5547 NULL,
5548 ldap_state->domain_name,
5549 uid,
5550 gid);
5552 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5553 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5555 escape_name = escape_rdn_val_string_alloc(name);
5556 if (!escape_name) {
5557 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5558 ldap_mods_free(mods, true);
5559 return NT_STATUS_NO_MEMORY;
5562 if (is_machine) {
5563 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix (talloc_tos()));
5564 } else {
5565 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix (talloc_tos()));
5568 SAFE_FREE(escape_name);
5570 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5571 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5572 ldap_mods_free(mods, true);
5573 return NT_STATUS_NO_MEMORY;
5576 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5577 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5578 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5579 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5580 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5581 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5582 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5585 if (add_posix) {
5586 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5587 } else {
5588 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5591 ldap_mods_free(mods, true);
5593 if (rc != LDAP_SUCCESS) {
5594 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5595 return NT_STATUS_UNSUCCESSFUL;
5598 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5600 flush_pwnam_cache();
5602 return NT_STATUS_OK;
5605 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5607 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5608 LDAPMessage *result = NULL;
5609 LDAPMessage *entry = NULL;
5610 int num_result;
5611 const char *dn;
5612 char *filter;
5613 int rc;
5615 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5617 filter = talloc_asprintf(tmp_ctx,
5618 "(&(uid=%s)"
5619 "(objectClass=%s)"
5620 "(objectClass=%s))",
5621 pdb_get_username(sam_acct),
5622 LDAP_OBJ_POSIXACCOUNT,
5623 LDAP_OBJ_SAMBASAMACCOUNT);
5624 if (filter == NULL) {
5625 return NT_STATUS_NO_MEMORY;
5628 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5629 if (rc != LDAP_SUCCESS) {
5630 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5631 return NT_STATUS_UNSUCCESSFUL;
5633 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5635 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5637 if (num_result == 0) {
5638 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5639 return NT_STATUS_NO_SUCH_USER;
5642 if (num_result > 1) {
5643 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5644 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5647 entry = ldap_first_entry(priv2ld(ldap_state), result);
5648 if (!entry) {
5649 return NT_STATUS_UNSUCCESSFUL;
5652 /* it is just a posix account, retrieve the dn for later use */
5653 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5654 if (!dn) {
5655 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5656 return NT_STATUS_NO_MEMORY;
5659 /* try to remove memberships first */
5661 NTSTATUS status;
5662 struct dom_sid *sids = NULL;
5663 gid_t *gids = NULL;
5664 uint32_t num_groups = 0;
5665 int i;
5666 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5668 status = ldapsam_enum_group_memberships(my_methods,
5669 tmp_ctx,
5670 sam_acct,
5671 &sids,
5672 &gids,
5673 &num_groups);
5674 if (!NT_STATUS_IS_OK(status)) {
5675 goto delete_dn;
5678 for (i=0; i < num_groups; i++) {
5680 uint32_t group_rid;
5682 sid_peek_rid(&sids[i], &group_rid);
5684 ldapsam_del_groupmem(my_methods,
5685 tmp_ctx,
5686 group_rid,
5687 user_rid);
5691 delete_dn:
5693 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5694 if (rc != LDAP_SUCCESS) {
5695 return NT_STATUS_UNSUCCESSFUL;
5698 flush_pwnam_cache();
5700 return NT_STATUS_OK;
5704 * ldapsam_create_group creates a new
5705 * posixGroup and sambaGroupMapping object
5706 * in the ldap groups subtree
5708 * The gid is allocated by winbindd.
5711 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5712 TALLOC_CTX *tmp_ctx,
5713 const char *name,
5714 uint32_t *rid)
5716 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5717 NTSTATUS ret;
5718 LDAPMessage *entry = NULL;
5719 LDAPMessage *result = NULL;
5720 uint32_t num_result;
5721 bool is_new_entry = False;
5722 LDAPMod **mods = NULL;
5723 char *filter;
5724 char *groupname;
5725 char *grouptype;
5726 char *gidstr;
5727 const char *dn = NULL;
5728 struct dom_sid group_sid;
5729 struct dom_sid_buf buf;
5730 gid_t gid = -1;
5731 int rc;
5732 int error = 0;
5734 groupname = escape_ldap_string(talloc_tos(), name);
5735 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5736 groupname, LDAP_OBJ_POSIXGROUP);
5737 TALLOC_FREE(groupname);
5739 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5740 if (rc != LDAP_SUCCESS) {
5741 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5742 return NT_STATUS_UNSUCCESSFUL;
5744 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5746 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5748 if (num_result > 1) {
5749 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5750 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5753 if (num_result == 1) {
5754 char *tmp;
5755 /* check if it is just a posix group.
5756 * or if there is a sid attached to this entry
5759 entry = ldap_first_entry(priv2ld(ldap_state), result);
5760 if (!entry) {
5761 return NT_STATUS_UNSUCCESSFUL;
5764 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5765 if (tmp) {
5766 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5767 return NT_STATUS_GROUP_EXISTS;
5770 /* it is just a posix group, retrieve the gid and the dn for later use */
5771 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5772 if (!tmp) {
5773 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5774 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5777 gid = smb_strtoul(tmp, NULL, 10, &error, SMB_STR_STANDARD);
5778 if (error != 0) {
5779 DBG_ERR("Failed to convert gidNumber\n");
5780 return NT_STATUS_UNSUCCESSFUL;
5783 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5784 if (!dn) {
5785 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5786 return NT_STATUS_NO_MEMORY;
5790 if (num_result == 0) {
5791 is_new_entry = true;
5794 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5795 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5796 return ret;
5799 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5801 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5803 if (!grouptype) {
5804 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5805 return NT_STATUS_NO_MEMORY;
5808 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5809 smbldap_set_mod(&mods,
5810 LDAP_MOD_ADD,
5811 "sambaSid",
5812 dom_sid_str_buf(&group_sid, &buf));
5813 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5814 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5816 if (is_new_entry) {
5817 char *escape_name;
5819 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5821 /* lets allocate a new groupid for this group */
5822 if (!winbind_allocate_gid(&gid)) {
5823 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5824 return NT_STATUS_UNSUCCESSFUL;
5827 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5829 escape_name = escape_rdn_val_string_alloc(name);
5830 if (!escape_name) {
5831 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5832 return NT_STATUS_NO_MEMORY;
5835 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix(talloc_tos()));
5837 SAFE_FREE(escape_name);
5839 if (!gidstr || !dn) {
5840 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5841 return NT_STATUS_NO_MEMORY;
5844 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5845 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5846 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5849 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
5851 if (is_new_entry) {
5852 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5853 #if 0
5854 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5855 /* This call may fail with rfc2307bis schema */
5856 /* Retry adding a structural class */
5857 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5858 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5860 #endif
5861 } else {
5862 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5865 if (rc != LDAP_SUCCESS) {
5866 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5867 return NT_STATUS_UNSUCCESSFUL;
5870 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5872 return NT_STATUS_OK;
5875 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5877 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5878 LDAPMessage *result = NULL;
5879 LDAPMessage *entry = NULL;
5880 int num_result;
5881 const char *dn;
5882 char *gidstr;
5883 char *filter;
5884 struct dom_sid group_sid;
5885 struct dom_sid_buf buf;
5886 int rc;
5888 /* get the group sid */
5889 sid_compose(&group_sid, get_global_sam_sid(), rid);
5891 filter = talloc_asprintf(tmp_ctx,
5892 "(&(sambaSID=%s)"
5893 "(objectClass=%s)"
5894 "(objectClass=%s))",
5895 dom_sid_str_buf(&group_sid, &buf),
5896 LDAP_OBJ_POSIXGROUP,
5897 LDAP_OBJ_GROUPMAP);
5898 if (filter == NULL) {
5899 return NT_STATUS_NO_MEMORY;
5902 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5903 if (rc != LDAP_SUCCESS) {
5904 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5905 return NT_STATUS_UNSUCCESSFUL;
5907 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5909 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5911 if (num_result == 0) {
5912 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5913 return NT_STATUS_NO_SUCH_GROUP;
5916 if (num_result > 1) {
5917 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5918 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5921 entry = ldap_first_entry(priv2ld(ldap_state), result);
5922 if (!entry) {
5923 return NT_STATUS_UNSUCCESSFUL;
5926 /* here it is, retrieve the dn for later use */
5927 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5928 if (!dn) {
5929 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5930 return NT_STATUS_NO_MEMORY;
5933 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5934 if (!gidstr) {
5935 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5936 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5939 /* check no user have this group marked as primary group */
5940 filter = talloc_asprintf(tmp_ctx,
5941 "(&(gidNumber=%s)"
5942 "(objectClass=%s)"
5943 "(objectClass=%s))",
5944 gidstr,
5945 LDAP_OBJ_POSIXACCOUNT,
5946 LDAP_OBJ_SAMBASAMACCOUNT);
5948 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5949 if (rc != LDAP_SUCCESS) {
5950 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5951 return NT_STATUS_UNSUCCESSFUL;
5953 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
5955 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5957 if (num_result != 0) {
5958 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5959 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5962 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5963 if (rc != LDAP_SUCCESS) {
5964 return NT_STATUS_UNSUCCESSFUL;
5967 return NT_STATUS_OK;
5970 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5971 TALLOC_CTX *tmp_ctx,
5972 uint32_t group_rid,
5973 uint32_t member_rid,
5974 int modop)
5976 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5977 LDAPMessage *entry = NULL;
5978 LDAPMessage *result = NULL;
5979 uint32_t num_result;
5980 LDAPMod **mods = NULL;
5981 char *filter;
5982 char *uidstr;
5983 const char *dn = NULL;
5984 struct dom_sid group_sid;
5985 struct dom_sid member_sid;
5986 struct dom_sid_buf buf;
5987 int rc;
5988 int error = 0;
5990 switch (modop) {
5991 case LDAP_MOD_ADD:
5992 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5993 break;
5994 case LDAP_MOD_DELETE:
5995 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5996 break;
5997 default:
5998 return NT_STATUS_UNSUCCESSFUL;
6001 /* get member sid */
6002 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
6004 /* get the group sid */
6005 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
6007 filter = talloc_asprintf(tmp_ctx,
6008 "(&(sambaSID=%s)"
6009 "(objectClass=%s)"
6010 "(objectClass=%s))",
6011 dom_sid_str_buf(&member_sid, &buf),
6012 LDAP_OBJ_POSIXACCOUNT,
6013 LDAP_OBJ_SAMBASAMACCOUNT);
6014 if (filter == NULL) {
6015 return NT_STATUS_NO_MEMORY;
6018 /* get the member uid */
6019 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6020 if (rc != LDAP_SUCCESS) {
6021 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
6022 return NT_STATUS_UNSUCCESSFUL;
6024 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
6026 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6028 if (num_result == 0) {
6029 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
6030 return NT_STATUS_NO_SUCH_MEMBER;
6033 if (num_result > 1) {
6034 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
6035 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6038 entry = ldap_first_entry(priv2ld(ldap_state), result);
6039 if (!entry) {
6040 return NT_STATUS_UNSUCCESSFUL;
6043 if (modop == LDAP_MOD_DELETE) {
6044 /* check if we are trying to remove the member from his primary group */
6045 char *gidstr;
6046 gid_t user_gid, group_gid;
6048 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
6049 if (!gidstr) {
6050 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
6051 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6054 user_gid = smb_strtoul(gidstr, NULL, 10, &error, SMB_STR_STANDARD);
6055 if (error != 0) {
6056 DBG_ERR("Failed to convert user gid\n");
6057 return NT_STATUS_UNSUCCESSFUL;
6060 if (!sid_to_gid(&group_sid, &group_gid)) {
6061 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
6062 return NT_STATUS_UNSUCCESSFUL;
6065 if (user_gid == group_gid) {
6066 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
6067 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
6071 /* here it is, retrieve the uid for later use */
6072 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
6073 if (!uidstr) {
6074 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
6075 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6078 filter = talloc_asprintf(tmp_ctx,
6079 "(&(sambaSID=%s)"
6080 "(objectClass=%s)"
6081 "(objectClass=%s))",
6082 dom_sid_str_buf(&group_sid, &buf),
6083 LDAP_OBJ_POSIXGROUP,
6084 LDAP_OBJ_GROUPMAP);
6086 /* get the group */
6087 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6088 if (rc != LDAP_SUCCESS) {
6089 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
6090 return NT_STATUS_UNSUCCESSFUL;
6092 smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
6094 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6096 if (num_result == 0) {
6097 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
6098 return NT_STATUS_NO_SUCH_GROUP;
6101 if (num_result > 1) {
6102 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
6103 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6106 entry = ldap_first_entry(priv2ld(ldap_state), result);
6107 if (!entry) {
6108 return NT_STATUS_UNSUCCESSFUL;
6111 /* here it is, retrieve the dn for later use */
6112 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
6113 if (!dn) {
6114 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
6115 return NT_STATUS_NO_MEMORY;
6118 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
6120 smbldap_talloc_autofree_ldapmod(tmp_ctx, mods);
6122 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6123 if (rc != LDAP_SUCCESS) {
6124 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
6125 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
6126 return NT_STATUS_MEMBER_IN_GROUP;
6128 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
6129 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
6130 return NT_STATUS_MEMBER_NOT_IN_GROUP;
6132 return NT_STATUS_UNSUCCESSFUL;
6135 return NT_STATUS_OK;
6138 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
6139 TALLOC_CTX *tmp_ctx,
6140 uint32_t group_rid,
6141 uint32_t member_rid)
6143 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
6145 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
6146 TALLOC_CTX *tmp_ctx,
6147 uint32_t group_rid,
6148 uint32_t member_rid)
6150 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
6153 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
6154 TALLOC_CTX *mem_ctx,
6155 struct samu *sampass)
6157 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6158 LDAPMessage *entry = NULL;
6159 LDAPMessage *result = NULL;
6160 uint32_t num_result;
6161 LDAPMod **mods = NULL;
6162 char *filter;
6163 char *escape_username;
6164 char *gidstr;
6165 char *dn = NULL;
6166 gid_t gid;
6167 int rc;
6169 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6171 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6172 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6173 return NT_STATUS_UNSUCCESSFUL;
6175 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6176 if (!gidstr) {
6177 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6178 return NT_STATUS_NO_MEMORY;
6181 escape_username = escape_ldap_string(talloc_tos(),
6182 pdb_get_username(sampass));
6183 if (escape_username== NULL) {
6184 return NT_STATUS_NO_MEMORY;
6187 filter = talloc_asprintf(mem_ctx,
6188 "(&(uid=%s)"
6189 "(objectClass=%s)"
6190 "(objectClass=%s))",
6191 escape_username,
6192 LDAP_OBJ_POSIXACCOUNT,
6193 LDAP_OBJ_SAMBASAMACCOUNT);
6195 TALLOC_FREE(escape_username);
6197 if (filter == NULL) {
6198 return NT_STATUS_NO_MEMORY;
6201 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6202 if (rc != LDAP_SUCCESS) {
6203 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6204 return NT_STATUS_UNSUCCESSFUL;
6206 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6208 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6210 if (num_result == 0) {
6211 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6212 return NT_STATUS_NO_SUCH_USER;
6215 if (num_result > 1) {
6216 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6217 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6220 entry = ldap_first_entry(priv2ld(ldap_state), result);
6221 if (!entry) {
6222 return NT_STATUS_UNSUCCESSFUL;
6225 /* retrieve the dn for later use */
6226 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6227 if (!dn) {
6228 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6229 return NT_STATUS_NO_MEMORY;
6232 /* remove the old one, and add the new one, this way we do not risk races */
6233 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6235 if (mods == NULL) {
6236 TALLOC_FREE(dn);
6237 return NT_STATUS_OK;
6240 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6241 TALLOC_FREE(dn);
6242 if (rc != LDAP_SUCCESS) {
6243 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6244 pdb_get_username(sampass), gidstr));
6245 return NT_STATUS_UNSUCCESSFUL;
6248 flush_pwnam_cache();
6250 return NT_STATUS_OK;
6254 /**********************************************************************
6255 trusted domains functions
6256 *********************************************************************/
6258 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6259 const char *domain)
6261 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6262 ldap_state->domain_dn);
6265 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6266 TALLOC_CTX *mem_ctx,
6267 const char *domain, LDAPMessage **entry)
6269 int rc;
6270 char *filter;
6271 int scope = LDAP_SCOPE_SUBTREE;
6272 const char **attrs = NULL; /* NULL: get all attrs */
6273 int attrsonly = 0; /* 0: return values too */
6274 LDAPMessage *result = NULL;
6275 char *trusted_dn;
6276 uint32_t num_result;
6278 filter = talloc_asprintf(talloc_tos(),
6279 "(&(objectClass=%s)(sambaDomainName=%s))",
6280 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6282 trusted_dn = trusteddom_dn(ldap_state, domain);
6283 if (trusted_dn == NULL) {
6284 return False;
6286 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6287 filter, attrs, attrsonly, &result);
6289 if (result != NULL) {
6290 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6293 if (rc == LDAP_NO_SUCH_OBJECT) {
6294 *entry = NULL;
6295 return True;
6298 if (rc != LDAP_SUCCESS) {
6299 return False;
6302 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6304 if (num_result > 1) {
6305 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6306 "%s object for domain '%s'?!\n",
6307 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6308 return False;
6311 if (num_result == 0) {
6312 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6313 "%s object for domain %s.\n",
6314 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6315 *entry = NULL;
6316 } else {
6317 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6320 return True;
6323 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6324 const char *domain,
6325 char** pwd,
6326 struct dom_sid *sid,
6327 time_t *pass_last_set_time)
6329 struct ldapsam_privates *ldap_state =
6330 (struct ldapsam_privates *)methods->private_data;
6331 LDAPMessage *entry = NULL;
6333 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6335 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6336 (entry == NULL))
6338 return False;
6341 /* password */
6342 if (pwd != NULL) {
6343 char *pwd_str;
6344 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6345 entry, "sambaClearTextPassword", talloc_tos());
6346 if (pwd_str == NULL) {
6347 return False;
6349 /* trusteddom_pw routines do not use talloc yet... */
6350 *pwd = SMB_STRDUP(pwd_str);
6351 if (*pwd == NULL) {
6352 return False;
6356 /* last change time */
6357 if (pass_last_set_time != NULL) {
6358 char *time_str;
6359 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6360 entry, "sambaPwdLastSet", talloc_tos());
6361 if (time_str == NULL) {
6362 return False;
6364 *pass_last_set_time = (time_t)atol(time_str);
6367 /* domain sid */
6368 if (sid != NULL) {
6369 char *sid_str;
6370 struct dom_sid dom_sid;
6371 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6372 entry, "sambaSID",
6373 talloc_tos());
6374 if (sid_str == NULL) {
6375 return False;
6377 if (!string_to_sid(&dom_sid, sid_str)) {
6378 return False;
6380 sid_copy(sid, &dom_sid);
6383 return True;
6386 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6387 const char* domain,
6388 const char* pwd,
6389 const struct dom_sid *sid)
6391 struct ldapsam_privates *ldap_state =
6392 (struct ldapsam_privates *)methods->private_data;
6393 LDAPMessage *entry = NULL;
6394 LDAPMod **mods = NULL;
6395 char *prev_pwd = NULL;
6396 char *trusted_dn = NULL;
6397 struct dom_sid_buf buf;
6398 int rc;
6400 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6403 * get the current entry (if there is one) in order to put the
6404 * current password into the previous password attribute
6406 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6407 return False;
6410 mods = NULL;
6411 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6412 LDAP_OBJ_TRUSTDOM_PASSWORD);
6413 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6414 domain);
6415 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6416 dom_sid_str_buf(sid, &buf));
6417 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6418 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6419 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6420 "sambaClearTextPassword", pwd);
6422 if (entry != NULL) {
6423 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6424 entry, "sambaClearTextPassword", talloc_tos());
6425 if (prev_pwd != NULL) {
6426 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6427 "sambaPreviousClearTextPassword",
6428 prev_pwd);
6432 smbldap_talloc_autofree_ldapmod(talloc_tos(), mods);
6434 trusted_dn = trusteddom_dn(ldap_state, domain);
6435 if (trusted_dn == NULL) {
6436 return False;
6438 if (entry == NULL) {
6439 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6440 } else {
6441 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6444 if (rc != LDAP_SUCCESS) {
6445 DEBUG(1, ("error writing trusted domain password!\n"));
6446 return False;
6449 return True;
6452 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6453 const char *domain)
6455 int rc;
6456 struct ldapsam_privates *ldap_state =
6457 (struct ldapsam_privates *)methods->private_data;
6458 LDAPMessage *entry = NULL;
6459 const char *trusted_dn;
6461 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6462 return False;
6465 if (entry == NULL) {
6466 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6467 "%s\n", domain));
6468 return True;
6471 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6472 entry);
6473 if (trusted_dn == NULL) {
6474 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6475 return False;
6478 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6479 if (rc != LDAP_SUCCESS) {
6480 return False;
6483 return True;
6486 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6487 TALLOC_CTX *mem_ctx,
6488 uint32_t *num_domains,
6489 struct trustdom_info ***domains)
6491 int rc;
6492 struct ldapsam_privates *ldap_state =
6493 (struct ldapsam_privates *)methods->private_data;
6494 char *filter;
6495 int scope = LDAP_SCOPE_SUBTREE;
6496 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6497 int attrsonly = 0; /* 0: return values too */
6498 LDAPMessage *result = NULL;
6499 LDAPMessage *entry = NULL;
6501 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6502 LDAP_OBJ_TRUSTDOM_PASSWORD);
6504 rc = smbldap_search(ldap_state->smbldap_state,
6505 ldap_state->domain_dn,
6506 scope,
6507 filter,
6508 attrs,
6509 attrsonly,
6510 &result);
6512 if (result != NULL) {
6513 smbldap_talloc_autofree_ldapmsg(mem_ctx, result);
6516 if (rc != LDAP_SUCCESS) {
6517 return NT_STATUS_UNSUCCESSFUL;
6520 *num_domains = 0;
6521 if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
6522 DEBUG(1, ("talloc failed\n"));
6523 return NT_STATUS_NO_MEMORY;
6526 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6527 entry != NULL;
6528 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6530 char *dom_name, *dom_sid_str;
6531 struct trustdom_info *dom_info;
6533 dom_info = talloc(*domains, struct trustdom_info);
6534 if (dom_info == NULL) {
6535 DEBUG(1, ("talloc failed\n"));
6536 return NT_STATUS_NO_MEMORY;
6539 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6540 entry,
6541 "sambaDomainName",
6542 talloc_tos());
6543 if (dom_name == NULL) {
6544 DEBUG(1, ("talloc failed\n"));
6545 return NT_STATUS_NO_MEMORY;
6547 dom_info->name = dom_name;
6549 dom_sid_str = smbldap_talloc_single_attribute(
6550 priv2ld(ldap_state), entry, "sambaSID",
6551 talloc_tos());
6552 if (dom_sid_str == NULL) {
6553 DEBUG(1, ("talloc failed\n"));
6554 return NT_STATUS_NO_MEMORY;
6556 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6557 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6558 dom_sid_str));
6559 return NT_STATUS_UNSUCCESSFUL;
6562 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6563 domains, num_domains);
6565 if (*domains == NULL) {
6566 DEBUG(1, ("talloc failed\n"));
6567 return NT_STATUS_NO_MEMORY;
6571 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6572 return NT_STATUS_OK;
6576 /**********************************************************************
6577 Housekeeping
6578 *********************************************************************/
6580 static void free_private_data(void **vp)
6582 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6584 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6586 if ((*ldap_state)->result != NULL) {
6587 ldap_msgfree((*ldap_state)->result);
6588 (*ldap_state)->result = NULL;
6590 if ((*ldap_state)->domain_dn != NULL) {
6591 SAFE_FREE((*ldap_state)->domain_dn);
6594 *ldap_state = NULL;
6596 /* No need to free any further, as it is talloc()ed */
6599 /*********************************************************************
6600 Intitalise the parts of the pdb_methods structure that are common to
6601 all pdb_ldap modes
6602 *********************************************************************/
6604 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6606 NTSTATUS nt_status;
6607 struct ldapsam_privates *ldap_state;
6608 char *bind_dn = NULL;
6609 char *bind_secret = NULL;
6611 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6612 return nt_status;
6615 (*pdb_method)->name = "ldapsam";
6617 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6618 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6619 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6620 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6621 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6622 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6624 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6625 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6626 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6627 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6628 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6629 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6630 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6632 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6633 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6635 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6637 (*pdb_method)->capabilities = ldapsam_capabilities;
6638 (*pdb_method)->new_rid = ldapsam_new_rid;
6640 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6641 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6642 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6643 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6645 /* TODO: Setup private data and free */
6647 if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
6648 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6649 return NT_STATUS_NO_MEMORY;
6652 if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
6653 DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
6654 return NT_STATUS_NO_MEMORY;
6657 nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
6658 location, false, bind_dn, bind_secret,
6659 &ldap_state->smbldap_state);
6660 memset(bind_secret, '\0', strlen(bind_secret));
6661 SAFE_FREE(bind_secret);
6662 SAFE_FREE(bind_dn);
6663 if ( !NT_STATUS_IS_OK(nt_status) ) {
6664 return nt_status;
6667 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6668 return NT_STATUS_NO_MEMORY;
6671 (*pdb_method)->private_data = ldap_state;
6673 (*pdb_method)->free_private_data = free_private_data;
6675 return NT_STATUS_OK;
6678 static bool ldapsam_is_responsible_for_wellknown(struct pdb_methods *m)
6680 return true;
6683 /**********************************************************************
6684 Initialise the normal mode for pdb_ldap
6685 *********************************************************************/
6687 NTSTATUS pdb_ldapsam_init_common(struct pdb_methods **pdb_method,
6688 const char *location)
6690 NTSTATUS nt_status;
6691 struct ldapsam_privates *ldap_state = NULL;
6692 uint32_t alg_rid_base;
6693 char *alg_rid_base_string = NULL;
6694 LDAPMessage *result = NULL;
6695 LDAPMessage *entry = NULL;
6696 struct dom_sid ldap_domain_sid;
6697 struct dom_sid secrets_domain_sid;
6698 char *domain_sid_string = NULL;
6699 char *dn = NULL;
6700 char *uri = talloc_strdup( NULL, location );
6702 trim_char( uri, '\"', '\"' );
6703 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6705 TALLOC_FREE(uri);
6707 if (!NT_STATUS_IS_OK(nt_status)) {
6708 return nt_status;
6711 (*pdb_method)->name = "ldapsam";
6713 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6714 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6715 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6716 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6717 (*pdb_method)->search_users = ldapsam_search_users;
6718 (*pdb_method)->search_groups = ldapsam_search_groups;
6719 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6720 (*pdb_method)->is_responsible_for_wellknown =
6721 ldapsam_is_responsible_for_wellknown;
6723 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6724 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6725 (*pdb_method)->enum_group_memberships =
6726 ldapsam_enum_group_memberships;
6727 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6728 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6729 (*pdb_method)->id_to_sid = ldapsam_id_to_sid;
6731 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6732 (*pdb_method)->create_user = ldapsam_create_user;
6733 (*pdb_method)->delete_user = ldapsam_delete_user;
6734 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6735 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6736 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6737 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6738 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6742 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6743 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6745 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6747 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6748 &result,
6749 ldap_state->domain_name, True);
6751 if ( !NT_STATUS_IS_OK(nt_status) ) {
6752 DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
6753 "info, nor add one to the domain. "
6754 "We cannot work reliably without it.\n"));
6755 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
6758 /* Given that the above might fail, everything below this must be
6759 * optional */
6761 entry = ldap_first_entry(smbldap_get_ldap(ldap_state->smbldap_state),
6762 result);
6763 if (!entry) {
6764 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6765 "entry\n"));
6766 ldap_msgfree(result);
6767 return NT_STATUS_UNSUCCESSFUL;
6770 dn = smbldap_talloc_dn(talloc_tos(),
6771 smbldap_get_ldap(ldap_state->smbldap_state),
6772 entry);
6773 if (!dn) {
6774 ldap_msgfree(result);
6775 return NT_STATUS_UNSUCCESSFUL;
6778 ldap_state->domain_dn = smb_xstrdup(dn);
6779 TALLOC_FREE(dn);
6781 domain_sid_string = smbldap_talloc_single_attribute(
6782 smbldap_get_ldap(ldap_state->smbldap_state),
6783 entry,
6784 get_userattr_key2string(ldap_state->schema_ver,
6785 LDAP_ATTR_USER_SID),
6786 talloc_tos());
6788 if (domain_sid_string) {
6789 bool found_sid;
6790 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6791 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6792 "read as a valid SID\n", domain_sid_string));
6793 ldap_msgfree(result);
6794 TALLOC_FREE(domain_sid_string);
6795 return NT_STATUS_INVALID_PARAMETER;
6797 found_sid = PDB_secrets_fetch_domain_sid(ldap_state->domain_name,
6798 &secrets_domain_sid);
6799 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6800 &ldap_domain_sid)) {
6801 struct dom_sid_buf buf1, buf2;
6802 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6803 "%s based on pdb_ldap results %s -> %s\n",
6804 ldap_state->domain_name,
6805 dom_sid_str_buf(&secrets_domain_sid, &buf1),
6806 dom_sid_str_buf(&ldap_domain_sid, &buf2)));
6808 /* reset secrets.tdb sid */
6809 PDB_secrets_store_domain_sid(ldap_state->domain_name,
6810 &ldap_domain_sid);
6811 DEBUG(1, ("New global sam SID: %s\n",
6812 dom_sid_str_buf(get_global_sam_sid(),
6813 &buf1)));
6815 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6816 TALLOC_FREE(domain_sid_string);
6819 alg_rid_base_string = smbldap_talloc_single_attribute(
6820 smbldap_get_ldap(ldap_state->smbldap_state),
6821 entry,
6822 get_attr_key2string( dominfo_attr_list,
6823 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6824 talloc_tos());
6825 if (alg_rid_base_string) {
6826 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6827 if (alg_rid_base != algorithmic_rid_base()) {
6828 DEBUG(0, ("The value of 'algorithmic RID base' has "
6829 "changed since the LDAP\n"
6830 "database was initialised. Aborting. \n"));
6831 ldap_msgfree(result);
6832 TALLOC_FREE(alg_rid_base_string);
6833 return NT_STATUS_UNSUCCESSFUL;
6835 TALLOC_FREE(alg_rid_base_string);
6837 ldap_msgfree(result);
6839 return NT_STATUS_OK;
6842 NTSTATUS pdb_ldapsam_init(TALLOC_CTX *ctx)
6844 NTSTATUS nt_status;
6846 nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION,
6847 "ldapsam",
6848 pdb_ldapsam_init_common);
6849 if (!NT_STATUS_IS_OK(nt_status)) {
6850 return nt_status;
6853 /* Let pdb_nds register backends */
6854 pdb_nds_init(ctx);
6856 return NT_STATUS_OK;