2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997,
7 * Copyright (C) Marc Jacobsen 1999,
8 * Copyright (C) Jeremy Allison 2001-2008,
9 * Copyright (C) Jean François Micouleau 1998-2001,
10 * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002,
11 * Copyright (C) Gerald (Jerry) Carter 2003-2004,
12 * Copyright (C) Simo Sorce 2003.
13 * Copyright (C) Volker Lendecke 2005.
14 * Copyright (C) Guenther Deschner 2008.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 * This is the implementation of the SAMR code.
35 #include "system/passwd.h"
36 #include "../libcli/auth/libcli_auth.h"
38 #include "../librpc/gen_ndr/srv_samr.h"
39 #include "rpc_server/samr/srv_samr_util.h"
40 #include "../lib/crypto/arcfour.h"
42 #include "rpc_client/init_lsa.h"
43 #include "../libcli/security/security.h"
46 #include "rpc_server/srv_access_check.h"
47 #include "../lib/tsocket/tsocket.h"
50 #define DBGC_CLASS DBGC_RPC_SRV
52 #define SAMR_USR_RIGHTS_WRITE_PW \
53 ( READ_CONTROL_ACCESS | \
54 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
55 SAMR_USER_ACCESS_SET_LOC_COM)
56 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
57 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
59 #define DISP_INFO_CACHE_TIMEOUT 10
61 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
62 #define MAX_SAM_ENTRIES_W95 50
64 struct samr_connect_info {
68 struct samr_domain_info {
70 struct disp_info *disp_info;
73 struct samr_user_info {
77 struct samr_group_info {
81 struct samr_alias_info {
85 typedef struct disp_info {
86 struct dom_sid sid; /* identify which domain this is. */
87 struct pdb_search *users; /* querydispinfo 1 and 4 */
88 struct pdb_search *machines; /* querydispinfo 2 */
89 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
90 struct pdb_search *aliases; /* enumaliases */
92 uint32_t enum_acb_mask;
93 struct pdb_search *enum_users; /* enumusers with a mask */
95 struct timed_event *cache_timeout_event; /* cache idle timeout
99 static const struct generic_mapping sam_generic_mapping = {
100 GENERIC_RIGHTS_SAM_READ,
101 GENERIC_RIGHTS_SAM_WRITE,
102 GENERIC_RIGHTS_SAM_EXECUTE,
103 GENERIC_RIGHTS_SAM_ALL_ACCESS};
104 static const struct generic_mapping dom_generic_mapping = {
105 GENERIC_RIGHTS_DOMAIN_READ,
106 GENERIC_RIGHTS_DOMAIN_WRITE,
107 GENERIC_RIGHTS_DOMAIN_EXECUTE,
108 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
109 static const struct generic_mapping usr_generic_mapping = {
110 GENERIC_RIGHTS_USER_READ,
111 GENERIC_RIGHTS_USER_WRITE,
112 GENERIC_RIGHTS_USER_EXECUTE,
113 GENERIC_RIGHTS_USER_ALL_ACCESS};
114 static const struct generic_mapping usr_nopwchange_generic_mapping = {
115 GENERIC_RIGHTS_USER_READ,
116 GENERIC_RIGHTS_USER_WRITE,
117 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
118 GENERIC_RIGHTS_USER_ALL_ACCESS};
119 static const struct generic_mapping grp_generic_mapping = {
120 GENERIC_RIGHTS_GROUP_READ,
121 GENERIC_RIGHTS_GROUP_WRITE,
122 GENERIC_RIGHTS_GROUP_EXECUTE,
123 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
124 static const struct generic_mapping ali_generic_mapping = {
125 GENERIC_RIGHTS_ALIAS_READ,
126 GENERIC_RIGHTS_ALIAS_WRITE,
127 GENERIC_RIGHTS_ALIAS_EXECUTE,
128 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
130 /*******************************************************************
131 *******************************************************************/
133 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, struct security_descriptor **psd, size_t *sd_size,
134 const struct generic_mapping *map,
135 struct dom_sid *sid, uint32 sid_access )
137 struct dom_sid domadmin_sid;
138 struct security_ace ace[5]; /* at most 5 entries */
141 struct security_acl *psa = NULL;
143 /* basic access for Everyone */
145 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
146 map->generic_execute | map->generic_read, 0);
148 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
150 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
151 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
152 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
153 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
155 /* Add Full Access for Domain Admins if we are a DC */
158 sid_compose(&domadmin_sid, get_global_sam_sid(),
160 init_sec_ace(&ace[i++], &domadmin_sid,
161 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
164 /* if we have a sid, give it some special access */
167 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
170 /* create the security descriptor */
172 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
173 return NT_STATUS_NO_MEMORY;
175 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
176 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
177 psa, sd_size)) == NULL)
178 return NT_STATUS_NO_MEMORY;
183 /*******************************************************************
184 Fetch or create a dispinfo struct.
185 ********************************************************************/
187 static DISP_INFO *get_samr_dispinfo_by_sid(const struct dom_sid *psid)
190 * We do a static cache for DISP_INFO's here. Explanation can be found
191 * in Jeremy's checkin message to r11793:
193 * Fix the SAMR cache so it works across completely insane
194 * client behaviour (ie.:
195 * open pipe/open SAMR handle/enumerate 0 - 1024
196 * close SAMR handle, close pipe.
197 * open pipe/open SAMR handle/enumerate 1024 - 2048...
198 * close SAMR handle, close pipe.
199 * And on ad-nausium. Amazing.... probably object-oriented
200 * client side programming in action yet again.
201 * This change should *massively* improve performance when
202 * enumerating users from an LDAP database.
205 * "Our" and the builtin domain are the only ones where we ever
206 * enumerate stuff, so just cache 2 entries.
209 static struct disp_info *builtin_dispinfo;
210 static struct disp_info *domain_dispinfo;
212 /* There are two cases to consider here:
213 1) The SID is a domain SID and we look for an equality match, or
214 2) This is an account SID and so we return the DISP_INFO* for our
221 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
223 * Necessary only once, but it does not really hurt.
225 if (builtin_dispinfo == NULL) {
226 builtin_dispinfo = talloc_zero(NULL, struct disp_info);
227 if (builtin_dispinfo == NULL) {
231 sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
233 return builtin_dispinfo;
236 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
238 * Necessary only once, but it does not really hurt.
240 if (domain_dispinfo == NULL) {
241 domain_dispinfo = talloc_zero(NULL, struct disp_info);
242 if (domain_dispinfo == NULL) {
246 sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
248 return domain_dispinfo;
254 /*******************************************************************
255 Function to free the per SID data.
256 ********************************************************************/
258 static void free_samr_cache(DISP_INFO *disp_info)
260 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
261 sid_string_dbg(&disp_info->sid)));
263 /* We need to become root here because the paged search might have to
264 * tell the LDAP server we're not interested in the rest anymore. */
268 TALLOC_FREE(disp_info->users);
269 TALLOC_FREE(disp_info->machines);
270 TALLOC_FREE(disp_info->groups);
271 TALLOC_FREE(disp_info->aliases);
272 TALLOC_FREE(disp_info->enum_users);
277 /*******************************************************************
278 Idle event handler. Throw away the disp info cache.
279 ********************************************************************/
281 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
282 struct timed_event *te,
286 DISP_INFO *disp_info = (DISP_INFO *)private_data;
288 TALLOC_FREE(disp_info->cache_timeout_event);
290 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
292 free_samr_cache(disp_info);
295 /*******************************************************************
296 Setup cache removal idle event handler.
297 ********************************************************************/
299 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
301 /* Remove any pending timeout and update. */
303 TALLOC_FREE(disp_info->cache_timeout_event);
305 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
306 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
307 (unsigned int)secs_fromnow ));
309 disp_info->cache_timeout_event = event_add_timed(
310 server_event_context(), NULL,
311 timeval_current_ofs(secs_fromnow, 0),
312 disp_info_cache_idle_timeout_handler, (void *)disp_info);
315 /*******************************************************************
316 Force flush any cache. We do this on any samr_set_xxx call.
317 We must also remove the timeout handler.
318 ********************************************************************/
320 static void force_flush_samr_cache(const struct dom_sid *sid)
322 struct disp_info *disp_info = get_samr_dispinfo_by_sid(sid);
324 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
328 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
329 TALLOC_FREE(disp_info->cache_timeout_event);
330 free_samr_cache(disp_info);
333 /*******************************************************************
334 Ensure password info is never given out. Paranioa... JRA.
335 ********************************************************************/
337 static void samr_clear_sam_passwd(struct samu *sam_pass)
343 /* These now zero out the old password */
345 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
346 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
349 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
351 struct samr_displayentry *entry;
353 if (sid_check_is_builtin(&info->sid)) {
354 /* No users in builtin. */
358 if (info->users == NULL) {
359 info->users = pdb_search_users(info, acct_flags);
360 if (info->users == NULL) {
364 /* Fetch the last possible entry, thus trigger an enumeration */
365 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
367 /* Ensure we cache this enumeration. */
368 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
370 return info->users->num_entries;
373 static uint32 count_sam_groups(struct disp_info *info)
375 struct samr_displayentry *entry;
377 if (sid_check_is_builtin(&info->sid)) {
378 /* No groups in builtin. */
382 if (info->groups == NULL) {
383 info->groups = pdb_search_groups(info);
384 if (info->groups == NULL) {
388 /* Fetch the last possible entry, thus trigger an enumeration */
389 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
391 /* Ensure we cache this enumeration. */
392 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
394 return info->groups->num_entries;
397 static uint32 count_sam_aliases(struct disp_info *info)
399 struct samr_displayentry *entry;
401 if (info->aliases == NULL) {
402 info->aliases = pdb_search_aliases(info, &info->sid);
403 if (info->aliases == NULL) {
407 /* Fetch the last possible entry, thus trigger an enumeration */
408 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
410 /* Ensure we cache this enumeration. */
411 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
413 return info->aliases->num_entries;
416 /*******************************************************************
418 ********************************************************************/
420 NTSTATUS _samr_Close(struct pipes_struct *p, struct samr_Close *r)
422 if (!close_policy_hnd(p, r->in.handle)) {
423 return NT_STATUS_INVALID_HANDLE;
426 ZERO_STRUCTP(r->out.handle);
431 /*******************************************************************
433 ********************************************************************/
435 NTSTATUS _samr_OpenDomain(struct pipes_struct *p,
436 struct samr_OpenDomain *r)
438 struct samr_connect_info *cinfo;
439 struct samr_domain_info *dinfo;
440 struct security_descriptor *psd = NULL;
442 uint32 des_access = r->in.access_mask;
445 uint32_t extra_access = SAMR_DOMAIN_ACCESS_CREATE_USER;
447 /* find the connection policy handle. */
449 cinfo = policy_handle_find(p, r->in.connect_handle, 0, NULL,
450 struct samr_connect_info, &status);
451 if (!NT_STATUS_IS_OK(status)) {
455 /*check if access can be granted as requested by client. */
456 map_max_allowed_access(p->session_info->security_token,
457 p->session_info->unix_token,
460 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
461 se_map_generic( &des_access, &dom_generic_mapping );
464 * Users with SeAddUser get the ability to manipulate groups
467 if (security_token_has_privilege(p->session_info->security_token, SEC_PRIV_ADD_USERS)) {
468 extra_access |= (SAMR_DOMAIN_ACCESS_CREATE_GROUP |
469 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS |
470 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT |
471 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS |
472 SAMR_DOMAIN_ACCESS_CREATE_ALIAS);
476 * Users with SeMachineAccount or SeAddUser get additional
477 * SAMR_DOMAIN_ACCESS_CREATE_USER access.
480 status = access_check_object( psd, p->session_info->security_token,
481 SEC_PRIV_MACHINE_ACCOUNT, SEC_PRIV_ADD_USERS,
482 extra_access, des_access,
483 &acc_granted, "_samr_OpenDomain" );
485 if ( !NT_STATUS_IS_OK(status) )
488 if (!sid_check_is_domain(r->in.sid) &&
489 !sid_check_is_builtin(r->in.sid)) {
490 return NT_STATUS_NO_SUCH_DOMAIN;
493 dinfo = policy_handle_create(p, r->out.domain_handle, acc_granted,
494 struct samr_domain_info, &status);
495 if (!NT_STATUS_IS_OK(status)) {
498 dinfo->sid = *r->in.sid;
499 dinfo->disp_info = get_samr_dispinfo_by_sid(r->in.sid);
501 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
506 /*******************************************************************
508 ********************************************************************/
510 NTSTATUS _samr_GetUserPwInfo(struct pipes_struct *p,
511 struct samr_GetUserPwInfo *r)
513 struct samr_user_info *uinfo;
514 enum lsa_SidType sid_type;
515 uint32_t min_password_length = 0;
516 uint32_t password_properties = 0;
520 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
522 uinfo = policy_handle_find(p, r->in.user_handle,
523 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
524 struct samr_user_info, &status);
525 if (!NT_STATUS_IS_OK(status)) {
529 if (!sid_check_is_in_our_domain(&uinfo->sid)) {
530 return NT_STATUS_OBJECT_TYPE_MISMATCH;
534 ret = lookup_sid(p->mem_ctx, &uinfo->sid, NULL, NULL, &sid_type);
537 return NT_STATUS_NO_SUCH_USER;
543 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
544 &min_password_length);
545 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
546 &password_properties);
549 if (lp_check_password_script() && *lp_check_password_script()) {
550 password_properties |= DOMAIN_PASSWORD_COMPLEX;
558 r->out.info->min_password_length = min_password_length;
559 r->out.info->password_properties = password_properties;
561 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
566 /*******************************************************************
568 ********************************************************************/
570 NTSTATUS _samr_SetSecurity(struct pipes_struct *p,
571 struct samr_SetSecurity *r)
573 struct samr_user_info *uinfo;
575 struct security_acl *dacl;
577 struct samu *sampass=NULL;
580 uinfo = policy_handle_find(p, r->in.handle,
581 SAMR_USER_ACCESS_SET_ATTRIBUTES, NULL,
582 struct samr_user_info, &status);
583 if (!NT_STATUS_IS_OK(status)) {
587 if (!(sampass = samu_new( p->mem_ctx))) {
588 DEBUG(0,("No memory!\n"));
589 return NT_STATUS_NO_MEMORY;
592 /* get the user record */
594 ret = pdb_getsampwsid(sampass, &uinfo->sid);
598 DEBUG(4, ("User %s not found\n",
599 sid_string_dbg(&uinfo->sid)));
600 TALLOC_FREE(sampass);
601 return NT_STATUS_INVALID_HANDLE;
604 dacl = r->in.sdbuf->sd->dacl;
605 for (i=0; i < dacl->num_aces; i++) {
606 if (dom_sid_equal(&uinfo->sid, &dacl->aces[i].trustee)) {
607 ret = pdb_set_pass_can_change(sampass,
608 (dacl->aces[i].access_mask &
609 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
616 TALLOC_FREE(sampass);
617 return NT_STATUS_ACCESS_DENIED;
621 status = pdb_update_sam_account(sampass);
624 TALLOC_FREE(sampass);
629 /*******************************************************************
630 build correct perms based on policies and password times for _samr_query_sec_obj
631 *******************************************************************/
632 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, struct dom_sid *user_sid)
634 struct samu *sampass=NULL;
637 if ( !(sampass = samu_new( mem_ctx )) ) {
638 DEBUG(0,("No memory!\n"));
643 ret = pdb_getsampwsid(sampass, user_sid);
647 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
648 TALLOC_FREE(sampass);
652 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
654 if (pdb_get_pass_can_change(sampass)) {
655 TALLOC_FREE(sampass);
658 TALLOC_FREE(sampass);
663 /*******************************************************************
665 ********************************************************************/
667 NTSTATUS _samr_QuerySecurity(struct pipes_struct *p,
668 struct samr_QuerySecurity *r)
670 struct samr_connect_info *cinfo;
671 struct samr_domain_info *dinfo;
672 struct samr_user_info *uinfo;
673 struct samr_group_info *ginfo;
674 struct samr_alias_info *ainfo;
676 struct security_descriptor * psd = NULL;
679 cinfo = policy_handle_find(p, r->in.handle,
680 SEC_STD_READ_CONTROL, NULL,
681 struct samr_connect_info, &status);
682 if (NT_STATUS_IS_OK(status)) {
683 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
684 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
685 &sam_generic_mapping, NULL, 0);
689 dinfo = policy_handle_find(p, r->in.handle,
690 SEC_STD_READ_CONTROL, NULL,
691 struct samr_domain_info, &status);
692 if (NT_STATUS_IS_OK(status)) {
693 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
694 "with SID: %s\n", sid_string_dbg(&dinfo->sid)));
696 * TODO: Builtin probably needs a different SD with restricted
699 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
700 &dom_generic_mapping, NULL, 0);
704 uinfo = policy_handle_find(p, r->in.handle,
705 SEC_STD_READ_CONTROL, NULL,
706 struct samr_user_info, &status);
707 if (NT_STATUS_IS_OK(status)) {
708 DEBUG(10,("_samr_QuerySecurity: querying security on user "
709 "Object with SID: %s\n",
710 sid_string_dbg(&uinfo->sid)));
711 if (check_change_pw_access(p->mem_ctx, &uinfo->sid)) {
712 status = make_samr_object_sd(
713 p->mem_ctx, &psd, &sd_size,
714 &usr_generic_mapping,
715 &uinfo->sid, SAMR_USR_RIGHTS_WRITE_PW);
717 status = make_samr_object_sd(
718 p->mem_ctx, &psd, &sd_size,
719 &usr_nopwchange_generic_mapping,
720 &uinfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
725 ginfo = policy_handle_find(p, r->in.handle,
726 SEC_STD_READ_CONTROL, NULL,
727 struct samr_group_info, &status);
728 if (NT_STATUS_IS_OK(status)) {
730 * TODO: different SDs have to be generated for aliases groups
731 * and users. Currently all three get a default user SD
733 DEBUG(10,("_samr_QuerySecurity: querying security on group "
734 "Object with SID: %s\n",
735 sid_string_dbg(&ginfo->sid)));
736 status = make_samr_object_sd(
737 p->mem_ctx, &psd, &sd_size,
738 &usr_nopwchange_generic_mapping,
739 &ginfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
743 ainfo = policy_handle_find(p, r->in.handle,
744 SEC_STD_READ_CONTROL, NULL,
745 struct samr_alias_info, &status);
746 if (NT_STATUS_IS_OK(status)) {
748 * TODO: different SDs have to be generated for aliases groups
749 * and users. Currently all three get a default user SD
751 DEBUG(10,("_samr_QuerySecurity: querying security on alias "
752 "Object with SID: %s\n",
753 sid_string_dbg(&ainfo->sid)));
754 status = make_samr_object_sd(
755 p->mem_ctx, &psd, &sd_size,
756 &usr_nopwchange_generic_mapping,
757 &ainfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
761 return NT_STATUS_OBJECT_TYPE_MISMATCH;
763 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
764 return NT_STATUS_NO_MEMORY;
769 /*******************************************************************
770 makes a SAM_ENTRY / UNISTR2* structure from a user list.
771 ********************************************************************/
773 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
774 struct samr_SamEntry **sam_pp,
775 uint32_t num_entries,
777 struct samr_displayentry *entries)
780 struct samr_SamEntry *sam;
784 if (num_entries == 0) {
788 sam = talloc_zero_array(ctx, struct samr_SamEntry, num_entries);
790 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
791 return NT_STATUS_NO_MEMORY;
794 for (i = 0; i < num_entries; i++) {
797 * usrmgr expects a non-NULL terminated string with
798 * trust relationships
800 if (entries[i].acct_flags & ACB_DOMTRUST) {
801 init_unistr2(&uni_temp_name, entries[i].account_name,
804 init_unistr2(&uni_temp_name, entries[i].account_name,
808 init_lsa_String(&sam[i].name, entries[i].account_name);
809 sam[i].idx = entries[i].rid;
817 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
819 /*******************************************************************
820 _samr_EnumDomainUsers
821 ********************************************************************/
823 NTSTATUS _samr_EnumDomainUsers(struct pipes_struct *p,
824 struct samr_EnumDomainUsers *r)
827 struct samr_domain_info *dinfo;
829 uint32 enum_context = *r->in.resume_handle;
830 enum remote_arch_types ra_type = get_remote_arch();
831 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
832 uint32 max_entries = max_sam_entries;
833 struct samr_displayentry *entries = NULL;
834 struct samr_SamArray *samr_array = NULL;
835 struct samr_SamEntry *samr_entries = NULL;
837 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
839 dinfo = policy_handle_find(p, r->in.domain_handle,
840 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
841 struct samr_domain_info, &status);
842 if (!NT_STATUS_IS_OK(status)) {
846 samr_array = talloc_zero(p->mem_ctx, struct samr_SamArray);
848 return NT_STATUS_NO_MEMORY;
850 *r->out.sam = samr_array;
852 if (sid_check_is_builtin(&dinfo->sid)) {
853 /* No users in builtin. */
854 *r->out.resume_handle = *r->in.resume_handle;
855 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
863 if ((dinfo->disp_info->enum_users != NULL) &&
864 (dinfo->disp_info->enum_acb_mask != r->in.acct_flags)) {
865 TALLOC_FREE(dinfo->disp_info->enum_users);
868 if (dinfo->disp_info->enum_users == NULL) {
869 dinfo->disp_info->enum_users = pdb_search_users(
870 dinfo->disp_info, r->in.acct_flags);
871 dinfo->disp_info->enum_acb_mask = r->in.acct_flags;
874 if (dinfo->disp_info->enum_users == NULL) {
875 /* END AS ROOT !!!! */
877 return NT_STATUS_ACCESS_DENIED;
880 num_account = pdb_search_entries(dinfo->disp_info->enum_users,
881 enum_context, max_entries,
884 /* END AS ROOT !!!! */
888 if (num_account == 0) {
889 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
891 *r->out.resume_handle = *r->in.resume_handle;
895 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
896 num_account, enum_context,
898 if (!NT_STATUS_IS_OK(status)) {
902 if (max_entries <= num_account) {
903 status = STATUS_MORE_ENTRIES;
905 status = NT_STATUS_OK;
908 /* Ensure we cache this enumeration. */
909 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
911 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
913 samr_array->count = num_account;
914 samr_array->entries = samr_entries;
916 *r->out.resume_handle = *r->in.resume_handle + num_account;
917 *r->out.num_entries = num_account;
919 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
924 /*******************************************************************
925 makes a SAM_ENTRY / UNISTR2* structure from a group list.
926 ********************************************************************/
928 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
929 struct samr_SamEntry **sam_pp,
930 uint32_t num_sam_entries,
931 struct samr_displayentry *entries)
933 struct samr_SamEntry *sam;
938 if (num_sam_entries == 0) {
942 sam = talloc_zero_array(ctx, struct samr_SamEntry, num_sam_entries);
947 for (i = 0; i < num_sam_entries; i++) {
949 * JRA. I think this should include the null. TNG does not.
951 init_lsa_String(&sam[i].name, entries[i].account_name);
952 sam[i].idx = entries[i].rid;
958 /*******************************************************************
959 _samr_EnumDomainGroups
960 ********************************************************************/
962 NTSTATUS _samr_EnumDomainGroups(struct pipes_struct *p,
963 struct samr_EnumDomainGroups *r)
966 struct samr_domain_info *dinfo;
967 struct samr_displayentry *groups;
969 struct samr_SamArray *samr_array = NULL;
970 struct samr_SamEntry *samr_entries = NULL;
972 dinfo = policy_handle_find(p, r->in.domain_handle,
973 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
974 struct samr_domain_info, &status);
975 if (!NT_STATUS_IS_OK(status)) {
979 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
981 samr_array = talloc_zero(p->mem_ctx, struct samr_SamArray);
983 return NT_STATUS_NO_MEMORY;
985 *r->out.sam = samr_array;
987 if (sid_check_is_builtin(&dinfo->sid)) {
988 /* No groups in builtin. */
989 *r->out.resume_handle = *r->in.resume_handle;
990 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
994 /* the domain group array is being allocated in the function below */
998 if (dinfo->disp_info->groups == NULL) {
999 dinfo->disp_info->groups = pdb_search_groups(dinfo->disp_info);
1001 if (dinfo->disp_info->groups == NULL) {
1003 return NT_STATUS_ACCESS_DENIED;
1007 num_groups = pdb_search_entries(dinfo->disp_info->groups,
1008 *r->in.resume_handle,
1009 MAX_SAM_ENTRIES, &groups);
1012 /* Ensure we cache this enumeration. */
1013 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1015 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1016 num_groups, groups);
1018 if (MAX_SAM_ENTRIES <= num_groups) {
1019 status = STATUS_MORE_ENTRIES;
1021 status = NT_STATUS_OK;
1024 samr_array->count = num_groups;
1025 samr_array->entries = samr_entries;
1027 *r->out.num_entries = num_groups;
1028 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1030 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1035 /*******************************************************************
1036 _samr_EnumDomainAliases
1037 ********************************************************************/
1039 NTSTATUS _samr_EnumDomainAliases(struct pipes_struct *p,
1040 struct samr_EnumDomainAliases *r)
1043 struct samr_domain_info *dinfo;
1044 struct samr_displayentry *aliases;
1045 uint32 num_aliases = 0;
1046 struct samr_SamArray *samr_array = NULL;
1047 struct samr_SamEntry *samr_entries = NULL;
1049 dinfo = policy_handle_find(p, r->in.domain_handle,
1050 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1051 struct samr_domain_info, &status);
1052 if (!NT_STATUS_IS_OK(status)) {
1056 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1057 sid_string_dbg(&dinfo->sid)));
1059 samr_array = talloc_zero(p->mem_ctx, struct samr_SamArray);
1061 return NT_STATUS_NO_MEMORY;
1066 if (dinfo->disp_info->aliases == NULL) {
1067 dinfo->disp_info->aliases = pdb_search_aliases(
1068 dinfo->disp_info, &dinfo->sid);
1069 if (dinfo->disp_info->aliases == NULL) {
1071 return NT_STATUS_ACCESS_DENIED;
1075 num_aliases = pdb_search_entries(dinfo->disp_info->aliases,
1076 *r->in.resume_handle,
1077 MAX_SAM_ENTRIES, &aliases);
1080 /* Ensure we cache this enumeration. */
1081 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1083 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1084 num_aliases, aliases);
1086 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1088 if (MAX_SAM_ENTRIES <= num_aliases) {
1089 status = STATUS_MORE_ENTRIES;
1091 status = NT_STATUS_OK;
1094 samr_array->count = num_aliases;
1095 samr_array->entries = samr_entries;
1097 *r->out.sam = samr_array;
1098 *r->out.num_entries = num_aliases;
1099 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1104 /*******************************************************************
1105 inits a samr_DispInfoGeneral structure.
1106 ********************************************************************/
1108 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1109 struct samr_DispInfoGeneral *r,
1110 uint32_t num_entries,
1112 struct samr_displayentry *entries)
1116 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1118 if (num_entries == 0) {
1119 return NT_STATUS_OK;
1122 r->count = num_entries;
1124 r->entries = talloc_zero_array(ctx, struct samr_DispEntryGeneral, num_entries);
1126 return NT_STATUS_NO_MEMORY;
1129 for (i = 0; i < num_entries ; i++) {
1131 init_lsa_String(&r->entries[i].account_name,
1132 entries[i].account_name);
1134 init_lsa_String(&r->entries[i].description,
1135 entries[i].description);
1137 init_lsa_String(&r->entries[i].full_name,
1138 entries[i].fullname);
1140 r->entries[i].rid = entries[i].rid;
1141 r->entries[i].acct_flags = entries[i].acct_flags;
1142 r->entries[i].idx = start_idx+i+1;
1145 return NT_STATUS_OK;
1148 /*******************************************************************
1149 inits a samr_DispInfoFull structure.
1150 ********************************************************************/
1152 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1153 struct samr_DispInfoFull *r,
1154 uint32_t num_entries,
1156 struct samr_displayentry *entries)
1160 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1162 if (num_entries == 0) {
1163 return NT_STATUS_OK;
1166 r->count = num_entries;
1168 r->entries = talloc_zero_array(ctx, struct samr_DispEntryFull, num_entries);
1170 return NT_STATUS_NO_MEMORY;
1173 for (i = 0; i < num_entries ; i++) {
1175 init_lsa_String(&r->entries[i].account_name,
1176 entries[i].account_name);
1178 init_lsa_String(&r->entries[i].description,
1179 entries[i].description);
1181 r->entries[i].rid = entries[i].rid;
1182 r->entries[i].acct_flags = entries[i].acct_flags;
1183 r->entries[i].idx = start_idx+i+1;
1186 return NT_STATUS_OK;
1189 /*******************************************************************
1190 inits a samr_DispInfoFullGroups structure.
1191 ********************************************************************/
1193 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1194 struct samr_DispInfoFullGroups *r,
1195 uint32_t num_entries,
1197 struct samr_displayentry *entries)
1201 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1203 if (num_entries == 0) {
1204 return NT_STATUS_OK;
1207 r->count = num_entries;
1209 r->entries = talloc_zero_array(ctx, struct samr_DispEntryFullGroup, num_entries);
1211 return NT_STATUS_NO_MEMORY;
1214 for (i = 0; i < num_entries ; i++) {
1216 init_lsa_String(&r->entries[i].account_name,
1217 entries[i].account_name);
1219 init_lsa_String(&r->entries[i].description,
1220 entries[i].description);
1222 r->entries[i].rid = entries[i].rid;
1223 r->entries[i].acct_flags = entries[i].acct_flags;
1224 r->entries[i].idx = start_idx+i+1;
1227 return NT_STATUS_OK;
1230 /*******************************************************************
1231 inits a samr_DispInfoAscii structure.
1232 ********************************************************************/
1234 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1235 struct samr_DispInfoAscii *r,
1236 uint32_t num_entries,
1238 struct samr_displayentry *entries)
1242 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1244 if (num_entries == 0) {
1245 return NT_STATUS_OK;
1248 r->count = num_entries;
1250 r->entries = talloc_zero_array(ctx, struct samr_DispEntryAscii, num_entries);
1252 return NT_STATUS_NO_MEMORY;
1255 for (i = 0; i < num_entries ; i++) {
1257 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1258 entries[i].account_name);
1260 r->entries[i].idx = start_idx+i+1;
1263 return NT_STATUS_OK;
1266 /*******************************************************************
1267 inits a samr_DispInfoAscii structure.
1268 ********************************************************************/
1270 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1271 struct samr_DispInfoAscii *r,
1272 uint32_t num_entries,
1274 struct samr_displayentry *entries)
1278 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1280 if (num_entries == 0) {
1281 return NT_STATUS_OK;
1284 r->count = num_entries;
1286 r->entries = talloc_zero_array(ctx, struct samr_DispEntryAscii, num_entries);
1288 return NT_STATUS_NO_MEMORY;
1291 for (i = 0; i < num_entries ; i++) {
1293 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1294 entries[i].account_name);
1296 r->entries[i].idx = start_idx+i+1;
1299 return NT_STATUS_OK;
1302 /*******************************************************************
1303 _samr_QueryDisplayInfo
1304 ********************************************************************/
1306 NTSTATUS _samr_QueryDisplayInfo(struct pipes_struct *p,
1307 struct samr_QueryDisplayInfo *r)
1310 struct samr_domain_info *dinfo;
1311 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1313 uint32 max_entries = r->in.max_entries;
1315 union samr_DispInfo *disp_info = r->out.info;
1318 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1319 uint32 num_account = 0;
1320 enum remote_arch_types ra_type = get_remote_arch();
1321 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1322 struct samr_displayentry *entries = NULL;
1324 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1326 dinfo = policy_handle_find(p, r->in.domain_handle,
1327 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1328 struct samr_domain_info, &status);
1329 if (!NT_STATUS_IS_OK(status)) {
1333 if (sid_check_is_builtin(&dinfo->sid)) {
1334 DEBUG(5,("_samr_QueryDisplayInfo: no users in BUILTIN\n"));
1335 return NT_STATUS_OK;
1339 * calculate how many entries we will return.
1341 * - the number of entries the client asked
1342 * - our limit on that
1343 * - the starting point (enumeration context)
1344 * - the buffer size the client will accept
1348 * We are a lot more like W2K. Instead of reading the SAM
1349 * each time to find the records we need to send back,
1350 * we read it once and link that copy to the sam handle.
1351 * For large user list (over the MAX_SAM_ENTRIES)
1352 * it's a definitive win.
1353 * second point to notice: between enumerations
1354 * our sam is now the same as it's a snapshoot.
1355 * third point: got rid of the static SAM_USER_21 struct
1356 * no more intermediate.
1357 * con: it uses much more memory, as a full copy is stored
1360 * If you want to change it, think twice and think
1361 * of the second point , that's really important.
1366 if ((r->in.level < 1) || (r->in.level > 5)) {
1367 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1368 (unsigned int)r->in.level ));
1369 return NT_STATUS_INVALID_INFO_CLASS;
1372 /* first limit the number of entries we will return */
1373 if (r->in.max_entries > max_sam_entries) {
1374 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1375 "entries, limiting to %d\n", r->in.max_entries,
1377 max_entries = max_sam_entries;
1380 /* calculate the size and limit on the number of entries we will
1383 temp_size=max_entries*struct_size;
1385 if (temp_size > r->in.buf_size) {
1386 max_entries = MIN((r->in.buf_size / struct_size),max_entries);
1387 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1388 "only %d entries\n", max_entries));
1393 /* THe following done as ROOT. Don't return without unbecome_root(). */
1395 switch (r->in.level) {
1398 if (dinfo->disp_info->users == NULL) {
1399 dinfo->disp_info->users = pdb_search_users(
1400 dinfo->disp_info, ACB_NORMAL);
1401 if (dinfo->disp_info->users == NULL) {
1403 return NT_STATUS_ACCESS_DENIED;
1405 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1406 (unsigned int)r->in.start_idx));
1408 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1409 (unsigned int)r->in.start_idx));
1412 num_account = pdb_search_entries(dinfo->disp_info->users,
1413 r->in.start_idx, max_entries,
1417 if (dinfo->disp_info->machines == NULL) {
1418 dinfo->disp_info->machines = pdb_search_users(
1419 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
1420 if (dinfo->disp_info->machines == NULL) {
1422 return NT_STATUS_ACCESS_DENIED;
1424 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1425 (unsigned int)r->in.start_idx));
1427 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1428 (unsigned int)r->in.start_idx));
1431 num_account = pdb_search_entries(dinfo->disp_info->machines,
1432 r->in.start_idx, max_entries,
1437 if (dinfo->disp_info->groups == NULL) {
1438 dinfo->disp_info->groups = pdb_search_groups(
1440 if (dinfo->disp_info->groups == NULL) {
1442 return NT_STATUS_ACCESS_DENIED;
1444 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1445 (unsigned int)r->in.start_idx));
1447 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1448 (unsigned int)r->in.start_idx));
1451 num_account = pdb_search_entries(dinfo->disp_info->groups,
1452 r->in.start_idx, max_entries,
1457 smb_panic("info class changed");
1463 /* Now create reply structure */
1464 switch (r->in.level) {
1466 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1467 num_account, r->in.start_idx,
1471 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1472 num_account, r->in.start_idx,
1476 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1477 num_account, r->in.start_idx,
1481 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1482 num_account, r->in.start_idx,
1486 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1487 num_account, r->in.start_idx,
1491 smb_panic("info class changed");
1495 if (!NT_STATUS_IS_OK(disp_ret))
1498 if (max_entries <= num_account) {
1499 status = STATUS_MORE_ENTRIES;
1501 status = NT_STATUS_OK;
1504 /* Ensure we cache this enumeration. */
1505 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1507 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1509 *r->out.total_size = num_account * struct_size;
1510 *r->out.returned_size = num_account ? temp_size : 0;
1515 /****************************************************************
1516 _samr_QueryDisplayInfo2
1517 ****************************************************************/
1519 NTSTATUS _samr_QueryDisplayInfo2(struct pipes_struct *p,
1520 struct samr_QueryDisplayInfo2 *r)
1522 struct samr_QueryDisplayInfo q;
1524 q.in.domain_handle = r->in.domain_handle;
1525 q.in.level = r->in.level;
1526 q.in.start_idx = r->in.start_idx;
1527 q.in.max_entries = r->in.max_entries;
1528 q.in.buf_size = r->in.buf_size;
1530 q.out.total_size = r->out.total_size;
1531 q.out.returned_size = r->out.returned_size;
1532 q.out.info = r->out.info;
1534 return _samr_QueryDisplayInfo(p, &q);
1537 /****************************************************************
1538 _samr_QueryDisplayInfo3
1539 ****************************************************************/
1541 NTSTATUS _samr_QueryDisplayInfo3(struct pipes_struct *p,
1542 struct samr_QueryDisplayInfo3 *r)
1544 struct samr_QueryDisplayInfo q;
1546 q.in.domain_handle = r->in.domain_handle;
1547 q.in.level = r->in.level;
1548 q.in.start_idx = r->in.start_idx;
1549 q.in.max_entries = r->in.max_entries;
1550 q.in.buf_size = r->in.buf_size;
1552 q.out.total_size = r->out.total_size;
1553 q.out.returned_size = r->out.returned_size;
1554 q.out.info = r->out.info;
1556 return _samr_QueryDisplayInfo(p, &q);
1559 /*******************************************************************
1560 _samr_QueryAliasInfo
1561 ********************************************************************/
1563 NTSTATUS _samr_QueryAliasInfo(struct pipes_struct *p,
1564 struct samr_QueryAliasInfo *r)
1566 struct samr_alias_info *ainfo;
1567 struct acct_info *info;
1569 union samr_AliasInfo *alias_info = NULL;
1570 const char *alias_name = NULL;
1571 const char *alias_description = NULL;
1573 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1575 ainfo = policy_handle_find(p, r->in.alias_handle,
1576 SAMR_ALIAS_ACCESS_LOOKUP_INFO, NULL,
1577 struct samr_alias_info, &status);
1578 if (!NT_STATUS_IS_OK(status)) {
1582 alias_info = talloc_zero(p->mem_ctx, union samr_AliasInfo);
1584 return NT_STATUS_NO_MEMORY;
1587 info = talloc_zero(p->mem_ctx, struct acct_info);
1589 return NT_STATUS_NO_MEMORY;
1593 status = pdb_get_aliasinfo(&ainfo->sid, info);
1596 if (!NT_STATUS_IS_OK(status)) {
1601 alias_name = talloc_steal(r, info->acct_name);
1602 alias_description = talloc_steal(r, info->acct_desc);
1605 switch (r->in.level) {
1607 alias_info->all.name.string = alias_name;
1608 alias_info->all.num_members = 1; /* ??? */
1609 alias_info->all.description.string = alias_description;
1612 alias_info->name.string = alias_name;
1614 case ALIASINFODESCRIPTION:
1615 alias_info->description.string = alias_description;
1618 return NT_STATUS_INVALID_INFO_CLASS;
1621 *r->out.info = alias_info;
1623 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1625 return NT_STATUS_OK;
1628 /*******************************************************************
1630 ********************************************************************/
1632 NTSTATUS _samr_LookupNames(struct pipes_struct *p,
1633 struct samr_LookupNames *r)
1635 struct samr_domain_info *dinfo;
1638 enum lsa_SidType *type;
1640 int num_rids = r->in.num_names;
1641 struct samr_Ids rids, types;
1642 uint32_t num_mapped = 0;
1644 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1646 dinfo = policy_handle_find(p, r->in.domain_handle,
1647 0 /* Don't know the acc_bits yet */, NULL,
1648 struct samr_domain_info, &status);
1649 if (!NT_STATUS_IS_OK(status)) {
1653 if (num_rids > MAX_SAM_ENTRIES) {
1654 num_rids = MAX_SAM_ENTRIES;
1655 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1658 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1659 NT_STATUS_HAVE_NO_MEMORY(rid);
1661 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1662 NT_STATUS_HAVE_NO_MEMORY(type);
1664 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1665 sid_string_dbg(&dinfo->sid)));
1667 for (i = 0; i < num_rids; i++) {
1669 status = NT_STATUS_NONE_MAPPED;
1670 type[i] = SID_NAME_UNKNOWN;
1672 rid[i] = 0xffffffff;
1674 if (sid_check_is_builtin(&dinfo->sid)) {
1675 if (lookup_builtin_name(r->in.names[i].string,
1678 type[i] = SID_NAME_ALIAS;
1681 lookup_global_sam_name(r->in.names[i].string, 0,
1685 if (type[i] != SID_NAME_UNKNOWN) {
1690 if (num_mapped == num_rids) {
1691 status = NT_STATUS_OK;
1692 } else if (num_mapped == 0) {
1693 status = NT_STATUS_NONE_MAPPED;
1695 status = STATUS_SOME_UNMAPPED;
1698 rids.count = num_rids;
1701 types.count = num_rids;
1702 types.ids = talloc_array(p->mem_ctx, uint32_t, num_rids);
1703 NT_STATUS_HAVE_NO_MEMORY(type);
1704 for (i = 0; i < num_rids; i++) {
1705 types.ids[i] = (type[i] & 0xffffffff);
1708 *r->out.rids = rids;
1709 *r->out.types = types;
1711 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1716 /****************************************************************
1717 _samr_ChangePasswordUser
1718 ****************************************************************/
1720 NTSTATUS _samr_ChangePasswordUser(struct pipes_struct *p,
1721 struct samr_ChangePasswordUser *r)
1725 struct samr_user_info *uinfo;
1727 struct samr_Password new_lmPwdHash, new_ntPwdHash, checkHash;
1728 struct samr_Password lm_pwd, nt_pwd;
1730 uinfo = policy_handle_find(p, r->in.user_handle,
1731 SAMR_USER_ACCESS_SET_PASSWORD, NULL,
1732 struct samr_user_info, &status);
1733 if (!NT_STATUS_IS_OK(status)) {
1737 DEBUG(5,("_samr_ChangePasswordUser: sid:%s\n",
1738 sid_string_dbg(&uinfo->sid)));
1740 if (!(pwd = samu_new(NULL))) {
1741 return NT_STATUS_NO_MEMORY;
1745 ret = pdb_getsampwsid(pwd, &uinfo->sid);
1750 return NT_STATUS_WRONG_PASSWORD;
1754 const uint8_t *lm_pass, *nt_pass;
1756 lm_pass = pdb_get_lanman_passwd(pwd);
1757 nt_pass = pdb_get_nt_passwd(pwd);
1759 if (!lm_pass || !nt_pass) {
1760 status = NT_STATUS_WRONG_PASSWORD;
1764 memcpy(&lm_pwd.hash, lm_pass, sizeof(lm_pwd.hash));
1765 memcpy(&nt_pwd.hash, nt_pass, sizeof(nt_pwd.hash));
1768 /* basic sanity checking on parameters. Do this before any database ops */
1769 if (!r->in.lm_present || !r->in.nt_present ||
1770 !r->in.old_lm_crypted || !r->in.new_lm_crypted ||
1771 !r->in.old_nt_crypted || !r->in.new_nt_crypted) {
1772 /* we should really handle a change with lm not
1774 status = NT_STATUS_INVALID_PARAMETER_MIX;
1778 /* decrypt and check the new lm hash */
1779 D_P16(lm_pwd.hash, r->in.new_lm_crypted->hash, new_lmPwdHash.hash);
1780 D_P16(new_lmPwdHash.hash, r->in.old_lm_crypted->hash, checkHash.hash);
1781 if (memcmp(checkHash.hash, lm_pwd.hash, 16) != 0) {
1782 status = NT_STATUS_WRONG_PASSWORD;
1786 /* decrypt and check the new nt hash */
1787 D_P16(nt_pwd.hash, r->in.new_nt_crypted->hash, new_ntPwdHash.hash);
1788 D_P16(new_ntPwdHash.hash, r->in.old_nt_crypted->hash, checkHash.hash);
1789 if (memcmp(checkHash.hash, nt_pwd.hash, 16) != 0) {
1790 status = NT_STATUS_WRONG_PASSWORD;
1794 /* The NT Cross is not required by Win2k3 R2, but if present
1795 check the nt cross hash */
1796 if (r->in.cross1_present && r->in.nt_cross) {
1797 D_P16(lm_pwd.hash, r->in.nt_cross->hash, checkHash.hash);
1798 if (memcmp(checkHash.hash, new_ntPwdHash.hash, 16) != 0) {
1799 status = NT_STATUS_WRONG_PASSWORD;
1804 /* The LM Cross is not required by Win2k3 R2, but if present
1805 check the lm cross hash */
1806 if (r->in.cross2_present && r->in.lm_cross) {
1807 D_P16(nt_pwd.hash, r->in.lm_cross->hash, checkHash.hash);
1808 if (memcmp(checkHash.hash, new_lmPwdHash.hash, 16) != 0) {
1809 status = NT_STATUS_WRONG_PASSWORD;
1814 if (!pdb_set_nt_passwd(pwd, new_ntPwdHash.hash, PDB_CHANGED) ||
1815 !pdb_set_lanman_passwd(pwd, new_lmPwdHash.hash, PDB_CHANGED)) {
1816 status = NT_STATUS_ACCESS_DENIED;
1820 status = pdb_update_sam_account(pwd);
1827 /*******************************************************************
1828 _samr_ChangePasswordUser2
1829 ********************************************************************/
1831 NTSTATUS _samr_ChangePasswordUser2(struct pipes_struct *p,
1832 struct samr_ChangePasswordUser2 *r)
1835 char *user_name = NULL;
1837 const char *wks = NULL;
1839 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1841 if (!r->in.account->string) {
1842 return NT_STATUS_INVALID_PARAMETER;
1844 if (r->in.server && r->in.server->string) {
1845 wks = r->in.server->string;
1848 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1851 * Pass the user through the NT -> unix user mapping
1855 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
1857 return NT_STATUS_NO_MEMORY;
1860 rhost = tsocket_address_inet_addr_string(p->remote_address,
1862 if (rhost == NULL) {
1863 return NT_STATUS_NO_MEMORY;
1867 * UNIX username case mangling not required, pass_oem_change
1868 * is case insensitive.
1871 status = pass_oem_change(user_name,
1873 r->in.lm_password->data,
1874 r->in.lm_verifier->hash,
1875 r->in.nt_password->data,
1876 r->in.nt_verifier->hash,
1879 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1881 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1882 return NT_STATUS_WRONG_PASSWORD;
1888 /****************************************************************
1889 _samr_OemChangePasswordUser2
1890 ****************************************************************/
1892 NTSTATUS _samr_OemChangePasswordUser2(struct pipes_struct *p,
1893 struct samr_OemChangePasswordUser2 *r)
1896 char *user_name = NULL;
1897 const char *wks = NULL;
1900 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
1902 if (!r->in.account->string) {
1903 return NT_STATUS_INVALID_PARAMETER;
1905 if (r->in.server && r->in.server->string) {
1906 wks = r->in.server->string;
1909 DEBUG(5,("_samr_OemChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1912 * Pass the user through the NT -> unix user mapping
1916 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
1918 return NT_STATUS_NO_MEMORY;
1922 * UNIX username case mangling not required, pass_oem_change
1923 * is case insensitive.
1926 if (!r->in.hash || !r->in.password) {
1927 return NT_STATUS_INVALID_PARAMETER;
1930 rhost = tsocket_address_inet_addr_string(p->remote_address,
1932 if (rhost == NULL) {
1933 return NT_STATUS_NO_MEMORY;
1936 status = pass_oem_change(user_name,
1938 r->in.password->data,
1944 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1945 return NT_STATUS_WRONG_PASSWORD;
1948 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
1953 /*******************************************************************
1954 _samr_ChangePasswordUser3
1955 ********************************************************************/
1957 NTSTATUS _samr_ChangePasswordUser3(struct pipes_struct *p,
1958 struct samr_ChangePasswordUser3 *r)
1961 char *user_name = NULL;
1962 const char *wks = NULL;
1963 enum samPwdChangeReason reject_reason;
1964 struct samr_DomInfo1 *dominfo = NULL;
1965 struct userPwdChangeFailureInformation *reject = NULL;
1969 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1971 if (!r->in.account->string) {
1972 return NT_STATUS_INVALID_PARAMETER;
1974 if (r->in.server && r->in.server->string) {
1975 wks = r->in.server->string;
1978 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1981 * Pass the user through the NT -> unix user mapping
1985 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
1987 return NT_STATUS_NO_MEMORY;
1990 rhost = tsocket_address_inet_addr_string(p->remote_address,
1992 if (rhost == NULL) {
1993 return NT_STATUS_NO_MEMORY;
1997 * UNIX username case mangling not required, pass_oem_change
1998 * is case insensitive.
2001 status = pass_oem_change(user_name,
2003 r->in.lm_password->data,
2004 r->in.lm_verifier->hash,
2005 r->in.nt_password->data,
2006 r->in.nt_verifier->hash,
2008 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2009 return NT_STATUS_WRONG_PASSWORD;
2012 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
2013 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
2015 time_t u_expire, u_min_age;
2016 uint32 account_policy_temp;
2018 dominfo = talloc_zero(p->mem_ctx, struct samr_DomInfo1);
2020 return NT_STATUS_NO_MEMORY;
2023 reject = talloc_zero(p->mem_ctx,
2024 struct userPwdChangeFailureInformation);
2026 return NT_STATUS_NO_MEMORY;
2033 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &tmp);
2034 dominfo->min_password_length = tmp;
2036 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &tmp);
2037 dominfo->password_history_length = tmp;
2039 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
2040 &dominfo->password_properties);
2042 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
2043 u_expire = account_policy_temp;
2045 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
2046 u_min_age = account_policy_temp;
2052 unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
2053 unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
2055 if (lp_check_password_script() && *lp_check_password_script()) {
2056 dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
2059 reject->extendedFailureReason = reject_reason;
2061 *r->out.dominfo = dominfo;
2062 *r->out.reject = reject;
2065 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2070 /*******************************************************************
2071 makes a SAMR_R_LOOKUP_RIDS structure.
2072 ********************************************************************/
2074 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
2076 struct lsa_String **lsa_name_array_p)
2078 struct lsa_String *lsa_name_array = NULL;
2081 *lsa_name_array_p = NULL;
2083 if (num_names != 0) {
2084 lsa_name_array = talloc_zero_array(ctx, struct lsa_String, num_names);
2085 if (!lsa_name_array) {
2090 for (i = 0; i < num_names; i++) {
2091 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2092 init_lsa_String(&lsa_name_array[i], names[i]);
2095 *lsa_name_array_p = lsa_name_array;
2100 /*******************************************************************
2102 ********************************************************************/
2104 NTSTATUS _samr_LookupRids(struct pipes_struct *p,
2105 struct samr_LookupRids *r)
2107 struct samr_domain_info *dinfo;
2110 enum lsa_SidType *attrs = NULL;
2111 uint32 *wire_attrs = NULL;
2112 int num_rids = (int)r->in.num_rids;
2114 struct lsa_Strings names_array;
2115 struct samr_Ids types_array;
2116 struct lsa_String *lsa_names = NULL;
2118 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2120 dinfo = policy_handle_find(p, r->in.domain_handle,
2121 0 /* Don't know the acc_bits yet */, NULL,
2122 struct samr_domain_info, &status);
2123 if (!NT_STATUS_IS_OK(status)) {
2127 if (num_rids > 1000) {
2128 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2129 "to samba4 idl this is not possible\n", num_rids));
2130 return NT_STATUS_UNSUCCESSFUL;
2134 names = talloc_zero_array(p->mem_ctx, const char *, num_rids);
2135 attrs = talloc_zero_array(p->mem_ctx, enum lsa_SidType, num_rids);
2136 wire_attrs = talloc_zero_array(p->mem_ctx, uint32, num_rids);
2138 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2139 return NT_STATUS_NO_MEMORY;
2146 become_root(); /* lookup_sid can require root privs */
2147 status = pdb_lookup_rids(&dinfo->sid, num_rids, r->in.rids,
2151 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2152 status = NT_STATUS_OK;
2155 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2157 return NT_STATUS_NO_MEMORY;
2160 /* Convert from enum lsa_SidType to uint32 for wire format. */
2161 for (i = 0; i < num_rids; i++) {
2162 wire_attrs[i] = (uint32)attrs[i];
2165 names_array.count = num_rids;
2166 names_array.names = lsa_names;
2168 types_array.count = num_rids;
2169 types_array.ids = wire_attrs;
2171 *r->out.names = names_array;
2172 *r->out.types = types_array;
2174 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2179 /*******************************************************************
2181 ********************************************************************/
2183 NTSTATUS _samr_OpenUser(struct pipes_struct *p,
2184 struct samr_OpenUser *r)
2186 struct samu *sampass=NULL;
2188 struct samr_domain_info *dinfo;
2189 struct samr_user_info *uinfo;
2190 struct security_descriptor *psd = NULL;
2192 uint32 des_access = r->in.access_mask;
2193 uint32_t extra_access = 0;
2198 /* These two privileges, if != SEC_PRIV_INVALID, indicate
2199 * privileges that the user must have to complete this
2200 * operation in defience of the fixed ACL */
2201 enum sec_privilege needed_priv_1, needed_priv_2;
2204 dinfo = policy_handle_find(p, r->in.domain_handle,
2205 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
2206 struct samr_domain_info, &status);
2207 if (!NT_STATUS_IS_OK(status)) {
2211 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2212 return NT_STATUS_NO_MEMORY;
2215 /* append the user's RID to it */
2217 if (!sid_compose(&sid, &dinfo->sid, r->in.rid))
2218 return NT_STATUS_NO_SUCH_USER;
2220 /* check if access can be granted as requested by client. */
2221 map_max_allowed_access(p->session_info->security_token,
2222 p->session_info->unix_token,
2225 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2226 se_map_generic(&des_access, &usr_generic_mapping);
2229 * Get the sampass first as we need to check privileges
2230 * based on what kind of user object this is.
2231 * But don't reveal info too early if it didn't exist.
2235 ret=pdb_getsampwsid(sampass, &sid);
2238 needed_priv_1 = SEC_PRIV_INVALID;
2239 needed_priv_2 = SEC_PRIV_INVALID;
2241 * We do the override access checks on *open*, not at
2245 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
2247 if (acb_info & ACB_WSTRUST) {
2249 * SeMachineAccount is needed to add
2250 * GENERIC_RIGHTS_USER_WRITE to a machine
2253 needed_priv_1 = SEC_PRIV_MACHINE_ACCOUNT;
2255 if (acb_info & ACB_NORMAL) {
2257 * SeAddUsers is needed to add
2258 * GENERIC_RIGHTS_USER_WRITE to a normal
2261 needed_priv_1 = SEC_PRIV_ADD_USERS;
2264 * Cheat - we have not set a specific privilege for
2265 * server (BDC) or domain trust account, so allow
2266 * GENERIC_RIGHTS_USER_WRITE if pipe user is in
2267 * DOMAIN_RID_ADMINS.
2269 if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
2270 if (lp_enable_privileges() && nt_token_check_domain_rid(p->session_info->security_token,
2271 DOMAIN_RID_ADMINS)) {
2272 des_access &= ~GENERIC_RIGHTS_USER_WRITE;
2273 extra_access = GENERIC_RIGHTS_USER_WRITE;
2274 DEBUG(4,("_samr_OpenUser: Allowing "
2275 "GENERIC_RIGHTS_USER_WRITE for "
2281 TALLOC_FREE(sampass);
2283 nt_status = access_check_object(psd, p->session_info->security_token,
2284 needed_priv_1, needed_priv_2,
2285 GENERIC_RIGHTS_USER_WRITE, des_access,
2286 &acc_granted, "_samr_OpenUser");
2288 if ( !NT_STATUS_IS_OK(nt_status) )
2291 /* check that the SID exists in our domain. */
2293 return NT_STATUS_NO_SUCH_USER;
2296 /* If we did the rid admins hack above, allow access. */
2297 acc_granted |= extra_access;
2299 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
2300 struct samr_user_info, &nt_status);
2301 if (!NT_STATUS_IS_OK(nt_status)) {
2306 return NT_STATUS_OK;
2309 /*************************************************************************
2310 *************************************************************************/
2312 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2314 struct lsa_BinaryString **_r)
2316 struct lsa_BinaryString *r;
2319 return NT_STATUS_INVALID_PARAMETER;
2322 r = talloc_zero(mem_ctx, struct lsa_BinaryString);
2324 return NT_STATUS_NO_MEMORY;
2327 r->array = talloc_zero_array(mem_ctx, uint16_t, blob->length/2);
2329 return NT_STATUS_NO_MEMORY;
2331 memcpy(r->array, blob->data, blob->length);
2332 r->size = blob->length;
2333 r->length = blob->length;
2336 return NT_STATUS_NO_MEMORY;
2341 return NT_STATUS_OK;
2344 /*************************************************************************
2345 *************************************************************************/
2347 static struct samr_LogonHours get_logon_hours_from_pdb(TALLOC_CTX *mem_ctx,
2350 struct samr_LogonHours hours;
2351 const int units_per_week = 168;
2354 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
2359 hours.units_per_week = units_per_week;
2360 memset(hours.bits, 0xFF, units_per_week);
2362 if (pdb_get_hours(pw)) {
2363 memcpy(hours.bits, pdb_get_hours(pw),
2364 MIN(pdb_get_hours_len(pw), units_per_week));
2370 /*************************************************************************
2372 *************************************************************************/
2374 static NTSTATUS get_user_info_1(TALLOC_CTX *mem_ctx,
2375 struct samr_UserInfo1 *r,
2377 struct dom_sid *domain_sid)
2379 const struct dom_sid *sid_group;
2380 uint32_t primary_gid;
2383 sid_group = pdb_get_group_sid(pw);
2386 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2387 DEBUG(0, ("get_user_info_1: User %s has Primary Group SID %s, \n"
2388 "which conflicts with the domain sid %s. Failing operation.\n",
2389 pdb_get_username(pw), sid_string_dbg(sid_group),
2390 sid_string_dbg(domain_sid)));
2391 return NT_STATUS_UNSUCCESSFUL;
2394 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2395 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2396 r->primary_gid = primary_gid;
2397 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2398 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2400 return NT_STATUS_OK;
2403 /*************************************************************************
2405 *************************************************************************/
2407 static NTSTATUS get_user_info_2(TALLOC_CTX *mem_ctx,
2408 struct samr_UserInfo2 *r,
2411 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2412 r->reserved.string = NULL;
2413 r->country_code = pdb_get_country_code(pw);
2414 r->code_page = pdb_get_code_page(pw);
2416 return NT_STATUS_OK;
2419 /*************************************************************************
2421 *************************************************************************/
2423 static NTSTATUS get_user_info_3(TALLOC_CTX *mem_ctx,
2424 struct samr_UserInfo3 *r,
2426 struct dom_sid *domain_sid)
2428 const struct dom_sid *sid_user, *sid_group;
2429 uint32_t rid, primary_gid;
2431 sid_user = pdb_get_user_sid(pw);
2433 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2434 DEBUG(0, ("get_user_info_3: User %s has SID %s, \nwhich conflicts with "
2435 "the domain sid %s. Failing operation.\n",
2436 pdb_get_username(pw), sid_string_dbg(sid_user),
2437 sid_string_dbg(domain_sid)));
2438 return NT_STATUS_UNSUCCESSFUL;
2442 sid_group = pdb_get_group_sid(pw);
2445 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2446 DEBUG(0, ("get_user_info_3: User %s has Primary Group SID %s, \n"
2447 "which conflicts with the domain sid %s. Failing operation.\n",
2448 pdb_get_username(pw), sid_string_dbg(sid_group),
2449 sid_string_dbg(domain_sid)));
2450 return NT_STATUS_UNSUCCESSFUL;
2453 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2454 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2455 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2456 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2457 unix_to_nt_time(&r->force_password_change, pdb_get_pass_must_change_time(pw));
2459 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2460 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2461 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2462 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2463 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2464 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2465 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2467 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2469 r->primary_gid = primary_gid;
2470 r->acct_flags = pdb_get_acct_ctrl(pw);
2471 r->bad_password_count = pdb_get_bad_password_count(pw);
2472 r->logon_count = pdb_get_logon_count(pw);
2474 return NT_STATUS_OK;
2477 /*************************************************************************
2479 *************************************************************************/
2481 static NTSTATUS get_user_info_4(TALLOC_CTX *mem_ctx,
2482 struct samr_UserInfo4 *r,
2485 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2487 return NT_STATUS_OK;
2490 /*************************************************************************
2492 *************************************************************************/
2494 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2495 struct samr_UserInfo5 *r,
2497 struct dom_sid *domain_sid)
2499 const struct dom_sid *sid_user, *sid_group;
2500 uint32_t rid, primary_gid;
2502 sid_user = pdb_get_user_sid(pw);
2504 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2505 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2506 "the domain sid %s. Failing operation.\n",
2507 pdb_get_username(pw), sid_string_dbg(sid_user),
2508 sid_string_dbg(domain_sid)));
2509 return NT_STATUS_UNSUCCESSFUL;
2513 sid_group = pdb_get_group_sid(pw);
2516 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2517 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2518 "which conflicts with the domain sid %s. Failing operation.\n",
2519 pdb_get_username(pw), sid_string_dbg(sid_group),
2520 sid_string_dbg(domain_sid)));
2521 return NT_STATUS_UNSUCCESSFUL;
2524 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2525 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2526 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2527 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2529 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2530 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2531 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2532 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2533 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2534 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2535 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2536 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2538 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2540 r->primary_gid = primary_gid;
2541 r->acct_flags = pdb_get_acct_ctrl(pw);
2542 r->bad_password_count = pdb_get_bad_password_count(pw);
2543 r->logon_count = pdb_get_logon_count(pw);
2545 return NT_STATUS_OK;
2548 /*************************************************************************
2550 *************************************************************************/
2552 static NTSTATUS get_user_info_6(TALLOC_CTX *mem_ctx,
2553 struct samr_UserInfo6 *r,
2556 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2557 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2559 return NT_STATUS_OK;
2562 /*************************************************************************
2563 get_user_info_7. Safe. Only gives out account_name.
2564 *************************************************************************/
2566 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2567 struct samr_UserInfo7 *r,
2568 struct samu *smbpass)
2570 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2571 if (!r->account_name.string) {
2572 return NT_STATUS_NO_MEMORY;
2575 return NT_STATUS_OK;
2578 /*************************************************************************
2580 *************************************************************************/
2582 static NTSTATUS get_user_info_8(TALLOC_CTX *mem_ctx,
2583 struct samr_UserInfo8 *r,
2586 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2588 return NT_STATUS_OK;
2591 /*************************************************************************
2592 get_user_info_9. Only gives out primary group SID.
2593 *************************************************************************/
2595 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2596 struct samr_UserInfo9 *r,
2597 struct samu *smbpass)
2599 r->primary_gid = pdb_get_group_rid(smbpass);
2601 return NT_STATUS_OK;
2604 /*************************************************************************
2606 *************************************************************************/
2608 static NTSTATUS get_user_info_10(TALLOC_CTX *mem_ctx,
2609 struct samr_UserInfo10 *r,
2612 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2613 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2615 return NT_STATUS_OK;
2618 /*************************************************************************
2620 *************************************************************************/
2622 static NTSTATUS get_user_info_11(TALLOC_CTX *mem_ctx,
2623 struct samr_UserInfo11 *r,
2626 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2628 return NT_STATUS_OK;
2631 /*************************************************************************
2633 *************************************************************************/
2635 static NTSTATUS get_user_info_12(TALLOC_CTX *mem_ctx,
2636 struct samr_UserInfo12 *r,
2639 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2641 return NT_STATUS_OK;
2644 /*************************************************************************
2646 *************************************************************************/
2648 static NTSTATUS get_user_info_13(TALLOC_CTX *mem_ctx,
2649 struct samr_UserInfo13 *r,
2652 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2654 return NT_STATUS_OK;
2657 /*************************************************************************
2659 *************************************************************************/
2661 static NTSTATUS get_user_info_14(TALLOC_CTX *mem_ctx,
2662 struct samr_UserInfo14 *r,
2665 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2667 return NT_STATUS_OK;
2670 /*************************************************************************
2671 get_user_info_16. Safe. Only gives out acb bits.
2672 *************************************************************************/
2674 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2675 struct samr_UserInfo16 *r,
2676 struct samu *smbpass)
2678 r->acct_flags = pdb_get_acct_ctrl(smbpass);
2680 return NT_STATUS_OK;
2683 /*************************************************************************
2685 *************************************************************************/
2687 static NTSTATUS get_user_info_17(TALLOC_CTX *mem_ctx,
2688 struct samr_UserInfo17 *r,
2691 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2693 return NT_STATUS_OK;
2696 /*************************************************************************
2697 get_user_info_18. OK - this is the killer as it gives out password info.
2698 Ensure that this is only allowed on an encrypted connection with a root
2700 *************************************************************************/
2702 static NTSTATUS get_user_info_18(struct pipes_struct *p,
2703 TALLOC_CTX *mem_ctx,
2704 struct samr_UserInfo18 *r,
2705 struct dom_sid *user_sid)
2707 struct samu *smbpass=NULL;
2709 const uint8_t *nt_pass = NULL;
2710 const uint8_t *lm_pass = NULL;
2714 if (p->session_info->unix_info->system) {
2718 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) ||
2719 (p->auth.auth_type != DCERPC_AUTH_TYPE_KRB5) ||
2720 (p->auth.auth_type != DCERPC_AUTH_TYPE_SPNEGO)) {
2721 return NT_STATUS_ACCESS_DENIED;
2724 if (p->auth.auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
2725 return NT_STATUS_ACCESS_DENIED;
2730 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2733 if ( !(smbpass = samu_new( mem_ctx )) ) {
2734 return NT_STATUS_NO_MEMORY;
2737 ret = pdb_getsampwsid(smbpass, user_sid);
2740 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2741 TALLOC_FREE(smbpass);
2742 return (geteuid() == sec_initial_uid()) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2745 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2747 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2748 TALLOC_FREE(smbpass);
2749 return NT_STATUS_ACCOUNT_DISABLED;
2752 lm_pass = pdb_get_lanman_passwd(smbpass);
2753 if (lm_pass != NULL) {
2754 memcpy(r->lm_pwd.hash, lm_pass, 16);
2755 r->lm_pwd_active = true;
2758 nt_pass = pdb_get_nt_passwd(smbpass);
2759 if (nt_pass != NULL) {
2760 memcpy(r->nt_pwd.hash, nt_pass, 16);
2761 r->nt_pwd_active = true;
2763 r->password_expired = 0; /* FIXME */
2765 TALLOC_FREE(smbpass);
2767 return NT_STATUS_OK;
2770 /*************************************************************************
2772 *************************************************************************/
2774 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2775 struct samr_UserInfo20 *r,
2776 struct samu *sampass)
2778 const char *munged_dial = NULL;
2781 struct lsa_BinaryString *parameters = NULL;
2785 munged_dial = pdb_get_munged_dial(sampass);
2787 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2788 munged_dial, (int)strlen(munged_dial)));
2791 blob = base64_decode_data_blob(munged_dial);
2793 blob = data_blob_string_const_null("");
2796 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2797 data_blob_free(&blob);
2798 if (!NT_STATUS_IS_OK(status)) {
2802 r->parameters = *parameters;
2804 return NT_STATUS_OK;
2808 /*************************************************************************
2810 *************************************************************************/
2812 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2813 struct samr_UserInfo21 *r,
2815 struct dom_sid *domain_sid,
2816 uint32_t acc_granted)
2819 const struct dom_sid *sid_user, *sid_group;
2820 uint32_t rid, primary_gid;
2821 NTTIME force_password_change;
2822 time_t must_change_time;
2823 struct lsa_BinaryString *parameters = NULL;
2824 const char *munged_dial = NULL;
2829 sid_user = pdb_get_user_sid(pw);
2831 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2832 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2833 "the domain sid %s. Failing operation.\n",
2834 pdb_get_username(pw), sid_string_dbg(sid_user),
2835 sid_string_dbg(domain_sid)));
2836 return NT_STATUS_UNSUCCESSFUL;
2840 sid_group = pdb_get_group_sid(pw);
2843 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2844 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2845 "which conflicts with the domain sid %s. Failing operation.\n",
2846 pdb_get_username(pw), sid_string_dbg(sid_group),
2847 sid_string_dbg(domain_sid)));
2848 return NT_STATUS_UNSUCCESSFUL;
2851 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2852 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2853 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2854 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2855 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2857 must_change_time = pdb_get_pass_must_change_time(pw);
2858 if (pdb_is_password_change_time_max(must_change_time)) {
2859 unix_to_nt_time_abs(&force_password_change, must_change_time);
2861 unix_to_nt_time(&force_password_change, must_change_time);
2864 munged_dial = pdb_get_munged_dial(pw);
2866 blob = base64_decode_data_blob(munged_dial);
2868 blob = data_blob_string_const_null("");
2871 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2872 data_blob_free(&blob);
2873 if (!NT_STATUS_IS_OK(status)) {
2877 r->force_password_change = force_password_change;
2879 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2880 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2881 r->home_directory.string = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2882 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2883 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2884 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2885 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2886 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2887 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2889 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2890 r->parameters = *parameters;
2892 r->primary_gid = primary_gid;
2893 r->acct_flags = pdb_get_acct_ctrl(pw);
2894 r->bad_password_count = pdb_get_bad_password_count(pw);
2895 r->logon_count = pdb_get_logon_count(pw);
2896 r->fields_present = pdb_build_fields_present(pw);
2897 r->password_expired = (pdb_get_pass_must_change_time(pw) == 0) ?
2898 PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
2899 r->country_code = pdb_get_country_code(pw);
2900 r->code_page = pdb_get_code_page(pw);
2901 r->lm_password_set = 0;
2902 r->nt_password_set = 0;
2907 Look at a user on a real NT4 PDC with usrmgr, press
2908 'ok'. Then you will see that fields_present is set to
2909 0x08f827fa. Look at the user immediately after that again,
2910 and you will see that 0x00fffff is returned. This solves
2911 the problem that you get access denied after having looked
2919 return NT_STATUS_OK;
2922 /*******************************************************************
2924 ********************************************************************/
2926 NTSTATUS _samr_QueryUserInfo(struct pipes_struct *p,
2927 struct samr_QueryUserInfo *r)
2930 union samr_UserInfo *user_info = NULL;
2931 struct samr_user_info *uinfo;
2932 struct dom_sid domain_sid;
2935 struct samu *pwd = NULL;
2936 uint32_t acc_required, acc_granted;
2938 switch (r->in.level) {
2939 case 1: /* UserGeneralInformation */
2940 /* USER_READ_GENERAL */
2941 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
2943 case 2: /* UserPreferencesInformation */
2944 /* USER_READ_PREFERENCES | USER_READ_GENERAL */
2945 acc_required = SAMR_USER_ACCESS_GET_LOCALE |
2946 SAMR_USER_ACCESS_GET_NAME_ETC;
2948 case 3: /* UserLogonInformation */
2949 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
2950 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
2951 SAMR_USER_ACCESS_GET_LOCALE |
2952 SAMR_USER_ACCESS_GET_LOGONINFO |
2953 SAMR_USER_ACCESS_GET_ATTRIBUTES;
2955 case 4: /* UserLogonHoursInformation */
2956 /* USER_READ_LOGON */
2957 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
2959 case 5: /* UserAccountInformation */
2960 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
2961 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
2962 SAMR_USER_ACCESS_GET_LOCALE |
2963 SAMR_USER_ACCESS_GET_LOGONINFO |
2964 SAMR_USER_ACCESS_GET_ATTRIBUTES;
2966 case 6: /* UserNameInformation */
2967 case 7: /* UserAccountNameInformation */
2968 case 8: /* UserFullNameInformation */
2969 case 9: /* UserPrimaryGroupInformation */
2970 case 13: /* UserAdminCommentInformation */
2971 /* USER_READ_GENERAL */
2972 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
2974 case 10: /* UserHomeInformation */
2975 case 11: /* UserScriptInformation */
2976 case 12: /* UserProfileInformation */
2977 case 14: /* UserWorkStationsInformation */
2978 /* USER_READ_LOGON */
2979 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
2981 case 16: /* UserControlInformation */
2982 case 17: /* UserExpiresInformation */
2983 case 20: /* UserParametersInformation */
2984 /* USER_READ_ACCOUNT */
2985 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
2987 case 21: /* UserAllInformation */
2989 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
2991 case 18: /* UserInternal1Information */
2993 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
2995 case 23: /* UserInternal4Information */
2996 case 24: /* UserInternal4InformationNew */
2997 case 25: /* UserInternal4InformationNew */
2998 case 26: /* UserInternal5InformationNew */
3000 return NT_STATUS_INVALID_INFO_CLASS;
3004 uinfo = policy_handle_find(p, r->in.user_handle,
3005 acc_required, &acc_granted,
3006 struct samr_user_info, &status);
3007 if (!NT_STATUS_IS_OK(status)) {
3011 domain_sid = uinfo->sid;
3013 sid_split_rid(&domain_sid, &rid);
3015 if (!sid_check_is_in_our_domain(&uinfo->sid))
3016 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3018 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
3019 sid_string_dbg(&uinfo->sid)));
3021 user_info = talloc_zero(p->mem_ctx, union samr_UserInfo);
3023 return NT_STATUS_NO_MEMORY;
3026 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
3028 if (!(pwd = samu_new(p->mem_ctx))) {
3029 return NT_STATUS_NO_MEMORY;
3033 ret = pdb_getsampwsid(pwd, &uinfo->sid);
3037 DEBUG(4,("User %s not found\n", sid_string_dbg(&uinfo->sid)));
3039 return NT_STATUS_NO_SUCH_USER;
3042 DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
3044 samr_clear_sam_passwd(pwd);
3046 switch (r->in.level) {
3048 status = get_user_info_1(p->mem_ctx, &user_info->info1, pwd, &domain_sid);
3051 status = get_user_info_2(p->mem_ctx, &user_info->info2, pwd);
3054 status = get_user_info_3(p->mem_ctx, &user_info->info3, pwd, &domain_sid);
3057 status = get_user_info_4(p->mem_ctx, &user_info->info4, pwd);
3060 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
3063 status = get_user_info_6(p->mem_ctx, &user_info->info6, pwd);
3066 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
3069 status = get_user_info_8(p->mem_ctx, &user_info->info8, pwd);
3072 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
3075 status = get_user_info_10(p->mem_ctx, &user_info->info10, pwd);
3078 status = get_user_info_11(p->mem_ctx, &user_info->info11, pwd);
3081 status = get_user_info_12(p->mem_ctx, &user_info->info12, pwd);
3084 status = get_user_info_13(p->mem_ctx, &user_info->info13, pwd);
3087 status = get_user_info_14(p->mem_ctx, &user_info->info14, pwd);
3090 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
3093 status = get_user_info_17(p->mem_ctx, &user_info->info17, pwd);
3096 /* level 18 is special */
3097 status = get_user_info_18(p, p->mem_ctx, &user_info->info18,
3101 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
3104 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid, acc_granted);
3107 status = NT_STATUS_INVALID_INFO_CLASS;
3111 if (!NT_STATUS_IS_OK(status)) {
3115 *r->out.info = user_info;
3120 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
3125 /****************************************************************
3126 ****************************************************************/
3128 NTSTATUS _samr_QueryUserInfo2(struct pipes_struct *p,
3129 struct samr_QueryUserInfo2 *r)
3131 struct samr_QueryUserInfo u;
3133 u.in.user_handle = r->in.user_handle;
3134 u.in.level = r->in.level;
3135 u.out.info = r->out.info;
3137 return _samr_QueryUserInfo(p, &u);
3140 /*******************************************************************
3141 _samr_GetGroupsForUser
3142 ********************************************************************/
3144 NTSTATUS _samr_GetGroupsForUser(struct pipes_struct *p,
3145 struct samr_GetGroupsForUser *r)
3147 struct samr_user_info *uinfo;
3148 struct samu *sam_pass=NULL;
3149 struct dom_sid *sids;
3150 struct samr_RidWithAttribute dom_gid;
3151 struct samr_RidWithAttribute *gids = NULL;
3152 uint32 primary_group_rid;
3153 uint32_t num_groups = 0;
3155 uint32_t i, num_gids;
3158 bool success = False;
3160 struct samr_RidWithAttributeArray *rids = NULL;
3163 * from the SID in the request:
3164 * we should send back the list of DOMAIN GROUPS
3165 * the user is a member of
3167 * and only the DOMAIN GROUPS
3168 * no ALIASES !!! neither aliases of the domain
3169 * nor aliases of the builtin SID
3174 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3176 uinfo = policy_handle_find(p, r->in.user_handle,
3177 SAMR_USER_ACCESS_GET_GROUPS, NULL,
3178 struct samr_user_info, &result);
3179 if (!NT_STATUS_IS_OK(result)) {
3183 rids = talloc_zero(p->mem_ctx, struct samr_RidWithAttributeArray);
3185 return NT_STATUS_NO_MEMORY;
3188 if (!sid_check_is_in_our_domain(&uinfo->sid))
3189 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3191 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
3192 return NT_STATUS_NO_MEMORY;
3196 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
3200 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
3201 sid_string_dbg(&uinfo->sid)));
3202 return NT_STATUS_NO_SUCH_USER;
3207 /* make both calls inside the root block */
3209 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
3210 &sids, &unix_gids, &num_groups);
3211 if ( NT_STATUS_IS_OK(result) ) {
3212 success = sid_peek_check_rid(get_global_sam_sid(),
3213 pdb_get_group_sid(sam_pass),
3214 &primary_group_rid);
3218 if (!NT_STATUS_IS_OK(result)) {
3219 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
3220 sid_string_dbg(&uinfo->sid)));
3225 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
3226 sid_string_dbg(pdb_get_group_sid(sam_pass)),
3227 pdb_get_username(sam_pass)));
3228 TALLOC_FREE(sam_pass);
3229 return NT_STATUS_INTERNAL_DB_CORRUPTION;
3235 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
3237 dom_gid.rid = primary_group_rid;
3238 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3240 for (i=0; i<num_groups; i++) {
3242 if (!sid_peek_check_rid(get_global_sam_sid(),
3243 &(sids[i]), &dom_gid.rid)) {
3244 DEBUG(10, ("Found sid %s not in our domain\n",
3245 sid_string_dbg(&sids[i])));
3249 if (dom_gid.rid == primary_group_rid) {
3250 /* We added the primary group directly from the
3251 * sam_account. The other SIDs are unique from
3252 * enum_group_memberships */
3256 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3259 rids->count = num_gids;
3262 *r->out.rids = rids;
3264 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3269 /*******************************************************************
3270 ********************************************************************/
3272 static uint32_t samr_get_server_role(void)
3274 uint32_t role = ROLE_DOMAIN_PDC;
3276 if (lp_server_role() == ROLE_DOMAIN_BDC) {
3277 role = ROLE_DOMAIN_BDC;
3283 /*******************************************************************
3284 ********************************************************************/
3286 static NTSTATUS query_dom_info_1(TALLOC_CTX *mem_ctx,
3287 struct samr_DomInfo1 *r)
3289 uint32_t account_policy_temp;
3290 time_t u_expire, u_min_age;
3296 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &account_policy_temp);
3297 r->min_password_length = account_policy_temp;
3299 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &account_policy_temp);
3300 r->password_history_length = account_policy_temp;
3302 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
3303 &r->password_properties);
3305 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
3306 u_expire = account_policy_temp;
3308 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
3309 u_min_age = account_policy_temp;
3315 unix_to_nt_time_abs((NTTIME *)&r->max_password_age, u_expire);
3316 unix_to_nt_time_abs((NTTIME *)&r->min_password_age, u_min_age);
3318 if (lp_check_password_script() && *lp_check_password_script()) {
3319 r->password_properties |= DOMAIN_PASSWORD_COMPLEX;
3322 return NT_STATUS_OK;
3325 /*******************************************************************
3326 ********************************************************************/
3328 static NTSTATUS query_dom_info_2(TALLOC_CTX *mem_ctx,
3329 struct samr_DomGeneralInformation *r,
3330 struct samr_domain_info *dinfo)
3339 r->num_users = count_sam_users(dinfo->disp_info, ACB_NORMAL);
3340 r->num_groups = count_sam_groups(dinfo->disp_info);
3341 r->num_aliases = count_sam_aliases(dinfo->disp_info);
3343 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &u_logout);
3345 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3347 if (!pdb_get_seq_num(&seq_num)) {
3348 seq_num = time(NULL);
3355 r->oem_information.string = lp_serverstring();
3356 r->domain_name.string = lp_workgroup();
3357 r->primary.string = lp_netbios_name();
3358 r->sequence_num = seq_num;
3359 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3360 r->role = (enum samr_Role) samr_get_server_role();
3363 return NT_STATUS_OK;
3366 /*******************************************************************
3367 ********************************************************************/
3369 static NTSTATUS query_dom_info_3(TALLOC_CTX *mem_ctx,
3370 struct samr_DomInfo3 *r)
3380 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &ul);
3381 u_logout = (time_t)ul;
3388 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3390 return NT_STATUS_OK;
3393 /*******************************************************************
3394 ********************************************************************/
3396 static NTSTATUS query_dom_info_4(TALLOC_CTX *mem_ctx,
3397 struct samr_DomOEMInformation *r)
3399 r->oem_information.string = lp_serverstring();
3401 return NT_STATUS_OK;
3404 /*******************************************************************
3405 ********************************************************************/
3407 static NTSTATUS query_dom_info_5(TALLOC_CTX *mem_ctx,
3408 struct samr_DomInfo5 *r)
3410 r->domain_name.string = get_global_sam_name();
3412 return NT_STATUS_OK;
3415 /*******************************************************************
3416 ********************************************************************/
3418 static NTSTATUS query_dom_info_6(TALLOC_CTX *mem_ctx,
3419 struct samr_DomInfo6 *r)
3421 /* NT returns its own name when a PDC. win2k and later
3422 * only the name of the PDC if itself is a BDC (samba4
3424 r->primary.string = lp_netbios_name();
3426 return NT_STATUS_OK;
3429 /*******************************************************************
3430 ********************************************************************/
3432 static NTSTATUS query_dom_info_7(TALLOC_CTX *mem_ctx,
3433 struct samr_DomInfo7 *r)
3435 r->role = (enum samr_Role) samr_get_server_role();
3437 return NT_STATUS_OK;
3440 /*******************************************************************
3441 ********************************************************************/
3443 static NTSTATUS query_dom_info_8(TALLOC_CTX *mem_ctx,
3444 struct samr_DomInfo8 *r)
3452 if (!pdb_get_seq_num(&seq_num)) {
3453 seq_num = time(NULL);
3460 r->sequence_num = seq_num;
3461 r->domain_create_time = 0;
3463 return NT_STATUS_OK;
3466 /*******************************************************************
3467 ********************************************************************/
3469 static NTSTATUS query_dom_info_9(TALLOC_CTX *mem_ctx,
3470 struct samr_DomInfo9 *r)
3472 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3474 return NT_STATUS_OK;
3477 /*******************************************************************
3478 ********************************************************************/
3480 static NTSTATUS query_dom_info_11(TALLOC_CTX *mem_ctx,
3481 struct samr_DomGeneralInformation2 *r,
3482 struct samr_domain_info *dinfo)
3485 uint32_t account_policy_temp;
3486 time_t u_lock_duration, u_reset_time;
3488 status = query_dom_info_2(mem_ctx, &r->general, dinfo);
3489 if (!NT_STATUS_IS_OK(status)) {
3497 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3498 u_lock_duration = account_policy_temp;
3499 if (u_lock_duration != -1) {
3500 u_lock_duration *= 60;
3503 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3504 u_reset_time = account_policy_temp * 60;
3506 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3507 r->lockout_threshold = account_policy_temp;
3513 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3514 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3516 return NT_STATUS_OK;
3519 /*******************************************************************
3520 ********************************************************************/
3522 static NTSTATUS query_dom_info_12(TALLOC_CTX *mem_ctx,
3523 struct samr_DomInfo12 *r)
3525 uint32_t account_policy_temp;
3526 time_t u_lock_duration, u_reset_time;
3532 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3533 u_lock_duration = account_policy_temp;
3534 if (u_lock_duration != -1) {
3535 u_lock_duration *= 60;
3538 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3539 u_reset_time = account_policy_temp * 60;
3541 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3542 r->lockout_threshold = account_policy_temp;
3548 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3549 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3551 return NT_STATUS_OK;
3554 /*******************************************************************
3555 ********************************************************************/
3557 static NTSTATUS query_dom_info_13(TALLOC_CTX *mem_ctx,
3558 struct samr_DomInfo13 *r)
3566 if (!pdb_get_seq_num(&seq_num)) {
3567 seq_num = time(NULL);
3574 r->sequence_num = seq_num;
3575 r->domain_create_time = 0;
3576 r->modified_count_at_last_promotion = 0;
3578 return NT_STATUS_OK;
3581 /*******************************************************************
3582 _samr_QueryDomainInfo
3583 ********************************************************************/
3585 NTSTATUS _samr_QueryDomainInfo(struct pipes_struct *p,
3586 struct samr_QueryDomainInfo *r)
3588 NTSTATUS status = NT_STATUS_OK;
3589 struct samr_domain_info *dinfo;
3590 union samr_DomainInfo *dom_info;
3592 uint32_t acc_required;
3594 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3596 switch (r->in.level) {
3597 case 1: /* DomainPasswordInformation */
3598 case 12: /* DomainLockoutInformation */
3599 /* DOMAIN_READ_PASSWORD_PARAMETERS */
3600 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1;
3602 case 11: /* DomainGeneralInformation2 */
3603 /* DOMAIN_READ_PASSWORD_PARAMETERS |
3604 * DOMAIN_READ_OTHER_PARAMETERS */
3605 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1 |
3606 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3608 case 2: /* DomainGeneralInformation */
3609 case 3: /* DomainLogoffInformation */
3610 case 4: /* DomainOemInformation */
3611 case 5: /* DomainReplicationInformation */
3612 case 6: /* DomainReplicationInformation */
3613 case 7: /* DomainServerRoleInformation */
3614 case 8: /* DomainModifiedInformation */
3615 case 9: /* DomainStateInformation */
3616 case 10: /* DomainUasInformation */
3617 case 13: /* DomainModifiedInformation2 */
3618 /* DOMAIN_READ_OTHER_PARAMETERS */
3619 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3622 return NT_STATUS_INVALID_INFO_CLASS;
3625 dinfo = policy_handle_find(p, r->in.domain_handle,
3627 struct samr_domain_info, &status);
3628 if (!NT_STATUS_IS_OK(status)) {
3632 dom_info = talloc_zero(p->mem_ctx, union samr_DomainInfo);
3634 return NT_STATUS_NO_MEMORY;
3637 switch (r->in.level) {
3639 status = query_dom_info_1(p->mem_ctx, &dom_info->info1);
3642 status = query_dom_info_2(p->mem_ctx, &dom_info->general, dinfo);
3645 status = query_dom_info_3(p->mem_ctx, &dom_info->info3);
3648 status = query_dom_info_4(p->mem_ctx, &dom_info->oem);
3651 status = query_dom_info_5(p->mem_ctx, &dom_info->info5);
3654 status = query_dom_info_6(p->mem_ctx, &dom_info->info6);
3657 status = query_dom_info_7(p->mem_ctx, &dom_info->info7);
3660 status = query_dom_info_8(p->mem_ctx, &dom_info->info8);
3663 status = query_dom_info_9(p->mem_ctx, &dom_info->info9);
3666 status = query_dom_info_11(p->mem_ctx, &dom_info->general2, dinfo);
3669 status = query_dom_info_12(p->mem_ctx, &dom_info->info12);
3672 status = query_dom_info_13(p->mem_ctx, &dom_info->info13);
3675 return NT_STATUS_INVALID_INFO_CLASS;
3678 if (!NT_STATUS_IS_OK(status)) {
3682 *r->out.info = dom_info;
3684 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3689 /* W2k3 seems to use the same check for all 3 objects that can be created via
3690 * SAMR, if you try to create for example "Dialup" as an alias it says
3691 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3694 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3696 enum lsa_SidType type;
3699 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3702 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3703 * whether the name already exists */
3704 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3705 NULL, NULL, NULL, &type);
3709 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3710 return NT_STATUS_OK;
3713 DEBUG(5, ("trying to create %s, exists as %s\n",
3714 new_name, sid_type_lookup(type)));
3716 if (type == SID_NAME_DOM_GRP) {
3717 return NT_STATUS_GROUP_EXISTS;
3719 if (type == SID_NAME_ALIAS) {
3720 return NT_STATUS_ALIAS_EXISTS;
3723 /* Yes, the default is NT_STATUS_USER_EXISTS */
3724 return NT_STATUS_USER_EXISTS;
3727 /*******************************************************************
3729 ********************************************************************/
3731 NTSTATUS _samr_CreateUser2(struct pipes_struct *p,
3732 struct samr_CreateUser2 *r)
3734 const char *account = NULL;
3736 uint32_t acb_info = r->in.acct_flags;
3737 struct samr_domain_info *dinfo;
3738 struct samr_user_info *uinfo;
3741 struct security_descriptor *psd;
3743 /* check this, when giving away 'add computer to domain' privs */
3744 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3745 bool can_add_account = False;
3747 /* Which privilege is needed to override the ACL? */
3748 enum sec_privilege needed_priv = SEC_PRIV_INVALID;
3750 dinfo = policy_handle_find(p, r->in.domain_handle,
3751 SAMR_DOMAIN_ACCESS_CREATE_USER, NULL,
3752 struct samr_domain_info, &nt_status);
3753 if (!NT_STATUS_IS_OK(nt_status)) {
3757 if (sid_check_is_builtin(&dinfo->sid)) {
3758 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3759 return NT_STATUS_ACCESS_DENIED;
3762 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3763 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3764 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3765 this parameter is not an account type */
3766 return NT_STATUS_INVALID_PARAMETER;
3769 account = r->in.account_name->string;
3770 if (account == NULL) {
3771 return NT_STATUS_NO_MEMORY;
3774 nt_status = can_create(p->mem_ctx, account);
3775 if (!NT_STATUS_IS_OK(nt_status)) {
3779 /* determine which user right we need to check based on the acb_info */
3781 if (geteuid() == sec_initial_uid()) {
3782 can_add_account = true;
3783 } else if (acb_info & ACB_WSTRUST) {
3784 needed_priv = SEC_PRIV_MACHINE_ACCOUNT;
3785 can_add_account = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_MACHINE_ACCOUNT);
3786 } else if (acb_info & ACB_NORMAL &&
3787 (account[strlen(account)-1] != '$')) {
3788 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3789 account for domain trusts and changes the ACB flags later */
3790 needed_priv = SEC_PRIV_ADD_USERS;
3791 can_add_account = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_ADD_USERS);
3792 } else if (lp_enable_privileges()) {
3793 /* implicit assumption of a BDC or domain trust account here
3794 * (we already check the flags earlier) */
3795 /* only Domain Admins can add a BDC or domain trust */
3796 can_add_account = nt_token_check_domain_rid(
3797 p->session_info->security_token,
3798 DOMAIN_RID_ADMINS );
3801 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3802 uidtoname(p->session_info->unix_token->uid),
3803 can_add_account ? "True":"False" ));
3805 if (!can_add_account) {
3806 return NT_STATUS_ACCESS_DENIED;
3809 /********** BEGIN Admin BLOCK **********/
3812 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3816 /********** END Admin BLOCK **********/
3818 /* now check for failure */
3820 if ( !NT_STATUS_IS_OK(nt_status) )
3823 /* Get the user's SID */
3825 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3827 map_max_allowed_access(p->session_info->security_token,
3828 p->session_info->unix_token,
3831 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3832 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3833 se_map_generic(&des_access, &usr_generic_mapping);
3836 * JRA - TESTME. We just created this user so we
3837 * had rights to create them. Do we need to check
3838 * any further access on this object ? Can't we
3839 * just assume we have all the rights we need ?
3842 nt_status = access_check_object(psd, p->session_info->security_token,
3843 needed_priv, SEC_PRIV_INVALID,
3844 GENERIC_RIGHTS_USER_WRITE, des_access,
3845 &acc_granted, "_samr_CreateUser2");
3847 if ( !NT_STATUS_IS_OK(nt_status) ) {
3851 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
3852 struct samr_user_info, &nt_status);
3853 if (!NT_STATUS_IS_OK(nt_status)) {
3858 /* After a "set" ensure we have no cached display info. */
3859 force_flush_samr_cache(&sid);
3861 *r->out.access_granted = acc_granted;
3863 return NT_STATUS_OK;
3866 /****************************************************************
3867 ****************************************************************/
3869 NTSTATUS _samr_CreateUser(struct pipes_struct *p,
3870 struct samr_CreateUser *r)
3872 struct samr_CreateUser2 c;
3873 uint32_t access_granted;
3875 c.in.domain_handle = r->in.domain_handle;
3876 c.in.account_name = r->in.account_name;
3877 c.in.acct_flags = ACB_NORMAL;
3878 c.in.access_mask = r->in.access_mask;
3879 c.out.user_handle = r->out.user_handle;
3880 c.out.access_granted = &access_granted;
3881 c.out.rid = r->out.rid;
3883 return _samr_CreateUser2(p, &c);
3886 /*******************************************************************
3888 ********************************************************************/
3890 NTSTATUS _samr_Connect(struct pipes_struct *p,
3891 struct samr_Connect *r)
3893 struct samr_connect_info *info;
3894 uint32_t acc_granted;
3895 struct policy_handle hnd;
3896 uint32 des_access = r->in.access_mask;
3901 if (!pipe_access_check(p)) {
3902 DEBUG(3, ("access denied to _samr_Connect\n"));
3903 return NT_STATUS_ACCESS_DENIED;
3906 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3907 was observed from a win98 client trying to enumerate users (when configured
3908 user level access control on shares) --jerry */
3910 map_max_allowed_access(p->session_info->security_token,
3911 p->session_info->unix_token,
3914 se_map_generic( &des_access, &sam_generic_mapping );
3916 acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS
3917 |SAMR_ACCESS_LOOKUP_DOMAIN);
3919 /* set up the SAMR connect_anon response */
3921 info = policy_handle_create(p, &hnd, acc_granted,
3922 struct samr_connect_info,
3924 if (!NT_STATUS_IS_OK(status)) {
3928 *r->out.connect_handle = hnd;
3929 return NT_STATUS_OK;
3932 /*******************************************************************
3934 ********************************************************************/
3936 NTSTATUS _samr_Connect2(struct pipes_struct *p,
3937 struct samr_Connect2 *r)
3939 struct samr_connect_info *info = NULL;
3940 struct policy_handle hnd;
3941 struct security_descriptor *psd = NULL;
3943 uint32 des_access = r->in.access_mask;
3946 const char *fn = "_samr_Connect2";
3949 case NDR_SAMR_CONNECT2:
3950 fn = "_samr_Connect2";
3952 case NDR_SAMR_CONNECT3:
3953 fn = "_samr_Connect3";
3955 case NDR_SAMR_CONNECT4:
3956 fn = "_samr_Connect4";
3958 case NDR_SAMR_CONNECT5:
3959 fn = "_samr_Connect5";
3963 DEBUG(5,("%s: %d\n", fn, __LINE__));
3967 if (!pipe_access_check(p)) {
3968 DEBUG(3, ("access denied to %s\n", fn));
3969 return NT_STATUS_ACCESS_DENIED;
3972 map_max_allowed_access(p->session_info->security_token,
3973 p->session_info->unix_token,
3976 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3977 se_map_generic(&des_access, &sam_generic_mapping);
3979 nt_status = access_check_object(psd, p->session_info->security_token,
3980 SEC_PRIV_INVALID, SEC_PRIV_INVALID,
3981 0, des_access, &acc_granted, fn);
3983 if ( !NT_STATUS_IS_OK(nt_status) )
3986 info = policy_handle_create(p, &hnd, acc_granted,
3987 struct samr_connect_info, &nt_status);
3988 if (!NT_STATUS_IS_OK(nt_status)) {
3992 DEBUG(5,("%s: %d\n", fn, __LINE__));
3994 *r->out.connect_handle = hnd;
3995 return NT_STATUS_OK;
3998 /****************************************************************
4000 ****************************************************************/
4002 NTSTATUS _samr_Connect3(struct pipes_struct *p,
4003 struct samr_Connect3 *r)
4005 struct samr_Connect2 c;
4007 c.in.system_name = r->in.system_name;
4008 c.in.access_mask = r->in.access_mask;
4009 c.out.connect_handle = r->out.connect_handle;
4011 return _samr_Connect2(p, &c);
4014 /*******************************************************************
4016 ********************************************************************/
4018 NTSTATUS _samr_Connect4(struct pipes_struct *p,
4019 struct samr_Connect4 *r)
4021 struct samr_Connect2 c;
4023 c.in.system_name = r->in.system_name;
4024 c.in.access_mask = r->in.access_mask;
4025 c.out.connect_handle = r->out.connect_handle;
4027 return _samr_Connect2(p, &c);
4030 /*******************************************************************
4032 ********************************************************************/
4034 NTSTATUS _samr_Connect5(struct pipes_struct *p,
4035 struct samr_Connect5 *r)
4038 struct samr_Connect2 c;
4039 struct samr_ConnectInfo1 info1;
4041 info1.client_version = SAMR_CONNECT_AFTER_W2K;
4044 c.in.system_name = r->in.system_name;
4045 c.in.access_mask = r->in.access_mask;
4046 c.out.connect_handle = r->out.connect_handle;
4048 *r->out.level_out = 1;
4050 status = _samr_Connect2(p, &c);
4051 if (!NT_STATUS_IS_OK(status)) {
4055 r->out.info_out->info1 = info1;
4057 return NT_STATUS_OK;
4060 /**********************************************************************
4062 **********************************************************************/
4064 NTSTATUS _samr_LookupDomain(struct pipes_struct *p,
4065 struct samr_LookupDomain *r)
4068 struct samr_connect_info *info;
4069 const char *domain_name;
4070 struct dom_sid *sid = NULL;
4072 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
4073 Reverted that change so we will work with RAS servers again */
4075 info = policy_handle_find(p, r->in.connect_handle,
4076 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
4077 struct samr_connect_info,
4079 if (!NT_STATUS_IS_OK(status)) {
4083 domain_name = r->in.domain_name->string;
4085 return NT_STATUS_INVALID_PARAMETER;
4088 sid = talloc_zero(p->mem_ctx, struct dom_sid2);
4090 return NT_STATUS_NO_MEMORY;
4093 if (strequal(domain_name, builtin_domain_name())) {
4094 sid_copy(sid, &global_sid_Builtin);
4096 if (!secrets_fetch_domain_sid(domain_name, sid)) {
4097 status = NT_STATUS_NO_SUCH_DOMAIN;
4101 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
4102 sid_string_dbg(sid)));
4109 /**********************************************************************
4111 **********************************************************************/
4113 NTSTATUS _samr_EnumDomains(struct pipes_struct *p,
4114 struct samr_EnumDomains *r)
4117 struct samr_connect_info *info;
4118 uint32_t num_entries = 2;
4119 struct samr_SamEntry *entry_array = NULL;
4120 struct samr_SamArray *sam;
4122 info = policy_handle_find(p, r->in.connect_handle,
4123 SAMR_ACCESS_ENUM_DOMAINS, NULL,
4124 struct samr_connect_info, &status);
4125 if (!NT_STATUS_IS_OK(status)) {
4129 sam = talloc_zero(p->mem_ctx, struct samr_SamArray);
4131 return NT_STATUS_NO_MEMORY;
4134 entry_array = talloc_zero_array(p->mem_ctx,
4135 struct samr_SamEntry,
4138 return NT_STATUS_NO_MEMORY;
4141 entry_array[0].idx = 0;
4142 init_lsa_String(&entry_array[0].name, get_global_sam_name());
4144 entry_array[1].idx = 1;
4145 init_lsa_String(&entry_array[1].name, "Builtin");
4147 sam->count = num_entries;
4148 sam->entries = entry_array;
4151 *r->out.num_entries = num_entries;
4156 /*******************************************************************
4158 ********************************************************************/
4160 NTSTATUS _samr_OpenAlias(struct pipes_struct *p,
4161 struct samr_OpenAlias *r)
4164 uint32 alias_rid = r->in.rid;
4165 struct samr_alias_info *ainfo;
4166 struct samr_domain_info *dinfo;
4167 struct security_descriptor *psd = NULL;
4169 uint32 des_access = r->in.access_mask;
4173 dinfo = policy_handle_find(p, r->in.domain_handle,
4174 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
4175 struct samr_domain_info, &status);
4176 if (!NT_STATUS_IS_OK(status)) {
4180 /* append the alias' RID to it */
4182 if (!sid_compose(&sid, &dinfo->sid, alias_rid))
4183 return NT_STATUS_NO_SUCH_ALIAS;
4185 /*check if access can be granted as requested by client. */
4187 map_max_allowed_access(p->session_info->security_token,
4188 p->session_info->unix_token,
4191 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
4192 se_map_generic(&des_access,&ali_generic_mapping);
4194 status = access_check_object(psd, p->session_info->security_token,
4195 SEC_PRIV_ADD_USERS, SEC_PRIV_INVALID,
4196 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
4197 des_access, &acc_granted, "_samr_OpenAlias");
4199 if ( !NT_STATUS_IS_OK(status) )
4203 /* Check we actually have the requested alias */
4204 enum lsa_SidType type;
4209 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
4212 if (!result || (type != SID_NAME_ALIAS)) {
4213 return NT_STATUS_NO_SUCH_ALIAS;
4216 /* make sure there is a mapping */
4218 if ( !sid_to_gid( &sid, &gid ) ) {
4219 return NT_STATUS_NO_SUCH_ALIAS;
4224 ainfo = policy_handle_create(p, r->out.alias_handle, acc_granted,
4225 struct samr_alias_info, &status);
4226 if (!NT_STATUS_IS_OK(status)) {
4231 return NT_STATUS_OK;
4234 /*******************************************************************
4236 ********************************************************************/
4238 static NTSTATUS set_user_info_2(TALLOC_CTX *mem_ctx,
4239 struct samr_UserInfo2 *id2,
4243 DEBUG(5,("set_user_info_2: NULL id2\n"));
4244 return NT_STATUS_ACCESS_DENIED;
4247 copy_id2_to_sam_passwd(pwd, id2);
4249 return pdb_update_sam_account(pwd);
4252 /*******************************************************************
4254 ********************************************************************/
4256 static NTSTATUS set_user_info_4(TALLOC_CTX *mem_ctx,
4257 struct samr_UserInfo4 *id4,
4261 DEBUG(5,("set_user_info_2: NULL id4\n"));
4262 return NT_STATUS_ACCESS_DENIED;
4265 copy_id4_to_sam_passwd(pwd, id4);
4267 return pdb_update_sam_account(pwd);
4270 /*******************************************************************
4272 ********************************************************************/
4274 static NTSTATUS set_user_info_6(TALLOC_CTX *mem_ctx,
4275 struct samr_UserInfo6 *id6,
4279 DEBUG(5,("set_user_info_6: NULL id6\n"));
4280 return NT_STATUS_ACCESS_DENIED;
4283 copy_id6_to_sam_passwd(pwd, id6);
4285 return pdb_update_sam_account(pwd);
4288 /*******************************************************************
4290 ********************************************************************/
4292 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
4293 struct samr_UserInfo7 *id7,
4299 DEBUG(5, ("set_user_info_7: NULL id7\n"));
4300 return NT_STATUS_ACCESS_DENIED;
4303 if (!id7->account_name.string) {
4304 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
4305 return NT_STATUS_ACCESS_DENIED;
4308 /* check to see if the new username already exists. Note: we can't
4309 reliably lock all backends, so there is potentially the
4310 possibility that a user can be created in between this check and
4311 the rename. The rename should fail, but may not get the
4312 exact same failure status code. I think this is small enough
4313 of a window for this type of operation and the results are
4314 simply that the rename fails with a slightly different status
4315 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4317 rc = can_create(mem_ctx, id7->account_name.string);
4319 /* when there is nothing to change, we're done here */
4320 if (NT_STATUS_EQUAL(rc, NT_STATUS_USER_EXISTS) &&
4321 strequal(id7->account_name.string, pdb_get_username(pwd))) {
4322 return NT_STATUS_OK;
4324 if (!NT_STATUS_IS_OK(rc)) {
4328 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
4333 /*******************************************************************
4335 ********************************************************************/
4337 static NTSTATUS set_user_info_8(TALLOC_CTX *mem_ctx,
4338 struct samr_UserInfo8 *id8,
4342 DEBUG(5,("set_user_info_8: NULL id8\n"));
4343 return NT_STATUS_ACCESS_DENIED;
4346 copy_id8_to_sam_passwd(pwd, id8);
4348 return pdb_update_sam_account(pwd);
4351 /*******************************************************************
4353 ********************************************************************/
4355 static NTSTATUS set_user_info_10(TALLOC_CTX *mem_ctx,
4356 struct samr_UserInfo10 *id10,
4360 DEBUG(5,("set_user_info_8: NULL id10\n"));
4361 return NT_STATUS_ACCESS_DENIED;
4364 copy_id10_to_sam_passwd(pwd, id10);
4366 return pdb_update_sam_account(pwd);
4369 /*******************************************************************
4371 ********************************************************************/
4373 static NTSTATUS set_user_info_11(TALLOC_CTX *mem_ctx,
4374 struct samr_UserInfo11 *id11,
4378 DEBUG(5,("set_user_info_11: NULL id11\n"));
4379 return NT_STATUS_ACCESS_DENIED;
4382 copy_id11_to_sam_passwd(pwd, id11);
4384 return pdb_update_sam_account(pwd);
4387 /*******************************************************************
4389 ********************************************************************/
4391 static NTSTATUS set_user_info_12(TALLOC_CTX *mem_ctx,
4392 struct samr_UserInfo12 *id12,
4396 DEBUG(5,("set_user_info_12: NULL id12\n"));
4397 return NT_STATUS_ACCESS_DENIED;
4400 copy_id12_to_sam_passwd(pwd, id12);
4402 return pdb_update_sam_account(pwd);
4405 /*******************************************************************
4407 ********************************************************************/
4409 static NTSTATUS set_user_info_13(TALLOC_CTX *mem_ctx,
4410 struct samr_UserInfo13 *id13,
4414 DEBUG(5,("set_user_info_13: NULL id13\n"));
4415 return NT_STATUS_ACCESS_DENIED;
4418 copy_id13_to_sam_passwd(pwd, id13);
4420 return pdb_update_sam_account(pwd);
4423 /*******************************************************************
4425 ********************************************************************/
4427 static NTSTATUS set_user_info_14(TALLOC_CTX *mem_ctx,
4428 struct samr_UserInfo14 *id14,
4432 DEBUG(5,("set_user_info_14: NULL id14\n"));
4433 return NT_STATUS_ACCESS_DENIED;
4436 copy_id14_to_sam_passwd(pwd, id14);
4438 return pdb_update_sam_account(pwd);
4441 /*******************************************************************
4443 ********************************************************************/
4445 static NTSTATUS set_user_info_16(TALLOC_CTX *mem_ctx,
4446 struct samr_UserInfo16 *id16,
4450 DEBUG(5,("set_user_info_16: NULL id16\n"));
4451 return NT_STATUS_ACCESS_DENIED;
4454 copy_id16_to_sam_passwd(pwd, id16);
4456 return pdb_update_sam_account(pwd);
4459 /*******************************************************************
4461 ********************************************************************/
4463 static NTSTATUS set_user_info_17(TALLOC_CTX *mem_ctx,
4464 struct samr_UserInfo17 *id17,
4468 DEBUG(5,("set_user_info_17: NULL id17\n"));
4469 return NT_STATUS_ACCESS_DENIED;
4472 copy_id17_to_sam_passwd(pwd, id17);
4474 return pdb_update_sam_account(pwd);
4477 /*******************************************************************
4479 ********************************************************************/
4481 static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
4482 TALLOC_CTX *mem_ctx,
4483 DATA_BLOB *session_key,
4487 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
4488 return NT_STATUS_INVALID_PARAMETER;
4491 if (id18->nt_pwd_active || id18->lm_pwd_active) {
4492 if (!session_key->length) {
4493 return NT_STATUS_NO_USER_SESSION_KEY;
4497 if (id18->nt_pwd_active) {
4501 in = data_blob_const(id18->nt_pwd.hash, 16);
4502 out = data_blob_talloc_zero(mem_ctx, 16);
4504 sess_crypt_blob(&out, &in, session_key, false);
4506 if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
4507 return NT_STATUS_ACCESS_DENIED;
4510 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4513 if (id18->lm_pwd_active) {
4517 in = data_blob_const(id18->lm_pwd.hash, 16);
4518 out = data_blob_talloc_zero(mem_ctx, 16);
4520 sess_crypt_blob(&out, &in, session_key, false);
4522 if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
4523 return NT_STATUS_ACCESS_DENIED;
4526 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4529 copy_id18_to_sam_passwd(pwd, id18);
4531 return pdb_update_sam_account(pwd);
4534 /*******************************************************************
4536 ********************************************************************/
4538 static NTSTATUS set_user_info_20(TALLOC_CTX *mem_ctx,
4539 struct samr_UserInfo20 *id20,
4543 DEBUG(5,("set_user_info_20: NULL id20\n"));
4544 return NT_STATUS_ACCESS_DENIED;
4547 copy_id20_to_sam_passwd(pwd, id20);
4549 return pdb_update_sam_account(pwd);
4552 /*******************************************************************
4554 ********************************************************************/
4556 static NTSTATUS set_user_info_21(struct samr_UserInfo21 *id21,
4557 TALLOC_CTX *mem_ctx,
4558 DATA_BLOB *session_key,
4564 DEBUG(5, ("set_user_info_21: NULL id21\n"));
4565 return NT_STATUS_INVALID_PARAMETER;
4568 if (id21->fields_present == 0) {
4569 return NT_STATUS_INVALID_PARAMETER;
4572 if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4573 return NT_STATUS_ACCESS_DENIED;
4576 if (id21->fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) {
4577 if (id21->nt_password_set) {
4580 if ((id21->nt_owf_password.length != 16) ||
4581 (id21->nt_owf_password.size != 16)) {
4582 return NT_STATUS_INVALID_PARAMETER;
4585 if (!session_key->length) {
4586 return NT_STATUS_NO_USER_SESSION_KEY;
4589 in = data_blob_const(id21->nt_owf_password.array, 16);
4590 out = data_blob_talloc_zero(mem_ctx, 16);
4592 sess_crypt_blob(&out, &in, session_key, false);
4594 pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED);
4595 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4599 if (id21->fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT) {
4600 if (id21->lm_password_set) {
4603 if ((id21->lm_owf_password.length != 16) ||
4604 (id21->lm_owf_password.size != 16)) {
4605 return NT_STATUS_INVALID_PARAMETER;
4608 if (!session_key->length) {
4609 return NT_STATUS_NO_USER_SESSION_KEY;
4612 in = data_blob_const(id21->lm_owf_password.array, 16);
4613 out = data_blob_talloc_zero(mem_ctx, 16);
4615 sess_crypt_blob(&out, &in, session_key, false);
4617 pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED);
4618 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4622 /* we need to separately check for an account rename first */
4624 if (id21->account_name.string &&
4625 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
4628 /* check to see if the new username already exists. Note: we can't
4629 reliably lock all backends, so there is potentially the
4630 possibility that a user can be created in between this check and
4631 the rename. The rename should fail, but may not get the
4632 exact same failure status code. I think this is small enough
4633 of a window for this type of operation and the results are
4634 simply that the rename fails with a slightly different status
4635 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4637 status = can_create(mem_ctx, id21->account_name.string);
4638 if (!NT_STATUS_IS_OK(status)) {
4642 status = pdb_rename_sam_account(pwd, id21->account_name.string);
4644 if (!NT_STATUS_IS_OK(status)) {
4645 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
4646 nt_errstr(status)));
4650 /* set the new username so that later
4651 functions can work on the new account */
4652 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
4655 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
4658 * The funny part about the previous two calls is
4659 * that pwd still has the password hashes from the
4660 * passdb entry. These have not been updated from
4661 * id21. I don't know if they need to be set. --jerry
4664 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4665 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4666 if ( !NT_STATUS_IS_OK(status) ) {
4671 /* Don't worry about writing out the user account since the
4672 primary group SID is generated solely from the user's Unix
4675 /* write the change out */
4676 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4680 return NT_STATUS_OK;
4683 /*******************************************************************
4685 ********************************************************************/
4687 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
4688 struct samr_UserInfo23 *id23,
4692 char *plaintext_buf = NULL;
4698 DEBUG(5, ("set_user_info_23: NULL id23\n"));
4699 return NT_STATUS_INVALID_PARAMETER;
4702 if (id23->info.fields_present == 0) {
4703 return NT_STATUS_INVALID_PARAMETER;
4706 if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4707 return NT_STATUS_ACCESS_DENIED;
4710 if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4711 (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4713 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
4714 pdb_get_username(pwd)));
4716 if (!decode_pw_buffer(mem_ctx,
4717 id23->password.data,
4721 return NT_STATUS_WRONG_PASSWORD;
4724 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4725 return NT_STATUS_ACCESS_DENIED;
4729 copy_id23_to_sam_passwd(pwd, id23);
4731 acct_ctrl = pdb_get_acct_ctrl(pwd);
4733 /* if it's a trust account, don't update /etc/passwd */
4734 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4735 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4736 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4737 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
4738 } else if (plaintext_buf) {
4739 /* update the UNIX password */
4740 if (lp_unix_password_sync() ) {
4741 struct passwd *passwd;
4742 if (pdb_get_username(pwd) == NULL) {
4743 DEBUG(1, ("chgpasswd: User without name???\n"));
4744 return NT_STATUS_ACCESS_DENIED;
4747 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4748 if (passwd == NULL) {
4749 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4752 if(!chgpasswd(pdb_get_username(pwd), rhost,
4753 passwd, "", plaintext_buf, True)) {
4754 return NT_STATUS_ACCESS_DENIED;
4756 TALLOC_FREE(passwd);
4760 if (plaintext_buf) {
4761 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4764 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
4765 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
4770 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4774 return NT_STATUS_OK;
4777 /*******************************************************************
4779 ********************************************************************/
4781 static bool set_user_info_pw(uint8 *pass, const char *rhost, struct samu *pwd)
4784 char *plaintext_buf = NULL;
4787 DEBUG(5, ("Attempting administrator password change for user %s\n",
4788 pdb_get_username(pwd)));
4790 acct_ctrl = pdb_get_acct_ctrl(pwd);
4792 if (!decode_pw_buffer(talloc_tos(),
4800 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4804 /* if it's a trust account, don't update /etc/passwd */
4805 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4806 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4807 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4808 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4810 /* update the UNIX password */
4811 if (lp_unix_password_sync()) {
4812 struct passwd *passwd;
4814 if (pdb_get_username(pwd) == NULL) {
4815 DEBUG(1, ("chgpasswd: User without name???\n"));
4819 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4820 if (passwd == NULL) {
4821 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4824 if(!chgpasswd(pdb_get_username(pwd), rhost, passwd,
4825 "", plaintext_buf, True)) {
4828 TALLOC_FREE(passwd);
4832 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4834 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4839 /*******************************************************************
4841 ********************************************************************/
4843 static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
4845 struct samr_UserInfo24 *id24,
4851 DEBUG(5, ("set_user_info_24: NULL id24\n"));
4852 return NT_STATUS_INVALID_PARAMETER;
4855 if (!set_user_info_pw(id24->password.data, rhost, pwd)) {
4856 return NT_STATUS_WRONG_PASSWORD;
4859 copy_id24_to_sam_passwd(pwd, id24);
4861 status = pdb_update_sam_account(pwd);
4862 if (!NT_STATUS_IS_OK(status)) {
4866 return NT_STATUS_OK;
4869 /*******************************************************************
4871 ********************************************************************/
4873 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4875 struct samr_UserInfo25 *id25,
4881 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4882 return NT_STATUS_INVALID_PARAMETER;
4885 if (id25->info.fields_present == 0) {
4886 return NT_STATUS_INVALID_PARAMETER;
4889 if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4890 return NT_STATUS_ACCESS_DENIED;
4893 if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4894 (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4896 if (!set_user_info_pw(id25->password.data, rhost, pwd)) {
4897 return NT_STATUS_WRONG_PASSWORD;
4901 copy_id25_to_sam_passwd(pwd, id25);
4903 /* write the change out */
4904 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4909 * We need to "pdb_update_sam_account" before the unix primary group
4910 * is set, because the idealx scripts would also change the
4911 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4912 * the delete explicit / add explicit, which would then fail to find
4913 * the previous primaryGroupSid value.
4916 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4917 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4918 if ( !NT_STATUS_IS_OK(status) ) {
4923 return NT_STATUS_OK;
4926 /*******************************************************************
4928 ********************************************************************/
4930 static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
4932 struct samr_UserInfo26 *id26,
4938 DEBUG(5, ("set_user_info_26: NULL id26\n"));
4939 return NT_STATUS_INVALID_PARAMETER;
4942 if (!set_user_info_pw(id26->password.data, rhost, pwd)) {
4943 return NT_STATUS_WRONG_PASSWORD;
4946 copy_id26_to_sam_passwd(pwd, id26);
4948 status = pdb_update_sam_account(pwd);
4949 if (!NT_STATUS_IS_OK(status)) {
4953 return NT_STATUS_OK;
4956 /*************************************************************
4957 **************************************************************/
4959 static uint32_t samr_set_user_info_map_fields_to_access_mask(uint32_t fields)
4961 uint32_t acc_required = 0;
4963 /* USER_ALL_USERNAME */
4964 if (fields & SAMR_FIELD_ACCOUNT_NAME)
4965 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4966 /* USER_ALL_FULLNAME */
4967 if (fields & SAMR_FIELD_FULL_NAME)
4968 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4969 /* USER_ALL_PRIMARYGROUPID */
4970 if (fields & SAMR_FIELD_PRIMARY_GID)
4971 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4972 /* USER_ALL_HOMEDIRECTORY */
4973 if (fields & SAMR_FIELD_HOME_DIRECTORY)
4974 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4975 /* USER_ALL_HOMEDIRECTORYDRIVE */
4976 if (fields & SAMR_FIELD_HOME_DRIVE)
4977 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4978 /* USER_ALL_SCRIPTPATH */
4979 if (fields & SAMR_FIELD_LOGON_SCRIPT)
4980 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4981 /* USER_ALL_PROFILEPATH */
4982 if (fields & SAMR_FIELD_PROFILE_PATH)
4983 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4984 /* USER_ALL_ADMINCOMMENT */
4985 if (fields & SAMR_FIELD_COMMENT)
4986 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4987 /* USER_ALL_WORKSTATIONS */
4988 if (fields & SAMR_FIELD_WORKSTATIONS)
4989 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4990 /* USER_ALL_LOGONHOURS */
4991 if (fields & SAMR_FIELD_LOGON_HOURS)
4992 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4993 /* USER_ALL_ACCOUNTEXPIRES */
4994 if (fields & SAMR_FIELD_ACCT_EXPIRY)
4995 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4996 /* USER_ALL_USERACCOUNTCONTROL */
4997 if (fields & SAMR_FIELD_ACCT_FLAGS)
4998 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
4999 /* USER_ALL_PARAMETERS */
5000 if (fields & SAMR_FIELD_PARAMETERS)
5001 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5002 /* USER_ALL_USERCOMMENT */
5003 if (fields & SAMR_FIELD_COMMENT)
5004 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5005 /* USER_ALL_COUNTRYCODE */
5006 if (fields & SAMR_FIELD_COUNTRY_CODE)
5007 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5008 /* USER_ALL_CODEPAGE */
5009 if (fields & SAMR_FIELD_CODE_PAGE)
5010 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5011 /* USER_ALL_NTPASSWORDPRESENT */
5012 if (fields & SAMR_FIELD_NT_PASSWORD_PRESENT)
5013 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5014 /* USER_ALL_LMPASSWORDPRESENT */
5015 if (fields & SAMR_FIELD_LM_PASSWORD_PRESENT)
5016 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5017 /* USER_ALL_PASSWORDEXPIRED */
5018 if (fields & SAMR_FIELD_EXPIRED_FLAG)
5019 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5021 return acc_required;
5024 /*******************************************************************
5026 ********************************************************************/
5028 NTSTATUS _samr_SetUserInfo(struct pipes_struct *p,
5029 struct samr_SetUserInfo *r)
5031 struct samr_user_info *uinfo;
5033 struct samu *pwd = NULL;
5034 union samr_UserInfo *info = r->in.info;
5035 uint32_t acc_required = 0;
5036 uint32_t fields = 0;
5039 DATA_BLOB session_key;
5041 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
5043 /* This is tricky. A WinXP domain join sets
5044 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
5045 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
5046 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
5047 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
5048 we'll use the set from the WinXP join as the basis. */
5050 switch (r->in.level) {
5051 case 2: /* UserPreferencesInformation */
5052 /* USER_WRITE_ACCOUNT | USER_WRITE_PREFERENCES */
5053 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES | SAMR_USER_ACCESS_SET_LOC_COM;
5055 case 4: /* UserLogonHoursInformation */
5056 case 6: /* UserNameInformation */
5057 case 7: /* UserAccountNameInformation */
5058 case 8: /* UserFullNameInformation */
5059 case 9: /* UserPrimaryGroupInformation */
5060 case 10: /* UserHomeInformation */
5061 case 11: /* UserScriptInformation */
5062 case 12: /* UserProfileInformation */
5063 case 13: /* UserAdminCommentInformation */
5064 case 14: /* UserWorkStationsInformation */
5065 case 16: /* UserControlInformation */
5066 case 17: /* UserExpiresInformation */
5067 case 20: /* UserParametersInformation */
5068 /* USER_WRITE_ACCOUNT */
5069 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES;
5071 case 18: /* UserInternal1Information */
5072 /* FIXME: gd, this is a guess */
5073 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5075 case 21: /* UserAllInformation */
5076 fields = info->info21.fields_present;
5077 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5079 case 23: /* UserInternal4Information */
5080 fields = info->info23.info.fields_present;
5081 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5083 case 25: /* UserInternal4InformationNew */
5084 fields = info->info25.info.fields_present;
5085 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5087 case 24: /* UserInternal5Information */
5088 case 26: /* UserInternal5InformationNew */
5089 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5092 return NT_STATUS_INVALID_INFO_CLASS;
5095 uinfo = policy_handle_find(p, r->in.user_handle, acc_required, NULL,
5096 struct samr_user_info, &status);
5097 if (!NT_STATUS_IS_OK(status)) {
5101 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
5102 sid_string_dbg(&uinfo->sid), r->in.level));
5105 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
5106 return NT_STATUS_INVALID_INFO_CLASS;
5109 if (!(pwd = samu_new(NULL))) {
5110 return NT_STATUS_NO_MEMORY;
5114 ret = pdb_getsampwsid(pwd, &uinfo->sid);
5119 return NT_STATUS_NO_SUCH_USER;
5122 rhost = tsocket_address_inet_addr_string(p->remote_address,
5124 if (rhost == NULL) {
5125 return NT_STATUS_NO_MEMORY;
5128 /* ================ BEGIN Privilege BLOCK ================ */
5132 /* ok! user info levels (lots: see MSDEV help), off we go... */
5134 switch (r->in.level) {
5137 status = set_user_info_2(p->mem_ctx,
5142 status = set_user_info_4(p->mem_ctx,
5147 status = set_user_info_6(p->mem_ctx,
5152 status = set_user_info_7(p->mem_ctx,
5157 status = set_user_info_8(p->mem_ctx,
5162 status = set_user_info_10(p->mem_ctx,
5163 &info->info10, pwd);
5167 status = set_user_info_11(p->mem_ctx,
5168 &info->info11, pwd);
5172 status = set_user_info_12(p->mem_ctx,
5173 &info->info12, pwd);
5177 status = set_user_info_13(p->mem_ctx,
5178 &info->info13, pwd);
5182 status = set_user_info_14(p->mem_ctx,
5183 &info->info14, pwd);
5187 status = set_user_info_16(p->mem_ctx,
5188 &info->info16, pwd);
5192 status = set_user_info_17(p->mem_ctx,
5193 &info->info17, pwd);
5197 status = session_extract_session_key(p->session_info, &session_key, KEY_USE_16BYTES);
5198 if(!NT_STATUS_IS_OK(status)) {
5201 /* Used by AS/U JRA. */
5202 status = set_user_info_18(&info->info18,
5209 status = set_user_info_20(p->mem_ctx,
5210 &info->info20, pwd);
5214 status = session_extract_session_key(p->session_info, &session_key, KEY_USE_16BYTES);
5215 if(!NT_STATUS_IS_OK(status)) {
5218 status = set_user_info_21(&info->info21,
5225 status = session_extract_session_key(p->session_info, &session_key, KEY_USE_16BYTES);
5226 arcfour_crypt_blob(info->info23.password.data, 516,
5229 dump_data(100, info->info23.password.data, 516);
5231 status = set_user_info_23(p->mem_ctx,
5238 status = session_extract_session_key(p->session_info, &session_key, KEY_USE_16BYTES);
5239 arcfour_crypt_blob(info->info24.password.data,
5243 dump_data(100, info->info24.password.data, 516);
5245 status = set_user_info_24(p->mem_ctx,
5247 &info->info24, pwd);
5251 status = session_extract_session_key(p->session_info, &session_key, KEY_USE_16BYTES);
5252 encode_or_decode_arc4_passwd_buffer(
5253 info->info25.password.data,
5256 dump_data(100, info->info25.password.data, 532);
5258 status = set_user_info_25(p->mem_ctx,
5260 &info->info25, pwd);
5264 status = session_extract_session_key(p->session_info, &session_key, KEY_USE_16BYTES);
5265 encode_or_decode_arc4_passwd_buffer(
5266 info->info26.password.data,
5269 dump_data(100, info->info26.password.data, 516);
5271 status = set_user_info_26(p->mem_ctx,
5273 &info->info26, pwd);
5277 status = NT_STATUS_INVALID_INFO_CLASS;
5284 /* ================ END Privilege BLOCK ================ */
5286 if (NT_STATUS_IS_OK(status)) {
5287 force_flush_samr_cache(&uinfo->sid);
5293 /*******************************************************************
5295 ********************************************************************/
5297 NTSTATUS _samr_SetUserInfo2(struct pipes_struct *p,
5298 struct samr_SetUserInfo2 *r)
5300 struct samr_SetUserInfo q;
5302 q.in.user_handle = r->in.user_handle;
5303 q.in.level = r->in.level;
5304 q.in.info = r->in.info;
5306 return _samr_SetUserInfo(p, &q);
5309 /*********************************************************************
5310 _samr_GetAliasMembership
5311 *********************************************************************/
5313 NTSTATUS _samr_GetAliasMembership(struct pipes_struct *p,
5314 struct samr_GetAliasMembership *r)
5316 size_t num_alias_rids;
5318 struct samr_domain_info *dinfo;
5323 struct dom_sid *members;
5325 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
5327 dinfo = policy_handle_find(p, r->in.domain_handle,
5328 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS
5329 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
5330 struct samr_domain_info, &status);
5331 if (!NT_STATUS_IS_OK(status)) {
5335 if (!sid_check_is_domain(&dinfo->sid) &&
5336 !sid_check_is_builtin(&dinfo->sid))
5337 return NT_STATUS_OBJECT_TYPE_MISMATCH;
5339 if (r->in.sids->num_sids) {
5340 members = talloc_array(p->mem_ctx, struct dom_sid, r->in.sids->num_sids);
5342 if (members == NULL)
5343 return NT_STATUS_NO_MEMORY;
5348 for (i=0; i<r->in.sids->num_sids; i++)
5349 sid_copy(&members[i], r->in.sids->sids[i].sid);
5355 status = pdb_enum_alias_memberships(p->mem_ctx, &dinfo->sid, members,
5356 r->in.sids->num_sids,
5357 &alias_rids, &num_alias_rids);
5360 if (!NT_STATUS_IS_OK(status)) {
5364 r->out.rids->count = num_alias_rids;
5365 r->out.rids->ids = alias_rids;
5367 if (r->out.rids->ids == NULL) {
5368 /* Windows domain clients don't accept a NULL ptr here */
5369 r->out.rids->ids = talloc_zero(p->mem_ctx, uint32_t);
5371 if (r->out.rids->ids == NULL) {
5372 return NT_STATUS_NO_MEMORY;
5375 return NT_STATUS_OK;
5378 /*********************************************************************
5379 _samr_GetMembersInAlias
5380 *********************************************************************/
5382 NTSTATUS _samr_GetMembersInAlias(struct pipes_struct *p,
5383 struct samr_GetMembersInAlias *r)
5385 struct samr_alias_info *ainfo;
5388 size_t num_sids = 0;
5389 struct lsa_SidPtr *sids = NULL;
5390 struct dom_sid *pdb_sids = NULL;
5392 ainfo = policy_handle_find(p, r->in.alias_handle,
5393 SAMR_ALIAS_ACCESS_GET_MEMBERS, NULL,
5394 struct samr_alias_info, &status);
5395 if (!NT_STATUS_IS_OK(status)) {
5399 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5402 status = pdb_enum_aliasmem(&ainfo->sid, talloc_tos(), &pdb_sids,
5406 if (!NT_STATUS_IS_OK(status)) {
5411 sids = talloc_zero_array(p->mem_ctx, struct lsa_SidPtr, num_sids);
5413 TALLOC_FREE(pdb_sids);
5414 return NT_STATUS_NO_MEMORY;
5418 for (i = 0; i < num_sids; i++) {
5419 sids[i].sid = dom_sid_dup(p->mem_ctx, &pdb_sids[i]);
5421 TALLOC_FREE(pdb_sids);
5422 return NT_STATUS_NO_MEMORY;
5426 r->out.sids->num_sids = num_sids;
5427 r->out.sids->sids = sids;
5429 TALLOC_FREE(pdb_sids);
5431 return NT_STATUS_OK;
5434 /*********************************************************************
5435 _samr_QueryGroupMember
5436 *********************************************************************/
5438 NTSTATUS _samr_QueryGroupMember(struct pipes_struct *p,
5439 struct samr_QueryGroupMember *r)
5441 struct samr_group_info *ginfo;
5442 size_t i, num_members;
5448 struct samr_RidAttrArray *rids = NULL;
5450 ginfo = policy_handle_find(p, r->in.group_handle,
5451 SAMR_GROUP_ACCESS_GET_MEMBERS, NULL,
5452 struct samr_group_info, &status);
5453 if (!NT_STATUS_IS_OK(status)) {
5457 rids = talloc_zero(p->mem_ctx, struct samr_RidAttrArray);
5459 return NT_STATUS_NO_MEMORY;
5462 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5464 if (!sid_check_is_in_our_domain(&ginfo->sid)) {
5465 DEBUG(3, ("sid %s is not in our domain\n",
5466 sid_string_dbg(&ginfo->sid)));
5467 return NT_STATUS_NO_SUCH_GROUP;
5470 DEBUG(10, ("lookup on Domain SID\n"));
5473 status = pdb_enum_group_members(p->mem_ctx, &ginfo->sid,
5474 &rid, &num_members);
5477 if (!NT_STATUS_IS_OK(status))
5481 attr=talloc_zero_array(p->mem_ctx, uint32, num_members);
5483 return NT_STATUS_NO_MEMORY;
5489 for (i=0; i<num_members; i++) {
5490 attr[i] = SE_GROUP_MANDATORY |
5491 SE_GROUP_ENABLED_BY_DEFAULT |
5495 rids->count = num_members;
5496 rids->attributes = attr;
5499 *r->out.rids = rids;
5501 return NT_STATUS_OK;
5504 /*********************************************************************
5505 _samr_AddAliasMember
5506 *********************************************************************/
5508 NTSTATUS _samr_AddAliasMember(struct pipes_struct *p,
5509 struct samr_AddAliasMember *r)
5511 struct samr_alias_info *ainfo;
5514 ainfo = policy_handle_find(p, r->in.alias_handle,
5515 SAMR_ALIAS_ACCESS_ADD_MEMBER, NULL,
5516 struct samr_alias_info, &status);
5517 if (!NT_STATUS_IS_OK(status)) {
5521 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5523 /******** BEGIN SeAddUsers BLOCK *********/
5526 status = pdb_add_aliasmem(&ainfo->sid, r->in.sid);
5529 /******** END SeAddUsers BLOCK *********/
5531 if (NT_STATUS_IS_OK(status)) {
5532 force_flush_samr_cache(&ainfo->sid);
5538 /*********************************************************************
5539 _samr_DeleteAliasMember
5540 *********************************************************************/
5542 NTSTATUS _samr_DeleteAliasMember(struct pipes_struct *p,
5543 struct samr_DeleteAliasMember *r)
5545 struct samr_alias_info *ainfo;
5548 ainfo = policy_handle_find(p, r->in.alias_handle,
5549 SAMR_ALIAS_ACCESS_REMOVE_MEMBER, NULL,
5550 struct samr_alias_info, &status);
5551 if (!NT_STATUS_IS_OK(status)) {
5555 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
5556 sid_string_dbg(&ainfo->sid)));
5558 /******** BEGIN SeAddUsers BLOCK *********/
5561 status = pdb_del_aliasmem(&ainfo->sid, r->in.sid);
5564 /******** END SeAddUsers BLOCK *********/
5566 if (NT_STATUS_IS_OK(status)) {
5567 force_flush_samr_cache(&ainfo->sid);
5573 /*********************************************************************
5574 _samr_AddGroupMember
5575 *********************************************************************/
5577 NTSTATUS _samr_AddGroupMember(struct pipes_struct *p,
5578 struct samr_AddGroupMember *r)
5580 struct samr_group_info *ginfo;
5584 ginfo = policy_handle_find(p, r->in.group_handle,
5585 SAMR_GROUP_ACCESS_ADD_MEMBER, NULL,
5586 struct samr_group_info, &status);
5587 if (!NT_STATUS_IS_OK(status)) {
5591 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5593 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5595 return NT_STATUS_INVALID_HANDLE;
5598 /******** BEGIN SeAddUsers BLOCK *********/
5601 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
5604 /******** END SeAddUsers BLOCK *********/
5606 force_flush_samr_cache(&ginfo->sid);
5611 /*********************************************************************
5612 _samr_DeleteGroupMember
5613 *********************************************************************/
5615 NTSTATUS _samr_DeleteGroupMember(struct pipes_struct *p,
5616 struct samr_DeleteGroupMember *r)
5619 struct samr_group_info *ginfo;
5624 * delete the group member named r->in.rid
5625 * who is a member of the sid associated with the handle
5626 * the rid is a user's rid as the group is a domain group.
5629 ginfo = policy_handle_find(p, r->in.group_handle,
5630 SAMR_GROUP_ACCESS_REMOVE_MEMBER, NULL,
5631 struct samr_group_info, &status);
5632 if (!NT_STATUS_IS_OK(status)) {
5636 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5638 return NT_STATUS_INVALID_HANDLE;
5641 /******** BEGIN SeAddUsers BLOCK *********/
5644 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
5647 /******** END SeAddUsers BLOCK *********/
5649 force_flush_samr_cache(&ginfo->sid);
5654 /*********************************************************************
5656 *********************************************************************/
5658 NTSTATUS _samr_DeleteUser(struct pipes_struct *p,
5659 struct samr_DeleteUser *r)
5661 struct samr_user_info *uinfo;
5663 struct samu *sam_pass=NULL;
5666 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
5668 uinfo = policy_handle_find(p, r->in.user_handle,
5669 SEC_STD_DELETE, NULL,
5670 struct samr_user_info, &status);
5671 if (!NT_STATUS_IS_OK(status)) {
5675 if (!sid_check_is_in_our_domain(&uinfo->sid))
5676 return NT_STATUS_CANNOT_DELETE;
5678 /* check if the user exists before trying to delete */
5679 if ( !(sam_pass = samu_new( NULL )) ) {
5680 return NT_STATUS_NO_MEMORY;
5684 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
5688 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
5689 sid_string_dbg(&uinfo->sid)));
5690 TALLOC_FREE(sam_pass);
5691 return NT_STATUS_NO_SUCH_USER;
5694 /******** BEGIN SeAddUsers BLOCK *********/
5697 status = pdb_delete_user(p->mem_ctx, sam_pass);
5700 /******** END SeAddUsers BLOCK *********/
5702 if ( !NT_STATUS_IS_OK(status) ) {
5703 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
5704 "user %s: %s.\n", pdb_get_username(sam_pass),
5705 nt_errstr(status)));
5706 TALLOC_FREE(sam_pass);
5711 TALLOC_FREE(sam_pass);
5713 force_flush_samr_cache(&uinfo->sid);
5715 if (!close_policy_hnd(p, r->in.user_handle))
5716 return NT_STATUS_OBJECT_NAME_INVALID;
5718 ZERO_STRUCTP(r->out.user_handle);
5720 return NT_STATUS_OK;
5723 /*********************************************************************
5724 _samr_DeleteDomainGroup
5725 *********************************************************************/
5727 NTSTATUS _samr_DeleteDomainGroup(struct pipes_struct *p,
5728 struct samr_DeleteDomainGroup *r)
5730 struct samr_group_info *ginfo;
5734 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
5736 ginfo = policy_handle_find(p, r->in.group_handle,
5737 SEC_STD_DELETE, NULL,
5738 struct samr_group_info, &status);
5739 if (!NT_STATUS_IS_OK(status)) {
5743 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5745 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5747 return NT_STATUS_NO_SUCH_GROUP;
5750 /******** BEGIN SeAddUsers BLOCK *********/
5753 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
5756 /******** END SeAddUsers BLOCK *********/
5758 if ( !NT_STATUS_IS_OK(status) ) {
5759 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
5760 "entry for group %s: %s\n",
5761 sid_string_dbg(&ginfo->sid),
5762 nt_errstr(status)));
5766 force_flush_samr_cache(&ginfo->sid);
5768 if (!close_policy_hnd(p, r->in.group_handle))
5769 return NT_STATUS_OBJECT_NAME_INVALID;
5771 return NT_STATUS_OK;
5774 /*********************************************************************
5775 _samr_DeleteDomAlias
5776 *********************************************************************/
5778 NTSTATUS _samr_DeleteDomAlias(struct pipes_struct *p,
5779 struct samr_DeleteDomAlias *r)
5781 struct samr_alias_info *ainfo;
5784 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
5786 ainfo = policy_handle_find(p, r->in.alias_handle,
5787 SEC_STD_DELETE, NULL,
5788 struct samr_alias_info, &status);
5789 if (!NT_STATUS_IS_OK(status)) {
5793 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5795 /* Don't let Windows delete builtin groups */
5797 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
5798 return NT_STATUS_SPECIAL_ACCOUNT;
5801 if (!sid_check_is_in_our_domain(&ainfo->sid))
5802 return NT_STATUS_NO_SUCH_ALIAS;
5804 DEBUG(10, ("lookup on Local SID\n"));
5806 /******** BEGIN SeAddUsers BLOCK *********/
5809 /* Have passdb delete the alias */
5810 status = pdb_delete_alias(&ainfo->sid);
5813 /******** END SeAddUsers BLOCK *********/
5815 if ( !NT_STATUS_IS_OK(status))
5818 force_flush_samr_cache(&ainfo->sid);
5820 if (!close_policy_hnd(p, r->in.alias_handle))
5821 return NT_STATUS_OBJECT_NAME_INVALID;
5823 return NT_STATUS_OK;
5826 /*********************************************************************
5827 _samr_CreateDomainGroup
5828 *********************************************************************/
5830 NTSTATUS _samr_CreateDomainGroup(struct pipes_struct *p,
5831 struct samr_CreateDomainGroup *r)
5836 struct samr_domain_info *dinfo;
5837 struct samr_group_info *ginfo;
5839 dinfo = policy_handle_find(p, r->in.domain_handle,
5840 SAMR_DOMAIN_ACCESS_CREATE_GROUP, NULL,
5841 struct samr_domain_info, &status);
5842 if (!NT_STATUS_IS_OK(status)) {
5846 if (!sid_check_is_domain(&dinfo->sid)) {
5847 return NT_STATUS_ACCESS_DENIED;
5850 name = r->in.name->string;
5852 return NT_STATUS_NO_MEMORY;
5855 status = can_create(p->mem_ctx, name);
5856 if (!NT_STATUS_IS_OK(status)) {
5860 /******** BEGIN SeAddUsers BLOCK *********/
5863 /* check that we successfully create the UNIX group */
5864 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
5867 /******** END SeAddUsers BLOCK *********/
5869 /* check if we should bail out here */
5871 if ( !NT_STATUS_IS_OK(status) )
5874 ginfo = policy_handle_create(p, r->out.group_handle,
5875 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
5876 struct samr_group_info, &status);
5877 if (!NT_STATUS_IS_OK(status)) {
5880 sid_compose(&ginfo->sid, &dinfo->sid, *r->out.rid);
5882 force_flush_samr_cache(&dinfo->sid);
5884 return NT_STATUS_OK;
5887 /*********************************************************************
5888 _samr_CreateDomAlias
5889 *********************************************************************/
5891 NTSTATUS _samr_CreateDomAlias(struct pipes_struct *p,
5892 struct samr_CreateDomAlias *r)
5894 struct dom_sid info_sid;
5895 const char *name = NULL;
5896 struct samr_domain_info *dinfo;
5897 struct samr_alias_info *ainfo;
5901 dinfo = policy_handle_find(p, r->in.domain_handle,
5902 SAMR_DOMAIN_ACCESS_CREATE_ALIAS, NULL,
5903 struct samr_domain_info, &result);
5904 if (!NT_STATUS_IS_OK(result)) {
5908 if (!sid_check_is_domain(&dinfo->sid)) {
5909 return NT_STATUS_ACCESS_DENIED;
5912 name = r->in.alias_name->string;
5914 result = can_create(p->mem_ctx, name);
5915 if (!NT_STATUS_IS_OK(result)) {
5919 /******** BEGIN SeAddUsers BLOCK *********/
5922 /* Have passdb create the alias */
5923 result = pdb_create_alias(name, r->out.rid);
5926 /******** END SeAddUsers BLOCK *********/
5928 if (!NT_STATUS_IS_OK(result)) {
5929 DEBUG(10, ("pdb_create_alias failed: %s\n",
5930 nt_errstr(result)));
5934 sid_compose(&info_sid, &dinfo->sid, *r->out.rid);
5936 if (!sid_to_gid(&info_sid, &gid)) {
5937 DEBUG(10, ("Could not find alias just created\n"));
5938 return NT_STATUS_ACCESS_DENIED;
5941 /* check if the group has been successfully created */
5942 if ( getgrgid(gid) == NULL ) {
5943 DEBUG(1, ("getgrgid(%u) of just created alias failed\n",
5944 (unsigned int)gid));
5945 return NT_STATUS_ACCESS_DENIED;
5948 ainfo = policy_handle_create(p, r->out.alias_handle,
5949 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
5950 struct samr_alias_info, &result);
5951 if (!NT_STATUS_IS_OK(result)) {
5954 ainfo->sid = info_sid;
5956 force_flush_samr_cache(&info_sid);
5958 return NT_STATUS_OK;
5961 /*********************************************************************
5962 _samr_QueryGroupInfo
5963 *********************************************************************/
5965 NTSTATUS _samr_QueryGroupInfo(struct pipes_struct *p,
5966 struct samr_QueryGroupInfo *r)
5968 struct samr_group_info *ginfo;
5971 union samr_GroupInfo *info = NULL;
5973 uint32_t attributes = SE_GROUP_MANDATORY |
5974 SE_GROUP_ENABLED_BY_DEFAULT |
5976 const char *group_name = NULL;
5977 const char *group_description = NULL;
5979 ginfo = policy_handle_find(p, r->in.group_handle,
5980 SAMR_GROUP_ACCESS_LOOKUP_INFO, NULL,
5981 struct samr_group_info, &status);
5982 if (!NT_STATUS_IS_OK(status)) {
5986 map = talloc_zero(p->mem_ctx, GROUP_MAP);
5988 return NT_STATUS_NO_MEMORY;
5992 ret = get_domain_group_from_sid(ginfo->sid, map);
5995 return NT_STATUS_INVALID_HANDLE;
5997 group_name = talloc_move(r, &map->nt_name);
5998 group_description = talloc_move(r, &map->comment);
6002 info = talloc_zero(p->mem_ctx, union samr_GroupInfo);
6004 return NT_STATUS_NO_MEMORY;
6007 switch (r->in.level) {
6013 status = pdb_enum_group_members(
6014 p->mem_ctx, &ginfo->sid, &members,
6018 if (!NT_STATUS_IS_OK(status)) {
6022 info->all.name.string = group_name;
6023 info->all.attributes = attributes;
6024 info->all.num_members = num_members;
6025 info->all.description.string = group_description;
6029 info->name.string = group_name;
6032 info->attributes.attributes = attributes;
6035 info->description.string = group_description;
6045 status = pdb_enum_group_members(
6046 p->mem_ctx, &ginfo->sid, &members,
6050 if (!NT_STATUS_IS_OK(status)) {
6054 info->all2.name.string = group_name;
6055 info->all2.attributes = attributes;
6056 info->all2.num_members = 0; /* num_members - in w2k3 this is always 0 */
6057 info->all2.description.string = group_description;
6062 return NT_STATUS_INVALID_INFO_CLASS;
6065 *r->out.info = info;
6067 return NT_STATUS_OK;
6070 /*********************************************************************
6072 *********************************************************************/
6074 NTSTATUS _samr_SetGroupInfo(struct pipes_struct *p,
6075 struct samr_SetGroupInfo *r)
6077 struct samr_group_info *ginfo;
6082 ginfo = policy_handle_find(p, r->in.group_handle,
6083 SAMR_GROUP_ACCESS_SET_INFO, NULL,
6084 struct samr_group_info, &status);
6085 if (!NT_STATUS_IS_OK(status)) {
6089 map = talloc_zero(p->mem_ctx, GROUP_MAP);
6091 return NT_STATUS_NO_MEMORY;
6095 ret = get_domain_group_from_sid(ginfo->sid, map);
6098 return NT_STATUS_NO_SUCH_GROUP;
6100 switch (r->in.level) {
6102 map->nt_name = talloc_strdup(map,
6103 r->in.info->name.string);
6104 if (!map->nt_name) {
6105 return NT_STATUS_NO_MEMORY;
6111 map->comment = talloc_strdup(map,
6112 r->in.info->description.string);
6113 if (!map->comment) {
6114 return NT_STATUS_NO_MEMORY;
6118 return NT_STATUS_INVALID_INFO_CLASS;
6121 /******** BEGIN SeAddUsers BLOCK *********/
6124 status = pdb_update_group_mapping_entry(map);
6127 /******** End SeAddUsers BLOCK *********/
6131 if (NT_STATUS_IS_OK(status)) {
6132 force_flush_samr_cache(&ginfo->sid);
6138 /*********************************************************************
6140 *********************************************************************/
6142 NTSTATUS _samr_SetAliasInfo(struct pipes_struct *p,
6143 struct samr_SetAliasInfo *r)
6145 struct samr_alias_info *ainfo;
6146 struct acct_info *info;
6149 ainfo = policy_handle_find(p, r->in.alias_handle,
6150 SAMR_ALIAS_ACCESS_SET_INFO, NULL,
6151 struct samr_alias_info, &status);
6152 if (!NT_STATUS_IS_OK(status)) {
6156 info = talloc_zero(p->mem_ctx, struct acct_info);
6158 return NT_STATUS_NO_MEMORY;
6161 /* get the current group information */
6164 status = pdb_get_aliasinfo(&ainfo->sid, info);
6167 if ( !NT_STATUS_IS_OK(status))
6170 switch (r->in.level) {
6175 /* We currently do not support renaming groups in the
6176 the BUILTIN domain. Refer to util_builtin.c to understand
6177 why. The eventually needs to be fixed to be like Windows
6178 where you can rename builtin groups, just not delete them */
6180 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
6181 return NT_STATUS_SPECIAL_ACCOUNT;
6184 /* There has to be a valid name (and it has to be different) */
6186 if ( !r->in.info->name.string )
6187 return NT_STATUS_INVALID_PARAMETER;
6189 /* If the name is the same just reply "ok". Yes this
6190 doesn't allow you to change the case of a group name. */
6192 if (strequal(r->in.info->name.string, info->acct_name)) {
6193 return NT_STATUS_OK;
6196 talloc_free(info->acct_name);
6197 info->acct_name = talloc_strdup(info, r->in.info->name.string);
6198 if (!info->acct_name) {
6199 return NT_STATUS_NO_MEMORY;
6202 /* make sure the name doesn't already exist as a user
6205 group_name = talloc_asprintf(p->mem_ctx,
6209 if (group_name == NULL) {
6210 return NT_STATUS_NO_MEMORY;
6213 status = can_create( p->mem_ctx, group_name );
6214 talloc_free(group_name);
6215 if ( !NT_STATUS_IS_OK( status ) )
6219 case ALIASINFODESCRIPTION:
6220 TALLOC_FREE(info->acct_desc);
6221 if (r->in.info->description.string) {
6222 info->acct_desc = talloc_strdup(info,
6223 r->in.info->description.string);
6225 info->acct_desc = talloc_strdup(info, "");
6227 if (!info->acct_desc) {
6228 return NT_STATUS_NO_MEMORY;
6232 return NT_STATUS_INVALID_INFO_CLASS;
6235 /******** BEGIN SeAddUsers BLOCK *********/
6238 status = pdb_set_aliasinfo(&ainfo->sid, info);
6241 /******** End SeAddUsers BLOCK *********/
6243 if (NT_STATUS_IS_OK(status))
6244 force_flush_samr_cache(&ainfo->sid);
6249 /****************************************************************
6251 ****************************************************************/
6253 NTSTATUS _samr_GetDomPwInfo(struct pipes_struct *p,
6254 struct samr_GetDomPwInfo *r)
6256 uint32_t min_password_length = 0;
6257 uint32_t password_properties = 0;
6259 /* Perform access check. Since this rpc does not require a
6260 policy handle it will not be caught by the access checks on
6261 SAMR_CONNECT or SAMR_CONNECT_ANON. */
6263 if (!pipe_access_check(p)) {
6264 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
6265 return NT_STATUS_ACCESS_DENIED;
6269 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6270 &min_password_length);
6271 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6272 &password_properties);
6275 if (lp_check_password_script() && *lp_check_password_script()) {
6276 password_properties |= DOMAIN_PASSWORD_COMPLEX;
6279 r->out.info->min_password_length = min_password_length;
6280 r->out.info->password_properties = password_properties;
6282 return NT_STATUS_OK;
6285 /*********************************************************************
6287 *********************************************************************/
6289 NTSTATUS _samr_OpenGroup(struct pipes_struct *p,
6290 struct samr_OpenGroup *r)
6293 struct dom_sid info_sid;
6295 struct samr_domain_info *dinfo;
6296 struct samr_group_info *ginfo;
6297 struct security_descriptor *psd = NULL;
6299 uint32 des_access = r->in.access_mask;
6304 dinfo = policy_handle_find(p, r->in.domain_handle,
6305 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6306 struct samr_domain_info, &status);
6307 if (!NT_STATUS_IS_OK(status)) {
6311 /*check if access can be granted as requested by client. */
6312 map_max_allowed_access(p->session_info->security_token,
6313 p->session_info->unix_token,
6316 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
6317 se_map_generic(&des_access,&grp_generic_mapping);
6319 status = access_check_object(psd, p->session_info->security_token,
6320 SEC_PRIV_ADD_USERS, SEC_PRIV_INVALID, GENERIC_RIGHTS_GROUP_ALL_ACCESS,
6321 des_access, &acc_granted, "_samr_OpenGroup");
6323 if ( !NT_STATUS_IS_OK(status) )
6326 /* this should not be hard-coded like this */
6328 if (!sid_check_is_domain(&dinfo->sid)) {
6329 return NT_STATUS_ACCESS_DENIED;
6332 sid_compose(&info_sid, &dinfo->sid, r->in.rid);
6334 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n",
6335 sid_string_dbg(&info_sid)));
6337 map = talloc_zero(p->mem_ctx, GROUP_MAP);
6339 return NT_STATUS_NO_MEMORY;
6342 /* check if that group really exists */
6344 ret = get_domain_group_from_sid(info_sid, map);
6347 return NT_STATUS_NO_SUCH_GROUP;
6351 ginfo = policy_handle_create(p, r->out.group_handle,
6353 struct samr_group_info, &status);
6354 if (!NT_STATUS_IS_OK(status)) {
6357 ginfo->sid = info_sid;
6359 return NT_STATUS_OK;
6362 /*********************************************************************
6363 _samr_RemoveMemberFromForeignDomain
6364 *********************************************************************/
6366 NTSTATUS _samr_RemoveMemberFromForeignDomain(struct pipes_struct *p,
6367 struct samr_RemoveMemberFromForeignDomain *r)
6369 struct samr_domain_info *dinfo;
6372 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
6373 sid_string_dbg(r->in.sid)));
6375 /* Find the policy handle. Open a policy on it. */
6377 dinfo = policy_handle_find(p, r->in.domain_handle,
6378 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6379 struct samr_domain_info, &result);
6380 if (!NT_STATUS_IS_OK(result)) {
6384 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
6385 sid_string_dbg(&dinfo->sid)));
6387 /* we can only delete a user from a group since we don't have
6388 nested groups anyways. So in the latter case, just say OK */
6390 /* TODO: The above comment nowadays is bogus. Since we have nested
6391 * groups now, and aliases members are never reported out of the unix
6392 * group membership, the "just say OK" makes this call a no-op. For
6393 * us. This needs fixing however. */
6395 /* I've only ever seen this in the wild when deleting a user from
6396 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
6397 * is the user about to be deleted. I very much suspect this is the
6398 * only application of this call. To verify this, let people report
6401 if (!sid_check_is_builtin(&dinfo->sid)) {
6402 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
6403 "global_sam_sid() = %s\n",
6404 sid_string_dbg(&dinfo->sid),
6405 sid_string_dbg(get_global_sam_sid())));
6406 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
6407 return NT_STATUS_OK;
6410 force_flush_samr_cache(&dinfo->sid);
6412 result = NT_STATUS_OK;
6417 /*******************************************************************
6418 _samr_QueryDomainInfo2
6419 ********************************************************************/
6421 NTSTATUS _samr_QueryDomainInfo2(struct pipes_struct *p,
6422 struct samr_QueryDomainInfo2 *r)
6424 struct samr_QueryDomainInfo q;
6426 q.in.domain_handle = r->in.domain_handle;
6427 q.in.level = r->in.level;
6429 q.out.info = r->out.info;
6431 return _samr_QueryDomainInfo(p, &q);
6434 /*******************************************************************
6435 ********************************************************************/
6437 static NTSTATUS set_dom_info_1(TALLOC_CTX *mem_ctx,
6438 struct samr_DomInfo1 *r)
6440 time_t u_expire, u_min_age;
6442 u_expire = nt_time_to_unix_abs((NTTIME *)&r->max_password_age);
6443 u_min_age = nt_time_to_unix_abs((NTTIME *)&r->min_password_age);
6445 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6446 (uint32_t)r->min_password_length);
6447 pdb_set_account_policy(PDB_POLICY_PASSWORD_HISTORY,
6448 (uint32_t)r->password_history_length);
6449 pdb_set_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6450 (uint32_t)r->password_properties);
6451 pdb_set_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, (int)u_expire);
6452 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, (int)u_min_age);
6454 return NT_STATUS_OK;
6457 /*******************************************************************
6458 ********************************************************************/
6460 static NTSTATUS set_dom_info_3(TALLOC_CTX *mem_ctx,
6461 struct samr_DomInfo3 *r)
6465 u_logout = nt_time_to_unix_abs((NTTIME *)&r->force_logoff_time);
6467 pdb_set_account_policy(PDB_POLICY_TIME_TO_LOGOUT, (int)u_logout);
6469 return NT_STATUS_OK;
6472 /*******************************************************************
6473 ********************************************************************/
6475 static NTSTATUS set_dom_info_12(TALLOC_CTX *mem_ctx,
6476 struct samr_DomInfo12 *r)
6478 time_t u_lock_duration, u_reset_time;
6480 u_lock_duration = nt_time_to_unix_abs((NTTIME *)&r->lockout_duration);
6481 if (u_lock_duration != -1) {
6482 u_lock_duration /= 60;
6485 u_reset_time = nt_time_to_unix_abs((NTTIME *)&r->lockout_window)/60;
6487 pdb_set_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
6488 pdb_set_account_policy(PDB_POLICY_RESET_COUNT_TIME, (int)u_reset_time);
6489 pdb_set_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT,
6490 (uint32_t)r->lockout_threshold);
6492 return NT_STATUS_OK;
6495 /*******************************************************************
6497 ********************************************************************/
6499 NTSTATUS _samr_SetDomainInfo(struct pipes_struct *p,
6500 struct samr_SetDomainInfo *r)
6502 struct samr_domain_info *dinfo;
6504 uint32_t acc_required = 0;
6506 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6508 switch (r->in.level) {
6509 case 1: /* DomainPasswordInformation */
6510 case 12: /* DomainLockoutInformation */
6511 /* DOMAIN_WRITE_PASSWORD_PARAMETERS */
6512 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_1;
6514 case 3: /* DomainLogoffInformation */
6515 case 4: /* DomainOemInformation */
6516 /* DOMAIN_WRITE_OTHER_PARAMETERS */
6517 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_2;
6519 case 6: /* DomainReplicationInformation */
6520 case 9: /* DomainStateInformation */
6521 case 7: /* DomainServerRoleInformation */
6522 /* DOMAIN_ADMINISTER_SERVER */
6523 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_3;
6526 return NT_STATUS_INVALID_INFO_CLASS;
6529 dinfo = policy_handle_find(p, r->in.domain_handle,
6531 struct samr_domain_info, &status);
6532 if (!NT_STATUS_IS_OK(status)) {
6536 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
6538 switch (r->in.level) {
6540 status = set_dom_info_1(p->mem_ctx, &r->in.info->info1);
6543 status = set_dom_info_3(p->mem_ctx, &r->in.info->info3);
6554 status = set_dom_info_12(p->mem_ctx, &r->in.info->info12);
6557 return NT_STATUS_INVALID_INFO_CLASS;
6560 if (!NT_STATUS_IS_OK(status)) {
6564 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6566 return NT_STATUS_OK;
6569 /****************************************************************
6570 _samr_GetDisplayEnumerationIndex
6571 ****************************************************************/
6573 NTSTATUS _samr_GetDisplayEnumerationIndex(struct pipes_struct *p,
6574 struct samr_GetDisplayEnumerationIndex *r)
6576 struct samr_domain_info *dinfo;
6577 uint32_t max_entries = (uint32_t) -1;
6578 uint32_t enum_context = 0;
6580 uint32_t num_account = 0;
6581 struct samr_displayentry *entries = NULL;
6584 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
6586 dinfo = policy_handle_find(p, r->in.domain_handle,
6587 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
6588 struct samr_domain_info, &status);
6589 if (!NT_STATUS_IS_OK(status)) {
6593 if ((r->in.level < 1) || (r->in.level > 3)) {
6594 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
6595 "Unknown info level (%u)\n",
6597 return NT_STATUS_INVALID_INFO_CLASS;
6602 /* The following done as ROOT. Don't return without unbecome_root(). */
6604 switch (r->in.level) {
6606 if (dinfo->disp_info->users == NULL) {
6607 dinfo->disp_info->users = pdb_search_users(
6608 dinfo->disp_info, ACB_NORMAL);
6609 if (dinfo->disp_info->users == NULL) {
6611 return NT_STATUS_ACCESS_DENIED;
6613 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6614 "starting user enumeration at index %u\n",
6615 (unsigned int)enum_context));
6617 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6618 "using cached user enumeration at index %u\n",
6619 (unsigned int)enum_context));
6621 num_account = pdb_search_entries(dinfo->disp_info->users,
6622 enum_context, max_entries,
6626 if (dinfo->disp_info->machines == NULL) {
6627 dinfo->disp_info->machines = pdb_search_users(
6628 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
6629 if (dinfo->disp_info->machines == NULL) {
6631 return NT_STATUS_ACCESS_DENIED;
6633 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6634 "starting machine enumeration at index %u\n",
6635 (unsigned int)enum_context));
6637 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6638 "using cached machine enumeration at index %u\n",
6639 (unsigned int)enum_context));
6641 num_account = pdb_search_entries(dinfo->disp_info->machines,
6642 enum_context, max_entries,
6646 if (dinfo->disp_info->groups == NULL) {
6647 dinfo->disp_info->groups = pdb_search_groups(
6649 if (dinfo->disp_info->groups == NULL) {
6651 return NT_STATUS_ACCESS_DENIED;
6653 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6654 "starting group enumeration at index %u\n",
6655 (unsigned int)enum_context));
6657 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6658 "using cached group enumeration at index %u\n",
6659 (unsigned int)enum_context));
6661 num_account = pdb_search_entries(dinfo->disp_info->groups,
6662 enum_context, max_entries,
6667 smb_panic("info class changed");
6673 /* Ensure we cache this enumeration. */
6674 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
6676 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
6677 r->in.name->string));
6679 for (i=0; i<num_account; i++) {
6680 if (strequal(entries[i].account_name, r->in.name->string)) {
6681 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6682 "found %s at idx %d\n",
6683 r->in.name->string, i));
6685 return NT_STATUS_OK;
6689 /* assuming account_name lives at the very end */
6690 *r->out.idx = num_account;
6692 return NT_STATUS_NO_MORE_ENTRIES;
6695 /****************************************************************
6696 _samr_GetDisplayEnumerationIndex2
6697 ****************************************************************/
6699 NTSTATUS _samr_GetDisplayEnumerationIndex2(struct pipes_struct *p,
6700 struct samr_GetDisplayEnumerationIndex2 *r)
6702 struct samr_GetDisplayEnumerationIndex q;
6704 q.in.domain_handle = r->in.domain_handle;
6705 q.in.level = r->in.level;
6706 q.in.name = r->in.name;
6708 q.out.idx = r->out.idx;
6710 return _samr_GetDisplayEnumerationIndex(p, &q);
6713 /****************************************************************
6715 ****************************************************************/
6717 NTSTATUS _samr_RidToSid(struct pipes_struct *p,
6718 struct samr_RidToSid *r)
6720 struct samr_domain_info *dinfo;
6724 dinfo = policy_handle_find(p, r->in.domain_handle,
6726 struct samr_domain_info, &status);
6727 if (!NT_STATUS_IS_OK(status)) {
6731 if (!sid_compose(&sid, &dinfo->sid, r->in.rid)) {
6732 return NT_STATUS_NO_MEMORY;
6735 *r->out.sid = dom_sid_dup(p->mem_ctx, &sid);
6737 return NT_STATUS_NO_MEMORY;
6740 return NT_STATUS_OK;
6743 /****************************************************************
6744 ****************************************************************/
6746 static enum samr_ValidationStatus samr_ValidatePassword_Change(TALLOC_CTX *mem_ctx,
6747 const struct samr_PwInfo *dom_pw_info,
6748 const struct samr_ValidatePasswordReq2 *req,
6749 struct samr_ValidatePasswordRepCtr *rep)
6753 if (req->password.string == NULL) {
6754 return SAMR_VALIDATION_STATUS_SUCCESS;
6756 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6757 ZERO_STRUCT(rep->info);
6758 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6760 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6761 status = check_password_complexity(req->account.string,
6762 req->password.string,
6764 if (!NT_STATUS_IS_OK(status)) {
6765 ZERO_STRUCT(rep->info);
6766 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6770 return SAMR_VALIDATION_STATUS_SUCCESS;
6773 /****************************************************************
6774 ****************************************************************/
6776 static enum samr_ValidationStatus samr_ValidatePassword_Reset(TALLOC_CTX *mem_ctx,
6777 const struct samr_PwInfo *dom_pw_info,
6778 const struct samr_ValidatePasswordReq3 *req,
6779 struct samr_ValidatePasswordRepCtr *rep)
6783 if (req->password.string == NULL) {
6784 return SAMR_VALIDATION_STATUS_SUCCESS;
6786 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6787 ZERO_STRUCT(rep->info);
6788 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6790 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6791 status = check_password_complexity(req->account.string,
6792 req->password.string,
6794 if (!NT_STATUS_IS_OK(status)) {
6795 ZERO_STRUCT(rep->info);
6796 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6800 return SAMR_VALIDATION_STATUS_SUCCESS;
6803 /****************************************************************
6804 _samr_ValidatePassword
6805 ****************************************************************/
6807 NTSTATUS _samr_ValidatePassword(struct pipes_struct *p,
6808 struct samr_ValidatePassword *r)
6810 union samr_ValidatePasswordRep *rep;
6812 struct samr_GetDomPwInfo pw;
6813 struct samr_PwInfo dom_pw_info;
6815 if (r->in.level < 1 || r->in.level > 3) {
6816 return NT_STATUS_INVALID_INFO_CLASS;
6819 pw.in.domain_name = NULL;
6820 pw.out.info = &dom_pw_info;
6822 status = _samr_GetDomPwInfo(p, &pw);
6823 if (!NT_STATUS_IS_OK(status)) {
6827 rep = talloc_zero(p->mem_ctx, union samr_ValidatePasswordRep);
6829 return NT_STATUS_NO_MEMORY;
6832 switch (r->in.level) {
6834 status = NT_STATUS_NOT_SUPPORTED;
6837 rep->ctr2.status = samr_ValidatePassword_Change(p->mem_ctx,
6843 rep->ctr3.status = samr_ValidatePassword_Reset(p->mem_ctx,
6849 status = NT_STATUS_INVALID_INFO_CLASS;
6853 if (!NT_STATUS_IS_OK(status)) {
6860 return NT_STATUS_OK;
6863 /****************************************************************
6864 ****************************************************************/
6866 NTSTATUS _samr_Shutdown(struct pipes_struct *p,
6867 struct samr_Shutdown *r)
6869 p->rng_fault_state = true;
6870 return NT_STATUS_NOT_IMPLEMENTED;
6873 /****************************************************************
6874 ****************************************************************/
6876 NTSTATUS _samr_SetMemberAttributesOfGroup(struct pipes_struct *p,
6877 struct samr_SetMemberAttributesOfGroup *r)
6879 p->rng_fault_state = true;
6880 return NT_STATUS_NOT_IMPLEMENTED;
6883 /****************************************************************
6884 ****************************************************************/
6886 NTSTATUS _samr_TestPrivateFunctionsDomain(struct pipes_struct *p,
6887 struct samr_TestPrivateFunctionsDomain *r)
6889 return NT_STATUS_NOT_IMPLEMENTED;
6892 /****************************************************************
6893 ****************************************************************/
6895 NTSTATUS _samr_TestPrivateFunctionsUser(struct pipes_struct *p,
6896 struct samr_TestPrivateFunctionsUser *r)
6898 return NT_STATUS_NOT_IMPLEMENTED;
6901 /****************************************************************
6902 ****************************************************************/
6904 NTSTATUS _samr_AddMultipleMembersToAlias(struct pipes_struct *p,
6905 struct samr_AddMultipleMembersToAlias *r)
6907 p->rng_fault_state = true;
6908 return NT_STATUS_NOT_IMPLEMENTED;
6911 /****************************************************************
6912 ****************************************************************/
6914 NTSTATUS _samr_RemoveMultipleMembersFromAlias(struct pipes_struct *p,
6915 struct samr_RemoveMultipleMembersFromAlias *r)
6917 p->rng_fault_state = true;
6918 return NT_STATUS_NOT_IMPLEMENTED;
6921 /****************************************************************
6922 ****************************************************************/
6924 NTSTATUS _samr_SetBootKeyInformation(struct pipes_struct *p,
6925 struct samr_SetBootKeyInformation *r)
6927 p->rng_fault_state = true;
6928 return NT_STATUS_NOT_IMPLEMENTED;
6931 /****************************************************************
6932 ****************************************************************/
6934 NTSTATUS _samr_GetBootKeyInformation(struct pipes_struct *p,
6935 struct samr_GetBootKeyInformation *r)
6937 p->rng_fault_state = true;
6938 return NT_STATUS_NOT_IMPLEMENTED;
6941 /****************************************************************
6942 ****************************************************************/
6944 NTSTATUS _samr_SetDsrmPassword(struct pipes_struct *p,
6945 struct samr_SetDsrmPassword *r)
6947 p->rng_fault_state = true;
6948 return NT_STATUS_NOT_IMPLEMENTED;