2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * persistent connections: if using NSS LDAP, many connections are made
29 * however, using only one within Samba would be nice
31 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
33 * Other LDAP based login attributes: accountExpires, etc.
34 * (should be the domain of Samba proper, but the sam_password/struct samu
35 * structures don't have fields for some of these attributes)
37 * SSL is done, but can't get the certificate based authentication to work
38 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
41 /* NOTE: this will NOT work against an Active Directory server
42 * due to the fact that the two password fields cannot be retrieved
43 * from a server; recommend using security = domain in this situation
50 #define DBGC_CLASS DBGC_PASSDB
56 * Work around versions of the LDAP client libs that don't have the OIDs
57 * defined, or have them defined under the old name.
58 * This functionality is really a factor of the server, not the client
62 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
63 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
64 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
65 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
68 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
69 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
70 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
71 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
74 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
75 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
76 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
77 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
83 /**********************************************************************
84 Simple helper function to make stuff better readable
85 **********************************************************************/
87 static LDAP *priv2ld(struct ldapsam_privates *priv)
89 return priv->smbldap_state->ldap_struct;
92 /**********************************************************************
93 Get the attribute name given a user schame version.
94 **********************************************************************/
96 static const char* get_userattr_key2string( int schema_ver, int key )
98 switch ( schema_ver ) {
99 case SCHEMAVER_SAMBAACCOUNT:
100 return get_attr_key2string( attrib_map_v22, key );
102 case SCHEMAVER_SAMBASAMACCOUNT:
103 return get_attr_key2string( attrib_map_v30, key );
106 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
112 /**********************************************************************
113 Return the list of attribute names given a user schema version.
114 **********************************************************************/
116 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
118 switch ( schema_ver ) {
119 case SCHEMAVER_SAMBAACCOUNT:
120 return get_attr_list( mem_ctx, attrib_map_v22 );
122 case SCHEMAVER_SAMBASAMACCOUNT:
123 return get_attr_list( mem_ctx, attrib_map_v30 );
125 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
132 /**************************************************************************
133 Return the list of attribute names to delete given a user schema version.
134 **************************************************************************/
136 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
139 switch ( schema_ver ) {
140 case SCHEMAVER_SAMBAACCOUNT:
141 return get_attr_list( mem_ctx,
142 attrib_map_to_delete_v22 );
144 case SCHEMAVER_SAMBASAMACCOUNT:
145 return get_attr_list( mem_ctx,
146 attrib_map_to_delete_v30 );
148 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
156 /*******************************************************************
157 Generate the LDAP search filter for the objectclass based on the
158 version of the schema we are using.
159 ******************************************************************/
161 static const char* get_objclass_filter( int schema_ver )
163 static fstring objclass_filter;
165 switch( schema_ver ) {
166 case SCHEMAVER_SAMBAACCOUNT:
167 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
169 case SCHEMAVER_SAMBASAMACCOUNT:
170 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
173 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
177 return objclass_filter;
180 /*****************************************************************
181 Scan a sequence number off OpenLDAP's syncrepl contextCSN
182 ******************************************************************/
184 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
186 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
187 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
188 LDAPMessage *msg = NULL;
189 LDAPMessage *entry = NULL;
191 char **values = NULL;
192 int rc, num_result, num_values, rid;
198 /* Unfortunatly there is no proper way to detect syncrepl-support in
199 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
200 * but do not show up in the root-DSE yet. Neither we can query the
201 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
202 * objectclass. Currently we require lp_ldap_suffix() to show up as
203 * namingContext. - Guenther
206 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
211 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
215 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
216 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
217 "as top-level namingContext\n", lp_ldap_suffix()));
221 mem_ctx = talloc_init("ldapsam_get_seq_num");
224 return NT_STATUS_NO_MEMORY;
226 if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
227 ntstatus = NT_STATUS_NO_MEMORY;
231 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
232 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
235 /* consumer syncreplCookie: */
236 /* csn=20050126161620Z#0000001#00#00000 */
237 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
239 pstr_sprintf( suffix, "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
243 /* provider contextCSN */
244 /* 20050126161620Z#000009#00#000000 */
245 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
247 pstr_sprintf( suffix, "cn=ldapsync,%s", lp_ldap_suffix());
251 rc = smbldap_search(ldap_state->smbldap_state, suffix,
252 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
254 if (rc != LDAP_SUCCESS) {
258 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
259 if (num_result != 1) {
260 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
264 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
266 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
270 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
271 if (values == NULL) {
272 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
276 num_values = ldap_count_values(values);
277 if (num_values == 0) {
278 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
283 if (!next_token(&p, tok, "#", sizeof(tok))) {
284 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
289 if (!strncmp(p, "csn=", strlen("csn=")))
292 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
294 *seq_num = generalized_to_unix_time(p);
296 /* very basic sanity check */
298 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
303 ntstatus = NT_STATUS_OK;
307 ldap_value_free(values);
311 talloc_destroy(mem_ctx);
316 /*******************************************************************
317 Run the search by name.
318 ******************************************************************/
320 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
322 LDAPMessage ** result,
326 char *escape_user = escape_ldap_string_alloc(user);
329 return LDAP_NO_MEMORY;
333 * in the filter expression, replace %u with the real name
334 * so in ldap filter, %u MUST exist :-)
336 pstr_sprintf(filter, "(&%s%s)", "(uid=%u)",
337 get_objclass_filter(ldap_state->schema_ver));
340 * have to use this here because $ is filtered out
345 all_string_sub(filter, "%u", escape_user, sizeof(pstring));
346 SAFE_FREE(escape_user);
348 return smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
351 /*******************************************************************
352 Run the search by rid.
353 ******************************************************************/
355 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
356 uint32 rid, LDAPMessage ** result,
362 pstr_sprintf(filter, "(&(rid=%i)%s)", rid,
363 get_objclass_filter(ldap_state->schema_ver));
365 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
370 /*******************************************************************
371 Run the search by SID.
372 ******************************************************************/
374 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
375 const DOM_SID *sid, LDAPMessage ** result,
382 pstr_sprintf(filter, "(&(%s=%s)%s)",
383 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
384 sid_to_string(sid_string, sid),
385 get_objclass_filter(ldap_state->schema_ver));
387 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, attr, result);
392 /*******************************************************************
393 Delete complete object or objectclass and attrs from
394 object found in search_result depending on lp_ldap_delete_dn
395 ******************************************************************/
397 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
400 const char *objectclass,
403 LDAPMod **mods = NULL;
406 BerElement *ptr = NULL;
408 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
410 return LDAP_NO_MEMORY;
413 if (lp_ldap_delete_dn()) {
414 return smbldap_delete(priv->smbldap_state, dn);
417 /* Ok, delete only the SAM attributes */
419 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
421 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
424 /* We are only allowed to delete the attributes that
427 for (attrib = attrs; *attrib != NULL; attrib++) {
428 if (strequal(*attrib, name)) {
429 DEBUG(10, ("ldapsam_delete_entry: deleting "
430 "attribute %s\n", name));
431 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
442 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
443 talloc_autofree_ldapmod(mem_ctx, mods);
445 return smbldap_modify(priv->smbldap_state, dn, mods);
448 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
453 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
454 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
458 strptime(temp, "%Y%m%d%H%M%SZ", &tm);
463 /**********************************************************************
464 Initialize struct samu from an LDAP query.
465 (Based on init_sam_from_buffer in pdb_tdb.c)
466 *********************************************************************/
468 static BOOL init_sam_from_ldap(struct ldapsam_privates *ldap_state,
469 struct samu * sampass,
476 pass_can_change_time,
477 pass_must_change_time,
490 char munged_dial[2048];
492 uint8 smblmpwd[LM_HASH_LEN],
493 smbntpwd[NT_HASH_LEN];
494 BOOL use_samba_attrs = True;
495 uint32 acct_ctrl = 0;
497 uint16 bad_password_count = 0,
500 uint8 hours[MAX_HOURS_LEN];
502 LOGIN_CACHE *cache_entry = NULL;
505 BOOL expand_explicit = lp_passdb_expand_explicit();
508 * do a little initialization
512 nt_username[0] = '\0';
516 logon_script[0] = '\0';
517 profile_path[0] = '\0';
519 munged_dial[0] = '\0';
520 workstations[0] = '\0';
523 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
524 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
528 if (priv2ld(ldap_state) == NULL) {
529 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
530 "ldap_struct is NULL!\n"));
534 if (!smbldap_get_single_pstring(priv2ld(ldap_state), entry, "uid",
536 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
541 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
543 pstrcpy(nt_username, username);
545 pstrcpy(domain, ldap_state->domain_name);
547 pdb_set_username(sampass, username, PDB_SET);
549 pdb_set_domain(sampass, domain, PDB_DEFAULT);
550 pdb_set_nt_username(sampass, nt_username, PDB_SET);
552 /* deal with different attributes between the schema first */
554 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
555 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
556 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID), temp)) {
557 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
560 if (smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
561 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID), temp)) {
562 user_rid = (uint32)atol(temp);
563 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
567 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
568 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
569 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
570 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
575 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
576 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET), temp)) {
577 /* leave as default */
579 pass_last_set_time = (time_t) atol(temp);
580 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
583 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
584 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp)) {
585 /* leave as default */
587 logon_time = (time_t) atol(temp);
588 pdb_set_logon_time(sampass, logon_time, PDB_SET);
591 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
592 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp)) {
593 /* leave as default */
595 logoff_time = (time_t) atol(temp);
596 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
599 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
600 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp)) {
601 /* leave as default */
603 kickoff_time = (time_t) atol(temp);
604 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
607 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
608 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp)) {
609 /* leave as default */
611 pass_can_change_time = (time_t) atol(temp);
612 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
615 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
616 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp)) {
617 /* leave as default */
619 pass_must_change_time = (time_t) atol(temp);
620 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
623 /* recommend that 'gecos' and 'displayName' should refer to the same
624 * attribute OID. userFullName depreciated, only used by Samba
625 * primary rules of LDAP: don't make a new attribute when one is already defined
626 * that fits your needs; using cn then displayName rather than 'userFullName'
629 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
630 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME), fullname)) {
631 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
632 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_CN), fullname)) {
633 /* leave as default */
635 pdb_set_fullname(sampass, fullname, PDB_SET);
638 pdb_set_fullname(sampass, fullname, PDB_SET);
641 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
642 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE), dir_drive))
644 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
646 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
649 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
650 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH), homedir))
652 pdb_set_homedir( sampass,
653 talloc_sub_basic(sampass, username, lp_logon_home()),
656 pstrcpy( tmpstring, homedir );
657 if (expand_explicit) {
658 standard_sub_basic( username, tmpstring,
661 pdb_set_homedir(sampass, tmpstring, PDB_SET);
664 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
665 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT), logon_script))
667 pdb_set_logon_script( sampass,
668 talloc_sub_basic(sampass, username, lp_logon_script()),
671 pstrcpy( tmpstring, logon_script );
672 if (expand_explicit) {
673 standard_sub_basic( username, tmpstring,
676 pdb_set_logon_script(sampass, tmpstring, 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_PROFILE_PATH), profile_path))
682 pdb_set_profile_path( sampass,
683 talloc_sub_basic( sampass, username, lp_logon_path()),
686 pstrcpy( tmpstring, profile_path );
687 if (expand_explicit) {
688 standard_sub_basic( username, tmpstring,
691 pdb_set_profile_path(sampass, tmpstring, PDB_SET);
694 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
695 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC), acct_desc))
697 /* leave as default */
699 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
702 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
703 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS), workstations)) {
704 /* leave as default */;
706 pdb_set_workstations(sampass, workstations, PDB_SET);
709 if (!smbldap_get_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
710 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL), munged_dial, sizeof(munged_dial))) {
711 /* leave as default */;
713 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
716 /* FIXME: hours stuff should be cleaner */
720 memset(hours, 0xff, hours_len);
722 if (ldap_state->is_nds_ldap) {
725 char clear_text_pw[512];
727 /* Make call to Novell eDirectory ldap extension to get clear text password.
728 NOTE: This will only work if we have an SSL connection to eDirectory. */
729 user_dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
730 if (user_dn != NULL) {
731 DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username, user_dn));
733 pwd_len = sizeof(clear_text_pw);
734 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
735 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
736 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
740 ZERO_STRUCT(smblmpwd);
741 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
745 ZERO_STRUCT(smbntpwd);
746 use_samba_attrs = False;
752 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
756 if (use_samba_attrs) {
757 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
758 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW), temp)) {
759 /* leave as default */
761 pdb_gethexpwd(temp, smblmpwd);
762 memset((char *)temp, '\0', strlen(temp)+1);
763 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET))
765 ZERO_STRUCT(smblmpwd);
768 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
769 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW), temp)) {
770 /* leave as default */
772 pdb_gethexpwd(temp, smbntpwd);
773 memset((char *)temp, '\0', strlen(temp)+1);
774 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET))
776 ZERO_STRUCT(smbntpwd);
782 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
784 uint8 *pwhist = NULL;
786 char history_string[MAX_PW_HISTORY_LEN*64];
788 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
790 if ((pwhist = SMB_MALLOC(pwHistLen * PW_HISTORY_ENTRY_LEN)) == NULL){
791 DEBUG(0, ("init_sam_from_ldap: malloc failed!\n"));
794 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
796 if (!smbldap_get_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
797 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
798 history_string, sizeof(history_string))) {
799 /* leave as default - zeros */
801 BOOL hex_failed = False;
802 for (i = 0; i < pwHistLen; i++){
803 /* Get the 16 byte salt. */
804 if (!pdb_gethexpwd(&history_string[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
808 /* Get the 16 byte MD5 hash of salt+passwd. */
809 if (!pdb_gethexpwd(&history_string[(i*64)+32],
810 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN])) {
816 DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
818 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
821 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
828 if (!smbldap_get_single_pstring (ldap_state->smbldap_state->ldap_struct, entry,
829 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO), temp)) {
830 acct_ctrl |= ACB_NORMAL;
832 acct_ctrl = pdb_decode_acct_ctrl(temp);
835 acct_ctrl |= ACB_NORMAL;
837 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
840 pdb_set_hours_len(sampass, hours_len, PDB_SET);
841 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
843 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
844 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_COUNT), temp)) {
845 /* leave as default */
847 bad_password_count = (uint32) atol(temp);
848 pdb_set_bad_password_count(sampass, bad_password_count, PDB_SET);
851 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
852 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_BAD_PASSWORD_TIME), temp)) {
853 /* leave as default */
855 bad_password_time = (time_t) atol(temp);
856 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
860 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
861 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_COUNT), temp)) {
862 /* leave as default */
864 logon_count = (uint32) atol(temp);
865 pdb_set_logon_count(sampass, logon_count, PDB_SET);
868 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
870 if(!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
871 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_HOURS), temp)) {
872 /* leave as default */
874 pdb_gethexhours(temp, hours);
875 memset((char *)temp, '\0', strlen(temp) +1);
876 pdb_set_hours(sampass, hours, PDB_SET);
880 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
881 if (smbldap_get_single_pstring(priv2ld(ldap_state), entry,
882 "uidNumber", temp)) {
883 /* We've got a uid, feed the cache */
884 uid_t uid = strtoul(temp, NULL, 10);
885 store_uid_sid_cache(pdb_get_user_sid(sampass), uid);
889 /* check the timestamp of the cache vs ldap entry */
890 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
894 /* see if we have newer updates */
895 if (!(cache_entry = login_cache_read(sampass))) {
896 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
897 (unsigned int)pdb_get_bad_password_count(sampass),
898 (unsigned int)pdb_get_bad_password_time(sampass)));
902 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
903 (unsigned int)ldap_entry_time, (unsigned int)cache_entry->entry_timestamp,
904 (unsigned int)cache_entry->bad_password_time));
906 if (ldap_entry_time > cache_entry->entry_timestamp) {
907 /* cache is older than directory , so
908 we need to delete the entry but allow the
909 fields to be written out */
910 login_cache_delentry(sampass);
913 pdb_set_acct_ctrl(sampass,
914 pdb_get_acct_ctrl(sampass) |
915 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
917 pdb_set_bad_password_count(sampass,
918 cache_entry->bad_password_count,
920 pdb_set_bad_password_time(sampass,
921 cache_entry->bad_password_time,
925 SAFE_FREE(cache_entry);
929 /**********************************************************************
930 Initialize the ldap db from a struct samu. Called on update.
931 (Based on init_buffer_from_sam in pdb_tdb.c)
932 *********************************************************************/
934 static BOOL init_ldap_from_sam (struct ldapsam_privates *ldap_state,
935 LDAPMessage *existing,
936 LDAPMod *** mods, struct samu * sampass,
937 BOOL (*need_update)(const struct samu *,
943 if (mods == NULL || sampass == NULL) {
944 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
951 * took out adding "objectclass: sambaAccount"
952 * do this on a per-mod basis
954 if (need_update(sampass, PDB_USERNAME)) {
955 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
956 "uid", pdb_get_username(sampass));
957 if (ldap_state->is_nds_ldap) {
958 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
959 "cn", pdb_get_username(sampass));
960 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
961 "sn", pdb_get_username(sampass));
965 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
967 /* only update the RID if we actually need to */
968 if (need_update(sampass, PDB_USERSID)) {
970 fstring dom_sid_string;
971 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
973 switch ( ldap_state->schema_ver ) {
974 case SCHEMAVER_SAMBAACCOUNT:
975 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
976 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
977 sid_to_string(sid_string, user_sid),
978 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
981 slprintf(temp, sizeof(temp) - 1, "%i", rid);
982 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
983 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
987 case SCHEMAVER_SAMBASAMACCOUNT:
988 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
989 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
990 sid_to_string(sid_string, user_sid));
994 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
999 /* we don't need to store the primary group RID - so leaving it
1000 'free' to hang off the unix primary group makes life easier */
1002 if (need_update(sampass, PDB_GROUPSID)) {
1004 fstring dom_sid_string;
1005 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1007 switch ( ldap_state->schema_ver ) {
1008 case SCHEMAVER_SAMBAACCOUNT:
1009 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1010 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1011 sid_to_string(sid_string, group_sid),
1012 sid_to_string(dom_sid_string, &ldap_state->domain_sid)));
1016 slprintf(temp, sizeof(temp) - 1, "%i", rid);
1017 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1018 get_userattr_key2string(ldap_state->schema_ver,
1019 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1022 case SCHEMAVER_SAMBASAMACCOUNT:
1023 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1024 get_userattr_key2string(ldap_state->schema_ver,
1025 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_string(sid_string, group_sid));
1029 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1035 /* displayName, cn, and gecos should all be the same
1036 * most easily accomplished by giving them the same OID
1037 * gecos isn't set here b/c it should be handled by the
1039 * We change displayName only and fall back to cn if
1040 * it does not exist.
1043 if (need_update(sampass, PDB_FULLNAME))
1044 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1045 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1046 pdb_get_fullname(sampass));
1048 if (need_update(sampass, PDB_ACCTDESC))
1049 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1050 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1051 pdb_get_acct_desc(sampass));
1053 if (need_update(sampass, PDB_WORKSTATIONS))
1054 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1055 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1056 pdb_get_workstations(sampass));
1058 if (need_update(sampass, PDB_MUNGEDDIAL))
1059 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1060 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1061 pdb_get_munged_dial(sampass));
1063 if (need_update(sampass, PDB_SMBHOME))
1064 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1065 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1066 pdb_get_homedir(sampass));
1068 if (need_update(sampass, PDB_DRIVE))
1069 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1070 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1071 pdb_get_dir_drive(sampass));
1073 if (need_update(sampass, PDB_LOGONSCRIPT))
1074 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1075 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1076 pdb_get_logon_script(sampass));
1078 if (need_update(sampass, PDB_PROFILE))
1079 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1080 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1081 pdb_get_profile_path(sampass));
1083 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logon_time(sampass));
1084 if (need_update(sampass, PDB_LOGONTIME))
1085 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1086 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1088 slprintf(temp, sizeof(temp) - 1, "%li", pdb_get_logoff_time(sampass));
1089 if (need_update(sampass, PDB_LOGOFFTIME))
1090 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1091 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1093 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_kickoff_time(sampass));
1094 if (need_update(sampass, PDB_KICKOFFTIME))
1095 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1096 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1098 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_can_change_time(sampass));
1099 if (need_update(sampass, PDB_CANCHANGETIME))
1100 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1101 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1103 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_must_change_time(sampass));
1104 if (need_update(sampass, PDB_MUSTCHANGETIME))
1105 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1106 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1109 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1110 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1112 if (need_update(sampass, PDB_LMPASSWD)) {
1113 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1115 pdb_sethexpwd(temp, lm_pw,
1116 pdb_get_acct_ctrl(sampass));
1117 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1118 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1121 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1122 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1126 if (need_update(sampass, PDB_NTPASSWD)) {
1127 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1129 pdb_sethexpwd(temp, nt_pw,
1130 pdb_get_acct_ctrl(sampass));
1131 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1132 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1135 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1136 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1141 if (need_update(sampass, PDB_PWHISTORY)) {
1142 uint32 pwHistLen = 0;
1143 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1144 if (pwHistLen == 0) {
1145 /* Remove any password history from the LDAP store. */
1146 memset(temp, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1150 uint32 currHistLen = 0;
1151 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1152 if (pwhist != NULL) {
1153 /* We can only store (sizeof(pstring)-1)/64 password history entries. */
1154 pwHistLen = MIN(pwHistLen, ((sizeof(temp)-1)/64));
1155 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1156 /* Store the salt. */
1157 pdb_sethexpwd(&temp[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1158 /* Followed by the md5 hash of salt + md4 hash */
1159 pdb_sethexpwd(&temp[(i*64)+32],
1160 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1161 DEBUG(100, ("temp=%s\n", temp));
1165 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1166 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1170 if (need_update(sampass, PDB_PASSLASTSET)) {
1171 slprintf (temp, sizeof (temp) - 1, "%li", pdb_get_pass_last_set_time(sampass));
1172 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1173 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1178 if (need_update(sampass, PDB_HOURS)) {
1179 const uint8 *hours = pdb_get_hours(sampass);
1181 pdb_sethexhours(temp, hours);
1182 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1185 get_userattr_key2string(ldap_state->schema_ver,
1186 LDAP_ATTR_LOGON_HOURS),
1191 if (need_update(sampass, PDB_ACCTCTRL))
1192 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1193 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1194 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1196 /* password lockout cache:
1197 - If we are now autolocking or clearing, we write to ldap
1198 - If we are clearing, we delete the cache entry
1199 - If the count is > 0, we update the cache
1201 This even means when autolocking, we cache, just in case the
1202 update doesn't work, and we have to cache the autolock flag */
1204 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1205 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1206 uint16 badcount = pdb_get_bad_password_count(sampass);
1207 time_t badtime = pdb_get_bad_password_time(sampass);
1209 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1211 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1212 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1214 if ((badcount >= pol) || (badcount == 0)) {
1215 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1216 (unsigned int)badcount, (unsigned int)badtime));
1217 slprintf (temp, sizeof (temp) - 1, "%li", (long)badcount);
1219 ldap_state->smbldap_state->ldap_struct,
1221 get_userattr_key2string(
1222 ldap_state->schema_ver,
1223 LDAP_ATTR_BAD_PASSWORD_COUNT),
1226 slprintf (temp, sizeof (temp) - 1, "%li", badtime);
1228 ldap_state->smbldap_state->ldap_struct,
1230 get_userattr_key2string(
1231 ldap_state->schema_ver,
1232 LDAP_ATTR_BAD_PASSWORD_TIME),
1235 if (badcount == 0) {
1236 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1237 login_cache_delentry(sampass);
1239 LOGIN_CACHE cache_entry;
1241 cache_entry.entry_timestamp = time(NULL);
1242 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1243 cache_entry.bad_password_count = badcount;
1244 cache_entry.bad_password_time = badtime;
1246 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1247 login_cache_write(sampass, cache_entry);
1254 /**********************************************************************
1255 Connect to LDAP server for password enumeration.
1256 *********************************************************************/
1258 static NTSTATUS ldapsam_setsampwent(struct pdb_methods *my_methods, BOOL update, uint32 acb_mask)
1260 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1262 pstring filter, suffix;
1263 const char **attr_list;
1264 BOOL machine_mask = False, user_mask = False;
1266 pstr_sprintf( filter, "(&%s%s)", "(uid=%u)",
1267 get_objclass_filter(ldap_state->schema_ver));
1268 all_string_sub(filter, "%u", "*", sizeof(pstring));
1270 machine_mask = ((acb_mask != 0) && (acb_mask & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)));
1271 user_mask = ((acb_mask != 0) && (acb_mask & ACB_NORMAL));
1274 pstrcpy(suffix, lp_ldap_machine_suffix());
1275 } else if (user_mask) {
1276 pstrcpy(suffix, lp_ldap_user_suffix());
1278 pstrcpy(suffix, lp_ldap_suffix());
1281 DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n",
1284 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1285 rc = smbldap_search(ldap_state->smbldap_state, suffix, LDAP_SCOPE_SUBTREE, filter,
1286 attr_list, 0, &ldap_state->result);
1287 TALLOC_FREE( attr_list );
1289 if (rc != LDAP_SUCCESS) {
1290 DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc)));
1291 DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix, filter));
1292 ldap_msgfree(ldap_state->result);
1293 ldap_state->result = NULL;
1294 return NT_STATUS_UNSUCCESSFUL;
1297 DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
1298 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
1299 ldap_state->result), suffix));
1301 ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
1302 ldap_state->result);
1303 ldap_state->index = 0;
1305 return NT_STATUS_OK;
1308 /**********************************************************************
1309 End enumeration of the LDAP password list.
1310 *********************************************************************/
1312 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1314 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1315 if (ldap_state->result) {
1316 ldap_msgfree(ldap_state->result);
1317 ldap_state->result = NULL;
1321 /**********************************************************************
1322 Get the next entry in the LDAP password database.
1323 *********************************************************************/
1325 static NTSTATUS ldapsam_getsampwent(struct pdb_methods *my_methods,
1328 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1329 struct ldapsam_privates *ldap_state =
1330 (struct ldapsam_privates *)my_methods->private_data;
1334 if (!ldap_state->entry)
1337 ldap_state->index++;
1338 bret = init_sam_from_ldap(ldap_state, user, ldap_state->entry);
1340 ldap_state->entry = ldap_next_entry(priv2ld(ldap_state),
1344 return NT_STATUS_OK;
1347 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1348 const char *new_attr)
1352 if (new_attr == NULL) {
1356 for (i=0; (*attr_list)[i] != NULL; i++) {
1360 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1362 SMB_ASSERT((*attr_list) != NULL);
1363 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1364 (*attr_list)[i+1] = NULL;
1367 /**********************************************************************
1368 Get struct samu entry from LDAP by username.
1369 *********************************************************************/
1371 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1373 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1374 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1375 LDAPMessage *result = NULL;
1376 LDAPMessage *entry = NULL;
1378 const char ** attr_list;
1381 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1382 append_attr(user, &attr_list,
1383 get_userattr_key2string(ldap_state->schema_ver,
1384 LDAP_ATTR_MOD_TIMESTAMP));
1385 append_attr(user, &attr_list, "uidNumber");
1386 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1388 TALLOC_FREE( attr_list );
1390 if ( rc != LDAP_SUCCESS )
1391 return NT_STATUS_NO_SUCH_USER;
1393 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1396 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1397 ldap_msgfree(result);
1398 return NT_STATUS_NO_SUCH_USER;
1399 } else if (count > 1) {
1400 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1401 ldap_msgfree(result);
1402 return NT_STATUS_NO_SUCH_USER;
1405 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1407 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1408 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1409 ldap_msgfree(result);
1410 return NT_STATUS_NO_SUCH_USER;
1412 pdb_set_backend_private_data(user, result, NULL,
1413 my_methods, PDB_CHANGED);
1414 talloc_autofree_ldapmsg(user, result);
1417 ldap_msgfree(result);
1422 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1423 const DOM_SID *sid, LDAPMessage **result)
1426 const char ** attr_list;
1429 switch ( ldap_state->schema_ver ) {
1430 case SCHEMAVER_SAMBASAMACCOUNT: {
1431 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1432 if (tmp_ctx == NULL) {
1433 return LDAP_NO_MEMORY;
1436 attr_list = get_userattr_list(tmp_ctx,
1437 ldap_state->schema_ver);
1438 append_attr(tmp_ctx, &attr_list,
1439 get_userattr_key2string(
1440 ldap_state->schema_ver,
1441 LDAP_ATTR_MOD_TIMESTAMP));
1442 append_attr(tmp_ctx, &attr_list, "uidNumber");
1443 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1445 TALLOC_FREE(tmp_ctx);
1447 if ( rc != LDAP_SUCCESS )
1452 case SCHEMAVER_SAMBAACCOUNT:
1453 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1457 attr_list = get_userattr_list(NULL,
1458 ldap_state->schema_ver);
1459 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1460 TALLOC_FREE( attr_list );
1462 if ( rc != LDAP_SUCCESS )
1469 /**********************************************************************
1470 Get struct samu entry from LDAP by SID.
1471 *********************************************************************/
1473 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1475 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1476 LDAPMessage *result = NULL;
1477 LDAPMessage *entry = NULL;
1482 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1484 if (rc != LDAP_SUCCESS)
1485 return NT_STATUS_NO_SUCH_USER;
1487 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1490 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string, sid),
1492 ldap_msgfree(result);
1493 return NT_STATUS_NO_SUCH_USER;
1494 } else if (count > 1) {
1495 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string, sid),
1497 ldap_msgfree(result);
1498 return NT_STATUS_NO_SUCH_USER;
1501 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1503 ldap_msgfree(result);
1504 return NT_STATUS_NO_SUCH_USER;
1507 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1508 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1509 ldap_msgfree(result);
1510 return NT_STATUS_NO_SUCH_USER;
1513 pdb_set_backend_private_data(user, result, NULL,
1514 my_methods, PDB_CHANGED);
1515 talloc_autofree_ldapmsg(user, result);
1516 return NT_STATUS_OK;
1519 /********************************************************************
1520 Do the actual modification - also change a plaintext passord if
1522 **********************************************************************/
1524 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1525 struct samu *newpwd, char *dn,
1526 LDAPMod **mods, int ldap_op,
1527 BOOL (*need_update)(const struct samu *, enum pdb_elements))
1529 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1532 if (!newpwd || !dn) {
1533 return NT_STATUS_INVALID_PARAMETER;
1537 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1538 /* may be password change below however */
1542 if (ldap_state->is_nds_ldap) {
1543 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1547 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1551 rc = smbldap_add(ldap_state->smbldap_state,
1554 case LDAP_MOD_REPLACE:
1555 rc = smbldap_modify(ldap_state->smbldap_state,
1559 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1561 return NT_STATUS_INVALID_PARAMETER;
1564 if (rc!=LDAP_SUCCESS) {
1565 return NT_STATUS_UNSUCCESSFUL;
1569 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1570 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1571 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1572 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1575 char *retoid = NULL;
1576 struct berval *retdata = NULL;
1577 char *utf8_password;
1580 if (!ldap_state->is_nds_ldap) {
1582 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1583 LDAP_EXOP_MODIFY_PASSWD)) {
1584 DEBUG(2, ("ldap password change requested, but LDAP "
1585 "server does not support it -- ignoring\n"));
1586 return NT_STATUS_OK;
1590 if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
1591 return NT_STATUS_NO_MEMORY;
1594 if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
1595 return NT_STATUS_NO_MEMORY;
1598 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1599 DEBUG(0,("ber_alloc_t returns NULL\n"));
1600 SAFE_FREE(utf8_password);
1601 return NT_STATUS_UNSUCCESSFUL;
1604 ber_printf (ber, "{");
1605 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
1606 ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
1607 ber_printf (ber, "N}");
1609 if ((rc = ber_flatten (ber, &bv))<0) {
1610 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1613 SAFE_FREE(utf8_password);
1614 return NT_STATUS_UNSUCCESSFUL;
1618 SAFE_FREE(utf8_password);
1621 if (!ldap_state->is_nds_ldap) {
1622 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1623 LDAP_EXOP_MODIFY_PASSWD,
1624 bv, NULL, NULL, &retoid,
1627 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1628 pdb_get_plaintext_passwd(newpwd));
1630 if (rc != LDAP_SUCCESS) {
1631 char *ld_error = NULL;
1633 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1634 DEBUG(3, ("Could not set userPassword "
1635 "attribute due to an objectClass "
1636 "violation -- ignoring\n"));
1638 return NT_STATUS_OK;
1641 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1643 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1644 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1645 SAFE_FREE(ld_error);
1647 return NT_STATUS_UNSUCCESSFUL;
1649 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1650 #ifdef DEBUG_PASSWORD
1651 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1654 ber_bvfree(retdata);
1656 ldap_memfree(retoid);
1660 return NT_STATUS_OK;
1663 /**********************************************************************
1664 Delete entry from LDAP for username.
1665 *********************************************************************/
1667 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1668 struct samu * sam_acct)
1670 struct ldapsam_privates *priv =
1671 (struct ldapsam_privates *)my_methods->private_data;
1674 LDAPMessage *msg, *entry;
1675 NTSTATUS result = NT_STATUS_NO_MEMORY;
1676 const char **attr_list;
1677 TALLOC_CTX *mem_ctx;
1680 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1681 return NT_STATUS_INVALID_PARAMETER;
1684 sname = pdb_get_username(sam_acct);
1686 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1689 mem_ctx = talloc_new(NULL);
1690 if (mem_ctx == NULL) {
1691 DEBUG(0, ("talloc_new failed\n"));
1695 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1696 if (attr_list == NULL) {
1700 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1702 if ((rc != LDAP_SUCCESS) ||
1703 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1704 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1705 DEBUG(5, ("Could not find user %s\n", sname));
1706 result = NT_STATUS_NO_SUCH_USER;
1710 rc = ldapsam_delete_entry(
1711 priv, mem_ctx, entry,
1712 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1713 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1716 result = (rc == LDAP_SUCCESS) ?
1717 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1720 TALLOC_FREE(mem_ctx);
1724 /**********************************************************************
1725 Helper function to determine for update_sam_account whether
1726 we need LDAP modification.
1727 *********************************************************************/
1729 static BOOL element_is_changed(const struct samu *sampass,
1730 enum pdb_elements element)
1732 return IS_SAM_CHANGED(sampass, element);
1735 /**********************************************************************
1737 *********************************************************************/
1739 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1741 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1742 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1745 LDAPMessage *result = NULL;
1746 LDAPMessage *entry = NULL;
1747 LDAPMod **mods = NULL;
1748 const char **attr_list;
1750 result = pdb_get_backend_private_data(newpwd, my_methods);
1752 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1753 if (pdb_get_username(newpwd) == NULL) {
1754 return NT_STATUS_INVALID_PARAMETER;
1756 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1757 TALLOC_FREE( attr_list );
1758 if (rc != LDAP_SUCCESS) {
1759 return NT_STATUS_UNSUCCESSFUL;
1761 pdb_set_backend_private_data(newpwd, result, NULL,
1762 my_methods, PDB_CHANGED);
1763 talloc_autofree_ldapmsg(newpwd, result);
1766 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
1767 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
1768 return NT_STATUS_UNSUCCESSFUL;
1771 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1772 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
1774 return NT_STATUS_UNSUCCESSFUL;
1777 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
1779 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
1780 element_is_changed)) {
1781 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
1784 ldap_mods_free(mods,True);
1785 return NT_STATUS_UNSUCCESSFUL;
1789 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
1790 pdb_get_username(newpwd)));
1792 return NT_STATUS_OK;
1795 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
1796 ldap_mods_free(mods,True);
1799 if (!NT_STATUS_IS_OK(ret)) {
1803 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
1804 pdb_get_username(newpwd)));
1805 return NT_STATUS_OK;
1808 /***************************************************************************
1809 Renames a struct samu
1810 - The "rename user script" has full responsibility for changing everything
1811 ***************************************************************************/
1813 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
1814 struct samu *old_acct,
1815 const char *newname)
1817 const char *oldname;
1819 pstring rename_script;
1822 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
1823 return NT_STATUS_INVALID_PARAMETER;
1826 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
1827 return NT_STATUS_INVALID_PARAMETER;
1830 oldname = pdb_get_username(old_acct);
1832 /* rename the posix user */
1833 pstrcpy(rename_script, lp_renameuser_script());
1835 if (!(*rename_script))
1836 return NT_STATUS_ACCESS_DENIED;
1838 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
1841 /* we have to allow the account name to end with a '$' */
1842 string_sub2(rename_script, "%unew", newname, sizeof(pstring),
1844 string_sub2(rename_script, "%uold", oldname, sizeof(pstring),
1846 rc = smbrun(rename_script, NULL);
1848 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
1849 rename_script, rc));
1852 return NT_STATUS_UNSUCCESSFUL;
1854 return NT_STATUS_OK;
1857 /**********************************************************************
1858 Helper function to determine for update_sam_account whether
1859 we need LDAP modification.
1860 *********************************************************************/
1862 static BOOL element_is_set_or_changed(const struct samu *sampass,
1863 enum pdb_elements element)
1865 return (IS_SAM_SET(sampass, element) ||
1866 IS_SAM_CHANGED(sampass, element));
1869 /**********************************************************************
1870 Add struct samu to LDAP.
1871 *********************************************************************/
1873 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1875 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1876 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1878 LDAPMessage *result = NULL;
1879 LDAPMessage *entry = NULL;
1881 LDAPMod **mods = NULL;
1882 int ldap_op = LDAP_MOD_REPLACE;
1884 const char **attr_list;
1886 const char *username = pdb_get_username(newpwd);
1887 const DOM_SID *sid = pdb_get_user_sid(newpwd);
1891 if (!username || !*username) {
1892 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
1893 return NT_STATUS_INVALID_PARAMETER;
1896 /* free this list after the second search or in case we exit on failure */
1897 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1899 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
1901 if (rc != LDAP_SUCCESS) {
1902 TALLOC_FREE( attr_list );
1903 return NT_STATUS_UNSUCCESSFUL;
1906 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1907 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
1909 ldap_msgfree(result);
1910 TALLOC_FREE( attr_list );
1911 return NT_STATUS_UNSUCCESSFUL;
1913 ldap_msgfree(result);
1916 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
1917 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1919 if (rc == LDAP_SUCCESS) {
1920 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
1921 DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n",
1922 sid_to_string(sid_string, sid)));
1923 TALLOC_FREE( attr_list );
1924 ldap_msgfree(result);
1925 return NT_STATUS_UNSUCCESSFUL;
1927 ldap_msgfree(result);
1931 /* does the entry already exist but without a samba attributes?
1932 we need to return the samba attributes here */
1934 escape_user = escape_ldap_string_alloc( username );
1935 pstrcpy( filter, "(uid=%u)" );
1936 all_string_sub( filter, "%u", escape_user, sizeof(filter) );
1937 SAFE_FREE( escape_user );
1939 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1940 filter, attr_list, &result);
1941 if ( rc != LDAP_SUCCESS ) {
1942 TALLOC_FREE( attr_list );
1943 return NT_STATUS_UNSUCCESSFUL;
1946 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1948 if (num_result > 1) {
1949 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
1950 TALLOC_FREE( attr_list );
1951 ldap_msgfree(result);
1952 return NT_STATUS_UNSUCCESSFUL;
1955 /* Check if we need to update an existing entry */
1956 if (num_result == 1) {
1959 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
1960 ldap_op = LDAP_MOD_REPLACE;
1961 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
1962 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
1964 TALLOC_FREE( attr_list );
1965 ldap_msgfree(result);
1966 return NT_STATUS_UNSUCCESSFUL;
1968 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
1971 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
1973 /* There might be a SID for this account already - say an idmap entry */
1975 pstr_sprintf(filter, "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
1976 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1977 sid_to_string(sid_string, sid),
1978 LDAP_OBJ_IDMAP_ENTRY,
1979 LDAP_OBJ_SID_ENTRY);
1981 /* free old result before doing a new search */
1982 if (result != NULL) {
1983 ldap_msgfree(result);
1986 rc = smbldap_search_suffix(ldap_state->smbldap_state,
1987 filter, attr_list, &result);
1989 if ( rc != LDAP_SUCCESS ) {
1990 TALLOC_FREE( attr_list );
1991 return NT_STATUS_UNSUCCESSFUL;
1994 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1996 if (num_result > 1) {
1997 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
1998 TALLOC_FREE( attr_list );
1999 ldap_msgfree(result);
2000 return NT_STATUS_UNSUCCESSFUL;
2003 /* Check if we need to update an existing entry */
2004 if (num_result == 1) {
2007 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2008 ldap_op = LDAP_MOD_REPLACE;
2009 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2010 tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
2012 TALLOC_FREE( attr_list );
2013 ldap_msgfree(result);
2014 return NT_STATUS_UNSUCCESSFUL;
2016 slprintf (dn, sizeof (dn) - 1, "%s", tmp);
2021 TALLOC_FREE( attr_list );
2023 if (num_result == 0) {
2024 /* Check if we need to add an entry */
2025 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2026 ldap_op = LDAP_MOD_ADD;
2027 if (username[strlen(username)-1] == '$') {
2028 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_machine_suffix ());
2030 slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", username, lp_ldap_user_suffix ());
2034 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2035 element_is_set_or_changed)) {
2036 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2037 ldap_msgfree(result);
2039 ldap_mods_free(mods,True);
2040 return NT_STATUS_UNSUCCESSFUL;
2043 ldap_msgfree(result);
2046 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2047 return NT_STATUS_UNSUCCESSFUL;
2049 switch ( ldap_state->schema_ver ) {
2050 case SCHEMAVER_SAMBAACCOUNT:
2051 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2053 case SCHEMAVER_SAMBASAMACCOUNT:
2054 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2057 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2061 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2062 if (!NT_STATUS_IS_OK(ret)) {
2063 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2064 pdb_get_username(newpwd),dn));
2065 ldap_mods_free(mods, True);
2069 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2070 ldap_mods_free(mods, True);
2072 return NT_STATUS_OK;
2075 /**********************************************************************
2076 *********************************************************************/
2078 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2080 LDAPMessage ** result)
2082 int scope = LDAP_SCOPE_SUBTREE;
2084 const char **attr_list;
2086 attr_list = get_attr_list(NULL, groupmap_attr_list);
2087 rc = smbldap_search(ldap_state->smbldap_state,
2088 lp_ldap_group_suffix (), scope,
2089 filter, attr_list, 0, result);
2090 TALLOC_FREE(attr_list);
2095 /**********************************************************************
2096 *********************************************************************/
2098 static BOOL init_group_from_ldap(struct ldapsam_privates *ldap_state,
2099 GROUP_MAP *map, LDAPMessage *entry)
2103 if (ldap_state == NULL || map == NULL || entry == NULL ||
2104 ldap_state->smbldap_state->ldap_struct == NULL) {
2105 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2109 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2110 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER), temp)) {
2111 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2112 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2115 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2117 map->gid = (gid_t)atol(temp);
2119 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2120 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID), temp)) {
2121 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2122 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2126 if (!string_to_sid(&map->sid, temp)) {
2127 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2131 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2132 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE), temp)) {
2133 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2134 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2137 map->sid_name_use = (enum SID_NAME_USE)atol(temp);
2139 if ((map->sid_name_use < SID_NAME_USER) ||
2140 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2141 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2145 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2146 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), temp)) {
2148 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2149 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_CN), temp))
2151 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2152 for gidNumber(%lu)\n",(unsigned long)map->gid));
2156 fstrcpy(map->nt_name, temp);
2158 if (!smbldap_get_single_pstring(ldap_state->smbldap_state->ldap_struct, entry,
2159 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_DESC), temp)) {
2162 fstrcpy(map->comment, temp);
2164 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
2165 store_gid_sid_cache(&map->sid, map->gid);
2171 /**********************************************************************
2172 *********************************************************************/
2174 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2178 struct ldapsam_privates *ldap_state =
2179 (struct ldapsam_privates *)methods->private_data;
2180 LDAPMessage *result = NULL;
2181 LDAPMessage *entry = NULL;
2184 if (ldapsam_search_one_group(ldap_state, filter, &result)
2186 return NT_STATUS_NO_SUCH_GROUP;
2189 count = ldap_count_entries(priv2ld(ldap_state), result);
2192 DEBUG(4, ("ldapsam_getgroup: Did not find group\n"));
2193 ldap_msgfree(result);
2194 return NT_STATUS_NO_SUCH_GROUP;
2198 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2199 "count=%d\n", filter, count));
2200 ldap_msgfree(result);
2201 return NT_STATUS_NO_SUCH_GROUP;
2204 entry = ldap_first_entry(priv2ld(ldap_state), result);
2207 ldap_msgfree(result);
2208 return NT_STATUS_UNSUCCESSFUL;
2211 if (!init_group_from_ldap(ldap_state, map, entry)) {
2212 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2213 "group filter %s\n", filter));
2214 ldap_msgfree(result);
2215 return NT_STATUS_NO_SUCH_GROUP;
2218 ldap_msgfree(result);
2219 return NT_STATUS_OK;
2222 /**********************************************************************
2223 *********************************************************************/
2225 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2230 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
2232 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2233 sid_string_static(&sid));
2235 return ldapsam_getgroup(methods, filter, map);
2238 /**********************************************************************
2239 *********************************************************************/
2241 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2246 pstr_sprintf(filter, "(&(objectClass=%s)(%s=%lu))",
2248 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2249 (unsigned long)gid);
2251 return ldapsam_getgroup(methods, filter, map);
2254 /**********************************************************************
2255 *********************************************************************/
2257 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2261 char *escape_name = escape_ldap_string_alloc(name);
2264 return NT_STATUS_NO_MEMORY;
2267 pstr_sprintf(filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2269 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2270 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN), escape_name);
2272 SAFE_FREE(escape_name);
2274 return ldapsam_getgroup(methods, filter, map);
2277 static BOOL ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2279 const DOM_SID *domain_sid,
2285 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2286 str, sizeof(str)-1)) {
2287 DEBUG(10, ("Could not find sambaSID attribute\n"));
2291 if (!string_to_sid(&sid, str)) {
2292 DEBUG(10, ("Could not convert string %s to sid\n", str));
2296 if (sid_compare_domain(&sid, domain_sid) != 0) {
2297 DEBUG(10, ("SID %s is not in expected domain %s\n",
2298 str, sid_string_static(domain_sid)));
2302 if (!sid_peek_rid(&sid, rid)) {
2303 DEBUG(10, ("Could not peek into RID\n"));
2310 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2311 TALLOC_CTX *mem_ctx,
2312 const DOM_SID *group,
2313 uint32 **pp_member_rids,
2314 size_t *p_num_members)
2316 struct ldapsam_privates *ldap_state =
2317 (struct ldapsam_privates *)methods->private_data;
2318 struct smbldap_state *conn = ldap_state->smbldap_state;
2319 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2320 const char *sid_attrs[] = { "sambaSID", NULL };
2321 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2322 LDAPMessage *result = NULL;
2325 char **values = NULL;
2330 *pp_member_rids = NULL;
2333 filter = talloc_asprintf(mem_ctx,
2334 "(&(objectClass=%s)"
2337 LDAP_OBJ_POSIXGROUP,
2339 sid_string_static(group));
2340 if (filter == NULL) {
2341 ret = NT_STATUS_NO_MEMORY;
2345 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2346 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2349 if (rc != LDAP_SUCCESS)
2352 talloc_autofree_ldapmsg(mem_ctx, result);
2354 count = ldap_count_entries(conn->ldap_struct, result);
2357 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2358 sid_string_static(group)));
2359 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2364 ret = NT_STATUS_NO_SUCH_GROUP;
2368 entry = ldap_first_entry(conn->ldap_struct, result);
2372 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2374 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2375 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2379 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2383 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBAACCOUNT);
2384 if (filter == NULL) {
2385 ret = NT_STATUS_NO_MEMORY;
2389 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2390 filter = talloc_asprintf_append(filter, "(uid=%s)", *memberuid);
2391 if (filter == NULL) {
2392 ret = NT_STATUS_NO_MEMORY;
2397 filter = talloc_asprintf_append(filter, "))");
2398 if (filter == NULL) {
2399 ret = NT_STATUS_NO_MEMORY;
2403 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2404 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2407 if (rc != LDAP_SUCCESS)
2410 count = ldap_count_entries(conn->ldap_struct, result);
2411 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2413 talloc_autofree_ldapmsg(mem_ctx, result);
2415 for (entry = ldap_first_entry(conn->ldap_struct, result);
2417 entry = ldap_next_entry(conn->ldap_struct, entry))
2423 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2427 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2428 "the sambaSID attribute\n"));
2429 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2433 if (!string_to_sid(&sid, sidstr))
2436 if (!sid_check_is_in_our_domain(&sid)) {
2437 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2438 "in our domain\n"));
2439 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2443 sid_peek_rid(&sid, &rid);
2445 add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2450 filter = talloc_asprintf(mem_ctx,
2451 "(&(objectClass=%s)"
2453 LDAP_OBJ_SAMBASAMACCOUNT,
2456 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2457 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2460 if (rc != LDAP_SUCCESS)
2463 talloc_autofree_ldapmsg(mem_ctx, result);
2465 for (entry = ldap_first_entry(conn->ldap_struct, result);
2467 entry = ldap_next_entry(conn->ldap_struct, entry))
2471 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2473 get_global_sam_sid(),
2475 DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
2476 "the sambaSID attribute\n"));
2477 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2481 add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2490 ldap_value_free(values);
2495 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2496 TALLOC_CTX *mem_ctx,
2500 size_t *p_num_groups)
2502 struct ldapsam_privates *ldap_state =
2503 (struct ldapsam_privates *)methods->private_data;
2504 struct smbldap_state *conn = ldap_state->smbldap_state;
2506 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2509 LDAPMessage *result = NULL;
2511 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2512 size_t num_sids, num_gids;
2514 gid_t primary_gid = -1;
2519 if (pdb_get_username(user) == NULL) {
2520 return NT_STATUS_INVALID_PARAMETER;
2523 escape_name = escape_ldap_string_alloc(pdb_get_username(user));
2524 if (escape_name == NULL)
2525 return NT_STATUS_NO_MEMORY;
2527 /* retrieve the users primary gid */
2528 filter = talloc_asprintf(mem_ctx,
2529 "(&(objectClass=%s)(uid=%s))",
2530 LDAP_OBJ_SAMBASAMACCOUNT,
2532 if (filter == NULL) {
2533 ret = NT_STATUS_NO_MEMORY;
2537 rc = smbldap_search(conn, lp_ldap_user_suffix(),
2538 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2540 if (rc != LDAP_SUCCESS)
2543 talloc_autofree_ldapmsg(mem_ctx, result);
2545 count = ldap_count_entries(priv2ld(ldap_state), result);
2549 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2550 ret = NT_STATUS_NO_SUCH_USER;
2553 entry = ldap_first_entry(priv2ld(ldap_state), result);
2555 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2557 DEBUG (1, ("Unable to find the member's gid!\n"));
2558 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2561 primary_gid = strtoul(gidstr, NULL, 10);
2564 DEBUG(1, ("found more than one accoutn with the same user name ?!\n"));
2565 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2569 filter = talloc_asprintf(mem_ctx,
2570 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
2571 LDAP_OBJ_POSIXGROUP, escape_name, primary_gid);
2572 if (filter == NULL) {
2573 ret = NT_STATUS_NO_MEMORY;
2577 rc = smbldap_search(conn, lp_ldap_group_suffix(),
2578 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2580 if (rc != LDAP_SUCCESS)
2583 talloc_autofree_ldapmsg(mem_ctx, result);
2591 /* We need to add the primary group as the first gid/sid */
2593 add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids);
2595 /* This sid will be replaced later */
2597 add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids, &num_sids);
2599 for (entry = ldap_first_entry(conn->ldap_struct, result);
2601 entry = ldap_next_entry(conn->ldap_struct, entry))
2608 if (!smbldap_get_single_attribute(conn->ldap_struct,
2610 str, sizeof(str)-1))
2613 if (!string_to_sid(&sid, str))
2616 if (!smbldap_get_single_attribute(conn->ldap_struct,
2618 str, sizeof(str)-1))
2621 gid = strtoul(str, &end, 10);
2623 if (PTR_DIFF(end, str) != strlen(str))
2626 if (gid == primary_gid) {
2627 sid_copy(&(*pp_sids)[0], &sid);
2629 add_gid_to_array_unique(mem_ctx, gid, pp_gids,
2631 add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
2636 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
2637 DEBUG(3, ("primary group of [%s] not found\n",
2638 pdb_get_username(user)));
2642 *p_num_groups = num_sids;
2648 SAFE_FREE(escape_name);
2652 /**********************************************************************
2653 * Augment a posixGroup object with a sambaGroupMapping domgroup
2654 *********************************************************************/
2656 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
2657 struct ldapsam_privates *ldap_state,
2660 const char *filter, *dn;
2661 LDAPMessage *msg, *entry;
2665 filter = talloc_asprintf(mem_ctx,
2666 "(&(objectClass=posixGroup)(gidNumber=%u))",
2668 if (filter == NULL) {
2669 return NT_STATUS_NO_MEMORY;
2672 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2673 get_attr_list(mem_ctx, groupmap_attr_list),
2675 talloc_autofree_ldapmsg(mem_ctx, msg);
2677 if ((rc != LDAP_SUCCESS) ||
2678 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2679 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2680 return NT_STATUS_NO_SUCH_GROUP;
2683 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2685 return NT_STATUS_NO_MEMORY;
2689 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
2690 "sambaGroupMapping");
2691 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
2692 sid_string_static(&map->sid));
2693 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
2694 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
2695 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
2697 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
2699 talloc_autofree_ldapmod(mem_ctx, mods);
2701 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2702 if (rc != LDAP_SUCCESS) {
2703 return NT_STATUS_ACCESS_DENIED;
2706 return NT_STATUS_OK;
2709 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
2712 struct ldapsam_privates *ldap_state =
2713 (struct ldapsam_privates *)methods->private_data;
2714 LDAPMessage *msg = NULL;
2715 LDAPMod **mods = NULL;
2716 const char *attrs[] = { NULL };
2720 TALLOC_CTX *mem_ctx;
2727 mem_ctx = talloc_new(NULL);
2728 if (mem_ctx == NULL) {
2729 DEBUG(0, ("talloc_new failed\n"));
2730 return NT_STATUS_NO_MEMORY;
2733 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
2734 sid_string_static(&map->sid));
2735 if (filter == NULL) {
2736 result = NT_STATUS_NO_MEMORY;
2740 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
2741 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
2742 talloc_autofree_ldapmsg(mem_ctx, msg);
2744 if ((rc == LDAP_SUCCESS) &&
2745 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
2747 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
2748 "group mapping entry\n",
2749 sid_string_static(&map->sid)));
2750 result = NT_STATUS_GROUP_EXISTS;
2754 switch (map->sid_name_use) {
2756 case SID_NAME_DOM_GRP:
2757 /* To map a domain group we need to have a posix group
2759 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
2763 case SID_NAME_ALIAS:
2764 if (!sid_check_is_in_our_domain(&map->sid)
2765 && !sid_check_is_in_builtin(&map->sid) )
2767 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
2768 sid_string_static(&map->sid)));
2769 result = NT_STATUS_INVALID_PARAMETER;
2775 DEBUG(3, ("Got invalid use '%s' for mapping\n",
2776 sid_type_lookup(map->sid_name_use)));
2777 result = NT_STATUS_INVALID_PARAMETER;
2781 /* Domain groups have been mapped in a separate routine, we have to
2782 * create an alias now */
2784 if (map->gid == -1) {
2785 DEBUG(10, ("Refusing to map gid==-1\n"));
2786 result = NT_STATUS_INVALID_PARAMETER;
2790 if (pdb_gid_to_sid(map->gid, &sid)) {
2791 DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
2792 "add\n", map->gid, sid_string_static(&sid)));
2793 result = NT_STATUS_GROUP_EXISTS;
2797 /* Ok, enough checks done. It's still racy to go ahead now, but that's
2798 * the best we can get out of LDAP. */
2800 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
2801 sid_string_static(&map->sid),
2802 lp_ldap_group_suffix());
2804 result = NT_STATUS_NO_MEMORY;
2810 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
2812 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
2813 "sambaGroupMapping");
2815 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
2816 sid_string_static(&map->sid));
2817 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
2818 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
2819 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
2821 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
2823 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
2824 talloc_asprintf(mem_ctx, "%u", map->gid));
2825 talloc_autofree_ldapmod(mem_ctx, mods);
2827 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
2829 result = (rc == LDAP_SUCCESS) ?
2830 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
2833 TALLOC_FREE(mem_ctx);
2837 /**********************************************************************
2838 * Update a group mapping entry. We're quite strict about what can be changed:
2839 * Only the description and displayname may be changed. It simply does not
2840 * make any sense to change the SID, gid or the type in a mapping.
2841 *********************************************************************/
2843 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
2846 struct ldapsam_privates *ldap_state =
2847 (struct ldapsam_privates *)methods->private_data;
2849 const char *filter, *dn;
2850 LDAPMessage *msg = NULL;
2851 LDAPMessage *entry = NULL;
2852 LDAPMod **mods = NULL;
2853 TALLOC_CTX *mem_ctx;
2856 mem_ctx = talloc_new(NULL);
2857 if (mem_ctx == NULL) {
2858 DEBUG(0, ("talloc_new failed\n"));
2859 return NT_STATUS_NO_MEMORY;
2862 /* Make 100% sure that sid, gid and type are not changed by looking up
2863 * exactly the values we're given in LDAP. */
2865 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
2866 "(sambaSid=%s)(gidNumber=%u)"
2867 "(sambaGroupType=%d))",
2869 sid_string_static(&map->sid), map->gid,
2871 if (filter == NULL) {
2872 result = NT_STATUS_NO_MEMORY;
2876 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
2877 get_attr_list(mem_ctx, groupmap_attr_list),
2879 talloc_autofree_ldapmsg(mem_ctx, msg);
2881 if ((rc != LDAP_SUCCESS) ||
2882 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
2883 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
2884 result = NT_STATUS_NO_SUCH_GROUP;
2888 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
2891 result = NT_STATUS_NO_MEMORY;
2896 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
2898 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
2900 talloc_autofree_ldapmod(mem_ctx, mods);
2903 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
2904 "nothing to do\n"));
2905 result = NT_STATUS_OK;
2909 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
2911 if (rc != LDAP_SUCCESS) {
2912 result = NT_STATUS_ACCESS_DENIED;
2916 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
2917 "group %lu in LDAP\n", (unsigned long)map->gid));
2919 result = NT_STATUS_OK;
2922 TALLOC_FREE(mem_ctx);
2926 /**********************************************************************
2927 *********************************************************************/
2929 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
2932 struct ldapsam_privates *priv =
2933 (struct ldapsam_privates *)methods->private_data;
2934 LDAPMessage *msg, *entry;
2937 TALLOC_CTX *mem_ctx;
2940 mem_ctx = talloc_new(NULL);
2941 if (mem_ctx == NULL) {
2942 DEBUG(0, ("talloc_new failed\n"));
2943 return NT_STATUS_NO_MEMORY;
2946 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
2947 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
2948 sid_string_static(&sid));
2949 if (filter == NULL) {
2950 result = NT_STATUS_NO_MEMORY;
2953 rc = smbldap_search_suffix(priv->smbldap_state, filter,
2954 get_attr_list(mem_ctx, groupmap_attr_list),
2956 talloc_autofree_ldapmsg(mem_ctx, msg);
2958 if ((rc != LDAP_SUCCESS) ||
2959 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
2960 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
2961 result = NT_STATUS_NO_SUCH_GROUP;
2965 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
2966 get_attr_list(mem_ctx,
2967 groupmap_attr_list_to_delete));
2969 if ((rc == LDAP_NAMING_VIOLATION) ||
2970 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
2971 const char *attrs[] = { "sambaGroupType", "description",
2972 "displayName", "sambaSIDList",
2975 /* Second try. Don't delete the sambaSID attribute, this is
2976 for "old" entries that are tacked on a winbind
2979 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
2980 LDAP_OBJ_GROUPMAP, attrs);
2983 if ((rc == LDAP_NAMING_VIOLATION) ||
2984 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
2985 const char *attrs[] = { "sambaGroupType", "description",
2986 "displayName", "sambaSIDList",
2987 "gidNumber", NULL };
2989 /* Third try. This is a post-3.0.21 alias (containing only
2990 * sambaSidEntry and sambaGroupMapping classes), we also have
2991 * to delete the gidNumber attribute, only the sambaSidEntry
2994 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
2995 LDAP_OBJ_GROUPMAP, attrs);
2998 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3001 TALLOC_FREE(mem_ctx);
3005 /**********************************************************************
3006 *********************************************************************/
3008 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3011 struct ldapsam_privates *ldap_state =
3012 (struct ldapsam_privates *)my_methods->private_data;
3015 const char **attr_list;
3017 pstr_sprintf( filter, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3018 attr_list = get_attr_list( NULL, groupmap_attr_list );
3019 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3020 LDAP_SCOPE_SUBTREE, filter,
3021 attr_list, 0, &ldap_state->result);
3022 TALLOC_FREE(attr_list);
3024 if (rc != LDAP_SUCCESS) {
3025 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3026 ldap_err2string(rc)));
3027 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3028 lp_ldap_group_suffix(), filter));
3029 ldap_msgfree(ldap_state->result);
3030 ldap_state->result = NULL;
3031 return NT_STATUS_UNSUCCESSFUL;
3034 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3035 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3036 ldap_state->result)));
3039 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3040 ldap_state->result);
3041 ldap_state->index = 0;
3043 return NT_STATUS_OK;
3046 /**********************************************************************
3047 *********************************************************************/
3049 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3051 ldapsam_endsampwent(my_methods);
3054 /**********************************************************************
3055 *********************************************************************/
3057 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3060 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3061 struct ldapsam_privates *ldap_state =
3062 (struct ldapsam_privates *)my_methods->private_data;
3066 if (!ldap_state->entry)
3069 ldap_state->index++;
3070 bret = init_group_from_ldap(ldap_state, map,
3074 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3078 return NT_STATUS_OK;
3081 /**********************************************************************
3082 *********************************************************************/
3084 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3085 const DOM_SID *domsid, enum SID_NAME_USE sid_name_use,
3086 GROUP_MAP **pp_rmap,
3087 size_t *p_num_entries,
3096 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3097 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3099 return NT_STATUS_ACCESS_DENIED;
3102 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3103 if (sid_name_use != SID_NAME_UNKNOWN &&
3104 sid_name_use != map.sid_name_use) {
3105 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3106 "not of the requested type\n", map.nt_name));
3109 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3110 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3111 "non mapped\n", map.nt_name));
3115 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3117 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3118 "enlarge group map!\n"));
3119 return NT_STATUS_UNSUCCESSFUL;
3122 (*pp_rmap)[entries] = map;
3127 ldapsam_endsamgrent(methods);
3129 *p_num_entries = entries;
3131 return NT_STATUS_OK;
3134 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3135 const DOM_SID *alias,
3136 const DOM_SID *member,
3139 struct ldapsam_privates *ldap_state =
3140 (struct ldapsam_privates *)methods->private_data;
3142 LDAPMessage *result = NULL;
3143 LDAPMessage *entry = NULL;
3145 LDAPMod **mods = NULL;
3147 enum SID_NAME_USE type = SID_NAME_USE_NONE;
3151 if (sid_check_is_in_builtin(alias)) {
3152 type = SID_NAME_ALIAS;
3155 if (sid_check_is_in_our_domain(alias)) {
3156 type = SID_NAME_ALIAS;
3159 if (type == SID_NAME_USE_NONE) {
3160 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3161 sid_string_static(alias)));
3162 return NT_STATUS_NO_SUCH_ALIAS;
3165 pstr_sprintf(filter,
3166 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3167 LDAP_OBJ_GROUPMAP, sid_string_static(alias),
3170 if (ldapsam_search_one_group(ldap_state, filter,
3171 &result) != LDAP_SUCCESS)
3172 return NT_STATUS_NO_SUCH_ALIAS;
3174 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3178 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3179 ldap_msgfree(result);
3180 return NT_STATUS_NO_SUCH_ALIAS;
3184 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3185 "filter %s: count=%d\n", filter, count));
3186 ldap_msgfree(result);
3187 return NT_STATUS_NO_SUCH_ALIAS;
3190 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3194 ldap_msgfree(result);
3195 return NT_STATUS_UNSUCCESSFUL;
3198 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
3200 ldap_msgfree(result);
3201 return NT_STATUS_UNSUCCESSFUL;
3204 smbldap_set_mod(&mods, modop,
3205 get_attr_key2string(groupmap_attr_list,
3206 LDAP_ATTR_SID_LIST),
3207 sid_string_static(member));
3209 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3211 ldap_mods_free(mods, True);
3212 ldap_msgfree(result);
3215 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3216 return NT_STATUS_MEMBER_IN_ALIAS;
3219 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3220 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3223 if (rc != LDAP_SUCCESS) {
3224 return NT_STATUS_UNSUCCESSFUL;
3227 return NT_STATUS_OK;
3230 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3231 const DOM_SID *alias,
3232 const DOM_SID *member)
3234 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3237 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3238 const DOM_SID *alias,
3239 const DOM_SID *member)
3241 return ldapsam_modify_aliasmem(methods, alias, member,
3245 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3246 const DOM_SID *alias,
3247 DOM_SID **pp_members,
3248 size_t *p_num_members)
3250 struct ldapsam_privates *ldap_state =
3251 (struct ldapsam_privates *)methods->private_data;
3252 LDAPMessage *result = NULL;
3253 LDAPMessage *entry = NULL;
3258 size_t num_members = 0;
3259 enum SID_NAME_USE type = SID_NAME_USE_NONE;
3264 if (sid_check_is_in_builtin(alias)) {
3265 type = SID_NAME_ALIAS;
3268 if (sid_check_is_in_our_domain(alias)) {
3269 type = SID_NAME_ALIAS;
3272 if (type == SID_NAME_USE_NONE) {
3273 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3274 sid_string_static(alias)));
3275 return NT_STATUS_NO_SUCH_ALIAS;
3278 pstr_sprintf(filter,
3279 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3280 LDAP_OBJ_GROUPMAP, sid_string_static(alias),
3283 if (ldapsam_search_one_group(ldap_state, filter,
3284 &result) != LDAP_SUCCESS)
3285 return NT_STATUS_NO_SUCH_ALIAS;
3287 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3291 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3292 ldap_msgfree(result);
3293 return NT_STATUS_NO_SUCH_ALIAS;
3297 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3298 "filter %s: count=%d\n", filter, count));
3299 ldap_msgfree(result);
3300 return NT_STATUS_NO_SUCH_ALIAS;
3303 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3307 ldap_msgfree(result);
3308 return NT_STATUS_UNSUCCESSFUL;
3311 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3313 get_attr_key2string(groupmap_attr_list,
3314 LDAP_ATTR_SID_LIST));
3316 if (values == NULL) {
3317 ldap_msgfree(result);
3318 return NT_STATUS_OK;
3321 count = ldap_count_values(values);
3323 for (i=0; i<count; i++) {
3326 if (!string_to_sid(&member, values[i]))
3329 add_sid_to_array(NULL, &member, pp_members, &num_members);
3332 *p_num_members = num_members;
3333 ldap_value_free(values);
3334 ldap_msgfree(result);
3336 return NT_STATUS_OK;
3339 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3340 TALLOC_CTX *mem_ctx,
3341 const DOM_SID *domain_sid,
3342 const DOM_SID *members,
3344 uint32 **pp_alias_rids,
3345 size_t *p_num_alias_rids)
3347 struct ldapsam_privates *ldap_state =
3348 (struct ldapsam_privates *)methods->private_data;
3351 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3353 LDAPMessage *result = NULL;
3354 LDAPMessage *entry = NULL;
3358 enum SID_NAME_USE type = SID_NAME_USE_NONE;
3360 if (sid_check_is_builtin(domain_sid)) {
3361 type = SID_NAME_ALIAS;
3364 if (sid_check_is_domain(domain_sid)) {
3365 type = SID_NAME_ALIAS;
3368 if (type == SID_NAME_USE_NONE) {
3369 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3370 sid_string_static(domain_sid)));
3371 return NT_STATUS_UNSUCCESSFUL;
3374 filter = talloc_asprintf(mem_ctx,
3375 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3376 LDAP_OBJ_GROUPMAP, type);
3378 for (i=0; i<num_members; i++)
3379 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3381 sid_string_static(&members[i]));
3383 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3385 if (filter == NULL) {
3386 return NT_STATUS_NO_MEMORY;
3389 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
3390 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3392 if (rc != LDAP_SUCCESS)
3393 return NT_STATUS_UNSUCCESSFUL;
3395 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3397 for (entry = ldap_first_entry(ldap_struct, result);
3399 entry = ldap_next_entry(ldap_struct, entry))
3405 if (!smbldap_get_single_attribute(ldap_struct, entry,
3411 if (!string_to_sid(&sid, sid_str))
3414 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3417 add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3421 ldap_msgfree(result);
3422 return NT_STATUS_OK;
3425 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3429 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3431 LDAPMod **mods = NULL;
3432 fstring value_string;
3433 const char *policy_attr = NULL;
3435 struct ldapsam_privates *ldap_state =
3436 (struct ldapsam_privates *)methods->private_data;
3438 const char *attrs[2];
3440 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3442 if (!ldap_state->domain_dn) {
3443 return NT_STATUS_INVALID_PARAMETER;
3446 policy_attr = get_account_policy_attr(policy_index);
3447 if (policy_attr == NULL) {
3448 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3453 attrs[0] = policy_attr;
3456 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3458 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3460 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3463 ldap_mods_free(mods, True);
3465 if (rc != LDAP_SUCCESS) {
3469 if (!cache_account_policy_set(policy_index, value)) {
3470 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3471 "update local tdb cache\n"));
3475 return NT_STATUS_OK;
3478 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3479 int policy_index, uint32 value)
3481 if (!account_policy_migrated(False)) {
3482 return (account_policy_set(policy_index, value)) ?
3483 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3486 return ldapsam_set_account_policy_in_ldap(methods, policy_index,
3490 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3494 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3495 LDAPMessage *result = NULL;
3496 LDAPMessage *entry = NULL;
3500 const char *policy_attr = NULL;
3502 struct ldapsam_privates *ldap_state =
3503 (struct ldapsam_privates *)methods->private_data;
3505 const char *attrs[2];
3507 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3509 if (!ldap_state->domain_dn) {
3510 return NT_STATUS_INVALID_PARAMETER;
3513 policy_attr = get_account_policy_attr(policy_index);
3515 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3516 "policy index: %d\n", policy_index));
3520 attrs[0] = policy_attr;
3523 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3524 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
3527 if (rc != LDAP_SUCCESS) {
3531 count = ldap_count_entries(priv2ld(ldap_state), result);
3536 entry = ldap_first_entry(priv2ld(ldap_state), result);
3537 if (entry == NULL) {
3541 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
3546 *value = (uint32)atol(vals[0]);
3548 ntstatus = NT_STATUS_OK;
3552 ldap_value_free(vals);
3553 ldap_msgfree(result);
3558 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
3560 - if user hasn't decided to use account policies inside LDAP just reuse the
3563 - if there is a valid cache entry, return that
3564 - if there is an LDAP entry, update cache and return
3565 - otherwise set to default, update cache and return
3569 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
3570 int policy_index, uint32 *value)
3572 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3574 if (!account_policy_migrated(False)) {
3575 return (account_policy_get(policy_index, value))
3576 ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3579 if (cache_account_policy_get(policy_index, value)) {
3580 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
3582 return NT_STATUS_OK;
3585 ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
3587 if (NT_STATUS_IS_OK(ntstatus)) {
3591 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
3595 /* should we automagically migrate old tdb value here ? */
3596 if (account_policy_get(policy_index, value))
3599 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
3600 "default\n", policy_index));
3603 if (!account_policy_get_default(policy_index, value)) {
3609 ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
3610 if (!NT_STATUS_IS_OK(ntstatus)) {
3616 if (!cache_account_policy_set(policy_index, *value)) {
3617 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
3618 "tdb as a cache\n"));
3619 return NT_STATUS_UNSUCCESSFUL;
3622 return NT_STATUS_OK;
3625 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
3626 const DOM_SID *domain_sid,
3632 struct ldapsam_privates *ldap_state =
3633 (struct ldapsam_privates *)methods->private_data;
3634 LDAPMessage *msg = NULL;
3636 char *allsids = NULL;
3637 int i, rc, num_mapped;
3638 NTSTATUS result = NT_STATUS_NO_MEMORY;
3639 TALLOC_CTX *mem_ctx;
3643 mem_ctx = talloc_new(NULL);
3644 if (mem_ctx == NULL) {
3645 DEBUG(0, ("talloc_new failed\n"));
3649 if (!sid_check_is_builtin(domain_sid) &&
3650 !sid_check_is_domain(domain_sid)) {
3651 result = NT_STATUS_INVALID_PARAMETER;
3655 for (i=0; i<num_rids; i++)
3656 attrs[i] = SID_NAME_UNKNOWN;
3658 allsids = talloc_strdup(mem_ctx, "");
3659 if (allsids == NULL) {
3663 for (i=0; i<num_rids; i++) {
3665 sid_compose(&sid, domain_sid, rids[i]);
3666 allsids = talloc_asprintf_append(allsids, "(sambaSid=%s)",
3667 sid_string_static(&sid));
3668 if (allsids == NULL) {
3673 /* First look for users */
3677 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
3679 filter = talloc_asprintf(
3680 mem_ctx, ("(&(objectClass=%s)(|%s))"),
3681 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
3683 if (filter == NULL) {
3687 rc = smbldap_search(ldap_state->smbldap_state,
3688 lp_ldap_user_suffix(),
3689 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
3691 talloc_autofree_ldapmsg(mem_ctx, msg);
3694 if (rc != LDAP_SUCCESS)
3697 ld = ldap_state->smbldap_state->ldap_struct;
3700 for (entry = ldap_first_entry(ld, msg);
3702 entry = ldap_next_entry(ld, entry)) {
3707 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
3709 DEBUG(2, ("Could not find sid from ldap entry\n"));
3713 name = smbldap_talloc_single_attribute(ld, entry, "uid",
3716 DEBUG(2, ("Could not retrieve uid attribute\n"));
3720 for (rid_index = 0; rid_index < num_rids; rid_index++) {
3721 if (rid == rids[rid_index])
3725 if (rid_index == num_rids) {
3726 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
3730 attrs[rid_index] = SID_NAME_USER;
3731 names[rid_index] = name;
3735 if (num_mapped == num_rids) {
3736 /* No need to look for groups anymore -- we're done */
3737 result = NT_STATUS_OK;
3741 /* Same game for groups */
3745 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
3746 "sambaGroupType", NULL };
3748 filter = talloc_asprintf(
3749 mem_ctx, "(&(objectClass=%s)(|%s))",
3750 LDAP_OBJ_GROUPMAP, allsids);
3751 if (filter == NULL) {
3755 rc = smbldap_search(ldap_state->smbldap_state,
3756 lp_ldap_group_suffix(),
3757 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
3759 talloc_autofree_ldapmsg(mem_ctx, msg);
3762 if (rc != LDAP_SUCCESS)
3765 /* ldap_struct might have changed due to a reconnect */
3767 ld = ldap_state->smbldap_state->ldap_struct;
3769 /* For consistency checks, we already checked we're only domain or builtin */
3771 is_builtin = sid_check_is_builtin(domain_sid);
3773 for (entry = ldap_first_entry(ld, msg);
3775 entry = ldap_next_entry(ld, entry))
3780 enum SID_NAME_USE type;
3781 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
3783 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
3786 DEBUG(2, ("Could not extract type from ldap entry %s\n",
3793 /* Consistency checks */
3794 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
3795 (!is_builtin && ((type != SID_NAME_ALIAS) &&
3796 (type != SID_NAME_DOM_GRP)))) {
3797 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
3800 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
3802 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
3806 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
3809 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
3811 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
3815 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
3820 for (rid_index = 0; rid_index < num_rids; rid_index++) {
3821 if (rid == rids[rid_index])
3825 if (rid_index == num_rids) {
3826 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
3830 attrs[rid_index] = type;
3831 names[rid_index] = attr;
3835 result = NT_STATUS_NONE_MAPPED;
3838 result = (num_mapped == num_rids) ?
3839 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
3841 TALLOC_FREE(mem_ctx);
3845 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
3847 char *filter = NULL;
3848 char *escaped = NULL;
3849 char *result = NULL;
3851 asprintf(&filter, "(&%s(objectclass=sambaSamAccount))",
3853 if (filter == NULL) goto done;
3855 escaped = escape_ldap_string_alloc(username);
3856 if (escaped == NULL) goto done;
3858 result = talloc_string_sub(mem_ctx, filter, "%u", username);
3867 const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
3871 const char **result;
3873 va_start(ap, mem_ctx);
3874 while (va_arg(ap, const char *) != NULL)
3878 if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
3882 va_start(ap, mem_ctx);
3883 for (i=0; i<num; i++) {
3884 result[i] = talloc_strdup(result, va_arg(ap, const char*));
3885 if (result[i] == NULL) {
3886 talloc_free(result);
3896 struct ldap_search_state {
3897 struct smbldap_state *connection;
3907 void *pagedresults_cookie;
3909 LDAPMessage *entries, *current_entry;
3910 BOOL (*ldap2displayentry)(struct ldap_search_state *state,
3911 TALLOC_CTX *mem_ctx,
3912 LDAP *ld, LDAPMessage *entry,
3913 struct samr_displayentry *result);
3916 static BOOL ldapsam_search_firstpage(struct pdb_search *search)
3918 struct ldap_search_state *state = search->private_data;
3920 int rc = LDAP_OPERATIONS_ERROR;
3922 state->entries = NULL;
3924 if (state->connection->paged_results) {
3925 rc = smbldap_search_paged(state->connection, state->base,
3926 state->scope, state->filter,
3927 state->attrs, state->attrsonly,
3928 lp_ldap_page_size(), &state->entries,
3929 &state->pagedresults_cookie);
3932 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
3934 if (state->entries != NULL) {
3935 /* Left over from unsuccessful paged attempt */
3936 ldap_msgfree(state->entries);
3937 state->entries = NULL;
3940 rc = smbldap_search(state->connection, state->base,
3941 state->scope, state->filter, state->attrs,
3942 state->attrsonly, &state->entries);
3944 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
3947 /* Ok, the server was lying. It told us it could do paged
3948 * searches when it could not. */
3949 state->connection->paged_results = False;
3952 ld = state->connection->ldap_struct;
3954 DEBUG(5, ("Don't have an LDAP connection right after a "
3958 state->current_entry = ldap_first_entry(ld, state->entries);
3960 if (state->current_entry == NULL) {
3961 ldap_msgfree(state->entries);
3962 state->entries = NULL;
3968 static BOOL ldapsam_search_nextpage(struct pdb_search *search)
3970 struct ldap_search_state *state = search->private_data;
3973 if (!state->connection->paged_results) {
3974 /* There is no next page when there are no paged results */
3978 rc = smbldap_search_paged(state->connection, state->base,
3979 state->scope, state->filter, state->attrs,
3980 state->attrsonly, lp_ldap_page_size(),
3982 &state->pagedresults_cookie);
3984 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
3987 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
3989 if (state->current_entry == NULL) {
3990 ldap_msgfree(state->entries);
3991 state->entries = NULL;
3997 static BOOL ldapsam_search_next_entry(struct pdb_search *search,
3998 struct samr_displayentry *entry)
4000 struct ldap_search_state *state = search->private_data;
4004 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4007 if ((state->entries == NULL) &&
4008 !ldapsam_search_nextpage(search))
4011 result = state->ldap2displayentry(state, search->mem_ctx, state->connection->ldap_struct,
4012 state->current_entry, entry);
4016 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4017 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4018 if (dn != NULL) ldap_memfree(dn);
4021 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4023 if (state->current_entry == NULL) {
4024 ldap_msgfree(state->entries);
4025 state->entries = NULL;
4028 if (!result) goto retry;
4033 static void ldapsam_search_end(struct pdb_search *search)
4035 struct ldap_search_state *state = search->private_data;
4038 if (state->pagedresults_cookie == NULL)
4041 if (state->entries != NULL)
4042 ldap_msgfree(state->entries);
4044 state->entries = NULL;
4045 state->current_entry = NULL;
4047 if (!state->connection->paged_results)
4050 /* Tell the LDAP server we're not interested in the rest anymore. */
4052 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4053 state->filter, state->attrs,
4054 state->attrsonly, 0, &state->entries,
4055 &state->pagedresults_cookie);
4057 if (rc != LDAP_SUCCESS)
4058 DEBUG(5, ("Could not end search properly\n"));
4063 static BOOL ldapuser2displayentry(struct ldap_search_state *state,
4064 TALLOC_CTX *mem_ctx,
4065 LDAP *ld, LDAPMessage *entry,
4066 struct samr_displayentry *result)
4072 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4073 if ((vals == NULL) || (vals[0] == NULL)) {
4074 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4077 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4078 ldap_value_free(vals);
4080 if ((state->acct_flags != 0) &&
4081 ((state->acct_flags & acct_flags) == 0))
4084 result->acct_flags = acct_flags;
4085 result->account_name = "";
4086 result->fullname = "";
4087 result->description = "";
4089 vals = ldap_get_values(ld, entry, "uid");
4090 if ((vals == NULL) || (vals[0] == NULL)) {
4091 DEBUG(5, ("\"uid\" not found\n"));
4094 pull_utf8_talloc(mem_ctx,
4095 CONST_DISCARD(char **, &result->account_name),
4097 ldap_value_free(vals);
4099 vals = ldap_get_values(ld, entry, "displayName");
4100 if ((vals == NULL) || (vals[0] == NULL))
4101 DEBUG(8, ("\"displayName\" not found\n"));
4103 pull_utf8_talloc(mem_ctx,
4104 CONST_DISCARD(char **, &result->fullname),
4106 ldap_value_free(vals);
4108 vals = ldap_get_values(ld, entry, "description");
4109 if ((vals == NULL) || (vals[0] == NULL))
4110 DEBUG(8, ("\"description\" not found\n"));
4112 pull_utf8_talloc(mem_ctx,
4113 CONST_DISCARD(char **, &result->description),
4115 ldap_value_free(vals);
4117 if ((result->account_name == NULL) ||
4118 (result->fullname == NULL) ||
4119 (result->description == NULL)) {
4120 DEBUG(0, ("talloc failed\n"));
4124 vals = ldap_get_values(ld, entry, "sambaSid");
4125 if ((vals == NULL) || (vals[0] == NULL)) {
4126 DEBUG(0, ("\"objectSid\" not found\n"));
4130 if (!string_to_sid(&sid, vals[0])) {
4131 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4132 ldap_value_free(vals);
4135 ldap_value_free(vals);
4137 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4138 DEBUG(0, ("sid %s does not belong to our domain\n",
4139 sid_string_static(&sid)));
4147 static BOOL ldapsam_search_users(struct pdb_methods *methods,
4148 struct pdb_search *search,
4151 struct ldapsam_privates *ldap_state = methods->private_data;
4152 struct ldap_search_state *state;
4154 state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
4155 if (state == NULL) {
4156 DEBUG(0, ("talloc failed\n"));
4160 state->connection = ldap_state->smbldap_state;
4162 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4163 state->base = lp_ldap_user_suffix();
4164 else if ((acct_flags != 0) &&
4165 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4166 state->base = lp_ldap_machine_suffix();
4168 state->base = lp_ldap_suffix();
4170 state->acct_flags = acct_flags;
4171 state->base = talloc_strdup(search->mem_ctx, state->base);
4172 state->scope = LDAP_SCOPE_SUBTREE;
4173 state->filter = get_ldap_filter(search->mem_ctx, "*");
4174 state->attrs = talloc_attrs(search->mem_ctx, "uid", "sambaSid",
4175 "displayName", "description",
4176 "sambaAcctFlags", NULL);
4177 state->attrsonly = 0;
4178 state->pagedresults_cookie = NULL;
4179 state->entries = NULL;
4180 state->ldap2displayentry = ldapuser2displayentry;
4182 if ((state->filter == NULL) || (state->attrs == NULL)) {
4183 DEBUG(0, ("talloc failed\n"));
4187 search->private_data = state;
4188 search->next_entry = ldapsam_search_next_entry;
4189 search->search_end = ldapsam_search_end;
4191 return ldapsam_search_firstpage(search);
4194 static BOOL ldapgroup2displayentry(struct ldap_search_state *state,
4195 TALLOC_CTX *mem_ctx,
4196 LDAP *ld, LDAPMessage *entry,
4197 struct samr_displayentry *result)
4203 result->account_name = "";
4204 result->fullname = "";
4205 result->description = "";
4208 vals = ldap_get_values(ld, entry, "sambaGroupType");
4209 if ((vals == NULL) || (vals[0] == NULL)) {
4210 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4212 ldap_value_free(vals);
4217 group_type = atoi(vals[0]);
4219 if ((state->group_type != 0) &&
4220 ((state->group_type != group_type))) {
4221 ldap_value_free(vals);
4225 ldap_value_free(vals);
4227 /* display name is the NT group name */
4229 vals = ldap_get_values(ld, entry, "displayName");
4230 if ((vals == NULL) || (vals[0] == NULL)) {
4231 DEBUG(8, ("\"displayName\" not found\n"));
4233 /* fallback to the 'cn' attribute */
4234 vals = ldap_get_values(ld, entry, "cn");
4235 if ((vals == NULL) || (vals[0] == NULL)) {
4236 DEBUG(5, ("\"cn\" not found\n"));
4239 pull_utf8_talloc(mem_ctx,
4240 CONST_DISCARD(char **, &result->account_name),
4244 pull_utf8_talloc(mem_ctx,
4245 CONST_DISCARD(char **, &result->account_name),
4249 ldap_value_free(vals);
4251 vals = ldap_get_values(ld, entry, "description");
4252 if ((vals == NULL) || (vals[0] == NULL))
4253 DEBUG(8, ("\"description\" not found\n"));
4255 pull_utf8_talloc(mem_ctx,
4256 CONST_DISCARD(char **, &result->description),
4258 ldap_value_free(vals);
4260 if ((result->account_name == NULL) ||
4261 (result->fullname == NULL) ||
4262 (result->description == NULL)) {
4263 DEBUG(0, ("talloc failed\n"));
4267 vals = ldap_get_values(ld, entry, "sambaSid");
4268 if ((vals == NULL) || (vals[0] == NULL)) {
4269 DEBUG(0, ("\"objectSid\" not found\n"));
4271 ldap_value_free(vals);
4276 if (!string_to_sid(&sid, vals[0])) {
4277 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4281 ldap_value_free(vals);
4283 switch (group_type) {
4284 case SID_NAME_DOM_GRP:
4285 case SID_NAME_ALIAS:
4287 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4288 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4290 DEBUG(0, ("%s is not in our domain\n",
4291 sid_string_static(&sid)));
4297 DEBUG(0,("unkown group type: %d\n", group_type));
4304 static BOOL ldapsam_search_grouptype(struct pdb_methods *methods,
4305 struct pdb_search *search,
4307 enum SID_NAME_USE type)
4309 struct ldapsam_privates *ldap_state = methods->private_data;
4310 struct ldap_search_state *state;
4312 state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
4313 if (state == NULL) {
4314 DEBUG(0, ("talloc failed\n"));
4318 state->connection = ldap_state->smbldap_state;
4320 state->base = talloc_strdup(search->mem_ctx, lp_ldap_group_suffix());
4321 state->connection = ldap_state->smbldap_state;
4322 state->scope = LDAP_SCOPE_SUBTREE;
4323 state->filter = talloc_asprintf(search->mem_ctx,
4324 "(&(objectclass=sambaGroupMapping)"
4325 "(sambaGroupType=%d)(sambaSID=%s*))",
4326 type, sid_string_static(sid));
4327 state->attrs = talloc_attrs(search->mem_ctx, "cn", "sambaSid",
4328 "displayName", "description",
4329 "sambaGroupType", NULL);
4330 state->attrsonly = 0;
4331 state->pagedresults_cookie = NULL;
4332 state->entries = NULL;
4333 state->group_type = type;
4334 state->ldap2displayentry = ldapgroup2displayentry;
4336 if ((state->filter == NULL) || (state->attrs == NULL)) {
4337 DEBUG(0, ("talloc failed\n"));
4341 search->private_data = state;
4342 search->next_entry = ldapsam_search_next_entry;
4343 search->search_end = ldapsam_search_end;
4345 return ldapsam_search_firstpage(search);
4348 static BOOL ldapsam_search_groups(struct pdb_methods *methods,
4349 struct pdb_search *search)
4351 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4354 static BOOL ldapsam_search_aliases(struct pdb_methods *methods,
4355 struct pdb_search *search,
4358 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4361 static BOOL ldapsam_rid_algorithm(struct pdb_methods *methods)
4366 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4369 struct smbldap_state *smbldap_state = priv->smbldap_state;
4371 LDAPMessage *result = NULL;
4372 LDAPMessage *entry = NULL;
4373 LDAPMod **mods = NULL;
4380 TALLOC_CTX *mem_ctx;
4382 mem_ctx = talloc_new(NULL);
4383 if (mem_ctx == NULL) {
4384 DEBUG(0, ("talloc_new failed\n"));
4385 return NT_STATUS_NO_MEMORY;
4388 status = smbldap_search_domain_info(smbldap_state, &result,
4389 get_global_sam_name(), False);
4390 if (!NT_STATUS_IS_OK(status)) {
4391 DEBUG(3, ("Could not get domain info: %s\n",
4392 nt_errstr(status)));
4396 talloc_autofree_ldapmsg(mem_ctx, result);
4398 entry = ldap_first_entry(priv2ld(priv), result);
4399 if (entry == NULL) {
4400 DEBUG(0, ("Could not get domain info entry\n"));
4401 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4405 /* Find the largest of the three attributes "sambaNextRid",
4406 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4407 concept of differentiating between user and group rids, and will
4408 use only "sambaNextRid" in the future. But for compatibility
4409 reasons I look if others have chosen different strategies -- VL */
4411 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4412 "sambaNextRid", mem_ctx);
4413 if (value != NULL) {
4414 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4415 nextRid = MAX(nextRid, tmp);
4418 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4419 "sambaNextUserRid", mem_ctx);
4420 if (value != NULL) {
4421 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4422 nextRid = MAX(nextRid, tmp);
4425 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4426 "sambaNextGroupRid", mem_ctx);
4427 if (value != NULL) {
4428 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4429 nextRid = MAX(nextRid, tmp);
4433 nextRid = BASE_RID-1;
4438 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4439 talloc_asprintf(mem_ctx, "%d", nextRid));
4440 talloc_autofree_ldapmod(mem_ctx, mods);
4442 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4443 status = NT_STATUS_NO_MEMORY;
4447 rc = smbldap_modify(smbldap_state, dn, mods);
4449 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4452 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4455 if (NT_STATUS_IS_OK(status)) {
4459 TALLOC_FREE(mem_ctx);
4463 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32 *rid)
4467 for (i=0; i<10; i++) {
4468 NTSTATUS result = ldapsam_get_new_rid(methods->private_data,
4470 if (NT_STATUS_IS_OK(result)) {
4474 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4478 /* The ldap update failed (maybe a race condition), retry */
4481 /* Tried 10 times, fail. */
4482 return NT_STATUS_ACCESS_DENIED;
4485 static BOOL ldapsam_new_rid(struct pdb_methods *methods, uint32 *rid)
4487 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4488 return NT_STATUS_IS_OK(result) ? True : False;
4491 static BOOL ldapsam_sid_to_id(struct pdb_methods *methods,
4493 union unid_t *id, enum SID_NAME_USE *type)
4495 struct ldapsam_privates *priv = methods->private_data;
4497 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4499 LDAPMessage *result = NULL;
4500 LDAPMessage *entry = NULL;
4505 TALLOC_CTX *mem_ctx;
4507 mem_ctx = talloc_new(NULL);
4508 if (mem_ctx == NULL) {
4509 DEBUG(0, ("talloc_new failed\n"));
4513 filter = talloc_asprintf(mem_ctx,
4515 "(|(objectClass=%s)(objectClass=%s)))",
4516 sid_string_static(sid),
4517 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
4518 if (filter == NULL) {
4519 DEBUG(5, ("talloc_asprintf failed\n"));
4523 rc = smbldap_search_suffix(priv->smbldap_state, filter,
4525 if (rc != LDAP_SUCCESS) {
4528 talloc_autofree_ldapmsg(mem_ctx, result);
4530 if (ldap_count_entries(priv2ld(priv), result) != 1) {
4531 DEBUG(10, ("Got %d entries, expected one\n",
4532 ldap_count_entries(priv2ld(priv), result)));
4536 entry = ldap_first_entry(priv2ld(priv), result);
4538 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4539 "sambaGroupType", mem_ctx);
4541 if (value != NULL) {
4542 const char *gid_str;
4545 gid_str = smbldap_talloc_single_attribute(
4546 priv2ld(priv), entry, "gidNumber", mem_ctx);
4547 if (gid_str == NULL) {
4548 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
4549 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
4554 id->gid = strtoul(gid_str, NULL, 10);
4555 *type = strtoul(value, NULL, 10);
4560 /* It must be a user */
4562 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4563 "uidNumber", mem_ctx);
4564 if (value == NULL) {
4565 DEBUG(1, ("Could not find uidNumber in %s\n",
4566 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
4570 id->uid = strtoul(value, NULL, 10);
4571 *type = SID_NAME_USER;
4575 TALLOC_FREE(mem_ctx);
4580 * The following functions is called only if
4581 * ldapsam:trusted and ldapsam:editposix are
4586 * ldapsam_create_user creates a new
4587 * posixAccount and sambaSamAccount object
4588 * in the ldap users subtree
4590 * The uid is allocated by winbindd.
4593 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
4594 TALLOC_CTX *tmp_ctx, const char *name,
4595 uint32 acb_info, uint32 *rid)
4597 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
4598 LDAPMessage *entry = NULL;
4599 LDAPMessage *result = NULL;
4601 BOOL is_machine = False;
4602 BOOL add_posix = False;
4603 LDAPMod **mods = NULL;
4611 const char *dn = NULL;
4619 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
4620 acb_info & ACB_WSTRUST ||
4621 acb_info & ACB_SVRTRUST ||
4622 acb_info & ACB_DOMTRUST) {
4626 username = escape_ldap_string_alloc(name);
4627 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
4628 username, LDAP_OBJ_POSIXACCOUNT);
4629 SAFE_FREE(username);
4631 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
4632 if (rc != LDAP_SUCCESS) {
4633 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
4634 return NT_STATUS_UNSUCCESSFUL;
4636 talloc_autofree_ldapmsg(tmp_ctx, result);
4638 num_result = ldap_count_entries(priv2ld(ldap_state), result);
4640 if (num_result > 1) {
4641 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
4642 return NT_STATUS_INTERNAL_DB_CORRUPTION;
4645 if (num_result == 1) {
4647 /* check if it is just a posix account.
4648 * or if there is a sid attached to this entry
4651 entry = ldap_first_entry(priv2ld(ldap_state), result);
4653 return NT_STATUS_UNSUCCESSFUL;
4656 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
4658 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
4659 return NT_STATUS_USER_EXISTS;
4662 /* it is just a posix account, retrieve the dn for later use */
4663 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
4665 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
4666 return NT_STATUS_NO_MEMORY;
4670 if (num_result == 0) {
4674 /* Create the basic samu structure and generate the mods for the ldap commit */
4675 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
4676 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
4680 sid_compose(&user_sid, get_global_sam_sid(), *rid);
4682 user = samu_new(tmp_ctx);
4684 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
4685 return NT_STATUS_NO_MEMORY;
4688 if (!pdb_set_username(user, name, PDB_SET)) {
4689 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4690 return NT_STATUS_UNSUCCESSFUL;
4692 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
4693 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4694 return NT_STATUS_UNSUCCESSFUL;
4697 if (acb_info & ACB_NORMAL) {
4698 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
4699 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4700 return NT_STATUS_UNSUCCESSFUL;
4703 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
4704 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4705 return NT_STATUS_UNSUCCESSFUL;
4709 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
4710 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4711 return NT_STATUS_UNSUCCESSFUL;
4715 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
4716 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4717 return NT_STATUS_UNSUCCESSFUL;
4720 if (!init_ldap_from_sam(ldap_state, NULL, &mods, user, element_is_set_or_changed)) {
4721 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
4722 return NT_STATUS_UNSUCCESSFUL;
4725 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
4726 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
4728 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
4731 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
4733 /* retrieve the Domain Users group gid */
4734 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS) ||
4735 !sid_to_gid(&group_sid, &gid)) {
4736 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
4737 return NT_STATUS_INVALID_PRIMARY_GROUP;
4740 /* lets allocate a new userid for this user */
4741 if (!winbind_allocate_uid(&uid)) {
4742 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
4743 return NT_STATUS_UNSUCCESSFUL;
4748 /* TODO: choose a more appropriate default for machines */
4749 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
4750 shell = talloc_strdup(tmp_ctx, "/bin/false");
4752 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
4753 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
4755 uidstr = talloc_asprintf(tmp_ctx, "%d", uid);
4756 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
4758 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", name, lp_ldap_machine_suffix ());
4760 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", name, lp_ldap_user_suffix ());
4763 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
4764 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
4765 return NT_STATUS_NO_MEMORY;
4768 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
4769 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
4770 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
4771 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
4772 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
4773 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
4774 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
4777 talloc_autofree_ldapmod(tmp_ctx, mods);
4780 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
4782 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
4785 if (rc != LDAP_SUCCESS) {
4786 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
4787 return NT_STATUS_UNSUCCESSFUL;
4790 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
4792 flush_pwnam_cache();
4794 return NT_STATUS_OK;
4797 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
4799 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
4800 LDAPMessage *result = NULL;
4801 LDAPMessage *entry = NULL;
4807 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
4809 filter = talloc_asprintf(tmp_ctx,
4812 "(objectClass=%s))",
4813 pdb_get_username(sam_acct),
4814 LDAP_OBJ_POSIXACCOUNT,
4815 LDAP_OBJ_SAMBASAMACCOUNT);
4816 if (filter == NULL) {
4817 return NT_STATUS_NO_MEMORY;
4820 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
4821 if (rc != LDAP_SUCCESS) {
4822 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
4823 return NT_STATUS_UNSUCCESSFUL;
4825 talloc_autofree_ldapmsg(tmp_ctx, result);
4827 num_result = ldap_count_entries(priv2ld(ldap_state), result);
4829 if (num_result == 0) {
4830 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
4831 return NT_STATUS_NO_SUCH_USER;
4834 if (num_result > 1) {
4835 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
4836 return NT_STATUS_INTERNAL_DB_CORRUPTION;
4839 entry = ldap_first_entry(priv2ld(ldap_state), result);
4841 return NT_STATUS_UNSUCCESSFUL;
4844 /* it is just a posix account, retrieve the dn for later use */
4845 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
4847 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
4848 return NT_STATUS_NO_MEMORY;
4851 rc = smbldap_delete(ldap_state->smbldap_state, dn);
4852 if (rc != LDAP_SUCCESS) {
4853 return NT_STATUS_UNSUCCESSFUL;
4856 flush_pwnam_cache();
4858 return NT_STATUS_OK;
4862 * ldapsam_create_group creates a new
4863 * posixGroup and sambaGroupMapping object
4864 * in the ldap groups subtree
4866 * The gid is allocated by winbindd.
4869 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
4870 TALLOC_CTX *tmp_ctx,
4874 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
4876 LDAPMessage *entry = NULL;
4877 LDAPMessage *result = NULL;
4879 BOOL is_new_entry = False;
4880 LDAPMod **mods = NULL;
4886 const char *dn = NULL;
4891 groupname = escape_ldap_string_alloc(name);
4892 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
4893 groupname, LDAP_OBJ_POSIXGROUP);
4894 SAFE_FREE(groupname);
4896 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
4897 if (rc != LDAP_SUCCESS) {
4898 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
4899 return NT_STATUS_UNSUCCESSFUL;
4901 talloc_autofree_ldapmsg(tmp_ctx, result);
4903 num_result = ldap_count_entries(priv2ld(ldap_state), result);
4905 if (num_result > 1) {
4906 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
4907 return NT_STATUS_INTERNAL_DB_CORRUPTION;
4910 if (num_result == 1) {
4912 /* check if it is just a posix group.
4913 * or if there is a sid attached to this entry
4916 entry = ldap_first_entry(priv2ld(ldap_state), result);
4918 return NT_STATUS_UNSUCCESSFUL;
4921 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
4923 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
4924 return NT_STATUS_GROUP_EXISTS;
4927 /* it is just a posix group, retrieve the gid and the dn for later use */
4928 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
4930 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
4931 return NT_STATUS_INTERNAL_DB_CORRUPTION;
4934 gid = strtoul(tmp, NULL, 10);
4936 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
4938 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
4939 return NT_STATUS_NO_MEMORY;
4943 if (num_result == 0) {
4944 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
4946 is_new_entry = True;
4948 /* lets allocate a new groupid for this group */
4949 if (!winbind_allocate_gid(&gid)) {
4950 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
4951 return NT_STATUS_UNSUCCESSFUL;
4954 gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
4955 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, lp_ldap_group_suffix());
4957 if (!gidstr || !dn) {
4958 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
4959 return NT_STATUS_NO_MEMORY;
4962 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
4963 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
4964 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
4967 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
4968 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
4972 sid_compose(&group_sid, get_global_sam_sid(), *rid);
4974 groupsidstr = talloc_strdup(tmp_ctx, sid_string_static(&group_sid));
4975 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
4977 if (!groupsidstr || !grouptype) {
4978 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
4979 return NT_STATUS_NO_MEMORY;
4982 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
4983 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
4984 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
4985 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
4986 talloc_autofree_ldapmod(tmp_ctx, mods);
4989 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
4991 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
4992 /* This call may fail with rfc2307bis schema */
4993 /* Retry adding a structural class */
4994 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
4995 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
4999 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5002 if (rc != LDAP_SUCCESS) {
5003 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5004 return NT_STATUS_UNSUCCESSFUL;
5007 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5009 return NT_STATUS_OK;
5012 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32 rid)
5014 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5015 LDAPMessage *result = NULL;
5016 LDAPMessage *entry = NULL;
5024 /* get the group sid */
5025 sid_compose(&group_sid, get_global_sam_sid(), rid);
5027 filter = talloc_asprintf(tmp_ctx,
5030 "(objectClass=%s))",
5031 sid_string_static(&group_sid),
5032 LDAP_OBJ_POSIXGROUP,
5034 if (filter == NULL) {
5035 return NT_STATUS_NO_MEMORY;
5038 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5039 if (rc != LDAP_SUCCESS) {
5040 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5041 return NT_STATUS_UNSUCCESSFUL;
5043 talloc_autofree_ldapmsg(tmp_ctx, result);
5045 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5047 if (num_result == 0) {
5048 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5049 return NT_STATUS_NO_SUCH_GROUP;
5052 if (num_result > 1) {
5053 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5054 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5057 entry = ldap_first_entry(priv2ld(ldap_state), result);
5059 return NT_STATUS_UNSUCCESSFUL;
5062 /* here it is, retrieve the dn for later use */
5063 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5065 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5066 return NT_STATUS_NO_MEMORY;
5069 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5071 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5072 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5075 /* check no user have this group marked as primary group */
5076 filter = talloc_asprintf(tmp_ctx,
5079 "(objectClass=%s))",
5081 LDAP_OBJ_POSIXACCOUNT,
5082 LDAP_OBJ_SAMBASAMACCOUNT);
5084 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5085 if (rc != LDAP_SUCCESS) {
5086 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5087 return NT_STATUS_UNSUCCESSFUL;
5089 talloc_autofree_ldapmsg(tmp_ctx, result);
5091 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5093 if (num_result != 0) {
5094 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5095 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5098 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5099 if (rc != LDAP_SUCCESS) {
5100 return NT_STATUS_UNSUCCESSFUL;
5103 return NT_STATUS_OK;
5106 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5107 TALLOC_CTX *tmp_ctx,
5112 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5113 LDAPMessage *entry = NULL;
5114 LDAPMessage *result = NULL;
5116 LDAPMod **mods = NULL;
5119 const char *dn = NULL;
5126 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5128 case LDAP_MOD_DELETE:
5129 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5132 return NT_STATUS_UNSUCCESSFUL;
5135 /* get member sid */
5136 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5138 /* get the group sid */
5139 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5141 filter = talloc_asprintf(tmp_ctx,
5144 "(objectClass=%s))",
5145 sid_string_static(&member_sid),
5146 LDAP_OBJ_POSIXACCOUNT,
5147 LDAP_OBJ_SAMBASAMACCOUNT);
5148 if (filter == NULL) {
5149 return NT_STATUS_NO_MEMORY;
5152 /* get the member uid */
5153 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5154 if (rc != LDAP_SUCCESS) {
5155 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5156 return NT_STATUS_UNSUCCESSFUL;
5158 talloc_autofree_ldapmsg(tmp_ctx, result);
5160 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5162 if (num_result == 0) {
5163 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5164 return NT_STATUS_NO_SUCH_MEMBER;
5167 if (num_result > 1) {
5168 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5169 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5172 entry = ldap_first_entry(priv2ld(ldap_state), result);
5174 return NT_STATUS_UNSUCCESSFUL;
5177 if (modop == LDAP_MOD_DELETE) {
5178 /* check if we are trying to remove the member from his primary group */
5180 gid_t user_gid, group_gid;
5182 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5184 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5185 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5188 user_gid = strtoul(gidstr, NULL, 10);
5190 if (!sid_to_gid(&group_sid, &group_gid)) {
5191 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5192 return NT_STATUS_UNSUCCESSFUL;
5195 if (user_gid == group_gid) {
5196 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from it's own primary group!\n"));
5197 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5201 /* here it is, retrieve the uid for later use */
5202 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5204 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5205 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5208 filter = talloc_asprintf(tmp_ctx,
5211 "(objectClass=%s))",
5212 sid_string_static(&group_sid),
5213 LDAP_OBJ_POSIXGROUP,
5217 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5218 if (rc != LDAP_SUCCESS) {
5219 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5220 return NT_STATUS_UNSUCCESSFUL;
5222 talloc_autofree_ldapmsg(tmp_ctx, result);
5224 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5226 if (num_result == 0) {
5227 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5228 return NT_STATUS_NO_SUCH_GROUP;
5231 if (num_result > 1) {
5232 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5233 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5236 entry = ldap_first_entry(priv2ld(ldap_state), result);
5238 return NT_STATUS_UNSUCCESSFUL;
5241 /* here it is, retrieve the dn for later use */
5242 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5244 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5245 return NT_STATUS_NO_MEMORY;
5248 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5250 talloc_autofree_ldapmod(tmp_ctx, mods);
5252 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5253 if (rc != LDAP_SUCCESS) {
5254 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5255 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5256 return NT_STATUS_MEMBER_IN_GROUP;
5258 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5259 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5260 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5262 return NT_STATUS_UNSUCCESSFUL;
5265 return NT_STATUS_OK;
5268 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5269 TALLOC_CTX *tmp_ctx,
5273 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5275 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5276 TALLOC_CTX *tmp_ctx,
5280 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5283 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5284 TALLOC_CTX *mem_ctx,
5285 struct samu *sampass)
5287 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5288 LDAPMessage *entry = NULL;
5289 LDAPMessage *result = NULL;
5291 LDAPMod **mods = NULL;
5294 const char *dn = NULL;
5298 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
5300 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
5301 DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
5302 return NT_STATUS_UNSUCCESSFUL;
5304 gidstr = talloc_asprintf(mem_ctx, "%d", gid);
5306 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
5307 return NT_STATUS_NO_MEMORY;
5310 filter = talloc_asprintf(mem_ctx,
5313 "(objectClass=%s))",
5314 pdb_get_username(sampass),
5315 LDAP_OBJ_POSIXACCOUNT,
5316 LDAP_OBJ_SAMBASAMACCOUNT);
5317 if (filter == NULL) {
5318 return NT_STATUS_NO_MEMORY;
5321 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5322 if (rc != LDAP_SUCCESS) {
5323 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
5324 return NT_STATUS_UNSUCCESSFUL;
5326 talloc_autofree_ldapmsg(mem_ctx, result);
5328 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5330 if (num_result == 0) {
5331 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
5332 return NT_STATUS_NO_SUCH_USER;
5335 if (num_result > 1) {
5336 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
5337 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5340 entry = ldap_first_entry(priv2ld(ldap_state), result);
5342 return NT_STATUS_UNSUCCESSFUL;
5345 /* retrieve the dn for later use */
5346 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
5348 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
5349 return NT_STATUS_NO_MEMORY;
5352 /* remove the old one, and add the new one, this way we do not risk races */
5353 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
5356 return NT_STATUS_OK;
5359 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5361 if (rc != LDAP_SUCCESS) {
5362 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
5363 pdb_get_username(sampass), gidstr));
5364 return NT_STATUS_UNSUCCESSFUL;
5367 flush_pwnam_cache();
5369 return NT_STATUS_OK;
5372 /**********************************************************************
5374 *********************************************************************/
5376 static void free_private_data(void **vp)
5378 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
5380 smbldap_free_struct(&(*ldap_state)->smbldap_state);
5382 if ((*ldap_state)->result != NULL) {
5383 ldap_msgfree((*ldap_state)->result);
5384 (*ldap_state)->result = NULL;
5386 if ((*ldap_state)->domain_dn != NULL) {
5387 SAFE_FREE((*ldap_state)->domain_dn);
5392 /* No need to free any further, as it is talloc()ed */
5395 /*********************************************************************
5396 Intitalise the parts of the pdb_methods structure that are common to
5398 *********************************************************************/
5400 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
5403 struct ldapsam_privates *ldap_state;
5405 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
5409 (*pdb_method)->name = "ldapsam";
5411 (*pdb_method)->setsampwent = ldapsam_setsampwent;
5412 (*pdb_method)->endsampwent = ldapsam_endsampwent;
5413 (*pdb_method)->getsampwent = ldapsam_getsampwent;
5414 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
5415 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
5416 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
5417 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
5418 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
5419 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
5421 (*pdb_method)->getgrsid = ldapsam_getgrsid;
5422 (*pdb_method)->getgrgid = ldapsam_getgrgid;
5423 (*pdb_method)->getgrnam = ldapsam_getgrnam;
5424 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
5425 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
5426 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
5427 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
5429 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
5430 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
5432 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
5434 (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
5435 (*pdb_method)->new_rid = ldapsam_new_rid;
5437 /* TODO: Setup private data and free */
5439 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
5440 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
5441 return NT_STATUS_NO_MEMORY;
5444 nt_status = smbldap_init(*pdb_method, location, &ldap_state->smbldap_state);
5446 if ( !NT_STATUS_IS_OK(nt_status) ) {
5450 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
5451 return NT_STATUS_NO_MEMORY;
5454 (*pdb_method)->private_data = ldap_state;
5456 (*pdb_method)->free_private_data = free_private_data;
5458 return NT_STATUS_OK;
5461 /**********************************************************************
5462 Initialise the 'compat' mode for pdb_ldap
5463 *********************************************************************/
5465 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
5468 struct ldapsam_privates *ldap_state;
5469 char *uri = talloc_strdup( NULL, location );
5471 if (!NT_STATUS_IS_OK(nt_status = pdb_init_ldapsam_common( pdb_method, uri ))) {
5475 /* the module itself stores a copy of the location so throw this one away */
5480 (*pdb_method)->name = "ldapsam_compat";
5482 ldap_state = (*pdb_method)->private_data;
5483 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
5485 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
5487 return NT_STATUS_OK;
5490 /**********************************************************************
5491 Initialise the normal mode for pdb_ldap
5492 *********************************************************************/
5494 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
5497 struct ldapsam_privates *ldap_state;
5498 uint32 alg_rid_base;
5499 pstring alg_rid_base_string;
5500 LDAPMessage *result = NULL;
5501 LDAPMessage *entry = NULL;
5502 DOM_SID ldap_domain_sid;
5503 DOM_SID secrets_domain_sid;
5504 pstring domain_sid_string;
5507 nt_status = pdb_init_ldapsam_common(pdb_method, location);
5508 if (!NT_STATUS_IS_OK(nt_status)) {
5512 (*pdb_method)->name = "ldapsam";
5514 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
5515 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
5516 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
5517 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
5518 (*pdb_method)->search_users = ldapsam_search_users;
5519 (*pdb_method)->search_groups = ldapsam_search_groups;
5520 (*pdb_method)->search_aliases = ldapsam_search_aliases;
5522 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
5523 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
5524 (*pdb_method)->enum_group_memberships =
5525 ldapsam_enum_group_memberships;
5526 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
5527 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
5529 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
5530 (*pdb_method)->create_user = ldapsam_create_user;
5531 (*pdb_method)->delete_user = ldapsam_delete_user;
5532 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
5533 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
5534 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
5535 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
5536 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
5540 ldap_state = (*pdb_method)->private_data;
5541 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
5543 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
5545 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
5547 ldap_state->domain_name, True);
5549 if ( !NT_STATUS_IS_OK(nt_status) ) {
5550 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
5551 "info, nor add one to the domain\n"));
5552 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
5553 "will be unable to allocate new users/groups, "
5554 "and will risk BDCs having inconsistant SIDs\n"));
5555 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
5556 return NT_STATUS_OK;
5559 /* Given that the above might fail, everything below this must be
5562 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
5565 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
5567 ldap_msgfree(result);
5568 return NT_STATUS_UNSUCCESSFUL;
5571 dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
5573 return NT_STATUS_UNSUCCESSFUL;
5576 ldap_state->domain_dn = smb_xstrdup(dn);
5579 if (smbldap_get_single_pstring(
5580 ldap_state->smbldap_state->ldap_struct,
5582 get_userattr_key2string(ldap_state->schema_ver,
5583 LDAP_ATTR_USER_SID),
5584 domain_sid_string)) {
5586 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
5587 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
5588 "read as a valid SID\n", domain_sid_string));
5589 return NT_STATUS_INVALID_PARAMETER;
5591 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
5592 &secrets_domain_sid);
5593 if (!found_sid || !sid_equal(&secrets_domain_sid,
5594 &ldap_domain_sid)) {
5595 fstring new_sid_str, old_sid_str;
5596 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
5597 "%s based on pdb_ldap results %s -> %s\n",
5598 ldap_state->domain_name,
5599 sid_to_string(old_sid_str,
5600 &secrets_domain_sid),
5601 sid_to_string(new_sid_str,
5602 &ldap_domain_sid)));
5604 /* reset secrets.tdb sid */
5605 secrets_store_domain_sid(ldap_state->domain_name,
5607 DEBUG(1, ("New global sam SID: %s\n",
5608 sid_to_string(new_sid_str,
5609 get_global_sam_sid())));
5611 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
5614 if (smbldap_get_single_pstring(
5615 ldap_state->smbldap_state->ldap_struct,
5617 get_attr_key2string( dominfo_attr_list,
5618 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
5619 alg_rid_base_string)) {
5620 alg_rid_base = (uint32)atol(alg_rid_base_string);
5621 if (alg_rid_base != algorithmic_rid_base()) {
5622 DEBUG(0, ("The value of 'algorithmic RID base' has "
5623 "changed since the LDAP\n"
5624 "database was initialised. Aborting. \n"));
5625 ldap_msgfree(result);
5626 return NT_STATUS_UNSUCCESSFUL;
5629 ldap_msgfree(result);
5631 return NT_STATUS_OK;
5634 NTSTATUS pdb_ldap_init(void)
5637 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
5640 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
5643 /* Let pdb_nds register backends */
5646 return NT_STATUS_OK;