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
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/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
49 #define DBGC_CLASS DBGC_PASSDB
55 * Work around versions of the LDAP client libs that don't have the OIDs
56 * defined, or have them defined under the old name.
57 * This functionality is really a factor of the server, not the client
61 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
62 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
63 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
64 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
67 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
68 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
69 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
70 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
73 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
75 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
76 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
82 /**********************************************************************
83 Simple helper function to make stuff better readable
84 **********************************************************************/
86 static LDAP *priv2ld(struct ldapsam_privates *priv)
88 return priv->smbldap_state->ldap_struct;
91 /**********************************************************************
92 Get the attribute name given a user schame version.
93 **********************************************************************/
95 static const char* get_userattr_key2string( int schema_ver, int key )
97 switch ( schema_ver ) {
98 case SCHEMAVER_SAMBAACCOUNT:
99 return get_attr_key2string( attrib_map_v22, key );
101 case SCHEMAVER_SAMBASAMACCOUNT:
102 return get_attr_key2string( attrib_map_v30, key );
105 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
111 /**********************************************************************
112 Return the list of attribute names given a user schema version.
113 **********************************************************************/
115 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
117 switch ( schema_ver ) {
118 case SCHEMAVER_SAMBAACCOUNT:
119 return get_attr_list( mem_ctx, attrib_map_v22 );
121 case SCHEMAVER_SAMBASAMACCOUNT:
122 return get_attr_list( mem_ctx, attrib_map_v30 );
124 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
131 /**************************************************************************
132 Return the list of attribute names to delete given a user schema version.
133 **************************************************************************/
135 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
138 switch ( schema_ver ) {
139 case SCHEMAVER_SAMBAACCOUNT:
140 return get_attr_list( mem_ctx,
141 attrib_map_to_delete_v22 );
143 case SCHEMAVER_SAMBASAMACCOUNT:
144 return get_attr_list( mem_ctx,
145 attrib_map_to_delete_v30 );
147 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
155 /*******************************************************************
156 Generate the LDAP search filter for the objectclass based on the
157 version of the schema we are using.
158 ******************************************************************/
160 static const char* get_objclass_filter( int schema_ver )
162 static fstring objclass_filter;
164 switch( schema_ver ) {
165 case SCHEMAVER_SAMBAACCOUNT:
166 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
168 case SCHEMAVER_SAMBASAMACCOUNT:
169 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
172 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
176 return objclass_filter;
179 /*****************************************************************
180 Scan a sequence number off OpenLDAP's syncrepl contextCSN
181 ******************************************************************/
183 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
185 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
186 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
187 LDAPMessage *msg = NULL;
188 LDAPMessage *entry = NULL;
190 char **values = NULL;
191 int rc, num_result, num_values, rid;
197 /* Unfortunatly there is no proper way to detect syncrepl-support in
198 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
199 * but do not show up in the root-DSE yet. Neither we can query the
200 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
201 * objectclass. Currently we require lp_ldap_suffix() to show up as
202 * namingContext. - Guenther
205 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
210 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
214 if (!smbldap_has_naming_context(ldap_state->smbldap_state, lp_ldap_suffix())) {
215 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
216 "as top-level namingContext\n", lp_ldap_suffix()));
220 mem_ctx = talloc_init("ldapsam_get_seq_num");
223 return NT_STATUS_NO_MEMORY;
225 attrs = TALLOC_ARRAY(mem_ctx, const char *, 2);
227 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
228 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
231 /* consumer syncreplCookie: */
232 /* csn=20050126161620Z#0000001#00#00000 */
233 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
235 pstr_sprintf( suffix, "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
239 /* provider contextCSN */
240 /* 20050126161620Z#000009#00#000000 */
241 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
243 pstr_sprintf( suffix, "cn=ldapsync,%s", lp_ldap_suffix());
247 rc = smbldap_search(ldap_state->smbldap_state, suffix,
248 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
250 if (rc != LDAP_SUCCESS) {
254 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
255 if (num_result != 1) {
256 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
260 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
262 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
266 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
267 if (values == NULL) {
268 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
272 num_values = ldap_count_values(values);
273 if (num_values == 0) {
274 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
279 if (!next_token(&p, tok, "#", sizeof(tok))) {
280 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
285 if (!strncmp(p, "csn=", strlen("csn=")))
288 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
290 *seq_num = generalized_to_unix_time(p);
292 /* very basic sanity check */
294 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
299 ntstatus = NT_STATUS_OK;
303 ldap_value_free(values);
307 talloc_destroy(mem_ctx);
312 /*******************************************************************
313 Run the search by name.
314 ******************************************************************/
316 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
318 LDAPMessage ** result,
322 char *escape_user = escape_ldap_string_alloc(user);
325 return LDAP_NO_MEMORY;
329 * in the filter expression, replace %u with the real name
330 * so in ldap filter, %u MUST exist :-)
332 pstr_sprintf(filter, "(&%s%s)", "(uid=%u)",
333 get_objclass_filter(ldap_state->schema_ver));
336 * have to use this here because $ is filtered out
341 all_string_sub(filter, "%u", escape_user, sizeof(pstring));
342 SAFE_FREE(escape_user);
344 return smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
347 /*******************************************************************
348 Run the search by rid.
349 ******************************************************************/
351 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
352 uint32 rid, LDAPMessage ** result,
358 pstr_sprintf(filter, "(&(rid=%i)%s)", rid,
359 get_objclass_filter(ldap_state->schema_ver));
361 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
366 /*******************************************************************
367 Run the search by SID.
368 ******************************************************************/
370 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
371 const DOM_SID *sid, LDAPMessage ** result,
378 pstr_sprintf(filter, "(&(%s=%s)%s)",
379 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
380 sid_to_string(sid_string, sid),
381 get_objclass_filter(ldap_state->schema_ver));
383 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
388 /*******************************************************************
389 Delete complete object or objectclass and attrs from
390 object found in search_result depending on lp_ldap_delete_dn
391 ******************************************************************/
393 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
396 const char *objectclass,
399 LDAPMod **mods = NULL;
402 BerElement *ptr = NULL;
404 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
406 return LDAP_NO_MEMORY;
409 if (lp_ldap_delete_dn()) {
410 return smbldap_delete(priv->smbldap_state, dn);
413 /* Ok, delete only the SAM attributes */
415 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
417 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
420 /* We are only allowed to delete the attributes that
423 for (attrib = attrs; *attrib != NULL; attrib++) {
424 if (strequal(*attrib, name)) {
425 DEBUG(10, ("ldapsam_delete_entry: deleting "
426 "attribute %s\n", name));
427 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
438 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
439 talloc_autofree_ldapmod(mem_ctx, mods);
441 return smbldap_modify(priv->smbldap_state, dn, mods);
444 /* New Interface is being implemented here */
446 #if 0 /* JERRY - not uesed anymore */
448 /**********************************************************************
449 Initialize struct samu from an LDAP query (unix attributes only)
450 *********************************************************************/
451 static BOOL get_unix_attributes (struct ldapsam_privates *ldap_state,
452 struct samu * sampass,
461 if ((ldap_values = ldap_get_values (ldap_state->smbldap_state->ldap_struct, entry, "objectClass")) == NULL) {
462 DEBUG (1, ("get_unix_attributes: no objectClass! \n"));
466 for (values=ldap_values;*values;values++) {
467 if (strequal(*values, LDAP_OBJ_POSIXACCOUNT )) {
472 if (!*values) { /*end of array, no posixAccount */
473 DEBUG(10, ("user does not have %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
474 ldap_value_free(ldap_values);
477 ldap_value_free(ldap_values);
479 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
480 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_UNIX_HOME), homedir) )
485 if ( !smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
486 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_GIDNUMBER), temp) )
491 *gid = (gid_t)atol(temp);
493 pdb_set_unix_homedir(sampass, homedir, PDB_SET);
495 DEBUG(10, ("user has %s attributes\n", LDAP_OBJ_POSIXACCOUNT));
502 static time_t ldapsam_get_entry_timestamp(
503 struct ldapsam_privates *ldap_state,
509 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
510 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
514 strptime(temp, "%Y%m%d%H%M%SZ", &tm);
519 /**********************************************************************
520 Initialize struct samu from an LDAP query.
521 (Based on init_sam_from_buffer in pdb_tdb.c)
522 *********************************************************************/
524 static BOOL init_sam_from_ldap(struct ldapsam_privates *ldap_state,
525 struct samu * sampass,
532 pass_can_change_time,
533 pass_must_change_time,
546 char munged_dial[2048];
548 uint8 smblmpwd[LM_HASH_LEN],
549 smbntpwd[NT_HASH_LEN];
550 BOOL use_samba_attrs = True;
551 uint16 acct_ctrl = 0,
553 uint16 bad_password_count = 0,
556 uint8 hours[MAX_HOURS_LEN];
558 LOGIN_CACHE *cache_entry = NULL;
561 BOOL expand_explicit = lp_passdb_expand_explicit();
564 * do a little initialization
568 nt_username[0] = '\0';
572 logon_script[0] = '\0';
573 profile_path[0] = '\0';
575 munged_dial[0] = '\0';
576 workstations[0] = '\0';
579 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
580 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
584 if (priv2ld(ldap_state) == NULL) {
585 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
586 "ldap_struct is NULL!\n"));
590 if (!smbldap_get_single_pstring(priv2ld(ldap_state), entry, "uid",
592 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
597 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
599 pstrcpy(nt_username, username);
601 pstrcpy(domain, ldap_state->domain_name);
603 pdb_set_username(sampass, username, PDB_SET);
605 pdb_set_domain(sampass, domain, PDB_DEFAULT);
606 pdb_set_nt_username(sampass, nt_username, PDB_SET);
608 /* deal with different attributes between the schema first */
610 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
611 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
612 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID), temp)) {
613 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
616 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
617 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_SID), temp)) {
618 pdb_set_group_sid_from_string(sampass, temp, PDB_SET);
620 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
623 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
624 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID), temp)) {
625 user_rid = (uint32)atol(temp);
626 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
629 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
630 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PRIMARY_GROUP_RID), temp)) {
631 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
635 group_rid = (uint32)atol(temp);
637 /* for some reason, we often have 0 as a primary group RID.
638 Make sure that we treat this just as a 'default' value */
641 pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
643 pdb_set_group_sid_from_rid(sampass, DOMAIN_GROUP_RID_USERS, PDB_DEFAULT);
647 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
648 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
649 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
650 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
655 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
656 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET), temp)) {
657 /* leave as default */
659 pass_last_set_time = (time_t) atol(temp);
660 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
663 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
664 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp)) {
665 /* leave as default */
667 logon_time = (time_t) atol(temp);
668 pdb_set_logon_time(sampass, logon_time, PDB_SET);
671 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
672 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp)) {
673 /* leave as default */
675 logoff_time = (time_t) atol(temp);
676 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
679 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
680 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp)) {
681 /* leave as default */
683 kickoff_time = (time_t) atol(temp);
684 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
687 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
688 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp)) {
689 /* leave as default */
691 pass_can_change_time = (time_t) atol(temp);
692 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
695 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
696 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp)) {
697 /* leave as default */
699 pass_must_change_time = (time_t) atol(temp);
700 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
703 /* recommend that 'gecos' and 'displayName' should refer to the same
704 * attribute OID. userFullName depreciated, only used by Samba
705 * primary rules of LDAP: don't make a new attribute when one is already defined
706 * that fits your needs; using cn then displayName rather than 'userFullName'
709 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
710 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME), fullname)) {
711 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
712 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_CN), fullname)) {
713 /* leave as default */
715 pdb_set_fullname(sampass, fullname, PDB_SET);
718 pdb_set_fullname(sampass, fullname, PDB_SET);
721 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
722 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE), dir_drive))
724 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
726 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
729 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
730 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH), homedir))
732 pdb_set_homedir( sampass,
733 talloc_sub_basic(sampass, username, lp_logon_home()),
736 pstrcpy( tmpstring, homedir );
737 if (expand_explicit) {
738 standard_sub_basic( username, tmpstring,
741 pdb_set_homedir(sampass, tmpstring, PDB_SET);
744 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
745 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT), logon_script))
747 pdb_set_logon_script( sampass,
748 talloc_sub_basic(sampass, username, lp_logon_script()),
751 pstrcpy( tmpstring, logon_script );
752 if (expand_explicit) {
753 standard_sub_basic( username, tmpstring,
756 pdb_set_logon_script(sampass, tmpstring, PDB_SET);
759 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
760 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH), profile_path))
762 pdb_set_profile_path( sampass,
763 talloc_sub_basic( sampass, username, lp_logon_path()),
766 pstrcpy( tmpstring, profile_path );
767 if (expand_explicit) {
768 standard_sub_basic( username, tmpstring,
771 pdb_set_profile_path(sampass, tmpstring, PDB_SET);
774 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
775 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC), acct_desc))
777 /* leave as default */
779 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
782 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
783 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS), workstations)) {
784 /* leave as default */;
786 pdb_set_workstations(sampass, workstations, PDB_SET);
789 if (!smbldap_get_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
790 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL), munged_dial, sizeof(munged_dial))) {
791 /* leave as default */;
793 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
796 /* FIXME: hours stuff should be cleaner */
800 memset(hours, 0xff, hours_len);
802 if (ldap_state->is_nds_ldap) {
805 char clear_text_pw[512];
807 /* Make call to Novell eDirectory ldap extension to get clear text password.
808 NOTE: This will only work if we have an SSL connection to eDirectory. */
809 user_dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
810 if (user_dn != NULL) {
811 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username, user_dn));
813 pwd_len = sizeof(clear_text_pw);
814 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
815 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
816 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET))
818 ZERO_STRUCT(smblmpwd);
819 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET))
821 ZERO_STRUCT(smbntpwd);
822 use_samba_attrs = False;
825 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
829 if (use_samba_attrs) {
830 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
831 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW), temp)) {
832 /* leave as default */
834 pdb_gethexpwd(temp, smblmpwd);
835 memset((char *)temp, '\0', strlen(temp)+1);
836 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET))
838 ZERO_STRUCT(smblmpwd);
841 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
842 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW), temp)) {
843 /* leave as default */
845 pdb_gethexpwd(temp, smbntpwd);
846 memset((char *)temp, '\0', strlen(temp)+1);
847 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET))
849 ZERO_STRUCT(smbntpwd);
855 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
857 uint8 *pwhist = NULL;
860 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
861 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
863 if ((pwhist = SMB_MALLOC(pwHistLen * PW_HISTORY_ENTRY_LEN)) == NULL){
864 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
867 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
869 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
870 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY), temp)) {
871 /* leave as default - zeros */
873 BOOL hex_failed = False;
874 for (i = 0; i < pwHistLen; i++){
875 /* Get the 16 byte salt. */
876 if (!pdb_gethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
880 /* Get the 16 byte MD5 hash of salt+passwd. */
881 if (!pdb_gethexpwd(&temp[(i*64)+32],
882 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN])) {
888 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
890 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
893 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
900 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
901 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO), temp)) {
902 acct_ctrl |= ACB_NORMAL;
904 acct_ctrl = pdb_decode_acct_ctrl(temp);
907 acct_ctrl |= ACB_NORMAL;
909 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
912 pdb_set_hours_len(sampass, hours_len, PDB_SET);
913 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
915 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
916 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_COUNT), temp)) {
917 /* leave as default */
919 bad_password_count = (uint32) atol(temp);
920 pdb_set_bad_password_count(sampass, bad_password_count, PDB_SET);
923 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
924 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_TIME), temp)) {
925 /* leave as default */
927 bad_password_time = (time_t) atol(temp);
928 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
932 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
933 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_COUNT), temp)) {
934 /* leave as default */
936 logon_count = (uint32) atol(temp);
937 pdb_set_logon_count(sampass, logon_count, PDB_SET);
940 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
942 if(!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
943 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_HOURS), temp)) {
944 /* leave as default */
946 pdb_gethexhours(temp, hours);
947 memset((char *)temp, '\0', strlen(temp) +1);
948 pdb_set_hours(sampass, hours, PDB_SET);
952 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
953 if (smbldap_get_single_pstring(priv2ld(ldap_state), entry,
954 "uidNumber", temp)) {
955 /* We've got a uid, feed the cache */
956 uid_t uid = strtoul(temp, NULL, 10);
957 store_uid_sid_cache(pdb_get_user_sid(sampass), uid);
961 /* check the timestamp of the cache vs ldap entry */
962 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
966 /* see if we have newer updates */
967 if (!(cache_entry = login_cache_read(sampass))) {
968 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
969 (unsigned int)pdb_get_bad_password_count(sampass),
970 (unsigned int)pdb_get_bad_password_time(sampass)));
974 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
975 (unsigned int)ldap_entry_time, (unsigned int)cache_entry->entry_timestamp,
976 (unsigned int)cache_entry->bad_password_time));
978 if (ldap_entry_time > cache_entry->entry_timestamp) {
979 /* cache is older than directory , so
980 we need to delete the entry but allow the
981 fields to be written out */
982 login_cache_delentry(sampass);
985 pdb_set_acct_ctrl(sampass,
986 pdb_get_acct_ctrl(sampass) |
987 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
989 pdb_set_bad_password_count(sampass,
990 cache_entry->bad_password_count,
992 pdb_set_bad_password_time(sampass,
993 cache_entry->bad_password_time,
997 SAFE_FREE(cache_entry);
1001 /**********************************************************************
1002 Initialize the ldap db from a struct samu. Called on update.
1003 (Based on init_buffer_from_sam in pdb_tdb.c)
1004 *********************************************************************/
1006 static BOOL init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1007 LDAPMessage *existing,
1008 LDAPMod *** mods, struct samu * sampass,
1009 BOOL (*need_update)(const struct samu *,
1015 if (mods == NULL || sampass == NULL) {
1016 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1023 * took out adding "objectclass: sambaAccount"
1024 * do this on a per-mod basis
1026 if (need_update(sampass, PDB_USERNAME))
1027 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1028 "uid", pdb_get_username(sampass));
1030 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1032 /* only update the RID if we actually need to */
1033 if (need_update(sampass, PDB_USERSID)) {
1035 fstring dom_sid_string;
1036 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
1038 switch ( ldap_state->schema_ver ) {
1039 case SCHEMAVER_SAMBAACCOUNT:
1040 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1041 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1042 sid_to_string(sid_string, user_sid),
1043 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
1046 slprintf(temp, sizeof(temp) - 1, "%i", rid);
1047 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1048 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1052 case SCHEMAVER_SAMBASAMACCOUNT:
1053 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1054 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1055 sid_to_string(sid_string, user_sid));
1059 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1064 /* we don't need to store the primary group RID - so leaving it
1065 'free' to hang off the unix primary group makes life easier */
1067 if (need_update(sampass, PDB_GROUPSID)) {
1069 fstring dom_sid_string;
1070 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1072 switch ( ldap_state->schema_ver ) {
1073 case SCHEMAVER_SAMBAACCOUNT:
1074 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1075 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1076 sid_to_string(sid_string, group_sid),
1077 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
1081 slprintf(temp, sizeof(temp) - 1, "%i", rid);
1082 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1083 get_userattr_key2string(ldap_state->schema_ver,
1084 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1087 case SCHEMAVER_SAMBASAMACCOUNT:
1088 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1089 get_userattr_key2string(ldap_state->schema_ver,
1090 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_string(sid_string, group_sid));
1094 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1100 /* displayName, cn, and gecos should all be the same
1101 * most easily accomplished by giving them the same OID
1102 * gecos isn't set here b/c it should be handled by the
1104 * We change displayName only and fall back to cn if
1105 * it does not exist.
1108 if (need_update(sampass, PDB_FULLNAME))
1109 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1110 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1111 pdb_get_fullname(sampass));
1113 if (need_update(sampass, PDB_ACCTDESC))
1114 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1115 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1116 pdb_get_acct_desc(sampass));
1118 if (need_update(sampass, PDB_WORKSTATIONS))
1119 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1120 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1121 pdb_get_workstations(sampass));
1123 if (need_update(sampass, PDB_MUNGEDDIAL))
1124 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1125 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1126 pdb_get_munged_dial(sampass));
1128 if (need_update(sampass, PDB_SMBHOME))
1129 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1130 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1131 pdb_get_homedir(sampass));
1133 if (need_update(sampass, PDB_DRIVE))
1134 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1135 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1136 pdb_get_dir_drive(sampass));
1138 if (need_update(sampass, PDB_LOGONSCRIPT))
1139 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1140 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1141 pdb_get_logon_script(sampass));
1143 if (need_update(sampass, PDB_PROFILE))
1144 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1145 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1146 pdb_get_profile_path(sampass));
1148 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
1149 if (need_update(sampass, PDB_LOGONTIME))
1150 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1151 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1153 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
1154 if (need_update(sampass, PDB_LOGOFFTIME))
1155 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1156 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1158 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
1159 if (need_update(sampass, PDB_KICKOFFTIME))
1160 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1161 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1163 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
1164 if (need_update(sampass, PDB_CANCHANGETIME))
1165 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1166 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1168 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
1169 if (need_update(sampass, PDB_MUSTCHANGETIME))
1170 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1171 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1174 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1175 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1177 if (need_update(sampass, PDB_LMPASSWD)) {
1178 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1180 pdb_sethexpwd(temp, lm_pw,
1181 pdb_get_acct_ctrl(sampass));
1182 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1183 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1186 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1187 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1191 if (need_update(sampass, PDB_NTPASSWD)) {
1192 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1194 pdb_sethexpwd(temp, nt_pw,
1195 pdb_get_acct_ctrl(sampass));
1196 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1197 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1200 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1201 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1206 if (need_update(sampass, PDB_PWHISTORY)) {
1207 uint32 pwHistLen = 0;
1208 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1209 if (pwHistLen == 0) {
1210 /* Remove any password history from the LDAP store. */
1211 memset(temp, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1215 uint32 currHistLen = 0;
1216 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1217 if (pwhist != NULL) {
1218 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1219 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
1220 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1221 /* Store the salt. */
1222 pdb_sethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1223 /* Followed by the md5 hash of salt + md4 hash */
1224 pdb_sethexpwd(&temp[(i*64)+32],
1225 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1226 DEBUG(100, ("temp=%s\n", temp));
1230 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1231 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1235 if (need_update(sampass, PDB_PASSLASTSET)) {
1236 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
1237 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1238 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1243 if (need_update(sampass, PDB_HOURS)) {
1244 const uint8 *hours = pdb_get_hours(sampass);
1246 pdb_sethexhours(temp, hours);
1247 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1250 get_userattr_key2string(ldap_state->schema_ver,
1251 LDAP_ATTR_LOGON_HOURS),
1256 if (need_update(sampass, PDB_ACCTCTRL))
1257 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1258 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1259 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1261 /* password lockout cache:
1262 - If we are now autolocking or clearing, we write to ldap
1263 - If we are clearing, we delete the cache entry
1264 - If the count is > 0, we update the cache
1266 This even means when autolocking, we cache, just in case the
1267 update doesn't work, and we have to cache the autolock flag */
1269 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1270 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1271 uint16 badcount = pdb_get_bad_password_count(sampass);
1272 time_t badtime = pdb_get_bad_password_time(sampass);
1274 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1276 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1277 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1279 if ((badcount >= pol) || (badcount == 0)) {
1280 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1281 (unsigned int)badcount, (unsigned int)badtime));
1282 slprintf (temp, sizeof (temp) - 1, "%li", (long)badcount);
1284 ldap_state->smbldap_state->ldap_struct,
1286 get_userattr_key2string(
1287 ldap_state->schema_ver,
1288 LDAP_ATTR_BAD_PASSWORD_COUNT),
1291 slprintf (temp, sizeof (temp) - 1, "%li", badtime);
1293 ldap_state->smbldap_state->ldap_struct,
1295 get_userattr_key2string(
1296 ldap_state->schema_ver,
1297 LDAP_ATTR_BAD_PASSWORD_TIME),
1300 if (badcount == 0) {
1301 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1302 login_cache_delentry(sampass);
1304 LOGIN_CACHE cache_entry;
1306 cache_entry.entry_timestamp = time(NULL);
1307 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1308 cache_entry.bad_password_count = badcount;
1309 cache_entry.bad_password_time = badtime;
1311 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1312 login_cache_write(sampass, cache_entry);
1319 /**********************************************************************
1320 Connect to LDAP server for password enumeration.
1321 *********************************************************************/
1323 static NTSTATUS ldapsam_setsampwent(struct pdb_methods *my_methods, BOOL update, uint16 acb_mask)
1325 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1327 pstring filter, suffix;
1328 const char **attr_list;
1329 BOOL machine_mask = False, user_mask = False;
1331 pstr_sprintf( filter, "(&%s%s)", "(uid=%u)",
1332 get_objclass_filter(ldap_state->schema_ver));
1333 all_string_sub(filter, "%u", "*", sizeof(pstring));
1335 machine_mask = ((acb_mask != 0) && (acb_mask & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)));
1336 user_mask = ((acb_mask != 0) && (acb_mask & ACB_NORMAL));
1339 pstrcpy(suffix, lp_ldap_machine_suffix());
1340 } else if (user_mask) {
1341 pstrcpy(suffix, lp_ldap_user_suffix());
1343 pstrcpy(suffix, lp_ldap_suffix());
1346 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1349 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1350 rc = smbldap_search(ldap_state->smbldap_state, suffix, LDAP_SCOPE_SUBTREE, filter,
1351 attr_list, 0, &ldap_state->result);
1352 TALLOC_FREE( attr_list );
1354 if (rc != LDAP_SUCCESS) {
1355 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc)));
1356 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix, filter));
1357 ldap_msgfree(ldap_state->result);
1358 ldap_state->result = NULL;
1359 return NT_STATUS_UNSUCCESSFUL;
1362 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1363 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
1364 ldap_state->result), suffix));
1366 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
1367 ldap_state->result);
1368 ldap_state->index = 0;
1370 return NT_STATUS_OK;
1373 /**********************************************************************
1374 End enumeration of the LDAP password list.
1375 *********************************************************************/
1377 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1379 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1380 if (ldap_state->result) {
1381 ldap_msgfree(ldap_state->result);
1382 ldap_state->result = NULL;
1386 /**********************************************************************
1387 Get the next entry in the LDAP password database.
1388 *********************************************************************/
1390 static NTSTATUS ldapsam_getsampwent(struct pdb_methods *my_methods,
1393 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1394 struct ldapsam_privates *ldap_state =
1395 (struct ldapsam_privates *)my_methods->private_data;
1399 if (!ldap_state->entry)
1402 ldap_state->index++;
1403 bret = init_sam_from_ldap(ldap_state, user, ldap_state->entry);
1405 ldap_state->entry = ldap_next_entry(priv2ld(ldap_state),
1409 return NT_STATUS_OK;
1412 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1413 const char *new_attr)
1417 if (new_attr == NULL) {
1421 for (i=0; (*attr_list)[i] != NULL; i++) {
1425 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1427 SMB_ASSERT((*attr_list) != NULL);
1428 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1429 (*attr_list)[i+1] = NULL;
1432 /**********************************************************************
1433 Get struct samu entry from LDAP by username.
1434 *********************************************************************/
1436 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1438 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1439 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1440 LDAPMessage *result = NULL;
1441 LDAPMessage *entry = NULL;
1443 const char ** attr_list;
1446 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1447 append_attr(user, &attr_list,
1448 get_userattr_key2string(ldap_state->schema_ver,
1449 LDAP_ATTR_MOD_TIMESTAMP));
1450 append_attr(user, &attr_list, "uidNumber");
1451 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1453 TALLOC_FREE( attr_list );
1455 if ( rc != LDAP_SUCCESS )
1456 return NT_STATUS_NO_SUCH_USER;
1458 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1461 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1462 ldap_msgfree(result);
1463 return NT_STATUS_NO_SUCH_USER;
1464 } else if (count > 1) {
1465 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1466 ldap_msgfree(result);
1467 return NT_STATUS_NO_SUCH_USER;
1470 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1472 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1473 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1474 ldap_msgfree(result);
1475 return NT_STATUS_NO_SUCH_USER;
1477 pdb_set_backend_private_data(user, result, NULL,
1478 my_methods, PDB_CHANGED);
1479 talloc_autofree_ldapmsg(user, result);
1482 ldap_msgfree(result);
1487 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1488 const DOM_SID *sid, LDAPMessage **result)
1491 const char ** attr_list;
1494 switch ( ldap_state->schema_ver ) {
1495 case SCHEMAVER_SAMBASAMACCOUNT: {
1496 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1497 if (tmp_ctx == NULL) {
1498 return LDAP_NO_MEMORY;
1501 attr_list = get_userattr_list(tmp_ctx,
1502 ldap_state->schema_ver);
1503 append_attr(tmp_ctx, &attr_list,
1504 get_userattr_key2string(
1505 ldap_state->schema_ver,
1506 LDAP_ATTR_MOD_TIMESTAMP));
1507 append_attr(tmp_ctx, &attr_list, "uidNumber");
1508 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1510 TALLOC_FREE(tmp_ctx);
1512 if ( rc != LDAP_SUCCESS )
1517 case SCHEMAVER_SAMBAACCOUNT:
1518 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1522 attr_list = get_userattr_list(NULL,
1523 ldap_state->schema_ver);
1524 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1525 TALLOC_FREE( attr_list );
1527 if ( rc != LDAP_SUCCESS )
1534 /**********************************************************************
1535 Get struct samu entry from LDAP by SID.
1536 *********************************************************************/
1538 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1540 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1541 LDAPMessage *result = NULL;
1542 LDAPMessage *entry = NULL;
1547 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1549 if (rc != LDAP_SUCCESS)
1550 return NT_STATUS_NO_SUCH_USER;
1552 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1555 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string, sid),
1557 ldap_msgfree(result);
1558 return NT_STATUS_NO_SUCH_USER;
1559 } else if (count > 1) {
1560 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string, sid),
1562 ldap_msgfree(result);
1563 return NT_STATUS_NO_SUCH_USER;
1566 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1568 ldap_msgfree(result);
1569 return NT_STATUS_NO_SUCH_USER;
1572 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1573 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1574 ldap_msgfree(result);
1575 return NT_STATUS_NO_SUCH_USER;
1578 pdb_set_backend_private_data(user, result, NULL,
1579 my_methods, PDB_CHANGED);
1580 talloc_autofree_ldapmsg(user, result);
1581 return NT_STATUS_OK;
1584 static BOOL ldapsam_can_pwchange_exop(struct smbldap_state *ldap_state)
1586 return smbldap_has_extension(ldap_state, LDAP_EXOP_MODIFY_PASSWD);
1589 /********************************************************************
1590 Do the actual modification - also change a plaintext passord if
1592 **********************************************************************/
1594 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1595 struct samu *newpwd, char *dn,
1596 LDAPMod **mods, int ldap_op,
1597 BOOL (*need_update)(const struct samu *, enum pdb_elements))
1599 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1602 if (!my_methods || !newpwd || !dn) {
1603 return NT_STATUS_INVALID_PARAMETER;
1607 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1608 /* may be password change below however */
1612 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1615 rc = smbldap_add(ldap_state->smbldap_state,
1618 case LDAP_MOD_REPLACE:
1619 rc = smbldap_modify(ldap_state->smbldap_state,
1623 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1625 return NT_STATUS_INVALID_PARAMETER;
1628 if (rc!=LDAP_SUCCESS) {
1629 return NT_STATUS_UNSUCCESSFUL;
1633 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1634 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1635 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1636 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1639 char *retoid = NULL;
1640 struct berval *retdata = NULL;
1641 char *utf8_password;
1644 if (!ldap_state->is_nds_ldap) {
1645 if (!ldapsam_can_pwchange_exop(ldap_state->smbldap_state)) {
1646 DEBUG(2, ("ldap password change requested, but LDAP "
1647 "server does not support it -- ignoring\n"));
1648 return NT_STATUS_OK;
1652 if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
1653 return NT_STATUS_NO_MEMORY;
1656 if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
1657 return NT_STATUS_NO_MEMORY;
1660 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1661 DEBUG(0,("ber_alloc_t returns NULL\n"));
1662 SAFE_FREE(utf8_password);
1663 return NT_STATUS_UNSUCCESSFUL;
1666 ber_printf (ber, "{");
1667 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
1668 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
1669 ber_printf (ber, "N}");
1671 if ((rc = ber_flatten (ber, &bv))<0) {
1672 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1675 SAFE_FREE(utf8_password);
1676 return NT_STATUS_UNSUCCESSFUL;
1680 SAFE_FREE(utf8_password);
1683 if (!ldap_state->is_nds_ldap) {
1684 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1685 LDAP_EXOP_MODIFY_PASSWD,
1686 bv, NULL, NULL, &retoid,
1689 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1690 pdb_get_plaintext_passwd(newpwd));
1692 if (rc != LDAP_SUCCESS) {
1693 char *ld_error = NULL;
1695 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1696 DEBUG(3, ("Could not set userPassword "
1697 "attribute due to an objectClass "
1698 "violation -- ignoring\n"));
1700 return NT_STATUS_OK;
1703 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1705 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1706 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1707 SAFE_FREE(ld_error);
1709 return NT_STATUS_UNSUCCESSFUL;
1711 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1712 #ifdef DEBUG_PASSWORD
1713 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1716 ber_bvfree(retdata);
1718 ldap_memfree(retoid);
1722 return NT_STATUS_OK;
1725 /**********************************************************************
1726 Delete entry from LDAP for username.
1727 *********************************************************************/
1729 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1730 struct samu * sam_acct)
1732 struct ldapsam_privates *priv =
1733 (struct ldapsam_privates *)my_methods->private_data;
1736 LDAPMessage *msg, *entry;
1737 NTSTATUS result = NT_STATUS_NO_MEMORY;
1738 const char **attr_list;
1739 TALLOC_CTX *mem_ctx;
1742 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1743 return NT_STATUS_INVALID_PARAMETER;
1746 sname = pdb_get_username(sam_acct);
1748 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1751 mem_ctx = talloc_new(NULL);
1752 if (mem_ctx == NULL) {
1753 DEBUG(0, ("talloc_new failed\n"));
1757 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1758 if (attr_list == NULL) {
1762 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1764 if ((rc != LDAP_SUCCESS) ||
1765 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1766 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1767 DEBUG(5, ("Could not find user %s\n", sname));
1768 result = NT_STATUS_NO_SUCH_USER;
1772 rc = ldapsam_delete_entry(
1773 priv, mem_ctx, entry,
1774 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1775 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1778 result = (rc == LDAP_SUCCESS) ?
1779 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1782 TALLOC_FREE(mem_ctx);
1786 /**********************************************************************
1787 Helper function to determine for update_sam_account whether
1788 we need LDAP modification.
1789 *********************************************************************/
1791 static BOOL element_is_changed(const struct samu *sampass,
1792 enum pdb_elements element)
1794 return IS_SAM_CHANGED(sampass, element);
1797 /**********************************************************************
1799 *********************************************************************/
1801 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1803 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1804 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1807 LDAPMessage *result = NULL;
1808 LDAPMessage *entry = NULL;
1809 LDAPMod **mods = NULL;
1810 const char **attr_list;
1812 result = pdb_get_backend_private_data(newpwd, my_methods);
1814 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1815 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1816 TALLOC_FREE( attr_list );
1817 if (rc != LDAP_SUCCESS) {
1818 return NT_STATUS_UNSUCCESSFUL;
1820 pdb_set_backend_private_data(newpwd, result, NULL,
1821 my_methods, PDB_CHANGED);
1822 talloc_autofree_ldapmsg(newpwd, result);
1825 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1826 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1827 return NT_STATUS_UNSUCCESSFUL;
1830 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1831 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
1833 return NT_STATUS_UNSUCCESSFUL;
1836 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1838 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1839 element_is_changed)) {
1840 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1843 ldap_mods_free(mods,True);
1844 return NT_STATUS_UNSUCCESSFUL;
1848 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1849 pdb_get_username(newpwd)));
1851 return NT_STATUS_OK;
1854 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1855 ldap_mods_free(mods,True);
1858 if (!NT_STATUS_IS_OK(ret)) {
1862 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1863 pdb_get_username(newpwd)));
1864 return NT_STATUS_OK;
1867 /***************************************************************************
1868 Renames a struct samu
1869 - The "rename user script" has full responsibility for changing everything
1870 ***************************************************************************/
1872 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
1873 struct samu *old_acct,
1874 const char *newname)
1876 const char *oldname;
1878 pstring rename_script;
1881 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1882 return NT_STATUS_INVALID_PARAMETER;
1885 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1886 return NT_STATUS_INVALID_PARAMETER;
1889 oldname = pdb_get_username(old_acct);
1891 /* rename the posix user */
1892 pstrcpy(rename_script, lp_renameuser_script());
1894 if (!(*rename_script))
1895 return NT_STATUS_ACCESS_DENIED;
1897 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1900 pstring_sub(rename_script, "%unew", newname);
1901 pstring_sub(rename_script, "%uold", oldname);
1902 rc = smbrun(rename_script, NULL);
1904 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
1905 rename_script, rc));
1908 return NT_STATUS_UNSUCCESSFUL;
1910 return NT_STATUS_OK;
1913 /**********************************************************************
1914 Helper function to determine for update_sam_account whether
1915 we need LDAP modification.
1916 *********************************************************************/
1918 static BOOL element_is_set_or_changed(const struct samu *sampass,
1919 enum pdb_elements element)
1921 return (IS_SAM_SET(sampass, element) ||
1922 IS_SAM_CHANGED(sampass, element));
1925 /**********************************************************************
1926 Add struct samu to LDAP.
1927 *********************************************************************/
1929 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1931 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1932 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1934 LDAPMessage *result = NULL;
1935 LDAPMessage *entry = NULL;
1937 LDAPMod **mods = NULL;
1938 int ldap_op = LDAP_MOD_REPLACE;
1940 const char **attr_list;
1942 const char *username = pdb_get_username(newpwd);
1943 const DOM_SID *sid = pdb_get_user_sid(newpwd);
1947 if (!username || !*username) {
1948 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1949 return NT_STATUS_INVALID_PARAMETER;
1952 /* free this list after the second search or in case we exit on failure */
1953 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1955 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
1957 if (rc != LDAP_SUCCESS) {
1958 TALLOC_FREE( attr_list );
1959 return NT_STATUS_UNSUCCESSFUL;
1962 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1963 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1965 ldap_msgfree(result);
1966 TALLOC_FREE( attr_list );
1967 return NT_STATUS_UNSUCCESSFUL;
1969 ldap_msgfree(result);
1972 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
1973 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1975 if (rc == LDAP_SUCCESS) {
1976 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1977 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1978 sid_to_string(sid_string, sid)));
1979 TALLOC_FREE( attr_list );
1980 ldap_msgfree(result);
1981 return NT_STATUS_UNSUCCESSFUL;
1983 ldap_msgfree(result);
1987 /* does the entry already exist but without a samba attributes?
1988 we need to return the samba attributes here */
1990 escape_user = escape_ldap_string_alloc( username );
1991 pstrcpy( filter, "(uid=%u)" );
1992 all_string_sub( filter, "%u", escape_user, sizeof(filter) );
1993 SAFE_FREE( escape_user );
1995 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1996 filter, attr_list, &result);
1997 if ( rc != LDAP_SUCCESS ) {
1998 TALLOC_FREE( attr_list );
1999 return NT_STATUS_UNSUCCESSFUL;
2002 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2004 if (num_result > 1) {
2005 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2006 TALLOC_FREE( attr_list );
2007 ldap_msgfree(result);
2008 return NT_STATUS_UNSUCCESSFUL;
2011 /* Check if we need to update an existing entry */
2012 if (num_result == 1) {
2015 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2016 ldap_op = LDAP_MOD_REPLACE;
2017 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2018 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
2020 TALLOC_FREE( attr_list );
2021 ldap_msgfree(result);
2022 return NT_STATUS_UNSUCCESSFUL;
2024 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
2027 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2029 /* There might be a SID for this account already - say an idmap entry */
2031 pstr_sprintf(filter, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2032 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
2033 sid_to_string(sid_string, sid),
2034 LDAP_OBJ_IDMAP_ENTRY,
2035 LDAP_OBJ_SID_ENTRY);
2037 /* free old result before doing a new search */
2038 if (result != NULL) {
2039 ldap_msgfree(result);
2042 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2043 filter, attr_list, &result);
2045 if ( rc != LDAP_SUCCESS ) {
2046 TALLOC_FREE( attr_list );
2047 return NT_STATUS_UNSUCCESSFUL;
2050 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2052 if (num_result > 1) {
2053 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2054 TALLOC_FREE( attr_list );
2055 ldap_msgfree(result);
2056 return NT_STATUS_UNSUCCESSFUL;
2059 /* Check if we need to update an existing entry */
2060 if (num_result == 1) {
2063 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2064 ldap_op = LDAP_MOD_REPLACE;
2065 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2066 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
2068 TALLOC_FREE( attr_list );
2069 ldap_msgfree(result);
2070 return NT_STATUS_UNSUCCESSFUL;
2072 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
2077 TALLOC_FREE( attr_list );
2079 if (num_result == 0) {
2080 /* Check if we need to add an entry */
2081 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2082 ldap_op = LDAP_MOD_ADD;
2083 if (username[strlen(username)-1] == '$') {
2084 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_machine_suffix ());
2086 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_user_suffix ());
2090 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2091 element_is_set_or_changed)) {
2092 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2093 ldap_msgfree(result);
2095 ldap_mods_free(mods,True);
2096 return NT_STATUS_UNSUCCESSFUL;
2099 ldap_msgfree(result);
2102 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2103 return NT_STATUS_UNSUCCESSFUL;
2105 switch ( ldap_state->schema_ver ) {
2106 case SCHEMAVER_SAMBAACCOUNT:
2107 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2109 case SCHEMAVER_SAMBASAMACCOUNT:
2110 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2113 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2117 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2118 if (!NT_STATUS_IS_OK(ret)) {
2119 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2120 pdb_get_username(newpwd),dn));
2121 ldap_mods_free(mods, True);
2125 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2126 ldap_mods_free(mods, True);
2128 return NT_STATUS_OK;
2131 /**********************************************************************
2132 *********************************************************************/
2134 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2136 LDAPMessage ** result)
2138 int scope = LDAP_SCOPE_SUBTREE;
2140 const char **attr_list;
2142 attr_list = get_attr_list(NULL, groupmap_attr_list);
2143 rc = smbldap_search(ldap_state->smbldap_state,
2144 lp_ldap_group_suffix (), scope,
2145 filter, attr_list, 0, result);
2146 TALLOC_FREE(attr_list);
2151 /**********************************************************************
2152 *********************************************************************/
2154 static BOOL init_group_from_ldap(struct ldapsam_privates *ldap_state,
2155 GROUP_MAP *map, LDAPMessage *entry)
2159 if (ldap_state == NULL || map == NULL || entry == NULL ||
2160 ldap_state->smbldap_state->ldap_struct == NULL) {
2161 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2165 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2166 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER), temp)) {
2167 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2168 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2171 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2173 map->gid = (gid_t)atol(temp);
2175 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2176 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID), temp)) {
2177 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2178 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2182 if (!string_to_sid(&map->sid, temp)) {
2183 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2187 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2188 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), temp)) {
2189 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2190 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2193 map->sid_name_use = (enum SID_NAME_USE)atol(temp);
2195 if ((map->sid_name_use < SID_NAME_USER) ||
2196 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2197 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2201 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2202 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), temp)) {
2204 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2205 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_CN), temp))
2207 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2208 for gidNumber(%lu)\n",(unsigned long)map->gid));
2212 fstrcpy(map->nt_name, temp);
2214 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2215 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), temp)) {
2218 fstrcpy(map->comment, temp);
2220 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
2221 store_gid_sid_cache(&map->sid, map->gid);
2227 /**********************************************************************
2228 *********************************************************************/
2230 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2234 struct ldapsam_privates *ldap_state =
2235 (struct ldapsam_privates *)methods->private_data;
2236 LDAPMessage *result = NULL;
2237 LDAPMessage *entry = NULL;
2240 if (ldapsam_search_one_group(ldap_state, filter, &result)
2242 return NT_STATUS_NO_SUCH_GROUP;
2245 count = ldap_count_entries(priv2ld(ldap_state), result);
2248 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2249 ldap_msgfree(result);
2250 return NT_STATUS_NO_SUCH_GROUP;
2254 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2255 "count=%d\n", filter, count));
2256 ldap_msgfree(result);
2257 return NT_STATUS_NO_SUCH_GROUP;
2260 entry = ldap_first_entry(priv2ld(ldap_state), result);
2263 ldap_msgfree(result);
2264 return NT_STATUS_UNSUCCESSFUL;
2267 if (!init_group_from_ldap(ldap_state, map, entry)) {
2268 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2269 "group filter %s\n", filter));
2270 ldap_msgfree(result);
2271 return NT_STATUS_NO_SUCH_GROUP;
2274 ldap_msgfree(result);
2275 return NT_STATUS_OK;
2278 /**********************************************************************
2279 *********************************************************************/
2281 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2286 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2288 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2289 sid_string_static(&sid));
2291 return ldapsam_getgroup(methods, filter, map);
2294 /**********************************************************************
2295 *********************************************************************/
2297 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2302 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%lu))",
2304 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2305 (unsigned long)gid);
2307 return ldapsam_getgroup(methods, filter, map);
2310 /**********************************************************************
2311 *********************************************************************/
2313 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2317 char *escape_name = escape_ldap_string_alloc(name);
2320 return NT_STATUS_NO_MEMORY;
2323 pstr_sprintf(filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2325 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2326 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN), escape_name);
2328 SAFE_FREE(escape_name);
2330 return ldapsam_getgroup(methods, filter, map);
2333 static void add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
2334 uint32 rid, uint32 **pp_rids, size_t *p_num)
2338 for (i=0; i<*p_num; i++) {
2339 if ((*pp_rids)[i] == rid)
2343 *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
2345 if (*pp_rids == NULL)
2348 (*pp_rids)[*p_num] = rid;
2352 static BOOL ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2354 const DOM_SID *domain_sid,
2360 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2361 str, sizeof(str)-1)) {
2362 DEBUG(10, ("Could not find sambaSID attribute\n"));
2366 if (!string_to_sid(&sid, str)) {
2367 DEBUG(10, ("Could not convert string %s to sid\n", str));
2371 if (sid_compare_domain(&sid, domain_sid) != 0) {
2372 DEBUG(10, ("SID %s is not in expected domain %s\n",
2373 str, sid_string_static(domain_sid)));
2377 if (!sid_peek_rid(&sid, rid)) {
2378 DEBUG(10, ("Could not peek into RID\n"));
2385 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2386 TALLOC_CTX *mem_ctx,
2387 const DOM_SID *group,
2388 uint32 **pp_member_rids,
2389 size_t *p_num_members)
2391 struct ldapsam_privates *ldap_state =
2392 (struct ldapsam_privates *)methods->private_data;
2393 struct smbldap_state *conn = ldap_state->smbldap_state;
2396 LDAPMessage *msg = NULL;
2398 char **values = NULL;
2400 char *sid_filter = NULL;
2402 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2404 *pp_member_rids = NULL;
2407 pstr_sprintf(filter,
2408 "(&(objectClass=sambaSamAccount)"
2409 "(sambaPrimaryGroupSid=%s))",
2410 sid_string_static(group));
2413 const char *attrs[] = { "sambaSID", NULL };
2414 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2415 LDAP_SCOPE_SUBTREE, filter, attrs, 0,
2419 if (rc != LDAP_SUCCESS)
2422 for (entry = ldap_first_entry(conn->ldap_struct, msg);
2424 entry = ldap_next_entry(conn->ldap_struct, entry))
2428 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2430 get_global_sam_sid(),
2432 DEBUG(2, ("Could not find sid from ldap entry\n"));
2436 add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2443 pstr_sprintf(filter,
2444 "(&(objectClass=sambaGroupMapping)"
2445 "(objectClass=posixGroup)"
2447 sid_string_static(group));
2450 const char *attrs[] = { "memberUid", NULL };
2451 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2452 LDAP_SCOPE_SUBTREE, filter, attrs, 0,
2456 if (rc != LDAP_SUCCESS)
2459 count = ldap_count_entries(conn->ldap_struct, msg);
2462 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2463 sid_string_static(group)));
2468 result = NT_STATUS_OK;
2472 entry = ldap_first_entry(conn->ldap_struct, msg);
2476 values = ldap_get_values(conn->ldap_struct, msg, "memberUid");
2477 if (values == NULL) {
2478 result = NT_STATUS_OK;
2482 sid_filter = SMB_STRDUP("(&(objectClass=sambaSamAccount)(|");
2483 if (sid_filter == NULL) {
2484 result = NT_STATUS_NO_MEMORY;
2488 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2490 asprintf(&sid_filter, "%s(uid=%s)", tmp, *memberuid);
2492 if (sid_filter == NULL) {
2493 result = NT_STATUS_NO_MEMORY;
2499 asprintf(&sid_filter, "%s))", sid_filter);
2501 if (sid_filter == NULL) {
2502 result = NT_STATUS_NO_MEMORY;
2507 const char *attrs[] = { "sambaSID", NULL };
2508 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2509 LDAP_SCOPE_SUBTREE, sid_filter, attrs, 0,
2513 if (rc != LDAP_SUCCESS)
2516 for (entry = ldap_first_entry(conn->ldap_struct, msg);
2518 entry = ldap_next_entry(conn->ldap_struct, entry))
2524 if (!smbldap_get_single_attribute(conn->ldap_struct,
2526 str, sizeof(str)-1))
2529 if (!string_to_sid(&sid, str))
2532 if (!sid_check_is_in_our_domain(&sid)) {
2533 DEBUG(1, ("Inconsistent SAM -- group member uid not "
2534 "in our domain\n"));
2538 sid_peek_rid(&sid, &rid);
2540 add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2544 result = NT_STATUS_OK;
2547 SAFE_FREE(sid_filter);
2550 ldap_value_free(values);
2558 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2559 TALLOC_CTX *mem_ctx,
2563 size_t *p_num_groups)
2565 struct ldapsam_privates *ldap_state =
2566 (struct ldapsam_privates *)methods->private_data;
2567 struct smbldap_state *conn = ldap_state->smbldap_state;
2569 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2572 LDAPMessage *msg = NULL;
2574 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2575 size_t num_sids, num_gids;
2581 if (!sid_to_gid(pdb_get_group_sid(user), &primary_gid)) {
2582 DEBUG(1, ("sid_to_gid failed for user's primary group\n"));
2583 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2586 escape_name = escape_ldap_string_alloc(pdb_get_username(user));
2588 if (escape_name == NULL)
2589 return NT_STATUS_NO_MEMORY;
2591 pstr_sprintf(filter, "(&(objectClass=posixGroup)"
2592 "(|(memberUid=%s)(gidNumber=%d)))",
2593 escape_name, primary_gid);
2595 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2596 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &msg);
2598 if (rc != LDAP_SUCCESS)
2607 /* We need to add the primary group as the first gid/sid */
2609 add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids);
2611 /* This sid will be replaced later */
2613 add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids, &num_sids);
2615 for (entry = ldap_first_entry(conn->ldap_struct, msg);
2617 entry = ldap_next_entry(conn->ldap_struct, entry))
2624 if (!smbldap_get_single_attribute(conn->ldap_struct,
2626 str, sizeof(str)-1))
2629 if (!string_to_sid(&sid, str))
2632 if (!smbldap_get_single_attribute(conn->ldap_struct,
2634 str, sizeof(str)-1))
2637 gid = strtoul(str, &end, 10);
2639 if (PTR_DIFF(end, str) != strlen(str))
2642 if (gid == primary_gid) {
2643 sid_copy(&(*pp_sids)[0], &sid);
2645 add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2647 add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2652 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
2653 DEBUG(3, ("primary group of [%s] not found\n",
2654 pdb_get_username(user)));
2658 *p_num_groups = num_sids;
2660 result = NT_STATUS_OK;
2664 SAFE_FREE(escape_name);
2671 /**********************************************************************
2672 * Augment a posixGroup object with a sambaGroupMapping domgroup
2673 *********************************************************************/
2675 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
2676 struct ldapsam_privates *ldap_state,
2679 const char *filter, *dn;
2680 LDAPMessage *msg, *entry;
2684 filter = talloc_asprintf(mem_ctx,
2685 "(&(objectClass=posixGroup)(gidNumber=%u))",
2687 if (filter == NULL) {
2688 return NT_STATUS_NO_MEMORY;
2691 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2692 get_attr_list(mem_ctx, groupmap_attr_list),
2694 talloc_autofree_ldapmsg(mem_ctx, msg);
2696 if ((rc != LDAP_SUCCESS) ||
2697 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2698 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2699 return NT_STATUS_NO_SUCH_GROUP;
2702 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2704 return NT_STATUS_NO_MEMORY;
2708 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
2709 "sambaGroupMapping");
2710 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
2711 sid_string_static(&map->sid));
2712 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
2713 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
2714 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
2716 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
2718 talloc_autofree_ldapmod(mem_ctx, mods);
2720 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2721 if (rc != LDAP_SUCCESS) {
2722 return NT_STATUS_ACCESS_DENIED;
2725 return NT_STATUS_OK;
2728 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
2731 struct ldapsam_privates *ldap_state =
2732 (struct ldapsam_privates *)methods->private_data;
2733 LDAPMessage *msg = NULL;
2734 LDAPMod **mods = NULL;
2735 const char *attrs[] = { NULL };
2739 TALLOC_CTX *mem_ctx;
2746 mem_ctx = talloc_new(NULL);
2747 if (mem_ctx == NULL) {
2748 DEBUG(0, ("talloc_new failed\n"));
2749 return NT_STATUS_NO_MEMORY;
2752 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
2753 sid_string_static(&map->sid));
2754 if (filter == NULL) {
2755 result = NT_STATUS_NO_MEMORY;
2759 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
2760 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
2761 talloc_autofree_ldapmsg(mem_ctx, msg);
2763 if ((rc == LDAP_SUCCESS) &&
2764 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
2766 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
2767 "group mapping entry\n",
2768 sid_string_static(&map->sid)));
2769 result = NT_STATUS_GROUP_EXISTS;
2773 switch (map->sid_name_use) {
2775 case SID_NAME_DOM_GRP:
2776 /* To map a domain group we need to have a posix group
2778 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
2782 case SID_NAME_ALIAS:
2783 if (!sid_check_is_in_our_domain(&map->sid)) {
2784 DEBUG(3, ("Refusing to map sid %s as an alias, not "
2786 sid_string_static(&map->sid)));
2787 result = NT_STATUS_INVALID_PARAMETER;
2792 case SID_NAME_WKN_GRP:
2793 if (!sid_check_is_in_builtin(&map->sid)) {
2794 DEBUG(3, ("Refusing to map sid %s as an alias, not "
2795 "in builtin domain\n",
2796 sid_string_static(&map->sid)));
2797 result = NT_STATUS_INVALID_PARAMETER;
2803 DEBUG(3, ("Got invalid use '%s' for mapping\n",
2804 sid_type_lookup(map->sid_name_use)));
2805 result = NT_STATUS_INVALID_PARAMETER;
2809 /* Domain groups have been mapped in a separate routine, we have to
2810 * create an alias now */
2812 if (map->gid == -1) {
2813 DEBUG(10, ("Refusing to map gid==-1\n"));
2814 result = NT_STATUS_INVALID_PARAMETER;
2818 if (pdb_gid_to_sid(map->gid, &sid)) {
2819 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
2820 "add\n", map->gid, sid_string_static(&sid)));
2821 result = NT_STATUS_GROUP_EXISTS;
2825 /* Ok, enough checks done. It's still racy to go ahead now, but that's
2826 * the best we can get out of LDAP. */
2828 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
2829 sid_string_static(&map->sid),
2830 lp_ldap_group_suffix());
2832 result = NT_STATUS_NO_MEMORY;
2838 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
2840 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
2841 "sambaGroupMapping");
2843 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
2844 sid_string_static(&map->sid));
2845 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
2846 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
2847 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
2849 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
2851 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
2852 talloc_asprintf(mem_ctx, "%u", map->gid));
2853 talloc_autofree_ldapmod(mem_ctx, mods);
2855 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
2857 result = (rc == LDAP_SUCCESS) ?
2858 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
2861 TALLOC_FREE(mem_ctx);
2865 /**********************************************************************
2866 * Update a group mapping entry. We're quite strict about what can be changed:
2867 * Only the description and displayname may be changed. It simply does not
2868 * make any sense to change the SID, gid or the type in a mapping.
2869 *********************************************************************/
2871 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
2874 struct ldapsam_privates *ldap_state =
2875 (struct ldapsam_privates *)methods->private_data;
2877 const char *filter, *dn;
2878 LDAPMessage *msg = NULL;
2879 LDAPMessage *entry = NULL;
2880 LDAPMod **mods = NULL;
2881 TALLOC_CTX *mem_ctx;
2884 mem_ctx = talloc_new(NULL);
2885 if (mem_ctx == NULL) {
2886 DEBUG(0, ("talloc_new failed\n"));
2887 return NT_STATUS_NO_MEMORY;
2890 /* Make 100% sure that sid, gid and type are not changed by looking up
2891 * exactly the values we're given in LDAP. */
2893 filter = talloc_asprintf(mem_ctx, "(&(objectClass=sambaGroupMapping)"
2894 "(sambaSid=%s)(gidNumber=%u)"
2895 "(sambaGroupType=%d))",
2896 sid_string_static(&map->sid), map->gid,
2898 if (filter == NULL) {
2899 result = NT_STATUS_NO_MEMORY;
2903 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2904 get_attr_list(mem_ctx, groupmap_attr_list),
2906 talloc_autofree_ldapmsg(mem_ctx, msg);
2908 if ((rc != LDAP_SUCCESS) ||
2909 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2910 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2911 result = NT_STATUS_NO_SUCH_GROUP;
2915 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2918 result = NT_STATUS_NO_MEMORY;
2923 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
2925 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
2927 talloc_autofree_ldapmod(mem_ctx, mods);
2930 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
2931 "nothing to do\n"));
2932 result = NT_STATUS_OK;
2936 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2938 if (rc != LDAP_SUCCESS) {
2939 result = NT_STATUS_ACCESS_DENIED;
2943 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
2944 "group %lu in LDAP\n", (unsigned long)map->gid));
2946 result = NT_STATUS_OK;
2949 TALLOC_FREE(mem_ctx);
2953 /**********************************************************************
2954 *********************************************************************/
2956 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
2959 struct ldapsam_privates *priv =
2960 (struct ldapsam_privates *)methods->private_data;
2961 LDAPMessage *msg, *entry;
2964 TALLOC_CTX *mem_ctx;
2967 mem_ctx = talloc_new(NULL);
2968 if (mem_ctx == NULL) {
2969 DEBUG(0, ("talloc_new failed\n"));
2970 return NT_STATUS_NO_MEMORY;
2973 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
2974 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
2975 sid_string_static(&sid));
2976 if (filter == NULL) {
2977 result = NT_STATUS_NO_MEMORY;
2980 rc = smbldap_search_suffix(priv->smbldap_state, filter,
2981 get_attr_list(mem_ctx, groupmap_attr_list),
2983 talloc_autofree_ldapmsg(mem_ctx, msg);
2985 if ((rc != LDAP_SUCCESS) ||
2986 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
2987 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
2988 result = NT_STATUS_NO_SUCH_GROUP;
2992 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
2993 get_attr_list(mem_ctx,
2994 groupmap_attr_list_to_delete));
2996 if ((rc == LDAP_NAMING_VIOLATION) ||
2997 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
2998 const char *attrs[] = { "sambaGroupType", "description",
2999 "displayName", "sambaSIDList",
3002 /* Second try. Don't delete the sambaSID attribute, this is
3003 for "old" entries that are tacked on a winbind
3006 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3007 LDAP_OBJ_GROUPMAP, attrs);
3010 if ((rc == LDAP_NAMING_VIOLATION) ||
3011 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3012 const char *attrs[] = { "sambaGroupType", "description",
3013 "displayName", "sambaSIDList",
3014 "gidNumber", NULL };
3016 /* Third try. This is a post-3.0.21 alias (containing only
3017 * sambaSidEntry and sambaGroupMapping classes), we also have
3018 * to delete the gidNumber attribute, only the sambaSidEntry
3021 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3022 LDAP_OBJ_GROUPMAP, attrs);
3025 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3028 TALLOC_FREE(mem_ctx);
3032 /**********************************************************************
3033 *********************************************************************/
3035 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3038 struct ldapsam_privates *ldap_state =
3039 (struct ldapsam_privates *)my_methods->private_data;
3042 const char **attr_list;
3044 pstr_sprintf( filter, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3045 attr_list = get_attr_list( NULL, groupmap_attr_list );
3046 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3047 LDAP_SCOPE_SUBTREE, filter,
3048 attr_list, 0, &ldap_state->result);
3049 TALLOC_FREE(attr_list);
3051 if (rc != LDAP_SUCCESS) {
3052 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3053 ldap_err2string(rc)));
3054 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3055 lp_ldap_group_suffix(), filter));
3056 ldap_msgfree(ldap_state->result);
3057 ldap_state->result = NULL;
3058 return NT_STATUS_UNSUCCESSFUL;
3061 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3062 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3063 ldap_state->result)));
3066 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3067 ldap_state->result);
3068 ldap_state->index = 0;
3070 return NT_STATUS_OK;
3073 /**********************************************************************
3074 *********************************************************************/
3076 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3078 ldapsam_endsampwent(my_methods);
3081 /**********************************************************************
3082 *********************************************************************/
3084 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3087 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3088 struct ldapsam_privates *ldap_state =
3089 (struct ldapsam_privates *)my_methods->private_data;
3093 if (!ldap_state->entry)
3096 ldap_state->index++;
3097 bret = init_group_from_ldap(ldap_state, map,
3101 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3105 return NT_STATUS_OK;
3108 /**********************************************************************
3109 *********************************************************************/
3111 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3112 enum SID_NAME_USE sid_name_use,
3113 GROUP_MAP **pp_rmap,
3114 size_t *p_num_entries,
3124 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3125 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3127 return NT_STATUS_ACCESS_DENIED;
3130 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3131 if (sid_name_use != SID_NAME_UNKNOWN &&
3132 sid_name_use != map.sid_name_use) {
3133 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3134 "not of the requested type\n", map.nt_name));
3137 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3138 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3139 "non mapped\n", map.nt_name));
3143 mapt=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3145 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3146 "enlarge group map!\n"));
3147 SAFE_FREE(*pp_rmap);
3148 return NT_STATUS_UNSUCCESSFUL;
3153 mapt[entries] = map;
3158 ldapsam_endsamgrent(methods);
3160 *p_num_entries = entries;
3162 return NT_STATUS_OK;
3165 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3166 const DOM_SID *alias,
3167 const DOM_SID *member,
3170 struct ldapsam_privates *ldap_state =
3171 (struct ldapsam_privates *)methods->private_data;
3173 LDAPMessage *result = NULL;
3174 LDAPMessage *entry = NULL;
3176 LDAPMod **mods = NULL;
3178 enum SID_NAME_USE type = SID_NAME_USE_NONE;
3182 if (sid_check_is_in_builtin(alias)) {
3183 type = SID_NAME_WKN_GRP;
3186 if (sid_check_is_in_our_domain(alias)) {
3187 type = SID_NAME_ALIAS;
3190 if (type == SID_NAME_USE_NONE) {
3191 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3192 sid_string_static(alias)));
3193 return NT_STATUS_NO_SUCH_ALIAS;
3196 pstr_sprintf(filter,
3197 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3198 LDAP_OBJ_GROUPMAP, sid_string_static(alias),
3201 if (ldapsam_search_one_group(ldap_state, filter,
3202 &result) != LDAP_SUCCESS)
3203 return NT_STATUS_NO_SUCH_ALIAS;
3205 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3209 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3210 ldap_msgfree(result);
3211 return NT_STATUS_NO_SUCH_ALIAS;
3215 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3216 "filter %s: count=%d\n", filter, count));
3217 ldap_msgfree(result);
3218 return NT_STATUS_NO_SUCH_ALIAS;
3221 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3225 ldap_msgfree(result);
3226 return NT_STATUS_UNSUCCESSFUL;
3229 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
3231 ldap_msgfree(result);
3232 return NT_STATUS_UNSUCCESSFUL;
3235 smbldap_set_mod(&mods, modop,
3236 get_attr_key2string(groupmap_attr_list,
3237 LDAP_ATTR_SID_LIST),
3238 sid_string_static(member));
3240 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3242 ldap_mods_free(mods, True);
3243 ldap_msgfree(result);
3246 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3247 return NT_STATUS_MEMBER_IN_ALIAS;
3250 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3251 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3254 if (rc != LDAP_SUCCESS) {
3255 return NT_STATUS_UNSUCCESSFUL;
3258 return NT_STATUS_OK;
3261 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3262 const DOM_SID *alias,
3263 const DOM_SID *member)
3265 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3268 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3269 const DOM_SID *alias,
3270 const DOM_SID *member)
3272 return ldapsam_modify_aliasmem(methods, alias, member,
3276 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3277 const DOM_SID *alias,
3278 DOM_SID **pp_members,
3279 size_t *p_num_members)
3281 struct ldapsam_privates *ldap_state =
3282 (struct ldapsam_privates *)methods->private_data;
3283 LDAPMessage *result = NULL;
3284 LDAPMessage *entry = NULL;
3289 size_t num_members = 0;
3290 enum SID_NAME_USE type = SID_NAME_USE_NONE;
3295 if (sid_check_is_in_builtin(alias)) {
3296 type = SID_NAME_WKN_GRP;
3299 if (sid_check_is_in_our_domain(alias)) {
3300 type = SID_NAME_ALIAS;
3303 if (type == SID_NAME_USE_NONE) {
3304 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3305 sid_string_static(alias)));
3306 return NT_STATUS_NO_SUCH_ALIAS;
3309 pstr_sprintf(filter,
3310 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3311 LDAP_OBJ_GROUPMAP, sid_string_static(alias),
3314 if (ldapsam_search_one_group(ldap_state, filter,
3315 &result) != LDAP_SUCCESS)
3316 return NT_STATUS_NO_SUCH_ALIAS;
3318 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3322 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3323 ldap_msgfree(result);
3324 return NT_STATUS_NO_SUCH_ALIAS;
3328 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3329 "filter %s: count=%d\n", filter, count));
3330 ldap_msgfree(result);
3331 return NT_STATUS_NO_SUCH_ALIAS;
3334 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3338 ldap_msgfree(result);
3339 return NT_STATUS_UNSUCCESSFUL;
3342 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3344 get_attr_key2string(groupmap_attr_list,
3345 LDAP_ATTR_SID_LIST));
3347 if (values == NULL) {
3348 ldap_msgfree(result);
3349 return NT_STATUS_OK;
3352 count = ldap_count_values(values);
3354 for (i=0; i<count; i++) {
3357 if (!string_to_sid(&member, values[i]))
3360 add_sid_to_array(NULL, &member, pp_members, &num_members);
3363 *p_num_members = num_members;
3364 ldap_value_free(values);
3365 ldap_msgfree(result);
3367 return NT_STATUS_OK;
3370 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3371 TALLOC_CTX *mem_ctx,
3372 const DOM_SID *domain_sid,
3373 const DOM_SID *members,
3375 uint32 **pp_alias_rids,
3376 size_t *p_num_alias_rids)
3378 struct ldapsam_privates *ldap_state =
3379 (struct ldapsam_privates *)methods->private_data;
3382 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3384 LDAPMessage *result = NULL;
3385 LDAPMessage *entry = NULL;
3389 enum SID_NAME_USE type = SID_NAME_USE_NONE;
3391 if (sid_check_is_builtin(domain_sid)) {
3392 type = SID_NAME_WKN_GRP;
3395 if (sid_check_is_domain(domain_sid)) {
3396 type = SID_NAME_ALIAS;
3399 if (type == SID_NAME_USE_NONE) {
3400 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3401 sid_string_static(domain_sid)));
3402 return NT_STATUS_UNSUCCESSFUL;
3405 filter = talloc_asprintf(mem_ctx,
3406 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3407 LDAP_OBJ_GROUPMAP, type);
3409 for (i=0; i<num_members; i++)
3410 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3412 sid_string_static(&members[i]));
3414 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3416 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3417 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3419 if (rc != LDAP_SUCCESS)
3420 return NT_STATUS_UNSUCCESSFUL;
3422 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3424 for (entry = ldap_first_entry(ldap_struct, result);
3426 entry = ldap_next_entry(ldap_struct, entry))
3432 if (!smbldap_get_single_attribute(ldap_struct, entry,
3438 if (!string_to_sid(&sid, sid_str))
3441 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3444 add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3448 ldap_msgfree(result);
3449 return NT_STATUS_OK;
3452 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3456 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3458 LDAPMod **mods = NULL;
3459 fstring value_string;
3460 const char *policy_attr = NULL;
3462 struct ldapsam_privates *ldap_state =
3463 (struct ldapsam_privates *)methods->private_data;
3465 const char *attrs[2];
3467 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3469 if (!ldap_state->domain_dn) {
3470 return NT_STATUS_INVALID_PARAMETER;
3473 policy_attr = get_account_policy_attr(policy_index);
3474 if (policy_attr == NULL) {
3475 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3480 attrs[0] = policy_attr;
3483 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3485 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3487 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3490 ldap_mods_free(mods, True);
3492 if (rc != LDAP_SUCCESS) {
3496 if (!cache_account_policy_set(policy_index, value)) {
3497 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3498 "update local tdb cache\n"));
3502 return NT_STATUS_OK;
3505 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3506 int policy_index, uint32 value)
3508 if (!account_policy_migrated(False)) {
3509 return (account_policy_set(policy_index, value)) ?
3510 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3513 return ldapsam_set_account_policy_in_ldap(methods, policy_index,
3517 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3521 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3522 LDAPMessage *result = NULL;
3523 LDAPMessage *entry = NULL;
3527 const char *policy_attr = NULL;
3529 struct ldapsam_privates *ldap_state =
3530 (struct ldapsam_privates *)methods->private_data;
3532 const char *attrs[2];
3534 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3536 if (!ldap_state->domain_dn) {
3537 return NT_STATUS_INVALID_PARAMETER;
3540 policy_attr = get_account_policy_attr(policy_index);
3542 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3543 "policy index: %d\n", policy_index));
3547 attrs[0] = policy_attr;
3550 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3551 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
3554 if (rc != LDAP_SUCCESS) {
3558 count = ldap_count_entries(priv2ld(ldap_state), result);
3563 entry = ldap_first_entry(priv2ld(ldap_state), result);
3564 if (entry == NULL) {
3568 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3573 *value = (uint32)atol(vals[0]);
3575 ntstatus = NT_STATUS_OK;
3579 ldap_value_free(vals);
3580 ldap_msgfree(result);
3585 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3587 - if user hasn't decided to use account policies inside LDAP just reuse the
3590 - if there is a valid cache entry, return that
3591 - if there is an LDAP entry, update cache and return
3592 - otherwise set to default, update cache and return
3596 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
3597 int policy_index, uint32 *value)
3599 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3601 if (!account_policy_migrated(False)) {
3602 return (account_policy_get(policy_index, value))
3603 ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3606 if (cache_account_policy_get(policy_index, value)) {
3607 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3609 return NT_STATUS_OK;
3612 ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
3614 if (NT_STATUS_IS_OK(ntstatus)) {
3618 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3622 /* should we automagically migrate old tdb value here ? */
3623 if (account_policy_get(policy_index, value))
3626 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3627 "default\n", policy_index));
3630 if (!account_policy_get_default(policy_index, value)) {
3636 ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
3637 if (!NT_STATUS_IS_OK(ntstatus)) {
3643 if (!cache_account_policy_set(policy_index, *value)) {
3644 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3645 "tdb as a cache\n"));
3646 return NT_STATUS_UNSUCCESSFUL;
3649 return NT_STATUS_OK;
3652 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
3653 const DOM_SID *domain_sid,
3659 struct ldapsam_privates *ldap_state =
3660 (struct ldapsam_privates *)methods->private_data;
3661 LDAPMessage *msg = NULL;
3663 char *allsids = NULL;
3664 int i, rc, num_mapped;
3665 NTSTATUS result = NT_STATUS_NO_MEMORY;
3666 TALLOC_CTX *mem_ctx;
3670 mem_ctx = talloc_new(NULL);
3671 if (mem_ctx == NULL) {
3672 DEBUG(0, ("talloc_new failed\n"));
3676 if (!sid_check_is_builtin(domain_sid) &&
3677 !sid_check_is_domain(domain_sid)) {
3678 result = NT_STATUS_INVALID_PARAMETER;
3682 for (i=0; i<num_rids; i++)
3683 attrs[i] = SID_NAME_UNKNOWN;
3685 allsids = talloc_strdup(mem_ctx, "");
3686 if (allsids == NULL) {
3690 for (i=0; i<num_rids; i++) {
3692 sid_copy(&sid, domain_sid);
3693 sid_append_rid(&sid, rids[i]);
3694 allsids = talloc_asprintf_append(allsids, "(sambaSid=%s)",
3695 sid_string_static(&sid));
3696 if (allsids == NULL) {
3701 /* First look for users */
3705 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
3707 filter = talloc_asprintf(
3708 mem_ctx, ("(&(objectClass=sambaSamAccount)(|%s))"),
3711 if (filter == NULL) {
3715 rc = smbldap_search(ldap_state->smbldap_state,
3716 lp_ldap_user_suffix(),
3717 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
3719 talloc_autofree_ldapmsg(mem_ctx, msg);
3722 if (rc != LDAP_SUCCESS)
3725 ld = ldap_state->smbldap_state->ldap_struct;
3728 for (entry = ldap_first_entry(ld, msg);
3730 entry = ldap_next_entry(ld, entry)) {
3735 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
3737 DEBUG(2, ("Could not find sid from ldap entry\n"));
3741 name = smbldap_talloc_single_attribute(ld, entry, "uid",
3744 DEBUG(2, ("Could not retrieve uid attribute\n"));
3748 for (rid_index = 0; rid_index < num_rids; rid_index++) {
3749 if (rid == rids[rid_index])
3753 if (rid_index == num_rids) {
3754 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
3758 attrs[rid_index] = SID_NAME_USER;
3759 names[rid_index] = name;
3763 if (num_mapped == num_rids) {
3764 /* No need to look for groups anymore -- we're done */
3765 result = NT_STATUS_OK;
3769 /* Same game for groups */
3773 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
3774 "sambaGroupType", NULL };
3776 filter = talloc_asprintf(
3777 mem_ctx, "(&(objectClass=sambaGroupMapping)(|%s))",
3779 if (filter == NULL) {
3783 rc = smbldap_search(ldap_state->smbldap_state,
3784 lp_ldap_group_suffix(),
3785 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
3787 talloc_autofree_ldapmsg(mem_ctx, msg);
3790 if (rc != LDAP_SUCCESS)
3793 /* ldap_struct might have changed due to a reconnect */
3795 ld = ldap_state->smbldap_state->ldap_struct;
3797 /* For consistency checks, we already checked we're only domain or builtin */
3799 is_builtin = sid_check_is_builtin(domain_sid);
3801 for (entry = ldap_first_entry(ld, msg);
3803 entry = ldap_next_entry(ld, entry))
3808 enum SID_NAME_USE type;
3809 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
3811 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
3814 DEBUG(2, ("Could not extract type from ldap entry %s\n",
3821 /* Consistency checks */
3822 if ((is_builtin && (type != SID_NAME_WKN_GRP)) ||
3823 (!is_builtin && ((type != SID_NAME_ALIAS) &&
3824 (type != SID_NAME_DOM_GRP)))) {
3825 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
3828 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
3830 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
3834 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
3837 DEBUG(10, ("Could not retrieve 'cn' attribute from %s\n",
3839 attr = smbldap_talloc_single_attribute(
3840 ld, entry, "displayName", names);
3844 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
3849 for (rid_index = 0; rid_index < num_rids; rid_index++) {
3850 if (rid == rids[rid_index])
3854 if (rid_index == num_rids) {
3855 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
3859 attrs[rid_index] = type;
3860 names[rid_index] = attr;
3864 result = NT_STATUS_NONE_MAPPED;
3867 result = (num_mapped == num_rids) ?
3868 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
3870 TALLOC_FREE(mem_ctx);
3874 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
3876 char *filter = NULL;
3877 char *escaped = NULL;
3878 char *result = NULL;
3880 asprintf(&filter, "(&%s(objectclass=sambaSamAccount))",
3882 if (filter == NULL) goto done;
3884 escaped = escape_ldap_string_alloc(username);
3885 if (escaped == NULL) goto done;
3887 result = talloc_string_sub(mem_ctx, filter, "%u", username);
3896 const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
3900 const char **result;
3902 va_start(ap, mem_ctx);
3903 while (va_arg(ap, const char *) != NULL)
3907 result = TALLOC_ARRAY(mem_ctx, const char *, num+1);
3909 va_start(ap, mem_ctx);
3910 for (i=0; i<num; i++)
3911 result[i] = talloc_strdup(mem_ctx, va_arg(ap, const char*));
3918 struct ldap_search_state {
3919 struct smbldap_state *connection;
3929 void *pagedresults_cookie;
3931 LDAPMessage *entries, *current_entry;
3932 BOOL (*ldap2displayentry)(struct ldap_search_state *state,
3933 TALLOC_CTX *mem_ctx,
3934 LDAP *ld, LDAPMessage *entry,
3935 struct samr_displayentry *result);
3938 static BOOL ldapsam_search_firstpage(struct pdb_search *search)
3940 struct ldap_search_state *state = search->private_data;
3942 int rc = LDAP_OPERATIONS_ERROR;
3944 state->entries = NULL;
3946 if (state->connection->paged_results) {
3947 rc = smbldap_search_paged(state->connection, state->base,
3948 state->scope, state->filter,
3949 state->attrs, state->attrsonly,
3950 lp_ldap_page_size(), &state->entries,
3951 &state->pagedresults_cookie);
3954 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
3956 if (state->entries != NULL) {
3957 /* Left over from unsuccessful paged attempt */
3958 ldap_msgfree(state->entries);
3959 state->entries = NULL;
3962 rc = smbldap_search(state->connection, state->base,
3963 state->scope, state->filter, state->attrs,
3964 state->attrsonly, &state->entries);
3966 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
3969 /* Ok, the server was lying. It told us it could do paged
3970 * searches when it could not. */
3971 state->connection->paged_results = False;
3974 ld = state->connection->ldap_struct;
3976 DEBUG(5, ("Don't have an LDAP connection right after a "
3980 state->current_entry = ldap_first_entry(ld, state->entries);
3982 if (state->current_entry == NULL) {
3983 ldap_msgfree(state->entries);
3984 state->entries = NULL;
3990 static BOOL ldapsam_search_nextpage(struct pdb_search *search)
3992 struct ldap_search_state *state = search->private_data;
3995 if (!state->connection->paged_results) {
3996 /* There is no next page when there are no paged results */
4000 rc = smbldap_search_paged(state->connection, state->base,
4001 state->scope, state->filter, state->attrs,
4002 state->attrsonly, lp_ldap_page_size(),
4004 &state->pagedresults_cookie);
4006 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4009 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4011 if (state->current_entry == NULL) {
4012 ldap_msgfree(state->entries);
4013 state->entries = NULL;
4019 static BOOL ldapsam_search_next_entry(struct pdb_search *search,
4020 struct samr_displayentry *entry)
4022 struct ldap_search_state *state = search->private_data;
4026 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4029 if ((state->entries == NULL) &&
4030 !ldapsam_search_nextpage(search))
4033 result = state->ldap2displayentry(state, search->mem_ctx, state->connection->ldap_struct,
4034 state->current_entry, entry);
4038 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4039 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4040 if (dn != NULL) ldap_memfree(dn);
4043 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4045 if (state->current_entry == NULL) {
4046 ldap_msgfree(state->entries);
4047 state->entries = NULL;
4050 if (!result) goto retry;
4055 static void ldapsam_search_end(struct pdb_search *search)
4057 struct ldap_search_state *state = search->private_data;
4060 if (state->pagedresults_cookie == NULL)
4063 if (state->entries != NULL)
4064 ldap_msgfree(state->entries);
4066 state->entries = NULL;
4067 state->current_entry = NULL;
4069 if (!state->connection->paged_results)
4072 /* Tell the LDAP server we're not interested in the rest anymore. */
4074 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4075 state->filter, state->attrs,
4076 state->attrsonly, 0, &state->entries,
4077 &state->pagedresults_cookie);
4079 if (rc != LDAP_SUCCESS)
4080 DEBUG(5, ("Could not end search properly\n"));
4085 static BOOL ldapuser2displayentry(struct ldap_search_state *state,
4086 TALLOC_CTX *mem_ctx,
4087 LDAP *ld, LDAPMessage *entry,
4088 struct samr_displayentry *result)
4094 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4095 if ((vals == NULL) || (vals[0] == NULL)) {
4096 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4099 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4100 ldap_value_free(vals);
4102 if ((state->acct_flags != 0) &&
4103 ((state->acct_flags & acct_flags) == 0))
4106 result->acct_flags = acct_flags;
4107 result->account_name = "";
4108 result->fullname = "";
4109 result->description = "";
4111 vals = ldap_get_values(ld, entry, "uid");
4112 if ((vals == NULL) || (vals[0] == NULL)) {
4113 DEBUG(5, ("\"uid\" not found\n"));
4116 pull_utf8_talloc(mem_ctx,
4117 CONST_DISCARD(char **, &result->account_name),
4119 ldap_value_free(vals);
4121 vals = ldap_get_values(ld, entry, "displayName");
4122 if ((vals == NULL) || (vals[0] == NULL))
4123 DEBUG(8, ("\"displayName\" not found\n"));
4125 pull_utf8_talloc(mem_ctx,
4126 CONST_DISCARD(char **, &result->fullname),
4128 ldap_value_free(vals);
4130 vals = ldap_get_values(ld, entry, "description");
4131 if ((vals == NULL) || (vals[0] == NULL))
4132 DEBUG(8, ("\"description\" not found\n"));
4134 pull_utf8_talloc(mem_ctx,
4135 CONST_DISCARD(char **, &result->description),
4137 ldap_value_free(vals);
4139 if ((result->account_name == NULL) ||
4140 (result->fullname == NULL) ||
4141 (result->description == NULL)) {
4142 DEBUG(0, ("talloc failed\n"));
4146 vals = ldap_get_values(ld, entry, "sambaSid");
4147 if ((vals == NULL) || (vals[0] == NULL)) {
4148 DEBUG(0, ("\"objectSid\" not found\n"));
4152 if (!string_to_sid(&sid, vals[0])) {
4153 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4154 ldap_value_free(vals);
4157 ldap_value_free(vals);
4159 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4160 DEBUG(0, ("sid %s does not belong to our domain\n",
4161 sid_string_static(&sid)));
4169 static BOOL ldapsam_search_users(struct pdb_methods *methods,
4170 struct pdb_search *search,
4173 struct ldapsam_privates *ldap_state = methods->private_data;
4174 struct ldap_search_state *state;
4176 state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
4177 if (state == NULL) {
4178 DEBUG(0, ("talloc failed\n"));
4182 state->connection = ldap_state->smbldap_state;
4184 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4185 state->base = lp_ldap_user_suffix();
4186 else if ((acct_flags != 0) &&
4187 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4188 state->base = lp_ldap_machine_suffix();
4190 state->base = lp_ldap_suffix();
4192 state->acct_flags = acct_flags;
4193 state->base = talloc_strdup(search->mem_ctx, state->base);
4194 state->scope = LDAP_SCOPE_SUBTREE;
4195 state->filter = get_ldap_filter(search->mem_ctx, "*");
4196 state->attrs = talloc_attrs(search->mem_ctx, "uid", "sambaSid",
4197 "displayName", "description",
4198 "sambaAcctFlags", NULL);
4199 state->attrsonly = 0;
4200 state->pagedresults_cookie = NULL;
4201 state->entries = NULL;
4202 state->ldap2displayentry = ldapuser2displayentry;
4204 if ((state->filter == NULL) || (state->attrs == NULL)) {
4205 DEBUG(0, ("talloc failed\n"));
4209 search->private_data = state;
4210 search->next_entry = ldapsam_search_next_entry;
4211 search->search_end = ldapsam_search_end;
4213 return ldapsam_search_firstpage(search);
4216 static BOOL ldapgroup2displayentry(struct ldap_search_state *state,
4217 TALLOC_CTX *mem_ctx,
4218 LDAP *ld, LDAPMessage *entry,
4219 struct samr_displayentry *result)
4225 result->account_name = "";
4226 result->fullname = "";
4227 result->description = "";
4230 vals = ldap_get_values(ld, entry, "sambaGroupType");
4231 if ((vals == NULL) || (vals[0] == NULL)) {
4232 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4234 ldap_value_free(vals);
4239 group_type = atoi(vals[0]);
4241 if ((state->group_type != 0) &&
4242 ((state->group_type != group_type))) {
4243 ldap_value_free(vals);
4247 ldap_value_free(vals);
4249 /* display name is the NT group name */
4251 vals = ldap_get_values(ld, entry, "displayName");
4252 if ((vals == NULL) || (vals[0] == NULL)) {
4253 DEBUG(8, ("\"displayName\" not found\n"));
4255 /* fallback to the 'cn' attribute */
4256 vals = ldap_get_values(ld, entry, "cn");
4257 if ((vals == NULL) || (vals[0] == NULL)) {
4258 DEBUG(5, ("\"cn\" not found\n"));
4261 pull_utf8_talloc(mem_ctx,
4262 CONST_DISCARD(char **, &result->account_name),
4266 pull_utf8_talloc(mem_ctx,
4267 CONST_DISCARD(char **, &result->account_name),
4271 ldap_value_free(vals);
4273 vals = ldap_get_values(ld, entry, "description");
4274 if ((vals == NULL) || (vals[0] == NULL))
4275 DEBUG(8, ("\"description\" not found\n"));
4277 pull_utf8_talloc(mem_ctx,
4278 CONST_DISCARD(char **, &result->description),
4280 ldap_value_free(vals);
4282 if ((result->account_name == NULL) ||
4283 (result->fullname == NULL) ||
4284 (result->description == NULL)) {
4285 DEBUG(0, ("talloc failed\n"));
4289 vals = ldap_get_values(ld, entry, "sambaSid");
4290 if ((vals == NULL) || (vals[0] == NULL)) {
4291 DEBUG(0, ("\"objectSid\" not found\n"));
4293 ldap_value_free(vals);
4298 if (!string_to_sid(&sid, vals[0])) {
4299 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4303 ldap_value_free(vals);
4305 switch (group_type) {
4306 case SID_NAME_DOM_GRP:
4307 case SID_NAME_ALIAS:
4309 if (!sid_peek_check_rid(get_global_sam_sid(), &sid,
4311 DEBUG(0, ("%s is not in our domain\n",
4312 sid_string_static(&sid)));
4317 case SID_NAME_WKN_GRP:
4319 if (!sid_peek_check_rid(&global_sid_Builtin, &sid,
4322 DEBUG(0, ("%s is not in builtin sid\n",
4323 sid_string_static(&sid)));
4329 DEBUG(0,("unkown group type: %d\n", group_type));
4336 static BOOL ldapsam_search_grouptype(struct pdb_methods *methods,
4337 struct pdb_search *search,
4338 enum SID_NAME_USE type)
4340 struct ldapsam_privates *ldap_state = methods->private_data;
4341 struct ldap_search_state *state;
4343 state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
4344 if (state == NULL) {
4345 DEBUG(0, ("talloc failed\n"));
4349 state->connection = ldap_state->smbldap_state;
4351 state->base = talloc_strdup(search->mem_ctx, lp_ldap_group_suffix());
4352 state->connection = ldap_state->smbldap_state;
4353 state->scope = LDAP_SCOPE_SUBTREE;
4354 state->filter = talloc_asprintf(search->mem_ctx,
4355 "(&(objectclass=sambaGroupMapping)"
4356 "(sambaGroupType=%d))", type);
4357 state->attrs = talloc_attrs(search->mem_ctx, "cn", "sambaSid",
4358 "displayName", "description",
4359 "sambaGroupType", NULL);
4360 state->attrsonly = 0;
4361 state->pagedresults_cookie = NULL;
4362 state->entries = NULL;
4363 state->group_type = type;
4364 state->ldap2displayentry = ldapgroup2displayentry;
4366 if ((state->filter == NULL) || (state->attrs == NULL)) {
4367 DEBUG(0, ("talloc failed\n"));
4371 search->private_data = state;
4372 search->next_entry = ldapsam_search_next_entry;
4373 search->search_end = ldapsam_search_end;
4375 return ldapsam_search_firstpage(search);
4378 static BOOL ldapsam_search_groups(struct pdb_methods *methods,
4379 struct pdb_search *search)
4381 return ldapsam_search_grouptype(methods, search, SID_NAME_DOM_GRP);
4384 static BOOL ldapsam_search_aliases(struct pdb_methods *methods,
4385 struct pdb_search *search,
4388 if (sid_check_is_domain(sid))
4389 return ldapsam_search_grouptype(methods, search,
4392 if (sid_check_is_builtin(sid))
4393 return ldapsam_search_grouptype(methods, search,
4396 DEBUG(5, ("Don't know SID %s\n", sid_string_static(sid)));
4400 static BOOL ldapsam_rid_algorithm(struct pdb_methods *methods)
4405 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4408 struct smbldap_state *smbldap_state = priv->smbldap_state;
4410 LDAPMessage *result = NULL;
4411 LDAPMessage *entry = NULL;
4412 LDAPMod **mods = NULL;
4418 TALLOC_CTX *mem_ctx;
4420 mem_ctx = talloc_new(NULL);
4421 if (mem_ctx == NULL) {
4422 DEBUG(0, ("talloc_new failed\n"));
4423 return NT_STATUS_NO_MEMORY;
4426 status = smbldap_search_domain_info(smbldap_state, &result,
4427 get_global_sam_name(), False);
4428 if (!NT_STATUS_IS_OK(status)) {
4429 DEBUG(3, ("Could not get domain info: %s\n",
4430 nt_errstr(status)));
4434 talloc_autofree_ldapmsg(mem_ctx, result);
4436 entry = ldap_first_entry(priv2ld(priv), result);
4437 if (entry == NULL) {
4438 DEBUG(0, ("Could not get domain info entry\n"));
4439 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4443 /* Find the largest of the three attributes "sambaNextRid",
4444 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4445 concept of differentiating between user and group rids, and will
4446 use only "sambaNextRid" in the future. But for compatibility
4447 reasons I look if others have chosen different strategies -- VL */
4449 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4450 "sambaNextRid", mem_ctx);
4451 if (value != NULL) {
4452 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4453 nextRid = MAX(nextRid, tmp);
4456 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4457 "sambaNextUserRid", mem_ctx);
4458 if (value != NULL) {
4459 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4460 nextRid = MAX(nextRid, tmp);
4463 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4464 "sambaNextGroupRid", mem_ctx);
4465 if (value != NULL) {
4466 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4467 nextRid = MAX(nextRid, tmp);
4471 nextRid = BASE_RID-1;
4476 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4477 talloc_asprintf(mem_ctx, "%d", nextRid));
4478 talloc_autofree_ldapmod(mem_ctx, mods);
4480 rc = smbldap_modify(smbldap_state,
4481 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry),
4484 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4487 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4490 if (NT_STATUS_IS_OK(status)) {
4494 TALLOC_FREE(mem_ctx);
4498 static BOOL ldapsam_new_rid(struct pdb_methods *methods, uint32 *rid)
4502 for (i=0; i<10; i++) {
4503 NTSTATUS result = ldapsam_get_new_rid(methods->private_data,
4505 if (NT_STATUS_IS_OK(result)) {
4509 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4513 /* The ldap update failed (maybe a race condition), retry */
4516 /* Tried 10 times, fail. */
4520 static BOOL ldapsam_sid_to_id(struct pdb_methods *methods,
4522 union unid_t *id, enum SID_NAME_USE *type)
4524 struct ldapsam_privates *priv = methods->private_data;
4526 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4528 LDAPMessage *result = NULL;
4529 LDAPMessage *entry = NULL;
4534 TALLOC_CTX *mem_ctx;
4536 mem_ctx = talloc_new(NULL);
4537 if (mem_ctx == NULL) {
4538 DEBUG(0, ("talloc_new failed\n"));
4542 filter = talloc_asprintf(mem_ctx,
4544 "(|(objectClass=sambaGroupMapping)"
4545 "(objectClass=sambaSamAccount)))",
4546 sid_string_static(sid));
4547 if (filter == NULL) {
4548 DEBUG(5, ("talloc_asprintf failed\n"));
4552 rc = smbldap_search_suffix(priv->smbldap_state, filter,
4554 if (rc != LDAP_SUCCESS) {
4557 talloc_autofree_ldapmsg(mem_ctx, result);
4559 if (ldap_count_entries(priv2ld(priv), result) != 1) {
4560 DEBUG(10, ("Got %d entries, expected one\n",
4561 ldap_count_entries(priv2ld(priv), result)));
4565 entry = ldap_first_entry(priv2ld(priv), result);
4567 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4568 "sambaGroupType", mem_ctx);
4570 if (value != NULL) {
4571 const char *gid_str;
4574 gid_str = smbldap_talloc_single_attribute(
4575 priv2ld(priv), entry, "gidNumber", mem_ctx);
4576 if (gid_str == NULL) {
4577 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4578 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
4583 id->gid = strtoul(gid_str, NULL, 10);
4584 *type = strtoul(value, NULL, 10);
4589 /* It must be a user */
4591 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4592 "uidNumber", mem_ctx);
4593 if (value == NULL) {
4594 DEBUG(1, ("Could not find uidNumber in %s\n",
4595 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
4599 id->uid = strtoul(value, NULL, 10);
4600 *type = SID_NAME_USER;
4604 TALLOC_FREE(mem_ctx);
4608 /**********************************************************************
4610 *********************************************************************/
4612 static void free_private_data(void **vp)
4614 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
4616 smbldap_free_struct(&(*ldap_state)->smbldap_state);
4618 if ((*ldap_state)->result != NULL) {
4619 ldap_msgfree((*ldap_state)->result);
4620 (*ldap_state)->result = NULL;
4622 if ((*ldap_state)->domain_dn != NULL) {
4623 SAFE_FREE((*ldap_state)->domain_dn);
4628 /* No need to free any further, as it is talloc()ed */
4631 /*********************************************************************
4632 Intitalise the parts of the pdb_methods structure that are common to
4634 *********************************************************************/
4636 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
4639 struct ldapsam_privates *ldap_state;
4641 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
4645 (*pdb_method)->name = "ldapsam";
4647 (*pdb_method)->setsampwent = ldapsam_setsampwent;
4648 (*pdb_method)->endsampwent = ldapsam_endsampwent;
4649 (*pdb_method)->getsampwent = ldapsam_getsampwent;
4650 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
4651 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
4652 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
4653 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
4654 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
4655 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
4657 (*pdb_method)->getgrsid = ldapsam_getgrsid;
4658 (*pdb_method)->getgrgid = ldapsam_getgrgid;
4659 (*pdb_method)->getgrnam = ldapsam_getgrnam;
4660 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
4661 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
4662 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
4663 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
4665 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
4666 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
4668 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
4670 (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
4671 (*pdb_method)->new_rid = ldapsam_new_rid;
4673 /* TODO: Setup private data and free */
4675 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
4676 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
4677 return NT_STATUS_NO_MEMORY;
4680 nt_status = smbldap_init(*pdb_method, location, &ldap_state->smbldap_state);
4682 if ( !NT_STATUS_IS_OK(nt_status) ) {
4686 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
4687 return NT_STATUS_NO_MEMORY;
4690 (*pdb_method)->private_data = ldap_state;
4692 (*pdb_method)->free_private_data = free_private_data;
4694 return NT_STATUS_OK;
4697 /**********************************************************************
4698 Initialise the 'compat' mode for pdb_ldap
4699 *********************************************************************/
4701 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
4704 struct ldapsam_privates *ldap_state;
4705 char *uri = talloc_strdup( NULL, location );
4707 #ifdef WITH_LDAP_SAMCONFIG
4709 int ldap_port = lp_ldap_port();
4711 /* remap default port if not using SSL (ie clear or TLS) */
4712 if ( (lp_ldap_ssl() != LDAP_SSL_ON) && (ldap_port == 636) ) {
4716 uri = talloc_asprintf(NULL, "%s://%s:%d", lp_ldap_ssl() == LDAP_SSL_ON ? "ldaps" : "ldap", lp_ldap_server(), ldap_port);
4718 return NT_STATUS_NO_MEMORY;
4724 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common( pdb_method, uri ))) {
4728 /* the module itself stores a copy of the location so throw this one away */
4733 (*pdb_method)->name = "ldapsam_compat";
4735 ldap_state = (*pdb_method)->private_data;
4736 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
4738 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
4740 return NT_STATUS_OK;
4743 /**********************************************************************
4744 Initialise the normal mode for pdb_ldap
4745 *********************************************************************/
4747 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
4750 struct ldapsam_privates *ldap_state;
4751 uint32 alg_rid_base;
4752 pstring alg_rid_base_string;
4753 LDAPMessage *result = NULL;
4754 LDAPMessage *entry = NULL;
4755 DOM_SID ldap_domain_sid;
4756 DOM_SID secrets_domain_sid;
4757 pstring domain_sid_string;
4760 nt_status = pdb_init_ldapsam_common(pdb_method, location);
4761 if (!NT_STATUS_IS_OK(nt_status)) {
4765 (*pdb_method)->name = "ldapsam";
4767 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
4768 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
4769 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
4770 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
4771 (*pdb_method)->search_users = ldapsam_search_users;
4772 (*pdb_method)->search_groups = ldapsam_search_groups;
4773 (*pdb_method)->search_aliases = ldapsam_search_aliases;
4775 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
4776 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
4777 (*pdb_method)->enum_group_memberships =
4778 ldapsam_enum_group_memberships;
4779 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
4780 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
4783 ldap_state = (*pdb_method)->private_data;
4784 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
4786 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
4788 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
4790 ldap_state->domain_name, True);
4792 if ( !NT_STATUS_IS_OK(nt_status) ) {
4793 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
4794 "info, nor add one to the domain\n"));
4795 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
4796 "will be unable to allocate new users/groups, "
4797 "and will risk BDCs having inconsistant SIDs\n"));
4798 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
4799 return NT_STATUS_OK;
4802 /* Given that the above might fail, everything below this must be
4805 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
4808 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
4810 ldap_msgfree(result);
4811 return NT_STATUS_UNSUCCESSFUL;
4814 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
4816 return NT_STATUS_UNSUCCESSFUL;
4819 ldap_state->domain_dn = smb_xstrdup(dn);
4822 if (smbldap_get_single_pstring(
4823 ldap_state->smbldap_state->ldap_struct,
4825 get_userattr_key2string(ldap_state->schema_ver,
4826 LDAP_ATTR_USER_SID),
4827 domain_sid_string)) {
4829 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
4830 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
4831 "read as a valid SID\n", domain_sid_string));
4832 return NT_STATUS_INVALID_PARAMETER;
4834 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
4835 &secrets_domain_sid);
4836 if (!found_sid || !sid_equal(&secrets_domain_sid,
4837 &ldap_domain_sid)) {
4838 fstring new_sid_str, old_sid_str;
4839 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
4840 "%s based on pdb_ldap results %s -> %s\n",
4841 ldap_state->domain_name,
4842 sid_to_string(old_sid_str,
4843 &secrets_domain_sid),
4844 sid_to_string(new_sid_str,
4845 &ldap_domain_sid)));
4847 /* reset secrets.tdb sid */
4848 secrets_store_domain_sid(ldap_state->domain_name,
4850 DEBUG(1, ("New global sam SID: %s\n",
4851 sid_to_string(new_sid_str,
4852 get_global_sam_sid())));
4854 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
4857 if (smbldap_get_single_pstring(
4858 ldap_state->smbldap_state->ldap_struct,
4860 get_attr_key2string( dominfo_attr_list,
4861 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
4862 alg_rid_base_string)) {
4863 alg_rid_base = (uint32)atol(alg_rid_base_string);
4864 if (alg_rid_base != algorithmic_rid_base()) {
4865 DEBUG(0, ("The value of 'algorithmic RID base' has "
4866 "changed since the LDAP\n"
4867 "database was initialised. Aborting. \n"));
4868 ldap_msgfree(result);
4869 return NT_STATUS_UNSUCCESSFUL;
4872 ldap_msgfree(result);
4874 return NT_STATUS_OK;
4877 NTSTATUS pdb_ldap_init(void)
4880 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
4883 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
4886 /* Let pdb_nds register backends */
4889 return NT_STATUS_OK;