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-2005,
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.
37 #define DBGC_CLASS DBGC_RPC_SRV
39 #define SAMR_USR_RIGHTS_WRITE_PW \
40 ( READ_CONTROL_ACCESS | \
41 SA_RIGHT_USER_CHANGE_PASSWORD | \
42 SA_RIGHT_USER_SET_LOC_COM )
43 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
44 ( READ_CONTROL_ACCESS | SA_RIGHT_USER_SET_LOC_COM )
46 #define DISP_INFO_CACHE_TIMEOUT 10
48 typedef struct disp_info {
49 DOM_SID sid; /* identify which domain this is. */
50 bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
51 struct pdb_search *users; /* querydispinfo 1 and 4 */
52 struct pdb_search *machines; /* querydispinfo 2 */
53 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
54 struct pdb_search *aliases; /* enumaliases */
57 struct pdb_search *enum_users; /* enumusers with a mask */
59 struct timed_event *cache_timeout_event; /* cache idle timeout
63 /* We keep a static list of these by SID as modern clients close down
64 all resources between each request in a complete enumeration. */
67 /* for use by the \PIPE\samr policy */
69 bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
70 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
76 static const struct generic_mapping sam_generic_mapping = {
77 GENERIC_RIGHTS_SAM_READ,
78 GENERIC_RIGHTS_SAM_WRITE,
79 GENERIC_RIGHTS_SAM_EXECUTE,
80 GENERIC_RIGHTS_SAM_ALL_ACCESS};
81 static const struct generic_mapping dom_generic_mapping = {
82 GENERIC_RIGHTS_DOMAIN_READ,
83 GENERIC_RIGHTS_DOMAIN_WRITE,
84 GENERIC_RIGHTS_DOMAIN_EXECUTE,
85 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
86 static const struct generic_mapping usr_generic_mapping = {
87 GENERIC_RIGHTS_USER_READ,
88 GENERIC_RIGHTS_USER_WRITE,
89 GENERIC_RIGHTS_USER_EXECUTE,
90 GENERIC_RIGHTS_USER_ALL_ACCESS};
91 static const struct generic_mapping usr_nopwchange_generic_mapping = {
92 GENERIC_RIGHTS_USER_READ,
93 GENERIC_RIGHTS_USER_WRITE,
94 GENERIC_RIGHTS_USER_EXECUTE & ~SA_RIGHT_USER_CHANGE_PASSWORD,
95 GENERIC_RIGHTS_USER_ALL_ACCESS};
96 static const struct generic_mapping grp_generic_mapping = {
97 GENERIC_RIGHTS_GROUP_READ,
98 GENERIC_RIGHTS_GROUP_WRITE,
99 GENERIC_RIGHTS_GROUP_EXECUTE,
100 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
101 static const struct generic_mapping ali_generic_mapping = {
102 GENERIC_RIGHTS_ALIAS_READ,
103 GENERIC_RIGHTS_ALIAS_WRITE,
104 GENERIC_RIGHTS_ALIAS_EXECUTE,
105 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
107 /*******************************************************************
108 *******************************************************************/
110 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
111 const struct generic_mapping *map,
112 DOM_SID *sid, uint32 sid_access )
114 DOM_SID domadmin_sid;
115 SEC_ACE ace[5]; /* at most 5 entries */
121 /* basic access for Everyone */
123 init_sec_access(&mask, map->generic_execute | map->generic_read );
124 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
126 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
128 init_sec_access(&mask, map->generic_all);
130 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
131 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
133 /* Add Full Access for Domain Admins if we are a DC */
136 sid_copy( &domadmin_sid, get_global_sam_sid() );
137 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
138 init_sec_ace(&ace[i++], &domadmin_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
141 /* if we have a sid, give it some special access */
144 init_sec_access( &mask, sid_access );
145 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
148 /* create the security descriptor */
150 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
151 return NT_STATUS_NO_MEMORY;
153 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
154 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
155 psa, sd_size)) == NULL)
156 return NT_STATUS_NO_MEMORY;
161 /*******************************************************************
162 Checks if access to an object should be granted, and returns that
163 level of access for further checks.
164 ********************************************************************/
166 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
167 SE_PRIV *rights, uint32 rights_mask,
168 uint32 des_access, uint32 *acc_granted,
171 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
172 uint32 saved_mask = 0;
174 /* check privileges; certain SAM access bits should be overridden
175 by privileges (mostly having to do with creating/modifying/deleting
178 if ( rights && user_has_any_privilege( token, rights ) ) {
180 saved_mask = (des_access & rights_mask);
181 des_access &= ~saved_mask;
183 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
188 /* check the security descriptor first */
190 if ( se_access_check(psd, token, des_access, acc_granted, &status) )
193 /* give root a free pass */
195 if ( geteuid() == sec_initial_uid() ) {
197 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
198 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
200 *acc_granted = des_access;
202 status = NT_STATUS_OK;
208 /* add in any bits saved during the privilege check (only
209 matters is status is ok) */
211 *acc_granted |= rights_mask;
213 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
214 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
215 des_access, *acc_granted));
220 /*******************************************************************
221 Checks if access to a function can be granted
222 ********************************************************************/
224 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
226 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
227 debug, acc_granted, acc_required));
229 /* check the security descriptor first */
231 if ( (acc_granted&acc_required) == acc_required )
234 /* give root a free pass */
236 if (geteuid() == sec_initial_uid()) {
238 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
239 debug, acc_granted, acc_required));
240 DEBUGADD(4,("but overwritten by euid == 0\n"));
245 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
246 debug, acc_granted, acc_required));
248 return NT_STATUS_ACCESS_DENIED;
251 /*******************************************************************
252 Fetch or create a dispinfo struct.
253 ********************************************************************/
255 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
258 * We do a static cache for DISP_INFO's here. Explanation can be found
259 * in Jeremy's checkin message to r11793:
261 * Fix the SAMR cache so it works across completely insane
262 * client behaviour (ie.:
263 * open pipe/open SAMR handle/enumerate 0 - 1024
264 * close SAMR handle, close pipe.
265 * open pipe/open SAMR handle/enumerate 1024 - 2048...
266 * close SAMR handle, close pipe.
267 * And on ad-nausium. Amazing.... probably object-oriented
268 * client side programming in action yet again.
269 * This change should *massively* improve performance when
270 * enumerating users from an LDAP database.
273 * "Our" and the builtin domain are the only ones where we ever
274 * enumerate stuff, so just cache 2 entries.
277 static struct disp_info builtin_dispinfo;
278 static struct disp_info domain_dispinfo;
280 /* There are two cases to consider here:
281 1) The SID is a domain SID and we look for an equality match, or
282 2) This is an account SID and so we return the DISP_INFO* for our
289 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
291 * Necessary only once, but it does not really hurt.
293 sid_copy(&builtin_dispinfo.sid, &global_sid_Builtin);
295 return &builtin_dispinfo;
298 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
300 * Necessary only once, but it does not really hurt.
302 sid_copy(&domain_dispinfo.sid, get_global_sam_sid());
304 return &domain_dispinfo;
310 /*******************************************************************
311 Create a samr_info struct.
312 ********************************************************************/
314 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
316 struct samr_info *info;
321 sid_to_fstring(sid_str, psid);
323 fstrcpy(sid_str,"(NULL)");
326 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
328 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
331 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
333 sid_copy( &info->sid, psid);
334 info->builtin_domain = sid_check_is_builtin(psid);
336 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
337 info->builtin_domain = False;
339 info->mem_ctx = mem_ctx;
341 info->disp_info = get_samr_dispinfo_by_sid(psid);
346 /*******************************************************************
347 Function to free the per SID data.
348 ********************************************************************/
350 static void free_samr_cache(DISP_INFO *disp_info)
352 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
353 sid_string_dbg(&disp_info->sid)));
355 /* We need to become root here because the paged search might have to
356 * tell the LDAP server we're not interested in the rest anymore. */
360 if (disp_info->users) {
361 DEBUG(10,("free_samr_cache: deleting users cache\n"));
362 pdb_search_destroy(disp_info->users);
363 disp_info->users = NULL;
365 if (disp_info->machines) {
366 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
367 pdb_search_destroy(disp_info->machines);
368 disp_info->machines = NULL;
370 if (disp_info->groups) {
371 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
372 pdb_search_destroy(disp_info->groups);
373 disp_info->groups = NULL;
375 if (disp_info->aliases) {
376 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
377 pdb_search_destroy(disp_info->aliases);
378 disp_info->aliases = NULL;
380 if (disp_info->enum_users) {
381 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
382 pdb_search_destroy(disp_info->enum_users);
383 disp_info->enum_users = NULL;
385 disp_info->enum_acb_mask = 0;
390 /*******************************************************************
391 Function to free the per handle data.
392 ********************************************************************/
394 static void free_samr_info(void *ptr)
396 struct samr_info *info=(struct samr_info *) ptr;
398 /* Only free the dispinfo cache if no one bothered to set up
401 if (info->disp_info && info->disp_info->cache_timeout_event == NULL) {
402 free_samr_cache(info->disp_info);
405 talloc_destroy(info->mem_ctx);
408 /*******************************************************************
409 Idle event handler. Throw away the disp info cache.
410 ********************************************************************/
412 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
413 struct timed_event *te,
414 const struct timeval *now,
417 DISP_INFO *disp_info = (DISP_INFO *)private_data;
419 TALLOC_FREE(disp_info->cache_timeout_event);
421 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
423 free_samr_cache(disp_info);
426 /*******************************************************************
427 Setup cache removal idle event handler.
428 ********************************************************************/
430 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
432 /* Remove any pending timeout and update. */
434 TALLOC_FREE(disp_info->cache_timeout_event);
436 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
437 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
438 (unsigned int)secs_fromnow ));
440 disp_info->cache_timeout_event = event_add_timed(
441 smbd_event_context(), NULL,
442 timeval_current_ofs(secs_fromnow, 0),
443 "disp_info_cache_idle_timeout_handler",
444 disp_info_cache_idle_timeout_handler, (void *)disp_info);
447 /*******************************************************************
448 Force flush any cache. We do this on any samr_set_xxx call.
449 We must also remove the timeout handler.
450 ********************************************************************/
452 static void force_flush_samr_cache(DISP_INFO *disp_info)
454 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
458 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
459 TALLOC_FREE(disp_info->cache_timeout_event);
460 free_samr_cache(disp_info);
463 /*******************************************************************
464 Ensure password info is never given out. Paranioa... JRA.
465 ********************************************************************/
467 static void samr_clear_sam_passwd(struct samu *sam_pass)
473 /* These now zero out the old password */
475 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
476 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
479 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
481 struct samr_displayentry *entry;
483 if (info->builtin_domain) {
484 /* No users in builtin. */
488 if (info->users == NULL) {
489 info->users = pdb_search_users(acct_flags);
490 if (info->users == NULL) {
494 /* Fetch the last possible entry, thus trigger an enumeration */
495 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
497 /* Ensure we cache this enumeration. */
498 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
500 return info->users->num_entries;
503 static uint32 count_sam_groups(struct disp_info *info)
505 struct samr_displayentry *entry;
507 if (info->builtin_domain) {
508 /* No groups in builtin. */
512 if (info->groups == NULL) {
513 info->groups = pdb_search_groups();
514 if (info->groups == NULL) {
518 /* Fetch the last possible entry, thus trigger an enumeration */
519 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
521 /* Ensure we cache this enumeration. */
522 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
524 return info->groups->num_entries;
527 static uint32 count_sam_aliases(struct disp_info *info)
529 struct samr_displayentry *entry;
531 if (info->aliases == NULL) {
532 info->aliases = pdb_search_aliases(&info->sid);
533 if (info->aliases == NULL) {
537 /* Fetch the last possible entry, thus trigger an enumeration */
538 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
540 /* Ensure we cache this enumeration. */
541 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
543 return info->aliases->num_entries;
546 /*******************************************************************
548 ********************************************************************/
550 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
552 if (!close_policy_hnd(p, r->in.handle)) {
553 return NT_STATUS_INVALID_HANDLE;
556 ZERO_STRUCTP(r->out.handle);
561 /*******************************************************************
563 ********************************************************************/
565 NTSTATUS _samr_OpenDomain(pipes_struct *p,
566 struct samr_OpenDomain *r)
568 struct samr_info *info;
569 SEC_DESC *psd = NULL;
571 uint32 des_access = r->in.access_mask;
576 /* find the connection policy handle. */
578 if ( !find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info) )
579 return NT_STATUS_INVALID_HANDLE;
581 status = access_check_samr_function(info->acc_granted,
582 SA_RIGHT_SAM_OPEN_DOMAIN,
583 "_samr_OpenDomain" );
585 if ( !NT_STATUS_IS_OK(status) )
588 /*check if access can be granted as requested by client. */
590 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
591 se_map_generic( &des_access, &dom_generic_mapping );
593 se_priv_copy( &se_rights, &se_machine_account );
594 se_priv_add( &se_rights, &se_add_users );
596 status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
597 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
598 &acc_granted, "_samr_OpenDomain" );
600 if ( !NT_STATUS_IS_OK(status) )
603 if (!sid_check_is_domain(r->in.sid) &&
604 !sid_check_is_builtin(r->in.sid)) {
605 return NT_STATUS_NO_SUCH_DOMAIN;
608 /* associate the domain SID with the (unique) handle. */
609 if ((info = get_samr_info_by_sid(r->in.sid))==NULL)
610 return NT_STATUS_NO_MEMORY;
611 info->acc_granted = acc_granted;
613 /* get a (unique) handle. open a policy on it. */
614 if (!create_policy_hnd(p, r->out.domain_handle, free_samr_info, (void *)info))
615 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
617 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
622 /*******************************************************************
624 ********************************************************************/
626 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
627 struct samr_GetUserPwInfo *r)
629 struct samr_info *info = NULL;
631 /* find the policy handle. open a policy on it. */
632 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info))
633 return NT_STATUS_INVALID_HANDLE;
635 if (!sid_check_is_in_our_domain(&info->sid))
636 return NT_STATUS_OBJECT_TYPE_MISMATCH;
638 ZERO_STRUCTP(r->out.info);
640 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
643 * NT sometimes return NT_STATUS_ACCESS_DENIED
644 * I don't know yet why.
650 /*******************************************************************
651 ********************************************************************/
653 static bool get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
654 DOM_SID *sid, uint32 *acc_granted,
655 DISP_INFO **ppdisp_info)
657 struct samr_info *info = NULL;
659 /* find the policy handle. open a policy on it. */
660 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
667 *acc_granted = info->acc_granted;
669 *ppdisp_info = info->disp_info;
675 /*******************************************************************
677 ********************************************************************/
679 NTSTATUS _samr_SetSecurity(pipes_struct *p,
680 struct samr_SetSecurity *r)
683 uint32 acc_granted, i;
686 struct samu *sampass=NULL;
689 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
690 return NT_STATUS_INVALID_HANDLE;
692 if (!(sampass = samu_new( p->mem_ctx))) {
693 DEBUG(0,("No memory!\n"));
694 return NT_STATUS_NO_MEMORY;
697 /* get the user record */
699 ret = pdb_getsampwsid(sampass, &pol_sid);
703 DEBUG(4, ("User %s not found\n", sid_string_dbg(&pol_sid)));
704 TALLOC_FREE(sampass);
705 return NT_STATUS_INVALID_HANDLE;
708 dacl = r->in.sdbuf->sd->dacl;
709 for (i=0; i < dacl->num_aces; i++) {
710 if (sid_equal(&pol_sid, &dacl->aces[i].trustee)) {
711 ret = pdb_set_pass_can_change(sampass,
712 (dacl->aces[i].access_mask &
713 SA_RIGHT_USER_CHANGE_PASSWORD) ?
720 TALLOC_FREE(sampass);
721 return NT_STATUS_ACCESS_DENIED;
724 status = access_check_samr_function(acc_granted,
725 SA_RIGHT_USER_SET_ATTRIBUTES,
726 "_samr_SetSecurity");
727 if (NT_STATUS_IS_OK(status)) {
729 status = pdb_update_sam_account(sampass);
733 TALLOC_FREE(sampass);
738 /*******************************************************************
739 build correct perms based on policies and password times for _samr_query_sec_obj
740 *******************************************************************/
741 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, DOM_SID *user_sid)
743 struct samu *sampass=NULL;
746 if ( !(sampass = samu_new( mem_ctx )) ) {
747 DEBUG(0,("No memory!\n"));
752 ret = pdb_getsampwsid(sampass, user_sid);
756 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
757 TALLOC_FREE(sampass);
761 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
763 if (pdb_get_pass_can_change(sampass)) {
764 TALLOC_FREE(sampass);
767 TALLOC_FREE(sampass);
772 /*******************************************************************
774 ********************************************************************/
776 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
777 struct samr_QuerySecurity *r)
781 SEC_DESC * psd = NULL;
786 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
787 return NT_STATUS_INVALID_HANDLE;
789 DEBUG(10,("_samr_QuerySecurity: querying security on SID: %s\n",
790 sid_string_dbg(&pol_sid)));
792 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
794 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
795 if (pol_sid.sid_rev_num == 0) {
796 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
797 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
798 } else if (sid_equal(&pol_sid,get_global_sam_sid())) {
799 /* check if it is our domain SID */
800 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
801 "with SID: %s\n", sid_string_dbg(&pol_sid)));
802 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
803 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
804 /* check if it is the Builtin Domain */
805 /* TODO: Builtin probably needs a different SD with restricted write access*/
806 DEBUG(5,("_samr_QuerySecurity: querying security on Builtin "
807 "Domain with SID: %s\n", sid_string_dbg(&pol_sid)));
808 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
809 } else if (sid_check_is_in_our_domain(&pol_sid) ||
810 sid_check_is_in_builtin(&pol_sid)) {
811 /* TODO: different SDs have to be generated for aliases groups and users.
812 Currently all three get a default user SD */
813 DEBUG(10,("_samr_QuerySecurity: querying security on Object "
814 "with SID: %s\n", sid_string_dbg(&pol_sid)));
815 if (check_change_pw_access(p->mem_ctx, &pol_sid)) {
816 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
817 &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
819 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_nopwchange_generic_mapping,
820 &pol_sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
823 return NT_STATUS_OBJECT_TYPE_MISMATCH;
826 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
827 return NT_STATUS_NO_MEMORY;
832 /*******************************************************************
833 makes a SAM_ENTRY / UNISTR2* structure from a user list.
834 ********************************************************************/
836 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
837 struct samr_SamEntry **sam_pp,
838 uint32_t num_entries,
840 struct samr_displayentry *entries)
843 struct samr_SamEntry *sam;
847 if (num_entries == 0) {
851 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
853 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
854 return NT_STATUS_NO_MEMORY;
857 for (i = 0; i < num_entries; i++) {
860 * usrmgr expects a non-NULL terminated string with
861 * trust relationships
863 if (entries[i].acct_flags & ACB_DOMTRUST) {
864 init_unistr2(&uni_temp_name, entries[i].account_name,
867 init_unistr2(&uni_temp_name, entries[i].account_name,
871 init_lsa_String(&sam[i].name, entries[i].account_name);
872 sam[i].idx = entries[i].rid;
880 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
882 /*******************************************************************
883 _samr_EnumDomainUsers
884 ********************************************************************/
886 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
887 struct samr_EnumDomainUsers *r)
890 struct samr_info *info = NULL;
892 uint32 enum_context = *r->in.resume_handle;
893 enum remote_arch_types ra_type = get_remote_arch();
894 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
895 uint32 max_entries = max_sam_entries;
896 struct samr_displayentry *entries = NULL;
897 struct samr_SamArray *samr_array = NULL;
898 struct samr_SamEntry *samr_entries = NULL;
900 /* find the policy handle. open a policy on it. */
901 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
902 return NT_STATUS_INVALID_HANDLE;
904 status = access_check_samr_function(info->acc_granted,
905 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
906 "_samr_EnumDomainUsers");
907 if (!NT_STATUS_IS_OK(status)) {
911 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
913 if (info->builtin_domain) {
914 /* No users in builtin. */
915 *r->out.resume_handle = *r->in.resume_handle;
916 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
920 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
922 return NT_STATUS_NO_MEMORY;
929 if ((info->disp_info->enum_users != NULL) &&
930 (info->disp_info->enum_acb_mask != r->in.acct_flags)) {
931 pdb_search_destroy(info->disp_info->enum_users);
932 info->disp_info->enum_users = NULL;
935 if (info->disp_info->enum_users == NULL) {
936 info->disp_info->enum_users = pdb_search_users(r->in.acct_flags);
937 info->disp_info->enum_acb_mask = r->in.acct_flags;
940 if (info->disp_info->enum_users == NULL) {
941 /* END AS ROOT !!!! */
943 return NT_STATUS_ACCESS_DENIED;
946 num_account = pdb_search_entries(info->disp_info->enum_users,
947 enum_context, max_entries,
950 /* END AS ROOT !!!! */
954 if (num_account == 0) {
955 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
957 *r->out.resume_handle = *r->in.resume_handle;
961 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
962 num_account, enum_context,
964 if (!NT_STATUS_IS_OK(status)) {
968 if (max_entries <= num_account) {
969 status = STATUS_MORE_ENTRIES;
971 status = NT_STATUS_OK;
974 /* Ensure we cache this enumeration. */
975 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
977 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
979 samr_array->count = num_account;
980 samr_array->entries = samr_entries;
982 *r->out.resume_handle = *r->in.resume_handle + num_account;
983 *r->out.sam = samr_array;
984 *r->out.num_entries = num_account;
986 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
991 /*******************************************************************
992 makes a SAM_ENTRY / UNISTR2* structure from a group list.
993 ********************************************************************/
995 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
996 struct samr_SamEntry **sam_pp,
997 uint32_t num_sam_entries,
998 struct samr_displayentry *entries)
1000 struct samr_SamEntry *sam;
1005 if (num_sam_entries == 0) {
1009 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1014 for (i = 0; i < num_sam_entries; i++) {
1016 * JRA. I think this should include the null. TNG does not.
1018 init_lsa_String(&sam[i].name, entries[i].account_name);
1019 sam[i].idx = entries[i].rid;
1025 /*******************************************************************
1026 _samr_EnumDomainGroups
1027 ********************************************************************/
1029 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1030 struct samr_EnumDomainGroups *r)
1033 struct samr_info *info = NULL;
1034 struct samr_displayentry *groups;
1036 struct samr_SamArray *samr_array = NULL;
1037 struct samr_SamEntry *samr_entries = NULL;
1039 /* find the policy handle. open a policy on it. */
1040 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1041 return NT_STATUS_INVALID_HANDLE;
1043 status = access_check_samr_function(info->acc_granted,
1044 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1045 "_samr_EnumDomainGroups");
1046 if (!NT_STATUS_IS_OK(status)) {
1050 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1052 if (info->builtin_domain) {
1053 /* No groups in builtin. */
1054 *r->out.resume_handle = *r->in.resume_handle;
1055 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1059 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1061 return NT_STATUS_NO_MEMORY;
1064 /* the domain group array is being allocated in the function below */
1068 if (info->disp_info->groups == NULL) {
1069 info->disp_info->groups = pdb_search_groups();
1071 if (info->disp_info->groups == NULL) {
1073 return NT_STATUS_ACCESS_DENIED;
1077 num_groups = pdb_search_entries(info->disp_info->groups,
1078 *r->in.resume_handle,
1079 MAX_SAM_ENTRIES, &groups);
1082 /* Ensure we cache this enumeration. */
1083 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1085 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1086 num_groups, groups);
1088 samr_array->count = num_groups;
1089 samr_array->entries = samr_entries;
1091 *r->out.sam = samr_array;
1092 *r->out.num_entries = num_groups;
1093 /* this was missing, IMHO:
1094 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1097 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1102 /*******************************************************************
1103 _samr_EnumDomainAliases
1104 ********************************************************************/
1106 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1107 struct samr_EnumDomainAliases *r)
1110 struct samr_info *info;
1111 struct samr_displayentry *aliases;
1112 uint32 num_aliases = 0;
1113 struct samr_SamArray *samr_array = NULL;
1114 struct samr_SamEntry *samr_entries = NULL;
1116 /* find the policy handle. open a policy on it. */
1117 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1118 return NT_STATUS_INVALID_HANDLE;
1120 status = access_check_samr_function(info->acc_granted,
1121 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1122 "_samr_EnumDomainAliases");
1123 if (!NT_STATUS_IS_OK(status)) {
1127 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1128 sid_string_dbg(&info->sid)));
1130 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1132 return NT_STATUS_NO_MEMORY;
1137 if (info->disp_info->aliases == NULL) {
1138 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1139 if (info->disp_info->aliases == NULL) {
1141 return NT_STATUS_ACCESS_DENIED;
1145 num_aliases = pdb_search_entries(info->disp_info->aliases,
1146 *r->in.resume_handle,
1147 MAX_SAM_ENTRIES, &aliases);
1150 /* Ensure we cache this enumeration. */
1151 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1153 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1154 num_aliases, aliases);
1156 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1158 samr_array->count = num_aliases;
1159 samr_array->entries = samr_entries;
1161 *r->out.sam = samr_array;
1162 *r->out.num_entries = num_aliases;
1163 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1168 /*******************************************************************
1169 inits a samr_DispInfoGeneral structure.
1170 ********************************************************************/
1172 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1173 struct samr_DispInfoGeneral *r,
1174 uint32_t num_entries,
1176 struct samr_displayentry *entries)
1180 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1182 if (num_entries == 0) {
1183 return NT_STATUS_OK;
1186 r->count = num_entries;
1188 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1190 return NT_STATUS_NO_MEMORY;
1193 for (i = 0; i < num_entries ; i++) {
1195 init_lsa_String(&r->entries[i].account_name,
1196 entries[i].account_name);
1198 init_lsa_String(&r->entries[i].description,
1199 entries[i].description);
1201 init_lsa_String(&r->entries[i].full_name,
1202 entries[i].fullname);
1204 r->entries[i].rid = entries[i].rid;
1205 r->entries[i].acct_flags = entries[i].acct_flags;
1206 r->entries[i].idx = start_idx+i+1;
1209 return NT_STATUS_OK;
1212 /*******************************************************************
1213 inits a samr_DispInfoFull structure.
1214 ********************************************************************/
1216 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1217 struct samr_DispInfoFull *r,
1218 uint32_t num_entries,
1220 struct samr_displayentry *entries)
1224 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1226 if (num_entries == 0) {
1227 return NT_STATUS_OK;
1230 r->count = num_entries;
1232 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1234 return NT_STATUS_NO_MEMORY;
1237 for (i = 0; i < num_entries ; i++) {
1239 init_lsa_String(&r->entries[i].account_name,
1240 entries[i].account_name);
1242 init_lsa_String(&r->entries[i].description,
1243 entries[i].description);
1245 r->entries[i].rid = entries[i].rid;
1246 r->entries[i].acct_flags = entries[i].acct_flags;
1247 r->entries[i].idx = start_idx+i+1;
1250 return NT_STATUS_OK;
1253 /*******************************************************************
1254 inits a samr_DispInfoFullGroups structure.
1255 ********************************************************************/
1257 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1258 struct samr_DispInfoFullGroups *r,
1259 uint32_t num_entries,
1261 struct samr_displayentry *entries)
1265 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1267 if (num_entries == 0) {
1268 return NT_STATUS_OK;
1271 r->count = num_entries;
1273 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1275 return NT_STATUS_NO_MEMORY;
1278 for (i = 0; i < num_entries ; i++) {
1280 init_lsa_String(&r->entries[i].account_name,
1281 entries[i].account_name);
1283 init_lsa_String(&r->entries[i].description,
1284 entries[i].description);
1286 r->entries[i].rid = entries[i].rid;
1287 r->entries[i].acct_flags = entries[i].acct_flags;
1288 r->entries[i].idx = start_idx+i+1;
1291 return NT_STATUS_OK;
1294 /*******************************************************************
1295 inits a samr_DispInfoAscii structure.
1296 ********************************************************************/
1298 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1299 struct samr_DispInfoAscii *r,
1300 uint32_t num_entries,
1302 struct samr_displayentry *entries)
1306 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1308 if (num_entries == 0) {
1309 return NT_STATUS_OK;
1312 r->count = num_entries;
1314 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1316 return NT_STATUS_NO_MEMORY;
1319 for (i = 0; i < num_entries ; i++) {
1321 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1322 entries[i].account_name);
1324 r->entries[i].idx = start_idx+i+1;
1327 return NT_STATUS_OK;
1330 /*******************************************************************
1331 inits a samr_DispInfoAscii structure.
1332 ********************************************************************/
1334 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1335 struct samr_DispInfoAscii *r,
1336 uint32_t num_entries,
1338 struct samr_displayentry *entries)
1342 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1344 if (num_entries == 0) {
1345 return NT_STATUS_OK;
1348 r->count = num_entries;
1350 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1352 return NT_STATUS_NO_MEMORY;
1355 for (i = 0; i < num_entries ; i++) {
1357 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1358 entries[i].account_name);
1360 r->entries[i].idx = start_idx+i+1;
1363 return NT_STATUS_OK;
1366 /*******************************************************************
1367 _samr_QueryDisplayInfo
1368 ********************************************************************/
1370 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1371 struct samr_QueryDisplayInfo *r)
1374 struct samr_info *info = NULL;
1375 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1377 uint32 max_entries = r->in.max_entries;
1378 uint32 enum_context = r->in.start_idx;
1379 uint32 max_size = r->in.buf_size;
1381 union samr_DispInfo *disp_info = r->out.info;
1383 uint32 temp_size=0, total_data_size=0;
1384 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1385 uint32 num_account = 0;
1386 enum remote_arch_types ra_type = get_remote_arch();
1387 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1388 struct samr_displayentry *entries = NULL;
1390 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1392 /* find the policy handle. open a policy on it. */
1393 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1394 return NT_STATUS_INVALID_HANDLE;
1397 * calculate how many entries we will return.
1399 * - the number of entries the client asked
1400 * - our limit on that
1401 * - the starting point (enumeration context)
1402 * - the buffer size the client will accept
1406 * We are a lot more like W2K. Instead of reading the SAM
1407 * each time to find the records we need to send back,
1408 * we read it once and link that copy to the sam handle.
1409 * For large user list (over the MAX_SAM_ENTRIES)
1410 * it's a definitive win.
1411 * second point to notice: between enumerations
1412 * our sam is now the same as it's a snapshoot.
1413 * third point: got rid of the static SAM_USER_21 struct
1414 * no more intermediate.
1415 * con: it uses much more memory, as a full copy is stored
1418 * If you want to change it, think twice and think
1419 * of the second point , that's really important.
1424 if ((r->in.level < 1) || (r->in.level > 5)) {
1425 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1426 (unsigned int)r->in.level ));
1427 return NT_STATUS_INVALID_INFO_CLASS;
1430 /* first limit the number of entries we will return */
1431 if(max_entries > max_sam_entries) {
1432 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1433 "entries, limiting to %d\n", max_entries,
1435 max_entries = max_sam_entries;
1438 /* calculate the size and limit on the number of entries we will
1441 temp_size=max_entries*struct_size;
1443 if (temp_size>max_size) {
1444 max_entries=MIN((max_size/struct_size),max_entries);;
1445 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1446 "only %d entries\n", max_entries));
1451 /* THe following done as ROOT. Don't return without unbecome_root(). */
1453 switch (r->in.level) {
1456 if (info->disp_info->users == NULL) {
1457 info->disp_info->users = pdb_search_users(ACB_NORMAL);
1458 if (info->disp_info->users == NULL) {
1460 return NT_STATUS_ACCESS_DENIED;
1462 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1463 (unsigned int)enum_context ));
1465 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1466 (unsigned int)enum_context ));
1469 num_account = pdb_search_entries(info->disp_info->users,
1470 enum_context, max_entries,
1474 if (info->disp_info->machines == NULL) {
1475 info->disp_info->machines =
1476 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1477 if (info->disp_info->machines == NULL) {
1479 return NT_STATUS_ACCESS_DENIED;
1481 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1482 (unsigned int)enum_context ));
1484 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1485 (unsigned int)enum_context ));
1488 num_account = pdb_search_entries(info->disp_info->machines,
1489 enum_context, max_entries,
1494 if (info->disp_info->groups == NULL) {
1495 info->disp_info->groups = pdb_search_groups();
1496 if (info->disp_info->groups == NULL) {
1498 return NT_STATUS_ACCESS_DENIED;
1500 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1501 (unsigned int)enum_context ));
1503 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1504 (unsigned int)enum_context ));
1507 num_account = pdb_search_entries(info->disp_info->groups,
1508 enum_context, max_entries,
1513 smb_panic("info class changed");
1519 /* Now create reply structure */
1520 switch (r->in.level) {
1522 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1523 num_account, enum_context,
1527 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1528 num_account, enum_context,
1532 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1533 num_account, enum_context,
1537 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1538 num_account, enum_context,
1542 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1543 num_account, enum_context,
1547 smb_panic("info class changed");
1551 if (!NT_STATUS_IS_OK(disp_ret))
1554 /* calculate the total size */
1555 total_data_size=num_account*struct_size;
1558 status = STATUS_MORE_ENTRIES;
1560 status = NT_STATUS_OK;
1563 /* Ensure we cache this enumeration. */
1564 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1566 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1568 *r->out.total_size = total_data_size;
1569 *r->out.returned_size = temp_size;
1574 /****************************************************************
1575 _samr_QueryDisplayInfo2
1576 ****************************************************************/
1578 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1579 struct samr_QueryDisplayInfo2 *r)
1581 struct samr_QueryDisplayInfo q;
1583 q.in.domain_handle = r->in.domain_handle;
1584 q.in.level = r->in.level;
1585 q.in.start_idx = r->in.start_idx;
1586 q.in.max_entries = r->in.max_entries;
1587 q.in.buf_size = r->in.buf_size;
1589 q.out.total_size = r->out.total_size;
1590 q.out.returned_size = r->out.returned_size;
1591 q.out.info = r->out.info;
1593 return _samr_QueryDisplayInfo(p, &q);
1596 /****************************************************************
1597 _samr_QueryDisplayInfo3
1598 ****************************************************************/
1600 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1601 struct samr_QueryDisplayInfo3 *r)
1603 struct samr_QueryDisplayInfo q;
1605 q.in.domain_handle = r->in.domain_handle;
1606 q.in.level = r->in.level;
1607 q.in.start_idx = r->in.start_idx;
1608 q.in.max_entries = r->in.max_entries;
1609 q.in.buf_size = r->in.buf_size;
1611 q.out.total_size = r->out.total_size;
1612 q.out.returned_size = r->out.returned_size;
1613 q.out.info = r->out.info;
1615 return _samr_QueryDisplayInfo(p, &q);
1618 /*******************************************************************
1619 _samr_QueryAliasInfo
1620 ********************************************************************/
1622 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1623 struct samr_QueryAliasInfo *r)
1626 struct acct_info info;
1629 union samr_AliasInfo *alias_info = NULL;
1630 const char *alias_name = NULL;
1631 const char *alias_description = NULL;
1633 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1635 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1637 return NT_STATUS_NO_MEMORY;
1640 /* find the policy handle. open a policy on it. */
1641 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &sid, &acc_granted, NULL))
1642 return NT_STATUS_INVALID_HANDLE;
1644 status = access_check_samr_function(acc_granted,
1645 SA_RIGHT_ALIAS_LOOKUP_INFO,
1646 "_samr_QueryAliasInfo");
1647 if (!NT_STATUS_IS_OK(status)) {
1652 status = pdb_get_aliasinfo(&sid, &info);
1655 if ( !NT_STATUS_IS_OK(status))
1658 /* FIXME: info contains fstrings */
1659 alias_name = talloc_strdup(r, info.acct_name);
1660 alias_description = talloc_strdup(r, info.acct_desc);
1662 switch (r->in.level) {
1664 init_samr_alias_info1(&alias_info->all,
1669 case ALIASINFODESCRIPTION:
1670 init_samr_alias_info3(&alias_info->description,
1674 return NT_STATUS_INVALID_INFO_CLASS;
1677 *r->out.info = alias_info;
1679 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1681 return NT_STATUS_OK;
1685 /*******************************************************************
1686 samr_reply_lookup_ids
1687 ********************************************************************/
1689 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1691 uint32 rid[MAX_SAM_ENTRIES];
1692 int num_rids = q_u->num_sids1;
1694 r_u->status = NT_STATUS_OK;
1696 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1698 if (num_rids > MAX_SAM_ENTRIES) {
1699 num_rids = MAX_SAM_ENTRIES;
1700 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1705 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1707 for (i = 0; i < num_rids && status == 0; i++)
1709 struct sam_passwd *sam_pass;
1713 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1714 q_u->uni_user_name[i].uni_str_len));
1716 /* find the user account */
1718 sam_pass = get_smb21pwd_entry(user_name, 0);
1721 if (sam_pass == NULL)
1723 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1728 rid[i] = sam_pass->user_rid;
1734 rid[0] = BUILTIN_ALIAS_RID_USERS;
1736 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1738 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1744 /*******************************************************************
1746 ********************************************************************/
1748 NTSTATUS _samr_LookupNames(pipes_struct *p,
1749 struct samr_LookupNames *r)
1752 uint32 rid[MAX_SAM_ENTRIES];
1753 enum lsa_SidType type[MAX_SAM_ENTRIES];
1755 int num_rids = r->in.num_names;
1758 struct samr_Ids rids, types;
1760 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1765 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL)) {
1766 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1769 status = access_check_samr_function(acc_granted,
1770 0, /* Don't know the acc_bits yet */
1771 "_samr_LookupNames");
1772 if (!NT_STATUS_IS_OK(status)) {
1776 if (num_rids > MAX_SAM_ENTRIES) {
1777 num_rids = MAX_SAM_ENTRIES;
1778 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1781 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1782 sid_string_dbg(&pol_sid)));
1784 for (i = 0; i < num_rids; i++) {
1786 status = NT_STATUS_NONE_MAPPED;
1787 type[i] = SID_NAME_UNKNOWN;
1789 rid[i] = 0xffffffff;
1791 if (sid_check_is_builtin(&pol_sid)) {
1792 if (lookup_builtin_name(r->in.names[i].string,
1795 type[i] = SID_NAME_ALIAS;
1798 lookup_global_sam_name(r->in.names[i].string, 0,
1802 if (type[i] != SID_NAME_UNKNOWN) {
1803 status = NT_STATUS_OK;
1807 rids.count = num_rids;
1810 types.count = num_rids;
1813 *r->out.rids = rids;
1814 *r->out.types = types;
1816 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1821 /*******************************************************************
1822 _samr_ChangePasswordUser2
1823 ********************************************************************/
1825 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1826 struct samr_ChangePasswordUser2 *r)
1832 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1834 fstrcpy(user_name, r->in.account->string);
1835 fstrcpy(wks, r->in.server->string);
1837 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1840 * Pass the user through the NT -> unix user mapping
1844 (void)map_username(user_name);
1847 * UNIX username case mangling not required, pass_oem_change
1848 * is case insensitive.
1851 status = pass_oem_change(user_name,
1852 r->in.lm_password->data,
1853 r->in.lm_verifier->hash,
1854 r->in.nt_password->data,
1855 r->in.nt_verifier->hash,
1858 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1863 /*******************************************************************
1864 _samr_ChangePasswordUser3
1865 ********************************************************************/
1867 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
1868 struct samr_ChangePasswordUser3 *r)
1872 const char *wks = NULL;
1873 uint32 reject_reason;
1874 struct samr_DomInfo1 *dominfo = NULL;
1875 struct samr_ChangeReject *reject = NULL;
1877 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1879 fstrcpy(user_name, r->in.account->string);
1880 if (r->in.server && r->in.server->string) {
1881 wks = r->in.server->string;
1884 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1887 * Pass the user through the NT -> unix user mapping
1891 (void)map_username(user_name);
1894 * UNIX username case mangling not required, pass_oem_change
1895 * is case insensitive.
1898 status = pass_oem_change(user_name,
1899 r->in.lm_password->data,
1900 r->in.lm_verifier->hash,
1901 r->in.nt_password->data,
1902 r->in.nt_verifier->hash,
1905 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
1906 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
1908 uint32 min_pass_len,pass_hist,password_properties;
1909 time_t u_expire, u_min_age;
1910 NTTIME nt_expire, nt_min_age;
1911 uint32 account_policy_temp;
1913 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
1915 return NT_STATUS_NO_MEMORY;
1918 reject = TALLOC_ZERO_P(p->mem_ctx, struct samr_ChangeReject);
1920 return NT_STATUS_NO_MEMORY;
1927 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
1928 min_pass_len = account_policy_temp;
1930 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
1931 pass_hist = account_policy_temp;
1933 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
1934 password_properties = account_policy_temp;
1936 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1937 u_expire = account_policy_temp;
1939 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1940 u_min_age = account_policy_temp;
1946 unix_to_nt_time_abs(&nt_expire, u_expire);
1947 unix_to_nt_time_abs(&nt_min_age, u_min_age);
1949 if (lp_check_password_script() && *lp_check_password_script()) {
1950 password_properties |= DOMAIN_PASSWORD_COMPLEX;
1953 init_samr_DomInfo1(dominfo,
1956 password_properties,
1960 reject->reason = reject_reason;
1962 *r->out.dominfo = dominfo;
1963 *r->out.reject = reject;
1966 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1971 /*******************************************************************
1972 makes a SAMR_R_LOOKUP_RIDS structure.
1973 ********************************************************************/
1975 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1977 struct lsa_String **lsa_name_array_p)
1979 struct lsa_String *lsa_name_array = NULL;
1982 *lsa_name_array_p = NULL;
1984 if (num_names != 0) {
1985 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
1986 if (!lsa_name_array) {
1991 for (i = 0; i < num_names; i++) {
1992 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1993 init_lsa_String(&lsa_name_array[i], names[i]);
1996 *lsa_name_array_p = lsa_name_array;
2001 /*******************************************************************
2003 ********************************************************************/
2005 NTSTATUS _samr_LookupRids(pipes_struct *p,
2006 struct samr_LookupRids *r)
2010 enum lsa_SidType *attrs = NULL;
2011 uint32 *wire_attrs = NULL;
2013 int num_rids = (int)r->in.num_rids;
2016 struct lsa_Strings names_array;
2017 struct samr_Ids types_array;
2018 struct lsa_String *lsa_names = NULL;
2020 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2022 /* find the policy handle. open a policy on it. */
2023 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL))
2024 return NT_STATUS_INVALID_HANDLE;
2026 if (num_rids > 1000) {
2027 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2028 "to samba4 idl this is not possible\n", num_rids));
2029 return NT_STATUS_UNSUCCESSFUL;
2033 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2034 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2035 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2037 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2038 return NT_STATUS_NO_MEMORY;
2045 become_root(); /* lookup_sid can require root privs */
2046 status = pdb_lookup_rids(&pol_sid, num_rids, r->in.rids,
2050 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2051 status = NT_STATUS_OK;
2054 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2056 return NT_STATUS_NO_MEMORY;
2059 /* Convert from enum lsa_SidType to uint32 for wire format. */
2060 for (i = 0; i < num_rids; i++) {
2061 wire_attrs[i] = (uint32)attrs[i];
2064 names_array.count = num_rids;
2065 names_array.names = lsa_names;
2067 types_array.count = num_rids;
2068 types_array.ids = wire_attrs;
2070 *r->out.names = names_array;
2071 *r->out.types = types_array;
2073 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2078 /*******************************************************************
2080 ********************************************************************/
2082 NTSTATUS _samr_OpenUser(pipes_struct *p,
2083 struct samr_OpenUser *r)
2085 struct samu *sampass=NULL;
2087 POLICY_HND domain_pol = *r->in.domain_handle;
2088 POLICY_HND *user_pol = r->out.user_handle;
2089 struct samr_info *info = NULL;
2090 SEC_DESC *psd = NULL;
2092 uint32 des_access = r->in.access_mask;
2098 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
2100 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2101 return NT_STATUS_INVALID_HANDLE;
2103 nt_status = access_check_samr_function(acc_granted,
2104 SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
2107 if ( !NT_STATUS_IS_OK(nt_status) )
2110 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2111 return NT_STATUS_NO_MEMORY;
2114 /* append the user's RID to it */
2116 if (!sid_append_rid(&sid, r->in.rid))
2117 return NT_STATUS_NO_SUCH_USER;
2119 /* check if access can be granted as requested by client. */
2121 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2122 se_map_generic(&des_access, &usr_generic_mapping);
2124 se_priv_copy( &se_rights, &se_machine_account );
2125 se_priv_add( &se_rights, &se_add_users );
2127 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2128 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2129 &acc_granted, "_samr_OpenUser");
2131 if ( !NT_STATUS_IS_OK(nt_status) )
2135 ret=pdb_getsampwsid(sampass, &sid);
2138 /* check that the SID exists in our domain. */
2140 return NT_STATUS_NO_SUCH_USER;
2143 TALLOC_FREE(sampass);
2145 /* associate the user's SID and access bits with the new handle. */
2146 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2147 return NT_STATUS_NO_MEMORY;
2148 info->acc_granted = acc_granted;
2150 /* get a (unique) handle. open a policy on it. */
2151 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
2152 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2154 return NT_STATUS_OK;
2157 /*************************************************************************
2158 get_user_info_7. Safe. Only gives out account_name.
2159 *************************************************************************/
2161 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2162 struct samr_UserInfo7 *r,
2165 struct samu *smbpass=NULL;
2167 const char *account_name = NULL;
2171 if ( !(smbpass = samu_new( mem_ctx )) ) {
2172 return NT_STATUS_NO_MEMORY;
2176 ret = pdb_getsampwsid(smbpass, user_sid);
2180 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2181 return NT_STATUS_NO_SUCH_USER;
2184 account_name = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2185 if (!account_name) {
2186 TALLOC_FREE(smbpass);
2187 return NT_STATUS_NO_MEMORY;
2189 TALLOC_FREE(smbpass);
2191 DEBUG(3,("User:[%s]\n", account_name));
2193 init_samr_user_info7(r, account_name);
2195 return NT_STATUS_OK;
2198 /*************************************************************************
2199 get_user_info_9. Only gives out primary group SID.
2200 *************************************************************************/
2202 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2203 struct samr_UserInfo9 *r,
2206 struct samu *smbpass=NULL;
2211 if ( !(smbpass = samu_new( mem_ctx )) ) {
2212 return NT_STATUS_NO_MEMORY;
2216 ret = pdb_getsampwsid(smbpass, user_sid);
2220 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2221 TALLOC_FREE(smbpass);
2222 return NT_STATUS_NO_SUCH_USER;
2225 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
2227 init_samr_user_info9(r, pdb_get_group_rid(smbpass));
2229 TALLOC_FREE(smbpass);
2231 return NT_STATUS_OK;
2234 /*************************************************************************
2235 get_user_info_16. Safe. Only gives out acb bits.
2236 *************************************************************************/
2238 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2239 struct samr_UserInfo16 *r,
2242 struct samu *smbpass=NULL;
2247 if ( !(smbpass = samu_new( mem_ctx )) ) {
2248 return NT_STATUS_NO_MEMORY;
2252 ret = pdb_getsampwsid(smbpass, user_sid);
2256 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2257 TALLOC_FREE(smbpass);
2258 return NT_STATUS_NO_SUCH_USER;
2261 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
2263 init_samr_user_info16(r, pdb_get_acct_ctrl(smbpass));
2265 TALLOC_FREE(smbpass);
2267 return NT_STATUS_OK;
2270 /*************************************************************************
2271 get_user_info_18. OK - this is the killer as it gives out password info.
2272 Ensure that this is only allowed on an encrypted connection with a root
2274 *************************************************************************/
2276 static NTSTATUS get_user_info_18(pipes_struct *p,
2277 TALLOC_CTX *mem_ctx,
2278 struct samr_UserInfo18 *r,
2281 struct samu *smbpass=NULL;
2286 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2287 return NT_STATUS_ACCESS_DENIED;
2290 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
2291 return NT_STATUS_ACCESS_DENIED;
2295 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2298 if ( !(smbpass = samu_new( mem_ctx )) ) {
2299 return NT_STATUS_NO_MEMORY;
2302 ret = pdb_getsampwsid(smbpass, user_sid);
2305 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2306 TALLOC_FREE(smbpass);
2307 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2310 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2312 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2313 TALLOC_FREE(smbpass);
2314 return NT_STATUS_ACCOUNT_DISABLED;
2317 init_samr_user_info18(r, pdb_get_lanman_passwd(smbpass),
2318 pdb_get_nt_passwd(smbpass));
2320 TALLOC_FREE(smbpass);
2322 return NT_STATUS_OK;
2325 /*************************************************************************
2327 *************************************************************************/
2329 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2330 struct samr_UserInfo20 *r,
2333 struct samu *sampass=NULL;
2335 const char *munged_dial = NULL;
2336 const char *munged_dial_decoded = NULL;
2341 if ( !(sampass = samu_new( mem_ctx )) ) {
2342 return NT_STATUS_NO_MEMORY;
2346 ret = pdb_getsampwsid(sampass, user_sid);
2350 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2351 TALLOC_FREE(sampass);
2352 return NT_STATUS_NO_SUCH_USER;
2355 munged_dial = pdb_get_munged_dial(sampass);
2357 samr_clear_sam_passwd(sampass);
2359 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
2362 blob = base64_decode_data_blob(munged_dial);
2363 munged_dial_decoded = talloc_strndup(mem_ctx,
2364 (const char *)blob.data,
2366 data_blob_free(&blob);
2367 if (!munged_dial_decoded) {
2368 TALLOC_FREE(sampass);
2369 return NT_STATUS_NO_MEMORY;
2374 init_unistr2_from_datablob(&usr->uni_munged_dial, &blob);
2375 init_uni_hdr(&usr->hdr_munged_dial, &usr->uni_munged_dial);
2376 data_blob_free(&blob);
2378 init_samr_user_info20(r, munged_dial_decoded);
2380 TALLOC_FREE(sampass);
2382 return NT_STATUS_OK;
2386 /*************************************************************************
2388 *************************************************************************/
2390 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2391 struct samr_UserInfo21 *r,
2393 DOM_SID *domain_sid)
2395 struct samu *pw = NULL;
2397 const DOM_SID *sid_user, *sid_group;
2398 uint32_t rid, primary_gid;
2399 NTTIME last_logon, last_logoff, last_password_change,
2400 acct_expiry, allow_password_change, force_password_change;
2401 time_t must_change_time;
2402 uint8_t password_expired;
2403 const char *account_name, *full_name, *home_directory, *home_drive,
2404 *logon_script, *profile_path, *description,
2405 *workstations, *comment, *parameters;
2406 struct samr_LogonHours logon_hours;
2407 const char *munged_dial = NULL;
2412 if (!(pw = samu_new(mem_ctx))) {
2413 return NT_STATUS_NO_MEMORY;
2417 ret = pdb_getsampwsid(pw, user_sid);
2421 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2423 return NT_STATUS_NO_SUCH_USER;
2426 samr_clear_sam_passwd(pw);
2428 DEBUG(3,("User:[%s]\n", pdb_get_username(pw)));
2430 sid_user = pdb_get_user_sid(pw);
2432 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2433 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2434 "the domain sid %s. Failing operation.\n",
2435 pdb_get_username(pw), sid_string_dbg(sid_user),
2436 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_21: 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)));
2451 return NT_STATUS_UNSUCCESSFUL;
2454 unix_to_nt_time(&last_logon, pdb_get_logon_time(pw));
2455 unix_to_nt_time(&last_logoff, pdb_get_logoff_time(pw));
2456 unix_to_nt_time(&acct_expiry, pdb_get_kickoff_time(pw));
2457 unix_to_nt_time(&last_password_change, pdb_get_pass_last_set_time(pw));
2458 unix_to_nt_time(&allow_password_change, pdb_get_pass_can_change_time(pw));
2460 must_change_time = pdb_get_pass_must_change_time(pw);
2461 if (must_change_time == get_time_t_max()) {
2462 unix_to_nt_time_abs(&force_password_change, must_change_time);
2464 unix_to_nt_time(&force_password_change, must_change_time);
2467 if (pdb_get_pass_must_change_time(pw) == 0) {
2468 password_expired = PASS_MUST_CHANGE_AT_NEXT_LOGON;
2470 password_expired = 0;
2473 munged_dial = pdb_get_munged_dial(pw);
2475 blob = base64_decode_data_blob(munged_dial);
2476 parameters = talloc_strndup(mem_ctx, (const char *)blob.data, blob.length);
2477 data_blob_free(&blob);
2480 return NT_STATUS_NO_MEMORY;
2487 account_name = talloc_strdup(mem_ctx, pdb_get_username(pw));
2488 full_name = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2489 home_directory = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2490 home_drive = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2491 logon_script = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2492 profile_path = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2493 description = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2494 workstations = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2495 comment = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2497 logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2501 Look at a user on a real NT4 PDC with usrmgr, press
2502 'ok'. Then you will see that fields_present is set to
2503 0x08f827fa. Look at the user immediately after that again,
2504 and you will see that 0x00fffff is returned. This solves
2505 the problem that you get access denied after having looked
2511 init_unistr2_from_datablob(&usr->uni_munged_dial, &munged_dial_blob);
2512 init_uni_hdr(&usr->hdr_munged_dial, &usr->uni_munged_dial);
2513 data_blob_free(&munged_dial_blob);
2517 init_samr_user_info21(r,
2520 last_password_change,
2522 allow_password_change,
2523 force_password_change,
2536 pdb_get_acct_ctrl(pw),
2537 pdb_build_fields_present(pw),
2539 pdb_get_bad_password_count(pw),
2540 pdb_get_logon_count(pw),
2541 0, /* country_code */
2543 0, /* nt_password_set */
2544 0, /* lm_password_set */
2548 return NT_STATUS_OK;
2551 /*******************************************************************
2553 ********************************************************************/
2555 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2556 struct samr_QueryUserInfo *r)
2559 union samr_UserInfo *user_info = NULL;
2560 struct samr_info *info = NULL;
2564 /* search for the handle */
2565 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info))
2566 return NT_STATUS_INVALID_HANDLE;
2568 domain_sid = info->sid;
2570 sid_split_rid(&domain_sid, &rid);
2572 if (!sid_check_is_in_our_domain(&info->sid))
2573 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2575 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2576 sid_string_dbg(&info->sid)));
2578 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
2580 return NT_STATUS_NO_MEMORY;
2583 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
2585 switch (r->in.level) {
2587 status = get_user_info_7(p->mem_ctx, &user_info->info7, &info->sid);
2588 if (!NT_STATUS_IS_OK(status)) {
2593 status = get_user_info_9(p->mem_ctx, &user_info->info9, &info->sid);
2594 if (!NT_STATUS_IS_OK(status)) {
2599 status = get_user_info_16(p->mem_ctx, &user_info->info16, &info->sid);
2600 if (!NT_STATUS_IS_OK(status)) {
2606 status = get_user_info_18(p, p->mem_ctx, &user_info->info18, &info->sid);
2607 if (!NT_STATUS_IS_OK(status)) {
2613 status = get_user_info_20(p->mem_ctx, &user_info->info20, &info->sid);
2614 if (!NT_STATUS_IS_OK(status)) {
2620 status = get_user_info_21(p->mem_ctx, &user_info->info21,
2621 &info->sid, &domain_sid);
2622 if (!NT_STATUS_IS_OK(status)) {
2628 return NT_STATUS_INVALID_INFO_CLASS;
2631 *r->out.info = user_info;
2633 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
2638 /*******************************************************************
2639 _samr_GetGroupsForUser
2640 ********************************************************************/
2642 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
2643 struct samr_GetGroupsForUser *r)
2645 struct samu *sam_pass=NULL;
2648 struct samr_RidWithAttribute dom_gid;
2649 struct samr_RidWithAttribute *gids = NULL;
2650 uint32 primary_group_rid;
2651 size_t num_groups = 0;
2657 bool success = False;
2659 struct samr_RidWithAttributeArray *rids = NULL;
2662 * from the SID in the request:
2663 * we should send back the list of DOMAIN GROUPS
2664 * the user is a member of
2666 * and only the DOMAIN GROUPS
2667 * no ALIASES !!! neither aliases of the domain
2668 * nor aliases of the builtin SID
2673 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2675 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
2677 return NT_STATUS_NO_MEMORY;
2680 /* find the policy handle. open a policy on it. */
2681 if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &sid, &acc_granted, NULL))
2682 return NT_STATUS_INVALID_HANDLE;
2684 result = access_check_samr_function(acc_granted,
2685 SA_RIGHT_USER_GET_GROUPS,
2686 "_samr_GetGroupsForUser");
2687 if (!NT_STATUS_IS_OK(result)) {
2691 if (!sid_check_is_in_our_domain(&sid))
2692 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2694 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2695 return NT_STATUS_NO_MEMORY;
2699 ret = pdb_getsampwsid(sam_pass, &sid);
2703 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2704 sid_string_dbg(&sid)));
2705 return NT_STATUS_NO_SUCH_USER;
2710 /* make both calls inside the root block */
2712 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2713 &sids, &unix_gids, &num_groups);
2714 if ( NT_STATUS_IS_OK(result) ) {
2715 success = sid_peek_check_rid(get_global_sam_sid(),
2716 pdb_get_group_sid(sam_pass),
2717 &primary_group_rid);
2721 if (!NT_STATUS_IS_OK(result)) {
2722 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2723 sid_string_dbg(&sid)));
2728 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2729 sid_string_dbg(pdb_get_group_sid(sam_pass)),
2730 pdb_get_username(sam_pass)));
2731 TALLOC_FREE(sam_pass);
2732 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2738 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2740 dom_gid.rid = primary_group_rid;
2741 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2743 for (i=0; i<num_groups; i++) {
2745 if (!sid_peek_check_rid(get_global_sam_sid(),
2746 &(sids[i]), &dom_gid.rid)) {
2747 DEBUG(10, ("Found sid %s not in our domain\n",
2748 sid_string_dbg(&sids[i])));
2752 if (dom_gid.rid == primary_group_rid) {
2753 /* We added the primary group directly from the
2754 * sam_account. The other SIDs are unique from
2755 * enum_group_memberships */
2759 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2762 rids->count = num_gids;
2765 *r->out.rids = rids;
2767 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2772 /*******************************************************************
2773 samr_QueryDomainInfo_internal
2774 ********************************************************************/
2776 static NTSTATUS samr_QueryDomainInfo_internal(const char *fn_name,
2778 struct policy_handle *handle,
2780 union samr_DomainInfo **dom_info_ptr)
2782 NTSTATUS status = NT_STATUS_OK;
2783 struct samr_info *info = NULL;
2784 union samr_DomainInfo *dom_info;
2785 uint32 min_pass_len,pass_hist,password_properties;
2786 time_t u_expire, u_min_age;
2787 NTTIME nt_expire, nt_min_age;
2789 time_t u_lock_duration, u_reset_time;
2790 NTTIME nt_lock_duration, nt_reset_time;
2795 uint32 account_policy_temp;
2800 uint32 num_users=0, num_groups=0, num_aliases=0;
2802 DEBUG(5,("%s: %d\n", fn_name, __LINE__));
2804 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
2806 return NT_STATUS_NO_MEMORY;
2809 *dom_info_ptr = dom_info;
2811 /* find the policy handle. open a policy on it. */
2812 if (!find_policy_by_hnd(p, handle, (void **)(void *)&info)) {
2813 return NT_STATUS_INVALID_HANDLE;
2823 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2824 min_pass_len = account_policy_temp;
2826 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2827 pass_hist = account_policy_temp;
2829 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2830 password_properties = account_policy_temp;
2832 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2833 u_expire = account_policy_temp;
2835 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2836 u_min_age = account_policy_temp;
2842 unix_to_nt_time_abs(&nt_expire, u_expire);
2843 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2845 init_samr_DomInfo1(&dom_info->info1,
2846 (uint16)min_pass_len,
2848 password_properties,
2858 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
2859 num_groups = count_sam_groups(info->disp_info);
2860 num_aliases = count_sam_aliases(info->disp_info);
2862 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
2863 u_logout = account_policy_temp;
2865 unix_to_nt_time_abs(&nt_logout, u_logout);
2867 if (!pdb_get_seq_num(&seq_num))
2868 seq_num = time(NULL);
2874 server_role = ROLE_DOMAIN_PDC;
2875 if (lp_server_role() == ROLE_DOMAIN_BDC)
2876 server_role = ROLE_DOMAIN_BDC;
2878 init_samr_DomInfo2(&dom_info->info2,
2899 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
2900 u_logout = (time_t)ul;
2907 unix_to_nt_time_abs(&nt_logout, u_logout);
2909 init_samr_DomInfo3(&dom_info->info3,
2914 init_samr_DomInfo4(&dom_info->info4,
2918 init_samr_DomInfo5(&dom_info->info5,
2919 get_global_sam_name());
2922 /* NT returns its own name when a PDC. win2k and later
2923 * only the name of the PDC if itself is a BDC (samba4
2925 init_samr_DomInfo6(&dom_info->info6,
2929 server_role = ROLE_DOMAIN_PDC;
2930 if (lp_server_role() == ROLE_DOMAIN_BDC)
2931 server_role = ROLE_DOMAIN_BDC;
2933 init_samr_DomInfo7(&dom_info->info7,
2942 if (!pdb_get_seq_num(&seq_num)) {
2943 seq_num = time(NULL);
2950 init_samr_DomInfo8(&dom_info->info8,
2960 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2961 u_lock_duration = account_policy_temp;
2962 if (u_lock_duration != -1) {
2963 u_lock_duration *= 60;
2966 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2967 u_reset_time = account_policy_temp * 60;
2969 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
2970 lockout = account_policy_temp;
2976 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
2977 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
2979 init_samr_DomInfo12(&dom_info->info12,
2985 return NT_STATUS_INVALID_INFO_CLASS;
2988 DEBUG(5,("%s: %d\n", fn_name, __LINE__));
2993 /*******************************************************************
2994 _samr_QueryDomainInfo
2995 ********************************************************************/
2997 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
2998 struct samr_QueryDomainInfo *r)
3000 return samr_QueryDomainInfo_internal("_samr_QueryDomainInfo",
3002 r->in.domain_handle,
3007 /* W2k3 seems to use the same check for all 3 objects that can be created via
3008 * SAMR, if you try to create for example "Dialup" as an alias it says
3009 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3012 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3014 enum lsa_SidType type;
3017 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3020 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3021 * whether the name already exists */
3022 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3023 NULL, NULL, NULL, &type);
3027 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3028 return NT_STATUS_OK;
3031 DEBUG(5, ("trying to create %s, exists as %s\n",
3032 new_name, sid_type_lookup(type)));
3034 if (type == SID_NAME_DOM_GRP) {
3035 return NT_STATUS_GROUP_EXISTS;
3037 if (type == SID_NAME_ALIAS) {
3038 return NT_STATUS_ALIAS_EXISTS;
3041 /* Yes, the default is NT_STATUS_USER_EXISTS */
3042 return NT_STATUS_USER_EXISTS;
3045 /*******************************************************************
3047 ********************************************************************/
3049 NTSTATUS _samr_CreateUser2(pipes_struct *p,
3050 struct samr_CreateUser2 *r)
3052 const char *account = NULL;
3054 POLICY_HND dom_pol = *r->in.domain_handle;
3055 uint32_t acb_info = r->in.acct_flags;
3056 POLICY_HND *user_pol = r->out.user_handle;
3057 struct samr_info *info = NULL;
3062 /* check this, when giving away 'add computer to domain' privs */
3063 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3064 bool can_add_account = False;
3066 DISP_INFO *disp_info = NULL;
3068 /* Get the domain SID stored in the domain policy */
3069 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
3071 return NT_STATUS_INVALID_HANDLE;
3073 nt_status = access_check_samr_function(acc_granted,
3074 SA_RIGHT_DOMAIN_CREATE_USER,
3075 "_samr_CreateUser2");
3076 if (!NT_STATUS_IS_OK(nt_status)) {
3080 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3081 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3082 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3083 this parameter is not an account type */
3084 return NT_STATUS_INVALID_PARAMETER;
3087 account = r->in.account_name->string;
3088 if (account == NULL) {
3089 return NT_STATUS_NO_MEMORY;
3092 nt_status = can_create(p->mem_ctx, account);
3093 if (!NT_STATUS_IS_OK(nt_status)) {
3097 /* determine which user right we need to check based on the acb_info */
3099 if ( acb_info & ACB_WSTRUST )
3101 se_priv_copy( &se_rights, &se_machine_account );
3102 can_add_account = user_has_privileges(
3103 p->pipe_user.nt_user_token, &se_rights );
3105 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3106 account for domain trusts and changes the ACB flags later */
3107 else if ( acb_info & ACB_NORMAL &&
3108 (account[strlen(account)-1] != '$') )
3110 se_priv_copy( &se_rights, &se_add_users );
3111 can_add_account = user_has_privileges(
3112 p->pipe_user.nt_user_token, &se_rights );
3114 else /* implicit assumption of a BDC or domain trust account here
3115 * (we already check the flags earlier) */
3117 if ( lp_enable_privileges() ) {
3118 /* only Domain Admins can add a BDC or domain trust */
3119 se_priv_copy( &se_rights, &se_priv_none );
3120 can_add_account = nt_token_check_domain_rid(
3121 p->pipe_user.nt_user_token,
3122 DOMAIN_GROUP_RID_ADMINS );
3126 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3127 uidtoname(p->pipe_user.ut.uid),
3128 can_add_account ? "True":"False" ));
3130 /********** BEGIN Admin BLOCK **********/
3132 if ( can_add_account )
3135 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3138 if ( can_add_account )
3141 /********** END Admin BLOCK **********/
3143 /* now check for failure */
3145 if ( !NT_STATUS_IS_OK(nt_status) )
3148 /* Get the user's SID */
3150 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3152 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3153 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3154 se_map_generic(&des_access, &usr_generic_mapping);
3156 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3157 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
3158 &acc_granted, "_samr_CreateUser2");
3160 if ( !NT_STATUS_IS_OK(nt_status) ) {
3164 /* associate the user's SID with the new handle. */
3165 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
3166 return NT_STATUS_NO_MEMORY;
3171 info->acc_granted = acc_granted;
3173 /* get a (unique) handle. open a policy on it. */
3174 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
3175 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3178 /* After a "set" ensure we have no cached display info. */
3179 force_flush_samr_cache(info->disp_info);
3181 *r->out.access_granted = acc_granted;
3183 return NT_STATUS_OK;
3186 /*******************************************************************
3188 ********************************************************************/
3190 NTSTATUS _samr_Connect(pipes_struct *p,
3191 struct samr_Connect *r)
3193 struct samr_info *info = NULL;
3194 uint32 des_access = r->in.access_mask;
3198 if (!pipe_access_check(p)) {
3199 DEBUG(3, ("access denied to _samr_Connect\n"));
3200 return NT_STATUS_ACCESS_DENIED;
3203 /* set up the SAMR connect_anon response */
3205 /* associate the user's SID with the new handle. */
3206 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3207 return NT_STATUS_NO_MEMORY;
3209 /* don't give away the farm but this is probably ok. The SA_RIGHT_SAM_ENUM_DOMAINS
3210 was observed from a win98 client trying to enumerate users (when configured
3211 user level access control on shares) --jerry */
3213 if (des_access == MAXIMUM_ALLOWED_ACCESS) {
3214 /* Map to max possible knowing we're filtered below. */
3215 des_access = GENERIC_ALL_ACCESS;
3218 se_map_generic( &des_access, &sam_generic_mapping );
3219 info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
3221 /* get a (unique) handle. open a policy on it. */
3222 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3223 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3225 return NT_STATUS_OK;
3228 /*******************************************************************
3230 ********************************************************************/
3232 NTSTATUS _samr_Connect2(pipes_struct *p,
3233 struct samr_Connect2 *r)
3235 struct samr_info *info = NULL;
3236 SEC_DESC *psd = NULL;
3238 uint32 des_access = r->in.access_mask;
3243 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3247 if (!pipe_access_check(p)) {
3248 DEBUG(3, ("access denied to _samr_Connect2\n"));
3249 return NT_STATUS_ACCESS_DENIED;
3252 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3253 se_map_generic(&des_access, &sam_generic_mapping);
3255 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3256 NULL, 0, des_access, &acc_granted, "_samr_Connect2");
3258 if ( !NT_STATUS_IS_OK(nt_status) )
3261 /* associate the user's SID and access granted with the new handle. */
3262 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3263 return NT_STATUS_NO_MEMORY;
3265 info->acc_granted = acc_granted;
3266 info->status = r->in.access_mask; /* this looks so wrong... - gd */
3268 /* get a (unique) handle. open a policy on it. */
3269 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3270 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3272 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3277 /*******************************************************************
3279 ********************************************************************/
3281 NTSTATUS _samr_Connect4(pipes_struct *p,
3282 struct samr_Connect4 *r)
3284 struct samr_info *info = NULL;
3285 SEC_DESC *psd = NULL;
3287 uint32 des_access = r->in.access_mask;
3292 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3296 if (!pipe_access_check(p)) {
3297 DEBUG(3, ("access denied to samr_Connect4\n"));
3298 return NT_STATUS_ACCESS_DENIED;
3301 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3302 se_map_generic(&des_access, &sam_generic_mapping);
3304 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3305 NULL, 0, des_access, &acc_granted, "_samr_Connect4");
3307 if ( !NT_STATUS_IS_OK(nt_status) )
3310 /* associate the user's SID and access granted with the new handle. */
3311 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3312 return NT_STATUS_NO_MEMORY;
3314 info->acc_granted = acc_granted;
3315 info->status = r->in.access_mask; /* ??? */
3317 /* get a (unique) handle. open a policy on it. */
3318 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3319 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3321 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3323 return NT_STATUS_OK;
3326 /*******************************************************************
3328 ********************************************************************/
3330 NTSTATUS _samr_Connect5(pipes_struct *p,
3331 struct samr_Connect5 *r)
3333 struct samr_info *info = NULL;
3334 SEC_DESC *psd = NULL;
3336 uint32 des_access = r->in.access_mask;
3339 struct samr_ConnectInfo1 info1;
3341 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3345 if (!pipe_access_check(p)) {
3346 DEBUG(3, ("access denied to samr_Connect5\n"));
3347 return NT_STATUS_ACCESS_DENIED;
3350 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3351 se_map_generic(&des_access, &sam_generic_mapping);
3353 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3354 NULL, 0, des_access, &acc_granted, "_samr_Connect5");
3356 if ( !NT_STATUS_IS_OK(nt_status) )
3359 /* associate the user's SID and access granted with the new handle. */
3360 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3361 return NT_STATUS_NO_MEMORY;
3363 info->acc_granted = acc_granted;
3364 info->status = r->in.access_mask; /* ??? */
3366 /* get a (unique) handle. open a policy on it. */
3367 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3368 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3370 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3372 info1.client_version = SAMR_CONNECT_AFTER_W2K;
3375 *r->out.level_out = 1;
3376 r->out.info_out->info1 = info1;
3378 return NT_STATUS_OK;
3381 /**********************************************************************
3383 **********************************************************************/
3385 NTSTATUS _samr_LookupDomain(pipes_struct *p,
3386 struct samr_LookupDomain *r)
3388 NTSTATUS status = NT_STATUS_OK;
3389 struct samr_info *info;
3390 const char *domain_name;
3391 DOM_SID *sid = NULL;
3393 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3394 return NT_STATUS_INVALID_HANDLE;
3396 /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
3397 Reverted that change so we will work with RAS servers again */
3399 status = access_check_samr_function(info->acc_granted,
3400 SA_RIGHT_SAM_OPEN_DOMAIN,
3401 "_samr_LookupDomain");
3402 if (!NT_STATUS_IS_OK(status)) {
3406 domain_name = r->in.domain_name->string;
3408 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
3410 return NT_STATUS_NO_MEMORY;
3413 if (strequal(domain_name, builtin_domain_name())) {
3414 sid_copy(sid, &global_sid_Builtin);
3416 if (!secrets_fetch_domain_sid(domain_name, sid)) {
3417 status = NT_STATUS_NO_SUCH_DOMAIN;
3421 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
3422 sid_string_dbg(sid)));
3429 /**********************************************************************
3431 **********************************************************************/
3433 NTSTATUS _samr_EnumDomains(pipes_struct *p,
3434 struct samr_EnumDomains *r)
3437 struct samr_info *info;
3438 uint32_t num_entries = 2;
3439 struct samr_SamEntry *entry_array = NULL;
3440 struct samr_SamArray *sam;
3442 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3443 return NT_STATUS_INVALID_HANDLE;
3445 status = access_check_samr_function(info->acc_granted,
3446 SA_RIGHT_SAM_ENUM_DOMAINS,
3447 "_samr_EnumDomains");
3448 if (!NT_STATUS_IS_OK(status)) {
3452 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
3454 return NT_STATUS_NO_MEMORY;
3457 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
3458 struct samr_SamEntry,
3461 return NT_STATUS_NO_MEMORY;
3464 entry_array[0].idx = 0;
3465 init_lsa_String(&entry_array[0].name, get_global_sam_name());
3467 entry_array[1].idx = 1;
3468 init_lsa_String(&entry_array[1].name, "Builtin");
3470 sam->count = num_entries;
3471 sam->entries = entry_array;
3474 *r->out.num_entries = num_entries;
3479 /*******************************************************************
3481 ********************************************************************/
3483 NTSTATUS _samr_OpenAlias(pipes_struct *p,
3484 struct samr_OpenAlias *r)
3487 POLICY_HND domain_pol = *r->in.domain_handle;
3488 uint32 alias_rid = r->in.rid;
3489 POLICY_HND *alias_pol = r->out.alias_handle;
3490 struct samr_info *info = NULL;
3491 SEC_DESC *psd = NULL;
3493 uint32 des_access = r->in.access_mask;
3498 /* find the domain policy and get the SID / access bits stored in the domain policy */
3500 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
3501 return NT_STATUS_INVALID_HANDLE;
3503 status = access_check_samr_function(acc_granted,
3504 SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
3507 if ( !NT_STATUS_IS_OK(status) )
3510 /* append the alias' RID to it */
3512 if (!sid_append_rid(&sid, alias_rid))
3513 return NT_STATUS_NO_SUCH_ALIAS;
3515 /*check if access can be granted as requested by client. */
3517 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
3518 se_map_generic(&des_access,&ali_generic_mapping);
3520 se_priv_copy( &se_rights, &se_add_users );
3523 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3524 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
3525 &acc_granted, "_samr_OpenAlias");
3527 if ( !NT_STATUS_IS_OK(status) )
3531 /* Check we actually have the requested alias */
3532 enum lsa_SidType type;
3537 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
3540 if (!result || (type != SID_NAME_ALIAS)) {
3541 return NT_STATUS_NO_SUCH_ALIAS;
3544 /* make sure there is a mapping */
3546 if ( !sid_to_gid( &sid, &gid ) ) {
3547 return NT_STATUS_NO_SUCH_ALIAS;
3552 /* associate the alias SID with the new handle. */
3553 if ((info = get_samr_info_by_sid(&sid)) == NULL)
3554 return NT_STATUS_NO_MEMORY;
3556 info->acc_granted = acc_granted;
3558 /* get a (unique) handle. open a policy on it. */
3559 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
3560 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3562 return NT_STATUS_OK;
3565 /*******************************************************************
3567 ********************************************************************/
3569 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
3570 struct samr_UserInfo7 *id7,
3576 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3578 return NT_STATUS_ACCESS_DENIED;
3581 if (!id7->account_name.string) {
3582 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3584 return NT_STATUS_ACCESS_DENIED;
3587 /* check to see if the new username already exists. Note: we can't
3588 reliably lock all backends, so there is potentially the
3589 possibility that a user can be created in between this check and
3590 the rename. The rename should fail, but may not get the
3591 exact same failure status code. I think this is small enough
3592 of a window for this type of operation and the results are
3593 simply that the rename fails with a slightly different status
3594 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3596 rc = can_create(mem_ctx, id7->account_name.string);
3597 if (!NT_STATUS_IS_OK(rc)) {
3601 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
3607 /*******************************************************************
3609 ********************************************************************/
3611 static bool set_user_info_16(struct samr_UserInfo16 *id16,
3615 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3620 /* FIX ME: check if the value is really changed --metze */
3621 if (!pdb_set_acct_ctrl(pwd, id16->acct_flags, PDB_CHANGED)) {
3626 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3636 /*******************************************************************
3638 ********************************************************************/
3640 static bool set_user_info_18(struct samr_UserInfo18 *id18,
3644 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3649 if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd.hash, PDB_CHANGED)) {
3653 if (!pdb_set_nt_passwd (pwd, id18->nt_pwd.hash, PDB_CHANGED)) {
3657 if (!pdb_set_pass_last_set_time (pwd, time(NULL), PDB_CHANGED)) {
3662 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3671 /*******************************************************************
3673 ********************************************************************/
3675 static bool set_user_info_20(struct samr_UserInfo20 *id20,
3679 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3683 copy_id20_to_sam_passwd(pwd, id20);
3685 /* write the change out */
3686 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3696 /*******************************************************************
3698 ********************************************************************/
3700 static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
3701 struct samr_UserInfo21 *id21,
3707 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3708 return NT_STATUS_INVALID_PARAMETER;
3711 /* we need to separately check for an account rename first */
3713 if (id21->account_name.string &&
3714 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
3717 /* check to see if the new username already exists. Note: we can't
3718 reliably lock all backends, so there is potentially the
3719 possibility that a user can be created in between this check and
3720 the rename. The rename should fail, but may not get the
3721 exact same failure status code. I think this is small enough
3722 of a window for this type of operation and the results are
3723 simply that the rename fails with a slightly different status
3724 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3726 status = can_create(mem_ctx, id21->account_name.string);
3727 if (!NT_STATUS_IS_OK(status)) {
3731 status = pdb_rename_sam_account(pwd, id21->account_name.string);
3733 if (!NT_STATUS_IS_OK(status)) {
3734 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3735 nt_errstr(status)));
3740 /* set the new username so that later
3741 functions can work on the new account */
3742 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
3745 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
3748 * The funny part about the previous two calls is
3749 * that pwd still has the password hashes from the
3750 * passdb entry. These have not been updated from
3751 * id21. I don't know if they need to be set. --jerry
3754 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3755 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3756 if ( !NT_STATUS_IS_OK(status) ) {
3761 /* Don't worry about writing out the user account since the
3762 primary group SID is generated solely from the user's Unix
3765 /* write the change out */
3766 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3773 return NT_STATUS_OK;
3776 /*******************************************************************
3778 ********************************************************************/
3780 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
3781 struct samr_UserInfo23 *id23,
3784 char *plaintext_buf = NULL;
3790 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3791 return NT_STATUS_INVALID_PARAMETER;
3794 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3795 pdb_get_username(pwd)));
3797 acct_ctrl = pdb_get_acct_ctrl(pwd);
3799 if (!decode_pw_buffer(mem_ctx,
3800 id23->password.data,
3805 return NT_STATUS_INVALID_PARAMETER;
3808 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3810 return NT_STATUS_ACCESS_DENIED;
3813 copy_id23_to_sam_passwd(pwd, id23);
3815 /* if it's a trust account, don't update /etc/passwd */
3816 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3817 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3818 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3819 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
3821 /* update the UNIX password */
3822 if (lp_unix_password_sync() ) {
3823 struct passwd *passwd;
3824 if (pdb_get_username(pwd) == NULL) {
3825 DEBUG(1, ("chgpasswd: User without name???\n"));
3827 return NT_STATUS_ACCESS_DENIED;
3830 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
3831 if (passwd == NULL) {
3832 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3835 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3837 return NT_STATUS_ACCESS_DENIED;
3839 TALLOC_FREE(passwd);
3843 memset(plaintext_buf, '\0', strlen(plaintext_buf));
3845 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
3846 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
3852 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3859 return NT_STATUS_OK;
3862 /*******************************************************************
3864 ********************************************************************/
3866 static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
3870 char *plaintext_buf = NULL;
3872 time_t last_set_time;
3873 enum pdb_value_state last_set_state;
3875 DEBUG(5, ("Attempting administrator password change for user %s\n",
3876 pdb_get_username(pwd)));
3878 acct_ctrl = pdb_get_acct_ctrl(pwd);
3879 /* we need to know if it's expired, because this is an admin change, not a
3880 user change, so it's still expired when we're done */
3881 last_set_state = pdb_get_init_flags(pwd, PDB_PASSLASTSET);
3882 last_set_time = pdb_get_pass_last_set_time(pwd);
3884 if (!decode_pw_buffer(talloc_tos(),
3893 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3898 /* if it's a trust account, don't update /etc/passwd */
3899 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3900 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3901 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3902 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3904 /* update the UNIX password */
3905 if (lp_unix_password_sync()) {
3906 struct passwd *passwd;
3908 if (pdb_get_username(pwd) == NULL) {
3909 DEBUG(1, ("chgpasswd: User without name???\n"));
3914 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
3915 if (passwd == NULL) {
3916 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3919 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3923 TALLOC_FREE(passwd);
3927 memset(plaintext_buf, '\0', strlen(plaintext_buf));
3930 * A level 25 change does reset the pwdlastset field, a level 24
3931 * change does not. I know this is probably not the full story, but
3932 * it is needed to make XP join LDAP correctly, without it the later
3933 * auth2 check can fail with PWD_MUST_CHANGE.
3937 * restore last set time as this is an admin change, not a
3940 pdb_set_pass_last_set_time (pwd, last_set_time,
3944 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
3946 /* update the SAMBA password */
3947 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3957 /*******************************************************************
3959 ********************************************************************/
3961 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
3962 struct samr_UserInfo25 *id25,
3968 DEBUG(5, ("set_user_info_25: NULL id25\n"));
3969 return NT_STATUS_INVALID_PARAMETER;
3972 copy_id25_to_sam_passwd(pwd, id25);
3974 /* write the change out */
3975 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3981 * We need to "pdb_update_sam_account" before the unix primary group
3982 * is set, because the idealx scripts would also change the
3983 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
3984 * the delete explicit / add explicit, which would then fail to find
3985 * the previous primaryGroupSid value.
3988 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3989 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3990 if ( !NT_STATUS_IS_OK(status) ) {
3995 /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
3998 return NT_STATUS_OK;
4001 /*******************************************************************
4002 samr_SetUserInfo_internal
4003 ********************************************************************/
4005 static NTSTATUS samr_SetUserInfo_internal(const char *fn_name,
4007 struct policy_handle *user_handle,
4009 union samr_UserInfo *info)
4012 struct samu *pwd = NULL;
4014 POLICY_HND *pol = user_handle;
4015 uint16_t switch_value = level;
4016 uint32_t acc_granted;
4017 uint32_t acc_required;
4019 bool has_enough_rights = False;
4021 DISP_INFO *disp_info = NULL;
4023 DEBUG(5,("%s: %d\n", fn_name, __LINE__));
4025 /* find the policy handle. open a policy on it. */
4026 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info)) {
4027 return NT_STATUS_INVALID_HANDLE;
4030 /* This is tricky. A WinXP domain join sets
4031 (SA_RIGHT_USER_SET_PASSWORD|SA_RIGHT_USER_SET_ATTRIBUTES|SA_RIGHT_USER_ACCT_FLAGS_EXPIRY)
4032 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
4033 standard Win32 API calls just ask for SA_RIGHT_USER_SET_PASSWORD in the SamrOpenUser().
4034 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
4035 we'll use the set from the WinXP join as the basis. */
4037 switch (switch_value) {
4042 acc_required = SA_RIGHT_USER_SET_PASSWORD;
4045 acc_required = SA_RIGHT_USER_SET_PASSWORD |
4046 SA_RIGHT_USER_SET_ATTRIBUTES |
4047 SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
4051 status = access_check_samr_function(acc_granted,
4054 if (!NT_STATUS_IS_OK(status)) {
4058 DEBUG(5, ("%s: sid:%s, level:%d\n",
4059 fn_name, sid_string_dbg(&sid), switch_value));
4062 DEBUG(5, ("%s: NULL info level\n", fn_name));
4063 return NT_STATUS_INVALID_INFO_CLASS;
4066 if (!(pwd = samu_new(NULL))) {
4067 return NT_STATUS_NO_MEMORY;
4071 ret = pdb_getsampwsid(pwd, &sid);
4076 return NT_STATUS_NO_SUCH_USER;
4079 /* deal with machine password changes differently from userinfo changes */
4080 /* check to see if we have the sufficient rights */
4082 acb_info = pdb_get_acct_ctrl(pwd);
4083 if (acb_info & ACB_WSTRUST)
4084 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4085 &se_machine_account);
4086 else if (acb_info & ACB_NORMAL)
4087 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4089 else if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
4090 if (lp_enable_privileges()) {
4091 has_enough_rights = nt_token_check_domain_rid(p->pipe_user.nt_user_token,
4092 DOMAIN_GROUP_RID_ADMINS);
4096 DEBUG(5, ("%s: %s does%s possess sufficient rights\n",
4098 uidtoname(p->pipe_user.ut.uid),
4099 has_enough_rights ? "" : " not"));
4101 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
4103 if (has_enough_rights) {
4107 /* ok! user info levels (lots: see MSDEV help), off we go... */
4109 switch (switch_value) {
4112 status = set_user_info_7(p->mem_ctx,
4117 if (!set_user_info_16(&info->info16, pwd)) {
4118 status = NT_STATUS_ACCESS_DENIED;
4123 /* Used by AS/U JRA. */
4124 if (!set_user_info_18(&info->info18, pwd)) {
4125 status = NT_STATUS_ACCESS_DENIED;
4130 if (!set_user_info_20(&info->info20, pwd)) {
4131 status = NT_STATUS_ACCESS_DENIED;
4136 status = set_user_info_21(p->mem_ctx,
4137 &info->info21, pwd);
4141 if (!p->session_key.length) {
4142 status = NT_STATUS_NO_USER_SESSION_KEY;
4144 SamOEMhashBlob(info->info23.password.data, 516,
4147 dump_data(100, info->info23.password.data, 516);
4149 status = set_user_info_23(p->mem_ctx,
4150 &info->info23, pwd);
4154 if (!p->session_key.length) {
4155 status = NT_STATUS_NO_USER_SESSION_KEY;
4157 SamOEMhashBlob(info->info24.password.data,
4161 dump_data(100, info->info24.password.data, 516);
4163 if (!set_user_info_pw(info->info24.password.data, pwd,
4165 status = NT_STATUS_ACCESS_DENIED;
4170 if (!p->session_key.length) {
4171 status = NT_STATUS_NO_USER_SESSION_KEY;
4173 encode_or_decode_arc4_passwd_buffer(info->info25.password.data,
4176 dump_data(100, info->info25.password.data, 532);
4178 status = set_user_info_25(p->mem_ctx,
4179 &info->info25, pwd);
4180 if (!NT_STATUS_IS_OK(status)) {
4183 if (!set_user_info_pw(info->info25.password.data, pwd,
4185 status = NT_STATUS_ACCESS_DENIED;
4190 if (!p->session_key.length) {
4191 status = NT_STATUS_NO_USER_SESSION_KEY;
4193 encode_or_decode_arc4_passwd_buffer(info->info26.password.data,
4196 dump_data(100, info->info26.password.data, 516);
4198 if (!set_user_info_pw(info->info26.password.data, pwd,
4200 status = NT_STATUS_ACCESS_DENIED;
4205 status = NT_STATUS_INVALID_INFO_CLASS;
4210 if (has_enough_rights) {
4214 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
4216 if (NT_STATUS_IS_OK(status)) {
4217 force_flush_samr_cache(disp_info);
4223 /*******************************************************************
4225 ********************************************************************/
4227 NTSTATUS _samr_SetUserInfo(pipes_struct *p,
4228 struct samr_SetUserInfo *r)
4230 return samr_SetUserInfo_internal("_samr_SetUserInfo",
4237 /*******************************************************************
4239 ********************************************************************/
4241 NTSTATUS _samr_SetUserInfo2(pipes_struct *p,
4242 struct samr_SetUserInfo2 *r)
4244 return samr_SetUserInfo_internal("_samr_SetUserInfo2",
4251 /*********************************************************************
4252 _samr_GetAliasMembership
4253 *********************************************************************/
4255 NTSTATUS _samr_GetAliasMembership(pipes_struct *p,
4256 struct samr_GetAliasMembership *r)
4258 size_t num_alias_rids;
4260 struct samr_info *info = NULL;
4268 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
4270 /* find the policy handle. open a policy on it. */
4271 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
4272 return NT_STATUS_INVALID_HANDLE;
4274 ntstatus1 = access_check_samr_function(info->acc_granted,
4275 SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM,
4276 "_samr_GetAliasMembership");
4277 ntstatus2 = access_check_samr_function(info->acc_granted,
4278 SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
4279 "_samr_GetAliasMembership");
4281 if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
4282 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
4283 !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
4284 return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
4288 if (!sid_check_is_domain(&info->sid) &&
4289 !sid_check_is_builtin(&info->sid))
4290 return NT_STATUS_OBJECT_TYPE_MISMATCH;
4292 if (r->in.sids->num_sids) {
4293 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, r->in.sids->num_sids);
4295 if (members == NULL)
4296 return NT_STATUS_NO_MEMORY;
4301 for (i=0; i<r->in.sids->num_sids; i++)
4302 sid_copy(&members[i], r->in.sids->sids[i].sid);
4308 ntstatus1 = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
4309 r->in.sids->num_sids,
4310 &alias_rids, &num_alias_rids);
4313 if (!NT_STATUS_IS_OK(ntstatus1)) {
4317 r->out.rids->count = num_alias_rids;
4318 r->out.rids->ids = alias_rids;
4320 return NT_STATUS_OK;
4323 /*********************************************************************
4324 _samr_GetMembersInAlias
4325 *********************************************************************/
4327 NTSTATUS _samr_GetMembersInAlias(pipes_struct *p,
4328 struct samr_GetMembersInAlias *r)
4332 size_t num_sids = 0;
4333 struct lsa_SidPtr *sids = NULL;
4334 DOM_SID *pdb_sids = NULL;
4340 /* find the policy handle. open a policy on it. */
4341 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, NULL))
4342 return NT_STATUS_INVALID_HANDLE;
4344 status = access_check_samr_function(acc_granted,
4345 SA_RIGHT_ALIAS_GET_MEMBERS,
4346 "_samr_GetMembersInAlias");
4347 if (!NT_STATUS_IS_OK(status)) {
4351 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4354 status = pdb_enum_aliasmem(&alias_sid, &pdb_sids, &num_sids);
4357 if (!NT_STATUS_IS_OK(status)) {
4362 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
4364 TALLOC_FREE(pdb_sids);
4365 return NT_STATUS_NO_MEMORY;
4369 for (i = 0; i < num_sids; i++) {
4370 sids[i].sid = sid_dup_talloc(p->mem_ctx, &pdb_sids[i]);
4372 TALLOC_FREE(pdb_sids);
4373 return NT_STATUS_NO_MEMORY;
4377 r->out.sids->num_sids = num_sids;
4378 r->out.sids->sids = sids;
4380 TALLOC_FREE(pdb_sids);
4382 return NT_STATUS_OK;
4385 /*********************************************************************
4386 _samr_QueryGroupMember
4387 *********************************************************************/
4389 NTSTATUS _samr_QueryGroupMember(pipes_struct *p,
4390 struct samr_QueryGroupMember *r)
4393 size_t i, num_members;
4401 struct samr_RidTypeArray *rids = NULL;
4403 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidTypeArray);
4405 return NT_STATUS_NO_MEMORY;
4408 /* find the policy handle. open a policy on it. */
4409 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, NULL))
4410 return NT_STATUS_INVALID_HANDLE;
4412 status = access_check_samr_function(acc_granted,
4413 SA_RIGHT_GROUP_GET_MEMBERS,
4414 "_samr_QueryGroupMember");
4415 if (!NT_STATUS_IS_OK(status)) {
4419 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4421 if (!sid_check_is_in_our_domain(&group_sid)) {
4422 DEBUG(3, ("sid %s is not in our domain\n",
4423 sid_string_dbg(&group_sid)));
4424 return NT_STATUS_NO_SUCH_GROUP;
4427 DEBUG(10, ("lookup on Domain SID\n"));
4430 status = pdb_enum_group_members(p->mem_ctx, &group_sid,
4431 &rid, &num_members);
4434 if (!NT_STATUS_IS_OK(status))
4438 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
4440 return NT_STATUS_NO_MEMORY;
4446 for (i=0; i<num_members; i++)
4447 attr[i] = SID_NAME_USER;
4449 rids->count = num_members;
4453 *r->out.rids = rids;
4455 return NT_STATUS_OK;
4458 /*********************************************************************
4459 _samr_AddAliasMember
4460 *********************************************************************/
4462 NTSTATUS _samr_AddAliasMember(pipes_struct *p,
4463 struct samr_AddAliasMember *r)
4468 bool can_add_accounts;
4470 DISP_INFO *disp_info = NULL;
4472 /* Find the policy handle. Open a policy on it. */
4473 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4474 return NT_STATUS_INVALID_HANDLE;
4476 status = access_check_samr_function(acc_granted,
4477 SA_RIGHT_ALIAS_ADD_MEMBER,
4478 "_samr_AddAliasMember");
4479 if (!NT_STATUS_IS_OK(status)) {
4483 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4485 se_priv_copy( &se_rights, &se_add_users );
4486 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4488 /******** BEGIN SeAddUsers BLOCK *********/
4490 if ( can_add_accounts )
4493 status = pdb_add_aliasmem(&alias_sid, r->in.sid);
4495 if ( can_add_accounts )
4498 /******** END SeAddUsers BLOCK *********/
4500 if (NT_STATUS_IS_OK(status)) {
4501 force_flush_samr_cache(disp_info);
4507 /*********************************************************************
4508 _samr_DeleteAliasMember
4509 *********************************************************************/
4511 NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
4512 struct samr_DeleteAliasMember *r)
4517 bool can_add_accounts;
4519 DISP_INFO *disp_info = NULL;
4521 /* Find the policy handle. Open a policy on it. */
4522 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4523 return NT_STATUS_INVALID_HANDLE;
4525 status = access_check_samr_function(acc_granted,
4526 SA_RIGHT_ALIAS_REMOVE_MEMBER,
4527 "_samr_DeleteAliasMember");
4528 if (!NT_STATUS_IS_OK(status)) {
4532 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
4533 sid_string_dbg(&alias_sid)));
4535 se_priv_copy( &se_rights, &se_add_users );
4536 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4538 /******** BEGIN SeAddUsers BLOCK *********/
4540 if ( can_add_accounts )
4543 status = pdb_del_aliasmem(&alias_sid, r->in.sid);
4545 if ( can_add_accounts )
4548 /******** END SeAddUsers BLOCK *********/
4550 if (NT_STATUS_IS_OK(status)) {
4551 force_flush_samr_cache(disp_info);
4557 /*********************************************************************
4558 _samr_AddGroupMember
4559 *********************************************************************/
4561 NTSTATUS _samr_AddGroupMember(pipes_struct *p,
4562 struct samr_AddGroupMember *r)
4569 bool can_add_accounts;
4570 DISP_INFO *disp_info = NULL;
4572 /* Find the policy handle. Open a policy on it. */
4573 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4574 return NT_STATUS_INVALID_HANDLE;
4576 status = access_check_samr_function(acc_granted,
4577 SA_RIGHT_GROUP_ADD_MEMBER,
4578 "_samr_AddGroupMember");
4579 if (!NT_STATUS_IS_OK(status)) {
4583 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4585 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4587 return NT_STATUS_INVALID_HANDLE;
4590 se_priv_copy( &se_rights, &se_add_users );
4591 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4593 /******** BEGIN SeAddUsers BLOCK *********/
4595 if ( can_add_accounts )
4598 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
4600 if ( can_add_accounts )
4603 /******** END SeAddUsers BLOCK *********/
4605 force_flush_samr_cache(disp_info);
4610 /*********************************************************************
4611 _samr_DeleteGroupMember
4612 *********************************************************************/
4614 NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
4615 struct samr_DeleteGroupMember *r)
4623 bool can_add_accounts;
4624 DISP_INFO *disp_info = NULL;
4627 * delete the group member named r->in.rid
4628 * who is a member of the sid associated with the handle
4629 * the rid is a user's rid as the group is a domain group.
4632 /* Find the policy handle. Open a policy on it. */
4633 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4634 return NT_STATUS_INVALID_HANDLE;
4636 status = access_check_samr_function(acc_granted,
4637 SA_RIGHT_GROUP_REMOVE_MEMBER,
4638 "_samr_DeleteGroupMember");
4639 if (!NT_STATUS_IS_OK(status)) {
4643 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4645 return NT_STATUS_INVALID_HANDLE;
4648 se_priv_copy( &se_rights, &se_add_users );
4649 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4651 /******** BEGIN SeAddUsers BLOCK *********/
4653 if ( can_add_accounts )
4656 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
4658 if ( can_add_accounts )
4661 /******** END SeAddUsers BLOCK *********/
4663 force_flush_samr_cache(disp_info);
4668 /*********************************************************************
4670 *********************************************************************/
4672 NTSTATUS _samr_DeleteUser(pipes_struct *p,
4673 struct samr_DeleteUser *r)
4677 struct samu *sam_pass=NULL;
4679 bool can_add_accounts;
4681 DISP_INFO *disp_info = NULL;
4684 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
4686 /* Find the policy handle. Open a policy on it. */
4687 if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &user_sid, &acc_granted, &disp_info))
4688 return NT_STATUS_INVALID_HANDLE;
4690 status = access_check_samr_function(acc_granted,
4691 STD_RIGHT_DELETE_ACCESS,
4692 "_samr_DeleteUser");
4693 if (!NT_STATUS_IS_OK(status)) {
4697 if (!sid_check_is_in_our_domain(&user_sid))
4698 return NT_STATUS_CANNOT_DELETE;
4700 /* check if the user exists before trying to delete */
4701 if ( !(sam_pass = samu_new( NULL )) ) {
4702 return NT_STATUS_NO_MEMORY;
4706 ret = pdb_getsampwsid(sam_pass, &user_sid);
4710 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
4711 sid_string_dbg(&user_sid)));
4712 TALLOC_FREE(sam_pass);
4713 return NT_STATUS_NO_SUCH_USER;
4716 acb_info = pdb_get_acct_ctrl(sam_pass);
4718 /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
4719 if ( acb_info & ACB_WSTRUST ) {
4720 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account );
4722 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4725 /******** BEGIN SeAddUsers BLOCK *********/
4727 if ( can_add_accounts )
4730 status = pdb_delete_user(p->mem_ctx, sam_pass);
4732 if ( can_add_accounts )
4735 /******** END SeAddUsers BLOCK *********/
4737 if ( !NT_STATUS_IS_OK(status) ) {
4738 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
4739 "user %s: %s.\n", pdb_get_username(sam_pass),
4740 nt_errstr(status)));
4741 TALLOC_FREE(sam_pass);
4746 TALLOC_FREE(sam_pass);
4748 if (!close_policy_hnd(p, r->in.user_handle))
4749 return NT_STATUS_OBJECT_NAME_INVALID;
4751 force_flush_samr_cache(disp_info);
4753 return NT_STATUS_OK;
4756 /*********************************************************************
4757 _samr_DeleteDomainGroup
4758 *********************************************************************/
4760 NTSTATUS _samr_DeleteDomainGroup(pipes_struct *p,
4761 struct samr_DeleteDomainGroup *r)
4768 bool can_add_accounts;
4769 DISP_INFO *disp_info = NULL;
4771 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
4773 /* Find the policy handle. Open a policy on it. */
4774 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4775 return NT_STATUS_INVALID_HANDLE;
4777 status = access_check_samr_function(acc_granted,
4778 STD_RIGHT_DELETE_ACCESS,
4779 "_samr_DeleteDomainGroup");
4780 if (!NT_STATUS_IS_OK(status)) {
4784 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4786 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4788 return NT_STATUS_NO_SUCH_GROUP;
4791 se_priv_copy( &se_rights, &se_add_users );
4792 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4794 /******** BEGIN SeAddUsers BLOCK *********/
4796 if ( can_add_accounts )
4799 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
4801 if ( can_add_accounts )
4804 /******** END SeAddUsers BLOCK *********/
4806 if ( !NT_STATUS_IS_OK(status) ) {
4807 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
4808 "entry for group %s: %s\n",
4809 sid_string_dbg(&group_sid),
4810 nt_errstr(status)));
4814 if (!close_policy_hnd(p, r->in.group_handle))
4815 return NT_STATUS_OBJECT_NAME_INVALID;
4817 force_flush_samr_cache(disp_info);
4819 return NT_STATUS_OK;
4822 /*********************************************************************
4823 _samr_DeleteDomAlias
4824 *********************************************************************/
4826 NTSTATUS _samr_DeleteDomAlias(pipes_struct *p,
4827 struct samr_DeleteDomAlias *r)
4832 bool can_add_accounts;
4834 DISP_INFO *disp_info = NULL;
4836 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
4838 /* Find the policy handle. Open a policy on it. */
4839 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4840 return NT_STATUS_INVALID_HANDLE;
4842 /* copy the handle to the outgoing reply */
4844 memcpy(r->out.alias_handle, r->in.alias_handle, sizeof(r->out.alias_handle));
4846 status = access_check_samr_function(acc_granted,
4847 STD_RIGHT_DELETE_ACCESS,
4848 "_samr_DeleteDomAlias");
4849 if (!NT_STATUS_IS_OK(status)) {
4853 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4855 /* Don't let Windows delete builtin groups */
4857 if ( sid_check_is_in_builtin( &alias_sid ) ) {
4858 return NT_STATUS_SPECIAL_ACCOUNT;
4861 if (!sid_check_is_in_our_domain(&alias_sid))
4862 return NT_STATUS_NO_SUCH_ALIAS;
4864 DEBUG(10, ("lookup on Local SID\n"));
4866 se_priv_copy( &se_rights, &se_add_users );
4867 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4869 /******** BEGIN SeAddUsers BLOCK *********/
4871 if ( can_add_accounts )
4874 /* Have passdb delete the alias */
4875 status = pdb_delete_alias(&alias_sid);
4877 if ( can_add_accounts )
4880 /******** END SeAddUsers BLOCK *********/
4882 if ( !NT_STATUS_IS_OK(status))
4885 if (!close_policy_hnd(p, r->in.alias_handle))
4886 return NT_STATUS_OBJECT_NAME_INVALID;
4888 force_flush_samr_cache(disp_info);
4890 return NT_STATUS_OK;
4893 /*********************************************************************
4894 _samr_CreateDomainGroup
4895 *********************************************************************/
4897 NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
4898 struct samr_CreateDomainGroup *r)
4905 struct samr_info *info;
4908 bool can_add_accounts;
4909 DISP_INFO *disp_info = NULL;
4911 /* Find the policy handle. Open a policy on it. */
4912 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &dom_sid, &acc_granted, &disp_info))
4913 return NT_STATUS_INVALID_HANDLE;
4915 status = access_check_samr_function(acc_granted,
4916 SA_RIGHT_DOMAIN_CREATE_GROUP,
4917 "_samr_CreateDomainGroup");
4918 if (!NT_STATUS_IS_OK(status)) {
4922 if (!sid_equal(&dom_sid, get_global_sam_sid()))
4923 return NT_STATUS_ACCESS_DENIED;
4925 name = r->in.name->string;
4927 return NT_STATUS_NO_MEMORY;
4930 status = can_create(p->mem_ctx, name);
4931 if (!NT_STATUS_IS_OK(status)) {
4935 se_priv_copy( &se_rights, &se_add_users );
4936 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4938 /******** BEGIN SeAddUsers BLOCK *********/
4940 if ( can_add_accounts )
4943 /* check that we successfully create the UNIX group */
4945 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
4947 if ( can_add_accounts )
4950 /******** END SeAddUsers BLOCK *********/
4952 /* check if we should bail out here */
4954 if ( !NT_STATUS_IS_OK(status) )
4957 sid_compose(&info_sid, get_global_sam_sid(), *r->out.rid);
4959 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4960 return NT_STATUS_NO_MEMORY;
4962 /* they created it; let the user do what he wants with it */
4964 info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
4966 /* get a (unique) handle. open a policy on it. */
4967 if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
4968 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4970 force_flush_samr_cache(disp_info);
4972 return NT_STATUS_OK;
4975 /*********************************************************************
4976 _samr_CreateDomAlias
4977 *********************************************************************/
4979 NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
4980 struct samr_CreateDomAlias *r)
4984 const char *name = NULL;
4985 struct samr_info *info;
4990 bool can_add_accounts;
4991 DISP_INFO *disp_info = NULL;
4993 /* Find the policy handle. Open a policy on it. */
4994 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &dom_sid, &acc_granted, &disp_info))
4995 return NT_STATUS_INVALID_HANDLE;
4997 result = access_check_samr_function(acc_granted,
4998 SA_RIGHT_DOMAIN_CREATE_ALIAS,
4999 "_samr_CreateDomAlias");
5000 if (!NT_STATUS_IS_OK(result)) {
5004 if (!sid_equal(&dom_sid, get_global_sam_sid()))
5005 return NT_STATUS_ACCESS_DENIED;
5007 name = r->in.alias_name->string;
5009 se_priv_copy( &se_rights, &se_add_users );
5010 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
5012 result = can_create(p->mem_ctx, name);
5013 if (!NT_STATUS_IS_OK(result)) {
5017 /******** BEGIN SeAddUsers BLOCK *********/
5019 if ( can_add_accounts )
5022 /* Have passdb create the alias */
5023 result = pdb_create_alias(name, r->out.rid);
5025 if ( can_add_accounts )
5028 /******** END SeAddUsers BLOCK *********/
5030 if (!NT_STATUS_IS_OK(result)) {
5031 DEBUG(10, ("pdb_create_alias failed: %s\n",
5032 nt_errstr(result)));
5036 sid_copy(&info_sid, get_global_sam_sid());
5037 sid_append_rid(&info_sid, *r->out.rid);
5039 if (!sid_to_gid(&info_sid, &gid)) {
5040 DEBUG(10, ("Could not find alias just created\n"));
5041 return NT_STATUS_ACCESS_DENIED;
5044 /* check if the group has been successfully created */
5045 if ( getgrgid(gid) == NULL ) {
5046 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
5048 return NT_STATUS_ACCESS_DENIED;
5051 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5052 return NT_STATUS_NO_MEMORY;
5054 /* they created it; let the user do what he wants with it */
5056 info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
5058 /* get a (unique) handle. open a policy on it. */
5059 if (!create_policy_hnd(p, r->out.alias_handle, free_samr_info, (void *)info))
5060 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5062 force_flush_samr_cache(disp_info);
5064 return NT_STATUS_OK;
5067 /*********************************************************************
5068 _samr_QueryGroupInfo
5069 *********************************************************************/
5071 NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
5072 struct samr_QueryGroupInfo *r)
5077 union samr_GroupInfo *info = NULL;
5080 uint32_t attributes = SE_GROUP_MANDATORY |
5081 SE_GROUP_ENABLED_BY_DEFAULT |
5083 const char *group_name = NULL;
5084 const char *group_description = NULL;
5086 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, NULL))
5087 return NT_STATUS_INVALID_HANDLE;
5089 status = access_check_samr_function(acc_granted,
5090 SA_RIGHT_GROUP_LOOKUP_INFO,
5091 "_samr_QueryGroupInfo");
5092 if (!NT_STATUS_IS_OK(status)) {
5097 ret = get_domain_group_from_sid(group_sid, &map);
5100 return NT_STATUS_INVALID_HANDLE;
5102 /* FIXME: map contains fstrings */
5103 group_name = talloc_strdup(r, map.nt_name);
5104 group_description = talloc_strdup(r, map.comment);
5106 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
5108 return NT_STATUS_NO_MEMORY;
5111 switch (r->in.level) {
5117 status = pdb_enum_group_members(
5118 p->mem_ctx, &group_sid, &members, &num_members);
5121 if (!NT_STATUS_IS_OK(status)) {
5125 init_samr_group_info1(&info->all,
5133 init_samr_group_info2(&info->name,
5137 init_samr_group_info3(&info->attributes,
5141 init_samr_group_info4(&info->description,
5152 status = pdb_enum_group_members(
5153 p->mem_ctx, &group_sid, &members, &num_members);
5156 if (!NT_STATUS_IS_OK(status)) {
5160 init_samr_group_info5(&info->all2,
5163 0, /* num_members - in w2k3 this is always 0 */
5169 return NT_STATUS_INVALID_INFO_CLASS;
5172 *r->out.info = info;
5174 return NT_STATUS_OK;
5177 /*********************************************************************
5179 *********************************************************************/
5181 NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
5182 struct samr_SetGroupInfo *r)
5189 bool can_mod_accounts;
5190 DISP_INFO *disp_info = NULL;
5192 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
5193 return NT_STATUS_INVALID_HANDLE;
5195 status = access_check_samr_function(acc_granted,
5196 SA_RIGHT_GROUP_SET_INFO,
5197 "_samr_SetGroupInfo");
5198 if (!NT_STATUS_IS_OK(status)) {
5203 ret = get_domain_group_from_sid(group_sid, &map);
5206 return NT_STATUS_NO_SUCH_GROUP;
5208 switch (r->in.level) {
5210 fstrcpy(map.comment, r->in.info->all.description.string);
5213 fstrcpy(map.comment, r->in.info->description.string);
5216 return NT_STATUS_INVALID_INFO_CLASS;
5219 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
5221 /******** BEGIN SeAddUsers BLOCK *********/
5223 if ( can_mod_accounts )
5226 status = pdb_update_group_mapping_entry(&map);
5228 if ( can_mod_accounts )
5231 /******** End SeAddUsers BLOCK *********/
5233 if (NT_STATUS_IS_OK(status)) {
5234 force_flush_samr_cache(disp_info);
5240 /*********************************************************************
5242 *********************************************************************/
5244 NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
5245 struct samr_SetAliasInfo *r)
5248 struct acct_info info;
5250 bool can_mod_accounts;
5252 DISP_INFO *disp_info = NULL;
5254 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &group_sid, &acc_granted, &disp_info))
5255 return NT_STATUS_INVALID_HANDLE;
5257 status = access_check_samr_function(acc_granted,
5258 SA_RIGHT_ALIAS_SET_INFO,
5259 "_samr_SetAliasInfo");
5260 if (!NT_STATUS_IS_OK(status)) {
5264 /* get the current group information */
5267 status = pdb_get_aliasinfo( &group_sid, &info );
5270 if ( !NT_STATUS_IS_OK(status))
5273 switch (r->in.level) {
5278 /* We currently do not support renaming groups in the
5279 the BUILTIN domain. Refer to util_builtin.c to understand
5280 why. The eventually needs to be fixed to be like Windows
5281 where you can rename builtin groups, just not delete them */
5283 if ( sid_check_is_in_builtin( &group_sid ) ) {
5284 return NT_STATUS_SPECIAL_ACCOUNT;
5287 /* There has to be a valid name (and it has to be different) */
5289 if ( !r->in.info->name.string )
5290 return NT_STATUS_INVALID_PARAMETER;
5292 /* If the name is the same just reply "ok". Yes this
5293 doesn't allow you to change the case of a group name. */
5295 if ( strequal( r->in.info->name.string, info.acct_name ) )
5296 return NT_STATUS_OK;
5298 fstrcpy( info.acct_name, r->in.info->name.string);
5300 /* make sure the name doesn't already exist as a user
5303 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
5304 status = can_create( p->mem_ctx, group_name );
5305 if ( !NT_STATUS_IS_OK( status ) )
5309 case ALIASINFODESCRIPTION:
5310 if (r->in.info->description.string) {
5311 fstrcpy(info.acct_desc,
5312 r->in.info->description.string);
5314 fstrcpy( info.acct_desc, "" );
5318 return NT_STATUS_INVALID_INFO_CLASS;
5321 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
5323 /******** BEGIN SeAddUsers BLOCK *********/
5325 if ( can_mod_accounts )
5328 status = pdb_set_aliasinfo( &group_sid, &info );
5330 if ( can_mod_accounts )
5333 /******** End SeAddUsers BLOCK *********/
5335 if (NT_STATUS_IS_OK(status))
5336 force_flush_samr_cache(disp_info);
5341 /****************************************************************
5343 ****************************************************************/
5345 NTSTATUS _samr_GetDomPwInfo(pipes_struct *p,
5346 struct samr_GetDomPwInfo *r)
5348 /* Perform access check. Since this rpc does not require a
5349 policy handle it will not be caught by the access checks on
5350 SAMR_CONNECT or SAMR_CONNECT_ANON. */
5352 if (!pipe_access_check(p)) {
5353 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
5354 return NT_STATUS_ACCESS_DENIED;
5357 /* Actually, returning zeros here works quite well :-). */
5358 ZERO_STRUCTP(r->out.info);
5360 return NT_STATUS_OK;
5363 /*********************************************************************
5365 *********************************************************************/
5367 NTSTATUS _samr_OpenGroup(pipes_struct *p,
5368 struct samr_OpenGroup *r)
5374 struct samr_info *info;
5375 SEC_DESC *psd = NULL;
5377 uint32 des_access = r->in.access_mask;
5384 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &sid, &acc_granted, NULL))
5385 return NT_STATUS_INVALID_HANDLE;
5387 status = access_check_samr_function(acc_granted,
5388 SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
5391 if ( !NT_STATUS_IS_OK(status) )
5394 /*check if access can be granted as requested by client. */
5395 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
5396 se_map_generic(&des_access,&grp_generic_mapping);
5398 se_priv_copy( &se_rights, &se_add_users );
5400 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
5401 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
5402 &acc_granted, "_samr_OpenGroup");
5404 if ( !NT_STATUS_IS_OK(status) )
5407 /* this should not be hard-coded like this */
5409 if (!sid_equal(&sid, get_global_sam_sid()))
5410 return NT_STATUS_ACCESS_DENIED;
5412 sid_copy(&info_sid, get_global_sam_sid());
5413 sid_append_rid(&info_sid, r->in.rid);
5414 sid_to_fstring(sid_string, &info_sid);
5416 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5417 return NT_STATUS_NO_MEMORY;
5419 info->acc_granted = acc_granted;
5421 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n", sid_string));
5423 /* check if that group really exists */
5425 ret = get_domain_group_from_sid(info->sid, &map);
5428 return NT_STATUS_NO_SUCH_GROUP;
5430 /* get a (unique) handle. open a policy on it. */
5431 if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
5432 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5434 return NT_STATUS_OK;
5437 /*********************************************************************
5438 _samr_RemoveMemberFromForeignDomain
5439 *********************************************************************/
5441 NTSTATUS _samr_RemoveMemberFromForeignDomain(pipes_struct *p,
5442 struct samr_RemoveMemberFromForeignDomain *r)
5444 DOM_SID delete_sid, domain_sid;
5447 DISP_INFO *disp_info = NULL;
5449 sid_copy( &delete_sid, r->in.sid );
5451 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
5452 sid_string_dbg(&delete_sid)));
5454 /* Find the policy handle. Open a policy on it. */
5456 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &domain_sid,
5457 &acc_granted, &disp_info))
5458 return NT_STATUS_INVALID_HANDLE;
5460 result = access_check_samr_function(acc_granted,
5461 STD_RIGHT_DELETE_ACCESS,
5462 "_samr_RemoveMemberFromForeignDomain");
5464 if (!NT_STATUS_IS_OK(result))
5467 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
5468 sid_string_dbg(&domain_sid)));
5470 /* we can only delete a user from a group since we don't have
5471 nested groups anyways. So in the latter case, just say OK */
5473 /* TODO: The above comment nowadays is bogus. Since we have nested
5474 * groups now, and aliases members are never reported out of the unix
5475 * group membership, the "just say OK" makes this call a no-op. For
5476 * us. This needs fixing however. */
5478 /* I've only ever seen this in the wild when deleting a user from
5479 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
5480 * is the user about to be deleted. I very much suspect this is the
5481 * only application of this call. To verify this, let people report
5484 if (!sid_check_is_builtin(&domain_sid)) {
5485 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
5486 "global_sam_sid() = %s\n",
5487 sid_string_dbg(&domain_sid),
5488 sid_string_dbg(get_global_sam_sid())));
5489 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
5490 return NT_STATUS_OK;
5493 force_flush_samr_cache(disp_info);
5495 result = NT_STATUS_OK;
5500 /*******************************************************************
5501 _samr_QueryDomainInfo2
5502 ********************************************************************/
5504 NTSTATUS _samr_QueryDomainInfo2(pipes_struct *p,
5505 struct samr_QueryDomainInfo2 *r)
5507 return samr_QueryDomainInfo_internal("_samr_QueryDomainInfo2",
5509 r->in.domain_handle,
5514 /*******************************************************************
5516 ********************************************************************/
5518 NTSTATUS _samr_SetDomainInfo(pipes_struct *p,
5519 struct samr_SetDomainInfo *r)
5521 time_t u_expire, u_min_age;
5523 time_t u_lock_duration, u_reset_time;
5525 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5527 /* find the policy handle. open a policy on it. */
5528 if (!find_policy_by_hnd(p, r->in.domain_handle, NULL))
5529 return NT_STATUS_INVALID_HANDLE;
5531 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
5533 switch (r->in.level) {
5535 u_expire=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.max_password_age);
5536 u_min_age=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.min_password_age);
5537 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)r->in.info->info1.min_password_length);
5538 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)r->in.info->info1.password_history_length);
5539 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)r->in.info->info1.password_properties);
5540 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
5541 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
5546 u_logout=nt_time_to_unix_abs((NTTIME *)&r->in.info->info3.force_logoff_time);
5547 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
5556 u_lock_duration=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_duration);
5557 if (u_lock_duration != -1)
5558 u_lock_duration /= 60;
5560 u_reset_time=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_window)/60;
5562 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
5563 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
5564 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)r->in.info->info12.lockout_threshold);
5567 return NT_STATUS_INVALID_INFO_CLASS;
5570 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5572 return NT_STATUS_OK;
5575 /****************************************************************
5576 ****************************************************************/
5578 NTSTATUS _samr_Shutdown(pipes_struct *p,
5579 struct samr_Shutdown *r)
5581 p->rng_fault_state = true;
5582 return NT_STATUS_NOT_IMPLEMENTED;
5585 /****************************************************************
5586 ****************************************************************/
5588 NTSTATUS _samr_CreateUser(pipes_struct *p,
5589 struct samr_CreateUser *r)
5591 p->rng_fault_state = true;
5592 return NT_STATUS_NOT_IMPLEMENTED;
5595 /****************************************************************
5596 ****************************************************************/
5598 NTSTATUS _samr_SetMemberAttributesOfGroup(pipes_struct *p,
5599 struct samr_SetMemberAttributesOfGroup *r)
5601 p->rng_fault_state = true;
5602 return NT_STATUS_NOT_IMPLEMENTED;
5605 /****************************************************************
5606 ****************************************************************/
5608 NTSTATUS _samr_ChangePasswordUser(pipes_struct *p,
5609 struct samr_ChangePasswordUser *r)
5611 p->rng_fault_state = true;
5612 return NT_STATUS_NOT_IMPLEMENTED;
5615 /****************************************************************
5616 ****************************************************************/
5618 NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
5619 struct samr_GetDisplayEnumerationIndex *r)
5621 p->rng_fault_state = true;
5622 return NT_STATUS_NOT_IMPLEMENTED;
5625 /****************************************************************
5626 ****************************************************************/
5628 NTSTATUS _samr_TestPrivateFunctionsDomain(pipes_struct *p,
5629 struct samr_TestPrivateFunctionsDomain *r)
5631 p->rng_fault_state = true;
5632 return NT_STATUS_NOT_IMPLEMENTED;
5635 /****************************************************************
5636 ****************************************************************/
5638 NTSTATUS _samr_TestPrivateFunctionsUser(pipes_struct *p,
5639 struct samr_TestPrivateFunctionsUser *r)
5641 p->rng_fault_state = true;
5642 return NT_STATUS_NOT_IMPLEMENTED;
5645 /****************************************************************
5646 ****************************************************************/
5648 NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
5649 struct samr_QueryUserInfo2 *r)
5651 p->rng_fault_state = true;
5652 return NT_STATUS_NOT_IMPLEMENTED;
5655 /****************************************************************
5656 ****************************************************************/
5658 NTSTATUS _samr_GetDisplayEnumerationIndex2(pipes_struct *p,
5659 struct samr_GetDisplayEnumerationIndex2 *r)
5661 p->rng_fault_state = true;
5662 return NT_STATUS_NOT_IMPLEMENTED;
5665 /****************************************************************
5666 ****************************************************************/
5668 NTSTATUS _samr_AddMultipleMembersToAlias(pipes_struct *p,
5669 struct samr_AddMultipleMembersToAlias *r)
5671 p->rng_fault_state = true;
5672 return NT_STATUS_NOT_IMPLEMENTED;
5675 /****************************************************************
5676 ****************************************************************/
5678 NTSTATUS _samr_RemoveMultipleMembersFromAlias(pipes_struct *p,
5679 struct samr_RemoveMultipleMembersFromAlias *r)
5681 p->rng_fault_state = true;
5682 return NT_STATUS_NOT_IMPLEMENTED;
5685 /****************************************************************
5686 ****************************************************************/
5688 NTSTATUS _samr_OemChangePasswordUser2(pipes_struct *p,
5689 struct samr_OemChangePasswordUser2 *r)
5691 p->rng_fault_state = true;
5692 return NT_STATUS_NOT_IMPLEMENTED;
5695 /****************************************************************
5696 ****************************************************************/
5698 NTSTATUS _samr_SetBootKeyInformation(pipes_struct *p,
5699 struct samr_SetBootKeyInformation *r)
5701 p->rng_fault_state = true;
5702 return NT_STATUS_NOT_IMPLEMENTED;
5705 /****************************************************************
5706 ****************************************************************/
5708 NTSTATUS _samr_GetBootKeyInformation(pipes_struct *p,
5709 struct samr_GetBootKeyInformation *r)
5711 p->rng_fault_state = true;
5712 return NT_STATUS_NOT_IMPLEMENTED;
5715 /****************************************************************
5716 ****************************************************************/
5718 NTSTATUS _samr_Connect3(pipes_struct *p,
5719 struct samr_Connect3 *r)
5721 p->rng_fault_state = true;
5722 return NT_STATUS_NOT_IMPLEMENTED;
5725 /****************************************************************
5726 ****************************************************************/
5728 NTSTATUS _samr_RidToSid(pipes_struct *p,
5729 struct samr_RidToSid *r)
5731 p->rng_fault_state = true;
5732 return NT_STATUS_NOT_IMPLEMENTED;
5735 /****************************************************************
5736 ****************************************************************/
5738 NTSTATUS _samr_SetDsrmPassword(pipes_struct *p,
5739 struct samr_SetDsrmPassword *r)
5741 p->rng_fault_state = true;
5742 return NT_STATUS_NOT_IMPLEMENTED;
5745 /****************************************************************
5746 ****************************************************************/
5748 NTSTATUS _samr_ValidatePassword(pipes_struct *p,
5749 struct samr_ValidatePassword *r)
5751 p->rng_fault_state = true;
5752 return NT_STATUS_NOT_IMPLEMENTED;