Fix a bunch of compiler warnings about wrong format types.
[Samba/gebeck_regimport.git] / source3 / passdb / pdb_ldap.c
blob2d3b91f1849f152cd8001cea063bf110a793dace
1 /*
2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /* TODO:
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
43 * and/or winbind
46 #include "includes.h"
47 #include "../libcli/auth/libcli_auth.h"
49 #undef DBGC_CLASS
50 #define DBGC_CLASS DBGC_PASSDB
52 #include <lber.h>
53 #include <ldap.h>
56 * Work around versions of the LDAP client libs that don't have the OIDs
57 * defined, or have them defined under the old name.
58 * This functionality is really a factor of the server, not the client
62 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
63 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
64 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
65 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
66 #endif
68 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
69 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
70 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
71 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
72 #endif
74 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
75 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
76 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
77 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
78 #endif
81 #include "smbldap.h"
83 /**********************************************************************
84 Simple helper function to make stuff better readable
85 **********************************************************************/
87 static LDAP *priv2ld(struct ldapsam_privates *priv)
89 return priv->smbldap_state->ldap_struct;
92 /**********************************************************************
93 Get the attribute name given a user schame version.
94 **********************************************************************/
96 static const char* get_userattr_key2string( int schema_ver, int key )
98 switch ( schema_ver ) {
99 case SCHEMAVER_SAMBAACCOUNT:
100 return get_attr_key2string( attrib_map_v22, key );
102 case SCHEMAVER_SAMBASAMACCOUNT:
103 return get_attr_key2string( attrib_map_v30, key );
105 default:
106 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
107 break;
109 return NULL;
112 /**********************************************************************
113 Return the list of attribute names given a user schema version.
114 **********************************************************************/
116 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
118 switch ( schema_ver ) {
119 case SCHEMAVER_SAMBAACCOUNT:
120 return get_attr_list( mem_ctx, attrib_map_v22 );
122 case SCHEMAVER_SAMBASAMACCOUNT:
123 return get_attr_list( mem_ctx, attrib_map_v30 );
124 default:
125 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
126 break;
129 return NULL;
132 /**************************************************************************
133 Return the list of attribute names to delete given a user schema version.
134 **************************************************************************/
136 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
137 int schema_ver )
139 switch ( schema_ver ) {
140 case SCHEMAVER_SAMBAACCOUNT:
141 return get_attr_list( mem_ctx,
142 attrib_map_to_delete_v22 );
144 case SCHEMAVER_SAMBASAMACCOUNT:
145 return get_attr_list( mem_ctx,
146 attrib_map_to_delete_v30 );
147 default:
148 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
149 break;
152 return NULL;
156 /*******************************************************************
157 Generate the LDAP search filter for the objectclass based on the
158 version of the schema we are using.
159 ******************************************************************/
161 static const char* get_objclass_filter( int schema_ver )
163 fstring objclass_filter;
164 char *result;
166 switch( schema_ver ) {
167 case SCHEMAVER_SAMBAACCOUNT:
168 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
169 break;
170 case SCHEMAVER_SAMBASAMACCOUNT:
171 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
172 break;
173 default:
174 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
175 objclass_filter[0] = '\0';
176 break;
179 result = talloc_strdup(talloc_tos(), objclass_filter);
180 SMB_ASSERT(result != NULL);
181 return result;
184 /*****************************************************************
185 Scan a sequence number off OpenLDAP's syncrepl contextCSN
186 ******************************************************************/
188 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
190 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
191 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
192 LDAPMessage *msg = NULL;
193 LDAPMessage *entry = NULL;
194 TALLOC_CTX *mem_ctx;
195 char **values = NULL;
196 int rc, num_result, num_values, rid;
197 char *suffix = NULL;
198 char *tok;
199 const char *p;
200 const char **attrs;
202 /* Unfortunatly there is no proper way to detect syncrepl-support in
203 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
204 * but do not show up in the root-DSE yet. Neither we can query the
205 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
206 * objectclass. Currently we require lp_ldap_suffix() to show up as
207 * namingContext. - Guenther
210 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
211 return ntstatus;
214 if (!seq_num) {
215 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
216 return ntstatus;
219 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
220 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
221 "as top-level namingContext\n", lp_ldap_suffix()));
222 return ntstatus;
225 mem_ctx = talloc_init("ldapsam_get_seq_num");
227 if (mem_ctx == NULL)
228 return NT_STATUS_NO_MEMORY;
230 if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
231 ntstatus = NT_STATUS_NO_MEMORY;
232 goto done;
235 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
236 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
237 if (rid > 0) {
239 /* consumer syncreplCookie: */
240 /* csn=20050126161620Z#0000001#00#00000 */
241 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
242 attrs[1] = NULL;
243 suffix = talloc_asprintf(mem_ctx,
244 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
245 if (!suffix) {
246 ntstatus = NT_STATUS_NO_MEMORY;
247 goto done;
249 } else {
251 /* provider contextCSN */
252 /* 20050126161620Z#000009#00#000000 */
253 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
254 attrs[1] = NULL;
255 suffix = talloc_asprintf(mem_ctx,
256 "cn=ldapsync,%s", lp_ldap_suffix());
258 if (!suffix) {
259 ntstatus = NT_STATUS_NO_MEMORY;
260 goto done;
264 rc = smbldap_search(ldap_state->smbldap_state, suffix,
265 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
267 if (rc != LDAP_SUCCESS) {
268 goto done;
271 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
272 if (num_result != 1) {
273 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
274 goto done;
277 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
278 if (entry == NULL) {
279 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
280 goto done;
283 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
284 if (values == NULL) {
285 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
286 goto done;
289 num_values = ldap_count_values(values);
290 if (num_values == 0) {
291 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
292 goto done;
295 p = values[0];
296 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
297 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
298 goto done;
301 p = tok;
302 if (!strncmp(p, "csn=", strlen("csn=")))
303 p += strlen("csn=");
305 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
307 *seq_num = generalized_to_unix_time(p);
309 /* very basic sanity check */
310 if (*seq_num <= 0) {
311 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
312 (int)*seq_num));
313 goto done;
316 ntstatus = NT_STATUS_OK;
318 done:
319 if (values != NULL)
320 ldap_value_free(values);
321 if (msg != NULL)
322 ldap_msgfree(msg);
323 if (mem_ctx)
324 talloc_destroy(mem_ctx);
326 return ntstatus;
329 /*******************************************************************
330 Run the search by name.
331 ******************************************************************/
333 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
334 const char *user,
335 LDAPMessage ** result,
336 const char **attr)
338 char *filter = NULL;
339 char *escape_user = escape_ldap_string_alloc(user);
340 int ret = -1;
342 if (!escape_user) {
343 return LDAP_NO_MEMORY;
347 * in the filter expression, replace %u with the real name
348 * so in ldap filter, %u MUST exist :-)
350 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
351 get_objclass_filter(ldap_state->schema_ver));
352 if (!filter) {
353 SAFE_FREE(escape_user);
354 return LDAP_NO_MEMORY;
357 * have to use this here because $ is filtered out
358 * in string_sub
361 filter = talloc_all_string_sub(talloc_tos(),
362 filter, "%u", escape_user);
363 SAFE_FREE(escape_user);
364 if (!filter) {
365 return LDAP_NO_MEMORY;
368 ret = smbldap_search_suffix(ldap_state->smbldap_state,
369 filter, attr, result);
370 TALLOC_FREE(filter);
371 return ret;
374 /*******************************************************************
375 Run the search by rid.
376 ******************************************************************/
378 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
379 uint32 rid, LDAPMessage ** result,
380 const char **attr)
382 char *filter = NULL;
383 int rc;
385 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
386 get_objclass_filter(ldap_state->schema_ver));
387 if (!filter) {
388 return LDAP_NO_MEMORY;
391 rc = smbldap_search_suffix(ldap_state->smbldap_state,
392 filter, attr, result);
393 TALLOC_FREE(filter);
394 return rc;
397 /*******************************************************************
398 Run the search by SID.
399 ******************************************************************/
401 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
402 const DOM_SID *sid, LDAPMessage ** result,
403 const char **attr)
405 char *filter = NULL;
406 int rc;
407 fstring sid_string;
409 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
410 get_userattr_key2string(ldap_state->schema_ver,
411 LDAP_ATTR_USER_SID),
412 sid_to_fstring(sid_string, sid),
413 get_objclass_filter(ldap_state->schema_ver));
414 if (!filter) {
415 return LDAP_NO_MEMORY;
418 rc = smbldap_search_suffix(ldap_state->smbldap_state,
419 filter, attr, result);
421 TALLOC_FREE(filter);
422 return rc;
425 /*******************************************************************
426 Delete complete object or objectclass and attrs from
427 object found in search_result depending on lp_ldap_delete_dn
428 ******************************************************************/
430 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
431 TALLOC_CTX *mem_ctx,
432 LDAPMessage *entry,
433 const char *objectclass,
434 const char **attrs)
436 LDAPMod **mods = NULL;
437 char *name;
438 const char *dn;
439 BerElement *ptr = NULL;
441 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
442 if (dn == NULL) {
443 return LDAP_NO_MEMORY;
446 if (lp_ldap_delete_dn()) {
447 return smbldap_delete(priv->smbldap_state, dn);
450 /* Ok, delete only the SAM attributes */
452 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
453 name != NULL;
454 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
455 const char **attrib;
457 /* We are only allowed to delete the attributes that
458 really exist. */
460 for (attrib = attrs; *attrib != NULL; attrib++) {
461 if (strequal(*attrib, name)) {
462 DEBUG(10, ("ldapsam_delete_entry: deleting "
463 "attribute %s\n", name));
464 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
465 NULL);
468 ldap_memfree(name);
471 if (ptr != NULL) {
472 ber_free(ptr, 0);
475 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
476 talloc_autofree_ldapmod(mem_ctx, mods);
478 return smbldap_modify(priv->smbldap_state, dn, mods);
481 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
483 char *temp;
484 struct tm tm;
486 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
487 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
488 talloc_tos());
489 if (!temp) {
490 return (time_t) 0;
493 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
494 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
495 (char*)temp));
496 TALLOC_FREE(temp);
497 return (time_t) 0;
499 TALLOC_FREE(temp);
500 tzset();
501 return timegm(&tm);
504 /**********************************************************************
505 Initialize struct samu from an LDAP query.
506 (Based on init_sam_from_buffer in pdb_tdb.c)
507 *********************************************************************/
509 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
510 struct samu * sampass,
511 LDAPMessage * entry)
513 time_t logon_time,
514 logoff_time,
515 kickoff_time,
516 pass_last_set_time,
517 pass_can_change_time,
518 pass_must_change_time,
519 ldap_entry_time,
520 bad_password_time;
521 char *username = NULL,
522 *domain = NULL,
523 *nt_username = NULL,
524 *fullname = NULL,
525 *homedir = NULL,
526 *dir_drive = NULL,
527 *logon_script = NULL,
528 *profile_path = NULL,
529 *acct_desc = NULL,
530 *workstations = NULL,
531 *munged_dial = NULL;
532 uint32 user_rid;
533 uint8 smblmpwd[LM_HASH_LEN],
534 smbntpwd[NT_HASH_LEN];
535 bool use_samba_attrs = True;
536 uint32 acct_ctrl = 0;
537 uint16 logon_divs;
538 uint16 bad_password_count = 0,
539 logon_count = 0;
540 uint32 hours_len;
541 uint8 hours[MAX_HOURS_LEN];
542 char *temp = NULL;
543 LOGIN_CACHE *cache_entry = NULL;
544 uint32 pwHistLen;
545 bool expand_explicit = lp_passdb_expand_explicit();
546 bool ret = false;
547 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
549 if (!ctx) {
550 return false;
552 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
553 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
554 goto fn_exit;
557 if (priv2ld(ldap_state) == NULL) {
558 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
559 "ldap_struct is NULL!\n"));
560 goto fn_exit;
563 if (!(username = smbldap_talloc_single_attribute(priv2ld(ldap_state),
564 entry,
565 "uid",
566 ctx))) {
567 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
568 "this user!\n"));
569 goto fn_exit;
572 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
574 nt_username = talloc_strdup(ctx, username);
575 if (!nt_username) {
576 goto fn_exit;
579 domain = talloc_strdup(ctx, ldap_state->domain_name);
580 if (!domain) {
581 goto fn_exit;
584 pdb_set_username(sampass, username, PDB_SET);
586 pdb_set_domain(sampass, domain, PDB_DEFAULT);
587 pdb_set_nt_username(sampass, nt_username, PDB_SET);
589 /* deal with different attributes between the schema first */
591 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
592 if ((temp = smbldap_talloc_single_attribute(
593 ldap_state->smbldap_state->ldap_struct,
594 entry,
595 get_userattr_key2string(ldap_state->schema_ver,
596 LDAP_ATTR_USER_SID),
597 ctx))!=NULL) {
598 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
600 } else {
601 if ((temp = smbldap_talloc_single_attribute(
602 ldap_state->smbldap_state->ldap_struct,
603 entry,
604 get_userattr_key2string(ldap_state->schema_ver,
605 LDAP_ATTR_USER_RID),
606 ctx))!=NULL) {
607 user_rid = (uint32)atol(temp);
608 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
612 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
613 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
614 get_userattr_key2string(ldap_state->schema_ver,
615 LDAP_ATTR_USER_SID),
616 get_userattr_key2string(ldap_state->schema_ver,
617 LDAP_ATTR_USER_RID),
618 username));
619 return False;
622 temp = smbldap_talloc_single_attribute(
623 ldap_state->smbldap_state->ldap_struct,
624 entry,
625 get_userattr_key2string(ldap_state->schema_ver,
626 LDAP_ATTR_PWD_LAST_SET),
627 ctx);
628 if (temp) {
629 pass_last_set_time = (time_t) atol(temp);
630 pdb_set_pass_last_set_time(sampass,
631 pass_last_set_time, PDB_SET);
634 temp = smbldap_talloc_single_attribute(
635 ldap_state->smbldap_state->ldap_struct,
636 entry,
637 get_userattr_key2string(ldap_state->schema_ver,
638 LDAP_ATTR_LOGON_TIME),
639 ctx);
640 if (temp) {
641 logon_time = (time_t) atol(temp);
642 pdb_set_logon_time(sampass, logon_time, PDB_SET);
645 temp = smbldap_talloc_single_attribute(
646 ldap_state->smbldap_state->ldap_struct,
647 entry,
648 get_userattr_key2string(ldap_state->schema_ver,
649 LDAP_ATTR_LOGOFF_TIME),
650 ctx);
651 if (temp) {
652 logoff_time = (time_t) atol(temp);
653 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
656 temp = smbldap_talloc_single_attribute(
657 ldap_state->smbldap_state->ldap_struct,
658 entry,
659 get_userattr_key2string(ldap_state->schema_ver,
660 LDAP_ATTR_KICKOFF_TIME),
661 ctx);
662 if (temp) {
663 kickoff_time = (time_t) atol(temp);
664 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
667 temp = smbldap_talloc_single_attribute(
668 ldap_state->smbldap_state->ldap_struct,
669 entry,
670 get_userattr_key2string(ldap_state->schema_ver,
671 LDAP_ATTR_PWD_CAN_CHANGE),
672 ctx);
673 if (temp) {
674 pass_can_change_time = (time_t) atol(temp);
675 pdb_set_pass_can_change_time(sampass,
676 pass_can_change_time, PDB_SET);
679 temp = smbldap_talloc_single_attribute(
680 ldap_state->smbldap_state->ldap_struct,
681 entry,
682 get_userattr_key2string(ldap_state->schema_ver,
683 LDAP_ATTR_PWD_MUST_CHANGE),
684 ctx);
685 if (temp) {
686 pass_must_change_time = (time_t) atol(temp);
687 pdb_set_pass_must_change_time(sampass,
688 pass_must_change_time, PDB_SET);
691 /* recommend that 'gecos' and 'displayName' should refer to the same
692 * attribute OID. userFullName depreciated, only used by Samba
693 * primary rules of LDAP: don't make a new attribute when one is already defined
694 * that fits your needs; using cn then displayName rather than 'userFullName'
697 fullname = smbldap_talloc_single_attribute(
698 ldap_state->smbldap_state->ldap_struct,
699 entry,
700 get_userattr_key2string(ldap_state->schema_ver,
701 LDAP_ATTR_DISPLAY_NAME),
702 ctx);
703 if (fullname) {
704 pdb_set_fullname(sampass, fullname, PDB_SET);
705 } else {
706 fullname = smbldap_talloc_single_attribute(
707 ldap_state->smbldap_state->ldap_struct,
708 entry,
709 get_userattr_key2string(ldap_state->schema_ver,
710 LDAP_ATTR_CN),
711 ctx);
712 if (fullname) {
713 pdb_set_fullname(sampass, fullname, PDB_SET);
717 dir_drive = smbldap_talloc_single_attribute(
718 ldap_state->smbldap_state->ldap_struct,
719 entry,
720 get_userattr_key2string(ldap_state->schema_ver,
721 LDAP_ATTR_HOME_DRIVE),
722 ctx);
723 if (dir_drive) {
724 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
725 } else {
726 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
729 homedir = smbldap_talloc_single_attribute(
730 ldap_state->smbldap_state->ldap_struct,
731 entry,
732 get_userattr_key2string(ldap_state->schema_ver,
733 LDAP_ATTR_HOME_PATH),
734 ctx);
735 if (homedir) {
736 if (expand_explicit) {
737 homedir = talloc_sub_basic(ctx,
738 username,
739 domain,
740 homedir);
741 if (!homedir) {
742 goto fn_exit;
745 pdb_set_homedir(sampass, homedir, PDB_SET);
746 } else {
747 pdb_set_homedir(sampass,
748 talloc_sub_basic(ctx, username, domain,
749 lp_logon_home()),
750 PDB_DEFAULT);
753 logon_script = smbldap_talloc_single_attribute(
754 ldap_state->smbldap_state->ldap_struct,
755 entry,
756 get_userattr_key2string(ldap_state->schema_ver,
757 LDAP_ATTR_LOGON_SCRIPT),
758 ctx);
759 if (logon_script) {
760 if (expand_explicit) {
761 logon_script = talloc_sub_basic(ctx,
762 username,
763 domain,
764 logon_script);
765 if (!logon_script) {
766 goto fn_exit;
769 pdb_set_logon_script(sampass, logon_script, PDB_SET);
770 } else {
771 pdb_set_logon_script(sampass,
772 talloc_sub_basic(ctx, username, domain,
773 lp_logon_script()),
774 PDB_DEFAULT );
777 profile_path = smbldap_talloc_single_attribute(
778 ldap_state->smbldap_state->ldap_struct,
779 entry,
780 get_userattr_key2string(ldap_state->schema_ver,
781 LDAP_ATTR_PROFILE_PATH),
782 ctx);
783 if (profile_path) {
784 if (expand_explicit) {
785 profile_path = talloc_sub_basic(ctx,
786 username,
787 domain,
788 profile_path);
789 if (!profile_path) {
790 goto fn_exit;
793 pdb_set_profile_path(sampass, profile_path, PDB_SET);
794 } else {
795 pdb_set_profile_path(sampass,
796 talloc_sub_basic(ctx, username, domain,
797 lp_logon_path()),
798 PDB_DEFAULT );
801 acct_desc = smbldap_talloc_single_attribute(
802 ldap_state->smbldap_state->ldap_struct,
803 entry,
804 get_userattr_key2string(ldap_state->schema_ver,
805 LDAP_ATTR_DESC),
806 ctx);
807 if (acct_desc) {
808 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
811 workstations = smbldap_talloc_single_attribute(
812 ldap_state->smbldap_state->ldap_struct,
813 entry,
814 get_userattr_key2string(ldap_state->schema_ver,
815 LDAP_ATTR_USER_WKS),
816 ctx);
817 if (workstations) {
818 pdb_set_workstations(sampass, workstations, PDB_SET);
821 munged_dial = smbldap_talloc_single_attribute(
822 ldap_state->smbldap_state->ldap_struct,
823 entry,
824 get_userattr_key2string(ldap_state->schema_ver,
825 LDAP_ATTR_MUNGED_DIAL),
826 ctx);
827 if (munged_dial) {
828 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
831 /* FIXME: hours stuff should be cleaner */
833 logon_divs = 168;
834 hours_len = 21;
835 memset(hours, 0xff, hours_len);
837 if (ldap_state->is_nds_ldap) {
838 char *user_dn;
839 size_t pwd_len;
840 char clear_text_pw[512];
842 /* Make call to Novell eDirectory ldap extension to get clear text password.
843 NOTE: This will only work if we have an SSL connection to eDirectory. */
844 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
845 if (user_dn != NULL) {
846 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
848 pwd_len = sizeof(clear_text_pw);
849 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
850 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
851 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
852 TALLOC_FREE(user_dn);
853 return False;
855 ZERO_STRUCT(smblmpwd);
856 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
857 TALLOC_FREE(user_dn);
858 return False;
860 ZERO_STRUCT(smbntpwd);
861 use_samba_attrs = False;
864 TALLOC_FREE(user_dn);
866 } else {
867 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
871 if (use_samba_attrs) {
872 temp = smbldap_talloc_single_attribute(
873 ldap_state->smbldap_state->ldap_struct,
874 entry,
875 get_userattr_key2string(ldap_state->schema_ver,
876 LDAP_ATTR_LMPW),
877 ctx);
878 if (temp) {
879 pdb_gethexpwd(temp, smblmpwd);
880 memset((char *)temp, '\0', strlen(temp)+1);
881 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
882 goto fn_exit;
884 ZERO_STRUCT(smblmpwd);
887 temp = smbldap_talloc_single_attribute(
888 ldap_state->smbldap_state->ldap_struct,
889 entry,
890 get_userattr_key2string(ldap_state->schema_ver,
891 LDAP_ATTR_NTPW),
892 ctx);
893 if (temp) {
894 pdb_gethexpwd(temp, smbntpwd);
895 memset((char *)temp, '\0', strlen(temp)+1);
896 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
897 goto fn_exit;
899 ZERO_STRUCT(smbntpwd);
903 pwHistLen = 0;
905 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
906 if (pwHistLen > 0){
907 uint8 *pwhist = NULL;
908 int i;
909 char *history_string = TALLOC_ARRAY(ctx, char,
910 MAX_PW_HISTORY_LEN*64);
912 if (!history_string) {
913 goto fn_exit;
916 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
918 if ((pwhist = TALLOC_ARRAY(ctx, uint8,
919 pwHistLen * PW_HISTORY_ENTRY_LEN)) ==
920 NULL){
921 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
922 goto fn_exit;
924 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
926 if (smbldap_get_single_attribute(
927 ldap_state->smbldap_state->ldap_struct,
928 entry,
929 get_userattr_key2string(ldap_state->schema_ver,
930 LDAP_ATTR_PWD_HISTORY),
931 history_string,
932 MAX_PW_HISTORY_LEN*64)) {
933 bool hex_failed = false;
934 for (i = 0; i < pwHistLen; i++){
935 /* Get the 16 byte salt. */
936 if (!pdb_gethexpwd(&history_string[i*64],
937 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
938 hex_failed = true;
939 break;
941 /* Get the 16 byte MD5 hash of salt+passwd. */
942 if (!pdb_gethexpwd(&history_string[(i*64)+32],
943 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
944 PW_HISTORY_SALT_LEN])) {
945 hex_failed = True;
946 break;
949 if (hex_failed) {
950 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
951 username));
952 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
955 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
956 goto fn_exit;
960 temp = smbldap_talloc_single_attribute(
961 ldap_state->smbldap_state->ldap_struct,
962 entry,
963 get_userattr_key2string(ldap_state->schema_ver,
964 LDAP_ATTR_ACB_INFO),
965 ctx);
966 if (temp) {
967 acct_ctrl = pdb_decode_acct_ctrl(temp);
969 if (acct_ctrl == 0) {
970 acct_ctrl |= ACB_NORMAL;
973 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
974 } else {
975 acct_ctrl |= ACB_NORMAL;
978 pdb_set_hours_len(sampass, hours_len, PDB_SET);
979 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
981 temp = smbldap_talloc_single_attribute(
982 ldap_state->smbldap_state->ldap_struct,
983 entry,
984 get_userattr_key2string(ldap_state->schema_ver,
985 LDAP_ATTR_BAD_PASSWORD_COUNT),
986 ctx);
987 if (temp) {
988 bad_password_count = (uint32) atol(temp);
989 pdb_set_bad_password_count(sampass,
990 bad_password_count, PDB_SET);
993 temp = smbldap_talloc_single_attribute(
994 ldap_state->smbldap_state->ldap_struct,
995 entry,
996 get_userattr_key2string(ldap_state->schema_ver,
997 LDAP_ATTR_BAD_PASSWORD_TIME),
998 ctx);
999 if (temp) {
1000 bad_password_time = (time_t) atol(temp);
1001 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
1005 temp = smbldap_talloc_single_attribute(
1006 ldap_state->smbldap_state->ldap_struct,
1007 entry,
1008 get_userattr_key2string(ldap_state->schema_ver,
1009 LDAP_ATTR_LOGON_COUNT),
1010 ctx);
1011 if (temp) {
1012 logon_count = (uint32) atol(temp);
1013 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1016 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1018 temp = smbldap_talloc_single_attribute(
1019 ldap_state->smbldap_state->ldap_struct,
1020 entry,
1021 get_userattr_key2string(ldap_state->schema_ver,
1022 LDAP_ATTR_LOGON_HOURS),
1023 ctx);
1024 if (temp) {
1025 pdb_gethexhours(temp, hours);
1026 memset((char *)temp, '\0', strlen(temp) +1);
1027 pdb_set_hours(sampass, hours, PDB_SET);
1028 ZERO_STRUCT(hours);
1031 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1032 temp = smbldap_talloc_single_attribute(
1033 priv2ld(ldap_state),
1034 entry,
1035 "uidNumber",
1036 ctx);
1037 if (temp) {
1038 /* We've got a uid, feed the cache */
1039 uid_t uid = strtoul(temp, NULL, 10);
1040 store_uid_sid_cache(pdb_get_user_sid(sampass), uid);
1044 /* check the timestamp of the cache vs ldap entry */
1045 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1046 entry))) {
1047 ret = true;
1048 goto fn_exit;
1051 /* see if we have newer updates */
1052 if (!(cache_entry = login_cache_read(sampass))) {
1053 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1054 (unsigned int)pdb_get_bad_password_count(sampass),
1055 (unsigned int)pdb_get_bad_password_time(sampass)));
1056 ret = true;
1057 goto fn_exit;
1060 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1061 (unsigned int)ldap_entry_time,
1062 (unsigned int)cache_entry->entry_timestamp,
1063 (unsigned int)cache_entry->bad_password_time));
1065 if (ldap_entry_time > cache_entry->entry_timestamp) {
1066 /* cache is older than directory , so
1067 we need to delete the entry but allow the
1068 fields to be written out */
1069 login_cache_delentry(sampass);
1070 } else {
1071 /* read cache in */
1072 pdb_set_acct_ctrl(sampass,
1073 pdb_get_acct_ctrl(sampass) |
1074 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
1075 PDB_SET);
1076 pdb_set_bad_password_count(sampass,
1077 cache_entry->bad_password_count,
1078 PDB_SET);
1079 pdb_set_bad_password_time(sampass,
1080 cache_entry->bad_password_time,
1081 PDB_SET);
1084 ret = true;
1086 fn_exit:
1088 TALLOC_FREE(ctx);
1089 SAFE_FREE(cache_entry);
1090 return ret;
1093 /**********************************************************************
1094 Initialize the ldap db from a struct samu. Called on update.
1095 (Based on init_buffer_from_sam in pdb_tdb.c)
1096 *********************************************************************/
1098 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1099 LDAPMessage *existing,
1100 LDAPMod *** mods, struct samu * sampass,
1101 bool (*need_update)(const struct samu *,
1102 enum pdb_elements))
1104 char *temp = NULL;
1105 uint32 rid;
1107 if (mods == NULL || sampass == NULL) {
1108 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1109 return False;
1112 *mods = NULL;
1115 * took out adding "objectclass: sambaAccount"
1116 * do this on a per-mod basis
1118 if (need_update(sampass, PDB_USERNAME)) {
1119 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1120 "uid", pdb_get_username(sampass));
1121 if (ldap_state->is_nds_ldap) {
1122 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1123 "cn", pdb_get_username(sampass));
1124 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1125 "sn", pdb_get_username(sampass));
1129 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1131 /* only update the RID if we actually need to */
1132 if (need_update(sampass, PDB_USERSID)) {
1133 fstring sid_string;
1134 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
1136 switch ( ldap_state->schema_ver ) {
1137 case SCHEMAVER_SAMBAACCOUNT:
1138 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1139 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1140 sid_string_dbg(user_sid),
1141 sid_string_dbg(
1142 &ldap_state->domain_sid)));
1143 return False;
1145 if (asprintf(&temp, "%i", rid) < 0) {
1146 return false;
1148 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1149 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1150 temp);
1151 SAFE_FREE(temp);
1152 break;
1154 case SCHEMAVER_SAMBASAMACCOUNT:
1155 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1156 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1157 sid_to_fstring(sid_string, user_sid));
1158 break;
1160 default:
1161 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1162 break;
1166 /* we don't need to store the primary group RID - so leaving it
1167 'free' to hang off the unix primary group makes life easier */
1169 if (need_update(sampass, PDB_GROUPSID)) {
1170 fstring sid_string;
1171 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1173 switch ( ldap_state->schema_ver ) {
1174 case SCHEMAVER_SAMBAACCOUNT:
1175 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1176 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1177 sid_string_dbg(group_sid),
1178 sid_string_dbg(
1179 &ldap_state->domain_sid)));
1180 return False;
1183 if (asprintf(&temp, "%i", rid) < 0) {
1184 return false;
1186 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1187 get_userattr_key2string(ldap_state->schema_ver,
1188 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1189 SAFE_FREE(temp);
1190 break;
1192 case SCHEMAVER_SAMBASAMACCOUNT:
1193 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1194 get_userattr_key2string(ldap_state->schema_ver,
1195 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1196 break;
1198 default:
1199 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1200 break;
1205 /* displayName, cn, and gecos should all be the same
1206 * most easily accomplished by giving them the same OID
1207 * gecos isn't set here b/c it should be handled by the
1208 * add-user script
1209 * We change displayName only and fall back to cn if
1210 * it does not exist.
1213 if (need_update(sampass, PDB_FULLNAME))
1214 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1215 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1216 pdb_get_fullname(sampass));
1218 if (need_update(sampass, PDB_ACCTDESC))
1219 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1220 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1221 pdb_get_acct_desc(sampass));
1223 if (need_update(sampass, PDB_WORKSTATIONS))
1224 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1225 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1226 pdb_get_workstations(sampass));
1228 if (need_update(sampass, PDB_MUNGEDDIAL))
1229 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1230 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1231 pdb_get_munged_dial(sampass));
1233 if (need_update(sampass, PDB_SMBHOME))
1234 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1235 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1236 pdb_get_homedir(sampass));
1238 if (need_update(sampass, PDB_DRIVE))
1239 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1240 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1241 pdb_get_dir_drive(sampass));
1243 if (need_update(sampass, PDB_LOGONSCRIPT))
1244 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1245 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1246 pdb_get_logon_script(sampass));
1248 if (need_update(sampass, PDB_PROFILE))
1249 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1250 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1251 pdb_get_profile_path(sampass));
1253 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1254 return false;
1256 if (need_update(sampass, PDB_LOGONTIME))
1257 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1258 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1259 SAFE_FREE(temp);
1261 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1262 return false;
1264 if (need_update(sampass, PDB_LOGOFFTIME))
1265 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1266 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1267 SAFE_FREE(temp);
1269 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1270 return false;
1272 if (need_update(sampass, PDB_KICKOFFTIME))
1273 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1274 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1275 SAFE_FREE(temp);
1277 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1278 return false;
1280 if (need_update(sampass, PDB_CANCHANGETIME))
1281 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1282 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1283 SAFE_FREE(temp);
1285 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1286 return false;
1288 if (need_update(sampass, PDB_MUSTCHANGETIME))
1289 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1290 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1291 SAFE_FREE(temp);
1293 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1294 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1296 if (need_update(sampass, PDB_LMPASSWD)) {
1297 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1298 if (lm_pw) {
1299 char pwstr[34];
1300 pdb_sethexpwd(pwstr, lm_pw,
1301 pdb_get_acct_ctrl(sampass));
1302 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1303 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1304 pwstr);
1305 } else {
1306 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1307 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1308 NULL);
1311 if (need_update(sampass, PDB_NTPASSWD)) {
1312 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1313 if (nt_pw) {
1314 char pwstr[34];
1315 pdb_sethexpwd(pwstr, nt_pw,
1316 pdb_get_acct_ctrl(sampass));
1317 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1318 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1319 pwstr);
1320 } else {
1321 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1322 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1323 NULL);
1327 if (need_update(sampass, PDB_PWHISTORY)) {
1328 char *pwstr = NULL;
1329 uint32 pwHistLen = 0;
1330 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1332 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1333 if (!pwstr) {
1334 return false;
1336 if (pwHistLen == 0) {
1337 /* Remove any password history from the LDAP store. */
1338 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1339 pwstr[64] = '\0';
1340 } else {
1341 int i;
1342 uint32 currHistLen = 0;
1343 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1344 if (pwhist != NULL) {
1345 /* We can only store (1024-1/64 password history entries. */
1346 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1347 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1348 /* Store the salt. */
1349 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1350 /* Followed by the md5 hash of salt + md4 hash */
1351 pdb_sethexpwd(&pwstr[(i*64)+32],
1352 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1353 DEBUG(100, ("pwstr=%s\n", pwstr));
1357 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1358 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1359 pwstr);
1360 SAFE_FREE(pwstr);
1363 if (need_update(sampass, PDB_PASSLASTSET)) {
1364 if (asprintf(&temp, "%li",
1365 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1366 return false;
1368 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1369 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1370 temp);
1371 SAFE_FREE(temp);
1375 if (need_update(sampass, PDB_HOURS)) {
1376 const uint8 *hours = pdb_get_hours(sampass);
1377 if (hours) {
1378 char hourstr[44];
1379 pdb_sethexhours(hourstr, hours);
1380 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1381 existing,
1382 mods,
1383 get_userattr_key2string(ldap_state->schema_ver,
1384 LDAP_ATTR_LOGON_HOURS),
1385 hourstr);
1389 if (need_update(sampass, PDB_ACCTCTRL))
1390 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1391 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1392 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1394 /* password lockout cache:
1395 - If we are now autolocking or clearing, we write to ldap
1396 - If we are clearing, we delete the cache entry
1397 - If the count is > 0, we update the cache
1399 This even means when autolocking, we cache, just in case the
1400 update doesn't work, and we have to cache the autolock flag */
1402 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1403 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1404 uint16 badcount = pdb_get_bad_password_count(sampass);
1405 time_t badtime = pdb_get_bad_password_time(sampass);
1406 uint32 pol;
1407 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1409 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1410 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1412 if ((badcount >= pol) || (badcount == 0)) {
1413 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1414 (unsigned int)badcount, (unsigned int)badtime));
1415 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1416 return false;
1418 smbldap_make_mod(
1419 ldap_state->smbldap_state->ldap_struct,
1420 existing, mods,
1421 get_userattr_key2string(
1422 ldap_state->schema_ver,
1423 LDAP_ATTR_BAD_PASSWORD_COUNT),
1424 temp);
1425 SAFE_FREE(temp);
1427 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1428 return false;
1430 smbldap_make_mod(
1431 ldap_state->smbldap_state->ldap_struct,
1432 existing, mods,
1433 get_userattr_key2string(
1434 ldap_state->schema_ver,
1435 LDAP_ATTR_BAD_PASSWORD_TIME),
1436 temp);
1437 SAFE_FREE(temp);
1439 if (badcount == 0) {
1440 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1441 login_cache_delentry(sampass);
1442 } else {
1443 LOGIN_CACHE cache_entry;
1445 cache_entry.entry_timestamp = time(NULL);
1446 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1447 cache_entry.bad_password_count = badcount;
1448 cache_entry.bad_password_time = badtime;
1450 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1451 login_cache_write(sampass, cache_entry);
1455 return True;
1458 /**********************************************************************
1459 End enumeration of the LDAP password list.
1460 *********************************************************************/
1462 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1464 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1465 if (ldap_state->result) {
1466 ldap_msgfree(ldap_state->result);
1467 ldap_state->result = NULL;
1471 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1472 const char *new_attr)
1474 int i;
1476 if (new_attr == NULL) {
1477 return;
1480 for (i=0; (*attr_list)[i] != NULL; i++) {
1484 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1485 const char *, i+2);
1486 SMB_ASSERT((*attr_list) != NULL);
1487 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1488 (*attr_list)[i+1] = NULL;
1491 /**********************************************************************
1492 Get struct samu entry from LDAP by username.
1493 *********************************************************************/
1495 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1497 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1498 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1499 LDAPMessage *result = NULL;
1500 LDAPMessage *entry = NULL;
1501 int count;
1502 const char ** attr_list;
1503 int rc;
1505 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1506 append_attr(user, &attr_list,
1507 get_userattr_key2string(ldap_state->schema_ver,
1508 LDAP_ATTR_MOD_TIMESTAMP));
1509 append_attr(user, &attr_list, "uidNumber");
1510 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1511 attr_list);
1512 TALLOC_FREE( attr_list );
1514 if ( rc != LDAP_SUCCESS )
1515 return NT_STATUS_NO_SUCH_USER;
1517 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1519 if (count < 1) {
1520 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1521 ldap_msgfree(result);
1522 return NT_STATUS_NO_SUCH_USER;
1523 } else if (count > 1) {
1524 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1525 ldap_msgfree(result);
1526 return NT_STATUS_NO_SUCH_USER;
1529 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1530 if (entry) {
1531 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1532 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1533 ldap_msgfree(result);
1534 return NT_STATUS_NO_SUCH_USER;
1536 pdb_set_backend_private_data(user, result, NULL,
1537 my_methods, PDB_CHANGED);
1538 talloc_autofree_ldapmsg(user, result);
1539 ret = NT_STATUS_OK;
1540 } else {
1541 ldap_msgfree(result);
1543 return ret;
1546 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1547 const DOM_SID *sid, LDAPMessage **result)
1549 int rc = -1;
1550 const char ** attr_list;
1551 uint32 rid;
1553 switch ( ldap_state->schema_ver ) {
1554 case SCHEMAVER_SAMBASAMACCOUNT: {
1555 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1556 if (tmp_ctx == NULL) {
1557 return LDAP_NO_MEMORY;
1560 attr_list = get_userattr_list(tmp_ctx,
1561 ldap_state->schema_ver);
1562 append_attr(tmp_ctx, &attr_list,
1563 get_userattr_key2string(
1564 ldap_state->schema_ver,
1565 LDAP_ATTR_MOD_TIMESTAMP));
1566 append_attr(tmp_ctx, &attr_list, "uidNumber");
1567 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1568 result, attr_list);
1569 TALLOC_FREE(tmp_ctx);
1571 if ( rc != LDAP_SUCCESS )
1572 return rc;
1573 break;
1576 case SCHEMAVER_SAMBAACCOUNT:
1577 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1578 return rc;
1581 attr_list = get_userattr_list(NULL,
1582 ldap_state->schema_ver);
1583 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1584 TALLOC_FREE( attr_list );
1586 if ( rc != LDAP_SUCCESS )
1587 return rc;
1588 break;
1590 return rc;
1593 /**********************************************************************
1594 Get struct samu entry from LDAP by SID.
1595 *********************************************************************/
1597 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1599 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1600 LDAPMessage *result = NULL;
1601 LDAPMessage *entry = NULL;
1602 int count;
1603 int rc;
1605 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1606 sid, &result);
1607 if (rc != LDAP_SUCCESS)
1608 return NT_STATUS_NO_SUCH_USER;
1610 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1612 if (count < 1) {
1613 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1614 "count=%d\n", sid_string_dbg(sid), count));
1615 ldap_msgfree(result);
1616 return NT_STATUS_NO_SUCH_USER;
1617 } else if (count > 1) {
1618 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1619 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1620 count));
1621 ldap_msgfree(result);
1622 return NT_STATUS_NO_SUCH_USER;
1625 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1626 if (!entry) {
1627 ldap_msgfree(result);
1628 return NT_STATUS_NO_SUCH_USER;
1631 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1632 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1633 ldap_msgfree(result);
1634 return NT_STATUS_NO_SUCH_USER;
1637 pdb_set_backend_private_data(user, result, NULL,
1638 my_methods, PDB_CHANGED);
1639 talloc_autofree_ldapmsg(user, result);
1640 return NT_STATUS_OK;
1643 /********************************************************************
1644 Do the actual modification - also change a plaintext passord if
1645 it it set.
1646 **********************************************************************/
1648 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1649 struct samu *newpwd, char *dn,
1650 LDAPMod **mods, int ldap_op,
1651 bool (*need_update)(const struct samu *, enum pdb_elements))
1653 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1654 int rc;
1656 if (!newpwd || !dn) {
1657 return NT_STATUS_INVALID_PARAMETER;
1660 if (!mods) {
1661 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1662 /* may be password change below however */
1663 } else {
1664 switch(ldap_op) {
1665 case LDAP_MOD_ADD:
1666 if (ldap_state->is_nds_ldap) {
1667 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1668 "objectclass",
1669 "inetOrgPerson");
1670 } else {
1671 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1672 "objectclass",
1673 LDAP_OBJ_ACCOUNT);
1675 rc = smbldap_add(ldap_state->smbldap_state,
1676 dn, mods);
1677 break;
1678 case LDAP_MOD_REPLACE:
1679 rc = smbldap_modify(ldap_state->smbldap_state,
1680 dn ,mods);
1681 break;
1682 default:
1683 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1684 ldap_op));
1685 return NT_STATUS_INVALID_PARAMETER;
1688 if (rc!=LDAP_SUCCESS) {
1689 return NT_STATUS_UNSUCCESSFUL;
1693 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1694 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1695 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1696 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1697 BerElement *ber;
1698 struct berval *bv;
1699 char *retoid = NULL;
1700 struct berval *retdata = NULL;
1701 char *utf8_password;
1702 char *utf8_dn;
1703 size_t converted_size;
1705 if (!ldap_state->is_nds_ldap) {
1707 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1708 LDAP_EXOP_MODIFY_PASSWD)) {
1709 DEBUG(2, ("ldap password change requested, but LDAP "
1710 "server does not support it -- ignoring\n"));
1711 return NT_STATUS_OK;
1715 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1716 pdb_get_plaintext_passwd(newpwd),
1717 &converted_size))
1719 return NT_STATUS_NO_MEMORY;
1722 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1723 TALLOC_FREE(utf8_password);
1724 return NT_STATUS_NO_MEMORY;
1727 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1728 DEBUG(0,("ber_alloc_t returns NULL\n"));
1729 TALLOC_FREE(utf8_password);
1730 TALLOC_FREE(utf8_dn);
1731 return NT_STATUS_UNSUCCESSFUL;
1734 if ((ber_printf (ber, "{") < 0) ||
1735 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn) < 0) ||
1736 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password) < 0) ||
1737 (ber_printf (ber, "n}") < 0)) {
1738 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a value <0\n"));
1739 ber_free(ber,1);
1740 TALLOC_FREE(utf8_dn);
1741 TALLOC_FREE(utf8_password);
1742 return NT_STATUS_UNSUCCESSFUL;
1745 if ((rc = ber_flatten (ber, &bv))<0) {
1746 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1747 ber_free(ber,1);
1748 TALLOC_FREE(utf8_dn);
1749 TALLOC_FREE(utf8_password);
1750 return NT_STATUS_UNSUCCESSFUL;
1753 TALLOC_FREE(utf8_dn);
1754 TALLOC_FREE(utf8_password);
1755 ber_free(ber, 1);
1757 if (!ldap_state->is_nds_ldap) {
1758 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1759 LDAP_EXOP_MODIFY_PASSWD,
1760 bv, NULL, NULL, &retoid,
1761 &retdata);
1762 } else {
1763 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1764 pdb_get_plaintext_passwd(newpwd));
1766 if (rc != LDAP_SUCCESS) {
1767 char *ld_error = NULL;
1769 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1770 DEBUG(3, ("Could not set userPassword "
1771 "attribute due to an objectClass "
1772 "violation -- ignoring\n"));
1773 ber_bvfree(bv);
1774 return NT_STATUS_OK;
1777 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1778 &ld_error);
1779 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1780 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1781 SAFE_FREE(ld_error);
1782 ber_bvfree(bv);
1783 #if defined(LDAP_CONSTRAINT_VIOLATION)
1784 if (rc == LDAP_CONSTRAINT_VIOLATION)
1785 return NT_STATUS_PASSWORD_RESTRICTION;
1786 #endif
1787 return NT_STATUS_UNSUCCESSFUL;
1788 } else {
1789 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1790 #ifdef DEBUG_PASSWORD
1791 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1792 #endif
1793 if (retdata)
1794 ber_bvfree(retdata);
1795 if (retoid)
1796 ldap_memfree(retoid);
1798 ber_bvfree(bv);
1800 return NT_STATUS_OK;
1803 /**********************************************************************
1804 Delete entry from LDAP for username.
1805 *********************************************************************/
1807 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1808 struct samu * sam_acct)
1810 struct ldapsam_privates *priv =
1811 (struct ldapsam_privates *)my_methods->private_data;
1812 const char *sname;
1813 int rc;
1814 LDAPMessage *msg, *entry;
1815 NTSTATUS result = NT_STATUS_NO_MEMORY;
1816 const char **attr_list;
1817 TALLOC_CTX *mem_ctx;
1819 if (!sam_acct) {
1820 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1821 return NT_STATUS_INVALID_PARAMETER;
1824 sname = pdb_get_username(sam_acct);
1826 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1827 "LDAP.\n", sname));
1829 mem_ctx = talloc_new(NULL);
1830 if (mem_ctx == NULL) {
1831 DEBUG(0, ("talloc_new failed\n"));
1832 goto done;
1835 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1836 if (attr_list == NULL) {
1837 goto done;
1840 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1842 if ((rc != LDAP_SUCCESS) ||
1843 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1844 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1845 DEBUG(5, ("Could not find user %s\n", sname));
1846 result = NT_STATUS_NO_SUCH_USER;
1847 goto done;
1850 rc = ldapsam_delete_entry(
1851 priv, mem_ctx, entry,
1852 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1853 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1854 attr_list);
1856 result = (rc == LDAP_SUCCESS) ?
1857 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1859 done:
1860 TALLOC_FREE(mem_ctx);
1861 return result;
1864 /**********************************************************************
1865 Helper function to determine for update_sam_account whether
1866 we need LDAP modification.
1867 *********************************************************************/
1869 static bool element_is_changed(const struct samu *sampass,
1870 enum pdb_elements element)
1872 return IS_SAM_CHANGED(sampass, element);
1875 /**********************************************************************
1876 Update struct samu.
1877 *********************************************************************/
1879 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1881 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1882 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1883 int rc = 0;
1884 char *dn;
1885 LDAPMessage *result = NULL;
1886 LDAPMessage *entry = NULL;
1887 LDAPMod **mods = NULL;
1888 const char **attr_list;
1890 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1891 if (!result) {
1892 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1893 if (pdb_get_username(newpwd) == NULL) {
1894 return NT_STATUS_INVALID_PARAMETER;
1896 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1897 TALLOC_FREE( attr_list );
1898 if (rc != LDAP_SUCCESS) {
1899 return NT_STATUS_UNSUCCESSFUL;
1901 pdb_set_backend_private_data(newpwd, result, NULL,
1902 my_methods, PDB_CHANGED);
1903 talloc_autofree_ldapmsg(newpwd, result);
1906 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1907 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1908 return NT_STATUS_UNSUCCESSFUL;
1911 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1912 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1913 if (!dn) {
1914 return NT_STATUS_UNSUCCESSFUL;
1917 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1919 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1920 element_is_changed)) {
1921 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1922 TALLOC_FREE(dn);
1923 if (mods != NULL)
1924 ldap_mods_free(mods,True);
1925 return NT_STATUS_UNSUCCESSFUL;
1928 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1929 && (mods == NULL)) {
1930 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1931 pdb_get_username(newpwd)));
1932 TALLOC_FREE(dn);
1933 return NT_STATUS_OK;
1936 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1938 if (mods != NULL) {
1939 ldap_mods_free(mods,True);
1942 TALLOC_FREE(dn);
1945 * We need to set the backend private data to NULL here. For example
1946 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1947 * same one, and with the explicit delete / add logic for attribute
1948 * values the second time we would use the wrong "old" value which
1949 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1950 * the update.
1951 * The existing LDAPMessage is still being auto-freed by the
1952 * destructor.
1954 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
1955 PDB_CHANGED);
1957 if (!NT_STATUS_IS_OK(ret)) {
1958 return ret;
1961 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1962 pdb_get_username(newpwd)));
1963 return NT_STATUS_OK;
1966 /***************************************************************************
1967 Renames a struct samu
1968 - The "rename user script" has full responsibility for changing everything
1969 ***************************************************************************/
1971 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
1972 struct samu *old_acct,
1973 const char *newname)
1975 const char *oldname;
1976 int rc;
1977 char *rename_script = NULL;
1978 fstring oldname_lower, newname_lower;
1980 if (!old_acct) {
1981 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1982 return NT_STATUS_INVALID_PARAMETER;
1984 if (!newname) {
1985 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1986 return NT_STATUS_INVALID_PARAMETER;
1989 oldname = pdb_get_username(old_acct);
1991 /* rename the posix user */
1992 rename_script = SMB_STRDUP(lp_renameuser_script());
1993 if (rename_script == NULL) {
1994 return NT_STATUS_NO_MEMORY;
1997 if (!(*rename_script)) {
1998 SAFE_FREE(rename_script);
1999 return NT_STATUS_ACCESS_DENIED;
2002 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2003 oldname, newname));
2005 /* We have to allow the account name to end with a '$'.
2006 Also, follow the semantics in _samr_create_user() and lower case the
2007 posix name but preserve the case in passdb */
2009 fstrcpy( oldname_lower, oldname );
2010 strlower_m( oldname_lower );
2011 fstrcpy( newname_lower, newname );
2012 strlower_m( newname_lower );
2013 rename_script = realloc_string_sub2(rename_script,
2014 "%unew",
2015 newname_lower,
2016 true,
2017 true);
2018 if (!rename_script) {
2019 return NT_STATUS_NO_MEMORY;
2021 rename_script = realloc_string_sub2(rename_script,
2022 "%uold",
2023 oldname_lower,
2024 true,
2025 true);
2026 rc = smbrun(rename_script, NULL);
2028 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2029 rename_script, rc));
2031 SAFE_FREE(rename_script);
2033 if (rc == 0) {
2034 smb_nscd_flush_user_cache();
2037 if (rc)
2038 return NT_STATUS_UNSUCCESSFUL;
2040 return NT_STATUS_OK;
2043 /**********************************************************************
2044 Helper function to determine for update_sam_account whether
2045 we need LDAP modification.
2046 *********************************************************************/
2048 static bool element_is_set_or_changed(const struct samu *sampass,
2049 enum pdb_elements element)
2051 return (IS_SAM_SET(sampass, element) ||
2052 IS_SAM_CHANGED(sampass, element));
2055 /**********************************************************************
2056 Add struct samu to LDAP.
2057 *********************************************************************/
2059 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2061 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2062 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2063 int rc;
2064 LDAPMessage *result = NULL;
2065 LDAPMessage *entry = NULL;
2066 LDAPMod **mods = NULL;
2067 int ldap_op = LDAP_MOD_REPLACE;
2068 uint32 num_result;
2069 const char **attr_list;
2070 char *escape_user = NULL;
2071 const char *username = pdb_get_username(newpwd);
2072 const DOM_SID *sid = pdb_get_user_sid(newpwd);
2073 char *filter = NULL;
2074 char *dn = NULL;
2075 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2076 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2078 if (!ctx) {
2079 return NT_STATUS_NO_MEMORY;
2082 if (!username || !*username) {
2083 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2084 status = NT_STATUS_INVALID_PARAMETER;
2085 goto fn_exit;
2088 /* free this list after the second search or in case we exit on failure */
2089 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2091 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2093 if (rc != LDAP_SUCCESS) {
2094 goto fn_exit;
2097 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2098 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2099 username));
2100 goto fn_exit;
2102 ldap_msgfree(result);
2103 result = NULL;
2105 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2106 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2107 sid, &result);
2108 if (rc == LDAP_SUCCESS) {
2109 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2110 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2111 "already in the base, with samba "
2112 "attributes\n", sid_string_dbg(sid)));
2113 goto fn_exit;
2115 ldap_msgfree(result);
2116 result = NULL;
2120 /* does the entry already exist but without a samba attributes?
2121 we need to return the samba attributes here */
2123 escape_user = escape_ldap_string_alloc( username );
2124 filter = talloc_strdup(attr_list, "(uid=%u)");
2125 if (!filter) {
2126 status = NT_STATUS_NO_MEMORY;
2127 goto fn_exit;
2129 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2130 if (!filter) {
2131 status = NT_STATUS_NO_MEMORY;
2132 goto fn_exit;
2134 SAFE_FREE(escape_user);
2136 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2137 filter, attr_list, &result);
2138 if ( rc != LDAP_SUCCESS ) {
2139 goto fn_exit;
2142 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2144 if (num_result > 1) {
2145 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2146 goto fn_exit;
2149 /* Check if we need to update an existing entry */
2150 if (num_result == 1) {
2151 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2152 ldap_op = LDAP_MOD_REPLACE;
2153 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2154 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2155 if (!dn) {
2156 status = NT_STATUS_NO_MEMORY;
2157 goto fn_exit;
2160 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2162 /* There might be a SID for this account already - say an idmap entry */
2164 filter = talloc_asprintf(ctx,
2165 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2166 get_userattr_key2string(ldap_state->schema_ver,
2167 LDAP_ATTR_USER_SID),
2168 sid_string_talloc(ctx, sid),
2169 LDAP_OBJ_IDMAP_ENTRY,
2170 LDAP_OBJ_SID_ENTRY);
2171 if (!filter) {
2172 status = NT_STATUS_NO_MEMORY;
2173 goto fn_exit;
2176 /* free old result before doing a new search */
2177 if (result != NULL) {
2178 ldap_msgfree(result);
2179 result = NULL;
2181 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2182 filter, attr_list, &result);
2184 if ( rc != LDAP_SUCCESS ) {
2185 goto fn_exit;
2188 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2190 if (num_result > 1) {
2191 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2192 goto fn_exit;
2195 /* Check if we need to update an existing entry */
2196 if (num_result == 1) {
2198 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2199 ldap_op = LDAP_MOD_REPLACE;
2200 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2201 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2202 if (!dn) {
2203 status = NT_STATUS_NO_MEMORY;
2204 goto fn_exit;
2209 if (num_result == 0) {
2210 char *escape_username;
2211 /* Check if we need to add an entry */
2212 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2213 ldap_op = LDAP_MOD_ADD;
2215 escape_username = escape_rdn_val_string_alloc(username);
2216 if (!escape_username) {
2217 status = NT_STATUS_NO_MEMORY;
2218 goto fn_exit;
2221 if (username[strlen(username)-1] == '$') {
2222 dn = talloc_asprintf(ctx,
2223 "uid=%s,%s",
2224 escape_username,
2225 lp_ldap_machine_suffix());
2226 } else {
2227 dn = talloc_asprintf(ctx,
2228 "uid=%s,%s",
2229 escape_username,
2230 lp_ldap_user_suffix());
2233 SAFE_FREE(escape_username);
2234 if (!dn) {
2235 status = NT_STATUS_NO_MEMORY;
2236 goto fn_exit;
2240 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2241 element_is_set_or_changed)) {
2242 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2243 if (mods != NULL) {
2244 ldap_mods_free(mods, true);
2246 goto fn_exit;
2249 if (mods == NULL) {
2250 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2251 goto fn_exit;
2253 switch ( ldap_state->schema_ver ) {
2254 case SCHEMAVER_SAMBAACCOUNT:
2255 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2256 break;
2257 case SCHEMAVER_SAMBASAMACCOUNT:
2258 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2259 break;
2260 default:
2261 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2262 break;
2265 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2266 if (!NT_STATUS_IS_OK(ret)) {
2267 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2268 pdb_get_username(newpwd),dn));
2269 ldap_mods_free(mods, true);
2270 goto fn_exit;
2273 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2274 ldap_mods_free(mods, true);
2276 status = NT_STATUS_OK;
2278 fn_exit:
2280 TALLOC_FREE(ctx);
2281 SAFE_FREE(escape_user);
2282 if (result) {
2283 ldap_msgfree(result);
2285 return status;
2288 /**********************************************************************
2289 *********************************************************************/
2291 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2292 const char *filter,
2293 LDAPMessage ** result)
2295 int scope = LDAP_SCOPE_SUBTREE;
2296 int rc;
2297 const char **attr_list;
2299 attr_list = get_attr_list(NULL, groupmap_attr_list);
2300 rc = smbldap_search(ldap_state->smbldap_state,
2301 lp_ldap_group_suffix (), scope,
2302 filter, attr_list, 0, result);
2303 TALLOC_FREE(attr_list);
2305 return rc;
2308 /**********************************************************************
2309 *********************************************************************/
2311 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2312 GROUP_MAP *map, LDAPMessage *entry)
2314 char *temp = NULL;
2315 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2317 if (ldap_state == NULL || map == NULL || entry == NULL ||
2318 ldap_state->smbldap_state->ldap_struct == NULL) {
2319 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2320 TALLOC_FREE(ctx);
2321 return false;
2324 temp = smbldap_talloc_single_attribute(
2325 ldap_state->smbldap_state->ldap_struct,
2326 entry,
2327 get_attr_key2string(groupmap_attr_list,
2328 LDAP_ATTR_GIDNUMBER),
2329 ctx);
2330 if (!temp) {
2331 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2332 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2333 TALLOC_FREE(ctx);
2334 return false;
2336 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2338 map->gid = (gid_t)atol(temp);
2340 TALLOC_FREE(temp);
2341 temp = smbldap_talloc_single_attribute(
2342 ldap_state->smbldap_state->ldap_struct,
2343 entry,
2344 get_attr_key2string(groupmap_attr_list,
2345 LDAP_ATTR_GROUP_SID),
2346 ctx);
2347 if (!temp) {
2348 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2349 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2350 TALLOC_FREE(ctx);
2351 return false;
2354 if (!string_to_sid(&map->sid, temp)) {
2355 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2356 TALLOC_FREE(ctx);
2357 return false;
2360 TALLOC_FREE(temp);
2361 temp = smbldap_talloc_single_attribute(
2362 ldap_state->smbldap_state->ldap_struct,
2363 entry,
2364 get_attr_key2string(groupmap_attr_list,
2365 LDAP_ATTR_GROUP_TYPE),
2366 ctx);
2367 if (!temp) {
2368 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2369 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2370 TALLOC_FREE(ctx);
2371 return false;
2373 map->sid_name_use = (enum lsa_SidType)atol(temp);
2375 if ((map->sid_name_use < SID_NAME_USER) ||
2376 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2377 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2378 TALLOC_FREE(ctx);
2379 return false;
2382 TALLOC_FREE(temp);
2383 temp = smbldap_talloc_single_attribute(
2384 ldap_state->smbldap_state->ldap_struct,
2385 entry,
2386 get_attr_key2string(groupmap_attr_list,
2387 LDAP_ATTR_DISPLAY_NAME),
2388 ctx);
2389 if (!temp) {
2390 temp = smbldap_talloc_single_attribute(
2391 ldap_state->smbldap_state->ldap_struct,
2392 entry,
2393 get_attr_key2string(groupmap_attr_list,
2394 LDAP_ATTR_CN),
2395 ctx);
2396 if (!temp) {
2397 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2398 for gidNumber(%lu)\n",(unsigned long)map->gid));
2399 TALLOC_FREE(ctx);
2400 return false;
2403 fstrcpy(map->nt_name, temp);
2405 TALLOC_FREE(temp);
2406 temp = smbldap_talloc_single_attribute(
2407 ldap_state->smbldap_state->ldap_struct,
2408 entry,
2409 get_attr_key2string(groupmap_attr_list,
2410 LDAP_ATTR_DESC),
2411 ctx);
2412 if (!temp) {
2413 temp = talloc_strdup(ctx, "");
2414 if (!temp) {
2415 TALLOC_FREE(ctx);
2416 return false;
2419 fstrcpy(map->comment, temp);
2421 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2422 store_gid_sid_cache(&map->sid, map->gid);
2425 TALLOC_FREE(ctx);
2426 return true;
2429 /**********************************************************************
2430 *********************************************************************/
2432 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2433 const char *filter,
2434 GROUP_MAP *map)
2436 struct ldapsam_privates *ldap_state =
2437 (struct ldapsam_privates *)methods->private_data;
2438 LDAPMessage *result = NULL;
2439 LDAPMessage *entry = NULL;
2440 int count;
2442 if (ldapsam_search_one_group(ldap_state, filter, &result)
2443 != LDAP_SUCCESS) {
2444 return NT_STATUS_NO_SUCH_GROUP;
2447 count = ldap_count_entries(priv2ld(ldap_state), result);
2449 if (count < 1) {
2450 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2451 "%s\n", filter));
2452 ldap_msgfree(result);
2453 return NT_STATUS_NO_SUCH_GROUP;
2456 if (count > 1) {
2457 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2458 "count=%d\n", filter, count));
2459 ldap_msgfree(result);
2460 return NT_STATUS_NO_SUCH_GROUP;
2463 entry = ldap_first_entry(priv2ld(ldap_state), result);
2465 if (!entry) {
2466 ldap_msgfree(result);
2467 return NT_STATUS_UNSUCCESSFUL;
2470 if (!init_group_from_ldap(ldap_state, map, entry)) {
2471 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2472 "group filter %s\n", filter));
2473 ldap_msgfree(result);
2474 return NT_STATUS_NO_SUCH_GROUP;
2477 ldap_msgfree(result);
2478 return NT_STATUS_OK;
2481 /**********************************************************************
2482 *********************************************************************/
2484 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2485 DOM_SID sid)
2487 char *filter = NULL;
2488 NTSTATUS status;
2489 fstring tmp;
2491 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2492 LDAP_OBJ_GROUPMAP,
2493 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2494 sid_to_fstring(tmp, &sid)) < 0) {
2495 return NT_STATUS_NO_MEMORY;
2498 status = ldapsam_getgroup(methods, filter, map);
2499 SAFE_FREE(filter);
2500 return status;
2503 /**********************************************************************
2504 *********************************************************************/
2506 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2507 gid_t gid)
2509 char *filter = NULL;
2510 NTSTATUS status;
2512 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2513 LDAP_OBJ_GROUPMAP,
2514 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2515 (unsigned long)gid) < 0) {
2516 return NT_STATUS_NO_MEMORY;
2519 status = ldapsam_getgroup(methods, filter, map);
2520 SAFE_FREE(filter);
2521 return status;
2524 /**********************************************************************
2525 *********************************************************************/
2527 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2528 const char *name)
2530 char *filter = NULL;
2531 char *escape_name = escape_ldap_string_alloc(name);
2532 NTSTATUS status;
2534 if (!escape_name) {
2535 return NT_STATUS_NO_MEMORY;
2538 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2539 LDAP_OBJ_GROUPMAP,
2540 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2541 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2542 escape_name) < 0) {
2543 SAFE_FREE(escape_name);
2544 return NT_STATUS_NO_MEMORY;
2547 SAFE_FREE(escape_name);
2548 status = ldapsam_getgroup(methods, filter, map);
2549 SAFE_FREE(filter);
2550 return status;
2553 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2554 LDAPMessage *entry,
2555 const DOM_SID *domain_sid,
2556 uint32 *rid)
2558 fstring str;
2559 DOM_SID sid;
2561 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2562 str, sizeof(str)-1)) {
2563 DEBUG(10, ("Could not find sambaSID attribute\n"));
2564 return False;
2567 if (!string_to_sid(&sid, str)) {
2568 DEBUG(10, ("Could not convert string %s to sid\n", str));
2569 return False;
2572 if (sid_compare_domain(&sid, domain_sid) != 0) {
2573 DEBUG(10, ("SID %s is not in expected domain %s\n",
2574 str, sid_string_dbg(domain_sid)));
2575 return False;
2578 if (!sid_peek_rid(&sid, rid)) {
2579 DEBUG(10, ("Could not peek into RID\n"));
2580 return False;
2583 return True;
2586 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2587 TALLOC_CTX *mem_ctx,
2588 const DOM_SID *group,
2589 uint32 **pp_member_rids,
2590 size_t *p_num_members)
2592 struct ldapsam_privates *ldap_state =
2593 (struct ldapsam_privates *)methods->private_data;
2594 struct smbldap_state *conn = ldap_state->smbldap_state;
2595 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2596 const char *sid_attrs[] = { "sambaSID", NULL };
2597 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2598 LDAPMessage *result = NULL;
2599 LDAPMessage *entry;
2600 char *filter;
2601 char **values = NULL;
2602 char **memberuid;
2603 char *gidstr;
2604 int rc, count;
2606 *pp_member_rids = NULL;
2607 *p_num_members = 0;
2609 filter = talloc_asprintf(mem_ctx,
2610 "(&(objectClass=%s)"
2611 "(objectClass=%s)"
2612 "(sambaSID=%s))",
2613 LDAP_OBJ_POSIXGROUP,
2614 LDAP_OBJ_GROUPMAP,
2615 sid_string_talloc(mem_ctx, group));
2616 if (filter == NULL) {
2617 ret = NT_STATUS_NO_MEMORY;
2618 goto done;
2621 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2622 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2623 &result);
2625 if (rc != LDAP_SUCCESS)
2626 goto done;
2628 talloc_autofree_ldapmsg(mem_ctx, result);
2630 count = ldap_count_entries(conn->ldap_struct, result);
2632 if (count > 1) {
2633 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2634 sid_string_dbg(group)));
2635 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2636 goto done;
2639 if (count == 0) {
2640 ret = NT_STATUS_NO_SUCH_GROUP;
2641 goto done;
2644 entry = ldap_first_entry(conn->ldap_struct, result);
2645 if (entry == NULL)
2646 goto done;
2648 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2649 if (!gidstr) {
2650 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2651 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2652 goto done;
2655 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2657 if (values) {
2659 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2660 if (filter == NULL) {
2661 ret = NT_STATUS_NO_MEMORY;
2662 goto done;
2665 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2666 char *escape_memberuid;
2668 escape_memberuid = escape_ldap_string_alloc(*memberuid);
2669 if (escape_memberuid == NULL) {
2670 ret = NT_STATUS_NO_MEMORY;
2671 goto done;
2674 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2675 if (filter == NULL) {
2676 SAFE_FREE(escape_memberuid);
2677 ret = NT_STATUS_NO_MEMORY;
2678 goto done;
2681 SAFE_FREE(escape_memberuid);
2684 filter = talloc_asprintf_append_buffer(filter, "))");
2685 if (filter == NULL) {
2686 ret = NT_STATUS_NO_MEMORY;
2687 goto done;
2690 rc = smbldap_search(conn, lp_ldap_suffix(),
2691 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2692 &result);
2694 if (rc != LDAP_SUCCESS)
2695 goto done;
2697 count = ldap_count_entries(conn->ldap_struct, result);
2698 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2700 talloc_autofree_ldapmsg(mem_ctx, result);
2702 for (entry = ldap_first_entry(conn->ldap_struct, result);
2703 entry != NULL;
2704 entry = ldap_next_entry(conn->ldap_struct, entry))
2706 char *sidstr;
2707 DOM_SID sid;
2708 uint32 rid;
2710 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2711 entry, "sambaSID",
2712 mem_ctx);
2713 if (!sidstr) {
2714 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2715 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2716 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2717 goto done;
2720 if (!string_to_sid(&sid, sidstr))
2721 goto done;
2723 if (!sid_check_is_in_our_domain(&sid)) {
2724 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2725 "in our domain\n"));
2726 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2727 goto done;
2730 sid_peek_rid(&sid, &rid);
2732 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2733 p_num_members)) {
2734 ret = NT_STATUS_NO_MEMORY;
2735 goto done;
2740 filter = talloc_asprintf(mem_ctx,
2741 "(&(objectClass=%s)"
2742 "(gidNumber=%s))",
2743 LDAP_OBJ_SAMBASAMACCOUNT,
2744 gidstr);
2746 rc = smbldap_search(conn, lp_ldap_suffix(),
2747 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2748 &result);
2750 if (rc != LDAP_SUCCESS)
2751 goto done;
2753 talloc_autofree_ldapmsg(mem_ctx, result);
2755 for (entry = ldap_first_entry(conn->ldap_struct, result);
2756 entry != NULL;
2757 entry = ldap_next_entry(conn->ldap_struct, entry))
2759 uint32 rid;
2761 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2762 entry,
2763 get_global_sam_sid(),
2764 &rid)) {
2765 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2766 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2767 goto done;
2770 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2771 p_num_members)) {
2772 ret = NT_STATUS_NO_MEMORY;
2773 goto done;
2777 ret = NT_STATUS_OK;
2779 done:
2781 if (values)
2782 ldap_value_free(values);
2784 return ret;
2787 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2788 TALLOC_CTX *mem_ctx,
2789 struct samu *user,
2790 DOM_SID **pp_sids,
2791 gid_t **pp_gids,
2792 size_t *p_num_groups)
2794 struct ldapsam_privates *ldap_state =
2795 (struct ldapsam_privates *)methods->private_data;
2796 struct smbldap_state *conn = ldap_state->smbldap_state;
2797 char *filter;
2798 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2799 char *escape_name;
2800 int rc, count;
2801 LDAPMessage *result = NULL;
2802 LDAPMessage *entry;
2803 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2804 size_t num_sids, num_gids;
2805 char *gidstr;
2806 gid_t primary_gid = -1;
2808 *pp_sids = NULL;
2809 num_sids = 0;
2811 if (pdb_get_username(user) == NULL) {
2812 return NT_STATUS_INVALID_PARAMETER;
2815 escape_name = escape_ldap_string_alloc(pdb_get_username(user));
2816 if (escape_name == NULL)
2817 return NT_STATUS_NO_MEMORY;
2819 /* retrieve the users primary gid */
2820 filter = talloc_asprintf(mem_ctx,
2821 "(&(objectClass=%s)(uid=%s))",
2822 LDAP_OBJ_SAMBASAMACCOUNT,
2823 escape_name);
2824 if (filter == NULL) {
2825 ret = NT_STATUS_NO_MEMORY;
2826 goto done;
2829 rc = smbldap_search(conn, lp_ldap_suffix(),
2830 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2832 if (rc != LDAP_SUCCESS)
2833 goto done;
2835 talloc_autofree_ldapmsg(mem_ctx, result);
2837 count = ldap_count_entries(priv2ld(ldap_state), result);
2839 switch (count) {
2840 case 0:
2841 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2842 ret = NT_STATUS_NO_SUCH_USER;
2843 goto done;
2844 case 1:
2845 entry = ldap_first_entry(priv2ld(ldap_state), result);
2847 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2848 if (!gidstr) {
2849 DEBUG (1, ("Unable to find the member's gid!\n"));
2850 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2851 goto done;
2853 primary_gid = strtoul(gidstr, NULL, 10);
2854 break;
2855 default:
2856 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2857 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2858 goto done;
2861 filter = talloc_asprintf(mem_ctx,
2862 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2863 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2864 if (filter == NULL) {
2865 ret = NT_STATUS_NO_MEMORY;
2866 goto done;
2869 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2870 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2872 if (rc != LDAP_SUCCESS)
2873 goto done;
2875 talloc_autofree_ldapmsg(mem_ctx, result);
2877 num_gids = 0;
2878 *pp_gids = NULL;
2880 num_sids = 0;
2881 *pp_sids = NULL;
2883 /* We need to add the primary group as the first gid/sid */
2885 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2886 ret = NT_STATUS_NO_MEMORY;
2887 goto done;
2890 /* This sid will be replaced later */
2892 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2893 &num_sids);
2894 if (!NT_STATUS_IS_OK(ret)) {
2895 goto done;
2898 for (entry = ldap_first_entry(conn->ldap_struct, result);
2899 entry != NULL;
2900 entry = ldap_next_entry(conn->ldap_struct, entry))
2902 fstring str;
2903 DOM_SID sid;
2904 gid_t gid;
2905 char *end;
2907 if (!smbldap_get_single_attribute(conn->ldap_struct,
2908 entry, "sambaSID",
2909 str, sizeof(str)-1))
2910 continue;
2912 if (!string_to_sid(&sid, str))
2913 goto done;
2915 if (!smbldap_get_single_attribute(conn->ldap_struct,
2916 entry, "gidNumber",
2917 str, sizeof(str)-1))
2918 continue;
2920 gid = strtoul(str, &end, 10);
2922 if (PTR_DIFF(end, str) != strlen(str))
2923 goto done;
2925 if (gid == primary_gid) {
2926 sid_copy(&(*pp_sids)[0], &sid);
2927 } else {
2928 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2929 &num_gids)) {
2930 ret = NT_STATUS_NO_MEMORY;
2931 goto done;
2933 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2934 &num_sids);
2935 if (!NT_STATUS_IS_OK(ret)) {
2936 goto done;
2941 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
2942 DEBUG(3, ("primary group of [%s] not found\n",
2943 pdb_get_username(user)));
2944 goto done;
2947 *p_num_groups = num_sids;
2949 ret = NT_STATUS_OK;
2951 done:
2953 SAFE_FREE(escape_name);
2954 return ret;
2957 /**********************************************************************
2958 * Augment a posixGroup object with a sambaGroupMapping domgroup
2959 *********************************************************************/
2961 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
2962 struct ldapsam_privates *ldap_state,
2963 GROUP_MAP *map)
2965 const char *filter, *dn;
2966 LDAPMessage *msg, *entry;
2967 LDAPMod **mods;
2968 int rc;
2970 filter = talloc_asprintf(mem_ctx,
2971 "(&(objectClass=%s)(gidNumber=%u))",
2972 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
2973 if (filter == NULL) {
2974 return NT_STATUS_NO_MEMORY;
2977 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2978 get_attr_list(mem_ctx, groupmap_attr_list),
2979 &msg);
2980 talloc_autofree_ldapmsg(mem_ctx, msg);
2982 if ((rc != LDAP_SUCCESS) ||
2983 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2984 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2985 return NT_STATUS_NO_SUCH_GROUP;
2988 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2989 if (dn == NULL) {
2990 return NT_STATUS_NO_MEMORY;
2993 mods = NULL;
2994 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
2995 LDAP_OBJ_GROUPMAP);
2996 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
2997 sid_string_talloc(mem_ctx, &map->sid));
2998 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
2999 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3000 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3001 map->nt_name);
3002 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3003 map->comment);
3004 talloc_autofree_ldapmod(mem_ctx, mods);
3006 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3007 if (rc != LDAP_SUCCESS) {
3008 return NT_STATUS_ACCESS_DENIED;
3011 return NT_STATUS_OK;
3014 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3015 GROUP_MAP *map)
3017 struct ldapsam_privates *ldap_state =
3018 (struct ldapsam_privates *)methods->private_data;
3019 LDAPMessage *msg = NULL;
3020 LDAPMod **mods = NULL;
3021 const char *attrs[] = { NULL };
3022 char *filter;
3024 char *dn;
3025 TALLOC_CTX *mem_ctx;
3026 NTSTATUS result;
3028 DOM_SID sid;
3030 int rc;
3032 mem_ctx = talloc_new(NULL);
3033 if (mem_ctx == NULL) {
3034 DEBUG(0, ("talloc_new failed\n"));
3035 return NT_STATUS_NO_MEMORY;
3038 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3039 sid_string_talloc(mem_ctx, &map->sid));
3040 if (filter == NULL) {
3041 result = NT_STATUS_NO_MEMORY;
3042 goto done;
3045 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3046 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3047 talloc_autofree_ldapmsg(mem_ctx, msg);
3049 if ((rc == LDAP_SUCCESS) &&
3050 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3052 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3053 "group mapping entry\n", sid_string_dbg(&map->sid)));
3054 result = NT_STATUS_GROUP_EXISTS;
3055 goto done;
3058 switch (map->sid_name_use) {
3060 case SID_NAME_DOM_GRP:
3061 /* To map a domain group we need to have a posix group
3062 to attach to. */
3063 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3064 goto done;
3065 break;
3067 case SID_NAME_ALIAS:
3068 if (!sid_check_is_in_our_domain(&map->sid)
3069 && !sid_check_is_in_builtin(&map->sid) )
3071 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3072 sid_string_dbg(&map->sid)));
3073 result = NT_STATUS_INVALID_PARAMETER;
3074 goto done;
3076 break;
3078 default:
3079 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3080 sid_type_lookup(map->sid_name_use)));
3081 result = NT_STATUS_INVALID_PARAMETER;
3082 goto done;
3085 /* Domain groups have been mapped in a separate routine, we have to
3086 * create an alias now */
3088 if (map->gid == -1) {
3089 DEBUG(10, ("Refusing to map gid==-1\n"));
3090 result = NT_STATUS_INVALID_PARAMETER;
3091 goto done;
3094 if (pdb_gid_to_sid(map->gid, &sid)) {
3095 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3096 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3097 result = NT_STATUS_GROUP_EXISTS;
3098 goto done;
3101 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3102 * the best we can get out of LDAP. */
3104 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3105 sid_string_talloc(mem_ctx, &map->sid),
3106 lp_ldap_group_suffix());
3107 if (dn == NULL) {
3108 result = NT_STATUS_NO_MEMORY;
3109 goto done;
3112 mods = NULL;
3114 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3115 LDAP_OBJ_SID_ENTRY);
3116 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3117 LDAP_OBJ_GROUPMAP);
3118 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3119 sid_string_talloc(mem_ctx, &map->sid));
3120 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3121 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3122 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3123 map->nt_name);
3124 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3125 map->comment);
3126 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3127 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3128 talloc_autofree_ldapmod(mem_ctx, mods);
3130 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3132 result = (rc == LDAP_SUCCESS) ?
3133 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3135 done:
3136 TALLOC_FREE(mem_ctx);
3137 return result;
3140 /**********************************************************************
3141 * Update a group mapping entry. We're quite strict about what can be changed:
3142 * Only the description and displayname may be changed. It simply does not
3143 * make any sense to change the SID, gid or the type in a mapping.
3144 *********************************************************************/
3146 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3147 GROUP_MAP *map)
3149 struct ldapsam_privates *ldap_state =
3150 (struct ldapsam_privates *)methods->private_data;
3151 int rc;
3152 const char *filter, *dn;
3153 LDAPMessage *msg = NULL;
3154 LDAPMessage *entry = NULL;
3155 LDAPMod **mods = NULL;
3156 TALLOC_CTX *mem_ctx;
3157 NTSTATUS result;
3159 mem_ctx = talloc_new(NULL);
3160 if (mem_ctx == NULL) {
3161 DEBUG(0, ("talloc_new failed\n"));
3162 return NT_STATUS_NO_MEMORY;
3165 /* Make 100% sure that sid, gid and type are not changed by looking up
3166 * exactly the values we're given in LDAP. */
3168 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3169 "(sambaSid=%s)(gidNumber=%u)"
3170 "(sambaGroupType=%d))",
3171 LDAP_OBJ_GROUPMAP,
3172 sid_string_talloc(mem_ctx, &map->sid),
3173 (unsigned int)map->gid, map->sid_name_use);
3174 if (filter == NULL) {
3175 result = NT_STATUS_NO_MEMORY;
3176 goto done;
3179 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3180 get_attr_list(mem_ctx, groupmap_attr_list),
3181 &msg);
3182 talloc_autofree_ldapmsg(mem_ctx, msg);
3184 if ((rc != LDAP_SUCCESS) ||
3185 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3186 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3187 result = NT_STATUS_NO_SUCH_GROUP;
3188 goto done;
3191 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3193 if (dn == NULL) {
3194 result = NT_STATUS_NO_MEMORY;
3195 goto done;
3198 mods = NULL;
3199 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3200 map->nt_name);
3201 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3202 map->comment);
3203 talloc_autofree_ldapmod(mem_ctx, mods);
3205 if (mods == NULL) {
3206 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3207 "nothing to do\n"));
3208 result = NT_STATUS_OK;
3209 goto done;
3212 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3214 if (rc != LDAP_SUCCESS) {
3215 result = NT_STATUS_ACCESS_DENIED;
3216 goto done;
3219 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3220 "group %lu in LDAP\n", (unsigned long)map->gid));
3222 result = NT_STATUS_OK;
3224 done:
3225 TALLOC_FREE(mem_ctx);
3226 return result;
3229 /**********************************************************************
3230 *********************************************************************/
3232 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3233 DOM_SID sid)
3235 struct ldapsam_privates *priv =
3236 (struct ldapsam_privates *)methods->private_data;
3237 LDAPMessage *msg, *entry;
3238 int rc;
3239 NTSTATUS result;
3240 TALLOC_CTX *mem_ctx;
3241 char *filter;
3243 mem_ctx = talloc_new(NULL);
3244 if (mem_ctx == NULL) {
3245 DEBUG(0, ("talloc_new failed\n"));
3246 return NT_STATUS_NO_MEMORY;
3249 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3250 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3251 sid_string_talloc(mem_ctx, &sid));
3252 if (filter == NULL) {
3253 result = NT_STATUS_NO_MEMORY;
3254 goto done;
3256 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3257 get_attr_list(mem_ctx, groupmap_attr_list),
3258 &msg);
3259 talloc_autofree_ldapmsg(mem_ctx, msg);
3261 if ((rc != LDAP_SUCCESS) ||
3262 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3263 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3264 result = NT_STATUS_NO_SUCH_GROUP;
3265 goto done;
3268 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3269 get_attr_list(mem_ctx,
3270 groupmap_attr_list_to_delete));
3272 if ((rc == LDAP_NAMING_VIOLATION) ||
3273 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3274 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3275 const char *attrs[] = { "sambaGroupType", "description",
3276 "displayName", "sambaSIDList",
3277 NULL };
3279 /* Second try. Don't delete the sambaSID attribute, this is
3280 for "old" entries that are tacked on a winbind
3281 sambaIdmapEntry. */
3283 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3284 LDAP_OBJ_GROUPMAP, attrs);
3287 if ((rc == LDAP_NAMING_VIOLATION) ||
3288 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3289 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3290 const char *attrs[] = { "sambaGroupType", "description",
3291 "displayName", "sambaSIDList",
3292 "gidNumber", NULL };
3294 /* Third try. This is a post-3.0.21 alias (containing only
3295 * sambaSidEntry and sambaGroupMapping classes), we also have
3296 * to delete the gidNumber attribute, only the sambaSidEntry
3297 * remains */
3299 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3300 LDAP_OBJ_GROUPMAP, attrs);
3303 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3305 done:
3306 TALLOC_FREE(mem_ctx);
3307 return result;
3310 /**********************************************************************
3311 *********************************************************************/
3313 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3314 bool update)
3316 struct ldapsam_privates *ldap_state =
3317 (struct ldapsam_privates *)my_methods->private_data;
3318 char *filter = NULL;
3319 int rc;
3320 const char **attr_list;
3322 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3323 if (!filter) {
3324 return NT_STATUS_NO_MEMORY;
3326 attr_list = get_attr_list( NULL, groupmap_attr_list );
3327 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3328 LDAP_SCOPE_SUBTREE, filter,
3329 attr_list, 0, &ldap_state->result);
3330 TALLOC_FREE(attr_list);
3332 if (rc != LDAP_SUCCESS) {
3333 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3334 ldap_err2string(rc)));
3335 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3336 lp_ldap_group_suffix(), filter));
3337 ldap_msgfree(ldap_state->result);
3338 ldap_state->result = NULL;
3339 TALLOC_FREE(filter);
3340 return NT_STATUS_UNSUCCESSFUL;
3343 TALLOC_FREE(filter);
3345 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3346 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3347 ldap_state->result)));
3349 ldap_state->entry =
3350 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3351 ldap_state->result);
3352 ldap_state->index = 0;
3354 return NT_STATUS_OK;
3357 /**********************************************************************
3358 *********************************************************************/
3360 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3362 ldapsam_endsampwent(my_methods);
3365 /**********************************************************************
3366 *********************************************************************/
3368 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3369 GROUP_MAP *map)
3371 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3372 struct ldapsam_privates *ldap_state =
3373 (struct ldapsam_privates *)my_methods->private_data;
3374 bool bret = False;
3376 while (!bret) {
3377 if (!ldap_state->entry)
3378 return ret;
3380 ldap_state->index++;
3381 bret = init_group_from_ldap(ldap_state, map,
3382 ldap_state->entry);
3384 ldap_state->entry =
3385 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3386 ldap_state->entry);
3389 return NT_STATUS_OK;
3392 /**********************************************************************
3393 *********************************************************************/
3395 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3396 const DOM_SID *domsid, enum lsa_SidType sid_name_use,
3397 GROUP_MAP **pp_rmap,
3398 size_t *p_num_entries,
3399 bool unix_only)
3401 GROUP_MAP map;
3402 size_t entries = 0;
3404 *p_num_entries = 0;
3405 *pp_rmap = NULL;
3407 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3408 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3409 "passdb\n"));
3410 return NT_STATUS_ACCESS_DENIED;
3413 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3414 if (sid_name_use != SID_NAME_UNKNOWN &&
3415 sid_name_use != map.sid_name_use) {
3416 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3417 "not of the requested type\n", map.nt_name));
3418 continue;
3420 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3421 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3422 "non mapped\n", map.nt_name));
3423 continue;
3426 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3427 if (!(*pp_rmap)) {
3428 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3429 "enlarge group map!\n"));
3430 return NT_STATUS_UNSUCCESSFUL;
3433 (*pp_rmap)[entries] = map;
3435 entries += 1;
3438 ldapsam_endsamgrent(methods);
3440 *p_num_entries = entries;
3442 return NT_STATUS_OK;
3445 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3446 const DOM_SID *alias,
3447 const DOM_SID *member,
3448 int modop)
3450 struct ldapsam_privates *ldap_state =
3451 (struct ldapsam_privates *)methods->private_data;
3452 char *dn = NULL;
3453 LDAPMessage *result = NULL;
3454 LDAPMessage *entry = NULL;
3455 int count;
3456 LDAPMod **mods = NULL;
3457 int rc;
3458 enum lsa_SidType type = SID_NAME_USE_NONE;
3459 fstring tmp;
3461 char *filter = NULL;
3463 if (sid_check_is_in_builtin(alias)) {
3464 type = SID_NAME_ALIAS;
3467 if (sid_check_is_in_our_domain(alias)) {
3468 type = SID_NAME_ALIAS;
3471 if (type == SID_NAME_USE_NONE) {
3472 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3473 sid_string_dbg(alias)));
3474 return NT_STATUS_NO_SUCH_ALIAS;
3477 if (asprintf(&filter,
3478 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3479 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3480 type) < 0) {
3481 return NT_STATUS_NO_MEMORY;
3484 if (ldapsam_search_one_group(ldap_state, filter,
3485 &result) != LDAP_SUCCESS) {
3486 SAFE_FREE(filter);
3487 return NT_STATUS_NO_SUCH_ALIAS;
3490 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3491 result);
3493 if (count < 1) {
3494 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3495 ldap_msgfree(result);
3496 SAFE_FREE(filter);
3497 return NT_STATUS_NO_SUCH_ALIAS;
3500 if (count > 1) {
3501 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3502 "filter %s: count=%d\n", filter, count));
3503 ldap_msgfree(result);
3504 SAFE_FREE(filter);
3505 return NT_STATUS_NO_SUCH_ALIAS;
3508 SAFE_FREE(filter);
3510 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3511 result);
3513 if (!entry) {
3514 ldap_msgfree(result);
3515 return NT_STATUS_UNSUCCESSFUL;
3518 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3519 if (!dn) {
3520 ldap_msgfree(result);
3521 return NT_STATUS_UNSUCCESSFUL;
3524 smbldap_set_mod(&mods, modop,
3525 get_attr_key2string(groupmap_attr_list,
3526 LDAP_ATTR_SID_LIST),
3527 sid_to_fstring(tmp, member));
3529 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3531 ldap_mods_free(mods, True);
3532 ldap_msgfree(result);
3533 TALLOC_FREE(dn);
3535 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3536 return NT_STATUS_MEMBER_IN_ALIAS;
3539 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3540 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3543 if (rc != LDAP_SUCCESS) {
3544 return NT_STATUS_UNSUCCESSFUL;
3547 return NT_STATUS_OK;
3550 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3551 const DOM_SID *alias,
3552 const DOM_SID *member)
3554 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3557 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3558 const DOM_SID *alias,
3559 const DOM_SID *member)
3561 return ldapsam_modify_aliasmem(methods, alias, member,
3562 LDAP_MOD_DELETE);
3565 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3566 const DOM_SID *alias,
3567 DOM_SID **pp_members,
3568 size_t *p_num_members)
3570 struct ldapsam_privates *ldap_state =
3571 (struct ldapsam_privates *)methods->private_data;
3572 LDAPMessage *result = NULL;
3573 LDAPMessage *entry = NULL;
3574 int count;
3575 char **values = NULL;
3576 int i;
3577 char *filter = NULL;
3578 size_t num_members = 0;
3579 enum lsa_SidType type = SID_NAME_USE_NONE;
3580 fstring tmp;
3582 *pp_members = NULL;
3583 *p_num_members = 0;
3585 if (sid_check_is_in_builtin(alias)) {
3586 type = SID_NAME_ALIAS;
3589 if (sid_check_is_in_our_domain(alias)) {
3590 type = SID_NAME_ALIAS;
3593 if (type == SID_NAME_USE_NONE) {
3594 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3595 sid_string_dbg(alias)));
3596 return NT_STATUS_NO_SUCH_ALIAS;
3599 if (asprintf(&filter,
3600 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3601 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3602 type) < 0) {
3603 return NT_STATUS_NO_MEMORY;
3606 if (ldapsam_search_one_group(ldap_state, filter,
3607 &result) != LDAP_SUCCESS) {
3608 SAFE_FREE(filter);
3609 return NT_STATUS_NO_SUCH_ALIAS;
3612 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3613 result);
3615 if (count < 1) {
3616 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3617 ldap_msgfree(result);
3618 SAFE_FREE(filter);
3619 return NT_STATUS_NO_SUCH_ALIAS;
3622 if (count > 1) {
3623 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3624 "filter %s: count=%d\n", filter, count));
3625 ldap_msgfree(result);
3626 SAFE_FREE(filter);
3627 return NT_STATUS_NO_SUCH_ALIAS;
3630 SAFE_FREE(filter);
3632 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3633 result);
3635 if (!entry) {
3636 ldap_msgfree(result);
3637 return NT_STATUS_UNSUCCESSFUL;
3640 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3641 entry,
3642 get_attr_key2string(groupmap_attr_list,
3643 LDAP_ATTR_SID_LIST));
3645 if (values == NULL) {
3646 ldap_msgfree(result);
3647 return NT_STATUS_OK;
3650 count = ldap_count_values(values);
3652 for (i=0; i<count; i++) {
3653 DOM_SID member;
3654 NTSTATUS status;
3656 if (!string_to_sid(&member, values[i]))
3657 continue;
3659 status = add_sid_to_array(NULL, &member, pp_members,
3660 &num_members);
3661 if (!NT_STATUS_IS_OK(status)) {
3662 ldap_value_free(values);
3663 ldap_msgfree(result);
3664 return status;
3668 *p_num_members = num_members;
3669 ldap_value_free(values);
3670 ldap_msgfree(result);
3672 return NT_STATUS_OK;
3675 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3676 TALLOC_CTX *mem_ctx,
3677 const DOM_SID *domain_sid,
3678 const DOM_SID *members,
3679 size_t num_members,
3680 uint32 **pp_alias_rids,
3681 size_t *p_num_alias_rids)
3683 struct ldapsam_privates *ldap_state =
3684 (struct ldapsam_privates *)methods->private_data;
3685 LDAP *ldap_struct;
3687 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3689 LDAPMessage *result = NULL;
3690 LDAPMessage *entry = NULL;
3691 int i;
3692 int rc;
3693 char *filter;
3694 enum lsa_SidType type = SID_NAME_USE_NONE;
3696 if (sid_check_is_builtin(domain_sid)) {
3697 type = SID_NAME_ALIAS;
3700 if (sid_check_is_domain(domain_sid)) {
3701 type = SID_NAME_ALIAS;
3704 if (type == SID_NAME_USE_NONE) {
3705 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3706 sid_string_dbg(domain_sid)));
3707 return NT_STATUS_UNSUCCESSFUL;
3710 filter = talloc_asprintf(mem_ctx,
3711 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3712 LDAP_OBJ_GROUPMAP, type);
3714 for (i=0; i<num_members; i++)
3715 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3716 filter,
3717 sid_string_talloc(mem_ctx,
3718 &members[i]));
3720 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3722 if (filter == NULL) {
3723 return NT_STATUS_NO_MEMORY;
3726 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3727 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3729 if (rc != LDAP_SUCCESS)
3730 return NT_STATUS_UNSUCCESSFUL;
3732 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3734 for (entry = ldap_first_entry(ldap_struct, result);
3735 entry != NULL;
3736 entry = ldap_next_entry(ldap_struct, entry))
3738 fstring sid_str;
3739 DOM_SID sid;
3740 uint32 rid;
3742 if (!smbldap_get_single_attribute(ldap_struct, entry,
3743 LDAP_ATTRIBUTE_SID,
3744 sid_str,
3745 sizeof(sid_str)-1))
3746 continue;
3748 if (!string_to_sid(&sid, sid_str))
3749 continue;
3751 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3752 continue;
3754 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3755 p_num_alias_rids)) {
3756 ldap_msgfree(result);
3757 return NT_STATUS_NO_MEMORY;
3761 ldap_msgfree(result);
3762 return NT_STATUS_OK;
3765 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3766 int policy_index,
3767 uint32 value)
3769 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3770 int rc;
3771 LDAPMod **mods = NULL;
3772 fstring value_string;
3773 const char *policy_attr = NULL;
3775 struct ldapsam_privates *ldap_state =
3776 (struct ldapsam_privates *)methods->private_data;
3778 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3780 if (!ldap_state->domain_dn) {
3781 return NT_STATUS_INVALID_PARAMETER;
3784 policy_attr = get_account_policy_attr(policy_index);
3785 if (policy_attr == NULL) {
3786 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3787 "policy\n"));
3788 return ntstatus;
3791 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3793 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3795 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3796 mods);
3798 ldap_mods_free(mods, True);
3800 if (rc != LDAP_SUCCESS) {
3801 return ntstatus;
3804 if (!cache_account_policy_set(policy_index, value)) {
3805 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3806 "update local tdb cache\n"));
3807 return ntstatus;
3810 return NT_STATUS_OK;
3813 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3814 int policy_index, uint32 value)
3816 return ldapsam_set_account_policy_in_ldap(methods, policy_index,
3817 value);
3820 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3821 int policy_index,
3822 uint32 *value)
3824 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3825 LDAPMessage *result = NULL;
3826 LDAPMessage *entry = NULL;
3827 int count;
3828 int rc;
3829 char **vals = NULL;
3830 const char *policy_attr = NULL;
3832 struct ldapsam_privates *ldap_state =
3833 (struct ldapsam_privates *)methods->private_data;
3835 const char *attrs[2];
3837 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3839 if (!ldap_state->domain_dn) {
3840 return NT_STATUS_INVALID_PARAMETER;
3843 policy_attr = get_account_policy_attr(policy_index);
3844 if (!policy_attr) {
3845 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3846 "policy index: %d\n", policy_index));
3847 return ntstatus;
3850 attrs[0] = policy_attr;
3851 attrs[1] = NULL;
3853 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3854 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
3855 &result);
3857 if (rc != LDAP_SUCCESS) {
3858 return ntstatus;
3861 count = ldap_count_entries(priv2ld(ldap_state), result);
3862 if (count < 1) {
3863 goto out;
3866 entry = ldap_first_entry(priv2ld(ldap_state), result);
3867 if (entry == NULL) {
3868 goto out;
3871 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3872 if (vals == NULL) {
3873 goto out;
3876 *value = (uint32)atol(vals[0]);
3878 ntstatus = NT_STATUS_OK;
3880 out:
3881 if (vals)
3882 ldap_value_free(vals);
3883 ldap_msgfree(result);
3885 return ntstatus;
3888 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3890 - if user hasn't decided to use account policies inside LDAP just reuse the
3891 old tdb values
3893 - if there is a valid cache entry, return that
3894 - if there is an LDAP entry, update cache and return
3895 - otherwise set to default, update cache and return
3897 Guenther
3899 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
3900 int policy_index, uint32 *value)
3902 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3904 if (cache_account_policy_get(policy_index, value)) {
3905 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3906 "cache\n"));
3907 return NT_STATUS_OK;
3910 ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
3911 value);
3912 if (NT_STATUS_IS_OK(ntstatus)) {
3913 goto update_cache;
3916 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3917 "ldap\n"));
3919 #if 0
3920 /* should we automagically migrate old tdb value here ? */
3921 if (account_policy_get(policy_index, value))
3922 goto update_ldap;
3924 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3925 "default\n", policy_index));
3926 #endif
3928 if (!account_policy_get_default(policy_index, value)) {
3929 return ntstatus;
3932 /* update_ldap: */
3934 ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
3935 if (!NT_STATUS_IS_OK(ntstatus)) {
3936 return ntstatus;
3939 update_cache:
3941 if (!cache_account_policy_set(policy_index, *value)) {
3942 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3943 "tdb as a cache\n"));
3944 return NT_STATUS_UNSUCCESSFUL;
3947 return NT_STATUS_OK;
3950 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
3951 const DOM_SID *domain_sid,
3952 int num_rids,
3953 uint32 *rids,
3954 const char **names,
3955 enum lsa_SidType *attrs)
3957 struct ldapsam_privates *ldap_state =
3958 (struct ldapsam_privates *)methods->private_data;
3959 LDAPMessage *msg = NULL;
3960 LDAPMessage *entry;
3961 char *allsids = NULL;
3962 int i, rc, num_mapped;
3963 NTSTATUS result = NT_STATUS_NO_MEMORY;
3964 TALLOC_CTX *mem_ctx;
3965 LDAP *ld;
3966 bool is_builtin;
3968 mem_ctx = talloc_new(NULL);
3969 if (mem_ctx == NULL) {
3970 DEBUG(0, ("talloc_new failed\n"));
3971 goto done;
3974 if (!sid_check_is_builtin(domain_sid) &&
3975 !sid_check_is_domain(domain_sid)) {
3976 result = NT_STATUS_INVALID_PARAMETER;
3977 goto done;
3980 for (i=0; i<num_rids; i++)
3981 attrs[i] = SID_NAME_UNKNOWN;
3983 allsids = talloc_strdup(mem_ctx, "");
3984 if (allsids == NULL) {
3985 goto done;
3988 for (i=0; i<num_rids; i++) {
3989 DOM_SID sid;
3990 sid_compose(&sid, domain_sid, rids[i]);
3991 allsids = talloc_asprintf_append_buffer(
3992 allsids, "(sambaSid=%s)",
3993 sid_string_talloc(mem_ctx, &sid));
3994 if (allsids == NULL) {
3995 goto done;
3999 /* First look for users */
4002 char *filter;
4003 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4005 filter = talloc_asprintf(
4006 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4007 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4009 if (filter == NULL) {
4010 goto done;
4013 rc = smbldap_search(ldap_state->smbldap_state,
4014 lp_ldap_user_suffix(),
4015 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4016 &msg);
4017 talloc_autofree_ldapmsg(mem_ctx, msg);
4020 if (rc != LDAP_SUCCESS)
4021 goto done;
4023 ld = ldap_state->smbldap_state->ldap_struct;
4024 num_mapped = 0;
4026 for (entry = ldap_first_entry(ld, msg);
4027 entry != NULL;
4028 entry = ldap_next_entry(ld, entry)) {
4029 uint32 rid;
4030 int rid_index;
4031 const char *name;
4033 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4034 &rid)) {
4035 DEBUG(2, ("Could not find sid from ldap entry\n"));
4036 continue;
4039 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4040 names);
4041 if (name == NULL) {
4042 DEBUG(2, ("Could not retrieve uid attribute\n"));
4043 continue;
4046 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4047 if (rid == rids[rid_index])
4048 break;
4051 if (rid_index == num_rids) {
4052 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4053 continue;
4056 attrs[rid_index] = SID_NAME_USER;
4057 names[rid_index] = name;
4058 num_mapped += 1;
4061 if (num_mapped == num_rids) {
4062 /* No need to look for groups anymore -- we're done */
4063 result = NT_STATUS_OK;
4064 goto done;
4067 /* Same game for groups */
4070 char *filter;
4071 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4072 "sambaGroupType", NULL };
4074 filter = talloc_asprintf(
4075 mem_ctx, "(&(objectClass=%s)(|%s))",
4076 LDAP_OBJ_GROUPMAP, allsids);
4077 if (filter == NULL) {
4078 goto done;
4081 rc = smbldap_search(ldap_state->smbldap_state,
4082 lp_ldap_group_suffix(),
4083 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4084 &msg);
4085 talloc_autofree_ldapmsg(mem_ctx, msg);
4088 if (rc != LDAP_SUCCESS)
4089 goto done;
4091 /* ldap_struct might have changed due to a reconnect */
4093 ld = ldap_state->smbldap_state->ldap_struct;
4095 /* For consistency checks, we already checked we're only domain or builtin */
4097 is_builtin = sid_check_is_builtin(domain_sid);
4099 for (entry = ldap_first_entry(ld, msg);
4100 entry != NULL;
4101 entry = ldap_next_entry(ld, entry))
4103 uint32 rid;
4104 int rid_index;
4105 const char *attr;
4106 enum lsa_SidType type;
4107 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4109 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4110 mem_ctx);
4111 if (attr == NULL) {
4112 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4113 dn));
4114 continue;
4117 type = (enum lsa_SidType)atol(attr);
4119 /* Consistency checks */
4120 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4121 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4122 (type != SID_NAME_DOM_GRP)))) {
4123 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4126 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4127 &rid)) {
4128 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4129 continue;
4132 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4134 if (attr == NULL) {
4135 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4136 dn));
4137 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4140 if (attr == NULL) {
4141 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4142 dn));
4143 continue;
4146 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4147 if (rid == rids[rid_index])
4148 break;
4151 if (rid_index == num_rids) {
4152 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4153 continue;
4156 attrs[rid_index] = type;
4157 names[rid_index] = attr;
4158 num_mapped += 1;
4161 result = NT_STATUS_NONE_MAPPED;
4163 if (num_mapped > 0)
4164 result = (num_mapped == num_rids) ?
4165 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4166 done:
4167 TALLOC_FREE(mem_ctx);
4168 return result;
4171 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4173 char *filter = NULL;
4174 char *escaped = NULL;
4175 char *result = NULL;
4177 if (asprintf(&filter, "(&%s(objectclass=%s))",
4178 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4179 goto done;
4182 escaped = escape_ldap_string_alloc(username);
4183 if (escaped == NULL) goto done;
4185 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4187 done:
4188 SAFE_FREE(filter);
4189 SAFE_FREE(escaped);
4191 return result;
4194 const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4196 int i, num = 0;
4197 va_list ap;
4198 const char **result;
4200 va_start(ap, mem_ctx);
4201 while (va_arg(ap, const char *) != NULL)
4202 num += 1;
4203 va_end(ap);
4205 if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
4206 return NULL;
4209 va_start(ap, mem_ctx);
4210 for (i=0; i<num; i++) {
4211 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4212 if (result[i] == NULL) {
4213 talloc_free(result);
4214 va_end(ap);
4215 return NULL;
4218 va_end(ap);
4220 result[num] = NULL;
4221 return result;
4224 struct ldap_search_state {
4225 struct smbldap_state *connection;
4227 uint32 acct_flags;
4228 uint16 group_type;
4230 const char *base;
4231 int scope;
4232 const char *filter;
4233 const char **attrs;
4234 int attrsonly;
4235 void *pagedresults_cookie;
4237 LDAPMessage *entries, *current_entry;
4238 bool (*ldap2displayentry)(struct ldap_search_state *state,
4239 TALLOC_CTX *mem_ctx,
4240 LDAP *ld, LDAPMessage *entry,
4241 struct samr_displayentry *result);
4244 static bool ldapsam_search_firstpage(struct pdb_search *search)
4246 struct ldap_search_state *state =
4247 (struct ldap_search_state *)search->private_data;
4248 LDAP *ld;
4249 int rc = LDAP_OPERATIONS_ERROR;
4251 state->entries = NULL;
4253 if (state->connection->paged_results) {
4254 rc = smbldap_search_paged(state->connection, state->base,
4255 state->scope, state->filter,
4256 state->attrs, state->attrsonly,
4257 lp_ldap_page_size(), &state->entries,
4258 &state->pagedresults_cookie);
4261 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4263 if (state->entries != NULL) {
4264 /* Left over from unsuccessful paged attempt */
4265 ldap_msgfree(state->entries);
4266 state->entries = NULL;
4269 rc = smbldap_search(state->connection, state->base,
4270 state->scope, state->filter, state->attrs,
4271 state->attrsonly, &state->entries);
4273 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4274 return False;
4276 /* Ok, the server was lying. It told us it could do paged
4277 * searches when it could not. */
4278 state->connection->paged_results = False;
4281 ld = state->connection->ldap_struct;
4282 if ( ld == NULL) {
4283 DEBUG(5, ("Don't have an LDAP connection right after a "
4284 "search\n"));
4285 return False;
4287 state->current_entry = ldap_first_entry(ld, state->entries);
4289 if (state->current_entry == NULL) {
4290 ldap_msgfree(state->entries);
4291 state->entries = NULL;
4294 return True;
4297 static bool ldapsam_search_nextpage(struct pdb_search *search)
4299 struct ldap_search_state *state =
4300 (struct ldap_search_state *)search->private_data;
4301 int rc;
4303 if (!state->connection->paged_results) {
4304 /* There is no next page when there are no paged results */
4305 return False;
4308 rc = smbldap_search_paged(state->connection, state->base,
4309 state->scope, state->filter, state->attrs,
4310 state->attrsonly, lp_ldap_page_size(),
4311 &state->entries,
4312 &state->pagedresults_cookie);
4314 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4315 return False;
4317 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4319 if (state->current_entry == NULL) {
4320 ldap_msgfree(state->entries);
4321 state->entries = NULL;
4324 return True;
4327 static bool ldapsam_search_next_entry(struct pdb_search *search,
4328 struct samr_displayentry *entry)
4330 struct ldap_search_state *state =
4331 (struct ldap_search_state *)search->private_data;
4332 bool result;
4334 retry:
4335 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4336 return False;
4338 if ((state->entries == NULL) &&
4339 !ldapsam_search_nextpage(search))
4340 return False;
4342 result = state->ldap2displayentry(state, search,
4343 state->connection->ldap_struct,
4344 state->current_entry, entry);
4346 if (!result) {
4347 char *dn;
4348 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4349 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4350 if (dn != NULL) ldap_memfree(dn);
4353 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4355 if (state->current_entry == NULL) {
4356 ldap_msgfree(state->entries);
4357 state->entries = NULL;
4360 if (!result) goto retry;
4362 return True;
4365 static void ldapsam_search_end(struct pdb_search *search)
4367 struct ldap_search_state *state =
4368 (struct ldap_search_state *)search->private_data;
4369 int rc;
4371 if (state->pagedresults_cookie == NULL)
4372 return;
4374 if (state->entries != NULL)
4375 ldap_msgfree(state->entries);
4377 state->entries = NULL;
4378 state->current_entry = NULL;
4380 if (!state->connection->paged_results)
4381 return;
4383 /* Tell the LDAP server we're not interested in the rest anymore. */
4385 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4386 state->filter, state->attrs,
4387 state->attrsonly, 0, &state->entries,
4388 &state->pagedresults_cookie);
4390 if (rc != LDAP_SUCCESS)
4391 DEBUG(5, ("Could not end search properly\n"));
4393 return;
4396 static bool ldapuser2displayentry(struct ldap_search_state *state,
4397 TALLOC_CTX *mem_ctx,
4398 LDAP *ld, LDAPMessage *entry,
4399 struct samr_displayentry *result)
4401 char **vals;
4402 size_t converted_size;
4403 DOM_SID sid;
4404 uint32 acct_flags;
4406 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4407 if ((vals == NULL) || (vals[0] == NULL)) {
4408 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4409 return False;
4411 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4412 ldap_value_free(vals);
4414 if ((state->acct_flags != 0) &&
4415 ((state->acct_flags & acct_flags) == 0))
4416 return False;
4418 result->acct_flags = acct_flags;
4419 result->account_name = "";
4420 result->fullname = "";
4421 result->description = "";
4423 vals = ldap_get_values(ld, entry, "uid");
4424 if ((vals == NULL) || (vals[0] == NULL)) {
4425 DEBUG(5, ("\"uid\" not found\n"));
4426 return False;
4428 if (!pull_utf8_talloc(mem_ctx,
4429 CONST_DISCARD(char **, &result->account_name),
4430 vals[0], &converted_size))
4432 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4433 strerror(errno)));
4436 ldap_value_free(vals);
4438 vals = ldap_get_values(ld, entry, "displayName");
4439 if ((vals == NULL) || (vals[0] == NULL))
4440 DEBUG(8, ("\"displayName\" not found\n"));
4441 else if (!pull_utf8_talloc(mem_ctx,
4442 CONST_DISCARD(char **, &result->fullname),
4443 vals[0], &converted_size))
4445 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4446 strerror(errno)));
4449 ldap_value_free(vals);
4451 vals = ldap_get_values(ld, entry, "description");
4452 if ((vals == NULL) || (vals[0] == NULL))
4453 DEBUG(8, ("\"description\" not found\n"));
4454 else if (!pull_utf8_talloc(mem_ctx,
4455 CONST_DISCARD(char **, &result->description),
4456 vals[0], &converted_size))
4458 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4459 strerror(errno)));
4462 ldap_value_free(vals);
4464 if ((result->account_name == NULL) ||
4465 (result->fullname == NULL) ||
4466 (result->description == NULL)) {
4467 DEBUG(0, ("talloc failed\n"));
4468 return False;
4471 vals = ldap_get_values(ld, entry, "sambaSid");
4472 if ((vals == NULL) || (vals[0] == NULL)) {
4473 DEBUG(0, ("\"objectSid\" not found\n"));
4474 return False;
4477 if (!string_to_sid(&sid, vals[0])) {
4478 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4479 ldap_value_free(vals);
4480 return False;
4482 ldap_value_free(vals);
4484 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4485 DEBUG(0, ("sid %s does not belong to our domain\n",
4486 sid_string_dbg(&sid)));
4487 return False;
4490 return True;
4494 static bool ldapsam_search_users(struct pdb_methods *methods,
4495 struct pdb_search *search,
4496 uint32 acct_flags)
4498 struct ldapsam_privates *ldap_state =
4499 (struct ldapsam_privates *)methods->private_data;
4500 struct ldap_search_state *state;
4502 state = talloc(search, struct ldap_search_state);
4503 if (state == NULL) {
4504 DEBUG(0, ("talloc failed\n"));
4505 return False;
4508 state->connection = ldap_state->smbldap_state;
4510 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4511 state->base = lp_ldap_user_suffix();
4512 else if ((acct_flags != 0) &&
4513 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4514 state->base = lp_ldap_machine_suffix();
4515 else
4516 state->base = lp_ldap_suffix();
4518 state->acct_flags = acct_flags;
4519 state->base = talloc_strdup(search, state->base);
4520 state->scope = LDAP_SCOPE_SUBTREE;
4521 state->filter = get_ldap_filter(search, "*");
4522 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4523 "displayName", "description",
4524 "sambaAcctFlags", NULL);
4525 state->attrsonly = 0;
4526 state->pagedresults_cookie = NULL;
4527 state->entries = NULL;
4528 state->ldap2displayentry = ldapuser2displayentry;
4530 if ((state->filter == NULL) || (state->attrs == NULL)) {
4531 DEBUG(0, ("talloc failed\n"));
4532 return False;
4535 search->private_data = state;
4536 search->next_entry = ldapsam_search_next_entry;
4537 search->search_end = ldapsam_search_end;
4539 return ldapsam_search_firstpage(search);
4542 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4543 TALLOC_CTX *mem_ctx,
4544 LDAP *ld, LDAPMessage *entry,
4545 struct samr_displayentry *result)
4547 char **vals;
4548 size_t converted_size;
4549 DOM_SID sid;
4550 uint16 group_type;
4552 result->account_name = "";
4553 result->fullname = "";
4554 result->description = "";
4557 vals = ldap_get_values(ld, entry, "sambaGroupType");
4558 if ((vals == NULL) || (vals[0] == NULL)) {
4559 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4560 if (vals != NULL) {
4561 ldap_value_free(vals);
4563 return False;
4566 group_type = atoi(vals[0]);
4568 if ((state->group_type != 0) &&
4569 ((state->group_type != group_type))) {
4570 ldap_value_free(vals);
4571 return False;
4574 ldap_value_free(vals);
4576 /* display name is the NT group name */
4578 vals = ldap_get_values(ld, entry, "displayName");
4579 if ((vals == NULL) || (vals[0] == NULL)) {
4580 DEBUG(8, ("\"displayName\" not found\n"));
4582 /* fallback to the 'cn' attribute */
4583 vals = ldap_get_values(ld, entry, "cn");
4584 if ((vals == NULL) || (vals[0] == NULL)) {
4585 DEBUG(5, ("\"cn\" not found\n"));
4586 return False;
4588 if (!pull_utf8_talloc(mem_ctx,
4589 CONST_DISCARD(char **,
4590 &result->account_name),
4591 vals[0], &converted_size))
4593 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4594 "failed: %s", strerror(errno)));
4597 else if (!pull_utf8_talloc(mem_ctx,
4598 CONST_DISCARD(char **,
4599 &result->account_name),
4600 vals[0], &converted_size))
4602 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4603 strerror(errno)));
4606 ldap_value_free(vals);
4608 vals = ldap_get_values(ld, entry, "description");
4609 if ((vals == NULL) || (vals[0] == NULL))
4610 DEBUG(8, ("\"description\" not found\n"));
4611 else if (!pull_utf8_talloc(mem_ctx,
4612 CONST_DISCARD(char **, &result->description),
4613 vals[0], &converted_size))
4615 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4616 strerror(errno)));
4618 ldap_value_free(vals);
4620 if ((result->account_name == NULL) ||
4621 (result->fullname == NULL) ||
4622 (result->description == NULL)) {
4623 DEBUG(0, ("talloc failed\n"));
4624 return False;
4627 vals = ldap_get_values(ld, entry, "sambaSid");
4628 if ((vals == NULL) || (vals[0] == NULL)) {
4629 DEBUG(0, ("\"objectSid\" not found\n"));
4630 if (vals != NULL) {
4631 ldap_value_free(vals);
4633 return False;
4636 if (!string_to_sid(&sid, vals[0])) {
4637 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4638 return False;
4641 ldap_value_free(vals);
4643 switch (group_type) {
4644 case SID_NAME_DOM_GRP:
4645 case SID_NAME_ALIAS:
4647 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4648 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4650 DEBUG(0, ("%s is not in our domain\n",
4651 sid_string_dbg(&sid)));
4652 return False;
4654 break;
4656 default:
4657 DEBUG(0,("unkown group type: %d\n", group_type));
4658 return False;
4661 result->acct_flags = 0;
4663 return True;
4666 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4667 struct pdb_search *search,
4668 const DOM_SID *sid,
4669 enum lsa_SidType type)
4671 struct ldapsam_privates *ldap_state =
4672 (struct ldapsam_privates *)methods->private_data;
4673 struct ldap_search_state *state;
4674 fstring tmp;
4676 state = talloc(search, struct ldap_search_state);
4677 if (state == NULL) {
4678 DEBUG(0, ("talloc failed\n"));
4679 return False;
4682 state->connection = ldap_state->smbldap_state;
4684 state->base = talloc_strdup(search, lp_ldap_group_suffix());
4685 state->connection = ldap_state->smbldap_state;
4686 state->scope = LDAP_SCOPE_SUBTREE;
4687 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4688 "(sambaGroupType=%d)(sambaSID=%s*))",
4689 LDAP_OBJ_GROUPMAP,
4690 type, sid_to_fstring(tmp, sid));
4691 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4692 "displayName", "description",
4693 "sambaGroupType", NULL);
4694 state->attrsonly = 0;
4695 state->pagedresults_cookie = NULL;
4696 state->entries = NULL;
4697 state->group_type = type;
4698 state->ldap2displayentry = ldapgroup2displayentry;
4700 if ((state->filter == NULL) || (state->attrs == NULL)) {
4701 DEBUG(0, ("talloc failed\n"));
4702 return False;
4705 search->private_data = state;
4706 search->next_entry = ldapsam_search_next_entry;
4707 search->search_end = ldapsam_search_end;
4709 return ldapsam_search_firstpage(search);
4712 static bool ldapsam_search_groups(struct pdb_methods *methods,
4713 struct pdb_search *search)
4715 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4718 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4719 struct pdb_search *search,
4720 const DOM_SID *sid)
4722 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4725 static bool ldapsam_rid_algorithm(struct pdb_methods *methods)
4727 return False;
4730 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4731 uint32 *rid)
4733 struct smbldap_state *smbldap_state = priv->smbldap_state;
4735 LDAPMessage *result = NULL;
4736 LDAPMessage *entry = NULL;
4737 LDAPMod **mods = NULL;
4738 NTSTATUS status;
4739 char *value;
4740 int rc;
4741 uint32 nextRid = 0;
4742 const char *dn;
4744 TALLOC_CTX *mem_ctx;
4746 mem_ctx = talloc_new(NULL);
4747 if (mem_ctx == NULL) {
4748 DEBUG(0, ("talloc_new failed\n"));
4749 return NT_STATUS_NO_MEMORY;
4752 status = smbldap_search_domain_info(smbldap_state, &result,
4753 get_global_sam_name(), False);
4754 if (!NT_STATUS_IS_OK(status)) {
4755 DEBUG(3, ("Could not get domain info: %s\n",
4756 nt_errstr(status)));
4757 goto done;
4760 talloc_autofree_ldapmsg(mem_ctx, result);
4762 entry = ldap_first_entry(priv2ld(priv), result);
4763 if (entry == NULL) {
4764 DEBUG(0, ("Could not get domain info entry\n"));
4765 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4766 goto done;
4769 /* Find the largest of the three attributes "sambaNextRid",
4770 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4771 concept of differentiating between user and group rids, and will
4772 use only "sambaNextRid" in the future. But for compatibility
4773 reasons I look if others have chosen different strategies -- VL */
4775 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4776 "sambaNextRid", mem_ctx);
4777 if (value != NULL) {
4778 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4779 nextRid = MAX(nextRid, tmp);
4782 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4783 "sambaNextUserRid", mem_ctx);
4784 if (value != NULL) {
4785 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4786 nextRid = MAX(nextRid, tmp);
4789 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4790 "sambaNextGroupRid", mem_ctx);
4791 if (value != NULL) {
4792 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4793 nextRid = MAX(nextRid, tmp);
4796 if (nextRid == 0) {
4797 nextRid = BASE_RID-1;
4800 nextRid += 1;
4802 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4803 talloc_asprintf(mem_ctx, "%d", nextRid));
4804 talloc_autofree_ldapmod(mem_ctx, mods);
4806 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4807 status = NT_STATUS_NO_MEMORY;
4808 goto done;
4811 rc = smbldap_modify(smbldap_state, dn, mods);
4813 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4814 * please retry" */
4816 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4818 done:
4819 if (NT_STATUS_IS_OK(status)) {
4820 *rid = nextRid;
4823 TALLOC_FREE(mem_ctx);
4824 return status;
4827 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32 *rid)
4829 int i;
4831 for (i=0; i<10; i++) {
4832 NTSTATUS result = ldapsam_get_new_rid(
4833 (struct ldapsam_privates *)methods->private_data, rid);
4834 if (NT_STATUS_IS_OK(result)) {
4835 return result;
4838 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4839 return result;
4842 /* The ldap update failed (maybe a race condition), retry */
4845 /* Tried 10 times, fail. */
4846 return NT_STATUS_ACCESS_DENIED;
4849 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32 *rid)
4851 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4852 return NT_STATUS_IS_OK(result) ? True : False;
4855 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4856 const DOM_SID *sid,
4857 union unid_t *id, enum lsa_SidType *type)
4859 struct ldapsam_privates *priv =
4860 (struct ldapsam_privates *)methods->private_data;
4861 char *filter;
4862 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4863 NULL };
4864 LDAPMessage *result = NULL;
4865 LDAPMessage *entry = NULL;
4866 bool ret = False;
4867 char *value;
4868 int rc;
4870 TALLOC_CTX *mem_ctx;
4872 mem_ctx = talloc_new(NULL);
4873 if (mem_ctx == NULL) {
4874 DEBUG(0, ("talloc_new failed\n"));
4875 return False;
4878 filter = talloc_asprintf(mem_ctx,
4879 "(&(sambaSid=%s)"
4880 "(|(objectClass=%s)(objectClass=%s)))",
4881 sid_string_talloc(mem_ctx, sid),
4882 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
4883 if (filter == NULL) {
4884 DEBUG(5, ("talloc_asprintf failed\n"));
4885 goto done;
4888 rc = smbldap_search_suffix(priv->smbldap_state, filter,
4889 attrs, &result);
4890 if (rc != LDAP_SUCCESS) {
4891 goto done;
4893 talloc_autofree_ldapmsg(mem_ctx, result);
4895 if (ldap_count_entries(priv2ld(priv), result) != 1) {
4896 DEBUG(10, ("Got %d entries, expected one\n",
4897 ldap_count_entries(priv2ld(priv), result)));
4898 goto done;
4901 entry = ldap_first_entry(priv2ld(priv), result);
4903 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4904 "sambaGroupType", mem_ctx);
4906 if (value != NULL) {
4907 const char *gid_str;
4908 /* It's a group */
4910 gid_str = smbldap_talloc_single_attribute(
4911 priv2ld(priv), entry, "gidNumber", mem_ctx);
4912 if (gid_str == NULL) {
4913 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4914 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
4915 entry)));
4916 goto done;
4919 id->gid = strtoul(gid_str, NULL, 10);
4920 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
4921 ret = True;
4922 goto done;
4925 /* It must be a user */
4927 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4928 "uidNumber", mem_ctx);
4929 if (value == NULL) {
4930 DEBUG(1, ("Could not find uidNumber in %s\n",
4931 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
4932 goto done;
4935 id->uid = strtoul(value, NULL, 10);
4936 *type = SID_NAME_USER;
4938 ret = True;
4939 done:
4940 TALLOC_FREE(mem_ctx);
4941 return ret;
4945 * The following functions is called only if
4946 * ldapsam:trusted and ldapsam:editposix are
4947 * set to true
4951 * ldapsam_create_user creates a new
4952 * posixAccount and sambaSamAccount object
4953 * in the ldap users subtree
4955 * The uid is allocated by winbindd.
4958 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
4959 TALLOC_CTX *tmp_ctx, const char *name,
4960 uint32 acb_info, uint32 *rid)
4962 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
4963 LDAPMessage *entry = NULL;
4964 LDAPMessage *result = NULL;
4965 uint32 num_result;
4966 bool is_machine = False;
4967 bool add_posix = False;
4968 LDAPMod **mods = NULL;
4969 struct samu *user;
4970 char *filter;
4971 char *username;
4972 char *homedir;
4973 char *gidstr;
4974 char *uidstr;
4975 char *shell;
4976 const char *dn = NULL;
4977 DOM_SID group_sid;
4978 DOM_SID user_sid;
4979 gid_t gid = -1;
4980 uid_t uid = -1;
4981 NTSTATUS ret;
4982 int rc;
4984 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
4985 acb_info & ACB_WSTRUST ||
4986 acb_info & ACB_SVRTRUST ||
4987 acb_info & ACB_DOMTRUST) {
4988 is_machine = True;
4991 username = escape_ldap_string_alloc(name);
4992 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
4993 username, LDAP_OBJ_POSIXACCOUNT);
4994 SAFE_FREE(username);
4996 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
4997 if (rc != LDAP_SUCCESS) {
4998 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
4999 return NT_STATUS_ACCESS_DENIED;
5001 talloc_autofree_ldapmsg(tmp_ctx, result);
5003 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5005 if (num_result > 1) {
5006 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5007 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5010 if (num_result == 1) {
5011 char *tmp;
5012 /* check if it is just a posix account.
5013 * or if there is a sid attached to this entry
5016 entry = ldap_first_entry(priv2ld(ldap_state), result);
5017 if (!entry) {
5018 return NT_STATUS_UNSUCCESSFUL;
5021 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5022 if (tmp) {
5023 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5024 return NT_STATUS_USER_EXISTS;
5027 /* it is just a posix account, retrieve the dn for later use */
5028 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5029 if (!dn) {
5030 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5031 return NT_STATUS_NO_MEMORY;
5035 if (num_result == 0) {
5036 add_posix = True;
5039 /* Create the basic samu structure and generate the mods for the ldap commit */
5040 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5041 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5042 return ret;
5045 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5047 user = samu_new(tmp_ctx);
5048 if (!user) {
5049 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5050 return NT_STATUS_NO_MEMORY;
5053 if (!pdb_set_username(user, name, PDB_SET)) {
5054 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5055 return NT_STATUS_UNSUCCESSFUL;
5057 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5058 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5059 return NT_STATUS_UNSUCCESSFUL;
5061 if (is_machine) {
5062 if (acb_info & ACB_NORMAL) {
5063 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5064 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5065 return NT_STATUS_UNSUCCESSFUL;
5067 } else {
5068 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5069 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5070 return NT_STATUS_UNSUCCESSFUL;
5073 } else {
5074 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5075 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5076 return NT_STATUS_UNSUCCESSFUL;
5080 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5081 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5082 return NT_STATUS_UNSUCCESSFUL;
5085 if (!init_ldap_from_sam(ldap_state, NULL, &mods, user, element_is_set_or_changed)) {
5086 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5087 return NT_STATUS_UNSUCCESSFUL;
5090 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5091 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5093 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5095 if (add_posix) {
5096 char *escape_name;
5098 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5100 /* retrieve the Domain Users group gid */
5101 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS) ||
5102 !sid_to_gid(&group_sid, &gid)) {
5103 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5104 return NT_STATUS_INVALID_PRIMARY_GROUP;
5107 /* lets allocate a new userid for this user */
5108 if (!winbind_allocate_uid(&uid)) {
5109 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5110 return NT_STATUS_UNSUCCESSFUL;
5114 if (is_machine) {
5115 /* TODO: choose a more appropriate default for machines */
5116 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5117 shell = talloc_strdup(tmp_ctx, "/bin/false");
5118 } else {
5119 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5120 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5122 uidstr = talloc_asprintf(tmp_ctx, "%d", uid);
5123 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
5125 escape_name = escape_rdn_val_string_alloc(name);
5126 if (!escape_name) {
5127 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5128 return NT_STATUS_NO_MEMORY;
5131 if (is_machine) {
5132 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5133 } else {
5134 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5137 SAFE_FREE(escape_name);
5139 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5140 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5141 return NT_STATUS_NO_MEMORY;
5144 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5145 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5146 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5147 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5148 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5149 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5150 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5153 talloc_autofree_ldapmod(tmp_ctx, mods);
5155 if (add_posix) {
5156 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5157 } else {
5158 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5161 if (rc != LDAP_SUCCESS) {
5162 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5163 return NT_STATUS_UNSUCCESSFUL;
5166 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5168 flush_pwnam_cache();
5170 return NT_STATUS_OK;
5173 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5175 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5176 LDAPMessage *result = NULL;
5177 LDAPMessage *entry = NULL;
5178 int num_result;
5179 const char *dn;
5180 char *filter;
5181 int rc;
5183 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5185 filter = talloc_asprintf(tmp_ctx,
5186 "(&(uid=%s)"
5187 "(objectClass=%s)"
5188 "(objectClass=%s))",
5189 pdb_get_username(sam_acct),
5190 LDAP_OBJ_POSIXACCOUNT,
5191 LDAP_OBJ_SAMBASAMACCOUNT);
5192 if (filter == NULL) {
5193 return NT_STATUS_NO_MEMORY;
5196 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5197 if (rc != LDAP_SUCCESS) {
5198 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5199 return NT_STATUS_UNSUCCESSFUL;
5201 talloc_autofree_ldapmsg(tmp_ctx, result);
5203 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5205 if (num_result == 0) {
5206 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5207 return NT_STATUS_NO_SUCH_USER;
5210 if (num_result > 1) {
5211 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5212 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5215 entry = ldap_first_entry(priv2ld(ldap_state), result);
5216 if (!entry) {
5217 return NT_STATUS_UNSUCCESSFUL;
5220 /* it is just a posix account, retrieve the dn for later use */
5221 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5222 if (!dn) {
5223 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5224 return NT_STATUS_NO_MEMORY;
5227 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5228 if (rc != LDAP_SUCCESS) {
5229 return NT_STATUS_UNSUCCESSFUL;
5232 flush_pwnam_cache();
5234 return NT_STATUS_OK;
5238 * ldapsam_create_group creates a new
5239 * posixGroup and sambaGroupMapping object
5240 * in the ldap groups subtree
5242 * The gid is allocated by winbindd.
5245 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5246 TALLOC_CTX *tmp_ctx,
5247 const char *name,
5248 uint32 *rid)
5250 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5251 NTSTATUS ret;
5252 LDAPMessage *entry = NULL;
5253 LDAPMessage *result = NULL;
5254 uint32 num_result;
5255 bool is_new_entry = False;
5256 LDAPMod **mods = NULL;
5257 char *filter;
5258 char *groupsidstr;
5259 char *groupname;
5260 char *grouptype;
5261 char *gidstr;
5262 const char *dn = NULL;
5263 DOM_SID group_sid;
5264 gid_t gid = -1;
5265 int rc;
5267 groupname = escape_ldap_string_alloc(name);
5268 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5269 groupname, LDAP_OBJ_POSIXGROUP);
5270 SAFE_FREE(groupname);
5272 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5273 if (rc != LDAP_SUCCESS) {
5274 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5275 return NT_STATUS_UNSUCCESSFUL;
5277 talloc_autofree_ldapmsg(tmp_ctx, result);
5279 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5281 if (num_result > 1) {
5282 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5283 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5286 if (num_result == 1) {
5287 char *tmp;
5288 /* check if it is just a posix group.
5289 * or if there is a sid attached to this entry
5292 entry = ldap_first_entry(priv2ld(ldap_state), result);
5293 if (!entry) {
5294 return NT_STATUS_UNSUCCESSFUL;
5297 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5298 if (tmp) {
5299 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5300 return NT_STATUS_GROUP_EXISTS;
5303 /* it is just a posix group, retrieve the gid and the dn for later use */
5304 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5305 if (!tmp) {
5306 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5307 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5310 gid = strtoul(tmp, NULL, 10);
5312 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5313 if (!dn) {
5314 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5315 return NT_STATUS_NO_MEMORY;
5319 if (num_result == 0) {
5320 char *escape_name;
5322 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5324 is_new_entry = True;
5326 /* lets allocate a new groupid for this group */
5327 if (!winbind_allocate_gid(&gid)) {
5328 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5329 return NT_STATUS_UNSUCCESSFUL;
5332 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
5334 escape_name = escape_rdn_val_string_alloc(name);
5335 if (!escape_name) {
5336 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5337 return NT_STATUS_NO_MEMORY;
5340 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5342 SAFE_FREE(escape_name);
5344 if (!gidstr || !dn) {
5345 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5346 return NT_STATUS_NO_MEMORY;
5349 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5350 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5351 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5354 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5355 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5356 return ret;
5359 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5361 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5362 &group_sid));
5363 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5365 if (!groupsidstr || !grouptype) {
5366 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5367 return NT_STATUS_NO_MEMORY;
5370 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5371 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5372 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5373 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5374 talloc_autofree_ldapmod(tmp_ctx, mods);
5376 if (is_new_entry) {
5377 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5378 #if 0
5379 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5380 /* This call may fail with rfc2307bis schema */
5381 /* Retry adding a structural class */
5382 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5383 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5385 #endif
5386 } else {
5387 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5390 if (rc != LDAP_SUCCESS) {
5391 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5392 return NT_STATUS_UNSUCCESSFUL;
5395 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5397 return NT_STATUS_OK;
5400 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32 rid)
5402 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5403 LDAPMessage *result = NULL;
5404 LDAPMessage *entry = NULL;
5405 int num_result;
5406 const char *dn;
5407 char *gidstr;
5408 char *filter;
5409 DOM_SID group_sid;
5410 int rc;
5412 /* get the group sid */
5413 sid_compose(&group_sid, get_global_sam_sid(), rid);
5415 filter = talloc_asprintf(tmp_ctx,
5416 "(&(sambaSID=%s)"
5417 "(objectClass=%s)"
5418 "(objectClass=%s))",
5419 sid_string_talloc(tmp_ctx, &group_sid),
5420 LDAP_OBJ_POSIXGROUP,
5421 LDAP_OBJ_GROUPMAP);
5422 if (filter == NULL) {
5423 return NT_STATUS_NO_MEMORY;
5426 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5427 if (rc != LDAP_SUCCESS) {
5428 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5429 return NT_STATUS_UNSUCCESSFUL;
5431 talloc_autofree_ldapmsg(tmp_ctx, result);
5433 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5435 if (num_result == 0) {
5436 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5437 return NT_STATUS_NO_SUCH_GROUP;
5440 if (num_result > 1) {
5441 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5442 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5445 entry = ldap_first_entry(priv2ld(ldap_state), result);
5446 if (!entry) {
5447 return NT_STATUS_UNSUCCESSFUL;
5450 /* here it is, retrieve the dn for later use */
5451 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5452 if (!dn) {
5453 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5454 return NT_STATUS_NO_MEMORY;
5457 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5458 if (!gidstr) {
5459 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5460 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5463 /* check no user have this group marked as primary group */
5464 filter = talloc_asprintf(tmp_ctx,
5465 "(&(gidNumber=%s)"
5466 "(objectClass=%s)"
5467 "(objectClass=%s))",
5468 gidstr,
5469 LDAP_OBJ_POSIXACCOUNT,
5470 LDAP_OBJ_SAMBASAMACCOUNT);
5472 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5473 if (rc != LDAP_SUCCESS) {
5474 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5475 return NT_STATUS_UNSUCCESSFUL;
5477 talloc_autofree_ldapmsg(tmp_ctx, result);
5479 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5481 if (num_result != 0) {
5482 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5483 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5486 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5487 if (rc != LDAP_SUCCESS) {
5488 return NT_STATUS_UNSUCCESSFUL;
5491 return NT_STATUS_OK;
5494 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5495 TALLOC_CTX *tmp_ctx,
5496 uint32 group_rid,
5497 uint32 member_rid,
5498 int modop)
5500 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5501 LDAPMessage *entry = NULL;
5502 LDAPMessage *result = NULL;
5503 uint32 num_result;
5504 LDAPMod **mods = NULL;
5505 char *filter;
5506 char *uidstr;
5507 const char *dn = NULL;
5508 DOM_SID group_sid;
5509 DOM_SID member_sid;
5510 int rc;
5512 switch (modop) {
5513 case LDAP_MOD_ADD:
5514 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5515 break;
5516 case LDAP_MOD_DELETE:
5517 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5518 break;
5519 default:
5520 return NT_STATUS_UNSUCCESSFUL;
5523 /* get member sid */
5524 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5526 /* get the group sid */
5527 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5529 filter = talloc_asprintf(tmp_ctx,
5530 "(&(sambaSID=%s)"
5531 "(objectClass=%s)"
5532 "(objectClass=%s))",
5533 sid_string_talloc(tmp_ctx, &member_sid),
5534 LDAP_OBJ_POSIXACCOUNT,
5535 LDAP_OBJ_SAMBASAMACCOUNT);
5536 if (filter == NULL) {
5537 return NT_STATUS_NO_MEMORY;
5540 /* get the member uid */
5541 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5542 if (rc != LDAP_SUCCESS) {
5543 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5544 return NT_STATUS_UNSUCCESSFUL;
5546 talloc_autofree_ldapmsg(tmp_ctx, result);
5548 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5550 if (num_result == 0) {
5551 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5552 return NT_STATUS_NO_SUCH_MEMBER;
5555 if (num_result > 1) {
5556 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5557 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5560 entry = ldap_first_entry(priv2ld(ldap_state), result);
5561 if (!entry) {
5562 return NT_STATUS_UNSUCCESSFUL;
5565 if (modop == LDAP_MOD_DELETE) {
5566 /* check if we are trying to remove the member from his primary group */
5567 char *gidstr;
5568 gid_t user_gid, group_gid;
5570 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5571 if (!gidstr) {
5572 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5573 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5576 user_gid = strtoul(gidstr, NULL, 10);
5578 if (!sid_to_gid(&group_sid, &group_gid)) {
5579 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5580 return NT_STATUS_UNSUCCESSFUL;
5583 if (user_gid == group_gid) {
5584 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5585 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5589 /* here it is, retrieve the uid for later use */
5590 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5591 if (!uidstr) {
5592 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5593 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5596 filter = talloc_asprintf(tmp_ctx,
5597 "(&(sambaSID=%s)"
5598 "(objectClass=%s)"
5599 "(objectClass=%s))",
5600 sid_string_talloc(tmp_ctx, &group_sid),
5601 LDAP_OBJ_POSIXGROUP,
5602 LDAP_OBJ_GROUPMAP);
5604 /* get the group */
5605 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5606 if (rc != LDAP_SUCCESS) {
5607 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5608 return NT_STATUS_UNSUCCESSFUL;
5610 talloc_autofree_ldapmsg(tmp_ctx, result);
5612 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5614 if (num_result == 0) {
5615 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5616 return NT_STATUS_NO_SUCH_GROUP;
5619 if (num_result > 1) {
5620 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5621 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5624 entry = ldap_first_entry(priv2ld(ldap_state), result);
5625 if (!entry) {
5626 return NT_STATUS_UNSUCCESSFUL;
5629 /* here it is, retrieve the dn for later use */
5630 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5631 if (!dn) {
5632 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5633 return NT_STATUS_NO_MEMORY;
5636 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5638 talloc_autofree_ldapmod(tmp_ctx, mods);
5640 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5641 if (rc != LDAP_SUCCESS) {
5642 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5643 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5644 return NT_STATUS_MEMBER_IN_GROUP;
5646 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5647 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5648 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5650 return NT_STATUS_UNSUCCESSFUL;
5653 return NT_STATUS_OK;
5656 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5657 TALLOC_CTX *tmp_ctx,
5658 uint32 group_rid,
5659 uint32 member_rid)
5661 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5663 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5664 TALLOC_CTX *tmp_ctx,
5665 uint32 group_rid,
5666 uint32 member_rid)
5668 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5671 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5672 TALLOC_CTX *mem_ctx,
5673 struct samu *sampass)
5675 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5676 LDAPMessage *entry = NULL;
5677 LDAPMessage *result = NULL;
5678 uint32 num_result;
5679 LDAPMod **mods = NULL;
5680 char *filter;
5681 char *escape_username;
5682 char *gidstr;
5683 const char *dn = NULL;
5684 gid_t gid;
5685 int rc;
5687 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5689 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5690 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
5691 return NT_STATUS_UNSUCCESSFUL;
5693 gidstr = talloc_asprintf(mem_ctx, "%d", gid);
5694 if (!gidstr) {
5695 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5696 return NT_STATUS_NO_MEMORY;
5699 escape_username = escape_ldap_string_alloc(pdb_get_username(sampass));
5700 if (escape_username== NULL) {
5701 return NT_STATUS_NO_MEMORY;
5704 filter = talloc_asprintf(mem_ctx,
5705 "(&(uid=%s)"
5706 "(objectClass=%s)"
5707 "(objectClass=%s))",
5708 escape_username,
5709 LDAP_OBJ_POSIXACCOUNT,
5710 LDAP_OBJ_SAMBASAMACCOUNT);
5712 SAFE_FREE(escape_username);
5714 if (filter == NULL) {
5715 return NT_STATUS_NO_MEMORY;
5718 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5719 if (rc != LDAP_SUCCESS) {
5720 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5721 return NT_STATUS_UNSUCCESSFUL;
5723 talloc_autofree_ldapmsg(mem_ctx, result);
5725 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5727 if (num_result == 0) {
5728 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5729 return NT_STATUS_NO_SUCH_USER;
5732 if (num_result > 1) {
5733 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
5734 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5737 entry = ldap_first_entry(priv2ld(ldap_state), result);
5738 if (!entry) {
5739 return NT_STATUS_UNSUCCESSFUL;
5742 /* retrieve the dn for later use */
5743 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
5744 if (!dn) {
5745 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5746 return NT_STATUS_NO_MEMORY;
5749 /* remove the old one, and add the new one, this way we do not risk races */
5750 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
5752 if (mods == NULL) {
5753 return NT_STATUS_OK;
5756 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5758 if (rc != LDAP_SUCCESS) {
5759 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5760 pdb_get_username(sampass), gidstr));
5761 return NT_STATUS_UNSUCCESSFUL;
5764 flush_pwnam_cache();
5766 return NT_STATUS_OK;
5770 /**********************************************************************
5771 trusted domains functions
5772 *********************************************************************/
5774 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
5775 const char *domain)
5777 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
5778 ldap_state->domain_dn);
5781 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
5782 TALLOC_CTX *mem_ctx,
5783 const char *domain, LDAPMessage **entry)
5785 int rc;
5786 char *filter;
5787 int scope = LDAP_SCOPE_SUBTREE;
5788 const char **attrs = NULL; /* NULL: get all attrs */
5789 int attrsonly = 0; /* 0: return values too */
5790 LDAPMessage *result = NULL;
5791 char *trusted_dn;
5792 uint32 num_result;
5794 filter = talloc_asprintf(talloc_tos(),
5795 "(&(objectClass=%s)(sambaDomainName=%s))",
5796 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
5798 trusted_dn = trusteddom_dn(ldap_state, domain);
5799 if (trusted_dn == NULL) {
5800 return False;
5802 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
5803 filter, attrs, attrsonly, &result);
5805 if (result != NULL) {
5806 talloc_autofree_ldapmsg(mem_ctx, result);
5809 if (rc == LDAP_NO_SUCH_OBJECT) {
5810 *entry = NULL;
5811 return True;
5814 if (rc != LDAP_SUCCESS) {
5815 return False;
5818 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5820 if (num_result > 1) {
5821 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5822 "%s object for domain '%s'?!\n",
5823 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
5824 return False;
5827 if (num_result == 0) {
5828 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5829 "%s object for domain %s.\n",
5830 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
5831 *entry = NULL;
5832 } else {
5833 *entry = ldap_first_entry(priv2ld(ldap_state), result);
5836 return True;
5839 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
5840 const char *domain,
5841 char** pwd,
5842 DOM_SID *sid,
5843 time_t *pass_last_set_time)
5845 struct ldapsam_privates *ldap_state =
5846 (struct ldapsam_privates *)methods->private_data;
5847 LDAPMessage *entry = NULL;
5849 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
5851 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
5852 (entry == NULL))
5854 return False;
5857 /* password */
5858 if (pwd != NULL) {
5859 char *pwd_str;
5860 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5861 entry, "sambaClearTextPassword", talloc_tos());
5862 if (pwd_str == NULL) {
5863 return False;
5865 /* trusteddom_pw routines do not use talloc yet... */
5866 *pwd = SMB_STRDUP(pwd_str);
5867 if (*pwd == NULL) {
5868 return False;
5872 /* last change time */
5873 if (pass_last_set_time != NULL) {
5874 char *time_str;
5875 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5876 entry, "sambaPwdLastSet", talloc_tos());
5877 if (time_str == NULL) {
5878 return False;
5880 *pass_last_set_time = (time_t)atol(time_str);
5883 /* domain sid */
5884 if (sid != NULL) {
5885 char *sid_str;
5886 DOM_SID *dom_sid;
5887 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5888 entry, "sambaSID",
5889 talloc_tos());
5890 if (sid_str == NULL) {
5891 return False;
5893 dom_sid = string_sid_talloc(talloc_tos(), sid_str);
5894 if (dom_sid == NULL) {
5895 return False;
5897 sid_copy(sid, dom_sid);
5900 return True;
5903 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
5904 const char* domain,
5905 const char* pwd,
5906 const DOM_SID *sid)
5908 struct ldapsam_privates *ldap_state =
5909 (struct ldapsam_privates *)methods->private_data;
5910 LDAPMessage *entry = NULL;
5911 LDAPMod **mods = NULL;
5912 char *prev_pwd = NULL;
5913 char *trusted_dn = NULL;
5914 int rc;
5916 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
5919 * get the current entry (if there is one) in order to put the
5920 * current password into the previous password attribute
5922 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
5923 return False;
5926 mods = NULL;
5927 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
5928 LDAP_OBJ_TRUSTDOM_PASSWORD);
5929 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
5930 domain);
5931 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
5932 sid_string_tos(sid));
5933 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
5934 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
5935 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5936 "sambaClearTextPassword", pwd);
5938 talloc_autofree_ldapmod(talloc_tos(), mods);
5940 if (entry != NULL) {
5941 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5942 entry, "sambaClearTextPassword", talloc_tos());
5943 if (prev_pwd != NULL) {
5944 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5945 "sambaPreviousClearTextPassword",
5946 prev_pwd);
5950 trusted_dn = trusteddom_dn(ldap_state, domain);
5951 if (trusted_dn == NULL) {
5952 return False;
5954 if (entry == NULL) {
5955 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
5956 } else {
5957 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
5960 if (rc != LDAP_SUCCESS) {
5961 DEBUG(1, ("error writing trusted domain password!\n"));
5962 return False;
5965 return True;
5968 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
5969 const char *domain)
5971 int rc;
5972 struct ldapsam_privates *ldap_state =
5973 (struct ldapsam_privates *)methods->private_data;
5974 LDAPMessage *entry = NULL;
5975 const char *trusted_dn;
5977 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
5978 return False;
5981 if (entry == NULL) {
5982 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
5983 "%s\n", domain));
5984 return True;
5987 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
5988 entry);
5989 if (trusted_dn == NULL) {
5990 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
5991 return False;
5994 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
5995 if (rc != LDAP_SUCCESS) {
5996 return False;
5999 return True;
6002 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6003 TALLOC_CTX *mem_ctx,
6004 uint32 *num_domains,
6005 struct trustdom_info ***domains)
6007 int rc;
6008 struct ldapsam_privates *ldap_state =
6009 (struct ldapsam_privates *)methods->private_data;
6010 char *filter;
6011 int scope = LDAP_SCOPE_SUBTREE;
6012 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6013 int attrsonly = 0; /* 0: return values too */
6014 LDAPMessage *result = NULL;
6015 LDAPMessage *entry = NULL;
6017 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6018 LDAP_OBJ_TRUSTDOM_PASSWORD);
6020 rc = smbldap_search(ldap_state->smbldap_state,
6021 ldap_state->domain_dn,
6022 scope,
6023 filter,
6024 attrs,
6025 attrsonly,
6026 &result);
6028 if (result != NULL) {
6029 talloc_autofree_ldapmsg(mem_ctx, result);
6032 if (rc != LDAP_SUCCESS) {
6033 return NT_STATUS_UNSUCCESSFUL;
6036 *num_domains = 0;
6037 if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
6038 DEBUG(1, ("talloc failed\n"));
6039 return NT_STATUS_NO_MEMORY;
6042 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6043 entry != NULL;
6044 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6046 char *dom_name, *dom_sid_str;
6047 struct trustdom_info *dom_info;
6049 dom_info = TALLOC_P(*domains, struct trustdom_info);
6050 if (dom_info == NULL) {
6051 DEBUG(1, ("talloc failed\n"));
6052 return NT_STATUS_NO_MEMORY;
6055 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6056 entry,
6057 "sambaDomainName",
6058 talloc_tos());
6059 if (dom_name == NULL) {
6060 DEBUG(1, ("talloc failed\n"));
6061 return NT_STATUS_NO_MEMORY;
6063 dom_info->name = dom_name;
6065 dom_sid_str = smbldap_talloc_single_attribute(
6066 priv2ld(ldap_state), entry, "sambaSID",
6067 talloc_tos());
6068 if (dom_sid_str == NULL) {
6069 DEBUG(1, ("talloc failed\n"));
6070 return NT_STATUS_NO_MEMORY;
6072 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6073 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6074 dom_sid_str));
6075 return NT_STATUS_UNSUCCESSFUL;
6078 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6079 domains, num_domains);
6081 if (*domains == NULL) {
6082 DEBUG(1, ("talloc failed\n"));
6083 return NT_STATUS_NO_MEMORY;
6087 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6088 return NT_STATUS_OK;
6092 /**********************************************************************
6093 Housekeeping
6094 *********************************************************************/
6096 static void free_private_data(void **vp)
6098 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6100 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6102 if ((*ldap_state)->result != NULL) {
6103 ldap_msgfree((*ldap_state)->result);
6104 (*ldap_state)->result = NULL;
6106 if ((*ldap_state)->domain_dn != NULL) {
6107 SAFE_FREE((*ldap_state)->domain_dn);
6110 *ldap_state = NULL;
6112 /* No need to free any further, as it is talloc()ed */
6115 /*********************************************************************
6116 Intitalise the parts of the pdb_methods structure that are common to
6117 all pdb_ldap modes
6118 *********************************************************************/
6120 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6122 NTSTATUS nt_status;
6123 struct ldapsam_privates *ldap_state;
6125 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6126 return nt_status;
6129 (*pdb_method)->name = "ldapsam";
6131 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6132 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6133 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6134 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6135 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6136 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6138 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6139 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6140 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6141 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6142 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6143 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6144 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6146 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6147 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6149 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6151 (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
6152 (*pdb_method)->new_rid = ldapsam_new_rid;
6154 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6155 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6156 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6157 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6159 /* TODO: Setup private data and free */
6161 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
6162 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6163 return NT_STATUS_NO_MEMORY;
6166 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6167 location, &ldap_state->smbldap_state);
6169 if ( !NT_STATUS_IS_OK(nt_status) ) {
6170 return nt_status;
6173 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6174 return NT_STATUS_NO_MEMORY;
6177 (*pdb_method)->private_data = ldap_state;
6179 (*pdb_method)->free_private_data = free_private_data;
6181 return NT_STATUS_OK;
6184 /**********************************************************************
6185 Initialise the 'compat' mode for pdb_ldap
6186 *********************************************************************/
6188 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6190 NTSTATUS nt_status;
6191 struct ldapsam_privates *ldap_state;
6192 char *uri = talloc_strdup( NULL, location );
6194 trim_char( uri, '\"', '\"' );
6195 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6196 if ( uri )
6197 TALLOC_FREE( uri );
6199 if ( !NT_STATUS_IS_OK(nt_status) ) {
6200 return nt_status;
6203 (*pdb_method)->name = "ldapsam_compat";
6205 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6206 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6208 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6210 return NT_STATUS_OK;
6213 /**********************************************************************
6214 Initialise the normal mode for pdb_ldap
6215 *********************************************************************/
6217 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6219 NTSTATUS nt_status;
6220 struct ldapsam_privates *ldap_state = NULL;
6221 uint32 alg_rid_base;
6222 char *alg_rid_base_string = NULL;
6223 LDAPMessage *result = NULL;
6224 LDAPMessage *entry = NULL;
6225 DOM_SID ldap_domain_sid;
6226 DOM_SID secrets_domain_sid;
6227 char *domain_sid_string = NULL;
6228 char *dn = NULL;
6229 char *uri = talloc_strdup( NULL, location );
6231 trim_char( uri, '\"', '\"' );
6232 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6233 if (uri) {
6234 TALLOC_FREE(uri);
6237 if (!NT_STATUS_IS_OK(nt_status)) {
6238 return nt_status;
6241 (*pdb_method)->name = "ldapsam";
6243 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6244 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6245 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6246 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6247 (*pdb_method)->search_users = ldapsam_search_users;
6248 (*pdb_method)->search_groups = ldapsam_search_groups;
6249 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6251 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6252 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6253 (*pdb_method)->enum_group_memberships =
6254 ldapsam_enum_group_memberships;
6255 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6256 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6258 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6259 (*pdb_method)->create_user = ldapsam_create_user;
6260 (*pdb_method)->delete_user = ldapsam_delete_user;
6261 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6262 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6263 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6264 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6265 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6269 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6270 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6272 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6274 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6275 &result,
6276 ldap_state->domain_name, True);
6278 if ( !NT_STATUS_IS_OK(nt_status) ) {
6279 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6280 "info, nor add one to the domain\n"));
6281 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6282 "will be unable to allocate new users/groups, "
6283 "and will risk BDCs having inconsistant SIDs\n"));
6284 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6285 return NT_STATUS_OK;
6288 /* Given that the above might fail, everything below this must be
6289 * optional */
6291 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6292 result);
6293 if (!entry) {
6294 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6295 "entry\n"));
6296 ldap_msgfree(result);
6297 return NT_STATUS_UNSUCCESSFUL;
6300 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6301 if (!dn) {
6302 ldap_msgfree(result);
6303 return NT_STATUS_UNSUCCESSFUL;
6306 ldap_state->domain_dn = smb_xstrdup(dn);
6307 TALLOC_FREE(dn);
6309 domain_sid_string = smbldap_talloc_single_attribute(
6310 ldap_state->smbldap_state->ldap_struct,
6311 entry,
6312 get_userattr_key2string(ldap_state->schema_ver,
6313 LDAP_ATTR_USER_SID),
6314 talloc_tos());
6316 if (domain_sid_string) {
6317 bool found_sid;
6318 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6319 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6320 "read as a valid SID\n", domain_sid_string));
6321 ldap_msgfree(result);
6322 TALLOC_FREE(domain_sid_string);
6323 return NT_STATUS_INVALID_PARAMETER;
6325 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6326 &secrets_domain_sid);
6327 if (!found_sid || !sid_equal(&secrets_domain_sid,
6328 &ldap_domain_sid)) {
6329 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6330 "%s based on pdb_ldap results %s -> %s\n",
6331 ldap_state->domain_name,
6332 sid_string_dbg(&secrets_domain_sid),
6333 sid_string_dbg(&ldap_domain_sid)));
6335 /* reset secrets.tdb sid */
6336 secrets_store_domain_sid(ldap_state->domain_name,
6337 &ldap_domain_sid);
6338 DEBUG(1, ("New global sam SID: %s\n",
6339 sid_string_dbg(get_global_sam_sid())));
6341 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6342 TALLOC_FREE(domain_sid_string);
6345 alg_rid_base_string = smbldap_talloc_single_attribute(
6346 ldap_state->smbldap_state->ldap_struct,
6347 entry,
6348 get_attr_key2string( dominfo_attr_list,
6349 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6350 talloc_tos());
6351 if (alg_rid_base_string) {
6352 alg_rid_base = (uint32)atol(alg_rid_base_string);
6353 if (alg_rid_base != algorithmic_rid_base()) {
6354 DEBUG(0, ("The value of 'algorithmic RID base' has "
6355 "changed since the LDAP\n"
6356 "database was initialised. Aborting. \n"));
6357 ldap_msgfree(result);
6358 TALLOC_FREE(alg_rid_base_string);
6359 return NT_STATUS_UNSUCCESSFUL;
6361 TALLOC_FREE(alg_rid_base_string);
6363 ldap_msgfree(result);
6365 return NT_STATUS_OK;
6368 NTSTATUS pdb_ldap_init(void)
6370 NTSTATUS nt_status;
6371 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6372 return nt_status;
6374 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6375 return nt_status;
6377 /* Let pdb_nds register backends */
6378 pdb_nds_init();
6380 return NT_STATUS_OK;