s3-samr: fix _samr_LookupNames return code.
[Samba/bb.git] / source / rpc_server / srv_samr_nt.c
blobc924af6a63d14e093a8de50e64733b4063df0642
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997,
7 * Copyright (C) Marc Jacobsen 1999,
8 * Copyright (C) Jeremy Allison 2001-2008,
9 * Copyright (C) Jean François Micouleau 1998-2001,
10 * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002,
11 * Copyright (C) Gerald (Jerry) Carter 2003-2004,
12 * Copyright (C) Simo Sorce 2003.
13 * Copyright (C) Volker Lendecke 2005.
14 * Copyright (C) Guenther Deschner 2008.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 * This is the implementation of the SAMR code.
34 #include "includes.h"
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_RPC_SRV
39 #define SAMR_USR_RIGHTS_WRITE_PW \
40 ( READ_CONTROL_ACCESS | \
41 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
42 SAMR_USER_ACCESS_SET_LOC_COM)
43 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
44 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
46 #define DISP_INFO_CACHE_TIMEOUT 10
48 typedef struct disp_info {
49 DOM_SID sid; /* identify which domain this is. */
50 bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
51 struct pdb_search *users; /* querydispinfo 1 and 4 */
52 struct pdb_search *machines; /* querydispinfo 2 */
53 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
54 struct pdb_search *aliases; /* enumaliases */
56 uint16 enum_acb_mask;
57 struct pdb_search *enum_users; /* enumusers with a mask */
59 struct timed_event *cache_timeout_event; /* cache idle timeout
60 * handler. */
61 } DISP_INFO;
63 /* We keep a static list of these by SID as modern clients close down
64 all resources between each request in a complete enumeration. */
66 struct samr_info {
67 /* for use by the \PIPE\samr policy */
68 DOM_SID sid;
69 bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
70 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
71 uint32 acc_granted;
72 DISP_INFO *disp_info;
73 TALLOC_CTX *mem_ctx;
76 static const struct generic_mapping sam_generic_mapping = {
77 GENERIC_RIGHTS_SAM_READ,
78 GENERIC_RIGHTS_SAM_WRITE,
79 GENERIC_RIGHTS_SAM_EXECUTE,
80 GENERIC_RIGHTS_SAM_ALL_ACCESS};
81 static const struct generic_mapping dom_generic_mapping = {
82 GENERIC_RIGHTS_DOMAIN_READ,
83 GENERIC_RIGHTS_DOMAIN_WRITE,
84 GENERIC_RIGHTS_DOMAIN_EXECUTE,
85 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
86 static const struct generic_mapping usr_generic_mapping = {
87 GENERIC_RIGHTS_USER_READ,
88 GENERIC_RIGHTS_USER_WRITE,
89 GENERIC_RIGHTS_USER_EXECUTE,
90 GENERIC_RIGHTS_USER_ALL_ACCESS};
91 static const struct generic_mapping usr_nopwchange_generic_mapping = {
92 GENERIC_RIGHTS_USER_READ,
93 GENERIC_RIGHTS_USER_WRITE,
94 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
95 GENERIC_RIGHTS_USER_ALL_ACCESS};
96 static const struct generic_mapping grp_generic_mapping = {
97 GENERIC_RIGHTS_GROUP_READ,
98 GENERIC_RIGHTS_GROUP_WRITE,
99 GENERIC_RIGHTS_GROUP_EXECUTE,
100 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
101 static const struct generic_mapping ali_generic_mapping = {
102 GENERIC_RIGHTS_ALIAS_READ,
103 GENERIC_RIGHTS_ALIAS_WRITE,
104 GENERIC_RIGHTS_ALIAS_EXECUTE,
105 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
107 /*******************************************************************
108 *******************************************************************/
110 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
111 const struct generic_mapping *map,
112 DOM_SID *sid, uint32 sid_access )
114 DOM_SID domadmin_sid;
115 SEC_ACE ace[5]; /* at most 5 entries */
116 size_t i = 0;
118 SEC_ACL *psa = NULL;
120 /* basic access for Everyone */
122 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
123 map->generic_execute | map->generic_read, 0);
125 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
127 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
128 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
129 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
130 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
132 /* Add Full Access for Domain Admins if we are a DC */
134 if ( IS_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,
138 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
141 /* if we have a sid, give it some special access */
143 if ( sid ) {
144 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 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, SECURITY_DESCRIPTOR_REVISION_1,
153 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
154 psa, sd_size)) == NULL)
155 return NT_STATUS_NO_MEMORY;
157 return NT_STATUS_OK;
160 /*******************************************************************
161 Checks if access to an object should be granted, and returns that
162 level of access for further checks.
163 ********************************************************************/
165 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
166 SE_PRIV *rights, uint32 rights_mask,
167 uint32 des_access, uint32 *acc_granted,
168 const char *debug )
170 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
171 uint32 saved_mask = 0;
173 /* check privileges; certain SAM access bits should be overridden
174 by privileges (mostly having to do with creating/modifying/deleting
175 users and groups) */
177 if ( rights && user_has_any_privilege( token, rights ) ) {
179 saved_mask = (des_access & rights_mask);
180 des_access &= ~saved_mask;
182 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
183 rights_mask));
187 /* check the security descriptor first */
189 status = se_access_check(psd, token, des_access, acc_granted);
190 if (NT_STATUS_IS_OK(status)) {
191 goto done;
194 /* give root a free pass */
196 if ( geteuid() == sec_initial_uid() ) {
198 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
199 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
201 *acc_granted = des_access;
203 status = NT_STATUS_OK;
204 goto done;
208 done:
209 /* add in any bits saved during the privilege check (only
210 matters is status is ok) */
212 *acc_granted |= rights_mask;
214 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
215 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
216 des_access, *acc_granted));
218 return status;
221 /*******************************************************************
222 Checks if access to a function can be granted
223 ********************************************************************/
225 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
227 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
228 debug, acc_granted, acc_required));
230 /* check the security descriptor first */
232 if ( (acc_granted&acc_required) == acc_required )
233 return NT_STATUS_OK;
235 /* give root a free pass */
237 if (geteuid() == sec_initial_uid()) {
239 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
240 debug, acc_granted, acc_required));
241 DEBUGADD(4,("but overwritten by euid == 0\n"));
243 return NT_STATUS_OK;
246 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
247 debug, acc_granted, acc_required));
249 return NT_STATUS_ACCESS_DENIED;
252 /*******************************************************************
253 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
254 ********************************************************************/
256 static void map_max_allowed_access(const NT_USER_TOKEN *token,
257 uint32_t *pacc_requested)
259 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
260 return;
262 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
264 /* At least try for generic read. */
265 *pacc_requested = GENERIC_READ_ACCESS;
267 /* root gets anything. */
268 if (geteuid() == sec_initial_uid()) {
269 *pacc_requested |= GENERIC_ALL_ACCESS;
270 return;
273 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
275 if (is_sid_in_token(token, &global_sid_Builtin_Administrators) ||
276 is_sid_in_token(token, &global_sid_Builtin_Account_Operators)) {
277 *pacc_requested |= GENERIC_ALL_ACCESS;
278 return;
281 /* Full access for DOMAIN\Domain Admins. */
282 if ( IS_DC ) {
283 DOM_SID domadmin_sid;
284 sid_copy( &domadmin_sid, get_global_sam_sid() );
285 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
286 if (is_sid_in_token(token, &domadmin_sid)) {
287 *pacc_requested |= GENERIC_ALL_ACCESS;
288 return;
291 /* TODO ! Check privileges. */
294 /*******************************************************************
295 Fetch or create a dispinfo struct.
296 ********************************************************************/
298 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
301 * We do a static cache for DISP_INFO's here. Explanation can be found
302 * in Jeremy's checkin message to r11793:
304 * Fix the SAMR cache so it works across completely insane
305 * client behaviour (ie.:
306 * open pipe/open SAMR handle/enumerate 0 - 1024
307 * close SAMR handle, close pipe.
308 * open pipe/open SAMR handle/enumerate 1024 - 2048...
309 * close SAMR handle, close pipe.
310 * And on ad-nausium. Amazing.... probably object-oriented
311 * client side programming in action yet again.
312 * This change should *massively* improve performance when
313 * enumerating users from an LDAP database.
314 * Jeremy.
316 * "Our" and the builtin domain are the only ones where we ever
317 * enumerate stuff, so just cache 2 entries.
320 static struct disp_info builtin_dispinfo;
321 static struct disp_info domain_dispinfo;
323 /* There are two cases to consider here:
324 1) The SID is a domain SID and we look for an equality match, or
325 2) This is an account SID and so we return the DISP_INFO* for our
326 domain */
328 if (psid == NULL) {
329 return NULL;
332 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
334 * Necessary only once, but it does not really hurt.
336 sid_copy(&builtin_dispinfo.sid, &global_sid_Builtin);
338 return &builtin_dispinfo;
341 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
343 * Necessary only once, but it does not really hurt.
345 sid_copy(&domain_dispinfo.sid, get_global_sam_sid());
347 return &domain_dispinfo;
350 return NULL;
353 /*******************************************************************
354 Create a samr_info struct.
355 ********************************************************************/
357 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
359 struct samr_info *info;
360 fstring sid_str;
361 TALLOC_CTX *mem_ctx;
363 if (psid) {
364 sid_to_fstring(sid_str, psid);
365 } else {
366 fstrcpy(sid_str,"(NULL)");
369 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
371 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
372 return NULL;
374 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
375 if (psid) {
376 sid_copy( &info->sid, psid);
377 info->builtin_domain = sid_check_is_builtin(psid);
378 } else {
379 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
380 info->builtin_domain = False;
382 info->mem_ctx = mem_ctx;
384 info->disp_info = get_samr_dispinfo_by_sid(psid);
386 return info;
389 /*******************************************************************
390 Function to free the per SID data.
391 ********************************************************************/
393 static void free_samr_cache(DISP_INFO *disp_info)
395 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
396 sid_string_dbg(&disp_info->sid)));
398 /* We need to become root here because the paged search might have to
399 * tell the LDAP server we're not interested in the rest anymore. */
401 become_root();
403 if (disp_info->users) {
404 DEBUG(10,("free_samr_cache: deleting users cache\n"));
405 pdb_search_destroy(disp_info->users);
406 disp_info->users = NULL;
408 if (disp_info->machines) {
409 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
410 pdb_search_destroy(disp_info->machines);
411 disp_info->machines = NULL;
413 if (disp_info->groups) {
414 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
415 pdb_search_destroy(disp_info->groups);
416 disp_info->groups = NULL;
418 if (disp_info->aliases) {
419 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
420 pdb_search_destroy(disp_info->aliases);
421 disp_info->aliases = NULL;
423 if (disp_info->enum_users) {
424 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
425 pdb_search_destroy(disp_info->enum_users);
426 disp_info->enum_users = NULL;
428 disp_info->enum_acb_mask = 0;
430 unbecome_root();
433 /*******************************************************************
434 Function to free the per handle data.
435 ********************************************************************/
437 static void free_samr_info(void *ptr)
439 struct samr_info *info=(struct samr_info *) ptr;
441 /* Only free the dispinfo cache if no one bothered to set up
442 a timeout. */
444 if (info->disp_info && info->disp_info->cache_timeout_event == NULL) {
445 free_samr_cache(info->disp_info);
448 talloc_destroy(info->mem_ctx);
451 /*******************************************************************
452 Idle event handler. Throw away the disp info cache.
453 ********************************************************************/
455 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
456 struct timed_event *te,
457 const struct timeval *now,
458 void *private_data)
460 DISP_INFO *disp_info = (DISP_INFO *)private_data;
462 TALLOC_FREE(disp_info->cache_timeout_event);
464 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
465 "out\n"));
466 free_samr_cache(disp_info);
469 /*******************************************************************
470 Setup cache removal idle event handler.
471 ********************************************************************/
473 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
475 /* Remove any pending timeout and update. */
477 TALLOC_FREE(disp_info->cache_timeout_event);
479 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
480 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
481 (unsigned int)secs_fromnow ));
483 disp_info->cache_timeout_event = event_add_timed(
484 smbd_event_context(), NULL,
485 timeval_current_ofs(secs_fromnow, 0),
486 "disp_info_cache_idle_timeout_handler",
487 disp_info_cache_idle_timeout_handler, (void *)disp_info);
490 /*******************************************************************
491 Force flush any cache. We do this on any samr_set_xxx call.
492 We must also remove the timeout handler.
493 ********************************************************************/
495 static void force_flush_samr_cache(DISP_INFO *disp_info)
497 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
498 return;
501 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
502 TALLOC_FREE(disp_info->cache_timeout_event);
503 free_samr_cache(disp_info);
506 /*******************************************************************
507 Ensure password info is never given out. Paranioa... JRA.
508 ********************************************************************/
510 static void samr_clear_sam_passwd(struct samu *sam_pass)
513 if (!sam_pass)
514 return;
516 /* These now zero out the old password */
518 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
519 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
522 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
524 struct samr_displayentry *entry;
526 if (info->builtin_domain) {
527 /* No users in builtin. */
528 return 0;
531 if (info->users == NULL) {
532 info->users = pdb_search_users(acct_flags);
533 if (info->users == NULL) {
534 return 0;
537 /* Fetch the last possible entry, thus trigger an enumeration */
538 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
540 /* Ensure we cache this enumeration. */
541 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
543 return info->users->num_entries;
546 static uint32 count_sam_groups(struct disp_info *info)
548 struct samr_displayentry *entry;
550 if (info->builtin_domain) {
551 /* No groups in builtin. */
552 return 0;
555 if (info->groups == NULL) {
556 info->groups = pdb_search_groups();
557 if (info->groups == NULL) {
558 return 0;
561 /* Fetch the last possible entry, thus trigger an enumeration */
562 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
564 /* Ensure we cache this enumeration. */
565 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
567 return info->groups->num_entries;
570 static uint32 count_sam_aliases(struct disp_info *info)
572 struct samr_displayentry *entry;
574 if (info->aliases == NULL) {
575 info->aliases = pdb_search_aliases(&info->sid);
576 if (info->aliases == NULL) {
577 return 0;
580 /* Fetch the last possible entry, thus trigger an enumeration */
581 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
583 /* Ensure we cache this enumeration. */
584 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
586 return info->aliases->num_entries;
589 /*******************************************************************
590 _samr_Close
591 ********************************************************************/
593 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
595 if (!close_policy_hnd(p, r->in.handle)) {
596 return NT_STATUS_INVALID_HANDLE;
599 ZERO_STRUCTP(r->out.handle);
601 return NT_STATUS_OK;
604 /*******************************************************************
605 _samr_OpenDomain
606 ********************************************************************/
608 NTSTATUS _samr_OpenDomain(pipes_struct *p,
609 struct samr_OpenDomain *r)
611 struct samr_info *info;
612 SEC_DESC *psd = NULL;
613 uint32 acc_granted;
614 uint32 des_access = r->in.access_mask;
615 NTSTATUS status;
616 size_t sd_size;
617 SE_PRIV se_rights;
619 /* find the connection policy handle. */
621 if ( !find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info) )
622 return NT_STATUS_INVALID_HANDLE;
624 status = access_check_samr_function(info->acc_granted,
625 SAMR_ACCESS_OPEN_DOMAIN,
626 "_samr_OpenDomain" );
628 if ( !NT_STATUS_IS_OK(status) )
629 return status;
631 /*check if access can be granted as requested by client. */
632 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
634 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
635 se_map_generic( &des_access, &dom_generic_mapping );
637 se_priv_copy( &se_rights, &se_machine_account );
638 se_priv_add( &se_rights, &se_add_users );
640 status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
641 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
642 &acc_granted, "_samr_OpenDomain" );
644 if ( !NT_STATUS_IS_OK(status) )
645 return status;
647 if (!sid_check_is_domain(r->in.sid) &&
648 !sid_check_is_builtin(r->in.sid)) {
649 return NT_STATUS_NO_SUCH_DOMAIN;
652 /* associate the domain SID with the (unique) handle. */
653 if ((info = get_samr_info_by_sid(r->in.sid))==NULL)
654 return NT_STATUS_NO_MEMORY;
655 info->acc_granted = acc_granted;
657 /* get a (unique) handle. open a policy on it. */
658 if (!create_policy_hnd(p, r->out.domain_handle, free_samr_info, (void *)info))
659 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
661 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
663 return NT_STATUS_OK;
666 /*******************************************************************
667 _samr_GetUserPwInfo
668 ********************************************************************/
670 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
671 struct samr_GetUserPwInfo *r)
673 struct samr_info *info = NULL;
674 enum lsa_SidType sid_type;
675 uint32_t min_password_length = 0;
676 uint32_t password_properties = 0;
677 bool ret = false;
678 NTSTATUS status;
680 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
682 /* find the policy handle. open a policy on it. */
683 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info)) {
684 return NT_STATUS_INVALID_HANDLE;
687 status = access_check_samr_function(info->acc_granted,
688 SAMR_USER_ACCESS_GET_ATTRIBUTES,
689 "_samr_GetUserPwInfo" );
690 if (!NT_STATUS_IS_OK(status)) {
691 return status;
694 if (!sid_check_is_in_our_domain(&info->sid)) {
695 return NT_STATUS_OBJECT_TYPE_MISMATCH;
698 become_root();
699 ret = lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, &sid_type);
700 unbecome_root();
701 if (ret == false) {
702 return NT_STATUS_NO_SUCH_USER;
705 switch (sid_type) {
706 case SID_NAME_USER:
707 become_root();
708 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
709 &min_password_length);
710 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
711 &password_properties);
712 unbecome_root();
714 if (lp_check_password_script() && *lp_check_password_script()) {
715 password_properties |= DOMAIN_PASSWORD_COMPLEX;
718 break;
719 default:
720 break;
723 r->out.info->min_password_length = min_password_length;
724 r->out.info->password_properties = password_properties;
726 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
728 return NT_STATUS_OK;
731 /*******************************************************************
732 ********************************************************************/
734 static bool get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
735 DOM_SID *sid, uint32 *acc_granted,
736 DISP_INFO **ppdisp_info)
738 struct samr_info *info = NULL;
740 /* find the policy handle. open a policy on it. */
741 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
742 return False;
744 if (!info)
745 return False;
747 *sid = info->sid;
748 *acc_granted = info->acc_granted;
749 if (ppdisp_info) {
750 *ppdisp_info = info->disp_info;
753 return True;
756 /*******************************************************************
757 _samr_SetSecurity
758 ********************************************************************/
760 NTSTATUS _samr_SetSecurity(pipes_struct *p,
761 struct samr_SetSecurity *r)
763 DOM_SID pol_sid;
764 uint32 acc_granted, i;
765 SEC_ACL *dacl;
766 bool ret;
767 struct samu *sampass=NULL;
768 NTSTATUS status;
770 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
771 return NT_STATUS_INVALID_HANDLE;
773 if (!(sampass = samu_new( p->mem_ctx))) {
774 DEBUG(0,("No memory!\n"));
775 return NT_STATUS_NO_MEMORY;
778 /* get the user record */
779 become_root();
780 ret = pdb_getsampwsid(sampass, &pol_sid);
781 unbecome_root();
783 if (!ret) {
784 DEBUG(4, ("User %s not found\n", sid_string_dbg(&pol_sid)));
785 TALLOC_FREE(sampass);
786 return NT_STATUS_INVALID_HANDLE;
789 dacl = r->in.sdbuf->sd->dacl;
790 for (i=0; i < dacl->num_aces; i++) {
791 if (sid_equal(&pol_sid, &dacl->aces[i].trustee)) {
792 ret = pdb_set_pass_can_change(sampass,
793 (dacl->aces[i].access_mask &
794 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
795 True: False);
796 break;
800 if (!ret) {
801 TALLOC_FREE(sampass);
802 return NT_STATUS_ACCESS_DENIED;
805 status = access_check_samr_function(acc_granted,
806 SAMR_USER_ACCESS_SET_ATTRIBUTES,
807 "_samr_SetSecurity");
808 if (NT_STATUS_IS_OK(status)) {
809 become_root();
810 status = pdb_update_sam_account(sampass);
811 unbecome_root();
814 TALLOC_FREE(sampass);
816 return status;
819 /*******************************************************************
820 build correct perms based on policies and password times for _samr_query_sec_obj
821 *******************************************************************/
822 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, DOM_SID *user_sid)
824 struct samu *sampass=NULL;
825 bool ret;
827 if ( !(sampass = samu_new( mem_ctx )) ) {
828 DEBUG(0,("No memory!\n"));
829 return False;
832 become_root();
833 ret = pdb_getsampwsid(sampass, user_sid);
834 unbecome_root();
836 if (ret == False) {
837 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
838 TALLOC_FREE(sampass);
839 return False;
842 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
844 if (pdb_get_pass_can_change(sampass)) {
845 TALLOC_FREE(sampass);
846 return True;
848 TALLOC_FREE(sampass);
849 return False;
853 /*******************************************************************
854 _samr_QuerySecurity
855 ********************************************************************/
857 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
858 struct samr_QuerySecurity *r)
860 NTSTATUS status;
861 DOM_SID pol_sid;
862 SEC_DESC * psd = NULL;
863 uint32 acc_granted;
864 size_t sd_size;
866 /* Get the SID. */
867 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
868 return NT_STATUS_INVALID_HANDLE;
870 DEBUG(10,("_samr_QuerySecurity: querying security on SID: %s\n",
871 sid_string_dbg(&pol_sid)));
873 status = access_check_samr_function(acc_granted,
874 STD_RIGHT_READ_CONTROL_ACCESS,
875 "_samr_QuerySecurity");
876 if (!NT_STATUS_IS_OK(status)) {
877 return status;
880 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
882 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
883 if (pol_sid.sid_rev_num == 0) {
884 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
885 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
886 } else if (sid_equal(&pol_sid,get_global_sam_sid())) {
887 /* check if it is our domain SID */
888 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
889 "with SID: %s\n", sid_string_dbg(&pol_sid)));
890 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
891 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
892 /* check if it is the Builtin Domain */
893 /* TODO: Builtin probably needs a different SD with restricted write access*/
894 DEBUG(5,("_samr_QuerySecurity: querying security on Builtin "
895 "Domain with SID: %s\n", sid_string_dbg(&pol_sid)));
896 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
897 } else if (sid_check_is_in_our_domain(&pol_sid) ||
898 sid_check_is_in_builtin(&pol_sid)) {
899 /* TODO: different SDs have to be generated for aliases groups and users.
900 Currently all three get a default user SD */
901 DEBUG(10,("_samr_QuerySecurity: querying security on Object "
902 "with SID: %s\n", sid_string_dbg(&pol_sid)));
903 if (check_change_pw_access(p->mem_ctx, &pol_sid)) {
904 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
905 &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
906 } else {
907 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_nopwchange_generic_mapping,
908 &pol_sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
910 } else {
911 return NT_STATUS_OBJECT_TYPE_MISMATCH;
914 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
915 return NT_STATUS_NO_MEMORY;
917 return status;
920 /*******************************************************************
921 makes a SAM_ENTRY / UNISTR2* structure from a user list.
922 ********************************************************************/
924 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
925 struct samr_SamEntry **sam_pp,
926 uint32_t num_entries,
927 uint32_t start_idx,
928 struct samr_displayentry *entries)
930 uint32_t i;
931 struct samr_SamEntry *sam;
933 *sam_pp = NULL;
935 if (num_entries == 0) {
936 return NT_STATUS_OK;
939 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
940 if (sam == NULL) {
941 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
942 return NT_STATUS_NO_MEMORY;
945 for (i = 0; i < num_entries; i++) {
946 #if 0
948 * usrmgr expects a non-NULL terminated string with
949 * trust relationships
951 if (entries[i].acct_flags & ACB_DOMTRUST) {
952 init_unistr2(&uni_temp_name, entries[i].account_name,
953 UNI_FLAGS_NONE);
954 } else {
955 init_unistr2(&uni_temp_name, entries[i].account_name,
956 UNI_STR_TERMINATE);
958 #endif
959 init_lsa_String(&sam[i].name, entries[i].account_name);
960 sam[i].idx = entries[i].rid;
963 *sam_pp = sam;
965 return NT_STATUS_OK;
968 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
970 /*******************************************************************
971 _samr_EnumDomainUsers
972 ********************************************************************/
974 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
975 struct samr_EnumDomainUsers *r)
977 NTSTATUS status;
978 struct samr_info *info = NULL;
979 int num_account;
980 uint32 enum_context = *r->in.resume_handle;
981 enum remote_arch_types ra_type = get_remote_arch();
982 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
983 uint32 max_entries = max_sam_entries;
984 struct samr_displayentry *entries = NULL;
985 struct samr_SamArray *samr_array = NULL;
986 struct samr_SamEntry *samr_entries = NULL;
988 /* find the policy handle. open a policy on it. */
989 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
990 return NT_STATUS_INVALID_HANDLE;
992 status = access_check_samr_function(info->acc_granted,
993 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
994 "_samr_EnumDomainUsers");
995 if (!NT_STATUS_IS_OK(status)) {
996 return status;
999 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1001 if (info->builtin_domain) {
1002 /* No users in builtin. */
1003 *r->out.resume_handle = *r->in.resume_handle;
1004 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
1005 return status;
1008 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1009 if (!samr_array) {
1010 return NT_STATUS_NO_MEMORY;
1013 become_root();
1015 /* AS ROOT !!!! */
1017 if ((info->disp_info->enum_users != NULL) &&
1018 (info->disp_info->enum_acb_mask != r->in.acct_flags)) {
1019 pdb_search_destroy(info->disp_info->enum_users);
1020 info->disp_info->enum_users = NULL;
1023 if (info->disp_info->enum_users == NULL) {
1024 info->disp_info->enum_users = pdb_search_users(r->in.acct_flags);
1025 info->disp_info->enum_acb_mask = r->in.acct_flags;
1028 if (info->disp_info->enum_users == NULL) {
1029 /* END AS ROOT !!!! */
1030 unbecome_root();
1031 return NT_STATUS_ACCESS_DENIED;
1034 num_account = pdb_search_entries(info->disp_info->enum_users,
1035 enum_context, max_entries,
1036 &entries);
1038 /* END AS ROOT !!!! */
1040 unbecome_root();
1042 if (num_account == 0) {
1043 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
1044 "total entries\n"));
1045 *r->out.resume_handle = *r->in.resume_handle;
1046 return NT_STATUS_OK;
1049 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
1050 num_account, enum_context,
1051 entries);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 return status;
1056 if (max_entries <= num_account) {
1057 status = STATUS_MORE_ENTRIES;
1058 } else {
1059 status = NT_STATUS_OK;
1062 /* Ensure we cache this enumeration. */
1063 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1065 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
1067 samr_array->count = num_account;
1068 samr_array->entries = samr_entries;
1070 *r->out.resume_handle = *r->in.resume_handle + num_account;
1071 *r->out.sam = samr_array;
1072 *r->out.num_entries = num_account;
1074 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1076 return status;
1079 /*******************************************************************
1080 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1081 ********************************************************************/
1083 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1084 struct samr_SamEntry **sam_pp,
1085 uint32_t num_sam_entries,
1086 struct samr_displayentry *entries)
1088 struct samr_SamEntry *sam;
1089 uint32_t i;
1091 *sam_pp = NULL;
1093 if (num_sam_entries == 0) {
1094 return;
1097 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1098 if (sam == NULL) {
1099 return;
1102 for (i = 0; i < num_sam_entries; i++) {
1104 * JRA. I think this should include the null. TNG does not.
1106 init_lsa_String(&sam[i].name, entries[i].account_name);
1107 sam[i].idx = entries[i].rid;
1110 *sam_pp = sam;
1113 /*******************************************************************
1114 _samr_EnumDomainGroups
1115 ********************************************************************/
1117 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1118 struct samr_EnumDomainGroups *r)
1120 NTSTATUS status;
1121 struct samr_info *info = NULL;
1122 struct samr_displayentry *groups;
1123 uint32 num_groups;
1124 struct samr_SamArray *samr_array = NULL;
1125 struct samr_SamEntry *samr_entries = NULL;
1127 /* find the policy handle. open a policy on it. */
1128 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1129 return NT_STATUS_INVALID_HANDLE;
1131 status = access_check_samr_function(info->acc_granted,
1132 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
1133 "_samr_EnumDomainGroups");
1134 if (!NT_STATUS_IS_OK(status)) {
1135 return status;
1138 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1140 if (info->builtin_domain) {
1141 /* No groups in builtin. */
1142 *r->out.resume_handle = *r->in.resume_handle;
1143 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1144 return status;
1147 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1148 if (!samr_array) {
1149 return NT_STATUS_NO_MEMORY;
1152 /* the domain group array is being allocated in the function below */
1154 become_root();
1156 if (info->disp_info->groups == NULL) {
1157 info->disp_info->groups = pdb_search_groups();
1159 if (info->disp_info->groups == NULL) {
1160 unbecome_root();
1161 return NT_STATUS_ACCESS_DENIED;
1165 num_groups = pdb_search_entries(info->disp_info->groups,
1166 *r->in.resume_handle,
1167 MAX_SAM_ENTRIES, &groups);
1168 unbecome_root();
1170 /* Ensure we cache this enumeration. */
1171 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1173 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1174 num_groups, groups);
1176 samr_array->count = num_groups;
1177 samr_array->entries = samr_entries;
1179 *r->out.sam = samr_array;
1180 *r->out.num_entries = num_groups;
1181 /* this was missing, IMHO:
1182 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1185 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1187 return status;
1190 /*******************************************************************
1191 _samr_EnumDomainAliases
1192 ********************************************************************/
1194 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1195 struct samr_EnumDomainAliases *r)
1197 NTSTATUS status;
1198 struct samr_info *info;
1199 struct samr_displayentry *aliases;
1200 uint32 num_aliases = 0;
1201 struct samr_SamArray *samr_array = NULL;
1202 struct samr_SamEntry *samr_entries = NULL;
1204 /* find the policy handle. open a policy on it. */
1205 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1206 return NT_STATUS_INVALID_HANDLE;
1208 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1209 sid_string_dbg(&info->sid)));
1211 status = access_check_samr_function(info->acc_granted,
1212 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
1213 "_samr_EnumDomainAliases");
1214 if (!NT_STATUS_IS_OK(status)) {
1215 return status;
1218 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1219 if (!samr_array) {
1220 return NT_STATUS_NO_MEMORY;
1223 become_root();
1225 if (info->disp_info->aliases == NULL) {
1226 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1227 if (info->disp_info->aliases == NULL) {
1228 unbecome_root();
1229 return NT_STATUS_ACCESS_DENIED;
1233 num_aliases = pdb_search_entries(info->disp_info->aliases,
1234 *r->in.resume_handle,
1235 MAX_SAM_ENTRIES, &aliases);
1236 unbecome_root();
1238 /* Ensure we cache this enumeration. */
1239 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1241 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1242 num_aliases, aliases);
1244 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1246 samr_array->count = num_aliases;
1247 samr_array->entries = samr_entries;
1249 *r->out.sam = samr_array;
1250 *r->out.num_entries = num_aliases;
1251 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1253 return status;
1256 /*******************************************************************
1257 inits a samr_DispInfoGeneral structure.
1258 ********************************************************************/
1260 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1261 struct samr_DispInfoGeneral *r,
1262 uint32_t num_entries,
1263 uint32_t start_idx,
1264 struct samr_displayentry *entries)
1266 uint32 i;
1268 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1270 if (num_entries == 0) {
1271 return NT_STATUS_OK;
1274 r->count = num_entries;
1276 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1277 if (!r->entries) {
1278 return NT_STATUS_NO_MEMORY;
1281 for (i = 0; i < num_entries ; i++) {
1283 init_lsa_String(&r->entries[i].account_name,
1284 entries[i].account_name);
1286 init_lsa_String(&r->entries[i].description,
1287 entries[i].description);
1289 init_lsa_String(&r->entries[i].full_name,
1290 entries[i].fullname);
1292 r->entries[i].rid = entries[i].rid;
1293 r->entries[i].acct_flags = entries[i].acct_flags;
1294 r->entries[i].idx = start_idx+i+1;
1297 return NT_STATUS_OK;
1300 /*******************************************************************
1301 inits a samr_DispInfoFull structure.
1302 ********************************************************************/
1304 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1305 struct samr_DispInfoFull *r,
1306 uint32_t num_entries,
1307 uint32_t start_idx,
1308 struct samr_displayentry *entries)
1310 uint32_t i;
1312 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1314 if (num_entries == 0) {
1315 return NT_STATUS_OK;
1318 r->count = num_entries;
1320 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1321 if (!r->entries) {
1322 return NT_STATUS_NO_MEMORY;
1325 for (i = 0; i < num_entries ; i++) {
1327 init_lsa_String(&r->entries[i].account_name,
1328 entries[i].account_name);
1330 init_lsa_String(&r->entries[i].description,
1331 entries[i].description);
1333 r->entries[i].rid = entries[i].rid;
1334 r->entries[i].acct_flags = entries[i].acct_flags;
1335 r->entries[i].idx = start_idx+i+1;
1338 return NT_STATUS_OK;
1341 /*******************************************************************
1342 inits a samr_DispInfoFullGroups structure.
1343 ********************************************************************/
1345 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1346 struct samr_DispInfoFullGroups *r,
1347 uint32_t num_entries,
1348 uint32_t start_idx,
1349 struct samr_displayentry *entries)
1351 uint32_t i;
1353 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1355 if (num_entries == 0) {
1356 return NT_STATUS_OK;
1359 r->count = num_entries;
1361 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1362 if (!r->entries) {
1363 return NT_STATUS_NO_MEMORY;
1366 for (i = 0; i < num_entries ; i++) {
1368 init_lsa_String(&r->entries[i].account_name,
1369 entries[i].account_name);
1371 init_lsa_String(&r->entries[i].description,
1372 entries[i].description);
1374 r->entries[i].rid = entries[i].rid;
1375 r->entries[i].acct_flags = entries[i].acct_flags;
1376 r->entries[i].idx = start_idx+i+1;
1379 return NT_STATUS_OK;
1382 /*******************************************************************
1383 inits a samr_DispInfoAscii structure.
1384 ********************************************************************/
1386 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1387 struct samr_DispInfoAscii *r,
1388 uint32_t num_entries,
1389 uint32_t start_idx,
1390 struct samr_displayentry *entries)
1392 uint32_t i;
1394 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1396 if (num_entries == 0) {
1397 return NT_STATUS_OK;
1400 r->count = num_entries;
1402 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1403 if (!r->entries) {
1404 return NT_STATUS_NO_MEMORY;
1407 for (i = 0; i < num_entries ; i++) {
1409 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1410 entries[i].account_name);
1412 r->entries[i].idx = start_idx+i+1;
1415 return NT_STATUS_OK;
1418 /*******************************************************************
1419 inits a samr_DispInfoAscii structure.
1420 ********************************************************************/
1422 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1423 struct samr_DispInfoAscii *r,
1424 uint32_t num_entries,
1425 uint32_t start_idx,
1426 struct samr_displayentry *entries)
1428 uint32_t i;
1430 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1432 if (num_entries == 0) {
1433 return NT_STATUS_OK;
1436 r->count = num_entries;
1438 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1439 if (!r->entries) {
1440 return NT_STATUS_NO_MEMORY;
1443 for (i = 0; i < num_entries ; i++) {
1445 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1446 entries[i].account_name);
1448 r->entries[i].idx = start_idx+i+1;
1451 return NT_STATUS_OK;
1454 /*******************************************************************
1455 _samr_QueryDisplayInfo
1456 ********************************************************************/
1458 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1459 struct samr_QueryDisplayInfo *r)
1461 NTSTATUS status;
1462 struct samr_info *info = NULL;
1463 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1465 uint32 max_entries = r->in.max_entries;
1466 uint32 enum_context = r->in.start_idx;
1467 uint32 max_size = r->in.buf_size;
1469 union samr_DispInfo *disp_info = r->out.info;
1471 uint32 temp_size=0, total_data_size=0;
1472 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1473 uint32 num_account = 0;
1474 enum remote_arch_types ra_type = get_remote_arch();
1475 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1476 struct samr_displayentry *entries = NULL;
1478 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1480 /* find the policy handle. open a policy on it. */
1481 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1482 return NT_STATUS_INVALID_HANDLE;
1484 status = access_check_samr_function(info->acc_granted,
1485 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
1486 "_samr_QueryDisplayInfo");
1487 if (!NT_STATUS_IS_OK(status)) {
1488 return status;
1492 * calculate how many entries we will return.
1493 * based on
1494 * - the number of entries the client asked
1495 * - our limit on that
1496 * - the starting point (enumeration context)
1497 * - the buffer size the client will accept
1501 * We are a lot more like W2K. Instead of reading the SAM
1502 * each time to find the records we need to send back,
1503 * we read it once and link that copy to the sam handle.
1504 * For large user list (over the MAX_SAM_ENTRIES)
1505 * it's a definitive win.
1506 * second point to notice: between enumerations
1507 * our sam is now the same as it's a snapshoot.
1508 * third point: got rid of the static SAM_USER_21 struct
1509 * no more intermediate.
1510 * con: it uses much more memory, as a full copy is stored
1511 * in memory.
1513 * If you want to change it, think twice and think
1514 * of the second point , that's really important.
1516 * JFM, 12/20/2001
1519 if ((r->in.level < 1) || (r->in.level > 5)) {
1520 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1521 (unsigned int)r->in.level ));
1522 return NT_STATUS_INVALID_INFO_CLASS;
1525 /* first limit the number of entries we will return */
1526 if(max_entries > max_sam_entries) {
1527 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1528 "entries, limiting to %d\n", max_entries,
1529 max_sam_entries));
1530 max_entries = max_sam_entries;
1533 /* calculate the size and limit on the number of entries we will
1534 * return */
1536 temp_size=max_entries*struct_size;
1538 if (temp_size>max_size) {
1539 max_entries=MIN((max_size/struct_size),max_entries);;
1540 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1541 "only %d entries\n", max_entries));
1544 become_root();
1546 /* THe following done as ROOT. Don't return without unbecome_root(). */
1548 switch (r->in.level) {
1549 case 0x1:
1550 case 0x4:
1551 if (info->disp_info->users == NULL) {
1552 info->disp_info->users = pdb_search_users(ACB_NORMAL);
1553 if (info->disp_info->users == NULL) {
1554 unbecome_root();
1555 return NT_STATUS_ACCESS_DENIED;
1557 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1558 (unsigned int)enum_context ));
1559 } else {
1560 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1561 (unsigned int)enum_context ));
1564 num_account = pdb_search_entries(info->disp_info->users,
1565 enum_context, max_entries,
1566 &entries);
1567 break;
1568 case 0x2:
1569 if (info->disp_info->machines == NULL) {
1570 info->disp_info->machines =
1571 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1572 if (info->disp_info->machines == NULL) {
1573 unbecome_root();
1574 return NT_STATUS_ACCESS_DENIED;
1576 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1577 (unsigned int)enum_context ));
1578 } else {
1579 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1580 (unsigned int)enum_context ));
1583 num_account = pdb_search_entries(info->disp_info->machines,
1584 enum_context, max_entries,
1585 &entries);
1586 break;
1587 case 0x3:
1588 case 0x5:
1589 if (info->disp_info->groups == NULL) {
1590 info->disp_info->groups = pdb_search_groups();
1591 if (info->disp_info->groups == NULL) {
1592 unbecome_root();
1593 return NT_STATUS_ACCESS_DENIED;
1595 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1596 (unsigned int)enum_context ));
1597 } else {
1598 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1599 (unsigned int)enum_context ));
1602 num_account = pdb_search_entries(info->disp_info->groups,
1603 enum_context, max_entries,
1604 &entries);
1605 break;
1606 default:
1607 unbecome_root();
1608 smb_panic("info class changed");
1609 break;
1611 unbecome_root();
1614 /* Now create reply structure */
1615 switch (r->in.level) {
1616 case 0x1:
1617 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1618 num_account, enum_context,
1619 entries);
1620 break;
1621 case 0x2:
1622 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1623 num_account, enum_context,
1624 entries);
1625 break;
1626 case 0x3:
1627 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1628 num_account, enum_context,
1629 entries);
1630 break;
1631 case 0x4:
1632 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1633 num_account, enum_context,
1634 entries);
1635 break;
1636 case 0x5:
1637 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1638 num_account, enum_context,
1639 entries);
1640 break;
1641 default:
1642 smb_panic("info class changed");
1643 break;
1646 if (!NT_STATUS_IS_OK(disp_ret))
1647 return disp_ret;
1649 /* calculate the total size */
1650 total_data_size=num_account*struct_size;
1652 if (max_entries <= num_account) {
1653 status = STATUS_MORE_ENTRIES;
1654 } else {
1655 status = NT_STATUS_OK;
1658 /* Ensure we cache this enumeration. */
1659 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1661 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1663 *r->out.total_size = total_data_size;
1664 *r->out.returned_size = temp_size;
1666 return status;
1669 /****************************************************************
1670 _samr_QueryDisplayInfo2
1671 ****************************************************************/
1673 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1674 struct samr_QueryDisplayInfo2 *r)
1676 struct samr_QueryDisplayInfo q;
1678 q.in.domain_handle = r->in.domain_handle;
1679 q.in.level = r->in.level;
1680 q.in.start_idx = r->in.start_idx;
1681 q.in.max_entries = r->in.max_entries;
1682 q.in.buf_size = r->in.buf_size;
1684 q.out.total_size = r->out.total_size;
1685 q.out.returned_size = r->out.returned_size;
1686 q.out.info = r->out.info;
1688 return _samr_QueryDisplayInfo(p, &q);
1691 /****************************************************************
1692 _samr_QueryDisplayInfo3
1693 ****************************************************************/
1695 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1696 struct samr_QueryDisplayInfo3 *r)
1698 struct samr_QueryDisplayInfo q;
1700 q.in.domain_handle = r->in.domain_handle;
1701 q.in.level = r->in.level;
1702 q.in.start_idx = r->in.start_idx;
1703 q.in.max_entries = r->in.max_entries;
1704 q.in.buf_size = r->in.buf_size;
1706 q.out.total_size = r->out.total_size;
1707 q.out.returned_size = r->out.returned_size;
1708 q.out.info = r->out.info;
1710 return _samr_QueryDisplayInfo(p, &q);
1713 /*******************************************************************
1714 _samr_QueryAliasInfo
1715 ********************************************************************/
1717 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1718 struct samr_QueryAliasInfo *r)
1720 DOM_SID sid;
1721 struct acct_info info;
1722 uint32 acc_granted;
1723 NTSTATUS status;
1724 union samr_AliasInfo *alias_info = NULL;
1725 const char *alias_name = NULL;
1726 const char *alias_description = NULL;
1728 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1730 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1731 if (!alias_info) {
1732 return NT_STATUS_NO_MEMORY;
1735 /* find the policy handle. open a policy on it. */
1736 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &sid, &acc_granted, NULL))
1737 return NT_STATUS_INVALID_HANDLE;
1739 status = access_check_samr_function(acc_granted,
1740 SAMR_ALIAS_ACCESS_LOOKUP_INFO,
1741 "_samr_QueryAliasInfo");
1742 if (!NT_STATUS_IS_OK(status)) {
1743 return status;
1746 become_root();
1747 status = pdb_get_aliasinfo(&sid, &info);
1748 unbecome_root();
1750 if ( !NT_STATUS_IS_OK(status))
1751 return status;
1753 /* FIXME: info contains fstrings */
1754 alias_name = talloc_strdup(r, info.acct_name);
1755 alias_description = talloc_strdup(r, info.acct_desc);
1757 switch (r->in.level) {
1758 case ALIASINFOALL:
1759 init_samr_alias_info1(&alias_info->all,
1760 alias_name,
1762 alias_description);
1763 break;
1764 case ALIASINFODESCRIPTION:
1765 init_samr_alias_info3(&alias_info->description,
1766 alias_description);
1767 break;
1768 default:
1769 return NT_STATUS_INVALID_INFO_CLASS;
1772 *r->out.info = alias_info;
1774 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1776 return NT_STATUS_OK;
1779 #if 0
1780 /*******************************************************************
1781 samr_reply_lookup_ids
1782 ********************************************************************/
1784 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1786 uint32 rid[MAX_SAM_ENTRIES];
1787 int num_rids = q_u->num_sids1;
1789 r_u->status = NT_STATUS_OK;
1791 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1793 if (num_rids > MAX_SAM_ENTRIES) {
1794 num_rids = MAX_SAM_ENTRIES;
1795 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1798 #if 0
1799 int i;
1800 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1802 for (i = 0; i < num_rids && status == 0; i++)
1804 struct sam_passwd *sam_pass;
1805 fstring user_name;
1808 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1809 q_u->uni_user_name[i].uni_str_len));
1811 /* find the user account */
1812 become_root();
1813 sam_pass = get_smb21pwd_entry(user_name, 0);
1814 unbecome_root();
1816 if (sam_pass == NULL)
1818 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1819 rid[i] = 0;
1821 else
1823 rid[i] = sam_pass->user_rid;
1826 #endif
1828 num_rids = 1;
1829 rid[0] = BUILTIN_ALIAS_RID_USERS;
1831 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1833 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1835 return r_u->status;
1837 #endif
1839 /*******************************************************************
1840 _samr_LookupNames
1841 ********************************************************************/
1843 NTSTATUS _samr_LookupNames(pipes_struct *p,
1844 struct samr_LookupNames *r)
1846 NTSTATUS status;
1847 uint32 *rid;
1848 enum lsa_SidType *type;
1849 int i;
1850 int num_rids = r->in.num_names;
1851 DOM_SID pol_sid;
1852 uint32 acc_granted;
1853 struct samr_Ids rids, types;
1854 uint32_t num_mapped = 0;
1856 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1858 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL)) {
1859 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1862 status = access_check_samr_function(acc_granted,
1863 0, /* Don't know the acc_bits yet */
1864 "_samr_LookupNames");
1865 if (!NT_STATUS_IS_OK(status)) {
1866 return status;
1869 if (num_rids > MAX_SAM_ENTRIES) {
1870 num_rids = MAX_SAM_ENTRIES;
1871 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1874 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1875 NT_STATUS_HAVE_NO_MEMORY(rid);
1877 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1878 NT_STATUS_HAVE_NO_MEMORY(type);
1880 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1881 sid_string_dbg(&pol_sid)));
1883 for (i = 0; i < num_rids; i++) {
1885 status = NT_STATUS_NONE_MAPPED;
1886 type[i] = SID_NAME_UNKNOWN;
1888 rid[i] = 0xffffffff;
1890 if (sid_check_is_builtin(&pol_sid)) {
1891 if (lookup_builtin_name(r->in.names[i].string,
1892 &rid[i]))
1894 type[i] = SID_NAME_ALIAS;
1896 } else {
1897 lookup_global_sam_name(r->in.names[i].string, 0,
1898 &rid[i], &type[i]);
1901 if (type[i] != SID_NAME_UNKNOWN) {
1902 num_mapped++;
1906 if (num_mapped == num_rids) {
1907 status = NT_STATUS_OK;
1908 } else if (num_mapped == 0) {
1909 status = NT_STATUS_NONE_MAPPED;
1910 } else {
1911 status = STATUS_SOME_UNMAPPED;
1914 rids.count = num_rids;
1915 rids.ids = rid;
1917 types.count = num_rids;
1918 types.ids = type;
1920 *r->out.rids = rids;
1921 *r->out.types = types;
1923 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1925 return status;
1928 /*******************************************************************
1929 _samr_ChangePasswordUser2
1930 ********************************************************************/
1932 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1933 struct samr_ChangePasswordUser2 *r)
1935 NTSTATUS status;
1936 fstring user_name;
1937 fstring wks;
1939 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1941 fstrcpy(user_name, r->in.account->string);
1942 fstrcpy(wks, r->in.server->string);
1944 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1947 * Pass the user through the NT -> unix user mapping
1948 * function.
1951 (void)map_username(user_name);
1954 * UNIX username case mangling not required, pass_oem_change
1955 * is case insensitive.
1958 status = pass_oem_change(user_name,
1959 r->in.lm_password->data,
1960 r->in.lm_verifier->hash,
1961 r->in.nt_password->data,
1962 r->in.nt_verifier->hash,
1963 NULL);
1965 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1967 return status;
1970 /*******************************************************************
1971 _samr_ChangePasswordUser3
1972 ********************************************************************/
1974 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
1975 struct samr_ChangePasswordUser3 *r)
1977 NTSTATUS status;
1978 fstring user_name;
1979 const char *wks = NULL;
1980 uint32 reject_reason;
1981 struct samr_DomInfo1 *dominfo = NULL;
1982 struct samr_ChangeReject *reject = NULL;
1984 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1986 fstrcpy(user_name, r->in.account->string);
1987 if (r->in.server && r->in.server->string) {
1988 wks = r->in.server->string;
1991 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1994 * Pass the user through the NT -> unix user mapping
1995 * function.
1998 (void)map_username(user_name);
2001 * UNIX username case mangling not required, pass_oem_change
2002 * is case insensitive.
2005 status = pass_oem_change(user_name,
2006 r->in.lm_password->data,
2007 r->in.lm_verifier->hash,
2008 r->in.nt_password->data,
2009 r->in.nt_verifier->hash,
2010 &reject_reason);
2012 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
2013 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
2015 uint32 min_pass_len,pass_hist,password_properties;
2016 time_t u_expire, u_min_age;
2017 NTTIME nt_expire, nt_min_age;
2018 uint32 account_policy_temp;
2020 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
2021 if (!dominfo) {
2022 return NT_STATUS_NO_MEMORY;
2025 reject = TALLOC_ZERO_P(p->mem_ctx, struct samr_ChangeReject);
2026 if (!reject) {
2027 return NT_STATUS_NO_MEMORY;
2030 become_root();
2032 /* AS ROOT !!! */
2034 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2035 min_pass_len = account_policy_temp;
2037 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2038 pass_hist = account_policy_temp;
2040 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2041 password_properties = account_policy_temp;
2043 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2044 u_expire = account_policy_temp;
2046 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2047 u_min_age = account_policy_temp;
2049 /* !AS ROOT */
2051 unbecome_root();
2053 unix_to_nt_time_abs(&nt_expire, u_expire);
2054 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2056 if (lp_check_password_script() && *lp_check_password_script()) {
2057 password_properties |= DOMAIN_PASSWORD_COMPLEX;
2060 init_samr_DomInfo1(dominfo,
2061 min_pass_len,
2062 pass_hist,
2063 password_properties,
2064 u_expire,
2065 u_min_age);
2067 reject->reason = reject_reason;
2069 *r->out.dominfo = dominfo;
2070 *r->out.reject = reject;
2073 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2075 return status;
2078 /*******************************************************************
2079 makes a SAMR_R_LOOKUP_RIDS structure.
2080 ********************************************************************/
2082 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
2083 const char **names,
2084 struct lsa_String **lsa_name_array_p)
2086 struct lsa_String *lsa_name_array = NULL;
2087 uint32_t i;
2089 *lsa_name_array_p = NULL;
2091 if (num_names != 0) {
2092 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
2093 if (!lsa_name_array) {
2094 return false;
2098 for (i = 0; i < num_names; i++) {
2099 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2100 init_lsa_String(&lsa_name_array[i], names[i]);
2103 *lsa_name_array_p = lsa_name_array;
2105 return true;
2108 /*******************************************************************
2109 _samr_LookupRids
2110 ********************************************************************/
2112 NTSTATUS _samr_LookupRids(pipes_struct *p,
2113 struct samr_LookupRids *r)
2115 NTSTATUS status;
2116 const char **names;
2117 enum lsa_SidType *attrs = NULL;
2118 uint32 *wire_attrs = NULL;
2119 DOM_SID pol_sid;
2120 int num_rids = (int)r->in.num_rids;
2121 uint32 acc_granted;
2122 int i;
2123 struct lsa_Strings names_array;
2124 struct samr_Ids types_array;
2125 struct lsa_String *lsa_names = NULL;
2127 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2129 /* find the policy handle. open a policy on it. */
2130 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL))
2131 return NT_STATUS_INVALID_HANDLE;
2133 status = access_check_samr_function(acc_granted,
2134 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
2135 "_samr_LookupRids");
2136 if (!NT_STATUS_IS_OK(status)) {
2137 return status;
2140 if (num_rids > 1000) {
2141 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2142 "to samba4 idl this is not possible\n", num_rids));
2143 return NT_STATUS_UNSUCCESSFUL;
2146 if (num_rids) {
2147 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2148 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2149 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2151 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2152 return NT_STATUS_NO_MEMORY;
2153 } else {
2154 names = NULL;
2155 attrs = NULL;
2156 wire_attrs = NULL;
2159 become_root(); /* lookup_sid can require root privs */
2160 status = pdb_lookup_rids(&pol_sid, num_rids, r->in.rids,
2161 names, attrs);
2162 unbecome_root();
2164 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2165 status = NT_STATUS_OK;
2168 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2169 &lsa_names)) {
2170 return NT_STATUS_NO_MEMORY;
2173 /* Convert from enum lsa_SidType to uint32 for wire format. */
2174 for (i = 0; i < num_rids; i++) {
2175 wire_attrs[i] = (uint32)attrs[i];
2178 names_array.count = num_rids;
2179 names_array.names = lsa_names;
2181 types_array.count = num_rids;
2182 types_array.ids = wire_attrs;
2184 *r->out.names = names_array;
2185 *r->out.types = types_array;
2187 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2189 return status;
2192 /*******************************************************************
2193 _samr_OpenUser
2194 ********************************************************************/
2196 NTSTATUS _samr_OpenUser(pipes_struct *p,
2197 struct samr_OpenUser *r)
2199 struct samu *sampass=NULL;
2200 DOM_SID sid;
2201 POLICY_HND domain_pol = *r->in.domain_handle;
2202 POLICY_HND *user_pol = r->out.user_handle;
2203 struct samr_info *info = NULL;
2204 SEC_DESC *psd = NULL;
2205 uint32 acc_granted;
2206 uint32 des_access = r->in.access_mask;
2207 size_t sd_size;
2208 bool ret;
2209 NTSTATUS nt_status;
2210 SE_PRIV se_rights;
2212 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
2214 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2215 return NT_STATUS_INVALID_HANDLE;
2217 nt_status = access_check_samr_function(acc_granted,
2218 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
2219 "_samr_OpenUser" );
2221 if ( !NT_STATUS_IS_OK(nt_status) )
2222 return nt_status;
2224 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2225 return NT_STATUS_NO_MEMORY;
2228 /* append the user's RID to it */
2230 if (!sid_append_rid(&sid, r->in.rid))
2231 return NT_STATUS_NO_SUCH_USER;
2233 /* check if access can be granted as requested by client. */
2235 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
2237 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2238 se_map_generic(&des_access, &usr_generic_mapping);
2240 se_priv_copy( &se_rights, &se_machine_account );
2241 se_priv_add( &se_rights, &se_add_users );
2243 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2244 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2245 &acc_granted, "_samr_OpenUser");
2247 if ( !NT_STATUS_IS_OK(nt_status) )
2248 return nt_status;
2250 become_root();
2251 ret=pdb_getsampwsid(sampass, &sid);
2252 unbecome_root();
2254 /* check that the SID exists in our domain. */
2255 if (ret == False) {
2256 return NT_STATUS_NO_SUCH_USER;
2259 TALLOC_FREE(sampass);
2261 /* associate the user's SID and access bits with the new handle. */
2262 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2263 return NT_STATUS_NO_MEMORY;
2264 info->acc_granted = acc_granted;
2266 /* get a (unique) handle. open a policy on it. */
2267 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
2268 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2270 return NT_STATUS_OK;
2273 /*************************************************************************
2274 *************************************************************************/
2276 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2277 DATA_BLOB *blob,
2278 struct lsa_BinaryString **_r)
2280 struct lsa_BinaryString *r;
2282 if (!blob || !_r) {
2283 return NT_STATUS_INVALID_PARAMETER;
2286 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2287 if (!r) {
2288 return NT_STATUS_NO_MEMORY;
2291 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2292 if (!r->array) {
2293 return NT_STATUS_NO_MEMORY;
2295 memcpy(r->array, blob->data, blob->length);
2296 r->size = blob->length;
2297 r->length = blob->length;
2299 if (!r->array) {
2300 return NT_STATUS_NO_MEMORY;
2303 *_r = r;
2305 return NT_STATUS_OK;
2308 /*************************************************************************
2309 get_user_info_7. Safe. Only gives out account_name.
2310 *************************************************************************/
2312 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2313 struct samr_UserInfo7 *r,
2314 DOM_SID *user_sid)
2316 struct samu *smbpass=NULL;
2317 bool ret;
2318 const char *account_name = NULL;
2320 ZERO_STRUCTP(r);
2322 if ( !(smbpass = samu_new( mem_ctx )) ) {
2323 return NT_STATUS_NO_MEMORY;
2326 become_root();
2327 ret = pdb_getsampwsid(smbpass, user_sid);
2328 unbecome_root();
2330 if ( !ret ) {
2331 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2332 return NT_STATUS_NO_SUCH_USER;
2335 account_name = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2336 if (!account_name) {
2337 TALLOC_FREE(smbpass);
2338 return NT_STATUS_NO_MEMORY;
2340 TALLOC_FREE(smbpass);
2342 DEBUG(3,("User:[%s]\n", account_name));
2344 init_samr_user_info7(r, account_name);
2346 return NT_STATUS_OK;
2349 /*************************************************************************
2350 get_user_info_9. Only gives out primary group SID.
2351 *************************************************************************/
2353 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2354 struct samr_UserInfo9 *r,
2355 DOM_SID *user_sid)
2357 struct samu *smbpass=NULL;
2358 bool ret;
2360 ZERO_STRUCTP(r);
2362 if ( !(smbpass = samu_new( mem_ctx )) ) {
2363 return NT_STATUS_NO_MEMORY;
2366 become_root();
2367 ret = pdb_getsampwsid(smbpass, user_sid);
2368 unbecome_root();
2370 if (ret==False) {
2371 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2372 TALLOC_FREE(smbpass);
2373 return NT_STATUS_NO_SUCH_USER;
2376 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
2378 init_samr_user_info9(r, pdb_get_group_rid(smbpass));
2380 TALLOC_FREE(smbpass);
2382 return NT_STATUS_OK;
2385 /*************************************************************************
2386 get_user_info_16. Safe. Only gives out acb bits.
2387 *************************************************************************/
2389 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2390 struct samr_UserInfo16 *r,
2391 DOM_SID *user_sid)
2393 struct samu *smbpass=NULL;
2394 bool ret;
2396 ZERO_STRUCTP(r);
2398 if ( !(smbpass = samu_new( mem_ctx )) ) {
2399 return NT_STATUS_NO_MEMORY;
2402 become_root();
2403 ret = pdb_getsampwsid(smbpass, user_sid);
2404 unbecome_root();
2406 if (ret==False) {
2407 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2408 TALLOC_FREE(smbpass);
2409 return NT_STATUS_NO_SUCH_USER;
2412 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
2414 init_samr_user_info16(r, pdb_get_acct_ctrl(smbpass));
2416 TALLOC_FREE(smbpass);
2418 return NT_STATUS_OK;
2421 /*************************************************************************
2422 get_user_info_18. OK - this is the killer as it gives out password info.
2423 Ensure that this is only allowed on an encrypted connection with a root
2424 user. JRA.
2425 *************************************************************************/
2427 static NTSTATUS get_user_info_18(pipes_struct *p,
2428 TALLOC_CTX *mem_ctx,
2429 struct samr_UserInfo18 *r,
2430 DOM_SID *user_sid)
2432 struct samu *smbpass=NULL;
2433 bool ret;
2435 ZERO_STRUCTP(r);
2437 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2438 return NT_STATUS_ACCESS_DENIED;
2441 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
2442 return NT_STATUS_ACCESS_DENIED;
2446 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2449 if ( !(smbpass = samu_new( mem_ctx )) ) {
2450 return NT_STATUS_NO_MEMORY;
2453 ret = pdb_getsampwsid(smbpass, user_sid);
2455 if (ret == False) {
2456 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2457 TALLOC_FREE(smbpass);
2458 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2461 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2463 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2464 TALLOC_FREE(smbpass);
2465 return NT_STATUS_ACCOUNT_DISABLED;
2468 init_samr_user_info18(r, pdb_get_lanman_passwd(smbpass),
2469 pdb_get_nt_passwd(smbpass));
2471 TALLOC_FREE(smbpass);
2473 return NT_STATUS_OK;
2476 /*************************************************************************
2477 get_user_info_20
2478 *************************************************************************/
2480 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2481 struct samr_UserInfo20 *r,
2482 DOM_SID *user_sid)
2484 struct samu *sampass=NULL;
2485 bool ret;
2486 const char *munged_dial = NULL;
2487 DATA_BLOB blob;
2488 NTSTATUS status;
2489 struct lsa_BinaryString *parameters = NULL;
2491 ZERO_STRUCTP(r);
2493 if ( !(sampass = samu_new( mem_ctx )) ) {
2494 return NT_STATUS_NO_MEMORY;
2497 become_root();
2498 ret = pdb_getsampwsid(sampass, user_sid);
2499 unbecome_root();
2501 if (ret == False) {
2502 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2503 TALLOC_FREE(sampass);
2504 return NT_STATUS_NO_SUCH_USER;
2507 munged_dial = pdb_get_munged_dial(sampass);
2509 samr_clear_sam_passwd(sampass);
2511 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2512 munged_dial, (int)strlen(munged_dial)));
2514 if (munged_dial) {
2515 blob = base64_decode_data_blob(munged_dial);
2516 } else {
2517 blob = data_blob_string_const("");
2520 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2521 data_blob_free(&blob);
2522 TALLOC_FREE(sampass);
2523 if (!NT_STATUS_IS_OK(status)) {
2524 return status;
2527 init_samr_user_info20(r, parameters);
2529 return NT_STATUS_OK;
2533 /*************************************************************************
2534 get_user_info_21
2535 *************************************************************************/
2537 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2538 struct samr_UserInfo21 *r,
2539 DOM_SID *user_sid,
2540 DOM_SID *domain_sid)
2542 NTSTATUS status;
2543 struct samu *pw = NULL;
2544 bool ret;
2545 const DOM_SID *sid_user, *sid_group;
2546 uint32_t rid, primary_gid;
2547 NTTIME last_logon, last_logoff, last_password_change,
2548 acct_expiry, allow_password_change, force_password_change;
2549 time_t must_change_time;
2550 uint8_t password_expired;
2551 const char *account_name, *full_name, *home_directory, *home_drive,
2552 *logon_script, *profile_path, *description,
2553 *workstations, *comment;
2554 struct samr_LogonHours logon_hours;
2555 struct lsa_BinaryString *parameters = NULL;
2556 const char *munged_dial = NULL;
2557 DATA_BLOB blob;
2559 ZERO_STRUCTP(r);
2561 if (!(pw = samu_new(mem_ctx))) {
2562 return NT_STATUS_NO_MEMORY;
2565 become_root();
2566 ret = pdb_getsampwsid(pw, user_sid);
2567 unbecome_root();
2569 if (ret == False) {
2570 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2571 TALLOC_FREE(pw);
2572 return NT_STATUS_NO_SUCH_USER;
2575 samr_clear_sam_passwd(pw);
2577 DEBUG(3,("User:[%s]\n", pdb_get_username(pw)));
2579 sid_user = pdb_get_user_sid(pw);
2581 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2582 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2583 "the domain sid %s. Failing operation.\n",
2584 pdb_get_username(pw), sid_string_dbg(sid_user),
2585 sid_string_dbg(domain_sid)));
2586 TALLOC_FREE(pw);
2587 return NT_STATUS_UNSUCCESSFUL;
2590 become_root();
2591 sid_group = pdb_get_group_sid(pw);
2592 unbecome_root();
2594 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2595 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2596 "which conflicts with the domain sid %s. Failing operation.\n",
2597 pdb_get_username(pw), sid_string_dbg(sid_group),
2598 sid_string_dbg(domain_sid)));
2599 TALLOC_FREE(pw);
2600 return NT_STATUS_UNSUCCESSFUL;
2603 unix_to_nt_time(&last_logon, pdb_get_logon_time(pw));
2604 unix_to_nt_time(&last_logoff, pdb_get_logoff_time(pw));
2605 unix_to_nt_time(&acct_expiry, pdb_get_kickoff_time(pw));
2606 unix_to_nt_time(&last_password_change, pdb_get_pass_last_set_time(pw));
2607 unix_to_nt_time(&allow_password_change, pdb_get_pass_can_change_time(pw));
2609 must_change_time = pdb_get_pass_must_change_time(pw);
2610 if (must_change_time == get_time_t_max()) {
2611 unix_to_nt_time_abs(&force_password_change, must_change_time);
2612 } else {
2613 unix_to_nt_time(&force_password_change, must_change_time);
2616 if (pdb_get_pass_must_change_time(pw) == 0) {
2617 password_expired = PASS_MUST_CHANGE_AT_NEXT_LOGON;
2618 } else {
2619 password_expired = 0;
2622 munged_dial = pdb_get_munged_dial(pw);
2623 if (munged_dial) {
2624 blob = base64_decode_data_blob(munged_dial);
2625 } else {
2626 blob = data_blob_string_const("");
2629 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2630 data_blob_free(&blob);
2631 if (!NT_STATUS_IS_OK(status)) {
2632 TALLOC_FREE(pw);
2633 return status;
2636 account_name = talloc_strdup(mem_ctx, pdb_get_username(pw));
2637 full_name = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2638 home_directory = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2639 home_drive = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2640 logon_script = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2641 profile_path = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2642 description = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2643 workstations = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2644 comment = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2646 logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2647 #if 0
2650 Look at a user on a real NT4 PDC with usrmgr, press
2651 'ok'. Then you will see that fields_present is set to
2652 0x08f827fa. Look at the user immediately after that again,
2653 and you will see that 0x00fffff is returned. This solves
2654 the problem that you get access denied after having looked
2655 at the user.
2656 -- Volker
2659 #endif
2661 init_samr_user_info21(r,
2662 last_logon,
2663 last_logoff,
2664 last_password_change,
2665 acct_expiry,
2666 allow_password_change,
2667 force_password_change,
2668 account_name,
2669 full_name,
2670 home_directory,
2671 home_drive,
2672 logon_script,
2673 profile_path,
2674 description,
2675 workstations,
2676 comment,
2677 parameters,
2678 rid,
2679 primary_gid,
2680 pdb_get_acct_ctrl(pw),
2681 pdb_build_fields_present(pw),
2682 logon_hours,
2683 pdb_get_bad_password_count(pw),
2684 pdb_get_logon_count(pw),
2685 0, /* country_code */
2686 0, /* code_page */
2687 0, /* nt_password_set */
2688 0, /* lm_password_set */
2689 password_expired);
2690 TALLOC_FREE(pw);
2692 return NT_STATUS_OK;
2695 /*******************************************************************
2696 _samr_QueryUserInfo
2697 ********************************************************************/
2699 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2700 struct samr_QueryUserInfo *r)
2702 NTSTATUS status;
2703 union samr_UserInfo *user_info = NULL;
2704 struct samr_info *info = NULL;
2705 DOM_SID domain_sid;
2706 uint32 rid;
2708 /* search for the handle */
2709 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info))
2710 return NT_STATUS_INVALID_HANDLE;
2712 status = access_check_samr_function(info->acc_granted,
2713 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
2714 "_samr_QueryUserInfo");
2715 if (!NT_STATUS_IS_OK(status)) {
2716 return status;
2719 domain_sid = info->sid;
2721 sid_split_rid(&domain_sid, &rid);
2723 if (!sid_check_is_in_our_domain(&info->sid))
2724 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2726 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2727 sid_string_dbg(&info->sid)));
2729 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
2730 if (!user_info) {
2731 return NT_STATUS_NO_MEMORY;
2734 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
2736 switch (r->in.level) {
2737 case 7:
2738 status = get_user_info_7(p->mem_ctx, &user_info->info7, &info->sid);
2739 if (!NT_STATUS_IS_OK(status)) {
2740 return status;
2742 break;
2743 case 9:
2744 status = get_user_info_9(p->mem_ctx, &user_info->info9, &info->sid);
2745 if (!NT_STATUS_IS_OK(status)) {
2746 return status;
2748 break;
2749 case 16:
2750 status = get_user_info_16(p->mem_ctx, &user_info->info16, &info->sid);
2751 if (!NT_STATUS_IS_OK(status)) {
2752 return status;
2754 break;
2756 case 18:
2757 status = get_user_info_18(p, p->mem_ctx, &user_info->info18, &info->sid);
2758 if (!NT_STATUS_IS_OK(status)) {
2759 return status;
2761 break;
2763 case 20:
2764 status = get_user_info_20(p->mem_ctx, &user_info->info20, &info->sid);
2765 if (!NT_STATUS_IS_OK(status)) {
2766 return status;
2768 break;
2770 case 21:
2771 status = get_user_info_21(p->mem_ctx, &user_info->info21,
2772 &info->sid, &domain_sid);
2773 if (!NT_STATUS_IS_OK(status)) {
2774 return status;
2776 break;
2778 default:
2779 return NT_STATUS_INVALID_INFO_CLASS;
2782 *r->out.info = user_info;
2784 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
2786 return status;
2789 /*******************************************************************
2790 _samr_GetGroupsForUser
2791 ********************************************************************/
2793 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
2794 struct samr_GetGroupsForUser *r)
2796 struct samu *sam_pass=NULL;
2797 DOM_SID sid;
2798 DOM_SID *sids;
2799 struct samr_RidWithAttribute dom_gid;
2800 struct samr_RidWithAttribute *gids = NULL;
2801 uint32 primary_group_rid;
2802 size_t num_groups = 0;
2803 gid_t *unix_gids;
2804 size_t i, num_gids;
2805 uint32 acc_granted;
2806 bool ret;
2807 NTSTATUS result;
2808 bool success = False;
2810 struct samr_RidWithAttributeArray *rids = NULL;
2813 * from the SID in the request:
2814 * we should send back the list of DOMAIN GROUPS
2815 * the user is a member of
2817 * and only the DOMAIN GROUPS
2818 * no ALIASES !!! neither aliases of the domain
2819 * nor aliases of the builtin SID
2821 * JFM, 12/2/2001
2824 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2826 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
2827 if (!rids) {
2828 return NT_STATUS_NO_MEMORY;
2831 /* find the policy handle. open a policy on it. */
2832 if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &sid, &acc_granted, NULL))
2833 return NT_STATUS_INVALID_HANDLE;
2835 result = access_check_samr_function(acc_granted,
2836 SAMR_USER_ACCESS_GET_GROUPS,
2837 "_samr_GetGroupsForUser");
2838 if (!NT_STATUS_IS_OK(result)) {
2839 return result;
2842 if (!sid_check_is_in_our_domain(&sid))
2843 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2845 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2846 return NT_STATUS_NO_MEMORY;
2849 become_root();
2850 ret = pdb_getsampwsid(sam_pass, &sid);
2851 unbecome_root();
2853 if (!ret) {
2854 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2855 sid_string_dbg(&sid)));
2856 return NT_STATUS_NO_SUCH_USER;
2859 sids = NULL;
2861 /* make both calls inside the root block */
2862 become_root();
2863 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2864 &sids, &unix_gids, &num_groups);
2865 if ( NT_STATUS_IS_OK(result) ) {
2866 success = sid_peek_check_rid(get_global_sam_sid(),
2867 pdb_get_group_sid(sam_pass),
2868 &primary_group_rid);
2870 unbecome_root();
2872 if (!NT_STATUS_IS_OK(result)) {
2873 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2874 sid_string_dbg(&sid)));
2875 return result;
2878 if ( !success ) {
2879 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2880 sid_string_dbg(pdb_get_group_sid(sam_pass)),
2881 pdb_get_username(sam_pass)));
2882 TALLOC_FREE(sam_pass);
2883 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2886 gids = NULL;
2887 num_gids = 0;
2889 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2890 SE_GROUP_ENABLED);
2891 dom_gid.rid = primary_group_rid;
2892 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2894 for (i=0; i<num_groups; i++) {
2896 if (!sid_peek_check_rid(get_global_sam_sid(),
2897 &(sids[i]), &dom_gid.rid)) {
2898 DEBUG(10, ("Found sid %s not in our domain\n",
2899 sid_string_dbg(&sids[i])));
2900 continue;
2903 if (dom_gid.rid == primary_group_rid) {
2904 /* We added the primary group directly from the
2905 * sam_account. The other SIDs are unique from
2906 * enum_group_memberships */
2907 continue;
2910 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2913 rids->count = num_gids;
2914 rids->rids = gids;
2916 *r->out.rids = rids;
2918 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2920 return result;
2923 /*******************************************************************
2924 _samr_QueryDomainInfo
2925 ********************************************************************/
2927 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
2928 struct samr_QueryDomainInfo *r)
2930 NTSTATUS status = NT_STATUS_OK;
2931 struct samr_info *info = NULL;
2932 union samr_DomainInfo *dom_info;
2933 uint32 min_pass_len,pass_hist,password_properties;
2934 time_t u_expire, u_min_age;
2935 NTTIME nt_expire, nt_min_age;
2937 time_t u_lock_duration, u_reset_time;
2938 NTTIME nt_lock_duration, nt_reset_time;
2939 uint32 lockout;
2940 time_t u_logout;
2941 NTTIME nt_logout;
2943 uint32 account_policy_temp;
2945 time_t seq_num;
2946 uint32 server_role;
2948 uint32 num_users=0, num_groups=0, num_aliases=0;
2950 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
2952 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
2953 if (!dom_info) {
2954 return NT_STATUS_NO_MEMORY;
2957 /* find the policy handle. open a policy on it. */
2958 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info)) {
2959 return NT_STATUS_INVALID_HANDLE;
2962 status = access_check_samr_function(info->acc_granted,
2963 SAMR_ACCESS_OPEN_DOMAIN,
2964 "_samr_QueryDomainInfo" );
2966 if ( !NT_STATUS_IS_OK(status) )
2967 return status;
2969 switch (r->in.level) {
2970 case 0x01:
2972 become_root();
2974 /* AS ROOT !!! */
2976 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2977 min_pass_len = account_policy_temp;
2979 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2980 pass_hist = account_policy_temp;
2982 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2983 password_properties = account_policy_temp;
2985 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2986 u_expire = account_policy_temp;
2988 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2989 u_min_age = account_policy_temp;
2991 /* !AS ROOT */
2993 unbecome_root();
2995 unix_to_nt_time_abs(&nt_expire, u_expire);
2996 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2998 if (lp_check_password_script() && *lp_check_password_script()) {
2999 password_properties |= DOMAIN_PASSWORD_COMPLEX;
3002 init_samr_DomInfo1(&dom_info->info1,
3003 (uint16)min_pass_len,
3004 (uint16)pass_hist,
3005 password_properties,
3006 nt_expire,
3007 nt_min_age);
3008 break;
3009 case 0x02:
3011 become_root();
3013 /* AS ROOT !!! */
3015 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
3016 num_groups = count_sam_groups(info->disp_info);
3017 num_aliases = count_sam_aliases(info->disp_info);
3019 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
3020 u_logout = account_policy_temp;
3022 unix_to_nt_time_abs(&nt_logout, u_logout);
3024 if (!pdb_get_seq_num(&seq_num))
3025 seq_num = time(NULL);
3027 /* !AS ROOT */
3029 unbecome_root();
3031 server_role = ROLE_DOMAIN_PDC;
3032 if (lp_server_role() == ROLE_DOMAIN_BDC)
3033 server_role = ROLE_DOMAIN_BDC;
3035 init_samr_DomInfo2(&dom_info->info2,
3036 nt_logout,
3037 lp_serverstring(),
3038 lp_workgroup(),
3039 global_myname(),
3040 seq_num,
3042 server_role,
3044 num_users,
3045 num_groups,
3046 num_aliases);
3047 break;
3048 case 0x03:
3050 become_root();
3052 /* AS ROOT !!! */
3055 uint32 ul;
3056 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
3057 u_logout = (time_t)ul;
3060 /* !AS ROOT */
3062 unbecome_root();
3064 unix_to_nt_time_abs(&nt_logout, u_logout);
3066 init_samr_DomInfo3(&dom_info->info3,
3067 nt_logout);
3069 break;
3070 case 0x04:
3071 init_samr_DomInfo4(&dom_info->info4,
3072 lp_serverstring());
3073 break;
3074 case 0x05:
3075 init_samr_DomInfo5(&dom_info->info5,
3076 get_global_sam_name());
3077 break;
3078 case 0x06:
3079 /* NT returns its own name when a PDC. win2k and later
3080 * only the name of the PDC if itself is a BDC (samba4
3081 * idl) */
3082 init_samr_DomInfo6(&dom_info->info6,
3083 global_myname());
3084 break;
3085 case 0x07:
3086 server_role = ROLE_DOMAIN_PDC;
3087 if (lp_server_role() == ROLE_DOMAIN_BDC)
3088 server_role = ROLE_DOMAIN_BDC;
3090 init_samr_DomInfo7(&dom_info->info7,
3091 server_role);
3092 break;
3093 case 0x08:
3095 become_root();
3097 /* AS ROOT !!! */
3099 if (!pdb_get_seq_num(&seq_num)) {
3100 seq_num = time(NULL);
3103 /* !AS ROOT */
3105 unbecome_root();
3107 init_samr_DomInfo8(&dom_info->info8,
3108 seq_num,
3110 break;
3111 case 0x0c:
3113 become_root();
3115 /* AS ROOT !!! */
3117 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3118 u_lock_duration = account_policy_temp;
3119 if (u_lock_duration != -1) {
3120 u_lock_duration *= 60;
3123 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
3124 u_reset_time = account_policy_temp * 60;
3126 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3127 lockout = account_policy_temp;
3129 /* !AS ROOT */
3131 unbecome_root();
3133 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
3134 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
3136 init_samr_DomInfo12(&dom_info->info12,
3137 nt_lock_duration,
3138 nt_reset_time,
3139 (uint16)lockout);
3140 break;
3141 default:
3142 return NT_STATUS_INVALID_INFO_CLASS;
3145 *r->out.info = dom_info;
3147 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3149 return status;
3152 /* W2k3 seems to use the same check for all 3 objects that can be created via
3153 * SAMR, if you try to create for example "Dialup" as an alias it says
3154 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3155 * database. */
3157 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3159 enum lsa_SidType type;
3160 bool result;
3162 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3164 become_root();
3165 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3166 * whether the name already exists */
3167 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3168 NULL, NULL, NULL, &type);
3169 unbecome_root();
3171 if (!result) {
3172 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3173 return NT_STATUS_OK;
3176 DEBUG(5, ("trying to create %s, exists as %s\n",
3177 new_name, sid_type_lookup(type)));
3179 if (type == SID_NAME_DOM_GRP) {
3180 return NT_STATUS_GROUP_EXISTS;
3182 if (type == SID_NAME_ALIAS) {
3183 return NT_STATUS_ALIAS_EXISTS;
3186 /* Yes, the default is NT_STATUS_USER_EXISTS */
3187 return NT_STATUS_USER_EXISTS;
3190 /*******************************************************************
3191 _samr_CreateUser2
3192 ********************************************************************/
3194 NTSTATUS _samr_CreateUser2(pipes_struct *p,
3195 struct samr_CreateUser2 *r)
3197 const char *account = NULL;
3198 DOM_SID sid;
3199 POLICY_HND dom_pol = *r->in.domain_handle;
3200 uint32_t acb_info = r->in.acct_flags;
3201 POLICY_HND *user_pol = r->out.user_handle;
3202 struct samr_info *info = NULL;
3203 NTSTATUS nt_status;
3204 uint32 acc_granted;
3205 SEC_DESC *psd;
3206 size_t sd_size;
3207 /* check this, when giving away 'add computer to domain' privs */
3208 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3209 bool can_add_account = False;
3210 SE_PRIV se_rights;
3211 DISP_INFO *disp_info = NULL;
3213 /* Get the domain SID stored in the domain policy */
3214 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
3215 &disp_info))
3216 return NT_STATUS_INVALID_HANDLE;
3218 nt_status = access_check_samr_function(acc_granted,
3219 SAMR_DOMAIN_ACCESS_CREATE_USER,
3220 "_samr_CreateUser2");
3221 if (!NT_STATUS_IS_OK(nt_status)) {
3222 return nt_status;
3225 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3226 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3227 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3228 this parameter is not an account type */
3229 return NT_STATUS_INVALID_PARAMETER;
3232 account = r->in.account_name->string;
3233 if (account == NULL) {
3234 return NT_STATUS_NO_MEMORY;
3237 nt_status = can_create(p->mem_ctx, account);
3238 if (!NT_STATUS_IS_OK(nt_status)) {
3239 return nt_status;
3242 /* determine which user right we need to check based on the acb_info */
3244 if ( acb_info & ACB_WSTRUST )
3246 se_priv_copy( &se_rights, &se_machine_account );
3247 can_add_account = user_has_privileges(
3248 p->pipe_user.nt_user_token, &se_rights );
3250 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3251 account for domain trusts and changes the ACB flags later */
3252 else if ( acb_info & ACB_NORMAL &&
3253 (account[strlen(account)-1] != '$') )
3255 se_priv_copy( &se_rights, &se_add_users );
3256 can_add_account = user_has_privileges(
3257 p->pipe_user.nt_user_token, &se_rights );
3259 else /* implicit assumption of a BDC or domain trust account here
3260 * (we already check the flags earlier) */
3262 if ( lp_enable_privileges() ) {
3263 /* only Domain Admins can add a BDC or domain trust */
3264 se_priv_copy( &se_rights, &se_priv_none );
3265 can_add_account = nt_token_check_domain_rid(
3266 p->pipe_user.nt_user_token,
3267 DOMAIN_GROUP_RID_ADMINS );
3271 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3272 uidtoname(p->pipe_user.ut.uid),
3273 can_add_account ? "True":"False" ));
3275 /********** BEGIN Admin BLOCK **********/
3277 if ( can_add_account )
3278 become_root();
3280 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3281 r->out.rid);
3283 if ( can_add_account )
3284 unbecome_root();
3286 /********** END Admin BLOCK **********/
3288 /* now check for failure */
3290 if ( !NT_STATUS_IS_OK(nt_status) )
3291 return nt_status;
3293 /* Get the user's SID */
3295 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3297 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3299 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3300 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3301 se_map_generic(&des_access, &usr_generic_mapping);
3303 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3304 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
3305 &acc_granted, "_samr_CreateUser2");
3307 if ( !NT_STATUS_IS_OK(nt_status) ) {
3308 return nt_status;
3311 /* associate the user's SID with the new handle. */
3312 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
3313 return NT_STATUS_NO_MEMORY;
3316 ZERO_STRUCTP(info);
3317 info->sid = sid;
3318 info->acc_granted = acc_granted;
3320 /* get a (unique) handle. open a policy on it. */
3321 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
3322 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3325 /* After a "set" ensure we have no cached display info. */
3326 force_flush_samr_cache(info->disp_info);
3328 *r->out.access_granted = acc_granted;
3330 return NT_STATUS_OK;
3333 /*******************************************************************
3334 _samr_Connect
3335 ********************************************************************/
3337 NTSTATUS _samr_Connect(pipes_struct *p,
3338 struct samr_Connect *r)
3340 struct samr_info *info = NULL;
3341 uint32 des_access = r->in.access_mask;
3343 /* Access check */
3345 if (!pipe_access_check(p)) {
3346 DEBUG(3, ("access denied to _samr_Connect\n"));
3347 return NT_STATUS_ACCESS_DENIED;
3350 /* set up the SAMR connect_anon response */
3352 /* associate the user's SID with the new handle. */
3353 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3354 return NT_STATUS_NO_MEMORY;
3356 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3357 was observed from a win98 client trying to enumerate users (when configured
3358 user level access control on shares) --jerry */
3360 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3362 se_map_generic( &des_access, &sam_generic_mapping );
3363 info->acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS|SAMR_ACCESS_OPEN_DOMAIN);
3365 /* get a (unique) handle. open a policy on it. */
3366 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3367 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3369 return NT_STATUS_OK;
3372 /*******************************************************************
3373 _samr_Connect2
3374 ********************************************************************/
3376 NTSTATUS _samr_Connect2(pipes_struct *p,
3377 struct samr_Connect2 *r)
3379 struct samr_info *info = NULL;
3380 SEC_DESC *psd = NULL;
3381 uint32 acc_granted;
3382 uint32 des_access = r->in.access_mask;
3383 NTSTATUS nt_status;
3384 size_t sd_size;
3387 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3389 /* Access check */
3391 if (!pipe_access_check(p)) {
3392 DEBUG(3, ("access denied to _samr_Connect2\n"));
3393 return NT_STATUS_ACCESS_DENIED;
3396 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3398 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3399 se_map_generic(&des_access, &sam_generic_mapping);
3401 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3402 NULL, 0, des_access, &acc_granted, "_samr_Connect2");
3404 if ( !NT_STATUS_IS_OK(nt_status) )
3405 return nt_status;
3407 /* associate the user's SID and access granted with the new handle. */
3408 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3409 return NT_STATUS_NO_MEMORY;
3411 info->acc_granted = acc_granted;
3412 info->status = r->in.access_mask; /* this looks so wrong... - gd */
3414 /* get a (unique) handle. open a policy on it. */
3415 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3416 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3418 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3420 return nt_status;
3423 /*******************************************************************
3424 _samr_Connect4
3425 ********************************************************************/
3427 NTSTATUS _samr_Connect4(pipes_struct *p,
3428 struct samr_Connect4 *r)
3430 struct samr_info *info = NULL;
3431 SEC_DESC *psd = NULL;
3432 uint32 acc_granted;
3433 uint32 des_access = r->in.access_mask;
3434 NTSTATUS nt_status;
3435 size_t sd_size;
3438 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3440 /* Access check */
3442 if (!pipe_access_check(p)) {
3443 DEBUG(3, ("access denied to samr_Connect4\n"));
3444 return NT_STATUS_ACCESS_DENIED;
3447 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3449 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3450 se_map_generic(&des_access, &sam_generic_mapping);
3452 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3453 NULL, 0, des_access, &acc_granted, "_samr_Connect4");
3455 if ( !NT_STATUS_IS_OK(nt_status) )
3456 return nt_status;
3458 /* associate the user's SID and access granted with the new handle. */
3459 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3460 return NT_STATUS_NO_MEMORY;
3462 info->acc_granted = acc_granted;
3463 info->status = r->in.access_mask; /* ??? */
3465 /* get a (unique) handle. open a policy on it. */
3466 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3467 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3469 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3471 return NT_STATUS_OK;
3474 /*******************************************************************
3475 _samr_Connect5
3476 ********************************************************************/
3478 NTSTATUS _samr_Connect5(pipes_struct *p,
3479 struct samr_Connect5 *r)
3481 struct samr_info *info = NULL;
3482 SEC_DESC *psd = NULL;
3483 uint32 acc_granted;
3484 uint32 des_access = r->in.access_mask;
3485 NTSTATUS nt_status;
3486 size_t sd_size;
3487 struct samr_ConnectInfo1 info1;
3489 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3491 /* Access check */
3493 if (!pipe_access_check(p)) {
3494 DEBUG(3, ("access denied to samr_Connect5\n"));
3495 return NT_STATUS_ACCESS_DENIED;
3498 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3500 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3501 se_map_generic(&des_access, &sam_generic_mapping);
3503 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3504 NULL, 0, des_access, &acc_granted, "_samr_Connect5");
3506 if ( !NT_STATUS_IS_OK(nt_status) )
3507 return nt_status;
3509 /* associate the user's SID and access granted with the new handle. */
3510 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3511 return NT_STATUS_NO_MEMORY;
3513 info->acc_granted = acc_granted;
3514 info->status = r->in.access_mask; /* ??? */
3516 /* get a (unique) handle. open a policy on it. */
3517 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3518 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3520 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3522 info1.client_version = SAMR_CONNECT_AFTER_W2K;
3523 info1.unknown2 = 0;
3525 *r->out.level_out = 1;
3526 r->out.info_out->info1 = info1;
3528 return NT_STATUS_OK;
3531 /**********************************************************************
3532 _samr_LookupDomain
3533 **********************************************************************/
3535 NTSTATUS _samr_LookupDomain(pipes_struct *p,
3536 struct samr_LookupDomain *r)
3538 NTSTATUS status = NT_STATUS_OK;
3539 struct samr_info *info;
3540 const char *domain_name;
3541 DOM_SID *sid = NULL;
3543 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3544 return NT_STATUS_INVALID_HANDLE;
3546 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
3547 Reverted that change so we will work with RAS servers again */
3549 status = access_check_samr_function(info->acc_granted,
3550 SAMR_ACCESS_OPEN_DOMAIN,
3551 "_samr_LookupDomain");
3552 if (!NT_STATUS_IS_OK(status)) {
3553 return status;
3556 domain_name = r->in.domain_name->string;
3558 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
3559 if (!sid) {
3560 return NT_STATUS_NO_MEMORY;
3563 if (strequal(domain_name, builtin_domain_name())) {
3564 sid_copy(sid, &global_sid_Builtin);
3565 } else {
3566 if (!secrets_fetch_domain_sid(domain_name, sid)) {
3567 status = NT_STATUS_NO_SUCH_DOMAIN;
3571 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
3572 sid_string_dbg(sid)));
3574 *r->out.sid = sid;
3576 return status;
3579 /**********************************************************************
3580 _samr_EnumDomains
3581 **********************************************************************/
3583 NTSTATUS _samr_EnumDomains(pipes_struct *p,
3584 struct samr_EnumDomains *r)
3586 NTSTATUS status;
3587 struct samr_info *info;
3588 uint32_t num_entries = 2;
3589 struct samr_SamEntry *entry_array = NULL;
3590 struct samr_SamArray *sam;
3592 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3593 return NT_STATUS_INVALID_HANDLE;
3595 status = access_check_samr_function(info->acc_granted,
3596 SAMR_ACCESS_ENUM_DOMAINS,
3597 "_samr_EnumDomains");
3598 if (!NT_STATUS_IS_OK(status)) {
3599 return status;
3602 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
3603 if (!sam) {
3604 return NT_STATUS_NO_MEMORY;
3607 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
3608 struct samr_SamEntry,
3609 num_entries);
3610 if (!entry_array) {
3611 return NT_STATUS_NO_MEMORY;
3614 entry_array[0].idx = 0;
3615 init_lsa_String(&entry_array[0].name, get_global_sam_name());
3617 entry_array[1].idx = 1;
3618 init_lsa_String(&entry_array[1].name, "Builtin");
3620 sam->count = num_entries;
3621 sam->entries = entry_array;
3623 *r->out.sam = sam;
3624 *r->out.num_entries = num_entries;
3626 return status;
3629 /*******************************************************************
3630 _samr_OpenAlias
3631 ********************************************************************/
3633 NTSTATUS _samr_OpenAlias(pipes_struct *p,
3634 struct samr_OpenAlias *r)
3636 DOM_SID sid;
3637 POLICY_HND domain_pol = *r->in.domain_handle;
3638 uint32 alias_rid = r->in.rid;
3639 POLICY_HND *alias_pol = r->out.alias_handle;
3640 struct samr_info *info = NULL;
3641 SEC_DESC *psd = NULL;
3642 uint32 acc_granted;
3643 uint32 des_access = r->in.access_mask;
3644 size_t sd_size;
3645 NTSTATUS status;
3646 SE_PRIV se_rights;
3648 /* find the domain policy and get the SID / access bits stored in the domain policy */
3650 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
3651 return NT_STATUS_INVALID_HANDLE;
3653 status = access_check_samr_function(acc_granted,
3654 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
3655 "_samr_OpenAlias");
3657 if ( !NT_STATUS_IS_OK(status) )
3658 return status;
3660 /* append the alias' RID to it */
3662 if (!sid_append_rid(&sid, alias_rid))
3663 return NT_STATUS_NO_SUCH_ALIAS;
3665 /*check if access can be granted as requested by client. */
3667 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3669 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
3670 se_map_generic(&des_access,&ali_generic_mapping);
3672 se_priv_copy( &se_rights, &se_add_users );
3675 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3676 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
3677 &acc_granted, "_samr_OpenAlias");
3679 if ( !NT_STATUS_IS_OK(status) )
3680 return status;
3683 /* Check we actually have the requested alias */
3684 enum lsa_SidType type;
3685 bool result;
3686 gid_t gid;
3688 become_root();
3689 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
3690 unbecome_root();
3692 if (!result || (type != SID_NAME_ALIAS)) {
3693 return NT_STATUS_NO_SUCH_ALIAS;
3696 /* make sure there is a mapping */
3698 if ( !sid_to_gid( &sid, &gid ) ) {
3699 return NT_STATUS_NO_SUCH_ALIAS;
3704 /* associate the alias SID with the new handle. */
3705 if ((info = get_samr_info_by_sid(&sid)) == NULL)
3706 return NT_STATUS_NO_MEMORY;
3708 info->acc_granted = acc_granted;
3710 /* get a (unique) handle. open a policy on it. */
3711 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
3712 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3714 return NT_STATUS_OK;
3717 /*******************************************************************
3718 set_user_info_7
3719 ********************************************************************/
3721 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
3722 struct samr_UserInfo7 *id7,
3723 struct samu *pwd)
3725 NTSTATUS rc;
3727 if (id7 == NULL) {
3728 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3729 TALLOC_FREE(pwd);
3730 return NT_STATUS_ACCESS_DENIED;
3733 if (!id7->account_name.string) {
3734 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3735 TALLOC_FREE(pwd);
3736 return NT_STATUS_ACCESS_DENIED;
3739 /* check to see if the new username already exists. Note: we can't
3740 reliably lock all backends, so there is potentially the
3741 possibility that a user can be created in between this check and
3742 the rename. The rename should fail, but may not get the
3743 exact same failure status code. I think this is small enough
3744 of a window for this type of operation and the results are
3745 simply that the rename fails with a slightly different status
3746 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3748 rc = can_create(mem_ctx, id7->account_name.string);
3749 if (!NT_STATUS_IS_OK(rc)) {
3750 return rc;
3753 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
3755 TALLOC_FREE(pwd);
3756 return rc;
3759 /*******************************************************************
3760 set_user_info_16
3761 ********************************************************************/
3763 static bool set_user_info_16(struct samr_UserInfo16 *id16,
3764 struct samu *pwd)
3766 if (id16 == NULL) {
3767 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3768 TALLOC_FREE(pwd);
3769 return False;
3772 /* FIX ME: check if the value is really changed --metze */
3773 if (!pdb_set_acct_ctrl(pwd, id16->acct_flags, PDB_CHANGED)) {
3774 TALLOC_FREE(pwd);
3775 return False;
3778 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3779 TALLOC_FREE(pwd);
3780 return False;
3783 TALLOC_FREE(pwd);
3785 return True;
3788 /*******************************************************************
3789 set_user_info_18
3790 ********************************************************************/
3792 static bool set_user_info_18(struct samr_UserInfo18 *id18,
3793 struct samu *pwd)
3795 if (id18 == NULL) {
3796 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3797 TALLOC_FREE(pwd);
3798 return False;
3801 if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd.hash, PDB_CHANGED)) {
3802 TALLOC_FREE(pwd);
3803 return False;
3805 if (!pdb_set_nt_passwd (pwd, id18->nt_pwd.hash, PDB_CHANGED)) {
3806 TALLOC_FREE(pwd);
3807 return False;
3809 if (!pdb_set_pass_last_set_time (pwd, time(NULL), PDB_CHANGED)) {
3810 TALLOC_FREE(pwd);
3811 return False;
3814 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3815 TALLOC_FREE(pwd);
3816 return False;
3819 TALLOC_FREE(pwd);
3820 return True;
3823 /*******************************************************************
3824 set_user_info_20
3825 ********************************************************************/
3827 static bool set_user_info_20(struct samr_UserInfo20 *id20,
3828 struct samu *pwd)
3830 if (id20 == NULL) {
3831 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3832 return False;
3835 copy_id20_to_sam_passwd(pwd, id20);
3837 /* write the change out */
3838 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3839 TALLOC_FREE(pwd);
3840 return False;
3843 TALLOC_FREE(pwd);
3845 return True;
3848 /*******************************************************************
3849 set_user_info_21
3850 ********************************************************************/
3852 static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
3853 struct samr_UserInfo21 *id21,
3854 struct samu *pwd)
3856 NTSTATUS status;
3858 if (id21 == NULL) {
3859 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3860 return NT_STATUS_INVALID_PARAMETER;
3863 /* we need to separately check for an account rename first */
3865 if (id21->account_name.string &&
3866 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
3869 /* check to see if the new username already exists. Note: we can't
3870 reliably lock all backends, so there is potentially the
3871 possibility that a user can be created in between this check and
3872 the rename. The rename should fail, but may not get the
3873 exact same failure status code. I think this is small enough
3874 of a window for this type of operation and the results are
3875 simply that the rename fails with a slightly different status
3876 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3878 status = can_create(mem_ctx, id21->account_name.string);
3879 if (!NT_STATUS_IS_OK(status)) {
3880 return status;
3883 status = pdb_rename_sam_account(pwd, id21->account_name.string);
3885 if (!NT_STATUS_IS_OK(status)) {
3886 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3887 nt_errstr(status)));
3888 TALLOC_FREE(pwd);
3889 return status;
3892 /* set the new username so that later
3893 functions can work on the new account */
3894 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
3897 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
3900 * The funny part about the previous two calls is
3901 * that pwd still has the password hashes from the
3902 * passdb entry. These have not been updated from
3903 * id21. I don't know if they need to be set. --jerry
3906 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3907 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3908 if ( !NT_STATUS_IS_OK(status) ) {
3909 return status;
3913 /* Don't worry about writing out the user account since the
3914 primary group SID is generated solely from the user's Unix
3915 primary group. */
3917 /* write the change out */
3918 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3919 TALLOC_FREE(pwd);
3920 return status;
3923 TALLOC_FREE(pwd);
3925 return NT_STATUS_OK;
3928 /*******************************************************************
3929 set_user_info_23
3930 ********************************************************************/
3932 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
3933 struct samr_UserInfo23 *id23,
3934 struct samu *pwd)
3936 char *plaintext_buf = NULL;
3937 uint32 len = 0;
3938 uint16 acct_ctrl;
3939 NTSTATUS status;
3941 if (id23 == NULL) {
3942 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3943 return NT_STATUS_INVALID_PARAMETER;
3946 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3947 pdb_get_username(pwd)));
3949 acct_ctrl = pdb_get_acct_ctrl(pwd);
3951 if (!decode_pw_buffer(mem_ctx,
3952 id23->password.data,
3953 &plaintext_buf,
3954 &len,
3955 STR_UNICODE)) {
3956 TALLOC_FREE(pwd);
3957 return NT_STATUS_INVALID_PARAMETER;
3960 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3961 TALLOC_FREE(pwd);
3962 return NT_STATUS_ACCESS_DENIED;
3965 copy_id23_to_sam_passwd(pwd, id23);
3967 /* if it's a trust account, don't update /etc/passwd */
3968 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3969 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3970 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3971 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
3972 } else {
3973 /* update the UNIX password */
3974 if (lp_unix_password_sync() ) {
3975 struct passwd *passwd;
3976 if (pdb_get_username(pwd) == NULL) {
3977 DEBUG(1, ("chgpasswd: User without name???\n"));
3978 TALLOC_FREE(pwd);
3979 return NT_STATUS_ACCESS_DENIED;
3982 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
3983 if (passwd == NULL) {
3984 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3987 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3988 TALLOC_FREE(pwd);
3989 return NT_STATUS_ACCESS_DENIED;
3991 TALLOC_FREE(passwd);
3995 memset(plaintext_buf, '\0', strlen(plaintext_buf));
3997 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
3998 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
3999 pwd)))) {
4000 TALLOC_FREE(pwd);
4001 return status;
4004 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4005 TALLOC_FREE(pwd);
4006 return status;
4009 TALLOC_FREE(pwd);
4011 return NT_STATUS_OK;
4014 /*******************************************************************
4015 set_user_info_pw
4016 ********************************************************************/
4018 static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
4019 int level)
4021 uint32 len = 0;
4022 char *plaintext_buf = NULL;
4023 uint32 acct_ctrl;
4024 time_t last_set_time;
4025 enum pdb_value_state last_set_state;
4027 DEBUG(5, ("Attempting administrator password change for user %s\n",
4028 pdb_get_username(pwd)));
4030 acct_ctrl = pdb_get_acct_ctrl(pwd);
4031 /* we need to know if it's expired, because this is an admin change, not a
4032 user change, so it's still expired when we're done */
4033 last_set_state = pdb_get_init_flags(pwd, PDB_PASSLASTSET);
4034 last_set_time = pdb_get_pass_last_set_time(pwd);
4036 if (!decode_pw_buffer(talloc_tos(),
4037 pass,
4038 &plaintext_buf,
4039 &len,
4040 STR_UNICODE)) {
4041 TALLOC_FREE(pwd);
4042 return False;
4045 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4046 TALLOC_FREE(pwd);
4047 return False;
4050 /* if it's a trust account, don't update /etc/passwd */
4051 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4052 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4053 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4054 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4055 } else {
4056 /* update the UNIX password */
4057 if (lp_unix_password_sync()) {
4058 struct passwd *passwd;
4060 if (pdb_get_username(pwd) == NULL) {
4061 DEBUG(1, ("chgpasswd: User without name???\n"));
4062 TALLOC_FREE(pwd);
4063 return False;
4066 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4067 if (passwd == NULL) {
4068 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4071 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4072 TALLOC_FREE(pwd);
4073 return False;
4075 TALLOC_FREE(passwd);
4079 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4082 * A level 25 change does reset the pwdlastset field, a level 24
4083 * change does not. I know this is probably not the full story, but
4084 * it is needed to make XP join LDAP correctly, without it the later
4085 * auth2 check can fail with PWD_MUST_CHANGE.
4087 if (level != 25) {
4089 * restore last set time as this is an admin change, not a
4090 * user pw change
4092 pdb_set_pass_last_set_time (pwd, last_set_time,
4093 last_set_state);
4096 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4098 /* update the SAMBA password */
4099 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
4100 TALLOC_FREE(pwd);
4101 return False;
4104 TALLOC_FREE(pwd);
4106 return True;
4109 /*******************************************************************
4110 set_user_info_25
4111 ********************************************************************/
4113 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4114 struct samr_UserInfo25 *id25,
4115 struct samu *pwd)
4117 NTSTATUS status;
4119 if (id25 == NULL) {
4120 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4121 return NT_STATUS_INVALID_PARAMETER;
4124 copy_id25_to_sam_passwd(pwd, id25);
4126 /* write the change out */
4127 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4128 TALLOC_FREE(pwd);
4129 return status;
4133 * We need to "pdb_update_sam_account" before the unix primary group
4134 * is set, because the idealx scripts would also change the
4135 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4136 * the delete explicit / add explicit, which would then fail to find
4137 * the previous primaryGroupSid value.
4140 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4141 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4142 if ( !NT_STATUS_IS_OK(status) ) {
4143 return status;
4147 /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
4148 * hereafter! */
4150 return NT_STATUS_OK;
4153 /*******************************************************************
4154 samr_SetUserInfo
4155 ********************************************************************/
4157 NTSTATUS _samr_SetUserInfo(pipes_struct *p,
4158 struct samr_SetUserInfo *r)
4160 NTSTATUS status;
4161 struct samu *pwd = NULL;
4162 DOM_SID sid;
4163 POLICY_HND *pol = r->in.user_handle;
4164 union samr_UserInfo *info = r->in.info;
4165 uint16_t switch_value = r->in.level;
4166 uint32_t acc_granted;
4167 uint32_t acc_required;
4168 bool ret;
4169 bool has_enough_rights = False;
4170 uint32_t acb_info;
4171 DISP_INFO *disp_info = NULL;
4173 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
4175 /* find the policy handle. open a policy on it. */
4176 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info)) {
4177 return NT_STATUS_INVALID_HANDLE;
4180 /* This is tricky. A WinXP domain join sets
4181 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
4182 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
4183 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
4184 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
4185 we'll use the set from the WinXP join as the basis. */
4187 switch (switch_value) {
4188 case 18:
4189 case 24:
4190 case 25:
4191 case 26:
4192 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
4193 break;
4194 default:
4195 acc_required = SAMR_USER_ACCESS_SET_PASSWORD |
4196 SAMR_USER_ACCESS_SET_ATTRIBUTES |
4197 SAMR_USER_ACCESS_GET_ATTRIBUTES;
4198 break;
4201 status = access_check_samr_function(acc_granted,
4202 acc_required,
4203 "_samr_SetUserInfo");
4204 if (!NT_STATUS_IS_OK(status)) {
4205 return status;
4208 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
4209 sid_string_dbg(&sid), switch_value));
4211 if (info == NULL) {
4212 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
4213 return NT_STATUS_INVALID_INFO_CLASS;
4216 if (!(pwd = samu_new(NULL))) {
4217 return NT_STATUS_NO_MEMORY;
4220 become_root();
4221 ret = pdb_getsampwsid(pwd, &sid);
4222 unbecome_root();
4224 if (!ret) {
4225 TALLOC_FREE(pwd);
4226 return NT_STATUS_NO_SUCH_USER;
4229 /* deal with machine password changes differently from userinfo changes */
4230 /* check to see if we have the sufficient rights */
4232 acb_info = pdb_get_acct_ctrl(pwd);
4233 if (acb_info & ACB_WSTRUST)
4234 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4235 &se_machine_account);
4236 else if (acb_info & ACB_NORMAL)
4237 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4238 &se_add_users);
4239 else if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
4240 if (lp_enable_privileges()) {
4241 has_enough_rights = nt_token_check_domain_rid(p->pipe_user.nt_user_token,
4242 DOMAIN_GROUP_RID_ADMINS);
4246 DEBUG(5, ("_samr_SetUserInfo: %s does%s possess sufficient rights\n",
4247 uidtoname(p->pipe_user.ut.uid),
4248 has_enough_rights ? "" : " not"));
4250 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
4252 if (has_enough_rights) {
4253 become_root();
4256 /* ok! user info levels (lots: see MSDEV help), off we go... */
4258 switch (switch_value) {
4260 case 7:
4261 status = set_user_info_7(p->mem_ctx,
4262 &info->info7, pwd);
4263 break;
4265 case 16:
4266 if (!set_user_info_16(&info->info16, pwd)) {
4267 status = NT_STATUS_ACCESS_DENIED;
4269 break;
4271 case 18:
4272 /* Used by AS/U JRA. */
4273 if (!set_user_info_18(&info->info18, pwd)) {
4274 status = NT_STATUS_ACCESS_DENIED;
4276 break;
4278 case 20:
4279 if (!set_user_info_20(&info->info20, pwd)) {
4280 status = NT_STATUS_ACCESS_DENIED;
4282 break;
4284 case 21:
4285 status = set_user_info_21(p->mem_ctx,
4286 &info->info21, pwd);
4287 break;
4289 case 23:
4290 if (!p->server_info->user_session_key.length) {
4291 status = NT_STATUS_NO_USER_SESSION_KEY;
4293 SamOEMhashBlob(info->info23.password.data, 516,
4294 &p->server_info->user_session_key);
4296 dump_data(100, info->info23.password.data, 516);
4298 status = set_user_info_23(p->mem_ctx,
4299 &info->info23, pwd);
4300 break;
4302 case 24:
4303 if (!p->server_info->user_session_key.length) {
4304 status = NT_STATUS_NO_USER_SESSION_KEY;
4306 SamOEMhashBlob(info->info24.password.data,
4307 516,
4308 &p->server_info->user_session_key);
4310 dump_data(100, info->info24.password.data, 516);
4312 if (!set_user_info_pw(info->info24.password.data, pwd,
4313 switch_value)) {
4314 status = NT_STATUS_ACCESS_DENIED;
4316 break;
4318 case 25:
4319 if (!p->server_info->user_session_key.length) {
4320 status = NT_STATUS_NO_USER_SESSION_KEY;
4322 encode_or_decode_arc4_passwd_buffer(
4323 info->info25.password.data,
4324 &p->server_info->user_session_key);
4326 dump_data(100, info->info25.password.data, 532);
4328 status = set_user_info_25(p->mem_ctx,
4329 &info->info25, pwd);
4330 if (!NT_STATUS_IS_OK(status)) {
4331 goto done;
4333 if (!set_user_info_pw(info->info25.password.data, pwd,
4334 switch_value)) {
4335 status = NT_STATUS_ACCESS_DENIED;
4337 break;
4339 case 26:
4340 if (!p->server_info->user_session_key.length) {
4341 status = NT_STATUS_NO_USER_SESSION_KEY;
4343 encode_or_decode_arc4_passwd_buffer(
4344 info->info26.password.data,
4345 &p->server_info->user_session_key);
4347 dump_data(100, info->info26.password.data, 516);
4349 if (!set_user_info_pw(info->info26.password.data, pwd,
4350 switch_value)) {
4351 status = NT_STATUS_ACCESS_DENIED;
4353 break;
4355 default:
4356 status = NT_STATUS_INVALID_INFO_CLASS;
4359 done:
4361 if (has_enough_rights) {
4362 unbecome_root();
4365 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
4367 if (NT_STATUS_IS_OK(status)) {
4368 force_flush_samr_cache(disp_info);
4371 return status;
4374 /*******************************************************************
4375 _samr_SetUserInfo2
4376 ********************************************************************/
4378 NTSTATUS _samr_SetUserInfo2(pipes_struct *p,
4379 struct samr_SetUserInfo2 *r)
4381 struct samr_SetUserInfo q;
4383 q.in.user_handle = r->in.user_handle;
4384 q.in.level = r->in.level;
4385 q.in.info = r->in.info;
4387 return _samr_SetUserInfo(p, &q);
4390 /*********************************************************************
4391 _samr_GetAliasMembership
4392 *********************************************************************/
4394 NTSTATUS _samr_GetAliasMembership(pipes_struct *p,
4395 struct samr_GetAliasMembership *r)
4397 size_t num_alias_rids;
4398 uint32 *alias_rids;
4399 struct samr_info *info = NULL;
4400 size_t i;
4402 NTSTATUS ntstatus1;
4403 NTSTATUS ntstatus2;
4405 DOM_SID *members;
4407 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
4409 /* find the policy handle. open a policy on it. */
4410 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
4411 return NT_STATUS_INVALID_HANDLE;
4413 ntstatus1 = access_check_samr_function(info->acc_granted,
4414 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS,
4415 "_samr_GetAliasMembership");
4416 ntstatus2 = access_check_samr_function(info->acc_granted,
4417 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
4418 "_samr_GetAliasMembership");
4420 if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
4421 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
4422 !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
4423 return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
4427 if (!sid_check_is_domain(&info->sid) &&
4428 !sid_check_is_builtin(&info->sid))
4429 return NT_STATUS_OBJECT_TYPE_MISMATCH;
4431 if (r->in.sids->num_sids) {
4432 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, r->in.sids->num_sids);
4434 if (members == NULL)
4435 return NT_STATUS_NO_MEMORY;
4436 } else {
4437 members = NULL;
4440 for (i=0; i<r->in.sids->num_sids; i++)
4441 sid_copy(&members[i], r->in.sids->sids[i].sid);
4443 alias_rids = NULL;
4444 num_alias_rids = 0;
4446 become_root();
4447 ntstatus1 = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
4448 r->in.sids->num_sids,
4449 &alias_rids, &num_alias_rids);
4450 unbecome_root();
4452 if (!NT_STATUS_IS_OK(ntstatus1)) {
4453 return ntstatus1;
4456 r->out.rids->count = num_alias_rids;
4457 r->out.rids->ids = alias_rids;
4459 return NT_STATUS_OK;
4462 /*********************************************************************
4463 _samr_GetMembersInAlias
4464 *********************************************************************/
4466 NTSTATUS _samr_GetMembersInAlias(pipes_struct *p,
4467 struct samr_GetMembersInAlias *r)
4469 NTSTATUS status;
4470 size_t i;
4471 size_t num_sids = 0;
4472 struct lsa_SidPtr *sids = NULL;
4473 DOM_SID *pdb_sids = NULL;
4475 DOM_SID alias_sid;
4477 uint32 acc_granted;
4479 /* find the policy handle. open a policy on it. */
4480 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, NULL))
4481 return NT_STATUS_INVALID_HANDLE;
4483 status = access_check_samr_function(acc_granted,
4484 SAMR_ALIAS_ACCESS_GET_MEMBERS,
4485 "_samr_GetMembersInAlias");
4486 if (!NT_STATUS_IS_OK(status)) {
4487 return status;
4490 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4492 become_root();
4493 status = pdb_enum_aliasmem(&alias_sid, &pdb_sids, &num_sids);
4494 unbecome_root();
4496 if (!NT_STATUS_IS_OK(status)) {
4497 return status;
4500 if (num_sids) {
4501 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
4502 if (sids == NULL) {
4503 TALLOC_FREE(pdb_sids);
4504 return NT_STATUS_NO_MEMORY;
4508 for (i = 0; i < num_sids; i++) {
4509 sids[i].sid = sid_dup_talloc(p->mem_ctx, &pdb_sids[i]);
4510 if (!sids[i].sid) {
4511 TALLOC_FREE(pdb_sids);
4512 return NT_STATUS_NO_MEMORY;
4516 r->out.sids->num_sids = num_sids;
4517 r->out.sids->sids = sids;
4519 TALLOC_FREE(pdb_sids);
4521 return NT_STATUS_OK;
4524 /*********************************************************************
4525 _samr_QueryGroupMember
4526 *********************************************************************/
4528 NTSTATUS _samr_QueryGroupMember(pipes_struct *p,
4529 struct samr_QueryGroupMember *r)
4531 DOM_SID group_sid;
4532 size_t i, num_members;
4534 uint32 *rid=NULL;
4535 uint32 *attr=NULL;
4537 uint32 acc_granted;
4539 NTSTATUS status;
4540 struct samr_RidTypeArray *rids = NULL;
4542 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidTypeArray);
4543 if (!rids) {
4544 return NT_STATUS_NO_MEMORY;
4547 /* find the policy handle. open a policy on it. */
4548 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, NULL))
4549 return NT_STATUS_INVALID_HANDLE;
4551 status = access_check_samr_function(acc_granted,
4552 SAMR_GROUP_ACCESS_GET_MEMBERS,
4553 "_samr_QueryGroupMember");
4554 if (!NT_STATUS_IS_OK(status)) {
4555 return status;
4558 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4560 if (!sid_check_is_in_our_domain(&group_sid)) {
4561 DEBUG(3, ("sid %s is not in our domain\n",
4562 sid_string_dbg(&group_sid)));
4563 return NT_STATUS_NO_SUCH_GROUP;
4566 DEBUG(10, ("lookup on Domain SID\n"));
4568 become_root();
4569 status = pdb_enum_group_members(p->mem_ctx, &group_sid,
4570 &rid, &num_members);
4571 unbecome_root();
4573 if (!NT_STATUS_IS_OK(status))
4574 return status;
4576 if (num_members) {
4577 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
4578 if (attr == NULL) {
4579 return NT_STATUS_NO_MEMORY;
4581 } else {
4582 attr = NULL;
4585 for (i=0; i<num_members; i++)
4586 attr[i] = SID_NAME_USER;
4588 rids->count = num_members;
4589 rids->types = attr;
4590 rids->rids = rid;
4592 *r->out.rids = rids;
4594 return NT_STATUS_OK;
4597 /*********************************************************************
4598 _samr_AddAliasMember
4599 *********************************************************************/
4601 NTSTATUS _samr_AddAliasMember(pipes_struct *p,
4602 struct samr_AddAliasMember *r)
4604 DOM_SID alias_sid;
4605 uint32 acc_granted;
4606 SE_PRIV se_rights;
4607 bool can_add_accounts;
4608 NTSTATUS status;
4609 DISP_INFO *disp_info = NULL;
4611 /* Find the policy handle. Open a policy on it. */
4612 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4613 return NT_STATUS_INVALID_HANDLE;
4615 status = access_check_samr_function(acc_granted,
4616 SAMR_ALIAS_ACCESS_ADD_MEMBER,
4617 "_samr_AddAliasMember");
4618 if (!NT_STATUS_IS_OK(status)) {
4619 return status;
4622 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4624 se_priv_copy( &se_rights, &se_add_users );
4625 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4627 /******** BEGIN SeAddUsers BLOCK *********/
4629 if ( can_add_accounts )
4630 become_root();
4632 status = pdb_add_aliasmem(&alias_sid, r->in.sid);
4634 if ( can_add_accounts )
4635 unbecome_root();
4637 /******** END SeAddUsers BLOCK *********/
4639 if (NT_STATUS_IS_OK(status)) {
4640 force_flush_samr_cache(disp_info);
4643 return status;
4646 /*********************************************************************
4647 _samr_DeleteAliasMember
4648 *********************************************************************/
4650 NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
4651 struct samr_DeleteAliasMember *r)
4653 DOM_SID alias_sid;
4654 uint32 acc_granted;
4655 SE_PRIV se_rights;
4656 bool can_add_accounts;
4657 NTSTATUS status;
4658 DISP_INFO *disp_info = NULL;
4660 /* Find the policy handle. Open a policy on it. */
4661 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4662 return NT_STATUS_INVALID_HANDLE;
4664 status = access_check_samr_function(acc_granted,
4665 SAMR_ALIAS_ACCESS_REMOVE_MEMBER,
4666 "_samr_DeleteAliasMember");
4667 if (!NT_STATUS_IS_OK(status)) {
4668 return status;
4671 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
4672 sid_string_dbg(&alias_sid)));
4674 se_priv_copy( &se_rights, &se_add_users );
4675 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4677 /******** BEGIN SeAddUsers BLOCK *********/
4679 if ( can_add_accounts )
4680 become_root();
4682 status = pdb_del_aliasmem(&alias_sid, r->in.sid);
4684 if ( can_add_accounts )
4685 unbecome_root();
4687 /******** END SeAddUsers BLOCK *********/
4689 if (NT_STATUS_IS_OK(status)) {
4690 force_flush_samr_cache(disp_info);
4693 return status;
4696 /*********************************************************************
4697 _samr_AddGroupMember
4698 *********************************************************************/
4700 NTSTATUS _samr_AddGroupMember(pipes_struct *p,
4701 struct samr_AddGroupMember *r)
4703 NTSTATUS status;
4704 DOM_SID group_sid;
4705 uint32 group_rid;
4706 uint32 acc_granted;
4707 SE_PRIV se_rights;
4708 bool can_add_accounts;
4709 DISP_INFO *disp_info = NULL;
4711 /* Find the policy handle. Open a policy on it. */
4712 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4713 return NT_STATUS_INVALID_HANDLE;
4715 status = access_check_samr_function(acc_granted,
4716 SAMR_GROUP_ACCESS_ADD_MEMBER,
4717 "_samr_AddGroupMember");
4718 if (!NT_STATUS_IS_OK(status)) {
4719 return status;
4722 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4724 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4725 &group_rid)) {
4726 return NT_STATUS_INVALID_HANDLE;
4729 se_priv_copy( &se_rights, &se_add_users );
4730 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4732 /******** BEGIN SeAddUsers BLOCK *********/
4734 if ( can_add_accounts )
4735 become_root();
4737 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
4739 if ( can_add_accounts )
4740 unbecome_root();
4742 /******** END SeAddUsers BLOCK *********/
4744 force_flush_samr_cache(disp_info);
4746 return status;
4749 /*********************************************************************
4750 _samr_DeleteGroupMember
4751 *********************************************************************/
4753 NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
4754 struct samr_DeleteGroupMember *r)
4757 NTSTATUS status;
4758 DOM_SID group_sid;
4759 uint32 group_rid;
4760 uint32 acc_granted;
4761 SE_PRIV se_rights;
4762 bool can_add_accounts;
4763 DISP_INFO *disp_info = NULL;
4766 * delete the group member named r->in.rid
4767 * who is a member of the sid associated with the handle
4768 * the rid is a user's rid as the group is a domain group.
4771 /* Find the policy handle. Open a policy on it. */
4772 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4773 return NT_STATUS_INVALID_HANDLE;
4775 status = access_check_samr_function(acc_granted,
4776 SAMR_GROUP_ACCESS_REMOVE_MEMBER,
4777 "_samr_DeleteGroupMember");
4778 if (!NT_STATUS_IS_OK(status)) {
4779 return status;
4782 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4783 &group_rid)) {
4784 return NT_STATUS_INVALID_HANDLE;
4787 se_priv_copy( &se_rights, &se_add_users );
4788 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4790 /******** BEGIN SeAddUsers BLOCK *********/
4792 if ( can_add_accounts )
4793 become_root();
4795 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
4797 if ( can_add_accounts )
4798 unbecome_root();
4800 /******** END SeAddUsers BLOCK *********/
4802 force_flush_samr_cache(disp_info);
4804 return status;
4807 /*********************************************************************
4808 _samr_DeleteUser
4809 *********************************************************************/
4811 NTSTATUS _samr_DeleteUser(pipes_struct *p,
4812 struct samr_DeleteUser *r)
4814 NTSTATUS status;
4815 DOM_SID user_sid;
4816 struct samu *sam_pass=NULL;
4817 uint32 acc_granted;
4818 bool can_add_accounts;
4819 uint32 acb_info;
4820 DISP_INFO *disp_info = NULL;
4821 bool ret;
4823 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
4825 /* Find the policy handle. Open a policy on it. */
4826 if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &user_sid, &acc_granted, &disp_info))
4827 return NT_STATUS_INVALID_HANDLE;
4829 status = access_check_samr_function(acc_granted,
4830 STD_RIGHT_DELETE_ACCESS,
4831 "_samr_DeleteUser");
4832 if (!NT_STATUS_IS_OK(status)) {
4833 return status;
4836 if (!sid_check_is_in_our_domain(&user_sid))
4837 return NT_STATUS_CANNOT_DELETE;
4839 /* check if the user exists before trying to delete */
4840 if ( !(sam_pass = samu_new( NULL )) ) {
4841 return NT_STATUS_NO_MEMORY;
4844 become_root();
4845 ret = pdb_getsampwsid(sam_pass, &user_sid);
4846 unbecome_root();
4848 if( !ret ) {
4849 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
4850 sid_string_dbg(&user_sid)));
4851 TALLOC_FREE(sam_pass);
4852 return NT_STATUS_NO_SUCH_USER;
4855 acb_info = pdb_get_acct_ctrl(sam_pass);
4857 /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
4858 if ( acb_info & ACB_WSTRUST ) {
4859 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account );
4860 } else {
4861 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4864 /******** BEGIN SeAddUsers BLOCK *********/
4866 if ( can_add_accounts )
4867 become_root();
4869 status = pdb_delete_user(p->mem_ctx, sam_pass);
4871 if ( can_add_accounts )
4872 unbecome_root();
4874 /******** END SeAddUsers BLOCK *********/
4876 if ( !NT_STATUS_IS_OK(status) ) {
4877 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
4878 "user %s: %s.\n", pdb_get_username(sam_pass),
4879 nt_errstr(status)));
4880 TALLOC_FREE(sam_pass);
4881 return status;
4885 TALLOC_FREE(sam_pass);
4887 if (!close_policy_hnd(p, r->in.user_handle))
4888 return NT_STATUS_OBJECT_NAME_INVALID;
4890 ZERO_STRUCTP(r->out.user_handle);
4892 force_flush_samr_cache(disp_info);
4894 return NT_STATUS_OK;
4897 /*********************************************************************
4898 _samr_DeleteDomainGroup
4899 *********************************************************************/
4901 NTSTATUS _samr_DeleteDomainGroup(pipes_struct *p,
4902 struct samr_DeleteDomainGroup *r)
4904 NTSTATUS status;
4905 DOM_SID group_sid;
4906 uint32 group_rid;
4907 uint32 acc_granted;
4908 SE_PRIV se_rights;
4909 bool can_add_accounts;
4910 DISP_INFO *disp_info = NULL;
4912 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
4914 /* Find the policy handle. Open a policy on it. */
4915 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4916 return NT_STATUS_INVALID_HANDLE;
4918 status = access_check_samr_function(acc_granted,
4919 STD_RIGHT_DELETE_ACCESS,
4920 "_samr_DeleteDomainGroup");
4921 if (!NT_STATUS_IS_OK(status)) {
4922 return status;
4925 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4927 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4928 &group_rid)) {
4929 return NT_STATUS_NO_SUCH_GROUP;
4932 se_priv_copy( &se_rights, &se_add_users );
4933 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4935 /******** BEGIN SeAddUsers BLOCK *********/
4937 if ( can_add_accounts )
4938 become_root();
4940 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
4942 if ( can_add_accounts )
4943 unbecome_root();
4945 /******** END SeAddUsers BLOCK *********/
4947 if ( !NT_STATUS_IS_OK(status) ) {
4948 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
4949 "entry for group %s: %s\n",
4950 sid_string_dbg(&group_sid),
4951 nt_errstr(status)));
4952 return status;
4955 if (!close_policy_hnd(p, r->in.group_handle))
4956 return NT_STATUS_OBJECT_NAME_INVALID;
4958 force_flush_samr_cache(disp_info);
4960 return NT_STATUS_OK;
4963 /*********************************************************************
4964 _samr_DeleteDomAlias
4965 *********************************************************************/
4967 NTSTATUS _samr_DeleteDomAlias(pipes_struct *p,
4968 struct samr_DeleteDomAlias *r)
4970 DOM_SID alias_sid;
4971 uint32 acc_granted;
4972 SE_PRIV se_rights;
4973 bool can_add_accounts;
4974 NTSTATUS status;
4975 DISP_INFO *disp_info = NULL;
4977 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
4979 /* Find the policy handle. Open a policy on it. */
4980 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4981 return NT_STATUS_INVALID_HANDLE;
4983 /* copy the handle to the outgoing reply */
4985 memcpy(r->out.alias_handle, r->in.alias_handle, sizeof(r->out.alias_handle));
4987 status = access_check_samr_function(acc_granted,
4988 STD_RIGHT_DELETE_ACCESS,
4989 "_samr_DeleteDomAlias");
4990 if (!NT_STATUS_IS_OK(status)) {
4991 return status;
4994 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4996 /* Don't let Windows delete builtin groups */
4998 if ( sid_check_is_in_builtin( &alias_sid ) ) {
4999 return NT_STATUS_SPECIAL_ACCOUNT;
5002 if (!sid_check_is_in_our_domain(&alias_sid))
5003 return NT_STATUS_NO_SUCH_ALIAS;
5005 DEBUG(10, ("lookup on Local SID\n"));
5007 se_priv_copy( &se_rights, &se_add_users );
5008 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
5010 /******** BEGIN SeAddUsers BLOCK *********/
5012 if ( can_add_accounts )
5013 become_root();
5015 /* Have passdb delete the alias */
5016 status = pdb_delete_alias(&alias_sid);
5018 if ( can_add_accounts )
5019 unbecome_root();
5021 /******** END SeAddUsers BLOCK *********/
5023 if ( !NT_STATUS_IS_OK(status))
5024 return status;
5026 if (!close_policy_hnd(p, r->in.alias_handle))
5027 return NT_STATUS_OBJECT_NAME_INVALID;
5029 force_flush_samr_cache(disp_info);
5031 return NT_STATUS_OK;
5034 /*********************************************************************
5035 _samr_CreateDomainGroup
5036 *********************************************************************/
5038 NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
5039 struct samr_CreateDomainGroup *r)
5042 NTSTATUS status;
5043 DOM_SID dom_sid;
5044 DOM_SID info_sid;
5045 const char *name;
5046 struct samr_info *info;
5047 uint32 acc_granted;
5048 SE_PRIV se_rights;
5049 bool can_add_accounts;
5050 DISP_INFO *disp_info = NULL;
5052 /* Find the policy handle. Open a policy on it. */
5053 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &dom_sid, &acc_granted, &disp_info))
5054 return NT_STATUS_INVALID_HANDLE;
5056 status = access_check_samr_function(acc_granted,
5057 SAMR_DOMAIN_ACCESS_CREATE_GROUP,
5058 "_samr_CreateDomainGroup");
5059 if (!NT_STATUS_IS_OK(status)) {
5060 return status;
5063 if (!sid_equal(&dom_sid, get_global_sam_sid()))
5064 return NT_STATUS_ACCESS_DENIED;
5066 name = r->in.name->string;
5067 if (name == NULL) {
5068 return NT_STATUS_NO_MEMORY;
5071 status = can_create(p->mem_ctx, name);
5072 if (!NT_STATUS_IS_OK(status)) {
5073 return status;
5076 se_priv_copy( &se_rights, &se_add_users );
5077 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
5079 /******** BEGIN SeAddUsers BLOCK *********/
5081 if ( can_add_accounts )
5082 become_root();
5084 /* check that we successfully create the UNIX group */
5086 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
5088 if ( can_add_accounts )
5089 unbecome_root();
5091 /******** END SeAddUsers BLOCK *********/
5093 /* check if we should bail out here */
5095 if ( !NT_STATUS_IS_OK(status) )
5096 return status;
5098 sid_compose(&info_sid, get_global_sam_sid(), *r->out.rid);
5100 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5101 return NT_STATUS_NO_MEMORY;
5103 /* they created it; let the user do what he wants with it */
5105 info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
5107 /* get a (unique) handle. open a policy on it. */
5108 if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
5109 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5111 force_flush_samr_cache(disp_info);
5113 return NT_STATUS_OK;
5116 /*********************************************************************
5117 _samr_CreateDomAlias
5118 *********************************************************************/
5120 NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
5121 struct samr_CreateDomAlias *r)
5123 DOM_SID dom_sid;
5124 DOM_SID info_sid;
5125 const char *name = NULL;
5126 struct samr_info *info;
5127 uint32 acc_granted;
5128 gid_t gid;
5129 NTSTATUS result;
5130 SE_PRIV se_rights;
5131 bool can_add_accounts;
5132 DISP_INFO *disp_info = NULL;
5134 /* Find the policy handle. Open a policy on it. */
5135 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &dom_sid, &acc_granted, &disp_info))
5136 return NT_STATUS_INVALID_HANDLE;
5138 result = access_check_samr_function(acc_granted,
5139 SAMR_DOMAIN_ACCESS_CREATE_ALIAS,
5140 "_samr_CreateDomAlias");
5141 if (!NT_STATUS_IS_OK(result)) {
5142 return result;
5145 if (!sid_equal(&dom_sid, get_global_sam_sid()))
5146 return NT_STATUS_ACCESS_DENIED;
5148 name = r->in.alias_name->string;
5150 se_priv_copy( &se_rights, &se_add_users );
5151 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
5153 result = can_create(p->mem_ctx, name);
5154 if (!NT_STATUS_IS_OK(result)) {
5155 return result;
5158 /******** BEGIN SeAddUsers BLOCK *********/
5160 if ( can_add_accounts )
5161 become_root();
5163 /* Have passdb create the alias */
5164 result = pdb_create_alias(name, r->out.rid);
5166 if ( can_add_accounts )
5167 unbecome_root();
5169 /******** END SeAddUsers BLOCK *********/
5171 if (!NT_STATUS_IS_OK(result)) {
5172 DEBUG(10, ("pdb_create_alias failed: %s\n",
5173 nt_errstr(result)));
5174 return result;
5177 sid_copy(&info_sid, get_global_sam_sid());
5178 sid_append_rid(&info_sid, *r->out.rid);
5180 if (!sid_to_gid(&info_sid, &gid)) {
5181 DEBUG(10, ("Could not find alias just created\n"));
5182 return NT_STATUS_ACCESS_DENIED;
5185 /* check if the group has been successfully created */
5186 if ( getgrgid(gid) == NULL ) {
5187 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
5188 gid));
5189 return NT_STATUS_ACCESS_DENIED;
5192 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5193 return NT_STATUS_NO_MEMORY;
5195 /* they created it; let the user do what he wants with it */
5197 info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
5199 /* get a (unique) handle. open a policy on it. */
5200 if (!create_policy_hnd(p, r->out.alias_handle, free_samr_info, (void *)info))
5201 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5203 force_flush_samr_cache(disp_info);
5205 return NT_STATUS_OK;
5208 /*********************************************************************
5209 _samr_QueryGroupInfo
5210 *********************************************************************/
5212 NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
5213 struct samr_QueryGroupInfo *r)
5215 NTSTATUS status;
5216 DOM_SID group_sid;
5217 GROUP_MAP map;
5218 union samr_GroupInfo *info = NULL;
5219 uint32 acc_granted;
5220 bool ret;
5221 uint32_t attributes = SE_GROUP_MANDATORY |
5222 SE_GROUP_ENABLED_BY_DEFAULT |
5223 SE_GROUP_ENABLED;
5224 const char *group_name = NULL;
5225 const char *group_description = NULL;
5227 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, NULL))
5228 return NT_STATUS_INVALID_HANDLE;
5230 status = access_check_samr_function(acc_granted,
5231 SAMR_GROUP_ACCESS_LOOKUP_INFO,
5232 "_samr_QueryGroupInfo");
5233 if (!NT_STATUS_IS_OK(status)) {
5234 return status;
5237 become_root();
5238 ret = get_domain_group_from_sid(group_sid, &map);
5239 unbecome_root();
5240 if (!ret)
5241 return NT_STATUS_INVALID_HANDLE;
5243 /* FIXME: map contains fstrings */
5244 group_name = talloc_strdup(r, map.nt_name);
5245 group_description = talloc_strdup(r, map.comment);
5247 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
5248 if (!info) {
5249 return NT_STATUS_NO_MEMORY;
5252 switch (r->in.level) {
5253 case 1: {
5254 uint32 *members;
5255 size_t num_members;
5257 become_root();
5258 status = pdb_enum_group_members(
5259 p->mem_ctx, &group_sid, &members, &num_members);
5260 unbecome_root();
5262 if (!NT_STATUS_IS_OK(status)) {
5263 return status;
5266 init_samr_group_info1(&info->all,
5267 group_name,
5268 attributes,
5269 num_members,
5270 group_description);
5271 break;
5273 case 2:
5274 init_samr_group_info2(&info->name,
5275 group_name);
5276 break;
5277 case 3:
5278 init_samr_group_info3(&info->attributes,
5279 attributes);
5280 break;
5281 case 4:
5282 init_samr_group_info4(&info->description,
5283 group_description);
5284 break;
5285 case 5: {
5287 uint32 *members;
5288 size_t num_members;
5292 become_root();
5293 status = pdb_enum_group_members(
5294 p->mem_ctx, &group_sid, &members, &num_members);
5295 unbecome_root();
5297 if (!NT_STATUS_IS_OK(status)) {
5298 return status;
5301 init_samr_group_info5(&info->all2,
5302 group_name,
5303 attributes,
5304 0, /* num_members - in w2k3 this is always 0 */
5305 group_description);
5307 break;
5309 default:
5310 return NT_STATUS_INVALID_INFO_CLASS;
5313 *r->out.info = info;
5315 return NT_STATUS_OK;
5318 /*********************************************************************
5319 _samr_SetGroupInfo
5320 *********************************************************************/
5322 NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
5323 struct samr_SetGroupInfo *r)
5325 DOM_SID group_sid;
5326 GROUP_MAP map;
5327 uint32 acc_granted;
5328 NTSTATUS status;
5329 bool ret;
5330 bool can_mod_accounts;
5331 DISP_INFO *disp_info = NULL;
5333 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
5334 return NT_STATUS_INVALID_HANDLE;
5336 status = access_check_samr_function(acc_granted,
5337 SAMR_GROUP_ACCESS_SET_INFO,
5338 "_samr_SetGroupInfo");
5339 if (!NT_STATUS_IS_OK(status)) {
5340 return status;
5343 become_root();
5344 ret = get_domain_group_from_sid(group_sid, &map);
5345 unbecome_root();
5346 if (!ret)
5347 return NT_STATUS_NO_SUCH_GROUP;
5349 switch (r->in.level) {
5350 case 1:
5351 fstrcpy(map.comment, r->in.info->all.description.string);
5352 break;
5353 case 4:
5354 fstrcpy(map.comment, r->in.info->description.string);
5355 break;
5356 default:
5357 return NT_STATUS_INVALID_INFO_CLASS;
5360 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
5362 /******** BEGIN SeAddUsers BLOCK *********/
5364 if ( can_mod_accounts )
5365 become_root();
5367 status = pdb_update_group_mapping_entry(&map);
5369 if ( can_mod_accounts )
5370 unbecome_root();
5372 /******** End SeAddUsers BLOCK *********/
5374 if (NT_STATUS_IS_OK(status)) {
5375 force_flush_samr_cache(disp_info);
5378 return status;
5381 /*********************************************************************
5382 _samr_SetAliasInfo
5383 *********************************************************************/
5385 NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
5386 struct samr_SetAliasInfo *r)
5388 DOM_SID group_sid;
5389 struct acct_info info;
5390 uint32 acc_granted;
5391 bool can_mod_accounts;
5392 NTSTATUS status;
5393 DISP_INFO *disp_info = NULL;
5395 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &group_sid, &acc_granted, &disp_info))
5396 return NT_STATUS_INVALID_HANDLE;
5398 status = access_check_samr_function(acc_granted,
5399 SAMR_ALIAS_ACCESS_SET_INFO,
5400 "_samr_SetAliasInfo");
5401 if (!NT_STATUS_IS_OK(status)) {
5402 return status;
5405 /* get the current group information */
5407 become_root();
5408 status = pdb_get_aliasinfo( &group_sid, &info );
5409 unbecome_root();
5411 if ( !NT_STATUS_IS_OK(status))
5412 return status;
5414 switch (r->in.level) {
5415 case ALIASINFONAME:
5417 fstring group_name;
5419 /* We currently do not support renaming groups in the
5420 the BUILTIN domain. Refer to util_builtin.c to understand
5421 why. The eventually needs to be fixed to be like Windows
5422 where you can rename builtin groups, just not delete them */
5424 if ( sid_check_is_in_builtin( &group_sid ) ) {
5425 return NT_STATUS_SPECIAL_ACCOUNT;
5428 /* There has to be a valid name (and it has to be different) */
5430 if ( !r->in.info->name.string )
5431 return NT_STATUS_INVALID_PARAMETER;
5433 /* If the name is the same just reply "ok". Yes this
5434 doesn't allow you to change the case of a group name. */
5436 if ( strequal( r->in.info->name.string, info.acct_name ) )
5437 return NT_STATUS_OK;
5439 fstrcpy( info.acct_name, r->in.info->name.string);
5441 /* make sure the name doesn't already exist as a user
5442 or local group */
5444 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
5445 status = can_create( p->mem_ctx, group_name );
5446 if ( !NT_STATUS_IS_OK( status ) )
5447 return status;
5448 break;
5450 case ALIASINFODESCRIPTION:
5451 if (r->in.info->description.string) {
5452 fstrcpy(info.acct_desc,
5453 r->in.info->description.string);
5454 } else {
5455 fstrcpy( info.acct_desc, "" );
5457 break;
5458 default:
5459 return NT_STATUS_INVALID_INFO_CLASS;
5462 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
5464 /******** BEGIN SeAddUsers BLOCK *********/
5466 if ( can_mod_accounts )
5467 become_root();
5469 status = pdb_set_aliasinfo( &group_sid, &info );
5471 if ( can_mod_accounts )
5472 unbecome_root();
5474 /******** End SeAddUsers BLOCK *********/
5476 if (NT_STATUS_IS_OK(status))
5477 force_flush_samr_cache(disp_info);
5479 return status;
5482 /****************************************************************
5483 _samr_GetDomPwInfo
5484 ****************************************************************/
5486 NTSTATUS _samr_GetDomPwInfo(pipes_struct *p,
5487 struct samr_GetDomPwInfo *r)
5489 uint32_t min_password_length = 0;
5490 uint32_t password_properties = 0;
5492 /* Perform access check. Since this rpc does not require a
5493 policy handle it will not be caught by the access checks on
5494 SAMR_CONNECT or SAMR_CONNECT_ANON. */
5496 if (!pipe_access_check(p)) {
5497 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
5498 return NT_STATUS_ACCESS_DENIED;
5501 become_root();
5502 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
5503 &min_password_length);
5504 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
5505 &password_properties);
5506 unbecome_root();
5508 if (lp_check_password_script() && *lp_check_password_script()) {
5509 password_properties |= DOMAIN_PASSWORD_COMPLEX;
5512 r->out.info->min_password_length = min_password_length;
5513 r->out.info->password_properties = password_properties;
5515 return NT_STATUS_OK;
5518 /*********************************************************************
5519 _samr_OpenGroup
5520 *********************************************************************/
5522 NTSTATUS _samr_OpenGroup(pipes_struct *p,
5523 struct samr_OpenGroup *r)
5526 DOM_SID sid;
5527 DOM_SID info_sid;
5528 GROUP_MAP map;
5529 struct samr_info *info;
5530 SEC_DESC *psd = NULL;
5531 uint32 acc_granted;
5532 uint32 des_access = r->in.access_mask;
5533 size_t sd_size;
5534 NTSTATUS status;
5535 fstring sid_string;
5536 bool ret;
5537 SE_PRIV se_rights;
5539 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &sid, &acc_granted, NULL))
5540 return NT_STATUS_INVALID_HANDLE;
5542 status = access_check_samr_function(acc_granted,
5543 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
5544 "_samr_OpenGroup");
5546 if ( !NT_STATUS_IS_OK(status) )
5547 return status;
5549 /*check if access can be granted as requested by client. */
5550 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
5552 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
5553 se_map_generic(&des_access,&grp_generic_mapping);
5555 se_priv_copy( &se_rights, &se_add_users );
5557 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
5558 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
5559 &acc_granted, "_samr_OpenGroup");
5561 if ( !NT_STATUS_IS_OK(status) )
5562 return status;
5564 /* this should not be hard-coded like this */
5566 if (!sid_equal(&sid, get_global_sam_sid()))
5567 return NT_STATUS_ACCESS_DENIED;
5569 sid_copy(&info_sid, get_global_sam_sid());
5570 sid_append_rid(&info_sid, r->in.rid);
5571 sid_to_fstring(sid_string, &info_sid);
5573 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5574 return NT_STATUS_NO_MEMORY;
5576 info->acc_granted = acc_granted;
5578 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n", sid_string));
5580 /* check if that group really exists */
5581 become_root();
5582 ret = get_domain_group_from_sid(info->sid, &map);
5583 unbecome_root();
5584 if (!ret)
5585 return NT_STATUS_NO_SUCH_GROUP;
5587 /* get a (unique) handle. open a policy on it. */
5588 if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
5589 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5591 return NT_STATUS_OK;
5594 /*********************************************************************
5595 _samr_RemoveMemberFromForeignDomain
5596 *********************************************************************/
5598 NTSTATUS _samr_RemoveMemberFromForeignDomain(pipes_struct *p,
5599 struct samr_RemoveMemberFromForeignDomain *r)
5601 DOM_SID delete_sid, domain_sid;
5602 uint32 acc_granted;
5603 NTSTATUS result;
5604 DISP_INFO *disp_info = NULL;
5606 sid_copy( &delete_sid, r->in.sid );
5608 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
5609 sid_string_dbg(&delete_sid)));
5611 /* Find the policy handle. Open a policy on it. */
5613 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &domain_sid,
5614 &acc_granted, &disp_info))
5615 return NT_STATUS_INVALID_HANDLE;
5617 result = access_check_samr_function(acc_granted,
5618 STD_RIGHT_DELETE_ACCESS,
5619 "_samr_RemoveMemberFromForeignDomain");
5621 if (!NT_STATUS_IS_OK(result))
5622 return result;
5624 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
5625 sid_string_dbg(&domain_sid)));
5627 /* we can only delete a user from a group since we don't have
5628 nested groups anyways. So in the latter case, just say OK */
5630 /* TODO: The above comment nowadays is bogus. Since we have nested
5631 * groups now, and aliases members are never reported out of the unix
5632 * group membership, the "just say OK" makes this call a no-op. For
5633 * us. This needs fixing however. */
5635 /* I've only ever seen this in the wild when deleting a user from
5636 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
5637 * is the user about to be deleted. I very much suspect this is the
5638 * only application of this call. To verify this, let people report
5639 * other cases. */
5641 if (!sid_check_is_builtin(&domain_sid)) {
5642 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
5643 "global_sam_sid() = %s\n",
5644 sid_string_dbg(&domain_sid),
5645 sid_string_dbg(get_global_sam_sid())));
5646 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
5647 return NT_STATUS_OK;
5650 force_flush_samr_cache(disp_info);
5652 result = NT_STATUS_OK;
5654 return result;
5657 /*******************************************************************
5658 _samr_QueryDomainInfo2
5659 ********************************************************************/
5661 NTSTATUS _samr_QueryDomainInfo2(pipes_struct *p,
5662 struct samr_QueryDomainInfo2 *r)
5664 struct samr_QueryDomainInfo q;
5666 q.in.domain_handle = r->in.domain_handle;
5667 q.in.level = r->in.level;
5669 q.out.info = r->out.info;
5671 return _samr_QueryDomainInfo(p, &q);
5674 /*******************************************************************
5675 _samr_SetDomainInfo
5676 ********************************************************************/
5678 NTSTATUS _samr_SetDomainInfo(pipes_struct *p,
5679 struct samr_SetDomainInfo *r)
5681 struct samr_info *info = NULL;
5682 time_t u_expire, u_min_age;
5683 time_t u_logout;
5684 time_t u_lock_duration, u_reset_time;
5685 NTSTATUS result;
5687 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5689 /* find the policy handle. open a policy on it. */
5690 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
5691 return NT_STATUS_INVALID_HANDLE;
5693 /* We do have different access bits for info
5694 * levels here, but we're really just looking for
5695 * GENERIC_RIGHTS_DOMAIN_WRITE access. Unfortunately
5696 * this maps to different specific bits. So
5697 * assume if we have SAMR_DOMAIN_ACCESS_SET_INFO_1
5698 * set we are ok. */
5700 result = access_check_samr_function(info->acc_granted,
5701 SAMR_DOMAIN_ACCESS_SET_INFO_1,
5702 "_samr_SetDomainInfo");
5704 if (!NT_STATUS_IS_OK(result))
5705 return result;
5707 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
5709 switch (r->in.level) {
5710 case 0x01:
5711 u_expire=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.max_password_age);
5712 u_min_age=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.min_password_age);
5713 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)r->in.info->info1.min_password_length);
5714 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)r->in.info->info1.password_history_length);
5715 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)r->in.info->info1.password_properties);
5716 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
5717 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
5718 break;
5719 case 0x02:
5720 break;
5721 case 0x03:
5722 u_logout=nt_time_to_unix_abs((NTTIME *)&r->in.info->info3.force_logoff_time);
5723 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
5724 break;
5725 case 0x05:
5726 break;
5727 case 0x06:
5728 break;
5729 case 0x07:
5730 break;
5731 case 0x0c:
5732 u_lock_duration=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_duration);
5733 if (u_lock_duration != -1)
5734 u_lock_duration /= 60;
5736 u_reset_time=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_window)/60;
5738 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
5739 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
5740 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)r->in.info->info12.lockout_threshold);
5741 break;
5742 default:
5743 return NT_STATUS_INVALID_INFO_CLASS;
5746 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5748 return NT_STATUS_OK;
5751 /****************************************************************
5752 _samr_GetDisplayEnumerationIndex
5753 ****************************************************************/
5755 NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
5756 struct samr_GetDisplayEnumerationIndex *r)
5758 struct samr_info *info = NULL;
5759 uint32_t max_entries = (uint32_t) -1;
5760 uint32_t enum_context = 0;
5761 int i;
5762 uint32_t num_account = 0;
5763 struct samr_displayentry *entries = NULL;
5764 NTSTATUS status;
5766 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
5768 /* find the policy handle. open a policy on it. */
5769 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info)) {
5770 return NT_STATUS_INVALID_HANDLE;
5773 status = access_check_samr_function(info->acc_granted,
5774 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
5775 "_samr_GetDisplayEnumerationIndex");
5776 if (!NT_STATUS_IS_OK(status)) {
5777 return status;
5780 if ((r->in.level < 1) || (r->in.level > 3)) {
5781 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
5782 "Unknown info level (%u)\n",
5783 r->in.level));
5784 return NT_STATUS_INVALID_INFO_CLASS;
5787 become_root();
5789 /* The following done as ROOT. Don't return without unbecome_root(). */
5791 switch (r->in.level) {
5792 case 1:
5793 if (info->disp_info->users == NULL) {
5794 info->disp_info->users = pdb_search_users(ACB_NORMAL);
5795 if (info->disp_info->users == NULL) {
5796 unbecome_root();
5797 return NT_STATUS_ACCESS_DENIED;
5799 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5800 "starting user enumeration at index %u\n",
5801 (unsigned int)enum_context));
5802 } else {
5803 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5804 "using cached user enumeration at index %u\n",
5805 (unsigned int)enum_context));
5807 num_account = pdb_search_entries(info->disp_info->users,
5808 enum_context, max_entries,
5809 &entries);
5810 break;
5811 case 2:
5812 if (info->disp_info->machines == NULL) {
5813 info->disp_info->machines =
5814 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
5815 if (info->disp_info->machines == NULL) {
5816 unbecome_root();
5817 return NT_STATUS_ACCESS_DENIED;
5819 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5820 "starting machine enumeration at index %u\n",
5821 (unsigned int)enum_context));
5822 } else {
5823 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5824 "using cached machine enumeration at index %u\n",
5825 (unsigned int)enum_context));
5827 num_account = pdb_search_entries(info->disp_info->machines,
5828 enum_context, max_entries,
5829 &entries);
5830 break;
5831 case 3:
5832 if (info->disp_info->groups == NULL) {
5833 info->disp_info->groups = pdb_search_groups();
5834 if (info->disp_info->groups == NULL) {
5835 unbecome_root();
5836 return NT_STATUS_ACCESS_DENIED;
5838 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5839 "starting group enumeration at index %u\n",
5840 (unsigned int)enum_context));
5841 } else {
5842 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5843 "using cached group enumeration at index %u\n",
5844 (unsigned int)enum_context));
5846 num_account = pdb_search_entries(info->disp_info->groups,
5847 enum_context, max_entries,
5848 &entries);
5849 break;
5850 default:
5851 unbecome_root();
5852 smb_panic("info class changed");
5853 break;
5856 unbecome_root();
5858 /* Ensure we cache this enumeration. */
5859 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
5861 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
5862 r->in.name->string));
5864 for (i=0; i<num_account; i++) {
5865 if (strequal(entries[i].account_name, r->in.name->string)) {
5866 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5867 "found %s at idx %d\n",
5868 r->in.name->string, i));
5869 *r->out.idx = i;
5870 return NT_STATUS_OK;
5874 /* assuming account_name lives at the very end */
5875 *r->out.idx = num_account;
5877 return NT_STATUS_NO_MORE_ENTRIES;
5880 /****************************************************************
5881 _samr_GetDisplayEnumerationIndex2
5882 ****************************************************************/
5884 NTSTATUS _samr_GetDisplayEnumerationIndex2(pipes_struct *p,
5885 struct samr_GetDisplayEnumerationIndex2 *r)
5887 struct samr_GetDisplayEnumerationIndex q;
5889 q.in.domain_handle = r->in.domain_handle;
5890 q.in.level = r->in.level;
5891 q.in.name = r->in.name;
5893 q.out.idx = r->out.idx;
5895 return _samr_GetDisplayEnumerationIndex(p, &q);
5898 /****************************************************************
5899 ****************************************************************/
5901 NTSTATUS _samr_Shutdown(pipes_struct *p,
5902 struct samr_Shutdown *r)
5904 p->rng_fault_state = true;
5905 return NT_STATUS_NOT_IMPLEMENTED;
5908 /****************************************************************
5909 ****************************************************************/
5911 NTSTATUS _samr_CreateUser(pipes_struct *p,
5912 struct samr_CreateUser *r)
5914 p->rng_fault_state = true;
5915 return NT_STATUS_NOT_IMPLEMENTED;
5918 /****************************************************************
5919 ****************************************************************/
5921 NTSTATUS _samr_SetMemberAttributesOfGroup(pipes_struct *p,
5922 struct samr_SetMemberAttributesOfGroup *r)
5924 p->rng_fault_state = true;
5925 return NT_STATUS_NOT_IMPLEMENTED;
5928 /****************************************************************
5929 ****************************************************************/
5931 NTSTATUS _samr_ChangePasswordUser(pipes_struct *p,
5932 struct samr_ChangePasswordUser *r)
5934 p->rng_fault_state = true;
5935 return NT_STATUS_NOT_IMPLEMENTED;
5938 /****************************************************************
5939 ****************************************************************/
5941 NTSTATUS _samr_TestPrivateFunctionsDomain(pipes_struct *p,
5942 struct samr_TestPrivateFunctionsDomain *r)
5944 p->rng_fault_state = true;
5945 return NT_STATUS_NOT_IMPLEMENTED;
5948 /****************************************************************
5949 ****************************************************************/
5951 NTSTATUS _samr_TestPrivateFunctionsUser(pipes_struct *p,
5952 struct samr_TestPrivateFunctionsUser *r)
5954 p->rng_fault_state = true;
5955 return NT_STATUS_NOT_IMPLEMENTED;
5958 /****************************************************************
5959 ****************************************************************/
5961 NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
5962 struct samr_QueryUserInfo2 *r)
5964 p->rng_fault_state = true;
5965 return NT_STATUS_NOT_IMPLEMENTED;
5968 /****************************************************************
5969 ****************************************************************/
5971 NTSTATUS _samr_AddMultipleMembersToAlias(pipes_struct *p,
5972 struct samr_AddMultipleMembersToAlias *r)
5974 p->rng_fault_state = true;
5975 return NT_STATUS_NOT_IMPLEMENTED;
5978 /****************************************************************
5979 ****************************************************************/
5981 NTSTATUS _samr_RemoveMultipleMembersFromAlias(pipes_struct *p,
5982 struct samr_RemoveMultipleMembersFromAlias *r)
5984 p->rng_fault_state = true;
5985 return NT_STATUS_NOT_IMPLEMENTED;
5988 /****************************************************************
5989 ****************************************************************/
5991 NTSTATUS _samr_OemChangePasswordUser2(pipes_struct *p,
5992 struct samr_OemChangePasswordUser2 *r)
5994 p->rng_fault_state = true;
5995 return NT_STATUS_NOT_IMPLEMENTED;
5998 /****************************************************************
5999 ****************************************************************/
6001 NTSTATUS _samr_SetBootKeyInformation(pipes_struct *p,
6002 struct samr_SetBootKeyInformation *r)
6004 p->rng_fault_state = true;
6005 return NT_STATUS_NOT_IMPLEMENTED;
6008 /****************************************************************
6009 ****************************************************************/
6011 NTSTATUS _samr_GetBootKeyInformation(pipes_struct *p,
6012 struct samr_GetBootKeyInformation *r)
6014 p->rng_fault_state = true;
6015 return NT_STATUS_NOT_IMPLEMENTED;
6018 /****************************************************************
6019 ****************************************************************/
6021 NTSTATUS _samr_Connect3(pipes_struct *p,
6022 struct samr_Connect3 *r)
6024 p->rng_fault_state = true;
6025 return NT_STATUS_NOT_IMPLEMENTED;
6028 /****************************************************************
6029 ****************************************************************/
6031 NTSTATUS _samr_RidToSid(pipes_struct *p,
6032 struct samr_RidToSid *r)
6034 p->rng_fault_state = true;
6035 return NT_STATUS_NOT_IMPLEMENTED;
6038 /****************************************************************
6039 ****************************************************************/
6041 NTSTATUS _samr_SetDsrmPassword(pipes_struct *p,
6042 struct samr_SetDsrmPassword *r)
6044 p->rng_fault_state = true;
6045 return NT_STATUS_NOT_IMPLEMENTED;
6048 /****************************************************************
6049 ****************************************************************/
6051 NTSTATUS _samr_ValidatePassword(pipes_struct *p,
6052 struct samr_ValidatePassword *r)
6054 p->rng_fault_state = true;
6055 return NT_STATUS_NOT_IMPLEMENTED;