s3: Fix Coverity ID 2195: NO_EFFECT
[Samba.git] / source3 / passdb / pdb_ldap.c
blob07c56ebd43834149bc099998b8a82ca6d4b67dac
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 "../libcli/auth/libcli_auth.h"
48 #include "secrets.h"
49 #include "idmap_cache.h"
50 #include "../libcli/security/security.h"
52 #undef DBGC_CLASS
53 #define DBGC_CLASS DBGC_PASSDB
55 #include <lber.h>
56 #include <ldap.h>
59 #include "smbldap.h"
61 /**********************************************************************
62 Simple helper function to make stuff better readable
63 **********************************************************************/
65 LDAP *priv2ld(struct ldapsam_privates *priv)
67 return priv->smbldap_state->ldap_struct;
70 /**********************************************************************
71 Get the attribute name given a user schame version.
72 **********************************************************************/
74 static const char* get_userattr_key2string( int schema_ver, int key )
76 switch ( schema_ver ) {
77 case SCHEMAVER_SAMBAACCOUNT:
78 return get_attr_key2string( attrib_map_v22, key );
80 case SCHEMAVER_SAMBASAMACCOUNT:
81 return get_attr_key2string( attrib_map_v30, key );
83 default:
84 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
85 break;
87 return NULL;
90 /**********************************************************************
91 Return the list of attribute names given a user schema version.
92 **********************************************************************/
94 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
96 switch ( schema_ver ) {
97 case SCHEMAVER_SAMBAACCOUNT:
98 return get_attr_list( mem_ctx, attrib_map_v22 );
100 case SCHEMAVER_SAMBASAMACCOUNT:
101 return get_attr_list( mem_ctx, attrib_map_v30 );
102 default:
103 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
104 break;
107 return NULL;
110 /**************************************************************************
111 Return the list of attribute names to delete given a user schema version.
112 **************************************************************************/
114 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
115 int schema_ver )
117 switch ( schema_ver ) {
118 case SCHEMAVER_SAMBAACCOUNT:
119 return get_attr_list( mem_ctx,
120 attrib_map_to_delete_v22 );
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_SAMBAACCOUNT:
146 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
147 break;
148 case SCHEMAVER_SAMBASAMACCOUNT:
149 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
150 break;
151 default:
152 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
153 objclass_filter[0] = '\0';
154 break;
157 result = talloc_strdup(talloc_tos(), objclass_filter);
158 SMB_ASSERT(result != NULL);
159 return result;
162 /*****************************************************************
163 Scan a sequence number off OpenLDAP's syncrepl contextCSN
164 ******************************************************************/
166 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
168 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
169 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
170 LDAPMessage *msg = NULL;
171 LDAPMessage *entry = NULL;
172 TALLOC_CTX *mem_ctx;
173 char **values = NULL;
174 int rc, num_result, num_values, rid;
175 char *suffix = NULL;
176 char *tok;
177 const char *p;
178 const char **attrs;
180 /* Unfortunatly there is no proper way to detect syncrepl-support in
181 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
182 * but do not show up in the root-DSE yet. Neither we can query the
183 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
184 * objectclass. Currently we require lp_ldap_suffix() to show up as
185 * namingContext. - Guenther
188 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
189 return ntstatus;
192 if (!seq_num) {
193 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
194 return ntstatus;
197 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
198 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
199 "as top-level namingContext\n", lp_ldap_suffix()));
200 return ntstatus;
203 mem_ctx = talloc_init("ldapsam_get_seq_num");
205 if (mem_ctx == NULL)
206 return NT_STATUS_NO_MEMORY;
208 if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
209 ntstatus = NT_STATUS_NO_MEMORY;
210 goto done;
213 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
214 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
215 if (rid > 0) {
217 /* consumer syncreplCookie: */
218 /* csn=20050126161620Z#0000001#00#00000 */
219 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
220 attrs[1] = NULL;
221 suffix = talloc_asprintf(mem_ctx,
222 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
223 if (!suffix) {
224 ntstatus = NT_STATUS_NO_MEMORY;
225 goto done;
227 } else {
229 /* provider contextCSN */
230 /* 20050126161620Z#000009#00#000000 */
231 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
232 attrs[1] = NULL;
233 suffix = talloc_asprintf(mem_ctx,
234 "cn=ldapsync,%s", lp_ldap_suffix());
236 if (!suffix) {
237 ntstatus = NT_STATUS_NO_MEMORY;
238 goto done;
242 rc = smbldap_search(ldap_state->smbldap_state, suffix,
243 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
245 if (rc != LDAP_SUCCESS) {
246 goto done;
249 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, 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(ldap_state->smbldap_state->ldap_struct, msg);
256 if (entry == NULL) {
257 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
258 goto done;
261 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
262 if (values == NULL) {
263 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
264 goto done;
267 num_values = ldap_count_values(values);
268 if (num_values == 0) {
269 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
270 goto done;
273 p = values[0];
274 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
275 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
276 goto done;
279 p = tok;
280 if (!strncmp(p, "csn=", strlen("csn=")))
281 p += strlen("csn=");
283 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
285 *seq_num = generalized_to_unix_time(p);
287 /* very basic sanity check */
288 if (*seq_num <= 0) {
289 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
290 (int)*seq_num));
291 goto done;
294 ntstatus = NT_STATUS_OK;
296 done:
297 if (values != NULL)
298 ldap_value_free(values);
299 if (msg != NULL)
300 ldap_msgfree(msg);
301 if (mem_ctx)
302 talloc_destroy(mem_ctx);
304 return ntstatus;
307 /*******************************************************************
308 Run the search by name.
309 ******************************************************************/
311 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
312 const char *user,
313 LDAPMessage ** result,
314 const char **attr)
316 char *filter = NULL;
317 char *escape_user = escape_ldap_string(talloc_tos(), user);
318 int ret = -1;
320 if (!escape_user) {
321 return LDAP_NO_MEMORY;
325 * in the filter expression, replace %u with the real name
326 * so in ldap filter, %u MUST exist :-)
328 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
329 get_objclass_filter(ldap_state->schema_ver));
330 if (!filter) {
331 TALLOC_FREE(escape_user);
332 return LDAP_NO_MEMORY;
335 * have to use this here because $ is filtered out
336 * in string_sub
339 filter = talloc_all_string_sub(talloc_tos(),
340 filter, "%u", escape_user);
341 TALLOC_FREE(escape_user);
342 if (!filter) {
343 return LDAP_NO_MEMORY;
346 ret = smbldap_search_suffix(ldap_state->smbldap_state,
347 filter, attr, result);
348 TALLOC_FREE(filter);
349 return ret;
352 /*******************************************************************
353 Run the search by rid.
354 ******************************************************************/
356 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
357 uint32_t rid, LDAPMessage ** result,
358 const char **attr)
360 char *filter = NULL;
361 int rc;
363 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
364 get_objclass_filter(ldap_state->schema_ver));
365 if (!filter) {
366 return LDAP_NO_MEMORY;
369 rc = smbldap_search_suffix(ldap_state->smbldap_state,
370 filter, attr, result);
371 TALLOC_FREE(filter);
372 return rc;
375 /*******************************************************************
376 Run the search by SID.
377 ******************************************************************/
379 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
380 const struct dom_sid *sid, LDAPMessage ** result,
381 const char **attr)
383 char *filter = NULL;
384 int rc;
385 fstring sid_string;
387 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
388 get_userattr_key2string(ldap_state->schema_ver,
389 LDAP_ATTR_USER_SID),
390 sid_to_fstring(sid_string, sid),
391 get_objclass_filter(ldap_state->schema_ver));
392 if (!filter) {
393 return LDAP_NO_MEMORY;
396 rc = smbldap_search_suffix(ldap_state->smbldap_state,
397 filter, attr, result);
399 TALLOC_FREE(filter);
400 return rc;
403 /*******************************************************************
404 Delete complete object or objectclass and attrs from
405 object found in search_result depending on lp_ldap_delete_dn
406 ******************************************************************/
408 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
409 TALLOC_CTX *mem_ctx,
410 LDAPMessage *entry,
411 const char *objectclass,
412 const char **attrs)
414 LDAPMod **mods = NULL;
415 char *name;
416 const char *dn;
417 BerElement *ptr = NULL;
419 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
420 if (dn == NULL) {
421 return LDAP_NO_MEMORY;
424 if (lp_ldap_delete_dn()) {
425 return smbldap_delete(priv->smbldap_state, dn);
428 /* Ok, delete only the SAM attributes */
430 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
431 name != NULL;
432 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
433 const char **attrib;
435 /* We are only allowed to delete the attributes that
436 really exist. */
438 for (attrib = attrs; *attrib != NULL; attrib++) {
439 if (strequal(*attrib, name)) {
440 DEBUG(10, ("ldapsam_delete_entry: deleting "
441 "attribute %s\n", name));
442 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
443 NULL);
446 ldap_memfree(name);
449 if (ptr != NULL) {
450 ber_free(ptr, 0);
453 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
454 talloc_autofree_ldapmod(mem_ctx, mods);
456 return smbldap_modify(priv->smbldap_state, dn, mods);
459 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
461 char *temp;
462 struct tm tm;
464 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
465 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
466 talloc_tos());
467 if (!temp) {
468 return (time_t) 0;
471 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
472 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
473 (char*)temp));
474 TALLOC_FREE(temp);
475 return (time_t) 0;
477 TALLOC_FREE(temp);
478 tzset();
479 return timegm(&tm);
482 /**********************************************************************
483 Initialize struct samu from an LDAP query.
484 (Based on init_sam_from_buffer in pdb_tdb.c)
485 *********************************************************************/
487 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
488 struct samu * sampass,
489 LDAPMessage * entry)
491 time_t logon_time,
492 logoff_time,
493 kickoff_time,
494 pass_last_set_time,
495 pass_can_change_time,
496 pass_must_change_time,
497 ldap_entry_time,
498 bad_password_time;
499 char *username = NULL,
500 *domain = NULL,
501 *nt_username = NULL,
502 *fullname = NULL,
503 *homedir = NULL,
504 *dir_drive = NULL,
505 *logon_script = NULL,
506 *profile_path = NULL,
507 *acct_desc = NULL,
508 *workstations = NULL,
509 *munged_dial = NULL;
510 uint32_t user_rid;
511 uint8 smblmpwd[LM_HASH_LEN],
512 smbntpwd[NT_HASH_LEN];
513 bool use_samba_attrs = True;
514 uint32_t acct_ctrl = 0;
515 uint16_t logon_divs;
516 uint16_t bad_password_count = 0,
517 logon_count = 0;
518 uint32_t hours_len;
519 uint8 hours[MAX_HOURS_LEN];
520 char *temp = NULL;
521 struct login_cache cache_entry;
522 uint32_t pwHistLen;
523 bool expand_explicit = lp_passdb_expand_explicit();
524 bool ret = false;
525 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
527 if (!ctx) {
528 return false;
530 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
531 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
532 goto fn_exit;
535 if (priv2ld(ldap_state) == NULL) {
536 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
537 "ldap_struct is NULL!\n"));
538 goto fn_exit;
541 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
542 entry,
543 "uid",
544 ctx))) {
545 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
546 "this user!\n"));
547 goto fn_exit;
550 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
552 nt_username = talloc_strdup(ctx, username);
553 if (!nt_username) {
554 goto fn_exit;
557 domain = talloc_strdup(ctx, ldap_state->domain_name);
558 if (!domain) {
559 goto fn_exit;
562 pdb_set_username(sampass, username, PDB_SET);
564 pdb_set_domain(sampass, domain, PDB_DEFAULT);
565 pdb_set_nt_username(sampass, nt_username, PDB_SET);
567 /* deal with different attributes between the schema first */
569 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
570 if ((temp = smbldap_talloc_single_attribute(
571 ldap_state->smbldap_state->ldap_struct,
572 entry,
573 get_userattr_key2string(ldap_state->schema_ver,
574 LDAP_ATTR_USER_SID),
575 ctx))!=NULL) {
576 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
578 } else {
579 if ((temp = smbldap_talloc_single_attribute(
580 ldap_state->smbldap_state->ldap_struct,
581 entry,
582 get_userattr_key2string(ldap_state->schema_ver,
583 LDAP_ATTR_USER_RID),
584 ctx))!=NULL) {
585 user_rid = (uint32_t)atol(temp);
586 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
590 if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
591 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
592 get_userattr_key2string(ldap_state->schema_ver,
593 LDAP_ATTR_USER_SID),
594 get_userattr_key2string(ldap_state->schema_ver,
595 LDAP_ATTR_USER_RID),
596 username));
597 return False;
600 temp = smbldap_talloc_single_attribute(
601 ldap_state->smbldap_state->ldap_struct,
602 entry,
603 get_userattr_key2string(ldap_state->schema_ver,
604 LDAP_ATTR_PWD_LAST_SET),
605 ctx);
606 if (temp) {
607 pass_last_set_time = (time_t) atol(temp);
608 pdb_set_pass_last_set_time(sampass,
609 pass_last_set_time, PDB_SET);
612 temp = smbldap_talloc_single_attribute(
613 ldap_state->smbldap_state->ldap_struct,
614 entry,
615 get_userattr_key2string(ldap_state->schema_ver,
616 LDAP_ATTR_LOGON_TIME),
617 ctx);
618 if (temp) {
619 logon_time = (time_t) atol(temp);
620 pdb_set_logon_time(sampass, logon_time, PDB_SET);
623 temp = smbldap_talloc_single_attribute(
624 ldap_state->smbldap_state->ldap_struct,
625 entry,
626 get_userattr_key2string(ldap_state->schema_ver,
627 LDAP_ATTR_LOGOFF_TIME),
628 ctx);
629 if (temp) {
630 logoff_time = (time_t) atol(temp);
631 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
634 temp = smbldap_talloc_single_attribute(
635 ldap_state->smbldap_state->ldap_struct,
636 entry,
637 get_userattr_key2string(ldap_state->schema_ver,
638 LDAP_ATTR_KICKOFF_TIME),
639 ctx);
640 if (temp) {
641 kickoff_time = (time_t) atol(temp);
642 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
645 temp = smbldap_talloc_single_attribute(
646 ldap_state->smbldap_state->ldap_struct,
647 entry,
648 get_userattr_key2string(ldap_state->schema_ver,
649 LDAP_ATTR_PWD_CAN_CHANGE),
650 ctx);
651 if (temp) {
652 pass_can_change_time = (time_t) atol(temp);
653 pdb_set_pass_can_change_time(sampass,
654 pass_can_change_time, PDB_SET);
657 temp = smbldap_talloc_single_attribute(
658 ldap_state->smbldap_state->ldap_struct,
659 entry,
660 get_userattr_key2string(ldap_state->schema_ver,
661 LDAP_ATTR_PWD_MUST_CHANGE),
662 ctx);
663 if (temp) {
664 pass_must_change_time = (time_t) atol(temp);
665 pdb_set_pass_must_change_time(sampass,
666 pass_must_change_time, PDB_SET);
669 /* recommend that 'gecos' and 'displayName' should refer to the same
670 * attribute OID. userFullName depreciated, only used by Samba
671 * primary rules of LDAP: don't make a new attribute when one is already defined
672 * that fits your needs; using cn then displayName rather than 'userFullName'
675 fullname = smbldap_talloc_single_attribute(
676 ldap_state->smbldap_state->ldap_struct,
677 entry,
678 get_userattr_key2string(ldap_state->schema_ver,
679 LDAP_ATTR_DISPLAY_NAME),
680 ctx);
681 if (fullname) {
682 pdb_set_fullname(sampass, fullname, PDB_SET);
683 } else {
684 fullname = smbldap_talloc_single_attribute(
685 ldap_state->smbldap_state->ldap_struct,
686 entry,
687 get_userattr_key2string(ldap_state->schema_ver,
688 LDAP_ATTR_CN),
689 ctx);
690 if (fullname) {
691 pdb_set_fullname(sampass, fullname, PDB_SET);
695 dir_drive = smbldap_talloc_single_attribute(
696 ldap_state->smbldap_state->ldap_struct,
697 entry,
698 get_userattr_key2string(ldap_state->schema_ver,
699 LDAP_ATTR_HOME_DRIVE),
700 ctx);
701 if (dir_drive) {
702 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
703 } else {
704 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
707 homedir = smbldap_talloc_single_attribute(
708 ldap_state->smbldap_state->ldap_struct,
709 entry,
710 get_userattr_key2string(ldap_state->schema_ver,
711 LDAP_ATTR_HOME_PATH),
712 ctx);
713 if (homedir) {
714 if (expand_explicit) {
715 homedir = talloc_sub_basic(ctx,
716 username,
717 domain,
718 homedir);
719 if (!homedir) {
720 goto fn_exit;
723 pdb_set_homedir(sampass, homedir, PDB_SET);
724 } else {
725 pdb_set_homedir(sampass,
726 talloc_sub_basic(ctx, username, domain,
727 lp_logon_home()),
728 PDB_DEFAULT);
731 logon_script = smbldap_talloc_single_attribute(
732 ldap_state->smbldap_state->ldap_struct,
733 entry,
734 get_userattr_key2string(ldap_state->schema_ver,
735 LDAP_ATTR_LOGON_SCRIPT),
736 ctx);
737 if (logon_script) {
738 if (expand_explicit) {
739 logon_script = talloc_sub_basic(ctx,
740 username,
741 domain,
742 logon_script);
743 if (!logon_script) {
744 goto fn_exit;
747 pdb_set_logon_script(sampass, logon_script, PDB_SET);
748 } else {
749 pdb_set_logon_script(sampass,
750 talloc_sub_basic(ctx, username, domain,
751 lp_logon_script()),
752 PDB_DEFAULT );
755 profile_path = smbldap_talloc_single_attribute(
756 ldap_state->smbldap_state->ldap_struct,
757 entry,
758 get_userattr_key2string(ldap_state->schema_ver,
759 LDAP_ATTR_PROFILE_PATH),
760 ctx);
761 if (profile_path) {
762 if (expand_explicit) {
763 profile_path = talloc_sub_basic(ctx,
764 username,
765 domain,
766 profile_path);
767 if (!profile_path) {
768 goto fn_exit;
771 pdb_set_profile_path(sampass, profile_path, PDB_SET);
772 } else {
773 pdb_set_profile_path(sampass,
774 talloc_sub_basic(ctx, username, domain,
775 lp_logon_path()),
776 PDB_DEFAULT );
779 acct_desc = smbldap_talloc_single_attribute(
780 ldap_state->smbldap_state->ldap_struct,
781 entry,
782 get_userattr_key2string(ldap_state->schema_ver,
783 LDAP_ATTR_DESC),
784 ctx);
785 if (acct_desc) {
786 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
789 workstations = smbldap_talloc_single_attribute(
790 ldap_state->smbldap_state->ldap_struct,
791 entry,
792 get_userattr_key2string(ldap_state->schema_ver,
793 LDAP_ATTR_USER_WKS),
794 ctx);
795 if (workstations) {
796 pdb_set_workstations(sampass, workstations, PDB_SET);
799 munged_dial = smbldap_talloc_single_attribute(
800 ldap_state->smbldap_state->ldap_struct,
801 entry,
802 get_userattr_key2string(ldap_state->schema_ver,
803 LDAP_ATTR_MUNGED_DIAL),
804 ctx);
805 if (munged_dial) {
806 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
809 /* FIXME: hours stuff should be cleaner */
811 logon_divs = 168;
812 hours_len = 21;
813 memset(hours, 0xff, hours_len);
815 if (ldap_state->is_nds_ldap) {
816 char *user_dn;
817 size_t pwd_len;
818 char clear_text_pw[512];
820 /* Make call to Novell eDirectory ldap extension to get clear text password.
821 NOTE: This will only work if we have an SSL connection to eDirectory. */
822 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
823 if (user_dn != NULL) {
824 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
826 pwd_len = sizeof(clear_text_pw);
827 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
828 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
829 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
830 TALLOC_FREE(user_dn);
831 return False;
833 ZERO_STRUCT(smblmpwd);
834 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
835 TALLOC_FREE(user_dn);
836 return False;
838 ZERO_STRUCT(smbntpwd);
839 use_samba_attrs = False;
842 TALLOC_FREE(user_dn);
844 } else {
845 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
849 if (use_samba_attrs) {
850 temp = smbldap_talloc_single_attribute(
851 ldap_state->smbldap_state->ldap_struct,
852 entry,
853 get_userattr_key2string(ldap_state->schema_ver,
854 LDAP_ATTR_LMPW),
855 ctx);
856 if (temp) {
857 pdb_gethexpwd(temp, smblmpwd);
858 memset((char *)temp, '\0', strlen(temp)+1);
859 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
860 goto fn_exit;
862 ZERO_STRUCT(smblmpwd);
865 temp = smbldap_talloc_single_attribute(
866 ldap_state->smbldap_state->ldap_struct,
867 entry,
868 get_userattr_key2string(ldap_state->schema_ver,
869 LDAP_ATTR_NTPW),
870 ctx);
871 if (temp) {
872 pdb_gethexpwd(temp, smbntpwd);
873 memset((char *)temp, '\0', strlen(temp)+1);
874 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
875 goto fn_exit;
877 ZERO_STRUCT(smbntpwd);
881 pwHistLen = 0;
883 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
884 if (pwHistLen > 0){
885 uint8 *pwhist = NULL;
886 int i;
887 char *history_string = TALLOC_ARRAY(ctx, char,
888 MAX_PW_HISTORY_LEN*64);
890 if (!history_string) {
891 goto fn_exit;
894 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
896 pwhist = TALLOC_ARRAY(ctx, uint8,
897 pwHistLen * PW_HISTORY_ENTRY_LEN);
898 if (pwhist == NULL) {
899 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
900 goto fn_exit;
902 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
904 if (smbldap_get_single_attribute(
905 ldap_state->smbldap_state->ldap_struct,
906 entry,
907 get_userattr_key2string(ldap_state->schema_ver,
908 LDAP_ATTR_PWD_HISTORY),
909 history_string,
910 MAX_PW_HISTORY_LEN*64)) {
911 bool hex_failed = false;
912 for (i = 0; i < pwHistLen; i++){
913 /* Get the 16 byte salt. */
914 if (!pdb_gethexpwd(&history_string[i*64],
915 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
916 hex_failed = true;
917 break;
919 /* Get the 16 byte MD5 hash of salt+passwd. */
920 if (!pdb_gethexpwd(&history_string[(i*64)+32],
921 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
922 PW_HISTORY_SALT_LEN])) {
923 hex_failed = True;
924 break;
927 if (hex_failed) {
928 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
929 username));
930 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
933 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
934 goto fn_exit;
938 temp = smbldap_talloc_single_attribute(
939 ldap_state->smbldap_state->ldap_struct,
940 entry,
941 get_userattr_key2string(ldap_state->schema_ver,
942 LDAP_ATTR_ACB_INFO),
943 ctx);
944 if (temp) {
945 acct_ctrl = pdb_decode_acct_ctrl(temp);
947 if (acct_ctrl == 0) {
948 acct_ctrl |= ACB_NORMAL;
951 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
952 } else {
953 acct_ctrl |= ACB_NORMAL;
956 pdb_set_hours_len(sampass, hours_len, PDB_SET);
957 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
959 temp = smbldap_talloc_single_attribute(
960 ldap_state->smbldap_state->ldap_struct,
961 entry,
962 get_userattr_key2string(ldap_state->schema_ver,
963 LDAP_ATTR_BAD_PASSWORD_COUNT),
964 ctx);
965 if (temp) {
966 bad_password_count = (uint32_t) atol(temp);
967 pdb_set_bad_password_count(sampass,
968 bad_password_count, PDB_SET);
971 temp = smbldap_talloc_single_attribute(
972 ldap_state->smbldap_state->ldap_struct,
973 entry,
974 get_userattr_key2string(ldap_state->schema_ver,
975 LDAP_ATTR_BAD_PASSWORD_TIME),
976 ctx);
977 if (temp) {
978 bad_password_time = (time_t) atol(temp);
979 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
983 temp = smbldap_talloc_single_attribute(
984 ldap_state->smbldap_state->ldap_struct,
985 entry,
986 get_userattr_key2string(ldap_state->schema_ver,
987 LDAP_ATTR_LOGON_COUNT),
988 ctx);
989 if (temp) {
990 logon_count = (uint32_t) atol(temp);
991 pdb_set_logon_count(sampass, logon_count, PDB_SET);
994 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
996 temp = smbldap_talloc_single_attribute(
997 ldap_state->smbldap_state->ldap_struct,
998 entry,
999 get_userattr_key2string(ldap_state->schema_ver,
1000 LDAP_ATTR_LOGON_HOURS),
1001 ctx);
1002 if (temp) {
1003 pdb_gethexhours(temp, hours);
1004 memset((char *)temp, '\0', strlen(temp) +1);
1005 pdb_set_hours(sampass, hours, hours_len, PDB_SET);
1006 ZERO_STRUCT(hours);
1009 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1010 struct passwd unix_pw;
1011 bool have_uid = false;
1012 bool have_gid = false;
1013 struct dom_sid mapped_gsid;
1014 const struct dom_sid *primary_gsid;
1016 ZERO_STRUCT(unix_pw);
1018 unix_pw.pw_name = username;
1019 unix_pw.pw_passwd = discard_const_p(char, "x");
1021 temp = smbldap_talloc_single_attribute(
1022 priv2ld(ldap_state),
1023 entry,
1024 "uidNumber",
1025 ctx);
1026 if (temp) {
1027 /* We've got a uid, feed the cache */
1028 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1029 have_uid = true;
1031 temp = smbldap_talloc_single_attribute(
1032 priv2ld(ldap_state),
1033 entry,
1034 "gidNumber",
1035 ctx);
1036 if (temp) {
1037 /* We've got a uid, feed the cache */
1038 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1039 have_gid = true;
1041 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1042 priv2ld(ldap_state),
1043 entry,
1044 "gecos",
1045 ctx);
1046 if (unix_pw.pw_gecos) {
1047 unix_pw.pw_gecos = fullname;
1049 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1050 priv2ld(ldap_state),
1051 entry,
1052 "homeDirectory",
1053 ctx);
1054 if (unix_pw.pw_dir) {
1055 unix_pw.pw_dir = discard_const_p(char, "");
1057 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1058 priv2ld(ldap_state),
1059 entry,
1060 "loginShell",
1061 ctx);
1062 if (unix_pw.pw_shell) {
1063 unix_pw.pw_shell = discard_const_p(char, "");
1066 if (have_uid && have_gid) {
1067 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1068 } else {
1069 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1072 if (sampass->unix_pw == NULL) {
1073 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1074 pdb_get_username(sampass)));
1075 goto fn_exit;
1078 store_uid_sid_cache(pdb_get_user_sid(sampass),
1079 sampass->unix_pw->pw_uid);
1080 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
1081 sampass->unix_pw->pw_uid);
1083 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1084 primary_gsid = pdb_get_group_sid(sampass);
1085 if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
1086 store_gid_sid_cache(primary_gsid,
1087 sampass->unix_pw->pw_gid);
1088 idmap_cache_set_sid2gid(primary_gsid,
1089 sampass->unix_pw->pw_gid);
1093 /* check the timestamp of the cache vs ldap entry */
1094 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1095 entry))) {
1096 ret = true;
1097 goto fn_exit;
1100 /* see if we have newer updates */
1101 if (!login_cache_read(sampass, &cache_entry)) {
1102 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1103 (unsigned int)pdb_get_bad_password_count(sampass),
1104 (unsigned int)pdb_get_bad_password_time(sampass)));
1105 ret = true;
1106 goto fn_exit;
1109 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1110 (unsigned int)ldap_entry_time,
1111 (unsigned int)cache_entry.entry_timestamp,
1112 (unsigned int)cache_entry.bad_password_time));
1114 if (ldap_entry_time > cache_entry.entry_timestamp) {
1115 /* cache is older than directory , so
1116 we need to delete the entry but allow the
1117 fields to be written out */
1118 login_cache_delentry(sampass);
1119 } else {
1120 /* read cache in */
1121 pdb_set_acct_ctrl(sampass,
1122 pdb_get_acct_ctrl(sampass) |
1123 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
1124 PDB_SET);
1125 pdb_set_bad_password_count(sampass,
1126 cache_entry.bad_password_count,
1127 PDB_SET);
1128 pdb_set_bad_password_time(sampass,
1129 cache_entry.bad_password_time,
1130 PDB_SET);
1133 ret = true;
1135 fn_exit:
1137 TALLOC_FREE(ctx);
1138 return ret;
1141 /**********************************************************************
1142 Initialize the ldap db from a struct samu. Called on update.
1143 (Based on init_buffer_from_sam in pdb_tdb.c)
1144 *********************************************************************/
1146 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1147 LDAPMessage *existing,
1148 LDAPMod *** mods, struct samu * sampass,
1149 bool (*need_update)(const struct samu *,
1150 enum pdb_elements))
1152 char *temp = NULL;
1153 uint32_t rid;
1155 if (mods == NULL || sampass == NULL) {
1156 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1157 return False;
1160 *mods = NULL;
1163 * took out adding "objectclass: sambaAccount"
1164 * do this on a per-mod basis
1166 if (need_update(sampass, PDB_USERNAME)) {
1167 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1168 "uid", pdb_get_username(sampass));
1169 if (ldap_state->is_nds_ldap) {
1170 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1171 "cn", pdb_get_username(sampass));
1172 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1173 "sn", pdb_get_username(sampass));
1177 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1179 /* only update the RID if we actually need to */
1180 if (need_update(sampass, PDB_USERSID)) {
1181 fstring sid_string;
1182 const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
1184 switch ( ldap_state->schema_ver ) {
1185 case SCHEMAVER_SAMBAACCOUNT:
1186 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1187 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1188 sid_string_dbg(user_sid),
1189 sid_string_dbg(
1190 &ldap_state->domain_sid)));
1191 return False;
1193 if (asprintf(&temp, "%i", rid) < 0) {
1194 return false;
1196 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1197 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1198 temp);
1199 SAFE_FREE(temp);
1200 break;
1202 case SCHEMAVER_SAMBASAMACCOUNT:
1203 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1204 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1205 sid_to_fstring(sid_string, user_sid));
1206 break;
1208 default:
1209 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1210 break;
1214 /* we don't need to store the primary group RID - so leaving it
1215 'free' to hang off the unix primary group makes life easier */
1217 if (need_update(sampass, PDB_GROUPSID)) {
1218 fstring sid_string;
1219 const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
1221 switch ( ldap_state->schema_ver ) {
1222 case SCHEMAVER_SAMBAACCOUNT:
1223 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1224 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1225 sid_string_dbg(group_sid),
1226 sid_string_dbg(
1227 &ldap_state->domain_sid)));
1228 return False;
1231 if (asprintf(&temp, "%i", rid) < 0) {
1232 return false;
1234 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1235 get_userattr_key2string(ldap_state->schema_ver,
1236 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1237 SAFE_FREE(temp);
1238 break;
1240 case SCHEMAVER_SAMBASAMACCOUNT:
1241 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1242 get_userattr_key2string(ldap_state->schema_ver,
1243 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1244 break;
1246 default:
1247 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1248 break;
1253 /* displayName, cn, and gecos should all be the same
1254 * most easily accomplished by giving them the same OID
1255 * gecos isn't set here b/c it should be handled by the
1256 * add-user script
1257 * We change displayName only and fall back to cn if
1258 * it does not exist.
1261 if (need_update(sampass, PDB_FULLNAME))
1262 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1263 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1264 pdb_get_fullname(sampass));
1266 if (need_update(sampass, PDB_ACCTDESC))
1267 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1268 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1269 pdb_get_acct_desc(sampass));
1271 if (need_update(sampass, PDB_WORKSTATIONS))
1272 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1273 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1274 pdb_get_workstations(sampass));
1276 if (need_update(sampass, PDB_MUNGEDDIAL))
1277 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1278 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1279 pdb_get_munged_dial(sampass));
1281 if (need_update(sampass, PDB_SMBHOME))
1282 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1283 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1284 pdb_get_homedir(sampass));
1286 if (need_update(sampass, PDB_DRIVE))
1287 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1288 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1289 pdb_get_dir_drive(sampass));
1291 if (need_update(sampass, PDB_LOGONSCRIPT))
1292 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1293 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1294 pdb_get_logon_script(sampass));
1296 if (need_update(sampass, PDB_PROFILE))
1297 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1298 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1299 pdb_get_profile_path(sampass));
1301 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1302 return false;
1304 if (need_update(sampass, PDB_LOGONTIME))
1305 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1306 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1307 SAFE_FREE(temp);
1309 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1310 return false;
1312 if (need_update(sampass, PDB_LOGOFFTIME))
1313 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1314 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1315 SAFE_FREE(temp);
1317 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1318 return false;
1320 if (need_update(sampass, PDB_KICKOFFTIME))
1321 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1322 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1323 SAFE_FREE(temp);
1325 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1326 return false;
1328 if (need_update(sampass, PDB_CANCHANGETIME))
1329 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1330 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1331 SAFE_FREE(temp);
1333 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1334 return false;
1336 if (need_update(sampass, PDB_MUSTCHANGETIME))
1337 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1338 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1339 SAFE_FREE(temp);
1341 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1342 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1344 if (need_update(sampass, PDB_LMPASSWD)) {
1345 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1346 if (lm_pw) {
1347 char pwstr[34];
1348 pdb_sethexpwd(pwstr, lm_pw,
1349 pdb_get_acct_ctrl(sampass));
1350 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1351 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1352 pwstr);
1353 } else {
1354 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1355 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1356 NULL);
1359 if (need_update(sampass, PDB_NTPASSWD)) {
1360 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1361 if (nt_pw) {
1362 char pwstr[34];
1363 pdb_sethexpwd(pwstr, nt_pw,
1364 pdb_get_acct_ctrl(sampass));
1365 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1366 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1367 pwstr);
1368 } else {
1369 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1370 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1371 NULL);
1375 if (need_update(sampass, PDB_PWHISTORY)) {
1376 char *pwstr = NULL;
1377 uint32_t pwHistLen = 0;
1378 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1380 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1381 if (!pwstr) {
1382 return false;
1384 if (pwHistLen == 0) {
1385 /* Remove any password history from the LDAP store. */
1386 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1387 pwstr[64] = '\0';
1388 } else {
1389 int i;
1390 uint32_t currHistLen = 0;
1391 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1392 if (pwhist != NULL) {
1393 /* We can only store (1024-1/64 password history entries. */
1394 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1395 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1396 /* Store the salt. */
1397 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1398 /* Followed by the md5 hash of salt + md4 hash */
1399 pdb_sethexpwd(&pwstr[(i*64)+32],
1400 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1401 DEBUG(100, ("pwstr=%s\n", pwstr));
1405 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1406 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1407 pwstr);
1408 SAFE_FREE(pwstr);
1411 if (need_update(sampass, PDB_PASSLASTSET)) {
1412 if (asprintf(&temp, "%li",
1413 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1414 return false;
1416 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1417 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1418 temp);
1419 SAFE_FREE(temp);
1423 if (need_update(sampass, PDB_HOURS)) {
1424 const uint8 *hours = pdb_get_hours(sampass);
1425 if (hours) {
1426 char hourstr[44];
1427 pdb_sethexhours(hourstr, hours);
1428 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1429 existing,
1430 mods,
1431 get_userattr_key2string(ldap_state->schema_ver,
1432 LDAP_ATTR_LOGON_HOURS),
1433 hourstr);
1437 if (need_update(sampass, PDB_ACCTCTRL))
1438 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1439 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1440 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1442 /* password lockout cache:
1443 - If we are now autolocking or clearing, we write to ldap
1444 - If we are clearing, we delete the cache entry
1445 - If the count is > 0, we update the cache
1447 This even means when autolocking, we cache, just in case the
1448 update doesn't work, and we have to cache the autolock flag */
1450 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1451 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1452 uint16_t badcount = pdb_get_bad_password_count(sampass);
1453 time_t badtime = pdb_get_bad_password_time(sampass);
1454 uint32_t pol;
1455 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1457 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1458 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1460 if ((badcount >= pol) || (badcount == 0)) {
1461 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1462 (unsigned int)badcount, (unsigned int)badtime));
1463 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1464 return false;
1466 smbldap_make_mod(
1467 ldap_state->smbldap_state->ldap_struct,
1468 existing, mods,
1469 get_userattr_key2string(
1470 ldap_state->schema_ver,
1471 LDAP_ATTR_BAD_PASSWORD_COUNT),
1472 temp);
1473 SAFE_FREE(temp);
1475 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1476 return false;
1478 smbldap_make_mod(
1479 ldap_state->smbldap_state->ldap_struct,
1480 existing, mods,
1481 get_userattr_key2string(
1482 ldap_state->schema_ver,
1483 LDAP_ATTR_BAD_PASSWORD_TIME),
1484 temp);
1485 SAFE_FREE(temp);
1487 if (badcount == 0) {
1488 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1489 login_cache_delentry(sampass);
1490 } else {
1491 struct login_cache cache_entry;
1493 cache_entry.entry_timestamp = time(NULL);
1494 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1495 cache_entry.bad_password_count = badcount;
1496 cache_entry.bad_password_time = badtime;
1498 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1499 login_cache_write(sampass, &cache_entry);
1503 return True;
1506 /**********************************************************************
1507 End enumeration of the LDAP password list.
1508 *********************************************************************/
1510 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1512 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1513 if (ldap_state->result) {
1514 ldap_msgfree(ldap_state->result);
1515 ldap_state->result = NULL;
1519 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1520 const char *new_attr)
1522 int i;
1524 if (new_attr == NULL) {
1525 return;
1528 for (i=0; (*attr_list)[i] != NULL; i++) {
1532 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1533 const char *, i+2);
1534 SMB_ASSERT((*attr_list) != NULL);
1535 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1536 (*attr_list)[i+1] = NULL;
1539 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1540 const char ***attr_list)
1542 append_attr(mem_ctx, attr_list, "uidNumber");
1543 append_attr(mem_ctx, attr_list, "gidNumber");
1544 append_attr(mem_ctx, attr_list, "homeDirectory");
1545 append_attr(mem_ctx, attr_list, "loginShell");
1546 append_attr(mem_ctx, attr_list, "gecos");
1549 /**********************************************************************
1550 Get struct samu entry from LDAP by username.
1551 *********************************************************************/
1553 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1555 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1556 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1557 LDAPMessage *result = NULL;
1558 LDAPMessage *entry = NULL;
1559 int count;
1560 const char ** attr_list;
1561 int rc;
1563 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1564 append_attr(user, &attr_list,
1565 get_userattr_key2string(ldap_state->schema_ver,
1566 LDAP_ATTR_MOD_TIMESTAMP));
1567 ldapsam_add_unix_attributes(user, &attr_list);
1568 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1569 attr_list);
1570 TALLOC_FREE( attr_list );
1572 if ( rc != LDAP_SUCCESS )
1573 return NT_STATUS_NO_SUCH_USER;
1575 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1577 if (count < 1) {
1578 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1579 ldap_msgfree(result);
1580 return NT_STATUS_NO_SUCH_USER;
1581 } else if (count > 1) {
1582 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1583 ldap_msgfree(result);
1584 return NT_STATUS_NO_SUCH_USER;
1587 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1588 if (entry) {
1589 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1590 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1591 ldap_msgfree(result);
1592 return NT_STATUS_NO_SUCH_USER;
1594 pdb_set_backend_private_data(user, result, NULL,
1595 my_methods, PDB_CHANGED);
1596 talloc_autofree_ldapmsg(user, result);
1597 ret = NT_STATUS_OK;
1598 } else {
1599 ldap_msgfree(result);
1601 return ret;
1604 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1605 const struct dom_sid *sid, LDAPMessage **result)
1607 int rc = -1;
1608 const char ** attr_list;
1609 uint32_t rid;
1611 switch ( ldap_state->schema_ver ) {
1612 case SCHEMAVER_SAMBASAMACCOUNT: {
1613 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1614 if (tmp_ctx == NULL) {
1615 return LDAP_NO_MEMORY;
1618 attr_list = get_userattr_list(tmp_ctx,
1619 ldap_state->schema_ver);
1620 append_attr(tmp_ctx, &attr_list,
1621 get_userattr_key2string(
1622 ldap_state->schema_ver,
1623 LDAP_ATTR_MOD_TIMESTAMP));
1624 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1625 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1626 result, attr_list);
1627 TALLOC_FREE(tmp_ctx);
1629 if ( rc != LDAP_SUCCESS )
1630 return rc;
1631 break;
1634 case SCHEMAVER_SAMBAACCOUNT:
1635 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1636 return rc;
1639 attr_list = get_userattr_list(NULL,
1640 ldap_state->schema_ver);
1641 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1642 TALLOC_FREE( attr_list );
1644 if ( rc != LDAP_SUCCESS )
1645 return rc;
1646 break;
1648 return rc;
1651 /**********************************************************************
1652 Get struct samu entry from LDAP by SID.
1653 *********************************************************************/
1655 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1657 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1658 LDAPMessage *result = NULL;
1659 LDAPMessage *entry = NULL;
1660 int count;
1661 int rc;
1663 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1664 sid, &result);
1665 if (rc != LDAP_SUCCESS)
1666 return NT_STATUS_NO_SUCH_USER;
1668 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1670 if (count < 1) {
1671 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1672 "count=%d\n", sid_string_dbg(sid), count));
1673 ldap_msgfree(result);
1674 return NT_STATUS_NO_SUCH_USER;
1675 } else if (count > 1) {
1676 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1677 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1678 count));
1679 ldap_msgfree(result);
1680 return NT_STATUS_NO_SUCH_USER;
1683 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1684 if (!entry) {
1685 ldap_msgfree(result);
1686 return NT_STATUS_NO_SUCH_USER;
1689 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1690 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1691 ldap_msgfree(result);
1692 return NT_STATUS_NO_SUCH_USER;
1695 pdb_set_backend_private_data(user, result, NULL,
1696 my_methods, PDB_CHANGED);
1697 talloc_autofree_ldapmsg(user, result);
1698 return NT_STATUS_OK;
1701 /********************************************************************
1702 Do the actual modification - also change a plaintext passord if
1703 it it set.
1704 **********************************************************************/
1706 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1707 struct samu *newpwd, char *dn,
1708 LDAPMod **mods, int ldap_op,
1709 bool (*need_update)(const struct samu *, enum pdb_elements))
1711 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1712 int rc;
1714 if (!newpwd || !dn) {
1715 return NT_STATUS_INVALID_PARAMETER;
1718 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1719 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1720 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1721 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1722 BerElement *ber;
1723 struct berval *bv;
1724 char *retoid = NULL;
1725 struct berval *retdata = NULL;
1726 char *utf8_password;
1727 char *utf8_dn;
1728 size_t converted_size;
1729 int ret;
1731 if (!ldap_state->is_nds_ldap) {
1733 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1734 LDAP_EXOP_MODIFY_PASSWD)) {
1735 DEBUG(2, ("ldap password change requested, but LDAP "
1736 "server does not support it -- ignoring\n"));
1737 return NT_STATUS_OK;
1741 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1742 pdb_get_plaintext_passwd(newpwd),
1743 &converted_size))
1745 return NT_STATUS_NO_MEMORY;
1748 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1749 TALLOC_FREE(utf8_password);
1750 return NT_STATUS_NO_MEMORY;
1753 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1754 DEBUG(0,("ber_alloc_t returns NULL\n"));
1755 TALLOC_FREE(utf8_password);
1756 TALLOC_FREE(utf8_dn);
1757 return NT_STATUS_UNSUCCESSFUL;
1760 if ((ber_printf (ber, "{") < 0) ||
1761 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1762 utf8_dn) < 0)) {
1763 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1764 "value <0\n"));
1765 ber_free(ber,1);
1766 TALLOC_FREE(utf8_dn);
1767 TALLOC_FREE(utf8_password);
1768 return NT_STATUS_UNSUCCESSFUL;
1771 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1772 ret = ber_printf(ber, "ts}",
1773 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1774 utf8_password);
1775 } else {
1776 ret = ber_printf(ber, "}");
1779 if (ret < 0) {
1780 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1781 "value <0\n"));
1782 ber_free(ber,1);
1783 TALLOC_FREE(utf8_dn);
1784 TALLOC_FREE(utf8_password);
1785 return NT_STATUS_UNSUCCESSFUL;
1788 if ((rc = ber_flatten (ber, &bv))<0) {
1789 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1790 ber_free(ber,1);
1791 TALLOC_FREE(utf8_dn);
1792 TALLOC_FREE(utf8_password);
1793 return NT_STATUS_UNSUCCESSFUL;
1796 TALLOC_FREE(utf8_dn);
1797 TALLOC_FREE(utf8_password);
1798 ber_free(ber, 1);
1800 if (!ldap_state->is_nds_ldap) {
1801 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1802 LDAP_EXOP_MODIFY_PASSWD,
1803 bv, NULL, NULL, &retoid,
1804 &retdata);
1805 } else {
1806 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1807 pdb_get_plaintext_passwd(newpwd));
1809 if (rc != LDAP_SUCCESS) {
1810 char *ld_error = NULL;
1812 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1813 DEBUG(3, ("Could not set userPassword "
1814 "attribute due to an objectClass "
1815 "violation -- ignoring\n"));
1816 ber_bvfree(bv);
1817 return NT_STATUS_OK;
1820 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1821 &ld_error);
1822 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1823 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1824 SAFE_FREE(ld_error);
1825 ber_bvfree(bv);
1826 #if defined(LDAP_CONSTRAINT_VIOLATION)
1827 if (rc == LDAP_CONSTRAINT_VIOLATION)
1828 return NT_STATUS_PASSWORD_RESTRICTION;
1829 #endif
1830 return NT_STATUS_UNSUCCESSFUL;
1831 } else {
1832 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1833 #ifdef DEBUG_PASSWORD
1834 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1835 #endif
1836 if (retdata)
1837 ber_bvfree(retdata);
1838 if (retoid)
1839 ldap_memfree(retoid);
1841 ber_bvfree(bv);
1844 if (!mods) {
1845 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1846 /* may be password change below however */
1847 } else {
1848 switch(ldap_op) {
1849 case LDAP_MOD_ADD:
1850 if (ldap_state->is_nds_ldap) {
1851 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1852 "objectclass",
1853 "inetOrgPerson");
1854 } else {
1855 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1856 "objectclass",
1857 LDAP_OBJ_ACCOUNT);
1859 rc = smbldap_add(ldap_state->smbldap_state,
1860 dn, mods);
1861 break;
1862 case LDAP_MOD_REPLACE:
1863 rc = smbldap_modify(ldap_state->smbldap_state,
1864 dn ,mods);
1865 break;
1866 default:
1867 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1868 ldap_op));
1869 return NT_STATUS_INVALID_PARAMETER;
1872 if (rc!=LDAP_SUCCESS) {
1873 return NT_STATUS_UNSUCCESSFUL;
1877 return NT_STATUS_OK;
1880 /**********************************************************************
1881 Delete entry from LDAP for username.
1882 *********************************************************************/
1884 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1885 struct samu * sam_acct)
1887 struct ldapsam_privates *priv =
1888 (struct ldapsam_privates *)my_methods->private_data;
1889 const char *sname;
1890 int rc;
1891 LDAPMessage *msg, *entry;
1892 NTSTATUS result = NT_STATUS_NO_MEMORY;
1893 const char **attr_list;
1894 TALLOC_CTX *mem_ctx;
1896 if (!sam_acct) {
1897 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1898 return NT_STATUS_INVALID_PARAMETER;
1901 sname = pdb_get_username(sam_acct);
1903 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1904 "LDAP.\n", sname));
1906 mem_ctx = talloc_new(NULL);
1907 if (mem_ctx == NULL) {
1908 DEBUG(0, ("talloc_new failed\n"));
1909 goto done;
1912 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1913 if (attr_list == NULL) {
1914 goto done;
1917 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1919 if ((rc != LDAP_SUCCESS) ||
1920 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1921 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1922 DEBUG(5, ("Could not find user %s\n", sname));
1923 result = NT_STATUS_NO_SUCH_USER;
1924 goto done;
1927 rc = ldapsam_delete_entry(
1928 priv, mem_ctx, entry,
1929 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1930 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1931 attr_list);
1933 result = (rc == LDAP_SUCCESS) ?
1934 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1936 done:
1937 TALLOC_FREE(mem_ctx);
1938 return result;
1941 /**********************************************************************
1942 Helper function to determine for update_sam_account whether
1943 we need LDAP modification.
1944 *********************************************************************/
1946 static bool element_is_changed(const struct samu *sampass,
1947 enum pdb_elements element)
1949 return IS_SAM_CHANGED(sampass, element);
1952 /**********************************************************************
1953 Update struct samu.
1954 *********************************************************************/
1956 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1958 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1959 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1960 int rc = 0;
1961 char *dn;
1962 LDAPMessage *result = NULL;
1963 LDAPMessage *entry = NULL;
1964 LDAPMod **mods = NULL;
1965 const char **attr_list;
1967 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1968 if (!result) {
1969 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1970 if (pdb_get_username(newpwd) == NULL) {
1971 return NT_STATUS_INVALID_PARAMETER;
1973 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1974 TALLOC_FREE( attr_list );
1975 if (rc != LDAP_SUCCESS) {
1976 return NT_STATUS_UNSUCCESSFUL;
1978 pdb_set_backend_private_data(newpwd, result, NULL,
1979 my_methods, PDB_CHANGED);
1980 talloc_autofree_ldapmsg(newpwd, result);
1983 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1984 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1985 return NT_STATUS_UNSUCCESSFUL;
1988 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1989 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1990 if (!dn) {
1991 return NT_STATUS_UNSUCCESSFUL;
1994 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1996 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1997 element_is_changed)) {
1998 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1999 TALLOC_FREE(dn);
2000 if (mods != NULL)
2001 ldap_mods_free(mods,True);
2002 return NT_STATUS_UNSUCCESSFUL;
2005 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
2006 && (mods == NULL)) {
2007 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2008 pdb_get_username(newpwd)));
2009 TALLOC_FREE(dn);
2010 return NT_STATUS_OK;
2013 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
2015 if (mods != NULL) {
2016 ldap_mods_free(mods,True);
2019 TALLOC_FREE(dn);
2022 * We need to set the backend private data to NULL here. For example
2023 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2024 * same one, and with the explicit delete / add logic for attribute
2025 * values the second time we would use the wrong "old" value which
2026 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2027 * the update.
2028 * The existing LDAPMessage is still being auto-freed by the
2029 * destructor.
2031 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2032 PDB_CHANGED);
2034 if (!NT_STATUS_IS_OK(ret)) {
2035 return ret;
2038 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2039 pdb_get_username(newpwd)));
2040 return NT_STATUS_OK;
2043 /***************************************************************************
2044 Renames a struct samu
2045 - The "rename user script" has full responsibility for changing everything
2046 ***************************************************************************/
2048 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
2049 TALLOC_CTX *tmp_ctx,
2050 uint32_t group_rid,
2051 uint32_t member_rid);
2053 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2054 TALLOC_CTX *mem_ctx,
2055 struct samu *user,
2056 struct dom_sid **pp_sids,
2057 gid_t **pp_gids,
2058 uint32_t *p_num_groups);
2060 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2061 struct samu *old_acct,
2062 const char *newname)
2064 const char *oldname;
2065 int rc;
2066 char *rename_script = NULL;
2067 fstring oldname_lower, newname_lower;
2069 if (!old_acct) {
2070 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2071 return NT_STATUS_INVALID_PARAMETER;
2073 if (!newname) {
2074 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2075 return NT_STATUS_INVALID_PARAMETER;
2078 oldname = pdb_get_username(old_acct);
2080 /* rename the posix user */
2081 rename_script = SMB_STRDUP(lp_renameuser_script());
2082 if (rename_script == NULL) {
2083 return NT_STATUS_NO_MEMORY;
2086 if (!(*rename_script)) {
2087 SAFE_FREE(rename_script);
2088 return NT_STATUS_ACCESS_DENIED;
2091 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2092 oldname, newname));
2094 /* We have to allow the account name to end with a '$'.
2095 Also, follow the semantics in _samr_create_user() and lower case the
2096 posix name but preserve the case in passdb */
2098 fstrcpy( oldname_lower, oldname );
2099 strlower_m( oldname_lower );
2100 fstrcpy( newname_lower, newname );
2101 strlower_m( newname_lower );
2102 rename_script = realloc_string_sub2(rename_script,
2103 "%unew",
2104 newname_lower,
2105 true,
2106 true);
2107 if (!rename_script) {
2108 return NT_STATUS_NO_MEMORY;
2110 rename_script = realloc_string_sub2(rename_script,
2111 "%uold",
2112 oldname_lower,
2113 true,
2114 true);
2115 rc = smbrun(rename_script, NULL);
2117 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2118 rename_script, rc));
2120 SAFE_FREE(rename_script);
2122 if (rc == 0) {
2123 smb_nscd_flush_user_cache();
2126 if (rc)
2127 return NT_STATUS_UNSUCCESSFUL;
2129 return NT_STATUS_OK;
2132 /**********************************************************************
2133 Helper function to determine for update_sam_account whether
2134 we need LDAP modification.
2135 *********************************************************************/
2137 static bool element_is_set_or_changed(const struct samu *sampass,
2138 enum pdb_elements element)
2140 return (IS_SAM_SET(sampass, element) ||
2141 IS_SAM_CHANGED(sampass, element));
2144 /**********************************************************************
2145 Add struct samu to LDAP.
2146 *********************************************************************/
2148 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2150 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2151 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2152 int rc;
2153 LDAPMessage *result = NULL;
2154 LDAPMessage *entry = NULL;
2155 LDAPMod **mods = NULL;
2156 int ldap_op = LDAP_MOD_REPLACE;
2157 uint32_t num_result;
2158 const char **attr_list;
2159 char *escape_user = NULL;
2160 const char *username = pdb_get_username(newpwd);
2161 const struct dom_sid *sid = pdb_get_user_sid(newpwd);
2162 char *filter = NULL;
2163 char *dn = NULL;
2164 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2165 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2167 if (!ctx) {
2168 return NT_STATUS_NO_MEMORY;
2171 if (!username || !*username) {
2172 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2173 status = NT_STATUS_INVALID_PARAMETER;
2174 goto fn_exit;
2177 /* free this list after the second search or in case we exit on failure */
2178 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2180 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2182 if (rc != LDAP_SUCCESS) {
2183 goto fn_exit;
2186 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2187 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2188 username));
2189 goto fn_exit;
2191 ldap_msgfree(result);
2192 result = NULL;
2194 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2195 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2196 sid, &result);
2197 if (rc == LDAP_SUCCESS) {
2198 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2199 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2200 "already in the base, with samba "
2201 "attributes\n", sid_string_dbg(sid)));
2202 goto fn_exit;
2204 ldap_msgfree(result);
2205 result = NULL;
2209 /* does the entry already exist but without a samba attributes?
2210 we need to return the samba attributes here */
2212 escape_user = escape_ldap_string(talloc_tos(), username);
2213 filter = talloc_strdup(attr_list, "(uid=%u)");
2214 if (!filter) {
2215 status = NT_STATUS_NO_MEMORY;
2216 goto fn_exit;
2218 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2219 TALLOC_FREE(escape_user);
2220 if (!filter) {
2221 status = NT_STATUS_NO_MEMORY;
2222 goto fn_exit;
2225 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2226 filter, attr_list, &result);
2227 if ( rc != LDAP_SUCCESS ) {
2228 goto fn_exit;
2231 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2233 if (num_result > 1) {
2234 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2235 goto fn_exit;
2238 /* Check if we need to update an existing entry */
2239 if (num_result == 1) {
2240 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2241 ldap_op = LDAP_MOD_REPLACE;
2242 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2243 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2244 if (!dn) {
2245 status = NT_STATUS_NO_MEMORY;
2246 goto fn_exit;
2249 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2251 /* There might be a SID for this account already - say an idmap entry */
2253 filter = talloc_asprintf(ctx,
2254 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2255 get_userattr_key2string(ldap_state->schema_ver,
2256 LDAP_ATTR_USER_SID),
2257 sid_string_talloc(ctx, sid),
2258 LDAP_OBJ_IDMAP_ENTRY,
2259 LDAP_OBJ_SID_ENTRY);
2260 if (!filter) {
2261 status = NT_STATUS_NO_MEMORY;
2262 goto fn_exit;
2265 /* free old result before doing a new search */
2266 if (result != NULL) {
2267 ldap_msgfree(result);
2268 result = NULL;
2270 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2271 filter, attr_list, &result);
2273 if ( rc != LDAP_SUCCESS ) {
2274 goto fn_exit;
2277 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2279 if (num_result > 1) {
2280 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2281 goto fn_exit;
2284 /* Check if we need to update an existing entry */
2285 if (num_result == 1) {
2287 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2288 ldap_op = LDAP_MOD_REPLACE;
2289 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2290 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2291 if (!dn) {
2292 status = NT_STATUS_NO_MEMORY;
2293 goto fn_exit;
2298 if (num_result == 0) {
2299 char *escape_username;
2300 /* Check if we need to add an entry */
2301 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2302 ldap_op = LDAP_MOD_ADD;
2304 escape_username = escape_rdn_val_string_alloc(username);
2305 if (!escape_username) {
2306 status = NT_STATUS_NO_MEMORY;
2307 goto fn_exit;
2310 if (username[strlen(username)-1] == '$') {
2311 dn = talloc_asprintf(ctx,
2312 "uid=%s,%s",
2313 escape_username,
2314 lp_ldap_machine_suffix());
2315 } else {
2316 dn = talloc_asprintf(ctx,
2317 "uid=%s,%s",
2318 escape_username,
2319 lp_ldap_user_suffix());
2322 SAFE_FREE(escape_username);
2323 if (!dn) {
2324 status = NT_STATUS_NO_MEMORY;
2325 goto fn_exit;
2329 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2330 element_is_set_or_changed)) {
2331 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2332 if (mods != NULL) {
2333 ldap_mods_free(mods, true);
2335 goto fn_exit;
2338 if (mods == NULL) {
2339 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2340 goto fn_exit;
2342 switch ( ldap_state->schema_ver ) {
2343 case SCHEMAVER_SAMBAACCOUNT:
2344 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2345 break;
2346 case SCHEMAVER_SAMBASAMACCOUNT:
2347 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2348 break;
2349 default:
2350 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2351 break;
2354 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2355 if (!NT_STATUS_IS_OK(ret)) {
2356 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2357 pdb_get_username(newpwd),dn));
2358 ldap_mods_free(mods, true);
2359 goto fn_exit;
2362 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2363 ldap_mods_free(mods, true);
2365 status = NT_STATUS_OK;
2367 fn_exit:
2369 TALLOC_FREE(ctx);
2370 if (result) {
2371 ldap_msgfree(result);
2373 return status;
2376 /**********************************************************************
2377 *********************************************************************/
2379 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2380 const char *filter,
2381 LDAPMessage ** result)
2383 int scope = LDAP_SCOPE_SUBTREE;
2384 int rc;
2385 const char **attr_list;
2387 attr_list = get_attr_list(NULL, groupmap_attr_list);
2388 rc = smbldap_search(ldap_state->smbldap_state,
2389 lp_ldap_suffix (), scope,
2390 filter, attr_list, 0, result);
2391 TALLOC_FREE(attr_list);
2393 return rc;
2396 /**********************************************************************
2397 *********************************************************************/
2399 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2400 GROUP_MAP *map, LDAPMessage *entry)
2402 char *temp = NULL;
2403 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2405 if (ldap_state == NULL || map == NULL || entry == NULL ||
2406 ldap_state->smbldap_state->ldap_struct == NULL) {
2407 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2408 TALLOC_FREE(ctx);
2409 return false;
2412 temp = smbldap_talloc_single_attribute(
2413 ldap_state->smbldap_state->ldap_struct,
2414 entry,
2415 get_attr_key2string(groupmap_attr_list,
2416 LDAP_ATTR_GIDNUMBER),
2417 ctx);
2418 if (!temp) {
2419 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2420 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2421 TALLOC_FREE(ctx);
2422 return false;
2424 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2426 map->gid = (gid_t)atol(temp);
2428 TALLOC_FREE(temp);
2429 temp = smbldap_talloc_single_attribute(
2430 ldap_state->smbldap_state->ldap_struct,
2431 entry,
2432 get_attr_key2string(groupmap_attr_list,
2433 LDAP_ATTR_GROUP_SID),
2434 ctx);
2435 if (!temp) {
2436 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2437 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2438 TALLOC_FREE(ctx);
2439 return false;
2442 if (!string_to_sid(&map->sid, temp)) {
2443 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2444 TALLOC_FREE(ctx);
2445 return false;
2448 TALLOC_FREE(temp);
2449 temp = smbldap_talloc_single_attribute(
2450 ldap_state->smbldap_state->ldap_struct,
2451 entry,
2452 get_attr_key2string(groupmap_attr_list,
2453 LDAP_ATTR_GROUP_TYPE),
2454 ctx);
2455 if (!temp) {
2456 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2457 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2458 TALLOC_FREE(ctx);
2459 return false;
2461 map->sid_name_use = (enum lsa_SidType)atol(temp);
2463 if ((map->sid_name_use < SID_NAME_USER) ||
2464 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2465 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2466 TALLOC_FREE(ctx);
2467 return false;
2470 TALLOC_FREE(temp);
2471 temp = smbldap_talloc_single_attribute(
2472 ldap_state->smbldap_state->ldap_struct,
2473 entry,
2474 get_attr_key2string(groupmap_attr_list,
2475 LDAP_ATTR_DISPLAY_NAME),
2476 ctx);
2477 if (!temp) {
2478 temp = smbldap_talloc_single_attribute(
2479 ldap_state->smbldap_state->ldap_struct,
2480 entry,
2481 get_attr_key2string(groupmap_attr_list,
2482 LDAP_ATTR_CN),
2483 ctx);
2484 if (!temp) {
2485 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2486 for gidNumber(%lu)\n",(unsigned long)map->gid));
2487 TALLOC_FREE(ctx);
2488 return false;
2491 fstrcpy(map->nt_name, temp);
2493 TALLOC_FREE(temp);
2494 temp = smbldap_talloc_single_attribute(
2495 ldap_state->smbldap_state->ldap_struct,
2496 entry,
2497 get_attr_key2string(groupmap_attr_list,
2498 LDAP_ATTR_DESC),
2499 ctx);
2500 if (!temp) {
2501 temp = talloc_strdup(ctx, "");
2502 if (!temp) {
2503 TALLOC_FREE(ctx);
2504 return false;
2507 fstrcpy(map->comment, temp);
2509 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2510 store_gid_sid_cache(&map->sid, map->gid);
2511 idmap_cache_set_sid2gid(&map->sid, map->gid);
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 fstring 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 sid_to_fstring(tmp, &sid)) < 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 DEBUG(10, ("SID %s is not in expected domain %s\n",
2663 str, sid_string_dbg(domain_sid)));
2664 return False;
2667 if (!sid_peek_rid(&sid, rid)) {
2668 DEBUG(10, ("Could not peek into RID\n"));
2669 return False;
2672 return True;
2675 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2676 TALLOC_CTX *mem_ctx,
2677 const struct dom_sid *group,
2678 uint32_t **pp_member_rids,
2679 size_t *p_num_members)
2681 struct ldapsam_privates *ldap_state =
2682 (struct ldapsam_privates *)methods->private_data;
2683 struct smbldap_state *conn = ldap_state->smbldap_state;
2684 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2685 const char *sid_attrs[] = { "sambaSID", NULL };
2686 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2687 LDAPMessage *result = NULL;
2688 LDAPMessage *entry;
2689 char *filter;
2690 char **values = NULL;
2691 char **memberuid;
2692 char *gidstr;
2693 int rc, count;
2695 *pp_member_rids = NULL;
2696 *p_num_members = 0;
2698 filter = talloc_asprintf(mem_ctx,
2699 "(&(objectClass=%s)"
2700 "(objectClass=%s)"
2701 "(sambaSID=%s))",
2702 LDAP_OBJ_POSIXGROUP,
2703 LDAP_OBJ_GROUPMAP,
2704 sid_string_talloc(mem_ctx, group));
2705 if (filter == NULL) {
2706 ret = NT_STATUS_NO_MEMORY;
2707 goto done;
2710 rc = smbldap_search(conn, lp_ldap_suffix(),
2711 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2712 &result);
2714 if (rc != LDAP_SUCCESS)
2715 goto done;
2717 talloc_autofree_ldapmsg(mem_ctx, result);
2719 count = ldap_count_entries(conn->ldap_struct, result);
2721 if (count > 1) {
2722 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2723 sid_string_dbg(group)));
2724 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2725 goto done;
2728 if (count == 0) {
2729 ret = NT_STATUS_NO_SUCH_GROUP;
2730 goto done;
2733 entry = ldap_first_entry(conn->ldap_struct, result);
2734 if (entry == NULL)
2735 goto done;
2737 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2738 if (!gidstr) {
2739 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2740 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2741 goto done;
2744 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2746 if ((values != NULL) && (values[0] != NULL)) {
2748 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2749 if (filter == NULL) {
2750 ret = NT_STATUS_NO_MEMORY;
2751 goto done;
2754 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2755 char *escape_memberuid;
2757 escape_memberuid = escape_ldap_string(talloc_tos(),
2758 *memberuid);
2759 if (escape_memberuid == NULL) {
2760 ret = NT_STATUS_NO_MEMORY;
2761 goto done;
2764 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2765 TALLOC_FREE(escape_memberuid);
2766 if (filter == NULL) {
2767 ret = NT_STATUS_NO_MEMORY;
2768 goto done;
2772 filter = talloc_asprintf_append_buffer(filter, "))");
2773 if (filter == NULL) {
2774 ret = NT_STATUS_NO_MEMORY;
2775 goto done;
2778 rc = smbldap_search(conn, lp_ldap_suffix(),
2779 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2780 &result);
2782 if (rc != LDAP_SUCCESS)
2783 goto done;
2785 count = ldap_count_entries(conn->ldap_struct, result);
2786 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2788 talloc_autofree_ldapmsg(mem_ctx, result);
2790 for (entry = ldap_first_entry(conn->ldap_struct, result);
2791 entry != NULL;
2792 entry = ldap_next_entry(conn->ldap_struct, entry))
2794 char *sidstr;
2795 struct dom_sid sid;
2796 uint32_t rid;
2798 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2799 entry, "sambaSID",
2800 mem_ctx);
2801 if (!sidstr) {
2802 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2803 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2804 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2805 goto done;
2808 if (!string_to_sid(&sid, sidstr))
2809 goto done;
2811 if (!sid_check_is_in_our_domain(&sid)) {
2812 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2813 "in our domain\n"));
2814 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2815 goto done;
2818 sid_peek_rid(&sid, &rid);
2820 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2821 p_num_members)) {
2822 ret = NT_STATUS_NO_MEMORY;
2823 goto done;
2828 filter = talloc_asprintf(mem_ctx,
2829 "(&(objectClass=%s)"
2830 "(gidNumber=%s))",
2831 LDAP_OBJ_SAMBASAMACCOUNT,
2832 gidstr);
2834 rc = smbldap_search(conn, lp_ldap_suffix(),
2835 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2836 &result);
2838 if (rc != LDAP_SUCCESS)
2839 goto done;
2841 talloc_autofree_ldapmsg(mem_ctx, result);
2843 for (entry = ldap_first_entry(conn->ldap_struct, result);
2844 entry != NULL;
2845 entry = ldap_next_entry(conn->ldap_struct, entry))
2847 uint32_t rid;
2849 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2850 entry,
2851 get_global_sam_sid(),
2852 &rid)) {
2853 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2854 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2855 goto done;
2858 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2859 p_num_members)) {
2860 ret = NT_STATUS_NO_MEMORY;
2861 goto done;
2865 ret = NT_STATUS_OK;
2867 done:
2869 if (values)
2870 ldap_value_free(values);
2872 return ret;
2875 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2876 TALLOC_CTX *mem_ctx,
2877 struct samu *user,
2878 struct dom_sid **pp_sids,
2879 gid_t **pp_gids,
2880 uint32_t *p_num_groups)
2882 struct ldapsam_privates *ldap_state =
2883 (struct ldapsam_privates *)methods->private_data;
2884 struct smbldap_state *conn = ldap_state->smbldap_state;
2885 char *filter;
2886 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2887 char *escape_name;
2888 int rc, count;
2889 LDAPMessage *result = NULL;
2890 LDAPMessage *entry;
2891 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2892 uint32_t num_sids;
2893 uint32_t num_gids;
2894 char *gidstr;
2895 gid_t primary_gid = -1;
2897 *pp_sids = NULL;
2898 num_sids = 0;
2900 if (pdb_get_username(user) == NULL) {
2901 return NT_STATUS_INVALID_PARAMETER;
2904 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2905 if (escape_name == NULL)
2906 return NT_STATUS_NO_MEMORY;
2908 if (user->unix_pw) {
2909 primary_gid = user->unix_pw->pw_gid;
2910 } else {
2911 /* retrieve the users primary gid */
2912 filter = talloc_asprintf(mem_ctx,
2913 "(&(objectClass=%s)(uid=%s))",
2914 LDAP_OBJ_SAMBASAMACCOUNT,
2915 escape_name);
2916 if (filter == NULL) {
2917 ret = NT_STATUS_NO_MEMORY;
2918 goto done;
2921 rc = smbldap_search(conn, lp_ldap_suffix(),
2922 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2924 if (rc != LDAP_SUCCESS)
2925 goto done;
2927 talloc_autofree_ldapmsg(mem_ctx, result);
2929 count = ldap_count_entries(priv2ld(ldap_state), result);
2931 switch (count) {
2932 case 0:
2933 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2934 ret = NT_STATUS_NO_SUCH_USER;
2935 goto done;
2936 case 1:
2937 entry = ldap_first_entry(priv2ld(ldap_state), result);
2939 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2940 if (!gidstr) {
2941 DEBUG (1, ("Unable to find the member's gid!\n"));
2942 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2943 goto done;
2945 primary_gid = strtoul(gidstr, NULL, 10);
2946 break;
2947 default:
2948 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2949 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2950 goto done;
2954 filter = talloc_asprintf(mem_ctx,
2955 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2956 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2957 if (filter == NULL) {
2958 ret = NT_STATUS_NO_MEMORY;
2959 goto done;
2962 rc = smbldap_search(conn, lp_ldap_suffix(),
2963 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2965 if (rc != LDAP_SUCCESS)
2966 goto done;
2968 talloc_autofree_ldapmsg(mem_ctx, result);
2970 num_gids = 0;
2971 *pp_gids = NULL;
2973 num_sids = 0;
2974 *pp_sids = NULL;
2976 /* We need to add the primary group as the first gid/sid */
2978 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2979 ret = NT_STATUS_NO_MEMORY;
2980 goto done;
2983 /* This sid will be replaced later */
2985 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2986 &num_sids);
2987 if (!NT_STATUS_IS_OK(ret)) {
2988 goto done;
2991 for (entry = ldap_first_entry(conn->ldap_struct, result);
2992 entry != NULL;
2993 entry = ldap_next_entry(conn->ldap_struct, entry))
2995 fstring str;
2996 struct dom_sid sid;
2997 gid_t gid;
2998 char *end;
3000 if (!smbldap_get_single_attribute(conn->ldap_struct,
3001 entry, "sambaSID",
3002 str, sizeof(str)-1))
3003 continue;
3005 if (!string_to_sid(&sid, str))
3006 goto done;
3008 if (!smbldap_get_single_attribute(conn->ldap_struct,
3009 entry, "gidNumber",
3010 str, sizeof(str)-1))
3011 continue;
3013 gid = strtoul(str, &end, 10);
3015 if (PTR_DIFF(end, str) != strlen(str))
3016 goto done;
3018 if (gid == primary_gid) {
3019 sid_copy(&(*pp_sids)[0], &sid);
3020 } else {
3021 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3022 &num_gids)) {
3023 ret = NT_STATUS_NO_MEMORY;
3024 goto done;
3026 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3027 &num_sids);
3028 if (!NT_STATUS_IS_OK(ret)) {
3029 goto done;
3034 if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3035 DEBUG(3, ("primary group of [%s] not found\n",
3036 pdb_get_username(user)));
3037 goto done;
3040 *p_num_groups = num_sids;
3042 ret = NT_STATUS_OK;
3044 done:
3046 TALLOC_FREE(escape_name);
3047 return ret;
3050 /**********************************************************************
3051 * Augment a posixGroup object with a sambaGroupMapping domgroup
3052 *********************************************************************/
3054 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3055 struct ldapsam_privates *ldap_state,
3056 GROUP_MAP *map)
3058 const char *filter, *dn;
3059 LDAPMessage *msg, *entry;
3060 LDAPMod **mods;
3061 int rc;
3063 filter = talloc_asprintf(mem_ctx,
3064 "(&(objectClass=%s)(gidNumber=%u))",
3065 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3066 if (filter == NULL) {
3067 return NT_STATUS_NO_MEMORY;
3070 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3071 get_attr_list(mem_ctx, groupmap_attr_list),
3072 &msg);
3073 talloc_autofree_ldapmsg(mem_ctx, msg);
3075 if ((rc != LDAP_SUCCESS) ||
3076 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3077 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3078 return NT_STATUS_NO_SUCH_GROUP;
3081 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3082 if (dn == NULL) {
3083 return NT_STATUS_NO_MEMORY;
3086 mods = NULL;
3087 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3088 LDAP_OBJ_GROUPMAP);
3089 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3090 sid_string_talloc(mem_ctx, &map->sid));
3091 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3092 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3093 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3094 map->nt_name);
3095 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3096 map->comment);
3097 talloc_autofree_ldapmod(mem_ctx, mods);
3099 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3100 if (rc != LDAP_SUCCESS) {
3101 return NT_STATUS_ACCESS_DENIED;
3104 return NT_STATUS_OK;
3107 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3108 GROUP_MAP *map)
3110 struct ldapsam_privates *ldap_state =
3111 (struct ldapsam_privates *)methods->private_data;
3112 LDAPMessage *msg = NULL;
3113 LDAPMod **mods = NULL;
3114 const char *attrs[] = { NULL };
3115 char *filter;
3117 char *dn;
3118 TALLOC_CTX *mem_ctx;
3119 NTSTATUS result;
3121 struct dom_sid sid;
3123 int rc;
3125 mem_ctx = talloc_new(NULL);
3126 if (mem_ctx == NULL) {
3127 DEBUG(0, ("talloc_new failed\n"));
3128 return NT_STATUS_NO_MEMORY;
3131 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3132 sid_string_talloc(mem_ctx, &map->sid));
3133 if (filter == NULL) {
3134 result = NT_STATUS_NO_MEMORY;
3135 goto done;
3138 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3139 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3140 talloc_autofree_ldapmsg(mem_ctx, msg);
3142 if ((rc == LDAP_SUCCESS) &&
3143 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3145 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3146 "group mapping entry\n", sid_string_dbg(&map->sid)));
3147 result = NT_STATUS_GROUP_EXISTS;
3148 goto done;
3151 switch (map->sid_name_use) {
3153 case SID_NAME_DOM_GRP:
3154 /* To map a domain group we need to have a posix group
3155 to attach to. */
3156 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3157 goto done;
3158 break;
3160 case SID_NAME_ALIAS:
3161 if (!sid_check_is_in_our_domain(&map->sid)
3162 && !sid_check_is_in_builtin(&map->sid) )
3164 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3165 sid_string_dbg(&map->sid)));
3166 result = NT_STATUS_INVALID_PARAMETER;
3167 goto done;
3169 break;
3171 default:
3172 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3173 sid_type_lookup(map->sid_name_use)));
3174 result = NT_STATUS_INVALID_PARAMETER;
3175 goto done;
3178 /* Domain groups have been mapped in a separate routine, we have to
3179 * create an alias now */
3181 if (map->gid == -1) {
3182 DEBUG(10, ("Refusing to map gid==-1\n"));
3183 result = NT_STATUS_INVALID_PARAMETER;
3184 goto done;
3187 if (pdb_gid_to_sid(map->gid, &sid)) {
3188 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3189 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3190 result = NT_STATUS_GROUP_EXISTS;
3191 goto done;
3194 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3195 * the best we can get out of LDAP. */
3197 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3198 sid_string_talloc(mem_ctx, &map->sid),
3199 lp_ldap_group_suffix());
3200 if (dn == NULL) {
3201 result = NT_STATUS_NO_MEMORY;
3202 goto done;
3205 mods = NULL;
3207 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3208 LDAP_OBJ_SID_ENTRY);
3209 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3210 LDAP_OBJ_GROUPMAP);
3211 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3212 sid_string_talloc(mem_ctx, &map->sid));
3213 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3214 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3215 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3216 map->nt_name);
3217 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3218 map->comment);
3219 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3220 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3221 talloc_autofree_ldapmod(mem_ctx, mods);
3223 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3225 result = (rc == LDAP_SUCCESS) ?
3226 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3228 done:
3229 TALLOC_FREE(mem_ctx);
3230 return result;
3233 /**********************************************************************
3234 * Update a group mapping entry. We're quite strict about what can be changed:
3235 * Only the description and displayname may be changed. It simply does not
3236 * make any sense to change the SID, gid or the type in a mapping.
3237 *********************************************************************/
3239 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3240 GROUP_MAP *map)
3242 struct ldapsam_privates *ldap_state =
3243 (struct ldapsam_privates *)methods->private_data;
3244 int rc;
3245 const char *filter, *dn;
3246 LDAPMessage *msg = NULL;
3247 LDAPMessage *entry = NULL;
3248 LDAPMod **mods = NULL;
3249 TALLOC_CTX *mem_ctx;
3250 NTSTATUS result;
3252 mem_ctx = talloc_new(NULL);
3253 if (mem_ctx == NULL) {
3254 DEBUG(0, ("talloc_new failed\n"));
3255 return NT_STATUS_NO_MEMORY;
3258 /* Make 100% sure that sid, gid and type are not changed by looking up
3259 * exactly the values we're given in LDAP. */
3261 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3262 "(sambaSid=%s)(gidNumber=%u)"
3263 "(sambaGroupType=%d))",
3264 LDAP_OBJ_GROUPMAP,
3265 sid_string_talloc(mem_ctx, &map->sid),
3266 (unsigned int)map->gid, map->sid_name_use);
3267 if (filter == NULL) {
3268 result = NT_STATUS_NO_MEMORY;
3269 goto done;
3272 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3273 get_attr_list(mem_ctx, groupmap_attr_list),
3274 &msg);
3275 talloc_autofree_ldapmsg(mem_ctx, msg);
3277 if ((rc != LDAP_SUCCESS) ||
3278 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3279 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3280 result = NT_STATUS_NO_SUCH_GROUP;
3281 goto done;
3284 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3286 if (dn == NULL) {
3287 result = NT_STATUS_NO_MEMORY;
3288 goto done;
3291 mods = NULL;
3292 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3293 map->nt_name);
3294 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3295 map->comment);
3296 talloc_autofree_ldapmod(mem_ctx, mods);
3298 if (mods == NULL) {
3299 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3300 "nothing to do\n"));
3301 result = NT_STATUS_OK;
3302 goto done;
3305 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3307 if (rc != LDAP_SUCCESS) {
3308 result = NT_STATUS_ACCESS_DENIED;
3309 goto done;
3312 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3313 "group %lu in LDAP\n", (unsigned long)map->gid));
3315 result = NT_STATUS_OK;
3317 done:
3318 TALLOC_FREE(mem_ctx);
3319 return result;
3322 /**********************************************************************
3323 *********************************************************************/
3325 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3326 struct dom_sid sid)
3328 struct ldapsam_privates *priv =
3329 (struct ldapsam_privates *)methods->private_data;
3330 LDAPMessage *msg, *entry;
3331 int rc;
3332 NTSTATUS result;
3333 TALLOC_CTX *mem_ctx;
3334 char *filter;
3336 mem_ctx = talloc_new(NULL);
3337 if (mem_ctx == NULL) {
3338 DEBUG(0, ("talloc_new failed\n"));
3339 return NT_STATUS_NO_MEMORY;
3342 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3343 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3344 sid_string_talloc(mem_ctx, &sid));
3345 if (filter == NULL) {
3346 result = NT_STATUS_NO_MEMORY;
3347 goto done;
3349 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3350 get_attr_list(mem_ctx, groupmap_attr_list),
3351 &msg);
3352 talloc_autofree_ldapmsg(mem_ctx, msg);
3354 if ((rc != LDAP_SUCCESS) ||
3355 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3356 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3357 result = NT_STATUS_NO_SUCH_GROUP;
3358 goto done;
3361 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3362 get_attr_list(mem_ctx,
3363 groupmap_attr_list_to_delete));
3365 if ((rc == LDAP_NAMING_VIOLATION) ||
3366 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3367 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3368 const char *attrs[] = { "sambaGroupType", "description",
3369 "displayName", "sambaSIDList",
3370 NULL };
3372 /* Second try. Don't delete the sambaSID attribute, this is
3373 for "old" entries that are tacked on a winbind
3374 sambaIdmapEntry. */
3376 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3377 LDAP_OBJ_GROUPMAP, attrs);
3380 if ((rc == LDAP_NAMING_VIOLATION) ||
3381 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3382 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3383 const char *attrs[] = { "sambaGroupType", "description",
3384 "displayName", "sambaSIDList",
3385 "gidNumber", NULL };
3387 /* Third try. This is a post-3.0.21 alias (containing only
3388 * sambaSidEntry and sambaGroupMapping classes), we also have
3389 * to delete the gidNumber attribute, only the sambaSidEntry
3390 * remains */
3392 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3393 LDAP_OBJ_GROUPMAP, attrs);
3396 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3398 done:
3399 TALLOC_FREE(mem_ctx);
3400 return result;
3403 /**********************************************************************
3404 *********************************************************************/
3406 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3407 bool update)
3409 struct ldapsam_privates *ldap_state =
3410 (struct ldapsam_privates *)my_methods->private_data;
3411 char *filter = NULL;
3412 int rc;
3413 const char **attr_list;
3415 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3416 if (!filter) {
3417 return NT_STATUS_NO_MEMORY;
3419 attr_list = get_attr_list( NULL, groupmap_attr_list );
3420 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3421 LDAP_SCOPE_SUBTREE, filter,
3422 attr_list, 0, &ldap_state->result);
3423 TALLOC_FREE(attr_list);
3425 if (rc != LDAP_SUCCESS) {
3426 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3427 ldap_err2string(rc)));
3428 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3429 lp_ldap_suffix(), filter));
3430 ldap_msgfree(ldap_state->result);
3431 ldap_state->result = NULL;
3432 TALLOC_FREE(filter);
3433 return NT_STATUS_UNSUCCESSFUL;
3436 TALLOC_FREE(filter);
3438 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3439 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3440 ldap_state->result)));
3442 ldap_state->entry =
3443 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3444 ldap_state->result);
3445 ldap_state->index = 0;
3447 return NT_STATUS_OK;
3450 /**********************************************************************
3451 *********************************************************************/
3453 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3455 ldapsam_endsampwent(my_methods);
3458 /**********************************************************************
3459 *********************************************************************/
3461 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3462 GROUP_MAP *map)
3464 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3465 struct ldapsam_privates *ldap_state =
3466 (struct ldapsam_privates *)my_methods->private_data;
3467 bool bret = False;
3469 while (!bret) {
3470 if (!ldap_state->entry)
3471 return ret;
3473 ldap_state->index++;
3474 bret = init_group_from_ldap(ldap_state, map,
3475 ldap_state->entry);
3477 ldap_state->entry =
3478 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3479 ldap_state->entry);
3482 return NT_STATUS_OK;
3485 /**********************************************************************
3486 *********************************************************************/
3488 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3489 const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
3490 GROUP_MAP **pp_rmap,
3491 size_t *p_num_entries,
3492 bool unix_only)
3494 GROUP_MAP map = { 0, };
3495 size_t entries = 0;
3497 *p_num_entries = 0;
3498 *pp_rmap = NULL;
3500 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3501 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3502 "passdb\n"));
3503 return NT_STATUS_ACCESS_DENIED;
3506 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3507 if (sid_name_use != SID_NAME_UNKNOWN &&
3508 sid_name_use != map.sid_name_use) {
3509 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3510 "not of the requested type\n", map.nt_name));
3511 continue;
3513 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3514 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3515 "non mapped\n", map.nt_name));
3516 continue;
3519 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3520 if (!(*pp_rmap)) {
3521 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3522 "enlarge group map!\n"));
3523 return NT_STATUS_UNSUCCESSFUL;
3526 (*pp_rmap)[entries] = map;
3528 entries += 1;
3531 ldapsam_endsamgrent(methods);
3533 *p_num_entries = entries;
3535 return NT_STATUS_OK;
3538 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3539 const struct dom_sid *alias,
3540 const struct dom_sid *member,
3541 int modop)
3543 struct ldapsam_privates *ldap_state =
3544 (struct ldapsam_privates *)methods->private_data;
3545 char *dn = NULL;
3546 LDAPMessage *result = NULL;
3547 LDAPMessage *entry = NULL;
3548 int count;
3549 LDAPMod **mods = NULL;
3550 int rc;
3551 enum lsa_SidType type = SID_NAME_USE_NONE;
3552 fstring tmp;
3554 char *filter = NULL;
3556 if (sid_check_is_in_builtin(alias)) {
3557 type = SID_NAME_ALIAS;
3560 if (sid_check_is_in_our_domain(alias)) {
3561 type = SID_NAME_ALIAS;
3564 if (type == SID_NAME_USE_NONE) {
3565 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3566 sid_string_dbg(alias)));
3567 return NT_STATUS_NO_SUCH_ALIAS;
3570 if (asprintf(&filter,
3571 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3572 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3573 type) < 0) {
3574 return NT_STATUS_NO_MEMORY;
3577 if (ldapsam_search_one_group(ldap_state, filter,
3578 &result) != LDAP_SUCCESS) {
3579 SAFE_FREE(filter);
3580 return NT_STATUS_NO_SUCH_ALIAS;
3583 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3584 result);
3586 if (count < 1) {
3587 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3588 ldap_msgfree(result);
3589 SAFE_FREE(filter);
3590 return NT_STATUS_NO_SUCH_ALIAS;
3593 if (count > 1) {
3594 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3595 "filter %s: count=%d\n", filter, count));
3596 ldap_msgfree(result);
3597 SAFE_FREE(filter);
3598 return NT_STATUS_NO_SUCH_ALIAS;
3601 SAFE_FREE(filter);
3603 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3604 result);
3606 if (!entry) {
3607 ldap_msgfree(result);
3608 return NT_STATUS_UNSUCCESSFUL;
3611 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3612 if (!dn) {
3613 ldap_msgfree(result);
3614 return NT_STATUS_UNSUCCESSFUL;
3617 smbldap_set_mod(&mods, modop,
3618 get_attr_key2string(groupmap_attr_list,
3619 LDAP_ATTR_SID_LIST),
3620 sid_to_fstring(tmp, member));
3622 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3624 ldap_mods_free(mods, True);
3625 ldap_msgfree(result);
3626 TALLOC_FREE(dn);
3628 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3629 return NT_STATUS_MEMBER_IN_ALIAS;
3632 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3633 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3636 if (rc != LDAP_SUCCESS) {
3637 return NT_STATUS_UNSUCCESSFUL;
3640 return NT_STATUS_OK;
3643 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3644 const struct dom_sid *alias,
3645 const struct dom_sid *member)
3647 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3650 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3651 const struct dom_sid *alias,
3652 const struct dom_sid *member)
3654 return ldapsam_modify_aliasmem(methods, alias, member,
3655 LDAP_MOD_DELETE);
3658 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3659 const struct dom_sid *alias,
3660 TALLOC_CTX *mem_ctx,
3661 struct dom_sid **pp_members,
3662 size_t *p_num_members)
3664 struct ldapsam_privates *ldap_state =
3665 (struct ldapsam_privates *)methods->private_data;
3666 LDAPMessage *result = NULL;
3667 LDAPMessage *entry = NULL;
3668 int count;
3669 char **values = NULL;
3670 int i;
3671 char *filter = NULL;
3672 uint32_t num_members = 0;
3673 enum lsa_SidType type = SID_NAME_USE_NONE;
3674 fstring tmp;
3676 *pp_members = NULL;
3677 *p_num_members = 0;
3679 if (sid_check_is_in_builtin(alias)) {
3680 type = SID_NAME_ALIAS;
3683 if (sid_check_is_in_our_domain(alias)) {
3684 type = SID_NAME_ALIAS;
3687 if (type == SID_NAME_USE_NONE) {
3688 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3689 sid_string_dbg(alias)));
3690 return NT_STATUS_NO_SUCH_ALIAS;
3693 if (asprintf(&filter,
3694 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3695 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3696 type) < 0) {
3697 return NT_STATUS_NO_MEMORY;
3700 if (ldapsam_search_one_group(ldap_state, filter,
3701 &result) != LDAP_SUCCESS) {
3702 SAFE_FREE(filter);
3703 return NT_STATUS_NO_SUCH_ALIAS;
3706 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3707 result);
3709 if (count < 1) {
3710 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3711 ldap_msgfree(result);
3712 SAFE_FREE(filter);
3713 return NT_STATUS_NO_SUCH_ALIAS;
3716 if (count > 1) {
3717 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3718 "filter %s: count=%d\n", filter, count));
3719 ldap_msgfree(result);
3720 SAFE_FREE(filter);
3721 return NT_STATUS_NO_SUCH_ALIAS;
3724 SAFE_FREE(filter);
3726 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3727 result);
3729 if (!entry) {
3730 ldap_msgfree(result);
3731 return NT_STATUS_UNSUCCESSFUL;
3734 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3735 entry,
3736 get_attr_key2string(groupmap_attr_list,
3737 LDAP_ATTR_SID_LIST));
3739 if (values == NULL) {
3740 ldap_msgfree(result);
3741 return NT_STATUS_OK;
3744 count = ldap_count_values(values);
3746 for (i=0; i<count; i++) {
3747 struct dom_sid member;
3748 NTSTATUS status;
3750 if (!string_to_sid(&member, values[i]))
3751 continue;
3753 status = add_sid_to_array(mem_ctx, &member, pp_members,
3754 &num_members);
3755 if (!NT_STATUS_IS_OK(status)) {
3756 ldap_value_free(values);
3757 ldap_msgfree(result);
3758 return status;
3762 *p_num_members = num_members;
3763 ldap_value_free(values);
3764 ldap_msgfree(result);
3766 return NT_STATUS_OK;
3769 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3770 TALLOC_CTX *mem_ctx,
3771 const struct dom_sid *domain_sid,
3772 const struct dom_sid *members,
3773 size_t num_members,
3774 uint32_t **pp_alias_rids,
3775 size_t *p_num_alias_rids)
3777 struct ldapsam_privates *ldap_state =
3778 (struct ldapsam_privates *)methods->private_data;
3779 LDAP *ldap_struct;
3781 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3783 LDAPMessage *result = NULL;
3784 LDAPMessage *entry = NULL;
3785 int i;
3786 int rc;
3787 char *filter;
3788 enum lsa_SidType type = SID_NAME_USE_NONE;
3789 bool is_builtin = false;
3790 bool sid_added = false;
3792 *pp_alias_rids = NULL;
3793 *p_num_alias_rids = 0;
3795 if (sid_check_is_builtin(domain_sid)) {
3796 is_builtin = true;
3797 type = SID_NAME_ALIAS;
3800 if (sid_check_is_domain(domain_sid)) {
3801 type = SID_NAME_ALIAS;
3804 if (type == SID_NAME_USE_NONE) {
3805 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3806 sid_string_dbg(domain_sid)));
3807 return NT_STATUS_UNSUCCESSFUL;
3810 if (num_members == 0) {
3811 return NT_STATUS_OK;
3814 filter = talloc_asprintf(mem_ctx,
3815 "(&(objectclass=%s)(sambaGroupType=%d)(|",
3816 LDAP_OBJ_GROUPMAP, type);
3818 for (i=0; i<num_members; i++)
3819 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3820 filter,
3821 sid_string_talloc(mem_ctx,
3822 &members[i]));
3824 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3826 if (filter == NULL) {
3827 return NT_STATUS_NO_MEMORY;
3830 if (is_builtin &&
3831 ldap_state->search_cache.filter &&
3832 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3833 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3834 result = ldap_state->search_cache.result;
3835 ldap_state->search_cache.result = NULL;
3836 } else {
3837 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3838 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3839 if (rc != LDAP_SUCCESS) {
3840 return NT_STATUS_UNSUCCESSFUL;
3842 talloc_autofree_ldapmsg(filter, result);
3845 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3847 for (entry = ldap_first_entry(ldap_struct, result);
3848 entry != NULL;
3849 entry = ldap_next_entry(ldap_struct, entry))
3851 fstring sid_str;
3852 struct dom_sid sid;
3853 uint32_t rid;
3855 if (!smbldap_get_single_attribute(ldap_struct, entry,
3856 LDAP_ATTRIBUTE_SID,
3857 sid_str,
3858 sizeof(sid_str)-1))
3859 continue;
3861 if (!string_to_sid(&sid, sid_str))
3862 continue;
3864 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3865 continue;
3867 sid_added = true;
3869 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3870 p_num_alias_rids)) {
3871 return NT_STATUS_NO_MEMORY;
3875 if (!is_builtin && !sid_added) {
3876 TALLOC_FREE(ldap_state->search_cache.filter);
3878 * Note: result is a talloc child of filter because of the
3879 * talloc_autofree_ldapmsg() usage
3881 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3882 ldap_state->search_cache.result = result;
3885 return NT_STATUS_OK;
3888 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3889 enum pdb_policy_type type,
3890 uint32_t value)
3892 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3893 int rc;
3894 LDAPMod **mods = NULL;
3895 fstring value_string;
3896 const char *policy_attr = NULL;
3898 struct ldapsam_privates *ldap_state =
3899 (struct ldapsam_privates *)methods->private_data;
3901 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3903 if (!ldap_state->domain_dn) {
3904 return NT_STATUS_INVALID_PARAMETER;
3907 policy_attr = get_account_policy_attr(type);
3908 if (policy_attr == NULL) {
3909 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3910 "policy\n"));
3911 return ntstatus;
3914 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3916 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3918 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3919 mods);
3921 ldap_mods_free(mods, True);
3923 if (rc != LDAP_SUCCESS) {
3924 return ntstatus;
3927 if (!cache_account_policy_set(type, value)) {
3928 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3929 "update local tdb cache\n"));
3930 return ntstatus;
3933 return NT_STATUS_OK;
3936 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3937 enum pdb_policy_type type,
3938 uint32_t value)
3940 return ldapsam_set_account_policy_in_ldap(methods, type,
3941 value);
3944 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3945 enum pdb_policy_type type,
3946 uint32_t *value)
3948 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3949 LDAPMessage *result = NULL;
3950 LDAPMessage *entry = NULL;
3951 int count;
3952 int rc;
3953 char **vals = NULL;
3954 char *filter;
3955 const char *policy_attr = NULL;
3957 struct ldapsam_privates *ldap_state =
3958 (struct ldapsam_privates *)methods->private_data;
3960 const char *attrs[2];
3962 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3964 if (!ldap_state->domain_dn) {
3965 return NT_STATUS_INVALID_PARAMETER;
3968 policy_attr = get_account_policy_attr(type);
3969 if (!policy_attr) {
3970 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3971 "policy index: %d\n", type));
3972 return ntstatus;
3975 attrs[0] = policy_attr;
3976 attrs[1] = NULL;
3978 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3979 if (filter == NULL) {
3980 return NT_STATUS_NO_MEMORY;
3982 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3983 LDAP_SCOPE_BASE, filter, attrs, 0,
3984 &result);
3985 TALLOC_FREE(filter);
3986 if (rc != LDAP_SUCCESS) {
3987 return ntstatus;
3990 count = ldap_count_entries(priv2ld(ldap_state), result);
3991 if (count < 1) {
3992 goto out;
3995 entry = ldap_first_entry(priv2ld(ldap_state), result);
3996 if (entry == NULL) {
3997 goto out;
4000 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4001 if (vals == NULL) {
4002 goto out;
4005 *value = (uint32_t)atol(vals[0]);
4007 ntstatus = NT_STATUS_OK;
4009 out:
4010 if (vals)
4011 ldap_value_free(vals);
4012 ldap_msgfree(result);
4014 return ntstatus;
4017 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4019 - if user hasn't decided to use account policies inside LDAP just reuse the
4020 old tdb values
4022 - if there is a valid cache entry, return that
4023 - if there is an LDAP entry, update cache and return
4024 - otherwise set to default, update cache and return
4026 Guenther
4028 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4029 enum pdb_policy_type type,
4030 uint32_t *value)
4032 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4034 if (cache_account_policy_get(type, value)) {
4035 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4036 "cache\n"));
4037 return NT_STATUS_OK;
4040 ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
4041 value);
4042 if (NT_STATUS_IS_OK(ntstatus)) {
4043 goto update_cache;
4046 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4047 "ldap\n"));
4049 #if 0
4050 /* should we automagically migrate old tdb value here ? */
4051 if (account_policy_get(type, value))
4052 goto update_ldap;
4054 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4055 "default\n", type));
4056 #endif
4058 if (!account_policy_get_default(type, value)) {
4059 return ntstatus;
4062 /* update_ldap: */
4064 ntstatus = ldapsam_set_account_policy(methods, type, *value);
4065 if (!NT_STATUS_IS_OK(ntstatus)) {
4066 return ntstatus;
4069 update_cache:
4071 if (!cache_account_policy_set(type, *value)) {
4072 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4073 "tdb as a cache\n"));
4074 return NT_STATUS_UNSUCCESSFUL;
4077 return NT_STATUS_OK;
4080 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4081 const struct dom_sid *domain_sid,
4082 int num_rids,
4083 uint32_t *rids,
4084 const char **names,
4085 enum lsa_SidType *attrs)
4087 struct ldapsam_privates *ldap_state =
4088 (struct ldapsam_privates *)methods->private_data;
4089 LDAPMessage *msg = NULL;
4090 LDAPMessage *entry;
4091 char *allsids = NULL;
4092 int i, rc, num_mapped;
4093 NTSTATUS result = NT_STATUS_NO_MEMORY;
4094 TALLOC_CTX *mem_ctx;
4095 LDAP *ld;
4096 bool is_builtin;
4098 mem_ctx = talloc_new(NULL);
4099 if (mem_ctx == NULL) {
4100 DEBUG(0, ("talloc_new failed\n"));
4101 goto done;
4104 if (!sid_check_is_builtin(domain_sid) &&
4105 !sid_check_is_domain(domain_sid)) {
4106 result = NT_STATUS_INVALID_PARAMETER;
4107 goto done;
4110 if (num_rids == 0) {
4111 result = NT_STATUS_NONE_MAPPED;
4112 goto done;
4115 for (i=0; i<num_rids; i++)
4116 attrs[i] = SID_NAME_UNKNOWN;
4118 allsids = talloc_strdup(mem_ctx, "");
4119 if (allsids == NULL) {
4120 goto done;
4123 for (i=0; i<num_rids; i++) {
4124 struct dom_sid sid;
4125 sid_compose(&sid, domain_sid, rids[i]);
4126 allsids = talloc_asprintf_append_buffer(
4127 allsids, "(sambaSid=%s)",
4128 sid_string_talloc(mem_ctx, &sid));
4129 if (allsids == NULL) {
4130 goto done;
4134 /* First look for users */
4137 char *filter;
4138 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4140 filter = talloc_asprintf(
4141 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4142 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4144 if (filter == NULL) {
4145 goto done;
4148 rc = smbldap_search(ldap_state->smbldap_state,
4149 lp_ldap_user_suffix(),
4150 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4151 &msg);
4152 talloc_autofree_ldapmsg(mem_ctx, msg);
4155 if (rc != LDAP_SUCCESS)
4156 goto done;
4158 ld = ldap_state->smbldap_state->ldap_struct;
4159 num_mapped = 0;
4161 for (entry = ldap_first_entry(ld, msg);
4162 entry != NULL;
4163 entry = ldap_next_entry(ld, entry)) {
4164 uint32_t rid;
4165 int rid_index;
4166 const char *name;
4168 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4169 &rid)) {
4170 DEBUG(2, ("Could not find sid from ldap entry\n"));
4171 continue;
4174 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4175 names);
4176 if (name == NULL) {
4177 DEBUG(2, ("Could not retrieve uid attribute\n"));
4178 continue;
4181 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4182 if (rid == rids[rid_index])
4183 break;
4186 if (rid_index == num_rids) {
4187 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4188 continue;
4191 attrs[rid_index] = SID_NAME_USER;
4192 names[rid_index] = name;
4193 num_mapped += 1;
4196 if (num_mapped == num_rids) {
4197 /* No need to look for groups anymore -- we're done */
4198 result = NT_STATUS_OK;
4199 goto done;
4202 /* Same game for groups */
4205 char *filter;
4206 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4207 "sambaGroupType", NULL };
4209 filter = talloc_asprintf(
4210 mem_ctx, "(&(objectClass=%s)(|%s))",
4211 LDAP_OBJ_GROUPMAP, allsids);
4212 if (filter == NULL) {
4213 goto done;
4216 rc = smbldap_search(ldap_state->smbldap_state,
4217 lp_ldap_suffix(),
4218 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4219 &msg);
4220 talloc_autofree_ldapmsg(mem_ctx, msg);
4223 if (rc != LDAP_SUCCESS)
4224 goto done;
4226 /* ldap_struct might have changed due to a reconnect */
4228 ld = ldap_state->smbldap_state->ldap_struct;
4230 /* For consistency checks, we already checked we're only domain or builtin */
4232 is_builtin = sid_check_is_builtin(domain_sid);
4234 for (entry = ldap_first_entry(ld, msg);
4235 entry != NULL;
4236 entry = ldap_next_entry(ld, entry))
4238 uint32_t rid;
4239 int rid_index;
4240 const char *attr;
4241 enum lsa_SidType type;
4242 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4244 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4245 mem_ctx);
4246 if (attr == NULL) {
4247 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4248 dn));
4249 continue;
4252 type = (enum lsa_SidType)atol(attr);
4254 /* Consistency checks */
4255 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4256 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4257 (type != SID_NAME_DOM_GRP)))) {
4258 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4261 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4262 &rid)) {
4263 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4264 continue;
4267 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4269 if (attr == NULL) {
4270 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4271 dn));
4272 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4275 if (attr == NULL) {
4276 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4277 dn));
4278 continue;
4281 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4282 if (rid == rids[rid_index])
4283 break;
4286 if (rid_index == num_rids) {
4287 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4288 continue;
4291 attrs[rid_index] = type;
4292 names[rid_index] = attr;
4293 num_mapped += 1;
4296 result = NT_STATUS_NONE_MAPPED;
4298 if (num_mapped > 0)
4299 result = (num_mapped == num_rids) ?
4300 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4301 done:
4302 TALLOC_FREE(mem_ctx);
4303 return result;
4306 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4308 char *filter = NULL;
4309 char *escaped = NULL;
4310 char *result = NULL;
4312 if (asprintf(&filter, "(&%s(objectclass=%s))",
4313 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4314 goto done;
4317 escaped = escape_ldap_string(talloc_tos(), username);
4318 if (escaped == NULL) goto done;
4320 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4322 done:
4323 SAFE_FREE(filter);
4324 TALLOC_FREE(escaped);
4326 return result;
4329 static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4331 int i, num = 0;
4332 va_list ap;
4333 const char **result;
4335 va_start(ap, mem_ctx);
4336 while (va_arg(ap, const char *) != NULL)
4337 num += 1;
4338 va_end(ap);
4340 if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
4341 return NULL;
4344 va_start(ap, mem_ctx);
4345 for (i=0; i<num; i++) {
4346 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4347 if (result[i] == NULL) {
4348 talloc_free(result);
4349 va_end(ap);
4350 return NULL;
4353 va_end(ap);
4355 result[num] = NULL;
4356 return result;
4359 struct ldap_search_state {
4360 struct smbldap_state *connection;
4362 uint32_t acct_flags;
4363 uint16_t group_type;
4365 const char *base;
4366 int scope;
4367 const char *filter;
4368 const char **attrs;
4369 int attrsonly;
4370 void *pagedresults_cookie;
4372 LDAPMessage *entries, *current_entry;
4373 bool (*ldap2displayentry)(struct ldap_search_state *state,
4374 TALLOC_CTX *mem_ctx,
4375 LDAP *ld, LDAPMessage *entry,
4376 struct samr_displayentry *result);
4379 static bool ldapsam_search_firstpage(struct pdb_search *search)
4381 struct ldap_search_state *state =
4382 (struct ldap_search_state *)search->private_data;
4383 LDAP *ld;
4384 int rc = LDAP_OPERATIONS_ERROR;
4386 state->entries = NULL;
4388 if (state->connection->paged_results) {
4389 rc = smbldap_search_paged(state->connection, state->base,
4390 state->scope, state->filter,
4391 state->attrs, state->attrsonly,
4392 lp_ldap_page_size(), &state->entries,
4393 &state->pagedresults_cookie);
4396 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4398 if (state->entries != NULL) {
4399 /* Left over from unsuccessful paged attempt */
4400 ldap_msgfree(state->entries);
4401 state->entries = NULL;
4404 rc = smbldap_search(state->connection, state->base,
4405 state->scope, state->filter, state->attrs,
4406 state->attrsonly, &state->entries);
4408 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4409 return False;
4411 /* Ok, the server was lying. It told us it could do paged
4412 * searches when it could not. */
4413 state->connection->paged_results = False;
4416 ld = state->connection->ldap_struct;
4417 if ( ld == NULL) {
4418 DEBUG(5, ("Don't have an LDAP connection right after a "
4419 "search\n"));
4420 return False;
4422 state->current_entry = ldap_first_entry(ld, state->entries);
4424 return True;
4427 static bool ldapsam_search_nextpage(struct pdb_search *search)
4429 struct ldap_search_state *state =
4430 (struct ldap_search_state *)search->private_data;
4431 int rc;
4433 if (!state->connection->paged_results) {
4434 /* There is no next page when there are no paged results */
4435 return False;
4438 rc = smbldap_search_paged(state->connection, state->base,
4439 state->scope, state->filter, state->attrs,
4440 state->attrsonly, lp_ldap_page_size(),
4441 &state->entries,
4442 &state->pagedresults_cookie);
4444 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4445 return False;
4447 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4449 if (state->current_entry == NULL) {
4450 ldap_msgfree(state->entries);
4451 state->entries = NULL;
4452 return false;
4455 return True;
4458 static bool ldapsam_search_next_entry(struct pdb_search *search,
4459 struct samr_displayentry *entry)
4461 struct ldap_search_state *state =
4462 (struct ldap_search_state *)search->private_data;
4463 bool result;
4465 retry:
4466 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4467 return False;
4469 if ((state->entries == NULL) &&
4470 !ldapsam_search_nextpage(search))
4471 return False;
4473 if (state->current_entry == NULL) {
4474 return false;
4477 result = state->ldap2displayentry(state, search,
4478 state->connection->ldap_struct,
4479 state->current_entry, entry);
4481 if (!result) {
4482 char *dn;
4483 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4484 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4485 if (dn != NULL) ldap_memfree(dn);
4488 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4490 if (state->current_entry == NULL) {
4491 ldap_msgfree(state->entries);
4492 state->entries = NULL;
4495 if (!result) goto retry;
4497 return True;
4500 static void ldapsam_search_end(struct pdb_search *search)
4502 struct ldap_search_state *state =
4503 (struct ldap_search_state *)search->private_data;
4504 int rc;
4506 if (state->pagedresults_cookie == NULL)
4507 return;
4509 if (state->entries != NULL)
4510 ldap_msgfree(state->entries);
4512 state->entries = NULL;
4513 state->current_entry = NULL;
4515 if (!state->connection->paged_results)
4516 return;
4518 /* Tell the LDAP server we're not interested in the rest anymore. */
4520 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4521 state->filter, state->attrs,
4522 state->attrsonly, 0, &state->entries,
4523 &state->pagedresults_cookie);
4525 if (rc != LDAP_SUCCESS)
4526 DEBUG(5, ("Could not end search properly\n"));
4528 return;
4531 static bool ldapuser2displayentry(struct ldap_search_state *state,
4532 TALLOC_CTX *mem_ctx,
4533 LDAP *ld, LDAPMessage *entry,
4534 struct samr_displayentry *result)
4536 char **vals;
4537 size_t converted_size;
4538 struct dom_sid sid;
4539 uint32_t acct_flags;
4541 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4542 if ((vals == NULL) || (vals[0] == NULL)) {
4543 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4544 return False;
4546 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4547 ldap_value_free(vals);
4549 if ((state->acct_flags != 0) &&
4550 ((state->acct_flags & acct_flags) == 0))
4551 return False;
4553 result->acct_flags = acct_flags;
4554 result->account_name = "";
4555 result->fullname = "";
4556 result->description = "";
4558 vals = ldap_get_values(ld, entry, "uid");
4559 if ((vals == NULL) || (vals[0] == NULL)) {
4560 DEBUG(5, ("\"uid\" not found\n"));
4561 return False;
4563 if (!pull_utf8_talloc(mem_ctx,
4564 CONST_DISCARD(char **, &result->account_name),
4565 vals[0], &converted_size))
4567 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4568 strerror(errno)));
4571 ldap_value_free(vals);
4573 vals = ldap_get_values(ld, entry, "displayName");
4574 if ((vals == NULL) || (vals[0] == NULL))
4575 DEBUG(8, ("\"displayName\" not found\n"));
4576 else if (!pull_utf8_talloc(mem_ctx,
4577 CONST_DISCARD(char **, &result->fullname),
4578 vals[0], &converted_size))
4580 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4581 strerror(errno)));
4584 ldap_value_free(vals);
4586 vals = ldap_get_values(ld, entry, "description");
4587 if ((vals == NULL) || (vals[0] == NULL))
4588 DEBUG(8, ("\"description\" not found\n"));
4589 else if (!pull_utf8_talloc(mem_ctx,
4590 CONST_DISCARD(char **, &result->description),
4591 vals[0], &converted_size))
4593 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4594 strerror(errno)));
4597 ldap_value_free(vals);
4599 if ((result->account_name == NULL) ||
4600 (result->fullname == NULL) ||
4601 (result->description == NULL)) {
4602 DEBUG(0, ("talloc failed\n"));
4603 return False;
4606 vals = ldap_get_values(ld, entry, "sambaSid");
4607 if ((vals == NULL) || (vals[0] == NULL)) {
4608 DEBUG(0, ("\"objectSid\" not found\n"));
4609 return False;
4612 if (!string_to_sid(&sid, vals[0])) {
4613 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4614 ldap_value_free(vals);
4615 return False;
4617 ldap_value_free(vals);
4619 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4620 DEBUG(0, ("sid %s does not belong to our domain\n",
4621 sid_string_dbg(&sid)));
4622 return False;
4625 return True;
4629 static bool ldapsam_search_users(struct pdb_methods *methods,
4630 struct pdb_search *search,
4631 uint32_t acct_flags)
4633 struct ldapsam_privates *ldap_state =
4634 (struct ldapsam_privates *)methods->private_data;
4635 struct ldap_search_state *state;
4637 state = talloc(search, struct ldap_search_state);
4638 if (state == NULL) {
4639 DEBUG(0, ("talloc failed\n"));
4640 return False;
4643 state->connection = ldap_state->smbldap_state;
4645 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4646 state->base = lp_ldap_user_suffix();
4647 else if ((acct_flags != 0) &&
4648 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4649 state->base = lp_ldap_machine_suffix();
4650 else
4651 state->base = lp_ldap_suffix();
4653 state->acct_flags = acct_flags;
4654 state->base = talloc_strdup(search, state->base);
4655 state->scope = LDAP_SCOPE_SUBTREE;
4656 state->filter = get_ldap_filter(search, "*");
4657 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4658 "displayName", "description",
4659 "sambaAcctFlags", NULL);
4660 state->attrsonly = 0;
4661 state->pagedresults_cookie = NULL;
4662 state->entries = NULL;
4663 state->ldap2displayentry = ldapuser2displayentry;
4665 if ((state->filter == NULL) || (state->attrs == NULL)) {
4666 DEBUG(0, ("talloc failed\n"));
4667 return False;
4670 search->private_data = state;
4671 search->next_entry = ldapsam_search_next_entry;
4672 search->search_end = ldapsam_search_end;
4674 return ldapsam_search_firstpage(search);
4677 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4678 TALLOC_CTX *mem_ctx,
4679 LDAP *ld, LDAPMessage *entry,
4680 struct samr_displayentry *result)
4682 char **vals;
4683 size_t converted_size;
4684 struct dom_sid sid;
4685 uint16_t group_type;
4687 result->account_name = "";
4688 result->fullname = "";
4689 result->description = "";
4692 vals = ldap_get_values(ld, entry, "sambaGroupType");
4693 if ((vals == NULL) || (vals[0] == NULL)) {
4694 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4695 if (vals != NULL) {
4696 ldap_value_free(vals);
4698 return False;
4701 group_type = atoi(vals[0]);
4703 if ((state->group_type != 0) &&
4704 ((state->group_type != group_type))) {
4705 ldap_value_free(vals);
4706 return False;
4709 ldap_value_free(vals);
4711 /* display name is the NT group name */
4713 vals = ldap_get_values(ld, entry, "displayName");
4714 if ((vals == NULL) || (vals[0] == NULL)) {
4715 DEBUG(8, ("\"displayName\" not found\n"));
4717 /* fallback to the 'cn' attribute */
4718 vals = ldap_get_values(ld, entry, "cn");
4719 if ((vals == NULL) || (vals[0] == NULL)) {
4720 DEBUG(5, ("\"cn\" not found\n"));
4721 return False;
4723 if (!pull_utf8_talloc(mem_ctx,
4724 CONST_DISCARD(char **,
4725 &result->account_name),
4726 vals[0], &converted_size))
4728 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4729 "failed: %s", strerror(errno)));
4732 else if (!pull_utf8_talloc(mem_ctx,
4733 CONST_DISCARD(char **,
4734 &result->account_name),
4735 vals[0], &converted_size))
4737 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4738 strerror(errno)));
4741 ldap_value_free(vals);
4743 vals = ldap_get_values(ld, entry, "description");
4744 if ((vals == NULL) || (vals[0] == NULL))
4745 DEBUG(8, ("\"description\" not found\n"));
4746 else if (!pull_utf8_talloc(mem_ctx,
4747 CONST_DISCARD(char **, &result->description),
4748 vals[0], &converted_size))
4750 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4751 strerror(errno)));
4753 ldap_value_free(vals);
4755 if ((result->account_name == NULL) ||
4756 (result->fullname == NULL) ||
4757 (result->description == NULL)) {
4758 DEBUG(0, ("talloc failed\n"));
4759 return False;
4762 vals = ldap_get_values(ld, entry, "sambaSid");
4763 if ((vals == NULL) || (vals[0] == NULL)) {
4764 DEBUG(0, ("\"objectSid\" not found\n"));
4765 if (vals != NULL) {
4766 ldap_value_free(vals);
4768 return False;
4771 if (!string_to_sid(&sid, vals[0])) {
4772 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4773 return False;
4776 ldap_value_free(vals);
4778 switch (group_type) {
4779 case SID_NAME_DOM_GRP:
4780 case SID_NAME_ALIAS:
4782 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4783 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4785 DEBUG(0, ("%s is not in our domain\n",
4786 sid_string_dbg(&sid)));
4787 return False;
4789 break;
4791 default:
4792 DEBUG(0,("unknown group type: %d\n", group_type));
4793 return False;
4796 result->acct_flags = 0;
4798 return True;
4801 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4802 struct pdb_search *search,
4803 const struct dom_sid *sid,
4804 enum lsa_SidType type)
4806 struct ldapsam_privates *ldap_state =
4807 (struct ldapsam_privates *)methods->private_data;
4808 struct ldap_search_state *state;
4809 fstring tmp;
4811 state = talloc(search, struct ldap_search_state);
4812 if (state == NULL) {
4813 DEBUG(0, ("talloc failed\n"));
4814 return False;
4817 state->connection = ldap_state->smbldap_state;
4819 state->base = talloc_strdup(search, lp_ldap_suffix());
4820 state->connection = ldap_state->smbldap_state;
4821 state->scope = LDAP_SCOPE_SUBTREE;
4822 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4823 "(sambaGroupType=%d)(sambaSID=%s*))",
4824 LDAP_OBJ_GROUPMAP,
4825 type, sid_to_fstring(tmp, sid));
4826 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4827 "displayName", "description",
4828 "sambaGroupType", NULL);
4829 state->attrsonly = 0;
4830 state->pagedresults_cookie = NULL;
4831 state->entries = NULL;
4832 state->group_type = type;
4833 state->ldap2displayentry = ldapgroup2displayentry;
4835 if ((state->filter == NULL) || (state->attrs == NULL)) {
4836 DEBUG(0, ("talloc failed\n"));
4837 return False;
4840 search->private_data = state;
4841 search->next_entry = ldapsam_search_next_entry;
4842 search->search_end = ldapsam_search_end;
4844 return ldapsam_search_firstpage(search);
4847 static bool ldapsam_search_groups(struct pdb_methods *methods,
4848 struct pdb_search *search)
4850 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4853 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4854 struct pdb_search *search,
4855 const struct dom_sid *sid)
4857 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4860 static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
4862 return PDB_CAP_STORE_RIDS;
4865 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4866 uint32_t *rid)
4868 struct smbldap_state *smbldap_state = priv->smbldap_state;
4870 LDAPMessage *result = NULL;
4871 LDAPMessage *entry = NULL;
4872 LDAPMod **mods = NULL;
4873 NTSTATUS status;
4874 char *value;
4875 int rc;
4876 uint32_t nextRid = 0;
4877 const char *dn;
4879 TALLOC_CTX *mem_ctx;
4881 mem_ctx = talloc_new(NULL);
4882 if (mem_ctx == NULL) {
4883 DEBUG(0, ("talloc_new failed\n"));
4884 return NT_STATUS_NO_MEMORY;
4887 status = smbldap_search_domain_info(smbldap_state, &result,
4888 get_global_sam_name(), False);
4889 if (!NT_STATUS_IS_OK(status)) {
4890 DEBUG(3, ("Could not get domain info: %s\n",
4891 nt_errstr(status)));
4892 goto done;
4895 talloc_autofree_ldapmsg(mem_ctx, result);
4897 entry = ldap_first_entry(priv2ld(priv), result);
4898 if (entry == NULL) {
4899 DEBUG(0, ("Could not get domain info entry\n"));
4900 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4901 goto done;
4904 /* Find the largest of the three attributes "sambaNextRid",
4905 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4906 concept of differentiating between user and group rids, and will
4907 use only "sambaNextRid" in the future. But for compatibility
4908 reasons I look if others have chosen different strategies -- VL */
4910 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4911 "sambaNextRid", mem_ctx);
4912 if (value != NULL) {
4913 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4914 nextRid = MAX(nextRid, tmp);
4917 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4918 "sambaNextUserRid", mem_ctx);
4919 if (value != NULL) {
4920 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4921 nextRid = MAX(nextRid, tmp);
4924 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4925 "sambaNextGroupRid", mem_ctx);
4926 if (value != NULL) {
4927 uint32_t tmp = (uint32_t)strtoul(value, NULL, 10);
4928 nextRid = MAX(nextRid, tmp);
4931 if (nextRid == 0) {
4932 nextRid = BASE_RID-1;
4935 nextRid += 1;
4937 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4938 talloc_asprintf(mem_ctx, "%d", nextRid));
4939 talloc_autofree_ldapmod(mem_ctx, mods);
4941 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4942 status = NT_STATUS_NO_MEMORY;
4943 goto done;
4946 rc = smbldap_modify(smbldap_state, dn, mods);
4948 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4949 * please retry" */
4951 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4953 done:
4954 if (NT_STATUS_IS_OK(status)) {
4955 *rid = nextRid;
4958 TALLOC_FREE(mem_ctx);
4959 return status;
4962 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32_t *rid)
4964 int i;
4966 for (i=0; i<10; i++) {
4967 NTSTATUS result = ldapsam_get_new_rid(
4968 (struct ldapsam_privates *)methods->private_data, rid);
4969 if (NT_STATUS_IS_OK(result)) {
4970 return result;
4973 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4974 return result;
4977 /* The ldap update failed (maybe a race condition), retry */
4980 /* Tried 10 times, fail. */
4981 return NT_STATUS_ACCESS_DENIED;
4984 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
4986 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4987 return NT_STATUS_IS_OK(result) ? True : False;
4990 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4991 const struct dom_sid *sid,
4992 union unid_t *id, enum lsa_SidType *type)
4994 struct ldapsam_privates *priv =
4995 (struct ldapsam_privates *)methods->private_data;
4996 char *filter;
4997 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4998 NULL };
4999 LDAPMessage *result = NULL;
5000 LDAPMessage *entry = NULL;
5001 bool ret = False;
5002 char *value;
5003 int rc;
5005 TALLOC_CTX *mem_ctx;
5007 mem_ctx = talloc_new(NULL);
5008 if (mem_ctx == NULL) {
5009 DEBUG(0, ("talloc_new failed\n"));
5010 return False;
5013 filter = talloc_asprintf(mem_ctx,
5014 "(&(sambaSid=%s)"
5015 "(|(objectClass=%s)(objectClass=%s)))",
5016 sid_string_talloc(mem_ctx, sid),
5017 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5018 if (filter == NULL) {
5019 DEBUG(5, ("talloc_asprintf failed\n"));
5020 goto done;
5023 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5024 attrs, &result);
5025 if (rc != LDAP_SUCCESS) {
5026 goto done;
5028 talloc_autofree_ldapmsg(mem_ctx, result);
5030 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5031 DEBUG(10, ("Got %d entries, expected one\n",
5032 ldap_count_entries(priv2ld(priv), result)));
5033 goto done;
5036 entry = ldap_first_entry(priv2ld(priv), result);
5038 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5039 "sambaGroupType", mem_ctx);
5041 if (value != NULL) {
5042 const char *gid_str;
5043 /* It's a group */
5045 gid_str = smbldap_talloc_single_attribute(
5046 priv2ld(priv), entry, "gidNumber", mem_ctx);
5047 if (gid_str == NULL) {
5048 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5049 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5050 entry)));
5051 goto done;
5054 id->gid = strtoul(gid_str, NULL, 10);
5055 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
5056 store_gid_sid_cache(sid, id->gid);
5057 idmap_cache_set_sid2gid(sid, id->gid);
5058 ret = True;
5059 goto done;
5062 /* It must be a user */
5064 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5065 "uidNumber", mem_ctx);
5066 if (value == NULL) {
5067 DEBUG(1, ("Could not find uidNumber in %s\n",
5068 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5069 goto done;
5072 id->uid = strtoul(value, NULL, 10);
5073 *type = SID_NAME_USER;
5074 store_uid_sid_cache(sid, id->uid);
5075 idmap_cache_set_sid2uid(sid, id->uid);
5077 ret = True;
5078 done:
5079 TALLOC_FREE(mem_ctx);
5080 return ret;
5084 * Find the SID for a uid.
5085 * This is shortcut is only used if ldapsam:trusted is set to true.
5087 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5088 struct dom_sid *sid)
5090 struct ldapsam_privates *priv =
5091 (struct ldapsam_privates *)methods->private_data;
5092 char *filter;
5093 const char *attrs[] = { "sambaSID", NULL };
5094 LDAPMessage *result = NULL;
5095 LDAPMessage *entry = NULL;
5096 bool ret = false;
5097 char *user_sid_string;
5098 struct dom_sid user_sid;
5099 int rc;
5100 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5102 filter = talloc_asprintf(tmp_ctx,
5103 "(&(uidNumber=%u)"
5104 "(objectClass=%s)"
5105 "(objectClass=%s))",
5106 (unsigned int)uid,
5107 LDAP_OBJ_POSIXACCOUNT,
5108 LDAP_OBJ_SAMBASAMACCOUNT);
5109 if (filter == NULL) {
5110 DEBUG(3, ("talloc_asprintf failed\n"));
5111 goto done;
5114 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5115 if (rc != LDAP_SUCCESS) {
5116 goto done;
5118 talloc_autofree_ldapmsg(tmp_ctx, result);
5120 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5121 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5122 ldap_count_entries(priv2ld(priv), result),
5123 (unsigned int)uid));
5124 goto done;
5127 entry = ldap_first_entry(priv2ld(priv), result);
5129 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5130 "sambaSID", tmp_ctx);
5131 if (user_sid_string == NULL) {
5132 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5133 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5134 goto done;
5137 if (!string_to_sid(&user_sid, user_sid_string)) {
5138 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5139 user_sid_string));
5140 goto done;
5143 sid_copy(sid, &user_sid);
5145 store_uid_sid_cache(sid, uid);
5146 idmap_cache_set_sid2uid(sid, uid);
5148 ret = true;
5150 done:
5151 TALLOC_FREE(tmp_ctx);
5152 return ret;
5156 * Find the SID for a gid.
5157 * This is shortcut is only used if ldapsam:trusted is set to true.
5159 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5160 struct dom_sid *sid)
5162 struct ldapsam_privates *priv =
5163 (struct ldapsam_privates *)methods->private_data;
5164 char *filter;
5165 const char *attrs[] = { "sambaSID", NULL };
5166 LDAPMessage *result = NULL;
5167 LDAPMessage *entry = NULL;
5168 bool ret = false;
5169 char *group_sid_string;
5170 struct dom_sid group_sid;
5171 int rc;
5172 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5174 filter = talloc_asprintf(tmp_ctx,
5175 "(&(gidNumber=%u)"
5176 "(objectClass=%s))",
5177 (unsigned int)gid,
5178 LDAP_OBJ_GROUPMAP);
5179 if (filter == NULL) {
5180 DEBUG(3, ("talloc_asprintf failed\n"));
5181 goto done;
5184 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5185 if (rc != LDAP_SUCCESS) {
5186 goto done;
5188 talloc_autofree_ldapmsg(tmp_ctx, result);
5190 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5191 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5192 ldap_count_entries(priv2ld(priv), result),
5193 (unsigned int)gid));
5194 goto done;
5197 entry = ldap_first_entry(priv2ld(priv), result);
5199 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5200 "sambaSID", tmp_ctx);
5201 if (group_sid_string == NULL) {
5202 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5203 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5204 goto done;
5207 if (!string_to_sid(&group_sid, group_sid_string)) {
5208 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5209 group_sid_string));
5210 goto done;
5213 sid_copy(sid, &group_sid);
5215 store_gid_sid_cache(sid, gid);
5216 idmap_cache_set_sid2gid(sid, gid);
5218 ret = true;
5220 done:
5221 TALLOC_FREE(tmp_ctx);
5222 return ret;
5227 * The following functions are called only if
5228 * ldapsam:trusted and ldapsam:editposix are
5229 * set to true
5233 * ldapsam_create_user creates a new
5234 * posixAccount and sambaSamAccount object
5235 * in the ldap users subtree
5237 * The uid is allocated by winbindd.
5240 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5241 TALLOC_CTX *tmp_ctx, const char *name,
5242 uint32_t acb_info, uint32_t *rid)
5244 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5245 LDAPMessage *entry = NULL;
5246 LDAPMessage *result = NULL;
5247 uint32_t num_result;
5248 bool is_machine = False;
5249 bool add_posix = False;
5250 LDAPMod **mods = NULL;
5251 struct samu *user;
5252 char *filter;
5253 char *username;
5254 char *homedir;
5255 char *gidstr;
5256 char *uidstr;
5257 char *shell;
5258 const char *dn = NULL;
5259 struct dom_sid group_sid;
5260 struct dom_sid user_sid;
5261 gid_t gid = -1;
5262 uid_t uid = -1;
5263 NTSTATUS ret;
5264 int rc;
5266 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5267 acb_info & ACB_WSTRUST ||
5268 acb_info & ACB_SVRTRUST ||
5269 acb_info & ACB_DOMTRUST) {
5270 is_machine = True;
5273 username = escape_ldap_string(talloc_tos(), name);
5274 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5275 username, LDAP_OBJ_POSIXACCOUNT);
5276 TALLOC_FREE(username);
5278 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5279 if (rc != LDAP_SUCCESS) {
5280 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5281 return NT_STATUS_ACCESS_DENIED;
5283 talloc_autofree_ldapmsg(tmp_ctx, result);
5285 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5287 if (num_result > 1) {
5288 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5289 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5292 if (num_result == 1) {
5293 char *tmp;
5294 /* check if it is just a posix account.
5295 * or if there is a sid attached to this entry
5298 entry = ldap_first_entry(priv2ld(ldap_state), result);
5299 if (!entry) {
5300 return NT_STATUS_UNSUCCESSFUL;
5303 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5304 if (tmp) {
5305 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5306 return NT_STATUS_USER_EXISTS;
5309 /* it is just a posix account, retrieve the dn for later use */
5310 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5311 if (!dn) {
5312 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5313 return NT_STATUS_NO_MEMORY;
5317 if (num_result == 0) {
5318 add_posix = True;
5321 /* Create the basic samu structure and generate the mods for the ldap commit */
5322 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5323 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5324 return ret;
5327 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5329 user = samu_new(tmp_ctx);
5330 if (!user) {
5331 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5332 return NT_STATUS_NO_MEMORY;
5335 if (!pdb_set_username(user, name, PDB_SET)) {
5336 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5337 return NT_STATUS_UNSUCCESSFUL;
5339 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5340 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5341 return NT_STATUS_UNSUCCESSFUL;
5343 if (is_machine) {
5344 if (acb_info & ACB_NORMAL) {
5345 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5346 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5347 return NT_STATUS_UNSUCCESSFUL;
5349 } else {
5350 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5351 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5352 return NT_STATUS_UNSUCCESSFUL;
5355 } else {
5356 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5357 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5358 return NT_STATUS_UNSUCCESSFUL;
5362 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5363 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5364 return NT_STATUS_UNSUCCESSFUL;
5367 if (!init_ldap_from_sam(ldap_state, entry, &mods, user, element_is_set_or_changed)) {
5368 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5369 return NT_STATUS_UNSUCCESSFUL;
5372 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5373 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5375 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5377 if (add_posix) {
5378 char *escape_name;
5380 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5382 /* retrieve the Domain Users group gid */
5383 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_RID_USERS) ||
5384 !sid_to_gid(&group_sid, &gid)) {
5385 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5386 return NT_STATUS_INVALID_PRIMARY_GROUP;
5389 /* lets allocate a new userid for this user */
5390 if (!winbind_allocate_uid(&uid)) {
5391 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5392 return NT_STATUS_UNSUCCESSFUL;
5396 if (is_machine) {
5397 /* TODO: choose a more appropriate default for machines */
5398 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5399 shell = talloc_strdup(tmp_ctx, "/bin/false");
5400 } else {
5401 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5402 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5404 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5405 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5407 escape_name = escape_rdn_val_string_alloc(name);
5408 if (!escape_name) {
5409 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5410 return NT_STATUS_NO_MEMORY;
5413 if (is_machine) {
5414 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5415 } else {
5416 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5419 SAFE_FREE(escape_name);
5421 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5422 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5423 return NT_STATUS_NO_MEMORY;
5426 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5427 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5428 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5429 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5430 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5431 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5432 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5435 talloc_autofree_ldapmod(tmp_ctx, mods);
5437 if (add_posix) {
5438 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5439 } else {
5440 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5443 if (rc != LDAP_SUCCESS) {
5444 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5445 return NT_STATUS_UNSUCCESSFUL;
5448 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5450 flush_pwnam_cache();
5452 return NT_STATUS_OK;
5455 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5457 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5458 LDAPMessage *result = NULL;
5459 LDAPMessage *entry = NULL;
5460 int num_result;
5461 const char *dn;
5462 char *filter;
5463 int rc;
5465 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5467 filter = talloc_asprintf(tmp_ctx,
5468 "(&(uid=%s)"
5469 "(objectClass=%s)"
5470 "(objectClass=%s))",
5471 pdb_get_username(sam_acct),
5472 LDAP_OBJ_POSIXACCOUNT,
5473 LDAP_OBJ_SAMBASAMACCOUNT);
5474 if (filter == NULL) {
5475 return NT_STATUS_NO_MEMORY;
5478 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5479 if (rc != LDAP_SUCCESS) {
5480 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5481 return NT_STATUS_UNSUCCESSFUL;
5483 talloc_autofree_ldapmsg(tmp_ctx, result);
5485 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5487 if (num_result == 0) {
5488 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5489 return NT_STATUS_NO_SUCH_USER;
5492 if (num_result > 1) {
5493 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5494 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5497 entry = ldap_first_entry(priv2ld(ldap_state), result);
5498 if (!entry) {
5499 return NT_STATUS_UNSUCCESSFUL;
5502 /* it is just a posix account, retrieve the dn for later use */
5503 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5504 if (!dn) {
5505 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5506 return NT_STATUS_NO_MEMORY;
5509 /* try to remove memberships first */
5511 NTSTATUS status;
5512 struct dom_sid *sids = NULL;
5513 gid_t *gids = NULL;
5514 uint32_t num_groups = 0;
5515 int i;
5516 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5518 status = ldapsam_enum_group_memberships(my_methods,
5519 tmp_ctx,
5520 sam_acct,
5521 &sids,
5522 &gids,
5523 &num_groups);
5524 if (!NT_STATUS_IS_OK(status)) {
5525 goto delete_dn;
5528 for (i=0; i < num_groups; i++) {
5530 uint32_t group_rid;
5532 sid_peek_rid(&sids[i], &group_rid);
5534 ldapsam_del_groupmem(my_methods,
5535 tmp_ctx,
5536 group_rid,
5537 user_rid);
5541 delete_dn:
5543 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5544 if (rc != LDAP_SUCCESS) {
5545 return NT_STATUS_UNSUCCESSFUL;
5548 flush_pwnam_cache();
5550 return NT_STATUS_OK;
5554 * ldapsam_create_group creates a new
5555 * posixGroup and sambaGroupMapping object
5556 * in the ldap groups subtree
5558 * The gid is allocated by winbindd.
5561 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5562 TALLOC_CTX *tmp_ctx,
5563 const char *name,
5564 uint32_t *rid)
5566 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5567 NTSTATUS ret;
5568 LDAPMessage *entry = NULL;
5569 LDAPMessage *result = NULL;
5570 uint32_t num_result;
5571 bool is_new_entry = False;
5572 LDAPMod **mods = NULL;
5573 char *filter;
5574 char *groupsidstr;
5575 char *groupname;
5576 char *grouptype;
5577 char *gidstr;
5578 const char *dn = NULL;
5579 struct dom_sid group_sid;
5580 gid_t gid = -1;
5581 int rc;
5583 groupname = escape_ldap_string(talloc_tos(), name);
5584 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5585 groupname, LDAP_OBJ_POSIXGROUP);
5586 TALLOC_FREE(groupname);
5588 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5589 if (rc != LDAP_SUCCESS) {
5590 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5591 return NT_STATUS_UNSUCCESSFUL;
5593 talloc_autofree_ldapmsg(tmp_ctx, result);
5595 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5597 if (num_result > 1) {
5598 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5599 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5602 if (num_result == 1) {
5603 char *tmp;
5604 /* check if it is just a posix group.
5605 * or if there is a sid attached to this entry
5608 entry = ldap_first_entry(priv2ld(ldap_state), result);
5609 if (!entry) {
5610 return NT_STATUS_UNSUCCESSFUL;
5613 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5614 if (tmp) {
5615 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5616 return NT_STATUS_GROUP_EXISTS;
5619 /* it is just a posix group, retrieve the gid and the dn for later use */
5620 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5621 if (!tmp) {
5622 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5623 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5626 gid = strtoul(tmp, NULL, 10);
5628 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5629 if (!dn) {
5630 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5631 return NT_STATUS_NO_MEMORY;
5635 if (num_result == 0) {
5636 is_new_entry = true;
5639 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5640 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5641 return ret;
5644 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5646 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5647 &group_sid));
5648 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5650 if (!groupsidstr || !grouptype) {
5651 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5652 return NT_STATUS_NO_MEMORY;
5655 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5656 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5657 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5658 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5660 if (is_new_entry) {
5661 char *escape_name;
5663 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5665 /* lets allocate a new groupid for this group */
5666 if (!winbind_allocate_gid(&gid)) {
5667 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5668 return NT_STATUS_UNSUCCESSFUL;
5671 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5673 escape_name = escape_rdn_val_string_alloc(name);
5674 if (!escape_name) {
5675 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5676 return NT_STATUS_NO_MEMORY;
5679 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5681 SAFE_FREE(escape_name);
5683 if (!gidstr || !dn) {
5684 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5685 return NT_STATUS_NO_MEMORY;
5688 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5689 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5690 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5693 talloc_autofree_ldapmod(tmp_ctx, mods);
5695 if (is_new_entry) {
5696 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5697 #if 0
5698 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5699 /* This call may fail with rfc2307bis schema */
5700 /* Retry adding a structural class */
5701 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5702 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5704 #endif
5705 } else {
5706 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5709 if (rc != LDAP_SUCCESS) {
5710 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5711 return NT_STATUS_UNSUCCESSFUL;
5714 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5716 return NT_STATUS_OK;
5719 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32_t rid)
5721 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5722 LDAPMessage *result = NULL;
5723 LDAPMessage *entry = NULL;
5724 int num_result;
5725 const char *dn;
5726 char *gidstr;
5727 char *filter;
5728 struct dom_sid group_sid;
5729 int rc;
5731 /* get the group sid */
5732 sid_compose(&group_sid, get_global_sam_sid(), rid);
5734 filter = talloc_asprintf(tmp_ctx,
5735 "(&(sambaSID=%s)"
5736 "(objectClass=%s)"
5737 "(objectClass=%s))",
5738 sid_string_talloc(tmp_ctx, &group_sid),
5739 LDAP_OBJ_POSIXGROUP,
5740 LDAP_OBJ_GROUPMAP);
5741 if (filter == NULL) {
5742 return NT_STATUS_NO_MEMORY;
5745 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5746 if (rc != LDAP_SUCCESS) {
5747 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5748 return NT_STATUS_UNSUCCESSFUL;
5750 talloc_autofree_ldapmsg(tmp_ctx, result);
5752 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5754 if (num_result == 0) {
5755 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5756 return NT_STATUS_NO_SUCH_GROUP;
5759 if (num_result > 1) {
5760 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5761 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5764 entry = ldap_first_entry(priv2ld(ldap_state), result);
5765 if (!entry) {
5766 return NT_STATUS_UNSUCCESSFUL;
5769 /* here it is, retrieve the dn for later use */
5770 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5771 if (!dn) {
5772 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5773 return NT_STATUS_NO_MEMORY;
5776 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5777 if (!gidstr) {
5778 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5779 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5782 /* check no user have this group marked as primary group */
5783 filter = talloc_asprintf(tmp_ctx,
5784 "(&(gidNumber=%s)"
5785 "(objectClass=%s)"
5786 "(objectClass=%s))",
5787 gidstr,
5788 LDAP_OBJ_POSIXACCOUNT,
5789 LDAP_OBJ_SAMBASAMACCOUNT);
5791 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5792 if (rc != LDAP_SUCCESS) {
5793 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5794 return NT_STATUS_UNSUCCESSFUL;
5796 talloc_autofree_ldapmsg(tmp_ctx, result);
5798 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5800 if (num_result != 0) {
5801 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5802 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5805 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5806 if (rc != LDAP_SUCCESS) {
5807 return NT_STATUS_UNSUCCESSFUL;
5810 return NT_STATUS_OK;
5813 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5814 TALLOC_CTX *tmp_ctx,
5815 uint32_t group_rid,
5816 uint32_t member_rid,
5817 int modop)
5819 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5820 LDAPMessage *entry = NULL;
5821 LDAPMessage *result = NULL;
5822 uint32_t num_result;
5823 LDAPMod **mods = NULL;
5824 char *filter;
5825 char *uidstr;
5826 const char *dn = NULL;
5827 struct dom_sid group_sid;
5828 struct dom_sid member_sid;
5829 int rc;
5831 switch (modop) {
5832 case LDAP_MOD_ADD:
5833 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5834 break;
5835 case LDAP_MOD_DELETE:
5836 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5837 break;
5838 default:
5839 return NT_STATUS_UNSUCCESSFUL;
5842 /* get member sid */
5843 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5845 /* get the group sid */
5846 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5848 filter = talloc_asprintf(tmp_ctx,
5849 "(&(sambaSID=%s)"
5850 "(objectClass=%s)"
5851 "(objectClass=%s))",
5852 sid_string_talloc(tmp_ctx, &member_sid),
5853 LDAP_OBJ_POSIXACCOUNT,
5854 LDAP_OBJ_SAMBASAMACCOUNT);
5855 if (filter == NULL) {
5856 return NT_STATUS_NO_MEMORY;
5859 /* get the member uid */
5860 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5861 if (rc != LDAP_SUCCESS) {
5862 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5863 return NT_STATUS_UNSUCCESSFUL;
5865 talloc_autofree_ldapmsg(tmp_ctx, result);
5867 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5869 if (num_result == 0) {
5870 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5871 return NT_STATUS_NO_SUCH_MEMBER;
5874 if (num_result > 1) {
5875 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5876 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5879 entry = ldap_first_entry(priv2ld(ldap_state), result);
5880 if (!entry) {
5881 return NT_STATUS_UNSUCCESSFUL;
5884 if (modop == LDAP_MOD_DELETE) {
5885 /* check if we are trying to remove the member from his primary group */
5886 char *gidstr;
5887 gid_t user_gid, group_gid;
5889 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5890 if (!gidstr) {
5891 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5892 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5895 user_gid = strtoul(gidstr, NULL, 10);
5897 if (!sid_to_gid(&group_sid, &group_gid)) {
5898 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5899 return NT_STATUS_UNSUCCESSFUL;
5902 if (user_gid == group_gid) {
5903 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5904 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5908 /* here it is, retrieve the uid for later use */
5909 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5910 if (!uidstr) {
5911 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5912 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5915 filter = talloc_asprintf(tmp_ctx,
5916 "(&(sambaSID=%s)"
5917 "(objectClass=%s)"
5918 "(objectClass=%s))",
5919 sid_string_talloc(tmp_ctx, &group_sid),
5920 LDAP_OBJ_POSIXGROUP,
5921 LDAP_OBJ_GROUPMAP);
5923 /* get the group */
5924 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5925 if (rc != LDAP_SUCCESS) {
5926 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5927 return NT_STATUS_UNSUCCESSFUL;
5929 talloc_autofree_ldapmsg(tmp_ctx, result);
5931 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5933 if (num_result == 0) {
5934 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5935 return NT_STATUS_NO_SUCH_GROUP;
5938 if (num_result > 1) {
5939 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5940 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5943 entry = ldap_first_entry(priv2ld(ldap_state), result);
5944 if (!entry) {
5945 return NT_STATUS_UNSUCCESSFUL;
5948 /* here it is, retrieve the dn for later use */
5949 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5950 if (!dn) {
5951 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5952 return NT_STATUS_NO_MEMORY;
5955 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5957 talloc_autofree_ldapmod(tmp_ctx, mods);
5959 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5960 if (rc != LDAP_SUCCESS) {
5961 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5962 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5963 return NT_STATUS_MEMBER_IN_GROUP;
5965 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5966 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5967 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5969 return NT_STATUS_UNSUCCESSFUL;
5972 return NT_STATUS_OK;
5975 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5976 TALLOC_CTX *tmp_ctx,
5977 uint32_t group_rid,
5978 uint32_t member_rid)
5980 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5982 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5983 TALLOC_CTX *tmp_ctx,
5984 uint32_t group_rid,
5985 uint32_t member_rid)
5987 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5990 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5991 TALLOC_CTX *mem_ctx,
5992 struct samu *sampass)
5994 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5995 LDAPMessage *entry = NULL;
5996 LDAPMessage *result = NULL;
5997 uint32_t num_result;
5998 LDAPMod **mods = NULL;
5999 char *filter;
6000 char *escape_username;
6001 char *gidstr;
6002 const char *dn = NULL;
6003 gid_t gid;
6004 int rc;
6006 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6008 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6009 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6010 return NT_STATUS_UNSUCCESSFUL;
6012 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6013 if (!gidstr) {
6014 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6015 return NT_STATUS_NO_MEMORY;
6018 escape_username = escape_ldap_string(talloc_tos(),
6019 pdb_get_username(sampass));
6020 if (escape_username== NULL) {
6021 return NT_STATUS_NO_MEMORY;
6024 filter = talloc_asprintf(mem_ctx,
6025 "(&(uid=%s)"
6026 "(objectClass=%s)"
6027 "(objectClass=%s))",
6028 escape_username,
6029 LDAP_OBJ_POSIXACCOUNT,
6030 LDAP_OBJ_SAMBASAMACCOUNT);
6032 TALLOC_FREE(escape_username);
6034 if (filter == NULL) {
6035 return NT_STATUS_NO_MEMORY;
6038 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6039 if (rc != LDAP_SUCCESS) {
6040 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6041 return NT_STATUS_UNSUCCESSFUL;
6043 talloc_autofree_ldapmsg(mem_ctx, result);
6045 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6047 if (num_result == 0) {
6048 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6049 return NT_STATUS_NO_SUCH_USER;
6052 if (num_result > 1) {
6053 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6054 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6057 entry = ldap_first_entry(priv2ld(ldap_state), result);
6058 if (!entry) {
6059 return NT_STATUS_UNSUCCESSFUL;
6062 /* retrieve the dn for later use */
6063 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6064 if (!dn) {
6065 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6066 return NT_STATUS_NO_MEMORY;
6069 /* remove the old one, and add the new one, this way we do not risk races */
6070 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6072 if (mods == NULL) {
6073 return NT_STATUS_OK;
6076 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6078 if (rc != LDAP_SUCCESS) {
6079 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6080 pdb_get_username(sampass), gidstr));
6081 return NT_STATUS_UNSUCCESSFUL;
6084 flush_pwnam_cache();
6086 return NT_STATUS_OK;
6090 /**********************************************************************
6091 trusted domains functions
6092 *********************************************************************/
6094 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6095 const char *domain)
6097 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6098 ldap_state->domain_dn);
6101 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6102 TALLOC_CTX *mem_ctx,
6103 const char *domain, LDAPMessage **entry)
6105 int rc;
6106 char *filter;
6107 int scope = LDAP_SCOPE_SUBTREE;
6108 const char **attrs = NULL; /* NULL: get all attrs */
6109 int attrsonly = 0; /* 0: return values too */
6110 LDAPMessage *result = NULL;
6111 char *trusted_dn;
6112 uint32_t num_result;
6114 filter = talloc_asprintf(talloc_tos(),
6115 "(&(objectClass=%s)(sambaDomainName=%s))",
6116 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6118 trusted_dn = trusteddom_dn(ldap_state, domain);
6119 if (trusted_dn == NULL) {
6120 return False;
6122 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6123 filter, attrs, attrsonly, &result);
6125 if (result != NULL) {
6126 talloc_autofree_ldapmsg(mem_ctx, result);
6129 if (rc == LDAP_NO_SUCH_OBJECT) {
6130 *entry = NULL;
6131 return True;
6134 if (rc != LDAP_SUCCESS) {
6135 return False;
6138 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6140 if (num_result > 1) {
6141 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6142 "%s object for domain '%s'?!\n",
6143 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6144 return False;
6147 if (num_result == 0) {
6148 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6149 "%s object for domain %s.\n",
6150 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6151 *entry = NULL;
6152 } else {
6153 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6156 return True;
6159 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6160 const char *domain,
6161 char** pwd,
6162 struct dom_sid *sid,
6163 time_t *pass_last_set_time)
6165 struct ldapsam_privates *ldap_state =
6166 (struct ldapsam_privates *)methods->private_data;
6167 LDAPMessage *entry = NULL;
6169 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6171 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6172 (entry == NULL))
6174 return False;
6177 /* password */
6178 if (pwd != NULL) {
6179 char *pwd_str;
6180 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6181 entry, "sambaClearTextPassword", talloc_tos());
6182 if (pwd_str == NULL) {
6183 return False;
6185 /* trusteddom_pw routines do not use talloc yet... */
6186 *pwd = SMB_STRDUP(pwd_str);
6187 if (*pwd == NULL) {
6188 return False;
6192 /* last change time */
6193 if (pass_last_set_time != NULL) {
6194 char *time_str;
6195 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6196 entry, "sambaPwdLastSet", talloc_tos());
6197 if (time_str == NULL) {
6198 return False;
6200 *pass_last_set_time = (time_t)atol(time_str);
6203 /* domain sid */
6204 if (sid != NULL) {
6205 char *sid_str;
6206 struct dom_sid dom_sid;
6207 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6208 entry, "sambaSID",
6209 talloc_tos());
6210 if (sid_str == NULL) {
6211 return False;
6213 if (!string_to_sid(&dom_sid, sid_str)) {
6214 return False;
6216 sid_copy(sid, &dom_sid);
6219 return True;
6222 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6223 const char* domain,
6224 const char* pwd,
6225 const struct dom_sid *sid)
6227 struct ldapsam_privates *ldap_state =
6228 (struct ldapsam_privates *)methods->private_data;
6229 LDAPMessage *entry = NULL;
6230 LDAPMod **mods = NULL;
6231 char *prev_pwd = NULL;
6232 char *trusted_dn = NULL;
6233 int rc;
6235 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6238 * get the current entry (if there is one) in order to put the
6239 * current password into the previous password attribute
6241 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6242 return False;
6245 mods = NULL;
6246 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6247 LDAP_OBJ_TRUSTDOM_PASSWORD);
6248 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6249 domain);
6250 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6251 sid_string_tos(sid));
6252 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6253 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6254 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6255 "sambaClearTextPassword", pwd);
6257 if (entry != NULL) {
6258 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6259 entry, "sambaClearTextPassword", talloc_tos());
6260 if (prev_pwd != NULL) {
6261 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6262 "sambaPreviousClearTextPassword",
6263 prev_pwd);
6267 talloc_autofree_ldapmod(talloc_tos(), mods);
6269 trusted_dn = trusteddom_dn(ldap_state, domain);
6270 if (trusted_dn == NULL) {
6271 return False;
6273 if (entry == NULL) {
6274 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6275 } else {
6276 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6279 if (rc != LDAP_SUCCESS) {
6280 DEBUG(1, ("error writing trusted domain password!\n"));
6281 return False;
6284 return True;
6287 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6288 const char *domain)
6290 int rc;
6291 struct ldapsam_privates *ldap_state =
6292 (struct ldapsam_privates *)methods->private_data;
6293 LDAPMessage *entry = NULL;
6294 const char *trusted_dn;
6296 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6297 return False;
6300 if (entry == NULL) {
6301 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6302 "%s\n", domain));
6303 return True;
6306 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6307 entry);
6308 if (trusted_dn == NULL) {
6309 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6310 return False;
6313 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6314 if (rc != LDAP_SUCCESS) {
6315 return False;
6318 return True;
6321 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6322 TALLOC_CTX *mem_ctx,
6323 uint32_t *num_domains,
6324 struct trustdom_info ***domains)
6326 int rc;
6327 struct ldapsam_privates *ldap_state =
6328 (struct ldapsam_privates *)methods->private_data;
6329 char *filter;
6330 int scope = LDAP_SCOPE_SUBTREE;
6331 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6332 int attrsonly = 0; /* 0: return values too */
6333 LDAPMessage *result = NULL;
6334 LDAPMessage *entry = NULL;
6336 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6337 LDAP_OBJ_TRUSTDOM_PASSWORD);
6339 rc = smbldap_search(ldap_state->smbldap_state,
6340 ldap_state->domain_dn,
6341 scope,
6342 filter,
6343 attrs,
6344 attrsonly,
6345 &result);
6347 if (result != NULL) {
6348 talloc_autofree_ldapmsg(mem_ctx, result);
6351 if (rc != LDAP_SUCCESS) {
6352 return NT_STATUS_UNSUCCESSFUL;
6355 *num_domains = 0;
6356 if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
6357 DEBUG(1, ("talloc failed\n"));
6358 return NT_STATUS_NO_MEMORY;
6361 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6362 entry != NULL;
6363 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6365 char *dom_name, *dom_sid_str;
6366 struct trustdom_info *dom_info;
6368 dom_info = TALLOC_P(*domains, struct trustdom_info);
6369 if (dom_info == NULL) {
6370 DEBUG(1, ("talloc failed\n"));
6371 return NT_STATUS_NO_MEMORY;
6374 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6375 entry,
6376 "sambaDomainName",
6377 talloc_tos());
6378 if (dom_name == NULL) {
6379 DEBUG(1, ("talloc failed\n"));
6380 return NT_STATUS_NO_MEMORY;
6382 dom_info->name = dom_name;
6384 dom_sid_str = smbldap_talloc_single_attribute(
6385 priv2ld(ldap_state), entry, "sambaSID",
6386 talloc_tos());
6387 if (dom_sid_str == NULL) {
6388 DEBUG(1, ("talloc failed\n"));
6389 return NT_STATUS_NO_MEMORY;
6391 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6392 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6393 dom_sid_str));
6394 return NT_STATUS_UNSUCCESSFUL;
6397 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6398 domains, num_domains);
6400 if (*domains == NULL) {
6401 DEBUG(1, ("talloc failed\n"));
6402 return NT_STATUS_NO_MEMORY;
6406 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6407 return NT_STATUS_OK;
6411 /**********************************************************************
6412 Housekeeping
6413 *********************************************************************/
6415 static void free_private_data(void **vp)
6417 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6419 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6421 if ((*ldap_state)->result != NULL) {
6422 ldap_msgfree((*ldap_state)->result);
6423 (*ldap_state)->result = NULL;
6425 if ((*ldap_state)->domain_dn != NULL) {
6426 SAFE_FREE((*ldap_state)->domain_dn);
6429 *ldap_state = NULL;
6431 /* No need to free any further, as it is talloc()ed */
6434 /*********************************************************************
6435 Intitalise the parts of the pdb_methods structure that are common to
6436 all pdb_ldap modes
6437 *********************************************************************/
6439 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6441 NTSTATUS nt_status;
6442 struct ldapsam_privates *ldap_state;
6444 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6445 return nt_status;
6448 (*pdb_method)->name = "ldapsam";
6450 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6451 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6452 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6453 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6454 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6455 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6457 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6458 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6459 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6460 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6461 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6462 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6463 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6465 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6466 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6468 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6470 (*pdb_method)->capabilities = ldapsam_capabilities;
6471 (*pdb_method)->new_rid = ldapsam_new_rid;
6473 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6474 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6475 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6476 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6478 /* TODO: Setup private data and free */
6480 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
6481 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6482 return NT_STATUS_NO_MEMORY;
6485 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6486 location, &ldap_state->smbldap_state);
6488 if ( !NT_STATUS_IS_OK(nt_status) ) {
6489 return nt_status;
6492 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6493 return NT_STATUS_NO_MEMORY;
6496 (*pdb_method)->private_data = ldap_state;
6498 (*pdb_method)->free_private_data = free_private_data;
6500 return NT_STATUS_OK;
6503 /**********************************************************************
6504 Initialise the 'compat' mode for pdb_ldap
6505 *********************************************************************/
6507 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6509 NTSTATUS nt_status;
6510 struct ldapsam_privates *ldap_state;
6511 char *uri = talloc_strdup( NULL, location );
6513 trim_char( uri, '\"', '\"' );
6514 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6515 if ( uri )
6516 TALLOC_FREE( uri );
6518 if ( !NT_STATUS_IS_OK(nt_status) ) {
6519 return nt_status;
6522 (*pdb_method)->name = "ldapsam_compat";
6524 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6525 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6527 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6529 return NT_STATUS_OK;
6532 /**********************************************************************
6533 Initialise the normal mode for pdb_ldap
6534 *********************************************************************/
6536 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6538 NTSTATUS nt_status;
6539 struct ldapsam_privates *ldap_state = NULL;
6540 uint32_t alg_rid_base;
6541 char *alg_rid_base_string = NULL;
6542 LDAPMessage *result = NULL;
6543 LDAPMessage *entry = NULL;
6544 struct dom_sid ldap_domain_sid;
6545 struct dom_sid secrets_domain_sid;
6546 char *domain_sid_string = NULL;
6547 char *dn = NULL;
6548 char *uri = talloc_strdup( NULL, location );
6550 trim_char( uri, '\"', '\"' );
6551 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6553 TALLOC_FREE(uri);
6555 if (!NT_STATUS_IS_OK(nt_status)) {
6556 return nt_status;
6559 (*pdb_method)->name = "ldapsam";
6561 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6562 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6563 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6564 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6565 (*pdb_method)->search_users = ldapsam_search_users;
6566 (*pdb_method)->search_groups = ldapsam_search_groups;
6567 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6569 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6570 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6571 (*pdb_method)->enum_group_memberships =
6572 ldapsam_enum_group_memberships;
6573 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6574 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6575 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6576 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6578 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6579 (*pdb_method)->create_user = ldapsam_create_user;
6580 (*pdb_method)->delete_user = ldapsam_delete_user;
6581 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6582 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6583 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6584 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6585 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6589 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6590 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6592 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6594 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6595 &result,
6596 ldap_state->domain_name, True);
6598 if ( !NT_STATUS_IS_OK(nt_status) ) {
6599 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6600 "info, nor add one to the domain\n"));
6601 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6602 "will be unable to allocate new users/groups, "
6603 "and will risk BDCs having inconsistent SIDs\n"));
6604 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6605 return NT_STATUS_OK;
6608 /* Given that the above might fail, everything below this must be
6609 * optional */
6611 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6612 result);
6613 if (!entry) {
6614 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6615 "entry\n"));
6616 ldap_msgfree(result);
6617 return NT_STATUS_UNSUCCESSFUL;
6620 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6621 if (!dn) {
6622 ldap_msgfree(result);
6623 return NT_STATUS_UNSUCCESSFUL;
6626 ldap_state->domain_dn = smb_xstrdup(dn);
6627 TALLOC_FREE(dn);
6629 domain_sid_string = smbldap_talloc_single_attribute(
6630 ldap_state->smbldap_state->ldap_struct,
6631 entry,
6632 get_userattr_key2string(ldap_state->schema_ver,
6633 LDAP_ATTR_USER_SID),
6634 talloc_tos());
6636 if (domain_sid_string) {
6637 bool found_sid;
6638 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6639 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6640 "read as a valid SID\n", domain_sid_string));
6641 ldap_msgfree(result);
6642 TALLOC_FREE(domain_sid_string);
6643 return NT_STATUS_INVALID_PARAMETER;
6645 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6646 &secrets_domain_sid);
6647 if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
6648 &ldap_domain_sid)) {
6649 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6650 "%s based on pdb_ldap results %s -> %s\n",
6651 ldap_state->domain_name,
6652 sid_string_dbg(&secrets_domain_sid),
6653 sid_string_dbg(&ldap_domain_sid)));
6655 /* reset secrets.tdb sid */
6656 secrets_store_domain_sid(ldap_state->domain_name,
6657 &ldap_domain_sid);
6658 DEBUG(1, ("New global sam SID: %s\n",
6659 sid_string_dbg(get_global_sam_sid())));
6661 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6662 TALLOC_FREE(domain_sid_string);
6665 alg_rid_base_string = smbldap_talloc_single_attribute(
6666 ldap_state->smbldap_state->ldap_struct,
6667 entry,
6668 get_attr_key2string( dominfo_attr_list,
6669 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6670 talloc_tos());
6671 if (alg_rid_base_string) {
6672 alg_rid_base = (uint32_t)atol(alg_rid_base_string);
6673 if (alg_rid_base != algorithmic_rid_base()) {
6674 DEBUG(0, ("The value of 'algorithmic RID base' has "
6675 "changed since the LDAP\n"
6676 "database was initialised. Aborting. \n"));
6677 ldap_msgfree(result);
6678 TALLOC_FREE(alg_rid_base_string);
6679 return NT_STATUS_UNSUCCESSFUL;
6681 TALLOC_FREE(alg_rid_base_string);
6683 ldap_msgfree(result);
6685 return NT_STATUS_OK;
6688 NTSTATUS pdb_ldap_init(void)
6690 NTSTATUS nt_status;
6691 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6692 return nt_status;
6694 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6695 return nt_status;
6697 /* Let pdb_nds register backends */
6698 pdb_nds_init();
6700 pdb_ipa_init();
6702 return NT_STATUS_OK;