Fix for bug #5163 from Laurent Pinchart <pinchart@skynet.be>
[Samba/bjacke.git] / source3 / passdb / pdb_ldap.c
blob205b178a93f8bae746e5f8a93e6ce25ed8b7f7cd
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"
48 #undef DBGC_CLASS
49 #define DBGC_CLASS DBGC_PASSDB
51 #include <lber.h>
52 #include <ldap.h>
55 * Work around versions of the LDAP client libs that don't have the OIDs
56 * defined, or have them defined under the old name.
57 * This functionality is really a factor of the server, not the client
61 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
62 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
63 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
64 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
65 #endif
67 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
68 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
69 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
70 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
71 #endif
73 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
75 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
76 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
77 #endif
80 #include "smbldap.h"
82 /**********************************************************************
83 Simple helper function to make stuff better readable
84 **********************************************************************/
86 static LDAP *priv2ld(struct ldapsam_privates *priv)
88 return priv->smbldap_state->ldap_struct;
91 /**********************************************************************
92 Get the attribute name given a user schame version.
93 **********************************************************************/
95 static const char* get_userattr_key2string( int schema_ver, int key )
97 switch ( schema_ver ) {
98 case SCHEMAVER_SAMBAACCOUNT:
99 return get_attr_key2string( attrib_map_v22, key );
101 case SCHEMAVER_SAMBASAMACCOUNT:
102 return get_attr_key2string( attrib_map_v30, key );
104 default:
105 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
106 break;
108 return NULL;
111 /**********************************************************************
112 Return the list of attribute names given a user schema version.
113 **********************************************************************/
115 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
117 switch ( schema_ver ) {
118 case SCHEMAVER_SAMBAACCOUNT:
119 return get_attr_list( mem_ctx, attrib_map_v22 );
121 case SCHEMAVER_SAMBASAMACCOUNT:
122 return get_attr_list( mem_ctx, attrib_map_v30 );
123 default:
124 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
125 break;
128 return NULL;
131 /**************************************************************************
132 Return the list of attribute names to delete given a user schema version.
133 **************************************************************************/
135 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
136 int schema_ver )
138 switch ( schema_ver ) {
139 case SCHEMAVER_SAMBAACCOUNT:
140 return get_attr_list( mem_ctx,
141 attrib_map_to_delete_v22 );
143 case SCHEMAVER_SAMBASAMACCOUNT:
144 return get_attr_list( mem_ctx,
145 attrib_map_to_delete_v30 );
146 default:
147 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
148 break;
151 return NULL;
155 /*******************************************************************
156 Generate the LDAP search filter for the objectclass based on the
157 version of the schema we are using.
158 ******************************************************************/
160 static const char* get_objclass_filter( int schema_ver )
162 fstring objclass_filter;
163 char *result;
165 switch( schema_ver ) {
166 case SCHEMAVER_SAMBAACCOUNT:
167 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
168 break;
169 case SCHEMAVER_SAMBASAMACCOUNT:
170 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
171 break;
172 default:
173 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
174 objclass_filter[0] = '\0';
175 break;
178 result = talloc_strdup(talloc_tos(), objclass_filter);
179 SMB_ASSERT(result != NULL);
180 return result;
183 /*****************************************************************
184 Scan a sequence number off OpenLDAP's syncrepl contextCSN
185 ******************************************************************/
187 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
189 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
190 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
191 LDAPMessage *msg = NULL;
192 LDAPMessage *entry = NULL;
193 TALLOC_CTX *mem_ctx;
194 char **values = NULL;
195 int rc, num_result, num_values, rid;
196 char *suffix = NULL;
197 char *tok;
198 const char *p;
199 const char **attrs;
201 /* Unfortunatly there is no proper way to detect syncrepl-support in
202 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
203 * but do not show up in the root-DSE yet. Neither we can query the
204 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
205 * objectclass. Currently we require lp_ldap_suffix() to show up as
206 * namingContext. - Guenther
209 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
210 return ntstatus;
213 if (!seq_num) {
214 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
215 return ntstatus;
218 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
219 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
220 "as top-level namingContext\n", lp_ldap_suffix()));
221 return ntstatus;
224 mem_ctx = talloc_init("ldapsam_get_seq_num");
226 if (mem_ctx == NULL)
227 return NT_STATUS_NO_MEMORY;
229 if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
230 ntstatus = NT_STATUS_NO_MEMORY;
231 goto done;
234 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
235 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
236 if (rid > 0) {
238 /* consumer syncreplCookie: */
239 /* csn=20050126161620Z#0000001#00#00000 */
240 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
241 attrs[1] = NULL;
242 suffix = talloc_asprintf(mem_ctx,
243 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
244 if (!suffix) {
245 ntstatus = NT_STATUS_NO_MEMORY;
246 goto done;
248 } else {
250 /* provider contextCSN */
251 /* 20050126161620Z#000009#00#000000 */
252 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
253 attrs[1] = NULL;
254 suffix = talloc_asprintf(mem_ctx,
255 "cn=ldapsync,%s", lp_ldap_suffix());
257 if (!suffix) {
258 ntstatus = NT_STATUS_NO_MEMORY;
259 goto done;
263 rc = smbldap_search(ldap_state->smbldap_state, suffix,
264 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
266 if (rc != LDAP_SUCCESS) {
267 goto done;
270 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
271 if (num_result != 1) {
272 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
273 goto done;
276 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
277 if (entry == NULL) {
278 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
279 goto done;
282 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
283 if (values == NULL) {
284 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
285 goto done;
288 num_values = ldap_count_values(values);
289 if (num_values == 0) {
290 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
291 goto done;
294 p = values[0];
295 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
296 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
297 goto done;
300 p = tok;
301 if (!strncmp(p, "csn=", strlen("csn=")))
302 p += strlen("csn=");
304 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
306 *seq_num = generalized_to_unix_time(p);
308 /* very basic sanity check */
309 if (*seq_num <= 0) {
310 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
311 (int)*seq_num));
312 goto done;
315 ntstatus = NT_STATUS_OK;
317 done:
318 if (values != NULL)
319 ldap_value_free(values);
320 if (msg != NULL)
321 ldap_msgfree(msg);
322 if (mem_ctx)
323 talloc_destroy(mem_ctx);
325 return ntstatus;
328 /*******************************************************************
329 Run the search by name.
330 ******************************************************************/
332 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
333 const char *user,
334 LDAPMessage ** result,
335 const char **attr)
337 char *filter = NULL;
338 char *escape_user = escape_ldap_string_alloc(user);
339 int ret = -1;
341 if (!escape_user) {
342 return LDAP_NO_MEMORY;
346 * in the filter expression, replace %u with the real name
347 * so in ldap filter, %u MUST exist :-)
349 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
350 get_objclass_filter(ldap_state->schema_ver));
351 if (!filter) {
352 return LDAP_NO_MEMORY;
355 * have to use this here because $ is filtered out
356 * in string_sub
359 filter = talloc_all_string_sub(talloc_tos(),
360 filter, "%u", escape_user);
361 if (!filter) {
362 return LDAP_NO_MEMORY;
364 SAFE_FREE(escape_user);
366 ret = smbldap_search_suffix(ldap_state->smbldap_state,
367 filter, attr, result);
368 TALLOC_FREE(filter);
369 return ret;
372 /*******************************************************************
373 Run the search by rid.
374 ******************************************************************/
376 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
377 uint32 rid, LDAPMessage ** result,
378 const char **attr)
380 char *filter = NULL;
381 int rc;
383 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
384 get_objclass_filter(ldap_state->schema_ver));
385 if (!filter) {
386 return LDAP_NO_MEMORY;
389 rc = smbldap_search_suffix(ldap_state->smbldap_state,
390 filter, attr, result);
391 TALLOC_FREE(filter);
392 return rc;
395 /*******************************************************************
396 Run the search by SID.
397 ******************************************************************/
399 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
400 const DOM_SID *sid, LDAPMessage ** result,
401 const char **attr)
403 char *filter = NULL;
404 int rc;
405 fstring sid_string;
407 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
408 get_userattr_key2string(ldap_state->schema_ver,
409 LDAP_ATTR_USER_SID),
410 sid_to_fstring(sid_string, sid),
411 get_objclass_filter(ldap_state->schema_ver));
412 if (!filter) {
413 return LDAP_NO_MEMORY;
416 rc = smbldap_search_suffix(ldap_state->smbldap_state,
417 filter, attr, result);
419 TALLOC_FREE(filter);
420 return rc;
423 /*******************************************************************
424 Delete complete object or objectclass and attrs from
425 object found in search_result depending on lp_ldap_delete_dn
426 ******************************************************************/
428 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
429 TALLOC_CTX *mem_ctx,
430 LDAPMessage *entry,
431 const char *objectclass,
432 const char **attrs)
434 LDAPMod **mods = NULL;
435 char *name;
436 const char *dn;
437 BerElement *ptr = NULL;
439 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
440 if (dn == NULL) {
441 return LDAP_NO_MEMORY;
444 if (lp_ldap_delete_dn()) {
445 return smbldap_delete(priv->smbldap_state, dn);
448 /* Ok, delete only the SAM attributes */
450 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
451 name != NULL;
452 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
453 const char **attrib;
455 /* We are only allowed to delete the attributes that
456 really exist. */
458 for (attrib = attrs; *attrib != NULL; attrib++) {
459 if (strequal(*attrib, name)) {
460 DEBUG(10, ("ldapsam_delete_entry: deleting "
461 "attribute %s\n", name));
462 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
463 NULL);
466 ldap_memfree(name);
469 if (ptr != NULL) {
470 ber_free(ptr, 0);
473 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
474 talloc_autofree_ldapmod(mem_ctx, mods);
476 return smbldap_modify(priv->smbldap_state, dn, mods);
479 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
481 char *temp;
482 struct tm tm;
484 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
485 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
486 talloc_tos());
487 if (!temp) {
488 return (time_t) 0;
491 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
492 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
493 (char*)temp));
494 TALLOC_FREE(temp);
495 return (time_t) 0;
497 TALLOC_FREE(temp);
498 tzset();
499 return timegm(&tm);
502 /**********************************************************************
503 Initialize struct samu from an LDAP query.
504 (Based on init_sam_from_buffer in pdb_tdb.c)
505 *********************************************************************/
507 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
508 struct samu * sampass,
509 LDAPMessage * entry)
511 time_t logon_time,
512 logoff_time,
513 kickoff_time,
514 pass_last_set_time,
515 pass_can_change_time,
516 pass_must_change_time,
517 ldap_entry_time,
518 bad_password_time;
519 char *username = NULL,
520 *domain = NULL,
521 *nt_username = NULL,
522 *fullname = NULL,
523 *homedir = NULL,
524 *dir_drive = NULL,
525 *logon_script = NULL,
526 *profile_path = NULL,
527 *acct_desc = NULL,
528 *workstations = NULL,
529 *munged_dial = NULL;
530 uint32 user_rid;
531 uint8 smblmpwd[LM_HASH_LEN],
532 smbntpwd[NT_HASH_LEN];
533 bool use_samba_attrs = True;
534 uint32 acct_ctrl = 0;
535 uint16 logon_divs;
536 uint16 bad_password_count = 0,
537 logon_count = 0;
538 uint32 hours_len;
539 uint8 hours[MAX_HOURS_LEN];
540 char *temp = NULL;
541 LOGIN_CACHE *cache_entry = NULL;
542 uint32 pwHistLen;
543 bool expand_explicit = lp_passdb_expand_explicit();
544 bool ret = false;
545 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
547 if (!ctx) {
548 return false;
550 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
551 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
552 goto fn_exit;
555 if (priv2ld(ldap_state) == NULL) {
556 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
557 "ldap_struct is NULL!\n"));
558 goto fn_exit;
561 if (!(username = smbldap_talloc_single_attribute(priv2ld(ldap_state),
562 entry,
563 "uid",
564 ctx))) {
565 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
566 "this user!\n"));
567 goto fn_exit;
570 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
572 nt_username = talloc_strdup(ctx, username);
573 if (!nt_username) {
574 goto fn_exit;
577 domain = talloc_strdup(ctx, ldap_state->domain_name);
578 if (!domain) {
579 goto fn_exit;
582 pdb_set_username(sampass, username, PDB_SET);
584 pdb_set_domain(sampass, domain, PDB_DEFAULT);
585 pdb_set_nt_username(sampass, nt_username, PDB_SET);
587 /* deal with different attributes between the schema first */
589 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
590 if ((temp = smbldap_talloc_single_attribute(
591 ldap_state->smbldap_state->ldap_struct,
592 entry,
593 get_userattr_key2string(ldap_state->schema_ver,
594 LDAP_ATTR_USER_SID),
595 ctx))!=NULL) {
596 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
598 } else {
599 if ((temp = smbldap_talloc_single_attribute(
600 ldap_state->smbldap_state->ldap_struct,
601 entry,
602 get_userattr_key2string(ldap_state->schema_ver,
603 LDAP_ATTR_USER_RID),
604 ctx))!=NULL) {
605 user_rid = (uint32)atol(temp);
606 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
610 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
611 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
612 get_userattr_key2string(ldap_state->schema_ver,
613 LDAP_ATTR_USER_SID),
614 get_userattr_key2string(ldap_state->schema_ver,
615 LDAP_ATTR_USER_RID),
616 username));
617 return False;
620 temp = smbldap_talloc_single_attribute(
621 ldap_state->smbldap_state->ldap_struct,
622 entry,
623 get_userattr_key2string(ldap_state->schema_ver,
624 LDAP_ATTR_PWD_LAST_SET),
625 ctx);
626 if (temp) {
627 pass_last_set_time = (time_t) atol(temp);
628 pdb_set_pass_last_set_time(sampass,
629 pass_last_set_time, PDB_SET);
632 temp = smbldap_talloc_single_attribute(
633 ldap_state->smbldap_state->ldap_struct,
634 entry,
635 get_userattr_key2string(ldap_state->schema_ver,
636 LDAP_ATTR_LOGON_TIME),
637 ctx);
638 if (temp) {
639 logon_time = (time_t) atol(temp);
640 pdb_set_logon_time(sampass, logon_time, PDB_SET);
643 temp = smbldap_talloc_single_attribute(
644 ldap_state->smbldap_state->ldap_struct,
645 entry,
646 get_userattr_key2string(ldap_state->schema_ver,
647 LDAP_ATTR_LOGOFF_TIME),
648 ctx);
649 if (temp) {
650 logoff_time = (time_t) atol(temp);
651 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
654 temp = smbldap_talloc_single_attribute(
655 ldap_state->smbldap_state->ldap_struct,
656 entry,
657 get_userattr_key2string(ldap_state->schema_ver,
658 LDAP_ATTR_KICKOFF_TIME),
659 ctx);
660 if (temp) {
661 kickoff_time = (time_t) atol(temp);
662 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
665 temp = smbldap_talloc_single_attribute(
666 ldap_state->smbldap_state->ldap_struct,
667 entry,
668 get_userattr_key2string(ldap_state->schema_ver,
669 LDAP_ATTR_PWD_CAN_CHANGE),
670 ctx);
671 if (temp) {
672 pass_can_change_time = (time_t) atol(temp);
673 pdb_set_pass_can_change_time(sampass,
674 pass_can_change_time, PDB_SET);
677 temp = smbldap_talloc_single_attribute(
678 ldap_state->smbldap_state->ldap_struct,
679 entry,
680 get_userattr_key2string(ldap_state->schema_ver,
681 LDAP_ATTR_PWD_MUST_CHANGE),
682 ctx);
683 if (temp) {
684 pass_must_change_time = (time_t) atol(temp);
685 pdb_set_pass_must_change_time(sampass,
686 pass_must_change_time, PDB_SET);
689 /* recommend that 'gecos' and 'displayName' should refer to the same
690 * attribute OID. userFullName depreciated, only used by Samba
691 * primary rules of LDAP: don't make a new attribute when one is already defined
692 * that fits your needs; using cn then displayName rather than 'userFullName'
695 fullname = smbldap_talloc_single_attribute(
696 ldap_state->smbldap_state->ldap_struct,
697 entry,
698 get_userattr_key2string(ldap_state->schema_ver,
699 LDAP_ATTR_DISPLAY_NAME),
700 ctx);
701 if (fullname) {
702 pdb_set_fullname(sampass, fullname, PDB_SET);
703 } else {
704 fullname = smbldap_talloc_single_attribute(
705 ldap_state->smbldap_state->ldap_struct,
706 entry,
707 get_userattr_key2string(ldap_state->schema_ver,
708 LDAP_ATTR_CN),
709 ctx);
710 if (fullname) {
711 pdb_set_fullname(sampass, fullname, PDB_SET);
715 dir_drive = smbldap_talloc_single_attribute(
716 ldap_state->smbldap_state->ldap_struct,
717 entry,
718 get_userattr_key2string(ldap_state->schema_ver,
719 LDAP_ATTR_HOME_DRIVE),
720 ctx);
721 if (dir_drive) {
722 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
723 } else {
724 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
727 homedir = smbldap_talloc_single_attribute(
728 ldap_state->smbldap_state->ldap_struct,
729 entry,
730 get_userattr_key2string(ldap_state->schema_ver,
731 LDAP_ATTR_HOME_PATH),
732 ctx);
733 if (homedir) {
734 if (expand_explicit) {
735 homedir = talloc_sub_basic(ctx,
736 username,
737 domain,
738 homedir);
739 if (!homedir) {
740 goto fn_exit;
743 pdb_set_homedir(sampass, homedir, PDB_SET);
744 } else {
745 pdb_set_homedir(sampass,
746 talloc_sub_basic(ctx, username, domain,
747 lp_logon_home()),
748 PDB_DEFAULT);
751 logon_script = smbldap_talloc_single_attribute(
752 ldap_state->smbldap_state->ldap_struct,
753 entry,
754 get_userattr_key2string(ldap_state->schema_ver,
755 LDAP_ATTR_LOGON_SCRIPT),
756 ctx);
757 if (logon_script) {
758 if (expand_explicit) {
759 logon_script = talloc_sub_basic(ctx,
760 username,
761 domain,
762 logon_script);
763 if (!logon_script) {
764 goto fn_exit;
767 pdb_set_logon_script(sampass, logon_script, PDB_SET);
768 } else {
769 pdb_set_logon_script(sampass,
770 talloc_sub_basic(ctx, username, domain,
771 lp_logon_script()),
772 PDB_DEFAULT );
775 profile_path = smbldap_talloc_single_attribute(
776 ldap_state->smbldap_state->ldap_struct,
777 entry,
778 get_userattr_key2string(ldap_state->schema_ver,
779 LDAP_ATTR_PROFILE_PATH),
780 ctx);
781 if (profile_path) {
782 if (expand_explicit) {
783 profile_path = talloc_sub_basic(ctx,
784 username,
785 domain,
786 profile_path);
787 if (!profile_path) {
788 goto fn_exit;
791 pdb_set_profile_path(sampass, profile_path, PDB_SET);
792 } else {
793 pdb_set_profile_path(sampass,
794 talloc_sub_basic(ctx, username, domain,
795 lp_logon_path()),
796 PDB_DEFAULT );
799 acct_desc = smbldap_talloc_single_attribute(
800 ldap_state->smbldap_state->ldap_struct,
801 entry,
802 get_userattr_key2string(ldap_state->schema_ver,
803 LDAP_ATTR_DESC),
804 ctx);
805 if (acct_desc) {
806 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
809 workstations = smbldap_talloc_single_attribute(
810 ldap_state->smbldap_state->ldap_struct,
811 entry,
812 get_userattr_key2string(ldap_state->schema_ver,
813 LDAP_ATTR_USER_WKS),
814 ctx);
815 if (workstations) {
816 pdb_set_workstations(sampass, workstations, PDB_SET);
819 munged_dial = smbldap_talloc_single_attribute(
820 ldap_state->smbldap_state->ldap_struct,
821 entry,
822 get_userattr_key2string(ldap_state->schema_ver,
823 LDAP_ATTR_MUNGED_DIAL),
824 ctx);
825 if (munged_dial) {
826 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
829 /* FIXME: hours stuff should be cleaner */
831 logon_divs = 168;
832 hours_len = 21;
833 memset(hours, 0xff, hours_len);
835 if (ldap_state->is_nds_ldap) {
836 char *user_dn;
837 size_t pwd_len;
838 char clear_text_pw[512];
840 /* Make call to Novell eDirectory ldap extension to get clear text password.
841 NOTE: This will only work if we have an SSL connection to eDirectory. */
842 user_dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
843 if (user_dn != NULL) {
844 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username, user_dn));
846 pwd_len = sizeof(clear_text_pw);
847 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
848 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
849 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
850 SAFE_FREE(user_dn);
851 return False;
853 ZERO_STRUCT(smblmpwd);
854 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
855 SAFE_FREE(user_dn);
856 return False;
858 ZERO_STRUCT(smbntpwd);
859 use_samba_attrs = False;
862 SAFE_FREE(user_dn);
864 } else {
865 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
869 if (use_samba_attrs) {
870 temp = smbldap_talloc_single_attribute(
871 ldap_state->smbldap_state->ldap_struct,
872 entry,
873 get_userattr_key2string(ldap_state->schema_ver,
874 LDAP_ATTR_LMPW),
875 ctx);
876 if (temp) {
877 pdb_gethexpwd(temp, smblmpwd);
878 memset((char *)temp, '\0', strlen(temp)+1);
879 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
880 goto fn_exit;
882 ZERO_STRUCT(smblmpwd);
885 temp = smbldap_talloc_single_attribute(
886 ldap_state->smbldap_state->ldap_struct,
887 entry,
888 get_userattr_key2string(ldap_state->schema_ver,
889 LDAP_ATTR_NTPW),
890 ctx);
891 if (temp) {
892 pdb_gethexpwd(temp, smbntpwd);
893 memset((char *)temp, '\0', strlen(temp)+1);
894 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
895 goto fn_exit;
897 ZERO_STRUCT(smbntpwd);
901 pwHistLen = 0;
903 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
904 if (pwHistLen > 0){
905 uint8 *pwhist = NULL;
906 int i;
907 char *history_string = TALLOC_ARRAY(ctx, char,
908 MAX_PW_HISTORY_LEN*64);
910 if (!history_string) {
911 goto fn_exit;
914 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
916 if ((pwhist = TALLOC_ARRAY(ctx, uint8,
917 pwHistLen * PW_HISTORY_ENTRY_LEN)) ==
918 NULL){
919 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
920 goto fn_exit;
922 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
924 if (smbldap_get_single_attribute(
925 ldap_state->smbldap_state->ldap_struct,
926 entry,
927 get_userattr_key2string(ldap_state->schema_ver,
928 LDAP_ATTR_PWD_HISTORY),
929 history_string,
930 MAX_PW_HISTORY_LEN*64)) {
931 bool hex_failed = false;
932 for (i = 0; i < pwHistLen; i++){
933 /* Get the 16 byte salt. */
934 if (!pdb_gethexpwd(&history_string[i*64],
935 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
936 hex_failed = true;
937 break;
939 /* Get the 16 byte MD5 hash of salt+passwd. */
940 if (!pdb_gethexpwd(&history_string[(i*64)+32],
941 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
942 PW_HISTORY_SALT_LEN])) {
943 hex_failed = True;
944 break;
947 if (hex_failed) {
948 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
949 username));
950 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
953 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
954 goto fn_exit;
958 temp = smbldap_talloc_single_attribute(
959 ldap_state->smbldap_state->ldap_struct,
960 entry,
961 get_userattr_key2string(ldap_state->schema_ver,
962 LDAP_ATTR_ACB_INFO),
963 ctx);
964 if (temp) {
965 acct_ctrl = pdb_decode_acct_ctrl(temp);
967 if (acct_ctrl == 0) {
968 acct_ctrl |= ACB_NORMAL;
971 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
972 } else {
973 acct_ctrl |= ACB_NORMAL;
976 pdb_set_hours_len(sampass, hours_len, PDB_SET);
977 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
979 temp = smbldap_talloc_single_attribute(
980 ldap_state->smbldap_state->ldap_struct,
981 entry,
982 get_userattr_key2string(ldap_state->schema_ver,
983 LDAP_ATTR_BAD_PASSWORD_COUNT),
984 ctx);
985 if (temp) {
986 bad_password_count = (uint32) atol(temp);
987 pdb_set_bad_password_count(sampass,
988 bad_password_count, PDB_SET);
991 temp = smbldap_talloc_single_attribute(
992 ldap_state->smbldap_state->ldap_struct,
993 entry,
994 get_userattr_key2string(ldap_state->schema_ver,
995 LDAP_ATTR_BAD_PASSWORD_TIME),
996 ctx);
997 if (temp) {
998 bad_password_time = (time_t) atol(temp);
999 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
1003 temp = smbldap_talloc_single_attribute(
1004 ldap_state->smbldap_state->ldap_struct,
1005 entry,
1006 get_userattr_key2string(ldap_state->schema_ver,
1007 LDAP_ATTR_LOGON_COUNT),
1008 ctx);
1009 if (temp) {
1010 logon_count = (uint32) atol(temp);
1011 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1014 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1016 temp = smbldap_talloc_single_attribute(
1017 ldap_state->smbldap_state->ldap_struct,
1018 entry,
1019 get_userattr_key2string(ldap_state->schema_ver,
1020 LDAP_ATTR_LOGON_HOURS),
1021 ctx);
1022 if (temp) {
1023 pdb_gethexhours(temp, hours);
1024 memset((char *)temp, '\0', strlen(temp) +1);
1025 pdb_set_hours(sampass, hours, PDB_SET);
1026 ZERO_STRUCT(hours);
1029 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1030 temp = smbldap_talloc_single_attribute(
1031 priv2ld(ldap_state),
1032 entry,
1033 "uidNumber",
1034 ctx);
1035 if (temp) {
1036 /* We've got a uid, feed the cache */
1037 uid_t uid = strtoul(temp, NULL, 10);
1038 store_uid_sid_cache(pdb_get_user_sid(sampass), uid);
1042 /* check the timestamp of the cache vs ldap entry */
1043 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1044 entry))) {
1045 ret = true;
1046 goto fn_exit;
1049 /* see if we have newer updates */
1050 if (!(cache_entry = login_cache_read(sampass))) {
1051 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1052 (unsigned int)pdb_get_bad_password_count(sampass),
1053 (unsigned int)pdb_get_bad_password_time(sampass)));
1054 ret = true;
1055 goto fn_exit;
1058 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1059 (unsigned int)ldap_entry_time,
1060 (unsigned int)cache_entry->entry_timestamp,
1061 (unsigned int)cache_entry->bad_password_time));
1063 if (ldap_entry_time > cache_entry->entry_timestamp) {
1064 /* cache is older than directory , so
1065 we need to delete the entry but allow the
1066 fields to be written out */
1067 login_cache_delentry(sampass);
1068 } else {
1069 /* read cache in */
1070 pdb_set_acct_ctrl(sampass,
1071 pdb_get_acct_ctrl(sampass) |
1072 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
1073 PDB_SET);
1074 pdb_set_bad_password_count(sampass,
1075 cache_entry->bad_password_count,
1076 PDB_SET);
1077 pdb_set_bad_password_time(sampass,
1078 cache_entry->bad_password_time,
1079 PDB_SET);
1082 ret = true;
1084 fn_exit:
1086 TALLOC_FREE(ctx);
1087 SAFE_FREE(cache_entry);
1088 return ret;
1091 /**********************************************************************
1092 Initialize the ldap db from a struct samu. Called on update.
1093 (Based on init_buffer_from_sam in pdb_tdb.c)
1094 *********************************************************************/
1096 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1097 LDAPMessage *existing,
1098 LDAPMod *** mods, struct samu * sampass,
1099 bool (*need_update)(const struct samu *,
1100 enum pdb_elements))
1102 char *temp = NULL;
1103 uint32 rid;
1105 if (mods == NULL || sampass == NULL) {
1106 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1107 return False;
1110 *mods = NULL;
1113 * took out adding "objectclass: sambaAccount"
1114 * do this on a per-mod basis
1116 if (need_update(sampass, PDB_USERNAME)) {
1117 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1118 "uid", pdb_get_username(sampass));
1119 if (ldap_state->is_nds_ldap) {
1120 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1121 "cn", pdb_get_username(sampass));
1122 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1123 "sn", pdb_get_username(sampass));
1127 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1129 /* only update the RID if we actually need to */
1130 if (need_update(sampass, PDB_USERSID)) {
1131 fstring sid_string;
1132 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
1134 switch ( ldap_state->schema_ver ) {
1135 case SCHEMAVER_SAMBAACCOUNT:
1136 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1137 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1138 sid_string_dbg(user_sid),
1139 sid_string_dbg(
1140 &ldap_state->domain_sid)));
1141 return False;
1143 if (asprintf(&temp, "%i", rid) < 0) {
1144 return false;
1146 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1147 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1148 temp);
1149 SAFE_FREE(temp);
1150 break;
1152 case SCHEMAVER_SAMBASAMACCOUNT:
1153 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1154 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1155 sid_to_fstring(sid_string, user_sid));
1156 break;
1158 default:
1159 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1160 break;
1164 /* we don't need to store the primary group RID - so leaving it
1165 'free' to hang off the unix primary group makes life easier */
1167 if (need_update(sampass, PDB_GROUPSID)) {
1168 fstring sid_string;
1169 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1171 switch ( ldap_state->schema_ver ) {
1172 case SCHEMAVER_SAMBAACCOUNT:
1173 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1174 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1175 sid_string_dbg(group_sid),
1176 sid_string_dbg(
1177 &ldap_state->domain_sid)));
1178 return False;
1181 if (asprintf(&temp, "%i", rid) < 0) {
1182 return false;
1184 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1185 get_userattr_key2string(ldap_state->schema_ver,
1186 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1187 SAFE_FREE(temp);
1188 break;
1190 case SCHEMAVER_SAMBASAMACCOUNT:
1191 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1192 get_userattr_key2string(ldap_state->schema_ver,
1193 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1194 break;
1196 default:
1197 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1198 break;
1203 /* displayName, cn, and gecos should all be the same
1204 * most easily accomplished by giving them the same OID
1205 * gecos isn't set here b/c it should be handled by the
1206 * add-user script
1207 * We change displayName only and fall back to cn if
1208 * it does not exist.
1211 if (need_update(sampass, PDB_FULLNAME))
1212 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1213 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1214 pdb_get_fullname(sampass));
1216 if (need_update(sampass, PDB_ACCTDESC))
1217 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1218 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1219 pdb_get_acct_desc(sampass));
1221 if (need_update(sampass, PDB_WORKSTATIONS))
1222 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1223 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1224 pdb_get_workstations(sampass));
1226 if (need_update(sampass, PDB_MUNGEDDIAL))
1227 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1228 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1229 pdb_get_munged_dial(sampass));
1231 if (need_update(sampass, PDB_SMBHOME))
1232 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1233 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1234 pdb_get_homedir(sampass));
1236 if (need_update(sampass, PDB_DRIVE))
1237 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1238 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1239 pdb_get_dir_drive(sampass));
1241 if (need_update(sampass, PDB_LOGONSCRIPT))
1242 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1243 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1244 pdb_get_logon_script(sampass));
1246 if (need_update(sampass, PDB_PROFILE))
1247 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1248 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1249 pdb_get_profile_path(sampass));
1251 if (asprintf(&temp, "%li", pdb_get_logon_time(sampass)) < 0) {
1252 return false;
1254 if (need_update(sampass, PDB_LOGONTIME))
1255 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1256 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1257 SAFE_FREE(temp);
1259 if (asprintf(&temp, "%li", pdb_get_logoff_time(sampass)) < 0) {
1260 return false;
1262 if (need_update(sampass, PDB_LOGOFFTIME))
1263 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1264 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1265 SAFE_FREE(temp);
1267 if (asprintf(&temp, "%li", pdb_get_kickoff_time(sampass)) < 0) {
1268 return false;
1270 if (need_update(sampass, PDB_KICKOFFTIME))
1271 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1272 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1273 SAFE_FREE(temp);
1275 if (asprintf(&temp, "%li", pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1276 return false;
1278 if (need_update(sampass, PDB_CANCHANGETIME))
1279 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1280 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1281 SAFE_FREE(temp);
1283 if (asprintf(&temp, "%li", pdb_get_pass_must_change_time(sampass)) < 0) {
1284 return false;
1286 if (need_update(sampass, PDB_MUSTCHANGETIME))
1287 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1288 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1289 SAFE_FREE(temp);
1291 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1292 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1294 if (need_update(sampass, PDB_LMPASSWD)) {
1295 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1296 if (lm_pw) {
1297 char pwstr[34];
1298 pdb_sethexpwd(pwstr, lm_pw,
1299 pdb_get_acct_ctrl(sampass));
1300 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1301 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1302 pwstr);
1303 } else {
1304 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1305 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1306 NULL);
1309 if (need_update(sampass, PDB_NTPASSWD)) {
1310 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1311 if (nt_pw) {
1312 char pwstr[34];
1313 pdb_sethexpwd(pwstr, nt_pw,
1314 pdb_get_acct_ctrl(sampass));
1315 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1316 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1317 pwstr);
1318 } else {
1319 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1320 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1321 NULL);
1325 if (need_update(sampass, PDB_PWHISTORY)) {
1326 char *pwstr = NULL;
1327 uint32 pwHistLen = 0;
1328 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1330 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1331 if (!pwstr) {
1332 return false;
1334 if (pwHistLen == 0) {
1335 /* Remove any password history from the LDAP store. */
1336 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1337 pwstr[64] = '\0';
1338 } else {
1339 int i;
1340 uint32 currHistLen = 0;
1341 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1342 if (pwhist != NULL) {
1343 /* We can only store (1024-1/64 password history entries. */
1344 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1345 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1346 /* Store the salt. */
1347 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1348 /* Followed by the md5 hash of salt + md4 hash */
1349 pdb_sethexpwd(&pwstr[(i*64)+32],
1350 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1351 DEBUG(100, ("pwstr=%s\n", pwstr));
1355 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1356 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1357 pwstr);
1358 SAFE_FREE(pwstr);
1361 if (need_update(sampass, PDB_PASSLASTSET)) {
1362 if (asprintf(&temp, "%li",
1363 pdb_get_pass_last_set_time(sampass)) < 0) {
1364 return false;
1366 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1367 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1368 temp);
1369 SAFE_FREE(temp);
1373 if (need_update(sampass, PDB_HOURS)) {
1374 const uint8 *hours = pdb_get_hours(sampass);
1375 if (hours) {
1376 char hourstr[44];
1377 pdb_sethexhours(hourstr, hours);
1378 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1379 existing,
1380 mods,
1381 get_userattr_key2string(ldap_state->schema_ver,
1382 LDAP_ATTR_LOGON_HOURS),
1383 hourstr);
1387 if (need_update(sampass, PDB_ACCTCTRL))
1388 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1389 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1390 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1392 /* password lockout cache:
1393 - If we are now autolocking or clearing, we write to ldap
1394 - If we are clearing, we delete the cache entry
1395 - If the count is > 0, we update the cache
1397 This even means when autolocking, we cache, just in case the
1398 update doesn't work, and we have to cache the autolock flag */
1400 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1401 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1402 uint16 badcount = pdb_get_bad_password_count(sampass);
1403 time_t badtime = pdb_get_bad_password_time(sampass);
1404 uint32 pol;
1405 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1407 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1408 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1410 if ((badcount >= pol) || (badcount == 0)) {
1411 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1412 (unsigned int)badcount, (unsigned int)badtime));
1413 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1414 return false;
1416 smbldap_make_mod(
1417 ldap_state->smbldap_state->ldap_struct,
1418 existing, mods,
1419 get_userattr_key2string(
1420 ldap_state->schema_ver,
1421 LDAP_ATTR_BAD_PASSWORD_COUNT),
1422 temp);
1423 SAFE_FREE(temp);
1425 if (asprintf(&temp, "%li", badtime) < 0) {
1426 return false;
1428 smbldap_make_mod(
1429 ldap_state->smbldap_state->ldap_struct,
1430 existing, mods,
1431 get_userattr_key2string(
1432 ldap_state->schema_ver,
1433 LDAP_ATTR_BAD_PASSWORD_TIME),
1434 temp);
1435 SAFE_FREE(temp);
1437 if (badcount == 0) {
1438 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1439 login_cache_delentry(sampass);
1440 } else {
1441 LOGIN_CACHE cache_entry;
1443 cache_entry.entry_timestamp = time(NULL);
1444 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1445 cache_entry.bad_password_count = badcount;
1446 cache_entry.bad_password_time = badtime;
1448 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1449 login_cache_write(sampass, cache_entry);
1453 return True;
1456 /**********************************************************************
1457 End enumeration of the LDAP password list.
1458 *********************************************************************/
1460 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1462 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1463 if (ldap_state->result) {
1464 ldap_msgfree(ldap_state->result);
1465 ldap_state->result = NULL;
1469 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1470 const char *new_attr)
1472 int i;
1474 if (new_attr == NULL) {
1475 return;
1478 for (i=0; (*attr_list)[i] != NULL; i++) {
1482 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1483 const char *, i+2);
1484 SMB_ASSERT((*attr_list) != NULL);
1485 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1486 (*attr_list)[i+1] = NULL;
1489 /**********************************************************************
1490 Get struct samu entry from LDAP by username.
1491 *********************************************************************/
1493 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1495 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1496 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1497 LDAPMessage *result = NULL;
1498 LDAPMessage *entry = NULL;
1499 int count;
1500 const char ** attr_list;
1501 int rc;
1503 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1504 append_attr(user, &attr_list,
1505 get_userattr_key2string(ldap_state->schema_ver,
1506 LDAP_ATTR_MOD_TIMESTAMP));
1507 append_attr(user, &attr_list, "uidNumber");
1508 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1509 attr_list);
1510 TALLOC_FREE( attr_list );
1512 if ( rc != LDAP_SUCCESS )
1513 return NT_STATUS_NO_SUCH_USER;
1515 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1517 if (count < 1) {
1518 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1519 ldap_msgfree(result);
1520 return NT_STATUS_NO_SUCH_USER;
1521 } else if (count > 1) {
1522 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1523 ldap_msgfree(result);
1524 return NT_STATUS_NO_SUCH_USER;
1527 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1528 if (entry) {
1529 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1530 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1531 ldap_msgfree(result);
1532 return NT_STATUS_NO_SUCH_USER;
1534 pdb_set_backend_private_data(user, result, NULL,
1535 my_methods, PDB_CHANGED);
1536 talloc_autofree_ldapmsg(user, result);
1537 ret = NT_STATUS_OK;
1538 } else {
1539 ldap_msgfree(result);
1541 return ret;
1544 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1545 const DOM_SID *sid, LDAPMessage **result)
1547 int rc = -1;
1548 const char ** attr_list;
1549 uint32 rid;
1551 switch ( ldap_state->schema_ver ) {
1552 case SCHEMAVER_SAMBASAMACCOUNT: {
1553 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1554 if (tmp_ctx == NULL) {
1555 return LDAP_NO_MEMORY;
1558 attr_list = get_userattr_list(tmp_ctx,
1559 ldap_state->schema_ver);
1560 append_attr(tmp_ctx, &attr_list,
1561 get_userattr_key2string(
1562 ldap_state->schema_ver,
1563 LDAP_ATTR_MOD_TIMESTAMP));
1564 append_attr(tmp_ctx, &attr_list, "uidNumber");
1565 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1566 result, attr_list);
1567 TALLOC_FREE(tmp_ctx);
1569 if ( rc != LDAP_SUCCESS )
1570 return rc;
1571 break;
1574 case SCHEMAVER_SAMBAACCOUNT:
1575 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1576 return rc;
1579 attr_list = get_userattr_list(NULL,
1580 ldap_state->schema_ver);
1581 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1582 TALLOC_FREE( attr_list );
1584 if ( rc != LDAP_SUCCESS )
1585 return rc;
1586 break;
1588 return rc;
1591 /**********************************************************************
1592 Get struct samu entry from LDAP by SID.
1593 *********************************************************************/
1595 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1597 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1598 LDAPMessage *result = NULL;
1599 LDAPMessage *entry = NULL;
1600 int count;
1601 int rc;
1603 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1604 sid, &result);
1605 if (rc != LDAP_SUCCESS)
1606 return NT_STATUS_NO_SUCH_USER;
1608 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1610 if (count < 1) {
1611 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1612 "count=%d\n", sid_string_dbg(sid), count));
1613 ldap_msgfree(result);
1614 return NT_STATUS_NO_SUCH_USER;
1615 } else if (count > 1) {
1616 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1617 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1618 count));
1619 ldap_msgfree(result);
1620 return NT_STATUS_NO_SUCH_USER;
1623 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1624 if (!entry) {
1625 ldap_msgfree(result);
1626 return NT_STATUS_NO_SUCH_USER;
1629 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1630 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1631 ldap_msgfree(result);
1632 return NT_STATUS_NO_SUCH_USER;
1635 pdb_set_backend_private_data(user, result, NULL,
1636 my_methods, PDB_CHANGED);
1637 talloc_autofree_ldapmsg(user, result);
1638 return NT_STATUS_OK;
1641 /********************************************************************
1642 Do the actual modification - also change a plaintext passord if
1643 it it set.
1644 **********************************************************************/
1646 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1647 struct samu *newpwd, char *dn,
1648 LDAPMod **mods, int ldap_op,
1649 bool (*need_update)(const struct samu *, enum pdb_elements))
1651 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1652 int rc;
1654 if (!newpwd || !dn) {
1655 return NT_STATUS_INVALID_PARAMETER;
1658 if (!mods) {
1659 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1660 /* may be password change below however */
1661 } else {
1662 switch(ldap_op) {
1663 case LDAP_MOD_ADD:
1664 if (ldap_state->is_nds_ldap) {
1665 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1666 "objectclass",
1667 "inetOrgPerson");
1668 } else {
1669 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1670 "objectclass",
1671 LDAP_OBJ_ACCOUNT);
1673 rc = smbldap_add(ldap_state->smbldap_state,
1674 dn, mods);
1675 break;
1676 case LDAP_MOD_REPLACE:
1677 rc = smbldap_modify(ldap_state->smbldap_state,
1678 dn ,mods);
1679 break;
1680 default:
1681 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1682 ldap_op));
1683 return NT_STATUS_INVALID_PARAMETER;
1686 if (rc!=LDAP_SUCCESS) {
1687 return NT_STATUS_UNSUCCESSFUL;
1691 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1692 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1693 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1694 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1695 BerElement *ber;
1696 struct berval *bv;
1697 char *retoid = NULL;
1698 struct berval *retdata = NULL;
1699 char *utf8_password;
1700 char *utf8_dn;
1702 if (!ldap_state->is_nds_ldap) {
1704 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1705 LDAP_EXOP_MODIFY_PASSWD)) {
1706 DEBUG(2, ("ldap password change requested, but LDAP "
1707 "server does not support it -- ignoring\n"));
1708 return NT_STATUS_OK;
1712 if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
1713 return NT_STATUS_NO_MEMORY;
1716 if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
1717 SAFE_FREE(utf8_password);
1718 return NT_STATUS_NO_MEMORY;
1721 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1722 DEBUG(0,("ber_alloc_t returns NULL\n"));
1723 SAFE_FREE(utf8_password);
1724 SAFE_FREE(utf8_dn);
1725 return NT_STATUS_UNSUCCESSFUL;
1728 ber_printf (ber, "{");
1729 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
1730 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
1731 ber_printf (ber, "n}");
1733 if ((rc = ber_flatten (ber, &bv))<0) {
1734 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1735 ber_free(ber,1);
1736 SAFE_FREE(utf8_dn);
1737 SAFE_FREE(utf8_password);
1738 return NT_STATUS_UNSUCCESSFUL;
1741 SAFE_FREE(utf8_dn);
1742 SAFE_FREE(utf8_password);
1743 ber_free(ber, 1);
1745 if (!ldap_state->is_nds_ldap) {
1746 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1747 LDAP_EXOP_MODIFY_PASSWD,
1748 bv, NULL, NULL, &retoid,
1749 &retdata);
1750 } else {
1751 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1752 pdb_get_plaintext_passwd(newpwd));
1754 if (rc != LDAP_SUCCESS) {
1755 char *ld_error = NULL;
1757 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1758 DEBUG(3, ("Could not set userPassword "
1759 "attribute due to an objectClass "
1760 "violation -- ignoring\n"));
1761 ber_bvfree(bv);
1762 return NT_STATUS_OK;
1765 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1766 &ld_error);
1767 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1768 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1769 SAFE_FREE(ld_error);
1770 ber_bvfree(bv);
1771 #if defined(LDAP_CONSTRAINT_VIOLATION)
1772 if (rc == LDAP_CONSTRAINT_VIOLATION)
1773 return NT_STATUS_PASSWORD_RESTRICTION;
1774 #endif
1775 return NT_STATUS_UNSUCCESSFUL;
1776 } else {
1777 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1778 #ifdef DEBUG_PASSWORD
1779 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1780 #endif
1781 if (retdata)
1782 ber_bvfree(retdata);
1783 if (retoid)
1784 ldap_memfree(retoid);
1786 ber_bvfree(bv);
1788 return NT_STATUS_OK;
1791 /**********************************************************************
1792 Delete entry from LDAP for username.
1793 *********************************************************************/
1795 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1796 struct samu * sam_acct)
1798 struct ldapsam_privates *priv =
1799 (struct ldapsam_privates *)my_methods->private_data;
1800 const char *sname;
1801 int rc;
1802 LDAPMessage *msg, *entry;
1803 NTSTATUS result = NT_STATUS_NO_MEMORY;
1804 const char **attr_list;
1805 TALLOC_CTX *mem_ctx;
1807 if (!sam_acct) {
1808 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1809 return NT_STATUS_INVALID_PARAMETER;
1812 sname = pdb_get_username(sam_acct);
1814 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1815 "LDAP.\n", sname));
1817 mem_ctx = talloc_new(NULL);
1818 if (mem_ctx == NULL) {
1819 DEBUG(0, ("talloc_new failed\n"));
1820 goto done;
1823 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1824 if (attr_list == NULL) {
1825 goto done;
1828 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1830 if ((rc != LDAP_SUCCESS) ||
1831 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1832 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1833 DEBUG(5, ("Could not find user %s\n", sname));
1834 result = NT_STATUS_NO_SUCH_USER;
1835 goto done;
1838 rc = ldapsam_delete_entry(
1839 priv, mem_ctx, entry,
1840 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1841 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1842 attr_list);
1844 result = (rc == LDAP_SUCCESS) ?
1845 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1847 done:
1848 TALLOC_FREE(mem_ctx);
1849 return result;
1852 /**********************************************************************
1853 Helper function to determine for update_sam_account whether
1854 we need LDAP modification.
1855 *********************************************************************/
1857 static bool element_is_changed(const struct samu *sampass,
1858 enum pdb_elements element)
1860 return IS_SAM_CHANGED(sampass, element);
1863 /**********************************************************************
1864 Update struct samu.
1865 *********************************************************************/
1867 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1869 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1870 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1871 int rc = 0;
1872 char *dn;
1873 LDAPMessage *result = NULL;
1874 LDAPMessage *entry = NULL;
1875 LDAPMod **mods = NULL;
1876 const char **attr_list;
1878 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1879 if (!result) {
1880 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1881 if (pdb_get_username(newpwd) == NULL) {
1882 return NT_STATUS_INVALID_PARAMETER;
1884 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1885 TALLOC_FREE( attr_list );
1886 if (rc != LDAP_SUCCESS) {
1887 return NT_STATUS_UNSUCCESSFUL;
1889 pdb_set_backend_private_data(newpwd, result, NULL,
1890 my_methods, PDB_CHANGED);
1891 talloc_autofree_ldapmsg(newpwd, result);
1894 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1895 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1896 return NT_STATUS_UNSUCCESSFUL;
1899 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1900 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
1901 if (!dn) {
1902 return NT_STATUS_UNSUCCESSFUL;
1905 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1907 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1908 element_is_changed)) {
1909 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1910 SAFE_FREE(dn);
1911 if (mods != NULL)
1912 ldap_mods_free(mods,True);
1913 return NT_STATUS_UNSUCCESSFUL;
1916 if (mods == NULL) {
1917 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1918 pdb_get_username(newpwd)));
1919 SAFE_FREE(dn);
1920 return NT_STATUS_OK;
1923 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1924 ldap_mods_free(mods,True);
1925 SAFE_FREE(dn);
1928 * We need to set the backend private data to NULL here. For example
1929 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1930 * same one, and with the explicit delete / add logic for attribute
1931 * values the second time we would use the wrong "old" value which
1932 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1933 * the update.
1934 * The existing LDAPMessage is still being auto-freed by the
1935 * destructor.
1937 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
1938 PDB_CHANGED);
1940 if (!NT_STATUS_IS_OK(ret)) {
1941 return ret;
1944 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1945 pdb_get_username(newpwd)));
1946 return NT_STATUS_OK;
1949 /***************************************************************************
1950 Renames a struct samu
1951 - The "rename user script" has full responsibility for changing everything
1952 ***************************************************************************/
1954 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
1955 struct samu *old_acct,
1956 const char *newname)
1958 const char *oldname;
1959 int rc;
1960 char *rename_script = NULL;
1961 fstring oldname_lower, newname_lower;
1963 if (!old_acct) {
1964 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1965 return NT_STATUS_INVALID_PARAMETER;
1967 if (!newname) {
1968 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1969 return NT_STATUS_INVALID_PARAMETER;
1972 oldname = pdb_get_username(old_acct);
1974 /* rename the posix user */
1975 rename_script = SMB_STRDUP(lp_renameuser_script());
1976 if (rename_script) {
1977 return NT_STATUS_NO_MEMORY;
1980 if (!(*rename_script)) {
1981 SAFE_FREE(rename_script);
1982 return NT_STATUS_ACCESS_DENIED;
1985 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1986 oldname, newname));
1988 /* We have to allow the account name to end with a '$'.
1989 Also, follow the semantics in _samr_create_user() and lower case the
1990 posix name but preserve the case in passdb */
1992 fstrcpy( oldname_lower, oldname );
1993 strlower_m( oldname_lower );
1994 fstrcpy( newname_lower, newname );
1995 strlower_m( newname_lower );
1996 rename_script = realloc_string_sub2(rename_script,
1997 "%unew",
1998 newname_lower,
1999 true,
2000 true);
2001 if (rename_script) {
2002 return NT_STATUS_NO_MEMORY;
2004 rename_script = realloc_string_sub2(rename_script,
2005 "%uold",
2006 oldname_lower,
2007 true,
2008 true);
2009 rc = smbrun(rename_script, NULL);
2011 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2012 rename_script, rc));
2014 SAFE_FREE(rename_script);
2016 if (rc == 0) {
2017 smb_nscd_flush_user_cache();
2020 if (rc)
2021 return NT_STATUS_UNSUCCESSFUL;
2023 return NT_STATUS_OK;
2026 /**********************************************************************
2027 Helper function to determine for update_sam_account whether
2028 we need LDAP modification.
2029 *********************************************************************/
2031 static bool element_is_set_or_changed(const struct samu *sampass,
2032 enum pdb_elements element)
2034 return (IS_SAM_SET(sampass, element) ||
2035 IS_SAM_CHANGED(sampass, element));
2038 /**********************************************************************
2039 Add struct samu to LDAP.
2040 *********************************************************************/
2042 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2044 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2045 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2046 int rc;
2047 LDAPMessage *result = NULL;
2048 LDAPMessage *entry = NULL;
2049 LDAPMod **mods = NULL;
2050 int ldap_op = LDAP_MOD_REPLACE;
2051 uint32 num_result;
2052 const char **attr_list;
2053 char *escape_user = NULL;
2054 const char *username = pdb_get_username(newpwd);
2055 const DOM_SID *sid = pdb_get_user_sid(newpwd);
2056 char *filter = NULL;
2057 char *dn = NULL;
2058 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2059 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2061 if (!ctx) {
2062 return NT_STATUS_NO_MEMORY;
2065 if (!username || !*username) {
2066 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2067 status = NT_STATUS_INVALID_PARAMETER;
2068 goto fn_exit;
2071 /* free this list after the second search or in case we exit on failure */
2072 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2074 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2076 if (rc != LDAP_SUCCESS) {
2077 goto fn_exit;
2080 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2081 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2082 username));
2083 goto fn_exit;
2085 ldap_msgfree(result);
2086 result = NULL;
2088 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2089 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2090 sid, &result);
2091 if (rc == LDAP_SUCCESS) {
2092 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2093 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2094 "already in the base, with samba "
2095 "attributes\n", sid_string_dbg(sid)));
2096 goto fn_exit;
2098 ldap_msgfree(result);
2099 result = NULL;
2103 /* does the entry already exist but without a samba attributes?
2104 we need to return the samba attributes here */
2106 escape_user = escape_ldap_string_alloc( username );
2107 filter = talloc_strdup(attr_list, "(uid=%u)");
2108 if (!filter) {
2109 status = NT_STATUS_NO_MEMORY;
2110 goto fn_exit;
2112 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2113 if (!filter) {
2114 status = NT_STATUS_NO_MEMORY;
2115 goto fn_exit;
2117 SAFE_FREE(escape_user);
2119 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2120 filter, attr_list, &result);
2121 if ( rc != LDAP_SUCCESS ) {
2122 goto fn_exit;
2125 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2127 if (num_result > 1) {
2128 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2129 goto fn_exit;
2132 /* Check if we need to update an existing entry */
2133 if (num_result == 1) {
2134 char *tmp;
2136 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2137 ldap_op = LDAP_MOD_REPLACE;
2138 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2139 tmp = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2140 if (!tmp) {
2141 goto fn_exit;
2143 dn = talloc_asprintf(ctx, "%s", tmp);
2144 SAFE_FREE(tmp);
2145 if (!dn) {
2146 status = NT_STATUS_NO_MEMORY;
2147 goto fn_exit;
2150 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2152 /* There might be a SID for this account already - say an idmap entry */
2154 filter = talloc_asprintf(ctx,
2155 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2156 get_userattr_key2string(ldap_state->schema_ver,
2157 LDAP_ATTR_USER_SID),
2158 sid_string_talloc(ctx, sid),
2159 LDAP_OBJ_IDMAP_ENTRY,
2160 LDAP_OBJ_SID_ENTRY);
2161 if (!filter) {
2162 status = NT_STATUS_NO_MEMORY;
2163 goto fn_exit;
2166 /* free old result before doing a new search */
2167 if (result != NULL) {
2168 ldap_msgfree(result);
2169 result = NULL;
2171 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2172 filter, attr_list, &result);
2174 if ( rc != LDAP_SUCCESS ) {
2175 goto fn_exit;
2178 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2180 if (num_result > 1) {
2181 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2182 goto fn_exit;
2185 /* Check if we need to update an existing entry */
2186 if (num_result == 1) {
2187 char *tmp;
2189 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2190 ldap_op = LDAP_MOD_REPLACE;
2191 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2192 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
2193 if (!tmp) {
2194 goto fn_exit;
2196 dn = talloc_asprintf(ctx, "%s", tmp);
2197 SAFE_FREE(tmp);
2198 if (!dn) {
2199 status = NT_STATUS_NO_MEMORY;
2200 goto fn_exit;
2205 if (num_result == 0) {
2206 char *escape_username;
2207 /* Check if we need to add an entry */
2208 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2209 ldap_op = LDAP_MOD_ADD;
2211 escape_username = escape_rdn_val_string_alloc(username);
2212 if (!escape_username) {
2213 status = NT_STATUS_NO_MEMORY;
2214 goto fn_exit;
2217 if (username[strlen(username)-1] == '$') {
2218 dn = talloc_asprintf(ctx,
2219 "uid=%s,%s",
2220 escape_username,
2221 lp_ldap_machine_suffix());
2222 } else {
2223 dn = talloc_asprintf(ctx,
2224 "uid=%s,%s",
2225 escape_username,
2226 lp_ldap_user_suffix());
2229 SAFE_FREE(escape_username);
2230 if (!dn) {
2231 status = NT_STATUS_NO_MEMORY;
2232 goto fn_exit;
2236 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2237 element_is_set_or_changed)) {
2238 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2239 if (mods != NULL) {
2240 ldap_mods_free(mods, true);
2242 goto fn_exit;
2245 if (mods == NULL) {
2246 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2247 goto fn_exit;
2249 switch ( ldap_state->schema_ver ) {
2250 case SCHEMAVER_SAMBAACCOUNT:
2251 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2252 break;
2253 case SCHEMAVER_SAMBASAMACCOUNT:
2254 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2255 break;
2256 default:
2257 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2258 break;
2261 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2262 if (!NT_STATUS_IS_OK(ret)) {
2263 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2264 pdb_get_username(newpwd),dn));
2265 ldap_mods_free(mods, true);
2266 goto fn_exit;
2269 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2270 ldap_mods_free(mods, true);
2272 status = NT_STATUS_OK;
2274 fn_exit:
2276 TALLOC_FREE(ctx);
2277 SAFE_FREE(escape_user);
2278 if (result) {
2279 ldap_msgfree(result);
2281 return status;
2284 /**********************************************************************
2285 *********************************************************************/
2287 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2288 const char *filter,
2289 LDAPMessage ** result)
2291 int scope = LDAP_SCOPE_SUBTREE;
2292 int rc;
2293 const char **attr_list;
2295 attr_list = get_attr_list(NULL, groupmap_attr_list);
2296 rc = smbldap_search(ldap_state->smbldap_state,
2297 lp_ldap_group_suffix (), scope,
2298 filter, attr_list, 0, result);
2299 TALLOC_FREE(attr_list);
2301 return rc;
2304 /**********************************************************************
2305 *********************************************************************/
2307 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2308 GROUP_MAP *map, LDAPMessage *entry)
2310 char *temp = NULL;
2311 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2313 if (ldap_state == NULL || map == NULL || entry == NULL ||
2314 ldap_state->smbldap_state->ldap_struct == NULL) {
2315 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2316 TALLOC_FREE(ctx);
2317 return false;
2320 temp = smbldap_talloc_single_attribute(
2321 ldap_state->smbldap_state->ldap_struct,
2322 entry,
2323 get_attr_key2string(groupmap_attr_list,
2324 LDAP_ATTR_GIDNUMBER),
2325 ctx);
2326 if (!temp) {
2327 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2328 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2329 TALLOC_FREE(ctx);
2330 return false;
2332 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2334 map->gid = (gid_t)atol(temp);
2336 TALLOC_FREE(temp);
2337 temp = smbldap_talloc_single_attribute(
2338 ldap_state->smbldap_state->ldap_struct,
2339 entry,
2340 get_attr_key2string(groupmap_attr_list,
2341 LDAP_ATTR_GROUP_SID),
2342 ctx);
2343 if (!temp) {
2344 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2345 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2346 TALLOC_FREE(ctx);
2347 return false;
2350 if (!string_to_sid(&map->sid, temp)) {
2351 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2352 TALLOC_FREE(ctx);
2353 return false;
2356 TALLOC_FREE(temp);
2357 temp = smbldap_talloc_single_attribute(
2358 ldap_state->smbldap_state->ldap_struct,
2359 entry,
2360 get_attr_key2string(groupmap_attr_list,
2361 LDAP_ATTR_GROUP_TYPE),
2362 ctx);
2363 if (!temp) {
2364 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2365 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2366 TALLOC_FREE(ctx);
2367 return false;
2369 map->sid_name_use = (enum lsa_SidType)atol(temp);
2371 if ((map->sid_name_use < SID_NAME_USER) ||
2372 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2373 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2374 TALLOC_FREE(ctx);
2375 return false;
2378 TALLOC_FREE(temp);
2379 temp = smbldap_talloc_single_attribute(
2380 ldap_state->smbldap_state->ldap_struct,
2381 entry,
2382 get_attr_key2string(groupmap_attr_list,
2383 LDAP_ATTR_DISPLAY_NAME),
2384 ctx);
2385 if (!temp) {
2386 temp = smbldap_talloc_single_attribute(
2387 ldap_state->smbldap_state->ldap_struct,
2388 entry,
2389 get_attr_key2string(groupmap_attr_list,
2390 LDAP_ATTR_CN),
2391 ctx);
2392 if (!temp) {
2393 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2394 for gidNumber(%lu)\n",(unsigned long)map->gid));
2395 TALLOC_FREE(ctx);
2396 return false;
2399 fstrcpy(map->nt_name, temp);
2401 TALLOC_FREE(temp);
2402 temp = smbldap_talloc_single_attribute(
2403 ldap_state->smbldap_state->ldap_struct,
2404 entry,
2405 get_attr_key2string(groupmap_attr_list,
2406 LDAP_ATTR_DESC),
2407 ctx);
2408 if (!temp) {
2409 temp = talloc_strdup(ctx, "");
2410 if (!temp) {
2411 TALLOC_FREE(ctx);
2412 return false;
2415 fstrcpy(map->comment, temp);
2417 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2418 store_gid_sid_cache(&map->sid, map->gid);
2421 TALLOC_FREE(ctx);
2422 return true;
2425 /**********************************************************************
2426 *********************************************************************/
2428 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2429 const char *filter,
2430 GROUP_MAP *map)
2432 struct ldapsam_privates *ldap_state =
2433 (struct ldapsam_privates *)methods->private_data;
2434 LDAPMessage *result = NULL;
2435 LDAPMessage *entry = NULL;
2436 int count;
2438 if (ldapsam_search_one_group(ldap_state, filter, &result)
2439 != LDAP_SUCCESS) {
2440 return NT_STATUS_NO_SUCH_GROUP;
2443 count = ldap_count_entries(priv2ld(ldap_state), result);
2445 if (count < 1) {
2446 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2447 "%s\n", filter));
2448 ldap_msgfree(result);
2449 return NT_STATUS_NO_SUCH_GROUP;
2452 if (count > 1) {
2453 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2454 "count=%d\n", filter, count));
2455 ldap_msgfree(result);
2456 return NT_STATUS_NO_SUCH_GROUP;
2459 entry = ldap_first_entry(priv2ld(ldap_state), result);
2461 if (!entry) {
2462 ldap_msgfree(result);
2463 return NT_STATUS_UNSUCCESSFUL;
2466 if (!init_group_from_ldap(ldap_state, map, entry)) {
2467 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2468 "group filter %s\n", filter));
2469 ldap_msgfree(result);
2470 return NT_STATUS_NO_SUCH_GROUP;
2473 ldap_msgfree(result);
2474 return NT_STATUS_OK;
2477 /**********************************************************************
2478 *********************************************************************/
2480 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2481 DOM_SID sid)
2483 char *filter = NULL;
2484 NTSTATUS status;
2485 fstring tmp;
2487 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2488 LDAP_OBJ_GROUPMAP,
2489 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2490 sid_to_fstring(tmp, &sid)) < 0) {
2491 return NT_STATUS_NO_MEMORY;
2494 status = ldapsam_getgroup(methods, filter, map);
2495 SAFE_FREE(filter);
2496 return status;
2499 /**********************************************************************
2500 *********************************************************************/
2502 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2503 gid_t gid)
2505 char *filter = NULL;
2506 NTSTATUS status;
2508 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2509 LDAP_OBJ_GROUPMAP,
2510 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2511 (unsigned long)gid) < 0) {
2512 return NT_STATUS_NO_MEMORY;
2515 status = ldapsam_getgroup(methods, filter, map);
2516 SAFE_FREE(filter);
2517 return status;
2520 /**********************************************************************
2521 *********************************************************************/
2523 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2524 const char *name)
2526 char *filter = NULL;
2527 char *escape_name = escape_ldap_string_alloc(name);
2528 NTSTATUS status;
2530 if (!escape_name) {
2531 return NT_STATUS_NO_MEMORY;
2534 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2535 LDAP_OBJ_GROUPMAP,
2536 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2537 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2538 escape_name) < 0) {
2539 SAFE_FREE(escape_name);
2540 return NT_STATUS_NO_MEMORY;
2543 SAFE_FREE(escape_name);
2544 status = ldapsam_getgroup(methods, filter, map);
2545 SAFE_FREE(filter);
2546 return status;
2549 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2550 LDAPMessage *entry,
2551 const DOM_SID *domain_sid,
2552 uint32 *rid)
2554 fstring str;
2555 DOM_SID sid;
2557 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2558 str, sizeof(str)-1)) {
2559 DEBUG(10, ("Could not find sambaSID attribute\n"));
2560 return False;
2563 if (!string_to_sid(&sid, str)) {
2564 DEBUG(10, ("Could not convert string %s to sid\n", str));
2565 return False;
2568 if (sid_compare_domain(&sid, domain_sid) != 0) {
2569 DEBUG(10, ("SID %s is not in expected domain %s\n",
2570 str, sid_string_dbg(domain_sid)));
2571 return False;
2574 if (!sid_peek_rid(&sid, rid)) {
2575 DEBUG(10, ("Could not peek into RID\n"));
2576 return False;
2579 return True;
2582 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2583 TALLOC_CTX *mem_ctx,
2584 const DOM_SID *group,
2585 uint32 **pp_member_rids,
2586 size_t *p_num_members)
2588 struct ldapsam_privates *ldap_state =
2589 (struct ldapsam_privates *)methods->private_data;
2590 struct smbldap_state *conn = ldap_state->smbldap_state;
2591 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2592 const char *sid_attrs[] = { "sambaSID", NULL };
2593 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2594 LDAPMessage *result = NULL;
2595 LDAPMessage *entry;
2596 char *filter;
2597 char **values = NULL;
2598 char **memberuid;
2599 char *gidstr;
2600 int rc, count;
2602 *pp_member_rids = NULL;
2603 *p_num_members = 0;
2605 filter = talloc_asprintf(mem_ctx,
2606 "(&(objectClass=%s)"
2607 "(objectClass=%s)"
2608 "(sambaSID=%s))",
2609 LDAP_OBJ_POSIXGROUP,
2610 LDAP_OBJ_GROUPMAP,
2611 sid_string_talloc(mem_ctx, group));
2612 if (filter == NULL) {
2613 ret = NT_STATUS_NO_MEMORY;
2614 goto done;
2617 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2618 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2619 &result);
2621 if (rc != LDAP_SUCCESS)
2622 goto done;
2624 talloc_autofree_ldapmsg(mem_ctx, result);
2626 count = ldap_count_entries(conn->ldap_struct, result);
2628 if (count > 1) {
2629 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2630 sid_string_dbg(group)));
2631 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2632 goto done;
2635 if (count == 0) {
2636 ret = NT_STATUS_NO_SUCH_GROUP;
2637 goto done;
2640 entry = ldap_first_entry(conn->ldap_struct, result);
2641 if (entry == NULL)
2642 goto done;
2644 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2645 if (!gidstr) {
2646 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2647 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2648 goto done;
2651 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2653 if (values) {
2655 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2656 if (filter == NULL) {
2657 ret = NT_STATUS_NO_MEMORY;
2658 goto done;
2661 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2662 char *escape_memberuid;
2664 escape_memberuid = escape_ldap_string_alloc(*memberuid);
2665 if (escape_memberuid == NULL) {
2666 ret = NT_STATUS_NO_MEMORY;
2667 goto done;
2670 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2671 if (filter == NULL) {
2672 SAFE_FREE(escape_memberuid);
2673 ret = NT_STATUS_NO_MEMORY;
2674 goto done;
2677 SAFE_FREE(escape_memberuid);
2680 filter = talloc_asprintf_append_buffer(filter, "))");
2681 if (filter == NULL) {
2682 ret = NT_STATUS_NO_MEMORY;
2683 goto done;
2686 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2687 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2688 &result);
2690 if (rc != LDAP_SUCCESS)
2691 goto done;
2693 count = ldap_count_entries(conn->ldap_struct, result);
2694 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2696 talloc_autofree_ldapmsg(mem_ctx, result);
2698 for (entry = ldap_first_entry(conn->ldap_struct, result);
2699 entry != NULL;
2700 entry = ldap_next_entry(conn->ldap_struct, entry))
2702 char *sidstr;
2703 DOM_SID sid;
2704 uint32 rid;
2706 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2707 entry, "sambaSID",
2708 mem_ctx);
2709 if (!sidstr) {
2710 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2711 "the sambaSID attribute\n"));
2712 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2713 goto done;
2716 if (!string_to_sid(&sid, sidstr))
2717 goto done;
2719 if (!sid_check_is_in_our_domain(&sid)) {
2720 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2721 "in our domain\n"));
2722 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2723 goto done;
2726 sid_peek_rid(&sid, &rid);
2728 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2729 p_num_members)) {
2730 ret = NT_STATUS_NO_MEMORY;
2731 goto done;
2736 filter = talloc_asprintf(mem_ctx,
2737 "(&(objectClass=%s)"
2738 "(gidNumber=%s))",
2739 LDAP_OBJ_SAMBASAMACCOUNT,
2740 gidstr);
2742 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2743 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2744 &result);
2746 if (rc != LDAP_SUCCESS)
2747 goto done;
2749 talloc_autofree_ldapmsg(mem_ctx, result);
2751 for (entry = ldap_first_entry(conn->ldap_struct, result);
2752 entry != NULL;
2753 entry = ldap_next_entry(conn->ldap_struct, entry))
2755 uint32 rid;
2757 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2758 entry,
2759 get_global_sam_sid(),
2760 &rid)) {
2761 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2762 "the sambaSID attribute\n"));
2763 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2764 goto done;
2767 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2768 p_num_members)) {
2769 ret = NT_STATUS_NO_MEMORY;
2770 goto done;
2774 ret = NT_STATUS_OK;
2776 done:
2778 if (values)
2779 ldap_value_free(values);
2781 return ret;
2784 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2785 TALLOC_CTX *mem_ctx,
2786 struct samu *user,
2787 DOM_SID **pp_sids,
2788 gid_t **pp_gids,
2789 size_t *p_num_groups)
2791 struct ldapsam_privates *ldap_state =
2792 (struct ldapsam_privates *)methods->private_data;
2793 struct smbldap_state *conn = ldap_state->smbldap_state;
2794 char *filter;
2795 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2796 char *escape_name;
2797 int rc, count;
2798 LDAPMessage *result = NULL;
2799 LDAPMessage *entry;
2800 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2801 size_t num_sids, num_gids;
2802 char *gidstr;
2803 gid_t primary_gid = -1;
2805 *pp_sids = NULL;
2806 num_sids = 0;
2808 if (pdb_get_username(user) == NULL) {
2809 return NT_STATUS_INVALID_PARAMETER;
2812 escape_name = escape_ldap_string_alloc(pdb_get_username(user));
2813 if (escape_name == NULL)
2814 return NT_STATUS_NO_MEMORY;
2816 /* retrieve the users primary gid */
2817 filter = talloc_asprintf(mem_ctx,
2818 "(&(objectClass=%s)(uid=%s))",
2819 LDAP_OBJ_SAMBASAMACCOUNT,
2820 escape_name);
2821 if (filter == NULL) {
2822 ret = NT_STATUS_NO_MEMORY;
2823 goto done;
2826 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2827 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2829 if (rc != LDAP_SUCCESS)
2830 goto done;
2832 talloc_autofree_ldapmsg(mem_ctx, result);
2834 count = ldap_count_entries(priv2ld(ldap_state), result);
2836 switch (count) {
2837 case 0:
2838 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2839 ret = NT_STATUS_NO_SUCH_USER;
2840 goto done;
2841 case 1:
2842 entry = ldap_first_entry(priv2ld(ldap_state), result);
2844 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2845 if (!gidstr) {
2846 DEBUG (1, ("Unable to find the member's gid!\n"));
2847 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2848 goto done;
2850 primary_gid = strtoul(gidstr, NULL, 10);
2851 break;
2852 default:
2853 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2854 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2855 goto done;
2858 filter = talloc_asprintf(mem_ctx,
2859 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
2860 LDAP_OBJ_POSIXGROUP, escape_name, primary_gid);
2861 if (filter == NULL) {
2862 ret = NT_STATUS_NO_MEMORY;
2863 goto done;
2866 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2867 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2869 if (rc != LDAP_SUCCESS)
2870 goto done;
2872 talloc_autofree_ldapmsg(mem_ctx, result);
2874 num_gids = 0;
2875 *pp_gids = NULL;
2877 num_sids = 0;
2878 *pp_sids = NULL;
2880 /* We need to add the primary group as the first gid/sid */
2882 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2883 ret = NT_STATUS_NO_MEMORY;
2884 goto done;
2887 /* This sid will be replaced later */
2889 if (!add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids, &num_sids)) {
2890 ret = NT_STATUS_NO_MEMORY;
2891 goto done;
2894 for (entry = ldap_first_entry(conn->ldap_struct, result);
2895 entry != NULL;
2896 entry = ldap_next_entry(conn->ldap_struct, entry))
2898 fstring str;
2899 DOM_SID sid;
2900 gid_t gid;
2901 char *end;
2903 if (!smbldap_get_single_attribute(conn->ldap_struct,
2904 entry, "sambaSID",
2905 str, sizeof(str)-1))
2906 continue;
2908 if (!string_to_sid(&sid, str))
2909 goto done;
2911 if (!smbldap_get_single_attribute(conn->ldap_struct,
2912 entry, "gidNumber",
2913 str, sizeof(str)-1))
2914 continue;
2916 gid = strtoul(str, &end, 10);
2918 if (PTR_DIFF(end, str) != strlen(str))
2919 goto done;
2921 if (gid == primary_gid) {
2922 sid_copy(&(*pp_sids)[0], &sid);
2923 } else {
2924 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2925 &num_gids)) {
2926 ret = NT_STATUS_NO_MEMORY;
2927 goto done;
2929 if (!add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2930 &num_sids)) {
2931 ret = NT_STATUS_NO_MEMORY;
2932 goto done;
2937 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
2938 DEBUG(3, ("primary group of [%s] not found\n",
2939 pdb_get_username(user)));
2940 goto done;
2943 *p_num_groups = num_sids;
2945 ret = NT_STATUS_OK;
2947 done:
2949 SAFE_FREE(escape_name);
2950 return ret;
2953 /**********************************************************************
2954 * Augment a posixGroup object with a sambaGroupMapping domgroup
2955 *********************************************************************/
2957 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
2958 struct ldapsam_privates *ldap_state,
2959 GROUP_MAP *map)
2961 const char *filter, *dn;
2962 LDAPMessage *msg, *entry;
2963 LDAPMod **mods;
2964 int rc;
2966 filter = talloc_asprintf(mem_ctx,
2967 "(&(objectClass=posixGroup)(gidNumber=%u))",
2968 map->gid);
2969 if (filter == NULL) {
2970 return NT_STATUS_NO_MEMORY;
2973 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2974 get_attr_list(mem_ctx, groupmap_attr_list),
2975 &msg);
2976 talloc_autofree_ldapmsg(mem_ctx, msg);
2978 if ((rc != LDAP_SUCCESS) ||
2979 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2980 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2981 return NT_STATUS_NO_SUCH_GROUP;
2984 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2985 if (dn == NULL) {
2986 return NT_STATUS_NO_MEMORY;
2989 mods = NULL;
2990 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
2991 "sambaGroupMapping");
2992 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
2993 sid_string_talloc(mem_ctx, &map->sid));
2994 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
2995 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
2996 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
2997 map->nt_name);
2998 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
2999 map->comment);
3000 talloc_autofree_ldapmod(mem_ctx, mods);
3002 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3003 if (rc != LDAP_SUCCESS) {
3004 return NT_STATUS_ACCESS_DENIED;
3007 return NT_STATUS_OK;
3010 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3011 GROUP_MAP *map)
3013 struct ldapsam_privates *ldap_state =
3014 (struct ldapsam_privates *)methods->private_data;
3015 LDAPMessage *msg = NULL;
3016 LDAPMod **mods = NULL;
3017 const char *attrs[] = { NULL };
3018 char *filter;
3020 char *dn;
3021 TALLOC_CTX *mem_ctx;
3022 NTSTATUS result;
3024 DOM_SID sid;
3026 int rc;
3028 mem_ctx = talloc_new(NULL);
3029 if (mem_ctx == NULL) {
3030 DEBUG(0, ("talloc_new failed\n"));
3031 return NT_STATUS_NO_MEMORY;
3034 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3035 sid_string_talloc(mem_ctx, &map->sid));
3036 if (filter == NULL) {
3037 result = NT_STATUS_NO_MEMORY;
3038 goto done;
3041 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3042 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3043 talloc_autofree_ldapmsg(mem_ctx, msg);
3045 if ((rc == LDAP_SUCCESS) &&
3046 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3048 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3049 "group mapping entry\n", sid_string_dbg(&map->sid)));
3050 result = NT_STATUS_GROUP_EXISTS;
3051 goto done;
3054 switch (map->sid_name_use) {
3056 case SID_NAME_DOM_GRP:
3057 /* To map a domain group we need to have a posix group
3058 to attach to. */
3059 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3060 goto done;
3061 break;
3063 case SID_NAME_ALIAS:
3064 if (!sid_check_is_in_our_domain(&map->sid)
3065 && !sid_check_is_in_builtin(&map->sid) )
3067 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3068 sid_string_dbg(&map->sid)));
3069 result = NT_STATUS_INVALID_PARAMETER;
3070 goto done;
3072 break;
3074 default:
3075 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3076 sid_type_lookup(map->sid_name_use)));
3077 result = NT_STATUS_INVALID_PARAMETER;
3078 goto done;
3081 /* Domain groups have been mapped in a separate routine, we have to
3082 * create an alias now */
3084 if (map->gid == -1) {
3085 DEBUG(10, ("Refusing to map gid==-1\n"));
3086 result = NT_STATUS_INVALID_PARAMETER;
3087 goto done;
3090 if (pdb_gid_to_sid(map->gid, &sid)) {
3091 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
3092 "add\n", map->gid, sid_string_dbg(&sid)));
3093 result = NT_STATUS_GROUP_EXISTS;
3094 goto done;
3097 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3098 * the best we can get out of LDAP. */
3100 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3101 sid_string_talloc(mem_ctx, &map->sid),
3102 lp_ldap_group_suffix());
3103 if (dn == NULL) {
3104 result = NT_STATUS_NO_MEMORY;
3105 goto done;
3108 mods = NULL;
3110 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3111 "sambaSidEntry");
3112 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3113 "sambaGroupMapping");
3115 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3116 sid_string_talloc(mem_ctx, &map->sid));
3117 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3118 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3119 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3120 map->nt_name);
3121 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3122 map->comment);
3123 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3124 talloc_asprintf(mem_ctx, "%u", map->gid));
3125 talloc_autofree_ldapmod(mem_ctx, mods);
3127 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3129 result = (rc == LDAP_SUCCESS) ?
3130 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3132 done:
3133 TALLOC_FREE(mem_ctx);
3134 return result;
3137 /**********************************************************************
3138 * Update a group mapping entry. We're quite strict about what can be changed:
3139 * Only the description and displayname may be changed. It simply does not
3140 * make any sense to change the SID, gid or the type in a mapping.
3141 *********************************************************************/
3143 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3144 GROUP_MAP *map)
3146 struct ldapsam_privates *ldap_state =
3147 (struct ldapsam_privates *)methods->private_data;
3148 int rc;
3149 const char *filter, *dn;
3150 LDAPMessage *msg = NULL;
3151 LDAPMessage *entry = NULL;
3152 LDAPMod **mods = NULL;
3153 TALLOC_CTX *mem_ctx;
3154 NTSTATUS result;
3156 mem_ctx = talloc_new(NULL);
3157 if (mem_ctx == NULL) {
3158 DEBUG(0, ("talloc_new failed\n"));
3159 return NT_STATUS_NO_MEMORY;
3162 /* Make 100% sure that sid, gid and type are not changed by looking up
3163 * exactly the values we're given in LDAP. */
3165 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3166 "(sambaSid=%s)(gidNumber=%u)"
3167 "(sambaGroupType=%d))",
3168 LDAP_OBJ_GROUPMAP,
3169 sid_string_talloc(mem_ctx, &map->sid),
3170 map->gid, map->sid_name_use);
3171 if (filter == NULL) {
3172 result = NT_STATUS_NO_MEMORY;
3173 goto done;
3176 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3177 get_attr_list(mem_ctx, groupmap_attr_list),
3178 &msg);
3179 talloc_autofree_ldapmsg(mem_ctx, msg);
3181 if ((rc != LDAP_SUCCESS) ||
3182 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3183 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3184 result = NT_STATUS_NO_SUCH_GROUP;
3185 goto done;
3188 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3190 if (dn == NULL) {
3191 result = NT_STATUS_NO_MEMORY;
3192 goto done;
3195 mods = NULL;
3196 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3197 map->nt_name);
3198 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3199 map->comment);
3200 talloc_autofree_ldapmod(mem_ctx, mods);
3202 if (mods == NULL) {
3203 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3204 "nothing to do\n"));
3205 result = NT_STATUS_OK;
3206 goto done;
3209 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3211 if (rc != LDAP_SUCCESS) {
3212 result = NT_STATUS_ACCESS_DENIED;
3213 goto done;
3216 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3217 "group %lu in LDAP\n", (unsigned long)map->gid));
3219 result = NT_STATUS_OK;
3221 done:
3222 TALLOC_FREE(mem_ctx);
3223 return result;
3226 /**********************************************************************
3227 *********************************************************************/
3229 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3230 DOM_SID sid)
3232 struct ldapsam_privates *priv =
3233 (struct ldapsam_privates *)methods->private_data;
3234 LDAPMessage *msg, *entry;
3235 int rc;
3236 NTSTATUS result;
3237 TALLOC_CTX *mem_ctx;
3238 char *filter;
3240 mem_ctx = talloc_new(NULL);
3241 if (mem_ctx == NULL) {
3242 DEBUG(0, ("talloc_new failed\n"));
3243 return NT_STATUS_NO_MEMORY;
3246 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3247 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3248 sid_string_talloc(mem_ctx, &sid));
3249 if (filter == NULL) {
3250 result = NT_STATUS_NO_MEMORY;
3251 goto done;
3253 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3254 get_attr_list(mem_ctx, groupmap_attr_list),
3255 &msg);
3256 talloc_autofree_ldapmsg(mem_ctx, msg);
3258 if ((rc != LDAP_SUCCESS) ||
3259 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3260 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3261 result = NT_STATUS_NO_SUCH_GROUP;
3262 goto done;
3265 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3266 get_attr_list(mem_ctx,
3267 groupmap_attr_list_to_delete));
3269 if ((rc == LDAP_NAMING_VIOLATION) ||
3270 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3271 const char *attrs[] = { "sambaGroupType", "description",
3272 "displayName", "sambaSIDList",
3273 NULL };
3275 /* Second try. Don't delete the sambaSID attribute, this is
3276 for "old" entries that are tacked on a winbind
3277 sambaIdmapEntry. */
3279 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3280 LDAP_OBJ_GROUPMAP, attrs);
3283 if ((rc == LDAP_NAMING_VIOLATION) ||
3284 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3285 const char *attrs[] = { "sambaGroupType", "description",
3286 "displayName", "sambaSIDList",
3287 "gidNumber", NULL };
3289 /* Third try. This is a post-3.0.21 alias (containing only
3290 * sambaSidEntry and sambaGroupMapping classes), we also have
3291 * to delete the gidNumber attribute, only the sambaSidEntry
3292 * remains */
3294 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3295 LDAP_OBJ_GROUPMAP, attrs);
3298 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3300 done:
3301 TALLOC_FREE(mem_ctx);
3302 return result;
3305 /**********************************************************************
3306 *********************************************************************/
3308 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3309 bool update)
3311 struct ldapsam_privates *ldap_state =
3312 (struct ldapsam_privates *)my_methods->private_data;
3313 char *filter = NULL;
3314 int rc;
3315 const char **attr_list;
3317 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3318 if (!filter) {
3319 return NT_STATUS_NO_MEMORY;
3321 attr_list = get_attr_list( NULL, groupmap_attr_list );
3322 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3323 LDAP_SCOPE_SUBTREE, filter,
3324 attr_list, 0, &ldap_state->result);
3325 TALLOC_FREE(attr_list);
3327 if (rc != LDAP_SUCCESS) {
3328 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3329 ldap_err2string(rc)));
3330 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3331 lp_ldap_group_suffix(), filter));
3332 ldap_msgfree(ldap_state->result);
3333 ldap_state->result = NULL;
3334 TALLOC_FREE(filter);
3335 return NT_STATUS_UNSUCCESSFUL;
3338 TALLOC_FREE(filter);
3340 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3341 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3342 ldap_state->result)));
3344 ldap_state->entry =
3345 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3346 ldap_state->result);
3347 ldap_state->index = 0;
3349 return NT_STATUS_OK;
3352 /**********************************************************************
3353 *********************************************************************/
3355 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3357 ldapsam_endsampwent(my_methods);
3360 /**********************************************************************
3361 *********************************************************************/
3363 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3364 GROUP_MAP *map)
3366 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3367 struct ldapsam_privates *ldap_state =
3368 (struct ldapsam_privates *)my_methods->private_data;
3369 bool bret = False;
3371 while (!bret) {
3372 if (!ldap_state->entry)
3373 return ret;
3375 ldap_state->index++;
3376 bret = init_group_from_ldap(ldap_state, map,
3377 ldap_state->entry);
3379 ldap_state->entry =
3380 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3381 ldap_state->entry);
3384 return NT_STATUS_OK;
3387 /**********************************************************************
3388 *********************************************************************/
3390 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3391 const DOM_SID *domsid, enum lsa_SidType sid_name_use,
3392 GROUP_MAP **pp_rmap,
3393 size_t *p_num_entries,
3394 bool unix_only)
3396 GROUP_MAP map;
3397 size_t entries = 0;
3399 *p_num_entries = 0;
3400 *pp_rmap = NULL;
3402 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3403 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3404 "passdb\n"));
3405 return NT_STATUS_ACCESS_DENIED;
3408 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3409 if (sid_name_use != SID_NAME_UNKNOWN &&
3410 sid_name_use != map.sid_name_use) {
3411 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3412 "not of the requested type\n", map.nt_name));
3413 continue;
3415 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3416 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3417 "non mapped\n", map.nt_name));
3418 continue;
3421 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3422 if (!(*pp_rmap)) {
3423 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3424 "enlarge group map!\n"));
3425 return NT_STATUS_UNSUCCESSFUL;
3428 (*pp_rmap)[entries] = map;
3430 entries += 1;
3433 ldapsam_endsamgrent(methods);
3435 *p_num_entries = entries;
3437 return NT_STATUS_OK;
3440 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3441 const DOM_SID *alias,
3442 const DOM_SID *member,
3443 int modop)
3445 struct ldapsam_privates *ldap_state =
3446 (struct ldapsam_privates *)methods->private_data;
3447 char *dn = NULL;
3448 LDAPMessage *result = NULL;
3449 LDAPMessage *entry = NULL;
3450 int count;
3451 LDAPMod **mods = NULL;
3452 int rc;
3453 enum lsa_SidType type = SID_NAME_USE_NONE;
3454 fstring tmp;
3456 char *filter = NULL;
3458 if (sid_check_is_in_builtin(alias)) {
3459 type = SID_NAME_ALIAS;
3462 if (sid_check_is_in_our_domain(alias)) {
3463 type = SID_NAME_ALIAS;
3466 if (type == SID_NAME_USE_NONE) {
3467 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3468 sid_string_dbg(alias)));
3469 return NT_STATUS_NO_SUCH_ALIAS;
3472 if (asprintf(&filter,
3473 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3474 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3475 type) < 0) {
3476 return NT_STATUS_NO_MEMORY;
3479 if (ldapsam_search_one_group(ldap_state, filter,
3480 &result) != LDAP_SUCCESS) {
3481 SAFE_FREE(filter);
3482 return NT_STATUS_NO_SUCH_ALIAS;
3485 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3486 result);
3488 if (count < 1) {
3489 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3490 ldap_msgfree(result);
3491 SAFE_FREE(filter);
3492 return NT_STATUS_NO_SUCH_ALIAS;
3495 if (count > 1) {
3496 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3497 "filter %s: count=%d\n", filter, count));
3498 ldap_msgfree(result);
3499 SAFE_FREE(filter);
3500 return NT_STATUS_NO_SUCH_ALIAS;
3503 SAFE_FREE(filter);
3505 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3506 result);
3508 if (!entry) {
3509 ldap_msgfree(result);
3510 return NT_STATUS_UNSUCCESSFUL;
3513 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
3514 if (!dn) {
3515 ldap_msgfree(result);
3516 return NT_STATUS_UNSUCCESSFUL;
3519 smbldap_set_mod(&mods, modop,
3520 get_attr_key2string(groupmap_attr_list,
3521 LDAP_ATTR_SID_LIST),
3522 sid_to_fstring(tmp, member));
3524 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3526 ldap_mods_free(mods, True);
3527 ldap_msgfree(result);
3528 SAFE_FREE(dn);
3530 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3531 return NT_STATUS_MEMBER_IN_ALIAS;
3534 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3535 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3538 if (rc != LDAP_SUCCESS) {
3539 return NT_STATUS_UNSUCCESSFUL;
3542 return NT_STATUS_OK;
3545 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3546 const DOM_SID *alias,
3547 const DOM_SID *member)
3549 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3552 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3553 const DOM_SID *alias,
3554 const DOM_SID *member)
3556 return ldapsam_modify_aliasmem(methods, alias, member,
3557 LDAP_MOD_DELETE);
3560 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3561 const DOM_SID *alias,
3562 DOM_SID **pp_members,
3563 size_t *p_num_members)
3565 struct ldapsam_privates *ldap_state =
3566 (struct ldapsam_privates *)methods->private_data;
3567 LDAPMessage *result = NULL;
3568 LDAPMessage *entry = NULL;
3569 int count;
3570 char **values = NULL;
3571 int i;
3572 char *filter = NULL;
3573 size_t num_members = 0;
3574 enum lsa_SidType type = SID_NAME_USE_NONE;
3575 fstring tmp;
3577 *pp_members = NULL;
3578 *p_num_members = 0;
3580 if (sid_check_is_in_builtin(alias)) {
3581 type = SID_NAME_ALIAS;
3584 if (sid_check_is_in_our_domain(alias)) {
3585 type = SID_NAME_ALIAS;
3588 if (type == SID_NAME_USE_NONE) {
3589 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3590 sid_string_dbg(alias)));
3591 return NT_STATUS_NO_SUCH_ALIAS;
3594 if (asprintf(&filter,
3595 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3596 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3597 type) < 0) {
3598 return NT_STATUS_NO_MEMORY;
3601 if (ldapsam_search_one_group(ldap_state, filter,
3602 &result) != LDAP_SUCCESS) {
3603 SAFE_FREE(filter);
3604 return NT_STATUS_NO_SUCH_ALIAS;
3607 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3608 result);
3610 if (count < 1) {
3611 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3612 ldap_msgfree(result);
3613 SAFE_FREE(filter);
3614 return NT_STATUS_NO_SUCH_ALIAS;
3617 if (count > 1) {
3618 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3619 "filter %s: count=%d\n", filter, count));
3620 ldap_msgfree(result);
3621 SAFE_FREE(filter);
3622 return NT_STATUS_NO_SUCH_ALIAS;
3625 SAFE_FREE(filter);
3627 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3628 result);
3630 if (!entry) {
3631 ldap_msgfree(result);
3632 return NT_STATUS_UNSUCCESSFUL;
3635 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3636 entry,
3637 get_attr_key2string(groupmap_attr_list,
3638 LDAP_ATTR_SID_LIST));
3640 if (values == NULL) {
3641 ldap_msgfree(result);
3642 return NT_STATUS_OK;
3645 count = ldap_count_values(values);
3647 for (i=0; i<count; i++) {
3648 DOM_SID member;
3650 if (!string_to_sid(&member, values[i]))
3651 continue;
3653 if (!add_sid_to_array(NULL, &member, pp_members, &num_members)) {
3654 ldap_value_free(values);
3655 ldap_msgfree(result);
3656 return NT_STATUS_NO_MEMORY;
3660 *p_num_members = num_members;
3661 ldap_value_free(values);
3662 ldap_msgfree(result);
3664 return NT_STATUS_OK;
3667 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3668 TALLOC_CTX *mem_ctx,
3669 const DOM_SID *domain_sid,
3670 const DOM_SID *members,
3671 size_t num_members,
3672 uint32 **pp_alias_rids,
3673 size_t *p_num_alias_rids)
3675 struct ldapsam_privates *ldap_state =
3676 (struct ldapsam_privates *)methods->private_data;
3677 LDAP *ldap_struct;
3679 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3681 LDAPMessage *result = NULL;
3682 LDAPMessage *entry = NULL;
3683 int i;
3684 int rc;
3685 char *filter;
3686 enum lsa_SidType type = SID_NAME_USE_NONE;
3688 if (sid_check_is_builtin(domain_sid)) {
3689 type = SID_NAME_ALIAS;
3692 if (sid_check_is_domain(domain_sid)) {
3693 type = SID_NAME_ALIAS;
3696 if (type == SID_NAME_USE_NONE) {
3697 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3698 sid_string_dbg(domain_sid)));
3699 return NT_STATUS_UNSUCCESSFUL;
3702 filter = talloc_asprintf(mem_ctx,
3703 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3704 LDAP_OBJ_GROUPMAP, type);
3706 for (i=0; i<num_members; i++)
3707 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3708 filter,
3709 sid_string_talloc(mem_ctx,
3710 &members[i]));
3712 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3714 if (filter == NULL) {
3715 return NT_STATUS_NO_MEMORY;
3718 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3719 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3721 if (rc != LDAP_SUCCESS)
3722 return NT_STATUS_UNSUCCESSFUL;
3724 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3726 for (entry = ldap_first_entry(ldap_struct, result);
3727 entry != NULL;
3728 entry = ldap_next_entry(ldap_struct, entry))
3730 fstring sid_str;
3731 DOM_SID sid;
3732 uint32 rid;
3734 if (!smbldap_get_single_attribute(ldap_struct, entry,
3735 LDAP_ATTRIBUTE_SID,
3736 sid_str,
3737 sizeof(sid_str)-1))
3738 continue;
3740 if (!string_to_sid(&sid, sid_str))
3741 continue;
3743 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3744 continue;
3746 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3747 p_num_alias_rids)) {
3748 ldap_msgfree(result);
3749 return NT_STATUS_NO_MEMORY;
3753 ldap_msgfree(result);
3754 return NT_STATUS_OK;
3757 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3758 int policy_index,
3759 uint32 value)
3761 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3762 int rc;
3763 LDAPMod **mods = NULL;
3764 fstring value_string;
3765 const char *policy_attr = NULL;
3767 struct ldapsam_privates *ldap_state =
3768 (struct ldapsam_privates *)methods->private_data;
3770 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3772 if (!ldap_state->domain_dn) {
3773 return NT_STATUS_INVALID_PARAMETER;
3776 policy_attr = get_account_policy_attr(policy_index);
3777 if (policy_attr == NULL) {
3778 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3779 "policy\n"));
3780 return ntstatus;
3783 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3785 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3787 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3788 mods);
3790 ldap_mods_free(mods, True);
3792 if (rc != LDAP_SUCCESS) {
3793 return ntstatus;
3796 if (!cache_account_policy_set(policy_index, value)) {
3797 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3798 "update local tdb cache\n"));
3799 return ntstatus;
3802 return NT_STATUS_OK;
3805 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3806 int policy_index, uint32 value)
3808 return ldapsam_set_account_policy_in_ldap(methods, policy_index,
3809 value);
3812 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3813 int policy_index,
3814 uint32 *value)
3816 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3817 LDAPMessage *result = NULL;
3818 LDAPMessage *entry = NULL;
3819 int count;
3820 int rc;
3821 char **vals = NULL;
3822 const char *policy_attr = NULL;
3824 struct ldapsam_privates *ldap_state =
3825 (struct ldapsam_privates *)methods->private_data;
3827 const char *attrs[2];
3829 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3831 if (!ldap_state->domain_dn) {
3832 return NT_STATUS_INVALID_PARAMETER;
3835 policy_attr = get_account_policy_attr(policy_index);
3836 if (!policy_attr) {
3837 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3838 "policy index: %d\n", policy_index));
3839 return ntstatus;
3842 attrs[0] = policy_attr;
3843 attrs[1] = NULL;
3845 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3846 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
3847 &result);
3849 if (rc != LDAP_SUCCESS) {
3850 return ntstatus;
3853 count = ldap_count_entries(priv2ld(ldap_state), result);
3854 if (count < 1) {
3855 goto out;
3858 entry = ldap_first_entry(priv2ld(ldap_state), result);
3859 if (entry == NULL) {
3860 goto out;
3863 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3864 if (vals == NULL) {
3865 goto out;
3868 *value = (uint32)atol(vals[0]);
3870 ntstatus = NT_STATUS_OK;
3872 out:
3873 if (vals)
3874 ldap_value_free(vals);
3875 ldap_msgfree(result);
3877 return ntstatus;
3880 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3882 - if user hasn't decided to use account policies inside LDAP just reuse the
3883 old tdb values
3885 - if there is a valid cache entry, return that
3886 - if there is an LDAP entry, update cache and return
3887 - otherwise set to default, update cache and return
3889 Guenther
3891 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
3892 int policy_index, uint32 *value)
3894 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3896 if (cache_account_policy_get(policy_index, value)) {
3897 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3898 "cache\n"));
3899 return NT_STATUS_OK;
3902 ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
3903 value);
3904 if (NT_STATUS_IS_OK(ntstatus)) {
3905 goto update_cache;
3908 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3909 "ldap\n"));
3911 #if 0
3912 /* should we automagically migrate old tdb value here ? */
3913 if (account_policy_get(policy_index, value))
3914 goto update_ldap;
3916 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3917 "default\n", policy_index));
3918 #endif
3920 if (!account_policy_get_default(policy_index, value)) {
3921 return ntstatus;
3924 /* update_ldap: */
3926 ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
3927 if (!NT_STATUS_IS_OK(ntstatus)) {
3928 return ntstatus;
3931 update_cache:
3933 if (!cache_account_policy_set(policy_index, *value)) {
3934 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3935 "tdb as a cache\n"));
3936 return NT_STATUS_UNSUCCESSFUL;
3939 return NT_STATUS_OK;
3942 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
3943 const DOM_SID *domain_sid,
3944 int num_rids,
3945 uint32 *rids,
3946 const char **names,
3947 enum lsa_SidType *attrs)
3949 struct ldapsam_privates *ldap_state =
3950 (struct ldapsam_privates *)methods->private_data;
3951 LDAPMessage *msg = NULL;
3952 LDAPMessage *entry;
3953 char *allsids = NULL;
3954 int i, rc, num_mapped;
3955 NTSTATUS result = NT_STATUS_NO_MEMORY;
3956 TALLOC_CTX *mem_ctx;
3957 LDAP *ld;
3958 bool is_builtin;
3960 mem_ctx = talloc_new(NULL);
3961 if (mem_ctx == NULL) {
3962 DEBUG(0, ("talloc_new failed\n"));
3963 goto done;
3966 if (!sid_check_is_builtin(domain_sid) &&
3967 !sid_check_is_domain(domain_sid)) {
3968 result = NT_STATUS_INVALID_PARAMETER;
3969 goto done;
3972 for (i=0; i<num_rids; i++)
3973 attrs[i] = SID_NAME_UNKNOWN;
3975 allsids = talloc_strdup(mem_ctx, "");
3976 if (allsids == NULL) {
3977 goto done;
3980 for (i=0; i<num_rids; i++) {
3981 DOM_SID sid;
3982 sid_compose(&sid, domain_sid, rids[i]);
3983 allsids = talloc_asprintf_append_buffer(
3984 allsids, "(sambaSid=%s)",
3985 sid_string_talloc(mem_ctx, &sid));
3986 if (allsids == NULL) {
3987 goto done;
3991 /* First look for users */
3994 char *filter;
3995 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
3997 filter = talloc_asprintf(
3998 mem_ctx, ("(&(objectClass=%s)(|%s))"),
3999 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4001 if (filter == NULL) {
4002 goto done;
4005 rc = smbldap_search(ldap_state->smbldap_state,
4006 lp_ldap_user_suffix(),
4007 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4008 &msg);
4009 talloc_autofree_ldapmsg(mem_ctx, msg);
4012 if (rc != LDAP_SUCCESS)
4013 goto done;
4015 ld = ldap_state->smbldap_state->ldap_struct;
4016 num_mapped = 0;
4018 for (entry = ldap_first_entry(ld, msg);
4019 entry != NULL;
4020 entry = ldap_next_entry(ld, entry)) {
4021 uint32 rid;
4022 int rid_index;
4023 const char *name;
4025 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4026 &rid)) {
4027 DEBUG(2, ("Could not find sid from ldap entry\n"));
4028 continue;
4031 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4032 names);
4033 if (name == NULL) {
4034 DEBUG(2, ("Could not retrieve uid attribute\n"));
4035 continue;
4038 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4039 if (rid == rids[rid_index])
4040 break;
4043 if (rid_index == num_rids) {
4044 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4045 continue;
4048 attrs[rid_index] = SID_NAME_USER;
4049 names[rid_index] = name;
4050 num_mapped += 1;
4053 if (num_mapped == num_rids) {
4054 /* No need to look for groups anymore -- we're done */
4055 result = NT_STATUS_OK;
4056 goto done;
4059 /* Same game for groups */
4062 char *filter;
4063 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4064 "sambaGroupType", NULL };
4066 filter = talloc_asprintf(
4067 mem_ctx, "(&(objectClass=%s)(|%s))",
4068 LDAP_OBJ_GROUPMAP, allsids);
4069 if (filter == NULL) {
4070 goto done;
4073 rc = smbldap_search(ldap_state->smbldap_state,
4074 lp_ldap_group_suffix(),
4075 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4076 &msg);
4077 talloc_autofree_ldapmsg(mem_ctx, msg);
4080 if (rc != LDAP_SUCCESS)
4081 goto done;
4083 /* ldap_struct might have changed due to a reconnect */
4085 ld = ldap_state->smbldap_state->ldap_struct;
4087 /* For consistency checks, we already checked we're only domain or builtin */
4089 is_builtin = sid_check_is_builtin(domain_sid);
4091 for (entry = ldap_first_entry(ld, msg);
4092 entry != NULL;
4093 entry = ldap_next_entry(ld, entry))
4095 uint32 rid;
4096 int rid_index;
4097 const char *attr;
4098 enum lsa_SidType type;
4099 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4101 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4102 mem_ctx);
4103 if (attr == NULL) {
4104 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4105 dn));
4106 continue;
4109 type = (enum lsa_SidType)atol(attr);
4111 /* Consistency checks */
4112 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4113 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4114 (type != SID_NAME_DOM_GRP)))) {
4115 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4118 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4119 &rid)) {
4120 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4121 continue;
4124 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4126 if (attr == NULL) {
4127 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4128 dn));
4129 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4132 if (attr == NULL) {
4133 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4134 dn));
4135 continue;
4138 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4139 if (rid == rids[rid_index])
4140 break;
4143 if (rid_index == num_rids) {
4144 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4145 continue;
4148 attrs[rid_index] = type;
4149 names[rid_index] = attr;
4150 num_mapped += 1;
4153 result = NT_STATUS_NONE_MAPPED;
4155 if (num_mapped > 0)
4156 result = (num_mapped == num_rids) ?
4157 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4158 done:
4159 TALLOC_FREE(mem_ctx);
4160 return result;
4163 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4165 char *filter = NULL;
4166 char *escaped = NULL;
4167 char *result = NULL;
4169 asprintf(&filter, "(&%s(objectclass=sambaSamAccount))",
4170 "(uid=%u)");
4171 if (filter == NULL) goto done;
4173 escaped = escape_ldap_string_alloc(username);
4174 if (escaped == NULL) goto done;
4176 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4178 done:
4179 SAFE_FREE(filter);
4180 SAFE_FREE(escaped);
4182 return result;
4185 const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4187 int i, num = 0;
4188 va_list ap;
4189 const char **result;
4191 va_start(ap, mem_ctx);
4192 while (va_arg(ap, const char *) != NULL)
4193 num += 1;
4194 va_end(ap);
4196 if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
4197 return NULL;
4200 va_start(ap, mem_ctx);
4201 for (i=0; i<num; i++) {
4202 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4203 if (result[i] == NULL) {
4204 talloc_free(result);
4205 return NULL;
4208 va_end(ap);
4210 result[num] = NULL;
4211 return result;
4214 struct ldap_search_state {
4215 struct smbldap_state *connection;
4217 uint32 acct_flags;
4218 uint16 group_type;
4220 const char *base;
4221 int scope;
4222 const char *filter;
4223 const char **attrs;
4224 int attrsonly;
4225 void *pagedresults_cookie;
4227 LDAPMessage *entries, *current_entry;
4228 bool (*ldap2displayentry)(struct ldap_search_state *state,
4229 TALLOC_CTX *mem_ctx,
4230 LDAP *ld, LDAPMessage *entry,
4231 struct samr_displayentry *result);
4234 static bool ldapsam_search_firstpage(struct pdb_search *search)
4236 struct ldap_search_state *state =
4237 (struct ldap_search_state *)search->private_data;
4238 LDAP *ld;
4239 int rc = LDAP_OPERATIONS_ERROR;
4241 state->entries = NULL;
4243 if (state->connection->paged_results) {
4244 rc = smbldap_search_paged(state->connection, state->base,
4245 state->scope, state->filter,
4246 state->attrs, state->attrsonly,
4247 lp_ldap_page_size(), &state->entries,
4248 &state->pagedresults_cookie);
4251 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4253 if (state->entries != NULL) {
4254 /* Left over from unsuccessful paged attempt */
4255 ldap_msgfree(state->entries);
4256 state->entries = NULL;
4259 rc = smbldap_search(state->connection, state->base,
4260 state->scope, state->filter, state->attrs,
4261 state->attrsonly, &state->entries);
4263 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4264 return False;
4266 /* Ok, the server was lying. It told us it could do paged
4267 * searches when it could not. */
4268 state->connection->paged_results = False;
4271 ld = state->connection->ldap_struct;
4272 if ( ld == NULL) {
4273 DEBUG(5, ("Don't have an LDAP connection right after a "
4274 "search\n"));
4275 return False;
4277 state->current_entry = ldap_first_entry(ld, state->entries);
4279 if (state->current_entry == NULL) {
4280 ldap_msgfree(state->entries);
4281 state->entries = NULL;
4284 return True;
4287 static bool ldapsam_search_nextpage(struct pdb_search *search)
4289 struct ldap_search_state *state =
4290 (struct ldap_search_state *)search->private_data;
4291 int rc;
4293 if (!state->connection->paged_results) {
4294 /* There is no next page when there are no paged results */
4295 return False;
4298 rc = smbldap_search_paged(state->connection, state->base,
4299 state->scope, state->filter, state->attrs,
4300 state->attrsonly, lp_ldap_page_size(),
4301 &state->entries,
4302 &state->pagedresults_cookie);
4304 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4305 return False;
4307 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4309 if (state->current_entry == NULL) {
4310 ldap_msgfree(state->entries);
4311 state->entries = NULL;
4314 return True;
4317 static bool ldapsam_search_next_entry(struct pdb_search *search,
4318 struct samr_displayentry *entry)
4320 struct ldap_search_state *state =
4321 (struct ldap_search_state *)search->private_data;
4322 bool result;
4324 retry:
4325 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4326 return False;
4328 if ((state->entries == NULL) &&
4329 !ldapsam_search_nextpage(search))
4330 return False;
4332 result = state->ldap2displayentry(state, search->mem_ctx, state->connection->ldap_struct,
4333 state->current_entry, entry);
4335 if (!result) {
4336 char *dn;
4337 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4338 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4339 if (dn != NULL) ldap_memfree(dn);
4342 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4344 if (state->current_entry == NULL) {
4345 ldap_msgfree(state->entries);
4346 state->entries = NULL;
4349 if (!result) goto retry;
4351 return True;
4354 static void ldapsam_search_end(struct pdb_search *search)
4356 struct ldap_search_state *state =
4357 (struct ldap_search_state *)search->private_data;
4358 int rc;
4360 if (state->pagedresults_cookie == NULL)
4361 return;
4363 if (state->entries != NULL)
4364 ldap_msgfree(state->entries);
4366 state->entries = NULL;
4367 state->current_entry = NULL;
4369 if (!state->connection->paged_results)
4370 return;
4372 /* Tell the LDAP server we're not interested in the rest anymore. */
4374 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4375 state->filter, state->attrs,
4376 state->attrsonly, 0, &state->entries,
4377 &state->pagedresults_cookie);
4379 if (rc != LDAP_SUCCESS)
4380 DEBUG(5, ("Could not end search properly\n"));
4382 return;
4385 static bool ldapuser2displayentry(struct ldap_search_state *state,
4386 TALLOC_CTX *mem_ctx,
4387 LDAP *ld, LDAPMessage *entry,
4388 struct samr_displayentry *result)
4390 char **vals;
4391 DOM_SID sid;
4392 uint32 acct_flags;
4394 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4395 if ((vals == NULL) || (vals[0] == NULL)) {
4396 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4397 return False;
4399 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4400 ldap_value_free(vals);
4402 if ((state->acct_flags != 0) &&
4403 ((state->acct_flags & acct_flags) == 0))
4404 return False;
4406 result->acct_flags = acct_flags;
4407 result->account_name = "";
4408 result->fullname = "";
4409 result->description = "";
4411 vals = ldap_get_values(ld, entry, "uid");
4412 if ((vals == NULL) || (vals[0] == NULL)) {
4413 DEBUG(5, ("\"uid\" not found\n"));
4414 return False;
4416 pull_utf8_talloc(mem_ctx,
4417 CONST_DISCARD(char **, &result->account_name),
4418 vals[0]);
4419 ldap_value_free(vals);
4421 vals = ldap_get_values(ld, entry, "displayName");
4422 if ((vals == NULL) || (vals[0] == NULL))
4423 DEBUG(8, ("\"displayName\" not found\n"));
4424 else
4425 pull_utf8_talloc(mem_ctx,
4426 CONST_DISCARD(char **, &result->fullname),
4427 vals[0]);
4428 ldap_value_free(vals);
4430 vals = ldap_get_values(ld, entry, "description");
4431 if ((vals == NULL) || (vals[0] == NULL))
4432 DEBUG(8, ("\"description\" not found\n"));
4433 else
4434 pull_utf8_talloc(mem_ctx,
4435 CONST_DISCARD(char **, &result->description),
4436 vals[0]);
4437 ldap_value_free(vals);
4439 if ((result->account_name == NULL) ||
4440 (result->fullname == NULL) ||
4441 (result->description == NULL)) {
4442 DEBUG(0, ("talloc failed\n"));
4443 return False;
4446 vals = ldap_get_values(ld, entry, "sambaSid");
4447 if ((vals == NULL) || (vals[0] == NULL)) {
4448 DEBUG(0, ("\"objectSid\" not found\n"));
4449 return False;
4452 if (!string_to_sid(&sid, vals[0])) {
4453 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4454 ldap_value_free(vals);
4455 return False;
4457 ldap_value_free(vals);
4459 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4460 DEBUG(0, ("sid %s does not belong to our domain\n",
4461 sid_string_dbg(&sid)));
4462 return False;
4465 return True;
4469 static bool ldapsam_search_users(struct pdb_methods *methods,
4470 struct pdb_search *search,
4471 uint32 acct_flags)
4473 struct ldapsam_privates *ldap_state =
4474 (struct ldapsam_privates *)methods->private_data;
4475 struct ldap_search_state *state;
4477 state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
4478 if (state == NULL) {
4479 DEBUG(0, ("talloc failed\n"));
4480 return False;
4483 state->connection = ldap_state->smbldap_state;
4485 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4486 state->base = lp_ldap_user_suffix();
4487 else if ((acct_flags != 0) &&
4488 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4489 state->base = lp_ldap_machine_suffix();
4490 else
4491 state->base = lp_ldap_suffix();
4493 state->acct_flags = acct_flags;
4494 state->base = talloc_strdup(search->mem_ctx, state->base);
4495 state->scope = LDAP_SCOPE_SUBTREE;
4496 state->filter = get_ldap_filter(search->mem_ctx, "*");
4497 state->attrs = talloc_attrs(search->mem_ctx, "uid", "sambaSid",
4498 "displayName", "description",
4499 "sambaAcctFlags", NULL);
4500 state->attrsonly = 0;
4501 state->pagedresults_cookie = NULL;
4502 state->entries = NULL;
4503 state->ldap2displayentry = ldapuser2displayentry;
4505 if ((state->filter == NULL) || (state->attrs == NULL)) {
4506 DEBUG(0, ("talloc failed\n"));
4507 return False;
4510 search->private_data = state;
4511 search->next_entry = ldapsam_search_next_entry;
4512 search->search_end = ldapsam_search_end;
4514 return ldapsam_search_firstpage(search);
4517 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4518 TALLOC_CTX *mem_ctx,
4519 LDAP *ld, LDAPMessage *entry,
4520 struct samr_displayentry *result)
4522 char **vals;
4523 DOM_SID sid;
4524 uint16 group_type;
4526 result->account_name = "";
4527 result->fullname = "";
4528 result->description = "";
4531 vals = ldap_get_values(ld, entry, "sambaGroupType");
4532 if ((vals == NULL) || (vals[0] == NULL)) {
4533 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4534 if (vals != NULL) {
4535 ldap_value_free(vals);
4537 return False;
4540 group_type = atoi(vals[0]);
4542 if ((state->group_type != 0) &&
4543 ((state->group_type != group_type))) {
4544 ldap_value_free(vals);
4545 return False;
4548 ldap_value_free(vals);
4550 /* display name is the NT group name */
4552 vals = ldap_get_values(ld, entry, "displayName");
4553 if ((vals == NULL) || (vals[0] == NULL)) {
4554 DEBUG(8, ("\"displayName\" not found\n"));
4556 /* fallback to the 'cn' attribute */
4557 vals = ldap_get_values(ld, entry, "cn");
4558 if ((vals == NULL) || (vals[0] == NULL)) {
4559 DEBUG(5, ("\"cn\" not found\n"));
4560 return False;
4562 pull_utf8_talloc(mem_ctx,
4563 CONST_DISCARD(char **, &result->account_name),
4564 vals[0]);
4566 else {
4567 pull_utf8_talloc(mem_ctx,
4568 CONST_DISCARD(char **, &result->account_name),
4569 vals[0]);
4572 ldap_value_free(vals);
4574 vals = ldap_get_values(ld, entry, "description");
4575 if ((vals == NULL) || (vals[0] == NULL))
4576 DEBUG(8, ("\"description\" not found\n"));
4577 else
4578 pull_utf8_talloc(mem_ctx,
4579 CONST_DISCARD(char **, &result->description),
4580 vals[0]);
4581 ldap_value_free(vals);
4583 if ((result->account_name == NULL) ||
4584 (result->fullname == NULL) ||
4585 (result->description == NULL)) {
4586 DEBUG(0, ("talloc failed\n"));
4587 return False;
4590 vals = ldap_get_values(ld, entry, "sambaSid");
4591 if ((vals == NULL) || (vals[0] == NULL)) {
4592 DEBUG(0, ("\"objectSid\" not found\n"));
4593 if (vals != NULL) {
4594 ldap_value_free(vals);
4596 return False;
4599 if (!string_to_sid(&sid, vals[0])) {
4600 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4601 return False;
4604 ldap_value_free(vals);
4606 switch (group_type) {
4607 case SID_NAME_DOM_GRP:
4608 case SID_NAME_ALIAS:
4610 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4611 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4613 DEBUG(0, ("%s is not in our domain\n",
4614 sid_string_dbg(&sid)));
4615 return False;
4617 break;
4619 default:
4620 DEBUG(0,("unkown group type: %d\n", group_type));
4621 return False;
4624 return True;
4627 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4628 struct pdb_search *search,
4629 const DOM_SID *sid,
4630 enum lsa_SidType type)
4632 struct ldapsam_privates *ldap_state =
4633 (struct ldapsam_privates *)methods->private_data;
4634 struct ldap_search_state *state;
4635 fstring tmp;
4637 state = TALLOC_P(search->mem_ctx, 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 state->base = talloc_strdup(search->mem_ctx, lp_ldap_group_suffix());
4646 state->connection = ldap_state->smbldap_state;
4647 state->scope = LDAP_SCOPE_SUBTREE;
4648 state->filter = talloc_asprintf(search->mem_ctx,
4649 "(&(objectclass=sambaGroupMapping)"
4650 "(sambaGroupType=%d)(sambaSID=%s*))",
4651 type, sid_to_fstring(tmp, sid));
4652 state->attrs = talloc_attrs(search->mem_ctx, "cn", "sambaSid",
4653 "displayName", "description",
4654 "sambaGroupType", NULL);
4655 state->attrsonly = 0;
4656 state->pagedresults_cookie = NULL;
4657 state->entries = NULL;
4658 state->group_type = type;
4659 state->ldap2displayentry = ldapgroup2displayentry;
4661 if ((state->filter == NULL) || (state->attrs == NULL)) {
4662 DEBUG(0, ("talloc failed\n"));
4663 return False;
4666 search->private_data = state;
4667 search->next_entry = ldapsam_search_next_entry;
4668 search->search_end = ldapsam_search_end;
4670 return ldapsam_search_firstpage(search);
4673 static bool ldapsam_search_groups(struct pdb_methods *methods,
4674 struct pdb_search *search)
4676 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4679 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4680 struct pdb_search *search,
4681 const DOM_SID *sid)
4683 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4686 static bool ldapsam_rid_algorithm(struct pdb_methods *methods)
4688 return False;
4691 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4692 uint32 *rid)
4694 struct smbldap_state *smbldap_state = priv->smbldap_state;
4696 LDAPMessage *result = NULL;
4697 LDAPMessage *entry = NULL;
4698 LDAPMod **mods = NULL;
4699 NTSTATUS status;
4700 char *value;
4701 int rc;
4702 uint32 nextRid = 0;
4703 const char *dn;
4705 TALLOC_CTX *mem_ctx;
4707 mem_ctx = talloc_new(NULL);
4708 if (mem_ctx == NULL) {
4709 DEBUG(0, ("talloc_new failed\n"));
4710 return NT_STATUS_NO_MEMORY;
4713 status = smbldap_search_domain_info(smbldap_state, &result,
4714 get_global_sam_name(), False);
4715 if (!NT_STATUS_IS_OK(status)) {
4716 DEBUG(3, ("Could not get domain info: %s\n",
4717 nt_errstr(status)));
4718 goto done;
4721 talloc_autofree_ldapmsg(mem_ctx, result);
4723 entry = ldap_first_entry(priv2ld(priv), result);
4724 if (entry == NULL) {
4725 DEBUG(0, ("Could not get domain info entry\n"));
4726 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4727 goto done;
4730 /* Find the largest of the three attributes "sambaNextRid",
4731 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4732 concept of differentiating between user and group rids, and will
4733 use only "sambaNextRid" in the future. But for compatibility
4734 reasons I look if others have chosen different strategies -- VL */
4736 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4737 "sambaNextRid", mem_ctx);
4738 if (value != NULL) {
4739 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4740 nextRid = MAX(nextRid, tmp);
4743 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4744 "sambaNextUserRid", mem_ctx);
4745 if (value != NULL) {
4746 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4747 nextRid = MAX(nextRid, tmp);
4750 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4751 "sambaNextGroupRid", mem_ctx);
4752 if (value != NULL) {
4753 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4754 nextRid = MAX(nextRid, tmp);
4757 if (nextRid == 0) {
4758 nextRid = BASE_RID-1;
4761 nextRid += 1;
4763 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4764 talloc_asprintf(mem_ctx, "%d", nextRid));
4765 talloc_autofree_ldapmod(mem_ctx, mods);
4767 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4768 status = NT_STATUS_NO_MEMORY;
4769 goto done;
4772 rc = smbldap_modify(smbldap_state, dn, mods);
4774 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4775 * please retry" */
4777 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4779 done:
4780 if (NT_STATUS_IS_OK(status)) {
4781 *rid = nextRid;
4784 TALLOC_FREE(mem_ctx);
4785 return status;
4788 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32 *rid)
4790 int i;
4792 for (i=0; i<10; i++) {
4793 NTSTATUS result = ldapsam_get_new_rid(
4794 (struct ldapsam_privates *)methods->private_data, rid);
4795 if (NT_STATUS_IS_OK(result)) {
4796 return result;
4799 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4800 return result;
4803 /* The ldap update failed (maybe a race condition), retry */
4806 /* Tried 10 times, fail. */
4807 return NT_STATUS_ACCESS_DENIED;
4810 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32 *rid)
4812 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4813 return NT_STATUS_IS_OK(result) ? True : False;
4816 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4817 const DOM_SID *sid,
4818 union unid_t *id, enum lsa_SidType *type)
4820 struct ldapsam_privates *priv =
4821 (struct ldapsam_privates *)methods->private_data;
4822 char *filter;
4823 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4824 NULL };
4825 LDAPMessage *result = NULL;
4826 LDAPMessage *entry = NULL;
4827 bool ret = False;
4828 char *value;
4829 int rc;
4831 TALLOC_CTX *mem_ctx;
4833 mem_ctx = talloc_new(NULL);
4834 if (mem_ctx == NULL) {
4835 DEBUG(0, ("talloc_new failed\n"));
4836 return False;
4839 filter = talloc_asprintf(mem_ctx,
4840 "(&(sambaSid=%s)"
4841 "(|(objectClass=%s)(objectClass=%s)))",
4842 sid_string_talloc(mem_ctx, sid),
4843 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
4844 if (filter == NULL) {
4845 DEBUG(5, ("talloc_asprintf failed\n"));
4846 goto done;
4849 rc = smbldap_search_suffix(priv->smbldap_state, filter,
4850 attrs, &result);
4851 if (rc != LDAP_SUCCESS) {
4852 goto done;
4854 talloc_autofree_ldapmsg(mem_ctx, result);
4856 if (ldap_count_entries(priv2ld(priv), result) != 1) {
4857 DEBUG(10, ("Got %d entries, expected one\n",
4858 ldap_count_entries(priv2ld(priv), result)));
4859 goto done;
4862 entry = ldap_first_entry(priv2ld(priv), result);
4864 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4865 "sambaGroupType", mem_ctx);
4867 if (value != NULL) {
4868 const char *gid_str;
4869 /* It's a group */
4871 gid_str = smbldap_talloc_single_attribute(
4872 priv2ld(priv), entry, "gidNumber", mem_ctx);
4873 if (gid_str == NULL) {
4874 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4875 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
4876 entry)));
4877 goto done;
4880 id->gid = strtoul(gid_str, NULL, 10);
4881 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
4882 ret = True;
4883 goto done;
4886 /* It must be a user */
4888 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4889 "uidNumber", mem_ctx);
4890 if (value == NULL) {
4891 DEBUG(1, ("Could not find uidNumber in %s\n",
4892 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
4893 goto done;
4896 id->uid = strtoul(value, NULL, 10);
4897 *type = SID_NAME_USER;
4899 ret = True;
4900 done:
4901 TALLOC_FREE(mem_ctx);
4902 return ret;
4906 * The following functions is called only if
4907 * ldapsam:trusted and ldapsam:editposix are
4908 * set to true
4912 * ldapsam_create_user creates a new
4913 * posixAccount and sambaSamAccount object
4914 * in the ldap users subtree
4916 * The uid is allocated by winbindd.
4919 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
4920 TALLOC_CTX *tmp_ctx, const char *name,
4921 uint32 acb_info, uint32 *rid)
4923 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
4924 LDAPMessage *entry = NULL;
4925 LDAPMessage *result = NULL;
4926 uint32 num_result;
4927 bool is_machine = False;
4928 bool add_posix = False;
4929 LDAPMod **mods = NULL;
4930 struct samu *user;
4931 char *filter;
4932 char *username;
4933 char *homedir;
4934 char *gidstr;
4935 char *uidstr;
4936 char *shell;
4937 const char *dn = NULL;
4938 DOM_SID group_sid;
4939 DOM_SID user_sid;
4940 gid_t gid = -1;
4941 uid_t uid = -1;
4942 NTSTATUS ret;
4943 int rc;
4945 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
4946 acb_info & ACB_WSTRUST ||
4947 acb_info & ACB_SVRTRUST ||
4948 acb_info & ACB_DOMTRUST) {
4949 is_machine = True;
4952 username = escape_ldap_string_alloc(name);
4953 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
4954 username, LDAP_OBJ_POSIXACCOUNT);
4955 SAFE_FREE(username);
4957 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
4958 if (rc != LDAP_SUCCESS) {
4959 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
4960 return NT_STATUS_UNSUCCESSFUL;
4962 talloc_autofree_ldapmsg(tmp_ctx, result);
4964 num_result = ldap_count_entries(priv2ld(ldap_state), result);
4966 if (num_result > 1) {
4967 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
4968 return NT_STATUS_INTERNAL_DB_CORRUPTION;
4971 if (num_result == 1) {
4972 char *tmp;
4973 /* check if it is just a posix account.
4974 * or if there is a sid attached to this entry
4977 entry = ldap_first_entry(priv2ld(ldap_state), result);
4978 if (!entry) {
4979 return NT_STATUS_UNSUCCESSFUL;
4982 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
4983 if (tmp) {
4984 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
4985 return NT_STATUS_USER_EXISTS;
4988 /* it is just a posix account, retrieve the dn for later use */
4989 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
4990 if (!dn) {
4991 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
4992 return NT_STATUS_NO_MEMORY;
4996 if (num_result == 0) {
4997 add_posix = True;
5000 /* Create the basic samu structure and generate the mods for the ldap commit */
5001 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5002 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5003 return ret;
5006 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5008 user = samu_new(tmp_ctx);
5009 if (!user) {
5010 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5011 return NT_STATUS_NO_MEMORY;
5014 if (!pdb_set_username(user, name, PDB_SET)) {
5015 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5016 return NT_STATUS_UNSUCCESSFUL;
5018 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5019 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5020 return NT_STATUS_UNSUCCESSFUL;
5022 if (is_machine) {
5023 if (acb_info & ACB_NORMAL) {
5024 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5025 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5026 return NT_STATUS_UNSUCCESSFUL;
5028 } else {
5029 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5030 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5031 return NT_STATUS_UNSUCCESSFUL;
5034 } else {
5035 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5036 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5037 return NT_STATUS_UNSUCCESSFUL;
5041 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5042 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5043 return NT_STATUS_UNSUCCESSFUL;
5046 if (!init_ldap_from_sam(ldap_state, NULL, &mods, user, element_is_set_or_changed)) {
5047 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5048 return NT_STATUS_UNSUCCESSFUL;
5051 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5052 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5054 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5056 if (add_posix) {
5057 char *escape_name;
5059 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5061 /* retrieve the Domain Users group gid */
5062 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS) ||
5063 !sid_to_gid(&group_sid, &gid)) {
5064 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5065 return NT_STATUS_INVALID_PRIMARY_GROUP;
5068 /* lets allocate a new userid for this user */
5069 if (!winbind_allocate_uid(&uid)) {
5070 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5071 return NT_STATUS_UNSUCCESSFUL;
5075 if (is_machine) {
5076 /* TODO: choose a more appropriate default for machines */
5077 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5078 shell = talloc_strdup(tmp_ctx, "/bin/false");
5079 } else {
5080 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5081 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5083 uidstr = talloc_asprintf(tmp_ctx, "%d", uid);
5084 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
5086 escape_name = escape_rdn_val_string_alloc(name);
5087 if (!escape_name) {
5088 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5089 return NT_STATUS_NO_MEMORY;
5092 if (is_machine) {
5093 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5094 } else {
5095 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5098 SAFE_FREE(escape_name);
5100 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5101 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5102 return NT_STATUS_NO_MEMORY;
5105 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5106 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5107 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5108 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5109 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5110 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5111 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5114 talloc_autofree_ldapmod(tmp_ctx, mods);
5116 if (add_posix) {
5117 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5118 } else {
5119 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5122 if (rc != LDAP_SUCCESS) {
5123 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5124 return NT_STATUS_UNSUCCESSFUL;
5127 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5129 flush_pwnam_cache();
5131 return NT_STATUS_OK;
5134 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5136 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5137 LDAPMessage *result = NULL;
5138 LDAPMessage *entry = NULL;
5139 int num_result;
5140 const char *dn;
5141 char *filter;
5142 int rc;
5144 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5146 filter = talloc_asprintf(tmp_ctx,
5147 "(&(uid=%s)"
5148 "(objectClass=%s)"
5149 "(objectClass=%s))",
5150 pdb_get_username(sam_acct),
5151 LDAP_OBJ_POSIXACCOUNT,
5152 LDAP_OBJ_SAMBASAMACCOUNT);
5153 if (filter == NULL) {
5154 return NT_STATUS_NO_MEMORY;
5157 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5158 if (rc != LDAP_SUCCESS) {
5159 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5160 return NT_STATUS_UNSUCCESSFUL;
5162 talloc_autofree_ldapmsg(tmp_ctx, result);
5164 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5166 if (num_result == 0) {
5167 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5168 return NT_STATUS_NO_SUCH_USER;
5171 if (num_result > 1) {
5172 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5173 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5176 entry = ldap_first_entry(priv2ld(ldap_state), result);
5177 if (!entry) {
5178 return NT_STATUS_UNSUCCESSFUL;
5181 /* it is just a posix account, retrieve the dn for later use */
5182 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5183 if (!dn) {
5184 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5185 return NT_STATUS_NO_MEMORY;
5188 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5189 if (rc != LDAP_SUCCESS) {
5190 return NT_STATUS_UNSUCCESSFUL;
5193 flush_pwnam_cache();
5195 return NT_STATUS_OK;
5199 * ldapsam_create_group creates a new
5200 * posixGroup and sambaGroupMapping object
5201 * in the ldap groups subtree
5203 * The gid is allocated by winbindd.
5206 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5207 TALLOC_CTX *tmp_ctx,
5208 const char *name,
5209 uint32 *rid)
5211 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5212 NTSTATUS ret;
5213 LDAPMessage *entry = NULL;
5214 LDAPMessage *result = NULL;
5215 uint32 num_result;
5216 bool is_new_entry = False;
5217 LDAPMod **mods = NULL;
5218 char *filter;
5219 char *groupsidstr;
5220 char *groupname;
5221 char *grouptype;
5222 char *gidstr;
5223 const char *dn = NULL;
5224 DOM_SID group_sid;
5225 gid_t gid = -1;
5226 int rc;
5228 groupname = escape_ldap_string_alloc(name);
5229 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5230 groupname, LDAP_OBJ_POSIXGROUP);
5231 SAFE_FREE(groupname);
5233 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5234 if (rc != LDAP_SUCCESS) {
5235 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5236 return NT_STATUS_UNSUCCESSFUL;
5238 talloc_autofree_ldapmsg(tmp_ctx, result);
5240 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5242 if (num_result > 1) {
5243 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5244 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5247 if (num_result == 1) {
5248 char *tmp;
5249 /* check if it is just a posix group.
5250 * or if there is a sid attached to this entry
5253 entry = ldap_first_entry(priv2ld(ldap_state), result);
5254 if (!entry) {
5255 return NT_STATUS_UNSUCCESSFUL;
5258 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5259 if (tmp) {
5260 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5261 return NT_STATUS_GROUP_EXISTS;
5264 /* it is just a posix group, retrieve the gid and the dn for later use */
5265 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5266 if (!tmp) {
5267 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5268 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5271 gid = strtoul(tmp, NULL, 10);
5273 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5274 if (!dn) {
5275 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5276 return NT_STATUS_NO_MEMORY;
5280 if (num_result == 0) {
5281 char *escape_name;
5283 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5285 is_new_entry = True;
5287 /* lets allocate a new groupid for this group */
5288 if (!winbind_allocate_gid(&gid)) {
5289 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5290 return NT_STATUS_UNSUCCESSFUL;
5293 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
5295 escape_name = escape_rdn_val_string_alloc(name);
5296 if (!escape_name) {
5297 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5298 return NT_STATUS_NO_MEMORY;
5301 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5303 SAFE_FREE(escape_name);
5305 if (!gidstr || !dn) {
5306 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5307 return NT_STATUS_NO_MEMORY;
5310 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5311 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5312 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5315 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5316 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5317 return ret;
5320 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5322 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5323 &group_sid));
5324 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5326 if (!groupsidstr || !grouptype) {
5327 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5328 return NT_STATUS_NO_MEMORY;
5331 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5332 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5333 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5334 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5335 talloc_autofree_ldapmod(tmp_ctx, mods);
5337 if (is_new_entry) {
5338 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5339 #if 0
5340 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5341 /* This call may fail with rfc2307bis schema */
5342 /* Retry adding a structural class */
5343 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5344 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5346 #endif
5347 } else {
5348 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5351 if (rc != LDAP_SUCCESS) {
5352 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5353 return NT_STATUS_UNSUCCESSFUL;
5356 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5358 return NT_STATUS_OK;
5361 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32 rid)
5363 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5364 LDAPMessage *result = NULL;
5365 LDAPMessage *entry = NULL;
5366 int num_result;
5367 const char *dn;
5368 char *gidstr;
5369 char *filter;
5370 DOM_SID group_sid;
5371 int rc;
5373 /* get the group sid */
5374 sid_compose(&group_sid, get_global_sam_sid(), rid);
5376 filter = talloc_asprintf(tmp_ctx,
5377 "(&(sambaSID=%s)"
5378 "(objectClass=%s)"
5379 "(objectClass=%s))",
5380 sid_string_talloc(tmp_ctx, &group_sid),
5381 LDAP_OBJ_POSIXGROUP,
5382 LDAP_OBJ_GROUPMAP);
5383 if (filter == NULL) {
5384 return NT_STATUS_NO_MEMORY;
5387 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5388 if (rc != LDAP_SUCCESS) {
5389 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5390 return NT_STATUS_UNSUCCESSFUL;
5392 talloc_autofree_ldapmsg(tmp_ctx, result);
5394 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5396 if (num_result == 0) {
5397 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5398 return NT_STATUS_NO_SUCH_GROUP;
5401 if (num_result > 1) {
5402 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5403 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5406 entry = ldap_first_entry(priv2ld(ldap_state), result);
5407 if (!entry) {
5408 return NT_STATUS_UNSUCCESSFUL;
5411 /* here it is, retrieve the dn for later use */
5412 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5413 if (!dn) {
5414 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5415 return NT_STATUS_NO_MEMORY;
5418 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5419 if (!gidstr) {
5420 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5421 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5424 /* check no user have this group marked as primary group */
5425 filter = talloc_asprintf(tmp_ctx,
5426 "(&(gidNumber=%s)"
5427 "(objectClass=%s)"
5428 "(objectClass=%s))",
5429 gidstr,
5430 LDAP_OBJ_POSIXACCOUNT,
5431 LDAP_OBJ_SAMBASAMACCOUNT);
5433 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5434 if (rc != LDAP_SUCCESS) {
5435 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5436 return NT_STATUS_UNSUCCESSFUL;
5438 talloc_autofree_ldapmsg(tmp_ctx, result);
5440 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5442 if (num_result != 0) {
5443 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5444 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5447 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5448 if (rc != LDAP_SUCCESS) {
5449 return NT_STATUS_UNSUCCESSFUL;
5452 return NT_STATUS_OK;
5455 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5456 TALLOC_CTX *tmp_ctx,
5457 uint32 group_rid,
5458 uint32 member_rid,
5459 int modop)
5461 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5462 LDAPMessage *entry = NULL;
5463 LDAPMessage *result = NULL;
5464 uint32 num_result;
5465 LDAPMod **mods = NULL;
5466 char *filter;
5467 char *uidstr;
5468 const char *dn = NULL;
5469 DOM_SID group_sid;
5470 DOM_SID member_sid;
5471 int rc;
5473 switch (modop) {
5474 case LDAP_MOD_ADD:
5475 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5476 break;
5477 case LDAP_MOD_DELETE:
5478 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5479 break;
5480 default:
5481 return NT_STATUS_UNSUCCESSFUL;
5484 /* get member sid */
5485 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5487 /* get the group sid */
5488 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5490 filter = talloc_asprintf(tmp_ctx,
5491 "(&(sambaSID=%s)"
5492 "(objectClass=%s)"
5493 "(objectClass=%s))",
5494 sid_string_talloc(tmp_ctx, &member_sid),
5495 LDAP_OBJ_POSIXACCOUNT,
5496 LDAP_OBJ_SAMBASAMACCOUNT);
5497 if (filter == NULL) {
5498 return NT_STATUS_NO_MEMORY;
5501 /* get the member uid */
5502 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5503 if (rc != LDAP_SUCCESS) {
5504 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5505 return NT_STATUS_UNSUCCESSFUL;
5507 talloc_autofree_ldapmsg(tmp_ctx, result);
5509 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5511 if (num_result == 0) {
5512 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5513 return NT_STATUS_NO_SUCH_MEMBER;
5516 if (num_result > 1) {
5517 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5518 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5521 entry = ldap_first_entry(priv2ld(ldap_state), result);
5522 if (!entry) {
5523 return NT_STATUS_UNSUCCESSFUL;
5526 if (modop == LDAP_MOD_DELETE) {
5527 /* check if we are trying to remove the member from his primary group */
5528 char *gidstr;
5529 gid_t user_gid, group_gid;
5531 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5532 if (!gidstr) {
5533 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5534 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5537 user_gid = strtoul(gidstr, NULL, 10);
5539 if (!sid_to_gid(&group_sid, &group_gid)) {
5540 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5541 return NT_STATUS_UNSUCCESSFUL;
5544 if (user_gid == group_gid) {
5545 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5546 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5550 /* here it is, retrieve the uid for later use */
5551 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5552 if (!uidstr) {
5553 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5554 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5557 filter = talloc_asprintf(tmp_ctx,
5558 "(&(sambaSID=%s)"
5559 "(objectClass=%s)"
5560 "(objectClass=%s))",
5561 sid_string_talloc(tmp_ctx, &group_sid),
5562 LDAP_OBJ_POSIXGROUP,
5563 LDAP_OBJ_GROUPMAP);
5565 /* get the group */
5566 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5567 if (rc != LDAP_SUCCESS) {
5568 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5569 return NT_STATUS_UNSUCCESSFUL;
5571 talloc_autofree_ldapmsg(tmp_ctx, result);
5573 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5575 if (num_result == 0) {
5576 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5577 return NT_STATUS_NO_SUCH_GROUP;
5580 if (num_result > 1) {
5581 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5582 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5585 entry = ldap_first_entry(priv2ld(ldap_state), result);
5586 if (!entry) {
5587 return NT_STATUS_UNSUCCESSFUL;
5590 /* here it is, retrieve the dn for later use */
5591 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5592 if (!dn) {
5593 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5594 return NT_STATUS_NO_MEMORY;
5597 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5599 talloc_autofree_ldapmod(tmp_ctx, mods);
5601 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5602 if (rc != LDAP_SUCCESS) {
5603 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5604 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5605 return NT_STATUS_MEMBER_IN_GROUP;
5607 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5608 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5609 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5611 return NT_STATUS_UNSUCCESSFUL;
5614 return NT_STATUS_OK;
5617 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5618 TALLOC_CTX *tmp_ctx,
5619 uint32 group_rid,
5620 uint32 member_rid)
5622 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5624 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5625 TALLOC_CTX *tmp_ctx,
5626 uint32 group_rid,
5627 uint32 member_rid)
5629 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5632 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5633 TALLOC_CTX *mem_ctx,
5634 struct samu *sampass)
5636 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5637 LDAPMessage *entry = NULL;
5638 LDAPMessage *result = NULL;
5639 uint32 num_result;
5640 LDAPMod **mods = NULL;
5641 char *filter;
5642 char *escape_username;
5643 char *gidstr;
5644 const char *dn = NULL;
5645 gid_t gid;
5646 int rc;
5648 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5650 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5651 DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
5652 return NT_STATUS_UNSUCCESSFUL;
5654 gidstr = talloc_asprintf(mem_ctx, "%d", gid);
5655 if (!gidstr) {
5656 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5657 return NT_STATUS_NO_MEMORY;
5660 escape_username = escape_ldap_string_alloc(pdb_get_username(sampass));
5661 if (escape_username== NULL) {
5662 return NT_STATUS_NO_MEMORY;
5665 filter = talloc_asprintf(mem_ctx,
5666 "(&(uid=%s)"
5667 "(objectClass=%s)"
5668 "(objectClass=%s))",
5669 escape_username,
5670 LDAP_OBJ_POSIXACCOUNT,
5671 LDAP_OBJ_SAMBASAMACCOUNT);
5673 SAFE_FREE(escape_username);
5675 if (filter == NULL) {
5676 return NT_STATUS_NO_MEMORY;
5679 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5680 if (rc != LDAP_SUCCESS) {
5681 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5682 return NT_STATUS_UNSUCCESSFUL;
5684 talloc_autofree_ldapmsg(mem_ctx, result);
5686 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5688 if (num_result == 0) {
5689 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5690 return NT_STATUS_NO_SUCH_USER;
5693 if (num_result > 1) {
5694 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
5695 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5698 entry = ldap_first_entry(priv2ld(ldap_state), result);
5699 if (!entry) {
5700 return NT_STATUS_UNSUCCESSFUL;
5703 /* retrieve the dn for later use */
5704 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
5705 if (!dn) {
5706 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5707 return NT_STATUS_NO_MEMORY;
5710 /* remove the old one, and add the new one, this way we do not risk races */
5711 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
5713 if (mods == NULL) {
5714 return NT_STATUS_OK;
5717 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5719 if (rc != LDAP_SUCCESS) {
5720 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5721 pdb_get_username(sampass), gidstr));
5722 return NT_STATUS_UNSUCCESSFUL;
5725 flush_pwnam_cache();
5727 return NT_STATUS_OK;
5731 /**********************************************************************
5732 trusted domains functions
5733 *********************************************************************/
5735 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
5736 const char *domain)
5738 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
5739 ldap_state->domain_dn);
5742 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
5743 const char *domain, LDAPMessage **entry)
5745 int rc;
5746 char *filter;
5747 int scope = LDAP_SCOPE_SUBTREE;
5748 const char **attrs = NULL; /* NULL: get all attrs */
5749 int attrsonly = 0; /* 0: return values too */
5750 LDAPMessage *result = NULL;
5751 char *trusted_dn;
5752 uint32 num_result;
5754 filter = talloc_asprintf(talloc_tos(),
5755 "(&(objectClass=%s)(sambaDomainName=%s))",
5756 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
5758 trusted_dn = trusteddom_dn(ldap_state, domain);
5759 if (trusted_dn == NULL) {
5760 return False;
5762 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
5763 filter, attrs, attrsonly, &result);
5765 if (rc == LDAP_NO_SUCH_OBJECT) {
5766 *entry = NULL;
5767 return True;
5770 if (rc != LDAP_SUCCESS) {
5771 return False;
5774 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5776 if (num_result > 1) {
5777 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5778 "sambaTrustedDomainPassword object for domain '%s'"
5779 "?!\n", domain));
5780 return False;
5783 if (num_result == 0) {
5784 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5785 "sambaTrustedDomainPassword object for domain %s.\n",
5786 domain));
5787 *entry = NULL;
5788 } else {
5789 *entry = ldap_first_entry(priv2ld(ldap_state), result);
5792 return True;
5795 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
5796 const char *domain,
5797 char** pwd,
5798 DOM_SID *sid,
5799 time_t *pass_last_set_time)
5801 struct ldapsam_privates *ldap_state =
5802 (struct ldapsam_privates *)methods->private_data;
5803 LDAPMessage *entry = NULL;
5805 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
5807 if (!get_trusteddom_pw_int(ldap_state, domain, &entry) ||
5808 (entry == NULL))
5810 return False;
5813 /* password */
5814 if (pwd != NULL) {
5815 char *pwd_str;
5816 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5817 entry, "sambaClearTextPassword", talloc_tos());
5818 if (pwd_str == NULL) {
5819 return False;
5821 /* trusteddom_pw routines do not use talloc yet... */
5822 *pwd = SMB_STRDUP(pwd_str);
5823 if (*pwd == NULL) {
5824 return False;
5828 /* last change time */
5829 if (pass_last_set_time != NULL) {
5830 char *time_str;
5831 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5832 entry, "sambaPwdLastSet", talloc_tos());
5833 if (time_str == NULL) {
5834 return False;
5836 *pass_last_set_time = (time_t)atol(time_str);
5839 /* domain sid */
5840 if (sid != NULL) {
5841 char *sid_str;
5842 DOM_SID *dom_sid;
5843 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5844 entry, "sambaSID",
5845 talloc_tos());
5846 if (sid_str == NULL) {
5847 return False;
5849 dom_sid = string_sid_talloc(talloc_tos(), sid_str);
5850 if (dom_sid == NULL) {
5851 return False;
5853 sid_copy(sid, dom_sid);
5856 return True;
5859 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
5860 const char* domain,
5861 const char* pwd,
5862 const DOM_SID *sid)
5864 struct ldapsam_privates *ldap_state =
5865 (struct ldapsam_privates *)methods->private_data;
5866 LDAPMessage *entry = NULL;
5867 LDAPMod **mods = NULL;
5868 char *prev_pwd = NULL;
5869 char *trusted_dn = NULL;
5870 int rc;
5872 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
5875 * get the current entry (if there is one) in order to put the
5876 * current password into the previous password attribute
5878 if (!get_trusteddom_pw_int(ldap_state, domain, &entry)) {
5879 return False;
5882 mods = NULL;
5883 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
5884 "sambaTrustedDomainPassword");
5885 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
5886 domain);
5887 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
5888 sid_string_tos(sid));
5889 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
5890 talloc_asprintf(talloc_tos(), "%li", time(NULL)));
5891 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5892 "sambaClearTextPassword", pwd);
5893 if (entry != NULL) {
5894 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5895 entry, "sambaClearTextPassword", talloc_tos());
5896 if (prev_pwd != NULL) {
5897 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5898 "sambaPreviousClearTextPassword",
5899 prev_pwd);
5903 trusted_dn = trusteddom_dn(ldap_state, domain);
5904 if (trusted_dn == NULL) {
5905 return False;
5907 if (entry == NULL) {
5908 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
5909 } else {
5910 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
5913 if (rc != LDAP_SUCCESS) {
5914 DEBUG(1, ("error writing trusted domain password!\n"));
5915 return False;
5918 return True;
5921 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
5922 const char *domain)
5924 int rc;
5925 struct ldapsam_privates *ldap_state =
5926 (struct ldapsam_privates *)methods->private_data;
5927 LDAPMessage *entry = NULL;
5928 const char *trusted_dn;
5930 if (!get_trusteddom_pw_int(ldap_state, domain, &entry)) {
5931 return False;
5934 if (entry == NULL) {
5935 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
5936 "%s\n", domain));
5937 return True;
5940 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
5941 entry);
5942 if (trusted_dn == NULL) {
5943 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
5944 return False;
5947 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
5948 if (rc != LDAP_SUCCESS) {
5949 return False;
5952 return True;
5955 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
5956 TALLOC_CTX *mem_ctx,
5957 uint32 *num_domains,
5958 struct trustdom_info ***domains)
5960 int rc;
5961 struct ldapsam_privates *ldap_state =
5962 (struct ldapsam_privates *)methods->private_data;
5963 char *filter;
5964 int scope = LDAP_SCOPE_SUBTREE;
5965 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
5966 int attrsonly = 0; /* 0: return values too */
5967 LDAPMessage *result = NULL;
5968 LDAPMessage *entry = NULL;
5970 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
5971 LDAP_OBJ_TRUSTDOM_PASSWORD);
5973 rc = smbldap_search(ldap_state->smbldap_state,
5974 ldap_state->domain_dn,
5975 scope,
5976 filter,
5977 attrs,
5978 attrsonly,
5979 &result);
5981 if (rc != LDAP_SUCCESS) {
5982 return NT_STATUS_UNSUCCESSFUL;
5985 *num_domains = 0;
5986 if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
5987 DEBUG(1, ("talloc failed\n"));
5988 return NT_STATUS_NO_MEMORY;
5991 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
5992 entry != NULL;
5993 entry = ldap_next_entry(priv2ld(ldap_state), entry))
5995 char *dom_name, *dom_sid_str;
5996 struct trustdom_info *dom_info;
5998 dom_info = TALLOC_P(*domains, struct trustdom_info);
5999 if (dom_info == NULL) {
6000 DEBUG(1, ("talloc failed\n"));
6001 return NT_STATUS_NO_MEMORY;
6004 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6005 entry,
6006 "sambaDomainName",
6007 talloc_tos());
6008 if (dom_name == NULL) {
6009 DEBUG(1, ("talloc failed\n"));
6010 return NT_STATUS_NO_MEMORY;
6012 dom_info->name = dom_name;
6014 dom_sid_str = smbldap_talloc_single_attribute(
6015 priv2ld(ldap_state), entry, "sambaSID",
6016 talloc_tos());
6017 if (dom_sid_str == NULL) {
6018 DEBUG(1, ("talloc failed\n"));
6019 return NT_STATUS_NO_MEMORY;
6021 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6022 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6023 dom_sid_str));
6024 return NT_STATUS_UNSUCCESSFUL;
6027 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6028 domains, num_domains);
6030 if (*domains == NULL) {
6031 DEBUG(1, ("talloc failed\n"));
6032 return NT_STATUS_NO_MEMORY;
6036 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6037 return NT_STATUS_OK;
6041 /**********************************************************************
6042 Housekeeping
6043 *********************************************************************/
6045 static void free_private_data(void **vp)
6047 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6049 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6051 if ((*ldap_state)->result != NULL) {
6052 ldap_msgfree((*ldap_state)->result);
6053 (*ldap_state)->result = NULL;
6055 if ((*ldap_state)->domain_dn != NULL) {
6056 SAFE_FREE((*ldap_state)->domain_dn);
6059 *ldap_state = NULL;
6061 /* No need to free any further, as it is talloc()ed */
6064 /*********************************************************************
6065 Intitalise the parts of the pdb_methods structure that are common to
6066 all pdb_ldap modes
6067 *********************************************************************/
6069 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6071 NTSTATUS nt_status;
6072 struct ldapsam_privates *ldap_state;
6074 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6075 return nt_status;
6078 (*pdb_method)->name = "ldapsam";
6080 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6081 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6082 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6083 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6084 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6085 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6087 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6088 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6089 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6090 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6091 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6092 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6093 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6095 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6096 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6098 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6100 (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
6101 (*pdb_method)->new_rid = ldapsam_new_rid;
6103 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6104 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6105 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6106 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6108 /* TODO: Setup private data and free */
6110 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
6111 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6112 return NT_STATUS_NO_MEMORY;
6115 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6116 location, &ldap_state->smbldap_state);
6118 if ( !NT_STATUS_IS_OK(nt_status) ) {
6119 return nt_status;
6122 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6123 return NT_STATUS_NO_MEMORY;
6126 (*pdb_method)->private_data = ldap_state;
6128 (*pdb_method)->free_private_data = free_private_data;
6130 return NT_STATUS_OK;
6133 /**********************************************************************
6134 Initialise the 'compat' mode for pdb_ldap
6135 *********************************************************************/
6137 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6139 NTSTATUS nt_status;
6140 struct ldapsam_privates *ldap_state;
6141 char *uri = talloc_strdup( NULL, location );
6143 trim_char( uri, '\"', '\"' );
6144 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6145 if ( uri )
6146 TALLOC_FREE( uri );
6148 if ( !NT_STATUS_IS_OK(nt_status) ) {
6149 return nt_status;
6152 (*pdb_method)->name = "ldapsam_compat";
6154 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6155 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6157 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6159 return NT_STATUS_OK;
6162 /**********************************************************************
6163 Initialise the normal mode for pdb_ldap
6164 *********************************************************************/
6166 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6168 NTSTATUS nt_status;
6169 struct ldapsam_privates *ldap_state = NULL;
6170 uint32 alg_rid_base;
6171 char *alg_rid_base_string = NULL;
6172 LDAPMessage *result = NULL;
6173 LDAPMessage *entry = NULL;
6174 DOM_SID ldap_domain_sid;
6175 DOM_SID secrets_domain_sid;
6176 char *domain_sid_string = NULL;
6177 char *dn = NULL;
6178 char *uri = talloc_strdup( NULL, location );
6180 trim_char( uri, '\"', '\"' );
6181 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6182 if (uri) {
6183 TALLOC_FREE(uri);
6186 if (!NT_STATUS_IS_OK(nt_status)) {
6187 return nt_status;
6190 (*pdb_method)->name = "ldapsam";
6192 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6193 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6194 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6195 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6196 (*pdb_method)->search_users = ldapsam_search_users;
6197 (*pdb_method)->search_groups = ldapsam_search_groups;
6198 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6200 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6201 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6202 (*pdb_method)->enum_group_memberships =
6203 ldapsam_enum_group_memberships;
6204 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6205 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6207 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6208 (*pdb_method)->create_user = ldapsam_create_user;
6209 (*pdb_method)->delete_user = ldapsam_delete_user;
6210 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6211 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6212 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6213 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6214 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6218 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6219 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6221 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6223 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6224 &result,
6225 ldap_state->domain_name, True);
6227 if ( !NT_STATUS_IS_OK(nt_status) ) {
6228 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6229 "info, nor add one to the domain\n"));
6230 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6231 "will be unable to allocate new users/groups, "
6232 "and will risk BDCs having inconsistant SIDs\n"));
6233 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6234 return NT_STATUS_OK;
6237 /* Given that the above might fail, everything below this must be
6238 * optional */
6240 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6241 result);
6242 if (!entry) {
6243 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6244 "entry\n"));
6245 ldap_msgfree(result);
6246 return NT_STATUS_UNSUCCESSFUL;
6249 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
6250 if (!dn) {
6251 ldap_msgfree(result);
6252 return NT_STATUS_UNSUCCESSFUL;
6255 ldap_state->domain_dn = smb_xstrdup(dn);
6256 ldap_memfree(dn);
6258 domain_sid_string = smbldap_talloc_single_attribute(
6259 ldap_state->smbldap_state->ldap_struct,
6260 entry,
6261 get_userattr_key2string(ldap_state->schema_ver,
6262 LDAP_ATTR_USER_SID),
6263 NULL);
6265 if (domain_sid_string) {
6266 bool found_sid;
6267 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6268 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6269 "read as a valid SID\n", domain_sid_string));
6270 ldap_msgfree(result);
6271 TALLOC_FREE(domain_sid_string);
6272 return NT_STATUS_INVALID_PARAMETER;
6274 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6275 &secrets_domain_sid);
6276 if (!found_sid || !sid_equal(&secrets_domain_sid,
6277 &ldap_domain_sid)) {
6278 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6279 "%s based on pdb_ldap results %s -> %s\n",
6280 ldap_state->domain_name,
6281 sid_string_dbg(&secrets_domain_sid),
6282 sid_string_dbg(&ldap_domain_sid)));
6284 /* reset secrets.tdb sid */
6285 secrets_store_domain_sid(ldap_state->domain_name,
6286 &ldap_domain_sid);
6287 DEBUG(1, ("New global sam SID: %s\n",
6288 sid_string_dbg(get_global_sam_sid())));
6290 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6291 TALLOC_FREE(domain_sid_string);
6294 alg_rid_base_string = smbldap_talloc_single_attribute(
6295 ldap_state->smbldap_state->ldap_struct,
6296 entry,
6297 get_attr_key2string( dominfo_attr_list,
6298 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6299 NULL);
6300 if (alg_rid_base_string) {
6301 alg_rid_base = (uint32)atol(alg_rid_base_string);
6302 if (alg_rid_base != algorithmic_rid_base()) {
6303 DEBUG(0, ("The value of 'algorithmic RID base' has "
6304 "changed since the LDAP\n"
6305 "database was initialised. Aborting. \n"));
6306 ldap_msgfree(result);
6307 TALLOC_FREE(alg_rid_base_string);
6308 return NT_STATUS_UNSUCCESSFUL;
6310 TALLOC_FREE(alg_rid_base_string);
6312 ldap_msgfree(result);
6314 return NT_STATUS_OK;
6317 NTSTATUS pdb_ldap_init(void)
6319 NTSTATUS nt_status;
6320 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6321 return nt_status;
6323 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6324 return nt_status;
6326 /* Let pdb_nds register backends */
6327 pdb_nds_init();
6329 return NT_STATUS_OK;