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.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 * This is the implementation of the SAMR code.
36 #define DBGC_CLASS DBGC_RPC_SRV
38 #define SAMR_USR_RIGHTS_WRITE_PW \
39 ( READ_CONTROL_ACCESS | \
40 SA_RIGHT_USER_CHANGE_PASSWORD | \
41 SA_RIGHT_USER_SET_LOC_COM )
42 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
43 ( READ_CONTROL_ACCESS | SA_RIGHT_USER_SET_LOC_COM )
45 #define DISP_INFO_CACHE_TIMEOUT 10
47 typedef struct disp_info {
48 DOM_SID sid; /* identify which domain this is. */
49 BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
50 struct pdb_search *users; /* querydispinfo 1 and 4 */
51 struct pdb_search *machines; /* querydispinfo 2 */
52 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
53 struct pdb_search *aliases; /* enumaliases */
56 struct pdb_search *enum_users; /* enumusers with a mask */
58 struct timed_event *cache_timeout_event; /* cache idle timeout
62 /* We keep a static list of these by SID as modern clients close down
63 all resources between each request in a complete enumeration. */
66 /* for use by the \PIPE\samr policy */
68 BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
69 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
75 static const struct generic_mapping sam_generic_mapping = {
76 GENERIC_RIGHTS_SAM_READ,
77 GENERIC_RIGHTS_SAM_WRITE,
78 GENERIC_RIGHTS_SAM_EXECUTE,
79 GENERIC_RIGHTS_SAM_ALL_ACCESS};
80 static const struct generic_mapping dom_generic_mapping = {
81 GENERIC_RIGHTS_DOMAIN_READ,
82 GENERIC_RIGHTS_DOMAIN_WRITE,
83 GENERIC_RIGHTS_DOMAIN_EXECUTE,
84 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
85 static const struct generic_mapping usr_generic_mapping = {
86 GENERIC_RIGHTS_USER_READ,
87 GENERIC_RIGHTS_USER_WRITE,
88 GENERIC_RIGHTS_USER_EXECUTE,
89 GENERIC_RIGHTS_USER_ALL_ACCESS};
90 static const struct generic_mapping usr_nopwchange_generic_mapping = {
91 GENERIC_RIGHTS_USER_READ,
92 GENERIC_RIGHTS_USER_WRITE,
93 GENERIC_RIGHTS_USER_EXECUTE & ~SA_RIGHT_USER_CHANGE_PASSWORD,
94 GENERIC_RIGHTS_USER_ALL_ACCESS};
95 static const struct generic_mapping grp_generic_mapping = {
96 GENERIC_RIGHTS_GROUP_READ,
97 GENERIC_RIGHTS_GROUP_WRITE,
98 GENERIC_RIGHTS_GROUP_EXECUTE,
99 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
100 static const struct generic_mapping ali_generic_mapping = {
101 GENERIC_RIGHTS_ALIAS_READ,
102 GENERIC_RIGHTS_ALIAS_WRITE,
103 GENERIC_RIGHTS_ALIAS_EXECUTE,
104 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
106 /*******************************************************************
107 *******************************************************************/
109 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
110 const struct generic_mapping *map,
111 DOM_SID *sid, uint32 sid_access )
113 DOM_SID domadmin_sid;
114 SEC_ACE ace[5]; /* at most 5 entries */
120 /* basic access for Everyone */
122 init_sec_access(&mask, map->generic_execute | map->generic_read );
123 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
125 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
127 init_sec_access(&mask, map->generic_all);
129 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
130 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
132 /* Add Full Access for Domain Admins if we are a DC */
135 sid_copy( &domadmin_sid, get_global_sam_sid() );
136 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
137 init_sec_ace(&ace[i++], &domadmin_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
140 /* if we have a sid, give it some special access */
143 init_sec_access( &mask, sid_access );
144 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
147 /* create the security descriptor */
149 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
150 return NT_STATUS_NO_MEMORY;
152 if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
153 return NT_STATUS_NO_MEMORY;
158 /*******************************************************************
159 Checks if access to an object should be granted, and returns that
160 level of access for further checks.
161 ********************************************************************/
163 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
164 SE_PRIV *rights, uint32 rights_mask,
165 uint32 des_access, uint32 *acc_granted,
168 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
169 uint32 saved_mask = 0;
171 /* check privileges; certain SAM access bits should be overridden
172 by privileges (mostly having to do with creating/modifying/deleting
175 if ( rights && user_has_any_privilege( token, rights ) ) {
177 saved_mask = (des_access & rights_mask);
178 des_access &= ~saved_mask;
180 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
185 /* check the security descriptor first */
187 if ( se_access_check(psd, token, des_access, acc_granted, &status) )
190 /* give root a free pass */
192 if ( geteuid() == sec_initial_uid() ) {
194 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
195 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
197 *acc_granted = des_access;
199 status = NT_STATUS_OK;
205 /* add in any bits saved during the privilege check (only
206 matters is status is ok) */
208 *acc_granted |= rights_mask;
210 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
211 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
212 des_access, *acc_granted));
217 /*******************************************************************
218 Checks if access to a function can be granted
219 ********************************************************************/
221 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
223 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
224 debug, acc_granted, acc_required));
226 /* check the security descriptor first */
228 if ( (acc_granted&acc_required) == acc_required )
231 /* give root a free pass */
233 if (geteuid() == sec_initial_uid()) {
235 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
236 debug, acc_granted, acc_required));
237 DEBUGADD(4,("but overwritten by euid == 0\n"));
242 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
243 debug, acc_granted, acc_required));
245 return NT_STATUS_ACCESS_DENIED;
248 /*******************************************************************
249 Fetch or create a dispinfo struct.
250 ********************************************************************/
252 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
255 * We do a static cache for DISP_INFO's here. Explanation can be found
256 * in Jeremy's checkin message to r11793:
258 * Fix the SAMR cache so it works across completely insane
259 * client behaviour (ie.:
260 * open pipe/open SAMR handle/enumerate 0 - 1024
261 * close SAMR handle, close pipe.
262 * open pipe/open SAMR handle/enumerate 1024 - 2048...
263 * close SAMR handle, close pipe.
264 * And on ad-nausium. Amazing.... probably object-oriented
265 * client side programming in action yet again.
266 * This change should *massively* improve performance when
267 * enumerating users from an LDAP database.
270 * "Our" and the builtin domain are the only ones where we ever
271 * enumerate stuff, so just cache 2 entries.
274 static struct disp_info builtin_dispinfo;
275 static struct disp_info domain_dispinfo;
277 /* There are two cases to consider here:
278 1) The SID is a domain SID and we look for an equality match, or
279 2) This is an account SID and so we return the DISP_INFO* for our
286 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
288 * Necessary only once, but it does not really hurt.
290 sid_copy(&builtin_dispinfo.sid, &global_sid_Builtin);
292 return &builtin_dispinfo;
295 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
297 * Necessary only once, but it does not really hurt.
299 sid_copy(&domain_dispinfo.sid, get_global_sam_sid());
301 return &domain_dispinfo;
307 /*******************************************************************
308 Create a samr_info struct.
309 ********************************************************************/
311 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
313 struct samr_info *info;
318 sid_to_string(sid_str, psid);
320 fstrcpy(sid_str,"(NULL)");
323 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
325 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
328 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
330 sid_copy( &info->sid, psid);
331 info->builtin_domain = sid_check_is_builtin(psid);
333 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
334 info->builtin_domain = False;
336 info->mem_ctx = mem_ctx;
338 info->disp_info = get_samr_dispinfo_by_sid(psid);
343 /*******************************************************************
344 Function to free the per SID data.
345 ********************************************************************/
347 static void free_samr_cache(DISP_INFO *disp_info)
349 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
350 sid_string_static(&disp_info->sid)));
352 /* We need to become root here because the paged search might have to
353 * tell the LDAP server we're not interested in the rest anymore. */
357 if (disp_info->users) {
358 DEBUG(10,("free_samr_cache: deleting users cache\n"));
359 pdb_search_destroy(disp_info->users);
360 disp_info->users = NULL;
362 if (disp_info->machines) {
363 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
364 pdb_search_destroy(disp_info->machines);
365 disp_info->machines = NULL;
367 if (disp_info->groups) {
368 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
369 pdb_search_destroy(disp_info->groups);
370 disp_info->groups = NULL;
372 if (disp_info->aliases) {
373 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
374 pdb_search_destroy(disp_info->aliases);
375 disp_info->aliases = NULL;
377 if (disp_info->enum_users) {
378 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
379 pdb_search_destroy(disp_info->enum_users);
380 disp_info->enum_users = NULL;
382 disp_info->enum_acb_mask = 0;
387 /*******************************************************************
388 Function to free the per handle data.
389 ********************************************************************/
391 static void free_samr_info(void *ptr)
393 struct samr_info *info=(struct samr_info *) ptr;
395 /* Only free the dispinfo cache if no one bothered to set up
398 if (info->disp_info && info->disp_info->cache_timeout_event == NULL) {
399 free_samr_cache(info->disp_info);
402 talloc_destroy(info->mem_ctx);
405 /*******************************************************************
406 Idle event handler. Throw away the disp info cache.
407 ********************************************************************/
409 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
410 struct timed_event *te,
411 const struct timeval *now,
414 DISP_INFO *disp_info = (DISP_INFO *)private_data;
416 TALLOC_FREE(disp_info->cache_timeout_event);
418 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
420 free_samr_cache(disp_info);
423 /*******************************************************************
424 Setup cache removal idle event handler.
425 ********************************************************************/
427 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
429 /* Remove any pending timeout and update. */
431 TALLOC_FREE(disp_info->cache_timeout_event);
433 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
434 "SID %s for %u seconds\n",
435 sid_string_static(&disp_info->sid),
436 (unsigned int)secs_fromnow ));
438 disp_info->cache_timeout_event = event_add_timed(
439 smbd_event_context(), NULL,
440 timeval_current_ofs(secs_fromnow, 0),
441 "disp_info_cache_idle_timeout_handler",
442 disp_info_cache_idle_timeout_handler, (void *)disp_info);
445 /*******************************************************************
446 Force flush any cache. We do this on any samr_set_xxx call.
447 We must also remove the timeout handler.
448 ********************************************************************/
450 static void force_flush_samr_cache(DISP_INFO *disp_info)
452 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
456 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
457 TALLOC_FREE(disp_info->cache_timeout_event);
458 free_samr_cache(disp_info);
461 /*******************************************************************
462 Ensure password info is never given out. Paranioa... JRA.
463 ********************************************************************/
465 static void samr_clear_sam_passwd(struct samu *sam_pass)
471 /* These now zero out the old password */
473 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
474 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
477 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
479 struct samr_displayentry *entry;
481 if (info->builtin_domain) {
482 /* No users in builtin. */
486 if (info->users == NULL) {
487 info->users = pdb_search_users(acct_flags);
488 if (info->users == NULL) {
492 /* Fetch the last possible entry, thus trigger an enumeration */
493 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
495 /* Ensure we cache this enumeration. */
496 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
498 return info->users->num_entries;
501 static uint32 count_sam_groups(struct disp_info *info)
503 struct samr_displayentry *entry;
505 if (info->builtin_domain) {
506 /* No groups in builtin. */
510 if (info->groups == NULL) {
511 info->groups = pdb_search_groups();
512 if (info->groups == NULL) {
516 /* Fetch the last possible entry, thus trigger an enumeration */
517 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
519 /* Ensure we cache this enumeration. */
520 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
522 return info->groups->num_entries;
525 static uint32 count_sam_aliases(struct disp_info *info)
527 struct samr_displayentry *entry;
529 if (info->aliases == NULL) {
530 info->aliases = pdb_search_aliases(&info->sid);
531 if (info->aliases == NULL) {
535 /* Fetch the last possible entry, thus trigger an enumeration */
536 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
538 /* Ensure we cache this enumeration. */
539 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
541 return info->aliases->num_entries;
544 /*******************************************************************
546 ********************************************************************/
548 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
550 r_u->status = NT_STATUS_OK;
552 /* close the policy handle */
553 if (!close_policy_hnd(p, &q_u->pol))
554 return NT_STATUS_OBJECT_NAME_INVALID;
556 DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
561 /*******************************************************************
562 samr_reply_open_domain
563 ********************************************************************/
565 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
567 struct samr_info *info;
568 SEC_DESC *psd = NULL;
570 uint32 des_access = q_u->flags;
575 r_u->status = NT_STATUS_OK;
577 /* find the connection policy handle. */
579 if ( !find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info) )
580 return NT_STATUS_INVALID_HANDLE;
582 status = access_check_samr_function( info->acc_granted,
583 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_open_domain" );
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_open_domain" );
600 if ( !NT_STATUS_IS_OK(status) )
603 if (!sid_check_is_domain(&q_u->dom_sid.sid) &&
604 !sid_check_is_builtin(&q_u->dom_sid.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(&q_u->dom_sid.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_u->domain_pol, free_samr_info, (void *)info))
615 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
617 DEBUG(5,("samr_open_domain: %d\n", __LINE__));
622 /*******************************************************************
623 _samr_get_usrdom_pwinfo
624 ********************************************************************/
626 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
628 struct samr_info *info = NULL;
630 r_u->status = NT_STATUS_OK;
632 /* find the policy handle. open a policy on it. */
633 if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)(void *)&info))
634 return NT_STATUS_INVALID_HANDLE;
636 if (!sid_check_is_in_our_domain(&info->sid))
637 return NT_STATUS_OBJECT_TYPE_MISMATCH;
639 init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
641 DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
644 * NT sometimes return NT_STATUS_ACCESS_DENIED
645 * I don't know yet why.
651 /*******************************************************************
652 ********************************************************************/
654 static BOOL get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
655 DOM_SID *sid, uint32 *acc_granted,
656 DISP_INFO **ppdisp_info)
658 struct samr_info *info = NULL;
660 /* find the policy handle. open a policy on it. */
661 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
668 *acc_granted = info->acc_granted;
670 *ppdisp_info = info->disp_info;
676 /*******************************************************************
678 ********************************************************************/
680 NTSTATUS _samr_set_sec_obj(pipes_struct *p, SAMR_Q_SET_SEC_OBJ *q_u, SAMR_R_SET_SEC_OBJ *r_u)
683 uint32 acc_granted, i;
686 struct samu *sampass=NULL;
689 r_u->status = NT_STATUS_OK;
691 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL))
692 return NT_STATUS_INVALID_HANDLE;
694 if (!(sampass = samu_new( p->mem_ctx))) {
695 DEBUG(0,("No memory!\n"));
696 return NT_STATUS_NO_MEMORY;
699 /* get the user record */
701 ret = pdb_getsampwsid(sampass, &pol_sid);
705 DEBUG(4, ("User %s not found\n", sid_string_static(&pol_sid)));
706 TALLOC_FREE(sampass);
707 return NT_STATUS_INVALID_HANDLE;
710 dacl = q_u->buf->sd->dacl;
711 for (i=0; i < dacl->num_aces; i++) {
712 if (sid_equal(&pol_sid, &dacl->aces[i].trustee)) {
713 ret = pdb_set_pass_can_change(sampass,
714 (dacl->aces[i].access_mask &
715 SA_RIGHT_USER_CHANGE_PASSWORD) ?
722 TALLOC_FREE(sampass);
723 return NT_STATUS_ACCESS_DENIED;
726 status = access_check_samr_function(acc_granted, SA_RIGHT_USER_SET_ATTRIBUTES, "_samr_set_sec_obj");
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_static(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_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
780 SEC_DESC * psd = NULL;
784 r_u->status = NT_STATUS_OK;
787 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid, &acc_granted, NULL))
788 return NT_STATUS_INVALID_HANDLE;
790 DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &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_query_sec_obj: querying security on SAM\n"));
797 r_u->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_query_sec_obj: querying security on Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
801 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
802 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
803 /* check if it is the Builtin Domain */
804 /* TODO: Builtin probably needs a different SD with restricted write access*/
805 DEBUG(5,("_samr_query_sec_obj: querying security on Builtin Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
806 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
807 } else if (sid_check_is_in_our_domain(&pol_sid) ||
808 sid_check_is_in_builtin(&pol_sid)) {
809 /* TODO: different SDs have to be generated for aliases groups and users.
810 Currently all three get a default user SD */
811 DEBUG(10,("_samr_query_sec_obj: querying security on Object with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
812 if (check_change_pw_access(p->mem_ctx, &pol_sid)) {
813 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
814 &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
816 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_nopwchange_generic_mapping,
817 &pol_sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
820 return NT_STATUS_OBJECT_TYPE_MISMATCH;
823 if ((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
824 return NT_STATUS_NO_MEMORY;
826 if (NT_STATUS_IS_OK(r_u->status))
832 /*******************************************************************
833 makes a SAM_ENTRY / UNISTR2* structure from a user list.
834 ********************************************************************/
836 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
837 UNISTR2 **uni_name_pp,
838 uint32 num_entries, uint32 start_idx,
839 struct samr_displayentry *entries)
848 if (num_entries == 0)
851 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_entries);
853 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_entries);
855 if (sam == NULL || uni_name == NULL) {
856 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
857 return NT_STATUS_NO_MEMORY;
860 for (i = 0; i < num_entries; i++) {
861 UNISTR2 uni_temp_name;
863 * usrmgr expects a non-NULL terminated string with
864 * trust relationships
866 if (entries[i].acct_flags & ACB_DOMTRUST) {
867 init_unistr2(&uni_temp_name, entries[i].account_name,
870 init_unistr2(&uni_temp_name, entries[i].account_name,
874 init_sam_entry(&sam[i], &uni_temp_name, entries[i].rid);
875 copy_unistr2(&uni_name[i], &uni_temp_name);
879 *uni_name_pp = uni_name;
883 /*******************************************************************
884 samr_reply_enum_dom_users
885 ********************************************************************/
887 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u,
888 SAMR_R_ENUM_DOM_USERS *r_u)
890 struct samr_info *info = NULL;
892 uint32 enum_context=q_u->start_idx;
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;
898 r_u->status = NT_STATUS_OK;
900 /* find the policy handle. open a policy on it. */
901 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
902 return NT_STATUS_INVALID_HANDLE;
904 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
905 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
906 "_samr_enum_dom_users"))) {
910 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
912 if (info->builtin_domain) {
913 /* No users in builtin. */
914 init_samr_r_enum_dom_users(r_u, q_u->start_idx, 0);
915 DEBUG(5,("_samr_enum_dom_users: No users in BUILTIN\n"));
923 if ((info->disp_info->enum_users != NULL) &&
924 (info->disp_info->enum_acb_mask != q_u->acb_mask)) {
925 pdb_search_destroy(info->disp_info->enum_users);
926 info->disp_info->enum_users = NULL;
929 if (info->disp_info->enum_users == NULL) {
930 info->disp_info->enum_users = pdb_search_users(q_u->acb_mask);
931 info->disp_info->enum_acb_mask = q_u->acb_mask;
934 if (info->disp_info->enum_users == NULL) {
935 /* END AS ROOT !!!! */
937 return NT_STATUS_ACCESS_DENIED;
940 num_account = pdb_search_entries(info->disp_info->enum_users,
941 enum_context, max_entries,
944 /* END AS ROOT !!!! */
948 if (num_account == 0) {
949 DEBUG(5, ("_samr_enum_dom_users: enumeration handle over "
951 init_samr_r_enum_dom_users(r_u, q_u->start_idx, 0);
955 r_u->status = make_user_sam_entry_list(p->mem_ctx, &r_u->sam,
957 num_account, enum_context,
960 if (!NT_STATUS_IS_OK(r_u->status))
963 if (max_entries <= num_account) {
964 r_u->status = STATUS_MORE_ENTRIES;
966 r_u->status = NT_STATUS_OK;
969 /* Ensure we cache this enumeration. */
970 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
972 DEBUG(5, ("_samr_enum_dom_users: %d\n", __LINE__));
974 init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_account,
977 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
982 /*******************************************************************
983 makes a SAM_ENTRY / UNISTR2* structure from a group list.
984 ********************************************************************/
986 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
987 UNISTR2 **uni_name_pp,
988 uint32 num_sam_entries,
989 struct samr_displayentry *entries)
998 if (num_sam_entries == 0)
1001 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
1002 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
1004 if (sam == NULL || uni_name == NULL) {
1005 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
1009 for (i = 0; i < num_sam_entries; i++) {
1011 * JRA. I think this should include the null. TNG does not.
1013 init_unistr2(&uni_name[i], entries[i].account_name,
1015 init_sam_entry(&sam[i], &uni_name[i], entries[i].rid);
1019 *uni_name_pp = uni_name;
1022 /*******************************************************************
1023 samr_reply_enum_dom_groups
1024 ********************************************************************/
1026 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
1028 struct samr_info *info = NULL;
1029 struct samr_displayentry *groups;
1032 r_u->status = NT_STATUS_OK;
1034 /* find the policy handle. open a policy on it. */
1035 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1036 return NT_STATUS_INVALID_HANDLE;
1038 r_u->status = access_check_samr_function(info->acc_granted,
1039 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1040 "_samr_enum_dom_groups");
1041 if (!NT_STATUS_IS_OK(r_u->status))
1044 DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
1046 if (info->builtin_domain) {
1047 /* No groups in builtin. */
1048 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, 0);
1049 DEBUG(5,("_samr_enum_dom_users: No groups in BUILTIN\n"));
1053 /* the domain group array is being allocated in the function below */
1057 if (info->disp_info->groups == NULL) {
1058 info->disp_info->groups = pdb_search_groups();
1060 if (info->disp_info->groups == NULL) {
1062 return NT_STATUS_ACCESS_DENIED;
1066 num_groups = pdb_search_entries(info->disp_info->groups, q_u->start_idx,
1067 MAX_SAM_ENTRIES, &groups);
1070 /* Ensure we cache this enumeration. */
1071 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1073 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
1074 num_groups, groups);
1076 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_groups);
1078 DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
1083 /*******************************************************************
1084 samr_reply_enum_dom_aliases
1085 ********************************************************************/
1087 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
1089 struct samr_info *info;
1090 struct samr_displayentry *aliases;
1091 uint32 num_aliases = 0;
1093 /* find the policy handle. open a policy on it. */
1094 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1095 return NT_STATUS_INVALID_HANDLE;
1097 r_u->status = access_check_samr_function(info->acc_granted,
1098 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1099 "_samr_enum_dom_aliases");
1100 if (!NT_STATUS_IS_OK(r_u->status))
1103 DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n",
1104 sid_string_static(&info->sid)));
1108 if (info->disp_info->aliases == NULL) {
1109 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1110 if (info->disp_info->aliases == NULL) {
1112 return NT_STATUS_ACCESS_DENIED;
1116 num_aliases = pdb_search_entries(info->disp_info->aliases, q_u->start_idx,
1117 MAX_SAM_ENTRIES, &aliases);
1120 /* Ensure we cache this enumeration. */
1121 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1123 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
1124 num_aliases, aliases);
1126 init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_aliases,
1129 DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
1134 /*******************************************************************
1135 samr_reply_query_dispinfo
1136 ********************************************************************/
1138 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u,
1139 SAMR_R_QUERY_DISPINFO *r_u)
1141 struct samr_info *info = NULL;
1142 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1144 uint32 max_entries=q_u->max_entries;
1145 uint32 enum_context=q_u->start_idx;
1146 uint32 max_size=q_u->max_size;
1148 SAM_DISPINFO_CTR *ctr;
1149 uint32 temp_size=0, total_data_size=0;
1150 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1151 uint32 num_account = 0;
1152 enum remote_arch_types ra_type = get_remote_arch();
1153 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1154 struct samr_displayentry *entries = NULL;
1156 DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
1157 r_u->status = NT_STATUS_UNSUCCESSFUL;
1159 /* find the policy handle. open a policy on it. */
1160 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info))
1161 return NT_STATUS_INVALID_HANDLE;
1164 * calculate how many entries we will return.
1166 * - the number of entries the client asked
1167 * - our limit on that
1168 * - the starting point (enumeration context)
1169 * - the buffer size the client will accept
1173 * We are a lot more like W2K. Instead of reading the SAM
1174 * each time to find the records we need to send back,
1175 * we read it once and link that copy to the sam handle.
1176 * For large user list (over the MAX_SAM_ENTRIES)
1177 * it's a definitive win.
1178 * second point to notice: between enumerations
1179 * our sam is now the same as it's a snapshoot.
1180 * third point: got rid of the static SAM_USER_21 struct
1181 * no more intermediate.
1182 * con: it uses much more memory, as a full copy is stored
1185 * If you want to change it, think twice and think
1186 * of the second point , that's really important.
1191 if ((q_u->switch_level < 1) || (q_u->switch_level > 5)) {
1192 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n",
1193 (unsigned int)q_u->switch_level ));
1194 return NT_STATUS_INVALID_INFO_CLASS;
1197 /* first limit the number of entries we will return */
1198 if(max_entries > max_sam_entries) {
1199 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d "
1200 "entries, limiting to %d\n", max_entries,
1202 max_entries = max_sam_entries;
1205 /* calculate the size and limit on the number of entries we will
1208 temp_size=max_entries*struct_size;
1210 if (temp_size>max_size) {
1211 max_entries=MIN((max_size/struct_size),max_entries);;
1212 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to "
1213 "only %d entries\n", max_entries));
1216 if (!(ctr = TALLOC_ZERO_P(p->mem_ctx,SAM_DISPINFO_CTR)))
1217 return NT_STATUS_NO_MEMORY;
1223 /* THe following done as ROOT. Don't return without unbecome_root(). */
1225 switch (q_u->switch_level) {
1228 if (info->disp_info->users == NULL) {
1229 info->disp_info->users = pdb_search_users(ACB_NORMAL);
1230 if (info->disp_info->users == NULL) {
1232 return NT_STATUS_ACCESS_DENIED;
1234 DEBUG(10,("samr_reply_query_dispinfo: starting user enumeration at index %u\n",
1235 (unsigned int)enum_context ));
1237 DEBUG(10,("samr_reply_query_dispinfo: using cached user enumeration at index %u\n",
1238 (unsigned int)enum_context ));
1241 num_account = pdb_search_entries(info->disp_info->users,
1242 enum_context, max_entries,
1246 if (info->disp_info->machines == NULL) {
1247 info->disp_info->machines =
1248 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1249 if (info->disp_info->machines == NULL) {
1251 return NT_STATUS_ACCESS_DENIED;
1253 DEBUG(10,("samr_reply_query_dispinfo: starting machine enumeration at index %u\n",
1254 (unsigned int)enum_context ));
1256 DEBUG(10,("samr_reply_query_dispinfo: using cached machine enumeration at index %u\n",
1257 (unsigned int)enum_context ));
1260 num_account = pdb_search_entries(info->disp_info->machines,
1261 enum_context, max_entries,
1266 if (info->disp_info->groups == NULL) {
1267 info->disp_info->groups = pdb_search_groups();
1268 if (info->disp_info->groups == NULL) {
1270 return NT_STATUS_ACCESS_DENIED;
1272 DEBUG(10,("samr_reply_query_dispinfo: starting group enumeration at index %u\n",
1273 (unsigned int)enum_context ));
1275 DEBUG(10,("samr_reply_query_dispinfo: using cached group enumeration at index %u\n",
1276 (unsigned int)enum_context ));
1279 num_account = pdb_search_entries(info->disp_info->groups,
1280 enum_context, max_entries,
1285 smb_panic("info class changed");
1290 /* Now create reply structure */
1291 switch (q_u->switch_level) {
1293 disp_ret = init_sam_dispinfo_1(p->mem_ctx, &ctr->sam.info1,
1294 num_account, enum_context,
1298 disp_ret = init_sam_dispinfo_2(p->mem_ctx, &ctr->sam.info2,
1299 num_account, enum_context,
1303 disp_ret = init_sam_dispinfo_3(p->mem_ctx, &ctr->sam.info3,
1304 num_account, enum_context,
1308 disp_ret = init_sam_dispinfo_4(p->mem_ctx, &ctr->sam.info4,
1309 num_account, enum_context,
1313 disp_ret = init_sam_dispinfo_5(p->mem_ctx, &ctr->sam.info5,
1314 num_account, enum_context,
1318 smb_panic("info class changed");
1322 if (!NT_STATUS_IS_OK(disp_ret))
1325 /* calculate the total size */
1326 total_data_size=num_account*struct_size;
1329 r_u->status = STATUS_MORE_ENTRIES;
1331 r_u->status = NT_STATUS_OK;
1334 /* Ensure we cache this enumeration. */
1335 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1337 DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
1339 init_samr_r_query_dispinfo(r_u, num_account, total_data_size,
1340 temp_size, q_u->switch_level, ctr,
1347 /*******************************************************************
1348 samr_reply_query_aliasinfo
1349 ********************************************************************/
1351 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
1354 struct acct_info info;
1358 r_u->status = NT_STATUS_OK;
1360 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1362 /* find the policy handle. open a policy on it. */
1363 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
1364 return NT_STATUS_INVALID_HANDLE;
1365 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_LOOKUP_INFO, "_samr_query_aliasinfo"))) {
1370 status = pdb_get_aliasinfo(&sid, &info);
1373 if ( !NT_STATUS_IS_OK(status))
1376 if ( !(r_u->ctr = TALLOC_ZERO_P( p->mem_ctx, ALIAS_INFO_CTR )) )
1377 return NT_STATUS_NO_MEMORY;
1380 switch (q_u->level ) {
1382 r_u->ctr->level = 1;
1383 init_samr_alias_info1(&r_u->ctr->alias.info1, info.acct_name, 1, info.acct_desc);
1386 r_u->ctr->level = 3;
1387 init_samr_alias_info3(&r_u->ctr->alias.info3, info.acct_desc);
1390 return NT_STATUS_INVALID_INFO_CLASS;
1393 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1399 /*******************************************************************
1400 samr_reply_lookup_ids
1401 ********************************************************************/
1403 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1405 uint32 rid[MAX_SAM_ENTRIES];
1406 int num_rids = q_u->num_sids1;
1408 r_u->status = NT_STATUS_OK;
1410 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1412 if (num_rids > MAX_SAM_ENTRIES) {
1413 num_rids = MAX_SAM_ENTRIES;
1414 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1419 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1421 for (i = 0; i < num_rids && status == 0; i++)
1423 struct sam_passwd *sam_pass;
1427 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1428 q_u->uni_user_name[i].uni_str_len));
1430 /* find the user account */
1432 sam_pass = get_smb21pwd_entry(user_name, 0);
1435 if (sam_pass == NULL)
1437 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1442 rid[i] = sam_pass->user_rid;
1448 rid[0] = BUILTIN_ALIAS_RID_USERS;
1450 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1452 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1458 /*******************************************************************
1460 ********************************************************************/
1462 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1464 uint32 rid[MAX_SAM_ENTRIES];
1465 enum lsa_SidType type[MAX_SAM_ENTRIES];
1467 int num_rids = q_u->num_names2;
1472 r_u->status = NT_STATUS_OK;
1474 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1479 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL)) {
1480 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1484 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, 0, "_samr_lookup_names"))) { /* Don't know the acc_bits yet */
1488 if (num_rids > MAX_SAM_ENTRIES) {
1489 num_rids = MAX_SAM_ENTRIES;
1490 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1493 DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1495 for (i = 0; i < num_rids; i++) {
1499 r_u->status = NT_STATUS_NONE_MAPPED;
1500 type[i] = SID_NAME_UNKNOWN;
1502 rid [i] = 0xffffffff;
1504 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1510 if (sid_check_is_builtin(&pol_sid)) {
1511 if (lookup_builtin_name(name, &rid[i])) {
1512 type[i] = SID_NAME_ALIAS;
1515 lookup_global_sam_name(name, 0, &rid[i], &type[i]);
1518 if (type[i] != SID_NAME_UNKNOWN) {
1519 r_u->status = NT_STATUS_OK;
1523 init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, type, r_u->status);
1525 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1530 /*******************************************************************
1531 _samr_chgpasswd_user
1532 ********************************************************************/
1534 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1539 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1541 r_u->status = NT_STATUS_OK;
1543 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1544 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1546 DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1549 * Pass the user through the NT -> unix user mapping
1553 (void)map_username(user_name);
1556 * UNIX username case mangling not required, pass_oem_change
1557 * is case insensitive.
1560 r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1561 q_u->nt_newpass.pass, q_u->nt_oldhash.hash, NULL);
1563 init_samr_r_chgpasswd_user(r_u, r_u->status);
1565 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1570 /*******************************************************************
1571 _samr_chgpasswd_user3
1572 ********************************************************************/
1574 NTSTATUS _samr_chgpasswd_user3(pipes_struct *p, SAMR_Q_CHGPASSWD_USER3 *q_u, SAMR_R_CHGPASSWD_USER3 *r_u)
1578 uint32 reject_reason;
1579 SAM_UNK_INFO_1 *info = NULL;
1580 SAMR_CHANGE_REJECT *reject = NULL;
1582 DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
1584 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1585 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1587 DEBUG(5,("_samr_chgpasswd_user3: user: %s wks: %s\n", user_name, wks));
1590 * Pass the user through the NT -> unix user mapping
1594 (void)map_username(user_name);
1597 * UNIX username case mangling not required, pass_oem_change
1598 * is case insensitive.
1601 r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1602 q_u->nt_newpass.pass, q_u->nt_oldhash.hash, &reject_reason);
1604 if (NT_STATUS_EQUAL(r_u->status, NT_STATUS_PASSWORD_RESTRICTION) ||
1605 NT_STATUS_EQUAL(r_u->status, NT_STATUS_ACCOUNT_RESTRICTION)) {
1607 uint32 min_pass_len,pass_hist,password_properties;
1608 time_t u_expire, u_min_age;
1609 NTTIME nt_expire, nt_min_age;
1610 uint32 account_policy_temp;
1612 if ((info = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_INFO_1)) == NULL) {
1613 return NT_STATUS_NO_MEMORY;
1616 if ((reject = TALLOC_ZERO_P(p->mem_ctx, SAMR_CHANGE_REJECT)) == NULL) {
1617 return NT_STATUS_NO_MEMORY;
1621 ZERO_STRUCTP(reject);
1627 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
1628 min_pass_len = account_policy_temp;
1630 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
1631 pass_hist = account_policy_temp;
1633 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
1634 password_properties = account_policy_temp;
1636 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1637 u_expire = account_policy_temp;
1639 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1640 u_min_age = account_policy_temp;
1646 unix_to_nt_time_abs(&nt_expire, u_expire);
1647 unix_to_nt_time_abs(&nt_min_age, u_min_age);
1649 init_unk_info1(info, (uint16)min_pass_len, (uint16)pass_hist,
1650 password_properties, nt_expire, nt_min_age);
1652 reject->reject_reason = reject_reason;
1655 init_samr_r_chgpasswd_user3(r_u, r_u->status, reject, info);
1657 DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
1662 /*******************************************************************
1663 makes a SAMR_R_LOOKUP_RIDS structure.
1664 ********************************************************************/
1666 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1667 const char **names, UNIHDR **pp_hdr_name,
1668 UNISTR2 **pp_uni_name)
1671 UNIHDR *hdr_name=NULL;
1672 UNISTR2 *uni_name=NULL;
1674 *pp_uni_name = NULL;
1675 *pp_hdr_name = NULL;
1677 if (num_names != 0) {
1678 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1679 if (hdr_name == NULL)
1682 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1683 if (uni_name == NULL)
1687 for (i = 0; i < num_names; i++) {
1688 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1689 init_unistr2(&uni_name[i], names[i], UNI_FLAGS_NONE);
1690 init_uni_hdr(&hdr_name[i], &uni_name[i]);
1693 *pp_uni_name = uni_name;
1694 *pp_hdr_name = hdr_name;
1699 /*******************************************************************
1701 ********************************************************************/
1703 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1706 enum lsa_SidType *attrs = NULL;
1707 uint32 *wire_attrs = NULL;
1708 UNIHDR *hdr_name = NULL;
1709 UNISTR2 *uni_name = NULL;
1711 int num_rids = (int)q_u->num_rids1;
1715 r_u->status = NT_STATUS_OK;
1717 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1719 /* find the policy handle. open a policy on it. */
1720 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL))
1721 return NT_STATUS_INVALID_HANDLE;
1723 if (num_rids > 1000) {
1724 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1725 "to samba4 idl this is not possible\n", num_rids));
1726 return NT_STATUS_UNSUCCESSFUL;
1730 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1731 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
1732 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1734 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
1735 return NT_STATUS_NO_MEMORY;
1742 become_root(); /* lookup_sid can require root privs */
1743 r_u->status = pdb_lookup_rids(&pol_sid, num_rids, q_u->rid,
1747 if ( NT_STATUS_EQUAL(r_u->status, NT_STATUS_NONE_MAPPED) && (num_rids == 0) ) {
1748 r_u->status = NT_STATUS_OK;
1751 if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1752 &hdr_name, &uni_name))
1753 return NT_STATUS_NO_MEMORY;
1755 /* Convert from enum lsa_SidType to uint32 for wire format. */
1756 for (i = 0; i < num_rids; i++) {
1757 wire_attrs[i] = (uint32)attrs[i];
1760 init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, wire_attrs);
1762 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1767 /*******************************************************************
1768 _samr_open_user. Safe - gives out no passwd info.
1769 ********************************************************************/
1771 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1773 struct samu *sampass=NULL;
1775 POLICY_HND domain_pol = q_u->domain_pol;
1776 POLICY_HND *user_pol = &r_u->user_pol;
1777 struct samr_info *info = NULL;
1778 SEC_DESC *psd = NULL;
1780 uint32 des_access = q_u->access_mask;
1786 r_u->status = NT_STATUS_OK;
1788 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1790 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
1791 return NT_STATUS_INVALID_HANDLE;
1793 nt_status = access_check_samr_function( acc_granted,
1794 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1796 if ( !NT_STATUS_IS_OK(nt_status) )
1799 if ( !(sampass = samu_new( p->mem_ctx )) ) {
1800 return NT_STATUS_NO_MEMORY;
1803 /* append the user's RID to it */
1805 if (!sid_append_rid(&sid, q_u->user_rid))
1806 return NT_STATUS_NO_SUCH_USER;
1808 /* check if access can be granted as requested by client. */
1810 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
1811 se_map_generic(&des_access, &usr_generic_mapping);
1813 se_priv_copy( &se_rights, &se_machine_account );
1814 se_priv_add( &se_rights, &se_add_users );
1816 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
1817 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
1818 &acc_granted, "_samr_open_user");
1820 if ( !NT_STATUS_IS_OK(nt_status) )
1824 ret=pdb_getsampwsid(sampass, &sid);
1827 /* check that the SID exists in our domain. */
1829 return NT_STATUS_NO_SUCH_USER;
1832 TALLOC_FREE(sampass);
1834 /* associate the user's SID and access bits with the new handle. */
1835 if ((info = get_samr_info_by_sid(&sid)) == NULL)
1836 return NT_STATUS_NO_MEMORY;
1837 info->acc_granted = acc_granted;
1839 /* get a (unique) handle. open a policy on it. */
1840 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1841 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1846 /*************************************************************************
1847 get_user_info_7. Safe. Only gives out account_name.
1848 *************************************************************************/
1850 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1852 struct samu *smbpass=NULL;
1855 if ( !(smbpass = samu_new( mem_ctx )) ) {
1856 return NT_STATUS_NO_MEMORY;
1860 ret = pdb_getsampwsid(smbpass, user_sid);
1864 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1865 return NT_STATUS_NO_SUCH_USER;
1868 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1871 init_sam_user_info7(id7, pdb_get_username(smbpass) );
1873 TALLOC_FREE(smbpass);
1875 return NT_STATUS_OK;
1878 /*************************************************************************
1879 get_user_info_9. Only gives out primary group SID.
1880 *************************************************************************/
1881 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx, SAM_USER_INFO_9 * id9, DOM_SID *user_sid)
1883 struct samu *smbpass=NULL;
1886 if ( !(smbpass = samu_new( mem_ctx )) ) {
1887 return NT_STATUS_NO_MEMORY;
1891 ret = pdb_getsampwsid(smbpass, user_sid);
1895 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1896 return NT_STATUS_NO_SUCH_USER;
1899 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1902 init_sam_user_info9(id9, pdb_get_group_rid(smbpass) );
1904 TALLOC_FREE(smbpass);
1906 return NT_STATUS_OK;
1909 /*************************************************************************
1910 get_user_info_16. Safe. Only gives out acb bits.
1911 *************************************************************************/
1913 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1915 struct samu *smbpass=NULL;
1918 if ( !(smbpass = samu_new( mem_ctx )) ) {
1919 return NT_STATUS_NO_MEMORY;
1923 ret = pdb_getsampwsid(smbpass, user_sid);
1927 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1928 return NT_STATUS_NO_SUCH_USER;
1931 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1934 init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1936 TALLOC_FREE(smbpass);
1938 return NT_STATUS_OK;
1941 /*************************************************************************
1942 get_user_info_18. OK - this is the killer as it gives out password info.
1943 Ensure that this is only allowed on an encrypted connection with a root
1945 *************************************************************************/
1947 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1949 struct samu *smbpass=NULL;
1952 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
1953 return NT_STATUS_ACCESS_DENIED;
1956 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1957 return NT_STATUS_ACCESS_DENIED;
1961 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1964 if ( !(smbpass = samu_new( mem_ctx )) ) {
1965 return NT_STATUS_NO_MEMORY;
1968 ret = pdb_getsampwsid(smbpass, user_sid);
1971 DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
1972 TALLOC_FREE(smbpass);
1973 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1976 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1978 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1979 TALLOC_FREE(smbpass);
1980 return NT_STATUS_ACCOUNT_DISABLED;
1984 init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1986 TALLOC_FREE(smbpass);
1988 return NT_STATUS_OK;
1991 /*************************************************************************
1993 *************************************************************************/
1995 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1997 struct samu *sampass=NULL;
2000 if ( !(sampass = samu_new( mem_ctx )) ) {
2001 return NT_STATUS_NO_MEMORY;
2005 ret = pdb_getsampwsid(sampass, user_sid);
2009 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
2010 return NT_STATUS_NO_SUCH_USER;
2013 samr_clear_sam_passwd(sampass);
2015 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
2018 init_sam_user_info20A(id20, sampass);
2020 TALLOC_FREE(sampass);
2022 return NT_STATUS_OK;
2025 /*************************************************************************
2027 *************************************************************************/
2029 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
2030 DOM_SID *user_sid, DOM_SID *domain_sid)
2032 struct samu *sampass=NULL;
2036 if ( !(sampass = samu_new( mem_ctx )) ) {
2037 return NT_STATUS_NO_MEMORY;
2041 ret = pdb_getsampwsid(sampass, user_sid);
2045 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
2046 return NT_STATUS_NO_SUCH_USER;
2049 samr_clear_sam_passwd(sampass);
2051 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
2054 nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
2056 TALLOC_FREE(sampass);
2061 /*******************************************************************
2062 _samr_query_userinfo
2063 ********************************************************************/
2065 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
2067 SAM_USERINFO_CTR *ctr;
2068 struct samr_info *info = NULL;
2072 r_u->status=NT_STATUS_OK;
2074 /* search for the handle */
2075 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
2076 return NT_STATUS_INVALID_HANDLE;
2078 domain_sid = info->sid;
2080 sid_split_rid(&domain_sid, &rid);
2082 if (!sid_check_is_in_our_domain(&info->sid))
2083 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2085 DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
2087 ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
2089 return NT_STATUS_NO_MEMORY;
2093 /* ok! user info levels (lots: see MSDEV help), off we go... */
2094 ctr->switch_value = q_u->switch_value;
2096 DEBUG(5,("_samr_query_userinfo: user info level: %d\n", q_u->switch_value));
2098 switch (q_u->switch_value) {
2100 ctr->info.id7 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_7);
2101 if (ctr->info.id7 == NULL)
2102 return NT_STATUS_NO_MEMORY;
2104 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_7(p->mem_ctx, ctr->info.id7, &info->sid)))
2108 ctr->info.id9 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_9);
2109 if (ctr->info.id9 == NULL)
2110 return NT_STATUS_NO_MEMORY;
2112 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_9(p->mem_ctx, ctr->info.id9, &info->sid)))
2116 ctr->info.id16 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_16);
2117 if (ctr->info.id16 == NULL)
2118 return NT_STATUS_NO_MEMORY;
2120 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_16(p->mem_ctx, ctr->info.id16, &info->sid)))
2125 ctr->info.id18 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_18);
2126 if (ctr->info.id18 == NULL)
2127 return NT_STATUS_NO_MEMORY;
2129 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_18(p, p->mem_ctx, ctr->info.id18, &info->sid)))
2134 ctr->info.id20 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_20);
2135 if (ctr->info.id20 == NULL)
2136 return NT_STATUS_NO_MEMORY;
2137 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_20(p->mem_ctx, ctr->info.id20, &info->sid)))
2142 ctr->info.id21 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_21);
2143 if (ctr->info.id21 == NULL)
2144 return NT_STATUS_NO_MEMORY;
2145 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_21(p->mem_ctx, ctr->info.id21,
2146 &info->sid, &domain_sid)))
2151 return NT_STATUS_INVALID_INFO_CLASS;
2154 init_samr_r_query_userinfo(r_u, ctr, r_u->status);
2156 DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
2161 /*******************************************************************
2162 samr_reply_query_usergroups
2163 ********************************************************************/
2165 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
2167 struct samu *sam_pass=NULL;
2171 DOM_GID *gids = NULL;
2172 uint32 primary_group_rid;
2173 size_t num_groups = 0;
2179 BOOL success = False;
2182 * from the SID in the request:
2183 * we should send back the list of DOMAIN GROUPS
2184 * the user is a member of
2186 * and only the DOMAIN GROUPS
2187 * no ALIASES !!! neither aliases of the domain
2188 * nor aliases of the builtin SID
2193 r_u->status = NT_STATUS_OK;
2195 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2197 /* find the policy handle. open a policy on it. */
2198 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
2199 return NT_STATUS_INVALID_HANDLE;
2201 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_USER_GET_GROUPS, "_samr_query_usergroups"))) {
2205 if (!sid_check_is_in_our_domain(&sid))
2206 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2208 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2209 return NT_STATUS_NO_MEMORY;
2213 ret = pdb_getsampwsid(sam_pass, &sid);
2217 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2218 sid_string_static(&sid)));
2219 return NT_STATUS_NO_SUCH_USER;
2224 /* make both calls inside the root block */
2226 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2227 &sids, &unix_gids, &num_groups);
2228 if ( NT_STATUS_IS_OK(result) ) {
2229 success = sid_peek_check_rid(get_global_sam_sid(),
2230 pdb_get_group_sid(sam_pass),
2231 &primary_group_rid);
2235 if (!NT_STATUS_IS_OK(result)) {
2236 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2237 sid_string_static(&sid)));
2242 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2243 sid_string_static(pdb_get_group_sid(sam_pass)),
2244 pdb_get_username(sam_pass)));
2245 TALLOC_FREE(sam_pass);
2246 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2252 dom_gid.attr = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2254 dom_gid.g_rid = primary_group_rid;
2255 ADD_TO_ARRAY(p->mem_ctx, DOM_GID, dom_gid, &gids, &num_gids);
2257 for (i=0; i<num_groups; i++) {
2259 if (!sid_peek_check_rid(get_global_sam_sid(),
2260 &(sids[i]), &dom_gid.g_rid)) {
2261 DEBUG(10, ("Found sid %s not in our domain\n",
2262 sid_string_static(&sids[i])));
2266 if (dom_gid.g_rid == primary_group_rid) {
2267 /* We added the primary group directly from the
2268 * sam_account. The other SIDs are unique from
2269 * enum_group_memberships */
2273 ADD_TO_ARRAY(p->mem_ctx, DOM_GID, dom_gid, &gids, &num_gids);
2276 /* construct the response. lkclXXXX: gids are not copied! */
2277 init_samr_r_query_usergroups(r_u, num_gids, gids, r_u->status);
2279 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2284 /*******************************************************************
2285 _samr_query_domain_info
2286 ********************************************************************/
2288 NTSTATUS _samr_query_domain_info(pipes_struct *p,
2289 SAMR_Q_QUERY_DOMAIN_INFO *q_u,
2290 SAMR_R_QUERY_DOMAIN_INFO *r_u)
2292 struct samr_info *info = NULL;
2294 uint32 min_pass_len,pass_hist,password_properties;
2295 time_t u_expire, u_min_age;
2296 NTTIME nt_expire, nt_min_age;
2298 time_t u_lock_duration, u_reset_time;
2299 NTTIME nt_lock_duration, nt_reset_time;
2304 uint32 account_policy_temp;
2309 uint32 num_users=0, num_groups=0, num_aliases=0;
2311 if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL) {
2312 return NT_STATUS_NO_MEMORY;
2317 r_u->status = NT_STATUS_OK;
2319 DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
2321 /* find the policy handle. open a policy on it. */
2322 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info)) {
2323 return NT_STATUS_INVALID_HANDLE;
2326 switch (q_u->switch_value) {
2333 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2334 min_pass_len = account_policy_temp;
2336 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2337 pass_hist = account_policy_temp;
2339 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2340 password_properties = account_policy_temp;
2342 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2343 u_expire = account_policy_temp;
2345 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2346 u_min_age = account_policy_temp;
2352 unix_to_nt_time_abs(&nt_expire, u_expire);
2353 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2355 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
2356 password_properties, nt_expire, nt_min_age);
2364 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
2365 num_groups = count_sam_groups(info->disp_info);
2366 num_aliases = count_sam_aliases(info->disp_info);
2368 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
2369 u_logout = account_policy_temp;
2371 unix_to_nt_time_abs(&nt_logout, u_logout);
2373 if (!pdb_get_seq_num(&seq_num))
2374 seq_num = time(NULL);
2380 server_role = ROLE_DOMAIN_PDC;
2381 if (lp_server_role() == ROLE_DOMAIN_BDC)
2382 server_role = ROLE_DOMAIN_BDC;
2384 init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num,
2385 num_users, num_groups, num_aliases, nt_logout, server_role);
2395 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
2396 u_logout = (time_t)ul;
2403 unix_to_nt_time_abs(&nt_logout, u_logout);
2405 init_unk_info3(&ctr->info.inf3, nt_logout);
2408 init_unk_info4(&ctr->info.inf4, lp_serverstring());
2411 init_unk_info5(&ctr->info.inf5, get_global_sam_name());
2414 /* NT returns its own name when a PDC. win2k and later
2415 * only the name of the PDC if itself is a BDC (samba4
2417 init_unk_info6(&ctr->info.inf6, global_myname());
2420 server_role = ROLE_DOMAIN_PDC;
2421 if (lp_server_role() == ROLE_DOMAIN_BDC)
2422 server_role = ROLE_DOMAIN_BDC;
2424 init_unk_info7(&ctr->info.inf7, server_role);
2432 if (!pdb_get_seq_num(&seq_num)) {
2433 seq_num = time(NULL);
2440 init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
2448 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2449 u_lock_duration = account_policy_temp;
2450 if (u_lock_duration != -1) {
2451 u_lock_duration *= 60;
2454 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2455 u_reset_time = account_policy_temp * 60;
2457 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
2458 lockout = account_policy_temp;
2464 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
2465 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
2467 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
2470 return NT_STATUS_INVALID_INFO_CLASS;
2474 init_samr_r_query_domain_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
2476 DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
2481 /* W2k3 seems to use the same check for all 3 objects that can be created via
2482 * SAMR, if you try to create for example "Dialup" as an alias it says
2483 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
2486 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
2488 enum lsa_SidType type;
2491 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
2494 /* Lookup in our local databases (only LOOKUP_NAME_ISOLATED set)
2495 * whether the name already exists */
2496 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_ISOLATED,
2497 NULL, NULL, NULL, &type);
2501 DEBUG(10, ("%s does not exist, can create it\n", new_name));
2502 return NT_STATUS_OK;
2505 DEBUG(5, ("trying to create %s, exists as %s\n",
2506 new_name, sid_type_lookup(type)));
2508 if (type == SID_NAME_DOM_GRP) {
2509 return NT_STATUS_GROUP_EXISTS;
2511 if (type == SID_NAME_ALIAS) {
2512 return NT_STATUS_ALIAS_EXISTS;
2515 /* Yes, the default is NT_STATUS_USER_EXISTS */
2516 return NT_STATUS_USER_EXISTS;
2519 /*******************************************************************
2521 Create an account, can be either a normal user or a machine.
2522 This funcion will need to be updated for bdc/domain trusts.
2523 ********************************************************************/
2525 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
2526 SAMR_R_CREATE_USER *r_u)
2530 POLICY_HND dom_pol = q_u->domain_pol;
2531 uint16 acb_info = q_u->acb_info;
2532 POLICY_HND *user_pol = &r_u->user_pol;
2533 struct samr_info *info = NULL;
2538 /* check this, when giving away 'add computer to domain' privs */
2539 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
2540 BOOL can_add_account = False;
2542 DISP_INFO *disp_info = NULL;
2544 /* Get the domain SID stored in the domain policy */
2545 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
2547 return NT_STATUS_INVALID_HANDLE;
2549 nt_status = access_check_samr_function(acc_granted,
2550 SA_RIGHT_DOMAIN_CREATE_USER,
2551 "_samr_create_user");
2552 if (!NT_STATUS_IS_OK(nt_status)) {
2556 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
2557 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
2558 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
2559 this parameter is not an account type */
2560 return NT_STATUS_INVALID_PARAMETER;
2563 account = rpcstr_pull_unistr2_talloc(p->mem_ctx, &q_u->uni_name);
2564 if (account == NULL) {
2565 return NT_STATUS_NO_MEMORY;
2568 nt_status = can_create(p->mem_ctx, account);
2569 if (!NT_STATUS_IS_OK(nt_status)) {
2573 /* determine which user right we need to check based on the acb_info */
2575 if ( acb_info & ACB_WSTRUST )
2577 se_priv_copy( &se_rights, &se_machine_account );
2578 can_add_account = user_has_privileges(
2579 p->pipe_user.nt_user_token, &se_rights );
2581 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
2582 account for domain trusts and changes the ACB flags later */
2583 else if ( acb_info & ACB_NORMAL &&
2584 (account[strlen(account)-1] != '$') )
2586 se_priv_copy( &se_rights, &se_add_users );
2587 can_add_account = user_has_privileges(
2588 p->pipe_user.nt_user_token, &se_rights );
2590 else /* implicit assumption of a BDC or domain trust account here
2591 * (we already check the flags earlier) */
2593 if ( lp_enable_privileges() ) {
2594 /* only Domain Admins can add a BDC or domain trust */
2595 se_priv_copy( &se_rights, &se_priv_none );
2596 can_add_account = nt_token_check_domain_rid(
2597 p->pipe_user.nt_user_token,
2598 DOMAIN_GROUP_RID_ADMINS );
2602 DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2603 uidtoname(p->pipe_user.ut.uid),
2604 can_add_account ? "True":"False" ));
2606 /********** BEGIN Admin BLOCK **********/
2608 if ( can_add_account )
2611 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
2614 if ( can_add_account )
2617 /********** END Admin BLOCK **********/
2619 /* now check for failure */
2621 if ( !NT_STATUS_IS_OK(nt_status) )
2624 /* Get the user's SID */
2626 sid_compose(&sid, get_global_sam_sid(), r_u->user_rid);
2628 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
2629 &sid, SAMR_USR_RIGHTS_WRITE_PW);
2630 se_map_generic(&des_access, &usr_generic_mapping);
2632 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2633 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2634 &acc_granted, "_samr_create_user");
2636 if ( !NT_STATUS_IS_OK(nt_status) ) {
2640 /* associate the user's SID with the new handle. */
2641 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2642 return NT_STATUS_NO_MEMORY;
2647 info->acc_granted = acc_granted;
2649 /* get a (unique) handle. open a policy on it. */
2650 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2651 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2654 /* After a "set" ensure we have no cached display info. */
2655 force_flush_samr_cache(info->disp_info);
2657 r_u->access_granted = acc_granted;
2659 return NT_STATUS_OK;
2662 /*******************************************************************
2663 samr_reply_connect_anon
2664 ********************************************************************/
2666 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2668 struct samr_info *info = NULL;
2669 uint32 des_access = q_u->access_mask;
2673 if (!pipe_access_check(p)) {
2674 DEBUG(3, ("access denied to samr_connect_anon\n"));
2675 r_u->status = NT_STATUS_ACCESS_DENIED;
2679 /* set up the SAMR connect_anon response */
2681 r_u->status = NT_STATUS_OK;
2683 /* associate the user's SID with the new handle. */
2684 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2685 return NT_STATUS_NO_MEMORY;
2687 /* don't give away the farm but this is probably ok. The SA_RIGHT_SAM_ENUM_DOMAINS
2688 was observed from a win98 client trying to enumerate users (when configured
2689 user level access control on shares) --jerry */
2691 if (des_access == MAXIMUM_ALLOWED_ACCESS) {
2692 /* Map to max possible knowing we're filtered below. */
2693 des_access = GENERIC_ALL_ACCESS;
2696 se_map_generic( &des_access, &sam_generic_mapping );
2697 info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2699 info->status = q_u->unknown_0;
2701 /* get a (unique) handle. open a policy on it. */
2702 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2703 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2708 /*******************************************************************
2710 ********************************************************************/
2712 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2714 struct samr_info *info = NULL;
2715 SEC_DESC *psd = NULL;
2717 uint32 des_access = q_u->access_mask;
2722 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2726 if (!pipe_access_check(p)) {
2727 DEBUG(3, ("access denied to samr_connect\n"));
2728 r_u->status = NT_STATUS_ACCESS_DENIED;
2732 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2733 se_map_generic(&des_access, &sam_generic_mapping);
2735 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2736 NULL, 0, des_access, &acc_granted, "_samr_connect");
2738 if ( !NT_STATUS_IS_OK(nt_status) )
2741 r_u->status = NT_STATUS_OK;
2743 /* associate the user's SID and access granted with the new handle. */
2744 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2745 return NT_STATUS_NO_MEMORY;
2747 info->acc_granted = acc_granted;
2748 info->status = q_u->access_mask;
2750 /* get a (unique) handle. open a policy on it. */
2751 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2752 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2754 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2759 /*******************************************************************
2761 ********************************************************************/
2763 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2765 struct samr_info *info = NULL;
2766 SEC_DESC *psd = NULL;
2768 uint32 des_access = q_u->access_mask;
2773 DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2777 if (!pipe_access_check(p)) {
2778 DEBUG(3, ("access denied to samr_connect4\n"));
2779 r_u->status = NT_STATUS_ACCESS_DENIED;
2783 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2784 se_map_generic(&des_access, &sam_generic_mapping);
2786 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2787 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2789 if ( !NT_STATUS_IS_OK(nt_status) )
2792 r_u->status = NT_STATUS_OK;
2794 /* associate the user's SID and access granted with the new handle. */
2795 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2796 return NT_STATUS_NO_MEMORY;
2798 info->acc_granted = acc_granted;
2799 info->status = q_u->access_mask;
2801 /* get a (unique) handle. open a policy on it. */
2802 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2803 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2805 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2810 /*******************************************************************
2812 ********************************************************************/
2814 NTSTATUS _samr_connect5(pipes_struct *p, SAMR_Q_CONNECT5 *q_u, SAMR_R_CONNECT5 *r_u)
2816 struct samr_info *info = NULL;
2817 SEC_DESC *psd = NULL;
2819 uint32 des_access = q_u->access_mask;
2825 DEBUG(5,("_samr_connect5: %d\n", __LINE__));
2831 if (!pipe_access_check(p)) {
2832 DEBUG(3, ("access denied to samr_connect5\n"));
2833 r_u->status = NT_STATUS_ACCESS_DENIED;
2837 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2838 se_map_generic(&des_access, &sam_generic_mapping);
2840 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2841 NULL, 0, des_access, &acc_granted, "_samr_connect5");
2843 if ( !NT_STATUS_IS_OK(nt_status) )
2846 /* associate the user's SID and access granted with the new handle. */
2847 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2848 return NT_STATUS_NO_MEMORY;
2850 info->acc_granted = acc_granted;
2851 info->status = q_u->access_mask;
2853 /* get a (unique) handle. open a policy on it. */
2854 if (!create_policy_hnd(p, &pol, free_samr_info, (void *)info))
2855 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2857 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2859 init_samr_r_connect5(r_u, &pol, NT_STATUS_OK);
2864 /**********************************************************************
2865 api_samr_lookup_domain
2866 **********************************************************************/
2868 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2870 struct samr_info *info;
2871 fstring domain_name;
2874 r_u->status = NT_STATUS_OK;
2876 if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)(void *)&info))
2877 return NT_STATUS_INVALID_HANDLE;
2879 /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
2880 Reverted that change so we will work with RAS servers again */
2882 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
2883 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain")))
2888 rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2892 if (strequal(domain_name, builtin_domain_name())) {
2893 sid_copy(&sid, &global_sid_Builtin);
2895 if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2896 r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2900 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2902 init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2907 /******************************************************************
2908 makes a SAMR_R_ENUM_DOMAINS structure.
2909 ********************************************************************/
2911 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2912 UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2918 DEBUG(5, ("make_enum_domains\n"));
2921 *pp_uni_name = NULL;
2923 if (num_sam_entries == 0)
2926 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2927 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2929 if (sam == NULL || uni_name == NULL)
2932 for (i = 0; i < num_sam_entries; i++) {
2933 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2934 init_sam_entry(&sam[i], &uni_name[i], 0);
2938 *pp_uni_name = uni_name;
2943 /**********************************************************************
2944 api_samr_enum_domains
2945 **********************************************************************/
2947 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2949 struct samr_info *info;
2950 uint32 num_entries = 2;
2954 r_u->status = NT_STATUS_OK;
2956 if (!find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info))
2957 return NT_STATUS_INVALID_HANDLE;
2959 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2963 name = get_global_sam_name();
2965 fstrcpy(dom[0],name);
2967 fstrcpy(dom[1],"Builtin");
2969 if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2970 return NT_STATUS_NO_MEMORY;
2972 init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2977 /*******************************************************************
2979 ********************************************************************/
2981 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2984 POLICY_HND domain_pol = q_u->dom_pol;
2985 uint32 alias_rid = q_u->rid_alias;
2986 POLICY_HND *alias_pol = &r_u->pol;
2987 struct samr_info *info = NULL;
2988 SEC_DESC *psd = NULL;
2990 uint32 des_access = q_u->access_mask;
2995 r_u->status = NT_STATUS_OK;
2997 /* find the domain policy and get the SID / access bits stored in the domain policy */
2999 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
3000 return NT_STATUS_INVALID_HANDLE;
3002 status = access_check_samr_function(acc_granted,
3003 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
3005 if ( !NT_STATUS_IS_OK(status) )
3008 /* append the alias' RID to it */
3010 if (!sid_append_rid(&sid, alias_rid))
3011 return NT_STATUS_NO_SUCH_ALIAS;
3013 /*check if access can be granted as requested by client. */
3015 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
3016 se_map_generic(&des_access,&ali_generic_mapping);
3018 se_priv_copy( &se_rights, &se_add_users );
3021 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3022 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
3023 &acc_granted, "_samr_open_alias");
3025 if ( !NT_STATUS_IS_OK(status) )
3029 /* Check we actually have the requested alias */
3030 enum lsa_SidType type;
3035 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
3038 if (!result || (type != SID_NAME_ALIAS)) {
3039 return NT_STATUS_NO_SUCH_ALIAS;
3042 /* make sure there is a mapping */
3044 if ( !sid_to_gid( &sid, &gid ) ) {
3045 return NT_STATUS_NO_SUCH_ALIAS;
3050 /* associate the alias SID with the new handle. */
3051 if ((info = get_samr_info_by_sid(&sid)) == NULL)
3052 return NT_STATUS_NO_MEMORY;
3054 info->acc_granted = acc_granted;
3056 /* get a (unique) handle. open a policy on it. */
3057 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
3058 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3063 /*******************************************************************
3065 ********************************************************************/
3066 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
3067 const SAM_USER_INFO_7 *id7, struct samu *pwd)
3073 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3075 return NT_STATUS_ACCESS_DENIED;
3078 if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
3079 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3081 return NT_STATUS_ACCESS_DENIED;
3084 /* check to see if the new username already exists. Note: we can't
3085 reliably lock all backends, so there is potentially the
3086 possibility that a user can be created in between this check and
3087 the rename. The rename should fail, but may not get the
3088 exact same failure status code. I think this is small enough
3089 of a window for this type of operation and the results are
3090 simply that the rename fails with a slightly different status
3091 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3093 rc = can_create(mem_ctx, new_name);
3094 if (!NT_STATUS_IS_OK(rc)) {
3098 rc = pdb_rename_sam_account(pwd, new_name);
3104 /*******************************************************************
3106 ********************************************************************/
3108 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, struct samu *pwd)
3111 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3116 /* FIX ME: check if the value is really changed --metze */
3117 if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
3122 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3132 /*******************************************************************
3134 ********************************************************************/
3136 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, struct samu *pwd)
3140 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3145 if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
3149 if (!pdb_set_nt_passwd (pwd, id18->nt_pwd, PDB_CHANGED)) {
3153 if (!pdb_set_pass_last_set_time (pwd, time(NULL), PDB_CHANGED)) {
3158 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3167 /*******************************************************************
3169 ********************************************************************/
3171 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, struct samu *pwd)
3174 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3178 copy_id20_to_sam_passwd(pwd, id20);
3180 /* write the change out */
3181 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3190 /*******************************************************************
3192 ********************************************************************/
3194 static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
3201 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3202 return NT_STATUS_INVALID_PARAMETER;
3205 /* we need to separately check for an account rename first */
3207 if (rpcstr_pull(new_name, id21->uni_user_name.buffer,
3208 sizeof(new_name), id21->uni_user_name.uni_str_len*2, 0)
3209 && (!strequal(new_name, pdb_get_username(pwd))))
3212 /* check to see if the new username already exists. Note: we can't
3213 reliably lock all backends, so there is potentially the
3214 possibility that a user can be created in between this check and
3215 the rename. The rename should fail, but may not get the
3216 exact same failure status code. I think this is small enough
3217 of a window for this type of operation and the results are
3218 simply that the rename fails with a slightly different status
3219 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3221 status = can_create(mem_ctx, new_name);
3222 if (!NT_STATUS_IS_OK(status)) {
3226 status = pdb_rename_sam_account(pwd, new_name);
3228 if (!NT_STATUS_IS_OK(status)) {
3229 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3230 nt_errstr(status)));
3235 /* set the new username so that later
3236 functions can work on the new account */
3237 pdb_set_username(pwd, new_name, PDB_SET);
3240 copy_id21_to_sam_passwd(pwd, id21);
3243 * The funny part about the previous two calls is
3244 * that pwd still has the password hashes from the
3245 * passdb entry. These have not been updated from
3246 * id21. I don't know if they need to be set. --jerry
3249 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3250 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3251 if ( !NT_STATUS_IS_OK(status) ) {
3256 /* Don't worry about writing out the user account since the
3257 primary group SID is generated solely from the user's Unix
3260 /* write the change out */
3261 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3268 return NT_STATUS_OK;
3271 /*******************************************************************
3273 ********************************************************************/
3275 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx, SAM_USER_INFO_23 *id23,
3278 pstring plaintext_buf;
3284 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3285 return NT_STATUS_INVALID_PARAMETER;
3288 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3289 pdb_get_username(pwd)));
3291 acct_ctrl = pdb_get_acct_ctrl(pwd);
3293 if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3295 return NT_STATUS_INVALID_PARAMETER;
3298 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3300 return NT_STATUS_ACCESS_DENIED;
3303 copy_id23_to_sam_passwd(pwd, id23);
3305 /* if it's a trust account, don't update /etc/passwd */
3306 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3307 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3308 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3309 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
3311 /* update the UNIX password */
3312 if (lp_unix_password_sync() ) {
3313 struct passwd *passwd;
3314 if (pdb_get_username(pwd) == NULL) {
3315 DEBUG(1, ("chgpasswd: User without name???\n"));
3317 return NT_STATUS_ACCESS_DENIED;
3320 if ((passwd = Get_Pwnam(pdb_get_username(pwd))) == NULL) {
3321 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3324 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3326 return NT_STATUS_ACCESS_DENIED;
3331 ZERO_STRUCT(plaintext_buf);
3333 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
3334 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
3340 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3347 return NT_STATUS_OK;
3350 /*******************************************************************
3352 ********************************************************************/
3354 static BOOL set_user_info_pw(uint8 *pass, struct samu *pwd)
3357 pstring plaintext_buf;
3359 time_t last_set_time;
3360 enum pdb_value_state last_set_state;
3362 DEBUG(5, ("Attempting administrator password change for user %s\n",
3363 pdb_get_username(pwd)));
3365 acct_ctrl = pdb_get_acct_ctrl(pwd);
3366 /* we need to know if it's expired, because this is an admin change, not a
3367 user change, so it's still expired when we're done */
3368 last_set_state = pdb_get_init_flags(pwd, PDB_PASSLASTSET);
3369 last_set_time = pdb_get_pass_last_set_time(pwd);
3371 ZERO_STRUCT(plaintext_buf);
3373 if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3378 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3383 /* if it's a trust account, don't update /etc/passwd */
3384 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3385 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3386 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3387 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3389 /* update the UNIX password */
3390 if (lp_unix_password_sync()) {
3391 struct passwd *passwd;
3393 if (pdb_get_username(pwd) == NULL) {
3394 DEBUG(1, ("chgpasswd: User without name???\n"));
3399 if ((passwd = Get_Pwnam(pdb_get_username(pwd))) == NULL) {
3400 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3403 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3410 ZERO_STRUCT(plaintext_buf);
3412 /* restore last set time as this is an admin change, not a user pw change */
3413 pdb_set_pass_last_set_time (pwd, last_set_time, last_set_state);
3415 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
3417 /* update the SAMBA password */
3418 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3428 /*******************************************************************
3430 ********************************************************************/
3432 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx, SAM_USER_INFO_25 *id25,
3438 DEBUG(5, ("set_user_info_25: NULL id25\n"));
3439 return NT_STATUS_INVALID_PARAMETER;
3442 copy_id25_to_sam_passwd(pwd, id25);
3444 /* write the change out */
3445 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3451 * We need to "pdb_update_sam_account" before the unix primary group
3452 * is set, because the idealx scripts would also change the
3453 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
3454 * the delete explicit / add explicit, which would then fail to find
3455 * the previous primaryGroupSid value.
3458 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3459 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3460 if ( !NT_STATUS_IS_OK(status) ) {
3465 /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
3468 return NT_STATUS_OK;
3471 /*******************************************************************
3472 samr_reply_set_userinfo
3473 ********************************************************************/
3475 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
3477 struct samu *pwd = NULL;
3479 POLICY_HND *pol = &q_u->pol;
3480 uint16 switch_value = q_u->switch_value;
3481 SAM_USERINFO_CTR *ctr = q_u->ctr;
3483 uint32 acc_required;
3485 BOOL has_enough_rights = False;
3487 DISP_INFO *disp_info = NULL;
3489 DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
3491 r_u->status = NT_STATUS_OK;
3493 /* find the policy handle. open a policy on it. */
3494 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3495 return NT_STATUS_INVALID_HANDLE;
3497 /* This is tricky. A WinXP domain join sets
3498 (SA_RIGHT_USER_SET_PASSWORD|SA_RIGHT_USER_SET_ATTRIBUTES|SA_RIGHT_USER_ACCT_FLAGS_EXPIRY)
3499 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
3500 standard Win32 API calls just ask for SA_RIGHT_USER_SET_PASSWORD in the SamrOpenUser().
3501 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
3502 we'll use the set from the WinXP join as the basis. */
3504 switch (switch_value) {
3509 acc_required = SA_RIGHT_USER_SET_PASSWORD;
3512 acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3516 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
3520 DEBUG(5, ("_samr_set_userinfo: sid:%s, level:%d\n", sid_string_static(&sid), switch_value));
3523 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
3524 return NT_STATUS_INVALID_INFO_CLASS;
3527 if ( !(pwd = samu_new( NULL )) ) {
3528 return NT_STATUS_NO_MEMORY;
3532 ret = pdb_getsampwsid(pwd, &sid);
3537 return NT_STATUS_NO_SUCH_USER;
3540 /* deal with machine password changes differently from userinfo changes */
3541 /* check to see if we have the sufficient rights */
3543 acb_info = pdb_get_acct_ctrl(pwd);
3544 if ( acb_info & ACB_WSTRUST )
3545 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3546 else if ( acb_info & ACB_NORMAL )
3547 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3548 else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3549 if ( lp_enable_privileges() )
3550 has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3553 DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3554 uidtoname(p->pipe_user.ut.uid),
3555 has_enough_rights ? "" : " not"));
3557 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3559 if ( has_enough_rights )
3562 /* ok! user info levels (lots: see MSDEV help), off we go... */
3564 switch (switch_value) {
3566 if (!set_user_info_18(ctr->info.id18, pwd))
3567 r_u->status = NT_STATUS_ACCESS_DENIED;
3571 if (!p->session_key.length) {
3572 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3574 SamOEMhashBlob(ctr->info.id24->pass, 516, &p->session_key);
3576 dump_data(100, ctr->info.id24->pass, 516);
3578 if (!set_user_info_pw(ctr->info.id24->pass, pwd))
3579 r_u->status = NT_STATUS_ACCESS_DENIED;
3583 if (!p->session_key.length) {
3584 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3586 encode_or_decode_arc4_passwd_buffer(ctr->info.id25->pass, &p->session_key);
3588 dump_data(100, ctr->info.id25->pass, 532);
3590 r_u->status = set_user_info_25(p->mem_ctx,
3591 ctr->info.id25, pwd);
3592 if (!NT_STATUS_IS_OK(r_u->status)) {
3595 if (!set_user_info_pw(ctr->info.id25->pass, pwd))
3596 r_u->status = NT_STATUS_ACCESS_DENIED;
3600 if (!p->session_key.length) {
3601 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3603 encode_or_decode_arc4_passwd_buffer(ctr->info.id26->pass, &p->session_key);
3605 dump_data(100, ctr->info.id26->pass, 516);
3607 if (!set_user_info_pw(ctr->info.id26->pass, pwd))
3608 r_u->status = NT_STATUS_ACCESS_DENIED;
3612 if (!p->session_key.length) {
3613 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3615 SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
3617 dump_data(100, ctr->info.id23->pass, 516);
3619 r_u->status = set_user_info_23(p->mem_ctx,
3620 ctr->info.id23, pwd);
3624 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3629 if ( has_enough_rights )
3632 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3634 if (NT_STATUS_IS_OK(r_u->status)) {
3635 force_flush_samr_cache(disp_info);
3641 /*******************************************************************
3642 samr_reply_set_userinfo2
3643 ********************************************************************/
3645 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
3647 struct samu *pwd = NULL;
3649 SAM_USERINFO_CTR *ctr = q_u->ctr;
3650 POLICY_HND *pol = &q_u->pol;
3651 uint16 switch_value = q_u->switch_value;
3653 uint32 acc_required;
3655 BOOL has_enough_rights = False;
3657 DISP_INFO *disp_info = NULL;
3659 DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
3661 r_u->status = NT_STATUS_OK;
3663 /* find the policy handle. open a policy on it. */
3664 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3665 return NT_STATUS_INVALID_HANDLE;
3668 #if 0 /* this really should be applied on a per info level basis --jerry */
3670 /* observed when joining XP client to Samba domain */
3671 acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3673 acc_required = SA_RIGHT_USER_SET_ATTRIBUTES;
3676 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
3680 DEBUG(5, ("samr_reply_set_userinfo2: sid:%s\n", sid_string_static(&sid)));
3683 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
3684 return NT_STATUS_INVALID_INFO_CLASS;
3687 switch_value=ctr->switch_value;
3689 if ( !(pwd = samu_new( NULL )) ) {
3690 return NT_STATUS_NO_MEMORY;
3694 ret = pdb_getsampwsid(pwd, &sid);
3699 return NT_STATUS_NO_SUCH_USER;
3702 acb_info = pdb_get_acct_ctrl(pwd);
3703 if ( acb_info & ACB_WSTRUST )
3704 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3705 else if ( acb_info & ACB_NORMAL )
3706 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3707 else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3708 if ( lp_enable_privileges() )
3709 has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3712 DEBUG(5, ("_samr_set_userinfo2: %s does%s possess sufficient rights\n",
3713 uidtoname(p->pipe_user.ut.uid),
3714 has_enough_rights ? "" : " not"));
3716 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3718 if ( has_enough_rights )
3721 /* ok! user info levels (lots: see MSDEV help), off we go... */
3723 switch (switch_value) {
3725 r_u->status = set_user_info_7(p->mem_ctx,
3726 ctr->info.id7, pwd);
3729 if (!set_user_info_16(ctr->info.id16, pwd))
3730 r_u->status = NT_STATUS_ACCESS_DENIED;
3733 /* Used by AS/U JRA. */
3734 if (!set_user_info_18(ctr->info.id18, pwd))
3735 r_u->status = NT_STATUS_ACCESS_DENIED;
3738 if (!set_user_info_20(ctr->info.id20, pwd))
3739 r_u->status = NT_STATUS_ACCESS_DENIED;
3742 r_u->status = set_user_info_21(p->mem_ctx,
3743 ctr->info.id21, pwd);
3746 if (!p->session_key.length) {
3747 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3749 SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
3751 dump_data(100, ctr->info.id23->pass, 516);
3753 r_u->status = set_user_info_23(p->mem_ctx,
3754 ctr->info.id23, pwd);
3757 if (!p->session_key.length) {
3758 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3760 encode_or_decode_arc4_passwd_buffer(ctr->info.id26->pass, &p->session_key);
3762 dump_data(100, ctr->info.id26->pass, 516);
3764 if (!set_user_info_pw(ctr->info.id26->pass, pwd))
3765 r_u->status = NT_STATUS_ACCESS_DENIED;
3768 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3771 if ( has_enough_rights )
3774 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3776 if (NT_STATUS_IS_OK(r_u->status)) {
3777 force_flush_samr_cache(disp_info);
3783 /*********************************************************************
3784 _samr_query_aliasmem
3785 *********************************************************************/
3787 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
3789 size_t num_alias_rids;
3791 struct samr_info *info = NULL;
3799 r_u->status = NT_STATUS_OK;
3801 DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
3803 /* find the policy handle. open a policy on it. */
3804 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
3805 return NT_STATUS_INVALID_HANDLE;
3807 ntstatus1 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM, "_samr_query_useraliases");
3808 ntstatus2 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_query_useraliases");
3810 if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
3811 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
3812 !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
3813 return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
3817 if (!sid_check_is_domain(&info->sid) &&
3818 !sid_check_is_builtin(&info->sid))
3819 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3821 if (q_u->num_sids1) {
3822 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, q_u->num_sids1);
3824 if (members == NULL)
3825 return NT_STATUS_NO_MEMORY;
3830 for (i=0; i<q_u->num_sids1; i++)
3831 sid_copy(&members[i], &q_u->sid[i].sid);
3837 ntstatus1 = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
3839 &alias_rids, &num_alias_rids);
3842 if (!NT_STATUS_IS_OK(ntstatus1)) {
3846 init_samr_r_query_useraliases(r_u, num_alias_rids, alias_rids,
3848 return NT_STATUS_OK;
3851 /*********************************************************************
3852 _samr_query_aliasmem
3853 *********************************************************************/
3855 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
3859 size_t num_sids = 0;
3867 /* find the policy handle. open a policy on it. */
3868 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, NULL))
3869 return NT_STATUS_INVALID_HANDLE;
3871 if (!NT_STATUS_IS_OK(r_u->status =
3872 access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_GET_MEMBERS, "_samr_query_aliasmem"))) {
3876 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3879 status = pdb_enum_aliasmem(&alias_sid, &sids, &num_sids);
3882 if (!NT_STATUS_IS_OK(status)) {
3887 sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_sids);
3890 return NT_STATUS_NO_MEMORY;
3896 for (i = 0; i < num_sids; i++) {
3897 init_dom_sid2(&sid[i], &sids[i]);
3900 init_samr_r_query_aliasmem(r_u, num_sids, sid, NT_STATUS_OK);
3904 return NT_STATUS_OK;
3907 /*********************************************************************
3908 _samr_query_groupmem
3909 *********************************************************************/
3911 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
3914 fstring group_sid_str;
3915 size_t i, num_members;
3924 /* find the policy handle. open a policy on it. */
3925 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, NULL))
3926 return NT_STATUS_INVALID_HANDLE;
3928 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_GET_MEMBERS, "_samr_query_groupmem"))) {
3932 sid_to_string(group_sid_str, &group_sid);
3933 DEBUG(10, ("sid is %s\n", group_sid_str));
3935 if (!sid_check_is_in_our_domain(&group_sid)) {
3936 DEBUG(3, ("sid %s is not in our domain\n", group_sid_str));
3937 return NT_STATUS_NO_SUCH_GROUP;
3940 DEBUG(10, ("lookup on Domain SID\n"));
3943 result = pdb_enum_group_members(p->mem_ctx, &group_sid,
3944 &rid, &num_members);
3947 if (!NT_STATUS_IS_OK(result))
3951 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
3953 return NT_STATUS_NO_MEMORY;
3959 for (i=0; i<num_members; i++)
3960 attr[i] = SID_NAME_USER;
3962 init_samr_r_query_groupmem(r_u, num_members, rid, attr, NT_STATUS_OK);
3964 return NT_STATUS_OK;
3967 /*********************************************************************
3969 *********************************************************************/
3971 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
3976 BOOL can_add_accounts;
3978 DISP_INFO *disp_info = NULL;
3980 /* Find the policy handle. Open a policy on it. */
3981 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info))
3982 return NT_STATUS_INVALID_HANDLE;
3984 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_ADD_MEMBER, "_samr_add_aliasmem"))) {
3988 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3990 se_priv_copy( &se_rights, &se_add_users );
3991 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3993 /******** BEGIN SeAddUsers BLOCK *********/
3995 if ( can_add_accounts )
3998 ret = pdb_add_aliasmem(&alias_sid, &q_u->sid.sid);
4000 if ( can_add_accounts )
4003 /******** END SeAddUsers BLOCK *********/
4005 if (NT_STATUS_IS_OK(ret)) {
4006 force_flush_samr_cache(disp_info);
4012 /*********************************************************************
4014 *********************************************************************/
4016 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
4021 BOOL can_add_accounts;
4023 DISP_INFO *disp_info = NULL;
4025 /* Find the policy handle. Open a policy on it. */
4026 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info))
4027 return NT_STATUS_INVALID_HANDLE;
4029 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_REMOVE_MEMBER, "_samr_del_aliasmem"))) {
4033 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
4034 sid_string_static(&alias_sid)));
4036 se_priv_copy( &se_rights, &se_add_users );
4037 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4039 /******** BEGIN SeAddUsers BLOCK *********/
4041 if ( can_add_accounts )
4044 ret = pdb_del_aliasmem(&alias_sid, &q_u->sid.sid);
4046 if ( can_add_accounts )
4049 /******** END SeAddUsers BLOCK *********/
4051 if (NT_STATUS_IS_OK(ret)) {
4052 force_flush_samr_cache(disp_info);
4058 /*********************************************************************
4060 *********************************************************************/
4062 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
4068 BOOL can_add_accounts;
4069 DISP_INFO *disp_info = NULL;
4071 /* Find the policy handle. Open a policy on it. */
4072 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
4073 return NT_STATUS_INVALID_HANDLE;
4075 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_ADD_MEMBER, "_samr_add_groupmem"))) {
4079 DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
4081 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4083 return NT_STATUS_INVALID_HANDLE;
4086 se_priv_copy( &se_rights, &se_add_users );
4087 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4089 /******** BEGIN SeAddUsers BLOCK *********/
4091 if ( can_add_accounts )
4094 r_u->status = pdb_add_groupmem(p->mem_ctx, group_rid, q_u->rid);
4096 if ( can_add_accounts )
4099 /******** END SeAddUsers BLOCK *********/
4101 force_flush_samr_cache(disp_info);
4106 /*********************************************************************
4108 *********************************************************************/
4110 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
4116 BOOL can_add_accounts;
4117 DISP_INFO *disp_info = NULL;
4120 * delete the group member named q_u->rid
4121 * who is a member of the sid associated with the handle
4122 * the rid is a user's rid as the group is a domain group.
4125 /* Find the policy handle. Open a policy on it. */
4126 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
4127 return NT_STATUS_INVALID_HANDLE;
4129 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
4133 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4135 return NT_STATUS_INVALID_HANDLE;
4138 se_priv_copy( &se_rights, &se_add_users );
4139 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4141 /******** BEGIN SeAddUsers BLOCK *********/
4143 if ( can_add_accounts )
4146 r_u->status = pdb_del_groupmem(p->mem_ctx, group_rid, q_u->rid);
4148 if ( can_add_accounts )
4151 /******** END SeAddUsers BLOCK *********/
4153 force_flush_samr_cache(disp_info);
4158 /*********************************************************************
4159 _samr_delete_dom_user
4160 *********************************************************************/
4162 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
4165 struct samu *sam_pass=NULL;
4167 BOOL can_add_accounts;
4169 DISP_INFO *disp_info = NULL;
4172 DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
4174 /* Find the policy handle. Open a policy on it. */
4175 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid, &acc_granted, &disp_info))
4176 return NT_STATUS_INVALID_HANDLE;
4178 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_user"))) {
4182 if (!sid_check_is_in_our_domain(&user_sid))
4183 return NT_STATUS_CANNOT_DELETE;
4185 /* check if the user exists before trying to delete */
4186 if ( !(sam_pass = samu_new( NULL )) ) {
4187 return NT_STATUS_NO_MEMORY;
4191 ret = pdb_getsampwsid(sam_pass, &user_sid);
4195 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n",
4196 sid_string_static(&user_sid)));
4197 TALLOC_FREE(sam_pass);
4198 return NT_STATUS_NO_SUCH_USER;
4201 acb_info = pdb_get_acct_ctrl(sam_pass);
4203 /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
4204 if ( acb_info & ACB_WSTRUST ) {
4205 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account );
4207 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4210 /******** BEGIN SeAddUsers BLOCK *********/
4212 if ( can_add_accounts )
4215 r_u->status = pdb_delete_user(p->mem_ctx, sam_pass);
4217 if ( can_add_accounts )
4220 /******** END SeAddUsers BLOCK *********/
4222 if ( !NT_STATUS_IS_OK(r_u->status) ) {
4223 DEBUG(5,("_samr_delete_dom_user: Failed to delete entry for "
4224 "user %s: %s.\n", pdb_get_username(sam_pass),
4225 nt_errstr(r_u->status)));
4226 TALLOC_FREE(sam_pass);
4231 TALLOC_FREE(sam_pass);
4233 if (!close_policy_hnd(p, &q_u->user_pol))
4234 return NT_STATUS_OBJECT_NAME_INVALID;
4236 force_flush_samr_cache(disp_info);
4238 return NT_STATUS_OK;
4241 /*********************************************************************
4242 _samr_delete_dom_group
4243 *********************************************************************/
4245 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
4251 BOOL can_add_accounts;
4252 DISP_INFO *disp_info = NULL;
4254 DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
4256 /* Find the policy handle. Open a policy on it. */
4257 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, &disp_info))
4258 return NT_STATUS_INVALID_HANDLE;
4260 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
4264 DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
4266 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4268 return NT_STATUS_NO_SUCH_GROUP;
4271 se_priv_copy( &se_rights, &se_add_users );
4272 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4274 /******** BEGIN SeAddUsers BLOCK *********/
4276 if ( can_add_accounts )
4279 r_u->status = pdb_delete_dom_group(p->mem_ctx, group_rid);
4281 if ( can_add_accounts )
4284 /******** END SeAddUsers BLOCK *********/
4286 if ( !NT_STATUS_IS_OK(r_u->status) ) {
4287 DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping "
4288 "entry for group %s: %s\n",
4289 sid_string_static(&group_sid),
4290 nt_errstr(r_u->status)));
4294 if (!close_policy_hnd(p, &q_u->group_pol))
4295 return NT_STATUS_OBJECT_NAME_INVALID;
4297 force_flush_samr_cache(disp_info);
4299 return NT_STATUS_OK;
4302 /*********************************************************************
4303 _samr_delete_dom_alias
4304 *********************************************************************/
4306 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
4311 BOOL can_add_accounts;
4313 DISP_INFO *disp_info = NULL;
4315 DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
4317 /* Find the policy handle. Open a policy on it. */
4318 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info))
4319 return NT_STATUS_INVALID_HANDLE;
4321 /* copy the handle to the outgoing reply */
4323 memcpy( &r_u->pol, &q_u->alias_pol, sizeof(r_u->pol) );
4325 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
4329 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
4331 /* Don't let Windows delete builtin groups */
4333 if ( sid_check_is_in_builtin( &alias_sid ) ) {
4334 return NT_STATUS_SPECIAL_ACCOUNT;
4337 if (!sid_check_is_in_our_domain(&alias_sid))
4338 return NT_STATUS_NO_SUCH_ALIAS;
4340 DEBUG(10, ("lookup on Local SID\n"));
4342 se_priv_copy( &se_rights, &se_add_users );
4343 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4345 /******** BEGIN SeAddUsers BLOCK *********/
4347 if ( can_add_accounts )
4350 /* Have passdb delete the alias */
4351 status = pdb_delete_alias(&alias_sid);
4353 if ( can_add_accounts )
4356 /******** END SeAddUsers BLOCK *********/
4358 if ( !NT_STATUS_IS_OK(status))
4361 if (!close_policy_hnd(p, &q_u->alias_pol))
4362 return NT_STATUS_OBJECT_NAME_INVALID;
4364 force_flush_samr_cache(disp_info);
4366 return NT_STATUS_OK;
4369 /*********************************************************************
4370 _samr_create_dom_group
4371 *********************************************************************/
4373 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
4378 struct samr_info *info;
4381 BOOL can_add_accounts;
4382 DISP_INFO *disp_info = NULL;
4384 /* Find the policy handle. Open a policy on it. */
4385 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid, &acc_granted, &disp_info))
4386 return NT_STATUS_INVALID_HANDLE;
4388 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_GROUP, "_samr_create_dom_group"))) {
4392 if (!sid_equal(&dom_sid, get_global_sam_sid()))
4393 return NT_STATUS_ACCESS_DENIED;
4395 name = rpcstr_pull_unistr2_talloc(p->mem_ctx, &q_u->uni_acct_desc);
4397 return NT_STATUS_NO_MEMORY;
4400 r_u->status = can_create(p->mem_ctx, name);
4401 if (!NT_STATUS_IS_OK(r_u->status)) {
4405 se_priv_copy( &se_rights, &se_add_users );
4406 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4408 /******** BEGIN SeAddUsers BLOCK *********/
4410 if ( can_add_accounts )
4413 /* check that we successfully create the UNIX group */
4415 r_u->status = pdb_create_dom_group(p->mem_ctx, name, &r_u->rid);
4417 if ( can_add_accounts )
4420 /******** END SeAddUsers BLOCK *********/
4422 /* check if we should bail out here */
4424 if ( !NT_STATUS_IS_OK(r_u->status) )
4427 sid_compose(&info_sid, get_global_sam_sid(), r_u->rid);
4429 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4430 return NT_STATUS_NO_MEMORY;
4432 /* they created it; let the user do what he wants with it */
4434 info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
4436 /* get a (unique) handle. open a policy on it. */
4437 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4438 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4440 force_flush_samr_cache(disp_info);
4442 return NT_STATUS_OK;
4445 /*********************************************************************
4446 _samr_create_dom_alias
4447 *********************************************************************/
4449 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
4454 struct samr_info *info;
4459 BOOL can_add_accounts;
4460 DISP_INFO *disp_info = NULL;
4462 /* Find the policy handle. Open a policy on it. */
4463 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid, &acc_granted, &disp_info))
4464 return NT_STATUS_INVALID_HANDLE;
4466 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_ALIAS, "_samr_create_alias"))) {
4470 if (!sid_equal(&dom_sid, get_global_sam_sid()))
4471 return NT_STATUS_ACCESS_DENIED;
4473 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name));
4475 se_priv_copy( &se_rights, &se_add_users );
4476 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4478 result = can_create(p->mem_ctx, name);
4479 if (!NT_STATUS_IS_OK(result)) {
4483 /******** BEGIN SeAddUsers BLOCK *********/
4485 if ( can_add_accounts )
4488 /* Have passdb create the alias */
4489 result = pdb_create_alias(name, &r_u->rid);
4491 if ( can_add_accounts )
4494 /******** END SeAddUsers BLOCK *********/
4496 if (!NT_STATUS_IS_OK(result)) {
4497 DEBUG(10, ("pdb_create_alias failed: %s\n",
4498 nt_errstr(result)));
4502 sid_copy(&info_sid, get_global_sam_sid());
4503 sid_append_rid(&info_sid, r_u->rid);
4505 if (!sid_to_gid(&info_sid, &gid)) {
4506 DEBUG(10, ("Could not find alias just created\n"));
4507 return NT_STATUS_ACCESS_DENIED;
4510 /* check if the group has been successfully created */
4511 if ( getgrgid(gid) == NULL ) {
4512 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
4514 return NT_STATUS_ACCESS_DENIED;
4517 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4518 return NT_STATUS_NO_MEMORY;
4520 /* they created it; let the user do what he wants with it */
4522 info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
4524 /* get a (unique) handle. open a policy on it. */
4525 if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
4526 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4528 force_flush_samr_cache(disp_info);
4530 return NT_STATUS_OK;
4533 /*********************************************************************
4534 _samr_query_groupinfo
4536 sends the name/comment pair of a domain group
4537 level 1 send also the number of users of that group
4538 *********************************************************************/
4540 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
4544 GROUP_INFO_CTR *ctr;
4548 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, NULL))
4549 return NT_STATUS_INVALID_HANDLE;
4551 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_LOOKUP_INFO, "_samr_query_groupinfo"))) {
4556 ret = get_domain_group_from_sid(group_sid, &map);
4559 return NT_STATUS_INVALID_HANDLE;
4561 ctr=TALLOC_ZERO_P(p->mem_ctx, GROUP_INFO_CTR);
4563 return NT_STATUS_NO_MEMORY;
4565 switch (q_u->switch_level) {
4570 ctr->switch_value1 = 1;
4573 r_u->status = pdb_enum_group_members(
4574 p->mem_ctx, &group_sid, &members, &num_members);
4577 if (!NT_STATUS_IS_OK(r_u->status)) {
4581 init_samr_group_info1(&ctr->group.info1, map.nt_name,
4582 map.comment, num_members);
4586 ctr->switch_value1 = 2;
4587 init_samr_group_info2(&ctr->group.info2, map.nt_name);
4590 ctr->switch_value1 = 3;
4591 init_samr_group_info3(&ctr->group.info3);
4594 ctr->switch_value1 = 4;
4595 init_samr_group_info4(&ctr->group.info4, map.comment);
4603 ctr->switch_value1 = 5;
4607 r_u->status = pdb_enum_group_members(
4608 p->mem_ctx, &group_sid, &members, &num_members);
4611 if (!NT_STATUS_IS_OK(r_u->status)) {
4615 init_samr_group_info5(&ctr->group.info5, map.nt_name,
4616 map.comment, 0 /* num_members */); /* in w2k3 this is always 0 */
4620 return NT_STATUS_INVALID_INFO_CLASS;
4623 init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
4625 return NT_STATUS_OK;
4628 /*********************************************************************
4631 update a domain group's comment.
4632 *********************************************************************/
4634 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
4638 GROUP_INFO_CTR *ctr;
4642 BOOL can_mod_accounts;
4643 DISP_INFO *disp_info = NULL;
4645 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
4646 return NT_STATUS_INVALID_HANDLE;
4648 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_SET_INFO, "_samr_set_groupinfo"))) {
4653 result = get_domain_group_from_sid(group_sid, &map);
4656 return NT_STATUS_NO_SUCH_GROUP;
4660 switch (ctr->switch_value1) {
4662 unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment));
4665 unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment));
4668 return NT_STATUS_INVALID_INFO_CLASS;
4671 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4673 /******** BEGIN SeAddUsers BLOCK *********/
4675 if ( can_mod_accounts )
4678 ret = pdb_update_group_mapping_entry(&map);
4680 if ( can_mod_accounts )
4683 /******** End SeAddUsers BLOCK *********/
4685 if (NT_STATUS_IS_OK(ret)) {
4686 force_flush_samr_cache(disp_info);
4692 /*********************************************************************
4695 update an alias's comment.
4696 *********************************************************************/
4698 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
4701 struct acct_info info;
4702 ALIAS_INFO_CTR *ctr;
4704 BOOL can_mod_accounts;
4706 DISP_INFO *disp_info = NULL;
4708 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid, &acc_granted, &disp_info))
4709 return NT_STATUS_INVALID_HANDLE;
4711 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_SET_INFO, "_samr_set_aliasinfo"))) {
4717 /* get the current group information */
4720 status = pdb_get_aliasinfo( &group_sid, &info );
4723 if ( !NT_STATUS_IS_OK(status))
4726 switch (ctr->level) {
4729 fstring group_name, acct_name;
4731 /* We currently do not support renaming groups in the
4732 the BUILTIN domain. Refer to util_builtin.c to understand
4733 why. The eventually needs to be fixed to be like Windows
4734 where you can rename builtin groups, just not delete them */
4736 if ( sid_check_is_in_builtin( &group_sid ) ) {
4737 return NT_STATUS_SPECIAL_ACCOUNT;
4740 /* There has to be a valid name (and it has to be different) */
4742 if ( !ctr->alias.info2.name.string )
4743 return NT_STATUS_INVALID_PARAMETER;
4745 unistr2_to_ascii( acct_name, ctr->alias.info2.name.string,
4748 /* If the name is the same just reply "ok". Yes this
4749 doesn't allow you to change the case of a group name. */
4751 if ( strequal( acct_name, info.acct_name ) )
4752 return NT_STATUS_OK;
4754 fstrcpy( info.acct_name, acct_name );
4756 /* make sure the name doesn't already exist as a user
4759 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
4760 status = can_create( p->mem_ctx, group_name );
4761 if ( !NT_STATUS_IS_OK( status ) )
4766 if ( ctr->alias.info3.description.string ) {
4767 unistr2_to_ascii( info.acct_desc,
4768 ctr->alias.info3.description.string,
4769 sizeof(info.acct_desc));
4772 fstrcpy( info.acct_desc, "" );
4775 return NT_STATUS_INVALID_INFO_CLASS;
4778 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4780 /******** BEGIN SeAddUsers BLOCK *********/
4782 if ( can_mod_accounts )
4785 status = pdb_set_aliasinfo( &group_sid, &info );
4787 if ( can_mod_accounts )
4790 /******** End SeAddUsers BLOCK *********/
4792 if (NT_STATUS_IS_OK(status))
4793 force_flush_samr_cache(disp_info);
4798 /*********************************************************************
4799 _samr_get_dom_pwinfo
4800 *********************************************************************/
4802 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
4804 /* Perform access check. Since this rpc does not require a
4805 policy handle it will not be caught by the access checks on
4806 SAMR_CONNECT or SAMR_CONNECT_ANON. */
4808 if (!pipe_access_check(p)) {
4809 DEBUG(3, ("access denied to samr_get_dom_pwinfo\n"));
4810 r_u->status = NT_STATUS_ACCESS_DENIED;
4814 /* Actually, returning zeros here works quite well :-). */
4816 return NT_STATUS_OK;
4819 /*********************************************************************
4821 *********************************************************************/
4823 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
4828 struct samr_info *info;
4829 SEC_DESC *psd = NULL;
4831 uint32 des_access = q_u->access_mask;
4838 if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid, &acc_granted, NULL))
4839 return NT_STATUS_INVALID_HANDLE;
4841 status = access_check_samr_function(acc_granted,
4842 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_group");
4844 if ( !NT_STATUS_IS_OK(status) )
4847 /*check if access can be granted as requested by client. */
4848 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
4849 se_map_generic(&des_access,&grp_generic_mapping);
4851 se_priv_copy( &se_rights, &se_add_users );
4853 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
4854 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
4855 &acc_granted, "_samr_open_group");
4857 if ( !NT_STATUS_IS_OK(status) )
4860 /* this should not be hard-coded like this */
4862 if (!sid_equal(&sid, get_global_sam_sid()))
4863 return NT_STATUS_ACCESS_DENIED;
4865 sid_copy(&info_sid, get_global_sam_sid());
4866 sid_append_rid(&info_sid, q_u->rid_group);
4867 sid_to_string(sid_string, &info_sid);
4869 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4870 return NT_STATUS_NO_MEMORY;
4872 info->acc_granted = acc_granted;
4874 DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
4876 /* check if that group really exists */
4878 ret = get_domain_group_from_sid(info->sid, &map);
4881 return NT_STATUS_NO_SUCH_GROUP;
4883 /* get a (unique) handle. open a policy on it. */
4884 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4885 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4887 return NT_STATUS_OK;
4890 /*********************************************************************
4891 _samr_remove_sid_foreign_domain
4892 *********************************************************************/
4894 NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p,
4895 SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN *q_u,
4896 SAMR_R_REMOVE_SID_FOREIGN_DOMAIN *r_u)
4898 DOM_SID delete_sid, domain_sid;
4901 DISP_INFO *disp_info = NULL;
4903 sid_copy( &delete_sid, &q_u->sid.sid );
4905 DEBUG(5,("_samr_remove_sid_foreign_domain: removing SID [%s]\n",
4906 sid_string_static(&delete_sid)));
4908 /* Find the policy handle. Open a policy on it. */
4910 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &domain_sid,
4911 &acc_granted, &disp_info))
4912 return NT_STATUS_INVALID_HANDLE;
4914 result = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS,
4915 "_samr_remove_sid_foreign_domain");
4917 if (!NT_STATUS_IS_OK(result))
4920 DEBUG(8, ("_samr_remove_sid_foreign_domain:sid is %s\n",
4921 sid_string_static(&domain_sid)));
4923 /* we can only delete a user from a group since we don't have
4924 nested groups anyways. So in the latter case, just say OK */
4926 /* TODO: The above comment nowadays is bogus. Since we have nested
4927 * groups now, and aliases members are never reported out of the unix
4928 * group membership, the "just say OK" makes this call a no-op. For
4929 * us. This needs fixing however. */
4931 /* I've only ever seen this in the wild when deleting a user from
4932 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
4933 * is the user about to be deleted. I very much suspect this is the
4934 * only application of this call. To verify this, let people report
4937 if (!sid_check_is_builtin(&domain_sid)) {
4938 DEBUG(1,("_samr_remove_sid_foreign_domain: domain_sid = %s, "
4939 "global_sam_sid() = %s\n",
4940 sid_string_static(&domain_sid),
4941 sid_string_static(get_global_sam_sid())));
4942 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
4943 return NT_STATUS_OK;
4946 force_flush_samr_cache(disp_info);
4948 result = NT_STATUS_OK;
4953 /*******************************************************************
4954 _samr_query_domain_info2
4955 ********************************************************************/
4957 NTSTATUS _samr_query_domain_info2(pipes_struct *p,
4958 SAMR_Q_QUERY_DOMAIN_INFO2 *q_u,
4959 SAMR_R_QUERY_DOMAIN_INFO2 *r_u)
4961 SAMR_Q_QUERY_DOMAIN_INFO q;
4962 SAMR_R_QUERY_DOMAIN_INFO r;
4967 DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4969 q.domain_pol = q_u->domain_pol;
4970 q.switch_value = q_u->switch_value;
4972 r_u->status = _samr_query_domain_info(p, &q, &r);
4974 r_u->ptr_0 = r.ptr_0;
4975 r_u->switch_value = r.switch_value;
4981 /*******************************************************************
4983 ********************************************************************/
4985 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
4987 time_t u_expire, u_min_age;
4989 time_t u_lock_duration, u_reset_time;
4991 r_u->status = NT_STATUS_OK;
4993 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4995 /* find the policy handle. open a policy on it. */
4996 if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
4997 return NT_STATUS_INVALID_HANDLE;
4999 DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
5001 switch (q_u->switch_value) {
5003 u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
5004 u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
5006 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
5007 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
5008 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.password_properties);
5009 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
5010 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
5015 u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
5016 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
5025 u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
5026 if (u_lock_duration != -1)
5027 u_lock_duration /= 60;
5029 u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count)/60;
5031 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
5032 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
5033 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
5036 return NT_STATUS_INVALID_INFO_CLASS;
5039 init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
5041 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));