2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
9 Copyright (C) Simo Sorce 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * persistent connections: if using NSS LDAP, many connections are made
28 * however, using only one within Samba would be nice
30 * Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
32 * Other LDAP based login attributes: accountExpires, etc.
33 * (should be the domain of Samba proper, but the sam_password/struct samu
34 * structures don't have fields for some of these attributes)
36 * SSL is done, but can't get the certificate based authentication to work
37 * against on my test platform (Linux 2.4, OpenLDAP 2.x)
40 /* NOTE: this will NOT work against an Active Directory server
41 * due to the fact that the two password fields cannot be retrieved
42 * from a server; recommend using security = domain in this situation
49 #define DBGC_CLASS DBGC_PASSDB
55 * Work around versions of the LDAP client libs that don't have the OIDs
56 * defined, or have them defined under the old name.
57 * This functionality is really a factor of the server, not the client
61 #if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
62 #define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
63 #elif !defined(LDAP_EXOP_MODIFY_PASSWD)
64 #define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
67 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
68 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
69 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
70 #define LDAP_TAG_EXOP_MODIFY_PASSWD_ID ((ber_tag_t) 0x80U)
73 #if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
74 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
75 #elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
76 #define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ((ber_tag_t) 0x82U)
82 /**********************************************************************
83 Simple helper function to make stuff better readable
84 **********************************************************************/
86 static LDAP *priv2ld(struct ldapsam_privates *priv)
88 return priv->smbldap_state->ldap_struct;
91 /**********************************************************************
92 Get the attribute name given a user schame version.
93 **********************************************************************/
95 static const char* get_userattr_key2string( int schema_ver, int key )
97 switch ( schema_ver ) {
98 case SCHEMAVER_SAMBAACCOUNT:
99 return get_attr_key2string( attrib_map_v22, key );
101 case SCHEMAVER_SAMBASAMACCOUNT:
102 return get_attr_key2string( attrib_map_v30, key );
105 DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
111 /**********************************************************************
112 Return the list of attribute names given a user schema version.
113 **********************************************************************/
115 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
117 switch ( schema_ver ) {
118 case SCHEMAVER_SAMBAACCOUNT:
119 return get_attr_list( mem_ctx, attrib_map_v22 );
121 case SCHEMAVER_SAMBASAMACCOUNT:
122 return get_attr_list( mem_ctx, attrib_map_v30 );
124 DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
131 /**************************************************************************
132 Return the list of attribute names to delete given a user schema version.
133 **************************************************************************/
135 static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
138 switch ( schema_ver ) {
139 case SCHEMAVER_SAMBAACCOUNT:
140 return get_attr_list( mem_ctx,
141 attrib_map_to_delete_v22 );
143 case SCHEMAVER_SAMBASAMACCOUNT:
144 return get_attr_list( mem_ctx,
145 attrib_map_to_delete_v30 );
147 DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
155 /*******************************************************************
156 Generate the LDAP search filter for the objectclass based on the
157 version of the schema we are using.
158 ******************************************************************/
160 static const char* get_objclass_filter( int schema_ver )
162 fstring objclass_filter;
165 switch( schema_ver ) {
166 case SCHEMAVER_SAMBAACCOUNT:
167 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
169 case SCHEMAVER_SAMBASAMACCOUNT:
170 fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
173 DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
174 objclass_filter[0] = '\0';
178 result = talloc_strdup(talloc_tos(), objclass_filter);
179 SMB_ASSERT(result != NULL);
183 /*****************************************************************
184 Scan a sequence number off OpenLDAP's syncrepl contextCSN
185 ******************************************************************/
187 static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
189 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
190 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
191 LDAPMessage *msg = NULL;
192 LDAPMessage *entry = NULL;
194 char **values = NULL;
195 int rc, num_result, num_values, rid;
201 /* Unfortunatly there is no proper way to detect syncrepl-support in
202 * smbldap_connect_system(). The syncrepl OIDs are submitted for publication
203 * but do not show up in the root-DSE yet. Neither we can query the
204 * subschema-context for the syncProviderSubentry or syncConsumerSubentry
205 * objectclass. Currently we require lp_ldap_suffix() to show up as
206 * namingContext. - Guenther
209 if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
214 DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
218 if (!smbldap_has_naming_context(ldap_state->smbldap_state->ldap_struct, lp_ldap_suffix())) {
219 DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
220 "as top-level namingContext\n", lp_ldap_suffix()));
224 mem_ctx = talloc_init("ldapsam_get_seq_num");
227 return NT_STATUS_NO_MEMORY;
229 if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
230 ntstatus = NT_STATUS_NO_MEMORY;
234 /* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
235 rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
238 /* consumer syncreplCookie: */
239 /* csn=20050126161620Z#0000001#00#00000 */
240 attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
242 suffix = talloc_asprintf(mem_ctx,
243 "cn=syncrepl%d,%s", rid, lp_ldap_suffix());
245 ntstatus = NT_STATUS_NO_MEMORY;
250 /* provider contextCSN */
251 /* 20050126161620Z#000009#00#000000 */
252 attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
254 suffix = talloc_asprintf(mem_ctx,
255 "cn=ldapsync,%s", lp_ldap_suffix());
258 ntstatus = NT_STATUS_NO_MEMORY;
263 rc = smbldap_search(ldap_state->smbldap_state, suffix,
264 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
266 if (rc != LDAP_SUCCESS) {
270 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg);
271 if (num_result != 1) {
272 DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
276 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg);
278 DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
282 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct, entry, attrs[0]);
283 if (values == NULL) {
284 DEBUG(3,("ldapsam_get_seq_num: no values\n"));
288 num_values = ldap_count_values(values);
289 if (num_values == 0) {
290 DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
295 if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
296 DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
301 if (!strncmp(p, "csn=", strlen("csn=")))
304 DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
306 *seq_num = generalized_to_unix_time(p);
308 /* very basic sanity check */
310 DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
315 ntstatus = NT_STATUS_OK;
319 ldap_value_free(values);
323 talloc_destroy(mem_ctx);
328 /*******************************************************************
329 Run the search by name.
330 ******************************************************************/
332 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
334 LDAPMessage ** result,
338 char *escape_user = escape_ldap_string_alloc(user);
342 return LDAP_NO_MEMORY;
346 * in the filter expression, replace %u with the real name
347 * so in ldap filter, %u MUST exist :-)
349 filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
350 get_objclass_filter(ldap_state->schema_ver));
352 SAFE_FREE(escape_user);
353 return LDAP_NO_MEMORY;
356 * have to use this here because $ is filtered out
360 filter = talloc_all_string_sub(talloc_tos(),
361 filter, "%u", escape_user);
362 SAFE_FREE(escape_user);
364 return LDAP_NO_MEMORY;
367 ret = smbldap_search_suffix(ldap_state->smbldap_state,
368 filter, attr, result);
373 /*******************************************************************
374 Run the search by rid.
375 ******************************************************************/
377 static int ldapsam_search_suffix_by_rid (struct ldapsam_privates *ldap_state,
378 uint32 rid, LDAPMessage ** result,
384 filter = talloc_asprintf(talloc_tos(), "(&(rid=%i)%s)", rid,
385 get_objclass_filter(ldap_state->schema_ver));
387 return LDAP_NO_MEMORY;
390 rc = smbldap_search_suffix(ldap_state->smbldap_state,
391 filter, attr, result);
396 /*******************************************************************
397 Run the search by SID.
398 ******************************************************************/
400 static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
401 const DOM_SID *sid, LDAPMessage ** result,
408 filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
409 get_userattr_key2string(ldap_state->schema_ver,
411 sid_to_fstring(sid_string, sid),
412 get_objclass_filter(ldap_state->schema_ver));
414 return LDAP_NO_MEMORY;
417 rc = smbldap_search_suffix(ldap_state->smbldap_state,
418 filter, attr, result);
424 /*******************************************************************
425 Delete complete object or objectclass and attrs from
426 object found in search_result depending on lp_ldap_delete_dn
427 ******************************************************************/
429 static int ldapsam_delete_entry(struct ldapsam_privates *priv,
432 const char *objectclass,
435 LDAPMod **mods = NULL;
438 BerElement *ptr = NULL;
440 dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
442 return LDAP_NO_MEMORY;
445 if (lp_ldap_delete_dn()) {
446 return smbldap_delete(priv->smbldap_state, dn);
449 /* Ok, delete only the SAM attributes */
451 for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
453 name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
456 /* We are only allowed to delete the attributes that
459 for (attrib = attrs; *attrib != NULL; attrib++) {
460 if (strequal(*attrib, name)) {
461 DEBUG(10, ("ldapsam_delete_entry: deleting "
462 "attribute %s\n", name));
463 smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
474 smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
475 talloc_autofree_ldapmod(mem_ctx, mods);
477 return smbldap_modify(priv->smbldap_state, dn, mods);
480 static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
485 temp = smbldap_talloc_single_attribute(ldap_state->smbldap_state->ldap_struct, entry,
486 get_userattr_key2string(ldap_state->schema_ver,LDAP_ATTR_MOD_TIMESTAMP),
492 if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
493 DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
503 /**********************************************************************
504 Initialize struct samu from an LDAP query.
505 (Based on init_sam_from_buffer in pdb_tdb.c)
506 *********************************************************************/
508 static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
509 struct samu * sampass,
516 pass_can_change_time,
517 pass_must_change_time,
520 char *username = NULL,
526 *logon_script = NULL,
527 *profile_path = NULL,
529 *workstations = NULL,
532 uint8 smblmpwd[LM_HASH_LEN],
533 smbntpwd[NT_HASH_LEN];
534 bool use_samba_attrs = True;
535 uint32 acct_ctrl = 0;
537 uint16 bad_password_count = 0,
540 uint8 hours[MAX_HOURS_LEN];
542 LOGIN_CACHE *cache_entry = NULL;
544 bool expand_explicit = lp_passdb_expand_explicit();
546 TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
551 if (sampass == NULL || ldap_state == NULL || entry == NULL) {
552 DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
556 if (priv2ld(ldap_state) == NULL) {
557 DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
558 "ldap_struct is NULL!\n"));
562 if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
566 DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
571 DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
573 nt_username = talloc_strdup(ctx, username);
578 domain = talloc_strdup(ctx, ldap_state->domain_name);
583 pdb_set_username(sampass, username, PDB_SET);
585 pdb_set_domain(sampass, domain, PDB_DEFAULT);
586 pdb_set_nt_username(sampass, nt_username, PDB_SET);
588 /* deal with different attributes between the schema first */
590 if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
591 if ((temp = smbldap_talloc_single_attribute(
592 ldap_state->smbldap_state->ldap_struct,
594 get_userattr_key2string(ldap_state->schema_ver,
597 pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
600 if ((temp = smbldap_talloc_single_attribute(
601 ldap_state->smbldap_state->ldap_struct,
603 get_userattr_key2string(ldap_state->schema_ver,
606 user_rid = (uint32)atol(temp);
607 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
611 if (pdb_get_init_flags(sampass,PDB_USERSID) == PDB_DEFAULT) {
612 DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
613 get_userattr_key2string(ldap_state->schema_ver,
615 get_userattr_key2string(ldap_state->schema_ver,
621 temp = smbldap_talloc_single_attribute(
622 ldap_state->smbldap_state->ldap_struct,
624 get_userattr_key2string(ldap_state->schema_ver,
625 LDAP_ATTR_PWD_LAST_SET),
628 pass_last_set_time = (time_t) atol(temp);
629 pdb_set_pass_last_set_time(sampass,
630 pass_last_set_time, PDB_SET);
633 temp = smbldap_talloc_single_attribute(
634 ldap_state->smbldap_state->ldap_struct,
636 get_userattr_key2string(ldap_state->schema_ver,
637 LDAP_ATTR_LOGON_TIME),
640 logon_time = (time_t) atol(temp);
641 pdb_set_logon_time(sampass, logon_time, PDB_SET);
644 temp = smbldap_talloc_single_attribute(
645 ldap_state->smbldap_state->ldap_struct,
647 get_userattr_key2string(ldap_state->schema_ver,
648 LDAP_ATTR_LOGOFF_TIME),
651 logoff_time = (time_t) atol(temp);
652 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
655 temp = smbldap_talloc_single_attribute(
656 ldap_state->smbldap_state->ldap_struct,
658 get_userattr_key2string(ldap_state->schema_ver,
659 LDAP_ATTR_KICKOFF_TIME),
662 kickoff_time = (time_t) atol(temp);
663 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
666 temp = smbldap_talloc_single_attribute(
667 ldap_state->smbldap_state->ldap_struct,
669 get_userattr_key2string(ldap_state->schema_ver,
670 LDAP_ATTR_PWD_CAN_CHANGE),
673 pass_can_change_time = (time_t) atol(temp);
674 pdb_set_pass_can_change_time(sampass,
675 pass_can_change_time, PDB_SET);
678 temp = smbldap_talloc_single_attribute(
679 ldap_state->smbldap_state->ldap_struct,
681 get_userattr_key2string(ldap_state->schema_ver,
682 LDAP_ATTR_PWD_MUST_CHANGE),
685 pass_must_change_time = (time_t) atol(temp);
686 pdb_set_pass_must_change_time(sampass,
687 pass_must_change_time, PDB_SET);
690 /* recommend that 'gecos' and 'displayName' should refer to the same
691 * attribute OID. userFullName depreciated, only used by Samba
692 * primary rules of LDAP: don't make a new attribute when one is already defined
693 * that fits your needs; using cn then displayName rather than 'userFullName'
696 fullname = smbldap_talloc_single_attribute(
697 ldap_state->smbldap_state->ldap_struct,
699 get_userattr_key2string(ldap_state->schema_ver,
700 LDAP_ATTR_DISPLAY_NAME),
703 pdb_set_fullname(sampass, fullname, PDB_SET);
705 fullname = smbldap_talloc_single_attribute(
706 ldap_state->smbldap_state->ldap_struct,
708 get_userattr_key2string(ldap_state->schema_ver,
712 pdb_set_fullname(sampass, fullname, PDB_SET);
716 dir_drive = smbldap_talloc_single_attribute(
717 ldap_state->smbldap_state->ldap_struct,
719 get_userattr_key2string(ldap_state->schema_ver,
720 LDAP_ATTR_HOME_DRIVE),
723 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
725 pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
728 homedir = smbldap_talloc_single_attribute(
729 ldap_state->smbldap_state->ldap_struct,
731 get_userattr_key2string(ldap_state->schema_ver,
732 LDAP_ATTR_HOME_PATH),
735 if (expand_explicit) {
736 homedir = talloc_sub_basic(ctx,
744 pdb_set_homedir(sampass, homedir, PDB_SET);
746 pdb_set_homedir(sampass,
747 talloc_sub_basic(ctx, username, domain,
752 logon_script = smbldap_talloc_single_attribute(
753 ldap_state->smbldap_state->ldap_struct,
755 get_userattr_key2string(ldap_state->schema_ver,
756 LDAP_ATTR_LOGON_SCRIPT),
759 if (expand_explicit) {
760 logon_script = talloc_sub_basic(ctx,
768 pdb_set_logon_script(sampass, logon_script, PDB_SET);
770 pdb_set_logon_script(sampass,
771 talloc_sub_basic(ctx, username, domain,
776 profile_path = smbldap_talloc_single_attribute(
777 ldap_state->smbldap_state->ldap_struct,
779 get_userattr_key2string(ldap_state->schema_ver,
780 LDAP_ATTR_PROFILE_PATH),
783 if (expand_explicit) {
784 profile_path = talloc_sub_basic(ctx,
792 pdb_set_profile_path(sampass, profile_path, PDB_SET);
794 pdb_set_profile_path(sampass,
795 talloc_sub_basic(ctx, username, domain,
800 acct_desc = smbldap_talloc_single_attribute(
801 ldap_state->smbldap_state->ldap_struct,
803 get_userattr_key2string(ldap_state->schema_ver,
807 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
810 workstations = smbldap_talloc_single_attribute(
811 ldap_state->smbldap_state->ldap_struct,
813 get_userattr_key2string(ldap_state->schema_ver,
817 pdb_set_workstations(sampass, workstations, PDB_SET);
820 munged_dial = smbldap_talloc_single_attribute(
821 ldap_state->smbldap_state->ldap_struct,
823 get_userattr_key2string(ldap_state->schema_ver,
824 LDAP_ATTR_MUNGED_DIAL),
827 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
830 /* FIXME: hours stuff should be cleaner */
834 memset(hours, 0xff, hours_len);
836 if (ldap_state->is_nds_ldap) {
839 char clear_text_pw[512];
841 /* Make call to Novell eDirectory ldap extension to get clear text password.
842 NOTE: This will only work if we have an SSL connection to eDirectory. */
843 user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
844 if (user_dn != NULL) {
845 DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
847 pwd_len = sizeof(clear_text_pw);
848 if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
849 nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
850 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
851 TALLOC_FREE(user_dn);
854 ZERO_STRUCT(smblmpwd);
855 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
856 TALLOC_FREE(user_dn);
859 ZERO_STRUCT(smbntpwd);
860 use_samba_attrs = False;
863 TALLOC_FREE(user_dn);
866 DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
870 if (use_samba_attrs) {
871 temp = smbldap_talloc_single_attribute(
872 ldap_state->smbldap_state->ldap_struct,
874 get_userattr_key2string(ldap_state->schema_ver,
878 pdb_gethexpwd(temp, smblmpwd);
879 memset((char *)temp, '\0', strlen(temp)+1);
880 if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
883 ZERO_STRUCT(smblmpwd);
886 temp = smbldap_talloc_single_attribute(
887 ldap_state->smbldap_state->ldap_struct,
889 get_userattr_key2string(ldap_state->schema_ver,
893 pdb_gethexpwd(temp, smbntpwd);
894 memset((char *)temp, '\0', strlen(temp)+1);
895 if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
898 ZERO_STRUCT(smbntpwd);
904 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
906 uint8 *pwhist = NULL;
908 char *history_string = TALLOC_ARRAY(ctx, char,
909 MAX_PW_HISTORY_LEN*64);
911 if (!history_string) {
915 pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
917 if ((pwhist = TALLOC_ARRAY(ctx, uint8,
918 pwHistLen * PW_HISTORY_ENTRY_LEN)) ==
920 DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
923 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
925 if (smbldap_get_single_attribute(
926 ldap_state->smbldap_state->ldap_struct,
928 get_userattr_key2string(ldap_state->schema_ver,
929 LDAP_ATTR_PWD_HISTORY),
931 MAX_PW_HISTORY_LEN*64)) {
932 bool hex_failed = false;
933 for (i = 0; i < pwHistLen; i++){
934 /* Get the 16 byte salt. */
935 if (!pdb_gethexpwd(&history_string[i*64],
936 &pwhist[i*PW_HISTORY_ENTRY_LEN])) {
940 /* Get the 16 byte MD5 hash of salt+passwd. */
941 if (!pdb_gethexpwd(&history_string[(i*64)+32],
942 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+
943 PW_HISTORY_SALT_LEN])) {
949 DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
951 memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
954 if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
959 temp = smbldap_talloc_single_attribute(
960 ldap_state->smbldap_state->ldap_struct,
962 get_userattr_key2string(ldap_state->schema_ver,
966 acct_ctrl = pdb_decode_acct_ctrl(temp);
968 if (acct_ctrl == 0) {
969 acct_ctrl |= ACB_NORMAL;
972 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
974 acct_ctrl |= ACB_NORMAL;
977 pdb_set_hours_len(sampass, hours_len, PDB_SET);
978 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
980 temp = smbldap_talloc_single_attribute(
981 ldap_state->smbldap_state->ldap_struct,
983 get_userattr_key2string(ldap_state->schema_ver,
984 LDAP_ATTR_BAD_PASSWORD_COUNT),
987 bad_password_count = (uint32) atol(temp);
988 pdb_set_bad_password_count(sampass,
989 bad_password_count, PDB_SET);
992 temp = smbldap_talloc_single_attribute(
993 ldap_state->smbldap_state->ldap_struct,
995 get_userattr_key2string(ldap_state->schema_ver,
996 LDAP_ATTR_BAD_PASSWORD_TIME),
999 bad_password_time = (time_t) atol(temp);
1000 pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
1004 temp = smbldap_talloc_single_attribute(
1005 ldap_state->smbldap_state->ldap_struct,
1007 get_userattr_key2string(ldap_state->schema_ver,
1008 LDAP_ATTR_LOGON_COUNT),
1011 logon_count = (uint32) atol(temp);
1012 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1015 /* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
1017 temp = smbldap_talloc_single_attribute(
1018 ldap_state->smbldap_state->ldap_struct,
1020 get_userattr_key2string(ldap_state->schema_ver,
1021 LDAP_ATTR_LOGON_HOURS),
1024 pdb_gethexhours(temp, hours);
1025 memset((char *)temp, '\0', strlen(temp) +1);
1026 pdb_set_hours(sampass, hours, PDB_SET);
1030 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
1031 struct passwd unix_pw;
1032 bool have_uid = false;
1033 bool have_gid = false;
1034 DOM_SID mapped_gsid;
1035 const DOM_SID *primary_gsid;
1037 ZERO_STRUCT(unix_pw);
1039 unix_pw.pw_name = username;
1040 unix_pw.pw_passwd = discard_const_p(char, "x");
1042 temp = smbldap_talloc_single_attribute(
1043 priv2ld(ldap_state),
1048 /* We've got a uid, feed the cache */
1049 unix_pw.pw_uid = strtoul(temp, NULL, 10);
1052 temp = smbldap_talloc_single_attribute(
1053 priv2ld(ldap_state),
1058 /* We've got a uid, feed the cache */
1059 unix_pw.pw_gid = strtoul(temp, NULL, 10);
1062 unix_pw.pw_gecos = smbldap_talloc_single_attribute(
1063 priv2ld(ldap_state),
1067 if (unix_pw.pw_gecos) {
1068 unix_pw.pw_gecos = fullname;
1070 unix_pw.pw_dir = smbldap_talloc_single_attribute(
1071 priv2ld(ldap_state),
1075 if (unix_pw.pw_dir) {
1076 unix_pw.pw_dir = discard_const_p(char, "");
1078 unix_pw.pw_shell = smbldap_talloc_single_attribute(
1079 priv2ld(ldap_state),
1083 if (unix_pw.pw_shell) {
1084 unix_pw.pw_shell = discard_const_p(char, "");
1087 if (have_uid && have_gid) {
1088 sampass->unix_pw = tcopy_passwd(sampass, &unix_pw);
1090 sampass->unix_pw = Get_Pwnam_alloc(sampass, unix_pw.pw_name);
1093 if (sampass->unix_pw == NULL) {
1094 DEBUG(0,("init_sam_from_ldap: Failed to find Unix account for %s\n",
1095 pdb_get_username(sampass)));
1099 store_uid_sid_cache(pdb_get_user_sid(sampass),
1100 sampass->unix_pw->pw_uid);
1101 idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
1102 sampass->unix_pw->pw_uid);
1104 gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
1105 primary_gsid = pdb_get_group_sid(sampass);
1106 if (primary_gsid && sid_equal(primary_gsid, &mapped_gsid)) {
1107 store_gid_sid_cache(primary_gsid,
1108 sampass->unix_pw->pw_gid);
1109 idmap_cache_set_sid2gid(primary_gsid,
1110 sampass->unix_pw->pw_gid);
1114 /* check the timestamp of the cache vs ldap entry */
1115 if (!(ldap_entry_time = ldapsam_get_entry_timestamp(ldap_state,
1121 /* see if we have newer updates */
1122 if (!(cache_entry = login_cache_read(sampass))) {
1123 DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
1124 (unsigned int)pdb_get_bad_password_count(sampass),
1125 (unsigned int)pdb_get_bad_password_time(sampass)));
1130 DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
1131 (unsigned int)ldap_entry_time,
1132 (unsigned int)cache_entry->entry_timestamp,
1133 (unsigned int)cache_entry->bad_password_time));
1135 if (ldap_entry_time > cache_entry->entry_timestamp) {
1136 /* cache is older than directory , so
1137 we need to delete the entry but allow the
1138 fields to be written out */
1139 login_cache_delentry(sampass);
1142 pdb_set_acct_ctrl(sampass,
1143 pdb_get_acct_ctrl(sampass) |
1144 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
1146 pdb_set_bad_password_count(sampass,
1147 cache_entry->bad_password_count,
1149 pdb_set_bad_password_time(sampass,
1150 cache_entry->bad_password_time,
1159 SAFE_FREE(cache_entry);
1163 /**********************************************************************
1164 Initialize the ldap db from a struct samu. Called on update.
1165 (Based on init_buffer_from_sam in pdb_tdb.c)
1166 *********************************************************************/
1168 static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
1169 LDAPMessage *existing,
1170 LDAPMod *** mods, struct samu * sampass,
1171 bool (*need_update)(const struct samu *,
1177 if (mods == NULL || sampass == NULL) {
1178 DEBUG(0, ("init_ldap_from_sam: NULL parameters found!\n"));
1185 * took out adding "objectclass: sambaAccount"
1186 * do this on a per-mod basis
1188 if (need_update(sampass, PDB_USERNAME)) {
1189 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1190 "uid", pdb_get_username(sampass));
1191 if (ldap_state->is_nds_ldap) {
1192 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1193 "cn", pdb_get_username(sampass));
1194 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1195 "sn", pdb_get_username(sampass));
1199 DEBUG(2, ("init_ldap_from_sam: Setting entry for user: %s\n", pdb_get_username(sampass)));
1201 /* only update the RID if we actually need to */
1202 if (need_update(sampass, PDB_USERSID)) {
1204 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
1206 switch ( ldap_state->schema_ver ) {
1207 case SCHEMAVER_SAMBAACCOUNT:
1208 if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
1209 DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1210 sid_string_dbg(user_sid),
1212 &ldap_state->domain_sid)));
1215 if (asprintf(&temp, "%i", rid) < 0) {
1218 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1219 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID),
1224 case SCHEMAVER_SAMBASAMACCOUNT:
1225 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1226 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID),
1227 sid_to_fstring(sid_string, user_sid));
1231 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1236 /* we don't need to store the primary group RID - so leaving it
1237 'free' to hang off the unix primary group makes life easier */
1239 if (need_update(sampass, PDB_GROUPSID)) {
1241 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
1243 switch ( ldap_state->schema_ver ) {
1244 case SCHEMAVER_SAMBAACCOUNT:
1245 if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
1246 DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
1247 sid_string_dbg(group_sid),
1249 &ldap_state->domain_sid)));
1253 if (asprintf(&temp, "%i", rid) < 0) {
1256 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1257 get_userattr_key2string(ldap_state->schema_ver,
1258 LDAP_ATTR_PRIMARY_GROUP_RID), temp);
1262 case SCHEMAVER_SAMBASAMACCOUNT:
1263 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1264 get_userattr_key2string(ldap_state->schema_ver,
1265 LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
1269 DEBUG(0,("init_ldap_from_sam: unknown schema version specified\n"));
1275 /* displayName, cn, and gecos should all be the same
1276 * most easily accomplished by giving them the same OID
1277 * gecos isn't set here b/c it should be handled by the
1279 * We change displayName only and fall back to cn if
1280 * it does not exist.
1283 if (need_update(sampass, PDB_FULLNAME))
1284 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1285 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DISPLAY_NAME),
1286 pdb_get_fullname(sampass));
1288 if (need_update(sampass, PDB_ACCTDESC))
1289 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1290 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_DESC),
1291 pdb_get_acct_desc(sampass));
1293 if (need_update(sampass, PDB_WORKSTATIONS))
1294 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1295 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_WKS),
1296 pdb_get_workstations(sampass));
1298 if (need_update(sampass, PDB_MUNGEDDIAL))
1299 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1300 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_MUNGED_DIAL),
1301 pdb_get_munged_dial(sampass));
1303 if (need_update(sampass, PDB_SMBHOME))
1304 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1305 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_PATH),
1306 pdb_get_homedir(sampass));
1308 if (need_update(sampass, PDB_DRIVE))
1309 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1310 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_HOME_DRIVE),
1311 pdb_get_dir_drive(sampass));
1313 if (need_update(sampass, PDB_LOGONSCRIPT))
1314 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1315 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_SCRIPT),
1316 pdb_get_logon_script(sampass));
1318 if (need_update(sampass, PDB_PROFILE))
1319 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1320 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH),
1321 pdb_get_profile_path(sampass));
1323 if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
1326 if (need_update(sampass, PDB_LOGONTIME))
1327 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1328 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
1331 if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
1334 if (need_update(sampass, PDB_LOGOFFTIME))
1335 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1336 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
1339 if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
1342 if (need_update(sampass, PDB_KICKOFFTIME))
1343 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1344 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
1347 if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
1350 if (need_update(sampass, PDB_CANCHANGETIME))
1351 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1352 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
1355 if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
1358 if (need_update(sampass, PDB_MUSTCHANGETIME))
1359 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1360 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
1363 if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
1364 || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
1366 if (need_update(sampass, PDB_LMPASSWD)) {
1367 const uchar *lm_pw = pdb_get_lanman_passwd(sampass);
1370 pdb_sethexpwd(pwstr, lm_pw,
1371 pdb_get_acct_ctrl(sampass));
1372 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1373 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1376 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1377 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LMPW),
1381 if (need_update(sampass, PDB_NTPASSWD)) {
1382 const uchar *nt_pw = pdb_get_nt_passwd(sampass);
1385 pdb_sethexpwd(pwstr, nt_pw,
1386 pdb_get_acct_ctrl(sampass));
1387 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1388 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1391 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1392 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_NTPW),
1397 if (need_update(sampass, PDB_PWHISTORY)) {
1399 uint32 pwHistLen = 0;
1400 pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
1402 pwstr = SMB_MALLOC_ARRAY(char, 1024);
1406 if (pwHistLen == 0) {
1407 /* Remove any password history from the LDAP store. */
1408 memset(pwstr, '0', 64); /* NOTE !!!! '0' *NOT '\0' */
1412 uint32 currHistLen = 0;
1413 const uint8 *pwhist = pdb_get_pw_history(sampass, &currHistLen);
1414 if (pwhist != NULL) {
1415 /* We can only store (1024-1/64 password history entries. */
1416 pwHistLen = MIN(pwHistLen, ((1024-1)/64));
1417 for (i=0; i< pwHistLen && i < currHistLen; i++) {
1418 /* Store the salt. */
1419 pdb_sethexpwd(&pwstr[i*64], &pwhist[i*PW_HISTORY_ENTRY_LEN], 0);
1420 /* Followed by the md5 hash of salt + md4 hash */
1421 pdb_sethexpwd(&pwstr[(i*64)+32],
1422 &pwhist[(i*PW_HISTORY_ENTRY_LEN)+PW_HISTORY_SALT_LEN], 0);
1423 DEBUG(100, ("pwstr=%s\n", pwstr));
1427 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1428 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_HISTORY),
1433 if (need_update(sampass, PDB_PASSLASTSET)) {
1434 if (asprintf(&temp, "%li",
1435 (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
1438 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1439 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_LAST_SET),
1445 if (need_update(sampass, PDB_HOURS)) {
1446 const uint8 *hours = pdb_get_hours(sampass);
1449 pdb_sethexhours(hourstr, hours);
1450 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct,
1453 get_userattr_key2string(ldap_state->schema_ver,
1454 LDAP_ATTR_LOGON_HOURS),
1459 if (need_update(sampass, PDB_ACCTCTRL))
1460 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
1461 get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_ACB_INFO),
1462 pdb_encode_acct_ctrl (pdb_get_acct_ctrl(sampass), NEW_PW_FORMAT_SPACE_PADDED_LEN));
1464 /* password lockout cache:
1465 - If we are now autolocking or clearing, we write to ldap
1466 - If we are clearing, we delete the cache entry
1467 - If the count is > 0, we update the cache
1469 This even means when autolocking, we cache, just in case the
1470 update doesn't work, and we have to cache the autolock flag */
1472 if (need_update(sampass, PDB_BAD_PASSWORD_COUNT)) /* &&
1473 need_update(sampass, PDB_BAD_PASSWORD_TIME)) */ {
1474 uint16 badcount = pdb_get_bad_password_count(sampass);
1475 time_t badtime = pdb_get_bad_password_time(sampass);
1477 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
1479 DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
1480 (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
1482 if ((badcount >= pol) || (badcount == 0)) {
1483 DEBUG(7, ("making mods to update ldap, count=%u, time=%u\n",
1484 (unsigned int)badcount, (unsigned int)badtime));
1485 if (asprintf(&temp, "%li", (long)badcount) < 0) {
1489 ldap_state->smbldap_state->ldap_struct,
1491 get_userattr_key2string(
1492 ldap_state->schema_ver,
1493 LDAP_ATTR_BAD_PASSWORD_COUNT),
1497 if (asprintf(&temp, "%li", (long int)badtime) < 0) {
1501 ldap_state->smbldap_state->ldap_struct,
1503 get_userattr_key2string(
1504 ldap_state->schema_ver,
1505 LDAP_ATTR_BAD_PASSWORD_TIME),
1509 if (badcount == 0) {
1510 DEBUG(7, ("bad password count is reset, deleting login cache entry for %s\n", pdb_get_nt_username(sampass)));
1511 login_cache_delentry(sampass);
1513 LOGIN_CACHE cache_entry;
1515 cache_entry.entry_timestamp = time(NULL);
1516 cache_entry.acct_ctrl = pdb_get_acct_ctrl(sampass);
1517 cache_entry.bad_password_count = badcount;
1518 cache_entry.bad_password_time = badtime;
1520 DEBUG(7, ("Updating bad password count and time in login cache\n"));
1521 login_cache_write(sampass, cache_entry);
1528 /**********************************************************************
1529 End enumeration of the LDAP password list.
1530 *********************************************************************/
1532 static void ldapsam_endsampwent(struct pdb_methods *my_methods)
1534 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1535 if (ldap_state->result) {
1536 ldap_msgfree(ldap_state->result);
1537 ldap_state->result = NULL;
1541 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
1542 const char *new_attr)
1546 if (new_attr == NULL) {
1550 for (i=0; (*attr_list)[i] != NULL; i++) {
1554 (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
1556 SMB_ASSERT((*attr_list) != NULL);
1557 (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
1558 (*attr_list)[i+1] = NULL;
1561 static void ldapsam_add_unix_attributes(TALLOC_CTX *mem_ctx,
1562 const char ***attr_list)
1564 append_attr(mem_ctx, attr_list, "uidNumber");
1565 append_attr(mem_ctx, attr_list, "gidNumber");
1566 append_attr(mem_ctx, attr_list, "homeDirectory");
1567 append_attr(mem_ctx, attr_list, "loginShell");
1568 append_attr(mem_ctx, attr_list, "gecos");
1571 /**********************************************************************
1572 Get struct samu entry from LDAP by username.
1573 *********************************************************************/
1575 static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu *user, const char *sname)
1577 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1578 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1579 LDAPMessage *result = NULL;
1580 LDAPMessage *entry = NULL;
1582 const char ** attr_list;
1585 attr_list = get_userattr_list( user, ldap_state->schema_ver );
1586 append_attr(user, &attr_list,
1587 get_userattr_key2string(ldap_state->schema_ver,
1588 LDAP_ATTR_MOD_TIMESTAMP));
1589 ldapsam_add_unix_attributes(user, &attr_list);
1590 rc = ldapsam_search_suffix_by_name(ldap_state, sname, &result,
1592 TALLOC_FREE( attr_list );
1594 if ( rc != LDAP_SUCCESS )
1595 return NT_STATUS_NO_SUCH_USER;
1597 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1600 DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
1601 ldap_msgfree(result);
1602 return NT_STATUS_NO_SUCH_USER;
1603 } else if (count > 1) {
1604 DEBUG(1, ("ldapsam_getsampwnam: Duplicate entries for this user [%s] Failing. count=%d\n", sname, count));
1605 ldap_msgfree(result);
1606 return NT_STATUS_NO_SUCH_USER;
1609 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1611 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1612 DEBUG(1,("ldapsam_getsampwnam: init_sam_from_ldap failed for user '%s'!\n", sname));
1613 ldap_msgfree(result);
1614 return NT_STATUS_NO_SUCH_USER;
1616 pdb_set_backend_private_data(user, result, NULL,
1617 my_methods, PDB_CHANGED);
1618 talloc_autofree_ldapmsg(user, result);
1621 ldap_msgfree(result);
1626 static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
1627 const DOM_SID *sid, LDAPMessage **result)
1630 const char ** attr_list;
1633 switch ( ldap_state->schema_ver ) {
1634 case SCHEMAVER_SAMBASAMACCOUNT: {
1635 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1636 if (tmp_ctx == NULL) {
1637 return LDAP_NO_MEMORY;
1640 attr_list = get_userattr_list(tmp_ctx,
1641 ldap_state->schema_ver);
1642 append_attr(tmp_ctx, &attr_list,
1643 get_userattr_key2string(
1644 ldap_state->schema_ver,
1645 LDAP_ATTR_MOD_TIMESTAMP));
1646 ldapsam_add_unix_attributes(tmp_ctx, &attr_list);
1647 rc = ldapsam_search_suffix_by_sid(ldap_state, sid,
1649 TALLOC_FREE(tmp_ctx);
1651 if ( rc != LDAP_SUCCESS )
1656 case SCHEMAVER_SAMBAACCOUNT:
1657 if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
1661 attr_list = get_userattr_list(NULL,
1662 ldap_state->schema_ver);
1663 rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
1664 TALLOC_FREE( attr_list );
1666 if ( rc != LDAP_SUCCESS )
1673 /**********************************************************************
1674 Get struct samu entry from LDAP by SID.
1675 *********************************************************************/
1677 static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1679 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1680 LDAPMessage *result = NULL;
1681 LDAPMessage *entry = NULL;
1685 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
1687 if (rc != LDAP_SUCCESS)
1688 return NT_STATUS_NO_SUCH_USER;
1690 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
1693 DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
1694 "count=%d\n", sid_string_dbg(sid), count));
1695 ldap_msgfree(result);
1696 return NT_STATUS_NO_SUCH_USER;
1697 } else if (count > 1) {
1698 DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
1699 "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
1701 ldap_msgfree(result);
1702 return NT_STATUS_NO_SUCH_USER;
1705 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
1707 ldap_msgfree(result);
1708 return NT_STATUS_NO_SUCH_USER;
1711 if (!init_sam_from_ldap(ldap_state, user, entry)) {
1712 DEBUG(1,("ldapsam_getsampwsid: init_sam_from_ldap failed!\n"));
1713 ldap_msgfree(result);
1714 return NT_STATUS_NO_SUCH_USER;
1717 pdb_set_backend_private_data(user, result, NULL,
1718 my_methods, PDB_CHANGED);
1719 talloc_autofree_ldapmsg(user, result);
1720 return NT_STATUS_OK;
1723 /********************************************************************
1724 Do the actual modification - also change a plaintext passord if
1726 **********************************************************************/
1728 static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
1729 struct samu *newpwd, char *dn,
1730 LDAPMod **mods, int ldap_op,
1731 bool (*need_update)(const struct samu *, enum pdb_elements))
1733 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1736 if (!newpwd || !dn) {
1737 return NT_STATUS_INVALID_PARAMETER;
1741 DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
1742 /* may be password change below however */
1746 if (ldap_state->is_nds_ldap) {
1747 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1751 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1755 rc = smbldap_add(ldap_state->smbldap_state,
1758 case LDAP_MOD_REPLACE:
1759 rc = smbldap_modify(ldap_state->smbldap_state,
1763 DEBUG(0,("ldapsam_modify_entry: Wrong LDAP operation type: %d!\n",
1765 return NT_STATUS_INVALID_PARAMETER;
1768 if (rc!=LDAP_SUCCESS) {
1769 return NT_STATUS_UNSUCCESSFUL;
1773 if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
1774 (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
1775 need_update(newpwd, PDB_PLAINTEXT_PW) &&
1776 (pdb_get_plaintext_passwd(newpwd)!=NULL)) {
1779 char *retoid = NULL;
1780 struct berval *retdata = NULL;
1781 char *utf8_password;
1783 size_t converted_size;
1786 if (!ldap_state->is_nds_ldap) {
1788 if (!smbldap_has_extension(ldap_state->smbldap_state->ldap_struct,
1789 LDAP_EXOP_MODIFY_PASSWD)) {
1790 DEBUG(2, ("ldap password change requested, but LDAP "
1791 "server does not support it -- ignoring\n"));
1792 return NT_STATUS_OK;
1796 if (!push_utf8_allocate(&utf8_password,
1797 pdb_get_plaintext_passwd(newpwd),
1800 return NT_STATUS_NO_MEMORY;
1803 if (!push_utf8_allocate(&utf8_dn, dn, &converted_size)) {
1804 SAFE_FREE(utf8_password);
1805 return NT_STATUS_NO_MEMORY;
1808 if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
1809 DEBUG(0,("ber_alloc_t returns NULL\n"));
1810 SAFE_FREE(utf8_password);
1812 return NT_STATUS_UNSUCCESSFUL;
1815 if ((ber_printf (ber, "{") < 0) ||
1816 (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1818 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1822 SAFE_FREE(utf8_password);
1823 return NT_STATUS_UNSUCCESSFUL;
1826 if ((utf8_password != NULL) && (*utf8_password != '\0')) {
1827 ret = ber_printf(ber, "ts}",
1828 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
1831 ret = ber_printf(ber, "}");
1835 DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
1839 SAFE_FREE(utf8_password);
1840 return NT_STATUS_UNSUCCESSFUL;
1843 if ((rc = ber_flatten (ber, &bv))<0) {
1844 DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
1847 SAFE_FREE(utf8_password);
1848 return NT_STATUS_UNSUCCESSFUL;
1852 SAFE_FREE(utf8_password);
1855 if (!ldap_state->is_nds_ldap) {
1856 rc = smbldap_extended_operation(ldap_state->smbldap_state,
1857 LDAP_EXOP_MODIFY_PASSWD,
1858 bv, NULL, NULL, &retoid,
1861 rc = pdb_nds_set_password(ldap_state->smbldap_state, dn,
1862 pdb_get_plaintext_passwd(newpwd));
1864 if (rc != LDAP_SUCCESS) {
1865 char *ld_error = NULL;
1867 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
1868 DEBUG(3, ("Could not set userPassword "
1869 "attribute due to an objectClass "
1870 "violation -- ignoring\n"));
1872 return NT_STATUS_OK;
1875 ldap_get_option(ldap_state->smbldap_state->ldap_struct, LDAP_OPT_ERROR_STRING,
1877 DEBUG(0,("ldapsam_modify_entry: LDAP Password could not be changed for user %s: %s\n\t%s\n",
1878 pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
1879 SAFE_FREE(ld_error);
1881 #if defined(LDAP_CONSTRAINT_VIOLATION)
1882 if (rc == LDAP_CONSTRAINT_VIOLATION)
1883 return NT_STATUS_PASSWORD_RESTRICTION;
1885 return NT_STATUS_UNSUCCESSFUL;
1887 DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
1888 #ifdef DEBUG_PASSWORD
1889 DEBUG(100,("ldapsam_modify_entry: LDAP Password changed to %s\n",pdb_get_plaintext_passwd(newpwd)));
1892 ber_bvfree(retdata);
1894 ldap_memfree(retoid);
1898 return NT_STATUS_OK;
1901 /**********************************************************************
1902 Delete entry from LDAP for username.
1903 *********************************************************************/
1905 static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
1906 struct samu * sam_acct)
1908 struct ldapsam_privates *priv =
1909 (struct ldapsam_privates *)my_methods->private_data;
1912 LDAPMessage *msg, *entry;
1913 NTSTATUS result = NT_STATUS_NO_MEMORY;
1914 const char **attr_list;
1915 TALLOC_CTX *mem_ctx;
1918 DEBUG(0, ("ldapsam_delete_sam_account: sam_acct was NULL!\n"));
1919 return NT_STATUS_INVALID_PARAMETER;
1922 sname = pdb_get_username(sam_acct);
1924 DEBUG(3, ("ldapsam_delete_sam_account: Deleting user %s from "
1927 mem_ctx = talloc_new(NULL);
1928 if (mem_ctx == NULL) {
1929 DEBUG(0, ("talloc_new failed\n"));
1933 attr_list = get_userattr_delete_list(mem_ctx, priv->schema_ver );
1934 if (attr_list == NULL) {
1938 rc = ldapsam_search_suffix_by_name(priv, sname, &msg, attr_list);
1940 if ((rc != LDAP_SUCCESS) ||
1941 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
1942 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
1943 DEBUG(5, ("Could not find user %s\n", sname));
1944 result = NT_STATUS_NO_SUCH_USER;
1948 rc = ldapsam_delete_entry(
1949 priv, mem_ctx, entry,
1950 priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
1951 LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
1954 result = (rc == LDAP_SUCCESS) ?
1955 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1958 TALLOC_FREE(mem_ctx);
1962 /**********************************************************************
1963 Helper function to determine for update_sam_account whether
1964 we need LDAP modification.
1965 *********************************************************************/
1967 static bool element_is_changed(const struct samu *sampass,
1968 enum pdb_elements element)
1970 return IS_SAM_CHANGED(sampass, element);
1973 /**********************************************************************
1975 *********************************************************************/
1977 static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
1979 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
1980 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
1983 LDAPMessage *result = NULL;
1984 LDAPMessage *entry = NULL;
1985 LDAPMod **mods = NULL;
1986 const char **attr_list;
1988 result = (LDAPMessage *)pdb_get_backend_private_data(newpwd, my_methods);
1990 attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
1991 if (pdb_get_username(newpwd) == NULL) {
1992 return NT_STATUS_INVALID_PARAMETER;
1994 rc = ldapsam_search_suffix_by_name(ldap_state, pdb_get_username(newpwd), &result, attr_list );
1995 TALLOC_FREE( attr_list );
1996 if (rc != LDAP_SUCCESS) {
1997 return NT_STATUS_UNSUCCESSFUL;
1999 pdb_set_backend_private_data(newpwd, result, NULL,
2000 my_methods, PDB_CHANGED);
2001 talloc_autofree_ldapmsg(newpwd, result);
2004 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {
2005 DEBUG(0, ("ldapsam_update_sam_account: No user to modify!\n"));
2006 return NT_STATUS_UNSUCCESSFUL;
2009 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
2010 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
2012 return NT_STATUS_UNSUCCESSFUL;
2015 DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
2017 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2018 element_is_changed)) {
2019 DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
2022 ldap_mods_free(mods,True);
2023 return NT_STATUS_UNSUCCESSFUL;
2026 if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
2027 && (mods == NULL)) {
2028 DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
2029 pdb_get_username(newpwd)));
2031 return NT_STATUS_OK;
2034 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
2037 ldap_mods_free(mods,True);
2043 * We need to set the backend private data to NULL here. For example
2044 * setuserinfo level 25 does a pdb_update_sam_account twice on the
2045 * same one, and with the explicit delete / add logic for attribute
2046 * values the second time we would use the wrong "old" value which
2047 * does not exist in LDAP anymore. Thus the LDAP server would refuse
2049 * The existing LDAPMessage is still being auto-freed by the
2052 pdb_set_backend_private_data(newpwd, NULL, NULL, my_methods,
2055 if (!NT_STATUS_IS_OK(ret)) {
2059 DEBUG(2, ("ldapsam_update_sam_account: successfully modified uid = %s in the LDAP database\n",
2060 pdb_get_username(newpwd)));
2061 return NT_STATUS_OK;
2064 /***************************************************************************
2065 Renames a struct samu
2066 - The "rename user script" has full responsibility for changing everything
2067 ***************************************************************************/
2069 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
2070 struct samu *old_acct,
2071 const char *newname)
2073 const char *oldname;
2075 char *rename_script = NULL;
2076 fstring oldname_lower, newname_lower;
2079 DEBUG(0, ("ldapsam_rename_sam_account: old_acct was NULL!\n"));
2080 return NT_STATUS_INVALID_PARAMETER;
2083 DEBUG(0, ("ldapsam_rename_sam_account: newname was NULL!\n"));
2084 return NT_STATUS_INVALID_PARAMETER;
2087 oldname = pdb_get_username(old_acct);
2089 /* rename the posix user */
2090 rename_script = SMB_STRDUP(lp_renameuser_script());
2091 if (rename_script == NULL) {
2092 return NT_STATUS_NO_MEMORY;
2095 if (!(*rename_script)) {
2096 SAFE_FREE(rename_script);
2097 return NT_STATUS_ACCESS_DENIED;
2100 DEBUG (3, ("ldapsam_rename_sam_account: Renaming user %s to %s.\n",
2103 /* We have to allow the account name to end with a '$'.
2104 Also, follow the semantics in _samr_create_user() and lower case the
2105 posix name but preserve the case in passdb */
2107 fstrcpy( oldname_lower, oldname );
2108 strlower_m( oldname_lower );
2109 fstrcpy( newname_lower, newname );
2110 strlower_m( newname_lower );
2111 rename_script = realloc_string_sub2(rename_script,
2116 if (!rename_script) {
2117 return NT_STATUS_NO_MEMORY;
2119 rename_script = realloc_string_sub2(rename_script,
2124 rc = smbrun(rename_script, NULL);
2126 DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
2127 rename_script, rc));
2129 SAFE_FREE(rename_script);
2132 smb_nscd_flush_user_cache();
2136 return NT_STATUS_UNSUCCESSFUL;
2138 return NT_STATUS_OK;
2141 /**********************************************************************
2142 Helper function to determine for update_sam_account whether
2143 we need LDAP modification.
2144 *********************************************************************/
2146 static bool element_is_set_or_changed(const struct samu *sampass,
2147 enum pdb_elements element)
2149 return (IS_SAM_SET(sampass, element) ||
2150 IS_SAM_CHANGED(sampass, element));
2153 /**********************************************************************
2154 Add struct samu to LDAP.
2155 *********************************************************************/
2157 static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct samu * newpwd)
2159 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2160 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
2162 LDAPMessage *result = NULL;
2163 LDAPMessage *entry = NULL;
2164 LDAPMod **mods = NULL;
2165 int ldap_op = LDAP_MOD_REPLACE;
2167 const char **attr_list;
2168 char *escape_user = NULL;
2169 const char *username = pdb_get_username(newpwd);
2170 const DOM_SID *sid = pdb_get_user_sid(newpwd);
2171 char *filter = NULL;
2173 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2174 TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
2177 return NT_STATUS_NO_MEMORY;
2180 if (!username || !*username) {
2181 DEBUG(0, ("ldapsam_add_sam_account: Cannot add user without a username!\n"));
2182 status = NT_STATUS_INVALID_PARAMETER;
2186 /* free this list after the second search or in case we exit on failure */
2187 attr_list = get_userattr_list(ctx, ldap_state->schema_ver);
2189 rc = ldapsam_search_suffix_by_name (ldap_state, username, &result, attr_list);
2191 if (rc != LDAP_SUCCESS) {
2195 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2196 DEBUG(0,("ldapsam_add_sam_account: User '%s' already in the base, with samba attributes\n",
2200 ldap_msgfree(result);
2203 if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
2204 rc = ldapsam_get_ldap_user_by_sid(ldap_state,
2206 if (rc == LDAP_SUCCESS) {
2207 if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
2208 DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
2209 "already in the base, with samba "
2210 "attributes\n", sid_string_dbg(sid)));
2213 ldap_msgfree(result);
2218 /* does the entry already exist but without a samba attributes?
2219 we need to return the samba attributes here */
2221 escape_user = escape_ldap_string_alloc( username );
2222 filter = talloc_strdup(attr_list, "(uid=%u)");
2224 status = NT_STATUS_NO_MEMORY;
2227 filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
2229 status = NT_STATUS_NO_MEMORY;
2232 SAFE_FREE(escape_user);
2234 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2235 filter, attr_list, &result);
2236 if ( rc != LDAP_SUCCESS ) {
2240 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2242 if (num_result > 1) {
2243 DEBUG (0, ("ldapsam_add_sam_account: More than one user with that uid exists: bailing out!\n"));
2247 /* Check if we need to update an existing entry */
2248 if (num_result == 1) {
2249 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2250 ldap_op = LDAP_MOD_REPLACE;
2251 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2252 dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
2254 status = NT_STATUS_NO_MEMORY;
2258 } else if (ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT) {
2260 /* There might be a SID for this account already - say an idmap entry */
2262 filter = talloc_asprintf(ctx,
2263 "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
2264 get_userattr_key2string(ldap_state->schema_ver,
2265 LDAP_ATTR_USER_SID),
2266 sid_string_talloc(ctx, sid),
2267 LDAP_OBJ_IDMAP_ENTRY,
2268 LDAP_OBJ_SID_ENTRY);
2270 status = NT_STATUS_NO_MEMORY;
2274 /* free old result before doing a new search */
2275 if (result != NULL) {
2276 ldap_msgfree(result);
2279 rc = smbldap_search_suffix(ldap_state->smbldap_state,
2280 filter, attr_list, &result);
2282 if ( rc != LDAP_SUCCESS ) {
2286 num_result = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
2288 if (num_result > 1) {
2289 DEBUG (0, ("ldapsam_add_sam_account: More than one user with specified Sid exists: bailing out!\n"));
2293 /* Check if we need to update an existing entry */
2294 if (num_result == 1) {
2296 DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
2297 ldap_op = LDAP_MOD_REPLACE;
2298 entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
2299 dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
2301 status = NT_STATUS_NO_MEMORY;
2307 if (num_result == 0) {
2308 char *escape_username;
2309 /* Check if we need to add an entry */
2310 DEBUG(3,("ldapsam_add_sam_account: Adding new user\n"));
2311 ldap_op = LDAP_MOD_ADD;
2313 escape_username = escape_rdn_val_string_alloc(username);
2314 if (!escape_username) {
2315 status = NT_STATUS_NO_MEMORY;
2319 if (username[strlen(username)-1] == '$') {
2320 dn = talloc_asprintf(ctx,
2323 lp_ldap_machine_suffix());
2325 dn = talloc_asprintf(ctx,
2328 lp_ldap_user_suffix());
2331 SAFE_FREE(escape_username);
2333 status = NT_STATUS_NO_MEMORY;
2338 if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
2339 element_is_set_or_changed)) {
2340 DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
2342 ldap_mods_free(mods, true);
2348 DEBUG(0,("ldapsam_add_sam_account: mods is empty: nothing to add for user: %s\n",pdb_get_username(newpwd)));
2351 switch ( ldap_state->schema_ver ) {
2352 case SCHEMAVER_SAMBAACCOUNT:
2353 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
2355 case SCHEMAVER_SAMBASAMACCOUNT:
2356 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
2359 DEBUG(0,("ldapsam_add_sam_account: invalid schema version specified\n"));
2363 ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
2364 if (!NT_STATUS_IS_OK(ret)) {
2365 DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
2366 pdb_get_username(newpwd),dn));
2367 ldap_mods_free(mods, true);
2371 DEBUG(2,("ldapsam_add_sam_account: added: uid == %s in the LDAP database\n", pdb_get_username(newpwd)));
2372 ldap_mods_free(mods, true);
2374 status = NT_STATUS_OK;
2379 SAFE_FREE(escape_user);
2381 ldap_msgfree(result);
2386 /**********************************************************************
2387 *********************************************************************/
2389 static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
2391 LDAPMessage ** result)
2393 int scope = LDAP_SCOPE_SUBTREE;
2395 const char **attr_list;
2397 attr_list = get_attr_list(NULL, groupmap_attr_list);
2398 rc = smbldap_search(ldap_state->smbldap_state,
2399 lp_ldap_suffix (), scope,
2400 filter, attr_list, 0, result);
2401 TALLOC_FREE(attr_list);
2406 /**********************************************************************
2407 *********************************************************************/
2409 static bool init_group_from_ldap(struct ldapsam_privates *ldap_state,
2410 GROUP_MAP *map, LDAPMessage *entry)
2413 TALLOC_CTX *ctx = talloc_init("init_group_from_ldap");
2415 if (ldap_state == NULL || map == NULL || entry == NULL ||
2416 ldap_state->smbldap_state->ldap_struct == NULL) {
2417 DEBUG(0, ("init_group_from_ldap: NULL parameters found!\n"));
2422 temp = smbldap_talloc_single_attribute(
2423 ldap_state->smbldap_state->ldap_struct,
2425 get_attr_key2string(groupmap_attr_list,
2426 LDAP_ATTR_GIDNUMBER),
2429 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2430 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GIDNUMBER)));
2434 DEBUG(2, ("init_group_from_ldap: Entry found for group: %s\n", temp));
2436 map->gid = (gid_t)atol(temp);
2439 temp = smbldap_talloc_single_attribute(
2440 ldap_state->smbldap_state->ldap_struct,
2442 get_attr_key2string(groupmap_attr_list,
2443 LDAP_ATTR_GROUP_SID),
2446 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2447 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_SID)));
2452 if (!string_to_sid(&map->sid, temp)) {
2453 DEBUG(1, ("SID string [%s] could not be read as a valid SID\n", temp));
2459 temp = smbldap_talloc_single_attribute(
2460 ldap_state->smbldap_state->ldap_struct,
2462 get_attr_key2string(groupmap_attr_list,
2463 LDAP_ATTR_GROUP_TYPE),
2466 DEBUG(0, ("init_group_from_ldap: Mandatory attribute %s not found\n",
2467 get_attr_key2string( groupmap_attr_list, LDAP_ATTR_GROUP_TYPE)));
2471 map->sid_name_use = (enum lsa_SidType)atol(temp);
2473 if ((map->sid_name_use < SID_NAME_USER) ||
2474 (map->sid_name_use > SID_NAME_UNKNOWN)) {
2475 DEBUG(0, ("init_group_from_ldap: Unknown Group type: %d\n", map->sid_name_use));
2481 temp = smbldap_talloc_single_attribute(
2482 ldap_state->smbldap_state->ldap_struct,
2484 get_attr_key2string(groupmap_attr_list,
2485 LDAP_ATTR_DISPLAY_NAME),
2488 temp = smbldap_talloc_single_attribute(
2489 ldap_state->smbldap_state->ldap_struct,
2491 get_attr_key2string(groupmap_attr_list,
2495 DEBUG(0, ("init_group_from_ldap: Attributes cn not found either \
2496 for gidNumber(%lu)\n",(unsigned long)map->gid));
2501 fstrcpy(map->nt_name, temp);
2504 temp = smbldap_talloc_single_attribute(
2505 ldap_state->smbldap_state->ldap_struct,
2507 get_attr_key2string(groupmap_attr_list,
2511 temp = talloc_strdup(ctx, "");
2517 fstrcpy(map->comment, temp);
2519 if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
2520 store_gid_sid_cache(&map->sid, map->gid);
2521 idmap_cache_set_sid2gid(&map->sid, map->gid);
2528 /**********************************************************************
2529 *********************************************************************/
2531 static NTSTATUS ldapsam_getgroup(struct pdb_methods *methods,
2535 struct ldapsam_privates *ldap_state =
2536 (struct ldapsam_privates *)methods->private_data;
2537 LDAPMessage *result = NULL;
2538 LDAPMessage *entry = NULL;
2541 if (ldapsam_search_one_group(ldap_state, filter, &result)
2543 return NT_STATUS_NO_SUCH_GROUP;
2546 count = ldap_count_entries(priv2ld(ldap_state), result);
2549 DEBUG(4, ("ldapsam_getgroup: Did not find group, filter was "
2551 ldap_msgfree(result);
2552 return NT_STATUS_NO_SUCH_GROUP;
2556 DEBUG(1, ("ldapsam_getgroup: Duplicate entries for filter %s: "
2557 "count=%d\n", filter, count));
2558 ldap_msgfree(result);
2559 return NT_STATUS_NO_SUCH_GROUP;
2562 entry = ldap_first_entry(priv2ld(ldap_state), result);
2565 ldap_msgfree(result);
2566 return NT_STATUS_UNSUCCESSFUL;
2569 if (!init_group_from_ldap(ldap_state, map, entry)) {
2570 DEBUG(1, ("ldapsam_getgroup: init_group_from_ldap failed for "
2571 "group filter %s\n", filter));
2572 ldap_msgfree(result);
2573 return NT_STATUS_NO_SUCH_GROUP;
2576 ldap_msgfree(result);
2577 return NT_STATUS_OK;
2580 /**********************************************************************
2581 *********************************************************************/
2583 static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
2586 char *filter = NULL;
2590 if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
2592 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
2593 sid_to_fstring(tmp, &sid)) < 0) {
2594 return NT_STATUS_NO_MEMORY;
2597 status = ldapsam_getgroup(methods, filter, map);
2602 /**********************************************************************
2603 *********************************************************************/
2605 static NTSTATUS ldapsam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
2608 char *filter = NULL;
2611 if (asprintf(&filter, "(&(objectClass=%s)(%s=%lu))",
2613 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GIDNUMBER),
2614 (unsigned long)gid) < 0) {
2615 return NT_STATUS_NO_MEMORY;
2618 status = ldapsam_getgroup(methods, filter, map);
2623 /**********************************************************************
2624 *********************************************************************/
2626 static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
2629 char *filter = NULL;
2630 char *escape_name = escape_ldap_string_alloc(name);
2634 return NT_STATUS_NO_MEMORY;
2637 if (asprintf(&filter, "(&(objectClass=%s)(|(%s=%s)(%s=%s)))",
2639 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
2640 get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
2642 SAFE_FREE(escape_name);
2643 return NT_STATUS_NO_MEMORY;
2646 SAFE_FREE(escape_name);
2647 status = ldapsam_getgroup(methods, filter, map);
2652 static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
2654 const DOM_SID *domain_sid,
2660 if (!smbldap_get_single_attribute(ldap_struct, entry, "sambaSID",
2661 str, sizeof(str)-1)) {
2662 DEBUG(10, ("Could not find sambaSID attribute\n"));
2666 if (!string_to_sid(&sid, str)) {
2667 DEBUG(10, ("Could not convert string %s to sid\n", str));
2671 if (sid_compare_domain(&sid, domain_sid) != 0) {
2672 DEBUG(10, ("SID %s is not in expected domain %s\n",
2673 str, sid_string_dbg(domain_sid)));
2677 if (!sid_peek_rid(&sid, rid)) {
2678 DEBUG(10, ("Could not peek into RID\n"));
2685 static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
2686 TALLOC_CTX *mem_ctx,
2687 const DOM_SID *group,
2688 uint32 **pp_member_rids,
2689 size_t *p_num_members)
2691 struct ldapsam_privates *ldap_state =
2692 (struct ldapsam_privates *)methods->private_data;
2693 struct smbldap_state *conn = ldap_state->smbldap_state;
2694 const char *id_attrs[] = { "memberUid", "gidNumber", NULL };
2695 const char *sid_attrs[] = { "sambaSID", NULL };
2696 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2697 LDAPMessage *result = NULL;
2700 char **values = NULL;
2705 *pp_member_rids = NULL;
2708 filter = talloc_asprintf(mem_ctx,
2709 "(&(objectClass=%s)"
2712 LDAP_OBJ_POSIXGROUP,
2714 sid_string_talloc(mem_ctx, group));
2715 if (filter == NULL) {
2716 ret = NT_STATUS_NO_MEMORY;
2720 rc = smbldap_search(conn, lp_ldap_suffix(),
2721 LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
2724 if (rc != LDAP_SUCCESS)
2727 talloc_autofree_ldapmsg(mem_ctx, result);
2729 count = ldap_count_entries(conn->ldap_struct, result);
2732 DEBUG(1, ("Found more than one groupmap entry for %s\n",
2733 sid_string_dbg(group)));
2734 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2739 ret = NT_STATUS_NO_SUCH_GROUP;
2743 entry = ldap_first_entry(conn->ldap_struct, result);
2747 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2749 DEBUG (0, ("ldapsam_enum_group_members: Unable to find the group's gid!\n"));
2750 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2754 values = ldap_get_values(conn->ldap_struct, entry, "memberUid");
2756 if ((values != NULL) && (values[0] != NULL)) {
2758 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(|", LDAP_OBJ_SAMBASAMACCOUNT);
2759 if (filter == NULL) {
2760 ret = NT_STATUS_NO_MEMORY;
2764 for (memberuid = values; *memberuid != NULL; memberuid += 1) {
2765 char *escape_memberuid;
2767 escape_memberuid = escape_ldap_string_alloc(*memberuid);
2768 if (escape_memberuid == NULL) {
2769 ret = NT_STATUS_NO_MEMORY;
2773 filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
2774 if (filter == NULL) {
2775 SAFE_FREE(escape_memberuid);
2776 ret = NT_STATUS_NO_MEMORY;
2780 SAFE_FREE(escape_memberuid);
2783 filter = talloc_asprintf_append_buffer(filter, "))");
2784 if (filter == NULL) {
2785 ret = NT_STATUS_NO_MEMORY;
2789 rc = smbldap_search(conn, lp_ldap_suffix(),
2790 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2793 if (rc != LDAP_SUCCESS)
2796 count = ldap_count_entries(conn->ldap_struct, result);
2797 DEBUG(10,("ldapsam_enum_group_members: found %d accounts\n", count));
2799 talloc_autofree_ldapmsg(mem_ctx, result);
2801 for (entry = ldap_first_entry(conn->ldap_struct, result);
2803 entry = ldap_next_entry(conn->ldap_struct, entry))
2809 sidstr = smbldap_talloc_single_attribute(conn->ldap_struct,
2813 DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
2814 "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2815 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2819 if (!string_to_sid(&sid, sidstr))
2822 if (!sid_check_is_in_our_domain(&sid)) {
2823 DEBUG(0, ("Inconsistent SAM -- group member uid not "
2824 "in our domain\n"));
2825 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2829 sid_peek_rid(&sid, &rid);
2831 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2833 ret = NT_STATUS_NO_MEMORY;
2839 filter = talloc_asprintf(mem_ctx,
2840 "(&(objectClass=%s)"
2842 LDAP_OBJ_SAMBASAMACCOUNT,
2845 rc = smbldap_search(conn, lp_ldap_suffix(),
2846 LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
2849 if (rc != LDAP_SUCCESS)
2852 talloc_autofree_ldapmsg(mem_ctx, result);
2854 for (entry = ldap_first_entry(conn->ldap_struct, result);
2856 entry = ldap_next_entry(conn->ldap_struct, entry))
2860 if (!ldapsam_extract_rid_from_entry(conn->ldap_struct,
2862 get_global_sam_sid(),
2864 DEBUG(0, ("Severe DB error, %s can't miss the samba SID" "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
2865 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2869 if (!add_rid_to_array_unique(mem_ctx, rid, pp_member_rids,
2871 ret = NT_STATUS_NO_MEMORY;
2881 ldap_value_free(values);
2886 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
2887 TALLOC_CTX *mem_ctx,
2891 size_t *p_num_groups)
2893 struct ldapsam_privates *ldap_state =
2894 (struct ldapsam_privates *)methods->private_data;
2895 struct smbldap_state *conn = ldap_state->smbldap_state;
2897 const char *attrs[] = { "gidNumber", "sambaSID", NULL };
2900 LDAPMessage *result = NULL;
2902 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
2903 size_t num_sids, num_gids;
2905 gid_t primary_gid = -1;
2910 if (pdb_get_username(user) == NULL) {
2911 return NT_STATUS_INVALID_PARAMETER;
2914 escape_name = escape_ldap_string_alloc(pdb_get_username(user));
2915 if (escape_name == NULL)
2916 return NT_STATUS_NO_MEMORY;
2918 if (user->unix_pw) {
2919 primary_gid = user->unix_pw->pw_gid;
2921 /* retrieve the users primary gid */
2922 filter = talloc_asprintf(mem_ctx,
2923 "(&(objectClass=%s)(uid=%s))",
2924 LDAP_OBJ_SAMBASAMACCOUNT,
2926 if (filter == NULL) {
2927 ret = NT_STATUS_NO_MEMORY;
2931 rc = smbldap_search(conn, lp_ldap_suffix(),
2932 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2934 if (rc != LDAP_SUCCESS)
2937 talloc_autofree_ldapmsg(mem_ctx, result);
2939 count = ldap_count_entries(priv2ld(ldap_state), result);
2943 DEBUG(1, ("User account [%s] not found!\n", pdb_get_username(user)));
2944 ret = NT_STATUS_NO_SUCH_USER;
2947 entry = ldap_first_entry(priv2ld(ldap_state), result);
2949 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", mem_ctx);
2951 DEBUG (1, ("Unable to find the member's gid!\n"));
2952 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2955 primary_gid = strtoul(gidstr, NULL, 10);
2958 DEBUG(1, ("found more than one account with the same user name ?!\n"));
2959 ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
2964 filter = talloc_asprintf(mem_ctx,
2965 "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
2966 LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
2967 if (filter == NULL) {
2968 ret = NT_STATUS_NO_MEMORY;
2972 rc = smbldap_search(conn, lp_ldap_suffix(),
2973 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
2975 if (rc != LDAP_SUCCESS)
2978 talloc_autofree_ldapmsg(mem_ctx, result);
2986 /* We need to add the primary group as the first gid/sid */
2988 if (!add_gid_to_array_unique(mem_ctx, primary_gid, pp_gids, &num_gids)) {
2989 ret = NT_STATUS_NO_MEMORY;
2993 /* This sid will be replaced later */
2995 ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
2997 if (!NT_STATUS_IS_OK(ret)) {
3001 for (entry = ldap_first_entry(conn->ldap_struct, result);
3003 entry = ldap_next_entry(conn->ldap_struct, entry))
3010 if (!smbldap_get_single_attribute(conn->ldap_struct,
3012 str, sizeof(str)-1))
3015 if (!string_to_sid(&sid, str))
3018 if (!smbldap_get_single_attribute(conn->ldap_struct,
3020 str, sizeof(str)-1))
3023 gid = strtoul(str, &end, 10);
3025 if (PTR_DIFF(end, str) != strlen(str))
3028 if (gid == primary_gid) {
3029 sid_copy(&(*pp_sids)[0], &sid);
3031 if (!add_gid_to_array_unique(mem_ctx, gid, pp_gids,
3033 ret = NT_STATUS_NO_MEMORY;
3036 ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
3038 if (!NT_STATUS_IS_OK(ret)) {
3044 if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
3045 DEBUG(3, ("primary group of [%s] not found\n",
3046 pdb_get_username(user)));
3050 *p_num_groups = num_sids;
3056 SAFE_FREE(escape_name);
3060 /**********************************************************************
3061 * Augment a posixGroup object with a sambaGroupMapping domgroup
3062 *********************************************************************/
3064 static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
3065 struct ldapsam_privates *ldap_state,
3068 const char *filter, *dn;
3069 LDAPMessage *msg, *entry;
3073 filter = talloc_asprintf(mem_ctx,
3074 "(&(objectClass=%s)(gidNumber=%u))",
3075 LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
3076 if (filter == NULL) {
3077 return NT_STATUS_NO_MEMORY;
3080 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3081 get_attr_list(mem_ctx, groupmap_attr_list),
3083 talloc_autofree_ldapmsg(mem_ctx, msg);
3085 if ((rc != LDAP_SUCCESS) ||
3086 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3087 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3088 return NT_STATUS_NO_SUCH_GROUP;
3091 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3093 return NT_STATUS_NO_MEMORY;
3097 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
3099 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
3100 sid_string_talloc(mem_ctx, &map->sid));
3101 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
3102 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3103 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3105 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3107 talloc_autofree_ldapmod(mem_ctx, mods);
3109 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3110 if (rc != LDAP_SUCCESS) {
3111 return NT_STATUS_ACCESS_DENIED;
3114 return NT_STATUS_OK;
3117 static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
3120 struct ldapsam_privates *ldap_state =
3121 (struct ldapsam_privates *)methods->private_data;
3122 LDAPMessage *msg = NULL;
3123 LDAPMod **mods = NULL;
3124 const char *attrs[] = { NULL };
3128 TALLOC_CTX *mem_ctx;
3135 mem_ctx = talloc_new(NULL);
3136 if (mem_ctx == NULL) {
3137 DEBUG(0, ("talloc_new failed\n"));
3138 return NT_STATUS_NO_MEMORY;
3141 filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
3142 sid_string_talloc(mem_ctx, &map->sid));
3143 if (filter == NULL) {
3144 result = NT_STATUS_NO_MEMORY;
3148 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3149 LDAP_SCOPE_SUBTREE, filter, attrs, True, &msg);
3150 talloc_autofree_ldapmsg(mem_ctx, msg);
3152 if ((rc == LDAP_SUCCESS) &&
3153 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
3155 DEBUG(3, ("SID %s already present in LDAP, refusing to add "
3156 "group mapping entry\n", sid_string_dbg(&map->sid)));
3157 result = NT_STATUS_GROUP_EXISTS;
3161 switch (map->sid_name_use) {
3163 case SID_NAME_DOM_GRP:
3164 /* To map a domain group we need to have a posix group
3166 result = ldapsam_map_posixgroup(mem_ctx, ldap_state, map);
3170 case SID_NAME_ALIAS:
3171 if (!sid_check_is_in_our_domain(&map->sid)
3172 && !sid_check_is_in_builtin(&map->sid) )
3174 DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
3175 sid_string_dbg(&map->sid)));
3176 result = NT_STATUS_INVALID_PARAMETER;
3182 DEBUG(3, ("Got invalid use '%s' for mapping\n",
3183 sid_type_lookup(map->sid_name_use)));
3184 result = NT_STATUS_INVALID_PARAMETER;
3188 /* Domain groups have been mapped in a separate routine, we have to
3189 * create an alias now */
3191 if (map->gid == -1) {
3192 DEBUG(10, ("Refusing to map gid==-1\n"));
3193 result = NT_STATUS_INVALID_PARAMETER;
3197 if (pdb_gid_to_sid(map->gid, &sid)) {
3198 DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
3199 "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
3200 result = NT_STATUS_GROUP_EXISTS;
3204 /* Ok, enough checks done. It's still racy to go ahead now, but that's
3205 * the best we can get out of LDAP. */
3207 dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
3208 sid_string_talloc(mem_ctx, &map->sid),
3209 lp_ldap_group_suffix());
3211 result = NT_STATUS_NO_MEMORY;
3217 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3218 LDAP_OBJ_SID_ENTRY);
3219 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
3221 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
3222 sid_string_talloc(mem_ctx, &map->sid));
3223 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
3224 talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
3225 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
3227 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
3229 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
3230 talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
3231 talloc_autofree_ldapmod(mem_ctx, mods);
3233 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
3235 result = (rc == LDAP_SUCCESS) ?
3236 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3239 TALLOC_FREE(mem_ctx);
3243 /**********************************************************************
3244 * Update a group mapping entry. We're quite strict about what can be changed:
3245 * Only the description and displayname may be changed. It simply does not
3246 * make any sense to change the SID, gid or the type in a mapping.
3247 *********************************************************************/
3249 static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
3252 struct ldapsam_privates *ldap_state =
3253 (struct ldapsam_privates *)methods->private_data;
3255 const char *filter, *dn;
3256 LDAPMessage *msg = NULL;
3257 LDAPMessage *entry = NULL;
3258 LDAPMod **mods = NULL;
3259 TALLOC_CTX *mem_ctx;
3262 mem_ctx = talloc_new(NULL);
3263 if (mem_ctx == NULL) {
3264 DEBUG(0, ("talloc_new failed\n"));
3265 return NT_STATUS_NO_MEMORY;
3268 /* Make 100% sure that sid, gid and type are not changed by looking up
3269 * exactly the values we're given in LDAP. */
3271 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)"
3272 "(sambaSid=%s)(gidNumber=%u)"
3273 "(sambaGroupType=%d))",
3275 sid_string_talloc(mem_ctx, &map->sid),
3276 (unsigned int)map->gid, map->sid_name_use);
3277 if (filter == NULL) {
3278 result = NT_STATUS_NO_MEMORY;
3282 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter,
3283 get_attr_list(mem_ctx, groupmap_attr_list),
3285 talloc_autofree_ldapmsg(mem_ctx, msg);
3287 if ((rc != LDAP_SUCCESS) ||
3288 (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) != 1) ||
3289 ((entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, msg)) == NULL)) {
3290 result = NT_STATUS_NO_SUCH_GROUP;
3294 dn = smbldap_talloc_dn(mem_ctx, ldap_state->smbldap_state->ldap_struct, entry);
3297 result = NT_STATUS_NO_MEMORY;
3302 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
3304 smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "description",
3306 talloc_autofree_ldapmod(mem_ctx, mods);
3309 DEBUG(4, ("ldapsam_update_group_mapping_entry: mods is empty: "
3310 "nothing to do\n"));
3311 result = NT_STATUS_OK;
3315 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3317 if (rc != LDAP_SUCCESS) {
3318 result = NT_STATUS_ACCESS_DENIED;
3322 DEBUG(2, ("ldapsam_update_group_mapping_entry: successfully modified "
3323 "group %lu in LDAP\n", (unsigned long)map->gid));
3325 result = NT_STATUS_OK;
3328 TALLOC_FREE(mem_ctx);
3332 /**********************************************************************
3333 *********************************************************************/
3335 static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
3338 struct ldapsam_privates *priv =
3339 (struct ldapsam_privates *)methods->private_data;
3340 LDAPMessage *msg, *entry;
3343 TALLOC_CTX *mem_ctx;
3346 mem_ctx = talloc_new(NULL);
3347 if (mem_ctx == NULL) {
3348 DEBUG(0, ("talloc_new failed\n"));
3349 return NT_STATUS_NO_MEMORY;
3352 filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
3353 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
3354 sid_string_talloc(mem_ctx, &sid));
3355 if (filter == NULL) {
3356 result = NT_STATUS_NO_MEMORY;
3359 rc = smbldap_search_suffix(priv->smbldap_state, filter,
3360 get_attr_list(mem_ctx, groupmap_attr_list),
3362 talloc_autofree_ldapmsg(mem_ctx, msg);
3364 if ((rc != LDAP_SUCCESS) ||
3365 (ldap_count_entries(priv2ld(priv), msg) != 1) ||
3366 ((entry = ldap_first_entry(priv2ld(priv), msg)) == NULL)) {
3367 result = NT_STATUS_NO_SUCH_GROUP;
3371 rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
3372 get_attr_list(mem_ctx,
3373 groupmap_attr_list_to_delete));
3375 if ((rc == LDAP_NAMING_VIOLATION) ||
3376 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3377 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3378 const char *attrs[] = { "sambaGroupType", "description",
3379 "displayName", "sambaSIDList",
3382 /* Second try. Don't delete the sambaSID attribute, this is
3383 for "old" entries that are tacked on a winbind
3386 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3387 LDAP_OBJ_GROUPMAP, attrs);
3390 if ((rc == LDAP_NAMING_VIOLATION) ||
3391 (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
3392 (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
3393 const char *attrs[] = { "sambaGroupType", "description",
3394 "displayName", "sambaSIDList",
3395 "gidNumber", NULL };
3397 /* Third try. This is a post-3.0.21 alias (containing only
3398 * sambaSidEntry and sambaGroupMapping classes), we also have
3399 * to delete the gidNumber attribute, only the sambaSidEntry
3402 rc = ldapsam_delete_entry(priv, mem_ctx, entry,
3403 LDAP_OBJ_GROUPMAP, attrs);
3406 result = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3409 TALLOC_FREE(mem_ctx);
3413 /**********************************************************************
3414 *********************************************************************/
3416 static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
3419 struct ldapsam_privates *ldap_state =
3420 (struct ldapsam_privates *)my_methods->private_data;
3421 char *filter = NULL;
3423 const char **attr_list;
3425 filter = talloc_asprintf(NULL, "(objectclass=%s)", LDAP_OBJ_GROUPMAP);
3427 return NT_STATUS_NO_MEMORY;
3429 attr_list = get_attr_list( NULL, groupmap_attr_list );
3430 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3431 LDAP_SCOPE_SUBTREE, filter,
3432 attr_list, 0, &ldap_state->result);
3433 TALLOC_FREE(attr_list);
3435 if (rc != LDAP_SUCCESS) {
3436 DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
3437 ldap_err2string(rc)));
3438 DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
3439 lp_ldap_suffix(), filter));
3440 ldap_msgfree(ldap_state->result);
3441 ldap_state->result = NULL;
3442 TALLOC_FREE(filter);
3443 return NT_STATUS_UNSUCCESSFUL;
3446 TALLOC_FREE(filter);
3448 DEBUG(2, ("ldapsam_setsamgrent: %d entries in the base!\n",
3449 ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3450 ldap_state->result)));
3453 ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3454 ldap_state->result);
3455 ldap_state->index = 0;
3457 return NT_STATUS_OK;
3460 /**********************************************************************
3461 *********************************************************************/
3463 static void ldapsam_endsamgrent(struct pdb_methods *my_methods)
3465 ldapsam_endsampwent(my_methods);
3468 /**********************************************************************
3469 *********************************************************************/
3471 static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
3474 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
3475 struct ldapsam_privates *ldap_state =
3476 (struct ldapsam_privates *)my_methods->private_data;
3480 if (!ldap_state->entry)
3483 ldap_state->index++;
3484 bret = init_group_from_ldap(ldap_state, map,
3488 ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
3492 return NT_STATUS_OK;
3495 /**********************************************************************
3496 *********************************************************************/
3498 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
3499 const DOM_SID *domsid, enum lsa_SidType sid_name_use,
3500 GROUP_MAP **pp_rmap,
3501 size_t *p_num_entries,
3510 if (!NT_STATUS_IS_OK(ldapsam_setsamgrent(methods, False))) {
3511 DEBUG(0, ("ldapsam_enum_group_mapping: Unable to open "
3513 return NT_STATUS_ACCESS_DENIED;
3516 while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
3517 if (sid_name_use != SID_NAME_UNKNOWN &&
3518 sid_name_use != map.sid_name_use) {
3519 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3520 "not of the requested type\n", map.nt_name));
3523 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
3524 DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
3525 "non mapped\n", map.nt_name));
3529 (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
3531 DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
3532 "enlarge group map!\n"));
3533 return NT_STATUS_UNSUCCESSFUL;
3536 (*pp_rmap)[entries] = map;
3541 ldapsam_endsamgrent(methods);
3543 *p_num_entries = entries;
3545 return NT_STATUS_OK;
3548 static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
3549 const DOM_SID *alias,
3550 const DOM_SID *member,
3553 struct ldapsam_privates *ldap_state =
3554 (struct ldapsam_privates *)methods->private_data;
3556 LDAPMessage *result = NULL;
3557 LDAPMessage *entry = NULL;
3559 LDAPMod **mods = NULL;
3561 enum lsa_SidType type = SID_NAME_USE_NONE;
3564 char *filter = NULL;
3566 if (sid_check_is_in_builtin(alias)) {
3567 type = SID_NAME_ALIAS;
3570 if (sid_check_is_in_our_domain(alias)) {
3571 type = SID_NAME_ALIAS;
3574 if (type == SID_NAME_USE_NONE) {
3575 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3576 sid_string_dbg(alias)));
3577 return NT_STATUS_NO_SUCH_ALIAS;
3580 if (asprintf(&filter,
3581 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3582 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3584 return NT_STATUS_NO_MEMORY;
3587 if (ldapsam_search_one_group(ldap_state, filter,
3588 &result) != LDAP_SUCCESS) {
3590 return NT_STATUS_NO_SUCH_ALIAS;
3593 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3597 DEBUG(4, ("ldapsam_modify_aliasmem: Did not find alias\n"));
3598 ldap_msgfree(result);
3600 return NT_STATUS_NO_SUCH_ALIAS;
3604 DEBUG(1, ("ldapsam_modify_aliasmem: Duplicate entries for "
3605 "filter %s: count=%d\n", filter, count));
3606 ldap_msgfree(result);
3608 return NT_STATUS_NO_SUCH_ALIAS;
3613 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3617 ldap_msgfree(result);
3618 return NT_STATUS_UNSUCCESSFUL;
3621 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
3623 ldap_msgfree(result);
3624 return NT_STATUS_UNSUCCESSFUL;
3627 smbldap_set_mod(&mods, modop,
3628 get_attr_key2string(groupmap_attr_list,
3629 LDAP_ATTR_SID_LIST),
3630 sid_to_fstring(tmp, member));
3632 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
3634 ldap_mods_free(mods, True);
3635 ldap_msgfree(result);
3638 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
3639 return NT_STATUS_MEMBER_IN_ALIAS;
3642 if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
3643 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
3646 if (rc != LDAP_SUCCESS) {
3647 return NT_STATUS_UNSUCCESSFUL;
3650 return NT_STATUS_OK;
3653 static NTSTATUS ldapsam_add_aliasmem(struct pdb_methods *methods,
3654 const DOM_SID *alias,
3655 const DOM_SID *member)
3657 return ldapsam_modify_aliasmem(methods, alias, member, LDAP_MOD_ADD);
3660 static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
3661 const DOM_SID *alias,
3662 const DOM_SID *member)
3664 return ldapsam_modify_aliasmem(methods, alias, member,
3668 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
3669 const DOM_SID *alias,
3670 DOM_SID **pp_members,
3671 size_t *p_num_members)
3673 struct ldapsam_privates *ldap_state =
3674 (struct ldapsam_privates *)methods->private_data;
3675 LDAPMessage *result = NULL;
3676 LDAPMessage *entry = NULL;
3678 char **values = NULL;
3680 char *filter = NULL;
3681 size_t num_members = 0;
3682 enum lsa_SidType type = SID_NAME_USE_NONE;
3688 if (sid_check_is_in_builtin(alias)) {
3689 type = SID_NAME_ALIAS;
3692 if (sid_check_is_in_our_domain(alias)) {
3693 type = SID_NAME_ALIAS;
3696 if (type == SID_NAME_USE_NONE) {
3697 DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
3698 sid_string_dbg(alias)));
3699 return NT_STATUS_NO_SUCH_ALIAS;
3702 if (asprintf(&filter,
3703 "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
3704 LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
3706 return NT_STATUS_NO_MEMORY;
3709 if (ldapsam_search_one_group(ldap_state, filter,
3710 &result) != LDAP_SUCCESS) {
3712 return NT_STATUS_NO_SUCH_ALIAS;
3715 count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
3719 DEBUG(4, ("ldapsam_enum_aliasmem: Did not find alias\n"));
3720 ldap_msgfree(result);
3722 return NT_STATUS_NO_SUCH_ALIAS;
3726 DEBUG(1, ("ldapsam_enum_aliasmem: Duplicate entries for "
3727 "filter %s: count=%d\n", filter, count));
3728 ldap_msgfree(result);
3730 return NT_STATUS_NO_SUCH_ALIAS;
3735 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
3739 ldap_msgfree(result);
3740 return NT_STATUS_UNSUCCESSFUL;
3743 values = ldap_get_values(ldap_state->smbldap_state->ldap_struct,
3745 get_attr_key2string(groupmap_attr_list,
3746 LDAP_ATTR_SID_LIST));
3748 if (values == NULL) {
3749 ldap_msgfree(result);
3750 return NT_STATUS_OK;
3753 count = ldap_count_values(values);
3755 for (i=0; i<count; i++) {
3759 if (!string_to_sid(&member, values[i]))
3762 status = add_sid_to_array(NULL, &member, pp_members,
3764 if (!NT_STATUS_IS_OK(status)) {
3765 ldap_value_free(values);
3766 ldap_msgfree(result);
3771 *p_num_members = num_members;
3772 ldap_value_free(values);
3773 ldap_msgfree(result);
3775 return NT_STATUS_OK;
3778 static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
3779 TALLOC_CTX *mem_ctx,
3780 const DOM_SID *domain_sid,
3781 const DOM_SID *members,
3783 uint32 **pp_alias_rids,
3784 size_t *p_num_alias_rids)
3786 struct ldapsam_privates *ldap_state =
3787 (struct ldapsam_privates *)methods->private_data;
3790 const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
3792 LDAPMessage *result = NULL;
3793 LDAPMessage *entry = NULL;
3797 enum lsa_SidType type = SID_NAME_USE_NONE;
3798 bool is_builtin = false;
3799 bool sid_added = false;
3801 if (sid_check_is_builtin(domain_sid)) {
3803 type = SID_NAME_ALIAS;
3806 if (sid_check_is_domain(domain_sid)) {
3807 type = SID_NAME_ALIAS;
3810 if (type == SID_NAME_USE_NONE) {
3811 DEBUG(5, ("SID %s is neither builtin nor domain!\n",
3812 sid_string_dbg(domain_sid)));
3813 return NT_STATUS_UNSUCCESSFUL;
3816 filter = talloc_asprintf(mem_ctx,
3817 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
3818 LDAP_OBJ_GROUPMAP, type);
3820 for (i=0; i<num_members; i++)
3821 filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
3823 sid_string_talloc(mem_ctx,
3826 filter = talloc_asprintf(mem_ctx, "%s))", filter);
3828 if (filter == NULL) {
3829 return NT_STATUS_NO_MEMORY;
3833 ldap_state->search_cache.filter &&
3834 strcmp(ldap_state->search_cache.filter, filter) == 0) {
3835 filter = talloc_move(filter, &ldap_state->search_cache.filter);
3836 result = ldap_state->search_cache.result;
3837 ldap_state->search_cache.result = NULL;
3839 rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
3840 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
3841 if (rc != LDAP_SUCCESS) {
3842 return NT_STATUS_UNSUCCESSFUL;
3844 talloc_autofree_ldapmsg(filter, result);
3847 ldap_struct = ldap_state->smbldap_state->ldap_struct;
3849 for (entry = ldap_first_entry(ldap_struct, result);
3851 entry = ldap_next_entry(ldap_struct, entry))
3857 if (!smbldap_get_single_attribute(ldap_struct, entry,
3863 if (!string_to_sid(&sid, sid_str))
3866 if (!sid_peek_check_rid(domain_sid, &sid, &rid))
3871 if (!add_rid_to_array_unique(mem_ctx, rid, pp_alias_rids,
3872 p_num_alias_rids)) {
3873 return NT_STATUS_NO_MEMORY;
3877 if (!is_builtin && !sid_added) {
3878 TALLOC_FREE(ldap_state->search_cache.filter);
3880 * Note: result is a talloc child of filter because of the
3881 * talloc_autofree_ldapmsg() usage
3883 ldap_state->search_cache.filter = talloc_move(ldap_state, &filter);
3884 ldap_state->search_cache.result = result;
3887 return NT_STATUS_OK;
3890 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
3894 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3896 LDAPMod **mods = NULL;
3897 fstring value_string;
3898 const char *policy_attr = NULL;
3900 struct ldapsam_privates *ldap_state =
3901 (struct ldapsam_privates *)methods->private_data;
3903 DEBUG(10,("ldapsam_set_account_policy_in_ldap\n"));
3905 if (!ldap_state->domain_dn) {
3906 return NT_STATUS_INVALID_PARAMETER;
3909 policy_attr = get_account_policy_attr(policy_index);
3910 if (policy_attr == NULL) {
3911 DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
3916 slprintf(value_string, sizeof(value_string) - 1, "%i", value);
3918 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, policy_attr, value_string);
3920 rc = smbldap_modify(ldap_state->smbldap_state, ldap_state->domain_dn,
3923 ldap_mods_free(mods, True);
3925 if (rc != LDAP_SUCCESS) {
3929 if (!cache_account_policy_set(policy_index, value)) {
3930 DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
3931 "update local tdb cache\n"));
3935 return NT_STATUS_OK;
3938 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
3939 int policy_index, uint32 value)
3941 return ldapsam_set_account_policy_in_ldap(methods, policy_index,
3945 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
3949 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
3950 LDAPMessage *result = NULL;
3951 LDAPMessage *entry = NULL;
3956 const char *policy_attr = NULL;
3958 struct ldapsam_privates *ldap_state =
3959 (struct ldapsam_privates *)methods->private_data;
3961 const char *attrs[2];
3963 DEBUG(10,("ldapsam_get_account_policy_from_ldap\n"));
3965 if (!ldap_state->domain_dn) {
3966 return NT_STATUS_INVALID_PARAMETER;
3969 policy_attr = get_account_policy_attr(policy_index);
3971 DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
3972 "policy index: %d\n", policy_index));
3976 attrs[0] = policy_attr;
3979 filter = talloc_asprintf(NULL, "(objectClass=%s)", LDAP_OBJ_DOMINFO);
3980 if (filter == NULL) {
3981 return NT_STATUS_NO_MEMORY;
3983 rc = smbldap_search(ldap_state->smbldap_state, ldap_state->domain_dn,
3984 LDAP_SCOPE_BASE, filter, attrs, 0,
3987 if (rc != LDAP_SUCCESS) {
3991 count = ldap_count_entries(priv2ld(ldap_state), result);
3996 entry = ldap_first_entry(priv2ld(ldap_state), result);
3997 if (entry == NULL) {
4001 vals = ldap_get_values(priv2ld(ldap_state), entry, policy_attr);
4006 *value = (uint32)atol(vals[0]);
4008 ntstatus = NT_STATUS_OK;
4012 ldap_value_free(vals);
4013 ldap_msgfree(result);
4018 /* wrapper around ldapsam_get_account_policy_from_ldap(), handles tdb as cache
4020 - if user hasn't decided to use account policies inside LDAP just reuse the
4023 - if there is a valid cache entry, return that
4024 - if there is an LDAP entry, update cache and return
4025 - otherwise set to default, update cache and return
4029 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
4030 int policy_index, uint32 *value)
4032 NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
4034 if (cache_account_policy_get(policy_index, value)) {
4035 DEBUG(11,("ldapsam_get_account_policy: got valid value from "
4037 return NT_STATUS_OK;
4040 ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
4042 if (NT_STATUS_IS_OK(ntstatus)) {
4046 DEBUG(10,("ldapsam_get_account_policy: failed to retrieve from "
4050 /* should we automagically migrate old tdb value here ? */
4051 if (account_policy_get(policy_index, value))
4054 DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
4055 "default\n", policy_index));
4058 if (!account_policy_get_default(policy_index, value)) {
4064 ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
4065 if (!NT_STATUS_IS_OK(ntstatus)) {
4071 if (!cache_account_policy_set(policy_index, *value)) {
4072 DEBUG(0,("ldapsam_get_account_policy: failed to update local "
4073 "tdb as a cache\n"));
4074 return NT_STATUS_UNSUCCESSFUL;
4077 return NT_STATUS_OK;
4080 static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
4081 const DOM_SID *domain_sid,
4085 enum lsa_SidType *attrs)
4087 struct ldapsam_privates *ldap_state =
4088 (struct ldapsam_privates *)methods->private_data;
4089 LDAPMessage *msg = NULL;
4091 char *allsids = NULL;
4092 int i, rc, num_mapped;
4093 NTSTATUS result = NT_STATUS_NO_MEMORY;
4094 TALLOC_CTX *mem_ctx;
4098 mem_ctx = talloc_new(NULL);
4099 if (mem_ctx == NULL) {
4100 DEBUG(0, ("talloc_new failed\n"));
4104 if (!sid_check_is_builtin(domain_sid) &&
4105 !sid_check_is_domain(domain_sid)) {
4106 result = NT_STATUS_INVALID_PARAMETER;
4110 for (i=0; i<num_rids; i++)
4111 attrs[i] = SID_NAME_UNKNOWN;
4113 allsids = talloc_strdup(mem_ctx, "");
4114 if (allsids == NULL) {
4118 for (i=0; i<num_rids; i++) {
4120 sid_compose(&sid, domain_sid, rids[i]);
4121 allsids = talloc_asprintf_append_buffer(
4122 allsids, "(sambaSid=%s)",
4123 sid_string_talloc(mem_ctx, &sid));
4124 if (allsids == NULL) {
4129 /* First look for users */
4133 const char *ldap_attrs[] = { "uid", "sambaSid", NULL };
4135 filter = talloc_asprintf(
4136 mem_ctx, ("(&(objectClass=%s)(|%s))"),
4137 LDAP_OBJ_SAMBASAMACCOUNT, allsids);
4139 if (filter == NULL) {
4143 rc = smbldap_search(ldap_state->smbldap_state,
4144 lp_ldap_user_suffix(),
4145 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4147 talloc_autofree_ldapmsg(mem_ctx, msg);
4150 if (rc != LDAP_SUCCESS)
4153 ld = ldap_state->smbldap_state->ldap_struct;
4156 for (entry = ldap_first_entry(ld, msg);
4158 entry = ldap_next_entry(ld, entry)) {
4163 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4165 DEBUG(2, ("Could not find sid from ldap entry\n"));
4169 name = smbldap_talloc_single_attribute(ld, entry, "uid",
4172 DEBUG(2, ("Could not retrieve uid attribute\n"));
4176 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4177 if (rid == rids[rid_index])
4181 if (rid_index == num_rids) {
4182 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4186 attrs[rid_index] = SID_NAME_USER;
4187 names[rid_index] = name;
4191 if (num_mapped == num_rids) {
4192 /* No need to look for groups anymore -- we're done */
4193 result = NT_STATUS_OK;
4197 /* Same game for groups */
4201 const char *ldap_attrs[] = { "cn", "displayName", "sambaSid",
4202 "sambaGroupType", NULL };
4204 filter = talloc_asprintf(
4205 mem_ctx, "(&(objectClass=%s)(|%s))",
4206 LDAP_OBJ_GROUPMAP, allsids);
4207 if (filter == NULL) {
4211 rc = smbldap_search(ldap_state->smbldap_state,
4213 LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
4215 talloc_autofree_ldapmsg(mem_ctx, msg);
4218 if (rc != LDAP_SUCCESS)
4221 /* ldap_struct might have changed due to a reconnect */
4223 ld = ldap_state->smbldap_state->ldap_struct;
4225 /* For consistency checks, we already checked we're only domain or builtin */
4227 is_builtin = sid_check_is_builtin(domain_sid);
4229 for (entry = ldap_first_entry(ld, msg);
4231 entry = ldap_next_entry(ld, entry))
4236 enum lsa_SidType type;
4237 const char *dn = smbldap_talloc_dn(mem_ctx, ld, entry);
4239 attr = smbldap_talloc_single_attribute(ld, entry, "sambaGroupType",
4242 DEBUG(2, ("Could not extract type from ldap entry %s\n",
4247 type = (enum lsa_SidType)atol(attr);
4249 /* Consistency checks */
4250 if ((is_builtin && (type != SID_NAME_ALIAS)) ||
4251 (!is_builtin && ((type != SID_NAME_ALIAS) &&
4252 (type != SID_NAME_DOM_GRP)))) {
4253 DEBUG(2, ("Rejecting invalid group mapping entry %s\n", dn));
4256 if (!ldapsam_extract_rid_from_entry(ld, entry, domain_sid,
4258 DEBUG(2, ("Could not find sid from ldap entry %s\n", dn));
4262 attr = smbldap_talloc_single_attribute(ld, entry, "displayName", names);
4265 DEBUG(10, ("Could not retrieve 'displayName' attribute from %s\n",
4267 attr = smbldap_talloc_single_attribute(ld, entry, "cn", names);
4271 DEBUG(2, ("Could not retrieve naming attribute from %s\n",
4276 for (rid_index = 0; rid_index < num_rids; rid_index++) {
4277 if (rid == rids[rid_index])
4281 if (rid_index == num_rids) {
4282 DEBUG(2, ("Got a RID not asked for: %d\n", rid));
4286 attrs[rid_index] = type;
4287 names[rid_index] = attr;
4291 result = NT_STATUS_NONE_MAPPED;
4294 result = (num_mapped == num_rids) ?
4295 NT_STATUS_OK : STATUS_SOME_UNMAPPED;
4297 TALLOC_FREE(mem_ctx);
4301 static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
4303 char *filter = NULL;
4304 char *escaped = NULL;
4305 char *result = NULL;
4307 if (asprintf(&filter, "(&%s(objectclass=%s))",
4308 "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
4312 escaped = escape_ldap_string_alloc(username);
4313 if (escaped == NULL) goto done;
4315 result = talloc_string_sub(mem_ctx, filter, "%u", username);
4324 const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
4328 const char **result;
4330 va_start(ap, mem_ctx);
4331 while (va_arg(ap, const char *) != NULL)
4335 if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
4339 va_start(ap, mem_ctx);
4340 for (i=0; i<num; i++) {
4341 result[i] = talloc_strdup(result, va_arg(ap, const char*));
4342 if (result[i] == NULL) {
4343 talloc_free(result);
4354 struct ldap_search_state {
4355 struct smbldap_state *connection;
4365 void *pagedresults_cookie;
4367 LDAPMessage *entries, *current_entry;
4368 bool (*ldap2displayentry)(struct ldap_search_state *state,
4369 TALLOC_CTX *mem_ctx,
4370 LDAP *ld, LDAPMessage *entry,
4371 struct samr_displayentry *result);
4374 static bool ldapsam_search_firstpage(struct pdb_search *search)
4376 struct ldap_search_state *state =
4377 (struct ldap_search_state *)search->private_data;
4379 int rc = LDAP_OPERATIONS_ERROR;
4381 state->entries = NULL;
4383 if (state->connection->paged_results) {
4384 rc = smbldap_search_paged(state->connection, state->base,
4385 state->scope, state->filter,
4386 state->attrs, state->attrsonly,
4387 lp_ldap_page_size(), &state->entries,
4388 &state->pagedresults_cookie);
4391 if ((rc != LDAP_SUCCESS) || (state->entries == NULL)) {
4393 if (state->entries != NULL) {
4394 /* Left over from unsuccessful paged attempt */
4395 ldap_msgfree(state->entries);
4396 state->entries = NULL;
4399 rc = smbldap_search(state->connection, state->base,
4400 state->scope, state->filter, state->attrs,
4401 state->attrsonly, &state->entries);
4403 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4406 /* Ok, the server was lying. It told us it could do paged
4407 * searches when it could not. */
4408 state->connection->paged_results = False;
4411 ld = state->connection->ldap_struct;
4413 DEBUG(5, ("Don't have an LDAP connection right after a "
4417 state->current_entry = ldap_first_entry(ld, state->entries);
4419 if (state->current_entry == NULL) {
4420 ldap_msgfree(state->entries);
4421 state->entries = NULL;
4428 static bool ldapsam_search_nextpage(struct pdb_search *search)
4430 struct ldap_search_state *state =
4431 (struct ldap_search_state *)search->private_data;
4434 if (!state->connection->paged_results) {
4435 /* There is no next page when there are no paged results */
4439 rc = smbldap_search_paged(state->connection, state->base,
4440 state->scope, state->filter, state->attrs,
4441 state->attrsonly, lp_ldap_page_size(),
4443 &state->pagedresults_cookie);
4445 if ((rc != LDAP_SUCCESS) || (state->entries == NULL))
4448 state->current_entry = ldap_first_entry(state->connection->ldap_struct, state->entries);
4450 if (state->current_entry == NULL) {
4451 ldap_msgfree(state->entries);
4452 state->entries = NULL;
4459 static bool ldapsam_search_next_entry(struct pdb_search *search,
4460 struct samr_displayentry *entry)
4462 struct ldap_search_state *state =
4463 (struct ldap_search_state *)search->private_data;
4467 if ((state->entries == NULL) && (state->pagedresults_cookie == NULL))
4470 if ((state->entries == NULL) &&
4471 !ldapsam_search_nextpage(search))
4474 result = state->ldap2displayentry(state, search,
4475 state->connection->ldap_struct,
4476 state->current_entry, entry);
4480 dn = ldap_get_dn(state->connection->ldap_struct, state->current_entry);
4481 DEBUG(5, ("Skipping entry %s\n", dn != NULL ? dn : "<NULL>"));
4482 if (dn != NULL) ldap_memfree(dn);
4485 state->current_entry = ldap_next_entry(state->connection->ldap_struct, state->current_entry);
4487 if (state->current_entry == NULL) {
4488 ldap_msgfree(state->entries);
4489 state->entries = NULL;
4492 if (!result) goto retry;
4497 static void ldapsam_search_end(struct pdb_search *search)
4499 struct ldap_search_state *state =
4500 (struct ldap_search_state *)search->private_data;
4503 if (state->pagedresults_cookie == NULL)
4506 if (state->entries != NULL)
4507 ldap_msgfree(state->entries);
4509 state->entries = NULL;
4510 state->current_entry = NULL;
4512 if (!state->connection->paged_results)
4515 /* Tell the LDAP server we're not interested in the rest anymore. */
4517 rc = smbldap_search_paged(state->connection, state->base, state->scope,
4518 state->filter, state->attrs,
4519 state->attrsonly, 0, &state->entries,
4520 &state->pagedresults_cookie);
4522 if (rc != LDAP_SUCCESS)
4523 DEBUG(5, ("Could not end search properly\n"));
4528 static bool ldapuser2displayentry(struct ldap_search_state *state,
4529 TALLOC_CTX *mem_ctx,
4530 LDAP *ld, LDAPMessage *entry,
4531 struct samr_displayentry *result)
4534 size_t converted_size;
4538 vals = ldap_get_values(ld, entry, "sambaAcctFlags");
4539 if ((vals == NULL) || (vals[0] == NULL)) {
4540 DEBUG(5, ("\"sambaAcctFlags\" not found\n"));
4543 acct_flags = pdb_decode_acct_ctrl(vals[0]);
4544 ldap_value_free(vals);
4546 if ((state->acct_flags != 0) &&
4547 ((state->acct_flags & acct_flags) == 0))
4550 result->acct_flags = acct_flags;
4551 result->account_name = "";
4552 result->fullname = "";
4553 result->description = "";
4555 vals = ldap_get_values(ld, entry, "uid");
4556 if ((vals == NULL) || (vals[0] == NULL)) {
4557 DEBUG(5, ("\"uid\" not found\n"));
4560 if (!pull_utf8_talloc(mem_ctx,
4561 CONST_DISCARD(char **, &result->account_name),
4562 vals[0], &converted_size))
4564 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4568 ldap_value_free(vals);
4570 vals = ldap_get_values(ld, entry, "displayName");
4571 if ((vals == NULL) || (vals[0] == NULL))
4572 DEBUG(8, ("\"displayName\" not found\n"));
4573 else if (!pull_utf8_talloc(mem_ctx,
4574 CONST_DISCARD(char **, &result->fullname),
4575 vals[0], &converted_size))
4577 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4581 ldap_value_free(vals);
4583 vals = ldap_get_values(ld, entry, "description");
4584 if ((vals == NULL) || (vals[0] == NULL))
4585 DEBUG(8, ("\"description\" not found\n"));
4586 else if (!pull_utf8_talloc(mem_ctx,
4587 CONST_DISCARD(char **, &result->description),
4588 vals[0], &converted_size))
4590 DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
4594 ldap_value_free(vals);
4596 if ((result->account_name == NULL) ||
4597 (result->fullname == NULL) ||
4598 (result->description == NULL)) {
4599 DEBUG(0, ("talloc failed\n"));
4603 vals = ldap_get_values(ld, entry, "sambaSid");
4604 if ((vals == NULL) || (vals[0] == NULL)) {
4605 DEBUG(0, ("\"objectSid\" not found\n"));
4609 if (!string_to_sid(&sid, vals[0])) {
4610 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4611 ldap_value_free(vals);
4614 ldap_value_free(vals);
4616 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
4617 DEBUG(0, ("sid %s does not belong to our domain\n",
4618 sid_string_dbg(&sid)));
4626 static bool ldapsam_search_users(struct pdb_methods *methods,
4627 struct pdb_search *search,
4630 struct ldapsam_privates *ldap_state =
4631 (struct ldapsam_privates *)methods->private_data;
4632 struct ldap_search_state *state;
4634 state = talloc(search, struct ldap_search_state);
4635 if (state == NULL) {
4636 DEBUG(0, ("talloc failed\n"));
4640 state->connection = ldap_state->smbldap_state;
4642 if ((acct_flags != 0) && ((acct_flags & ACB_NORMAL) != 0))
4643 state->base = lp_ldap_user_suffix();
4644 else if ((acct_flags != 0) &&
4645 ((acct_flags & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) != 0))
4646 state->base = lp_ldap_machine_suffix();
4648 state->base = lp_ldap_suffix();
4650 state->acct_flags = acct_flags;
4651 state->base = talloc_strdup(search, state->base);
4652 state->scope = LDAP_SCOPE_SUBTREE;
4653 state->filter = get_ldap_filter(search, "*");
4654 state->attrs = talloc_attrs(search, "uid", "sambaSid",
4655 "displayName", "description",
4656 "sambaAcctFlags", NULL);
4657 state->attrsonly = 0;
4658 state->pagedresults_cookie = NULL;
4659 state->entries = NULL;
4660 state->ldap2displayentry = ldapuser2displayentry;
4662 if ((state->filter == NULL) || (state->attrs == NULL)) {
4663 DEBUG(0, ("talloc failed\n"));
4667 search->private_data = state;
4668 search->next_entry = ldapsam_search_next_entry;
4669 search->search_end = ldapsam_search_end;
4671 return ldapsam_search_firstpage(search);
4674 static bool ldapgroup2displayentry(struct ldap_search_state *state,
4675 TALLOC_CTX *mem_ctx,
4676 LDAP *ld, LDAPMessage *entry,
4677 struct samr_displayentry *result)
4680 size_t converted_size;
4684 result->account_name = "";
4685 result->fullname = "";
4686 result->description = "";
4689 vals = ldap_get_values(ld, entry, "sambaGroupType");
4690 if ((vals == NULL) || (vals[0] == NULL)) {
4691 DEBUG(5, ("\"sambaGroupType\" not found\n"));
4693 ldap_value_free(vals);
4698 group_type = atoi(vals[0]);
4700 if ((state->group_type != 0) &&
4701 ((state->group_type != group_type))) {
4702 ldap_value_free(vals);
4706 ldap_value_free(vals);
4708 /* display name is the NT group name */
4710 vals = ldap_get_values(ld, entry, "displayName");
4711 if ((vals == NULL) || (vals[0] == NULL)) {
4712 DEBUG(8, ("\"displayName\" not found\n"));
4714 /* fallback to the 'cn' attribute */
4715 vals = ldap_get_values(ld, entry, "cn");
4716 if ((vals == NULL) || (vals[0] == NULL)) {
4717 DEBUG(5, ("\"cn\" not found\n"));
4720 if (!pull_utf8_talloc(mem_ctx,
4721 CONST_DISCARD(char **,
4722 &result->account_name),
4723 vals[0], &converted_size))
4725 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
4726 "failed: %s", strerror(errno)));
4729 else if (!pull_utf8_talloc(mem_ctx,
4730 CONST_DISCARD(char **,
4731 &result->account_name),
4732 vals[0], &converted_size))
4734 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4738 ldap_value_free(vals);
4740 vals = ldap_get_values(ld, entry, "description");
4741 if ((vals == NULL) || (vals[0] == NULL))
4742 DEBUG(8, ("\"description\" not found\n"));
4743 else if (!pull_utf8_talloc(mem_ctx,
4744 CONST_DISCARD(char **, &result->description),
4745 vals[0], &converted_size))
4747 DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
4750 ldap_value_free(vals);
4752 if ((result->account_name == NULL) ||
4753 (result->fullname == NULL) ||
4754 (result->description == NULL)) {
4755 DEBUG(0, ("talloc failed\n"));
4759 vals = ldap_get_values(ld, entry, "sambaSid");
4760 if ((vals == NULL) || (vals[0] == NULL)) {
4761 DEBUG(0, ("\"objectSid\" not found\n"));
4763 ldap_value_free(vals);
4768 if (!string_to_sid(&sid, vals[0])) {
4769 DEBUG(0, ("Could not convert %s to SID\n", vals[0]));
4773 ldap_value_free(vals);
4775 switch (group_type) {
4776 case SID_NAME_DOM_GRP:
4777 case SID_NAME_ALIAS:
4779 if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)
4780 && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid))
4782 DEBUG(0, ("%s is not in our domain\n",
4783 sid_string_dbg(&sid)));
4789 DEBUG(0,("unkown group type: %d\n", group_type));
4793 result->acct_flags = 0;
4798 static bool ldapsam_search_grouptype(struct pdb_methods *methods,
4799 struct pdb_search *search,
4801 enum lsa_SidType type)
4803 struct ldapsam_privates *ldap_state =
4804 (struct ldapsam_privates *)methods->private_data;
4805 struct ldap_search_state *state;
4808 state = talloc(search, struct ldap_search_state);
4809 if (state == NULL) {
4810 DEBUG(0, ("talloc failed\n"));
4814 state->connection = ldap_state->smbldap_state;
4816 state->base = talloc_strdup(search, lp_ldap_suffix());
4817 state->connection = ldap_state->smbldap_state;
4818 state->scope = LDAP_SCOPE_SUBTREE;
4819 state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
4820 "(sambaGroupType=%d)(sambaSID=%s*))",
4822 type, sid_to_fstring(tmp, sid));
4823 state->attrs = talloc_attrs(search, "cn", "sambaSid",
4824 "displayName", "description",
4825 "sambaGroupType", NULL);
4826 state->attrsonly = 0;
4827 state->pagedresults_cookie = NULL;
4828 state->entries = NULL;
4829 state->group_type = type;
4830 state->ldap2displayentry = ldapgroup2displayentry;
4832 if ((state->filter == NULL) || (state->attrs == NULL)) {
4833 DEBUG(0, ("talloc failed\n"));
4837 search->private_data = state;
4838 search->next_entry = ldapsam_search_next_entry;
4839 search->search_end = ldapsam_search_end;
4841 return ldapsam_search_firstpage(search);
4844 static bool ldapsam_search_groups(struct pdb_methods *methods,
4845 struct pdb_search *search)
4847 return ldapsam_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
4850 static bool ldapsam_search_aliases(struct pdb_methods *methods,
4851 struct pdb_search *search,
4854 return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
4857 static bool ldapsam_rid_algorithm(struct pdb_methods *methods)
4862 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
4865 struct smbldap_state *smbldap_state = priv->smbldap_state;
4867 LDAPMessage *result = NULL;
4868 LDAPMessage *entry = NULL;
4869 LDAPMod **mods = NULL;
4876 TALLOC_CTX *mem_ctx;
4878 mem_ctx = talloc_new(NULL);
4879 if (mem_ctx == NULL) {
4880 DEBUG(0, ("talloc_new failed\n"));
4881 return NT_STATUS_NO_MEMORY;
4884 status = smbldap_search_domain_info(smbldap_state, &result,
4885 get_global_sam_name(), False);
4886 if (!NT_STATUS_IS_OK(status)) {
4887 DEBUG(3, ("Could not get domain info: %s\n",
4888 nt_errstr(status)));
4892 talloc_autofree_ldapmsg(mem_ctx, result);
4894 entry = ldap_first_entry(priv2ld(priv), result);
4895 if (entry == NULL) {
4896 DEBUG(0, ("Could not get domain info entry\n"));
4897 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
4901 /* Find the largest of the three attributes "sambaNextRid",
4902 "sambaNextGroupRid" and "sambaNextUserRid". I gave up on the
4903 concept of differentiating between user and group rids, and will
4904 use only "sambaNextRid" in the future. But for compatibility
4905 reasons I look if others have chosen different strategies -- VL */
4907 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4908 "sambaNextRid", mem_ctx);
4909 if (value != NULL) {
4910 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4911 nextRid = MAX(nextRid, tmp);
4914 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4915 "sambaNextUserRid", mem_ctx);
4916 if (value != NULL) {
4917 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4918 nextRid = MAX(nextRid, tmp);
4921 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
4922 "sambaNextGroupRid", mem_ctx);
4923 if (value != NULL) {
4924 uint32 tmp = (uint32)strtoul(value, NULL, 10);
4925 nextRid = MAX(nextRid, tmp);
4929 nextRid = BASE_RID-1;
4934 smbldap_make_mod(priv2ld(priv), entry, &mods, "sambaNextRid",
4935 talloc_asprintf(mem_ctx, "%d", nextRid));
4936 talloc_autofree_ldapmod(mem_ctx, mods);
4938 if ((dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)) == NULL) {
4939 status = NT_STATUS_NO_MEMORY;
4943 rc = smbldap_modify(smbldap_state, dn, mods);
4945 /* ACCESS_DENIED is used as a placeholder for "the modify failed,
4948 status = (rc == LDAP_SUCCESS) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4951 if (NT_STATUS_IS_OK(status)) {
4955 TALLOC_FREE(mem_ctx);
4959 static NTSTATUS ldapsam_new_rid_internal(struct pdb_methods *methods, uint32 *rid)
4963 for (i=0; i<10; i++) {
4964 NTSTATUS result = ldapsam_get_new_rid(
4965 (struct ldapsam_privates *)methods->private_data, rid);
4966 if (NT_STATUS_IS_OK(result)) {
4970 if (!NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
4974 /* The ldap update failed (maybe a race condition), retry */
4977 /* Tried 10 times, fail. */
4978 return NT_STATUS_ACCESS_DENIED;
4981 static bool ldapsam_new_rid(struct pdb_methods *methods, uint32 *rid)
4983 NTSTATUS result = ldapsam_new_rid_internal(methods, rid);
4984 return NT_STATUS_IS_OK(result) ? True : False;
4987 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
4989 union unid_t *id, enum lsa_SidType *type)
4991 struct ldapsam_privates *priv =
4992 (struct ldapsam_privates *)methods->private_data;
4994 const char *attrs[] = { "sambaGroupType", "gidNumber", "uidNumber",
4996 LDAPMessage *result = NULL;
4997 LDAPMessage *entry = NULL;
5002 TALLOC_CTX *mem_ctx;
5004 mem_ctx = talloc_new(NULL);
5005 if (mem_ctx == NULL) {
5006 DEBUG(0, ("talloc_new failed\n"));
5010 filter = talloc_asprintf(mem_ctx,
5012 "(|(objectClass=%s)(objectClass=%s)))",
5013 sid_string_talloc(mem_ctx, sid),
5014 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
5015 if (filter == NULL) {
5016 DEBUG(5, ("talloc_asprintf failed\n"));
5020 rc = smbldap_search_suffix(priv->smbldap_state, filter,
5022 if (rc != LDAP_SUCCESS) {
5025 talloc_autofree_ldapmsg(mem_ctx, result);
5027 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5028 DEBUG(10, ("Got %d entries, expected one\n",
5029 ldap_count_entries(priv2ld(priv), result)));
5033 entry = ldap_first_entry(priv2ld(priv), result);
5035 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5036 "sambaGroupType", mem_ctx);
5038 if (value != NULL) {
5039 const char *gid_str;
5042 gid_str = smbldap_talloc_single_attribute(
5043 priv2ld(priv), entry, "gidNumber", mem_ctx);
5044 if (gid_str == NULL) {
5045 DEBUG(1, ("%s has sambaGroupType but no gidNumber\n",
5046 smbldap_talloc_dn(mem_ctx, priv2ld(priv),
5051 id->gid = strtoul(gid_str, NULL, 10);
5052 *type = (enum lsa_SidType)strtoul(value, NULL, 10);
5053 store_gid_sid_cache(sid, id->gid);
5054 idmap_cache_set_sid2gid(sid, id->gid);
5059 /* It must be a user */
5061 value = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5062 "uidNumber", mem_ctx);
5063 if (value == NULL) {
5064 DEBUG(1, ("Could not find uidNumber in %s\n",
5065 smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry)));
5069 id->uid = strtoul(value, NULL, 10);
5070 *type = SID_NAME_USER;
5071 store_uid_sid_cache(sid, id->uid);
5072 idmap_cache_set_sid2uid(sid, id->uid);
5076 TALLOC_FREE(mem_ctx);
5081 * Find the SID for a uid.
5082 * This is shortcut is only used if ldapsam:trusted is set to true.
5084 static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
5087 struct ldapsam_privates *priv =
5088 (struct ldapsam_privates *)methods->private_data;
5090 const char *attrs[] = { "sambaSID", NULL };
5091 LDAPMessage *result = NULL;
5092 LDAPMessage *entry = NULL;
5094 char *user_sid_string;
5097 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5099 filter = talloc_asprintf(tmp_ctx,
5102 "(objectClass=%s))",
5104 LDAP_OBJ_POSIXACCOUNT,
5105 LDAP_OBJ_SAMBASAMACCOUNT);
5106 if (filter == NULL) {
5107 DEBUG(3, ("talloc_asprintf failed\n"));
5111 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5112 if (rc != LDAP_SUCCESS) {
5115 talloc_autofree_ldapmsg(tmp_ctx, result);
5117 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5118 DEBUG(3, ("ERROR: Got %d entries for uid %u, expected one\n",
5119 ldap_count_entries(priv2ld(priv), result),
5120 (unsigned int)uid));
5124 entry = ldap_first_entry(priv2ld(priv), result);
5126 user_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5127 "sambaSID", tmp_ctx);
5128 if (user_sid_string == NULL) {
5129 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5130 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5134 user_sid = string_sid_talloc(tmp_ctx, user_sid_string);
5135 if (user_sid == NULL) {
5136 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5141 sid_copy(sid, user_sid);
5143 store_uid_sid_cache(sid, uid);
5144 idmap_cache_set_sid2uid(sid, uid);
5149 TALLOC_FREE(tmp_ctx);
5154 * Find the SID for a gid.
5155 * This is shortcut is only used if ldapsam:trusted is set to true.
5157 static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
5160 struct ldapsam_privates *priv =
5161 (struct ldapsam_privates *)methods->private_data;
5163 const char *attrs[] = { "sambaSID", NULL };
5164 LDAPMessage *result = NULL;
5165 LDAPMessage *entry = NULL;
5167 char *group_sid_string;
5170 TALLOC_CTX *tmp_ctx = talloc_stackframe();
5172 filter = talloc_asprintf(tmp_ctx,
5174 "(objectClass=%s))",
5177 if (filter == NULL) {
5178 DEBUG(3, ("talloc_asprintf failed\n"));
5182 rc = smbldap_search_suffix(priv->smbldap_state, filter, attrs, &result);
5183 if (rc != LDAP_SUCCESS) {
5186 talloc_autofree_ldapmsg(tmp_ctx, result);
5188 if (ldap_count_entries(priv2ld(priv), result) != 1) {
5189 DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
5190 ldap_count_entries(priv2ld(priv), result),
5191 (unsigned int)gid));
5195 entry = ldap_first_entry(priv2ld(priv), result);
5197 group_sid_string = smbldap_talloc_single_attribute(priv2ld(priv), entry,
5198 "sambaSID", tmp_ctx);
5199 if (group_sid_string == NULL) {
5200 DEBUG(1, ("Could not find sambaSID in object '%s'\n",
5201 smbldap_talloc_dn(tmp_ctx, priv2ld(priv), entry)));
5205 group_sid = string_sid_talloc(tmp_ctx, group_sid_string);
5206 if (group_sid == NULL) {
5207 DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
5212 sid_copy(sid, group_sid);
5214 store_gid_sid_cache(sid, gid);
5215 idmap_cache_set_sid2gid(sid, gid);
5220 TALLOC_FREE(tmp_ctx);
5226 * The following functions is called only if
5227 * ldapsam:trusted and ldapsam:editposix are
5232 * ldapsam_create_user creates a new
5233 * posixAccount and sambaSamAccount object
5234 * in the ldap users subtree
5236 * The uid is allocated by winbindd.
5239 static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
5240 TALLOC_CTX *tmp_ctx, const char *name,
5241 uint32 acb_info, uint32 *rid)
5243 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5244 LDAPMessage *entry = NULL;
5245 LDAPMessage *result = NULL;
5247 bool is_machine = False;
5248 bool add_posix = False;
5249 LDAPMod **mods = NULL;
5257 const char *dn = NULL;
5265 if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
5266 acb_info & ACB_WSTRUST ||
5267 acb_info & ACB_SVRTRUST ||
5268 acb_info & ACB_DOMTRUST) {
5272 username = escape_ldap_string_alloc(name);
5273 filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
5274 username, LDAP_OBJ_POSIXACCOUNT);
5275 SAFE_FREE(username);
5277 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5278 if (rc != LDAP_SUCCESS) {
5279 DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
5280 return NT_STATUS_ACCESS_DENIED;
5282 talloc_autofree_ldapmsg(tmp_ctx, result);
5284 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5286 if (num_result > 1) {
5287 DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
5288 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5291 if (num_result == 1) {
5293 /* check if it is just a posix account.
5294 * or if there is a sid attached to this entry
5297 entry = ldap_first_entry(priv2ld(ldap_state), result);
5299 return NT_STATUS_UNSUCCESSFUL;
5302 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5304 DEBUG (1, ("ldapsam_create_user: The user [%s] already exist!\n", name));
5305 return NT_STATUS_USER_EXISTS;
5308 /* it is just a posix account, retrieve the dn for later use */
5309 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5311 DEBUG(0,("ldapsam_create_user: Out of memory!\n"));
5312 return NT_STATUS_NO_MEMORY;
5316 if (num_result == 0) {
5320 /* Create the basic samu structure and generate the mods for the ldap commit */
5321 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5322 DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
5326 sid_compose(&user_sid, get_global_sam_sid(), *rid);
5328 user = samu_new(tmp_ctx);
5330 DEBUG(1,("ldapsam_create_user: Unable to allocate user struct\n"));
5331 return NT_STATUS_NO_MEMORY;
5334 if (!pdb_set_username(user, name, PDB_SET)) {
5335 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5336 return NT_STATUS_UNSUCCESSFUL;
5338 if (!pdb_set_domain(user, get_global_sam_name(), PDB_SET)) {
5339 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5340 return NT_STATUS_UNSUCCESSFUL;
5343 if (acb_info & ACB_NORMAL) {
5344 if (!pdb_set_acct_ctrl(user, ACB_WSTRUST, PDB_SET)) {
5345 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5346 return NT_STATUS_UNSUCCESSFUL;
5349 if (!pdb_set_acct_ctrl(user, acb_info, PDB_SET)) {
5350 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5351 return NT_STATUS_UNSUCCESSFUL;
5355 if (!pdb_set_acct_ctrl(user, ACB_NORMAL | ACB_DISABLED, PDB_SET)) {
5356 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5357 return NT_STATUS_UNSUCCESSFUL;
5361 if (!pdb_set_user_sid(user, &user_sid, PDB_SET)) {
5362 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5363 return NT_STATUS_UNSUCCESSFUL;
5366 if (!init_ldap_from_sam(ldap_state, NULL, &mods, user, element_is_set_or_changed)) {
5367 DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
5368 return NT_STATUS_UNSUCCESSFUL;
5371 if (ldap_state->schema_ver != SCHEMAVER_SAMBASAMACCOUNT) {
5372 DEBUG(1,("ldapsam_create_user: Unsupported schema version\n"));
5374 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
5379 DEBUG(3,("ldapsam_create_user: Creating new posix user\n"));
5381 /* retrieve the Domain Users group gid */
5382 if (!sid_compose(&group_sid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS) ||
5383 !sid_to_gid(&group_sid, &gid)) {
5384 DEBUG (0, ("ldapsam_create_user: Unable to get the Domain Users gid: bailing out!\n"));
5385 return NT_STATUS_INVALID_PRIMARY_GROUP;
5388 /* lets allocate a new userid for this user */
5389 if (!winbind_allocate_uid(&uid)) {
5390 DEBUG (0, ("ldapsam_create_user: Unable to allocate a new user id: bailing out!\n"));
5391 return NT_STATUS_UNSUCCESSFUL;
5396 /* TODO: choose a more appropriate default for machines */
5397 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
5398 shell = talloc_strdup(tmp_ctx, "/bin/false");
5400 homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
5401 shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
5403 uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
5404 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5406 escape_name = escape_rdn_val_string_alloc(name);
5408 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5409 return NT_STATUS_NO_MEMORY;
5413 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_machine_suffix ());
5415 dn = talloc_asprintf(tmp_ctx, "uid=%s,%s", escape_name, lp_ldap_user_suffix ());
5418 SAFE_FREE(escape_name);
5420 if (!homedir || !shell || !uidstr || !gidstr || !dn) {
5421 DEBUG (0, ("ldapsam_create_user: Out of memory!\n"));
5422 return NT_STATUS_NO_MEMORY;
5425 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
5426 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
5427 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5428 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
5429 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5430 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", homedir);
5431 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
5434 talloc_autofree_ldapmod(tmp_ctx, mods);
5437 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5439 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5442 if (rc != LDAP_SUCCESS) {
5443 DEBUG(0,("ldapsam_create_user: failed to create a new user [%s] (dn = %s)\n", name ,dn));
5444 return NT_STATUS_UNSUCCESSFUL;
5447 DEBUG(2,("ldapsam_create_user: added account [%s] in the LDAP database\n", name));
5449 flush_pwnam_cache();
5451 return NT_STATUS_OK;
5454 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5455 TALLOC_CTX *tmp_ctx,
5459 static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
5460 TALLOC_CTX *mem_ctx,
5464 size_t *p_num_groups);
5466 static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, struct samu *sam_acct)
5468 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5469 LDAPMessage *result = NULL;
5470 LDAPMessage *entry = NULL;
5476 DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
5478 filter = talloc_asprintf(tmp_ctx,
5481 "(objectClass=%s))",
5482 pdb_get_username(sam_acct),
5483 LDAP_OBJ_POSIXACCOUNT,
5484 LDAP_OBJ_SAMBASAMACCOUNT);
5485 if (filter == NULL) {
5486 return NT_STATUS_NO_MEMORY;
5489 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5490 if (rc != LDAP_SUCCESS) {
5491 DEBUG(0,("ldapsam_delete_user: user search failed!\n"));
5492 return NT_STATUS_UNSUCCESSFUL;
5494 talloc_autofree_ldapmsg(tmp_ctx, result);
5496 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5498 if (num_result == 0) {
5499 DEBUG(0,("ldapsam_delete_user: user not found!\n"));
5500 return NT_STATUS_NO_SUCH_USER;
5503 if (num_result > 1) {
5504 DEBUG (0, ("ldapsam_delete_user: More than one user with name [%s] ?!\n", pdb_get_username(sam_acct)));
5505 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5508 entry = ldap_first_entry(priv2ld(ldap_state), result);
5510 return NT_STATUS_UNSUCCESSFUL;
5513 /* it is just a posix account, retrieve the dn for later use */
5514 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5516 DEBUG(0,("ldapsam_delete_user: Out of memory!\n"));
5517 return NT_STATUS_NO_MEMORY;
5520 /* try to remove memberships first */
5523 struct dom_sid *sids = NULL;
5525 size_t num_groups = 0;
5527 uint32_t user_rid = pdb_get_user_rid(sam_acct);
5529 status = ldapsam_enum_group_memberships(my_methods,
5535 if (!NT_STATUS_IS_OK(status)) {
5539 for (i=0; i < num_groups; i++) {
5543 sid_peek_rid(&sids[i], &group_rid);
5545 ldapsam_del_groupmem(my_methods,
5554 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5555 if (rc != LDAP_SUCCESS) {
5556 return NT_STATUS_UNSUCCESSFUL;
5559 flush_pwnam_cache();
5561 return NT_STATUS_OK;
5565 * ldapsam_create_group creates a new
5566 * posixGroup and sambaGroupMapping object
5567 * in the ldap groups subtree
5569 * The gid is allocated by winbindd.
5572 static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
5573 TALLOC_CTX *tmp_ctx,
5577 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5579 LDAPMessage *entry = NULL;
5580 LDAPMessage *result = NULL;
5582 bool is_new_entry = False;
5583 LDAPMod **mods = NULL;
5589 const char *dn = NULL;
5594 groupname = escape_ldap_string_alloc(name);
5595 filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
5596 groupname, LDAP_OBJ_POSIXGROUP);
5597 SAFE_FREE(groupname);
5599 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5600 if (rc != LDAP_SUCCESS) {
5601 DEBUG(0,("ldapsam_create_group: ldap search failed!\n"));
5602 return NT_STATUS_UNSUCCESSFUL;
5604 talloc_autofree_ldapmsg(tmp_ctx, result);
5606 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5608 if (num_result > 1) {
5609 DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
5610 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5613 if (num_result == 1) {
5615 /* check if it is just a posix group.
5616 * or if there is a sid attached to this entry
5619 entry = ldap_first_entry(priv2ld(ldap_state), result);
5621 return NT_STATUS_UNSUCCESSFUL;
5624 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "sambaSID", tmp_ctx);
5626 DEBUG (1, ("ldapsam_create_group: The group [%s] already exist!\n", name));
5627 return NT_STATUS_GROUP_EXISTS;
5630 /* it is just a posix group, retrieve the gid and the dn for later use */
5631 tmp = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5633 DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
5634 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5637 gid = strtoul(tmp, NULL, 10);
5639 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5641 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5642 return NT_STATUS_NO_MEMORY;
5646 if (num_result == 0) {
5649 DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
5651 is_new_entry = True;
5653 /* lets allocate a new groupid for this group */
5654 if (!winbind_allocate_gid(&gid)) {
5655 DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
5656 return NT_STATUS_UNSUCCESSFUL;
5659 gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
5661 escape_name = escape_rdn_val_string_alloc(name);
5663 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5664 return NT_STATUS_NO_MEMORY;
5667 dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", escape_name, lp_ldap_group_suffix());
5669 SAFE_FREE(escape_name);
5671 if (!gidstr || !dn) {
5672 DEBUG (0, ("ldapsam_create_group: Out of memory!\n"));
5673 return NT_STATUS_NO_MEMORY;
5676 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
5677 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
5678 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
5681 if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
5682 DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
5686 sid_compose(&group_sid, get_global_sam_sid(), *rid);
5688 groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
5690 grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
5692 if (!groupsidstr || !grouptype) {
5693 DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
5694 return NT_STATUS_NO_MEMORY;
5697 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
5698 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
5699 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
5700 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
5701 talloc_autofree_ldapmod(tmp_ctx, mods);
5704 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5706 if (rc == LDAP_OBJECT_CLASS_VIOLATION) {
5707 /* This call may fail with rfc2307bis schema */
5708 /* Retry adding a structural class */
5709 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "????");
5710 rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
5714 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5717 if (rc != LDAP_SUCCESS) {
5718 DEBUG(0,("ldapsam_create_group: failed to create a new group [%s] (dn = %s)\n", name ,dn));
5719 return NT_STATUS_UNSUCCESSFUL;
5722 DEBUG(2,("ldapsam_create_group: added group [%s] in the LDAP database\n", name));
5724 return NT_STATUS_OK;
5727 static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_CTX *tmp_ctx, uint32 rid)
5729 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5730 LDAPMessage *result = NULL;
5731 LDAPMessage *entry = NULL;
5739 /* get the group sid */
5740 sid_compose(&group_sid, get_global_sam_sid(), rid);
5742 filter = talloc_asprintf(tmp_ctx,
5745 "(objectClass=%s))",
5746 sid_string_talloc(tmp_ctx, &group_sid),
5747 LDAP_OBJ_POSIXGROUP,
5749 if (filter == NULL) {
5750 return NT_STATUS_NO_MEMORY;
5753 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5754 if (rc != LDAP_SUCCESS) {
5755 DEBUG(1,("ldapsam_delete_dom_group: group search failed!\n"));
5756 return NT_STATUS_UNSUCCESSFUL;
5758 talloc_autofree_ldapmsg(tmp_ctx, result);
5760 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5762 if (num_result == 0) {
5763 DEBUG(1,("ldapsam_delete_dom_group: group not found!\n"));
5764 return NT_STATUS_NO_SUCH_GROUP;
5767 if (num_result > 1) {
5768 DEBUG (0, ("ldapsam_delete_dom_group: More than one group with the same SID ?!\n"));
5769 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5772 entry = ldap_first_entry(priv2ld(ldap_state), result);
5774 return NT_STATUS_UNSUCCESSFUL;
5777 /* here it is, retrieve the dn for later use */
5778 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5780 DEBUG(0,("ldapsam_delete_dom_group: Out of memory!\n"));
5781 return NT_STATUS_NO_MEMORY;
5784 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5786 DEBUG (0, ("ldapsam_delete_dom_group: Unable to find the group's gid!\n"));
5787 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5790 /* check no user have this group marked as primary group */
5791 filter = talloc_asprintf(tmp_ctx,
5794 "(objectClass=%s))",
5796 LDAP_OBJ_POSIXACCOUNT,
5797 LDAP_OBJ_SAMBASAMACCOUNT);
5799 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5800 if (rc != LDAP_SUCCESS) {
5801 DEBUG(1,("ldapsam_delete_dom_group: accounts search failed!\n"));
5802 return NT_STATUS_UNSUCCESSFUL;
5804 talloc_autofree_ldapmsg(tmp_ctx, result);
5806 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5808 if (num_result != 0) {
5809 DEBUG(3,("ldapsam_delete_dom_group: Can't delete group, it is a primary group for %d users\n", num_result));
5810 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5813 rc = smbldap_delete(ldap_state->smbldap_state, dn);
5814 if (rc != LDAP_SUCCESS) {
5815 return NT_STATUS_UNSUCCESSFUL;
5818 return NT_STATUS_OK;
5821 static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
5822 TALLOC_CTX *tmp_ctx,
5827 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
5828 LDAPMessage *entry = NULL;
5829 LDAPMessage *result = NULL;
5831 LDAPMod **mods = NULL;
5834 const char *dn = NULL;
5841 DEBUG(1,("ldapsam_change_groupmem: add new member(rid=%d) to a domain group(rid=%d)", member_rid, group_rid));
5843 case LDAP_MOD_DELETE:
5844 DEBUG(1,("ldapsam_change_groupmem: delete member(rid=%d) from a domain group(rid=%d)", member_rid, group_rid));
5847 return NT_STATUS_UNSUCCESSFUL;
5850 /* get member sid */
5851 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
5853 /* get the group sid */
5854 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
5856 filter = talloc_asprintf(tmp_ctx,
5859 "(objectClass=%s))",
5860 sid_string_talloc(tmp_ctx, &member_sid),
5861 LDAP_OBJ_POSIXACCOUNT,
5862 LDAP_OBJ_SAMBASAMACCOUNT);
5863 if (filter == NULL) {
5864 return NT_STATUS_NO_MEMORY;
5867 /* get the member uid */
5868 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5869 if (rc != LDAP_SUCCESS) {
5870 DEBUG(1,("ldapsam_change_groupmem: member search failed!\n"));
5871 return NT_STATUS_UNSUCCESSFUL;
5873 talloc_autofree_ldapmsg(tmp_ctx, result);
5875 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5877 if (num_result == 0) {
5878 DEBUG(1,("ldapsam_change_groupmem: member not found!\n"));
5879 return NT_STATUS_NO_SUCH_MEMBER;
5882 if (num_result > 1) {
5883 DEBUG (0, ("ldapsam_change_groupmem: More than one account with the same SID ?!\n"));
5884 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5887 entry = ldap_first_entry(priv2ld(ldap_state), result);
5889 return NT_STATUS_UNSUCCESSFUL;
5892 if (modop == LDAP_MOD_DELETE) {
5893 /* check if we are trying to remove the member from his primary group */
5895 gid_t user_gid, group_gid;
5897 gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
5899 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
5900 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5903 user_gid = strtoul(gidstr, NULL, 10);
5905 if (!sid_to_gid(&group_sid, &group_gid)) {
5906 DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
5907 return NT_STATUS_UNSUCCESSFUL;
5910 if (user_gid == group_gid) {
5911 DEBUG (3, ("ldapsam_change_groupmem: can't remove user from its own primary group!\n"));
5912 return NT_STATUS_MEMBERS_PRIMARY_GROUP;
5916 /* here it is, retrieve the uid for later use */
5917 uidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "uid", tmp_ctx);
5919 DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's name!\n"));
5920 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5923 filter = talloc_asprintf(tmp_ctx,
5926 "(objectClass=%s))",
5927 sid_string_talloc(tmp_ctx, &group_sid),
5928 LDAP_OBJ_POSIXGROUP,
5932 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
5933 if (rc != LDAP_SUCCESS) {
5934 DEBUG(1,("ldapsam_change_groupmem: group search failed!\n"));
5935 return NT_STATUS_UNSUCCESSFUL;
5937 talloc_autofree_ldapmsg(tmp_ctx, result);
5939 num_result = ldap_count_entries(priv2ld(ldap_state), result);
5941 if (num_result == 0) {
5942 DEBUG(1,("ldapsam_change_groupmem: group not found!\n"));
5943 return NT_STATUS_NO_SUCH_GROUP;
5946 if (num_result > 1) {
5947 DEBUG (0, ("ldapsam_change_groupmem: More than one group with the same SID ?!\n"));
5948 return NT_STATUS_INTERNAL_DB_CORRUPTION;
5951 entry = ldap_first_entry(priv2ld(ldap_state), result);
5953 return NT_STATUS_UNSUCCESSFUL;
5956 /* here it is, retrieve the dn for later use */
5957 dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
5959 DEBUG(0,("ldapsam_change_groupmem: Out of memory!\n"));
5960 return NT_STATUS_NO_MEMORY;
5963 smbldap_set_mod(&mods, modop, "memberUid", uidstr);
5965 talloc_autofree_ldapmod(tmp_ctx, mods);
5967 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
5968 if (rc != LDAP_SUCCESS) {
5969 if (rc == LDAP_TYPE_OR_VALUE_EXISTS && modop == LDAP_MOD_ADD) {
5970 DEBUG(1,("ldapsam_change_groupmem: member is already in group, add failed!\n"));
5971 return NT_STATUS_MEMBER_IN_GROUP;
5973 if (rc == LDAP_NO_SUCH_ATTRIBUTE && modop == LDAP_MOD_DELETE) {
5974 DEBUG(1,("ldapsam_change_groupmem: member is not in group, delete failed!\n"));
5975 return NT_STATUS_MEMBER_NOT_IN_GROUP;
5977 return NT_STATUS_UNSUCCESSFUL;
5980 return NT_STATUS_OK;
5983 static NTSTATUS ldapsam_add_groupmem(struct pdb_methods *my_methods,
5984 TALLOC_CTX *tmp_ctx,
5988 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_ADD);
5990 static NTSTATUS ldapsam_del_groupmem(struct pdb_methods *my_methods,
5991 TALLOC_CTX *tmp_ctx,
5995 return ldapsam_change_groupmem(my_methods, tmp_ctx, group_rid, member_rid, LDAP_MOD_DELETE);
5998 static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
5999 TALLOC_CTX *mem_ctx,
6000 struct samu *sampass)
6002 struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
6003 LDAPMessage *entry = NULL;
6004 LDAPMessage *result = NULL;
6006 LDAPMod **mods = NULL;
6008 char *escape_username;
6010 const char *dn = NULL;
6014 DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
6016 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
6017 DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
6018 return NT_STATUS_UNSUCCESSFUL;
6020 gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
6022 DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
6023 return NT_STATUS_NO_MEMORY;
6026 escape_username = escape_ldap_string_alloc(pdb_get_username(sampass));
6027 if (escape_username== NULL) {
6028 return NT_STATUS_NO_MEMORY;
6031 filter = talloc_asprintf(mem_ctx,
6034 "(objectClass=%s))",
6036 LDAP_OBJ_POSIXACCOUNT,
6037 LDAP_OBJ_SAMBASAMACCOUNT);
6039 SAFE_FREE(escape_username);
6041 if (filter == NULL) {
6042 return NT_STATUS_NO_MEMORY;
6045 rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
6046 if (rc != LDAP_SUCCESS) {
6047 DEBUG(0,("ldapsam_set_primary_group: user search failed!\n"));
6048 return NT_STATUS_UNSUCCESSFUL;
6050 talloc_autofree_ldapmsg(mem_ctx, result);
6052 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6054 if (num_result == 0) {
6055 DEBUG(0,("ldapsam_set_primary_group: user not found!\n"));
6056 return NT_STATUS_NO_SUCH_USER;
6059 if (num_result > 1) {
6060 DEBUG (0, ("ldapsam_set_primary_group: More than one user with name [%s] ?!\n", pdb_get_username(sampass)));
6061 return NT_STATUS_INTERNAL_DB_CORRUPTION;
6064 entry = ldap_first_entry(priv2ld(ldap_state), result);
6066 return NT_STATUS_UNSUCCESSFUL;
6069 /* retrieve the dn for later use */
6070 dn = smbldap_talloc_dn(mem_ctx, priv2ld(ldap_state), entry);
6072 DEBUG(0,("ldapsam_set_primary_group: Out of memory!\n"));
6073 return NT_STATUS_NO_MEMORY;
6076 /* remove the old one, and add the new one, this way we do not risk races */
6077 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "gidNumber", gidstr);
6080 return NT_STATUS_OK;
6083 rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
6085 if (rc != LDAP_SUCCESS) {
6086 DEBUG(0,("ldapsam_set_primary_group: failed to modify [%s] primary group to [%s]\n",
6087 pdb_get_username(sampass), gidstr));
6088 return NT_STATUS_UNSUCCESSFUL;
6091 flush_pwnam_cache();
6093 return NT_STATUS_OK;
6097 /**********************************************************************
6098 trusted domains functions
6099 *********************************************************************/
6101 static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
6104 return talloc_asprintf(talloc_tos(), "sambaDomainName=%s,%s", domain,
6105 ldap_state->domain_dn);
6108 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
6109 TALLOC_CTX *mem_ctx,
6110 const char *domain, LDAPMessage **entry)
6114 int scope = LDAP_SCOPE_SUBTREE;
6115 const char **attrs = NULL; /* NULL: get all attrs */
6116 int attrsonly = 0; /* 0: return values too */
6117 LDAPMessage *result = NULL;
6121 filter = talloc_asprintf(talloc_tos(),
6122 "(&(objectClass=%s)(sambaDomainName=%s))",
6123 LDAP_OBJ_TRUSTDOM_PASSWORD, domain);
6125 trusted_dn = trusteddom_dn(ldap_state, domain);
6126 if (trusted_dn == NULL) {
6129 rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
6130 filter, attrs, attrsonly, &result);
6132 if (result != NULL) {
6133 talloc_autofree_ldapmsg(mem_ctx, result);
6136 if (rc == LDAP_NO_SUCH_OBJECT) {
6141 if (rc != LDAP_SUCCESS) {
6145 num_result = ldap_count_entries(priv2ld(ldap_state), result);
6147 if (num_result > 1) {
6148 DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
6149 "%s object for domain '%s'?!\n",
6150 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6154 if (num_result == 0) {
6155 DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
6156 "%s object for domain %s.\n",
6157 LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
6160 *entry = ldap_first_entry(priv2ld(ldap_state), result);
6166 static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
6170 time_t *pass_last_set_time)
6172 struct ldapsam_privates *ldap_state =
6173 (struct ldapsam_privates *)methods->private_data;
6174 LDAPMessage *entry = NULL;
6176 DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
6178 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
6187 pwd_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6188 entry, "sambaClearTextPassword", talloc_tos());
6189 if (pwd_str == NULL) {
6192 /* trusteddom_pw routines do not use talloc yet... */
6193 *pwd = SMB_STRDUP(pwd_str);
6199 /* last change time */
6200 if (pass_last_set_time != NULL) {
6202 time_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6203 entry, "sambaPwdLastSet", talloc_tos());
6204 if (time_str == NULL) {
6207 *pass_last_set_time = (time_t)atol(time_str);
6214 sid_str = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6217 if (sid_str == NULL) {
6220 dom_sid = string_sid_talloc(talloc_tos(), sid_str);
6221 if (dom_sid == NULL) {
6224 sid_copy(sid, dom_sid);
6230 static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
6235 struct ldapsam_privates *ldap_state =
6236 (struct ldapsam_privates *)methods->private_data;
6237 LDAPMessage *entry = NULL;
6238 LDAPMod **mods = NULL;
6239 char *prev_pwd = NULL;
6240 char *trusted_dn = NULL;
6243 DEBUG(10, ("ldapsam_set_trusteddom_pw called for domain %s\n", domain));
6246 * get the current entry (if there is one) in order to put the
6247 * current password into the previous password attribute
6249 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6254 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
6255 LDAP_OBJ_TRUSTDOM_PASSWORD);
6256 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
6258 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
6259 sid_string_tos(sid));
6260 smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
6261 talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
6262 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6263 "sambaClearTextPassword", pwd);
6265 talloc_autofree_ldapmod(talloc_tos(), mods);
6267 if (entry != NULL) {
6268 prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6269 entry, "sambaClearTextPassword", talloc_tos());
6270 if (prev_pwd != NULL) {
6271 smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
6272 "sambaPreviousClearTextPassword",
6277 trusted_dn = trusteddom_dn(ldap_state, domain);
6278 if (trusted_dn == NULL) {
6281 if (entry == NULL) {
6282 rc = smbldap_add(ldap_state->smbldap_state, trusted_dn, mods);
6284 rc = smbldap_modify(ldap_state->smbldap_state, trusted_dn, mods);
6287 if (rc != LDAP_SUCCESS) {
6288 DEBUG(1, ("error writing trusted domain password!\n"));
6295 static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
6299 struct ldapsam_privates *ldap_state =
6300 (struct ldapsam_privates *)methods->private_data;
6301 LDAPMessage *entry = NULL;
6302 const char *trusted_dn;
6304 if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
6308 if (entry == NULL) {
6309 DEBUG(5, ("ldapsam_del_trusteddom_pw: no such trusted domain: "
6314 trusted_dn = smbldap_talloc_dn(talloc_tos(), priv2ld(ldap_state),
6316 if (trusted_dn == NULL) {
6317 DEBUG(0,("ldapsam_del_trusteddom_pw: Out of memory!\n"));
6321 rc = smbldap_delete(ldap_state->smbldap_state, trusted_dn);
6322 if (rc != LDAP_SUCCESS) {
6329 static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
6330 TALLOC_CTX *mem_ctx,
6331 uint32 *num_domains,
6332 struct trustdom_info ***domains)
6335 struct ldapsam_privates *ldap_state =
6336 (struct ldapsam_privates *)methods->private_data;
6338 int scope = LDAP_SCOPE_SUBTREE;
6339 const char *attrs[] = { "sambaDomainName", "sambaSID", NULL };
6340 int attrsonly = 0; /* 0: return values too */
6341 LDAPMessage *result = NULL;
6342 LDAPMessage *entry = NULL;
6344 filter = talloc_asprintf(talloc_tos(), "(objectClass=%s)",
6345 LDAP_OBJ_TRUSTDOM_PASSWORD);
6347 rc = smbldap_search(ldap_state->smbldap_state,
6348 ldap_state->domain_dn,
6355 if (result != NULL) {
6356 talloc_autofree_ldapmsg(mem_ctx, result);
6359 if (rc != LDAP_SUCCESS) {
6360 return NT_STATUS_UNSUCCESSFUL;
6364 if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
6365 DEBUG(1, ("talloc failed\n"));
6366 return NT_STATUS_NO_MEMORY;
6369 for (entry = ldap_first_entry(priv2ld(ldap_state), result);
6371 entry = ldap_next_entry(priv2ld(ldap_state), entry))
6373 char *dom_name, *dom_sid_str;
6374 struct trustdom_info *dom_info;
6376 dom_info = TALLOC_P(*domains, struct trustdom_info);
6377 if (dom_info == NULL) {
6378 DEBUG(1, ("talloc failed\n"));
6379 return NT_STATUS_NO_MEMORY;
6382 dom_name = smbldap_talloc_single_attribute(priv2ld(ldap_state),
6386 if (dom_name == NULL) {
6387 DEBUG(1, ("talloc failed\n"));
6388 return NT_STATUS_NO_MEMORY;
6390 dom_info->name = dom_name;
6392 dom_sid_str = smbldap_talloc_single_attribute(
6393 priv2ld(ldap_state), entry, "sambaSID",
6395 if (dom_sid_str == NULL) {
6396 DEBUG(1, ("talloc failed\n"));
6397 return NT_STATUS_NO_MEMORY;
6399 if (!string_to_sid(&dom_info->sid, dom_sid_str)) {
6400 DEBUG(1, ("Error calling string_to_sid on SID %s\n",
6402 return NT_STATUS_UNSUCCESSFUL;
6405 ADD_TO_ARRAY(*domains, struct trustdom_info *, dom_info,
6406 domains, num_domains);
6408 if (*domains == NULL) {
6409 DEBUG(1, ("talloc failed\n"));
6410 return NT_STATUS_NO_MEMORY;
6414 DEBUG(5, ("ldapsam_enum_trusteddoms: got %d domains\n", *num_domains));
6415 return NT_STATUS_OK;
6419 /**********************************************************************
6421 *********************************************************************/
6423 static void free_private_data(void **vp)
6425 struct ldapsam_privates **ldap_state = (struct ldapsam_privates **)vp;
6427 smbldap_free_struct(&(*ldap_state)->smbldap_state);
6429 if ((*ldap_state)->result != NULL) {
6430 ldap_msgfree((*ldap_state)->result);
6431 (*ldap_state)->result = NULL;
6433 if ((*ldap_state)->domain_dn != NULL) {
6434 SAFE_FREE((*ldap_state)->domain_dn);
6439 /* No need to free any further, as it is talloc()ed */
6442 /*********************************************************************
6443 Intitalise the parts of the pdb_methods structure that are common to
6445 *********************************************************************/
6447 static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const char *location)
6450 struct ldapsam_privates *ldap_state;
6452 if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
6456 (*pdb_method)->name = "ldapsam";
6458 (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
6459 (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
6460 (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
6461 (*pdb_method)->update_sam_account = ldapsam_update_sam_account;
6462 (*pdb_method)->delete_sam_account = ldapsam_delete_sam_account;
6463 (*pdb_method)->rename_sam_account = ldapsam_rename_sam_account;
6465 (*pdb_method)->getgrsid = ldapsam_getgrsid;
6466 (*pdb_method)->getgrgid = ldapsam_getgrgid;
6467 (*pdb_method)->getgrnam = ldapsam_getgrnam;
6468 (*pdb_method)->add_group_mapping_entry = ldapsam_add_group_mapping_entry;
6469 (*pdb_method)->update_group_mapping_entry = ldapsam_update_group_mapping_entry;
6470 (*pdb_method)->delete_group_mapping_entry = ldapsam_delete_group_mapping_entry;
6471 (*pdb_method)->enum_group_mapping = ldapsam_enum_group_mapping;
6473 (*pdb_method)->get_account_policy = ldapsam_get_account_policy;
6474 (*pdb_method)->set_account_policy = ldapsam_set_account_policy;
6476 (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
6478 (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
6479 (*pdb_method)->new_rid = ldapsam_new_rid;
6481 (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
6482 (*pdb_method)->set_trusteddom_pw = ldapsam_set_trusteddom_pw;
6483 (*pdb_method)->del_trusteddom_pw = ldapsam_del_trusteddom_pw;
6484 (*pdb_method)->enum_trusteddoms = ldapsam_enum_trusteddoms;
6486 /* TODO: Setup private data and free */
6488 if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
6489 DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
6490 return NT_STATUS_NO_MEMORY;
6493 nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
6494 location, &ldap_state->smbldap_state);
6496 if ( !NT_STATUS_IS_OK(nt_status) ) {
6500 if ( !(ldap_state->domain_name = talloc_strdup(*pdb_method, get_global_sam_name()) ) ) {
6501 return NT_STATUS_NO_MEMORY;
6504 (*pdb_method)->private_data = ldap_state;
6506 (*pdb_method)->free_private_data = free_private_data;
6508 return NT_STATUS_OK;
6511 /**********************************************************************
6512 Initialise the 'compat' mode for pdb_ldap
6513 *********************************************************************/
6515 NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
6518 struct ldapsam_privates *ldap_state;
6519 char *uri = talloc_strdup( NULL, location );
6521 trim_char( uri, '\"', '\"' );
6522 nt_status = pdb_init_ldapsam_common( pdb_method, uri );
6526 if ( !NT_STATUS_IS_OK(nt_status) ) {
6530 (*pdb_method)->name = "ldapsam_compat";
6532 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6533 ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
6535 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6537 return NT_STATUS_OK;
6540 /**********************************************************************
6541 Initialise the normal mode for pdb_ldap
6542 *********************************************************************/
6544 NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
6547 struct ldapsam_privates *ldap_state = NULL;
6548 uint32 alg_rid_base;
6549 char *alg_rid_base_string = NULL;
6550 LDAPMessage *result = NULL;
6551 LDAPMessage *entry = NULL;
6552 DOM_SID ldap_domain_sid;
6553 DOM_SID secrets_domain_sid;
6554 char *domain_sid_string = NULL;
6556 char *uri = talloc_strdup( NULL, location );
6558 trim_char( uri, '\"', '\"' );
6559 nt_status = pdb_init_ldapsam_common(pdb_method, uri);
6564 if (!NT_STATUS_IS_OK(nt_status)) {
6568 (*pdb_method)->name = "ldapsam";
6570 (*pdb_method)->add_aliasmem = ldapsam_add_aliasmem;
6571 (*pdb_method)->del_aliasmem = ldapsam_del_aliasmem;
6572 (*pdb_method)->enum_aliasmem = ldapsam_enum_aliasmem;
6573 (*pdb_method)->enum_alias_memberships = ldapsam_alias_memberships;
6574 (*pdb_method)->search_users = ldapsam_search_users;
6575 (*pdb_method)->search_groups = ldapsam_search_groups;
6576 (*pdb_method)->search_aliases = ldapsam_search_aliases;
6578 if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
6579 (*pdb_method)->enum_group_members = ldapsam_enum_group_members;
6580 (*pdb_method)->enum_group_memberships =
6581 ldapsam_enum_group_memberships;
6582 (*pdb_method)->lookup_rids = ldapsam_lookup_rids;
6583 (*pdb_method)->sid_to_id = ldapsam_sid_to_id;
6584 (*pdb_method)->uid_to_sid = ldapsam_uid_to_sid;
6585 (*pdb_method)->gid_to_sid = ldapsam_gid_to_sid;
6587 if (lp_parm_bool(-1, "ldapsam", "editposix", False)) {
6588 (*pdb_method)->create_user = ldapsam_create_user;
6589 (*pdb_method)->delete_user = ldapsam_delete_user;
6590 (*pdb_method)->create_dom_group = ldapsam_create_dom_group;
6591 (*pdb_method)->delete_dom_group = ldapsam_delete_dom_group;
6592 (*pdb_method)->add_groupmem = ldapsam_add_groupmem;
6593 (*pdb_method)->del_groupmem = ldapsam_del_groupmem;
6594 (*pdb_method)->set_unix_primary_group = ldapsam_set_primary_group;
6598 ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
6599 ldap_state->schema_ver = SCHEMAVER_SAMBASAMACCOUNT;
6601 /* Try to setup the Domain Name, Domain SID, algorithmic rid base */
6603 nt_status = smbldap_search_domain_info(ldap_state->smbldap_state,
6605 ldap_state->domain_name, True);
6607 if ( !NT_STATUS_IS_OK(nt_status) ) {
6608 DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
6609 "info, nor add one to the domain\n"));
6610 DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
6611 "will be unable to allocate new users/groups, "
6612 "and will risk BDCs having inconsistant SIDs\n"));
6613 sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
6614 return NT_STATUS_OK;
6617 /* Given that the above might fail, everything below this must be
6620 entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
6623 DEBUG(0, ("pdb_init_ldapsam: Could not get domain info "
6625 ldap_msgfree(result);
6626 return NT_STATUS_UNSUCCESSFUL;
6629 dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
6631 ldap_msgfree(result);
6632 return NT_STATUS_UNSUCCESSFUL;
6635 ldap_state->domain_dn = smb_xstrdup(dn);
6638 domain_sid_string = smbldap_talloc_single_attribute(
6639 ldap_state->smbldap_state->ldap_struct,
6641 get_userattr_key2string(ldap_state->schema_ver,
6642 LDAP_ATTR_USER_SID),
6645 if (domain_sid_string) {
6647 if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
6648 DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
6649 "read as a valid SID\n", domain_sid_string));
6650 ldap_msgfree(result);
6651 TALLOC_FREE(domain_sid_string);
6652 return NT_STATUS_INVALID_PARAMETER;
6654 found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
6655 &secrets_domain_sid);
6656 if (!found_sid || !sid_equal(&secrets_domain_sid,
6657 &ldap_domain_sid)) {
6658 DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
6659 "%s based on pdb_ldap results %s -> %s\n",
6660 ldap_state->domain_name,
6661 sid_string_dbg(&secrets_domain_sid),
6662 sid_string_dbg(&ldap_domain_sid)));
6664 /* reset secrets.tdb sid */
6665 secrets_store_domain_sid(ldap_state->domain_name,
6667 DEBUG(1, ("New global sam SID: %s\n",
6668 sid_string_dbg(get_global_sam_sid())));
6670 sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
6671 TALLOC_FREE(domain_sid_string);
6674 alg_rid_base_string = smbldap_talloc_single_attribute(
6675 ldap_state->smbldap_state->ldap_struct,
6677 get_attr_key2string( dominfo_attr_list,
6678 LDAP_ATTR_ALGORITHMIC_RID_BASE ),
6680 if (alg_rid_base_string) {
6681 alg_rid_base = (uint32)atol(alg_rid_base_string);
6682 if (alg_rid_base != algorithmic_rid_base()) {
6683 DEBUG(0, ("The value of 'algorithmic RID base' has "
6684 "changed since the LDAP\n"
6685 "database was initialised. Aborting. \n"));
6686 ldap_msgfree(result);
6687 TALLOC_FREE(alg_rid_base_string);
6688 return NT_STATUS_UNSUCCESSFUL;
6690 TALLOC_FREE(alg_rid_base_string);
6692 ldap_msgfree(result);
6694 return NT_STATUS_OK;
6697 NTSTATUS pdb_ldap_init(void)
6700 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
6703 if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
6706 /* Let pdb_nds register backends */
6709 return NT_STATUS_OK;