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
47 #include "../libcli/auth/libcli_auth.h"
50 #define DBGC_CLASS DBGC_PASSDB
56 * Work around versions of the LDAP client libs that don't have the OIDs
57 * defined, or have them defined under the old name.
58 * This functionality is really a factor of the server, not the client
62 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
63 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
64 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
65 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
68 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
69 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
70 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
71 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
74 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
75 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
76 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
77 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
83 /**********************************************************************
84 Simple helper function to make stuff better readable
85 **********************************************************************/
87 static LDAP *priv2ld(struct ldapsam_privates *priv)
89 return priv->smbldap_state->ldap_struct;
92 /**********************************************************************
93 Get the attribute name given a user schame version.
94 **********************************************************************/
96 static const char* get_userattr_key2string( int schema_ver, int key )
98 switch ( schema_ver ) {
99 case SCHEMAVER_SAMBAACCOUNT:
100 return get_attr_key2string( attrib_map_v22, key );
102 case SCHEMAVER_SAMBASAMACCOUNT:
103 return get_attr_key2string( attrib_map_v30, key );
106 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
112 /**********************************************************************
113 Return the list of attribute names given a user schema version.
114 **********************************************************************/
116 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
118 switch ( schema_ver ) {
119 case SCHEMAVER_SAMBAACCOUNT:
120 return get_attr_list( mem_ctx, attrib_map_v22 );
122 case SCHEMAVER_SAMBASAMACCOUNT:
123 return get_attr_list( mem_ctx, attrib_map_v30 );
125 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
132 /**************************************************************************
133 Return the list of attribute names to delete given a user schema version.
134 **************************************************************************/
136 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
139 switch ( schema_ver ) {
140 case SCHEMAVER_SAMBAACCOUNT:
141 return get_attr_list( mem_ctx,
142 attrib_map_to_delete_v22 );
144 case SCHEMAVER_SAMBASAMACCOUNT:
145 return get_attr_list( mem_ctx,
146 attrib_map_to_delete_v30 );
148 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
156 /*******************************************************************
157 Generate the LDAP search filter for the objectclass based on the
158 version of the schema we are using.
159 ******************************************************************/
161 static const char* get_objclass_filter( int schema_ver )
163 fstring objclass_filter;
166 switch( schema_ver ) {
167 case SCHEMAVER_SAMBAACCOUNT:
168 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
170 case SCHEMAVER_SAMBASAMACCOUNT:
171 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
174 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
175 objclass_filter[0] = '\0';
179 result = talloc_strdup(talloc_tos(), objclass_filter);
180 SMB_ASSERT(result != NULL);
184 /*****************************************************************
185 Scan a sequence number off OpenLDAP's syncrepl contextCSN
186 ******************************************************************/
188 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
190 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
191 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
192 LDAPMessage *msg = NULL;
193 LDAPMessage *entry = NULL;
195 char **values = NULL;
196 int rc, num_result, num_values, rid;
202 /* Unfortunatly there is no proper way to detect syncrepl-support in
203 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
204 * but do not show up in the root-DSE yet. Neither we can query the
205 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
206 * objectclass. Currently we require lp_ldap_suffix() to show up as
207 * namingContext. - Guenther
210 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
215 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
219 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
220 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
221 "as top-level namingContext\n", lp_ldap_suffix()));
225 mem_ctx = talloc_init("ldapsam_get_seq_num");
228 return NT_STATUS_NO_MEMORY;
230 if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
231 ntstatus = NT_STATUS_NO_MEMORY;
235 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
236 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
239 /* consumer syncreplCookie: */
240 /* csn=20050126161620Z#0000001#00#00000 */
241 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
243 suffix = talloc_asprintf(mem_ctx,
244 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
246 ntstatus = NT_STATUS_NO_MEMORY;
251 /* provider contextCSN */
252 /* 20050126161620Z#000009#00#000000 */
253 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
255 suffix = talloc_asprintf(mem_ctx,
256 "cn=ldapsync,%s", lp_ldap_suffix());
259 ntstatus = NT_STATUS_NO_MEMORY;
264 rc = smbldap_search(ldap_state->smbldap_state, suffix,
265 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
267 if (rc != LDAP_SUCCESS) {
271 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
272 if (num_result != 1) {
273 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
277 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
279 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
283 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
284 if (values == NULL) {
285 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
289 num_values = ldap_count_values(values);
290 if (num_values == 0) {
291 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
296 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
297 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
302 if (!strncmp(p, "csn=", strlen("csn=")))
305 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
307 *seq_num = generalized_to_unix_time(p);
309 /* very basic sanity check */
311 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
316 ntstatus = NT_STATUS_OK;
320 ldap_value_free(values);
324 talloc_destroy(mem_ctx);
329 /*******************************************************************
330 Run the search by name.
331 ******************************************************************/
333 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
335 LDAPMessage ** result,
339 char *escape_user = escape_ldap_string(talloc_tos(), user);
343 return LDAP_NO_MEMORY;
347 * in the filter expression, replace %u with the real name
348 * so in ldap filter, %u MUST exist :-)
350 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
351 get_objclass_filter(ldap_state->schema_ver));
353 TALLOC_FREE(escape_user);
354 return LDAP_NO_MEMORY;
357 * have to use this here because $ is filtered out
361 filter = talloc_all_string_sub(talloc_tos(),
362 filter, "%u", escape_user);
363 TALLOC_FREE(escape_user);
365 return LDAP_NO_MEMORY;
368 ret = smbldap_search_suffix(ldap_state->smbldap_state,
369 filter, attr, result);
374 /*******************************************************************
375 Run the search by rid.
376 ******************************************************************/
378 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
379 uint32 rid, LDAPMessage ** result,
385 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
386 get_objclass_filter(ldap_state->schema_ver));
388 return LDAP_NO_MEMORY;
391 rc = smbldap_search_suffix(ldap_state->smbldap_state,
392 filter, attr, result);
397 /*******************************************************************
398 Run the search by SID.
399 ******************************************************************/
401 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
402 const DOM_SID *sid, LDAPMessage ** result,
409 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
410 get_userattr_key2string(ldap_state->schema_ver,
412 sid_to_fstring(sid_string, sid),
413 get_objclass_filter(ldap_state->schema_ver));
415 return LDAP_NO_MEMORY;
418 rc = smbldap_search_suffix(ldap_state->smbldap_state,
419 filter, attr, result);
425 /*******************************************************************
426 Delete complete object or objectclass and attrs from
427 object found in search_result depending on lp_ldap_delete_dn
428 ******************************************************************/
430 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
433 const char *objectclass,
436 LDAPMod **mods = NULL;
439 BerElement *ptr = NULL;
441 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
443 return LDAP_NO_MEMORY;
446 if (lp_ldap_delete_dn()) {
447 return smbldap_delete(priv->smbldap_state, dn);
450 /* Ok, delete only the SAM attributes */
452 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
454 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
457 /* We are only allowed to delete the attributes that
460 for (attrib = attrs; *attrib != NULL; attrib++) {
461 if (strequal(*attrib, name)) {
462 DEBUG(10, ("ldapsam_delete_entry: deleting "
463 "attribute %s\n", name));
464 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
475 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
476 talloc_autofree_ldapmod(mem_ctx, mods);
478 return smbldap_modify(priv->smbldap_state, dn, mods);
481 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
486 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
487 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
493 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
494 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
504 /**********************************************************************
505 Initialize struct samu from an LDAP query.
506 (Based on init_sam_from_buffer in pdb_tdb.c)
507 *********************************************************************/
509 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
510 struct samu * sampass,
517 pass_can_change_time,
518 pass_must_change_time,
521 char *username = NULL,
527 *logon_script = NULL,
528 *profile_path = NULL,
530 *workstations = NULL,
533 uint8 smblmpwd[LM_HASH_LEN],
534 smbntpwd[NT_HASH_LEN];
535 bool use_samba_attrs = True;
536 uint32 acct_ctrl = 0;
538 uint16 bad_password_count = 0,
541 uint8 hours[MAX_HOURS_LEN];
543 LOGIN_CACHE *cache_entry = NULL;
545 bool expand_explicit = lp_passdb_expand_explicit();
547 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
552 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
553 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
557 if (priv2ld(ldap_state) == NULL) {
558 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
559 "ldap_struct is NULL!\n"));
563 if (!(username = smbldap_talloc_smallest_attribute(priv2ld(ldap_state),
567 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
572 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
574 nt_username = talloc_strdup(ctx, username);
579 domain = talloc_strdup(ctx, ldap_state->domain_name);
584 pdb_set_username(sampass, username, PDB_SET);
586 pdb_set_domain(sampass, domain, PDB_DEFAULT);
587 pdb_set_nt_username(sampass, nt_username, PDB_SET);
589 /* deal with different attributes between the schema first */
591 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
592 if ((temp = smbldap_talloc_single_attribute(
593 ldap_state->smbldap_state->ldap_struct,
595 get_userattr_key2string(ldap_state->schema_ver,
598 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
601 if ((temp = smbldap_talloc_single_attribute(
602 ldap_state->smbldap_state->ldap_struct,
604 get_userattr_key2string(ldap_state->schema_ver,
607 user_rid = (uint32)atol(temp);
608 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
612 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
613 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
614 get_userattr_key2string(ldap_state->schema_ver,
616 get_userattr_key2string(ldap_state->schema_ver,
622 temp = smbldap_talloc_single_attribute(
623 ldap_state->smbldap_state->ldap_struct,
625 get_userattr_key2string(ldap_state->schema_ver,
626 LDAP_ATTR_PWD_LAST_SET),
629 pass_last_set_time = (time_t) atol(temp);
630 pdb_set_pass_last_set_time(sampass,
631 pass_last_set_time, PDB_SET);
634 temp = smbldap_talloc_single_attribute(
635 ldap_state->smbldap_state->ldap_struct,
637 get_userattr_key2string(ldap_state->schema_ver,
638 LDAP_ATTR_LOGON_TIME),
641 logon_time = (time_t) atol(temp);
642 pdb_set_logon_time(sampass, logon_time, PDB_SET);
645 temp = smbldap_talloc_single_attribute(
646 ldap_state->smbldap_state->ldap_struct,
648 get_userattr_key2string(ldap_state->schema_ver,
649 LDAP_ATTR_LOGOFF_TIME),
652 logoff_time = (time_t) atol(temp);
653 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
656 temp = smbldap_talloc_single_attribute(
657 ldap_state->smbldap_state->ldap_struct,
659 get_userattr_key2string(ldap_state->schema_ver,
660 LDAP_ATTR_KICKOFF_TIME),
663 kickoff_time = (time_t) atol(temp);
664 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
667 temp = smbldap_talloc_single_attribute(
668 ldap_state->smbldap_state->ldap_struct,
670 get_userattr_key2string(ldap_state->schema_ver,
671 LDAP_ATTR_PWD_CAN_CHANGE),
674 pass_can_change_time = (time_t) atol(temp);
675 pdb_set_pass_can_change_time(sampass,
676 pass_can_change_time, PDB_SET);
679 temp = smbldap_talloc_single_attribute(
680 ldap_state->smbldap_state->ldap_struct,
682 get_userattr_key2string(ldap_state->schema_ver,
683 LDAP_ATTR_PWD_MUST_CHANGE),
686 pass_must_change_time = (time_t) atol(temp);
687 pdb_set_pass_must_change_time(sampass,
688 pass_must_change_time, PDB_SET);
691 /* recommend that 'gecos' and 'displayName' should refer to the same
692 * attribute OID. userFullName depreciated, only used by Samba
693 * primary rules of LDAP: don't make a new attribute when one is already defined
694 * that fits your needs; using cn then displayName rather than 'userFullName'
697 fullname = smbldap_talloc_single_attribute(
698 ldap_state->smbldap_state->ldap_struct,
700 get_userattr_key2string(ldap_state->schema_ver,
701 LDAP_ATTR_DISPLAY_NAME),
704 pdb_set_fullname(sampass, fullname, PDB_SET);
706 fullname = smbldap_talloc_single_attribute(
707 ldap_state->smbldap_state->ldap_struct,
709 get_userattr_key2string(ldap_state->schema_ver,
713 pdb_set_fullname(sampass, fullname, PDB_SET);
717 dir_drive = smbldap_talloc_single_attribute(
718 ldap_state->smbldap_state->ldap_struct,
720 get_userattr_key2string(ldap_state->schema_ver,
721 LDAP_ATTR_HOME_DRIVE),
724 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
726 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
729 homedir = smbldap_talloc_single_attribute(
730 ldap_state->smbldap_state->ldap_struct,
732 get_userattr_key2string(ldap_state->schema_ver,
733 LDAP_ATTR_HOME_PATH),
736 if (expand_explicit) {
737 homedir = talloc_sub_basic(ctx,
745 pdb_set_homedir(sampass, homedir, PDB_SET);
747 pdb_set_homedir(sampass,
748 talloc_sub_basic(ctx, username, domain,
753 logon_script = smbldap_talloc_single_attribute(
754 ldap_state->smbldap_state->ldap_struct,
756 get_userattr_key2string(ldap_state->schema_ver,
757 LDAP_ATTR_LOGON_SCRIPT),
760 if (expand_explicit) {
761 logon_script = talloc_sub_basic(ctx,
769 pdb_set_logon_script(sampass, logon_script, PDB_SET);
771 pdb_set_logon_script(sampass,
772 talloc_sub_basic(ctx, username, domain,
777 profile_path = smbldap_talloc_single_attribute(
778 ldap_state->smbldap_state->ldap_struct,
780 get_userattr_key2string(ldap_state->schema_ver,
781 LDAP_ATTR_PROFILE_PATH),
784 if (expand_explicit) {
785 profile_path = talloc_sub_basic(ctx,
793 pdb_set_profile_path(sampass, profile_path, PDB_SET);
795 pdb_set_profile_path(sampass,
796 talloc_sub_basic(ctx, username, domain,
801 acct_desc = smbldap_talloc_single_attribute(
802 ldap_state->smbldap_state->ldap_struct,
804 get_userattr_key2string(ldap_state->schema_ver,
808 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
811 workstations = smbldap_talloc_single_attribute(
812 ldap_state->smbldap_state->ldap_struct,
814 get_userattr_key2string(ldap_state->schema_ver,
818 pdb_set_workstations(sampass, workstations, PDB_SET);
821 munged_dial = smbldap_talloc_single_attribute(
822 ldap_state->smbldap_state->ldap_struct,
824 get_userattr_key2string(ldap_state->schema_ver,
825 LDAP_ATTR_MUNGED_DIAL),
828 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
831 /* FIXME: hours stuff should be cleaner */
835 memset(hours, 0xff, hours_len);
837 if (ldap_state->is_nds_ldap) {
840 char clear_text_pw[512];
842 /* Make call to Novell eDirectory ldap extension to get clear text password.
843 NOTE: This will only work if we have an SSL connection to eDirectory. */
844 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
845 if (user_dn != NULL) {
846 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
848 pwd_len = sizeof(clear_text_pw);
849 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
850 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
851 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
852 TALLOC_FREE(user_dn);
855 ZERO_STRUCT(smblmpwd);
856 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
857 TALLOC_FREE(user_dn);
860 ZERO_STRUCT(smbntpwd);
861 use_samba_attrs = False;
864 TALLOC_FREE(user_dn);
867 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
871 if (use_samba_attrs) {
872 temp = smbldap_talloc_single_attribute(
873 ldap_state->smbldap_state->ldap_struct,
875 get_userattr_key2string(ldap_state->schema_ver,
879 pdb_gethexpwd(temp, smblmpwd);
880 memset((char *)temp, '\0', strlen(temp)+1);
881 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
884 ZERO_STRUCT(smblmpwd);
887 temp = smbldap_talloc_single_attribute(
888 ldap_state->smbldap_state->ldap_struct,
890 get_userattr_key2string(ldap_state->schema_ver,
894 pdb_gethexpwd(temp, smbntpwd);
895 memset((char *)temp, '\0', strlen(temp)+1);
896 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
899 ZERO_STRUCT(smbntpwd);
905 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
907 uint8 *pwhist = NULL;
909 char *history_string = TALLOC_ARRAY(ctx, char,
910 MAX_PW_HISTORY_LEN*64);
912 if (!history_string) {
916 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
918 if ((pwhist = TALLOC_ARRAY(ctx, uint8,
919 pwHistLen * PW_HISTORY_ENTRY_LEN)) ==
921 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
924 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
926 if (smbldap_get_single_attribute(
927 ldap_state->smbldap_state->ldap_struct,
929 get_userattr_key2string(ldap_state->schema_ver,
930 LDAP_ATTR_PWD_HISTORY),
932 MAX_PW_HISTORY_LEN*64)) {
933 bool hex_failed = false;
934 for (i = 0; i < pwHistLen; i++){
935 /* Get the 16 byte salt. */
936 if (!pdb_gethexpwd(&history_string[i*64],
937 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
941 /* Get the 16 byte MD5 hash of salt+passwd. */
942 if (!pdb_gethexpwd(&history_string[(i*64)+32],
943 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
944 PW_HISTORY_SALT_LEN])) {
950 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
952 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
955 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
960 temp = smbldap_talloc_single_attribute(
961 ldap_state->smbldap_state->ldap_struct,
963 get_userattr_key2string(ldap_state->schema_ver,
967 acct_ctrl = pdb_decode_acct_ctrl(temp);
969 if (acct_ctrl == 0) {
970 acct_ctrl |= ACB_NORMAL;
973 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
975 acct_ctrl |= ACB_NORMAL;
978 pdb_set_hours_len(sampass, hours_len, PDB_SET);
979 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
981 temp = smbldap_talloc_single_attribute(
982 ldap_state->smbldap_state->ldap_struct,
984 get_userattr_key2string(ldap_state->schema_ver,
985 LDAP_ATTR_BAD_PASSWORD_COUNT),
988 bad_password_count = (uint32) atol(temp);
989 pdb_set_bad_password_count(sampass,
990 bad_password_count, PDB_SET);
993 temp = smbldap_talloc_single_attribute(
994 ldap_state->smbldap_state->ldap_struct,
996 get_userattr_key2string(ldap_state->schema_ver,
997 LDAP_ATTR_BAD_PASSWORD_TIME),
1000 bad_password_time = (time_t) atol(temp);
1001 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
1005 temp = smbldap_talloc_single_attribute(
1006 ldap_state->smbldap_state->ldap_struct,
1008 get_userattr_key2string(ldap_state->schema_ver,
1009 LDAP_ATTR_LOGON_COUNT),
1012 logon_count = (uint32) atol(temp);
1013 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1016 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1018 temp = smbldap_talloc_single_attribute(
1019 ldap_state->smbldap_state->ldap_struct,
1021 get_userattr_key2string(ldap_state->schema_ver,
1022 LDAP_ATTR_LOGON_HOURS),
1025 pdb_gethexhours(temp, hours);
1026 memset((char *)temp, '\0', strlen(temp) +1);
1027 pdb_set_hours(sampass, hours, PDB_SET);
1031 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1032 temp = smbldap_talloc_single_attribute(
1033 priv2ld(ldap_state),
1038 /* We've got a uid, feed the cache */
1039 uid_t uid = strtoul(temp, NULL, 10);
1040 store_uid_sid_cache(pdb_get_user_sid(sampass), uid);
1041 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass), uid);
1045 /* check the timestamp of the cache vs ldap entry */
1046 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1052 /* see if we have newer updates */
1053 if (!(cache_entry = login_cache_read(sampass))) {
1054 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1055 (unsigned int)pdb_get_bad_password_count(sampass),
1056 (unsigned int)pdb_get_bad_password_time(sampass)));
1061 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1062 (unsigned int)ldap_entry_time,
1063 (unsigned int)cache_entry->entry_timestamp,
1064 (unsigned int)cache_entry->bad_password_time));
1066 if (ldap_entry_time > cache_entry->entry_timestamp) {
1067 /* cache is older than directory , so
1068 we need to delete the entry but allow the
1069 fields to be written out */
1070 login_cache_delentry(sampass);
1073 pdb_set_acct_ctrl(sampass,
1074 pdb_get_acct_ctrl(sampass) |
1075 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
1077 pdb_set_bad_password_count(sampass,
1078 cache_entry->bad_password_count,
1080 pdb_set_bad_password_time(sampass,
1081 cache_entry->bad_password_time,
1090 SAFE_FREE(cache_entry);
1094 /**********************************************************************
1095 Initialize the ldap db from a struct samu. Called on update.
1096 (Based on init_buffer_from_sam in pdb_tdb.c)
1097 *********************************************************************/
1099 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1100 LDAPMessage *existing,
1101 LDAPMod *** mods, struct samu * sampass,
1102 bool (*need_update)(const struct samu *,
1108 if (mods == NULL || sampass == NULL) {
1109 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1116 * took out adding "objectclass: sambaAccount"
1117 * do this on a per-mod basis
1119 if (need_update(sampass, PDB_USERNAME)) {
1120 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1121 "uid", pdb_get_username(sampass));
1122 if (ldap_state->is_nds_ldap) {
1123 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1124 "cn", pdb_get_username(sampass));
1125 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1126 "sn", pdb_get_username(sampass));
1130 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1132 /* only update the RID if we actually need to */
1133 if (need_update(sampass, PDB_USERSID)) {
1135 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
1137 switch ( ldap_state->schema_ver ) {
1138 case SCHEMAVER_SAMBAACCOUNT:
1139 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1140 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1141 sid_string_dbg(user_sid),
1143 &ldap_state->domain_sid)));
1146 if (asprintf(&temp, "%i", rid) < 0) {
1149 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1150 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1155 case SCHEMAVER_SAMBASAMACCOUNT:
1156 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1157 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1158 sid_to_fstring(sid_string, user_sid));
1162 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1167 /* we don't need to store the primary group RID - so leaving it
1168 'free' to hang off the unix primary group makes life easier */
1170 if (need_update(sampass, PDB_GROUPSID)) {
1172 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1174 switch ( ldap_state->schema_ver ) {
1175 case SCHEMAVER_SAMBAACCOUNT:
1176 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1177 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1178 sid_string_dbg(group_sid),
1180 &ldap_state->domain_sid)));
1184 if (asprintf(&temp, "%i", rid) < 0) {
1187 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1188 get_userattr_key2string(ldap_state->schema_ver,
1189 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1193 case SCHEMAVER_SAMBASAMACCOUNT:
1194 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1195 get_userattr_key2string(ldap_state->schema_ver,
1196 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1200 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1206 /* displayName, cn, and gecos should all be the same
1207 * most easily accomplished by giving them the same OID
1208 * gecos isn't set here b/c it should be handled by the
1210 * We change displayName only and fall back to cn if
1211 * it does not exist.
1214 if (need_update(sampass, PDB_FULLNAME))
1215 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1216 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1217 pdb_get_fullname(sampass));
1219 if (need_update(sampass, PDB_ACCTDESC))
1220 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1221 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1222 pdb_get_acct_desc(sampass));
1224 if (need_update(sampass, PDB_WORKSTATIONS))
1225 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1226 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1227 pdb_get_workstations(sampass));
1229 if (need_update(sampass, PDB_MUNGEDDIAL))
1230 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1231 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1232 pdb_get_munged_dial(sampass));
1234 if (need_update(sampass, PDB_SMBHOME))
1235 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1236 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1237 pdb_get_homedir(sampass));
1239 if (need_update(sampass, PDB_DRIVE))
1240 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1241 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1242 pdb_get_dir_drive(sampass));
1244 if (need_update(sampass, PDB_LOGONSCRIPT))
1245 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1246 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1247 pdb_get_logon_script(sampass));
1249 if (need_update(sampass, PDB_PROFILE))
1250 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1251 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1252 pdb_get_profile_path(sampass));
1254 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1257 if (need_update(sampass, PDB_LOGONTIME))
1258 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1259 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1262 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1265 if (need_update(sampass, PDB_LOGOFFTIME))
1266 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1267 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1270 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1273 if (need_update(sampass, PDB_KICKOFFTIME))
1274 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1275 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1278 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1281 if (need_update(sampass, PDB_CANCHANGETIME))
1282 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1283 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1286 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1289 if (need_update(sampass, PDB_MUSTCHANGETIME))
1290 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1291 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1294 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1295 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1297 if (need_update(sampass, PDB_LMPASSWD)) {
1298 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1301 pdb_sethexpwd(pwstr, lm_pw,
1302 pdb_get_acct_ctrl(sampass));
1303 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1304 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1307 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1308 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1312 if (need_update(sampass, PDB_NTPASSWD)) {
1313 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1316 pdb_sethexpwd(pwstr, nt_pw,
1317 pdb_get_acct_ctrl(sampass));
1318 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1319 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1322 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1323 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1328 if (need_update(sampass, PDB_PWHISTORY)) {
1330 uint32 pwHistLen = 0;
1331 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1333 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1337 if (pwHistLen == 0) {
1338 /* Remove any password history from the LDAP store. */
1339 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1343 uint32 currHistLen = 0;
1344 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1345 if (pwhist != NULL) {
1346 /* We can only store (1024-1/64 password history entries. */
1347 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1348 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1349 /* Store the salt. */
1350 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1351 /* Followed by the md5 hash of salt + md4 hash */
1352 pdb_sethexpwd(&pwstr[(i*64)+32],
1353 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1354 DEBUG(100, ("pwstr=%s\n", pwstr));
1358 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1359 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1364 if (need_update(sampass, PDB_PASSLASTSET)) {
1365 if (asprintf(&temp, "%li",
1366 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1369 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1370 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1376 if (need_update(sampass, PDB_HOURS)) {
1377 const uint8 *hours = pdb_get_hours(sampass);
1380 pdb_sethexhours(hourstr, hours);
1381 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1384 get_userattr_key2string(ldap_state->schema_ver,
1385 LDAP_ATTR_LOGON_HOURS),
1390 if (need_update(sampass, PDB_ACCTCTRL))
1391 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1392 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1393 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1395 /* password lockout cache:
1396 - If we are now autolocking or clearing, we write to ldap
1397 - If we are clearing, we delete the cache entry
1398 - If the count is > 0, we update the cache
1400 This even means when autolocking, we cache, just in case the
1401 update doesn't work, and we have to cache the autolock flag */
1403 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1404 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1405 uint16 badcount = pdb_get_bad_password_count(sampass);
1406 time_t badtime = pdb_get_bad_password_time(sampass);
1408 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
1410 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1411 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1413 if ((badcount >= pol) || (badcount == 0)) {
1414 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1415 (unsigned int)badcount, (unsigned int)badtime));
1416 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1420 ldap_state->smbldap_state->ldap_struct,
1422 get_userattr_key2string(
1423 ldap_state->schema_ver,
1424 LDAP_ATTR_BAD_PASSWORD_COUNT),
1428 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1432 ldap_state->smbldap_state->ldap_struct,
1434 get_userattr_key2string(
1435 ldap_state->schema_ver,
1436 LDAP_ATTR_BAD_PASSWORD_TIME),
1440 if (badcount == 0) {
1441 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1442 login_cache_delentry(sampass);
1444 LOGIN_CACHE cache_entry;
1446 cache_entry.entry_timestamp = time(NULL);
1447 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1448 cache_entry.bad_password_count = badcount;
1449 cache_entry.bad_password_time = badtime;
1451 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1452 login_cache_write(sampass, cache_entry);
1459 /**********************************************************************
1460 End enumeration of the LDAP password list.
1461 *********************************************************************/
1463 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1465 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1466 if (ldap_state->result) {
1467 ldap_msgfree(ldap_state->result);
1468 ldap_state->result = NULL;
1472 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1473 const char *new_attr)
1477 if (new_attr == NULL) {
1481 for (i=0; (*attr_list)[i] != NULL; i++) {
1485 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1487 SMB_ASSERT((*attr_list) != NULL);
1488 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1489 (*attr_list)[i+1] = NULL;
1492 /**********************************************************************
1493 Get struct samu entry from LDAP by username.
1494 *********************************************************************/
1496 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1498 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1499 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1500 LDAPMessage *result = NULL;
1501 LDAPMessage *entry = NULL;
1503 const char ** attr_list;
1506 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1507 append_attr(user, &attr_list,
1508 get_userattr_key2string(ldap_state->schema_ver,
1509 LDAP_ATTR_MOD_TIMESTAMP));
1510 append_attr(user, &attr_list, "uidNumber");
1511 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1513 TALLOC_FREE( attr_list );
1515 if ( rc != LDAP_SUCCESS )
1516 return NT_STATUS_NO_SUCH_USER;
1518 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1521 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1522 ldap_msgfree(result);
1523 return NT_STATUS_NO_SUCH_USER;
1524 } else if (count > 1) {
1525 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1526 ldap_msgfree(result);
1527 return NT_STATUS_NO_SUCH_USER;
1530 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1532 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1533 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1534 ldap_msgfree(result);
1535 return NT_STATUS_NO_SUCH_USER;
1537 pdb_set_backend_private_data(user, result, NULL,
1538 my_methods, PDB_CHANGED);
1539 talloc_autofree_ldapmsg(user, result);
1542 ldap_msgfree(result);
1547 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1548 const DOM_SID *sid, LDAPMessage **result)
1551 const char ** attr_list;
1554 switch ( ldap_state->schema_ver ) {
1555 case SCHEMAVER_SAMBASAMACCOUNT: {
1556 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1557 if (tmp_ctx == NULL) {
1558 return LDAP_NO_MEMORY;
1561 attr_list = get_userattr_list(tmp_ctx,
1562 ldap_state->schema_ver);
1563 append_attr(tmp_ctx, &attr_list,
1564 get_userattr_key2string(
1565 ldap_state->schema_ver,
1566 LDAP_ATTR_MOD_TIMESTAMP));
1567 append_attr(tmp_ctx, &attr_list, "uidNumber");
1568 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1570 TALLOC_FREE(tmp_ctx);
1572 if ( rc != LDAP_SUCCESS )
1577 case SCHEMAVER_SAMBAACCOUNT:
1578 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1582 attr_list = get_userattr_list(NULL,
1583 ldap_state->schema_ver);
1584 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1585 TALLOC_FREE( attr_list );
1587 if ( rc != LDAP_SUCCESS )
1594 /**********************************************************************
1595 Get struct samu entry from LDAP by SID.
1596 *********************************************************************/
1598 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1600 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1601 LDAPMessage *result = NULL;
1602 LDAPMessage *entry = NULL;
1606 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1608 if (rc != LDAP_SUCCESS)
1609 return NT_STATUS_NO_SUCH_USER;
1611 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1614 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1615 "count=%d\n", sid_string_dbg(sid), count));
1616 ldap_msgfree(result);
1617 return NT_STATUS_NO_SUCH_USER;
1618 } else if (count > 1) {
1619 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1620 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1622 ldap_msgfree(result);
1623 return NT_STATUS_NO_SUCH_USER;
1626 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1628 ldap_msgfree(result);
1629 return NT_STATUS_NO_SUCH_USER;
1632 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1633 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1634 ldap_msgfree(result);
1635 return NT_STATUS_NO_SUCH_USER;
1638 pdb_set_backend_private_data(user, result, NULL,
1639 my_methods, PDB_CHANGED);
1640 talloc_autofree_ldapmsg(user, result);
1641 return NT_STATUS_OK;
1644 /********************************************************************
1645 Do the actual modification - also change a plaintext passord if
1647 **********************************************************************/
1649 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1650 struct samu *newpwd, char *dn,
1651 LDAPMod **mods, int ldap_op,
1652 bool (*need_update)(const struct samu *, enum pdb_elements))
1654 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1657 if (!newpwd || !dn) {
1658 return NT_STATUS_INVALID_PARAMETER;
1662 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1663 /* may be password change below however */
1667 if (ldap_state->is_nds_ldap) {
1668 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1672 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1676 rc = smbldap_add(ldap_state->smbldap_state,
1679 case LDAP_MOD_REPLACE:
1680 rc = smbldap_modify(ldap_state->smbldap_state,
1684 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1686 return NT_STATUS_INVALID_PARAMETER;
1689 if (rc!=LDAP_SUCCESS) {
1690 return NT_STATUS_UNSUCCESSFUL;
1694 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1695 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1696 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1697 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1700 char *retoid = NULL;
1701 struct berval *retdata = NULL;
1702 char *utf8_password;
1704 size_t converted_size;
1707 if (!ldap_state->is_nds_ldap) {
1709 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1710 LDAP_EXOP_MODIFY_PASSWD)) {
1711 DEBUG(2, ("ldap password change requested, but LDAP "
1712 "server does not support it -- ignoring\n"));
1713 return NT_STATUS_OK;
1717 if (!push_utf8_talloc(talloc_tos(), &utf8_password,
1718 pdb_get_plaintext_passwd(newpwd),
1721 return NT_STATUS_NO_MEMORY;
1724 if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
1725 TALLOC_FREE(utf8_password);
1726 return NT_STATUS_NO_MEMORY;
1729 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1730 DEBUG(0,("ber_alloc_t returns NULL\n"));
1731 TALLOC_FREE(utf8_password);
1732 TALLOC_FREE(utf8_dn);
1733 return NT_STATUS_UNSUCCESSFUL;
1736 if ((ber_printf (ber, "{") < 0) ||
1737 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1739 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1742 TALLOC_FREE(utf8_dn);
1743 TALLOC_FREE(utf8_password);
1744 return NT_STATUS_UNSUCCESSFUL;
1747 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1748 ret = ber_printf(ber, "ts}",
1749 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1752 ret = ber_printf(ber, "}");
1756 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1759 TALLOC_FREE(utf8_dn);
1760 TALLOC_FREE(utf8_password);
1761 return NT_STATUS_UNSUCCESSFUL;
1764 if ((rc = ber_flatten (ber, &bv))<0) {
1765 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1767 TALLOC_FREE(utf8_dn);
1768 TALLOC_FREE(utf8_password);
1769 return NT_STATUS_UNSUCCESSFUL;
1772 TALLOC_FREE(utf8_dn);
1773 TALLOC_FREE(utf8_password);
1776 if (!ldap_state->is_nds_ldap) {
1777 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1778 LDAP_EXOP_MODIFY_PASSWD,
1779 bv, NULL, NULL, &retoid,
1782 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1783 pdb_get_plaintext_passwd(newpwd));
1785 if (rc != LDAP_SUCCESS) {
1786 char *ld_error = NULL;
1788 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1789 DEBUG(3, ("Could not set userPassword "
1790 "attribute due to an objectClass "
1791 "violation -- ignoring\n"));
1793 return NT_STATUS_OK;
1796 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1798 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1799 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1800 SAFE_FREE(ld_error);
1802 #if defined(LDAP_CONSTRAINT_VIOLATION)
1803 if (rc == LDAP_CONSTRAINT_VIOLATION)
1804 return NT_STATUS_PASSWORD_RESTRICTION;
1806 return NT_STATUS_UNSUCCESSFUL;
1808 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1809 #ifdef DEBUG_PASSWORD
1810 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1813 ber_bvfree(retdata);
1815 ldap_memfree(retoid);
1819 return NT_STATUS_OK;
1822 /**********************************************************************
1823 Delete entry from LDAP for username.
1824 *********************************************************************/
1826 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1827 struct samu * sam_acct)
1829 struct ldapsam_privates *priv =
1830 (struct ldapsam_privates *)my_methods->private_data;
1833 LDAPMessage *msg, *entry;
1834 NTSTATUS result = NT_STATUS_NO_MEMORY;
1835 const char **attr_list;
1836 TALLOC_CTX *mem_ctx;
1839 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1840 return NT_STATUS_INVALID_PARAMETER;
1843 sname = pdb_get_username(sam_acct);
1845 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1848 mem_ctx = talloc_new(NULL);
1849 if (mem_ctx == NULL) {
1850 DEBUG(0, ("talloc_new failed\n"));
1854 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1855 if (attr_list == NULL) {
1859 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1861 if ((rc != LDAP_SUCCESS) ||
1862 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1863 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1864 DEBUG(5, ("Could not find user %s\n", sname));
1865 result = NT_STATUS_NO_SUCH_USER;
1869 rc = ldapsam_delete_entry(
1870 priv, mem_ctx, entry,
1871 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1872 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1875 result = (rc == LDAP_SUCCESS) ?
1876 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1879 TALLOC_FREE(mem_ctx);
1883 /**********************************************************************
1884 Helper function to determine for update_sam_account whether
1885 we need LDAP modification.
1886 *********************************************************************/
1888 static bool element_is_changed(const struct samu *sampass,
1889 enum pdb_elements element)
1891 return IS_SAM_CHANGED(sampass, element);
1894 /**********************************************************************
1896 *********************************************************************/
1898 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1900 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1901 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1904 LDAPMessage *result = NULL;
1905 LDAPMessage *entry = NULL;
1906 LDAPMod **mods = NULL;
1907 const char **attr_list;
1909 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1911 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1912 if (pdb_get_username(newpwd) == NULL) {
1913 return NT_STATUS_INVALID_PARAMETER;
1915 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1916 TALLOC_FREE( attr_list );
1917 if (rc != LDAP_SUCCESS) {
1918 return NT_STATUS_UNSUCCESSFUL;
1920 pdb_set_backend_private_data(newpwd, result, NULL,
1921 my_methods, PDB_CHANGED);
1922 talloc_autofree_ldapmsg(newpwd, result);
1925 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1926 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1927 return NT_STATUS_UNSUCCESSFUL;
1930 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1931 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
1933 return NT_STATUS_UNSUCCESSFUL;
1936 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1938 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1939 element_is_changed)) {
1940 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1943 ldap_mods_free(mods,True);
1944 return NT_STATUS_UNSUCCESSFUL;
1947 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
1948 && (mods == NULL)) {
1949 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1950 pdb_get_username(newpwd)));
1952 return NT_STATUS_OK;
1955 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1958 ldap_mods_free(mods,True);
1964 * We need to set the backend private data to NULL here. For example
1965 * setuserinfo level 25 does a pdb_update_sam_account twice on the
1966 * same one, and with the explicit delete / add logic for attribute
1967 * values the second time we would use the wrong "old" value which
1968 * does not exist in LDAP anymore. Thus the LDAP server would refuse
1970 * The existing LDAPMessage is still being auto-freed by the
1973 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
1976 if (!NT_STATUS_IS_OK(ret)) {
1980 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1981 pdb_get_username(newpwd)));
1982 return NT_STATUS_OK;
1985 /***************************************************************************
1986 Renames a struct samu
1987 - The "rename user script" has full responsibility for changing everything
1988 ***************************************************************************/
1990 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
1991 TALLOC_CTX *tmp_ctx,
1995 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
1996 TALLOC_CTX *mem_ctx,
2000 size_t *p_num_groups);
2002 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2003 struct samu *old_acct,
2004 const char *newname)
2006 const char *oldname;
2008 char *rename_script = NULL;
2009 fstring oldname_lower, newname_lower;
2012 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2013 return NT_STATUS_INVALID_PARAMETER;
2016 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2017 return NT_STATUS_INVALID_PARAMETER;
2020 oldname = pdb_get_username(old_acct);
2022 /* rename the posix user */
2023 rename_script = SMB_STRDUP(lp_renameuser_script());
2024 if (rename_script == NULL) {
2025 return NT_STATUS_NO_MEMORY;
2028 if (!(*rename_script)) {
2029 SAFE_FREE(rename_script);
2030 return NT_STATUS_ACCESS_DENIED;
2033 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2036 /* We have to allow the account name to end with a '$'.
2037 Also, follow the semantics in _samr_create_user() and lower case the
2038 posix name but preserve the case in passdb */
2040 fstrcpy( oldname_lower, oldname );
2041 strlower_m( oldname_lower );
2042 fstrcpy( newname_lower, newname );
2043 strlower_m( newname_lower );
2044 rename_script = realloc_string_sub2(rename_script,
2049 if (!rename_script) {
2050 return NT_STATUS_NO_MEMORY;
2052 rename_script = realloc_string_sub2(rename_script,
2057 rc = smbrun(rename_script, NULL);
2059 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2060 rename_script, rc));
2062 SAFE_FREE(rename_script);
2065 smb_nscd_flush_user_cache();
2069 return NT_STATUS_UNSUCCESSFUL;
2071 return NT_STATUS_OK;
2074 /**********************************************************************
2075 Helper function to determine for update_sam_account whether
2076 we need LDAP modification.
2077 *********************************************************************/
2079 static bool element_is_set_or_changed(const struct samu *sampass,
2080 enum pdb_elements element)
2082 return (IS_SAM_SET(sampass, element) ||
2083 IS_SAM_CHANGED(sampass, element));
2086 /**********************************************************************
2087 Add struct samu to LDAP.
2088 *********************************************************************/
2090 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2092 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2093 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2095 LDAPMessage *result = NULL;
2096 LDAPMessage *entry = NULL;
2097 LDAPMod **mods = NULL;
2098 int ldap_op = LDAP_MOD_REPLACE;
2100 const char **attr_list;
2101 char *escape_user = NULL;
2102 const char *username = pdb_get_username(newpwd);
2103 const DOM_SID *sid = pdb_get_user_sid(newpwd);
2104 char *filter = NULL;
2106 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2107 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2110 return NT_STATUS_NO_MEMORY;
2113 if (!username || !*username) {
2114 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2115 status = NT_STATUS_INVALID_PARAMETER;
2119 /* free this list after the second search or in case we exit on failure */
2120 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2122 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2124 if (rc != LDAP_SUCCESS) {
2128 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2129 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2133 ldap_msgfree(result);
2136 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2137 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2139 if (rc == LDAP_SUCCESS) {
2140 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2141 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2142 "already in the base, with samba "
2143 "attributes\n", sid_string_dbg(sid)));
2146 ldap_msgfree(result);
2151 /* does the entry already exist but without a samba attributes?
2152 we need to return the samba attributes here */
2154 escape_user = escape_ldap_string(talloc_tos(), username);
2155 filter = talloc_strdup(attr_list, "(uid=%u)");
2157 status = NT_STATUS_NO_MEMORY;
2160 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2161 TALLOC_FREE(escape_user);
2163 status = NT_STATUS_NO_MEMORY;
2167 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2168 filter, attr_list, &result);
2169 if ( rc != LDAP_SUCCESS ) {
2173 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2175 if (num_result > 1) {
2176 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2180 /* Check if we need to update an existing entry */
2181 if (num_result == 1) {
2182 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2183 ldap_op = LDAP_MOD_REPLACE;
2184 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2185 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2187 status = NT_STATUS_NO_MEMORY;
2191 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2193 /* There might be a SID for this account already - say an idmap entry */
2195 filter = talloc_asprintf(ctx,
2196 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2197 get_userattr_key2string(ldap_state->schema_ver,
2198 LDAP_ATTR_USER_SID),
2199 sid_string_talloc(ctx, sid),
2200 LDAP_OBJ_IDMAP_ENTRY,
2201 LDAP_OBJ_SID_ENTRY);
2203 status = NT_STATUS_NO_MEMORY;
2207 /* free old result before doing a new search */
2208 if (result != NULL) {
2209 ldap_msgfree(result);
2212 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2213 filter, attr_list, &result);
2215 if ( rc != LDAP_SUCCESS ) {
2219 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2221 if (num_result > 1) {
2222 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2226 /* Check if we need to update an existing entry */
2227 if (num_result == 1) {
2229 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2230 ldap_op = LDAP_MOD_REPLACE;
2231 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2232 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2234 status = NT_STATUS_NO_MEMORY;
2240 if (num_result == 0) {
2241 char *escape_username;
2242 /* Check if we need to add an entry */
2243 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2244 ldap_op = LDAP_MOD_ADD;
2246 escape_username = escape_rdn_val_string_alloc(username);
2247 if (!escape_username) {
2248 status = NT_STATUS_NO_MEMORY;
2252 if (username[strlen(username)-1] == '$') {
2253 dn = talloc_asprintf(ctx,
2256 lp_ldap_machine_suffix());
2258 dn = talloc_asprintf(ctx,
2261 lp_ldap_user_suffix());
2264 SAFE_FREE(escape_username);
2266 status = NT_STATUS_NO_MEMORY;
2271 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2272 element_is_set_or_changed)) {
2273 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2275 ldap_mods_free(mods, true);
2281 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2284 switch ( ldap_state->schema_ver ) {
2285 case SCHEMAVER_SAMBAACCOUNT:
2286 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2288 case SCHEMAVER_SAMBASAMACCOUNT:
2289 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2292 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2296 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2297 if (!NT_STATUS_IS_OK(ret)) {
2298 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2299 pdb_get_username(newpwd),dn));
2300 ldap_mods_free(mods, true);
2304 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2305 ldap_mods_free(mods, true);
2307 status = NT_STATUS_OK;
2313 ldap_msgfree(result);
2318 /**********************************************************************
2319 *********************************************************************/
2321 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2323 LDAPMessage ** result)
2325 int scope = LDAP_SCOPE_SUBTREE;
2327 const char **attr_list;
2329 attr_list = get_attr_list(NULL, groupmap_attr_list);
2330 rc = smbldap_search(ldap_state->smbldap_state,
2331 lp_ldap_suffix (), scope,
2332 filter, attr_list, 0, result);
2333 TALLOC_FREE(attr_list);
2338 /**********************************************************************
2339 *********************************************************************/
2341 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2342 GROUP_MAP *map, LDAPMessage *entry)
2345 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2347 if (ldap_state == NULL || map == NULL || entry == NULL ||
2348 ldap_state->smbldap_state->ldap_struct == NULL) {
2349 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2354 temp = smbldap_talloc_single_attribute(
2355 ldap_state->smbldap_state->ldap_struct,
2357 get_attr_key2string(groupmap_attr_list,
2358 LDAP_ATTR_GIDNUMBER),
2361 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2362 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2366 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2368 map->gid = (gid_t)atol(temp);
2371 temp = smbldap_talloc_single_attribute(
2372 ldap_state->smbldap_state->ldap_struct,
2374 get_attr_key2string(groupmap_attr_list,
2375 LDAP_ATTR_GROUP_SID),
2378 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2379 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2384 if (!string_to_sid(&map->sid, temp)) {
2385 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2391 temp = smbldap_talloc_single_attribute(
2392 ldap_state->smbldap_state->ldap_struct,
2394 get_attr_key2string(groupmap_attr_list,
2395 LDAP_ATTR_GROUP_TYPE),
2398 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2399 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2403 map->sid_name_use = (enum lsa_SidType)atol(temp);
2405 if ((map->sid_name_use < SID_NAME_USER) ||
2406 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2407 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2413 temp = smbldap_talloc_single_attribute(
2414 ldap_state->smbldap_state->ldap_struct,
2416 get_attr_key2string(groupmap_attr_list,
2417 LDAP_ATTR_DISPLAY_NAME),
2420 temp = smbldap_talloc_single_attribute(
2421 ldap_state->smbldap_state->ldap_struct,
2423 get_attr_key2string(groupmap_attr_list,
2427 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2428 for gidNumber(%lu)\n",(unsigned long)map->gid));
2433 fstrcpy(map->nt_name, temp);
2436 temp = smbldap_talloc_single_attribute(
2437 ldap_state->smbldap_state->ldap_struct,
2439 get_attr_key2string(groupmap_attr_list,
2443 temp = talloc_strdup(ctx, "");
2449 fstrcpy(map->comment, temp);
2451 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2452 store_gid_sid_cache(&map->sid, map->gid);
2453 idmap_cache_set_sid2gid(&map->sid, map->gid);
2460 /**********************************************************************
2461 *********************************************************************/
2463 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2467 struct ldapsam_privates *ldap_state =
2468 (struct ldapsam_privates *)methods->private_data;
2469 LDAPMessage *result = NULL;
2470 LDAPMessage *entry = NULL;
2473 if (ldapsam_search_one_group(ldap_state, filter, &result)
2475 return NT_STATUS_NO_SUCH_GROUP;
2478 count = ldap_count_entries(priv2ld(ldap_state), result);
2481 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2483 ldap_msgfree(result);
2484 return NT_STATUS_NO_SUCH_GROUP;
2488 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2489 "count=%d\n", filter, count));
2490 ldap_msgfree(result);
2491 return NT_STATUS_NO_SUCH_GROUP;
2494 entry = ldap_first_entry(priv2ld(ldap_state), result);
2497 ldap_msgfree(result);
2498 return NT_STATUS_UNSUCCESSFUL;
2501 if (!init_group_from_ldap(ldap_state, map, entry)) {
2502 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2503 "group filter %s\n", filter));
2504 ldap_msgfree(result);
2505 return NT_STATUS_NO_SUCH_GROUP;
2508 ldap_msgfree(result);
2509 return NT_STATUS_OK;
2512 /**********************************************************************
2513 *********************************************************************/
2515 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2518 char *filter = NULL;
2522 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2524 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2525 sid_to_fstring(tmp, &sid)) < 0) {
2526 return NT_STATUS_NO_MEMORY;
2529 status = ldapsam_getgroup(methods, filter, map);
2534 /**********************************************************************
2535 *********************************************************************/
2537 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2540 char *filter = NULL;
2543 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2545 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2546 (unsigned long)gid) < 0) {
2547 return NT_STATUS_NO_MEMORY;
2550 status = ldapsam_getgroup(methods, filter, map);
2555 /**********************************************************************
2556 *********************************************************************/
2558 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2561 char *filter = NULL;
2562 char *escape_name = escape_ldap_string(talloc_tos(), name);
2566 return NT_STATUS_NO_MEMORY;
2569 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2571 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2572 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2574 TALLOC_FREE(escape_name);
2575 return NT_STATUS_NO_MEMORY;
2578 TALLOC_FREE(escape_name);
2579 status = ldapsam_getgroup(methods, filter, map);
2584 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2586 const DOM_SID *domain_sid,
2592 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2593 str, sizeof(str)-1)) {
2594 DEBUG(10, ("Could not find sambaSID attribute\n"));
2598 if (!string_to_sid(&sid, str)) {
2599 DEBUG(10, ("Could not convert string %s to sid\n", str));
2603 if (sid_compare_domain(&sid, domain_sid) != 0) {
2604 DEBUG(10, ("SID %s is not in expected domain %s\n",
2605 str, sid_string_dbg(domain_sid)));
2609 if (!sid_peek_rid(&sid, rid)) {
2610 DEBUG(10, ("Could not peek into RID\n"));
2617 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2618 TALLOC_CTX *mem_ctx,
2619 const DOM_SID *group,
2620 uint32 **pp_member_rids,
2621 size_t *p_num_members)
2623 struct ldapsam_privates *ldap_state =
2624 (struct ldapsam_privates *)methods->private_data;
2625 struct smbldap_state *conn = ldap_state->smbldap_state;
2626 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2627 const char *sid_attrs[] = { "sambaSID", NULL };
2628 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2629 LDAPMessage *result = NULL;
2632 char **values = NULL;
2637 *pp_member_rids = NULL;
2640 filter = talloc_asprintf(mem_ctx,
2641 "(&(objectClass=%s)"
2644 LDAP_OBJ_POSIXGROUP,
2646 sid_string_talloc(mem_ctx, group));
2647 if (filter == NULL) {
2648 ret = NT_STATUS_NO_MEMORY;
2652 rc = smbldap_search(conn, lp_ldap_suffix(),
2653 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2656 if (rc != LDAP_SUCCESS)
2659 talloc_autofree_ldapmsg(mem_ctx, result);
2661 count = ldap_count_entries(conn->ldap_struct, result);
2664 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2665 sid_string_dbg(group)));
2666 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2671 ret = NT_STATUS_NO_SUCH_GROUP;
2675 entry = ldap_first_entry(conn->ldap_struct, result);
2679 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2681 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2682 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2686 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2688 if ((values != NULL) && (values[0] != NULL)) {
2690 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2691 if (filter == NULL) {
2692 ret = NT_STATUS_NO_MEMORY;
2696 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2697 char *escape_memberuid;
2699 escape_memberuid = escape_ldap_string(talloc_tos(),
2701 if (escape_memberuid == NULL) {
2702 ret = NT_STATUS_NO_MEMORY;
2706 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2707 TALLOC_FREE(escape_memberuid);
2708 if (filter == NULL) {
2709 ret = NT_STATUS_NO_MEMORY;
2714 filter = talloc_asprintf_append_buffer(filter, "))");
2715 if (filter == NULL) {
2716 ret = NT_STATUS_NO_MEMORY;
2720 rc = smbldap_search(conn, lp_ldap_suffix(),
2721 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2724 if (rc != LDAP_SUCCESS)
2727 count = ldap_count_entries(conn->ldap_struct, result);
2728 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2730 talloc_autofree_ldapmsg(mem_ctx, result);
2732 for (entry = ldap_first_entry(conn->ldap_struct, result);
2734 entry = ldap_next_entry(conn->ldap_struct, entry))
2740 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2744 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2745 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2746 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2750 if (!string_to_sid(&sid, sidstr))
2753 if (!sid_check_is_in_our_domain(&sid)) {
2754 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2755 "in our domain\n"));
2756 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2760 sid_peek_rid(&sid, &rid);
2762 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2764 ret = NT_STATUS_NO_MEMORY;
2770 filter = talloc_asprintf(mem_ctx,
2771 "(&(objectClass=%s)"
2773 LDAP_OBJ_SAMBASAMACCOUNT,
2776 rc = smbldap_search(conn, lp_ldap_suffix(),
2777 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2780 if (rc != LDAP_SUCCESS)
2783 talloc_autofree_ldapmsg(mem_ctx, result);
2785 for (entry = ldap_first_entry(conn->ldap_struct, result);
2787 entry = ldap_next_entry(conn->ldap_struct, entry))
2791 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2793 get_global_sam_sid(),
2795 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2796 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2800 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2802 ret = NT_STATUS_NO_MEMORY;
2812 ldap_value_free(values);
2817 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2818 TALLOC_CTX *mem_ctx,
2822 size_t *p_num_groups)
2824 struct ldapsam_privates *ldap_state =
2825 (struct ldapsam_privates *)methods->private_data;
2826 struct smbldap_state *conn = ldap_state->smbldap_state;
2828 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2831 LDAPMessage *result = NULL;
2833 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2834 size_t num_sids, num_gids;
2836 gid_t primary_gid = -1;
2841 if (pdb_get_username(user) == NULL) {
2842 return NT_STATUS_INVALID_PARAMETER;
2845 escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
2846 if (escape_name == NULL)
2847 return NT_STATUS_NO_MEMORY;
2849 /* retrieve the users primary gid */
2850 filter = talloc_asprintf(mem_ctx,
2851 "(&(objectClass=%s)(uid=%s))",
2852 LDAP_OBJ_SAMBASAMACCOUNT,
2854 if (filter == NULL) {
2855 ret = NT_STATUS_NO_MEMORY;
2859 rc = smbldap_search(conn, lp_ldap_suffix(),
2860 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2862 if (rc != LDAP_SUCCESS)
2865 talloc_autofree_ldapmsg(mem_ctx, result);
2867 count = ldap_count_entries(priv2ld(ldap_state), result);
2871 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2872 ret = NT_STATUS_NO_SUCH_USER;
2875 entry = ldap_first_entry(priv2ld(ldap_state), result);
2877 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2879 DEBUG (1, ("Unable to find the member's gid!\n"));
2880 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2883 primary_gid = strtoul(gidstr, NULL, 10);
2886 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2887 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2891 filter = talloc_asprintf(mem_ctx,
2892 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2893 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2894 if (filter == NULL) {
2895 ret = NT_STATUS_NO_MEMORY;
2899 rc = smbldap_search(conn, lp_ldap_suffix(),
2900 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2902 if (rc != LDAP_SUCCESS)
2905 talloc_autofree_ldapmsg(mem_ctx, result);
2913 /* We need to add the primary group as the first gid/sid */
2915 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2916 ret = NT_STATUS_NO_MEMORY;
2920 /* This sid will be replaced later */
2922 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2924 if (!NT_STATUS_IS_OK(ret)) {
2928 for (entry = ldap_first_entry(conn->ldap_struct, result);
2930 entry = ldap_next_entry(conn->ldap_struct, entry))
2937 if (!smbldap_get_single_attribute(conn->ldap_struct,
2939 str, sizeof(str)-1))
2942 if (!string_to_sid(&sid, str))
2945 if (!smbldap_get_single_attribute(conn->ldap_struct,
2947 str, sizeof(str)-1))
2950 gid = strtoul(str, &end, 10);
2952 if (PTR_DIFF(end, str) != strlen(str))
2955 if (gid == primary_gid) {
2956 sid_copy(&(*pp_sids)[0], &sid);
2958 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2960 ret = NT_STATUS_NO_MEMORY;
2963 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2965 if (!NT_STATUS_IS_OK(ret)) {
2971 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
2972 DEBUG(3, ("primary group of [%s] not found\n",
2973 pdb_get_username(user)));
2977 *p_num_groups = num_sids;
2983 TALLOC_FREE(escape_name);
2987 /**********************************************************************
2988 * Augment a posixGroup object with a sambaGroupMapping domgroup
2989 *********************************************************************/
2991 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
2992 struct ldapsam_privates *ldap_state,
2995 const char *filter, *dn;
2996 LDAPMessage *msg, *entry;
3000 filter = talloc_asprintf(mem_ctx,
3001 "(&(objectClass=%s)(gidNumber=%u))",
3002 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3003 if (filter == NULL) {
3004 return NT_STATUS_NO_MEMORY;
3007 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3008 get_attr_list(mem_ctx, groupmap_attr_list),
3010 talloc_autofree_ldapmsg(mem_ctx, msg);
3012 if ((rc != LDAP_SUCCESS) ||
3013 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3014 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3015 return NT_STATUS_NO_SUCH_GROUP;
3018 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3020 return NT_STATUS_NO_MEMORY;
3024 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3026 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3027 sid_string_talloc(mem_ctx, &map->sid));
3028 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3029 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3030 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3032 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3034 talloc_autofree_ldapmod(mem_ctx, mods);
3036 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3037 if (rc != LDAP_SUCCESS) {
3038 return NT_STATUS_ACCESS_DENIED;
3041 return NT_STATUS_OK;
3044 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3047 struct ldapsam_privates *ldap_state =
3048 (struct ldapsam_privates *)methods->private_data;
3049 LDAPMessage *msg = NULL;
3050 LDAPMod **mods = NULL;
3051 const char *attrs[] = { NULL };
3055 TALLOC_CTX *mem_ctx;
3062 mem_ctx = talloc_new(NULL);
3063 if (mem_ctx == NULL) {
3064 DEBUG(0, ("talloc_new failed\n"));
3065 return NT_STATUS_NO_MEMORY;