s3-groupdb: fix enum_aliasmem in ldb branch.
[Samba.git] / source / rpc_server / srv_samr_nt.c
bloba394eb3b90c5869b1b3369cb3a0da1f56d635dca
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 SA_RIGHT_USER_CHANGE_PASSWORD | \
42 SA_RIGHT_USER_SET_LOC_COM )
43 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
44 ( READ_CONTROL_ACCESS | SA_RIGHT_USER_SET_LOC_COM )
46 #define DISP_INFO_CACHE_TIMEOUT 10
48 typedef struct disp_info {
49 DOM_SID sid; /* identify which domain this is. */
50 bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
51 struct pdb_search *users; /* querydispinfo 1 and 4 */
52 struct pdb_search *machines; /* querydispinfo 2 */
53 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
54 struct pdb_search *aliases; /* enumaliases */
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 & ~SA_RIGHT_USER_CHANGE_PASSWORD,
95 GENERIC_RIGHTS_USER_ALL_ACCESS};
96 static const struct generic_mapping grp_generic_mapping = {
97 GENERIC_RIGHTS_GROUP_READ,
98 GENERIC_RIGHTS_GROUP_WRITE,
99 GENERIC_RIGHTS_GROUP_EXECUTE,
100 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
101 static const struct generic_mapping ali_generic_mapping = {
102 GENERIC_RIGHTS_ALIAS_READ,
103 GENERIC_RIGHTS_ALIAS_WRITE,
104 GENERIC_RIGHTS_ALIAS_EXECUTE,
105 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
107 /*******************************************************************
108 *******************************************************************/
110 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
111 const struct generic_mapping *map,
112 DOM_SID *sid, uint32 sid_access )
114 DOM_SID domadmin_sid;
115 SEC_ACE ace[5]; /* at most 5 entries */
116 SEC_ACCESS mask;
117 size_t i = 0;
119 SEC_ACL *psa = NULL;
121 /* basic access for Everyone */
123 init_sec_access(&mask, map->generic_execute | map->generic_read );
124 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
126 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
128 init_sec_access(&mask, map->generic_all);
130 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
131 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
133 /* Add Full Access for Domain Admins if we are a DC */
135 if ( IS_DC ) {
136 sid_copy( &domadmin_sid, get_global_sam_sid() );
137 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
138 init_sec_ace(&ace[i++], &domadmin_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
141 /* if we have a sid, give it some special access */
143 if ( sid ) {
144 init_sec_access( &mask, sid_access );
145 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
148 /* create the security descriptor */
150 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
151 return NT_STATUS_NO_MEMORY;
153 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
154 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
155 psa, sd_size)) == NULL)
156 return NT_STATUS_NO_MEMORY;
158 return NT_STATUS_OK;
161 /*******************************************************************
162 Checks if access to an object should be granted, and returns that
163 level of access for further checks.
164 ********************************************************************/
166 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
167 SE_PRIV *rights, uint32 rights_mask,
168 uint32 des_access, uint32 *acc_granted,
169 const char *debug )
171 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
172 uint32 saved_mask = 0;
174 /* check privileges; certain SAM access bits should be overridden
175 by privileges (mostly having to do with creating/modifying/deleting
176 users and groups) */
178 if ( rights && user_has_any_privilege( token, rights ) ) {
180 saved_mask = (des_access & rights_mask);
181 des_access &= ~saved_mask;
183 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
184 rights_mask));
188 /* check the security descriptor first */
190 if ( se_access_check(psd, token, des_access, acc_granted, &status) )
191 goto done;
193 /* give root a free pass */
195 if ( geteuid() == sec_initial_uid() ) {
197 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
198 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
200 *acc_granted = des_access;
202 status = NT_STATUS_OK;
203 goto done;
207 done:
208 /* add in any bits saved during the privilege check (only
209 matters is status is ok) */
211 *acc_granted |= rights_mask;
213 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
214 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
215 des_access, *acc_granted));
217 return status;
220 /*******************************************************************
221 Checks if access to a function can be granted
222 ********************************************************************/
224 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
226 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
227 debug, acc_granted, acc_required));
229 /* check the security descriptor first */
231 if ( (acc_granted&acc_required) == acc_required )
232 return NT_STATUS_OK;
234 /* give root a free pass */
236 if (geteuid() == sec_initial_uid()) {
238 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
239 debug, acc_granted, acc_required));
240 DEBUGADD(4,("but overwritten by euid == 0\n"));
242 return NT_STATUS_OK;
245 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
246 debug, acc_granted, acc_required));
248 return NT_STATUS_ACCESS_DENIED;
251 /*******************************************************************
252 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
253 ********************************************************************/
255 static void map_max_allowed_access(const NT_USER_TOKEN *token,
256 uint32_t *pacc_requested)
258 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
259 return;
261 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
263 /* At least try for generic read. */
264 *pacc_requested = GENERIC_READ_ACCESS;
266 /* root gets anything. */
267 if (geteuid() == sec_initial_uid()) {
268 *pacc_requested |= GENERIC_ALL_ACCESS;
269 return;
272 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
274 if (is_sid_in_token(token, &global_sid_Builtin_Administrators) ||
275 is_sid_in_token(token, &global_sid_Builtin_Account_Operators)) {
276 *pacc_requested |= GENERIC_ALL_ACCESS;
277 return;
280 /* Full access for DOMAIN\Domain Admins. */
281 if ( IS_DC ) {
282 DOM_SID domadmin_sid;
283 sid_copy( &domadmin_sid, get_global_sam_sid() );
284 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
285 if (is_sid_in_token(token, &domadmin_sid)) {
286 *pacc_requested |= GENERIC_ALL_ACCESS;
287 return;
290 /* TODO ! Check privileges. */
293 /*******************************************************************
294 Fetch or create a dispinfo struct.
295 ********************************************************************/
297 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
300 * We do a static cache for DISP_INFO's here. Explanation can be found
301 * in Jeremy's checkin message to r11793:
303 * Fix the SAMR cache so it works across completely insane
304 * client behaviour (ie.:
305 * open pipe/open SAMR handle/enumerate 0 - 1024
306 * close SAMR handle, close pipe.
307 * open pipe/open SAMR handle/enumerate 1024 - 2048...
308 * close SAMR handle, close pipe.
309 * And on ad-nausium. Amazing.... probably object-oriented
310 * client side programming in action yet again.
311 * This change should *massively* improve performance when
312 * enumerating users from an LDAP database.
313 * Jeremy.
315 * "Our" and the builtin domain are the only ones where we ever
316 * enumerate stuff, so just cache 2 entries.
319 static struct disp_info builtin_dispinfo;
320 static struct disp_info domain_dispinfo;
322 /* There are two cases to consider here:
323 1) The SID is a domain SID and we look for an equality match, or
324 2) This is an account SID and so we return the DISP_INFO* for our
325 domain */
327 if (psid == NULL) {
328 return NULL;
331 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
333 * Necessary only once, but it does not really hurt.
335 sid_copy(&builtin_dispinfo.sid, &global_sid_Builtin);
337 return &builtin_dispinfo;
340 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
342 * Necessary only once, but it does not really hurt.
344 sid_copy(&domain_dispinfo.sid, get_global_sam_sid());
346 return &domain_dispinfo;
349 return NULL;
352 /*******************************************************************
353 Create a samr_info struct.
354 ********************************************************************/
356 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
358 struct samr_info *info;
359 fstring sid_str;
360 TALLOC_CTX *mem_ctx;
362 if (psid) {
363 sid_to_fstring(sid_str, psid);
364 } else {
365 fstrcpy(sid_str,"(NULL)");
368 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
370 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
371 return NULL;
373 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
374 if (psid) {
375 sid_copy( &info->sid, psid);
376 info->builtin_domain = sid_check_is_builtin(psid);
377 } else {
378 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
379 info->builtin_domain = False;
381 info->mem_ctx = mem_ctx;
383 info->disp_info = get_samr_dispinfo_by_sid(psid);
385 return info;
388 /*******************************************************************
389 Function to free the per SID data.
390 ********************************************************************/
392 static void free_samr_cache(DISP_INFO *disp_info)
394 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
395 sid_string_dbg(&disp_info->sid)));
397 /* We need to become root here because the paged search might have to
398 * tell the LDAP server we're not interested in the rest anymore. */
400 become_root();
402 if (disp_info->users) {
403 DEBUG(10,("free_samr_cache: deleting users cache\n"));
404 pdb_search_destroy(disp_info->users);
405 disp_info->users = NULL;
407 if (disp_info->machines) {
408 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
409 pdb_search_destroy(disp_info->machines);
410 disp_info->machines = NULL;
412 if (disp_info->groups) {
413 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
414 pdb_search_destroy(disp_info->groups);
415 disp_info->groups = NULL;
417 if (disp_info->aliases) {
418 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
419 pdb_search_destroy(disp_info->aliases);
420 disp_info->aliases = NULL;
422 if (disp_info->enum_users) {
423 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
424 pdb_search_destroy(disp_info->enum_users);
425 disp_info->enum_users = NULL;
427 disp_info->enum_acb_mask = 0;
429 unbecome_root();
432 /*******************************************************************
433 Function to free the per handle data.
434 ********************************************************************/
436 static void free_samr_info(void *ptr)
438 struct samr_info *info=(struct samr_info *) ptr;
440 /* Only free the dispinfo cache if no one bothered to set up
441 a timeout. */
443 if (info->disp_info && info->disp_info->cache_timeout_event == NULL) {
444 free_samr_cache(info->disp_info);
447 talloc_destroy(info->mem_ctx);
450 /*******************************************************************
451 Idle event handler. Throw away the disp info cache.
452 ********************************************************************/
454 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
455 struct timed_event *te,
456 const struct timeval *now,
457 void *private_data)
459 DISP_INFO *disp_info = (DISP_INFO *)private_data;
461 TALLOC_FREE(disp_info->cache_timeout_event);
463 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
464 "out\n"));
465 free_samr_cache(disp_info);
468 /*******************************************************************
469 Setup cache removal idle event handler.
470 ********************************************************************/
472 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
474 /* Remove any pending timeout and update. */
476 TALLOC_FREE(disp_info->cache_timeout_event);
478 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
479 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
480 (unsigned int)secs_fromnow ));
482 disp_info->cache_timeout_event = event_add_timed(
483 smbd_event_context(), NULL,
484 timeval_current_ofs(secs_fromnow, 0),
485 "disp_info_cache_idle_timeout_handler",
486 disp_info_cache_idle_timeout_handler, (void *)disp_info);
489 /*******************************************************************
490 Force flush any cache. We do this on any samr_set_xxx call.
491 We must also remove the timeout handler.
492 ********************************************************************/
494 static void force_flush_samr_cache(DISP_INFO *disp_info)
496 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
497 return;
500 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
501 TALLOC_FREE(disp_info->cache_timeout_event);
502 free_samr_cache(disp_info);
505 /*******************************************************************
506 Ensure password info is never given out. Paranioa... JRA.
507 ********************************************************************/
509 static void samr_clear_sam_passwd(struct samu *sam_pass)
512 if (!sam_pass)
513 return;
515 /* These now zero out the old password */
517 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
518 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
521 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
523 struct samr_displayentry *entry;
525 if (info->builtin_domain) {
526 /* No users in builtin. */
527 return 0;
530 if (info->users == NULL) {
531 info->users = pdb_search_users(acct_flags);
532 if (info->users == NULL) {
533 return 0;
536 /* Fetch the last possible entry, thus trigger an enumeration */
537 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
539 /* Ensure we cache this enumeration. */
540 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
542 return info->users->num_entries;
545 static uint32 count_sam_groups(struct disp_info *info)
547 struct samr_displayentry *entry;
549 if (info->builtin_domain) {
550 /* No groups in builtin. */
551 return 0;
554 if (info->groups == NULL) {
555 info->groups = pdb_search_groups();
556 if (info->groups == NULL) {
557 return 0;
560 /* Fetch the last possible entry, thus trigger an enumeration */
561 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
563 /* Ensure we cache this enumeration. */
564 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
566 return info->groups->num_entries;
569 static uint32 count_sam_aliases(struct disp_info *info)
571 struct samr_displayentry *entry;
573 if (info->aliases == NULL) {
574 info->aliases = pdb_search_aliases(&info->sid);
575 if (info->aliases == NULL) {
576 return 0;
579 /* Fetch the last possible entry, thus trigger an enumeration */
580 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
582 /* Ensure we cache this enumeration. */
583 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
585 return info->aliases->num_entries;
588 /*******************************************************************
589 _samr_Close
590 ********************************************************************/
592 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
594 if (!close_policy_hnd(p, r->in.handle)) {
595 return NT_STATUS_INVALID_HANDLE;
598 ZERO_STRUCTP(r->out.handle);
600 return NT_STATUS_OK;
603 /*******************************************************************
604 _samr_OpenDomain
605 ********************************************************************/
607 NTSTATUS _samr_OpenDomain(pipes_struct *p,
608 struct samr_OpenDomain *r)
610 struct samr_info *info;
611 SEC_DESC *psd = NULL;
612 uint32 acc_granted;
613 uint32 des_access = r->in.access_mask;
614 NTSTATUS status;
615 size_t sd_size;
616 SE_PRIV se_rights;
618 /* find the connection policy handle. */
620 if ( !find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info) )
621 return NT_STATUS_INVALID_HANDLE;
623 /*check if access can be granted as requested by client. */
624 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
626 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
627 se_map_generic( &des_access, &dom_generic_mapping );
629 se_priv_copy( &se_rights, &se_machine_account );
630 se_priv_add( &se_rights, &se_add_users );
632 status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
633 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
634 &acc_granted, "_samr_OpenDomain" );
636 if ( !NT_STATUS_IS_OK(status) )
637 return status;
639 if (!sid_check_is_domain(r->in.sid) &&
640 !sid_check_is_builtin(r->in.sid)) {
641 return NT_STATUS_NO_SUCH_DOMAIN;
644 /* associate the domain SID with the (unique) handle. */
645 if ((info = get_samr_info_by_sid(r->in.sid))==NULL)
646 return NT_STATUS_NO_MEMORY;
647 info->acc_granted = acc_granted;
649 /* get a (unique) handle. open a policy on it. */
650 if (!create_policy_hnd(p, r->out.domain_handle, free_samr_info, (void *)info))
651 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
653 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
655 return NT_STATUS_OK;
658 /*******************************************************************
659 _samr_GetUserPwInfo
660 ********************************************************************/
662 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
663 struct samr_GetUserPwInfo *r)
665 struct samr_info *info = NULL;
666 enum lsa_SidType sid_type;
667 uint32_t min_password_length = 0;
668 uint32_t password_properties = 0;
669 bool ret = false;
670 NTSTATUS status;
672 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
674 /* find the policy handle. open a policy on it. */
675 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info)) {
676 return NT_STATUS_INVALID_HANDLE;
679 status = access_check_samr_function(info->acc_granted,
680 SAMR_USER_ACCESS_GET_ATTRIBUTES,
681 "_samr_GetUserPwInfo" );
682 if (!NT_STATUS_IS_OK(status)) {
683 return status;
686 if (!sid_check_is_in_our_domain(&info->sid)) {
687 return NT_STATUS_OBJECT_TYPE_MISMATCH;
690 become_root();
691 ret = lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, &sid_type);
692 unbecome_root();
693 if (ret == false) {
694 return NT_STATUS_NO_SUCH_USER;
697 switch (sid_type) {
698 case SID_NAME_USER:
699 become_root();
700 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
701 &min_password_length);
702 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
703 &password_properties);
704 unbecome_root();
706 if (lp_check_password_script() && *lp_check_password_script()) {
707 password_properties |= DOMAIN_PASSWORD_COMPLEX;
710 break;
711 default:
712 break;
715 r->out.info->min_password_length = min_password_length;
716 r->out.info->password_properties = password_properties;
718 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
720 return NT_STATUS_OK;
723 /*******************************************************************
724 ********************************************************************/
726 static bool get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
727 DOM_SID *sid, uint32 *acc_granted,
728 DISP_INFO **ppdisp_info)
730 struct samr_info *info = NULL;
732 /* find the policy handle. open a policy on it. */
733 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
734 return False;
736 if (!info)
737 return False;
739 *sid = info->sid;
740 *acc_granted = info->acc_granted;
741 if (ppdisp_info) {
742 *ppdisp_info = info->disp_info;
745 return True;
748 /*******************************************************************
749 _samr_SetSecurity
750 ********************************************************************/
752 NTSTATUS _samr_SetSecurity(pipes_struct *p,
753 struct samr_SetSecurity *r)
755 DOM_SID pol_sid;
756 uint32 acc_granted, i;
757 SEC_ACL *dacl;
758 bool ret;
759 struct samu *sampass=NULL;
760 NTSTATUS status;
762 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
763 return NT_STATUS_INVALID_HANDLE;
765 if (!(sampass = samu_new( p->mem_ctx))) {
766 DEBUG(0,("No memory!\n"));
767 return NT_STATUS_NO_MEMORY;
770 /* get the user record */
771 become_root();
772 ret = pdb_getsampwsid(sampass, &pol_sid);
773 unbecome_root();
775 if (!ret) {
776 DEBUG(4, ("User %s not found\n", sid_string_dbg(&pol_sid)));
777 TALLOC_FREE(sampass);
778 return NT_STATUS_INVALID_HANDLE;
781 dacl = r->in.sdbuf->sd->dacl;
782 for (i=0; i < dacl->num_aces; i++) {
783 if (sid_equal(&pol_sid, &dacl->aces[i].trustee)) {
784 ret = pdb_set_pass_can_change(sampass,
785 (dacl->aces[i].access_mask &
786 SA_RIGHT_USER_CHANGE_PASSWORD) ?
787 True: False);
788 break;
792 if (!ret) {
793 TALLOC_FREE(sampass);
794 return NT_STATUS_ACCESS_DENIED;
797 status = access_check_samr_function(acc_granted,
798 SA_RIGHT_USER_SET_ATTRIBUTES,
799 "_samr_SetSecurity");
800 if (NT_STATUS_IS_OK(status)) {
801 become_root();
802 status = pdb_update_sam_account(sampass);
803 unbecome_root();
806 TALLOC_FREE(sampass);
808 return status;
811 /*******************************************************************
812 build correct perms based on policies and password times for _samr_query_sec_obj
813 *******************************************************************/
814 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, DOM_SID *user_sid)
816 struct samu *sampass=NULL;
817 bool ret;
819 if ( !(sampass = samu_new( mem_ctx )) ) {
820 DEBUG(0,("No memory!\n"));
821 return False;
824 become_root();
825 ret = pdb_getsampwsid(sampass, user_sid);
826 unbecome_root();
828 if (ret == False) {
829 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
830 TALLOC_FREE(sampass);
831 return False;
834 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
836 if (pdb_get_pass_can_change(sampass)) {
837 TALLOC_FREE(sampass);
838 return True;
840 TALLOC_FREE(sampass);
841 return False;
845 /*******************************************************************
846 _samr_QuerySecurity
847 ********************************************************************/
849 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
850 struct samr_QuerySecurity *r)
852 NTSTATUS status;
853 DOM_SID pol_sid;
854 SEC_DESC * psd = NULL;
855 uint32 acc_granted;
856 size_t sd_size;
858 /* Get the SID. */
859 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
860 return NT_STATUS_INVALID_HANDLE;
862 DEBUG(10,("_samr_QuerySecurity: querying security on SID: %s\n",
863 sid_string_dbg(&pol_sid)));
865 status = access_check_samr_function(acc_granted,
866 STD_RIGHT_READ_CONTROL_ACCESS,
867 "_samr_QuerySecurity");
868 if (!NT_STATUS_IS_OK(status)) {
869 return status;
872 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
874 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
875 if (pol_sid.sid_rev_num == 0) {
876 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
877 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
878 } else if (sid_equal(&pol_sid,get_global_sam_sid())) {
879 /* check if it is our domain SID */
880 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
881 "with SID: %s\n", sid_string_dbg(&pol_sid)));
882 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
883 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
884 /* check if it is the Builtin Domain */
885 /* TODO: Builtin probably needs a different SD with restricted write access*/
886 DEBUG(5,("_samr_QuerySecurity: querying security on Builtin "
887 "Domain with SID: %s\n", sid_string_dbg(&pol_sid)));
888 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
889 } else if (sid_check_is_in_our_domain(&pol_sid) ||
890 sid_check_is_in_builtin(&pol_sid)) {
891 /* TODO: different SDs have to be generated for aliases groups and users.
892 Currently all three get a default user SD */
893 DEBUG(10,("_samr_QuerySecurity: querying security on Object "
894 "with SID: %s\n", sid_string_dbg(&pol_sid)));
895 if (check_change_pw_access(p->mem_ctx, &pol_sid)) {
896 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
897 &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
898 } else {
899 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_nopwchange_generic_mapping,
900 &pol_sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
902 } else {
903 return NT_STATUS_OBJECT_TYPE_MISMATCH;
906 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
907 return NT_STATUS_NO_MEMORY;
909 return status;
912 /*******************************************************************
913 makes a SAM_ENTRY / UNISTR2* structure from a user list.
914 ********************************************************************/
916 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
917 struct samr_SamEntry **sam_pp,
918 uint32_t num_entries,
919 uint32_t start_idx,
920 struct samr_displayentry *entries)
922 uint32_t i;
923 struct samr_SamEntry *sam;
925 *sam_pp = NULL;
927 if (num_entries == 0) {
928 return NT_STATUS_OK;
931 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
932 if (sam == NULL) {
933 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
934 return NT_STATUS_NO_MEMORY;
937 for (i = 0; i < num_entries; i++) {
938 #if 0
940 * usrmgr expects a non-NULL terminated string with
941 * trust relationships
943 if (entries[i].acct_flags & ACB_DOMTRUST) {
944 init_unistr2(&uni_temp_name, entries[i].account_name,
945 UNI_FLAGS_NONE);
946 } else {
947 init_unistr2(&uni_temp_name, entries[i].account_name,
948 UNI_STR_TERMINATE);
950 #endif
951 init_lsa_String(&sam[i].name, entries[i].account_name);
952 sam[i].idx = entries[i].rid;
955 *sam_pp = sam;
957 return NT_STATUS_OK;
960 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
962 /*******************************************************************
963 _samr_EnumDomainUsers
964 ********************************************************************/
966 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
967 struct samr_EnumDomainUsers *r)
969 NTSTATUS status;
970 struct samr_info *info = NULL;
971 int num_account;
972 uint32 enum_context = *r->in.resume_handle;
973 enum remote_arch_types ra_type = get_remote_arch();
974 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
975 uint32 max_entries = max_sam_entries;
976 struct samr_displayentry *entries = NULL;
977 struct samr_SamArray *samr_array = NULL;
978 struct samr_SamEntry *samr_entries = NULL;
980 /* find the policy handle. open a policy on it. */
981 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
982 return NT_STATUS_INVALID_HANDLE;
984 status = access_check_samr_function(info->acc_granted,
985 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
986 "_samr_EnumDomainUsers");
987 if (!NT_STATUS_IS_OK(status)) {
988 return status;
991 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
993 if (info->builtin_domain) {
994 /* No users in builtin. */
995 *r->out.resume_handle = *r->in.resume_handle;
996 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
997 return status;
1000 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1001 if (!samr_array) {
1002 return NT_STATUS_NO_MEMORY;
1005 become_root();
1007 /* AS ROOT !!!! */
1009 if ((info->disp_info->enum_users != NULL) &&
1010 (info->disp_info->enum_acb_mask != r->in.acct_flags)) {
1011 pdb_search_destroy(info->disp_info->enum_users);
1012 info->disp_info->enum_users = NULL;
1015 if (info->disp_info->enum_users == NULL) {
1016 info->disp_info->enum_users = pdb_search_users(r->in.acct_flags);
1017 info->disp_info->enum_acb_mask = r->in.acct_flags;
1020 if (info->disp_info->enum_users == NULL) {
1021 /* END AS ROOT !!!! */
1022 unbecome_root();
1023 return NT_STATUS_ACCESS_DENIED;
1026 num_account = pdb_search_entries(info->disp_info->enum_users,
1027 enum_context, max_entries,
1028 &entries);
1030 /* END AS ROOT !!!! */
1032 unbecome_root();
1034 if (num_account == 0) {
1035 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
1036 "total entries\n"));
1037 *r->out.resume_handle = *r->in.resume_handle;
1038 return NT_STATUS_OK;
1041 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
1042 num_account, enum_context,
1043 entries);
1044 if (!NT_STATUS_IS_OK(status)) {
1045 return status;
1048 if (max_entries <= num_account) {
1049 status = STATUS_MORE_ENTRIES;
1050 } else {
1051 status = NT_STATUS_OK;
1054 /* Ensure we cache this enumeration. */
1055 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1057 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
1059 samr_array->count = num_account;
1060 samr_array->entries = samr_entries;
1062 *r->out.resume_handle = *r->in.resume_handle + num_account;
1063 *r->out.sam = samr_array;
1064 *r->out.num_entries = num_account;
1066 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1068 return status;
1071 /*******************************************************************
1072 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1073 ********************************************************************/
1075 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1076 struct samr_SamEntry **sam_pp,
1077 uint32_t num_sam_entries,
1078 struct samr_displayentry *entries)
1080 struct samr_SamEntry *sam;
1081 uint32_t i;
1083 *sam_pp = NULL;
1085 if (num_sam_entries == 0) {
1086 return;
1089 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1090 if (sam == NULL) {
1091 return;
1094 for (i = 0; i < num_sam_entries; i++) {
1096 * JRA. I think this should include the null. TNG does not.
1098 init_lsa_String(&sam[i].name, entries[i].account_name);
1099 sam[i].idx = entries[i].rid;
1102 *sam_pp = sam;
1105 /*******************************************************************
1106 _samr_EnumDomainGroups
1107 ********************************************************************/
1109 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1110 struct samr_EnumDomainGroups *r)
1112 NTSTATUS status;
1113 struct samr_info *info = NULL;
1114 struct samr_displayentry *groups;
1115 uint32 num_groups;
1116 struct samr_SamArray *samr_array = NULL;
1117 struct samr_SamEntry *samr_entries = NULL;
1119 /* find the policy handle. open a policy on it. */
1120 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1121 return NT_STATUS_INVALID_HANDLE;
1123 status = access_check_samr_function(info->acc_granted,
1124 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1125 "_samr_EnumDomainGroups");
1126 if (!NT_STATUS_IS_OK(status)) {
1127 return status;
1130 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1132 if (info->builtin_domain) {
1133 /* No groups in builtin. */
1134 *r->out.resume_handle = *r->in.resume_handle;
1135 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1136 return status;
1139 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1140 if (!samr_array) {
1141 return NT_STATUS_NO_MEMORY;
1144 /* the domain group array is being allocated in the function below */
1146 become_root();
1148 if (info->disp_info->groups == NULL) {
1149 info->disp_info->groups = pdb_search_groups();
1151 if (info->disp_info->groups == NULL) {
1152 unbecome_root();
1153 return NT_STATUS_ACCESS_DENIED;
1157 num_groups = pdb_search_entries(info->disp_info->groups,
1158 *r->in.resume_handle,
1159 MAX_SAM_ENTRIES, &groups);
1160 unbecome_root();
1162 /* Ensure we cache this enumeration. */
1163 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1165 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1166 num_groups, groups);
1168 if (MAX_SAM_ENTRIES <= num_groups) {
1169 status = STATUS_MORE_ENTRIES;
1170 } else {
1171 status = NT_STATUS_OK;
1174 samr_array->count = num_groups;
1175 samr_array->entries = samr_entries;
1177 *r->out.sam = samr_array;
1178 *r->out.num_entries = num_groups;
1179 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1181 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1183 return status;
1186 /*******************************************************************
1187 _samr_EnumDomainAliases
1188 ********************************************************************/
1190 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1191 struct samr_EnumDomainAliases *r)
1193 NTSTATUS status;
1194 struct samr_info *info;
1195 struct samr_displayentry *aliases;
1196 uint32 num_aliases = 0;
1197 struct samr_SamArray *samr_array = NULL;
1198 struct samr_SamEntry *samr_entries = NULL;
1200 /* find the policy handle. open a policy on it. */
1201 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1202 return NT_STATUS_INVALID_HANDLE;
1204 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1205 sid_string_dbg(&info->sid)));
1207 status = access_check_samr_function(info->acc_granted,
1208 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1209 "_samr_EnumDomainAliases");
1210 if (!NT_STATUS_IS_OK(status)) {
1211 return status;
1214 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1215 if (!samr_array) {
1216 return NT_STATUS_NO_MEMORY;
1219 become_root();
1221 if (info->disp_info->aliases == NULL) {
1222 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1223 if (info->disp_info->aliases == NULL) {
1224 unbecome_root();
1225 return NT_STATUS_ACCESS_DENIED;
1229 num_aliases = pdb_search_entries(info->disp_info->aliases,
1230 *r->in.resume_handle,
1231 MAX_SAM_ENTRIES, &aliases);
1232 unbecome_root();
1234 /* Ensure we cache this enumeration. */
1235 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1237 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1238 num_aliases, aliases);
1240 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1242 if (MAX_SAM_ENTRIES <= num_aliases) {
1243 status = STATUS_MORE_ENTRIES;
1244 } else {
1245 status = NT_STATUS_OK;
1248 samr_array->count = num_aliases;
1249 samr_array->entries = samr_entries;
1251 *r->out.sam = samr_array;
1252 *r->out.num_entries = num_aliases;
1253 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1255 return status;
1258 /*******************************************************************
1259 inits a samr_DispInfoGeneral structure.
1260 ********************************************************************/
1262 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1263 struct samr_DispInfoGeneral *r,
1264 uint32_t num_entries,
1265 uint32_t start_idx,
1266 struct samr_displayentry *entries)
1268 uint32 i;
1270 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1272 if (num_entries == 0) {
1273 return NT_STATUS_OK;
1276 r->count = num_entries;
1278 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1279 if (!r->entries) {
1280 return NT_STATUS_NO_MEMORY;
1283 for (i = 0; i < num_entries ; i++) {
1285 init_lsa_String(&r->entries[i].account_name,
1286 entries[i].account_name);
1288 init_lsa_String(&r->entries[i].description,
1289 entries[i].description);
1291 init_lsa_String(&r->entries[i].full_name,
1292 entries[i].fullname);
1294 r->entries[i].rid = entries[i].rid;
1295 r->entries[i].acct_flags = entries[i].acct_flags;
1296 r->entries[i].idx = start_idx+i+1;
1299 return NT_STATUS_OK;
1302 /*******************************************************************
1303 inits a samr_DispInfoFull structure.
1304 ********************************************************************/
1306 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1307 struct samr_DispInfoFull *r,
1308 uint32_t num_entries,
1309 uint32_t start_idx,
1310 struct samr_displayentry *entries)
1312 uint32_t i;
1314 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1316 if (num_entries == 0) {
1317 return NT_STATUS_OK;
1320 r->count = num_entries;
1322 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1323 if (!r->entries) {
1324 return NT_STATUS_NO_MEMORY;
1327 for (i = 0; i < num_entries ; i++) {
1329 init_lsa_String(&r->entries[i].account_name,
1330 entries[i].account_name);
1332 init_lsa_String(&r->entries[i].description,
1333 entries[i].description);
1335 r->entries[i].rid = entries[i].rid;
1336 r->entries[i].acct_flags = entries[i].acct_flags;
1337 r->entries[i].idx = start_idx+i+1;
1340 return NT_STATUS_OK;
1343 /*******************************************************************
1344 inits a samr_DispInfoFullGroups structure.
1345 ********************************************************************/
1347 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1348 struct samr_DispInfoFullGroups *r,
1349 uint32_t num_entries,
1350 uint32_t start_idx,
1351 struct samr_displayentry *entries)
1353 uint32_t i;
1355 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1357 if (num_entries == 0) {
1358 return NT_STATUS_OK;
1361 r->count = num_entries;
1363 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1364 if (!r->entries) {
1365 return NT_STATUS_NO_MEMORY;
1368 for (i = 0; i < num_entries ; i++) {
1370 init_lsa_String(&r->entries[i].account_name,
1371 entries[i].account_name);
1373 init_lsa_String(&r->entries[i].description,
1374 entries[i].description);
1376 r->entries[i].rid = entries[i].rid;
1377 r->entries[i].acct_flags = entries[i].acct_flags;
1378 r->entries[i].idx = start_idx+i+1;
1381 return NT_STATUS_OK;
1384 /*******************************************************************
1385 inits a samr_DispInfoAscii structure.
1386 ********************************************************************/
1388 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1389 struct samr_DispInfoAscii *r,
1390 uint32_t num_entries,
1391 uint32_t start_idx,
1392 struct samr_displayentry *entries)
1394 uint32_t i;
1396 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1398 if (num_entries == 0) {
1399 return NT_STATUS_OK;
1402 r->count = num_entries;
1404 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1405 if (!r->entries) {
1406 return NT_STATUS_NO_MEMORY;
1409 for (i = 0; i < num_entries ; i++) {
1411 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1412 entries[i].account_name);
1414 r->entries[i].idx = start_idx+i+1;
1417 return NT_STATUS_OK;
1420 /*******************************************************************
1421 inits a samr_DispInfoAscii structure.
1422 ********************************************************************/
1424 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1425 struct samr_DispInfoAscii *r,
1426 uint32_t num_entries,
1427 uint32_t start_idx,
1428 struct samr_displayentry *entries)
1430 uint32_t i;
1432 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1434 if (num_entries == 0) {
1435 return NT_STATUS_OK;
1438 r->count = num_entries;
1440 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1441 if (!r->entries) {
1442 return NT_STATUS_NO_MEMORY;
1445 for (i = 0; i < num_entries ; i++) {
1447 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1448 entries[i].account_name);
1450 r->entries[i].idx = start_idx+i+1;
1453 return NT_STATUS_OK;
1456 /*******************************************************************
1457 _samr_QueryDisplayInfo
1458 ********************************************************************/
1460 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1461 struct samr_QueryDisplayInfo *r)
1463 NTSTATUS status;
1464 struct samr_info *info = NULL;
1465 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1467 uint32 max_entries = r->in.max_entries;
1468 uint32 enum_context = r->in.start_idx;
1469 uint32 max_size = r->in.buf_size;
1471 union samr_DispInfo *disp_info = r->out.info;
1473 uint32 temp_size=0, total_data_size=0;
1474 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1475 uint32 num_account = 0;
1476 enum remote_arch_types ra_type = get_remote_arch();
1477 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1478 struct samr_displayentry *entries = NULL;
1480 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1482 /* find the policy handle. open a policy on it. */
1483 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1484 return NT_STATUS_INVALID_HANDLE;
1486 status = access_check_samr_function(info->acc_granted,
1487 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1488 "_samr_QueryDisplayInfo");
1489 if (!NT_STATUS_IS_OK(status)) {
1490 return status;
1494 * calculate how many entries we will return.
1495 * based on
1496 * - the number of entries the client asked
1497 * - our limit on that
1498 * - the starting point (enumeration context)
1499 * - the buffer size the client will accept
1503 * We are a lot more like W2K. Instead of reading the SAM
1504 * each time to find the records we need to send back,
1505 * we read it once and link that copy to the sam handle.
1506 * For large user list (over the MAX_SAM_ENTRIES)
1507 * it's a definitive win.
1508 * second point to notice: between enumerations
1509 * our sam is now the same as it's a snapshoot.
1510 * third point: got rid of the static SAM_USER_21 struct
1511 * no more intermediate.
1512 * con: it uses much more memory, as a full copy is stored
1513 * in memory.
1515 * If you want to change it, think twice and think
1516 * of the second point , that's really important.
1518 * JFM, 12/20/2001
1521 if ((r->in.level < 1) || (r->in.level > 5)) {
1522 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1523 (unsigned int)r->in.level ));
1524 return NT_STATUS_INVALID_INFO_CLASS;
1527 /* first limit the number of entries we will return */
1528 if(max_entries > max_sam_entries) {
1529 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1530 "entries, limiting to %d\n", max_entries,
1531 max_sam_entries));
1532 max_entries = max_sam_entries;
1535 /* calculate the size and limit on the number of entries we will
1536 * return */
1538 temp_size=max_entries*struct_size;
1540 if (temp_size>max_size) {
1541 max_entries=MIN((max_size/struct_size),max_entries);;
1542 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1543 "only %d entries\n", max_entries));
1546 become_root();
1548 /* THe following done as ROOT. Don't return without unbecome_root(). */
1550 switch (r->in.level) {
1551 case 0x1:
1552 case 0x4:
1553 if (info->disp_info->users == NULL) {
1554 info->disp_info->users = pdb_search_users(ACB_NORMAL);
1555 if (info->disp_info->users == NULL) {
1556 unbecome_root();
1557 return NT_STATUS_ACCESS_DENIED;
1559 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1560 (unsigned int)enum_context ));
1561 } else {
1562 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1563 (unsigned int)enum_context ));
1566 num_account = pdb_search_entries(info->disp_info->users,
1567 enum_context, max_entries,
1568 &entries);
1569 break;
1570 case 0x2:
1571 if (info->disp_info->machines == NULL) {
1572 info->disp_info->machines =
1573 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1574 if (info->disp_info->machines == NULL) {
1575 unbecome_root();
1576 return NT_STATUS_ACCESS_DENIED;
1578 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1579 (unsigned int)enum_context ));
1580 } else {
1581 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1582 (unsigned int)enum_context ));
1585 num_account = pdb_search_entries(info->disp_info->machines,
1586 enum_context, max_entries,
1587 &entries);
1588 break;
1589 case 0x3:
1590 case 0x5:
1591 if (info->disp_info->groups == NULL) {
1592 info->disp_info->groups = pdb_search_groups();
1593 if (info->disp_info->groups == NULL) {
1594 unbecome_root();
1595 return NT_STATUS_ACCESS_DENIED;
1597 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1598 (unsigned int)enum_context ));
1599 } else {
1600 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1601 (unsigned int)enum_context ));
1604 num_account = pdb_search_entries(info->disp_info->groups,
1605 enum_context, max_entries,
1606 &entries);
1607 break;
1608 default:
1609 unbecome_root();
1610 smb_panic("info class changed");
1611 break;
1613 unbecome_root();
1616 /* Now create reply structure */
1617 switch (r->in.level) {
1618 case 0x1:
1619 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1620 num_account, enum_context,
1621 entries);
1622 break;
1623 case 0x2:
1624 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1625 num_account, enum_context,
1626 entries);
1627 break;
1628 case 0x3:
1629 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1630 num_account, enum_context,
1631 entries);
1632 break;
1633 case 0x4:
1634 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1635 num_account, enum_context,
1636 entries);
1637 break;
1638 case 0x5:
1639 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1640 num_account, enum_context,
1641 entries);
1642 break;
1643 default:
1644 smb_panic("info class changed");
1645 break;
1648 if (!NT_STATUS_IS_OK(disp_ret))
1649 return disp_ret;
1651 /* calculate the total size */
1652 total_data_size=num_account*struct_size;
1654 if (max_entries <= num_account) {
1655 status = STATUS_MORE_ENTRIES;
1656 } else {
1657 status = NT_STATUS_OK;
1660 /* Ensure we cache this enumeration. */
1661 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1663 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1665 *r->out.total_size = total_data_size;
1666 *r->out.returned_size = temp_size;
1668 return status;
1671 /****************************************************************
1672 _samr_QueryDisplayInfo2
1673 ****************************************************************/
1675 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1676 struct samr_QueryDisplayInfo2 *r)
1678 struct samr_QueryDisplayInfo q;
1680 q.in.domain_handle = r->in.domain_handle;
1681 q.in.level = r->in.level;
1682 q.in.start_idx = r->in.start_idx;
1683 q.in.max_entries = r->in.max_entries;
1684 q.in.buf_size = r->in.buf_size;
1686 q.out.total_size = r->out.total_size;
1687 q.out.returned_size = r->out.returned_size;
1688 q.out.info = r->out.info;
1690 return _samr_QueryDisplayInfo(p, &q);
1693 /****************************************************************
1694 _samr_QueryDisplayInfo3
1695 ****************************************************************/
1697 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1698 struct samr_QueryDisplayInfo3 *r)
1700 struct samr_QueryDisplayInfo q;
1702 q.in.domain_handle = r->in.domain_handle;
1703 q.in.level = r->in.level;
1704 q.in.start_idx = r->in.start_idx;
1705 q.in.max_entries = r->in.max_entries;
1706 q.in.buf_size = r->in.buf_size;
1708 q.out.total_size = r->out.total_size;
1709 q.out.returned_size = r->out.returned_size;
1710 q.out.info = r->out.info;
1712 return _samr_QueryDisplayInfo(p, &q);
1715 /*******************************************************************
1716 _samr_QueryAliasInfo
1717 ********************************************************************/
1719 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1720 struct samr_QueryAliasInfo *r)
1722 DOM_SID sid;
1723 struct acct_info info;
1724 uint32 acc_granted;
1725 NTSTATUS status;
1726 union samr_AliasInfo *alias_info = NULL;
1727 const char *alias_name = NULL;
1728 const char *alias_description = NULL;
1730 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1732 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1733 if (!alias_info) {
1734 return NT_STATUS_NO_MEMORY;
1737 /* find the policy handle. open a policy on it. */
1738 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &sid, &acc_granted, NULL))
1739 return NT_STATUS_INVALID_HANDLE;
1741 status = access_check_samr_function(acc_granted,
1742 SA_RIGHT_ALIAS_LOOKUP_INFO,
1743 "_samr_QueryAliasInfo");
1744 if (!NT_STATUS_IS_OK(status)) {
1745 return status;
1748 become_root();
1749 status = pdb_get_aliasinfo(&sid, &info);
1750 unbecome_root();
1752 if ( !NT_STATUS_IS_OK(status))
1753 return status;
1755 /* FIXME: info contains fstrings */
1756 alias_name = talloc_strdup(r, info.acct_name);
1757 alias_description = talloc_strdup(r, info.acct_desc);
1759 switch (r->in.level) {
1760 case ALIASINFOALL:
1761 init_samr_alias_info1(&alias_info->all,
1762 alias_name,
1764 alias_description);
1765 break;
1766 case ALIASINFODESCRIPTION:
1767 init_samr_alias_info3(&alias_info->description,
1768 alias_description);
1769 break;
1770 default:
1771 return NT_STATUS_INVALID_INFO_CLASS;
1774 *r->out.info = alias_info;
1776 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1778 return NT_STATUS_OK;
1781 #if 0
1782 /*******************************************************************
1783 samr_reply_lookup_ids
1784 ********************************************************************/
1786 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1788 uint32 rid[MAX_SAM_ENTRIES];
1789 int num_rids = q_u->num_sids1;
1791 r_u->status = NT_STATUS_OK;
1793 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1795 if (num_rids > MAX_SAM_ENTRIES) {
1796 num_rids = MAX_SAM_ENTRIES;
1797 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1800 #if 0
1801 int i;
1802 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1804 for (i = 0; i < num_rids && status == 0; i++)
1806 struct sam_passwd *sam_pass;
1807 fstring user_name;
1810 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1811 q_u->uni_user_name[i].uni_str_len));
1813 /* find the user account */
1814 become_root();
1815 sam_pass = get_smb21pwd_entry(user_name, 0);
1816 unbecome_root();
1818 if (sam_pass == NULL)
1820 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1821 rid[i] = 0;
1823 else
1825 rid[i] = sam_pass->user_rid;
1828 #endif
1830 num_rids = 1;
1831 rid[0] = BUILTIN_ALIAS_RID_USERS;
1833 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1835 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1837 return r_u->status;
1839 #endif
1841 /*******************************************************************
1842 _samr_LookupNames
1843 ********************************************************************/
1845 NTSTATUS _samr_LookupNames(pipes_struct *p,
1846 struct samr_LookupNames *r)
1848 NTSTATUS status;
1849 uint32 *rid;
1850 enum lsa_SidType *type;
1851 int i;
1852 int num_rids = r->in.num_names;
1853 DOM_SID pol_sid;
1854 uint32 acc_granted;
1855 struct samr_Ids rids, types;
1856 uint32_t num_mapped = 0;
1858 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1860 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL)) {
1861 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1864 status = access_check_samr_function(acc_granted,
1865 0, /* Don't know the acc_bits yet */
1866 "_samr_LookupNames");
1867 if (!NT_STATUS_IS_OK(status)) {
1868 return status;
1871 if (num_rids > MAX_SAM_ENTRIES) {
1872 num_rids = MAX_SAM_ENTRIES;
1873 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1876 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1877 NT_STATUS_HAVE_NO_MEMORY(rid);
1879 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1880 NT_STATUS_HAVE_NO_MEMORY(type);
1882 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1883 sid_string_dbg(&pol_sid)));
1885 for (i = 0; i < num_rids; i++) {
1887 status = NT_STATUS_NONE_MAPPED;
1888 type[i] = SID_NAME_UNKNOWN;
1890 rid[i] = 0xffffffff;
1892 if (sid_check_is_builtin(&pol_sid)) {
1893 if (lookup_builtin_name(r->in.names[i].string,
1894 &rid[i]))
1896 type[i] = SID_NAME_ALIAS;
1898 } else {
1899 lookup_global_sam_name(r->in.names[i].string, 0,
1900 &rid[i], &type[i]);
1903 if (type[i] != SID_NAME_UNKNOWN) {
1904 num_mapped++;
1908 if (num_mapped == num_rids) {
1909 status = NT_STATUS_OK;
1910 } else if (num_mapped == 0) {
1911 status = NT_STATUS_NONE_MAPPED;
1912 } else {
1913 status = STATUS_SOME_UNMAPPED;
1916 rids.count = num_rids;
1917 rids.ids = rid;
1919 types.count = num_rids;
1920 types.ids = type;
1922 *r->out.rids = rids;
1923 *r->out.types = types;
1925 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1927 return status;
1930 /*******************************************************************
1931 _samr_ChangePasswordUser2
1932 ********************************************************************/
1934 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1935 struct samr_ChangePasswordUser2 *r)
1937 NTSTATUS status;
1938 fstring user_name;
1939 fstring wks;
1941 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1943 fstrcpy(user_name, r->in.account->string);
1944 fstrcpy(wks, r->in.server->string);
1946 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1949 * Pass the user through the NT -> unix user mapping
1950 * function.
1953 (void)map_username(user_name);
1956 * UNIX username case mangling not required, pass_oem_change
1957 * is case insensitive.
1960 status = pass_oem_change(user_name,
1961 r->in.lm_password->data,
1962 r->in.lm_verifier->hash,
1963 r->in.nt_password->data,
1964 r->in.nt_verifier->hash,
1965 NULL);
1967 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1969 return status;
1972 /*******************************************************************
1973 _samr_ChangePasswordUser3
1974 ********************************************************************/
1976 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
1977 struct samr_ChangePasswordUser3 *r)
1979 NTSTATUS status;
1980 fstring user_name;
1981 const char *wks = NULL;
1982 uint32 reject_reason;
1983 struct samr_DomInfo1 *dominfo = NULL;
1984 struct samr_ChangeReject *reject = NULL;
1986 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1988 fstrcpy(user_name, r->in.account->string);
1989 if (r->in.server && r->in.server->string) {
1990 wks = r->in.server->string;
1993 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1996 * Pass the user through the NT -> unix user mapping
1997 * function.
2000 (void)map_username(user_name);
2003 * UNIX username case mangling not required, pass_oem_change
2004 * is case insensitive.
2007 status = pass_oem_change(user_name,
2008 r->in.lm_password->data,
2009 r->in.lm_verifier->hash,
2010 r->in.nt_password->data,
2011 r->in.nt_verifier->hash,
2012 &reject_reason);
2014 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
2015 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
2017 uint32 min_pass_len,pass_hist,password_properties;
2018 time_t u_expire, u_min_age;
2019 NTTIME nt_expire, nt_min_age;
2020 uint32 account_policy_temp;
2022 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
2023 if (!dominfo) {
2024 return NT_STATUS_NO_MEMORY;
2027 reject = TALLOC_ZERO_P(p->mem_ctx, struct samr_ChangeReject);
2028 if (!reject) {
2029 return NT_STATUS_NO_MEMORY;
2032 become_root();
2034 /* AS ROOT !!! */
2036 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2037 min_pass_len = account_policy_temp;
2039 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2040 pass_hist = account_policy_temp;
2042 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2043 password_properties = account_policy_temp;
2045 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2046 u_expire = account_policy_temp;
2048 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2049 u_min_age = account_policy_temp;
2051 /* !AS ROOT */
2053 unbecome_root();
2055 unix_to_nt_time_abs(&nt_expire, u_expire);
2056 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2058 if (lp_check_password_script() && *lp_check_password_script()) {
2059 password_properties |= DOMAIN_PASSWORD_COMPLEX;
2062 init_samr_DomInfo1(dominfo,
2063 min_pass_len,
2064 pass_hist,
2065 password_properties,
2066 u_expire,
2067 u_min_age);
2069 reject->reason = reject_reason;
2071 *r->out.dominfo = dominfo;
2072 *r->out.reject = reject;
2075 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2077 return status;
2080 /*******************************************************************
2081 makes a SAMR_R_LOOKUP_RIDS structure.
2082 ********************************************************************/
2084 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
2085 const char **names,
2086 struct lsa_String **lsa_name_array_p)
2088 struct lsa_String *lsa_name_array = NULL;
2089 uint32_t i;
2091 *lsa_name_array_p = NULL;
2093 if (num_names != 0) {
2094 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
2095 if (!lsa_name_array) {
2096 return false;
2100 for (i = 0; i < num_names; i++) {
2101 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2102 init_lsa_String(&lsa_name_array[i], names[i]);
2105 *lsa_name_array_p = lsa_name_array;
2107 return true;
2110 /*******************************************************************
2111 _samr_LookupRids
2112 ********************************************************************/
2114 NTSTATUS _samr_LookupRids(pipes_struct *p,
2115 struct samr_LookupRids *r)
2117 NTSTATUS status;
2118 const char **names;
2119 enum lsa_SidType *attrs = NULL;
2120 uint32 *wire_attrs = NULL;
2121 DOM_SID pol_sid;
2122 int num_rids = (int)r->in.num_rids;
2123 uint32 acc_granted;
2124 int i;
2125 struct lsa_Strings names_array;
2126 struct samr_Ids types_array;
2127 struct lsa_String *lsa_names = NULL;
2129 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2131 /* find the policy handle. open a policy on it. */
2132 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL))
2133 return NT_STATUS_INVALID_HANDLE;
2135 status = access_check_samr_function(acc_granted,
2136 0, /* Don't know the acc_bits yet */
2137 "_samr__LookupRids");
2138 if (!NT_STATUS_IS_OK(status)) {
2139 return status;
2142 if (num_rids > 1000) {
2143 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2144 "to samba4 idl this is not possible\n", num_rids));
2145 return NT_STATUS_UNSUCCESSFUL;
2148 if (num_rids) {
2149 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2150 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2151 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2153 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2154 return NT_STATUS_NO_MEMORY;
2155 } else {
2156 names = NULL;
2157 attrs = NULL;
2158 wire_attrs = NULL;
2161 become_root(); /* lookup_sid can require root privs */
2162 status = pdb_lookup_rids(&pol_sid, num_rids, r->in.rids,
2163 names, attrs);
2164 unbecome_root();
2166 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2167 status = NT_STATUS_OK;
2170 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2171 &lsa_names)) {
2172 return NT_STATUS_NO_MEMORY;
2175 /* Convert from enum lsa_SidType to uint32 for wire format. */
2176 for (i = 0; i < num_rids; i++) {
2177 wire_attrs[i] = (uint32)attrs[i];
2180 names_array.count = num_rids;
2181 names_array.names = lsa_names;
2183 types_array.count = num_rids;
2184 types_array.ids = wire_attrs;
2186 *r->out.names = names_array;
2187 *r->out.types = types_array;
2189 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2191 return status;
2194 /*******************************************************************
2195 _samr_OpenUser
2196 ********************************************************************/
2198 NTSTATUS _samr_OpenUser(pipes_struct *p,
2199 struct samr_OpenUser *r)
2201 struct samu *sampass=NULL;
2202 DOM_SID sid;
2203 POLICY_HND domain_pol = *r->in.domain_handle;
2204 POLICY_HND *user_pol = r->out.user_handle;
2205 struct samr_info *info = NULL;
2206 SEC_DESC *psd = NULL;
2207 uint32 acc_granted;
2208 uint32 des_access = r->in.access_mask;
2209 size_t sd_size;
2210 bool ret;
2211 NTSTATUS nt_status;
2212 SE_PRIV se_rights;
2214 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
2216 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2217 return NT_STATUS_INVALID_HANDLE;
2219 nt_status = access_check_samr_function(acc_granted,
2220 SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
2221 "_samr_OpenUser" );
2223 if ( !NT_STATUS_IS_OK(nt_status) )
2224 return nt_status;
2226 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2227 return NT_STATUS_NO_MEMORY;
2230 /* append the user's RID to it */
2232 if (!sid_append_rid(&sid, r->in.rid))
2233 return NT_STATUS_NO_SUCH_USER;
2235 /* check if access can be granted as requested by client. */
2237 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
2239 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2240 se_map_generic(&des_access, &usr_generic_mapping);
2242 se_priv_copy( &se_rights, &se_machine_account );
2243 se_priv_add( &se_rights, &se_add_users );
2245 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2246 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2247 &acc_granted, "_samr_OpenUser");
2249 if ( !NT_STATUS_IS_OK(nt_status) )
2250 return nt_status;
2252 become_root();
2253 ret=pdb_getsampwsid(sampass, &sid);
2254 unbecome_root();
2256 /* check that the SID exists in our domain. */
2257 if (ret == False) {
2258 return NT_STATUS_NO_SUCH_USER;
2261 TALLOC_FREE(sampass);
2263 /* associate the user's SID and access bits with the new handle. */
2264 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2265 return NT_STATUS_NO_MEMORY;
2266 info->acc_granted = acc_granted;
2268 /* get a (unique) handle. open a policy on it. */
2269 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
2270 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2272 return NT_STATUS_OK;
2275 /*************************************************************************
2276 *************************************************************************/
2278 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2279 DATA_BLOB *blob,
2280 struct lsa_BinaryString **_r)
2282 struct lsa_BinaryString *r;
2284 if (!blob || !_r) {
2285 return NT_STATUS_INVALID_PARAMETER;
2288 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2289 if (!r) {
2290 return NT_STATUS_NO_MEMORY;
2293 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2294 if (!r->array) {
2295 return NT_STATUS_NO_MEMORY;
2297 memcpy(r->array, blob->data, blob->length);
2298 r->size = blob->length;
2299 r->length = blob->length;
2301 if (!r->array) {
2302 return NT_STATUS_NO_MEMORY;
2305 *_r = r;
2307 return NT_STATUS_OK;
2310 /*************************************************************************
2311 get_user_info_7. Safe. Only gives out account_name.
2312 *************************************************************************/
2314 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2315 struct samr_UserInfo7 *r,
2316 DOM_SID *user_sid)
2318 struct samu *smbpass=NULL;
2319 bool ret;
2320 const char *account_name = NULL;
2322 ZERO_STRUCTP(r);
2324 if ( !(smbpass = samu_new( mem_ctx )) ) {
2325 return NT_STATUS_NO_MEMORY;
2328 become_root();
2329 ret = pdb_getsampwsid(smbpass, user_sid);
2330 unbecome_root();
2332 if ( !ret ) {
2333 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2334 return NT_STATUS_NO_SUCH_USER;
2337 account_name = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2338 if (!account_name) {
2339 TALLOC_FREE(smbpass);
2340 return NT_STATUS_NO_MEMORY;
2342 TALLOC_FREE(smbpass);
2344 DEBUG(3,("User:[%s]\n", account_name));
2346 init_samr_user_info7(r, account_name);
2348 return NT_STATUS_OK;
2351 /*************************************************************************
2352 get_user_info_9. Only gives out primary group SID.
2353 *************************************************************************/
2355 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2356 struct samr_UserInfo9 *r,
2357 DOM_SID *user_sid)
2359 struct samu *smbpass=NULL;
2360 bool ret;
2362 ZERO_STRUCTP(r);
2364 if ( !(smbpass = samu_new( mem_ctx )) ) {
2365 return NT_STATUS_NO_MEMORY;
2368 become_root();
2369 ret = pdb_getsampwsid(smbpass, user_sid);
2370 unbecome_root();
2372 if (ret==False) {
2373 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2374 TALLOC_FREE(smbpass);
2375 return NT_STATUS_NO_SUCH_USER;
2378 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
2380 init_samr_user_info9(r, pdb_get_group_rid(smbpass));
2382 TALLOC_FREE(smbpass);
2384 return NT_STATUS_OK;
2387 /*************************************************************************
2388 get_user_info_16. Safe. Only gives out acb bits.
2389 *************************************************************************/
2391 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2392 struct samr_UserInfo16 *r,
2393 DOM_SID *user_sid)
2395 struct samu *smbpass=NULL;
2396 bool ret;
2398 ZERO_STRUCTP(r);
2400 if ( !(smbpass = samu_new( mem_ctx )) ) {
2401 return NT_STATUS_NO_MEMORY;
2404 become_root();
2405 ret = pdb_getsampwsid(smbpass, user_sid);
2406 unbecome_root();
2408 if (ret==False) {
2409 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2410 TALLOC_FREE(smbpass);
2411 return NT_STATUS_NO_SUCH_USER;
2414 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
2416 init_samr_user_info16(r, pdb_get_acct_ctrl(smbpass));
2418 TALLOC_FREE(smbpass);
2420 return NT_STATUS_OK;
2423 /*************************************************************************
2424 get_user_info_18. OK - this is the killer as it gives out password info.
2425 Ensure that this is only allowed on an encrypted connection with a root
2426 user. JRA.
2427 *************************************************************************/
2429 static NTSTATUS get_user_info_18(pipes_struct *p,
2430 TALLOC_CTX *mem_ctx,
2431 struct samr_UserInfo18 *r,
2432 DOM_SID *user_sid)
2434 struct samu *smbpass=NULL;
2435 bool ret;
2437 ZERO_STRUCTP(r);
2439 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2440 return NT_STATUS_ACCESS_DENIED;
2443 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
2444 return NT_STATUS_ACCESS_DENIED;
2448 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2451 if ( !(smbpass = samu_new( mem_ctx )) ) {
2452 return NT_STATUS_NO_MEMORY;
2455 ret = pdb_getsampwsid(smbpass, user_sid);
2457 if (ret == False) {
2458 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2459 TALLOC_FREE(smbpass);
2460 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2463 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2465 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2466 TALLOC_FREE(smbpass);
2467 return NT_STATUS_ACCOUNT_DISABLED;
2470 init_samr_user_info18(r, pdb_get_lanman_passwd(smbpass),
2471 pdb_get_nt_passwd(smbpass));
2473 TALLOC_FREE(smbpass);
2475 return NT_STATUS_OK;
2478 /*************************************************************************
2479 get_user_info_20
2480 *************************************************************************/
2482 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2483 struct samr_UserInfo20 *r,
2484 DOM_SID *user_sid)
2486 struct samu *sampass=NULL;
2487 bool ret;
2488 const char *munged_dial = NULL;
2489 DATA_BLOB blob;
2490 NTSTATUS status;
2491 struct lsa_BinaryString *parameters = NULL;
2493 ZERO_STRUCTP(r);
2495 if ( !(sampass = samu_new( mem_ctx )) ) {
2496 return NT_STATUS_NO_MEMORY;
2499 become_root();
2500 ret = pdb_getsampwsid(sampass, user_sid);
2501 unbecome_root();
2503 if (ret == False) {
2504 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2505 TALLOC_FREE(sampass);
2506 return NT_STATUS_NO_SUCH_USER;
2509 munged_dial = pdb_get_munged_dial(sampass);
2511 samr_clear_sam_passwd(sampass);
2513 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2514 munged_dial, (int)strlen(munged_dial)));
2516 if (munged_dial) {
2517 blob = base64_decode_data_blob(munged_dial);
2518 } else {
2519 blob = data_blob_string_const("");
2522 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2523 data_blob_free(&blob);
2524 TALLOC_FREE(sampass);
2525 if (!NT_STATUS_IS_OK(status)) {
2526 return status;
2529 init_samr_user_info20(r, parameters);
2531 return NT_STATUS_OK;
2535 /*************************************************************************
2536 get_user_info_21
2537 *************************************************************************/
2539 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2540 struct samr_UserInfo21 *r,
2541 DOM_SID *user_sid,
2542 DOM_SID *domain_sid)
2544 NTSTATUS status;
2545 struct samu *pw = NULL;
2546 bool ret;
2547 const DOM_SID *sid_user, *sid_group;
2548 uint32_t rid, primary_gid;
2549 NTTIME last_logon, last_logoff, last_password_change,
2550 acct_expiry, allow_password_change, force_password_change;
2551 time_t must_change_time;
2552 uint8_t password_expired;
2553 const char *account_name, *full_name, *home_directory, *home_drive,
2554 *logon_script, *profile_path, *description,
2555 *workstations, *comment;
2556 struct samr_LogonHours logon_hours;
2557 struct lsa_BinaryString *parameters = NULL;
2558 const char *munged_dial = NULL;
2559 DATA_BLOB blob;
2561 ZERO_STRUCTP(r);
2563 if (!(pw = samu_new(mem_ctx))) {
2564 return NT_STATUS_NO_MEMORY;
2567 become_root();
2568 ret = pdb_getsampwsid(pw, user_sid);
2569 unbecome_root();
2571 if (ret == False) {
2572 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
2573 TALLOC_FREE(pw);
2574 return NT_STATUS_NO_SUCH_USER;
2577 samr_clear_sam_passwd(pw);
2579 DEBUG(3,("User:[%s]\n", pdb_get_username(pw)));
2581 sid_user = pdb_get_user_sid(pw);
2583 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2584 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2585 "the domain sid %s. Failing operation.\n",
2586 pdb_get_username(pw), sid_string_dbg(sid_user),
2587 sid_string_dbg(domain_sid)));
2588 TALLOC_FREE(pw);
2589 return NT_STATUS_UNSUCCESSFUL;
2592 become_root();
2593 sid_group = pdb_get_group_sid(pw);
2594 unbecome_root();
2596 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2597 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2598 "which conflicts with the domain sid %s. Failing operation.\n",
2599 pdb_get_username(pw), sid_string_dbg(sid_group),
2600 sid_string_dbg(domain_sid)));
2601 TALLOC_FREE(pw);
2602 return NT_STATUS_UNSUCCESSFUL;
2605 unix_to_nt_time(&last_logon, pdb_get_logon_time(pw));
2606 unix_to_nt_time(&last_logoff, pdb_get_logoff_time(pw));
2607 unix_to_nt_time(&acct_expiry, pdb_get_kickoff_time(pw));
2608 unix_to_nt_time(&last_password_change, pdb_get_pass_last_set_time(pw));
2609 unix_to_nt_time(&allow_password_change, pdb_get_pass_can_change_time(pw));
2611 must_change_time = pdb_get_pass_must_change_time(pw);
2612 if (must_change_time == get_time_t_max()) {
2613 unix_to_nt_time_abs(&force_password_change, must_change_time);
2614 } else {
2615 unix_to_nt_time(&force_password_change, must_change_time);
2618 if (pdb_get_pass_must_change_time(pw) == 0) {
2619 password_expired = PASS_MUST_CHANGE_AT_NEXT_LOGON;
2620 } else {
2621 password_expired = 0;
2624 munged_dial = pdb_get_munged_dial(pw);
2625 if (munged_dial) {
2626 blob = base64_decode_data_blob(munged_dial);
2627 } else {
2628 blob = data_blob_string_const("");
2631 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2632 data_blob_free(&blob);
2633 if (!NT_STATUS_IS_OK(status)) {
2634 TALLOC_FREE(pw);
2635 return status;
2638 account_name = talloc_strdup(mem_ctx, pdb_get_username(pw));
2639 full_name = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2640 home_directory = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2641 home_drive = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2642 logon_script = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2643 profile_path = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2644 description = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2645 workstations = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2646 comment = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2648 logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2649 #if 0
2652 Look at a user on a real NT4 PDC with usrmgr, press
2653 'ok'. Then you will see that fields_present is set to
2654 0x08f827fa. Look at the user immediately after that again,
2655 and you will see that 0x00fffff is returned. This solves
2656 the problem that you get access denied after having looked
2657 at the user.
2658 -- Volker
2661 #endif
2663 init_samr_user_info21(r,
2664 last_logon,
2665 last_logoff,
2666 last_password_change,
2667 acct_expiry,
2668 allow_password_change,
2669 force_password_change,
2670 account_name,
2671 full_name,
2672 home_directory,
2673 home_drive,
2674 logon_script,
2675 profile_path,
2676 description,
2677 workstations,
2678 comment,
2679 parameters,
2680 rid,
2681 primary_gid,
2682 pdb_get_acct_ctrl(pw),
2683 pdb_build_fields_present(pw),
2684 logon_hours,
2685 pdb_get_bad_password_count(pw),
2686 pdb_get_logon_count(pw),
2687 0, /* country_code */
2688 0, /* code_page */
2689 0, /* nt_password_set */
2690 0, /* lm_password_set */
2691 password_expired);
2692 TALLOC_FREE(pw);
2694 return NT_STATUS_OK;
2697 /*******************************************************************
2698 _samr_QueryUserInfo
2699 ********************************************************************/
2701 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2702 struct samr_QueryUserInfo *r)
2704 NTSTATUS status;
2705 union samr_UserInfo *user_info = NULL;
2706 struct samr_info *info = NULL;
2707 DOM_SID domain_sid;
2708 uint32 rid;
2710 /* search for the handle */
2711 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info))
2712 return NT_STATUS_INVALID_HANDLE;
2714 status = access_check_samr_function(info->acc_granted,
2715 SAMR_USER_ACCESS_GET_ATTRIBUTES,
2716 "_samr_QueryUserInfo");
2717 if (!NT_STATUS_IS_OK(status)) {
2718 return status;
2721 domain_sid = info->sid;
2723 sid_split_rid(&domain_sid, &rid);
2725 if (!sid_check_is_in_our_domain(&info->sid))
2726 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2728 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2729 sid_string_dbg(&info->sid)));
2731 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
2732 if (!user_info) {
2733 return NT_STATUS_NO_MEMORY;
2736 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
2738 switch (r->in.level) {
2739 case 7:
2740 status = get_user_info_7(p->mem_ctx, &user_info->info7, &info->sid);
2741 if (!NT_STATUS_IS_OK(status)) {
2742 return status;
2744 break;
2745 case 9:
2746 status = get_user_info_9(p->mem_ctx, &user_info->info9, &info->sid);
2747 if (!NT_STATUS_IS_OK(status)) {
2748 return status;
2750 break;
2751 case 16:
2752 status = get_user_info_16(p->mem_ctx, &user_info->info16, &info->sid);
2753 if (!NT_STATUS_IS_OK(status)) {
2754 return status;
2756 break;
2758 case 18:
2759 status = get_user_info_18(p, p->mem_ctx, &user_info->info18, &info->sid);
2760 if (!NT_STATUS_IS_OK(status)) {
2761 return status;
2763 break;
2765 case 20:
2766 status = get_user_info_20(p->mem_ctx, &user_info->info20, &info->sid);
2767 if (!NT_STATUS_IS_OK(status)) {
2768 return status;
2770 break;
2772 case 21:
2773 status = get_user_info_21(p->mem_ctx, &user_info->info21,
2774 &info->sid, &domain_sid);
2775 if (!NT_STATUS_IS_OK(status)) {
2776 return status;
2778 break;
2780 default:
2781 return NT_STATUS_INVALID_INFO_CLASS;
2784 *r->out.info = user_info;
2786 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
2788 return status;
2791 /*******************************************************************
2792 _samr_GetGroupsForUser
2793 ********************************************************************/
2795 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
2796 struct samr_GetGroupsForUser *r)
2798 struct samu *sam_pass=NULL;
2799 DOM_SID sid;
2800 DOM_SID *sids;
2801 struct samr_RidWithAttribute dom_gid;
2802 struct samr_RidWithAttribute *gids = NULL;
2803 uint32 primary_group_rid;
2804 size_t num_groups = 0;
2805 gid_t *unix_gids;
2806 size_t i, num_gids;
2807 uint32 acc_granted;
2808 bool ret;
2809 NTSTATUS result;
2810 bool success = False;
2812 struct samr_RidWithAttributeArray *rids = NULL;
2815 * from the SID in the request:
2816 * we should send back the list of DOMAIN GROUPS
2817 * the user is a member of
2819 * and only the DOMAIN GROUPS
2820 * no ALIASES !!! neither aliases of the domain
2821 * nor aliases of the builtin SID
2823 * JFM, 12/2/2001
2826 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2828 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
2829 if (!rids) {
2830 return NT_STATUS_NO_MEMORY;
2833 /* find the policy handle. open a policy on it. */
2834 if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &sid, &acc_granted, NULL))
2835 return NT_STATUS_INVALID_HANDLE;
2837 result = access_check_samr_function(acc_granted,
2838 SA_RIGHT_USER_GET_GROUPS,
2839 "_samr_GetGroupsForUser");
2840 if (!NT_STATUS_IS_OK(result)) {
2841 return result;
2844 if (!sid_check_is_in_our_domain(&sid))
2845 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2847 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2848 return NT_STATUS_NO_MEMORY;
2851 become_root();
2852 ret = pdb_getsampwsid(sam_pass, &sid);
2853 unbecome_root();
2855 if (!ret) {
2856 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2857 sid_string_dbg(&sid)));
2858 return NT_STATUS_NO_SUCH_USER;
2861 sids = NULL;
2863 /* make both calls inside the root block */
2864 become_root();
2865 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2866 &sids, &unix_gids, &num_groups);
2867 if ( NT_STATUS_IS_OK(result) ) {
2868 success = sid_peek_check_rid(get_global_sam_sid(),
2869 pdb_get_group_sid(sam_pass),
2870 &primary_group_rid);
2872 unbecome_root();
2874 if (!NT_STATUS_IS_OK(result)) {
2875 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2876 sid_string_dbg(&sid)));
2877 return result;
2880 if ( !success ) {
2881 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2882 sid_string_dbg(pdb_get_group_sid(sam_pass)),
2883 pdb_get_username(sam_pass)));
2884 TALLOC_FREE(sam_pass);
2885 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2888 gids = NULL;
2889 num_gids = 0;
2891 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2892 SE_GROUP_ENABLED);
2893 dom_gid.rid = primary_group_rid;
2894 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2896 for (i=0; i<num_groups; i++) {
2898 if (!sid_peek_check_rid(get_global_sam_sid(),
2899 &(sids[i]), &dom_gid.rid)) {
2900 DEBUG(10, ("Found sid %s not in our domain\n",
2901 sid_string_dbg(&sids[i])));
2902 continue;
2905 if (dom_gid.rid == primary_group_rid) {
2906 /* We added the primary group directly from the
2907 * sam_account. The other SIDs are unique from
2908 * enum_group_memberships */
2909 continue;
2912 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2915 rids->count = num_gids;
2916 rids->rids = gids;
2918 *r->out.rids = rids;
2920 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2922 return result;
2925 /*******************************************************************
2926 _samr_QueryDomainInfo
2927 ********************************************************************/
2929 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
2930 struct samr_QueryDomainInfo *r)
2932 NTSTATUS status = NT_STATUS_OK;
2933 struct samr_info *info = NULL;
2934 union samr_DomainInfo *dom_info;
2935 uint32 min_pass_len,pass_hist,password_properties;
2936 time_t u_expire, u_min_age;
2937 NTTIME nt_expire, nt_min_age;
2939 time_t u_lock_duration, u_reset_time;
2940 NTTIME nt_lock_duration, nt_reset_time;
2941 uint32 lockout;
2942 time_t u_logout;
2943 NTTIME nt_logout;
2945 uint32 account_policy_temp;
2947 time_t seq_num;
2948 uint32 server_role;
2950 uint32 num_users=0, num_groups=0, num_aliases=0;
2952 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
2954 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
2955 if (!dom_info) {
2956 return NT_STATUS_NO_MEMORY;
2959 /* find the policy handle. open a policy on it. */
2960 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info)) {
2961 return NT_STATUS_INVALID_HANDLE;
2964 status = access_check_samr_function(info->acc_granted,
2965 SA_RIGHT_SAM_LOOKUP_DOMAIN,
2966 "_samr_QueryDomainInfo" );
2968 if ( !NT_STATUS_IS_OK(status) )
2969 return status;
2971 switch (r->in.level) {
2972 case 0x01:
2974 become_root();
2976 /* AS ROOT !!! */
2978 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2979 min_pass_len = account_policy_temp;
2981 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2982 pass_hist = account_policy_temp;
2984 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2985 password_properties = account_policy_temp;
2987 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2988 u_expire = account_policy_temp;
2990 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2991 u_min_age = account_policy_temp;
2993 /* !AS ROOT */
2995 unbecome_root();
2997 unix_to_nt_time_abs(&nt_expire, u_expire);
2998 unix_to_nt_time_abs(&nt_min_age, u_min_age);
3000 if (lp_check_password_script() && *lp_check_password_script()) {
3001 password_properties |= DOMAIN_PASSWORD_COMPLEX;
3004 init_samr_DomInfo1(&dom_info->info1,
3005 (uint16)min_pass_len,
3006 (uint16)pass_hist,
3007 password_properties,
3008 nt_expire,
3009 nt_min_age);
3010 break;
3011 case 0x02:
3013 become_root();
3015 /* AS ROOT !!! */
3017 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
3018 num_groups = count_sam_groups(info->disp_info);
3019 num_aliases = count_sam_aliases(info->disp_info);
3021 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
3022 u_logout = account_policy_temp;
3024 unix_to_nt_time_abs(&nt_logout, u_logout);
3026 if (!pdb_get_seq_num(&seq_num))
3027 seq_num = time(NULL);
3029 /* !AS ROOT */
3031 unbecome_root();
3033 server_role = ROLE_DOMAIN_PDC;
3034 if (lp_server_role() == ROLE_DOMAIN_BDC)
3035 server_role = ROLE_DOMAIN_BDC;
3037 init_samr_DomInfo2(&dom_info->info2,
3038 nt_logout,
3039 lp_serverstring(),
3040 lp_workgroup(),
3041 global_myname(),
3042 seq_num,
3044 server_role,
3046 num_users,
3047 num_groups,
3048 num_aliases);
3049 break;
3050 case 0x03:
3052 become_root();
3054 /* AS ROOT !!! */
3057 uint32 ul;
3058 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
3059 u_logout = (time_t)ul;
3062 /* !AS ROOT */
3064 unbecome_root();
3066 unix_to_nt_time_abs(&nt_logout, u_logout);
3068 init_samr_DomInfo3(&dom_info->info3,
3069 nt_logout);
3071 break;
3072 case 0x04:
3073 init_samr_DomInfo4(&dom_info->info4,
3074 lp_serverstring());
3075 break;
3076 case 0x05:
3077 init_samr_DomInfo5(&dom_info->info5,
3078 get_global_sam_name());
3079 break;
3080 case 0x06:
3081 /* NT returns its own name when a PDC. win2k and later
3082 * only the name of the PDC if itself is a BDC (samba4
3083 * idl) */
3084 init_samr_DomInfo6(&dom_info->info6,
3085 global_myname());
3086 break;
3087 case 0x07:
3088 server_role = ROLE_DOMAIN_PDC;
3089 if (lp_server_role() == ROLE_DOMAIN_BDC)
3090 server_role = ROLE_DOMAIN_BDC;
3092 init_samr_DomInfo7(&dom_info->info7,
3093 server_role);
3094 break;
3095 case 0x08:
3097 become_root();
3099 /* AS ROOT !!! */
3101 if (!pdb_get_seq_num(&seq_num)) {
3102 seq_num = time(NULL);
3105 /* !AS ROOT */
3107 unbecome_root();
3109 init_samr_DomInfo8(&dom_info->info8,
3110 seq_num,
3112 break;
3113 case 0x0c:
3115 become_root();
3117 /* AS ROOT !!! */
3119 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3120 u_lock_duration = account_policy_temp;
3121 if (u_lock_duration != -1) {
3122 u_lock_duration *= 60;
3125 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
3126 u_reset_time = account_policy_temp * 60;
3128 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3129 lockout = account_policy_temp;
3131 /* !AS ROOT */
3133 unbecome_root();
3135 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
3136 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
3138 init_samr_DomInfo12(&dom_info->info12,
3139 nt_lock_duration,
3140 nt_reset_time,
3141 (uint16)lockout);
3142 break;
3143 default:
3144 return NT_STATUS_INVALID_INFO_CLASS;
3147 *r->out.info = dom_info;
3149 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3151 return status;
3154 /* W2k3 seems to use the same check for all 3 objects that can be created via
3155 * SAMR, if you try to create for example "Dialup" as an alias it says
3156 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3157 * database. */
3159 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3161 enum lsa_SidType type;
3162 bool result;
3164 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3166 become_root();
3167 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3168 * whether the name already exists */
3169 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3170 NULL, NULL, NULL, &type);
3171 unbecome_root();
3173 if (!result) {
3174 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3175 return NT_STATUS_OK;
3178 DEBUG(5, ("trying to create %s, exists as %s\n",
3179 new_name, sid_type_lookup(type)));
3181 if (type == SID_NAME_DOM_GRP) {
3182 return NT_STATUS_GROUP_EXISTS;
3184 if (type == SID_NAME_ALIAS) {
3185 return NT_STATUS_ALIAS_EXISTS;
3188 /* Yes, the default is NT_STATUS_USER_EXISTS */
3189 return NT_STATUS_USER_EXISTS;
3192 /*******************************************************************
3193 _samr_CreateUser2
3194 ********************************************************************/
3196 NTSTATUS _samr_CreateUser2(pipes_struct *p,
3197 struct samr_CreateUser2 *r)
3199 const char *account = NULL;
3200 DOM_SID sid;
3201 POLICY_HND dom_pol = *r->in.domain_handle;
3202 uint32_t acb_info = r->in.acct_flags;
3203 POLICY_HND *user_pol = r->out.user_handle;
3204 struct samr_info *info = NULL;
3205 NTSTATUS nt_status;
3206 uint32 acc_granted;
3207 SEC_DESC *psd;
3208 size_t sd_size;
3209 /* check this, when giving away 'add computer to domain' privs */
3210 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3211 bool can_add_account = False;
3212 SE_PRIV se_rights;
3213 DISP_INFO *disp_info = NULL;
3215 /* Get the domain SID stored in the domain policy */
3216 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
3217 &disp_info))
3218 return NT_STATUS_INVALID_HANDLE;
3220 nt_status = access_check_samr_function(acc_granted,
3221 SA_RIGHT_DOMAIN_CREATE_USER,
3222 "_samr_CreateUser2");
3223 if (!NT_STATUS_IS_OK(nt_status)) {
3224 return nt_status;
3227 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3228 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3229 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3230 this parameter is not an account type */
3231 return NT_STATUS_INVALID_PARAMETER;
3234 account = r->in.account_name->string;
3235 if (account == NULL) {
3236 return NT_STATUS_NO_MEMORY;
3239 nt_status = can_create(p->mem_ctx, account);
3240 if (!NT_STATUS_IS_OK(nt_status)) {
3241 return nt_status;
3244 /* determine which user right we need to check based on the acb_info */
3246 if ( acb_info & ACB_WSTRUST )
3248 se_priv_copy( &se_rights, &se_machine_account );
3249 can_add_account = user_has_privileges(
3250 p->pipe_user.nt_user_token, &se_rights );
3252 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3253 account for domain trusts and changes the ACB flags later */
3254 else if ( acb_info & ACB_NORMAL &&
3255 (account[strlen(account)-1] != '$') )
3257 se_priv_copy( &se_rights, &se_add_users );
3258 can_add_account = user_has_privileges(
3259 p->pipe_user.nt_user_token, &se_rights );
3261 else /* implicit assumption of a BDC or domain trust account here
3262 * (we already check the flags earlier) */
3264 if ( lp_enable_privileges() ) {
3265 /* only Domain Admins can add a BDC or domain trust */
3266 se_priv_copy( &se_rights, &se_priv_none );
3267 can_add_account = nt_token_check_domain_rid(
3268 p->pipe_user.nt_user_token,
3269 DOMAIN_GROUP_RID_ADMINS );
3273 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3274 uidtoname(p->pipe_user.ut.uid),
3275 can_add_account ? "True":"False" ));
3277 /********** BEGIN Admin BLOCK **********/
3279 if ( can_add_account )
3280 become_root();
3282 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3283 r->out.rid);
3285 if ( can_add_account )
3286 unbecome_root();
3288 /********** END Admin BLOCK **********/
3290 /* now check for failure */
3292 if ( !NT_STATUS_IS_OK(nt_status) )
3293 return nt_status;
3295 /* Get the user's SID */
3297 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3299 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3301 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3302 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3303 se_map_generic(&des_access, &usr_generic_mapping);
3305 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3306 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
3307 &acc_granted, "_samr_CreateUser2");
3309 if ( !NT_STATUS_IS_OK(nt_status) ) {
3310 return nt_status;
3313 /* associate the user's SID with the new handle. */
3314 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
3315 return NT_STATUS_NO_MEMORY;
3318 ZERO_STRUCTP(info);
3319 info->sid = sid;
3320 info->acc_granted = acc_granted;
3322 /* get a (unique) handle. open a policy on it. */
3323 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
3324 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3327 /* After a "set" ensure we have no cached display info. */
3328 force_flush_samr_cache(info->disp_info);
3330 *r->out.access_granted = acc_granted;
3332 return NT_STATUS_OK;
3335 /*******************************************************************
3336 _samr_Connect
3337 ********************************************************************/
3339 NTSTATUS _samr_Connect(pipes_struct *p,
3340 struct samr_Connect *r)
3342 struct samr_info *info = NULL;
3343 uint32 des_access = r->in.access_mask;
3345 /* Access check */
3347 if (!pipe_access_check(p)) {
3348 DEBUG(3, ("access denied to _samr_Connect\n"));
3349 return NT_STATUS_ACCESS_DENIED;
3352 /* set up the SAMR connect_anon response */
3354 /* associate the user's SID with the new handle. */
3355 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3356 return NT_STATUS_NO_MEMORY;
3358 /* don't give away the farm but this is probably ok. The SA_RIGHT_SAM_ENUM_DOMAINS
3359 was observed from a win98 client trying to enumerate users (when configured
3360 user level access control on shares) --jerry */
3362 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3364 se_map_generic( &des_access, &sam_generic_mapping );
3365 info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_LOOKUP_DOMAIN);
3367 /* get a (unique) handle. open a policy on it. */
3368 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3369 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3371 return NT_STATUS_OK;
3374 /*******************************************************************
3375 _samr_Connect2
3376 ********************************************************************/
3378 NTSTATUS _samr_Connect2(pipes_struct *p,
3379 struct samr_Connect2 *r)
3381 struct samr_info *info = NULL;
3382 SEC_DESC *psd = NULL;
3383 uint32 acc_granted;
3384 uint32 des_access = r->in.access_mask;
3385 NTSTATUS nt_status;
3386 size_t sd_size;
3389 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3391 /* Access check */
3393 if (!pipe_access_check(p)) {
3394 DEBUG(3, ("access denied to _samr_Connect2\n"));
3395 return NT_STATUS_ACCESS_DENIED;
3398 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3400 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3401 se_map_generic(&des_access, &sam_generic_mapping);
3403 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3404 NULL, 0, des_access, &acc_granted, "_samr_Connect2");
3406 if ( !NT_STATUS_IS_OK(nt_status) )
3407 return nt_status;
3409 /* associate the user's SID and access granted with the new handle. */
3410 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3411 return NT_STATUS_NO_MEMORY;
3413 info->acc_granted = acc_granted;
3414 info->status = r->in.access_mask; /* this looks so wrong... - gd */
3416 /* get a (unique) handle. open a policy on it. */
3417 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3418 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3420 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3422 return nt_status;
3425 /*******************************************************************
3426 _samr_Connect4
3427 ********************************************************************/
3429 NTSTATUS _samr_Connect4(pipes_struct *p,
3430 struct samr_Connect4 *r)
3432 struct samr_info *info = NULL;
3433 SEC_DESC *psd = NULL;
3434 uint32 acc_granted;
3435 uint32 des_access = r->in.access_mask;
3436 NTSTATUS nt_status;
3437 size_t sd_size;
3440 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3442 /* Access check */
3444 if (!pipe_access_check(p)) {
3445 DEBUG(3, ("access denied to samr_Connect4\n"));
3446 return NT_STATUS_ACCESS_DENIED;
3449 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3451 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3452 se_map_generic(&des_access, &sam_generic_mapping);
3454 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3455 NULL, 0, des_access, &acc_granted, "_samr_Connect4");
3457 if ( !NT_STATUS_IS_OK(nt_status) )
3458 return nt_status;
3460 /* associate the user's SID and access granted with the new handle. */
3461 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3462 return NT_STATUS_NO_MEMORY;
3464 info->acc_granted = acc_granted;
3465 info->status = r->in.access_mask; /* ??? */
3467 /* get a (unique) handle. open a policy on it. */
3468 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3469 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3471 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3473 return NT_STATUS_OK;
3476 /*******************************************************************
3477 _samr_Connect5
3478 ********************************************************************/
3480 NTSTATUS _samr_Connect5(pipes_struct *p,
3481 struct samr_Connect5 *r)
3483 struct samr_info *info = NULL;
3484 SEC_DESC *psd = NULL;
3485 uint32 acc_granted;
3486 uint32 des_access = r->in.access_mask;
3487 NTSTATUS nt_status;
3488 size_t sd_size;
3489 struct samr_ConnectInfo1 info1;
3491 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3493 /* Access check */
3495 if (!pipe_access_check(p)) {
3496 DEBUG(3, ("access denied to samr_Connect5\n"));
3497 return NT_STATUS_ACCESS_DENIED;
3500 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3502 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3503 se_map_generic(&des_access, &sam_generic_mapping);
3505 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3506 NULL, 0, des_access, &acc_granted, "_samr_Connect5");
3508 if ( !NT_STATUS_IS_OK(nt_status) )
3509 return nt_status;
3511 /* associate the user's SID and access granted with the new handle. */
3512 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3513 return NT_STATUS_NO_MEMORY;
3515 info->acc_granted = acc_granted;
3516 info->status = r->in.access_mask; /* ??? */
3518 /* get a (unique) handle. open a policy on it. */
3519 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3520 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3522 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3524 info1.client_version = SAMR_CONNECT_AFTER_W2K;
3525 info1.unknown2 = 0;
3527 *r->out.level_out = 1;
3528 r->out.info_out->info1 = info1;
3530 return NT_STATUS_OK;
3533 /**********************************************************************
3534 _samr_LookupDomain
3535 **********************************************************************/
3537 NTSTATUS _samr_LookupDomain(pipes_struct *p,
3538 struct samr_LookupDomain *r)
3540 NTSTATUS status = NT_STATUS_OK;
3541 struct samr_info *info;
3542 const char *domain_name;
3543 DOM_SID *sid = NULL;
3545 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3546 return NT_STATUS_INVALID_HANDLE;
3548 /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
3549 Reverted that change so we will work with RAS servers again */
3551 status = access_check_samr_function(info->acc_granted,
3552 SA_RIGHT_SAM_LOOKUP_DOMAIN,
3553 "_samr_LookupDomain");
3554 if (!NT_STATUS_IS_OK(status)) {
3555 return status;
3558 domain_name = r->in.domain_name->string;
3560 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
3561 if (!sid) {
3562 return NT_STATUS_NO_MEMORY;
3565 if (strequal(domain_name, builtin_domain_name())) {
3566 sid_copy(sid, &global_sid_Builtin);
3567 } else {
3568 if (!secrets_fetch_domain_sid(domain_name, sid)) {
3569 status = NT_STATUS_NO_SUCH_DOMAIN;
3573 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
3574 sid_string_dbg(sid)));
3576 *r->out.sid = sid;
3578 return status;
3581 /**********************************************************************
3582 _samr_EnumDomains
3583 **********************************************************************/
3585 NTSTATUS _samr_EnumDomains(pipes_struct *p,
3586 struct samr_EnumDomains *r)
3588 NTSTATUS status;
3589 struct samr_info *info;
3590 uint32_t num_entries = 2;
3591 struct samr_SamEntry *entry_array = NULL;
3592 struct samr_SamArray *sam;
3594 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3595 return NT_STATUS_INVALID_HANDLE;
3597 status = access_check_samr_function(info->acc_granted,
3598 SA_RIGHT_SAM_ENUM_DOMAINS,
3599 "_samr_EnumDomains");
3600 if (!NT_STATUS_IS_OK(status)) {
3601 return status;
3604 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
3605 if (!sam) {
3606 return NT_STATUS_NO_MEMORY;
3609 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
3610 struct samr_SamEntry,
3611 num_entries);
3612 if (!entry_array) {
3613 return NT_STATUS_NO_MEMORY;
3616 entry_array[0].idx = 0;
3617 init_lsa_String(&entry_array[0].name, get_global_sam_name());
3619 entry_array[1].idx = 1;
3620 init_lsa_String(&entry_array[1].name, "Builtin");
3622 sam->count = num_entries;
3623 sam->entries = entry_array;
3625 *r->out.sam = sam;
3626 *r->out.num_entries = num_entries;
3628 return status;
3631 /*******************************************************************
3632 _samr_OpenAlias
3633 ********************************************************************/
3635 NTSTATUS _samr_OpenAlias(pipes_struct *p,
3636 struct samr_OpenAlias *r)
3638 DOM_SID sid;
3639 POLICY_HND domain_pol = *r->in.domain_handle;
3640 uint32 alias_rid = r->in.rid;
3641 POLICY_HND *alias_pol = r->out.alias_handle;
3642 struct samr_info *info = NULL;
3643 SEC_DESC *psd = NULL;
3644 uint32 acc_granted;
3645 uint32 des_access = r->in.access_mask;
3646 size_t sd_size;
3647 NTSTATUS status;
3648 SE_PRIV se_rights;
3650 /* find the domain policy and get the SID / access bits stored in the domain policy */
3652 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
3653 return NT_STATUS_INVALID_HANDLE;
3655 status = access_check_samr_function(acc_granted,
3656 SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
3657 "_samr_OpenAlias");
3659 if ( !NT_STATUS_IS_OK(status) )
3660 return status;
3662 /* append the alias' RID to it */
3664 if (!sid_append_rid(&sid, alias_rid))
3665 return NT_STATUS_NO_SUCH_ALIAS;
3667 /*check if access can be granted as requested by client. */
3669 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3671 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
3672 se_map_generic(&des_access,&ali_generic_mapping);
3674 se_priv_copy( &se_rights, &se_add_users );
3677 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3678 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
3679 &acc_granted, "_samr_OpenAlias");
3681 if ( !NT_STATUS_IS_OK(status) )
3682 return status;
3685 /* Check we actually have the requested alias */
3686 enum lsa_SidType type;
3687 bool result;
3688 gid_t gid;
3690 become_root();
3691 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
3692 unbecome_root();
3694 if (!result || (type != SID_NAME_ALIAS)) {
3695 return NT_STATUS_NO_SUCH_ALIAS;
3698 /* make sure there is a mapping */
3700 if ( !sid_to_gid( &sid, &gid ) ) {
3701 return NT_STATUS_NO_SUCH_ALIAS;
3706 /* associate the alias SID with the new handle. */
3707 if ((info = get_samr_info_by_sid(&sid)) == NULL)
3708 return NT_STATUS_NO_MEMORY;
3710 info->acc_granted = acc_granted;
3712 /* get a (unique) handle. open a policy on it. */
3713 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
3714 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3716 return NT_STATUS_OK;
3719 /*******************************************************************
3720 set_user_info_7
3721 ********************************************************************/
3723 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
3724 struct samr_UserInfo7 *id7,
3725 struct samu *pwd)
3727 NTSTATUS rc;
3729 if (id7 == NULL) {
3730 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3731 TALLOC_FREE(pwd);
3732 return NT_STATUS_ACCESS_DENIED;
3735 if (!id7->account_name.string) {
3736 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3737 TALLOC_FREE(pwd);
3738 return NT_STATUS_ACCESS_DENIED;
3741 /* check to see if the new username already exists. Note: we can't
3742 reliably lock all backends, so there is potentially the
3743 possibility that a user can be created in between this check and
3744 the rename. The rename should fail, but may not get the
3745 exact same failure status code. I think this is small enough
3746 of a window for this type of operation and the results are
3747 simply that the rename fails with a slightly different status
3748 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3750 rc = can_create(mem_ctx, id7->account_name.string);
3751 if (!NT_STATUS_IS_OK(rc)) {
3752 return rc;
3755 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
3757 TALLOC_FREE(pwd);
3758 return rc;
3761 /*******************************************************************
3762 set_user_info_16
3763 ********************************************************************/
3765 static bool set_user_info_16(struct samr_UserInfo16 *id16,
3766 struct samu *pwd)
3768 if (id16 == NULL) {
3769 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3770 TALLOC_FREE(pwd);
3771 return False;
3774 /* FIX ME: check if the value is really changed --metze */
3775 if (!pdb_set_acct_ctrl(pwd, id16->acct_flags, PDB_CHANGED)) {
3776 TALLOC_FREE(pwd);
3777 return False;
3780 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3781 TALLOC_FREE(pwd);
3782 return False;
3785 TALLOC_FREE(pwd);
3787 return True;
3790 /*******************************************************************
3791 set_user_info_18
3792 ********************************************************************/
3794 static bool set_user_info_18(struct samr_UserInfo18 *id18,
3795 struct samu *pwd)
3797 if (id18 == NULL) {
3798 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3799 TALLOC_FREE(pwd);
3800 return False;
3803 if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd.hash, PDB_CHANGED)) {
3804 TALLOC_FREE(pwd);
3805 return False;
3807 if (!pdb_set_nt_passwd (pwd, id18->nt_pwd.hash, PDB_CHANGED)) {
3808 TALLOC_FREE(pwd);
3809 return False;
3811 if (!pdb_set_pass_last_set_time (pwd, time(NULL), PDB_CHANGED)) {
3812 TALLOC_FREE(pwd);
3813 return False;
3816 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3817 TALLOC_FREE(pwd);
3818 return False;
3821 TALLOC_FREE(pwd);
3822 return True;
3825 /*******************************************************************
3826 set_user_info_20
3827 ********************************************************************/
3829 static bool set_user_info_20(struct samr_UserInfo20 *id20,
3830 struct samu *pwd)
3832 if (id20 == NULL) {
3833 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3834 return False;
3837 copy_id20_to_sam_passwd(pwd, id20);
3839 /* write the change out */
3840 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3841 TALLOC_FREE(pwd);
3842 return False;
3845 TALLOC_FREE(pwd);
3847 return True;
3850 /*******************************************************************
3851 set_user_info_21
3852 ********************************************************************/
3854 static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
3855 struct samr_UserInfo21 *id21,
3856 struct samu *pwd)
3858 NTSTATUS status;
3860 if (id21 == NULL) {
3861 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3862 return NT_STATUS_INVALID_PARAMETER;
3865 /* we need to separately check for an account rename first */
3867 if (id21->account_name.string &&
3868 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
3871 /* check to see if the new username already exists. Note: we can't
3872 reliably lock all backends, so there is potentially the
3873 possibility that a user can be created in between this check and
3874 the rename. The rename should fail, but may not get the
3875 exact same failure status code. I think this is small enough
3876 of a window for this type of operation and the results are
3877 simply that the rename fails with a slightly different status
3878 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3880 status = can_create(mem_ctx, id21->account_name.string);
3881 if (!NT_STATUS_IS_OK(status)) {
3882 return status;
3885 status = pdb_rename_sam_account(pwd, id21->account_name.string);
3887 if (!NT_STATUS_IS_OK(status)) {
3888 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3889 nt_errstr(status)));
3890 TALLOC_FREE(pwd);
3891 return status;
3894 /* set the new username so that later
3895 functions can work on the new account */
3896 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
3899 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
3902 * The funny part about the previous two calls is
3903 * that pwd still has the password hashes from the
3904 * passdb entry. These have not been updated from
3905 * id21. I don't know if they need to be set. --jerry
3908 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3909 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3910 if ( !NT_STATUS_IS_OK(status) ) {
3911 return status;
3915 /* Don't worry about writing out the user account since the
3916 primary group SID is generated solely from the user's Unix
3917 primary group. */
3919 /* write the change out */
3920 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3921 TALLOC_FREE(pwd);
3922 return status;
3925 TALLOC_FREE(pwd);
3927 return NT_STATUS_OK;
3930 /*******************************************************************
3931 set_user_info_23
3932 ********************************************************************/
3934 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
3935 struct samr_UserInfo23 *id23,
3936 struct samu *pwd)
3938 char *plaintext_buf = NULL;
3939 uint32 len = 0;
3940 uint16 acct_ctrl;
3941 NTSTATUS status;
3943 if (id23 == NULL) {
3944 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3945 return NT_STATUS_INVALID_PARAMETER;
3948 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3949 pdb_get_username(pwd)));
3951 acct_ctrl = pdb_get_acct_ctrl(pwd);
3953 if (!decode_pw_buffer(mem_ctx,
3954 id23->password.data,
3955 &plaintext_buf,
3956 &len,
3957 STR_UNICODE)) {
3958 TALLOC_FREE(pwd);
3959 return NT_STATUS_INVALID_PARAMETER;
3962 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3963 TALLOC_FREE(pwd);
3964 return NT_STATUS_ACCESS_DENIED;
3967 copy_id23_to_sam_passwd(pwd, id23);
3969 /* if it's a trust account, don't update /etc/passwd */
3970 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3971 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3972 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3973 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
3974 } else {
3975 /* update the UNIX password */
3976 if (lp_unix_password_sync() ) {
3977 struct passwd *passwd;
3978 if (pdb_get_username(pwd) == NULL) {
3979 DEBUG(1, ("chgpasswd: User without name???\n"));
3980 TALLOC_FREE(pwd);
3981 return NT_STATUS_ACCESS_DENIED;
3984 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
3985 if (passwd == NULL) {
3986 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3989 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3990 TALLOC_FREE(pwd);
3991 return NT_STATUS_ACCESS_DENIED;
3993 TALLOC_FREE(passwd);
3997 memset(plaintext_buf, '\0', strlen(plaintext_buf));
3999 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
4000 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
4001 pwd)))) {
4002 TALLOC_FREE(pwd);
4003 return status;
4006 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4007 TALLOC_FREE(pwd);
4008 return status;
4011 TALLOC_FREE(pwd);
4013 return NT_STATUS_OK;
4016 /*******************************************************************
4017 set_user_info_pw
4018 ********************************************************************/
4020 static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
4021 int level)
4023 uint32 len = 0;
4024 char *plaintext_buf = NULL;
4025 uint32 acct_ctrl;
4026 time_t last_set_time;
4027 enum pdb_value_state last_set_state;
4029 DEBUG(5, ("Attempting administrator password change for user %s\n",
4030 pdb_get_username(pwd)));
4032 acct_ctrl = pdb_get_acct_ctrl(pwd);
4033 /* we need to know if it's expired, because this is an admin change, not a
4034 user change, so it's still expired when we're done */
4035 last_set_state = pdb_get_init_flags(pwd, PDB_PASSLASTSET);
4036 last_set_time = pdb_get_pass_last_set_time(pwd);
4038 if (!decode_pw_buffer(talloc_tos(),
4039 pass,
4040 &plaintext_buf,
4041 &len,
4042 STR_UNICODE)) {
4043 TALLOC_FREE(pwd);
4044 return False;
4047 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4048 TALLOC_FREE(pwd);
4049 return False;
4052 /* if it's a trust account, don't update /etc/passwd */
4053 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4054 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4055 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4056 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4057 } else {
4058 /* update the UNIX password */
4059 if (lp_unix_password_sync()) {
4060 struct passwd *passwd;
4062 if (pdb_get_username(pwd) == NULL) {
4063 DEBUG(1, ("chgpasswd: User without name???\n"));
4064 TALLOC_FREE(pwd);
4065 return False;
4068 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4069 if (passwd == NULL) {
4070 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4073 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4074 TALLOC_FREE(pwd);
4075 return False;
4077 TALLOC_FREE(passwd);
4081 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4084 * A level 25 change does reset the pwdlastset field, a level 24
4085 * change does not. I know this is probably not the full story, but
4086 * it is needed to make XP join LDAP correctly, without it the later
4087 * auth2 check can fail with PWD_MUST_CHANGE.
4089 if (level != 25) {
4091 * restore last set time as this is an admin change, not a
4092 * user pw change
4094 pdb_set_pass_last_set_time (pwd, last_set_time,
4095 last_set_state);
4098 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4100 /* update the SAMBA password */
4101 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
4102 TALLOC_FREE(pwd);
4103 return False;
4106 TALLOC_FREE(pwd);
4108 return True;
4111 /*******************************************************************
4112 set_user_info_25
4113 ********************************************************************/
4115 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4116 struct samr_UserInfo25 *id25,
4117 struct samu *pwd)
4119 NTSTATUS status;
4121 if (id25 == NULL) {
4122 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4123 return NT_STATUS_INVALID_PARAMETER;
4126 copy_id25_to_sam_passwd(pwd, id25);
4128 /* write the change out */
4129 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4130 TALLOC_FREE(pwd);
4131 return status;
4135 * We need to "pdb_update_sam_account" before the unix primary group
4136 * is set, because the idealx scripts would also change the
4137 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4138 * the delete explicit / add explicit, which would then fail to find
4139 * the previous primaryGroupSid value.
4142 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4143 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4144 if ( !NT_STATUS_IS_OK(status) ) {
4145 return status;
4149 /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
4150 * hereafter! */
4152 return NT_STATUS_OK;
4155 /*******************************************************************
4156 samr_SetUserInfo
4157 ********************************************************************/
4159 NTSTATUS _samr_SetUserInfo(pipes_struct *p,
4160 struct samr_SetUserInfo *r)
4162 NTSTATUS status;
4163 struct samu *pwd = NULL;
4164 DOM_SID sid;
4165 POLICY_HND *pol = r->in.user_handle;
4166 union samr_UserInfo *info = r->in.info;
4167 uint16_t switch_value = r->in.level;
4168 uint32_t acc_granted;
4169 uint32_t acc_required;
4170 bool ret;
4171 bool has_enough_rights = False;
4172 uint32_t acb_info;
4173 DISP_INFO *disp_info = NULL;
4175 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
4177 /* find the policy handle. open a policy on it. */
4178 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info)) {
4179 return NT_STATUS_INVALID_HANDLE;
4182 /* This is tricky. A WinXP domain join sets
4183 (SA_RIGHT_USER_SET_PASSWORD|SA_RIGHT_USER_SET_ATTRIBUTES|SA_RIGHT_USER_ACCT_FLAGS_EXPIRY)
4184 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
4185 standard Win32 API calls just ask for SA_RIGHT_USER_SET_PASSWORD in the SamrOpenUser().
4186 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
4187 we'll use the set from the WinXP join as the basis. */
4189 switch (switch_value) {
4190 case 18:
4191 case 24:
4192 case 25:
4193 case 26:
4194 acc_required = SA_RIGHT_USER_SET_PASSWORD;
4195 break;
4196 default:
4197 acc_required = SA_RIGHT_USER_SET_PASSWORD |
4198 SA_RIGHT_USER_SET_ATTRIBUTES |
4199 SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
4200 break;
4203 status = access_check_samr_function(acc_granted,
4204 acc_required,
4205 "_samr_SetUserInfo");
4206 if (!NT_STATUS_IS_OK(status)) {
4207 return status;
4210 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
4211 sid_string_dbg(&sid), switch_value));
4213 if (info == NULL) {
4214 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
4215 return NT_STATUS_INVALID_INFO_CLASS;
4218 if (!(pwd = samu_new(NULL))) {
4219 return NT_STATUS_NO_MEMORY;
4222 become_root();
4223 ret = pdb_getsampwsid(pwd, &sid);
4224 unbecome_root();
4226 if (!ret) {
4227 TALLOC_FREE(pwd);
4228 return NT_STATUS_NO_SUCH_USER;
4231 /* deal with machine password changes differently from userinfo changes */
4232 /* check to see if we have the sufficient rights */
4234 acb_info = pdb_get_acct_ctrl(pwd);
4235 if (acb_info & ACB_WSTRUST)
4236 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4237 &se_machine_account);
4238 else if (acb_info & ACB_NORMAL)
4239 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4240 &se_add_users);
4241 else if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
4242 if (lp_enable_privileges()) {
4243 has_enough_rights = nt_token_check_domain_rid(p->pipe_user.nt_user_token,
4244 DOMAIN_GROUP_RID_ADMINS);
4248 DEBUG(5, ("_samr_SetUserInfo: %s does%s possess sufficient rights\n",
4249 uidtoname(p->pipe_user.ut.uid),
4250 has_enough_rights ? "" : " not"));
4252 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
4254 if (has_enough_rights) {
4255 become_root();
4258 /* ok! user info levels (lots: see MSDEV help), off we go... */
4260 switch (switch_value) {
4262 case 7:
4263 status = set_user_info_7(p->mem_ctx,
4264 &info->info7, pwd);
4265 break;
4267 case 16:
4268 if (!set_user_info_16(&info->info16, pwd)) {
4269 status = NT_STATUS_ACCESS_DENIED;
4271 break;
4273 case 18:
4274 /* Used by AS/U JRA. */
4275 if (!set_user_info_18(&info->info18, pwd)) {
4276 status = NT_STATUS_ACCESS_DENIED;
4278 break;
4280 case 20:
4281 if (!set_user_info_20(&info->info20, pwd)) {
4282 status = NT_STATUS_ACCESS_DENIED;
4284 break;
4286 case 21:
4287 status = set_user_info_21(p->mem_ctx,
4288 &info->info21, pwd);
4289 break;
4291 case 23:
4292 if (!p->session_key.length) {
4293 status = NT_STATUS_NO_USER_SESSION_KEY;
4295 SamOEMhashBlob(info->info23.password.data, 516,
4296 &p->session_key);
4298 dump_data(100, info->info23.password.data, 516);
4300 status = set_user_info_23(p->mem_ctx,
4301 &info->info23, pwd);
4302 break;
4304 case 24:
4305 if (!p->session_key.length) {
4306 status = NT_STATUS_NO_USER_SESSION_KEY;
4308 SamOEMhashBlob(info->info24.password.data,
4309 516,
4310 &p->session_key);
4312 dump_data(100, info->info24.password.data, 516);
4314 if (!set_user_info_pw(info->info24.password.data, pwd,
4315 switch_value)) {
4316 status = NT_STATUS_ACCESS_DENIED;
4318 break;
4320 case 25:
4321 if (!p->session_key.length) {
4322 status = NT_STATUS_NO_USER_SESSION_KEY;
4324 encode_or_decode_arc4_passwd_buffer(info->info25.password.data,
4325 &p->session_key);
4327 dump_data(100, info->info25.password.data, 532);
4329 status = set_user_info_25(p->mem_ctx,
4330 &info->info25, pwd);
4331 if (!NT_STATUS_IS_OK(status)) {
4332 goto done;
4334 if (!set_user_info_pw(info->info25.password.data, pwd,
4335 switch_value)) {
4336 status = NT_STATUS_ACCESS_DENIED;
4338 break;
4340 case 26:
4341 if (!p->session_key.length) {
4342 status = NT_STATUS_NO_USER_SESSION_KEY;
4344 encode_or_decode_arc4_passwd_buffer(info->info26.password.data,
4345 &p->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 SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM,
4415 "_samr_GetAliasMembership");
4416 ntstatus2 = access_check_samr_function(info->acc_granted,
4417 SA_RIGHT_DOMAIN_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 SA_RIGHT_ALIAS_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 SA_RIGHT_GROUP_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 SA_RIGHT_ALIAS_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 SA_RIGHT_ALIAS_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 SA_RIGHT_GROUP_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 SA_RIGHT_GROUP_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 SA_RIGHT_DOMAIN_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 SA_RIGHT_DOMAIN_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 SA_RIGHT_GROUP_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 SA_RIGHT_GROUP_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 SA_RIGHT_ALIAS_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 SA_RIGHT_DOMAIN_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 SA_RIGHT_DOMAIN_SET_INFO_1
5698 * set we are ok. */
5700 result = access_check_samr_function(info->acc_granted,
5701 SA_RIGHT_DOMAIN_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 SA_RIGHT_DOMAIN_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;