Fix bug #6431 - local groups from 3.0 setups no longer found.
[Samba.git] / source3 / passdb / pdb_ldap.c
blob0886a10afe53c9291fa8f83759c8132b20c0acd7
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 SAFE_FREE(escape_user);
353 return LDAP_NO_MEMORY;
356 * have to use this here because $ is filtered out
357 * in string_sub
360 filter = talloc_all_string_sub(talloc_tos(),
361 filter, "%u", escape_user);
362 SAFE_FREE(escape_user);
363 if (!filter) {
364 return LDAP_NO_MEMORY;
367 ret = smbldap_search_suffix(ldap_state->smbldap_state,
368 filter, attr, result);
369 TALLOC_FREE(filter);
370 return ret;
373 /*******************************************************************
374 Run the search by rid.
375 ******************************************************************/
377 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
378 uint32 rid, LDAPMessage ** result,
379 const char **attr)
381 char *filter = NULL;
382 int rc;
384 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
385 get_objclass_filter(ldap_state->schema_ver));
386 if (!filter) {
387 return LDAP_NO_MEMORY;
390 rc = smbldap_search_suffix(ldap_state->smbldap_state,
391 filter, attr, result);
392 TALLOC_FREE(filter);
393 return rc;
396 /*******************************************************************
397 Run the search by SID.
398 ******************************************************************/
400 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
401 const DOM_SID *sid, LDAPMessage ** result,
402 const char **attr)
404 char *filter = NULL;
405 int rc;
406 fstring sid_string;
408 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
409 get_userattr_key2string(ldap_state->schema_ver,
410 LDAP_ATTR_USER_SID),
411 sid_to_fstring(sid_string, sid),
412 get_objclass_filter(ldap_state->schema_ver));
413 if (!filter) {
414 return LDAP_NO_MEMORY;
417 rc = smbldap_search_suffix(ldap_state->smbldap_state,
418 filter, attr, result);
420 TALLOC_FREE(filter);
421 return rc;
424 /*******************************************************************
425 Delete complete object or objectclass and attrs from
426 object found in search_result depending on lp_ldap_delete_dn
427 ******************************************************************/
429 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
430 TALLOC_CTX *mem_ctx,
431 LDAPMessage *entry,
432 const char *objectclass,
433 const char **attrs)
435 LDAPMod **mods = NULL;
436 char *name;
437 const char *dn;
438 BerElement *ptr = NULL;
440 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
441 if (dn == NULL) {
442 return LDAP_NO_MEMORY;
445 if (lp_ldap_delete_dn()) {
446 return smbldap_delete(priv->smbldap_state, dn);
449 /* Ok, delete only the SAM attributes */
451 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
452 name != NULL;
453 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
454 const char **attrib;
456 /* We are only allowed to delete the attributes that
457 really exist. */
459 for (attrib = attrs; *attrib != NULL; attrib++) {
460 if (strequal(*attrib, name)) {
461 DEBUG(10, ("ldapsam_delete_entry: deleting "
462 "attribute %s\n", name));
463 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
464 NULL);
467 ldap_memfree(name);
470 if (ptr != NULL) {
471 ber_free(ptr, 0);
474 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
475 talloc_autofree_ldapmod(mem_ctx, mods);
477 return smbldap_modify(priv->smbldap_state, dn, mods);
480 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
482 char *temp;
483 struct tm tm;
485 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
486 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
487 talloc_tos());
488 if (!temp) {
489 return (time_t) 0;
492 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
493 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
494 (char*)temp));
495 TALLOC_FREE(temp);
496 return (time_t) 0;
498 TALLOC_FREE(temp);
499 tzset();
500 return timegm(&tm);
503 /**********************************************************************
504 Initialize struct samu from an LDAP query.
505 (Based on init_sam_from_buffer in pdb_tdb.c)
506 *********************************************************************/
508 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
509 struct samu * sampass,
510 LDAPMessage * entry)
512 time_t logon_time,
513 logoff_time,
514 kickoff_time,
515 pass_last_set_time,
516 pass_can_change_time,
517 pass_must_change_time,
518 ldap_entry_time,
519 bad_password_time;
520 char *username = NULL,
521 *domain = NULL,
522 *nt_username = NULL,
523 *fullname = NULL,
524 *homedir = NULL,
525 *dir_drive = NULL,
526 *logon_script = NULL,
527 *profile_path = NULL,
528 *acct_desc = NULL,
529 *workstations = NULL,
530 *munged_dial = NULL;
531 uint32 user_rid;
532 uint8 smblmpwd[LM_HASH_LEN],
533 smbntpwd[NT_HASH_LEN];
534 bool use_samba_attrs = True;
535 uint32 acct_ctrl = 0;
536 uint16 logon_divs;
537 uint16 bad_password_count = 0,
538 logon_count = 0;
539 uint32 hours_len;
540 uint8 hours[MAX_HOURS_LEN];
541 char *temp = NULL;
542 LOGIN_CACHE *cache_entry = NULL;
543 uint32 pwHistLen;
544 bool expand_explicit = lp_passdb_expand_explicit();
545 bool ret = false;
546 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
548 if (!ctx) {
549 return false;
551 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
552 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
553 goto fn_exit;
556 if (priv2ld(ldap_state) == NULL) {
557 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
558 "ldap_struct is NULL!\n"));
559 goto fn_exit;
562 if (!(username = smbldap_talloc_smallest_attribute(priv2ld(ldap_state),
563 entry,
564 "uid",
565 ctx))) {
566 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
567 "this user!\n"));
568 goto fn_exit;
571 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
573 nt_username = talloc_strdup(ctx, username);
574 if (!nt_username) {
575 goto fn_exit;
578 domain = talloc_strdup(ctx, ldap_state->domain_name);
579 if (!domain) {
580 goto fn_exit;
583 pdb_set_username(sampass, username, PDB_SET);
585 pdb_set_domain(sampass, domain, PDB_DEFAULT);
586 pdb_set_nt_username(sampass, nt_username, PDB_SET);
588 /* deal with different attributes between the schema first */
590 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
591 if ((temp = smbldap_talloc_single_attribute(
592 ldap_state->smbldap_state->ldap_struct,
593 entry,
594 get_userattr_key2string(ldap_state->schema_ver,
595 LDAP_ATTR_USER_SID),
596 ctx))!=NULL) {
597 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
599 } else {
600 if ((temp = smbldap_talloc_single_attribute(
601 ldap_state->smbldap_state->ldap_struct,
602 entry,
603 get_userattr_key2string(ldap_state->schema_ver,
604 LDAP_ATTR_USER_RID),
605 ctx))!=NULL) {
606 user_rid = (uint32)atol(temp);
607 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
611 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
612 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
613 get_userattr_key2string(ldap_state->schema_ver,
614 LDAP_ATTR_USER_SID),
615 get_userattr_key2string(ldap_state->schema_ver,
616 LDAP_ATTR_USER_RID),
617 username));
618 return False;
621 temp = smbldap_talloc_single_attribute(
622 ldap_state->smbldap_state->ldap_struct,
623 entry,
624 get_userattr_key2string(ldap_state->schema_ver,
625 LDAP_ATTR_PWD_LAST_SET),
626 ctx);
627 if (temp) {
628 pass_last_set_time = (time_t) atol(temp);
629 pdb_set_pass_last_set_time(sampass,
630 pass_last_set_time, PDB_SET);
633 temp = smbldap_talloc_single_attribute(
634 ldap_state->smbldap_state->ldap_struct,
635 entry,
636 get_userattr_key2string(ldap_state->schema_ver,
637 LDAP_ATTR_LOGON_TIME),
638 ctx);
639 if (temp) {
640 logon_time = (time_t) atol(temp);
641 pdb_set_logon_time(sampass, logon_time, PDB_SET);
644 temp = smbldap_talloc_single_attribute(
645 ldap_state->smbldap_state->ldap_struct,
646 entry,
647 get_userattr_key2string(ldap_state->schema_ver,
648 LDAP_ATTR_LOGOFF_TIME),
649 ctx);
650 if (temp) {
651 logoff_time = (time_t) atol(temp);
652 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
655 temp = smbldap_talloc_single_attribute(
656 ldap_state->smbldap_state->ldap_struct,
657 entry,
658 get_userattr_key2string(ldap_state->schema_ver,
659 LDAP_ATTR_KICKOFF_TIME),
660 ctx);
661 if (temp) {
662 kickoff_time = (time_t) atol(temp);
663 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
666 temp = smbldap_talloc_single_attribute(
667 ldap_state->smbldap_state->ldap_struct,
668 entry,
669 get_userattr_key2string(ldap_state->schema_ver,
670 LDAP_ATTR_PWD_CAN_CHANGE),
671 ctx);
672 if (temp) {
673 pass_can_change_time = (time_t) atol(temp);
674 pdb_set_pass_can_change_time(sampass,
675 pass_can_change_time, PDB_SET);
678 temp = smbldap_talloc_single_attribute(
679 ldap_state->smbldap_state->ldap_struct,
680 entry,
681 get_userattr_key2string(ldap_state->schema_ver,
682 LDAP_ATTR_PWD_MUST_CHANGE),
683 ctx);
684 if (temp) {
685 pass_must_change_time = (time_t) atol(temp);
686 pdb_set_pass_must_change_time(sampass,
687 pass_must_change_time, PDB_SET);
690 /* recommend that 'gecos' and 'displayName' should refer to the same
691 * attribute OID. userFullName depreciated, only used by Samba
692 * primary rules of LDAP: don't make a new attribute when one is already defined
693 * that fits your needs; using cn then displayName rather than 'userFullName'
696 fullname = smbldap_talloc_single_attribute(
697 ldap_state->smbldap_state->ldap_struct,
698 entry,
699 get_userattr_key2string(ldap_state->schema_ver,
700 LDAP_ATTR_DISPLAY_NAME),
701 ctx);
702 if (fullname) {
703 pdb_set_fullname(sampass, fullname, PDB_SET);
704 } else {
705 fullname = smbldap_talloc_single_attribute(
706 ldap_state->smbldap_state->ldap_struct,
707 entry,
708 get_userattr_key2string(ldap_state->schema_ver,
709 LDAP_ATTR_CN),
710 ctx);
711 if (fullname) {
712 pdb_set_fullname(sampass, fullname, PDB_SET);
716 dir_drive = smbldap_talloc_single_attribute(
717 ldap_state->smbldap_state->ldap_struct,
718 entry,
719 get_userattr_key2string(ldap_state->schema_ver,
720 LDAP_ATTR_HOME_DRIVE),
721 ctx);
722 if (dir_drive) {
723 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
724 } else {
725 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
728 homedir = smbldap_talloc_single_attribute(
729 ldap_state->smbldap_state->ldap_struct,
730 entry,
731 get_userattr_key2string(ldap_state->schema_ver,
732 LDAP_ATTR_HOME_PATH),
733 ctx);
734 if (homedir) {
735 if (expand_explicit) {
736 homedir = talloc_sub_basic(ctx,
737 username,
738 domain,
739 homedir);
740 if (!homedir) {
741 goto fn_exit;
744 pdb_set_homedir(sampass, homedir, PDB_SET);
745 } else {
746 pdb_set_homedir(sampass,
747 talloc_sub_basic(ctx, username, domain,
748 lp_logon_home()),
749 PDB_DEFAULT);
752 logon_script = smbldap_talloc_single_attribute(
753 ldap_state->smbldap_state->ldap_struct,
754 entry,
755 get_userattr_key2string(ldap_state->schema_ver,
756 LDAP_ATTR_LOGON_SCRIPT),
757 ctx);
758 if (logon_script) {
759 if (expand_explicit) {
760 logon_script = talloc_sub_basic(ctx,
761 username,
762 domain,
763 logon_script);
764 if (!logon_script) {
765 goto fn_exit;
768 pdb_set_logon_script(sampass, logon_script, PDB_SET);
769 } else {
770 pdb_set_logon_script(sampass,
771 talloc_sub_basic(ctx, username, domain,
772 lp_logon_script()),
773 PDB_DEFAULT );
776 profile_path = smbldap_talloc_single_attribute(
777 ldap_state->smbldap_state->ldap_struct,
778 entry,
779 get_userattr_key2string(ldap_state->schema_ver,
780 LDAP_ATTR_PROFILE_PATH),
781 ctx);
782 if (profile_path) {
783 if (expand_explicit) {
784 profile_path = talloc_sub_basic(ctx,
785 username,
786 domain,
787 profile_path);
788 if (!profile_path) {
789 goto fn_exit;
792 pdb_set_profile_path(sampass, profile_path, PDB_SET);
793 } else {
794 pdb_set_profile_path(sampass,
795 talloc_sub_basic(ctx, username, domain,
796 lp_logon_path()),
797 PDB_DEFAULT );
800 acct_desc = smbldap_talloc_single_attribute(
801 ldap_state->smbldap_state->ldap_struct,
802 entry,
803 get_userattr_key2string(ldap_state->schema_ver,
804 LDAP_ATTR_DESC),
805 ctx);
806 if (acct_desc) {
807 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
810 workstations = smbldap_talloc_single_attribute(
811 ldap_state->smbldap_state->ldap_struct,
812 entry,
813 get_userattr_key2string(ldap_state->schema_ver,
814 LDAP_ATTR_USER_WKS),
815 ctx);
816 if (workstations) {
817 pdb_set_workstations(sampass, workstations, PDB_SET);
820 munged_dial = smbldap_talloc_single_attribute(
821 ldap_state->smbldap_state->ldap_struct,
822 entry,
823 get_userattr_key2string(ldap_state->schema_ver,
824 LDAP_ATTR_MUNGED_DIAL),
825 ctx);
826 if (munged_dial) {
827 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
830 /* FIXME: hours stuff should be cleaner */
832 logon_divs = 168;
833 hours_len = 21;
834 memset(hours, 0xff, hours_len);
836 if (ldap_state->is_nds_ldap) {
837 char *user_dn;
838 size_t pwd_len;
839 char clear_text_pw[512];
841 /* Make call to Novell eDirectory ldap extension to get clear text password.
842 NOTE: This will only work if we have an SSL connection to eDirectory. */
843 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
844 if (user_dn != NULL) {
845 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
847 pwd_len = sizeof(clear_text_pw);
848 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
849 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
850 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
851 TALLOC_FREE(user_dn);
852 return False;
854 ZERO_STRUCT(smblmpwd);
855 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
856 TALLOC_FREE(user_dn);
857 return False;
859 ZERO_STRUCT(smbntpwd);
860 use_samba_attrs = False;
863 TALLOC_FREE(user_dn);
865 } else {
866 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
870 if (use_samba_attrs) {
871 temp = smbldap_talloc_single_attribute(
872 ldap_state->smbldap_state->ldap_struct,
873 entry,
874 get_userattr_key2string(ldap_state->schema_ver,
875 LDAP_ATTR_LMPW),
876 ctx);
877 if (temp) {
878 pdb_gethexpwd(temp, smblmpwd);
879 memset((char *)temp, '\0', strlen(temp)+1);
880 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
881 goto fn_exit;
883 ZERO_STRUCT(smblmpwd);
886 temp = smbldap_talloc_single_attribute(
887 ldap_state->smbldap_state->ldap_struct,
888 entry,
889 get_userattr_key2string(ldap_state->schema_ver,
890 LDAP_ATTR_NTPW),
891 ctx);
892 if (temp) {
893 pdb_gethexpwd(temp, smbntpwd);
894 memset((char *)temp, '\0', strlen(temp)+1);
895 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
896 goto fn_exit;
898 ZERO_STRUCT(smbntpwd);
902 pwHistLen = 0;
904 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
905 if (pwHistLen > 0){
906 uint8 *pwhist = NULL;
907 int i;
908 char *history_string = TALLOC_ARRAY(ctx, char,
909 MAX_PW_HISTORY_LEN*64);
911 if (!history_string) {
912 goto fn_exit;
915 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
917 if ((pwhist = TALLOC_ARRAY(ctx, uint8,
918 pwHistLen * PW_HISTORY_ENTRY_LEN)) ==
919 NULL){
920 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
921 goto fn_exit;
923 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
925 if (smbldap_get_single_attribute(
926 ldap_state->smbldap_state->ldap_struct,
927 entry,
928 get_userattr_key2string(ldap_state->schema_ver,
929 LDAP_ATTR_PWD_HISTORY),
930 history_string,
931 MAX_PW_HISTORY_LEN*64)) {
932 bool hex_failed = false;
933 for (i = 0; i < pwHistLen; i++){
934 /* Get the 16 byte salt. */
935 if (!pdb_gethexpwd(&history_string[i*64],
936 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
937 hex_failed = true;
938 break;
940 /* Get the 16 byte MD5 hash of salt+passwd. */
941 if (!pdb_gethexpwd(&history_string[(i*64)+32],
942 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
943 PW_HISTORY_SALT_LEN])) {
944 hex_failed = True;
945 break;
948 if (hex_failed) {
949 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
950 username));
951 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
954 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
955 goto fn_exit;
959 temp = smbldap_talloc_single_attribute(
960 ldap_state->smbldap_state->ldap_struct,
961 entry,
962 get_userattr_key2string(ldap_state->schema_ver,
963 LDAP_ATTR_ACB_INFO),
964 ctx);
965 if (temp) {
966 acct_ctrl = pdb_decode_acct_ctrl(temp);
968 if (acct_ctrl == 0) {
969 acct_ctrl |= ACB_NORMAL;
972 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
973 } else {
974 acct_ctrl |= ACB_NORMAL;
977 pdb_set_hours_len(sampass, hours_len, PDB_SET);
978 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
980 temp = smbldap_talloc_single_attribute(
981 ldap_state->smbldap_state->ldap_struct,
982 entry,
983 get_userattr_key2string(ldap_state->schema_ver,
984 LDAP_ATTR_BAD_PASSWORD_COUNT),
985 ctx);
986 if (temp) {
987 bad_password_count = (uint32) atol(temp);
988 pdb_set_bad_password_count(sampass,
989 bad_password_count, PDB_SET);
992 temp = smbldap_talloc_single_attribute(
993 ldap_state->smbldap_state->ldap_struct,
994 entry,
995 get_userattr_key2string(ldap_state->schema_ver,
996 LDAP_ATTR_BAD_PASSWORD_TIME),
997 ctx);
998 if (temp) {
999 bad_password_time = (time_t) atol(temp);
1000 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
1004 temp = smbldap_talloc_single_attribute(
1005 ldap_state->smbldap_state->ldap_struct,
1006 entry,
1007 get_userattr_key2string(ldap_state->schema_ver,
1008 LDAP_ATTR_LOGON_COUNT),
1009 ctx);
1010 if (temp) {
1011 logon_count = (uint32) atol(temp);
1012 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1015 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1017 temp = smbldap_talloc_single_attribute(
1018 ldap_state->smbldap_state->ldap_struct,
1019 entry,
1020 get_userattr_key2string(ldap_state->schema_ver,
1021 LDAP_ATTR_LOGON_HOURS),
1022 ctx);
1023 if (temp) {
1024 pdb_gethexhours(temp, hours);
1025 memset((char *)temp, '\0', strlen(temp) +1);
1026 pdb_set_hours(sampass, hours, PDB_SET);
1027 ZERO_STRUCT(hours);
1030 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1031 temp = smbldap_talloc_single_attribute(
1032 priv2ld(ldap_state),
1033 entry,
1034 "uidNumber",
1035 ctx);
1036 if (temp) {
1037 /* We've got a uid, feed the cache */
1038 uid_t uid = strtoul(temp, NULL, 10);
1039 store_uid_sid_cache(pdb_get_user_sid(sampass), uid);
1043 /* check the timestamp of the cache vs ldap entry */
1044 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1045 entry))) {
1046 ret = true;
1047 goto fn_exit;
1050 /* see if we have newer updates */
1051 if (!(cache_entry = login_cache_read(sampass))) {
1052 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1053 (unsigned int)pdb_get_bad_password_count(sampass),
1054 (unsigned int)pdb_get_bad_password_time(sampass)));
1055 ret = true;
1056 goto fn_exit;
1059 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1060 (unsigned int)ldap_entry_time,
1061 (unsigned int)cache_entry->entry_timestamp,
1062 (unsigned int)cache_entry->bad_password_time));
1064 if (ldap_entry_time > cache_entry->entry_timestamp) {
1065 /* cache is older than directory , so
1066 we need to delete the entry but allow the
1067 fields to be written out */
1068 login_cache_delentry(sampass);
1069 } else {
1070 /* read cache in */
1071 pdb_set_acct_ctrl(sampass,
1072 pdb_get_acct_ctrl(sampass) |
1073 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
1074 PDB_SET);
1075 pdb_set_bad_password_count(sampass,
1076 cache_entry->bad_password_count,
1077 PDB_SET);
1078 pdb_set_bad_password_time(sampass,
1079 cache_entry->bad_password_time,
1080 PDB_SET);
1083 ret = true;
1085 fn_exit:
1087 TALLOC_FREE(ctx);
1088 SAFE_FREE(cache_entry);
1089 return ret;
1092 /**********************************************************************
1093 Initialize the ldap db from a struct samu. Called on update.
1094 (Based on init_buffer_from_sam in pdb_tdb.c)
1095 *********************************************************************/
1097 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1098 LDAPMessage *existing,
1099 LDAPMod *** mods, struct samu * sampass,
1100 bool (*need_update)(const struct samu *,
1101 enum pdb_elements))
1103 char *temp = NULL;
1104 uint32 rid;
1106 if (mods == NULL || sampass == NULL) {
1107 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1108 return False;
1111 *mods = NULL;
1114 * took out adding "objectclass: sambaAccount"
1115 * do this on a per-mod basis
1117 if (need_update(sampass, PDB_USERNAME)) {
1118 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1119 "uid", pdb_get_username(sampass));
1120 if (ldap_state->is_nds_ldap) {
1121 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1122 "cn", pdb_get_username(sampass));
1123 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1124 "sn", pdb_get_username(sampass));
1128 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1130 /* only update the RID if we actually need to */
1131 if (need_update(sampass, PDB_USERSID)) {
1132 fstring sid_string;
1133 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
1135 switch ( ldap_state->schema_ver ) {
1136 case SCHEMAVER_SAMBAACCOUNT:
1137 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1138 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1139 sid_string_dbg(user_sid),
1140 sid_string_dbg(
1141 &ldap_state->domain_sid)));
1142 return False;
1144 if (asprintf(&temp, "%i", rid) < 0) {
1145 return false;
1147 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1148 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1149 temp);
1150 SAFE_FREE(temp);
1151 break;
1153 case SCHEMAVER_SAMBASAMACCOUNT:
1154 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1155 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1156 sid_to_fstring(sid_string, user_sid));
1157 break;
1159 default:
1160 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1161 break;
1165 /* we don't need to store the primary group RID - so leaving it
1166 'free' to hang off the unix primary group makes life easier */
1168 if (need_update(sampass, PDB_GROUPSID)) {
1169 fstring sid_string;
1170 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1172 switch ( ldap_state->schema_ver ) {
1173 case SCHEMAVER_SAMBAACCOUNT:
1174 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1175 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1176 sid_string_dbg(group_sid),
1177 sid_string_dbg(
1178 &ldap_state->domain_sid)));
1179 return False;
1182 if (asprintf(&temp, "%i", rid) < 0) {
1183 return false;
1185 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1186 get_userattr_key2string(ldap_state->schema_ver,
1187 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1188 SAFE_FREE(temp);
1189 break;
1191 case SCHEMAVER_SAMBASAMACCOUNT:
1192 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1193 get_userattr_key2string(ldap_state->schema_ver,
1194 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1195 break;
1197 default:
1198 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1199 break;
1204 /* displayName, cn, and gecos should all be the same
1205 * most easily accomplished by giving them the same OID
1206 * gecos isn't set here b/c it should be handled by the
1207 * add-user script
1208 * We change displayName only and fall back to cn if
1209 * it does not exist.
1212 if (need_update(sampass, PDB_FULLNAME))
1213 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1214 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1215 pdb_get_fullname(sampass));
1217 if (need_update(sampass, PDB_ACCTDESC))
1218 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1219 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1220 pdb_get_acct_desc(sampass));
1222 if (need_update(sampass, PDB_WORKSTATIONS))
1223 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1224 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1225 pdb_get_workstations(sampass));
1227 if (need_update(sampass, PDB_MUNGEDDIAL))
1228 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1229 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1230 pdb_get_munged_dial(sampass));
1232 if (need_update(sampass, PDB_SMBHOME))
1233 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1234 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1235 pdb_get_homedir(sampass));
1237 if (need_update(sampass, PDB_DRIVE))
1238 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1239 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1240 pdb_get_dir_drive(sampass));
1242 if (need_update(sampass, PDB_LOGONSCRIPT))
1243 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1244 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1245 pdb_get_logon_script(sampass));
1247 if (need_update(sampass, PDB_PROFILE))
1248 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1249 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1250 pdb_get_profile_path(sampass));
1252 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1253 return false;
1255 if (need_update(sampass, PDB_LOGONTIME))
1256 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1257 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1258 SAFE_FREE(temp);
1260 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1261 return false;
1263 if (need_update(sampass, PDB_LOGOFFTIME))
1264 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1265 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1266 SAFE_FREE(temp);
1268 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1269 return false;
1271 if (need_update(sampass, PDB_KICKOFFTIME))
1272 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1273 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1274 SAFE_FREE(temp);
1276 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1277 return false;
1279 if (need_update(sampass, PDB_CANCHANGETIME))
1280 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1281 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1282 SAFE_FREE(temp);
1284 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1285 return false;
1287 if (need_update(sampass, PDB_MUSTCHANGETIME))
1288 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1289 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1290 SAFE_FREE(temp);
1292 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1293 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1295 if (need_update(sampass, PDB_LMPASSWD)) {
1296 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1297 if (lm_pw) {
1298 char pwstr[34];
1299 pdb_sethexpwd(pwstr, lm_pw,
1300 pdb_get_acct_ctrl(sampass));
1301 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1302 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1303 pwstr);
1304 } else {
1305 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1306 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1307 NULL);
1310 if (need_update(sampass, PDB_NTPASSWD)) {
1311 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1312 if (nt_pw) {
1313 char pwstr[34];
1314 pdb_sethexpwd(pwstr, nt_pw,
1315 pdb_get_acct_ctrl(sampass));
1316 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1317 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1318 pwstr);
1319 } else {
1320 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1321 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1322 NULL);
1326 if (need_update(sampass, PDB_PWHISTORY)) {
1327 char *pwstr = NULL;
1328 uint32 pwHistLen = 0;
1329 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1331 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1332 if (!pwstr) {
1333 return false;
1335 if (pwHistLen == 0) {
1336 /* Remove any password history from the LDAP store. */
1337 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1338 pwstr[64] = '\0';
1339 } else {
1340 int i;
1341 uint32 currHistLen = 0;
1342 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1343 if (pwhist != NULL) {
1344 /* We can only store (1024-1/64 password history entries. */
1345 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1346 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1347 /* Store the salt. */
1348 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1349 /* Followed by the md5 hash of salt + md4 hash */
1350 pdb_sethexpwd(&pwstr[(i*64)+32],
1351 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1352 DEBUG(100, ("pwstr=%s\n", pwstr));
1356 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1357 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1358 pwstr);
1359 SAFE_FREE(pwstr);
1362 if (need_update(sampass, PDB_PASSLASTSET)) {
1363 if (asprintf(&temp, "%li",
1364 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1365 return false;
1367 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1368 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1369 temp);
1370 SAFE_FREE(temp);
1374 if (need_update(sampass, PDB_HOURS)) {
1375 const uint8 *hours = pdb_get_hours(sampass);
1376 if (hours) {
1377 char hourstr[44];
1378 pdb_sethexhours(hourstr, hours);
1379 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1380 existing,
1381 mods,
1382 get_userattr_key2string(ldap_state->schema_ver,
1383 LDAP_ATTR_LOGON_HOURS),
1384 hourstr);
1388 if (need_update(sampass, PDB_ACCTCTRL))
1389 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1390 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1391 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1393 /* password lockout cache:
1394 - If we are now autolocking or clearing, we write to ldap
1395 - If we are clearing, we delete the cache entry
1396 - If the count is > 0, we update the cache
1398 This even means when autolocking, we cache, just in case the
1399 update doesn't work, and we have to cache the autolock flag */
1401 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1402 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1403 uint16 badcount = pdb_get_bad_password_count(sampass);
1404 time_t badtime = pdb_get_bad_password_time(sampass);
1405 uint32 pol;
1406 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1408 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1409 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1411 if ((badcount >= pol) || (badcount == 0)) {
1412 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1413 (unsigned int)badcount, (unsigned int)badtime));
1414 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1415 return false;
1417 smbldap_make_mod(
1418 ldap_state->smbldap_state->ldap_struct,
1419 existing, mods,
1420 get_userattr_key2string(
1421 ldap_state->schema_ver,
1422 LDAP_ATTR_BAD_PASSWORD_COUNT),
1423 temp);
1424 SAFE_FREE(temp);
1426 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1427 return false;
1429 smbldap_make_mod(
1430 ldap_state->smbldap_state->ldap_struct,
1431 existing, mods,
1432 get_userattr_key2string(
1433 ldap_state->schema_ver,
1434 LDAP_ATTR_BAD_PASSWORD_TIME),
1435 temp);
1436 SAFE_FREE(temp);
1438 if (badcount == 0) {
1439 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1440 login_cache_delentry(sampass);
1441 } else {
1442 LOGIN_CACHE cache_entry;
1444 cache_entry.entry_timestamp = time(NULL);
1445 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1446 cache_entry.bad_password_count = badcount;
1447 cache_entry.bad_password_time = badtime;
1449 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1450 login_cache_write(sampass, cache_entry);
1454 return True;
1457 /**********************************************************************
1458 End enumeration of the LDAP password list.
1459 *********************************************************************/
1461 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1463 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1464 if (ldap_state->result) {
1465 ldap_msgfree(ldap_state->result);
1466 ldap_state->result = NULL;
1470 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1471 const char *new_attr)
1473 int i;
1475 if (new_attr == NULL) {
1476 return;
1479 for (i=0; (*attr_list)[i] != NULL; i++) {
1483 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1484 const char *, i+2);
1485 SMB_ASSERT((*attr_list) != NULL);
1486 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1487 (*attr_list)[i+1] = NULL;
1490 /**********************************************************************
1491 Get struct samu entry from LDAP by username.
1492 *********************************************************************/
1494 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1496 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1497 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1498 LDAPMessage *result = NULL;
1499 LDAPMessage *entry = NULL;
1500 int count;
1501 const char ** attr_list;
1502 int rc;
1504 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1505 append_attr(user, &attr_list,
1506 get_userattr_key2string(ldap_state->schema_ver,
1507 LDAP_ATTR_MOD_TIMESTAMP));
1508 append_attr(user, &attr_list, "uidNumber");
1509 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1510 attr_list);
1511 TALLOC_FREE( attr_list );
1513 if ( rc != LDAP_SUCCESS )
1514 return NT_STATUS_NO_SUCH_USER;
1516 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1518 if (count < 1) {
1519 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1520 ldap_msgfree(result);
1521 return NT_STATUS_NO_SUCH_USER;
1522 } else if (count > 1) {
1523 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1524 ldap_msgfree(result);
1525 return NT_STATUS_NO_SUCH_USER;
1528 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1529 if (entry) {
1530 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1531 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1532 ldap_msgfree(result);
1533 return NT_STATUS_NO_SUCH_USER;
1535 pdb_set_backend_private_data(user, result, NULL,
1536 my_methods, PDB_CHANGED);
1537 talloc_autofree_ldapmsg(user, result);
1538 ret = NT_STATUS_OK;
1539 } else {
1540 ldap_msgfree(result);
1542 return ret;
1545 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1546 const DOM_SID *sid, LDAPMessage **result)
1548 int rc = -1;
1549 const char ** attr_list;
1550 uint32 rid;
1552 switch ( ldap_state->schema_ver ) {
1553 case SCHEMAVER_SAMBASAMACCOUNT: {
1554 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1555 if (tmp_ctx == NULL) {
1556 return LDAP_NO_MEMORY;
1559 attr_list = get_userattr_list(tmp_ctx,
1560 ldap_state->schema_ver);
1561 append_attr(tmp_ctx, &attr_list,
1562 get_userattr_key2string(
1563 ldap_state->schema_ver,
1564 LDAP_ATTR_MOD_TIMESTAMP));
1565 append_attr(tmp_ctx, &attr_list, "uidNumber");
1566 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1567 result, attr_list);
1568 TALLOC_FREE(tmp_ctx);
1570 if ( rc != LDAP_SUCCESS )
1571 return rc;
1572 break;
1575 case SCHEMAVER_SAMBAACCOUNT:
1576 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1577 return rc;
1580 attr_list = get_userattr_list(NULL,
1581 ldap_state->schema_ver);
1582 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1583 TALLOC_FREE( attr_list );
1585 if ( rc != LDAP_SUCCESS )
1586 return rc;
1587 break;
1589 return rc;
1592 /**********************************************************************
1593 Get struct samu entry from LDAP by SID.
1594 *********************************************************************/
1596 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1598 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1599 LDAPMessage *result = NULL;
1600 LDAPMessage *entry = NULL;
1601 int count;
1602 int rc;
1604 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1605 sid, &result);
1606 if (rc != LDAP_SUCCESS)
1607 return NT_STATUS_NO_SUCH_USER;
1609 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1611 if (count < 1) {
1612 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1613 "count=%d\n", sid_string_dbg(sid), count));
1614 ldap_msgfree(result);
1615 return NT_STATUS_NO_SUCH_USER;
1616 } else if (count > 1) {
1617 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1618 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1619 count));
1620 ldap_msgfree(result);
1621 return NT_STATUS_NO_SUCH_USER;
1624 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1625 if (!entry) {
1626 ldap_msgfree(result);
1627 return NT_STATUS_NO_SUCH_USER;
1630 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1631 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1632 ldap_msgfree(result);
1633 return NT_STATUS_NO_SUCH_USER;
1636 pdb_set_backend_private_data(user, result, NULL,
1637 my_methods, PDB_CHANGED);
1638 talloc_autofree_ldapmsg(user, result);
1639 return NT_STATUS_OK;
1642 /********************************************************************
1643 Do the actual modification - also change a plaintext passord if
1644 it it set.
1645 **********************************************************************/
1647 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1648 struct samu *newpwd, char *dn,
1649 LDAPMod **mods, int ldap_op,
1650 bool (*need_update)(const struct samu *, enum pdb_elements))
1652 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1653 int rc;
1655 if (!newpwd || !dn) {
1656 return NT_STATUS_INVALID_PARAMETER;
1659 if (!mods) {
1660 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1661 /* may be password change below however */
1662 } else {
1663 switch(ldap_op) {
1664 case LDAP_MOD_ADD:
1665 if (ldap_state->is_nds_ldap) {
1666 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1667 "objectclass",
1668 "inetOrgPerson");
1669 } else {
1670 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1671 "objectclass",
1672 LDAP_OBJ_ACCOUNT);
1674 rc = smbldap_add(ldap_state->smbldap_state,
1675 dn, mods);
1676 break;
1677 case LDAP_MOD_REPLACE:
1678 rc = smbldap_modify(ldap_state->smbldap_state,
1679 dn ,mods);
1680 break;
1681 default:
1682 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1683 ldap_op));
1684 return NT_STATUS_INVALID_PARAMETER;
1687 if (rc!=LDAP_SUCCESS) {
1688 return NT_STATUS_UNSUCCESSFUL;
1692 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1693 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1694 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1695 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1696 BerElement *ber;
1697 struct berval *bv;
1698 char *retoid = NULL;
1699 struct berval *retdata = NULL;
1700 char *utf8_password;
1701 char *utf8_dn;
1702 size_t converted_size;
1704 if (!ldap_state->is_nds_ldap) {
1706 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1707 LDAP_EXOP_MODIFY_PASSWD)) {
1708 DEBUG(2, ("ldap password change requested, but LDAP "
1709 "server does not support it -- ignoring\n"));
1710 return NT_STATUS_OK;
1714 if (!push_utf8_allocate(&utf8_password,
1715 pdb_get_plaintext_passwd(newpwd),
1716 &converted_size))
1718 return NT_STATUS_NO_MEMORY;
1721 if (!push_utf8_allocate(&utf8_dn, dn, &converted_size)) {
1722 SAFE_FREE(utf8_password);
1723 return NT_STATUS_NO_MEMORY;
1726 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1727 DEBUG(0,("ber_alloc_t returns NULL\n"));
1728 SAFE_FREE(utf8_password);
1729 SAFE_FREE(utf8_dn);
1730 return NT_STATUS_UNSUCCESSFUL;
1733 if ((ber_printf (ber, "{") < 0) ||
1734 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn) < 0) ||
1735 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password) < 0) ||
1736 (ber_printf (ber, "n}") < 0)) {
1737 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a value <0\n"));
1738 ber_free(ber,1);
1739 SAFE_FREE(utf8_dn);
1740 SAFE_FREE(utf8_password);
1741 return NT_STATUS_UNSUCCESSFUL;
1744 if ((rc = ber_flatten (ber, &bv))<0) {
1745 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1746 ber_free(ber,1);
1747 SAFE_FREE(utf8_dn);
1748 SAFE_FREE(utf8_password);
1749 return NT_STATUS_UNSUCCESSFUL;
1752 SAFE_FREE(utf8_dn);
1753 SAFE_FREE(utf8_password);
1754 ber_free(ber, 1);
1756 if (!ldap_state->is_nds_ldap) {
1757 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1758 LDAP_EXOP_MODIFY_PASSWD,
1759 bv, NULL, NULL, &retoid,
1760 &retdata);
1761 } else {
1762 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1763 pdb_get_plaintext_passwd(newpwd));
1765 if (rc != LDAP_SUCCESS) {
1766 char *ld_error = NULL;
1768 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1769 DEBUG(3, ("Could not set userPassword "
1770 "attribute due to an objectClass "
1771 "violation -- ignoring\n"));
1772 ber_bvfree(bv);
1773 return NT_STATUS_OK;
1776 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1777 &ld_error);
1778 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1779 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1780 SAFE_FREE(ld_error);
1781 ber_bvfree(bv);
1782 #if defined(LDAP_CONSTRAINT_VIOLATION)
1783 if (rc == LDAP_CONSTRAINT_VIOLATION)
1784 return NT_STATUS_PASSWORD_RESTRICTION;
1785 #endif
1786 return NT_STATUS_UNSUCCESSFUL;
1787 } else {
1788 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1789 #ifdef DEBUG_PASSWORD
1790 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1791 #endif
1792 if (retdata)
1793 ber_bvfree(retdata);
1794 if (retoid)
1795 ldap_memfree(retoid);
1797 ber_bvfree(bv);
1799 return NT_STATUS_OK;
1802 /**********************************************************************
1803 Delete entry from LDAP for username.
1804 *********************************************************************/
1806 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1807 struct samu * sam_acct)
1809 struct ldapsam_privates *priv =
1810 (struct ldapsam_privates *)my_methods->private_data;
1811 const char *sname;
1812 int rc;
1813 LDAPMessage *msg, *entry;
1814 NTSTATUS result = NT_STATUS_NO_MEMORY;
1815 const char **attr_list;
1816 TALLOC_CTX *mem_ctx;
1818 if (!sam_acct) {
1819 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1820 return NT_STATUS_INVALID_PARAMETER;
1823 sname = pdb_get_username(sam_acct);
1825 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1826 "LDAP.\n", sname));
1828 mem_ctx = talloc_new(NULL);
1829 if (mem_ctx == NULL) {
1830 DEBUG(0, ("talloc_new failed\n"));
1831 goto done;
1834 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1835 if (attr_list == NULL) {
1836 goto done;
1839 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1841 if ((rc != LDAP_SUCCESS) ||
1842 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1843 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1844 DEBUG(5, ("Could not find user %s\n", sname));
1845 result = NT_STATUS_NO_SUCH_USER;
1846 goto done;
1849 rc = ldapsam_delete_entry(
1850 priv, mem_ctx, entry,
1851 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1852 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1853 attr_list);
1855 result = (rc == LDAP_SUCCESS) ?
1856 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1858 done:
1859 TALLOC_FREE(mem_ctx);
1860 return result;
1863 /**********************************************************************
1864 Helper function to determine for update_sam_account whether
1865 we need LDAP modification.
1866 *********************************************************************/
1868 static bool element_is_changed(const struct samu *sampass,
1869 enum pdb_elements element)
1871 return IS_SAM_CHANGED(sampass, element);
1874 /**********************************************************************
1875 Update struct samu.
1876 *********************************************************************/
1878 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1880 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1881 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1882 int rc = 0;
1883 char *dn;
1884 LDAPMessage *result = NULL;
1885 LDAPMessage *entry = NULL;
1886 LDAPMod **mods = NULL;
1887 const char **attr_list;
1889 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1890 if (!result) {
1891 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1892 if (pdb_get_username(newpwd) == NULL) {
1893 return NT_STATUS_INVALID_PARAMETER;
1895 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1896 TALLOC_FREE( attr_list );
1897 if (rc != LDAP_SUCCESS) {
1898 return NT_STATUS_UNSUCCESSFUL;
1900 pdb_set_backend_private_data(newpwd, result, NULL,
1901 my_methods, PDB_CHANGED);
1902 talloc_autofree_ldapmsg(newpwd, result);
1905 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1906 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1907 return NT_STATUS_UNSUCCESSFUL;
1910 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1911 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1912 if (!dn) {
1913 return NT_STATUS_UNSUCCESSFUL;
1916 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1918 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1919 element_is_changed)) {
1920 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1921 TALLOC_FREE(dn);
1922 if (mods != NULL)
1923 ldap_mods_free(mods,True);
1924 return NT_STATUS_UNSUCCESSFUL;
1927 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1928 && (mods == NULL)) {
1929 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1930 pdb_get_username(newpwd)));
1931 TALLOC_FREE(dn);
1932 return NT_STATUS_OK;
1935 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1937 if (mods != NULL) {
1938 ldap_mods_free(mods,True);
1941 TALLOC_FREE(dn);
1944 * We need to set the backend private data to NULL here. For example
1945 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1946 * same one, and with the explicit delete / add logic for attribute
1947 * values the second time we would use the wrong "old" value which
1948 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1949 * the update.
1950 * The existing LDAPMessage is still being auto-freed by the
1951 * destructor.
1953 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
1954 PDB_CHANGED);
1956 if (!NT_STATUS_IS_OK(ret)) {
1957 return ret;
1960 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1961 pdb_get_username(newpwd)));
1962 return NT_STATUS_OK;
1965 /***************************************************************************
1966 Renames a struct samu
1967 - The "rename user script" has full responsibility for changing everything
1968 ***************************************************************************/
1970 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
1971 struct samu *old_acct,
1972 const char *newname)
1974 const char *oldname;
1975 int rc;
1976 char *rename_script = NULL;
1977 fstring oldname_lower, newname_lower;
1979 if (!old_acct) {
1980 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1981 return NT_STATUS_INVALID_PARAMETER;
1983 if (!newname) {
1984 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1985 return NT_STATUS_INVALID_PARAMETER;
1988 oldname = pdb_get_username(old_acct);
1990 /* rename the posix user */
1991 rename_script = SMB_STRDUP(lp_renameuser_script());
1992 if (rename_script == NULL) {
1993 return NT_STATUS_NO_MEMORY;
1996 if (!(*rename_script)) {
1997 SAFE_FREE(rename_script);
1998 return NT_STATUS_ACCESS_DENIED;
2001 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2002 oldname, newname));
2004 /* We have to allow the account name to end with a '$'.
2005 Also, follow the semantics in _samr_create_user() and lower case the
2006 posix name but preserve the case in passdb */
2008 fstrcpy( oldname_lower, oldname );
2009 strlower_m( oldname_lower );
2010 fstrcpy( newname_lower, newname );
2011 strlower_m( newname_lower );
2012 rename_script = realloc_string_sub2(rename_script,
2013 "%unew",
2014 newname_lower,
2015 true,
2016 true);
2017 if (!rename_script) {
2018 return NT_STATUS_NO_MEMORY;
2020 rename_script = realloc_string_sub2(rename_script,
2021 "%uold",
2022 oldname_lower,
2023 true,
2024 true);
2025 rc = smbrun(rename_script, NULL);
2027 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2028 rename_script, rc));
2030 SAFE_FREE(rename_script);
2032 if (rc == 0) {
2033 smb_nscd_flush_user_cache();
2036 if (rc)
2037 return NT_STATUS_UNSUCCESSFUL;
2039 return NT_STATUS_OK;
2042 /**********************************************************************
2043 Helper function to determine for update_sam_account whether
2044 we need LDAP modification.
2045 *********************************************************************/
2047 static bool element_is_set_or_changed(const struct samu *sampass,
2048 enum pdb_elements element)
2050 return (IS_SAM_SET(sampass, element) ||
2051 IS_SAM_CHANGED(sampass, element));
2054 /**********************************************************************
2055 Add struct samu to LDAP.
2056 *********************************************************************/
2058 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2060 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2061 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2062 int rc;
2063 LDAPMessage *result = NULL;
2064 LDAPMessage *entry = NULL;
2065 LDAPMod **mods = NULL;
2066 int ldap_op = LDAP_MOD_REPLACE;
2067 uint32 num_result;
2068 const char **attr_list;
2069 char *escape_user = NULL;
2070 const char *username = pdb_get_username(newpwd);
2071 const DOM_SID *sid = pdb_get_user_sid(newpwd);
2072 char *filter = NULL;
2073 char *dn = NULL;
2074 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2075 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2077 if (!ctx) {
2078 return NT_STATUS_NO_MEMORY;
2081 if (!username || !*username) {
2082 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2083 status = NT_STATUS_INVALID_PARAMETER;
2084 goto fn_exit;
2087 /* free this list after the second search or in case we exit on failure */
2088 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2090 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2092 if (rc != LDAP_SUCCESS) {
2093 goto fn_exit;
2096 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2097 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2098 username));
2099 goto fn_exit;
2101 ldap_msgfree(result);
2102 result = NULL;
2104 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2105 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2106 sid, &result);
2107 if (rc == LDAP_SUCCESS) {
2108 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2109 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2110 "already in the base, with samba "
2111 "attributes\n", sid_string_dbg(sid)));
2112 goto fn_exit;
2114 ldap_msgfree(result);
2115 result = NULL;
2119 /* does the entry already exist but without a samba attributes?
2120 we need to return the samba attributes here */
2122 escape_user = escape_ldap_string_alloc( username );
2123 filter = talloc_strdup(attr_list, "(uid=%u)");
2124 if (!filter) {
2125 status = NT_STATUS_NO_MEMORY;
2126 goto fn_exit;
2128 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2129 if (!filter) {
2130 status = NT_STATUS_NO_MEMORY;
2131 goto fn_exit;
2133 SAFE_FREE(escape_user);
2135 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2136 filter, attr_list, &result);
2137 if ( rc != LDAP_SUCCESS ) {
2138 goto fn_exit;
2141 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2143 if (num_result > 1) {
2144 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2145 goto fn_exit;
2148 /* Check if we need to update an existing entry */
2149 if (num_result == 1) {
2150 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2151 ldap_op = LDAP_MOD_REPLACE;
2152 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2153 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2154 if (!dn) {
2155 status = NT_STATUS_NO_MEMORY;
2156 goto fn_exit;
2159 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2161 /* There might be a SID for this account already - say an idmap entry */
2163 filter = talloc_asprintf(ctx,
2164 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2165 get_userattr_key2string(ldap_state->schema_ver,
2166 LDAP_ATTR_USER_SID),
2167 sid_string_talloc(ctx, sid),
2168 LDAP_OBJ_IDMAP_ENTRY,
2169 LDAP_OBJ_SID_ENTRY);
2170 if (!filter) {
2171 status = NT_STATUS_NO_MEMORY;
2172 goto fn_exit;
2175 /* free old result before doing a new search */
2176 if (result != NULL) {
2177 ldap_msgfree(result);
2178 result = NULL;
2180 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2181 filter, attr_list, &result);
2183 if ( rc != LDAP_SUCCESS ) {
2184 goto fn_exit;
2187 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2189 if (num_result > 1) {
2190 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2191 goto fn_exit;
2194 /* Check if we need to update an existing entry */
2195 if (num_result == 1) {
2197 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2198 ldap_op = LDAP_MOD_REPLACE;
2199 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2200 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2201 if (!dn) {
2202 status = NT_STATUS_NO_MEMORY;
2203 goto fn_exit;
2208 if (num_result == 0) {
2209 char *escape_username;
2210 /* Check if we need to add an entry */
2211 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2212 ldap_op = LDAP_MOD_ADD;
2214 escape_username = escape_rdn_val_string_alloc(username);
2215 if (!escape_username) {
2216 status = NT_STATUS_NO_MEMORY;
2217 goto fn_exit;
2220 if (username[strlen(username)-1] == '$') {
2221 dn = talloc_asprintf(ctx,
2222 "uid=%s,%s",
2223 escape_username,
2224 lp_ldap_machine_suffix());
2225 } else {
2226 dn = talloc_asprintf(ctx,
2227 "uid=%s,%s",
2228 escape_username,
2229 lp_ldap_user_suffix());
2232 SAFE_FREE(escape_username);
2233 if (!dn) {
2234 status = NT_STATUS_NO_MEMORY;
2235 goto fn_exit;
2239 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2240 element_is_set_or_changed)) {
2241 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2242 if (mods != NULL) {
2243 ldap_mods_free(mods, true);
2245 goto fn_exit;
2248 if (mods == NULL) {
2249 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2250 goto fn_exit;
2252 switch ( ldap_state->schema_ver ) {
2253 case SCHEMAVER_SAMBAACCOUNT:
2254 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2255 break;
2256 case SCHEMAVER_SAMBASAMACCOUNT:
2257 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2258 break;
2259 default:
2260 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2261 break;
2264 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2265 if (!NT_STATUS_IS_OK(ret)) {
2266 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2267 pdb_get_username(newpwd),dn));
2268 ldap_mods_free(mods, true);
2269 goto fn_exit;
2272 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2273 ldap_mods_free(mods, true);
2275 status = NT_STATUS_OK;
2277 fn_exit:
2279 TALLOC_FREE(ctx);
2280 SAFE_FREE(escape_user);
2281 if (result) {
2282 ldap_msgfree(result);
2284 return status;
2287 /**********************************************************************
2288 *********************************************************************/
2290 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2291 const char *filter,
2292 LDAPMessage ** result)
2294 int scope = LDAP_SCOPE_SUBTREE;
2295 int rc;
2296 const char **attr_list;
2298 attr_list = get_attr_list(NULL, groupmap_attr_list);
2299 rc = smbldap_search(ldap_state->smbldap_state,
2300 lp_ldap_suffix (), scope,
2301 filter, attr_list, 0, result);
2302 TALLOC_FREE(attr_list);
2304 return rc;
2307 /**********************************************************************
2308 *********************************************************************/
2310 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2311 GROUP_MAP *map, LDAPMessage *entry)
2313 char *temp = NULL;
2314 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2316 if (ldap_state == NULL || map == NULL || entry == NULL ||
2317 ldap_state->smbldap_state->ldap_struct == NULL) {
2318 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2319 TALLOC_FREE(ctx);
2320 return false;
2323 temp = smbldap_talloc_single_attribute(
2324 ldap_state->smbldap_state->ldap_struct,
2325 entry,
2326 get_attr_key2string(groupmap_attr_list,
2327 LDAP_ATTR_GIDNUMBER),
2328 ctx);
2329 if (!temp) {
2330 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2331 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2332 TALLOC_FREE(ctx);
2333 return false;
2335 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2337 map->gid = (gid_t)atol(temp);
2339 TALLOC_FREE(temp);
2340 temp = smbldap_talloc_single_attribute(
2341 ldap_state->smbldap_state->ldap_struct,
2342 entry,
2343 get_attr_key2string(groupmap_attr_list,
2344 LDAP_ATTR_GROUP_SID),
2345 ctx);
2346 if (!temp) {
2347 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2348 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2349 TALLOC_FREE(ctx);
2350 return false;
2353 if (!string_to_sid(&map->sid, temp)) {
2354 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2355 TALLOC_FREE(ctx);
2356 return false;
2359 TALLOC_FREE(temp);
2360 temp = smbldap_talloc_single_attribute(
2361 ldap_state->smbldap_state->ldap_struct,
2362 entry,
2363 get_attr_key2string(groupmap_attr_list,
2364 LDAP_ATTR_GROUP_TYPE),
2365 ctx);
2366 if (!temp) {
2367 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2368 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2369 TALLOC_FREE(ctx);
2370 return false;
2372 map->sid_name_use = (enum lsa_SidType)atol(temp);
2374 if ((map->sid_name_use < SID_NAME_USER) ||
2375 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2376 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2377 TALLOC_FREE(ctx);
2378 return false;
2381 TALLOC_FREE(temp);
2382 temp = smbldap_talloc_single_attribute(
2383 ldap_state->smbldap_state->ldap_struct,
2384 entry,
2385 get_attr_key2string(groupmap_attr_list,
2386 LDAP_ATTR_DISPLAY_NAME),
2387 ctx);
2388 if (!temp) {
2389 temp = smbldap_talloc_single_attribute(
2390 ldap_state->smbldap_state->ldap_struct,
2391 entry,
2392 get_attr_key2string(groupmap_attr_list,
2393 LDAP_ATTR_CN),
2394 ctx);
2395 if (!temp) {
2396 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2397 for gidNumber(%lu)\n",(unsigned long)map->gid));
2398 TALLOC_FREE(ctx);
2399 return false;
2402 fstrcpy(map->nt_name, temp);
2404 TALLOC_FREE(temp);
2405 temp = smbldap_talloc_single_attribute(
2406 ldap_state->smbldap_state->ldap_struct,
2407 entry,
2408 get_attr_key2string(groupmap_attr_list,
2409 LDAP_ATTR_DESC),
2410 ctx);
2411 if (!temp) {
2412 temp = talloc_strdup(ctx, "");
2413 if (!temp) {
2414 TALLOC_FREE(ctx);
2415 return false;
2418 fstrcpy(map->comment, temp);
2420 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2421 store_gid_sid_cache(&map->sid, map->gid);
2424 TALLOC_FREE(ctx);
2425 return true;
2428 /**********************************************************************
2429 *********************************************************************/
2431 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2432 const char *filter,
2433 GROUP_MAP *map)
2435 struct ldapsam_privates *ldap_state =
2436 (struct ldapsam_privates *)methods->private_data;
2437 LDAPMessage *result = NULL;
2438 LDAPMessage *entry = NULL;
2439 int count;
2441 if (ldapsam_search_one_group(ldap_state, filter, &result)
2442 != LDAP_SUCCESS) {
2443 return NT_STATUS_NO_SUCH_GROUP;
2446 count = ldap_count_entries(priv2ld(ldap_state), result);
2448 if (count < 1) {
2449 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2450 "%s\n", filter));
2451 ldap_msgfree(result);
2452 return NT_STATUS_NO_SUCH_GROUP;
2455 if (count > 1) {
2456 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2457 "count=%d\n", filter, count));
2458 ldap_msgfree(result);
2459 return NT_STATUS_NO_SUCH_GROUP;
2462 entry = ldap_first_entry(priv2ld(ldap_state), result);
2464 if (!entry) {
2465 ldap_msgfree(result);
2466 return NT_STATUS_UNSUCCESSFUL;
2469 if (!init_group_from_ldap(ldap_state, map, entry)) {
2470 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2471 "group filter %s\n", filter));
2472 ldap_msgfree(result);
2473 return NT_STATUS_NO_SUCH_GROUP;
2476 ldap_msgfree(result);
2477 return NT_STATUS_OK;
2480 /**********************************************************************
2481 *********************************************************************/
2483 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2484 DOM_SID sid)
2486 char *filter = NULL;
2487 NTSTATUS status;
2488 fstring tmp;
2490 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2491 LDAP_OBJ_GROUPMAP,
2492 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2493 sid_to_fstring(tmp, &sid)) < 0) {
2494 return NT_STATUS_NO_MEMORY;
2497 status = ldapsam_getgroup(methods, filter, map);
2498 SAFE_FREE(filter);
2499 return status;
2502 /**********************************************************************
2503 *********************************************************************/
2505 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2506 gid_t gid)
2508 char *filter = NULL;
2509 NTSTATUS status;
2511 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2512 LDAP_OBJ_GROUPMAP,
2513 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2514 (unsigned long)gid) < 0) {
2515 return NT_STATUS_NO_MEMORY;
2518 status = ldapsam_getgroup(methods, filter, map);
2519 SAFE_FREE(filter);
2520 return status;
2523 /**********************************************************************
2524 *********************************************************************/
2526 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2527 const char *name)
2529 char *filter = NULL;
2530 char *escape_name = escape_ldap_string_alloc(name);
2531 NTSTATUS status;
2533 if (!escape_name) {
2534 return NT_STATUS_NO_MEMORY;
2537 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2538 LDAP_OBJ_GROUPMAP,
2539 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2540 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2541 escape_name) < 0) {
2542 SAFE_FREE(escape_name);
2543 return NT_STATUS_NO_MEMORY;
2546 SAFE_FREE(escape_name);
2547 status = ldapsam_getgroup(methods, filter, map);
2548 SAFE_FREE(filter);
2549 return status;
2552 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2553 LDAPMessage *entry,
2554 const DOM_SID *domain_sid,
2555 uint32 *rid)
2557 fstring str;
2558 DOM_SID sid;
2560 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2561 str, sizeof(str)-1)) {
2562 DEBUG(10, ("Could not find sambaSID attribute\n"));
2563 return False;
2566 if (!string_to_sid(&sid, str)) {
2567 DEBUG(10, ("Could not convert string %s to sid\n", str));
2568 return False;
2571 if (sid_compare_domain(&sid, domain_sid) != 0) {
2572 DEBUG(10, ("SID %s is not in expected domain %s\n",
2573 str, sid_string_dbg(domain_sid)));
2574 return False;
2577 if (!sid_peek_rid(&sid, rid)) {
2578 DEBUG(10, ("Could not peek into RID\n"));
2579 return False;
2582 return True;
2585 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2586 TALLOC_CTX *mem_ctx,
2587 const DOM_SID *group,
2588 uint32 **pp_member_rids,
2589 size_t *p_num_members)
2591 struct ldapsam_privates *ldap_state =
2592 (struct ldapsam_privates *)methods->private_data;
2593 struct smbldap_state *conn = ldap_state->smbldap_state;
2594 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2595 const char *sid_attrs[] = { "sambaSID", NULL };
2596 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2597 LDAPMessage *result = NULL;
2598 LDAPMessage *entry;
2599 char *filter;
2600 char **values = NULL;
2601 char **memberuid;
2602 char *gidstr;
2603 int rc, count;
2605 *pp_member_rids = NULL;
2606 *p_num_members = 0;
2608 filter = talloc_asprintf(mem_ctx,
2609 "(&(objectClass=%s)"
2610 "(objectClass=%s)"
2611 "(sambaSID=%s))",
2612 LDAP_OBJ_POSIXGROUP,
2613 LDAP_OBJ_GROUPMAP,
2614 sid_string_talloc(mem_ctx, group));
2615 if (filter == NULL) {
2616 ret = NT_STATUS_NO_MEMORY;
2617 goto done;
2620 rc = smbldap_search(conn, lp_ldap_suffix(),
2621 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2622 &result);
2624 if (rc != LDAP_SUCCESS)
2625 goto done;
2627 talloc_autofree_ldapmsg(mem_ctx, result);
2629 count = ldap_count_entries(conn->ldap_struct, result);
2631 if (count > 1) {
2632 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2633 sid_string_dbg(group)));
2634 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2635 goto done;
2638 if (count == 0) {
2639 ret = NT_STATUS_NO_SUCH_GROUP;
2640 goto done;
2643 entry = ldap_first_entry(conn->ldap_struct, result);
2644 if (entry == NULL)
2645 goto done;
2647 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2648 if (!gidstr) {
2649 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2650 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2651 goto done;
2654 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2656 if (values) {
2658 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2659 if (filter == NULL) {
2660 ret = NT_STATUS_NO_MEMORY;
2661 goto done;
2664 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2665 char *escape_memberuid;
2667 escape_memberuid = escape_ldap_string_alloc(*memberuid);
2668 if (escape_memberuid == NULL) {
2669 ret = NT_STATUS_NO_MEMORY;
2670 goto done;
2673 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2674 if (filter == NULL) {
2675 SAFE_FREE(escape_memberuid);
2676 ret = NT_STATUS_NO_MEMORY;
2677 goto done;
2680 SAFE_FREE(escape_memberuid);
2683 filter = talloc_asprintf_append_buffer(filter, "))");
2684 if (filter == NULL) {
2685 ret = NT_STATUS_NO_MEMORY;
2686 goto done;
2689 rc = smbldap_search(conn, lp_ldap_suffix(),
2690 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2691 &result);
2693 if (rc != LDAP_SUCCESS)
2694 goto done;
2696 count = ldap_count_entries(conn->ldap_struct, result);
2697 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2699 talloc_autofree_ldapmsg(mem_ctx, result);
2701 for (entry = ldap_first_entry(conn->ldap_struct, result);
2702 entry != NULL;
2703 entry = ldap_next_entry(conn->ldap_struct, entry))
2705 char *sidstr;
2706 DOM_SID sid;
2707 uint32 rid;
2709 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2710 entry, "sambaSID",
2711 mem_ctx);
2712 if (!sidstr) {
2713 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2714 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2715 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2716 goto done;
2719 if (!string_to_sid(&sid, sidstr))
2720 goto done;
2722 if (!sid_check_is_in_our_domain(&sid)) {
2723 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2724 "in our domain\n"));
2725 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2726 goto done;
2729 sid_peek_rid(&sid, &rid);
2731 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2732 p_num_members)) {
2733 ret = NT_STATUS_NO_MEMORY;
2734 goto done;
2739 filter = talloc_asprintf(mem_ctx,
2740 "(&(objectClass=%s)"
2741 "(gidNumber=%s))",
2742 LDAP_OBJ_SAMBASAMACCOUNT,
2743 gidstr);
2745 rc = smbldap_search(conn, lp_ldap_suffix(),
2746 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2747 &result);
2749 if (rc != LDAP_SUCCESS)
2750 goto done;
2752 talloc_autofree_ldapmsg(mem_ctx, result);
2754 for (entry = ldap_first_entry(conn->ldap_struct, result);
2755 entry != NULL;
2756 entry = ldap_next_entry(conn->ldap_struct, entry))
2758 uint32 rid;
2760 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2761 entry,
2762 get_global_sam_sid(),
2763 &rid)) {
2764 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2765 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2766 goto done;
2769 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2770 p_num_members)) {
2771 ret = NT_STATUS_NO_MEMORY;
2772 goto done;
2776 ret = NT_STATUS_OK;
2778 done:
2780 if (values)
2781 ldap_value_free(values);
2783 return ret;
2786 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2787 TALLOC_CTX *mem_ctx,
2788 struct samu *user,
2789 DOM_SID **pp_sids,
2790 gid_t **pp_gids,
2791 size_t *p_num_groups)
2793 struct ldapsam_privates *ldap_state =
2794 (struct ldapsam_privates *)methods->private_data;
2795 struct smbldap_state *conn = ldap_state->smbldap_state;
2796 char *filter;
2797 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2798 char *escape_name;
2799 int rc, count;
2800 LDAPMessage *result = NULL;
2801 LDAPMessage *entry;
2802 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2803 size_t num_sids, num_gids;
2804 char *gidstr;
2805 gid_t primary_gid = -1;
2807 *pp_sids = NULL;
2808 num_sids = 0;
2810 if (pdb_get_username(user) == NULL) {
2811 return NT_STATUS_INVALID_PARAMETER;
2814 escape_name = escape_ldap_string_alloc(pdb_get_username(user));
2815 if (escape_name == NULL)
2816 return NT_STATUS_NO_MEMORY;
2818 /* retrieve the users primary gid */
2819 filter = talloc_asprintf(mem_ctx,
2820 "(&(objectClass=%s)(uid=%s))",
2821 LDAP_OBJ_SAMBASAMACCOUNT,
2822 escape_name);
2823 if (filter == NULL) {
2824 ret = NT_STATUS_NO_MEMORY;
2825 goto done;
2828 rc = smbldap_search(conn, lp_ldap_suffix(),
2829 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2831 if (rc != LDAP_SUCCESS)
2832 goto done;
2834 talloc_autofree_ldapmsg(mem_ctx, result);
2836 count = ldap_count_entries(priv2ld(ldap_state), result);
2838 switch (count) {
2839 case 0:
2840 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2841 ret = NT_STATUS_NO_SUCH_USER;
2842 goto done;
2843 case 1:
2844 entry = ldap_first_entry(priv2ld(ldap_state), result);
2846 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2847 if (!gidstr) {
2848 DEBUG (1, ("Unable to find the member's gid!\n"));
2849 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2850 goto done;
2852 primary_gid = strtoul(gidstr, NULL, 10);
2853 break;
2854 default:
2855 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2856 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2857 goto done;
2860 filter = talloc_asprintf(mem_ctx,
2861 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2862 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2863 if (filter == NULL) {
2864 ret = NT_STATUS_NO_MEMORY;
2865 goto done;
2868 rc = smbldap_search(conn, lp_ldap_suffix(),
2869 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2871 if (rc != LDAP_SUCCESS)
2872 goto done;
2874 talloc_autofree_ldapmsg(mem_ctx, result);
2876 num_gids = 0;
2877 *pp_gids = NULL;
2879 num_sids = 0;
2880 *pp_sids = NULL;
2882 /* We need to add the primary group as the first gid/sid */
2884 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2885 ret = NT_STATUS_NO_MEMORY;
2886 goto done;
2889 /* This sid will be replaced later */
2891 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2892 &num_sids);
2893 if (!NT_STATUS_IS_OK(ret)) {
2894 goto done;
2897 for (entry = ldap_first_entry(conn->ldap_struct, result);
2898 entry != NULL;
2899 entry = ldap_next_entry(conn->ldap_struct, entry))
2901 fstring str;
2902 DOM_SID sid;
2903 gid_t gid;
2904 char *end;
2906 if (!smbldap_get_single_attribute(conn->ldap_struct,
2907 entry, "sambaSID",
2908 str, sizeof(str)-1))
2909 continue;
2911 if (!string_to_sid(&sid, str))
2912 goto done;
2914 if (!smbldap_get_single_attribute(conn->ldap_struct,
2915 entry, "gidNumber",
2916 str, sizeof(str)-1))
2917 continue;
2919 gid = strtoul(str, &end, 10);
2921 if (PTR_DIFF(end, str) != strlen(str))
2922 goto done;
2924 if (gid == primary_gid) {
2925 sid_copy(&(*pp_sids)[0], &sid);
2926 } else {
2927 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2928 &num_gids)) {
2929 ret = NT_STATUS_NO_MEMORY;
2930 goto done;
2932 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2933 &num_sids);
2934 if (!NT_STATUS_IS_OK(ret)) {
2935 goto done;
2940 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
2941 DEBUG(3, ("primary group of [%s] not found\n",
2942 pdb_get_username(user)));
2943 goto done;
2946 *p_num_groups = num_sids;
2948 ret = NT_STATUS_OK;
2950 done:
2952 SAFE_FREE(escape_name);
2953 return ret;
2956 /**********************************************************************
2957 * Augment a posixGroup object with a sambaGroupMapping domgroup
2958 *********************************************************************/
2960 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
2961 struct ldapsam_privates *ldap_state,
2962 GROUP_MAP *map)
2964 const char *filter, *dn;
2965 LDAPMessage *msg, *entry;
2966 LDAPMod **mods;
2967 int rc;
2969 filter = talloc_asprintf(mem_ctx,
2970 "(&(objectClass=%s)(gidNumber=%u))",
2971 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
2972 if (filter == NULL) {
2973 return NT_STATUS_NO_MEMORY;
2976 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2977 get_attr_list(mem_ctx, groupmap_attr_list),
2978 &msg);
2979 talloc_autofree_ldapmsg(mem_ctx, msg);
2981 if ((rc != LDAP_SUCCESS) ||
2982 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2983 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2984 return NT_STATUS_NO_SUCH_GROUP;
2987 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2988 if (dn == NULL) {
2989 return NT_STATUS_NO_MEMORY;
2992 mods = NULL;
2993 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
2994 LDAP_OBJ_GROUPMAP);
2995 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
2996 sid_string_talloc(mem_ctx, &map->sid));
2997 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
2998 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
2999 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3000 map->nt_name);
3001 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3002 map->comment);
3003 talloc_autofree_ldapmod(mem_ctx, mods);
3005 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3006 if (rc != LDAP_SUCCESS) {
3007 return NT_STATUS_ACCESS_DENIED;
3010 return NT_STATUS_OK;
3013 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3014 GROUP_MAP *map)
3016 struct ldapsam_privates *ldap_state =
3017 (struct ldapsam_privates *)methods->private_data;
3018 LDAPMessage *msg = NULL;
3019 LDAPMod **mods = NULL;
3020 const char *attrs[] = { NULL };
3021 char *filter;
3023 char *dn;
3024 TALLOC_CTX *mem_ctx;
3025 NTSTATUS result;
3027 DOM_SID sid;
3029 int rc;
3031 mem_ctx = talloc_new(NULL);
3032 if (mem_ctx == NULL) {
3033 DEBUG(0, ("talloc_new failed\n"));
3034 return NT_STATUS_NO_MEMORY;
3037 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3038 sid_string_talloc(mem_ctx, &map->sid));
3039 if (filter == NULL) {
3040 result = NT_STATUS_NO_MEMORY;
3041 goto done;
3044 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3045 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3046 talloc_autofree_ldapmsg(mem_ctx, msg);
3048 if ((rc == LDAP_SUCCESS) &&
3049 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3051 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3052 "group mapping entry\n", sid_string_dbg(&map->sid)));
3053 result = NT_STATUS_GROUP_EXISTS;
3054 goto done;
3057 switch (map->sid_name_use) {
3059 case SID_NAME_DOM_GRP:
3060 /* To map a domain group we need to have a posix group
3061 to attach to. */
3062 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3063 goto done;
3064 break;
3066 case SID_NAME_ALIAS:
3067 if (!sid_check_is_in_our_domain(&map->sid)
3068 && !sid_check_is_in_builtin(&map->sid) )
3070 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3071 sid_string_dbg(&map->sid)));
3072 result = NT_STATUS_INVALID_PARAMETER;
3073 goto done;
3075 break;
3077 default:
3078 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3079 sid_type_lookup(map->sid_name_use)));
3080 result = NT_STATUS_INVALID_PARAMETER;
3081 goto done;
3084 /* Domain groups have been mapped in a separate routine, we have to
3085 * create an alias now */
3087 if (map->gid == -1) {
3088 DEBUG(10, ("Refusing to map gid==-1\n"));
3089 result = NT_STATUS_INVALID_PARAMETER;
3090 goto done;
3093 if (pdb_gid_to_sid(map->gid, &sid)) {
3094 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3095 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3096 result = NT_STATUS_GROUP_EXISTS;
3097 goto done;
3100 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3101 * the best we can get out of LDAP. */
3103 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3104 sid_string_talloc(mem_ctx, &map->sid),
3105 lp_ldap_group_suffix());
3106 if (dn == NULL) {
3107 result = NT_STATUS_NO_MEMORY;
3108 goto done;
3111 mods = NULL;
3113 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3114 LDAP_OBJ_SID_ENTRY);
3115 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3116 LDAP_OBJ_GROUPMAP);
3117 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3118 sid_string_talloc(mem_ctx, &map->sid));
3119 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3120 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3121 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3122 map->nt_name);
3123 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3124 map->comment);
3125 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3126 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3127 talloc_autofree_ldapmod(mem_ctx, mods);
3129 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3131 result = (rc == LDAP_SUCCESS) ?
3132 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3134 done:
3135 TALLOC_FREE(mem_ctx);
3136 return result;
3139 /**********************************************************************
3140 * Update a group mapping entry. We're quite strict about what can be changed:
3141 * Only the description and displayname may be changed. It simply does not
3142 * make any sense to change the SID, gid or the type in a mapping.
3143 *********************************************************************/
3145 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3146 GROUP_MAP *map)
3148 struct ldapsam_privates *ldap_state =
3149 (struct ldapsam_privates *)methods->private_data;
3150 int rc;
3151 const char *filter, *dn;
3152 LDAPMessage *msg = NULL;
3153 LDAPMessage *entry = NULL;
3154 LDAPMod **mods = NULL;
3155 TALLOC_CTX *mem_ctx;
3156 NTSTATUS result;
3158 mem_ctx = talloc_new(NULL);
3159 if (mem_ctx == NULL) {
3160 DEBUG(0, ("talloc_new failed\n"));
3161 return NT_STATUS_NO_MEMORY;
3164 /* Make 100% sure that sid, gid and type are not changed by looking up
3165 * exactly the values we're given in LDAP. */
3167 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3168 "(sambaSid=%s)(gidNumber=%u)"
3169 "(sambaGroupType=%d))",
3170 LDAP_OBJ_GROUPMAP,
3171 sid_string_talloc(mem_ctx, &map->sid),
3172 (unsigned int)map->gid, map->sid_name_use);
3173 if (filter == NULL) {
3174 result = NT_STATUS_NO_MEMORY;
3175 goto done;
3178 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3179 get_attr_list(mem_ctx, groupmap_attr_list),
3180 &msg);
3181 talloc_autofree_ldapmsg(mem_ctx, msg);
3183 if ((rc != LDAP_SUCCESS) ||
3184 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3185 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3186 result = NT_STATUS_NO_SUCH_GROUP;
3187 goto done;
3190 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3192 if (dn == NULL) {
3193 result = NT_STATUS_NO_MEMORY;
3194 goto done;
3197 mods = NULL;
3198 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3199 map->nt_name);
3200 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3201 map->comment);
3202 talloc_autofree_ldapmod(mem_ctx, mods);
3204 if (mods == NULL) {
3205 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3206 "nothing to do\n"));
3207 result = NT_STATUS_OK;
3208 goto done;
3211 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3213 if (rc != LDAP_SUCCESS) {
3214 result = NT_STATUS_ACCESS_DENIED;
3215 goto done;
3218 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3219 "group %lu in LDAP\n", (unsigned long)map->gid));
3221 result = NT_STATUS_OK;
3223 done:
3224 TALLOC_FREE(mem_ctx);
3225 return result;
3228 /**********************************************************************
3229 *********************************************************************/
3231 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3232 DOM_SID sid)
3234 struct ldapsam_privates *priv =
3235 (struct ldapsam_privates *)methods->private_data;
3236 LDAPMessage *msg, *entry;
3237 int rc;
3238 NTSTATUS result;
3239 TALLOC_CTX *mem_ctx;
3240 char *filter;
3242 mem_ctx = talloc_new(NULL);
3243 if (mem_ctx == NULL) {
3244 DEBUG(0, ("talloc_new failed\n"));
3245 return NT_STATUS_NO_MEMORY;
3248 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3249 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3250 sid_string_talloc(mem_ctx, &sid));
3251 if (filter == NULL) {
3252 result = NT_STATUS_NO_MEMORY;
3253 goto done;
3255 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3256 get_attr_list(mem_ctx, groupmap_attr_list),
3257 &msg);
3258 talloc_autofree_ldapmsg(mem_ctx, msg);
3260 if ((rc != LDAP_SUCCESS) ||
3261 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3262 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3263 result = NT_STATUS_NO_SUCH_GROUP;
3264 goto done;
3267 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3268 get_attr_list(mem_ctx,
3269 groupmap_attr_list_to_delete));
3271 if ((rc == LDAP_NAMING_VIOLATION) ||
3272 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3273 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3274 const char *attrs[] = { "sambaGroupType", "description",
3275 "displayName", "sambaSIDList",
3276 NULL };
3278 /* Second try. Don't delete the sambaSID attribute, this is
3279 for "old" entries that are tacked on a winbind
3280 sambaIdmapEntry. */
3282 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3283 LDAP_OBJ_GROUPMAP, attrs);
3286 if ((rc == LDAP_NAMING_VIOLATION) ||
3287 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3288 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3289 const char *attrs[] = { "sambaGroupType", "description",
3290 "displayName", "sambaSIDList",
3291 "gidNumber", NULL };
3293 /* Third try. This is a post-3.0.21 alias (containing only
3294 * sambaSidEntry and sambaGroupMapping classes), we also have
3295 * to delete the gidNumber attribute, only the sambaSidEntry
3296 * remains */
3298 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3299 LDAP_OBJ_GROUPMAP, attrs);
3302 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3304 done:
3305 TALLOC_FREE(mem_ctx);
3306 return result;
3309 /**********************************************************************
3310 *********************************************************************/
3312 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3313 bool update)
3315 struct ldapsam_privates *ldap_state =
3316 (struct ldapsam_privates *)my_methods->private_data;
3317 char *filter = NULL;
3318 int rc;
3319 const char **attr_list;
3321 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3322 if (!filter) {
3323 return NT_STATUS_NO_MEMORY;
3325 attr_list = get_attr_list( NULL, groupmap_attr_list );
3326 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3327 LDAP_SCOPE_SUBTREE, filter,
3328 attr_list, 0, &ldap_state->result);
3329 TALLOC_FREE(attr_list);
3331 if (rc != LDAP_SUCCESS) {
3332 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3333 ldap_err2string(rc)));
3334 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3335 lp_ldap_suffix(), filter));
3336 ldap_msgfree(ldap_state->result);
3337 ldap_state->result = NULL;
3338 TALLOC_FREE(filter);
3339 return NT_STATUS_UNSUCCESSFUL;
3342 TALLOC_FREE(filter);
3344 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3345 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3346 ldap_state->result)));
3348 ldap_state->entry =
3349 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3350 ldap_state->result);
3351 ldap_state->index = 0;
3353 return NT_STATUS_OK;
3356 /**********************************************************************
3357 *********************************************************************/
3359 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3361 ldapsam_endsampwent(my_methods);
3364 /**********************************************************************
3365 *********************************************************************/
3367 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3368 GROUP_MAP *map)
3370 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3371 struct ldapsam_privates *ldap_state =
3372 (struct ldapsam_privates *)my_methods->private_data;
3373 bool bret = False;
3375 while (!bret) {
3376 if (!ldap_state->entry)
3377 return ret;
3379 ldap_state->index++;
3380 bret = init_group_from_ldap(ldap_state, map,
3381 ldap_state->entry);
3383 ldap_state->entry =
3384 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3385 ldap_state->entry);
3388 return NT_STATUS_OK;
3391 /**********************************************************************
3392 *********************************************************************/
3394 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3395 const DOM_SID *domsid, enum lsa_SidType sid_name_use,
3396 GROUP_MAP **pp_rmap,
3397 size_t *p_num_entries,
3398 bool unix_only)
3400 GROUP_MAP map;
3401 size_t entries = 0;
3403 *p_num_entries = 0;
3404 *pp_rmap = NULL;
3406 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3407 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3408 "passdb\n"));
3409 return NT_STATUS_ACCESS_DENIED;
3412 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3413 if (sid_name_use != SID_NAME_UNKNOWN &&
3414 sid_name_use != map.sid_name_use) {
3415 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3416 "not of the requested type\n", map.nt_name));
3417 continue;
3419 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3420 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3421 "non mapped\n", map.nt_name));
3422 continue;
3425 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3426 if (!(*pp_rmap)) {
3427 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3428 "enlarge group map!\n"));
3429 return NT_STATUS_UNSUCCESSFUL;
3432 (*pp_rmap)[entries] = map;
3434 entries += 1;
3437 ldapsam_endsamgrent(methods);
3439 *p_num_entries = entries;
3441 return NT_STATUS_OK;
3444 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3445 const DOM_SID *alias,
3446 const DOM_SID *member,
3447 int modop)
3449 struct ldapsam_privates *ldap_state =
3450 (struct ldapsam_privates *)methods->private_data;
3451 char *dn = NULL;
3452 LDAPMessage *result = NULL;
3453 LDAPMessage *entry = NULL;
3454 int count;
3455 LDAPMod **mods = NULL;
3456 int rc;
3457 enum lsa_SidType type = SID_NAME_USE_NONE;
3458 fstring tmp;
3460 char *filter = NULL;
3462 if (sid_check_is_in_builtin(alias)) {
3463 type = SID_NAME_ALIAS;
3466 if (sid_check_is_in_our_domain(alias)) {
3467 type = SID_NAME_ALIAS;
3470 if (type == SID_NAME_USE_NONE) {
3471 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3472 sid_string_dbg(alias)));
3473 return NT_STATUS_NO_SUCH_ALIAS;
3476 if (asprintf(&filter,
3477 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3478 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3479 type) < 0) {
3480 return NT_STATUS_NO_MEMORY;
3483 if (ldapsam_search_one_group(ldap_state, filter,
3484 &result) != LDAP_SUCCESS) {
3485 SAFE_FREE(filter);
3486 return NT_STATUS_NO_SUCH_ALIAS;
3489 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3490 result);
3492 if (count < 1) {
3493 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3494 ldap_msgfree(result);
3495 SAFE_FREE(filter);
3496 return NT_STATUS_NO_SUCH_ALIAS;
3499 if (count > 1) {
3500 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3501 "filter %s: count=%d\n", filter, count));
3502 ldap_msgfree(result);
3503 SAFE_FREE(filter);
3504 return NT_STATUS_NO_SUCH_ALIAS;
3507 SAFE_FREE(filter);
3509 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3510 result);
3512 if (!entry) {
3513 ldap_msgfree(result);
3514 return NT_STATUS_UNSUCCESSFUL;
3517 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3518 if (!dn) {
3519 ldap_msgfree(result);
3520 return NT_STATUS_UNSUCCESSFUL;
3523 smbldap_set_mod(&mods, modop,
3524 get_attr_key2string(groupmap_attr_list,
3525 LDAP_ATTR_SID_LIST),
3526 sid_to_fstring(tmp, member));
3528 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3530 ldap_mods_free(mods, True);
3531 ldap_msgfree(result);
3532 TALLOC_FREE(dn);
3534 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3535 return NT_STATUS_MEMBER_IN_ALIAS;
3538 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3539 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3542 if (rc != LDAP_SUCCESS) {
3543 return NT_STATUS_UNSUCCESSFUL;
3546 return NT_STATUS_OK;
3549 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3550 const DOM_SID *alias,
3551 const DOM_SID *member)
3553 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3556 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3557 const DOM_SID *alias,
3558 const DOM_SID *member)
3560 return ldapsam_modify_aliasmem(methods, alias, member,
3561 LDAP_MOD_DELETE);
3564 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3565 const DOM_SID *alias,
3566 DOM_SID **pp_members,
3567 size_t *p_num_members)
3569 struct ldapsam_privates *ldap_state =
3570 (struct ldapsam_privates *)methods->private_data;
3571 LDAPMessage *result = NULL;
3572 LDAPMessage *entry = NULL;
3573 int count;
3574 char **values = NULL;
3575 int i;
3576 char *filter = NULL;
3577 size_t num_members = 0;
3578 enum lsa_SidType type = SID_NAME_USE_NONE;
3579 fstring tmp;
3581 *pp_members = NULL;
3582 *p_num_members = 0;
3584 if (sid_check_is_in_builtin(alias)) {
3585 type = SID_NAME_ALIAS;
3588 if (sid_check_is_in_our_domain(alias)) {
3589 type = SID_NAME_ALIAS;
3592 if (type == SID_NAME_USE_NONE) {
3593 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3594 sid_string_dbg(alias)));
3595 return NT_STATUS_NO_SUCH_ALIAS;
3598 if (asprintf(&filter,
3599 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3600 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3601 type) < 0) {
3602 return NT_STATUS_NO_MEMORY;
3605 if (ldapsam_search_one_group(ldap_state, filter,
3606 &result) != LDAP_SUCCESS) {
3607 SAFE_FREE(filter);
3608 return NT_STATUS_NO_SUCH_ALIAS;
3611 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3612 result);
3614 if (count < 1) {
3615 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3616 ldap_msgfree(result);
3617 SAFE_FREE(filter);
3618 return NT_STATUS_NO_SUCH_ALIAS;
3621 if (count > 1) {
3622 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3623 "filter %s: count=%d\n", filter, count));
3624 ldap_msgfree(result);
3625 SAFE_FREE(filter);
3626 return NT_STATUS_NO_SUCH_ALIAS;
3629 SAFE_FREE(filter);
3631 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3632 result);
3634 if (!entry) {
3635 ldap_msgfree(result);
3636 return NT_STATUS_UNSUCCESSFUL;
3639 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3640 entry,
3641 get_attr_key2string(groupmap_attr_list,
3642 LDAP_ATTR_SID_LIST));
3644 if (values == NULL) {
3645 ldap_msgfree(result);
3646 return NT_STATUS_OK;
3649 count = ldap_count_values(values);
3651 for (i=0; i<count; i++) {
3652 DOM_SID member;
3653 NTSTATUS status;
3655 if (!string_to_sid(&member, values[i]))
3656 continue;
3658 status = add_sid_to_array(NULL, &member, pp_members,
3659 &num_members);
3660 if (!NT_STATUS_IS_OK(status)) {
3661 ldap_value_free(values);
3662 ldap_msgfree(result);
3663 return status;
3667 *p_num_members = num_members;
3668 ldap_value_free(values);
3669 ldap_msgfree(result);
3671 return NT_STATUS_OK;
3674 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3675 TALLOC_CTX *mem_ctx,
3676 const DOM_SID *domain_sid,
3677 const DOM_SID *members,
3678 size_t num_members,
3679 uint32 **pp_alias_rids,
3680 size_t *p_num_alias_rids)
3682 struct ldapsam_privates *ldap_state =
3683 (struct ldapsam_privates *)methods->private_data;
3684 LDAP *ldap_struct;
3686 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3688 LDAPMessage *result = NULL;
3689 LDAPMessage *entry = NULL;
3690 int i;
3691 int rc;
3692 char *filter;
3693 enum lsa_SidType type = SID_NAME_USE_NONE;
3695 if (sid_check_is_builtin(domain_sid)) {
3696 type = SID_NAME_ALIAS;
3699 if (sid_check_is_domain(domain_sid)) {
3700 type = SID_NAME_ALIAS;
3703 if (type == SID_NAME_USE_NONE) {
3704 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3705 sid_string_dbg(domain_sid)));
3706 return NT_STATUS_UNSUCCESSFUL;
3709 filter = talloc_asprintf(mem_ctx,
3710 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3711 LDAP_OBJ_GROUPMAP, type);
3713 for (i=0; i<num_members; i++)
3714 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3715 filter,
3716 sid_string_talloc(mem_ctx,
3717 &members[i]));
3719 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3721 if (filter == NULL) {
3722 return NT_STATUS_NO_MEMORY;
3725 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3726 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3728 if (rc != LDAP_SUCCESS)
3729 return NT_STATUS_UNSUCCESSFUL;
3731 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3733 for (entry = ldap_first_entry(ldap_struct, result);
3734 entry != NULL;
3735 entry = ldap_next_entry(ldap_struct, entry))
3737 fstring sid_str;
3738 DOM_SID sid;
3739 uint32 rid;
3741 if (!smbldap_get_single_attribute(ldap_struct, entry,
3742 LDAP_ATTRIBUTE_SID,
3743 sid_str,
3744 sizeof(sid_str)-1))
3745 continue;
3747 if (!string_to_sid(&sid, sid_str))
3748 continue;
3750 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3751 continue;
3753 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3754 p_num_alias_rids)) {
3755 ldap_msgfree(result);
3756 return NT_STATUS_NO_MEMORY;
3760 ldap_msgfree(result);
3761 return NT_STATUS_OK;
3764 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3765 int policy_index,
3766 uint32 value)
3768 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3769 int rc;
3770 LDAPMod **mods = NULL;
3771 fstring value_string;
3772 const char *policy_attr = NULL;
3774 struct ldapsam_privates *ldap_state =
3775 (struct ldapsam_privates *)methods->private_data;
3777 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3779 if (!ldap_state->domain_dn) {
3780 return NT_STATUS_INVALID_PARAMETER;
3783 policy_attr = get_account_policy_attr(policy_index);
3784 if (policy_attr == NULL) {
3785 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3786 "policy\n"));
3787 return ntstatus;
3790 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3792 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3794 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3795 mods);
3797 ldap_mods_free(mods, True);
3799 if (rc != LDAP_SUCCESS) {
3800 return ntstatus;
3803 if (!cache_account_policy_set(policy_index, value)) {
3804 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3805 "update local tdb cache\n"));
3806 return ntstatus;
3809 return NT_STATUS_OK;
3812 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3813 int policy_index, uint32 value)
3815 return ldapsam_set_account_policy_in_ldap(methods, policy_index,
3816 value);
3819 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3820 int policy_index,
3821 uint32 *value)
3823 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3824 LDAPMessage *result = NULL;
3825 LDAPMessage *entry = NULL;
3826 int count;
3827 int rc;
3828 char **vals = NULL;
3829 const char *policy_attr = NULL;
3831 struct ldapsam_privates *ldap_state =
3832 (struct ldapsam_privates *)methods->private_data;
3834 const char *attrs[2];
3836 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3838 if (!ldap_state->domain_dn) {
3839 return NT_STATUS_INVALID_PARAMETER;
3842 policy_attr = get_account_policy_attr(policy_index);
3843 if (!policy_attr) {
3844 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3845 "policy index: %d\n", policy_index));
3846 return ntstatus;
3849 attrs[0] = policy_attr;
3850 attrs[1] = NULL;
3852 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3853 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
3854 &result);
3856 if (rc != LDAP_SUCCESS) {
3857 return ntstatus;
3860 count = ldap_count_entries(priv2ld(ldap_state), result);
3861 if (count < 1) {
3862 goto out;
3865 entry = ldap_first_entry(priv2ld(ldap_state), result);
3866 if (entry == NULL) {
3867 goto out;
3870 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3871 if (vals == NULL) {
3872 goto out;
3875 *value = (uint32)atol(vals[0]);
3877 ntstatus = NT_STATUS_OK;
3879 out:
3880 if (vals)
3881 ldap_value_free(vals);
3882 ldap_msgfree(result);
3884 return ntstatus;
3887 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3889 - if user hasn't decided to use account policies inside LDAP just reuse the
3890 old tdb values
3892 - if there is a valid cache entry, return that
3893 - if there is an LDAP entry, update cache and return
3894 - otherwise set to default, update cache and return
3896 Guenther
3898 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
3899 int policy_index, uint32 *value)
3901 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3903 if (cache_account_policy_get(policy_index, value)) {
3904 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3905 "cache\n"));
3906 return NT_STATUS_OK;
3909 ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
3910 value);
3911 if (NT_STATUS_IS_OK(ntstatus)) {
3912 goto update_cache;
3915 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3916 "ldap\n"));
3918 #if 0
3919 /* should we automagically migrate old tdb value here ? */
3920 if (account_policy_get(policy_index, value))
3921 goto update_ldap;
3923 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3924 "default\n", policy_index));
3925 #endif
3927 if (!account_policy_get_default(policy_index, value)) {
3928 return ntstatus;
3931 /* update_ldap: */
3933 ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
3934 if (!NT_STATUS_IS_OK(ntstatus)) {
3935 return ntstatus;
3938 update_cache:
3940 if (!cache_account_policy_set(policy_index, *value)) {
3941 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3942 "tdb as a cache\n"));
3943 return NT_STATUS_UNSUCCESSFUL;
3946 return NT_STATUS_OK;
3949 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
3950 const DOM_SID *domain_sid,
3951 int num_rids,
3952 uint32 *rids,
3953 const char **names,
3954 enum lsa_SidType *attrs)
3956 struct ldapsam_privates *ldap_state =
3957 (struct ldapsam_privates *)methods->private_data;
3958 LDAPMessage *msg = NULL;
3959 LDAPMessage *entry;
3960 char *allsids = NULL;
3961 int i, rc, num_mapped;
3962 NTSTATUS result = NT_STATUS_NO_MEMORY;
3963 TALLOC_CTX *mem_ctx;
3964 LDAP *ld;
3965 bool is_builtin;
3967 mem_ctx = talloc_new(NULL);
3968 if (mem_ctx == NULL) {
3969 DEBUG(0, ("talloc_new failed\n"));
3970 goto done;
3973 if (!sid_check_is_builtin(domain_sid) &&
3974 !sid_check_is_domain(domain_sid)) {
3975 result = NT_STATUS_INVALID_PARAMETER;
3976 goto done;
3979 for (i=0; i<num_rids; i++)
3980 attrs[i] = SID_NAME_UNKNOWN;
3982 allsids = talloc_strdup(mem_ctx, "");
3983 if (allsids == NULL) {
3984 goto done;
3987 for (i=0; i<num_rids; i++) {
3988 DOM_SID sid;
3989 sid_compose(&sid, domain_sid, rids[i]);
3990 allsids = talloc_asprintf_append_buffer(
3991 allsids, "(sambaSid=%s)",
3992 sid_string_talloc(mem_ctx, &sid));
3993 if (allsids == NULL) {
3994 goto done;
3998 /* First look for users */
4001 char *filter;
4002 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4004 filter = talloc_asprintf(
4005 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4006 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4008 if (filter == NULL) {
4009 goto done;
4012 rc = smbldap_search(ldap_state->smbldap_state,
4013 lp_ldap_user_suffix(),
4014 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4015 &msg);
4016 talloc_autofree_ldapmsg(mem_ctx, msg);
4019 if (rc != LDAP_SUCCESS)
4020 goto done;
4022 ld = ldap_state->smbldap_state->ldap_struct;
4023 num_mapped = 0;
4025 for (entry = ldap_first_entry(ld, msg);
4026 entry != NULL;
4027 entry = ldap_next_entry(ld, entry)) {
4028 uint32 rid;
4029 int rid_index;
4030 const char *name;
4032 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4033 &rid)) {
4034 DEBUG(2, ("Could not find sid from ldap entry\n"));
4035 continue;
4038 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4039 names);
4040 if (name == NULL) {
4041 DEBUG(2, ("Could not retrieve uid attribute\n"));
4042 continue;
4045 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4046 if (rid == rids[rid_index])
4047 break;
4050 if (rid_index == num_rids) {
4051 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4052 continue;
4055 attrs[rid_index] = SID_NAME_USER;
4056 names[rid_index] = name;
4057 num_mapped += 1;
4060 if (num_mapped == num_rids) {
4061 /* No need to look for groups anymore -- we're done */
4062 result = NT_STATUS_OK;
4063 goto done;
4066 /* Same game for groups */
4069 char *filter;
4070 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4071 "sambaGroupType", NULL };
4073 filter = talloc_asprintf(
4074 mem_ctx, "(&(objectClass=%s)(|%s))",
4075 LDAP_OBJ_GROUPMAP, allsids);
4076 if (filter == NULL) {
4077 goto done;
4080 rc = smbldap_search(ldap_state->smbldap_state,
4081 lp_ldap_suffix(),
4082 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4083 &msg);
4084 talloc_autofree_ldapmsg(mem_ctx, msg);
4087 if (rc != LDAP_SUCCESS)
4088 goto done;
4090 /* ldap_struct might have changed due to a reconnect */
4092 ld = ldap_state->smbldap_state->ldap_struct;
4094 /* For consistency checks, we already checked we're only domain or builtin */
4096 is_builtin = sid_check_is_builtin(domain_sid);
4098 for (entry = ldap_first_entry(ld, msg);
4099 entry != NULL;
4100 entry = ldap_next_entry(ld, entry))
4102 uint32 rid;
4103 int rid_index;
4104 const char *attr;
4105 enum lsa_SidType type;
4106 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4108 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4109 mem_ctx);
4110 if (attr == NULL) {
4111 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4112 dn));
4113 continue;
4116 type = (enum lsa_SidType)atol(attr);
4118 /* Consistency checks */
4119 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4120 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4121 (type != SID_NAME_DOM_GRP)))) {
4122 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4125 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4126 &rid)) {
4127 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4128 continue;
4131 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4133 if (attr == NULL) {
4134 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4135 dn));
4136 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4139 if (attr == NULL) {
4140 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4141 dn));
4142 continue;
4145 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4146 if (rid == rids[rid_index])
4147 break;
4150 if (rid_index == num_rids) {
4151 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4152 continue;
4155 attrs[rid_index] = type;
4156 names[rid_index] = attr;
4157 num_mapped += 1;
4160 result = NT_STATUS_NONE_MAPPED;
4162 if (num_mapped > 0)
4163 result = (num_mapped == num_rids) ?
4164 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4165 done:
4166 TALLOC_FREE(mem_ctx);
4167 return result;
4170 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4172 char *filter = NULL;
4173 char *escaped = NULL;
4174 char *result = NULL;
4176 if (asprintf(&filter, "(&%s(objectclass=%s))",
4177 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4178 goto done;
4181 escaped = escape_ldap_string_alloc(username);
4182 if (escaped == NULL) goto done;
4184 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4186 done:
4187 SAFE_FREE(filter);
4188 SAFE_FREE(escaped);
4190 return result;
4193 const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4195 int i, num = 0;
4196 va_list ap;
4197 const char **result;
4199 va_start(ap, mem_ctx);
4200 while (va_arg(ap, const char *) != NULL)
4201 num += 1;
4202 va_end(ap);
4204 if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
4205 return NULL;
4208 va_start(ap, mem_ctx);
4209 for (i=0; i<num; i++) {
4210 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4211 if (result[i] == NULL) {
4212 talloc_free(result);
4213 va_end(ap);
4214 return NULL;
4217 va_end(ap);
4219 result[num] = NULL;
4220 return result;
4223 struct ldap_search_state {
4224 struct smbldap_state *connection;
4226 uint32 acct_flags;
4227 uint16 group_type;
4229 const char *base;
4230 int scope;
4231 const char *filter;
4232 const char **attrs;
4233 int attrsonly;
4234 void *pagedresults_cookie;
4236 LDAPMessage *entries, *current_entry;
4237 bool (*ldap2displayentry)(struct ldap_search_state *state,
4238 TALLOC_CTX *mem_ctx,
4239 LDAP *ld, LDAPMessage *entry,
4240 struct samr_displayentry *result);
4243 static bool ldapsam_search_firstpage(struct pdb_search *search)
4245 struct ldap_search_state *state =
4246 (struct ldap_search_state *)search->private_data;
4247 LDAP *ld;
4248 int rc = LDAP_OPERATIONS_ERROR;
4250 state->entries = NULL;
4252 if (state->connection->paged_results) {
4253 rc = smbldap_search_paged(state->connection, state->base,
4254 state->scope, state->filter,
4255 state->attrs, state->attrsonly,
4256 lp_ldap_page_size(), &state->entries,
4257 &state->pagedresults_cookie);
4260 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4262 if (state->entries != NULL) {
4263 /* Left over from unsuccessful paged attempt */
4264 ldap_msgfree(state->entries);
4265 state->entries = NULL;
4268 rc = smbldap_search(state->connection, state->base,
4269 state->scope, state->filter, state->attrs,
4270 state->attrsonly, &state->entries);
4272 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4273 return False;
4275 /* Ok, the server was lying. It told us it could do paged
4276 * searches when it could not. */
4277 state->connection->paged_results = False;
4280 ld = state->connection->ldap_struct;
4281 if ( ld == NULL) {
4282 DEBUG(5, ("Don't have an LDAP connection right after a "
4283 "search\n"));
4284 return False;
4286 state->current_entry = ldap_first_entry(ld, state->entries);
4288 if (state->current_entry == NULL) {
4289 ldap_msgfree(state->entries);
4290 state->entries = NULL;
4293 return True;
4296 static bool ldapsam_search_nextpage(struct pdb_search *search)
4298 struct ldap_search_state *state =
4299 (struct ldap_search_state *)search->private_data;
4300 int rc;
4302 if (!state->connection->paged_results) {
4303 /* There is no next page when there are no paged results */
4304 return False;
4307 rc = smbldap_search_paged(state->connection, state->base,
4308 state->scope, state->filter, state->attrs,
4309 state->attrsonly, lp_ldap_page_size(),
4310 &state->entries,
4311 &state->pagedresults_cookie);
4313 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4314 return False;
4316 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4318 if (state->current_entry == NULL) {
4319 ldap_msgfree(state->entries);
4320 state->entries = NULL;
4323 return True;
4326 static bool ldapsam_search_next_entry(struct pdb_search *search,
4327 struct samr_displayentry *entry)
4329 struct ldap_search_state *state =
4330 (struct ldap_search_state *)search->private_data;
4331 bool result;
4333 retry:
4334 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4335 return False;
4337 if ((state->entries == NULL) &&
4338 !ldapsam_search_nextpage(search))
4339 return False;
4341 result = state->ldap2displayentry(state, search,
4342 state->connection->ldap_struct,
4343 state->current_entry, entry);
4345 if (!result) {
4346 char *dn;
4347 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4348 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4349 if (dn != NULL) ldap_memfree(dn);
4352 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4354 if (state->current_entry == NULL) {
4355 ldap_msgfree(state->entries);
4356 state->entries = NULL;
4359 if (!result) goto retry;
4361 return True;
4364 static void ldapsam_search_end(struct pdb_search *search)
4366 struct ldap_search_state *state =
4367 (struct ldap_search_state *)search->private_data;
4368 int rc;
4370 if (state->pagedresults_cookie == NULL)
4371 return;
4373 if (state->entries != NULL)
4374 ldap_msgfree(state->entries);
4376 state->entries = NULL;
4377 state->current_entry = NULL;
4379 if (!state->connection->paged_results)
4380 return;
4382 /* Tell the LDAP server we're not interested in the rest anymore. */
4384 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4385 state->filter, state->attrs,
4386 state->attrsonly, 0, &state->entries,
4387 &state->pagedresults_cookie);
4389 if (rc != LDAP_SUCCESS)
4390 DEBUG(5, ("Could not end search properly\n"));
4392 return;
4395 static bool ldapuser2displayentry(struct ldap_search_state *state,
4396 TALLOC_CTX *mem_ctx,
4397 LDAP *ld, LDAPMessage *entry,
4398 struct samr_displayentry *result)
4400 char **vals;
4401 size_t converted_size;
4402 DOM_SID sid;
4403 uint32 acct_flags;
4405 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4406 if ((vals == NULL) || (vals[0] == NULL)) {
4407 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4408 return False;
4410 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4411 ldap_value_free(vals);
4413 if ((state->acct_flags != 0) &&
4414 ((state->acct_flags & acct_flags) == 0))
4415 return False;
4417 result->acct_flags = acct_flags;
4418 result->account_name = "";
4419 result->fullname = "";
4420 result->description = "";
4422 vals = ldap_get_values(ld, entry, "uid");
4423 if ((vals == NULL) || (vals[0] == NULL)) {
4424 DEBUG(5, ("\"uid\" not found\n"));
4425 return False;
4427 if (!pull_utf8_talloc(mem_ctx,
4428 CONST_DISCARD(char **, &result->account_name),
4429 vals[0], &converted_size))
4431 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4432 strerror(errno)));
4435 ldap_value_free(vals);
4437 vals = ldap_get_values(ld, entry, "displayName");
4438 if ((vals == NULL) || (vals[0] == NULL))
4439 DEBUG(8, ("\"displayName\" not found\n"));
4440 else if (!pull_utf8_talloc(mem_ctx,
4441 CONST_DISCARD(char **, &result->fullname),
4442 vals[0], &converted_size))
4444 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4445 strerror(errno)));
4448 ldap_value_free(vals);
4450 vals = ldap_get_values(ld, entry, "description");
4451 if ((vals == NULL) || (vals[0] == NULL))
4452 DEBUG(8, ("\"description\" not found\n"));
4453 else if (!pull_utf8_talloc(mem_ctx,
4454 CONST_DISCARD(char **, &result->description),
4455 vals[0], &converted_size))
4457 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4458 strerror(errno)));
4461 ldap_value_free(vals);
4463 if ((result->account_name == NULL) ||
4464 (result->fullname == NULL) ||
4465 (result->description == NULL)) {
4466 DEBUG(0, ("talloc failed\n"));
4467 return False;
4470 vals = ldap_get_values(ld, entry, "sambaSid");
4471 if ((vals == NULL) || (vals[0] == NULL)) {
4472 DEBUG(0, ("\"objectSid\" not found\n"));
4473 return False;
4476 if (!string_to_sid(&sid, vals[0])) {
4477 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4478 ldap_value_free(vals);
4479 return False;
4481 ldap_value_free(vals);
4483 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4484 DEBUG(0, ("sid %s does not belong to our domain\n",
4485 sid_string_dbg(&sid)));
4486 return False;
4489 return True;
4493 static bool ldapsam_search_users(struct pdb_methods *methods,
4494 struct pdb_search *search,
4495 uint32 acct_flags)
4497 struct ldapsam_privates *ldap_state =
4498 (struct ldapsam_privates *)methods->private_data;
4499 struct ldap_search_state *state;
4501 state = talloc(search, struct ldap_search_state);
4502 if (state == NULL) {
4503 DEBUG(0, ("talloc failed\n"));
4504 return False;
4507 state->connection = ldap_state->smbldap_state;
4509 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4510 state->base = lp_ldap_user_suffix();
4511 else if ((acct_flags != 0) &&
4512 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4513 state->base = lp_ldap_machine_suffix();
4514 else
4515 state->base = lp_ldap_suffix();
4517 state->acct_flags = acct_flags;
4518 state->base = talloc_strdup(search, state->base);
4519 state->scope = LDAP_SCOPE_SUBTREE;
4520 state->filter = get_ldap_filter(search, "*");
4521 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4522 "displayName", "description",
4523 "sambaAcctFlags", NULL);
4524 state->attrsonly = 0;
4525 state->pagedresults_cookie = NULL;
4526 state->entries = NULL;
4527 state->ldap2displayentry = ldapuser2displayentry;
4529 if ((state->filter == NULL) || (state->attrs == NULL)) {
4530 DEBUG(0, ("talloc failed\n"));
4531 return False;
4534 search->private_data = state;
4535 search->next_entry = ldapsam_search_next_entry;
4536 search->search_end = ldapsam_search_end;
4538 return ldapsam_search_firstpage(search);
4541 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4542 TALLOC_CTX *mem_ctx,
4543 LDAP *ld, LDAPMessage *entry,
4544 struct samr_displayentry *result)
4546 char **vals;
4547 size_t converted_size;
4548 DOM_SID sid;
4549 uint16 group_type;
4551 result->account_name = "";
4552 result->fullname = "";
4553 result->description = "";
4556 vals = ldap_get_values(ld, entry, "sambaGroupType");
4557 if ((vals == NULL) || (vals[0] == NULL)) {
4558 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4559 if (vals != NULL) {
4560 ldap_value_free(vals);
4562 return False;
4565 group_type = atoi(vals[0]);
4567 if ((state->group_type != 0) &&
4568 ((state->group_type != group_type))) {
4569 ldap_value_free(vals);
4570 return False;
4573 ldap_value_free(vals);
4575 /* display name is the NT group name */
4577 vals = ldap_get_values(ld, entry, "displayName");
4578 if ((vals == NULL) || (vals[0] == NULL)) {
4579 DEBUG(8, ("\"displayName\" not found\n"));
4581 /* fallback to the 'cn' attribute */
4582 vals = ldap_get_values(ld, entry, "cn");
4583 if ((vals == NULL) || (vals[0] == NULL)) {
4584 DEBUG(5, ("\"cn\" not found\n"));
4585 return False;
4587 if (!pull_utf8_talloc(mem_ctx,
4588 CONST_DISCARD(char **,
4589 &result->account_name),
4590 vals[0], &converted_size))
4592 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4593 "failed: %s", strerror(errno)));
4596 else if (!pull_utf8_talloc(mem_ctx,
4597 CONST_DISCARD(char **,
4598 &result->account_name),
4599 vals[0], &converted_size))
4601 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4602 strerror(errno)));
4605 ldap_value_free(vals);
4607 vals = ldap_get_values(ld, entry, "description");
4608 if ((vals == NULL) || (vals[0] == NULL))
4609 DEBUG(8, ("\"description\" not found\n"));
4610 else if (!pull_utf8_talloc(mem_ctx,
4611 CONST_DISCARD(char **, &result->description),
4612 vals[0], &converted_size))
4614 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4615 strerror(errno)));
4617 ldap_value_free(vals);
4619 if ((result->account_name == NULL) ||
4620 (result->fullname == NULL) ||
4621 (result->description == NULL)) {
4622 DEBUG(0, ("talloc failed\n"));
4623 return False;
4626 vals = ldap_get_values(ld, entry, "sambaSid");
4627 if ((vals == NULL) || (vals[0] == NULL)) {
4628 DEBUG(0, ("\"objectSid\" not found\n"));
4629 if (vals != NULL) {
4630 ldap_value_free(vals);
4632 return False;
4635 if (!string_to_sid(&sid, vals[0])) {
4636 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4637 return False;
4640 ldap_value_free(vals);
4642 switch (group_type) {
4643 case SID_NAME_DOM_GRP:
4644 case SID_NAME_ALIAS:
4646 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4647 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4649 DEBUG(0, ("%s is not in our domain\n",
4650 sid_string_dbg(&sid)));
4651 return False;
4653 break;
4655 default:
4656 DEBUG(0,("unkown group type: %d\n", group_type));
4657 return False;
4660 result->acct_flags = 0;
4662 return True;
4665 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4666 struct pdb_search *search,
4667 const DOM_SID *sid,
4668 enum lsa_SidType type)
4670 struct ldapsam_privates *ldap_state =
4671 (struct ldapsam_privates *)methods->private_data;
4672 struct ldap_search_state *state;
4673 fstring tmp;
4675 state = talloc(search, struct ldap_search_state);
4676 if (state == NULL) {
4677 DEBUG(0, ("talloc failed\n"));
4678 return False;
4681 state->connection = ldap_state->smbldap_state;
4683 state->base = talloc_strdup(search, lp_ldap_suffix());
4684 state->connection = ldap_state->smbldap_state;
4685 state->scope = LDAP_SCOPE_SUBTREE;
4686 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4687 "(sambaGroupType=%d)(sambaSID=%s*))",
4688 LDAP_OBJ_GROUPMAP,
4689 type, sid_to_fstring(tmp, sid));
4690 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4691 "displayName", "description",
4692 "sambaGroupType", NULL);
4693 state->attrsonly = 0;
4694 state->pagedresults_cookie = NULL;
4695 state->entries = NULL;
4696 state->group_type = type;
4697 state->ldap2displayentry = ldapgroup2displayentry;
4699 if ((state->filter == NULL) || (state->attrs == NULL)) {
4700 DEBUG(0, ("talloc failed\n"));
4701 return False;
4704 search->private_data = state;
4705 search->next_entry = ldapsam_search_next_entry;
4706 search->search_end = ldapsam_search_end;
4708 return ldapsam_search_firstpage(search);
4711 static bool ldapsam_search_groups(struct pdb_methods *methods,
4712 struct pdb_search *search)
4714 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4717 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4718 struct pdb_search *search,
4719 const DOM_SID *sid)
4721 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4724 static bool ldapsam_rid_algorithm(struct pdb_methods *methods)
4726 return False;
4729 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4730 uint32 *rid)
4732 struct smbldap_state *smbldap_state = priv->smbldap_state;
4734 LDAPMessage *result = NULL;
4735 LDAPMessage *entry = NULL;
4736 LDAPMod **mods = NULL;
4737 NTSTATUS status;
4738 char *value;
4739 int rc;
4740 uint32 nextRid = 0;
4741 const char *dn;
4743 TALLOC_CTX *mem_ctx;
4745 mem_ctx = talloc_new(NULL);
4746 if (mem_ctx == NULL) {
4747 DEBUG(0, ("talloc_new failed\n"));
4748 return NT_STATUS_NO_MEMORY;
4751 status = smbldap_search_domain_info(smbldap_state, &result,
4752 get_global_sam_name(), False);
4753 if (!NT_STATUS_IS_OK(status)) {
4754 DEBUG(3, ("Could not get domain info: %s\n",
4755 nt_errstr(status)));
4756 goto done;
4759 talloc_autofree_ldapmsg(mem_ctx, result);
4761 entry = ldap_first_entry(priv2ld(priv), result);
4762 if (entry == NULL) {
4763 DEBUG(0, ("Could not get domain info entry\n"));
4764 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4765 goto done;
4768 /* Find the largest of the three attributes "sambaNextRid",
4769 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4770 concept of differentiating between user and group rids, and will
4771 use only "sambaNextRid" in the future. But for compatibility
4772 reasons I look if others have chosen different strategies -- VL */
4774 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4775 "sambaNextRid", mem_ctx);
4776 if (value != NULL) {
4777 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4778 nextRid = MAX(nextRid, tmp);
4781 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4782 "sambaNextUserRid", mem_ctx);
4783 if (value != NULL) {
4784 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4785 nextRid = MAX(nextRid, tmp);
4788 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4789 "sambaNextGroupRid", mem_ctx);
4790 if (value != NULL) {
4791 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4792 nextRid = MAX(nextRid, tmp);
4795 if (nextRid == 0) {
4796 nextRid = BASE_RID-1;
4799 nextRid += 1;
4801 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4802 talloc_asprintf(mem_ctx, "%d", nextRid));
4803 talloc_autofree_ldapmod(mem_ctx, mods);
4805 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4806 status = NT_STATUS_NO_MEMORY;
4807 goto done;
4810 rc = smbldap_modify(smbldap_state, dn, mods);
4812 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4813 * please retry" */
4815 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4817 done:
4818 if (NT_STATUS_IS_OK(status)) {
4819 *rid = nextRid;
4822 TALLOC_FREE(mem_ctx);
4823 return status;
4826 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32 *rid)
4828 int i;
4830 for (i=0; i<10; i++) {
4831 NTSTATUS result = ldapsam_get_new_rid(
4832 (struct ldapsam_privates *)methods->private_data, rid);
4833 if (NT_STATUS_IS_OK(result)) {
4834 return result;
4837 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4838 return result;
4841 /* The ldap update failed (maybe a race condition), retry */
4844 /* Tried 10 times, fail. */
4845 return NT_STATUS_ACCESS_DENIED;
4848 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32 *rid)
4850 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4851 return NT_STATUS_IS_OK(result) ? True : False;
4854 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4855 const DOM_SID *sid,
4856 union unid_t *id, enum lsa_SidType *type)
4858 struct ldapsam_privates *priv =
4859 (struct ldapsam_privates *)methods->private_data;
4860 char *filter;
4861 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4862 NULL };
4863 LDAPMessage *result = NULL;
4864 LDAPMessage *entry = NULL;
4865 bool ret = False;
4866 char *value;
4867 int rc;
4869 TALLOC_CTX *mem_ctx;
4871 mem_ctx = talloc_new(NULL);
4872 if (mem_ctx == NULL) {
4873 DEBUG(0, ("talloc_new failed\n"));
4874 return False;
4877 filter = talloc_asprintf(mem_ctx,
4878 "(&(sambaSid=%s)"
4879 "(|(objectClass=%s)(objectClass=%s)))",
4880 sid_string_talloc(mem_ctx, sid),
4881 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
4882 if (filter == NULL) {
4883 DEBUG(5, ("talloc_asprintf failed\n"));
4884 goto done;
4887 rc = smbldap_search_suffix(priv->smbldap_state, filter,
4888 attrs, &result);
4889 if (rc != LDAP_SUCCESS) {
4890 goto done;
4892 talloc_autofree_ldapmsg(mem_ctx, result);
4894 if (ldap_count_entries(priv2ld(priv), result) != 1) {
4895 DEBUG(10, ("Got %d entries, expected one\n",
4896 ldap_count_entries(priv2ld(priv), result)));
4897 goto done;
4900 entry = ldap_first_entry(priv2ld(priv), result);
4902 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4903 "sambaGroupType", mem_ctx);
4905 if (value != NULL) {
4906 const char *gid_str;
4907 /* It's a group */
4909 gid_str = smbldap_talloc_single_attribute(
4910 priv2ld(priv), entry, "gidNumber", mem_ctx);
4911 if (gid_str == NULL) {
4912 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4913 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
4914 entry)));
4915 goto done;
4918 id->gid = strtoul(gid_str, NULL, 10);
4919 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
4920 ret = True;
4921 goto done;
4924 /* It must be a user */
4926 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4927 "uidNumber", mem_ctx);
4928 if (value == NULL) {
4929 DEBUG(1, ("Could not find uidNumber in %s\n",
4930 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
4931 goto done;
4934 id->uid = strtoul(value, NULL, 10);
4935 *type = SID_NAME_USER;
4937 ret = True;
4938 done:
4939 TALLOC_FREE(mem_ctx);
4940 return ret;
4944 * The following functions is called only if
4945 * ldapsam:trusted and ldapsam:editposix are
4946 * set to true
4950 * ldapsam_create_user creates a new
4951 * posixAccount and sambaSamAccount object
4952 * in the ldap users subtree
4954 * The uid is allocated by winbindd.
4957 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
4958 TALLOC_CTX *tmp_ctx, const char *name,
4959 uint32 acb_info, uint32 *rid)
4961 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
4962 LDAPMessage *entry = NULL;
4963 LDAPMessage *result = NULL;
4964 uint32 num_result;
4965 bool is_machine = False;
4966 bool add_posix = False;
4967 LDAPMod **mods = NULL;
4968 struct samu *user;
4969 char *filter;
4970 char *username;
4971 char *homedir;
4972 char *gidstr;
4973 char *uidstr;
4974 char *shell;
4975 const char *dn = NULL;
4976 DOM_SID group_sid;
4977 DOM_SID user_sid;
4978 gid_t gid = -1;
4979 uid_t uid = -1;
4980 NTSTATUS ret;
4981 int rc;
4983 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
4984 acb_info & ACB_WSTRUST ||
4985 acb_info & ACB_SVRTRUST ||
4986 acb_info & ACB_DOMTRUST) {
4987 is_machine = True;
4990 username = escape_ldap_string_alloc(name);
4991 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
4992 username, LDAP_OBJ_POSIXACCOUNT);
4993 SAFE_FREE(username);
4995 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
4996 if (rc != LDAP_SUCCESS) {
4997 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
4998 return NT_STATUS_ACCESS_DENIED;
5000 talloc_autofree_ldapmsg(tmp_ctx, result);
5002 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5004 if (num_result > 1) {
5005 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5006 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5009 if (num_result == 1) {
5010 char *tmp;
5011 /* check if it is just a posix account.
5012 * or if there is a sid attached to this entry
5015 entry = ldap_first_entry(priv2ld(ldap_state), result);
5016 if (!entry) {
5017 return NT_STATUS_UNSUCCESSFUL;
5020 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5021 if (tmp) {
5022 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5023 return NT_STATUS_USER_EXISTS;
5026 /* it is just a posix account, retrieve the dn for later use */
5027 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5028 if (!dn) {
5029 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5030 return NT_STATUS_NO_MEMORY;
5034 if (num_result == 0) {
5035 add_posix = True;
5038 /* Create the basic samu structure and generate the mods for the ldap commit */
5039 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5040 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5041 return ret;
5044 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5046 user = samu_new(tmp_ctx);
5047 if (!user) {
5048 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5049 return NT_STATUS_NO_MEMORY;
5052 if (!pdb_set_username(user, name, PDB_SET)) {
5053 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5054 return NT_STATUS_UNSUCCESSFUL;
5056 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5057 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5058 return NT_STATUS_UNSUCCESSFUL;
5060 if (is_machine) {
5061 if (acb_info & ACB_NORMAL) {
5062 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5063 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5064 return NT_STATUS_UNSUCCESSFUL;
5066 } else {
5067 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5068 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5069 return NT_STATUS_UNSUCCESSFUL;
5072 } else {
5073 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5074 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5075 return NT_STATUS_UNSUCCESSFUL;
5079 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5080 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5081 return NT_STATUS_UNSUCCESSFUL;
5084 if (!init_ldap_from_sam(ldap_state, NULL, &mods, user, element_is_set_or_changed)) {
5085 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5086 return NT_STATUS_UNSUCCESSFUL;
5089 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5090 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5092 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5094 if (add_posix) {
5095 char *escape_name;
5097 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5099 /* retrieve the Domain Users group gid */
5100 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS) ||
5101 !sid_to_gid(&group_sid, &gid)) {
5102 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5103 return NT_STATUS_INVALID_PRIMARY_GROUP;
5106 /* lets allocate a new userid for this user */
5107 if (!winbind_allocate_uid(&uid)) {
5108 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5109 return NT_STATUS_UNSUCCESSFUL;
5113 if (is_machine) {
5114 /* TODO: choose a more appropriate default for machines */
5115 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5116 shell = talloc_strdup(tmp_ctx, "/bin/false");
5117 } else {
5118 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5119 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5121 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5122 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5124 escape_name = escape_rdn_val_string_alloc(name);
5125 if (!escape_name) {
5126 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5127 return NT_STATUS_NO_MEMORY;
5130 if (is_machine) {
5131 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5132 } else {
5133 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5136 SAFE_FREE(escape_name);
5138 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5139 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5140 return NT_STATUS_NO_MEMORY;
5143 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5144 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5145 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5146 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5147 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5148 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5149 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5152 talloc_autofree_ldapmod(tmp_ctx, mods);
5154 if (add_posix) {
5155 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5156 } else {
5157 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5160 if (rc != LDAP_SUCCESS) {
5161 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5162 return NT_STATUS_UNSUCCESSFUL;
5165 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5167 flush_pwnam_cache();
5169 return NT_STATUS_OK;
5172 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5174 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5175 LDAPMessage *result = NULL;
5176 LDAPMessage *entry = NULL;
5177 int num_result;
5178 const char *dn;
5179 char *filter;
5180 int rc;
5182 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5184 filter = talloc_asprintf(tmp_ctx,
5185 "(&(uid=%s)"
5186 "(objectClass=%s)"
5187 "(objectClass=%s))",
5188 pdb_get_username(sam_acct),
5189 LDAP_OBJ_POSIXACCOUNT,
5190 LDAP_OBJ_SAMBASAMACCOUNT);
5191 if (filter == NULL) {
5192 return NT_STATUS_NO_MEMORY;
5195 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5196 if (rc != LDAP_SUCCESS) {
5197 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5198 return NT_STATUS_UNSUCCESSFUL;
5200 talloc_autofree_ldapmsg(tmp_ctx, result);
5202 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5204 if (num_result == 0) {
5205 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5206 return NT_STATUS_NO_SUCH_USER;
5209 if (num_result > 1) {
5210 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5211 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5214 entry = ldap_first_entry(priv2ld(ldap_state), result);
5215 if (!entry) {
5216 return NT_STATUS_UNSUCCESSFUL;
5219 /* it is just a posix account, retrieve the dn for later use */
5220 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5221 if (!dn) {
5222 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5223 return NT_STATUS_NO_MEMORY;
5226 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5227 if (rc != LDAP_SUCCESS) {
5228 return NT_STATUS_UNSUCCESSFUL;
5231 flush_pwnam_cache();
5233 return NT_STATUS_OK;
5237 * ldapsam_create_group creates a new
5238 * posixGroup and sambaGroupMapping object
5239 * in the ldap groups subtree
5241 * The gid is allocated by winbindd.
5244 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5245 TALLOC_CTX *tmp_ctx,
5246 const char *name,
5247 uint32 *rid)
5249 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5250 NTSTATUS ret;
5251 LDAPMessage *entry = NULL;
5252 LDAPMessage *result = NULL;
5253 uint32 num_result;
5254 bool is_new_entry = False;
5255 LDAPMod **mods = NULL;
5256 char *filter;
5257 char *groupsidstr;
5258 char *groupname;
5259 char *grouptype;
5260 char *gidstr;
5261 const char *dn = NULL;
5262 DOM_SID group_sid;
5263 gid_t gid = -1;
5264 int rc;
5266 groupname = escape_ldap_string_alloc(name);
5267 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5268 groupname, LDAP_OBJ_POSIXGROUP);
5269 SAFE_FREE(groupname);
5271 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5272 if (rc != LDAP_SUCCESS) {
5273 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5274 return NT_STATUS_UNSUCCESSFUL;
5276 talloc_autofree_ldapmsg(tmp_ctx, result);
5278 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5280 if (num_result > 1) {
5281 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5282 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5285 if (num_result == 1) {
5286 char *tmp;
5287 /* check if it is just a posix group.
5288 * or if there is a sid attached to this entry
5291 entry = ldap_first_entry(priv2ld(ldap_state), result);
5292 if (!entry) {
5293 return NT_STATUS_UNSUCCESSFUL;
5296 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5297 if (tmp) {
5298 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5299 return NT_STATUS_GROUP_EXISTS;
5302 /* it is just a posix group, retrieve the gid and the dn for later use */
5303 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5304 if (!tmp) {
5305 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5306 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5309 gid = strtoul(tmp, NULL, 10);
5311 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5312 if (!dn) {
5313 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5314 return NT_STATUS_NO_MEMORY;
5318 if (num_result == 0) {
5319 char *escape_name;
5321 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5323 is_new_entry = True;
5325 /* lets allocate a new groupid for this group */
5326 if (!winbind_allocate_gid(&gid)) {
5327 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5328 return NT_STATUS_UNSUCCESSFUL;
5331 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5333 escape_name = escape_rdn_val_string_alloc(name);
5334 if (!escape_name) {
5335 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5336 return NT_STATUS_NO_MEMORY;
5339 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5341 SAFE_FREE(escape_name);
5343 if (!gidstr || !dn) {
5344 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5345 return NT_STATUS_NO_MEMORY;
5348 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5349 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5350 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5353 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5354 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5355 return ret;
5358 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5360 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5361 &group_sid));
5362 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5364 if (!groupsidstr || !grouptype) {
5365 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5366 return NT_STATUS_NO_MEMORY;
5369 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5370 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5371 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5372 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5373 talloc_autofree_ldapmod(tmp_ctx, mods);
5375 if (is_new_entry) {
5376 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5377 #if 0
5378 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5379 /* This call may fail with rfc2307bis schema */
5380 /* Retry adding a structural class */
5381 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5382 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5384 #endif
5385 } else {
5386 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5389 if (rc != LDAP_SUCCESS) {
5390 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5391 return NT_STATUS_UNSUCCESSFUL;
5394 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5396 return NT_STATUS_OK;
5399 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32 rid)
5401 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5402 LDAPMessage *result = NULL;
5403 LDAPMessage *entry = NULL;
5404 int num_result;
5405 const char *dn;
5406 char *gidstr;
5407 char *filter;
5408 DOM_SID group_sid;
5409 int rc;
5411 /* get the group sid */
5412 sid_compose(&group_sid, get_global_sam_sid(), rid);
5414 filter = talloc_asprintf(tmp_ctx,
5415 "(&(sambaSID=%s)"
5416 "(objectClass=%s)"
5417 "(objectClass=%s))",
5418 sid_string_talloc(tmp_ctx, &group_sid),
5419 LDAP_OBJ_POSIXGROUP,
5420 LDAP_OBJ_GROUPMAP);
5421 if (filter == NULL) {
5422 return NT_STATUS_NO_MEMORY;
5425 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5426 if (rc != LDAP_SUCCESS) {
5427 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5428 return NT_STATUS_UNSUCCESSFUL;
5430 talloc_autofree_ldapmsg(tmp_ctx, result);
5432 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5434 if (num_result == 0) {
5435 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5436 return NT_STATUS_NO_SUCH_GROUP;
5439 if (num_result > 1) {
5440 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5441 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5444 entry = ldap_first_entry(priv2ld(ldap_state), result);
5445 if (!entry) {
5446 return NT_STATUS_UNSUCCESSFUL;
5449 /* here it is, retrieve the dn for later use */
5450 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5451 if (!dn) {
5452 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5453 return NT_STATUS_NO_MEMORY;
5456 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5457 if (!gidstr) {
5458 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5459 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5462 /* check no user have this group marked as primary group */
5463 filter = talloc_asprintf(tmp_ctx,
5464 "(&(gidNumber=%s)"
5465 "(objectClass=%s)"
5466 "(objectClass=%s))",
5467 gidstr,
5468 LDAP_OBJ_POSIXACCOUNT,
5469 LDAP_OBJ_SAMBASAMACCOUNT);
5471 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5472 if (rc != LDAP_SUCCESS) {
5473 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5474 return NT_STATUS_UNSUCCESSFUL;
5476 talloc_autofree_ldapmsg(tmp_ctx, result);
5478 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5480 if (num_result != 0) {
5481 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5482 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5485 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5486 if (rc != LDAP_SUCCESS) {
5487 return NT_STATUS_UNSUCCESSFUL;
5490 return NT_STATUS_OK;
5493 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5494 TALLOC_CTX *tmp_ctx,
5495 uint32 group_rid,
5496 uint32 member_rid,
5497 int modop)
5499 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5500 LDAPMessage *entry = NULL;
5501 LDAPMessage *result = NULL;
5502 uint32 num_result;
5503 LDAPMod **mods = NULL;
5504 char *filter;
5505 char *uidstr;
5506 const char *dn = NULL;
5507 DOM_SID group_sid;
5508 DOM_SID member_sid;
5509 int rc;
5511 switch (modop) {
5512 case LDAP_MOD_ADD:
5513 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5514 break;
5515 case LDAP_MOD_DELETE:
5516 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5517 break;
5518 default:
5519 return NT_STATUS_UNSUCCESSFUL;
5522 /* get member sid */
5523 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5525 /* get the group sid */
5526 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5528 filter = talloc_asprintf(tmp_ctx,
5529 "(&(sambaSID=%s)"
5530 "(objectClass=%s)"
5531 "(objectClass=%s))",
5532 sid_string_talloc(tmp_ctx, &member_sid),
5533 LDAP_OBJ_POSIXACCOUNT,
5534 LDAP_OBJ_SAMBASAMACCOUNT);
5535 if (filter == NULL) {
5536 return NT_STATUS_NO_MEMORY;
5539 /* get the member uid */
5540 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5541 if (rc != LDAP_SUCCESS) {
5542 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5543 return NT_STATUS_UNSUCCESSFUL;
5545 talloc_autofree_ldapmsg(tmp_ctx, result);
5547 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5549 if (num_result == 0) {
5550 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5551 return NT_STATUS_NO_SUCH_MEMBER;
5554 if (num_result > 1) {
5555 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5556 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5559 entry = ldap_first_entry(priv2ld(ldap_state), result);
5560 if (!entry) {
5561 return NT_STATUS_UNSUCCESSFUL;
5564 if (modop == LDAP_MOD_DELETE) {
5565 /* check if we are trying to remove the member from his primary group */
5566 char *gidstr;
5567 gid_t user_gid, group_gid;
5569 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5570 if (!gidstr) {
5571 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5572 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5575 user_gid = strtoul(gidstr, NULL, 10);
5577 if (!sid_to_gid(&group_sid, &group_gid)) {
5578 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5579 return NT_STATUS_UNSUCCESSFUL;
5582 if (user_gid == group_gid) {
5583 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5584 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5588 /* here it is, retrieve the uid for later use */
5589 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5590 if (!uidstr) {
5591 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5592 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5595 filter = talloc_asprintf(tmp_ctx,
5596 "(&(sambaSID=%s)"
5597 "(objectClass=%s)"
5598 "(objectClass=%s))",
5599 sid_string_talloc(tmp_ctx, &group_sid),
5600 LDAP_OBJ_POSIXGROUP,
5601 LDAP_OBJ_GROUPMAP);
5603 /* get the group */
5604 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5605 if (rc != LDAP_SUCCESS) {
5606 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5607 return NT_STATUS_UNSUCCESSFUL;
5609 talloc_autofree_ldapmsg(tmp_ctx, result);
5611 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5613 if (num_result == 0) {
5614 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5615 return NT_STATUS_NO_SUCH_GROUP;
5618 if (num_result > 1) {
5619 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5620 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5623 entry = ldap_first_entry(priv2ld(ldap_state), result);
5624 if (!entry) {
5625 return NT_STATUS_UNSUCCESSFUL;
5628 /* here it is, retrieve the dn for later use */
5629 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5630 if (!dn) {
5631 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5632 return NT_STATUS_NO_MEMORY;
5635 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5637 talloc_autofree_ldapmod(tmp_ctx, mods);
5639 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5640 if (rc != LDAP_SUCCESS) {
5641 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5642 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5643 return NT_STATUS_MEMBER_IN_GROUP;
5645 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5646 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5647 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5649 return NT_STATUS_UNSUCCESSFUL;
5652 return NT_STATUS_OK;
5655 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5656 TALLOC_CTX *tmp_ctx,
5657 uint32 group_rid,
5658 uint32 member_rid)
5660 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5662 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5663 TALLOC_CTX *tmp_ctx,
5664 uint32 group_rid,
5665 uint32 member_rid)
5667 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5670 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5671 TALLOC_CTX *mem_ctx,
5672 struct samu *sampass)
5674 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5675 LDAPMessage *entry = NULL;
5676 LDAPMessage *result = NULL;
5677 uint32 num_result;
5678 LDAPMod **mods = NULL;
5679 char *filter;
5680 char *escape_username;
5681 char *gidstr;
5682 const char *dn = NULL;
5683 gid_t gid;
5684 int rc;
5686 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5688 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5689 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5690 return NT_STATUS_UNSUCCESSFUL;
5692 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
5693 if (!gidstr) {
5694 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5695 return NT_STATUS_NO_MEMORY;
5698 escape_username = escape_ldap_string_alloc(pdb_get_username(sampass));
5699 if (escape_username== NULL) {
5700 return NT_STATUS_NO_MEMORY;
5703 filter = talloc_asprintf(mem_ctx,
5704 "(&(uid=%s)"
5705 "(objectClass=%s)"
5706 "(objectClass=%s))",
5707 escape_username,
5708 LDAP_OBJ_POSIXACCOUNT,
5709 LDAP_OBJ_SAMBASAMACCOUNT);
5711 SAFE_FREE(escape_username);
5713 if (filter == NULL) {
5714 return NT_STATUS_NO_MEMORY;
5717 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5718 if (rc != LDAP_SUCCESS) {
5719 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5720 return NT_STATUS_UNSUCCESSFUL;
5722 talloc_autofree_ldapmsg(mem_ctx, result);
5724 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5726 if (num_result == 0) {
5727 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5728 return NT_STATUS_NO_SUCH_USER;
5731 if (num_result > 1) {
5732 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
5733 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5736 entry = ldap_first_entry(priv2ld(ldap_state), result);
5737 if (!entry) {
5738 return NT_STATUS_UNSUCCESSFUL;
5741 /* retrieve the dn for later use */
5742 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
5743 if (!dn) {
5744 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5745 return NT_STATUS_NO_MEMORY;
5748 /* remove the old one, and add the new one, this way we do not risk races */
5749 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
5751 if (mods == NULL) {
5752 return NT_STATUS_OK;
5755 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5757 if (rc != LDAP_SUCCESS) {
5758 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5759 pdb_get_username(sampass), gidstr));
5760 return NT_STATUS_UNSUCCESSFUL;
5763 flush_pwnam_cache();
5765 return NT_STATUS_OK;
5769 /**********************************************************************
5770 trusted domains functions
5771 *********************************************************************/
5773 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
5774 const char *domain)
5776 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
5777 ldap_state->domain_dn);
5780 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
5781 TALLOC_CTX *mem_ctx,
5782 const char *domain, LDAPMessage **entry)
5784 int rc;
5785 char *filter;
5786 int scope = LDAP_SCOPE_SUBTREE;
5787 const char **attrs = NULL; /* NULL: get all attrs */
5788 int attrsonly = 0; /* 0: return values too */
5789 LDAPMessage *result = NULL;
5790 char *trusted_dn;
5791 uint32 num_result;
5793 filter = talloc_asprintf(talloc_tos(),
5794 "(&(objectClass=%s)(sambaDomainName=%s))",
5795 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
5797 trusted_dn = trusteddom_dn(ldap_state, domain);
5798 if (trusted_dn == NULL) {
5799 return False;
5801 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
5802 filter, attrs, attrsonly, &result);
5804 if (result != NULL) {
5805 talloc_autofree_ldapmsg(mem_ctx, result);
5808 if (rc == LDAP_NO_SUCH_OBJECT) {
5809 *entry = NULL;
5810 return True;
5813 if (rc != LDAP_SUCCESS) {
5814 return False;
5817 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5819 if (num_result > 1) {
5820 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5821 "%s object for domain '%s'?!\n",
5822 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
5823 return False;
5826 if (num_result == 0) {
5827 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5828 "%s object for domain %s.\n",
5829 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
5830 *entry = NULL;
5831 } else {
5832 *entry = ldap_first_entry(priv2ld(ldap_state), result);
5835 return True;
5838 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
5839 const char *domain,
5840 char** pwd,
5841 DOM_SID *sid,
5842 time_t *pass_last_set_time)
5844 struct ldapsam_privates *ldap_state =
5845 (struct ldapsam_privates *)methods->private_data;
5846 LDAPMessage *entry = NULL;
5848 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
5850 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
5851 (entry == NULL))
5853 return False;
5856 /* password */
5857 if (pwd != NULL) {
5858 char *pwd_str;
5859 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5860 entry, "sambaClearTextPassword", talloc_tos());
5861 if (pwd_str == NULL) {
5862 return False;
5864 /* trusteddom_pw routines do not use talloc yet... */
5865 *pwd = SMB_STRDUP(pwd_str);
5866 if (*pwd == NULL) {
5867 return False;
5871 /* last change time */
5872 if (pass_last_set_time != NULL) {
5873 char *time_str;
5874 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5875 entry, "sambaPwdLastSet", talloc_tos());
5876 if (time_str == NULL) {
5877 return False;
5879 *pass_last_set_time = (time_t)atol(time_str);
5882 /* domain sid */
5883 if (sid != NULL) {
5884 char *sid_str;
5885 DOM_SID *dom_sid;
5886 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5887 entry, "sambaSID",
5888 talloc_tos());
5889 if (sid_str == NULL) {
5890 return False;
5892 dom_sid = string_sid_talloc(talloc_tos(), sid_str);
5893 if (dom_sid == NULL) {
5894 return False;
5896 sid_copy(sid, dom_sid);
5899 return True;
5902 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
5903 const char* domain,
5904 const char* pwd,
5905 const DOM_SID *sid)
5907 struct ldapsam_privates *ldap_state =
5908 (struct ldapsam_privates *)methods->private_data;
5909 LDAPMessage *entry = NULL;
5910 LDAPMod **mods = NULL;
5911 char *prev_pwd = NULL;
5912 char *trusted_dn = NULL;
5913 int rc;
5915 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
5918 * get the current entry (if there is one) in order to put the
5919 * current password into the previous password attribute
5921 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
5922 return False;
5925 mods = NULL;
5926 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
5927 LDAP_OBJ_TRUSTDOM_PASSWORD);
5928 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
5929 domain);
5930 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
5931 sid_string_tos(sid));
5932 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
5933 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
5934 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5935 "sambaClearTextPassword", pwd);
5937 talloc_autofree_ldapmod(talloc_tos(), mods);
5939 if (entry != NULL) {
5940 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5941 entry, "sambaClearTextPassword", talloc_tos());
5942 if (prev_pwd != NULL) {
5943 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5944 "sambaPreviousClearTextPassword",
5945 prev_pwd);
5949 trusted_dn = trusteddom_dn(ldap_state, domain);
5950 if (trusted_dn == NULL) {
5951 return False;
5953 if (entry == NULL) {
5954 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
5955 } else {
5956 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
5959 if (rc != LDAP_SUCCESS) {
5960 DEBUG(1, ("error writing trusted domain password!\n"));
5961 return False;
5964 return True;
5967 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
5968 const char *domain)
5970 int rc;
5971 struct ldapsam_privates *ldap_state =
5972 (struct ldapsam_privates *)methods->private_data;
5973 LDAPMessage *entry = NULL;
5974 const char *trusted_dn;
5976 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
5977 return False;
5980 if (entry == NULL) {
5981 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
5982 "%s\n", domain));
5983 return True;
5986 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
5987 entry);
5988 if (trusted_dn == NULL) {
5989 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
5990 return False;
5993 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
5994 if (rc != LDAP_SUCCESS) {
5995 return False;
5998 return True;
6001 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6002 TALLOC_CTX *mem_ctx,
6003 uint32 *num_domains,
6004 struct trustdom_info ***domains)
6006 int rc;
6007 struct ldapsam_privates *ldap_state =
6008 (struct ldapsam_privates *)methods->private_data;
6009 char *filter;
6010 int scope = LDAP_SCOPE_SUBTREE;
6011 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6012 int attrsonly = 0; /* 0: return values too */
6013 LDAPMessage *result = NULL;
6014 LDAPMessage *entry = NULL;
6016 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6017 LDAP_OBJ_TRUSTDOM_PASSWORD);
6019 rc = smbldap_search(ldap_state->smbldap_state,
6020 ldap_state->domain_dn,
6021 scope,
6022 filter,
6023 attrs,
6024 attrsonly,
6025 &result);
6027 if (result != NULL) {
6028 talloc_autofree_ldapmsg(mem_ctx, result);
6031 if (rc != LDAP_SUCCESS) {
6032 return NT_STATUS_UNSUCCESSFUL;
6035 *num_domains = 0;
6036 if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
6037 DEBUG(1, ("talloc failed\n"));
6038 return NT_STATUS_NO_MEMORY;
6041 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6042 entry != NULL;
6043 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6045 char *dom_name, *dom_sid_str;
6046 struct trustdom_info *dom_info;
6048 dom_info = TALLOC_P(*domains, struct trustdom_info);
6049 if (dom_info == NULL) {
6050 DEBUG(1, ("talloc failed\n"));
6051 return NT_STATUS_NO_MEMORY;
6054 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6055 entry,
6056 "sambaDomainName",
6057 talloc_tos());
6058 if (dom_name == NULL) {
6059 DEBUG(1, ("talloc failed\n"));
6060 return NT_STATUS_NO_MEMORY;
6062 dom_info->name = dom_name;
6064 dom_sid_str = smbldap_talloc_single_attribute(
6065 priv2ld(ldap_state), entry, "sambaSID",
6066 talloc_tos());
6067 if (dom_sid_str == NULL) {
6068 DEBUG(1, ("talloc failed\n"));
6069 return NT_STATUS_NO_MEMORY;
6071 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6072 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6073 dom_sid_str));
6074 return NT_STATUS_UNSUCCESSFUL;
6077 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6078 domains, num_domains);
6080 if (*domains == NULL) {
6081 DEBUG(1, ("talloc failed\n"));
6082 return NT_STATUS_NO_MEMORY;
6086 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6087 return NT_STATUS_OK;
6091 /**********************************************************************
6092 Housekeeping
6093 *********************************************************************/
6095 static void free_private_data(void **vp)
6097 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6099 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6101 if ((*ldap_state)->result != NULL) {
6102 ldap_msgfree((*ldap_state)->result);
6103 (*ldap_state)->result = NULL;
6105 if ((*ldap_state)->domain_dn != NULL) {
6106 SAFE_FREE((*ldap_state)->domain_dn);
6109 *ldap_state = NULL;
6111 /* No need to free any further, as it is talloc()ed */
6114 /*********************************************************************
6115 Intitalise the parts of the pdb_methods structure that are common to
6116 all pdb_ldap modes
6117 *********************************************************************/
6119 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6121 NTSTATUS nt_status;
6122 struct ldapsam_privates *ldap_state;
6124 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6125 return nt_status;
6128 (*pdb_method)->name = "ldapsam";
6130 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6131 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6132 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6133 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6134 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6135 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6137 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6138 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6139 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6140 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6141 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6142 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6143 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6145 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6146 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6148 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6150 (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
6151 (*pdb_method)->new_rid = ldapsam_new_rid;
6153 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6154 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6155 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6156 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6158 /* TODO: Setup private data and free */
6160 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
6161 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6162 return NT_STATUS_NO_MEMORY;
6165 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6166 location, &ldap_state->smbldap_state);
6168 if ( !NT_STATUS_IS_OK(nt_status) ) {
6169 return nt_status;
6172 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6173 return NT_STATUS_NO_MEMORY;
6176 (*pdb_method)->private_data = ldap_state;
6178 (*pdb_method)->free_private_data = free_private_data;
6180 return NT_STATUS_OK;
6183 /**********************************************************************
6184 Initialise the 'compat' mode for pdb_ldap
6185 *********************************************************************/
6187 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6189 NTSTATUS nt_status;
6190 struct ldapsam_privates *ldap_state;
6191 char *uri = talloc_strdup( NULL, location );
6193 trim_char( uri, '\"', '\"' );
6194 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6195 if ( uri )
6196 TALLOC_FREE( uri );
6198 if ( !NT_STATUS_IS_OK(nt_status) ) {
6199 return nt_status;
6202 (*pdb_method)->name = "ldapsam_compat";
6204 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6205 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6207 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6209 return NT_STATUS_OK;
6212 /**********************************************************************
6213 Initialise the normal mode for pdb_ldap
6214 *********************************************************************/
6216 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6218 NTSTATUS nt_status;
6219 struct ldapsam_privates *ldap_state = NULL;
6220 uint32 alg_rid_base;
6221 char *alg_rid_base_string = NULL;
6222 LDAPMessage *result = NULL;
6223 LDAPMessage *entry = NULL;
6224 DOM_SID ldap_domain_sid;
6225 DOM_SID secrets_domain_sid;
6226 char *domain_sid_string = NULL;
6227 char *dn = NULL;
6228 char *uri = talloc_strdup( NULL, location );
6230 trim_char( uri, '\"', '\"' );
6231 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6232 if (uri) {
6233 TALLOC_FREE(uri);
6236 if (!NT_STATUS_IS_OK(nt_status)) {
6237 return nt_status;
6240 (*pdb_method)->name = "ldapsam";
6242 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6243 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6244 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6245 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6246 (*pdb_method)->search_users = ldapsam_search_users;
6247 (*pdb_method)->search_groups = ldapsam_search_groups;
6248 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6250 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6251 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6252 (*pdb_method)->enum_group_memberships =
6253 ldapsam_enum_group_memberships;
6254 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6255 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6257 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6258 (*pdb_method)->create_user = ldapsam_create_user;
6259 (*pdb_method)->delete_user = ldapsam_delete_user;
6260 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6261 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6262 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6263 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6264 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6268 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6269 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6271 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6273 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6274 &result,
6275 ldap_state->domain_name, True);
6277 if ( !NT_STATUS_IS_OK(nt_status) ) {
6278 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6279 "info, nor add one to the domain\n"));
6280 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6281 "will be unable to allocate new users/groups, "
6282 "and will risk BDCs having inconsistant SIDs\n"));
6283 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6284 return NT_STATUS_OK;
6287 /* Given that the above might fail, everything below this must be
6288 * optional */
6290 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6291 result);
6292 if (!entry) {
6293 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6294 "entry\n"));
6295 ldap_msgfree(result);
6296 return NT_STATUS_UNSUCCESSFUL;
6299 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6300 if (!dn) {
6301 ldap_msgfree(result);
6302 return NT_STATUS_UNSUCCESSFUL;
6305 ldap_state->domain_dn = smb_xstrdup(dn);
6306 TALLOC_FREE(dn);
6308 domain_sid_string = smbldap_talloc_single_attribute(
6309 ldap_state->smbldap_state->ldap_struct,
6310 entry,
6311 get_userattr_key2string(ldap_state->schema_ver,
6312 LDAP_ATTR_USER_SID),
6313 talloc_tos());
6315 if (domain_sid_string) {
6316 bool found_sid;
6317 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6318 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6319 "read as a valid SID\n", domain_sid_string));
6320 ldap_msgfree(result);
6321 TALLOC_FREE(domain_sid_string);
6322 return NT_STATUS_INVALID_PARAMETER;
6324 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6325 &secrets_domain_sid);
6326 if (!found_sid || !sid_equal(&secrets_domain_sid,
6327 &ldap_domain_sid)) {
6328 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6329 "%s based on pdb_ldap results %s -> %s\n",
6330 ldap_state->domain_name,
6331 sid_string_dbg(&secrets_domain_sid),
6332 sid_string_dbg(&ldap_domain_sid)));
6334 /* reset secrets.tdb sid */
6335 secrets_store_domain_sid(ldap_state->domain_name,
6336 &ldap_domain_sid);
6337 DEBUG(1, ("New global sam SID: %s\n",
6338 sid_string_dbg(get_global_sam_sid())));
6340 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6341 TALLOC_FREE(domain_sid_string);
6344 alg_rid_base_string = smbldap_talloc_single_attribute(
6345 ldap_state->smbldap_state->ldap_struct,
6346 entry,
6347 get_attr_key2string( dominfo_attr_list,
6348 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6349 talloc_tos());
6350 if (alg_rid_base_string) {
6351 alg_rid_base = (uint32)atol(alg_rid_base_string);
6352 if (alg_rid_base != algorithmic_rid_base()) {
6353 DEBUG(0, ("The value of 'algorithmic RID base' has "
6354 "changed since the LDAP\n"
6355 "database was initialised. Aborting. \n"));
6356 ldap_msgfree(result);
6357 TALLOC_FREE(alg_rid_base_string);
6358 return NT_STATUS_UNSUCCESSFUL;
6360 TALLOC_FREE(alg_rid_base_string);
6362 ldap_msgfree(result);
6364 return NT_STATUS_OK;
6367 NTSTATUS pdb_ldap_init(void)
6369 NTSTATUS nt_status;
6370 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6371 return nt_status;
6373 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6374 return nt_status;
6376 /* Let pdb_nds register backends */
6377 pdb_nds_init();
6379 return NT_STATUS_OK;