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/>.
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
49 #define DBGC_CLASS DBGC_PASSDB
55 * Work around versions of the LDAP client libs that don't have the OIDs
56 * defined, or have them defined under the old name.
57 * This functionality is really a factor of the server, not the client
61 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
62 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
63 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
64 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
67 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
68 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
69 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
70 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
73 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
75 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
76 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
82 /**********************************************************************
83 Simple helper function to make stuff better readable
84 **********************************************************************/
86 static LDAP *priv2ld(struct ldapsam_privates *priv)
88 return priv->smbldap_state->ldap_struct;
91 /**********************************************************************
92 Get the attribute name given a user schame version.
93 **********************************************************************/
95 static const char* get_userattr_key2string( int schema_ver, int key )
97 switch ( schema_ver ) {
98 case SCHEMAVER_SAMBAACCOUNT:
99 return get_attr_key2string( attrib_map_v22, key );
101 case SCHEMAVER_SAMBASAMACCOUNT:
102 return get_attr_key2string( attrib_map_v30, key );
105 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
111 /**********************************************************************
112 Return the list of attribute names given a user schema version.
113 **********************************************************************/
115 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
117 switch ( schema_ver ) {
118 case SCHEMAVER_SAMBAACCOUNT:
119 return get_attr_list( mem_ctx, attrib_map_v22 );
121 case SCHEMAVER_SAMBASAMACCOUNT:
122 return get_attr_list( mem_ctx, attrib_map_v30 );
124 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
131 /**************************************************************************
132 Return the list of attribute names to delete given a user schema version.
133 **************************************************************************/
135 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
138 switch ( schema_ver ) {
139 case SCHEMAVER_SAMBAACCOUNT:
140 return get_attr_list( mem_ctx,
141 attrib_map_to_delete_v22 );
143 case SCHEMAVER_SAMBASAMACCOUNT:
144 return get_attr_list( mem_ctx,
145 attrib_map_to_delete_v30 );
147 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
155 /*******************************************************************
156 Generate the LDAP search filter for the objectclass based on the
157 version of the schema we are using.
158 ******************************************************************/
160 static const char* get_objclass_filter( int schema_ver )
162 fstring objclass_filter;
165 switch( schema_ver ) {
166 case SCHEMAVER_SAMBAACCOUNT:
167 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
169 case SCHEMAVER_SAMBASAMACCOUNT:
170 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
173 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
174 objclass_filter[0] = '\0';
178 result = talloc_strdup(talloc_tos(), objclass_filter);
179 SMB_ASSERT(result != NULL);
183 /*****************************************************************
184 Scan a sequence number off OpenLDAP's syncrepl contextCSN
185 ******************************************************************/
187 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
189 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
190 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
191 LDAPMessage *msg = NULL;
192 LDAPMessage *entry = NULL;
194 char **values = NULL;
195 int rc, num_result, num_values, rid;
201 /* Unfortunatly there is no proper way to detect syncrepl-support in
202 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
203 * but do not show up in the root-DSE yet. Neither we can query the
204 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
205 * objectclass. Currently we require lp_ldap_suffix() to show up as
206 * namingContext. - Guenther
209 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
214 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
218 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
219 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
220 "as top-level namingContext\n", lp_ldap_suffix()));
224 mem_ctx = talloc_init("ldapsam_get_seq_num");
227 return NT_STATUS_NO_MEMORY;
229 if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
230 ntstatus = NT_STATUS_NO_MEMORY;
234 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
235 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
238 /* consumer syncreplCookie: */
239 /* csn=20050126161620Z#0000001#00#00000 */
240 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
242 suffix = talloc_asprintf(mem_ctx,
243 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
245 ntstatus = NT_STATUS_NO_MEMORY;
250 /* provider contextCSN */
251 /* 20050126161620Z#000009#00#000000 */
252 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
254 suffix = talloc_asprintf(mem_ctx,
255 "cn=ldapsync,%s", lp_ldap_suffix());
258 ntstatus = NT_STATUS_NO_MEMORY;
263 rc = smbldap_search(ldap_state->smbldap_state, suffix,
264 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
266 if (rc != LDAP_SUCCESS) {
270 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
271 if (num_result != 1) {
272 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
276 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
278 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
282 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
283 if (values == NULL) {
284 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
288 num_values = ldap_count_values(values);
289 if (num_values == 0) {
290 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
295 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
296 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
301 if (!strncmp(p, "csn=", strlen("csn=")))
304 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
306 *seq_num = generalized_to_unix_time(p);
308 /* very basic sanity check */
310 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
315 ntstatus = NT_STATUS_OK;
319 ldap_value_free(values);
323 talloc_destroy(mem_ctx);
328 /*******************************************************************
329 Run the search by name.
330 ******************************************************************/
332 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
334 LDAPMessage ** result,
338 char *escape_user = escape_ldap_string_alloc(user);
342 return LDAP_NO_MEMORY;
346 * in the filter expression, replace %u with the real name
347 * so in ldap filter, %u MUST exist :-)
349 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
350 get_objclass_filter(ldap_state->schema_ver));
352 return LDAP_NO_MEMORY;
355 * have to use this here because $ is filtered out
359 filter = talloc_all_string_sub(talloc_tos(),
360 filter, "%u", escape_user);
362 return LDAP_NO_MEMORY;
364 SAFE_FREE(escape_user);
366 ret = smbldap_search_suffix(ldap_state->smbldap_state,
367 filter, attr, result);
372 /*******************************************************************
373 Run the search by rid.
374 ******************************************************************/
376 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
377 uint32 rid, LDAPMessage ** result,
383 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
384 get_objclass_filter(ldap_state->schema_ver));
386 return LDAP_NO_MEMORY;
389 rc = smbldap_search_suffix(ldap_state->smbldap_state,
390 filter, attr, result);
395 /*******************************************************************
396 Run the search by SID.
397 ******************************************************************/
399 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
400 const DOM_SID *sid, LDAPMessage ** result,
407 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
408 get_userattr_key2string(ldap_state->schema_ver,
410 sid_to_string(sid_string, sid),
411 get_objclass_filter(ldap_state->schema_ver));
413 return LDAP_NO_MEMORY;
416 rc = smbldap_search_suffix(ldap_state->smbldap_state,
417 filter, attr, result);
423 /*******************************************************************
424 Delete complete object or objectclass and attrs from
425 object found in search_result depending on lp_ldap_delete_dn
426 ******************************************************************/
428 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
431 const char *objectclass,
434 LDAPMod **mods = NULL;
437 BerElement *ptr = NULL;
439 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
441 return LDAP_NO_MEMORY;
444 if (lp_ldap_delete_dn()) {
445 return smbldap_delete(priv->smbldap_state, dn);
448 /* Ok, delete only the SAM attributes */
450 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
452 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
455 /* We are only allowed to delete the attributes that
458 for (attrib = attrs; *attrib != NULL; attrib++) {
459 if (strequal(*attrib, name)) {
460 DEBUG(10, ("ldapsam_delete_entry: deleting "
461 "attribute %s\n", name));
462 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
473 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
474 talloc_autofree_ldapmod(mem_ctx, mods);
476 return smbldap_modify(priv->smbldap_state, dn, mods);
479 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
484 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
485 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
491 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
492 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
502 /**********************************************************************
503 Initialize struct samu from an LDAP query.
504 (Based on init_sam_from_buffer in pdb_tdb.c)
505 *********************************************************************/
507 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
508 struct samu * sampass,
515 pass_can_change_time,
516 pass_must_change_time,
519 char *username = NULL,
525 *logon_script = NULL,
526 *profile_path = NULL,
528 *workstations = NULL,
531 uint8 smblmpwd[LM_HASH_LEN],
532 smbntpwd[NT_HASH_LEN];
533 bool use_samba_attrs = True;
534 uint32 acct_ctrl = 0;
536 uint16 bad_password_count = 0,
539 uint8 hours[MAX_HOURS_LEN];
541 LOGIN_CACHE *cache_entry = NULL;
543 bool expand_explicit = lp_passdb_expand_explicit();
545 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
550 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
551 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
555 if (priv2ld(ldap_state) == NULL) {
556 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
557 "ldap_struct is NULL!\n"));
561 if (!(username = smbldap_talloc_single_attribute(priv2ld(ldap_state),
565 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
570 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
572 nt_username = talloc_strdup(ctx, username);
577 domain = talloc_strdup(ctx, ldap_state->domain_name);
582 pdb_set_username(sampass, username, PDB_SET);
584 pdb_set_domain(sampass, domain, PDB_DEFAULT);
585 pdb_set_nt_username(sampass, nt_username, PDB_SET);
587 /* deal with different attributes between the schema first */
589 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
590 if ((temp = smbldap_talloc_single_attribute(
591 ldap_state->smbldap_state->ldap_struct,
593 get_userattr_key2string(ldap_state->schema_ver,
596 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
599 if ((temp = smbldap_talloc_single_attribute(
600 ldap_state->smbldap_state->ldap_struct,
602 get_userattr_key2string(ldap_state->schema_ver,
605 user_rid = (uint32)atol(temp);
606 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
610 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
611 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
612 get_userattr_key2string(ldap_state->schema_ver,
614 get_userattr_key2string(ldap_state->schema_ver,
620 temp = smbldap_talloc_single_attribute(
621 ldap_state->smbldap_state->ldap_struct,
623 get_userattr_key2string(ldap_state->schema_ver,
624 LDAP_ATTR_PWD_LAST_SET),
627 pass_last_set_time = (time_t) atol(temp);
628 pdb_set_pass_last_set_time(sampass,
629 pass_last_set_time, PDB_SET);
632 temp = smbldap_talloc_single_attribute(
633 ldap_state->smbldap_state->ldap_struct,
635 get_userattr_key2string(ldap_state->schema_ver,
636 LDAP_ATTR_LOGON_TIME),
639 logon_time = (time_t) atol(temp);
640 pdb_set_logon_time(sampass, logon_time, PDB_SET);
643 temp = smbldap_talloc_single_attribute(
644 ldap_state->smbldap_state->ldap_struct,
646 get_userattr_key2string(ldap_state->schema_ver,
647 LDAP_ATTR_LOGOFF_TIME),
650 logoff_time = (time_t) atol(temp);
651 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
654 temp = smbldap_talloc_single_attribute(
655 ldap_state->smbldap_state->ldap_struct,
657 get_userattr_key2string(ldap_state->schema_ver,
658 LDAP_ATTR_KICKOFF_TIME),
661 kickoff_time = (time_t) atol(temp);
662 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
665 temp = smbldap_talloc_single_attribute(
666 ldap_state->smbldap_state->ldap_struct,
668 get_userattr_key2string(ldap_state->schema_ver,
669 LDAP_ATTR_PWD_CAN_CHANGE),
672 pass_can_change_time = (time_t) atol(temp);
673 pdb_set_pass_can_change_time(sampass,
674 pass_can_change_time, PDB_SET);
677 temp = smbldap_talloc_single_attribute(
678 ldap_state->smbldap_state->ldap_struct,
680 get_userattr_key2string(ldap_state->schema_ver,
681 LDAP_ATTR_PWD_MUST_CHANGE),
684 pass_must_change_time = (time_t) atol(temp);
685 pdb_set_pass_must_change_time(sampass,
686 pass_must_change_time, PDB_SET);
689 /* recommend that 'gecos' and 'displayName' should refer to the same
690 * attribute OID. userFullName depreciated, only used by Samba
691 * primary rules of LDAP: don't make a new attribute when one is already defined
692 * that fits your needs; using cn then displayName rather than 'userFullName'
695 fullname = smbldap_talloc_single_attribute(
696 ldap_state->smbldap_state->ldap_struct,
698 get_userattr_key2string(ldap_state->schema_ver,
699 LDAP_ATTR_DISPLAY_NAME),
702 pdb_set_fullname(sampass, fullname, PDB_SET);
704 fullname = smbldap_talloc_single_attribute(
705 ldap_state->smbldap_state->ldap_struct,
707 get_userattr_key2string(ldap_state->schema_ver,
711 pdb_set_fullname(sampass, fullname, PDB_SET);
715 dir_drive = smbldap_talloc_single_attribute(
716 ldap_state->smbldap_state->ldap_struct,
718 get_userattr_key2string(ldap_state->schema_ver,
719 LDAP_ATTR_HOME_DRIVE),
722 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
724 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
727 homedir = smbldap_talloc_single_attribute(
728 ldap_state->smbldap_state->ldap_struct,
730 get_userattr_key2string(ldap_state->schema_ver,
731 LDAP_ATTR_HOME_PATH),
734 if (expand_explicit) {
735 homedir = talloc_sub_basic(ctx,
743 pdb_set_homedir(sampass, homedir, PDB_SET);
745 pdb_set_homedir(sampass,
746 talloc_sub_basic(ctx, username, domain,
751 logon_script = smbldap_talloc_single_attribute(
752 ldap_state->smbldap_state->ldap_struct,
754 get_userattr_key2string(ldap_state->schema_ver,
755 LDAP_ATTR_LOGON_SCRIPT),
758 if (expand_explicit) {
759 logon_script = talloc_sub_basic(ctx,
767 pdb_set_logon_script(sampass, logon_script, PDB_SET);
769 pdb_set_logon_script(sampass,
770 talloc_sub_basic(ctx, username, domain,
775 profile_path = smbldap_talloc_single_attribute(
776 ldap_state->smbldap_state->ldap_struct,
778 get_userattr_key2string(ldap_state->schema_ver,
779 LDAP_ATTR_PROFILE_PATH),
782 if (expand_explicit) {
783 profile_path = talloc_sub_basic(ctx,
791 pdb_set_profile_path(sampass, profile_path, PDB_SET);
793 pdb_set_profile_path(sampass,
794 talloc_sub_basic(ctx, username, domain,
799 acct_desc = smbldap_talloc_single_attribute(
800 ldap_state->smbldap_state->ldap_struct,
802 get_userattr_key2string(ldap_state->schema_ver,
806 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
809 workstations = smbldap_talloc_single_attribute(
810 ldap_state->smbldap_state->ldap_struct,
812 get_userattr_key2string(ldap_state->schema_ver,
816 pdb_set_workstations(sampass, workstations, PDB_SET);
819 munged_dial = smbldap_talloc_single_attribute(
820 ldap_state->smbldap_state->ldap_struct,
822 get_userattr_key2string(ldap_state->schema_ver,
823 LDAP_ATTR_MUNGED_DIAL),
826 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
829 /* FIXME: hours stuff should be cleaner */
833 memset(hours, 0xff, hours_len);
835 if (ldap_state->is_nds_ldap) {
838 char clear_text_pw[512];
840 /* Make call to Novell eDirectory ldap extension to get clear text password.
841 NOTE: This will only work if we have an SSL connection to eDirectory. */
842 user_dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
843 if (user_dn != NULL) {
844 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username, user_dn));
846 pwd_len = sizeof(clear_text_pw);
847 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
848 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
849 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
853 ZERO_STRUCT(smblmpwd);
854 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
858 ZERO_STRUCT(smbntpwd);
859 use_samba_attrs = False;
865 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
869 if (use_samba_attrs) {
870 temp = smbldap_talloc_single_attribute(
871 ldap_state->smbldap_state->ldap_struct,
873 get_userattr_key2string(ldap_state->schema_ver,
877 pdb_gethexpwd(temp, smblmpwd);
878 memset((char *)temp, '\0', strlen(temp)+1);
879 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
882 ZERO_STRUCT(smblmpwd);
885 temp = smbldap_talloc_single_attribute(
886 ldap_state->smbldap_state->ldap_struct,
888 get_userattr_key2string(ldap_state->schema_ver,
892 pdb_gethexpwd(temp, smbntpwd);
893 memset((char *)temp, '\0', strlen(temp)+1);
894 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
897 ZERO_STRUCT(smbntpwd);
903 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
905 uint8 *pwhist = NULL;
907 char *history_string = TALLOC_ARRAY(ctx, char,
908 MAX_PW_HISTORY_LEN*64);
910 if (!history_string) {
914 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
916 if ((pwhist = TALLOC_ARRAY(ctx, uint8,
917 pwHistLen * PW_HISTORY_ENTRY_LEN)) ==
919 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
922 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
924 if (smbldap_get_single_attribute(
925 ldap_state->smbldap_state->ldap_struct,
927 get_userattr_key2string(ldap_state->schema_ver,
928 LDAP_ATTR_PWD_HISTORY),
930 MAX_PW_HISTORY_LEN*64)) {
931 bool hex_failed = false;
932 for (i = 0; i < pwHistLen; i++){
933 /* Get the 16 byte salt. */
934 if (!pdb_gethexpwd(&history_string[i*64],
935 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
939 /* Get the 16 byte MD5 hash of salt+passwd. */
940 if (!pdb_gethexpwd(&history_string[(i*64)+32],
941 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
942 PW_HISTORY_SALT_LEN])) {
948 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
950 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
953 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
958 temp = smbldap_talloc_single_attribute(
959 ldap_state->smbldap_state->ldap_struct,
961 get_userattr_key2string(ldap_state->schema_ver,
965 acct_ctrl = pdb_decode_acct_ctrl(temp);
967 if (acct_ctrl == 0) {
968 acct_ctrl |= ACB_NORMAL;
971 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
973 acct_ctrl |= ACB_NORMAL;
976 pdb_set_hours_len(sampass, hours_len, PDB_SET);
977 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
979 temp = smbldap_talloc_single_attribute(
980 ldap_state->smbldap_state->ldap_struct,
982 get_userattr_key2string(ldap_state->schema_ver,
983 LDAP_ATTR_BAD_PASSWORD_COUNT),
986 bad_password_count = (uint32) atol(temp);
987 pdb_set_bad_password_count(sampass,
988 bad_password_count, PDB_SET);
991 temp = smbldap_talloc_single_attribute(
992 ldap_state->smbldap_state->ldap_struct,
994 get_userattr_key2string(ldap_state->schema_ver,
995 LDAP_ATTR_BAD_PASSWORD_TIME),
998 bad_password_time = (time_t) atol(temp);
999 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
1003 temp = smbldap_talloc_single_attribute(
1004 ldap_state->smbldap_state->ldap_struct,
1006 get_userattr_key2string(ldap_state->schema_ver,
1007 LDAP_ATTR_LOGON_COUNT),
1010 logon_count = (uint32) atol(temp);
1011 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1014 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1016 temp = smbldap_talloc_single_attribute(
1017 ldap_state->smbldap_state->ldap_struct,
1019 get_userattr_key2string(ldap_state->schema_ver,
1020 LDAP_ATTR_LOGON_HOURS),
1023 pdb_gethexhours(temp, hours);
1024 memset((char *)temp, '\0', strlen(temp) +1);
1025 pdb_set_hours(sampass, hours, PDB_SET);
1029 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1030 temp = smbldap_talloc_single_attribute(
1031 priv2ld(ldap_state),
1036 /* We've got a uid, feed the cache */
1037 uid_t uid = strtoul(temp, NULL, 10);
1038 store_uid_sid_cache(pdb_get_user_sid(sampass), uid);
1042 /* check the timestamp of the cache vs ldap entry */
1043 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1049 /* see if we have newer updates */
1050 if (!(cache_entry = login_cache_read(sampass))) {
1051 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1052 (unsigned int)pdb_get_bad_password_count(sampass),
1053 (unsigned int)pdb_get_bad_password_time(sampass)));
1058 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1059 (unsigned int)ldap_entry_time,
1060 (unsigned int)cache_entry->entry_timestamp,
1061 (unsigned int)cache_entry->bad_password_time));
1063 if (ldap_entry_time > cache_entry->entry_timestamp) {
1064 /* cache is older than directory , so
1065 we need to delete the entry but allow the
1066 fields to be written out */
1067 login_cache_delentry(sampass);
1070 pdb_set_acct_ctrl(sampass,
1071 pdb_get_acct_ctrl(sampass) |
1072 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
1074 pdb_set_bad_password_count(sampass,
1075 cache_entry->bad_password_count,
1077 pdb_set_bad_password_time(sampass,
1078 cache_entry->bad_password_time,
1087 SAFE_FREE(cache_entry);
1091 /**********************************************************************
1092 Initialize the ldap db from a struct samu. Called on update.
1093 (Based on init_buffer_from_sam in pdb_tdb.c)
1094 *********************************************************************/
1096 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1097 LDAPMessage *existing,
1098 LDAPMod *** mods, struct samu * sampass,
1099 bool (*need_update)(const struct samu *,
1105 if (mods == NULL || sampass == NULL) {
1106 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1113 * took out adding "objectclass: sambaAccount"
1114 * do this on a per-mod basis
1116 if (need_update(sampass, PDB_USERNAME)) {
1117 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1118 "uid", pdb_get_username(sampass));
1119 if (ldap_state->is_nds_ldap) {
1120 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1121 "cn", pdb_get_username(sampass));
1122 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1123 "sn", pdb_get_username(sampass));
1127 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1129 /* only update the RID if we actually need to */
1130 if (need_update(sampass, PDB_USERSID)) {
1132 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
1134 switch ( ldap_state->schema_ver ) {
1135 case SCHEMAVER_SAMBAACCOUNT:
1136 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1137 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1138 sid_string_dbg(user_sid),
1140 &ldap_state->domain_sid)));
1143 if (asprintf(&temp, "%i", rid) < 0) {
1146 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1147 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1152 case SCHEMAVER_SAMBASAMACCOUNT:
1153 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1154 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1155 sid_to_string(sid_string, user_sid));
1159 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1164 /* we don't need to store the primary group RID - so leaving it
1165 'free' to hang off the unix primary group makes life easier */
1167 if (need_update(sampass, PDB_GROUPSID)) {
1169 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1171 switch ( ldap_state->schema_ver ) {
1172 case SCHEMAVER_SAMBAACCOUNT:
1173 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1174 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1175 sid_string_dbg(group_sid),
1177 &ldap_state->domain_sid)));
1181 if (asprintf(&temp, "%i", rid) < 0) {
1184 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1185 get_userattr_key2string(ldap_state->schema_ver,
1186 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1190 case SCHEMAVER_SAMBASAMACCOUNT:
1191 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1192 get_userattr_key2string(ldap_state->schema_ver,
1193 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_string(sid_string, group_sid));
1197 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1203 /* displayName, cn, and gecos should all be the same
1204 * most easily accomplished by giving them the same OID
1205 * gecos isn't set here b/c it should be handled by the
1207 * We change displayName only and fall back to cn if
1208 * it does not exist.
1211 if (need_update(sampass, PDB_FULLNAME))
1212 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1213 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1214 pdb_get_fullname(sampass));
1216 if (need_update(sampass, PDB_ACCTDESC))
1217 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1218 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1219 pdb_get_acct_desc(sampass));
1221 if (need_update(sampass, PDB_WORKSTATIONS))
1222 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1223 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1224 pdb_get_workstations(sampass));
1226 if (need_update(sampass, PDB_MUNGEDDIAL))
1227 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1228 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1229 pdb_get_munged_dial(sampass));
1231 if (need_update(sampass, PDB_SMBHOME))
1232 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1233 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1234 pdb_get_homedir(sampass));
1236 if (need_update(sampass, PDB_DRIVE))
1237 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1238 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1239 pdb_get_dir_drive(sampass));
1241 if (need_update(sampass, PDB_LOGONSCRIPT))
1242 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1243 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1244 pdb_get_logon_script(sampass));
1246 if (need_update(sampass, PDB_PROFILE))
1247 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1248 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1249 pdb_get_profile_path(sampass));
1251 if (asprintf(&temp, "%li", pdb_get_logon_time(sampass)) < 0) {
1254 if (need_update(sampass, PDB_LOGONTIME))
1255 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1256 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1259 if (asprintf(&temp, "%li", pdb_get_logoff_time(sampass)) < 0) {
1262 if (need_update(sampass, PDB_LOGOFFTIME))
1263 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1264 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1267 if (asprintf(&temp, "%li", pdb_get_kickoff_time(sampass)) < 0) {
1270 if (need_update(sampass, PDB_KICKOFFTIME))
1271 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1272 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1275 if (asprintf(&temp, "%li", pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1278 if (need_update(sampass, PDB_CANCHANGETIME))
1279 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1280 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1283 if (asprintf(&temp, "%li", pdb_get_pass_must_change_time(sampass)) < 0) {
1286 if (need_update(sampass, PDB_MUSTCHANGETIME))
1287 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1288 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1291 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1292 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1294 if (need_update(sampass, PDB_LMPASSWD)) {
1295 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1298 pdb_sethexpwd(pwstr, lm_pw,
1299 pdb_get_acct_ctrl(sampass));
1300 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1301 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1304 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1305 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1309 if (need_update(sampass, PDB_NTPASSWD)) {
1310 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1313 pdb_sethexpwd(pwstr, nt_pw,
1314 pdb_get_acct_ctrl(sampass));
1315 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1316 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1319 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1320 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1325 if (need_update(sampass, PDB_PWHISTORY)) {
1327 uint32 pwHistLen = 0;
1328 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1330 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1334 if (pwHistLen == 0) {
1335 /* Remove any password history from the LDAP store. */
1336 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1340 uint32 currHistLen = 0;
1341 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1342 if (pwhist != NULL) {
1343 /* We can only store (1024-1/64 password history entries. */
1344 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1345 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1346 /* Store the salt. */
1347 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1348 /* Followed by the md5 hash of salt + md4 hash */
1349 pdb_sethexpwd(&pwstr[(i*64)+32],
1350 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1351 DEBUG(100, ("pwstr=%s\n", pwstr));
1355 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1356 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1361 if (need_update(sampass, PDB_PASSLASTSET)) {
1362 if (asprintf(&temp, "%li",
1363 pdb_get_pass_last_set_time(sampass)) < 0) {
1366 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1367 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1373 if (need_update(sampass, PDB_HOURS)) {
1374 const uint8 *hours = pdb_get_hours(sampass);
1377 pdb_sethexhours(hourstr, hours);
1378 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1381 get_userattr_key2string(ldap_state->schema_ver,
1382 LDAP_ATTR_LOGON_HOURS),
1387 if (need_update(sampass, PDB_ACCTCTRL))
1388 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1389 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1390 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1392 /* password lockout cache:
1393 - If we are now autolocking or clearing, we write to ldap
1394 - If we are clearing, we delete the cache entry
1395 - If the count is > 0, we update the cache
1397 This even means when autolocking, we cache, just in case the
1398 update doesn't work, and we have to cache the autolock flag */
1400 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1401 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1402 uint16 badcount = pdb_get_bad_password_count(sampass);
1403 time_t badtime = pdb_get_bad_password_time(sampass);
1405 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1407 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1408 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1410 if ((badcount >= pol) || (badcount == 0)) {
1411 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1412 (unsigned int)badcount, (unsigned int)badtime));
1413 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1417 ldap_state->smbldap_state->ldap_struct,
1419 get_userattr_key2string(
1420 ldap_state->schema_ver,
1421 LDAP_ATTR_BAD_PASSWORD_COUNT),
1425 if (asprintf(&temp, "%li", badtime) < 0) {
1429 ldap_state->smbldap_state->ldap_struct,
1431 get_userattr_key2string(
1432 ldap_state->schema_ver,
1433 LDAP_ATTR_BAD_PASSWORD_TIME),
1437 if (badcount == 0) {
1438 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1439 login_cache_delentry(sampass);
1441 LOGIN_CACHE cache_entry;
1443 cache_entry.entry_timestamp = time(NULL);
1444 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1445 cache_entry.bad_password_count = badcount;
1446 cache_entry.bad_password_time = badtime;
1448 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1449 login_cache_write(sampass, cache_entry);
1456 /**********************************************************************
1457 Connect to LDAP server for password enumeration.
1458 *********************************************************************/
1460 static NTSTATUS ldapsam_setsampwent(struct pdb_methods *my_methods, bool update, uint32 acb_mask)
1462 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1464 char *filter = NULL;
1466 const char **attr_list;
1467 bool machine_mask = False, user_mask = False;
1468 NTSTATUS status = NT_STATUS_OK;
1469 TALLOC_CTX *ctx = talloc_init("ldapsam_setsampwent");
1472 return NT_STATUS_NO_MEMORY;
1474 filter = talloc_asprintf(ctx, "(&%s%s)", "(uid=%u)",
1475 get_objclass_filter(ldap_state->schema_ver));
1477 status = NT_STATUS_NO_MEMORY;
1481 filter = talloc_all_string_sub(ctx, filter, "%u", "*");
1483 status = NT_STATUS_NO_MEMORY;
1487 machine_mask = ((acb_mask != 0) && (acb_mask & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)));
1488 user_mask = ((acb_mask != 0) && (acb_mask & ACB_NORMAL));
1491 suffix = lp_ldap_machine_suffix();
1492 } else if (user_mask) {
1493 suffix = lp_ldap_user_suffix();
1495 suffix = lp_ldap_suffix();
1498 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1501 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1502 rc = smbldap_search(ldap_state->smbldap_state, suffix, LDAP_SCOPE_SUBTREE, filter,
1503 attr_list, 0, &ldap_state->result);
1504 TALLOC_FREE( attr_list );
1506 if (rc != LDAP_SUCCESS) {
1507 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc)));
1508 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix, filter));
1509 ldap_msgfree(ldap_state->result);
1510 ldap_state->result = NULL;
1511 status = NT_STATUS_UNSUCCESSFUL;
1515 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1516 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
1517 ldap_state->result), suffix));
1519 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
1520 ldap_state->result);
1521 ldap_state->index = 0;
1529 /**********************************************************************
1530 End enumeration of the LDAP password list.
1531 *********************************************************************/
1533 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1535 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1536 if (ldap_state->result) {
1537 ldap_msgfree(ldap_state->result);
1538 ldap_state->result = NULL;
1542 /**********************************************************************
1543 Get the next entry in the LDAP password database.
1544 *********************************************************************/
1546 static NTSTATUS ldapsam_getsampwent(struct pdb_methods *my_methods,
1549 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1550 struct ldapsam_privates *ldap_state =
1551 (struct ldapsam_privates *)my_methods->private_data;
1555 if (!ldap_state->entry)
1558 ldap_state->index++;
1559 bret = init_sam_from_ldap(ldap_state, user, ldap_state->entry);
1561 ldap_state->entry = ldap_next_entry(priv2ld(ldap_state),
1565 return NT_STATUS_OK;
1568 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1569 const char *new_attr)
1573 if (new_attr == NULL) {
1577 for (i=0; (*attr_list)[i] != NULL; i++) {
1581 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1583 SMB_ASSERT((*attr_list) != NULL);
1584 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1585 (*attr_list)[i+1] = NULL;
1588 /**********************************************************************
1589 Get struct samu entry from LDAP by username.
1590 *********************************************************************/
1592 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1594 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1595 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1596 LDAPMessage *result = NULL;
1597 LDAPMessage *entry = NULL;
1599 const char ** attr_list;
1602 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1603 append_attr(user, &attr_list,
1604 get_userattr_key2string(ldap_state->schema_ver,
1605 LDAP_ATTR_MOD_TIMESTAMP));
1606 append_attr(user, &attr_list, "uidNumber");
1607 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1609 TALLOC_FREE( attr_list );
1611 if ( rc != LDAP_SUCCESS )
1612 return NT_STATUS_NO_SUCH_USER;
1614 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1617 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1618 ldap_msgfree(result);
1619 return NT_STATUS_NO_SUCH_USER;
1620 } else if (count > 1) {
1621 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1622 ldap_msgfree(result);
1623 return NT_STATUS_NO_SUCH_USER;
1626 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1628 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1629 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1630 ldap_msgfree(result);
1631 return NT_STATUS_NO_SUCH_USER;
1633 pdb_set_backend_private_data(user, result, NULL,
1634 my_methods, PDB_CHANGED);
1635 talloc_autofree_ldapmsg(user, result);
1638 ldap_msgfree(result);
1643 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1644 const DOM_SID *sid, LDAPMessage **result)
1647 const char ** attr_list;
1650 switch ( ldap_state->schema_ver ) {
1651 case SCHEMAVER_SAMBASAMACCOUNT: {
1652 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1653 if (tmp_ctx == NULL) {
1654 return LDAP_NO_MEMORY;
1657 attr_list = get_userattr_list(tmp_ctx,
1658 ldap_state->schema_ver);
1659 append_attr(tmp_ctx, &attr_list,
1660 get_userattr_key2string(
1661 ldap_state->schema_ver,
1662 LDAP_ATTR_MOD_TIMESTAMP));
1663 append_attr(tmp_ctx, &attr_list, "uidNumber");
1664 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1666 TALLOC_FREE(tmp_ctx);
1668 if ( rc != LDAP_SUCCESS )
1673 case SCHEMAVER_SAMBAACCOUNT:
1674 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1678 attr_list = get_userattr_list(NULL,
1679 ldap_state->schema_ver);
1680 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1681 TALLOC_FREE( attr_list );
1683 if ( rc != LDAP_SUCCESS )
1690 /**********************************************************************
1691 Get struct samu entry from LDAP by SID.
1692 *********************************************************************/
1694 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1696 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1697 LDAPMessage *result = NULL;
1698 LDAPMessage *entry = NULL;
1702 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1704 if (rc != LDAP_SUCCESS)
1705 return NT_STATUS_NO_SUCH_USER;
1707 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1710 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1711 "count=%d\n", sid_string_dbg(sid), count));
1712 ldap_msgfree(result);
1713 return NT_STATUS_NO_SUCH_USER;
1714 } else if (count > 1) {
1715 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1716 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1718 ldap_msgfree(result);
1719 return NT_STATUS_NO_SUCH_USER;
1722 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1724 ldap_msgfree(result);
1725 return NT_STATUS_NO_SUCH_USER;
1728 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1729 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1730 ldap_msgfree(result);
1731 return NT_STATUS_NO_SUCH_USER;
1734 pdb_set_backend_private_data(user, result, NULL,
1735 my_methods, PDB_CHANGED);
1736 talloc_autofree_ldapmsg(user, result);
1737 return NT_STATUS_OK;
1740 /********************************************************************
1741 Do the actual modification - also change a plaintext passord if
1743 **********************************************************************/
1745 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1746 struct samu *newpwd, char *dn,
1747 LDAPMod **mods, int ldap_op,
1748 bool (*need_update)(const struct samu *, enum pdb_elements))
1750 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1753 if (!newpwd || !dn) {
1754 return NT_STATUS_INVALID_PARAMETER;
1758 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1759 /* may be password change below however */
1763 if (ldap_state->is_nds_ldap) {
1764 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1768 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1772 rc = smbldap_add(ldap_state->smbldap_state,
1775 case LDAP_MOD_REPLACE:
1776 rc = smbldap_modify(ldap_state->smbldap_state,
1780 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1782 return NT_STATUS_INVALID_PARAMETER;
1785 if (rc!=LDAP_SUCCESS) {
1786 return NT_STATUS_UNSUCCESSFUL;
1790 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1791 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1792 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1793 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1796 char *retoid = NULL;
1797 struct berval *retdata = NULL;
1798 char *utf8_password;
1801 if (!ldap_state->is_nds_ldap) {
1803 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1804 LDAP_EXOP_MODIFY_PASSWD)) {
1805 DEBUG(2, ("ldap password change requested, but LDAP "
1806 "server does not support it -- ignoring\n"));
1807 return NT_STATUS_OK;
1811 if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
1812 return NT_STATUS_NO_MEMORY;
1815 if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
1816 SAFE_FREE(utf8_password);
1817 return NT_STATUS_NO_MEMORY;
1820 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1821 DEBUG(0,("ber_alloc_t returns NULL\n"));
1822 SAFE_FREE(utf8_password);
1824 return NT_STATUS_UNSUCCESSFUL;
1827 ber_printf (ber, "{");
1828 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
1829 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
1830 ber_printf (ber, "n}");
1832 if ((rc = ber_flatten (ber, &bv))<0) {
1833 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1836 SAFE_FREE(utf8_password);
1837 return NT_STATUS_UNSUCCESSFUL;
1841 SAFE_FREE(utf8_password);
1844 if (!ldap_state->is_nds_ldap) {
1845 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1846 LDAP_EXOP_MODIFY_PASSWD,
1847 bv, NULL, NULL, &retoid,
1850 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1851 pdb_get_plaintext_passwd(newpwd));
1853 if (rc != LDAP_SUCCESS) {
1854 char *ld_error = NULL;
1856 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1857 DEBUG(3, ("Could not set userPassword "
1858 "attribute due to an objectClass "
1859 "violation -- ignoring\n"));
1861 return NT_STATUS_OK;
1864 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1866 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1867 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1868 SAFE_FREE(ld_error);
1870 return NT_STATUS_UNSUCCESSFUL;
1872 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1873 #ifdef DEBUG_PASSWORD
1874 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1877 ber_bvfree(retdata);
1879 ldap_memfree(retoid);
1883 return NT_STATUS_OK;
1886 /**********************************************************************
1887 Delete entry from LDAP for username.
1888 *********************************************************************/
1890 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1891 struct samu * sam_acct)
1893 struct ldapsam_privates *priv =
1894 (struct ldapsam_privates *)my_methods->private_data;
1897 LDAPMessage *msg, *entry;
1898 NTSTATUS result = NT_STATUS_NO_MEMORY;
1899 const char **attr_list;
1900 TALLOC_CTX *mem_ctx;
1903 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1904 return NT_STATUS_INVALID_PARAMETER;
1907 sname = pdb_get_username(sam_acct);
1909 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1912 mem_ctx = talloc_new(NULL);
1913 if (mem_ctx == NULL) {
1914 DEBUG(0, ("talloc_new failed\n"));
1918 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1919 if (attr_list == NULL) {
1923 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1925 if ((rc != LDAP_SUCCESS) ||
1926 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1927 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1928 DEBUG(5, ("Could not find user %s\n", sname));
1929 result = NT_STATUS_NO_SUCH_USER;
1933 rc = ldapsam_delete_entry(
1934 priv, mem_ctx, entry,
1935 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1936 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1939 result = (rc == LDAP_SUCCESS) ?
1940 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1943 TALLOC_FREE(mem_ctx);
1947 /**********************************************************************
1948 Helper function to determine for update_sam_account whether
1949 we need LDAP modification.
1950 *********************************************************************/
1952 static bool element_is_changed(const struct samu *sampass,
1953 enum pdb_elements element)
1955 return IS_SAM_CHANGED(sampass, element);
1958 /**********************************************************************
1960 *********************************************************************/
1962 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1964 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1965 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1968 LDAPMessage *result = NULL;
1969 LDAPMessage *entry = NULL;
1970 LDAPMod **mods = NULL;
1971 const char **attr_list;
1973 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1975 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1976 if (pdb_get_username(newpwd) == NULL) {
1977 return NT_STATUS_INVALID_PARAMETER;
1979 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1980 TALLOC_FREE( attr_list );
1981 if (rc != LDAP_SUCCESS) {
1982 return NT_STATUS_UNSUCCESSFUL;
1984 pdb_set_backend_private_data(newpwd, result, NULL,
1985 my_methods, PDB_CHANGED);
1986 talloc_autofree_ldapmsg(newpwd, result);
1989 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1990 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1991 return NT_STATUS_UNSUCCESSFUL;
1994 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1995 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
1997 return NT_STATUS_UNSUCCESSFUL;
2000 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
2002 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2003 element_is_changed)) {
2004 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2007 ldap_mods_free(mods,True);
2008 return NT_STATUS_UNSUCCESSFUL;
2012 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2013 pdb_get_username(newpwd)));
2015 return NT_STATUS_OK;
2018 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
2019 ldap_mods_free(mods,True);
2023 * We need to set the backend private data to NULL here. For example
2024 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2025 * same one, and with the explicit delete / add logic for attribute
2026 * values the second time we would use the wrong "old" value which
2027 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2029 * The existing LDAPMessage is still being auto-freed by the
2032 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2035 if (!NT_STATUS_IS_OK(ret)) {
2039 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2040 pdb_get_username(newpwd)));
2041 return NT_STATUS_OK;
2044 /***************************************************************************
2045 Renames a struct samu
2046 - The "rename user script" has full responsibility for changing everything
2047 ***************************************************************************/
2049 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2050 struct samu *old_acct,
2051 const char *newname)
2053 const char *oldname;
2055 char *rename_script = NULL;
2056 fstring oldname_lower, newname_lower;
2059 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2060 return NT_STATUS_INVALID_PARAMETER;
2063 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2064 return NT_STATUS_INVALID_PARAMETER;
2067 oldname = pdb_get_username(old_acct);
2069 /* rename the posix user */
2070 rename_script = SMB_STRDUP(lp_renameuser_script());
2071 if (rename_script) {
2072 return NT_STATUS_NO_MEMORY;
2075 if (!(*rename_script)) {
2076 SAFE_FREE(rename_script);
2077 return NT_STATUS_ACCESS_DENIED;
2080 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2083 /* We have to allow the account name to end with a '$'.
2084 Also, follow the semantics in _samr_create_user() and lower case the
2085 posix name but preserve the case in passdb */
2087 fstrcpy( oldname_lower, oldname );
2088 strlower_m( oldname_lower );
2089 fstrcpy( newname_lower, newname );
2090 strlower_m( newname_lower );
2091 rename_script = realloc_string_sub2(rename_script,
2096 if (rename_script) {
2097 return NT_STATUS_NO_MEMORY;
2099 rename_script = realloc_string_sub2(rename_script,
2104 rc = smbrun(rename_script, NULL);
2106 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2107 rename_script, rc));
2109 SAFE_FREE(rename_script);
2112 smb_nscd_flush_user_cache();
2116 return NT_STATUS_UNSUCCESSFUL;
2118 return NT_STATUS_OK;
2121 /**********************************************************************
2122 Helper function to determine for update_sam_account whether
2123 we need LDAP modification.
2124 *********************************************************************/
2126 static bool element_is_set_or_changed(const struct samu *sampass,
2127 enum pdb_elements element)
2129 return (IS_SAM_SET(sampass, element) ||
2130 IS_SAM_CHANGED(sampass, element));
2133 /**********************************************************************
2134 Add struct samu to LDAP.
2135 *********************************************************************/
2137 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2139 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2140 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2142 LDAPMessage *result = NULL;
2143 LDAPMessage *entry = NULL;
2144 LDAPMod **mods = NULL;
2145 int ldap_op = LDAP_MOD_REPLACE;
2147 const char **attr_list;
2148 char *escape_user = NULL;
2149 const char *username = pdb_get_username(newpwd);
2150 const DOM_SID *sid = pdb_get_user_sid(newpwd);
2151 char *filter = NULL;
2153 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2154 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2157 return NT_STATUS_NO_MEMORY;
2160 if (!username || !*username) {
2161 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2162 status = NT_STATUS_INVALID_PARAMETER;
2166 /* free this list after the second search or in case we exit on failure */
2167 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2169 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2171 if (rc != LDAP_SUCCESS) {
2175 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2176 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2180 ldap_msgfree(result);
2183 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2184 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2186 if (rc == LDAP_SUCCESS) {
2187 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2188 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2189 "already in the base, with samba "
2190 "attributes\n", sid_string_dbg(sid)));
2193 ldap_msgfree(result);
2198 /* does the entry already exist but without a samba attributes?
2199 we need to return the samba attributes here */
2201 escape_user = escape_ldap_string_alloc( username );
2202 filter = talloc_strdup(attr_list, "(uid=%u)");
2204 status = NT_STATUS_NO_MEMORY;
2207 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2209 status = NT_STATUS_NO_MEMORY;
2212 SAFE_FREE(escape_user);
2214 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2215 filter, attr_list, &result);
2216 if ( rc != LDAP_SUCCESS ) {
2220 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2222 if (num_result > 1) {
2223 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2227 /* Check if we need to update an existing entry */
2228 if (num_result == 1) {
2231 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2232 ldap_op = LDAP_MOD_REPLACE;
2233 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2234 tmp = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
2238 dn = talloc_asprintf(ctx, "%s", tmp);
2241 status = NT_STATUS_NO_MEMORY;
2245 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2247 /* There might be a SID for this account already - say an idmap entry */
2249 filter = talloc_asprintf(ctx,
2250 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2251 get_userattr_key2string(ldap_state->schema_ver,
2252 LDAP_ATTR_USER_SID),
2253 sid_string_talloc(ctx, sid),
2254 LDAP_OBJ_IDMAP_ENTRY,
2255 LDAP_OBJ_SID_ENTRY);
2257 status = NT_STATUS_NO_MEMORY;
2261 /* free old result before doing a new search */
2262 if (result != NULL) {
2263 ldap_msgfree(result);
2266 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2267 filter, attr_list, &result);
2269 if ( rc != LDAP_SUCCESS ) {
2273 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2275 if (num_result > 1) {
2276 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2280 /* Check if we need to update an existing entry */
2281 if (num_result == 1) {
2284 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2285 ldap_op = LDAP_MOD_REPLACE;
2286 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2287 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
2291 dn = talloc_asprintf(ctx, "%s", tmp);
2294 status = NT_STATUS_NO_MEMORY;
2300 if (num_result == 0) {
2301 char *escape_username;
2302 /* Check if we need to add an entry */
2303 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2304 ldap_op = LDAP_MOD_ADD;
2306 escape_username = escape_rdn_val_string_alloc(username);
2307 if (!escape_username) {
2308 status = NT_STATUS_NO_MEMORY;
2312 if (username[strlen(username)-1] == '$') {
2313 dn = talloc_asprintf(ctx,
2316 lp_ldap_machine_suffix());
2318 dn = talloc_asprintf(ctx,
2321 lp_ldap_user_suffix());
2324 SAFE_FREE(escape_username);
2326 status = NT_STATUS_NO_MEMORY;
2331 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2332 element_is_set_or_changed)) {
2333 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2335 ldap_mods_free(mods, true);
2341 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2344 switch ( ldap_state->schema_ver ) {
2345 case SCHEMAVER_SAMBAACCOUNT:
2346 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2348 case SCHEMAVER_SAMBASAMACCOUNT:
2349 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2352 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2356 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2357 if (!NT_STATUS_IS_OK(ret)) {
2358 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2359 pdb_get_username(newpwd),dn));
2360 ldap_mods_free(mods, true);
2364 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2365 ldap_mods_free(mods, true);
2367 status = NT_STATUS_OK;
2372 SAFE_FREE(escape_user);
2374 ldap_msgfree(result);
2379 /**********************************************************************
2380 *********************************************************************/
2382 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2384 LDAPMessage ** result)
2386 int scope = LDAP_SCOPE_SUBTREE;
2388 const char **attr_list;
2390 attr_list = get_attr_list(NULL, groupmap_attr_list);
2391 rc = smbldap_search(ldap_state->smbldap_state,
2392 lp_ldap_group_suffix (), scope,
2393 filter, attr_list, 0, result);
2394 TALLOC_FREE(attr_list);
2399 /**********************************************************************
2400 *********************************************************************/
2402 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2403 GROUP_MAP *map, LDAPMessage *entry)
2406 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2408 if (ldap_state == NULL || map == NULL || entry == NULL ||
2409 ldap_state->smbldap_state->ldap_struct == NULL) {
2410 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2415 temp = smbldap_talloc_single_attribute(
2416 ldap_state->smbldap_state->ldap_struct,
2418 get_attr_key2string(groupmap_attr_list,
2419 LDAP_ATTR_GIDNUMBER),
2422 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2423 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2427 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2429 map->gid = (gid_t)atol(temp);
2432 temp = smbldap_talloc_single_attribute(
2433 ldap_state->smbldap_state->ldap_struct,
2435 get_attr_key2string(groupmap_attr_list,
2436 LDAP_ATTR_GROUP_SID),
2439 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2440 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2445 if (!string_to_sid(&map->sid, temp)) {
2446 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2452 temp = smbldap_talloc_single_attribute(
2453 ldap_state->smbldap_state->ldap_struct,
2455 get_attr_key2string(groupmap_attr_list,
2456 LDAP_ATTR_GROUP_TYPE),
2459 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2460 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2464 map->sid_name_use = (enum lsa_SidType)atol(temp);
2466 if ((map->sid_name_use < SID_NAME_USER) ||
2467 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2468 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2474 temp = smbldap_talloc_single_attribute(
2475 ldap_state->smbldap_state->ldap_struct,
2477 get_attr_key2string(groupmap_attr_list,
2478 LDAP_ATTR_DISPLAY_NAME),
2481 temp = smbldap_talloc_single_attribute(
2482 ldap_state->smbldap_state->ldap_struct,
2484 get_attr_key2string(groupmap_attr_list,
2488 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2489 for gidNumber(%lu)\n",(unsigned long)map->gid));
2494 fstrcpy(map->nt_name, temp);
2497 temp = smbldap_talloc_single_attribute(
2498 ldap_state->smbldap_state->ldap_struct,
2500 get_attr_key2string(groupmap_attr_list,
2504 temp = talloc_strdup(ctx, "");
2510 fstrcpy(map->comment, temp);
2512 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2513 store_gid_sid_cache(&map->sid, map->gid);
2520 /**********************************************************************
2521 *********************************************************************/
2523 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2527 struct ldapsam_privates *ldap_state =
2528 (struct ldapsam_privates *)methods->private_data;
2529 LDAPMessage *result = NULL;
2530 LDAPMessage *entry = NULL;
2533 if (ldapsam_search_one_group(ldap_state, filter, &result)
2535 return NT_STATUS_NO_SUCH_GROUP;
2538 count = ldap_count_entries(priv2ld(ldap_state), result);
2541 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2543 ldap_msgfree(result);
2544 return NT_STATUS_NO_SUCH_GROUP;
2548 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2549 "count=%d\n", filter, count));
2550 ldap_msgfree(result);
2551 return NT_STATUS_NO_SUCH_GROUP;
2554 entry = ldap_first_entry(priv2ld(ldap_state), result);
2557 ldap_msgfree(result);
2558 return NT_STATUS_UNSUCCESSFUL;
2561 if (!init_group_from_ldap(ldap_state, map, entry)) {
2562 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2563 "group filter %s\n", filter));
2564 ldap_msgfree(result);
2565 return NT_STATUS_NO_SUCH_GROUP;
2568 ldap_msgfree(result);
2569 return NT_STATUS_OK;
2572 /**********************************************************************
2573 *********************************************************************/
2575 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2578 char *filter = NULL;
2581 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2583 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2584 sid_string_static(&sid)) < 0) {
2585 return NT_STATUS_NO_MEMORY;
2588 status = ldapsam_getgroup(methods, filter, map);
2593 /**********************************************************************
2594 *********************************************************************/
2596 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2599 char *filter = NULL;
2602 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2604 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2605 (unsigned long)gid) < 0) {
2606 return NT_STATUS_NO_MEMORY;
2609 status = ldapsam_getgroup(methods, filter, map);
2614 /**********************************************************************
2615 *********************************************************************/
2617 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2620 char *filter = NULL;
2621 char *escape_name = escape_ldap_string_alloc(name);
2625 return NT_STATUS_NO_MEMORY;
2628 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2630 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2631 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2633 SAFE_FREE(escape_name);
2634 return NT_STATUS_NO_MEMORY;
2637 SAFE_FREE(escape_name);
2638 status = ldapsam_getgroup(methods, filter, map);
2643 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2645 const DOM_SID *domain_sid,
2651 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2652 str, sizeof(str)-1)) {
2653 DEBUG(10, ("Could not find sambaSID attribute\n"));
2657 if (!string_to_sid(&sid, str)) {
2658 DEBUG(10, ("Could not convert string %s to sid\n", str));
2662 if (sid_compare_domain(&sid, domain_sid) != 0) {
2663 DEBUG(10, ("SID %s is not in expected domain %s\n",
2664 str, sid_string_dbg(domain_sid)));
2668 if (!sid_peek_rid(&sid, rid)) {
2669 DEBUG(10, ("Could not peek into RID\n"));
2676 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2677 TALLOC_CTX *mem_ctx,
2678 const DOM_SID *group,
2679 uint32 **pp_member_rids,
2680 size_t *p_num_members)
2682 struct ldapsam_privates *ldap_state =
2683 (struct ldapsam_privates *)methods->private_data;
2684 struct smbldap_state *conn = ldap_state->smbldap_state;
2685 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2686 const char *sid_attrs[] = { "sambaSID", NULL };
2687 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2688 LDAPMessage *result = NULL;
2691 char **values = NULL;
2696 *pp_member_rids = NULL;
2699 filter = talloc_asprintf(mem_ctx,
2700 "(&(objectClass=%s)"
2703 LDAP_OBJ_POSIXGROUP,
2705 sid_string_talloc(mem_ctx, group));
2706 if (filter == NULL) {
2707 ret = NT_STATUS_NO_MEMORY;
2711 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2712 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2715 if (rc != LDAP_SUCCESS)
2718 talloc_autofree_ldapmsg(mem_ctx, result);
2720 count = ldap_count_entries(conn->ldap_struct, result);
2723 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2724 sid_string_dbg(group)));
2725 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2730 ret = NT_STATUS_NO_SUCH_GROUP;
2734 entry = ldap_first_entry(conn->ldap_struct, result);
2738 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2740 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2741 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2745 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2749 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2750 if (filter == NULL) {
2751 ret = NT_STATUS_NO_MEMORY;
2755 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2756 char *escape_memberuid;
2758 escape_memberuid = escape_ldap_string_alloc(*memberuid);
2759 if (escape_memberuid == NULL) {
2760 ret = NT_STATUS_NO_MEMORY;
2764 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2765 if (filter == NULL) {
2766 SAFE_FREE(escape_memberuid);
2767 ret = NT_STATUS_NO_MEMORY;
2771 SAFE_FREE(escape_memberuid);
2774 filter = talloc_asprintf_append_buffer(filter, "))");
2775 if (filter == NULL) {
2776 ret = NT_STATUS_NO_MEMORY;
2780 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2781 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2784 if (rc != LDAP_SUCCESS)
2787 count = ldap_count_entries(conn->ldap_struct, result);
2788 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2790 talloc_autofree_ldapmsg(mem_ctx, result);
2792 for (entry = ldap_first_entry(conn->ldap_struct, result);
2794 entry = ldap_next_entry(conn->ldap_struct, entry))
2800 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2804 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2805 "the sambaSID attribute\n"));
2806 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2810 if (!string_to_sid(&sid, sidstr))
2813 if (!sid_check_is_in_our_domain(&sid)) {
2814 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2815 "in our domain\n"));
2816 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2820 sid_peek_rid(&sid, &rid);
2822 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2824 ret = NT_STATUS_NO_MEMORY;
2830 filter = talloc_asprintf(mem_ctx,
2831 "(&(objectClass=%s)"
2833 LDAP_OBJ_SAMBASAMACCOUNT,
2836 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2837 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2840 if (rc != LDAP_SUCCESS)
2843 talloc_autofree_ldapmsg(mem_ctx, result);
2845 for (entry = ldap_first_entry(conn->ldap_struct, result);
2847 entry = ldap_next_entry(conn->ldap_struct, entry))
2851 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2853 get_global_sam_sid(),
2855 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2856 "the sambaSID attribute\n"));
2857 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2861 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2863 ret = NT_STATUS_NO_MEMORY;
2873 ldap_value_free(values);
2878 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2879 TALLOC_CTX *mem_ctx,
2883 size_t *p_num_groups)
2885 struct ldapsam_privates *ldap_state =
2886 (struct ldapsam_privates *)methods->private_data;
2887 struct smbldap_state *conn = ldap_state->smbldap_state;
2889 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2892 LDAPMessage *result = NULL;
2894 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2895 size_t num_sids, num_gids;
2897 gid_t primary_gid = -1;
2902 if (pdb_get_username(user) == NULL) {
2903 return NT_STATUS_INVALID_PARAMETER;
2906 escape_name = escape_ldap_string_alloc(pdb_get_username(user));
2907 if (escape_name == NULL)
2908 return NT_STATUS_NO_MEMORY;
2910 /* retrieve the users primary gid */
2911 filter = talloc_asprintf(mem_ctx,
2912 "(&(objectClass=%s)(uid=%s))",
2913 LDAP_OBJ_SAMBASAMACCOUNT,
2915 if (filter == NULL) {
2916 ret = NT_STATUS_NO_MEMORY;
2920 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2921 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2923 if (rc != LDAP_SUCCESS)
2926 talloc_autofree_ldapmsg(mem_ctx, result);
2928 count = ldap_count_entries(priv2ld(ldap_state), result);
2932 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2933 ret = NT_STATUS_NO_SUCH_USER;
2936 entry = ldap_first_entry(priv2ld(ldap_state), result);
2938 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2940 DEBUG (1, ("Unable to find the member's gid!\n"));
2941 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2944 primary_gid = strtoul(gidstr, NULL, 10);
2947 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2948 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2952 filter = talloc_asprintf(mem_ctx,
2953 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
2954 LDAP_OBJ_POSIXGROUP, escape_name, primary_gid);
2955 if (filter == NULL) {
2956 ret = NT_STATUS_NO_MEMORY;
2960 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2961 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2963 if (rc != LDAP_SUCCESS)
2966 talloc_autofree_ldapmsg(mem_ctx, result);
2974 /* We need to add the primary group as the first gid/sid */
2976 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2977 ret = NT_STATUS_NO_MEMORY;
2981 /* This sid will be replaced later */
2983 if (!add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids, &num_sids)) {
2984 ret = NT_STATUS_NO_MEMORY;
2988 for (entry = ldap_first_entry(conn->ldap_struct, result);
2990 entry = ldap_next_entry(conn->ldap_struct, entry))
2997 if (!smbldap_get_single_attribute(conn->ldap_struct,
2999 str, sizeof(str)-1))
3002 if (!string_to_sid(&sid, str))
3005 if (!smbldap_get_single_attribute(conn->ldap_struct,
3007 str, sizeof(str)-1))
3010 gid = strtoul(str, &end, 10);
3012 if (PTR_DIFF(end, str) != strlen(str))
3015 if (gid == primary_gid) {
3016 sid_copy(&(*pp_sids)[0], &sid);
3018 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3020 ret = NT_STATUS_NO_MEMORY;
3023 if (!add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3025 ret = NT_STATUS_NO_MEMORY;
3031 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3032 DEBUG(3, ("primary group of [%s] not found\n",
3033 pdb_get_username(user)));
3037 *p_num_groups = num_sids;
3043 SAFE_FREE(escape_name);
3047 /**********************************************************************
3048 * Augment a posixGroup object with a sambaGroupMapping domgroup
3049 *********************************************************************/
3051 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3052 struct ldapsam_privates *ldap_state,
3055 const char *filter, *dn;
3056 LDAPMessage *msg, *entry;
3060 filter = talloc_asprintf(mem_ctx,
3061 "(&(objectClass=posixGroup)(gidNumber=%u))",
3063 if (filter == NULL) {
3064 return NT_STATUS_NO_MEMORY;
3067 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3068 get_attr_list(mem_ctx, groupmap_attr_list),
3070 talloc_autofree_ldapmsg(mem_ctx, msg);
3072 if ((rc != LDAP_SUCCESS) ||
3073 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3074 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3075 return NT_STATUS_NO_SUCH_GROUP;
3078 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3080 return NT_STATUS_NO_MEMORY;
3084 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3085 "sambaGroupMapping");
3086 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3087 sid_string_static(&map->sid));
3088 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3089 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3090 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3092 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3094 talloc_autofree_ldapmod(mem_ctx, mods);
3096 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3097 if (rc != LDAP_SUCCESS) {
3098 return NT_STATUS_ACCESS_DENIED;
3101 return NT_STATUS_OK;
3104 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3107 struct ldapsam_privates *ldap_state =
3108 (struct ldapsam_privates *)methods->private_data;
3109 LDAPMessage *msg = NULL;
3110 LDAPMod **mods = NULL;
3111 const char *attrs[] = { NULL };
3115 TALLOC_CTX *mem_ctx;
3122 mem_ctx = talloc_new(NULL);
3123 if (mem_ctx == NULL) {
3124 DEBUG(0, ("talloc_new failed\n"));
3125 return NT_STATUS_NO_MEMORY;
3128 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3129 sid_string_talloc(mem_ctx, &map->sid));
3130 if (filter == NULL) {
3131 result = NT_STATUS_NO_MEMORY;
3135 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3136 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3137 talloc_autofree_ldapmsg(mem_ctx, msg);
3139 if ((rc == LDAP_SUCCESS) &&
3140 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3142 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3143 "group mapping entry\n", sid_string_dbg(&map->sid)));
3144 result = NT_STATUS_GROUP_EXISTS;
3148 switch (map->sid_name_use) {
3150 case SID_NAME_DOM_GRP:
3151 /* To map a domain group we need to have a posix group
3153 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3157 case SID_NAME_ALIAS:
3158 if (!sid_check_is_in_our_domain(&map->sid)
3159 && !sid_check_is_in_builtin(&map->sid) )
3161 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3162 sid_string_dbg(&map->sid)));
3163 result = NT_STATUS_INVALID_PARAMETER;
3169 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3170 sid_type_lookup(map->sid_name_use)));
3171 result = NT_STATUS_INVALID_PARAMETER;
3175 /* Domain groups have been mapped in a separate routine, we have to
3176 * create an alias now */
3178 if (map->gid == -1) {
3179 DEBUG(10, ("Refusing to map gid==-1\n"));
3180 result = NT_STATUS_INVALID_PARAMETER;
3184 if (pdb_gid_to_sid(map->gid, &sid)) {
3185 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
3186 "add\n", map->gid, sid_string_dbg(&sid)));
3187 result = NT_STATUS_GROUP_EXISTS;
3191 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3192 * the best we can get out of LDAP. */
3194 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3195 sid_string_talloc(mem_ctx, &map->sid),
3196 lp_ldap_group_suffix());
3198 result = NT_STATUS_NO_MEMORY;
3204 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3206 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3207 "sambaGroupMapping");
3209 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3210 sid_string_talloc(mem_ctx, &map->sid));
3211 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3212 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3213 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3215 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3217 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3218 talloc_asprintf(mem_ctx, "%u", map->gid));
3219 talloc_autofree_ldapmod(mem_ctx, mods);
3221 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3223 result = (rc == LDAP_SUCCESS) ?
3224 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3227 TALLOC_FREE(mem_ctx);
3231 /**********************************************************************
3232 * Update a group mapping entry. We're quite strict about what can be changed:
3233 * Only the description and displayname may be changed. It simply does not
3234 * make any sense to change the SID, gid or the type in a mapping.
3235 *********************************************************************/
3237 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3240 struct ldapsam_privates *ldap_state =
3241 (struct ldapsam_privates *)methods->private_data;
3243 const char *filter, *dn;
3244 LDAPMessage *msg = NULL;
3245 LDAPMessage *entry = NULL;
3246 LDAPMod **mods = NULL;
3247 TALLOC_CTX *mem_ctx;
3250 mem_ctx = talloc_new(NULL);
3251 if (mem_ctx == NULL) {
3252 DEBUG(0, ("talloc_new failed\n"));
3253 return NT_STATUS_NO_MEMORY;
3256 /* Make 100% sure that sid, gid and type are not changed by looking up
3257 * exactly the values we're given in LDAP. */
3259 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3260 "(sambaSid=%s)(gidNumber=%u)"
3261 "(sambaGroupType=%d))",
3263 sid_string_talloc(mem_ctx, &map->sid),
3264 map->gid, map->sid_name_use);
3265 if (filter == NULL) {
3266 result = NT_STATUS_NO_MEMORY;
3270 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3271 get_attr_list(mem_ctx, groupmap_attr_list),
3273 talloc_autofree_ldapmsg(mem_ctx, msg);
3275 if ((rc != LDAP_SUCCESS) ||
3276 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3277 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3278 result = NT_STATUS_NO_SUCH_GROUP;
3282 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3285 result = NT_STATUS_NO_MEMORY;
3290 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3292 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3294 talloc_autofree_ldapmod(mem_ctx, mods);
3297 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3298 "nothing to do\n"));
3299 result = NT_STATUS_OK;
3303 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3305 if (rc != LDAP_SUCCESS) {
3306 result = NT_STATUS_ACCESS_DENIED;
3310 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3311 "group %lu in LDAP\n", (unsigned long)map->gid));
3313 result = NT_STATUS_OK;
3316 TALLOC_FREE(mem_ctx);
3320 /**********************************************************************
3321 *********************************************************************/
3323 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3326 struct ldapsam_privates *priv =
3327 (struct ldapsam_privates *)methods->private_data;
3328 LDAPMessage *msg, *entry;
3331 TALLOC_CTX *mem_ctx;
3334 mem_ctx = talloc_new(NULL);
3335 if (mem_ctx == NULL) {
3336 DEBUG(0, ("talloc_new failed\n"));
3337 return NT_STATUS_NO_MEMORY;
3340 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3341 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3342 sid_string_talloc(mem_ctx, &sid));
3343 if (filter == NULL) {
3344 result = NT_STATUS_NO_MEMORY;
3347 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3348 get_attr_list(mem_ctx, groupmap_attr_list),
3350 talloc_autofree_ldapmsg(mem_ctx, msg);
3352 if ((rc != LDAP_SUCCESS) ||
3353 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3354 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3355 result = NT_STATUS_NO_SUCH_GROUP;
3359 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3360 get_attr_list(mem_ctx,
3361 groupmap_attr_list_to_delete));
3363 if ((rc == LDAP_NAMING_VIOLATION) ||
3364 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3365 const char *attrs[] = { "sambaGroupType", "description",
3366 "displayName", "sambaSIDList",
3369 /* Second try. Don't delete the sambaSID attribute, this is
3370 for "old" entries that are tacked on a winbind
3373 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3374 LDAP_OBJ_GROUPMAP, attrs);
3377 if ((rc == LDAP_NAMING_VIOLATION) ||
3378 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3379 const char *attrs[] = { "sambaGroupType", "description",
3380 "displayName", "sambaSIDList",
3381 "gidNumber", NULL };
3383 /* Third try. This is a post-3.0.21 alias (containing only
3384 * sambaSidEntry and sambaGroupMapping classes), we also have
3385 * to delete the gidNumber attribute, only the sambaSidEntry
3388 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3389 LDAP_OBJ_GROUPMAP, attrs);
3392 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3395 TALLOC_FREE(mem_ctx);
3399 /**********************************************************************
3400 *********************************************************************/
3402 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3405 struct ldapsam_privates *ldap_state =
3406 (struct ldapsam_privates *)my_methods->private_data;
3407 char *filter = NULL;
3409 const char **attr_list;
3411 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3413 return NT_STATUS_NO_MEMORY;
3415 attr_list = get_attr_list( NULL, groupmap_attr_list );
3416 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3417 LDAP_SCOPE_SUBTREE, filter,
3418 attr_list, 0, &ldap_state->result);
3419 TALLOC_FREE(attr_list);
3421 if (rc != LDAP_SUCCESS) {
3422 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3423 ldap_err2string(rc)));
3424 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3425 lp_ldap_group_suffix(), filter));
3426 ldap_msgfree(ldap_state->result);
3427 ldap_state->result = NULL;
3428 TALLOC_FREE(filter);
3429 return NT_STATUS_UNSUCCESSFUL;
3432 TALLOC_FREE(filter);
3434 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3435 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3436 ldap_state->result)));
3439 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3440 ldap_state->result);
3441 ldap_state->index = 0;
3443 return NT_STATUS_OK;
3446 /**********************************************************************
3447 *********************************************************************/
3449 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3451 ldapsam_endsampwent(my_methods);
3454 /**********************************************************************
3455 *********************************************************************/
3457 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3460 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3461 struct ldapsam_privates *ldap_state =
3462 (struct ldapsam_privates *)my_methods->private_data;
3466 if (!ldap_state->entry)
3469 ldap_state->index++;
3470 bret = init_group_from_ldap(ldap_state, map,
3474 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3478 return NT_STATUS_OK;
3481 /**********************************************************************
3482 *********************************************************************/
3484 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3485 const DOM_SID *domsid, enum lsa_SidType sid_name_use,
3486 GROUP_MAP **pp_rmap,
3487 size_t *p_num_entries,
3496 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3497 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3499 return NT_STATUS_ACCESS_DENIED;
3502 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3503 if (sid_name_use != SID_NAME_UNKNOWN &&
3504 sid_name_use != map.sid_name_use) {
3505 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3506 "not of the requested type\n", map.nt_name));
3509 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3510 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3511 "non mapped\n", map.nt_name));
3515 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3517 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3518 "enlarge group map!\n"));
3519 return NT_STATUS_UNSUCCESSFUL;
3522 (*pp_rmap)[entries] = map;
3527 ldapsam_endsamgrent(methods);
3529 *p_num_entries = entries;
3531 return NT_STATUS_OK;
3534 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3535 const DOM_SID *alias,
3536 const DOM_SID *member,
3539 struct ldapsam_privates *ldap_state =
3540 (struct ldapsam_privates *)methods->private_data;
3542 LDAPMessage *result = NULL;
3543 LDAPMessage *entry = NULL;
3545 LDAPMod **mods = NULL;
3547 enum lsa_SidType type = SID_NAME_USE_NONE;
3549 char *filter = NULL;
3551 if (sid_check_is_in_builtin(alias)) {
3552 type = SID_NAME_ALIAS;
3555 if (sid_check_is_in_our_domain(alias)) {
3556 type = SID_NAME_ALIAS;
3559 if (type == SID_NAME_USE_NONE) {
3560 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3561 sid_string_dbg(alias)));
3562 return NT_STATUS_NO_SUCH_ALIAS;
3565 if (asprintf(&filter,
3566 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3567 LDAP_OBJ_GROUPMAP, sid_string_static(alias),
3569 return NT_STATUS_NO_MEMORY;
3572 if (ldapsam_search_one_group(ldap_state, filter,
3573 &result) != LDAP_SUCCESS) {
3575 return NT_STATUS_NO_SUCH_ALIAS;
3578 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3582 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3583 ldap_msgfree(result);
3585 return NT_STATUS_NO_SUCH_ALIAS;
3589 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3590 "filter %s: count=%d\n", filter, count));
3591 ldap_msgfree(result);
3593 return NT_STATUS_NO_SUCH_ALIAS;
3598 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3602 ldap_msgfree(result);
3603 return NT_STATUS_UNSUCCESSFUL;
3606 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
3608 ldap_msgfree(result);
3609 return NT_STATUS_UNSUCCESSFUL;
3612 smbldap_set_mod(&mods, modop,
3613 get_attr_key2string(groupmap_attr_list,
3614 LDAP_ATTR_SID_LIST),
3615 sid_string_static(member));
3617 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3619 ldap_mods_free(mods, True);
3620 ldap_msgfree(result);
3623 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3624 return NT_STATUS_MEMBER_IN_ALIAS;
3627 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3628 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3631 if (rc != LDAP_SUCCESS) {
3632 return NT_STATUS_UNSUCCESSFUL;
3635 return NT_STATUS_OK;
3638 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3639 const DOM_SID *alias,
3640 const DOM_SID *member)
3642 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3645 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3646 const DOM_SID *alias,
3647 const DOM_SID *member)
3649 return ldapsam_modify_aliasmem(methods, alias, member,
3653 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3654 const DOM_SID *alias,
3655 DOM_SID **pp_members,
3656 size_t *p_num_members)
3658 struct ldapsam_privates *ldap_state =
3659 (struct ldapsam_privates *)methods->private_data;
3660 LDAPMessage *result = NULL;
3661 LDAPMessage *entry = NULL;
3663 char **values = NULL;
3665 char *filter = NULL;
3666 size_t num_members = 0;
3667 enum lsa_SidType type = SID_NAME_USE_NONE;
3672 if (sid_check_is_in_builtin(alias)) {
3673 type = SID_NAME_ALIAS;
3676 if (sid_check_is_in_our_domain(alias)) {
3677 type = SID_NAME_ALIAS;
3680 if (type == SID_NAME_USE_NONE) {
3681 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3682 sid_string_dbg(alias)));
3683 return NT_STATUS_NO_SUCH_ALIAS;
3686 if (asprintf(&filter,
3687 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3688 LDAP_OBJ_GROUPMAP, sid_string_static(alias),
3690 return NT_STATUS_NO_MEMORY;
3693 if (ldapsam_search_one_group(ldap_state, filter,
3694 &result) != LDAP_SUCCESS) {
3696 return NT_STATUS_NO_SUCH_ALIAS;
3699 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3703 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3704 ldap_msgfree(result);
3706 return NT_STATUS_NO_SUCH_ALIAS;
3710 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3711 "filter %s: count=%d\n", filter, count));
3712 ldap_msgfree(result);
3714 return NT_STATUS_NO_SUCH_ALIAS;
3719 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3723 ldap_msgfree(result);
3724 return NT_STATUS_UNSUCCESSFUL;
3727 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3729 get_attr_key2string(groupmap_attr_list,
3730 LDAP_ATTR_SID_LIST));
3732 if (values == NULL) {
3733 ldap_msgfree(result);
3734 return NT_STATUS_OK;
3737 count = ldap_count_values(values);
3739 for (i=0; i<count; i++) {
3742 if (!string_to_sid(&member, values[i]))
3745 if (!add_sid_to_array(NULL, &member, pp_members, &num_members)) {
3746 ldap_value_free(values);
3747 ldap_msgfree(result);
3748 return NT_STATUS_NO_MEMORY;
3752 *p_num_members = num_members;
3753 ldap_value_free(values);
3754 ldap_msgfree(result);
3756 return NT_STATUS_OK;
3759 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3760 TALLOC_CTX *mem_ctx,
3761 const DOM_SID *domain_sid,
3762 const DOM_SID *members,
3764 uint32 **pp_alias_rids,
3765 size_t *p_num_alias_rids)
3767 struct ldapsam_privates *ldap_state =
3768 (struct ldapsam_privates *)methods->private_data;
3771 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3773 LDAPMessage *result = NULL;
3774 LDAPMessage *entry = NULL;
3778 enum lsa_SidType type = SID_NAME_USE_NONE;
3780 if (sid_check_is_builtin(domain_sid)) {
3781 type = SID_NAME_ALIAS;
3784 if (sid_check_is_domain(domain_sid)) {
3785 type = SID_NAME_ALIAS;
3788 if (type == SID_NAME_USE_NONE) {
3789 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3790 sid_string_dbg(domain_sid)));
3791 return NT_STATUS_UNSUCCESSFUL;
3794 filter = talloc_asprintf(mem_ctx,
3795 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3796 LDAP_OBJ_GROUPMAP, type);
3798 for (i=0; i<num_members; i++)
3799 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3801 sid_string_talloc(mem_ctx,
3804 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3806 if (filter == NULL) {
3807 return NT_STATUS_NO_MEMORY;
3810 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3811 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3813 if (rc != LDAP_SUCCESS)
3814 return NT_STATUS_UNSUCCESSFUL;
3816 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3818 for (entry = ldap_first_entry(ldap_struct, result);
3820 entry = ldap_next_entry(ldap_struct, entry))
3826 if (!smbldap_get_single_attribute(ldap_struct, entry,
3832 if (!string_to_sid(&sid, sid_str))
3835 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3838 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3839 p_num_alias_rids)) {
3840 ldap_msgfree(result);
3841 return NT_STATUS_NO_MEMORY;
3845 ldap_msgfree(result);
3846 return NT_STATUS_OK;
3849 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3853 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3855 LDAPMod **mods = NULL;
3856 fstring value_string;
3857 const char *policy_attr = NULL;
3859 struct ldapsam_privates *ldap_state =
3860 (struct ldapsam_privates *)methods->private_data;
3862 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3864 if (!ldap_state->domain_dn) {
3865 return NT_STATUS_INVALID_PARAMETER;
3868 policy_attr = get_account_policy_attr(policy_index);
3869 if (policy_attr == NULL) {
3870 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3875 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3877 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3879 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3882 ldap_mods_free(mods, True);
3884 if (rc != LDAP_SUCCESS) {
3888 if (!cache_account_policy_set(policy_index, value)) {
3889 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3890 "update local tdb cache\n"));
3894 return NT_STATUS_OK;
3897 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3898 int policy_index, uint32 value)
3900 return ldapsam_set_account_policy_in_ldap(methods, policy_index,
3904 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3908 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3909 LDAPMessage *result = NULL;
3910 LDAPMessage *entry = NULL;
3914 const char *policy_attr = NULL;
3916 struct ldapsam_privates *ldap_state =
3917 (struct ldapsam_privates *)methods->private_data;
3919 const char *attrs[2];
3921 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3923 if (!ldap_state->domain_dn) {
3924 return NT_STATUS_INVALID_PARAMETER;
3927 policy_attr = get_account_policy_attr(policy_index);
3929 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3930 "policy index: %d\n", policy_index));
3934 attrs[0] = policy_attr;
3937 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3938 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
3941 if (rc != LDAP_SUCCESS) {
3945 count = ldap_count_entries(priv2ld(ldap_state), result);
3950 entry = ldap_first_entry(priv2ld(ldap_state), result);
3951 if (entry == NULL) {
3955 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3960 *value = (uint32)atol(vals[0]);
3962 ntstatus = NT_STATUS_OK;
3966 ldap_value_free(vals);
3967 ldap_msgfree(result);
3972 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3974 - if user hasn't decided to use account policies inside LDAP just reuse the
3977 - if there is a valid cache entry, return that
3978 - if there is an LDAP entry, update cache and return
3979 - otherwise set to default, update cache and return
3983 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
3984 int policy_index, uint32 *value)
3986 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3988 if (cache_account_policy_get(policy_index, value)) {
3989 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3991 return NT_STATUS_OK;
3994 ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
3996 if (NT_STATUS_IS_OK(ntstatus)) {
4000 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4004 /* should we automagically migrate old tdb value here ? */
4005 if (account_policy_get(policy_index, value))
4008 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4009 "default\n", policy_index));
4012 if (!account_policy_get_default(policy_index, value)) {
4018 ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
4019 if (!NT_STATUS_IS_OK(ntstatus)) {
4025 if (!cache_account_policy_set(policy_index, *value)) {
4026 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4027 "tdb as a cache\n"));
4028 return NT_STATUS_UNSUCCESSFUL;
4031 return NT_STATUS_OK;
4034 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4035 const DOM_SID *domain_sid,
4039 enum lsa_SidType *attrs)
4041 struct ldapsam_privates *ldap_state =
4042 (struct ldapsam_privates *)methods->private_data;
4043 LDAPMessage *msg = NULL;
4045 char *allsids = NULL;
4046 int i, rc, num_mapped;
4047 NTSTATUS result = NT_STATUS_NO_MEMORY;
4048 TALLOC_CTX *mem_ctx;
4052 mem_ctx = talloc_new(NULL);
4053 if (mem_ctx == NULL) {
4054 DEBUG(0, ("talloc_new failed\n"));
4058 if (!sid_check_is_builtin(domain_sid) &&
4059 !sid_check_is_domain(domain_sid)) {
4060 result = NT_STATUS_INVALID_PARAMETER;
4064 for (i=0; i<num_rids; i++)
4065 attrs[i] = SID_NAME_UNKNOWN;
4067 allsids = talloc_strdup(mem_ctx, "");
4068 if (allsids == NULL) {
4072 for (i=0; i<num_rids; i++) {
4074 sid_compose(&sid, domain_sid, rids[i]);
4075 allsids = talloc_asprintf_append_buffer(allsids, "(sambaSid=%s)",
4076 sid_string_static(&sid));
4077 if (allsids == NULL) {
4082 /* First look for users */
4086 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4088 filter = talloc_asprintf(
4089 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4090 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4092 if (filter == NULL) {
4096 rc = smbldap_search(ldap_state->smbldap_state,
4097 lp_ldap_user_suffix(),
4098 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4100 talloc_autofree_ldapmsg(mem_ctx, msg);
4103 if (rc != LDAP_SUCCESS)
4106 ld = ldap_state->smbldap_state->ldap_struct;
4109 for (entry = ldap_first_entry(ld, msg);
4111 entry = ldap_next_entry(ld, entry)) {
4116 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4118 DEBUG(2, ("Could not find sid from ldap entry\n"));
4122 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4125 DEBUG(2, ("Could not retrieve uid attribute\n"));
4129 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4130 if (rid == rids[rid_index])
4134 if (rid_index == num_rids) {
4135 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4139 attrs[rid_index] = SID_NAME_USER;
4140 names[rid_index] = name;
4144 if (num_mapped == num_rids) {
4145 /* No need to look for groups anymore -- we're done */
4146 result = NT_STATUS_OK;
4150 /* Same game for groups */
4154 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4155 "sambaGroupType", NULL };
4157 filter = talloc_asprintf(
4158 mem_ctx, "(&(objectClass=%s)(|%s))",
4159 LDAP_OBJ_GROUPMAP, allsids);
4160 if (filter == NULL) {
4164 rc = smbldap_search(ldap_state->smbldap_state,
4165 lp_ldap_group_suffix(),
4166 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4168 talloc_autofree_ldapmsg(mem_ctx, msg);
4171 if (rc != LDAP_SUCCESS)
4174 /* ldap_struct might have changed due to a reconnect */
4176 ld = ldap_state->smbldap_state->ldap_struct;
4178 /* For consistency checks, we already checked we're only domain or builtin */
4180 is_builtin = sid_check_is_builtin(domain_sid);
4182 for (entry = ldap_first_entry(ld, msg);
4184 entry = ldap_next_entry(ld, entry))
4189 enum lsa_SidType type;
4190 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4192 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4195 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4200 type = (enum lsa_SidType)atol(attr);
4202 /* Consistency checks */
4203 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4204 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4205 (type != SID_NAME_DOM_GRP)))) {
4206 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4209 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4211 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4215 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4218 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4220 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4224 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4229 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4230 if (rid == rids[rid_index])
4234 if (rid_index == num_rids) {
4235 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4239 attrs[rid_index] = type;
4240 names[rid_index] = attr;
4244 result = NT_STATUS_NONE_MAPPED;
4247 result = (num_mapped == num_rids) ?
4248 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4250 TALLOC_FREE(mem_ctx);
4254 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4256 char *filter = NULL;
4257 char *escaped = NULL;
4258 char *result = NULL;
4260 asprintf(&filter, "(&%s(objectclass=sambaSamAccount))",
4262 if (filter == NULL) goto done;
4264 escaped = escape_ldap_string_alloc(username);
4265 if (escaped == NULL) goto done;
4267 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4276 const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4280 const char **result;
4282 va_start(ap, mem_ctx);
4283 while (va_arg(ap, const char *) != NULL)
4287 if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
4291 va_start(ap, mem_ctx);
4292 for (i=0; i<num; i++) {
4293 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4294 if (result[i] == NULL) {
4295 talloc_free(result);
4305 struct ldap_search_state {
4306 struct smbldap_state *connection;
4316 void *pagedresults_cookie;
4318 LDAPMessage *entries, *current_entry;
4319 bool (*ldap2displayentry)(struct ldap_search_state *state,
4320 TALLOC_CTX *mem_ctx,
4321 LDAP *ld, LDAPMessage *entry,
4322 struct samr_displayentry *result);
4325 static bool ldapsam_search_firstpage(struct pdb_search *search)
4327 struct ldap_search_state *state =
4328 (struct ldap_search_state *)search->private_data;
4330 int rc = LDAP_OPERATIONS_ERROR;
4332 state->entries = NULL;
4334 if (state->connection->paged_results) {
4335 rc = smbldap_search_paged(state->connection, state->base,
4336 state->scope, state->filter,
4337 state->attrs, state->attrsonly,
4338 lp_ldap_page_size(), &state->entries,
4339 &state->pagedresults_cookie);
4342 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4344 if (state->entries != NULL) {
4345 /* Left over from unsuccessful paged attempt */
4346 ldap_msgfree(state->entries);
4347 state->entries = NULL;
4350 rc = smbldap_search(state->connection, state->base,
4351 state->scope, state->filter, state->attrs,
4352 state->attrsonly, &state->entries);
4354 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4357 /* Ok, the server was lying. It told us it could do paged
4358 * searches when it could not. */
4359 state->connection->paged_results = False;
4362 ld = state->connection->ldap_struct;
4364 DEBUG(5, ("Don't have an LDAP connection right after a "
4368 state->current_entry = ldap_first_entry(ld, state->entries);
4370 if (state->current_entry == NULL) {
4371 ldap_msgfree(state->entries);
4372 state->entries = NULL;
4378 static bool ldapsam_search_nextpage(struct pdb_search *search)
4380 struct ldap_search_state *state =
4381 (struct ldap_search_state *)search->private_data;
4384 if (!state->connection->paged_results) {
4385 /* There is no next page when there are no paged results */
4389 rc = smbldap_search_paged(state->connection, state->base,
4390 state->scope, state->filter, state->attrs,
4391 state->attrsonly, lp_ldap_page_size(),
4393 &state->pagedresults_cookie);
4395 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4398 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4400 if (state->current_entry == NULL) {
4401 ldap_msgfree(state->entries);
4402 state->entries = NULL;
4408 static bool ldapsam_search_next_entry(struct pdb_search *search,
4409 struct samr_displayentry *entry)
4411 struct ldap_search_state *state =
4412 (struct ldap_search_state *)search->private_data;
4416 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4419 if ((state->entries == NULL) &&
4420 !ldapsam_search_nextpage(search))
4423 result = state->ldap2displayentry(state, search->mem_ctx, state->connection->ldap_struct,
4424 state->current_entry, entry);
4428 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4429 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4430 if (dn != NULL) ldap_memfree(dn);
4433 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4435 if (state->current_entry == NULL) {
4436 ldap_msgfree(state->entries);
4437 state->entries = NULL;
4440 if (!result) goto retry;
4445 static void ldapsam_search_end(struct pdb_search *search)
4447 struct ldap_search_state *state =
4448 (struct ldap_search_state *)search->private_data;
4451 if (state->pagedresults_cookie == NULL)
4454 if (state->entries != NULL)
4455 ldap_msgfree(state->entries);
4457 state->entries = NULL;
4458 state->current_entry = NULL;
4460 if (!state->connection->paged_results)
4463 /* Tell the LDAP server we're not interested in the rest anymore. */
4465 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4466 state->filter, state->attrs,
4467 state->attrsonly, 0, &state->entries,
4468 &state->pagedresults_cookie);
4470 if (rc != LDAP_SUCCESS)
4471 DEBUG(5, ("Could not end search properly\n"));
4476 static bool ldapuser2displayentry(struct ldap_search_state *state,
4477 TALLOC_CTX *mem_ctx,
4478 LDAP *ld, LDAPMessage *entry,
4479 struct samr_displayentry *result)
4485 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4486 if ((vals == NULL) || (vals[0] == NULL)) {
4487 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4490 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4491 ldap_value_free(vals);
4493 if ((state->acct_flags != 0) &&
4494 ((state->acct_flags & acct_flags) == 0))
4497 result->acct_flags = acct_flags;
4498 result->account_name = "";
4499 result->fullname = "";
4500 result->description = "";
4502 vals = ldap_get_values(ld, entry, "uid");
4503 if ((vals == NULL) || (vals[0] == NULL)) {
4504 DEBUG(5, ("\"uid\" not found\n"));
4507 pull_utf8_talloc(mem_ctx,
4508 CONST_DISCARD(char **, &result->account_name),
4510 ldap_value_free(vals);
4512 vals = ldap_get_values(ld, entry, "displayName");
4513 if ((vals == NULL) || (vals[0] == NULL))
4514 DEBUG(8, ("\"displayName\" not found\n"));
4516 pull_utf8_talloc(mem_ctx,
4517 CONST_DISCARD(char **, &result->fullname),
4519 ldap_value_free(vals);
4521 vals = ldap_get_values(ld, entry, "description");
4522 if ((vals == NULL) || (vals[0] == NULL))
4523 DEBUG(8, ("\"description\" not found\n"));
4525 pull_utf8_talloc(mem_ctx,
4526 CONST_DISCARD(char **, &result->description),
4528 ldap_value_free(vals);
4530 if ((result->account_name == NULL) ||
4531 (result->fullname == NULL) ||
4532 (result->description == NULL)) {
4533 DEBUG(0, ("talloc failed\n"));
4537 vals = ldap_get_values(ld, entry, "sambaSid");
4538 if ((vals == NULL) || (vals[0] == NULL)) {
4539 DEBUG(0, ("\"objectSid\" not found\n"));
4543 if (!string_to_sid(&sid, vals[0])) {
4544 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4545 ldap_value_free(vals);
4548 ldap_value_free(vals);
4550 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4551 DEBUG(0, ("sid %s does not belong to our domain\n",
4552 sid_string_dbg(&sid)));
4560 static bool ldapsam_search_users(struct pdb_methods *methods,
4561 struct pdb_search *search,
4564 struct ldapsam_privates *ldap_state =
4565 (struct ldapsam_privates *)methods->private_data;
4566 struct ldap_search_state *state;
4568 state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
4569 if (state == NULL) {
4570 DEBUG(0, ("talloc failed\n"));
4574 state->connection = ldap_state->smbldap_state;
4576 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4577 state->base = lp_ldap_user_suffix();
4578 else if ((acct_flags != 0) &&
4579 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4580 state->base = lp_ldap_machine_suffix();
4582 state->base = lp_ldap_suffix();
4584 state->acct_flags = acct_flags;
4585 state->base = talloc_strdup(search->mem_ctx, state->base);
4586 state->scope = LDAP_SCOPE_SUBTREE;
4587 state->filter = get_ldap_filter(search->mem_ctx, "*");
4588 state->attrs = talloc_attrs(search->mem_ctx, "uid", "sambaSid",
4589 "displayName", "description",
4590 "sambaAcctFlags", NULL);
4591 state->attrsonly = 0;
4592 state->pagedresults_cookie = NULL;
4593 state->entries = NULL;
4594 state->ldap2displayentry = ldapuser2displayentry;
4596 if ((state->filter == NULL) || (state->attrs == NULL)) {
4597 DEBUG(0, ("talloc failed\n"));
4601 search->private_data = state;
4602 search->next_entry = ldapsam_search_next_entry;
4603 search->search_end = ldapsam_search_end;
4605 return ldapsam_search_firstpage(search);
4608 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4609 TALLOC_CTX *mem_ctx,
4610 LDAP *ld, LDAPMessage *entry,
4611 struct samr_displayentry *result)
4617 result->account_name = "";
4618 result->fullname = "";
4619 result->description = "";
4622 vals = ldap_get_values(ld, entry, "sambaGroupType");
4623 if ((vals == NULL) || (vals[0] == NULL)) {
4624 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4626 ldap_value_free(vals);
4631 group_type = atoi(vals[0]);
4633 if ((state->group_type != 0) &&
4634 ((state->group_type != group_type))) {
4635 ldap_value_free(vals);
4639 ldap_value_free(vals);
4641 /* display name is the NT group name */
4643 vals = ldap_get_values(ld, entry, "displayName");
4644 if ((vals == NULL) || (vals[0] == NULL)) {
4645 DEBUG(8, ("\"displayName\" not found\n"));
4647 /* fallback to the 'cn' attribute */
4648 vals = ldap_get_values(ld, entry, "cn");
4649 if ((vals == NULL) || (vals[0] == NULL)) {
4650 DEBUG(5, ("\"cn\" not found\n"));
4653 pull_utf8_talloc(mem_ctx,
4654 CONST_DISCARD(char **, &result->account_name),
4658 pull_utf8_talloc(mem_ctx,
4659 CONST_DISCARD(char **, &result->account_name),
4663 ldap_value_free(vals);
4665 vals = ldap_get_values(ld, entry, "description");
4666 if ((vals == NULL) || (vals[0] == NULL))
4667 DEBUG(8, ("\"description\" not found\n"));
4669 pull_utf8_talloc(mem_ctx,
4670 CONST_DISCARD(char **, &result->description),
4672 ldap_value_free(vals);
4674 if ((result->account_name == NULL) ||
4675 (result->fullname == NULL) ||
4676 (result->description == NULL)) {
4677 DEBUG(0, ("talloc failed\n"));
4681 vals = ldap_get_values(ld, entry, "sambaSid");
4682 if ((vals == NULL) || (vals[0] == NULL)) {
4683 DEBUG(0, ("\"objectSid\" not found\n"));
4685 ldap_value_free(vals);
4690 if (!string_to_sid(&sid, vals[0])) {
4691 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4695 ldap_value_free(vals);
4697 switch (group_type) {
4698 case SID_NAME_DOM_GRP:
4699 case SID_NAME_ALIAS:
4701 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4702 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4704 DEBUG(0, ("%s is not in our domain\n",
4705 sid_string_dbg(&sid)));
4711 DEBUG(0,("unkown group type: %d\n", group_type));
4718 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4719 struct pdb_search *search,
4721 enum lsa_SidType type)
4723 struct ldapsam_privates *ldap_state =
4724 (struct ldapsam_privates *)methods->private_data;
4725 struct ldap_search_state *state;
4727 state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
4728 if (state == NULL) {
4729 DEBUG(0, ("talloc failed\n"));
4733 state->connection = ldap_state->smbldap_state;
4735 state->base = talloc_strdup(search->mem_ctx, lp_ldap_group_suffix());
4736 state->connection = ldap_state->smbldap_state;
4737 state->scope = LDAP_SCOPE_SUBTREE;
4738 state->filter = talloc_asprintf(search->mem_ctx,
4739 "(&(objectclass=sambaGroupMapping)"
4740 "(sambaGroupType=%d)(sambaSID=%s*))",
4741 type, sid_string_static(sid));
4742 state->attrs = talloc_attrs(search->mem_ctx, "cn", "sambaSid",
4743 "displayName", "description",
4744 "sambaGroupType", NULL);
4745 state->attrsonly = 0;
4746 state->pagedresults_cookie = NULL;
4747 state->entries = NULL;
4748 state->group_type = type;
4749 state->ldap2displayentry = ldapgroup2displayentry;
4751 if ((state->filter == NULL) || (state->attrs == NULL)) {
4752 DEBUG(0, ("talloc failed\n"));
4756 search->private_data = state;
4757 search->next_entry = ldapsam_search_next_entry;
4758 search->search_end = ldapsam_search_end;
4760 return ldapsam_search_firstpage(search);
4763 static bool ldapsam_search_groups(struct pdb_methods *methods,
4764 struct pdb_search *search)
4766 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4769 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4770 struct pdb_search *search,
4773 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4776 static bool ldapsam_rid_algorithm(struct pdb_methods *methods)
4781 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4784 struct smbldap_state *smbldap_state = priv->smbldap_state;
4786 LDAPMessage *result = NULL;
4787 LDAPMessage *entry = NULL;
4788 LDAPMod **mods = NULL;
4795 TALLOC_CTX *mem_ctx;
4797 mem_ctx = talloc_new(NULL);
4798 if (mem_ctx == NULL) {
4799 DEBUG(0, ("talloc_new failed\n"));
4800 return NT_STATUS_NO_MEMORY;
4803 status = smbldap_search_domain_info(smbldap_state, &result,
4804 get_global_sam_name(), False);
4805 if (!NT_STATUS_IS_OK(status)) {
4806 DEBUG(3, ("Could not get domain info: %s\n",
4807 nt_errstr(status)));
4811 talloc_autofree_ldapmsg(mem_ctx, result);
4813 entry = ldap_first_entry(priv2ld(priv), result);
4814 if (entry == NULL) {
4815 DEBUG(0, ("Could not get domain info entry\n"));
4816 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4820 /* Find the largest of the three attributes "sambaNextRid",
4821 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4822 concept of differentiating between user and group rids, and will
4823 use only "sambaNextRid" in the future. But for compatibility
4824 reasons I look if others have chosen different strategies -- VL */
4826 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4827 "sambaNextRid", mem_ctx);
4828 if (value != NULL) {
4829 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4830 nextRid = MAX(nextRid, tmp);
4833 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4834 "sambaNextUserRid", mem_ctx);
4835 if (value != NULL) {
4836 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4837 nextRid = MAX(nextRid, tmp);
4840 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4841 "sambaNextGroupRid", mem_ctx);
4842 if (value != NULL) {
4843 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4844 nextRid = MAX(nextRid, tmp);
4848 nextRid = BASE_RID-1;
4853 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4854 talloc_asprintf(mem_ctx, "%d", nextRid));
4855 talloc_autofree_ldapmod(mem_ctx, mods);
4857 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4858 status = NT_STATUS_NO_MEMORY;
4862 rc = smbldap_modify(smbldap_state, dn, mods);
4864 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4867 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4870 if (NT_STATUS_IS_OK(status)) {
4874 TALLOC_FREE(mem_ctx);
4878 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32 *rid)
4882 for (i=0; i<10; i++) {
4883 NTSTATUS result = ldapsam_get_new_rid(
4884 (struct ldapsam_privates *)methods->private_data, rid);
4885 if (NT_STATUS_IS_OK(result)) {
4889 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4893 /* The ldap update failed (maybe a race condition), retry */
4896 /* Tried 10 times, fail. */
4897 return NT_STATUS_ACCESS_DENIED;
4900 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32 *rid)
4902 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4903 return NT_STATUS_IS_OK(result) ? True : False;
4906 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4908 union unid_t *id, enum lsa_SidType *type)
4910 struct ldapsam_privates *priv =
4911 (struct ldapsam_privates *)methods->private_data;
4913 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4915 LDAPMessage *result = NULL;
4916 LDAPMessage *entry = NULL;
4921 TALLOC_CTX *mem_ctx;
4923 mem_ctx = talloc_new(NULL);
4924 if (mem_ctx == NULL) {
4925 DEBUG(0, ("talloc_new failed\n"));
4929 filter = talloc_asprintf(mem_ctx,
4931 "(|(objectClass=%s)(objectClass=%s)))",
4932 sid_string_talloc(mem_ctx, sid),
4933 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
4934 if (filter == NULL) {
4935 DEBUG(5, ("talloc_asprintf failed\n"));
4939 rc = smbldap_search_suffix(priv->smbldap_state, filter,
4941 if (rc != LDAP_SUCCESS) {
4944 talloc_autofree_ldapmsg(mem_ctx, result);
4946 if (ldap_count_entries(priv2ld(priv), result) != 1) {
4947 DEBUG(10, ("Got %d entries, expected one\n",
4948 ldap_count_entries(priv2ld(priv), result)));
4952 entry = ldap_first_entry(priv2ld(priv), result);
4954 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4955 "sambaGroupType", mem_ctx);
4957 if (value != NULL) {
4958 const char *gid_str;
4961 gid_str = smbldap_talloc_single_attribute(
4962 priv2ld(priv), entry, "gidNumber", mem_ctx);
4963 if (gid_str == NULL) {
4964 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4965 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
4970 id->gid = strtoul(gid_str, NULL, 10);
4971 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
4976 /* It must be a user */
4978 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4979 "uidNumber", mem_ctx);
4980 if (value == NULL) {
4981 DEBUG(1, ("Could not find uidNumber in %s\n",
4982 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
4986 id->uid = strtoul(value, NULL, 10);
4987 *type = SID_NAME_USER;
4991 TALLOC_FREE(mem_ctx);
4996 * The following functions is called only if
4997 * ldapsam:trusted and ldapsam:editposix are
5002 * ldapsam_create_user creates a new
5003 * posixAccount and sambaSamAccount object
5004 * in the ldap users subtree
5006 * The uid is allocated by winbindd.
5009 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5010 TALLOC_CTX *tmp_ctx, const char *name,
5011 uint32 acb_info, uint32 *rid)
5013 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5014 LDAPMessage *entry = NULL;
5015 LDAPMessage *result = NULL;
5017 bool is_machine = False;
5018 bool add_posix = False;
5019 LDAPMod **mods = NULL;
5027 const char *dn = NULL;
5035 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5036 acb_info & ACB_WSTRUST ||
5037 acb_info & ACB_SVRTRUST ||
5038 acb_info & ACB_DOMTRUST) {
5042 username = escape_ldap_string_alloc(name);
5043 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5044 username, LDAP_OBJ_POSIXACCOUNT);
5045 SAFE_FREE(username);
5047 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5048 if (rc != LDAP_SUCCESS) {
5049 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5050 return NT_STATUS_UNSUCCESSFUL;
5052 talloc_autofree_ldapmsg(tmp_ctx, result);
5054 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5056 if (num_result > 1) {
5057 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5058 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5061 if (num_result == 1) {
5063 /* check if it is just a posix account.
5064 * or if there is a sid attached to this entry
5067 entry = ldap_first_entry(priv2ld(ldap_state), result);
5069 return NT_STATUS_UNSUCCESSFUL;
5072 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5074 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5075 return NT_STATUS_USER_EXISTS;
5078 /* it is just a posix account, retrieve the dn for later use */
5079 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5081 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5082 return NT_STATUS_NO_MEMORY;
5086 if (num_result == 0) {
5090 /* Create the basic samu structure and generate the mods for the ldap commit */
5091 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5092 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5096 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5098 user = samu_new(tmp_ctx);
5100 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5101 return NT_STATUS_NO_MEMORY;
5104 if (!pdb_set_username(user, name, PDB_SET)) {
5105 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5106 return NT_STATUS_UNSUCCESSFUL;
5108 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5109 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5110 return NT_STATUS_UNSUCCESSFUL;
5113 if (acb_info & ACB_NORMAL) {
5114 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5115 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5116 return NT_STATUS_UNSUCCESSFUL;
5119 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5120 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5121 return NT_STATUS_UNSUCCESSFUL;
5125 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5126 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5127 return NT_STATUS_UNSUCCESSFUL;
5131 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5132 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5133 return NT_STATUS_UNSUCCESSFUL;
5136 if (!init_ldap_from_sam(ldap_state, NULL, &mods, user, element_is_set_or_changed)) {
5137 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5138 return NT_STATUS_UNSUCCESSFUL;
5141 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5142 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5144 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5149 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5151 /* retrieve the Domain Users group gid */
5152 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS) ||
5153 !sid_to_gid(&group_sid, &gid)) {
5154 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5155 return NT_STATUS_INVALID_PRIMARY_GROUP;
5158 /* lets allocate a new userid for this user */
5159 if (!winbind_allocate_uid(&uid)) {
5160 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5161 return NT_STATUS_UNSUCCESSFUL;
5166 /* TODO: choose a more appropriate default for machines */
5167 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5168 shell = talloc_strdup(tmp_ctx, "/bin/false");
5170 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5171 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5173 uidstr = talloc_asprintf(tmp_ctx, "%d", uid);
5174 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
5176 escape_name = escape_rdn_val_string_alloc(name);
5178 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5179 return NT_STATUS_NO_MEMORY;
5183 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5185 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5188 SAFE_FREE(escape_name);
5190 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5191 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5192 return NT_STATUS_NO_MEMORY;
5195 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5196 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5197 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5198 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5199 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5200 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5201 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5204 talloc_autofree_ldapmod(tmp_ctx, mods);
5207 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5209 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5212 if (rc != LDAP_SUCCESS) {
5213 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5214 return NT_STATUS_UNSUCCESSFUL;
5217 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5219 flush_pwnam_cache();
5221 return NT_STATUS_OK;
5224 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5226 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5227 LDAPMessage *result = NULL;
5228 LDAPMessage *entry = NULL;
5234 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5236 filter = talloc_asprintf(tmp_ctx,
5239 "(objectClass=%s))",
5240 pdb_get_username(sam_acct),
5241 LDAP_OBJ_POSIXACCOUNT,
5242 LDAP_OBJ_SAMBASAMACCOUNT);
5243 if (filter == NULL) {
5244 return NT_STATUS_NO_MEMORY;
5247 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5248 if (rc != LDAP_SUCCESS) {
5249 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5250 return NT_STATUS_UNSUCCESSFUL;
5252 talloc_autofree_ldapmsg(tmp_ctx, result);
5254 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5256 if (num_result == 0) {
5257 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5258 return NT_STATUS_NO_SUCH_USER;
5261 if (num_result > 1) {
5262 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5263 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5266 entry = ldap_first_entry(priv2ld(ldap_state), result);
5268 return NT_STATUS_UNSUCCESSFUL;
5271 /* it is just a posix account, retrieve the dn for later use */
5272 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5274 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5275 return NT_STATUS_NO_MEMORY;
5278 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5279 if (rc != LDAP_SUCCESS) {
5280 return NT_STATUS_UNSUCCESSFUL;
5283 flush_pwnam_cache();
5285 return NT_STATUS_OK;
5289 * ldapsam_create_group creates a new
5290 * posixGroup and sambaGroupMapping object
5291 * in the ldap groups subtree
5293 * The gid is allocated by winbindd.
5296 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5297 TALLOC_CTX *tmp_ctx,
5301 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5303 LDAPMessage *entry = NULL;
5304 LDAPMessage *result = NULL;
5306 bool is_new_entry = False;
5307 LDAPMod **mods = NULL;
5313 const char *dn = NULL;
5318 groupname = escape_ldap_string_alloc(name);
5319 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5320 groupname, LDAP_OBJ_POSIXGROUP);
5321 SAFE_FREE(groupname);
5323 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5324 if (rc != LDAP_SUCCESS) {
5325 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5326 return NT_STATUS_UNSUCCESSFUL;
5328 talloc_autofree_ldapmsg(tmp_ctx, result);
5330 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5332 if (num_result > 1) {
5333 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5334 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5337 if (num_result == 1) {
5339 /* check if it is just a posix group.
5340 * or if there is a sid attached to this entry
5343 entry = ldap_first_entry(priv2ld(ldap_state), result);
5345 return NT_STATUS_UNSUCCESSFUL;
5348 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5350 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5351 return NT_STATUS_GROUP_EXISTS;
5354 /* it is just a posix group, retrieve the gid and the dn for later use */
5355 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5357 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5358 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5361 gid = strtoul(tmp, NULL, 10);
5363 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5365 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5366 return NT_STATUS_NO_MEMORY;
5370 if (num_result == 0) {
5373 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5375 is_new_entry = True;
5377 /* lets allocate a new groupid for this group */
5378 if (!winbind_allocate_gid(&gid)) {
5379 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5380 return NT_STATUS_UNSUCCESSFUL;
5383 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
5385 escape_name = escape_rdn_val_string_alloc(name);
5387 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5388 return NT_STATUS_NO_MEMORY;
5391 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5393 SAFE_FREE(escape_name);
5395 if (!gidstr || !dn) {
5396 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5397 return NT_STATUS_NO_MEMORY;
5400 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5401 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5402 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5405 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5406 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5410 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5412 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5414 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5416 if (!groupsidstr || !grouptype) {
5417 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5418 return NT_STATUS_NO_MEMORY;
5421 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5422 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5423 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5424 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5425 talloc_autofree_ldapmod(tmp_ctx, mods);
5428 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5430 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5431 /* This call may fail with rfc2307bis schema */
5432 /* Retry adding a structural class */
5433 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5434 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5438 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5441 if (rc != LDAP_SUCCESS) {
5442 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5443 return NT_STATUS_UNSUCCESSFUL;
5446 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5448 return NT_STATUS_OK;
5451 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32 rid)
5453 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5454 LDAPMessage *result = NULL;
5455 LDAPMessage *entry = NULL;
5463 /* get the group sid */
5464 sid_compose(&group_sid, get_global_sam_sid(), rid);
5466 filter = talloc_asprintf(tmp_ctx,
5469 "(objectClass=%s))",
5470 sid_string_talloc(tmp_ctx, &group_sid),
5471 LDAP_OBJ_POSIXGROUP,
5473 if (filter == NULL) {
5474 return NT_STATUS_NO_MEMORY;
5477 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5478 if (rc != LDAP_SUCCESS) {
5479 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5480 return NT_STATUS_UNSUCCESSFUL;
5482 talloc_autofree_ldapmsg(tmp_ctx, result);
5484 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5486 if (num_result == 0) {
5487 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5488 return NT_STATUS_NO_SUCH_GROUP;
5491 if (num_result > 1) {
5492 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5493 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5496 entry = ldap_first_entry(priv2ld(ldap_state), result);
5498 return NT_STATUS_UNSUCCESSFUL;
5501 /* here it is, retrieve the dn for later use */
5502 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5504 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5505 return NT_STATUS_NO_MEMORY;
5508 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5510 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5511 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5514 /* check no user have this group marked as primary group */
5515 filter = talloc_asprintf(tmp_ctx,
5518 "(objectClass=%s))",
5520 LDAP_OBJ_POSIXACCOUNT,
5521 LDAP_OBJ_SAMBASAMACCOUNT);
5523 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5524 if (rc != LDAP_SUCCESS) {
5525 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5526 return NT_STATUS_UNSUCCESSFUL;
5528 talloc_autofree_ldapmsg(tmp_ctx, result);
5530 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5532 if (num_result != 0) {
5533 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5534 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5537 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5538 if (rc != LDAP_SUCCESS) {
5539 return NT_STATUS_UNSUCCESSFUL;
5542 return NT_STATUS_OK;
5545 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5546 TALLOC_CTX *tmp_ctx,
5551 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5552 LDAPMessage *entry = NULL;
5553 LDAPMessage *result = NULL;
5555 LDAPMod **mods = NULL;
5558 const char *dn = NULL;
5565 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5567 case LDAP_MOD_DELETE:
5568 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5571 return NT_STATUS_UNSUCCESSFUL;
5574 /* get member sid */
5575 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5577 /* get the group sid */
5578 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5580 filter = talloc_asprintf(tmp_ctx,
5583 "(objectClass=%s))",
5584 sid_string_talloc(tmp_ctx, &member_sid),
5585 LDAP_OBJ_POSIXACCOUNT,
5586 LDAP_OBJ_SAMBASAMACCOUNT);
5587 if (filter == NULL) {
5588 return NT_STATUS_NO_MEMORY;
5591 /* get the member uid */
5592 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5593 if (rc != LDAP_SUCCESS) {
5594 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5595 return NT_STATUS_UNSUCCESSFUL;
5597 talloc_autofree_ldapmsg(tmp_ctx, result);
5599 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5601 if (num_result == 0) {
5602 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5603 return NT_STATUS_NO_SUCH_MEMBER;
5606 if (num_result > 1) {
5607 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5608 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5611 entry = ldap_first_entry(priv2ld(ldap_state), result);
5613 return NT_STATUS_UNSUCCESSFUL;
5616 if (modop == LDAP_MOD_DELETE) {
5617 /* check if we are trying to remove the member from his primary group */
5619 gid_t user_gid, group_gid;
5621 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5623 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5624 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5627 user_gid = strtoul(gidstr, NULL, 10);
5629 if (!sid_to_gid(&group_sid, &group_gid)) {
5630 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5631 return NT_STATUS_UNSUCCESSFUL;
5634 if (user_gid == group_gid) {
5635 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5636 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5640 /* here it is, retrieve the uid for later use */
5641 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5643 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5644 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5647 filter = talloc_asprintf(tmp_ctx,
5650 "(objectClass=%s))",
5651 sid_string_talloc(tmp_ctx, &group_sid),
5652 LDAP_OBJ_POSIXGROUP,
5656 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5657 if (rc != LDAP_SUCCESS) {
5658 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5659 return NT_STATUS_UNSUCCESSFUL;
5661 talloc_autofree_ldapmsg(tmp_ctx, result);
5663 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5665 if (num_result == 0) {
5666 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5667 return NT_STATUS_NO_SUCH_GROUP;
5670 if (num_result > 1) {
5671 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5672 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5675 entry = ldap_first_entry(priv2ld(ldap_state), result);
5677 return NT_STATUS_UNSUCCESSFUL;
5680 /* here it is, retrieve the dn for later use */
5681 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5683 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5684 return NT_STATUS_NO_MEMORY;
5687 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5689 talloc_autofree_ldapmod(tmp_ctx, mods);
5691 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5692 if (rc != LDAP_SUCCESS) {
5693 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5694 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5695 return NT_STATUS_MEMBER_IN_GROUP;
5697 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5698 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5699 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5701 return NT_STATUS_UNSUCCESSFUL;
5704 return NT_STATUS_OK;
5707 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5708 TALLOC_CTX *tmp_ctx,
5712 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5714 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5715 TALLOC_CTX *tmp_ctx,
5719 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5722 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5723 TALLOC_CTX *mem_ctx,
5724 struct samu *sampass)
5726 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5727 LDAPMessage *entry = NULL;
5728 LDAPMessage *result = NULL;
5730 LDAPMod **mods = NULL;
5732 char *escape_username;
5734 const char *dn = NULL;
5738 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5740 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5741 DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
5742 return NT_STATUS_UNSUCCESSFUL;
5744 gidstr = talloc_asprintf(mem_ctx, "%d", gid);
5746 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5747 return NT_STATUS_NO_MEMORY;
5750 escape_username = escape_ldap_string_alloc(pdb_get_username(sampass));
5751 if (escape_username== NULL) {
5752 return NT_STATUS_NO_MEMORY;
5755 filter = talloc_asprintf(mem_ctx,
5758 "(objectClass=%s))",
5760 LDAP_OBJ_POSIXACCOUNT,
5761 LDAP_OBJ_SAMBASAMACCOUNT);
5763 SAFE_FREE(escape_username);
5765 if (filter == NULL) {
5766 return NT_STATUS_NO_MEMORY;
5769 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5770 if (rc != LDAP_SUCCESS) {
5771 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5772 return NT_STATUS_UNSUCCESSFUL;
5774 talloc_autofree_ldapmsg(mem_ctx, result);
5776 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5778 if (num_result == 0) {
5779 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5780 return NT_STATUS_NO_SUCH_USER;
5783 if (num_result > 1) {
5784 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
5785 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5788 entry = ldap_first_entry(priv2ld(ldap_state), result);
5790 return NT_STATUS_UNSUCCESSFUL;
5793 /* retrieve the dn for later use */
5794 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
5796 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5797 return NT_STATUS_NO_MEMORY;
5800 /* remove the old one, and add the new one, this way we do not risk races */
5801 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
5804 return NT_STATUS_OK;
5807 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5809 if (rc != LDAP_SUCCESS) {
5810 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5811 pdb_get_username(sampass), gidstr));
5812 return NT_STATUS_UNSUCCESSFUL;
5815 flush_pwnam_cache();
5817 return NT_STATUS_OK;
5821 /**********************************************************************
5822 trusted domains functions
5823 *********************************************************************/
5825 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
5828 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
5829 ldap_state->domain_dn);
5832 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
5833 const char *domain, LDAPMessage **entry)
5837 int scope = LDAP_SCOPE_SUBTREE;
5838 const char **attrs = NULL; /* NULL: get all attrs */
5839 int attrsonly = 0; /* 0: return values too */
5840 LDAPMessage *result = NULL;
5844 filter = talloc_asprintf(talloc_tos(),
5845 "(&(objectClass=%s)(sambaDomainName=%s))",
5846 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
5848 trusted_dn = trusteddom_dn(ldap_state, domain);
5849 if (trusted_dn == NULL) {
5852 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
5853 filter, attrs, attrsonly, &result);
5855 if (rc == LDAP_NO_SUCH_OBJECT) {
5860 if (rc != LDAP_SUCCESS) {
5864 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5866 if (num_result > 1) {
5867 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
5868 "sambaTrustedDomainPassword object for domain '%s'"
5873 if (num_result == 0) {
5874 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
5875 "sambaTrustedDomainPassword object for domain %s.\n",
5879 *entry = ldap_first_entry(priv2ld(ldap_state), result);
5885 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
5889 time_t *pass_last_set_time)
5891 struct ldapsam_privates *ldap_state =
5892 (struct ldapsam_privates *)methods->private_data;
5893 LDAPMessage *entry = NULL;
5895 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
5897 if (!get_trusteddom_pw_int(ldap_state, domain, &entry) ||
5906 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5907 entry, "sambaClearTextPassword", talloc_tos());
5908 if (pwd_str == NULL) {
5911 /* trusteddom_pw routines do not use talloc yet... */
5912 *pwd = SMB_STRDUP(pwd_str);
5918 /* last change time */
5919 if (pass_last_set_time != NULL) {
5921 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5922 entry, "sambaPwdLastSet", talloc_tos());
5923 if (time_str == NULL) {
5926 *pass_last_set_time = (time_t)atol(time_str);
5933 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5936 if (sid_str == NULL) {
5939 dom_sid = string_sid_talloc(talloc_tos(), sid_str);
5940 if (dom_sid == NULL) {
5943 sid_copy(sid, dom_sid);
5949 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
5954 struct ldapsam_privates *ldap_state =
5955 (struct ldapsam_privates *)methods->private_data;
5956 LDAPMessage *entry = NULL;
5957 LDAPMod **mods = NULL;
5958 char *prev_pwd = NULL;
5959 char *trusted_dn = NULL;
5962 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
5965 * get the current entry (if there is one) in order to put the
5966 * current password into the previous password attribute
5968 if (!get_trusteddom_pw_int(ldap_state, domain, &entry)) {
5973 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
5974 "sambaTrustedDomainPassword");
5975 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
5977 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
5978 sid_string_tos(sid));
5979 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
5980 talloc_asprintf(talloc_tos(), "%li", time(NULL)));
5981 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5982 "sambaClearTextPassword", pwd);
5983 if (entry != NULL) {
5984 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
5985 entry, "sambaClearTextPassword", talloc_tos());
5986 if (prev_pwd != NULL) {
5987 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
5988 "sambaPreviousClearTextPassword",
5993 trusted_dn = trusteddom_dn(ldap_state, domain);
5994 if (trusted_dn == NULL) {
5997 if (entry == NULL) {
5998 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6000 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6003 if (rc != LDAP_SUCCESS) {
6004 DEBUG(1, ("error writing trusted domain password!\n"));
6011 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6015 struct ldapsam_privates *ldap_state =
6016 (struct ldapsam_privates *)methods->private_data;
6017 LDAPMessage *entry = NULL;
6018 const char *trusted_dn;
6020 if (!get_trusteddom_pw_int(ldap_state, domain, &entry)) {
6024 if (entry == NULL) {
6025 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6030 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6032 if (trusted_dn == NULL) {
6033 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6037 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6038 if (rc != LDAP_SUCCESS) {
6045 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6046 TALLOC_CTX *mem_ctx,
6047 uint32 *num_domains,
6048 struct trustdom_info ***domains)
6051 struct ldapsam_privates *ldap_state =
6052 (struct ldapsam_privates *)methods->private_data;
6054 int scope = LDAP_SCOPE_SUBTREE;
6055 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6056 int attrsonly = 0; /* 0: return values too */
6057 LDAPMessage *result = NULL;
6058 LDAPMessage *entry = NULL;
6060 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6061 LDAP_OBJ_TRUSTDOM_PASSWORD);
6063 rc = smbldap_search(ldap_state->smbldap_state,
6064 ldap_state->domain_dn,
6071 if (rc != LDAP_SUCCESS) {
6072 return NT_STATUS_UNSUCCESSFUL;
6076 if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
6077 DEBUG(1, ("talloc failed\n"));
6078 return NT_STATUS_NO_MEMORY;
6081 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6083 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6085 char *dom_name, *dom_sid_str;
6086 struct trustdom_info *dom_info;
6088 dom_info = TALLOC_P(*domains, struct trustdom_info);
6089 if (dom_info == NULL) {
6090 DEBUG(1, ("talloc failed\n"));
6091 return NT_STATUS_NO_MEMORY;
6094 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6098 if (dom_name == NULL) {
6099 DEBUG(1, ("talloc failed\n"));
6100 return NT_STATUS_NO_MEMORY;
6102 dom_info->name = dom_name;
6104 dom_sid_str = smbldap_talloc_single_attribute(
6105 priv2ld(ldap_state), entry, "sambaSID",
6107 if (dom_sid_str == NULL) {
6108 DEBUG(1, ("talloc failed\n"));
6109 return NT_STATUS_NO_MEMORY;
6111 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6112 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6114 return NT_STATUS_UNSUCCESSFUL;
6117 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6118 domains, num_domains);
6120 if (*domains == NULL) {
6121 DEBUG(1, ("talloc failed\n"));
6122 return NT_STATUS_NO_MEMORY;
6126 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6127 return NT_STATUS_OK;
6131 /**********************************************************************
6133 *********************************************************************/
6135 static void free_private_data(void **vp)
6137 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6139 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6141 if ((*ldap_state)->result != NULL) {
6142 ldap_msgfree((*ldap_state)->result);
6143 (*ldap_state)->result = NULL;
6145 if ((*ldap_state)->domain_dn != NULL) {
6146 SAFE_FREE((*ldap_state)->domain_dn);
6151 /* No need to free any further, as it is talloc()ed */
6154 /*********************************************************************
6155 Intitalise the parts of the pdb_methods structure that are common to
6157 *********************************************************************/
6159 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6162 struct ldapsam_privates *ldap_state;
6164 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6168 (*pdb_method)->name = "ldapsam";
6170 (*pdb_method)->setsampwent = ldapsam_setsampwent;
6171 (*pdb_method)->endsampwent = ldapsam_endsampwent;
6172 (*pdb_method)->getsampwent = ldapsam_getsampwent;
6173 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6174 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6175 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6176 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6177 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6178 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6180 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6181 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6182 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6183 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6184 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6185 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6186 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6188 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6189 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6191 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6193 (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
6194 (*pdb_method)->new_rid = ldapsam_new_rid;
6196 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6197 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6198 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6199 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6201 /* TODO: Setup private data and free */
6203 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
6204 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6205 return NT_STATUS_NO_MEMORY;
6208 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6209 location, &ldap_state->smbldap_state);
6211 if ( !NT_STATUS_IS_OK(nt_status) ) {
6215 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6216 return NT_STATUS_NO_MEMORY;
6219 (*pdb_method)->private_data = ldap_state;
6221 (*pdb_method)->free_private_data = free_private_data;
6223 return NT_STATUS_OK;
6226 /**********************************************************************
6227 Initialise the 'compat' mode for pdb_ldap
6228 *********************************************************************/
6230 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6233 struct ldapsam_privates *ldap_state;
6234 char *uri = talloc_strdup( NULL, location );
6236 trim_char( uri, '\"', '\"' );
6237 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6241 if ( !NT_STATUS_IS_OK(nt_status) ) {
6245 (*pdb_method)->name = "ldapsam_compat";
6247 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6248 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6250 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6252 return NT_STATUS_OK;
6255 /**********************************************************************
6256 Initialise the normal mode for pdb_ldap
6257 *********************************************************************/
6259 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6262 struct ldapsam_privates *ldap_state = NULL;
6263 uint32 alg_rid_base;
6264 char *alg_rid_base_string = NULL;
6265 LDAPMessage *result = NULL;
6266 LDAPMessage *entry = NULL;
6267 DOM_SID ldap_domain_sid;
6268 DOM_SID secrets_domain_sid;
6269 char *domain_sid_string = NULL;
6271 char *uri = talloc_strdup( NULL, location );
6273 trim_char( uri, '\"', '\"' );
6274 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6279 if (!NT_STATUS_IS_OK(nt_status)) {
6283 (*pdb_method)->name = "ldapsam";
6285 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6286 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6287 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6288 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6289 (*pdb_method)->search_users = ldapsam_search_users;
6290 (*pdb_method)->search_groups = ldapsam_search_groups;
6291 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6293 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6294 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6295 (*pdb_method)->enum_group_memberships =
6296 ldapsam_enum_group_memberships;
6297 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6298 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6300 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6301 (*pdb_method)->create_user = ldapsam_create_user;
6302 (*pdb_method)->delete_user = ldapsam_delete_user;
6303 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6304 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6305 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6306 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6307 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6311 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6312 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6314 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6316 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6318 ldap_state->domain_name, True);
6320 if ( !NT_STATUS_IS_OK(nt_status) ) {
6321 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6322 "info, nor add one to the domain\n"));
6323 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6324 "will be unable to allocate new users/groups, "
6325 "and will risk BDCs having inconsistant SIDs\n"));
6326 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6327 return NT_STATUS_OK;
6330 /* Given that the above might fail, everything below this must be
6333 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6336 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6338 ldap_msgfree(result);
6339 return NT_STATUS_UNSUCCESSFUL;
6342 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
6344 ldap_msgfree(result);
6345 return NT_STATUS_UNSUCCESSFUL;
6348 ldap_state->domain_dn = smb_xstrdup(dn);
6351 domain_sid_string = smbldap_talloc_single_attribute(
6352 ldap_state->smbldap_state->ldap_struct,
6354 get_userattr_key2string(ldap_state->schema_ver,
6355 LDAP_ATTR_USER_SID),
6358 if (domain_sid_string) {
6360 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6361 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6362 "read as a valid SID\n", domain_sid_string));
6363 ldap_msgfree(result);
6364 TALLOC_FREE(domain_sid_string);
6365 return NT_STATUS_INVALID_PARAMETER;
6367 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6368 &secrets_domain_sid);
6369 if (!found_sid || !sid_equal(&secrets_domain_sid,
6370 &ldap_domain_sid)) {
6371 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6372 "%s based on pdb_ldap results %s -> %s\n",
6373 ldap_state->domain_name,
6374 sid_string_dbg(&secrets_domain_sid),
6375 sid_string_dbg(&ldap_domain_sid)));
6377 /* reset secrets.tdb sid */
6378 secrets_store_domain_sid(ldap_state->domain_name,
6380 DEBUG(1, ("New global sam SID: %s\n",
6381 sid_string_dbg(get_global_sam_sid())));
6383 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6384 TALLOC_FREE(domain_sid_string);
6387 alg_rid_base_string = smbldap_talloc_single_attribute(
6388 ldap_state->smbldap_state->ldap_struct,
6390 get_attr_key2string( dominfo_attr_list,
6391 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6393 if (alg_rid_base_string) {
6394 alg_rid_base = (uint32)atol(alg_rid_base_string);
6395 if (alg_rid_base != algorithmic_rid_base()) {
6396 DEBUG(0, ("The value of 'algorithmic RID base' has "
6397 "changed since the LDAP\n"
6398 "database was initialised. Aborting. \n"));
6399 ldap_msgfree(result);
6400 TALLOC_FREE(alg_rid_base_string);
6401 return NT_STATUS_UNSUCCESSFUL;
6403 TALLOC_FREE(alg_rid_base_string);
6405 ldap_msgfree(result);
6407 return NT_STATUS_OK;
6410 NTSTATUS pdb_ldap_init(void)
6413 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6416 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6419 /* Let pdb_nds register backends */
6422 return NT_STATUS_OK;