2 Unix SMB/CIFS mplementation.
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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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/SAM_ACCOUNT
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)
81 #define SAM_ACCOUNT struct sam_passwd
86 struct ldapsam_privates
{
87 struct smbldap_state
*smbldap_state
;
94 const char *domain_name
;
97 /* configuration items */
101 /**********************************************************************
102 Free a LDAPMessage (one is stored on the SAM_ACCOUNT).
103 **********************************************************************/
105 static void private_data_free_fn(void **result
)
107 ldap_msgfree(*result
);
111 /**********************************************************************
112 Get the attribute name given a user schame version.
113 **********************************************************************/
115 static const char* get_userattr_key2string( int schema_ver
, int key
)
117 switch ( schema_ver
) {
118 case SCHEMAVER_SAMBAACCOUNT
:
119 return get_attr_key2string( attrib_map_v22
, key
);
121 case SCHEMAVER_SAMBASAMACCOUNT
:
122 return get_attr_key2string( attrib_map_v30
, key
);
125 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
131 /**********************************************************************
132 Return the list of attribute names given a user schema version.
133 **********************************************************************/
135 static char** get_userattr_list( int schema_ver
)
137 switch ( schema_ver
) {
138 case SCHEMAVER_SAMBAACCOUNT
:
139 return get_attr_list( attrib_map_v22
);
141 case SCHEMAVER_SAMBASAMACCOUNT
:
142 return get_attr_list( attrib_map_v30
);
144 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
151 /*******************************************************************
152 Generate the LDAP search filter for the objectclass based on the
153 version of the schema we are using.
154 ******************************************************************/
156 static const char* get_objclass_filter( int schema_ver
)
158 static fstring objclass_filter
;
160 switch( schema_ver
) {
161 case SCHEMAVER_SAMBAACCOUNT
:
162 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT
);
164 case SCHEMAVER_SAMBASAMACCOUNT
:
165 fstr_sprintf( objclass_filter
, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT
);
168 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
172 return objclass_filter
;
175 /*******************************************************************
176 Run the search by name.
177 ******************************************************************/
179 static int ldapsam_search_suffix_by_name (struct ldapsam_privates
*ldap_state
,
181 LDAPMessage
** result
, char **attr
)
184 char *escape_user
= escape_ldap_string_alloc(user
);
187 return LDAP_NO_MEMORY
;
191 * in the filter expression, replace %u with the real name
192 * so in ldap filter, %u MUST exist :-)
194 pstr_sprintf(filter
, "(&%s%s)", lp_ldap_filter(),
195 get_objclass_filter(ldap_state
->schema_ver
));
198 * have to use this here because $ is filtered out
203 all_string_sub(filter
, "%u", escape_user
, sizeof(pstring
));
204 SAFE_FREE(escape_user
);
206 return smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
209 /*******************************************************************
210 Run the search by rid.
211 ******************************************************************/
213 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates
*ldap_state
,
214 uint32 rid
, LDAPMessage
** result
,
220 pstr_sprintf(filter
, "(&(rid=%i)%s)", rid
,
221 get_objclass_filter(ldap_state
->schema_ver
));
223 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
228 /*******************************************************************
229 Run the search by SID.
230 ******************************************************************/
232 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates
*ldap_state
,
233 const DOM_SID
*sid
, LDAPMessage
** result
,
240 pstr_sprintf(filter
, "(&(%s=%s)%s)",
241 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
242 sid_to_string(sid_string
, sid
),
243 get_objclass_filter(ldap_state
->schema_ver
));
245 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
, attr
, result
);
250 /*******************************************************************
251 Delete complete object or objectclass and attrs from
252 object found in search_result depending on lp_ldap_delete_dn
253 ******************************************************************/
255 static NTSTATUS
ldapsam_delete_entry(struct ldapsam_privates
*ldap_state
,
257 const char *objectclass
,
261 LDAPMessage
*entry
= NULL
;
262 LDAPMod
**mods
= NULL
;
264 BerElement
*ptr
= NULL
;
266 rc
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
269 DEBUG(0, ("ldapsam_delete_entry: Entry must exist exactly once!\n"));
270 return NT_STATUS_UNSUCCESSFUL
;
273 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
274 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
276 return NT_STATUS_UNSUCCESSFUL
;
279 if (lp_ldap_delete_dn()) {
280 NTSTATUS ret
= NT_STATUS_OK
;
281 rc
= smbldap_delete(ldap_state
->smbldap_state
, dn
);
283 if (rc
!= LDAP_SUCCESS
) {
284 DEBUG(0, ("ldapsam_delete_entry: Could not delete object %s\n", dn
));
285 ret
= NT_STATUS_UNSUCCESSFUL
;
291 /* Ok, delete only the SAM attributes */
293 for (name
= ldap_first_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
, &ptr
);
295 name
= ldap_next_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
, ptr
)) {
298 /* We are only allowed to delete the attributes that
301 for (attrib
= attrs
; *attrib
!= NULL
; attrib
++) {
302 if (StrCaseCmp(*attrib
, name
) == 0) {
303 DEBUG(10, ("ldapsam_delete_entry: deleting attribute %s\n", name
));
304 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, name
, NULL
);
315 smbldap_set_mod(&mods
, LDAP_MOD_DELETE
, "objectClass", objectclass
);
317 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
318 ldap_mods_free(mods
, True
);
320 if (rc
!= LDAP_SUCCESS
) {
321 char *ld_error
= NULL
;
322 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
325 DEBUG(0, ("ldapsam_delete_entry: Could not delete attributes for %s, error: %s (%s)\n",
326 dn
, ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
329 return NT_STATUS_UNSUCCESSFUL
;
336 /* New Interface is being implemented here */
338 #if 0 /* JERRY - not uesed anymore */
340 /**********************************************************************
341 Initialize SAM_ACCOUNT from an LDAP query (unix attributes only)
342 *********************************************************************/
343 static BOOL
get_unix_attributes (struct ldapsam_privates
*ldap_state
,
344 SAM_ACCOUNT
* sampass
,
353 if ((ldap_values
= ldap_get_values (ldap_state
->smbldap_state
->ldap_struct
, entry
, "objectClass")) == NULL
) {
354 DEBUG (1, ("get_unix_attributes: no objectClass! \n"));
358 for (values
=ldap_values
;*values
;values
++) {
359 if (strequal(*values
, LDAP_OBJ_POSIXACCOUNT
)) {
364 if (!*values
) { /*end of array, no posixAccount */
365 DEBUG(10, ("user does not have %s attributes\n", LDAP_OBJ_POSIXACCOUNT
));
366 ldap_value_free(ldap_values
);
369 ldap_value_free(ldap_values
);
371 if ( !smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
372 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_UNIX_HOME
), homedir
) )
377 if ( !smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
378 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_GIDNUMBER
), temp
) )
383 *gid
= (gid_t
)atol(temp
);
385 pdb_set_unix_homedir(sampass
, homedir
, PDB_SET
);
387 DEBUG(10, ("user has %s attributes\n", LDAP_OBJ_POSIXACCOUNT
));
394 /**********************************************************************
395 Initialize SAM_ACCOUNT from an LDAP query.
396 (Based on init_sam_from_buffer in pdb_tdb.c)
397 *********************************************************************/
399 static BOOL
init_sam_from_ldap (struct ldapsam_privates
*ldap_state
,
400 SAM_ACCOUNT
* sampass
,
407 pass_can_change_time
,
408 pass_must_change_time
;
419 char munged_dial
[2048];
421 uint8 smblmpwd
[LM_HASH_LEN
],
422 smbntpwd
[NT_HASH_LEN
];
423 uint16 acct_ctrl
= 0,
425 uint16 bad_password_count
= 0,
428 uint8 hours
[MAX_HOURS_LEN
];
432 * do a little initialization
436 nt_username
[0] = '\0';
440 logon_script
[0] = '\0';
441 profile_path
[0] = '\0';
443 munged_dial
[0] = '\0';
444 workstations
[0] = '\0';
447 if (sampass
== NULL
|| ldap_state
== NULL
|| entry
== NULL
) {
448 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
452 if (ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
453 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->ldap_struct is NULL!\n"));
457 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
, "uid", username
)) {
458 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for this user!\n"));
462 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username
));
464 pstrcpy(nt_username
, username
);
466 pstrcpy(domain
, ldap_state
->domain_name
);
468 pdb_set_username(sampass
, username
, PDB_SET
);
470 pdb_set_domain(sampass
, domain
, PDB_DEFAULT
);
471 pdb_set_nt_username(sampass
, nt_username
, PDB_SET
);
473 /* deal with different attributes between the schema first */
475 if ( ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
476 if (smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
477 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
), temp
)) {
478 pdb_set_user_sid_from_string(sampass
, temp
, PDB_SET
);
481 if (smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
482 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PRIMARY_GROUP_SID
), temp
)) {
483 pdb_set_group_sid_from_string(sampass
, temp
, PDB_SET
);
485 pdb_set_group_sid_from_rid(sampass
, DOMAIN_GROUP_RID_USERS
, PDB_DEFAULT
);
488 if (smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
489 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
), temp
)) {
490 user_rid
= (uint32
)atol(temp
);
491 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
494 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
495 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PRIMARY_GROUP_RID
), temp
)) {
496 pdb_set_group_sid_from_rid(sampass
, DOMAIN_GROUP_RID_USERS
, PDB_DEFAULT
);
500 group_rid
= (uint32
)atol(temp
);
502 /* for some reason, we often have 0 as a primary group RID.
503 Make sure that we treat this just as a 'default' value */
506 pdb_set_group_sid_from_rid(sampass
, group_rid
, PDB_SET
);
508 pdb_set_group_sid_from_rid(sampass
, DOMAIN_GROUP_RID_USERS
, PDB_DEFAULT
);
512 if (pdb_get_init_flags(sampass
,PDB_USERSID
) == PDB_DEFAULT
) {
513 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
514 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
515 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
521 #if 0 /* JERRY -- not used anymore */
523 * If so configured, try and get the values from LDAP
526 if (lp_ldap_trust_ids() && (get_unix_attributes(ldap_state
, sampass
, entry
, &gid
)))
528 if (pdb_get_init_flags(sampass
,PDB_GROUPSID
) == PDB_DEFAULT
)
531 /* call the mapping code here */
532 if(pdb_getgrgid(&map
, gid
)) {
533 pdb_set_group_sid(sampass
, &map
.sid
, PDB_SET
);
536 pdb_set_group_sid_from_rid(sampass
, pdb_gid_to_group_rid(gid
), PDB_SET
);
542 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
543 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
), temp
)) {
544 /* leave as default */
546 pass_last_set_time
= (time_t) atol(temp
);
547 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
, PDB_SET
);
550 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
551 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
)) {
552 /* leave as default */
554 logon_time
= (time_t) atol(temp
);
555 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
558 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
559 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
)) {
560 /* leave as default */
562 logoff_time
= (time_t) atol(temp
);
563 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
566 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
567 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
)) {
568 /* leave as default */
570 kickoff_time
= (time_t) atol(temp
);
571 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
574 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
575 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
)) {
576 /* leave as default */
578 pass_can_change_time
= (time_t) atol(temp
);
579 pdb_set_pass_can_change_time(sampass
, pass_can_change_time
, PDB_SET
);
582 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
583 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
)) {
584 /* leave as default */
586 pass_must_change_time
= (time_t) atol(temp
);
587 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, PDB_SET
);
590 /* recommend that 'gecos' and 'displayName' should refer to the same
591 * attribute OID. userFullName depreciated, only used by Samba
592 * primary rules of LDAP: don't make a new attribute when one is already defined
593 * that fits your needs; using cn then displayName rather than 'userFullName'
596 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
597 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
), fullname
)) {
598 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
599 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_CN
), fullname
)) {
600 /* leave as default */
602 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
605 pdb_set_fullname(sampass
, fullname
, PDB_SET
);
608 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
609 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
), dir_drive
))
611 pdb_set_dir_drive( sampass
,
612 talloc_sub_basic(sampass
->mem_ctx
, username
, lp_logon_drive()),
615 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
618 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
619 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
), homedir
))
621 pdb_set_homedir( sampass
,
622 talloc_sub_basic(sampass
->mem_ctx
, username
, lp_logon_home()),
625 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
628 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
629 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
), logon_script
))
631 pdb_set_logon_script( sampass
,
632 talloc_sub_basic(sampass
->mem_ctx
, username
, lp_logon_script()),
635 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
638 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
639 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
), profile_path
))
641 pdb_set_profile_path( sampass
,
642 talloc_sub_basic( sampass
->mem_ctx
, username
, lp_logon_path()),
645 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
648 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
649 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
), acct_desc
))
651 /* leave as default */
653 pdb_set_acct_desc(sampass
, acct_desc
, PDB_SET
);
656 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
657 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
), workstations
)) {
658 /* leave as default */;
660 pdb_set_workstations(sampass
, workstations
, PDB_SET
);
663 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
664 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
), munged_dial
)) {
665 /* leave as default */;
667 pdb_set_munged_dial(sampass
, munged_dial
, PDB_SET
);
670 /* FIXME: hours stuff should be cleaner */
674 memset(hours
, 0xff, hours_len
);
676 if (!smbldap_get_single_attribute (ldap_state
->smbldap_state
->ldap_struct
, entry
,
677 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
), temp
)) {
678 /* leave as default */
680 pdb_gethexpwd(temp
, smblmpwd
);
681 memset((char *)temp
, '\0', strlen(temp
)+1);
682 if (!pdb_set_lanman_passwd(sampass
, smblmpwd
, PDB_SET
))
684 ZERO_STRUCT(smblmpwd
);
687 if (!smbldap_get_single_attribute (ldap_state
->smbldap_state
->ldap_struct
, entry
,
688 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
), temp
)) {
689 /* leave as default */
691 pdb_gethexpwd(temp
, smbntpwd
);
692 memset((char *)temp
, '\0', strlen(temp
)+1);
693 if (!pdb_set_nt_passwd(sampass
, smbntpwd
, PDB_SET
))
695 ZERO_STRUCT(smbntpwd
);
698 if (!smbldap_get_single_attribute (ldap_state
->smbldap_state
->ldap_struct
, entry
,
699 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
), temp
)) {
700 acct_ctrl
|= ACB_NORMAL
;
702 acct_ctrl
= pdb_decode_acct_ctrl(temp
);
705 acct_ctrl
|= ACB_NORMAL
;
707 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
710 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
711 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
713 /* pdb_set_munged_dial(sampass, munged_dial, PDB_SET); */
715 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
716 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_BAD_PASSWORD_COUNT
), temp
)) {
717 /* leave as default */
719 bad_password_count
= (uint32
) atol(temp
);
720 pdb_set_bad_password_count(sampass
, bad_password_count
, PDB_SET
);
723 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
724 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_COUNT
), temp
)) {
725 /* leave as default */
727 logon_count
= (uint32
) atol(temp
);
728 pdb_set_logon_count(sampass
, logon_count
, PDB_SET
);
731 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
733 pdb_set_hours(sampass
, hours
, PDB_SET
);
738 /**********************************************************************
739 Initialize SAM_ACCOUNT from an LDAP query.
740 (Based on init_buffer_from_sam in pdb_tdb.c)
741 *********************************************************************/
743 static BOOL
init_ldap_from_sam (struct ldapsam_privates
*ldap_state
,
744 LDAPMessage
*existing
,
745 LDAPMod
*** mods
, SAM_ACCOUNT
* sampass
,
746 BOOL (*need_update
)(const SAM_ACCOUNT
*,
752 if (mods
== NULL
|| sampass
== NULL
) {
753 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
760 * took out adding "objectclass: sambaAccount"
761 * do this on a per-mod basis
763 if (need_update(sampass
, PDB_USERNAME
))
764 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
765 "uid", pdb_get_username(sampass
));
767 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass
)));
769 /* only update the RID if we actually need to */
770 if (need_update(sampass
, PDB_USERSID
)) {
772 fstring dom_sid_string
;
773 const DOM_SID
*user_sid
= pdb_get_user_sid(sampass
);
775 switch ( ldap_state
->schema_ver
) {
776 case SCHEMAVER_SAMBAACCOUNT
:
777 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, user_sid
, &rid
)) {
778 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
779 sid_to_string(sid_string
, user_sid
),
780 sid_to_string(dom_sid_string
, &ldap_state
->domain_sid
)));
783 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
784 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
785 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_RID
),
789 case SCHEMAVER_SAMBASAMACCOUNT
:
790 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
791 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
792 sid_to_string(sid_string
, user_sid
));
796 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
801 /* we don't need to store the primary group RID - so leaving it
802 'free' to hang off the unix primary group makes life easier */
804 if (need_update(sampass
, PDB_GROUPSID
)) {
806 fstring dom_sid_string
;
807 const DOM_SID
*group_sid
= pdb_get_group_sid(sampass
);
809 switch ( ldap_state
->schema_ver
) {
810 case SCHEMAVER_SAMBAACCOUNT
:
811 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, group_sid
, &rid
)) {
812 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
813 sid_to_string(sid_string
, group_sid
),
814 sid_to_string(dom_sid_string
, &ldap_state
->domain_sid
)));
818 slprintf(temp
, sizeof(temp
) - 1, "%i", rid
);
819 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
820 get_userattr_key2string(ldap_state
->schema_ver
,
821 LDAP_ATTR_PRIMARY_GROUP_RID
), temp
);
824 case SCHEMAVER_SAMBASAMACCOUNT
:
825 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
826 get_userattr_key2string(ldap_state
->schema_ver
,
827 LDAP_ATTR_PRIMARY_GROUP_SID
), sid_to_string(sid_string
, group_sid
));
831 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
837 /* displayName, cn, and gecos should all be the same
838 * most easily accomplished by giving them the same OID
839 * gecos isn't set here b/c it should be handled by the
841 * We change displayName only and fall back to cn if
845 if (need_update(sampass
, PDB_FULLNAME
))
846 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
847 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DISPLAY_NAME
),
848 pdb_get_fullname(sampass
));
850 if (need_update(sampass
, PDB_ACCTDESC
))
851 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
852 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_DESC
),
853 pdb_get_acct_desc(sampass
));
855 if (need_update(sampass
, PDB_WORKSTATIONS
))
856 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
857 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_WKS
),
858 pdb_get_workstations(sampass
));
860 if (need_update(sampass
, PDB_MUNGEDDIAL
))
861 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
862 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_MUNGED_DIAL
),
863 pdb_get_munged_dial(sampass
));
865 if (need_update(sampass
, PDB_SMBHOME
))
866 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
867 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_PATH
),
868 pdb_get_homedir(sampass
));
870 if (need_update(sampass
, PDB_DRIVE
))
871 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
872 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_HOME_DRIVE
),
873 pdb_get_dir_drive(sampass
));
875 if (need_update(sampass
, PDB_LOGONSCRIPT
))
876 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
877 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_SCRIPT
),
878 pdb_get_logon_script(sampass
));
880 if (need_update(sampass
, PDB_PROFILE
))
881 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
882 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PROFILE_PATH
),
883 pdb_get_profile_path(sampass
));
885 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logon_time(sampass
));
886 if (need_update(sampass
, PDB_LOGONTIME
))
887 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
888 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGON_TIME
), temp
);
890 slprintf(temp
, sizeof(temp
) - 1, "%li", pdb_get_logoff_time(sampass
));
891 if (need_update(sampass
, PDB_LOGOFFTIME
))
892 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
893 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LOGOFF_TIME
), temp
);
895 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_kickoff_time(sampass
));
896 if (need_update(sampass
, PDB_KICKOFFTIME
))
897 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
898 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_KICKOFF_TIME
), temp
);
900 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_can_change_time(sampass
));
901 if (need_update(sampass
, PDB_CANCHANGETIME
))
902 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
903 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_CAN_CHANGE
), temp
);
905 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_must_change_time(sampass
));
906 if (need_update(sampass
, PDB_MUSTCHANGETIME
))
907 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
908 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_MUST_CHANGE
), temp
);
910 if ((pdb_get_acct_ctrl(sampass
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
))
911 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY
)) {
913 pdb_sethexpwd(temp
, pdb_get_lanman_passwd(sampass
),
914 pdb_get_acct_ctrl(sampass
));
916 if (need_update(sampass
, PDB_LMPASSWD
))
917 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
918 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_LMPW
),
921 pdb_sethexpwd (temp
, pdb_get_nt_passwd(sampass
),
922 pdb_get_acct_ctrl(sampass
));
924 if (need_update(sampass
, PDB_NTPASSWD
))
925 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
926 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_NTPW
),
929 slprintf (temp
, sizeof (temp
) - 1, "%li", pdb_get_pass_last_set_time(sampass
));
930 if (need_update(sampass
, PDB_PASSLASTSET
))
931 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
932 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_PWD_LAST_SET
),
936 /* FIXME: Hours stuff goes in LDAP */
938 if (need_update(sampass
, PDB_ACCTCTRL
))
939 smbldap_make_mod(ldap_state
->smbldap_state
->ldap_struct
, existing
, mods
,
940 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ACB_INFO
),
941 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
946 /**********************************************************************
947 Connect to LDAP server for password enumeration.
948 *********************************************************************/
950 static NTSTATUS
ldapsam_setsampwent(struct pdb_methods
*my_methods
, BOOL update
)
952 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
957 pstr_sprintf( filter
, "(&%s%s)", lp_ldap_filter(),
958 get_objclass_filter(ldap_state
->schema_ver
));
959 all_string_sub(filter
, "%u", "*", sizeof(pstring
));
961 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
962 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
, filter
,
963 attr_list
, &ldap_state
->result
);
964 free_attr_list( attr_list
);
966 if (rc
!= LDAP_SUCCESS
) {
967 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc
)));
968 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", lp_ldap_suffix(), filter
));
969 ldap_msgfree(ldap_state
->result
);
970 ldap_state
->result
= NULL
;
971 return NT_STATUS_UNSUCCESSFUL
;
974 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base!\n",
975 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
976 ldap_state
->result
)));
978 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
,
980 ldap_state
->index
= 0;
985 /**********************************************************************
986 End enumeration of the LDAP password list.
987 *********************************************************************/
989 static void ldapsam_endsampwent(struct pdb_methods
*my_methods
)
991 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
992 if (ldap_state
->result
) {
993 ldap_msgfree(ldap_state
->result
);
994 ldap_state
->result
= NULL
;
998 /**********************************************************************
999 Get the next entry in the LDAP password database.
1000 *********************************************************************/
1002 static NTSTATUS
ldapsam_getsampwent(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
)
1004 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1005 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1009 if (!ldap_state
->entry
)
1012 ldap_state
->index
++;
1013 bret
= init_sam_from_ldap(ldap_state
, user
, ldap_state
->entry
);
1015 ldap_state
->entry
= ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
1019 return NT_STATUS_OK
;
1022 /**********************************************************************
1023 Get SAM_ACCOUNT entry from LDAP by username.
1024 *********************************************************************/
1026 static NTSTATUS
ldapsam_getsampwnam(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
, const char *sname
)
1028 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1029 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1030 LDAPMessage
*result
= NULL
;
1031 LDAPMessage
*entry
= NULL
;
1036 attr_list
= get_userattr_list( ldap_state
->schema_ver
);
1037 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
, attr_list
);
1038 free_attr_list( attr_list
);
1040 if ( rc
!= LDAP_SUCCESS
)
1041 return NT_STATUS_NO_SUCH_USER
;
1043 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1046 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname
, count
));
1047 ldap_msgfree(result
);
1048 return NT_STATUS_NO_SUCH_USER
;
1049 } else if (count
> 1) {
1050 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname
, count
));
1051 ldap_msgfree(result
);
1052 return NT_STATUS_NO_SUCH_USER
;
1055 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1057 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1058 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname
));
1059 ldap_msgfree(result
);
1060 return NT_STATUS_NO_SUCH_USER
;
1062 pdb_set_backend_private_data(user
, result
,
1063 private_data_free_fn
,
1064 my_methods
, PDB_CHANGED
);
1067 ldap_msgfree(result
);
1072 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates
*ldap_state
,
1073 const DOM_SID
*sid
, LDAPMessage
**result
)
1079 switch ( ldap_state
->schema_ver
) {
1080 case SCHEMAVER_SAMBASAMACCOUNT
:
1081 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1082 rc
= ldapsam_search_suffix_by_sid(ldap_state
, sid
, result
, attr_list
);
1083 free_attr_list( attr_list
);
1085 if ( rc
!= LDAP_SUCCESS
)
1089 case SCHEMAVER_SAMBAACCOUNT
:
1090 if (!sid_peek_check_rid(&ldap_state
->domain_sid
, sid
, &rid
)) {
1094 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1095 rc
= ldapsam_search_suffix_by_rid(ldap_state
, rid
, result
, attr_list
);
1096 free_attr_list( attr_list
);
1098 if ( rc
!= LDAP_SUCCESS
)
1105 /**********************************************************************
1106 Get SAM_ACCOUNT entry from LDAP by SID.
1107 *********************************************************************/
1109 static NTSTATUS
ldapsam_getsampwsid(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* user
, const DOM_SID
*sid
)
1111 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1112 LDAPMessage
*result
= NULL
;
1113 LDAPMessage
*entry
= NULL
;
1118 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1120 if (rc
!= LDAP_SUCCESS
)
1121 return NT_STATUS_NO_SUCH_USER
;
1123 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1126 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string
, sid
),
1128 ldap_msgfree(result
);
1129 return NT_STATUS_NO_SUCH_USER
;
1130 } else if (count
> 1) {
1131 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string
, sid
),
1133 ldap_msgfree(result
);
1134 return NT_STATUS_NO_SUCH_USER
;
1137 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1139 ldap_msgfree(result
);
1140 return NT_STATUS_NO_SUCH_USER
;
1143 if (!init_sam_from_ldap(ldap_state
, user
, entry
)) {
1144 DEBUG(1,("ldapsam_getsampwrid: init_sam_from_ldap failed!\n"));
1145 ldap_msgfree(result
);
1146 return NT_STATUS_NO_SUCH_USER
;
1149 pdb_set_backend_private_data(user
, result
,
1150 private_data_free_fn
,
1151 my_methods
, PDB_CHANGED
);
1152 return NT_STATUS_OK
;
1155 /********************************************************************
1156 Do the actual modification - also change a plaintext passord if
1158 **********************************************************************/
1160 static NTSTATUS
ldapsam_modify_entry(struct pdb_methods
*my_methods
,
1161 SAM_ACCOUNT
*newpwd
, char *dn
,
1162 LDAPMod
**mods
, int ldap_op
,
1163 BOOL (*need_update
)(const SAM_ACCOUNT
*, enum pdb_elements
))
1165 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1168 if (!my_methods
|| !newpwd
|| !dn
) {
1169 return NT_STATUS_INVALID_PARAMETER
;
1173 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1174 /* may be password change below however */
1178 smbldap_set_mod(&mods
, LDAP_MOD_ADD
,
1181 rc
= smbldap_add(ldap_state
->smbldap_state
,
1184 case LDAP_MOD_REPLACE
:
1185 rc
= smbldap_modify(ldap_state
->smbldap_state
,
1189 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1191 return NT_STATUS_INVALID_PARAMETER
;
1194 if (rc
!=LDAP_SUCCESS
) {
1195 char *ld_error
= NULL
;
1196 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1198 DEBUG(1, ("ldapsam_modify_entry: Failed to %s user dn= %s with: %s\n\t%s\n",
1199 ldap_op
== LDAP_MOD_ADD
? "add" : "modify",
1200 dn
, ldap_err2string(rc
),
1201 ld_error
?ld_error
:"unknown"));
1202 SAFE_FREE(ld_error
);
1203 return NT_STATUS_UNSUCCESSFUL
;
1207 if (!(pdb_get_acct_ctrl(newpwd
)&(ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
)) &&
1208 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF
) &&
1209 need_update(newpwd
, PDB_PLAINTEXT_PW
) &&
1210 (pdb_get_plaintext_passwd(newpwd
)!=NULL
)) {
1214 struct berval
*retdata
;
1215 char *utf8_password
;
1218 if (push_utf8_allocate(&utf8_password
, pdb_get_plaintext_passwd(newpwd
)) == (size_t)-1) {
1219 return NT_STATUS_NO_MEMORY
;
1222 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1223 return NT_STATUS_NO_MEMORY
;
1226 if ((ber
= ber_alloc_t(LBER_USE_DER
))==NULL
) {
1227 DEBUG(0,("ber_alloc_t returns NULL\n"));
1228 SAFE_FREE(utf8_password
);
1229 return NT_STATUS_UNSUCCESSFUL
;
1232 ber_printf (ber
, "{");
1233 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID
, utf8_dn
);
1234 ber_printf (ber
, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
, utf8_password
);
1235 ber_printf (ber
, "N}");
1237 if ((rc
= ber_flatten (ber
, &bv
))<0) {
1238 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1241 SAFE_FREE(utf8_password
);
1242 return NT_STATUS_UNSUCCESSFUL
;
1246 SAFE_FREE(utf8_password
);
1249 if ((rc
= smbldap_extended_operation(ldap_state
->smbldap_state
,
1250 LDAP_EXOP_MODIFY_PASSWD
,
1251 bv
, NULL
, NULL
, &retoid
,
1252 &retdata
)) != LDAP_SUCCESS
) {
1253 char *ld_error
= NULL
;
1254 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1256 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1257 pdb_get_username(newpwd
), ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
1258 SAFE_FREE(ld_error
);
1260 return NT_STATUS_UNSUCCESSFUL
;
1262 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd
)));
1263 #ifdef DEBUG_PASSWORD
1264 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd
)));
1266 ber_bvfree(retdata
);
1267 ber_memfree(retoid
);
1271 return NT_STATUS_OK
;
1274 /**********************************************************************
1275 Delete entry from LDAP for username.
1276 *********************************************************************/
1278 static NTSTATUS
ldapsam_delete_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* sam_acct
)
1280 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1283 LDAPMessage
*result
= NULL
;
1289 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1290 return NT_STATUS_INVALID_PARAMETER
;
1293 sname
= pdb_get_username(sam_acct
);
1295 DEBUG (3, ("ldapsam_delete_sam_account: Deleting user %s from LDAP.\n", sname
));
1297 attr_list
= get_userattr_list( ldap_state
->schema_ver
);
1298 rc
= ldapsam_search_suffix_by_name(ldap_state
, sname
, &result
, attr_list
);
1300 if (rc
!= LDAP_SUCCESS
) {
1301 free_attr_list( attr_list
);
1302 return NT_STATUS_NO_SUCH_USER
;
1305 switch ( ldap_state
->schema_ver
) {
1306 case SCHEMAVER_SAMBASAMACCOUNT
:
1307 fstrcpy( objclass
, LDAP_OBJ_SAMBASAMACCOUNT
);
1310 case SCHEMAVER_SAMBAACCOUNT
:
1311 fstrcpy( objclass
, LDAP_OBJ_SAMBAACCOUNT
);
1314 fstrcpy( objclass
, "UNKNOWN" );
1315 DEBUG(0,("ldapsam_delete_sam_account: Unknown schema version specified!\n"));
1319 ret
= ldapsam_delete_entry(ldap_state
, result
, objclass
, attr_list
);
1320 ldap_msgfree(result
);
1321 free_attr_list( attr_list
);
1326 /**********************************************************************
1327 Helper function to determine for update_sam_account whether
1328 we need LDAP modification.
1329 *********************************************************************/
1331 static BOOL
element_is_changed(const SAM_ACCOUNT
*sampass
,
1332 enum pdb_elements element
)
1334 return IS_SAM_CHANGED(sampass
, element
);
1337 /**********************************************************************
1339 *********************************************************************/
1341 static NTSTATUS
ldapsam_update_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* newpwd
)
1343 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1344 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1347 LDAPMessage
*result
= NULL
;
1348 LDAPMessage
*entry
= NULL
;
1349 LDAPMod
**mods
= NULL
;
1352 result
= pdb_get_backend_private_data(newpwd
, my_methods
);
1354 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1355 rc
= ldapsam_search_suffix_by_name(ldap_state
, pdb_get_username(newpwd
), &result
, attr_list
);
1356 free_attr_list( attr_list
);
1357 if (rc
!= LDAP_SUCCESS
) {
1358 return NT_STATUS_UNSUCCESSFUL
;
1360 pdb_set_backend_private_data(newpwd
, result
, private_data_free_fn
, my_methods
, PDB_CHANGED
);
1363 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1364 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1365 return NT_STATUS_UNSUCCESSFUL
;
1368 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1369 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1371 return NT_STATUS_UNSUCCESSFUL
;
1374 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd
), dn
));
1376 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1377 element_is_changed
)) {
1378 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1381 ldap_mods_free(mods
,True
);
1382 return NT_STATUS_UNSUCCESSFUL
;
1386 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1387 pdb_get_username(newpwd
)));
1389 return NT_STATUS_OK
;
1392 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,LDAP_MOD_REPLACE
, element_is_changed
);
1393 ldap_mods_free(mods
,True
);
1396 if (!NT_STATUS_IS_OK(ret
)) {
1397 char *ld_error
= NULL
;
1398 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1400 DEBUG(0,("ldapsam_update_sam_account: failed to modify user with uid = %s, error: %s (%s)\n",
1401 pdb_get_username(newpwd
), ld_error
?ld_error
:"(unknwon)", ldap_err2string(rc
)));
1402 SAFE_FREE(ld_error
);
1406 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1407 pdb_get_username(newpwd
)));
1408 return NT_STATUS_OK
;
1411 /**********************************************************************
1412 Helper function to determine for update_sam_account whether
1413 we need LDAP modification.
1414 *********************************************************************/
1416 static BOOL
element_is_set_or_changed(const SAM_ACCOUNT
*sampass
,
1417 enum pdb_elements element
)
1419 return (IS_SAM_SET(sampass
, element
) ||
1420 IS_SAM_CHANGED(sampass
, element
));
1423 /**********************************************************************
1424 Add SAM_ACCOUNT to LDAP.
1425 *********************************************************************/
1427 static NTSTATUS
ldapsam_add_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* newpwd
)
1429 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
1430 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
1432 LDAPMessage
*result
= NULL
;
1433 LDAPMessage
*entry
= NULL
;
1435 LDAPMod
**mods
= NULL
;
1436 int ldap_op
= LDAP_MOD_REPLACE
;
1440 const char *username
= pdb_get_username(newpwd
);
1441 const DOM_SID
*sid
= pdb_get_user_sid(newpwd
);
1445 if (!username
|| !*username
) {
1446 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1447 return NT_STATUS_INVALID_PARAMETER
;
1450 /* free this list after the second search or in case we exit on failure */
1451 attr_list
= get_userattr_list(ldap_state
->schema_ver
);
1453 rc
= ldapsam_search_suffix_by_name (ldap_state
, username
, &result
, attr_list
);
1455 if (rc
!= LDAP_SUCCESS
) {
1456 free_attr_list( attr_list
);
1457 return NT_STATUS_UNSUCCESSFUL
;
1460 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1461 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1463 ldap_msgfree(result
);
1464 free_attr_list( attr_list
);
1465 return NT_STATUS_UNSUCCESSFUL
;
1467 ldap_msgfree(result
);
1470 if (element_is_set_or_changed(newpwd
, PDB_USERSID
)) {
1471 rc
= ldapsam_get_ldap_user_by_sid(ldap_state
,
1473 if (rc
== LDAP_SUCCESS
) {
1474 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) != 0) {
1475 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1476 sid_to_string(sid_string
, sid
)));
1477 free_attr_list( attr_list
);
1478 ldap_msgfree(result
);
1479 return NT_STATUS_UNSUCCESSFUL
;
1481 ldap_msgfree(result
);
1485 /* does the entry already exist but without a samba attributes?
1486 we need to return the samba attributes here */
1488 escape_user
= escape_ldap_string_alloc( username
);
1489 pstrcpy( filter
, lp_ldap_filter() );
1490 all_string_sub( filter
, "%u", escape_user
, sizeof(filter
) );
1491 SAFE_FREE( escape_user
);
1493 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1494 filter
, attr_list
, &result
);
1495 if ( rc
!= LDAP_SUCCESS
) {
1496 free_attr_list( attr_list
);
1497 return NT_STATUS_UNSUCCESSFUL
;
1500 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1502 if (num_result
> 1) {
1503 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1504 free_attr_list( attr_list
);
1505 ldap_msgfree(result
);
1506 return NT_STATUS_UNSUCCESSFUL
;
1509 /* Check if we need to update an existing entry */
1510 if (num_result
== 1) {
1513 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1514 ldap_op
= LDAP_MOD_REPLACE
;
1515 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1516 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1518 free_attr_list( attr_list
);
1519 ldap_msgfree(result
);
1520 return NT_STATUS_UNSUCCESSFUL
;
1522 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
1525 } else if (ldap_state
->schema_ver
== SCHEMAVER_SAMBASAMACCOUNT
) {
1527 /* There might be a SID for this account already - say an idmap entry */
1529 pstr_sprintf(filter
, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
1530 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
1531 sid_to_string(sid_string
, sid
),
1532 LDAP_OBJ_IDMAP_ENTRY
,
1533 LDAP_OBJ_SID_ENTRY
);
1535 /* free old result before doing a new search */
1536 if (result
!= NULL
) {
1537 ldap_msgfree(result
);
1540 rc
= smbldap_search_suffix(ldap_state
->smbldap_state
,
1541 filter
, attr_list
, &result
);
1543 if ( rc
!= LDAP_SUCCESS
) {
1544 free_attr_list( attr_list
);
1545 return NT_STATUS_UNSUCCESSFUL
;
1548 num_result
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1550 if (num_result
> 1) {
1551 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1552 free_attr_list( attr_list
);
1553 ldap_msgfree(result
);
1554 return NT_STATUS_UNSUCCESSFUL
;
1557 /* Check if we need to update an existing entry */
1558 if (num_result
== 1) {
1561 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1562 ldap_op
= LDAP_MOD_REPLACE
;
1563 entry
= ldap_first_entry (ldap_state
->smbldap_state
->ldap_struct
, result
);
1564 tmp
= smbldap_get_dn (ldap_state
->smbldap_state
->ldap_struct
, entry
);
1566 free_attr_list( attr_list
);
1567 ldap_msgfree(result
);
1568 return NT_STATUS_UNSUCCESSFUL
;
1570 slprintf (dn
, sizeof (dn
) - 1, "%s", tmp
);
1575 free_attr_list( attr_list
);
1577 if (num_result
== 0) {
1578 /* Check if we need to add an entry */
1579 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
1580 ldap_op
= LDAP_MOD_ADD
;
1581 if (username
[strlen(username
)-1] == '$') {
1582 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_machine_suffix ());
1584 slprintf (dn
, sizeof (dn
) - 1, "uid=%s,%s", username
, lp_ldap_user_suffix ());
1588 if (!init_ldap_from_sam(ldap_state
, entry
, &mods
, newpwd
,
1589 element_is_set_or_changed
)) {
1590 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
1591 ldap_msgfree(result
);
1593 ldap_mods_free(mods
,True
);
1594 return NT_STATUS_UNSUCCESSFUL
;
1597 ldap_msgfree(result
);
1600 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd
)));
1601 return NT_STATUS_UNSUCCESSFUL
;
1603 switch ( ldap_state
->schema_ver
) {
1604 case SCHEMAVER_SAMBAACCOUNT
:
1605 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBAACCOUNT
);
1607 case SCHEMAVER_SAMBASAMACCOUNT
:
1608 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT
);
1611 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
1615 ret
= ldapsam_modify_entry(my_methods
,newpwd
,dn
,mods
,ldap_op
, element_is_set_or_changed
);
1616 if (!NT_STATUS_IS_OK(ret
)) {
1617 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
1618 pdb_get_username(newpwd
),dn
));
1619 ldap_mods_free(mods
, True
);
1623 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd
)));
1624 ldap_mods_free(mods
, True
);
1626 return NT_STATUS_OK
;
1629 /**********************************************************************
1630 *********************************************************************/
1632 static int ldapsam_search_one_group (struct ldapsam_privates
*ldap_state
,
1634 LDAPMessage
** result
)
1636 int scope
= LDAP_SCOPE_SUBTREE
;
1640 attr_list
= get_attr_list(groupmap_attr_list
);
1641 rc
= smbldap_search(ldap_state
->smbldap_state
,
1642 lp_ldap_group_suffix (), scope
,
1643 filter
, attr_list
, 0, result
);
1644 free_attr_list( attr_list
);
1646 if (rc
!= LDAP_SUCCESS
) {
1647 char *ld_error
= NULL
;
1648 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1650 DEBUG(0, ("ldapsam_search_one_group: "
1651 "Problem during the LDAP search: LDAP error: %s (%s)\n",
1652 ld_error
?ld_error
:"(unknown)", ldap_err2string(rc
)));
1653 DEBUGADD(3, ("ldapsam_search_one_group: Query was: %s, %s\n",
1654 lp_ldap_group_suffix(), filter
));
1655 SAFE_FREE(ld_error
);
1661 /**********************************************************************
1662 *********************************************************************/
1664 static BOOL
init_group_from_ldap(struct ldapsam_privates
*ldap_state
,
1665 GROUP_MAP
*map
, LDAPMessage
*entry
)
1669 if (ldap_state
== NULL
|| map
== NULL
|| entry
== NULL
||
1670 ldap_state
->smbldap_state
->ldap_struct
== NULL
) {
1671 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
1675 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1676 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
), temp
)) {
1677 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1678 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
)));
1681 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp
));
1683 map
->gid
= (gid_t
)atol(temp
);
1685 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1686 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
), temp
)) {
1687 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1688 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_SID
)));
1692 if (!string_to_sid(&map
->sid
, temp
)) {
1693 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp
));
1697 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1698 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
), temp
)) {
1699 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
1700 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
)));
1703 map
->sid_name_use
= (enum SID_NAME_USE
)atol(temp
);
1705 if ((map
->sid_name_use
< SID_NAME_USER
) ||
1706 (map
->sid_name_use
> SID_NAME_UNKNOWN
)) {
1707 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map
->sid_name_use
));
1711 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1712 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), temp
)) {
1714 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1715 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_CN
), temp
))
1717 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
1718 for gidNumber(%lu)\n",(unsigned long)map
->gid
));
1722 fstrcpy(map
->nt_name
, temp
);
1724 if (!smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
1725 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DESC
), temp
)) {
1728 fstrcpy(map
->comment
, temp
);
1733 /**********************************************************************
1734 *********************************************************************/
1736 static BOOL
init_ldap_from_group(LDAP
*ldap_struct
,
1737 LDAPMessage
*existing
,
1739 const GROUP_MAP
*map
)
1743 if (mods
== NULL
|| map
== NULL
) {
1744 DEBUG(0, ("init_ldap_from_group: NULL parameters found!\n"));
1750 sid_to_string(tmp
, &map
->sid
);
1752 smbldap_make_mod(ldap_struct
, existing
, mods
,
1753 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
), tmp
);
1754 pstr_sprintf(tmp
, "%i", map
->sid_name_use
);
1755 smbldap_make_mod(ldap_struct
, existing
, mods
,
1756 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_TYPE
), tmp
);
1758 smbldap_make_mod(ldap_struct
, existing
, mods
,
1759 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), map
->nt_name
);
1760 smbldap_make_mod(ldap_struct
, existing
, mods
,
1761 get_attr_key2string( groupmap_attr_list
, LDAP_ATTR_DESC
), map
->comment
);
1766 /**********************************************************************
1767 *********************************************************************/
1769 static NTSTATUS
ldapsam_getgroup(struct pdb_methods
*methods
,
1773 struct ldapsam_privates
*ldap_state
=
1774 (struct ldapsam_privates
*)methods
->private_data
;
1775 LDAPMessage
*result
= NULL
;
1776 LDAPMessage
*entry
= NULL
;
1779 if (ldapsam_search_one_group(ldap_state
, filter
, &result
)
1781 return NT_STATUS_NO_SUCH_GROUP
;
1784 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1787 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
1788 ldap_msgfree(result
);
1789 return NT_STATUS_NO_SUCH_GROUP
;
1793 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: count=%d\n",
1795 ldap_msgfree(result
);
1796 return NT_STATUS_NO_SUCH_GROUP
;
1799 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1802 ldap_msgfree(result
);
1803 return NT_STATUS_UNSUCCESSFUL
;
1806 if (!init_group_from_ldap(ldap_state
, map
, entry
)) {
1807 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for group filter %s\n",
1809 ldap_msgfree(result
);
1810 return NT_STATUS_NO_SUCH_GROUP
;
1813 ldap_msgfree(result
);
1814 return NT_STATUS_OK
;
1817 /**********************************************************************
1818 *********************************************************************/
1820 static NTSTATUS
ldapsam_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1825 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%s))",
1827 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GROUP_SID
),
1828 sid_string_static(&sid
));
1830 return ldapsam_getgroup(methods
, filter
, map
);
1833 /**********************************************************************
1834 *********************************************************************/
1836 static NTSTATUS
ldapsam_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1841 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%lu))",
1843 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
1844 (unsigned long)gid
);
1846 return ldapsam_getgroup(methods
, filter
, map
);
1849 /**********************************************************************
1850 *********************************************************************/
1852 static NTSTATUS
ldapsam_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1856 char *escape_name
= escape_ldap_string_alloc(name
);
1859 return NT_STATUS_NO_MEMORY
;
1862 pstr_sprintf(filter
, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
1864 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_DISPLAY_NAME
), escape_name
,
1865 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_CN
), escape_name
);
1867 SAFE_FREE(escape_name
);
1869 return ldapsam_getgroup(methods
, filter
, map
);
1872 /**********************************************************************
1873 *********************************************************************/
1875 static int ldapsam_search_one_group_by_gid(struct ldapsam_privates
*ldap_state
,
1877 LDAPMessage
**result
)
1881 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%lu))",
1882 LDAP_OBJ_POSIXGROUP
,
1883 get_attr_key2string(groupmap_attr_list
, LDAP_ATTR_GIDNUMBER
),
1884 (unsigned long)gid
);
1886 return ldapsam_search_one_group(ldap_state
, filter
, result
);
1889 /**********************************************************************
1890 *********************************************************************/
1892 static NTSTATUS
ldapsam_add_group_mapping_entry(struct pdb_methods
*methods
,
1895 struct ldapsam_privates
*ldap_state
=
1896 (struct ldapsam_privates
*)methods
->private_data
;
1897 LDAPMessage
*result
= NULL
;
1898 LDAPMod
**mods
= NULL
;
1909 if (NT_STATUS_IS_OK(ldapsam_getgrgid(methods
, &dummy
,
1911 DEBUG(0, ("ldapsam_add_group_mapping_entry: Group %ld already exists in LDAP\n", (unsigned long)map
->gid
));
1912 return NT_STATUS_UNSUCCESSFUL
;
1915 rc
= ldapsam_search_one_group_by_gid(ldap_state
, map
->gid
, &result
);
1916 if (rc
!= LDAP_SUCCESS
) {
1917 ldap_msgfree(result
);
1918 return NT_STATUS_UNSUCCESSFUL
;
1921 count
= ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
);
1924 ldap_msgfree(result
);
1925 return NT_STATUS_UNSUCCESSFUL
;
1929 DEBUG(2, ("ldapsam_add_group_mapping_entry: Group %lu must exist exactly once in LDAP\n",
1930 (unsigned long)map
->gid
));
1931 ldap_msgfree(result
);
1932 return NT_STATUS_UNSUCCESSFUL
;
1935 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
1936 tmp
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
1938 ldap_msgfree(result
);
1939 return NT_STATUS_UNSUCCESSFUL
;
1944 if (!init_ldap_from_group(ldap_state
->smbldap_state
->ldap_struct
,
1945 result
, &mods
, map
)) {
1946 DEBUG(0, ("ldapsam_add_group_mapping_entry: init_ldap_from_group failed!\n"));
1947 ldap_mods_free(mods
, True
);
1948 ldap_msgfree(result
);
1949 return NT_STATUS_UNSUCCESSFUL
;
1952 ldap_msgfree(result
);
1955 DEBUG(0, ("ldapsam_add_group_mapping_entry: mods is empty\n"));
1956 return NT_STATUS_UNSUCCESSFUL
;
1959 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1961 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
1962 ldap_mods_free(mods
, True
);
1964 if (rc
!= LDAP_SUCCESS
) {
1965 char *ld_error
= NULL
;
1966 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
1968 DEBUG(0, ("ldapsam_add_group_mapping_entry: failed to add group %lu error: %s (%s)\n", (unsigned long)map
->gid
,
1969 ld_error
? ld_error
: "(unknown)", ldap_err2string(rc
)));
1970 SAFE_FREE(ld_error
);
1971 return NT_STATUS_UNSUCCESSFUL
;
1974 DEBUG(2, ("ldapsam_add_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map
->gid
));
1975 return NT_STATUS_OK
;
1978 /**********************************************************************
1979 *********************************************************************/
1981 static NTSTATUS
ldapsam_update_group_mapping_entry(struct pdb_methods
*methods
,
1984 struct ldapsam_privates
*ldap_state
=
1985 (struct ldapsam_privates
*)methods
->private_data
;
1988 LDAPMessage
*result
= NULL
;
1989 LDAPMessage
*entry
= NULL
;
1990 LDAPMod
**mods
= NULL
;
1992 rc
= ldapsam_search_one_group_by_gid(ldap_state
, map
->gid
, &result
);
1994 if (rc
!= LDAP_SUCCESS
) {
1995 return NT_STATUS_UNSUCCESSFUL
;
1998 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
1999 DEBUG(0, ("ldapsam_update_group_mapping_entry: No group to modify!\n"));
2000 ldap_msgfree(result
);
2001 return NT_STATUS_UNSUCCESSFUL
;
2004 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2006 if (!init_ldap_from_group(ldap_state
->smbldap_state
->ldap_struct
,
2007 result
, &mods
, map
)) {
2008 DEBUG(0, ("ldapsam_update_group_mapping_entry: init_ldap_from_group failed\n"));
2009 ldap_msgfree(result
);
2011 ldap_mods_free(mods
,True
);
2012 return NT_STATUS_UNSUCCESSFUL
;
2016 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: nothing to do\n"));
2017 ldap_msgfree(result
);
2018 return NT_STATUS_OK
;
2021 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
2023 ldap_msgfree(result
);
2024 return NT_STATUS_UNSUCCESSFUL
;
2026 rc
= smbldap_modify(ldap_state
->smbldap_state
, dn
, mods
);
2029 ldap_mods_free(mods
, True
);
2030 ldap_msgfree(result
);
2032 if (rc
!= LDAP_SUCCESS
) {
2033 char *ld_error
= NULL
;
2034 ldap_get_option(ldap_state
->smbldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
2036 DEBUG(0, ("ldapsam_update_group_mapping_entry: failed to modify group %lu error: %s (%s)\n", (unsigned long)map
->gid
,
2037 ld_error
? ld_error
: "(unknown)", ldap_err2string(rc
)));
2038 SAFE_FREE(ld_error
);
2039 return NT_STATUS_UNSUCCESSFUL
;
2042 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified group %lu in LDAP\n", (unsigned long)map
->gid
));
2043 return NT_STATUS_OK
;
2046 /**********************************************************************
2047 *********************************************************************/
2049 static NTSTATUS
ldapsam_delete_group_mapping_entry(struct pdb_methods
*methods
,
2052 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)methods
->private_data
;
2053 pstring sidstring
, filter
;
2054 LDAPMessage
*result
= NULL
;
2059 sid_to_string(sidstring
, &sid
);
2061 pstr_sprintf(filter
, "(&(objectClass=%s)(%s=%s))",
2062 LDAP_OBJ_GROUPMAP
, LDAP_ATTRIBUTE_SID
, sidstring
);
2064 rc
= ldapsam_search_one_group(ldap_state
, filter
, &result
);
2066 if (rc
!= LDAP_SUCCESS
) {
2067 return NT_STATUS_NO_SUCH_GROUP
;
2070 attr_list
= get_attr_list( groupmap_attr_list_to_delete
);
2071 ret
= ldapsam_delete_entry(ldap_state
, result
, LDAP_OBJ_GROUPMAP
, attr_list
);
2072 free_attr_list ( attr_list
);
2074 ldap_msgfree(result
);
2079 /**********************************************************************
2080 *********************************************************************/
2082 static NTSTATUS
ldapsam_setsamgrent(struct pdb_methods
*my_methods
, BOOL update
)
2084 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2089 pstr_sprintf( filter
, "(objectclass=%s)", LDAP_OBJ_GROUPMAP
);
2090 attr_list
= get_attr_list( groupmap_attr_list
);
2091 rc
= smbldap_search(ldap_state
->smbldap_state
, lp_ldap_group_suffix(),
2092 LDAP_SCOPE_SUBTREE
, filter
,
2093 attr_list
, 0, &ldap_state
->result
);
2094 free_attr_list( attr_list
);
2096 if (rc
!= LDAP_SUCCESS
) {
2097 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n", ldap_err2string(rc
)));
2098 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n", lp_ldap_group_suffix(), filter
));
2099 ldap_msgfree(ldap_state
->result
);
2100 ldap_state
->result
= NULL
;
2101 return NT_STATUS_UNSUCCESSFUL
;
2104 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base!\n",
2105 ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
,
2106 ldap_state
->result
)));
2108 ldap_state
->entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, ldap_state
->result
);
2109 ldap_state
->index
= 0;
2111 return NT_STATUS_OK
;
2114 /**********************************************************************
2115 *********************************************************************/
2117 static void ldapsam_endsamgrent(struct pdb_methods
*my_methods
)
2119 ldapsam_endsampwent(my_methods
);
2122 /**********************************************************************
2123 *********************************************************************/
2125 static NTSTATUS
ldapsam_getsamgrent(struct pdb_methods
*my_methods
,
2128 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
2129 struct ldapsam_privates
*ldap_state
= (struct ldapsam_privates
*)my_methods
->private_data
;
2133 if (!ldap_state
->entry
)
2136 ldap_state
->index
++;
2137 bret
= init_group_from_ldap(ldap_state
, map
, ldap_state
->entry
);
2139 ldap_state
->entry
= ldap_next_entry(ldap_state
->smbldap_state
->ldap_struct
,
2143 return NT_STATUS_OK
;
2146 /**********************************************************************
2147 *********************************************************************/
2149 static NTSTATUS
ldapsam_enum_group_mapping(struct pdb_methods
*methods
,
2150 enum SID_NAME_USE sid_name_use
,
2151 GROUP_MAP
**rmap
, int *num_entries
,
2161 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods
, False
))) {
2162 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open passdb\n"));
2163 return NT_STATUS_ACCESS_DENIED
;
2166 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods
, &map
))) {
2167 if (sid_name_use
!= SID_NAME_UNKNOWN
&&
2168 sid_name_use
!= map
.sid_name_use
) {
2169 DEBUG(11,("ldapsam_enum_group_mapping: group %s is not of the requested type\n", map
.nt_name
));
2172 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
2173 DEBUG(11,("ldapsam_enum_group_mapping: group %s is non mapped\n", map
.nt_name
));
2177 mapt
=(GROUP_MAP
*)Realloc((*rmap
), (entries
+1)*sizeof(GROUP_MAP
));
2179 DEBUG(0,("ldapsam_enum_group_mapping: Unable to enlarge group map!\n"));
2181 return NT_STATUS_UNSUCCESSFUL
;
2186 mapt
[entries
] = map
;
2191 ldapsam_endsamgrent(methods
);
2193 *num_entries
= entries
;
2195 return NT_STATUS_OK
;
2198 /**********************************************************************
2200 *********************************************************************/
2202 static void free_private_data(void **vp
)
2204 struct ldapsam_privates
**ldap_state
= (struct ldapsam_privates
**)vp
;
2206 smbldap_free_struct(&(*ldap_state
)->smbldap_state
);
2208 if ((*ldap_state
)->result
!= NULL
) {
2209 ldap_msgfree((*ldap_state
)->result
);
2210 (*ldap_state
)->result
= NULL
;
2215 /* No need to free any further, as it is talloc()ed */
2218 /**********************************************************************
2219 Intitalise the parts of the pdb_context that are common to all pdb_ldap modes
2220 *********************************************************************/
2222 static NTSTATUS
pdb_init_ldapsam_common(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
,
2223 const char *location
)
2226 struct ldapsam_privates
*ldap_state
;
2228 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_methods(pdb_context
->mem_ctx
, pdb_method
))) {
2232 (*pdb_method
)->name
= "ldapsam";
2234 (*pdb_method
)->setsampwent
= ldapsam_setsampwent
;
2235 (*pdb_method
)->endsampwent
= ldapsam_endsampwent
;
2236 (*pdb_method
)->getsampwent
= ldapsam_getsampwent
;
2237 (*pdb_method
)->getsampwnam
= ldapsam_getsampwnam
;
2238 (*pdb_method
)->getsampwsid
= ldapsam_getsampwsid
;
2239 (*pdb_method
)->add_sam_account
= ldapsam_add_sam_account
;
2240 (*pdb_method
)->update_sam_account
= ldapsam_update_sam_account
;
2241 (*pdb_method
)->delete_sam_account
= ldapsam_delete_sam_account
;
2243 (*pdb_method
)->getgrsid
= ldapsam_getgrsid
;
2244 (*pdb_method
)->getgrgid
= ldapsam_getgrgid
;
2245 (*pdb_method
)->getgrnam
= ldapsam_getgrnam
;
2246 (*pdb_method
)->add_group_mapping_entry
= ldapsam_add_group_mapping_entry
;
2247 (*pdb_method
)->update_group_mapping_entry
= ldapsam_update_group_mapping_entry
;
2248 (*pdb_method
)->delete_group_mapping_entry
= ldapsam_delete_group_mapping_entry
;
2249 (*pdb_method
)->enum_group_mapping
= ldapsam_enum_group_mapping
;
2251 /* TODO: Setup private data and free */
2253 ldap_state
= talloc_zero(pdb_context
->mem_ctx
, sizeof(*ldap_state
));
2255 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
2256 return NT_STATUS_NO_MEMORY
;
2259 if (!NT_STATUS_IS_OK(nt_status
=
2260 smbldap_init(pdb_context
->mem_ctx
, location
,
2261 &ldap_state
->smbldap_state
)));
2263 ldap_state
->domain_name
= talloc_strdup(pdb_context
->mem_ctx
, get_global_sam_name());
2264 if (!ldap_state
->domain_name
) {
2265 return NT_STATUS_NO_MEMORY
;
2268 (*pdb_method
)->private_data
= ldap_state
;
2270 (*pdb_method
)->free_private_data
= free_private_data
;
2272 return NT_STATUS_OK
;
2275 /**********************************************************************
2276 Initialise the 'compat' mode for pdb_ldap
2277 *********************************************************************/
2279 static NTSTATUS
pdb_init_ldapsam_compat(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
2282 struct ldapsam_privates
*ldap_state
;
2284 #ifdef WITH_LDAP_SAMCONFIG
2286 int ldap_port
= lp_ldap_port();
2288 /* remap default port if not using SSL (ie clear or TLS) */
2289 if ( (lp_ldap_ssl() != LDAP_SSL_ON
) && (ldap_port
== 636) ) {
2293 location
= talloc_asprintf(pdb_context
->mem_ctx
, "%s://%s:%d", lp_ldap_ssl() == LDAP_SSL_ON
? "ldaps" : "ldap", lp_ldap_server(), ldap_port
);
2295 return NT_STATUS_NO_MEMORY
;
2300 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_ldapsam_common(pdb_context
, pdb_method
, location
))) {
2304 (*pdb_method
)->name
= "ldapsam_compat";
2306 ldap_state
= (*pdb_method
)->private_data
;
2307 ldap_state
->schema_ver
= SCHEMAVER_SAMBAACCOUNT
;
2309 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
2311 return NT_STATUS_OK
;
2314 /**********************************************************************
2315 Initialise the normal mode for pdb_ldap
2316 *********************************************************************/
2318 static NTSTATUS
pdb_init_ldapsam(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
2321 struct ldapsam_privates
*ldap_state
;
2322 uint32 alg_rid_base
;
2323 pstring alg_rid_base_string
;
2324 LDAPMessage
*result
= NULL
;
2325 LDAPMessage
*entry
= NULL
;
2326 DOM_SID ldap_domain_sid
;
2327 DOM_SID secrets_domain_sid
;
2328 pstring domain_sid_string
;
2330 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_ldapsam_common(pdb_context
, pdb_method
, location
))) {
2334 (*pdb_method
)->name
= "ldapsam";
2336 ldap_state
= (*pdb_method
)->private_data
;
2337 ldap_state
->schema_ver
= SCHEMAVER_SAMBASAMACCOUNT
;
2339 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
2341 nt_status
= smbldap_search_domain_info(ldap_state
->smbldap_state
, &result
,
2342 ldap_state
->domain_name
, True
);
2344 if ( !NT_STATUS_IS_OK(nt_status
) ) {
2345 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain info, nor add one to the domain\n"));
2346 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, will be unable to allocate new users/groups, \
2347 and will risk BDCs having inconsistant SIDs\n"));
2348 sid_copy(&ldap_state
->domain_sid
, get_global_sam_sid());
2349 return NT_STATUS_OK
;
2352 /* Given that the above might fail, everything below this must be optional */
2354 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
2356 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info entry\n"));
2357 ldap_msgfree(result
);
2358 return NT_STATUS_UNSUCCESSFUL
;
2361 if (smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2362 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_USER_SID
),
2363 domain_sid_string
)) {
2365 if (!string_to_sid(&ldap_domain_sid
, domain_sid_string
)) {
2366 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be read as a valid SID\n", domain_sid_string
));
2367 return NT_STATUS_INVALID_PARAMETER
;
2369 found_sid
= secrets_fetch_domain_sid(ldap_state
->domain_name
, &secrets_domain_sid
);
2370 if (!found_sid
|| !sid_equal(&secrets_domain_sid
, &ldap_domain_sid
)) {
2371 /* reset secrets.tdb sid */
2372 secrets_store_domain_sid(ldap_state
->domain_name
, &ldap_domain_sid
);
2374 sid_copy(&ldap_state
->domain_sid
, &ldap_domain_sid
);
2377 if (smbldap_get_single_attribute(ldap_state
->smbldap_state
->ldap_struct
, entry
,
2378 get_userattr_key2string(ldap_state
->schema_ver
, LDAP_ATTR_ALGORITHMIC_RID_BASE
),
2379 alg_rid_base_string
)) {
2380 alg_rid_base
= (uint32
)atol(alg_rid_base_string
);
2381 if (alg_rid_base
!= algorithmic_rid_base()) {
2382 DEBUG(0, ("The value of 'algorithmic RID base' has changed since the LDAP\n"
2383 "database was initialised. Aborting. \n"));
2384 ldap_msgfree(result
);
2385 return NT_STATUS_UNSUCCESSFUL
;
2388 ldap_msgfree(result
);
2390 return NT_STATUS_OK
;
2393 NTSTATUS
pdb_ldap_init(void)
2396 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam", pdb_init_ldapsam
)))
2399 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "ldapsam_compat", pdb_init_ldapsam_compat
)))
2402 return NT_STATUS_OK
;